OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
main.cpp
Go to the documentation of this file.
1 //
2 // Created by jstark on 2020-05-18.
3 //
64 
66 // Include level-set-method related header files
67 #include "util/PathsAndFiles.hpp"
70 #include "Draw/DrawSphere.hpp"
72 
85 int main(int argc, char* argv[])
87 {
88  typedef double phi_type;
89  // initialize library
90  openfpm_init(&argc, &argv);
91  // Set current working directory, define output paths and create folders where output will be saved
92  std::string cwd = get_cwd();
93  const std::string path_output = cwd + "/output_sphere";
94  create_directory_if_not_exist(path_output);
96 
111  // grid dimension
114  constexpr size_t grid_dim = 3;
115  // Some indices for the grid / grid properties
116  const size_t x = 0;
117  const size_t y = 1;
118  const size_t z = 2;
119 
120  const size_t Phi_0_grid = 0;
121  const size_t Phi_SDF_grid = 1;
124 
140  // Here we create a 3D grid that stores two properties:
142  // Prop1: store the initial Phi;
143  // Prop2: here the re-initialized Phi (signed distance function) will be written to in the re-distancing step
144  const size_t sz[grid_dim] = {128, 128, 128};
145  Box<grid_dim, phi_type> box({0.0, 0.0, 0.0}, {10.0, 10.0, 10.0});
146  Ghost<grid_dim, long int> ghost(0);
147  typedef aggregate<phi_type, phi_type> props;
148  typedef grid_dist_id<grid_dim, phi_type, props > grid_in_type;
149  grid_in_type g_dist(sz, box, ghost);
150  g_dist.setPropNames({"Phi_0", "Phi_SDF"});
152 
153 
172  // Now we initialize the grid with a filled sphere. Outside the sphere, the value of Phi_0 will be -1, inside +1.
174  phi_type radius = 1.0; // Radius of the sphere
175  init_grid_with_sphere<Phi_0_grid>(g_dist, radius, 5, 5, 5); // Initialize sphere onto grid, centered at (5, 5, 5)
176 
177  g_dist.write(path_output + "/grid_initial_sphere_preRedistancing_radius" + std::to_string((int)radius) , FORMAT_BINARY); // Save the sphere as vtk file
179 
180 
181 
217  // Now we want to convert the initial Phi into a signed distance function (SDF) with magnitude of gradient = 1.
220  // For the initial re-distancing we use the Sussman method. First of all, we can set some redistancing options.
221  Redist_options<phi_type> redist_options;
222  redist_options.min_iter = 1e3;
223  redist_options.max_iter = 1e4;
224 
225  redist_options.convTolChange.value = 1e-7;
226  redist_options.convTolChange.check = true;
227  redist_options.convTolResidual.value = 1e-6; // is ignored if convTolResidual.check = false;
228  redist_options.convTolResidual.check = false;
229 
230  redist_options.interval_check_convergence = 1e3;
231  redist_options.width_NB_in_grid_points = 10;
232  redist_options.print_current_iterChangeResidual = true;
233  redist_options.print_steadyState_iter = true;
234  redist_options.save_temp_grid = true;
236 
256  RedistancingSussman<grid_in_type, phi_type> redist_obj(g_dist, redist_options); // Instantiation of
258  // Sussman-redistancing class
259  // std::cout << "dt = " << redist_obj.get_time_step() << std::endl;
260  // Run the redistancing. in the <> brackets provide property-index where 1.) your initial Phi is stored and 2.)
261  // where the resulting SDF should be written to.
262  redist_obj.run_redistancing<Phi_0_grid, Phi_SDF_grid>();
263 
264  g_dist.write(path_output + "/grid_sphere_postRedistancing", FORMAT_BINARY); // Save the result as vtk file
265  g_dist.save(path_output + "/grid_sphere_postRedistancing" + ".bin"); // Save the result as hdf5 file that can be
266  // reloaded onto an openFPM grid
269 
283  // Get narrow band: Place particles on interface (narrow band width e.g. 2 grid points on each side of the interface)
285  size_t bc[grid_dim] = {NON_PERIODIC, NON_PERIODIC, NON_PERIODIC};
286  // Create an empty vector to which narrow-band particles will be added. You can choose, how many properties you want.
287  // Minimum is 1 property, to which the Phi_SDF can be written
288  // In this example we chose 3 properties. The 1st for the Phi_SDF, the 2nd for the gradient of phi and the 3rd for
289  // the magnitude of the gradient
290  typedef aggregate<phi_type, Point<grid_dim, phi_type>, phi_type> props_nb;
292  Ghost<grid_dim, phi_type> ghost_vd(0);
293  vd_type vd_narrow_band(0, box, bc, ghost_vd);
294  vd_narrow_band.setPropNames({"Phi_SDF", "Phi_grad", "Phi_magnOfGrad"});
296 
315  size_t thickness_of_narrowBand_in_grid_points = 6;
317  NarrowBand<grid_in_type, phi_type> narrowBand(g_dist, redist_options.width_NB_in_grid_points); // Instantiation of
318  // NarrowBand class
319 
321 
333  // Some indices
336  const size_t Phi_SDF_vd = 0;
337  const size_t Phi_grad_vd = 1;
338  const size_t Phi_magnOfGrad_vd = 2;
340 
363  // Get the narrow band. You can decide, if you only want the Phi_SDF saved to your particles or
364  // if you also want the gradients or gradients and magnitude of gradient.
365  // The function will know depending on how many property-indices you provide in the <> brackets.
366  // First property-index must always be the index of the SDF on the grid!
367  // E.g.: The following line would only write only the Phi_SDF from the grid to your narrow-band particles
368  // narrowBand.get_narrow_band<Phi_SDF_grid, Phi_SDF_vd>(g_dist, vd_narrow_band);
369  // Whereas this will give you the gradients and magnOfGrad of Phi as well:
371  narrowBand.get_narrow_band<Phi_SDF_grid, Phi_SDF_vd, Phi_grad_vd, Phi_magnOfGrad_vd>(g_dist, vd_narrow_band);
372 
373  vd_narrow_band.write(path_output + "/vd_narrow_band_sphere", FORMAT_BINARY); // Save particles as vtk file
374  vd_narrow_band.save(path_output + "/vd_narrow_band_sphere.bin"); // Save particles as hdf5 file -> can be reloaded as particles
376 
386  openfpm_finalize(); // Finalize openFPM library
388  return 0;
389 }
391 
static void create_directory_if_not_exist(std::string path)
Creates a directory if not already existent.
Class for getting the narrow band around the interface.
Definition: NarrowBand.hpp:42
Class for reinitializing a level-set function into a signed distance function using Sussman redistanc...
Structure to bundle options for redistancing.
static std::string get_cwd()
Gets the current working directory and returns path as string.
Definition: Ghost.hpp:39
Class for reinitializing a level-set function into a signed distance function using Sussman redistanc...
This is a distributed grid.
This class represent an N-dimensional box.
Definition: Box.hpp:60
Distributed vector.
Header file containing functions for creating files and folders.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:214
Class for getting the narrow band around the interface.