OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
redistancingSussman_fast_unit_test.cpp
1 //
2 // Created by jstark on 04.01.22.
3 //
4 #define BOOST_TEST_DYN_LINK
5 //#define BOOST_TEST_MAIN // in only one cpp file
6 #include <boost/test/unit_test.hpp>
7 
8 // Include redistancing files
9 #include "util/PathsAndFiles.hpp"
12 // Include header files for testing
13 #include "Draw/DrawSphere.hpp"
14 #include "l_norms/LNorms.hpp"
16 
17 BOOST_AUTO_TEST_SUITE(RedistancingSussmanFastTestSuite)
18 
19  BOOST_AUTO_TEST_CASE(RedistancingSussman_unit_sphere_fast_double_test)
20  {
21  typedef double phi_type;
22  typedef double space_type;
23  const phi_type EPSILON = std::numeric_limits<phi_type>::epsilon();
24  const size_t grid_dim = 3;
25  // some indices
26  const size_t x = 0;
27  const size_t y = 1;
28  const size_t z = 2;
29 
30  const size_t Phi_0_grid = 0;
31  const size_t SDF_sussman_grid = 1;
32  const size_t SDF_exact_grid = 2;
33  const size_t Error_grid = 3;
34 
35  size_t N = 32;
36  const size_t sz[grid_dim] = {N, N, N};
37  const space_type radius = 1.0;
38  const space_type box_lower = -2.0;
39  const space_type box_upper = 2.0;
40  Box<grid_dim, space_type> box({box_lower, box_lower, box_lower}, {box_upper, box_upper, box_upper});
43  typedef grid_dist_id<grid_dim, space_type, props > grid_in_type;
44  grid_in_type g_dist(sz, box, ghost);
45  g_dist.setPropNames({"Phi_0", "SDF_sussman", "SDF_exact", "Relative error"});
46 
47  const space_type center[grid_dim] = {(box_upper+box_lower)/(space_type)2,
48  (box_upper+box_lower)/(space_type)2,
49  (box_upper+box_lower)/(space_type)2};
50 
51  init_grid_with_sphere<Phi_0_grid>(g_dist, radius, center[x], center[y], center[z]); // Initialize sphere onto grid
52 
53  Redist_options<phi_type> redist_options;
54  redist_options.min_iter = 1e3;
55  redist_options.max_iter = 1e3;
56 
57  redist_options.convTolChange.check = false;
58  redist_options.convTolResidual.check = false;
59 
60  redist_options.interval_check_convergence = 1e3;
61  redist_options.width_NB_in_grid_points = 8;
62  redist_options.print_current_iterChangeResidual = true;
63  redist_options.print_steadyState_iter = true;
64 
65  RedistancingSussman<grid_in_type, phi_type> redist_obj(g_dist, redist_options); // Instantiation of
66 
67  std::cout << "Automatically found timestep is " << redist_obj.get_time_step() << std::endl;
68  // Run the redistancing. in the <> brackets provide property-index where 1.) your initial Phi is stored and 2.) where the resulting SDF should be written to.
69  redist_obj.run_redistancing<Phi_0_grid, SDF_sussman_grid>();
70 
71  // Compute exact signed distance function at each grid point
72  init_analytic_sdf_sphere<SDF_exact_grid>(g_dist, radius, center[x], center[y], center[z]);
73 
74  // Compute the absolute error between analytical and numerical solution at each grid point
75  get_absolute_error<SDF_sussman_grid, SDF_exact_grid, Error_grid>(g_dist);
77  // Get narrow band: Place particles on interface (narrow band width e.g. 4 grid points on each side of the
78  // interface)
79  size_t bc[grid_dim] = {NON_PERIODIC, NON_PERIODIC, NON_PERIODIC};
80  typedef aggregate<phi_type> props_nb;
82  Ghost<grid_dim, space_type> ghost_vd(0);
83  vd_type vd_narrow_band(0, box, bc, ghost_vd);
84  vd_narrow_band.setPropNames({"error"});
85  size_t narrow_band_width = 8;
86  NarrowBand<grid_in_type, phi_type> narrowBand(g_dist, narrow_band_width); // Instantiation of NarrowBand class
87  const size_t Error_vd = 0;
88  narrowBand.get_narrow_band_copy_specific_property<SDF_sussman_grid, Error_grid, Error_vd>(g_dist,
89  vd_narrow_band);
90  // Compute the L_2- and L_infinity-norm and save to file
91  LNorms<phi_type> lNorms_vd;
92  lNorms_vd.get_l_norms_vector<Error_vd>(vd_narrow_band);
93  std::cout << lNorms_vd.l2 << ", " << lNorms_vd.linf << std::endl;
94 
95 // N = 32, precision = double, 1e3 iterations
96 // Automatically found timestep is 0.00277489
97 // 1000,0.000029667373926,0.203437014506586,6032
98 // 0.0428668, 0.0763498
99 
100  BOOST_CHECK(lNorms_vd.l2 < 0.0428669);
101  BOOST_CHECK(lNorms_vd.linf < 0.0763499);
102  }
103 
104  BOOST_AUTO_TEST_CASE(RedistancingSussman_unit_sphere_fast_float_test)
105  {
106  typedef float phi_type;
107  typedef float space_type;
108  const phi_type EPSILON = std::numeric_limits<phi_type>::epsilon();
109  const size_t grid_dim = 3;
110  // some indices
111  const size_t x = 0;
112  const size_t y = 1;
113  const size_t z = 2;
114 
115  const size_t Phi_0_grid = 0;
116  const size_t SDF_sussman_grid = 1;
117  const size_t SDF_exact_grid = 2;
118  const size_t Error_grid = 3;
119 
120  size_t N = 32;
121  const size_t sz[grid_dim] = {N, N, N};
122  const space_type radius = 1.0;
123  const space_type box_lower = -2.0;
124  const space_type box_upper = 2.0;
125  Box<grid_dim, space_type> box({box_lower, box_lower, box_lower}, {box_upper, box_upper, box_upper});
126  Ghost<grid_dim, long int> ghost(0);
128  typedef grid_dist_id<grid_dim, space_type, props > grid_in_type;
129  grid_in_type g_dist(sz, box, ghost);
130  g_dist.setPropNames({"Phi_0", "SDF_sussman", "SDF_exact", "Relative error"});
131 
132  const space_type center[grid_dim] = {(box_upper+box_lower)/(space_type)2,
133  (box_upper+box_lower)/(space_type)2,
134  (box_upper+box_lower)/(space_type)2};
135 
136  init_grid_with_sphere<Phi_0_grid>(g_dist, radius, center[x], center[y], center[z]); // Initialize sphere onto grid
137 
138  Redist_options<phi_type> redist_options;
139  redist_options.min_iter = 1e3;
140  redist_options.max_iter = 1e3;
141 
142  redist_options.convTolChange.check = false;
143  redist_options.convTolResidual.check = false;
144 
145  redist_options.interval_check_convergence = 1e3;
146  redist_options.width_NB_in_grid_points = 8;
147  redist_options.print_current_iterChangeResidual = true;
148  redist_options.print_steadyState_iter = true;
149 
150  redist_options.save_temp_grid = false;
151 
152  RedistancingSussman<grid_in_type, phi_type> redist_obj(g_dist, redist_options); // Instantiation of
153 
154  std::cout << "Automatically found timestep is " << redist_obj.get_time_step() << std::endl;
155  // Run the redistancing. in the <> brackets provide property-index where 1.) your initial Phi is stored and 2.) where the resulting SDF should be written to.
156  redist_obj.run_redistancing<Phi_0_grid, SDF_sussman_grid>();
157 
158  // Compute exact signed distance function at each grid point
159  init_analytic_sdf_sphere<SDF_exact_grid>(g_dist, radius, center[x], center[y], center[z]);
160 
161  // Compute the absolute error between analytical and numerical solution at each grid point
162  get_absolute_error<SDF_sussman_grid, SDF_exact_grid, Error_grid>(g_dist);
164  // Get narrow band: Place particles on interface (narrow band width e.g. 4 grid points on each side of the
165  // interface)
166  size_t bc[grid_dim] = {NON_PERIODIC, NON_PERIODIC, NON_PERIODIC};
167  typedef aggregate<phi_type> props_nb;
169  Ghost<grid_dim, space_type> ghost_vd(0);
170  vd_type vd_narrow_band(0, box, bc, ghost_vd);
171  vd_narrow_band.setPropNames({"error"});
172  size_t narrow_band_width = 8;
173  NarrowBand<grid_in_type, phi_type> narrowBand(g_dist, narrow_band_width); // Instantiation of NarrowBand class
174  const size_t Error_vd = 0;
175  narrowBand.get_narrow_band_copy_specific_property<SDF_sussman_grid, Error_grid, Error_vd>(g_dist,
176  vd_narrow_band);
177  // Compute the L_2- and L_infinity-norm and save to file
178  LNorms<phi_type> lNorms_vd;
179  lNorms_vd.get_l_norms_vector<Error_vd>(vd_narrow_band);
180  std::cout << lNorms_vd.l2 << ", " << lNorms_vd.linf << std::endl;
181 
182 // N = 32, precision = float, 1e3 iterations
183 // Automatically found timestep is 0.00277489
184 // 1000,0.000029653310776,0.203437089920044,6032
185 // 0.0428669, 0.0763501
186 
187  BOOST_CHECK(lNorms_vd.l2 < 0.0428700);
188  BOOST_CHECK(lNorms_vd.linf < 0.0763502);
189  }
190 
191  BOOST_AUTO_TEST_CASE(RedistancingSussman_unit_sphere_fast_usingDefault_test)
192  {
193  typedef double phi_type;
194  typedef double space_type;
195  const phi_type EPSILON = std::numeric_limits<phi_type>::epsilon();
196  const size_t grid_dim = 3;
197  // some indices
198  const size_t x = 0;
199  const size_t y = 1;
200  const size_t z = 2;
201 
202  const size_t Phi_0_grid = 0;
203  const size_t SDF_sussman_grid = 1;
204  const size_t SDF_exact_grid = 2;
205  const size_t Error_grid = 3;
206 
207  size_t N = 32;
208  const size_t sz[grid_dim] = {N, N, N};
209  const space_type radius = 1.0;
210  const space_type box_lower = -2.0;
211  const space_type box_upper = 2.0;
212  Box<grid_dim, space_type> box({box_lower, box_lower, box_lower}, {box_upper, box_upper, box_upper});
213  Ghost<grid_dim, long int> ghost(0);
215  typedef grid_dist_id<grid_dim, space_type, props > grid_in_type;
216  grid_in_type g_dist(sz, box, ghost);
217  g_dist.setPropNames({"Phi_0", "SDF_sussman", "SDF_exact", "Relative error"});
218 
219  const space_type center[grid_dim] = {(box_upper+box_lower)/(space_type)2,
220  (box_upper+box_lower)/(space_type)2,
221  (box_upper+box_lower)/(space_type)2};
222 
223  init_grid_with_sphere<Phi_0_grid>(g_dist, radius, center[x], center[y], center[z]); // Initialize sphere onto grid
224 
225  Redist_options<> redist_options;
226  redist_options.min_iter = 1e3;
227  redist_options.max_iter = 1e3;
228 
229  redist_options.convTolChange.check = false;
230  redist_options.convTolResidual.check = false;
231 
232  redist_options.interval_check_convergence = 1e3;
233  redist_options.width_NB_in_grid_points = 8;
234  redist_options.print_current_iterChangeResidual = true;
235  redist_options.print_steadyState_iter = true;
236 
237  RedistancingSussman<grid_in_type> redist_obj(g_dist, redist_options); // Instantiation of
238 
239  std::cout << "Automatically found timestep is " << redist_obj.get_time_step() << std::endl;
240  // Run the redistancing. in the <> brackets provide property-index where 1.) your initial Phi is stored and 2.) where the resulting SDF should be written to.
241  redist_obj.run_redistancing<Phi_0_grid, SDF_sussman_grid>();
242 
243  // Compute exact signed distance function at each grid point
244  init_analytic_sdf_sphere<SDF_exact_grid>(g_dist, radius, center[x], center[y], center[z]);
245 
246  // Compute the absolute error between analytical and numerical solution at each grid point
247  get_absolute_error<SDF_sussman_grid, SDF_exact_grid, Error_grid>(g_dist);
249  // Get narrow band: Place particles on interface (narrow band width e.g. 4 grid points on each side of the
250  // interface)
251  size_t bc[grid_dim] = {NON_PERIODIC, NON_PERIODIC, NON_PERIODIC};
252  typedef aggregate<phi_type> props_nb;
254  Ghost<grid_dim, space_type> ghost_vd(0);
255  vd_type vd_narrow_band(0, box, bc, ghost_vd);
256  vd_narrow_band.setPropNames({"error"});
257  size_t narrow_band_width = 8;
258  NarrowBand<grid_in_type> narrowBand(g_dist, narrow_band_width); // Instantiation of NarrowBand class
259  const size_t Error_vd = 0;
260  narrowBand.get_narrow_band_copy_specific_property<SDF_sussman_grid, Error_grid, Error_vd>(g_dist,
261  vd_narrow_band);
262  // Compute the L_2- and L_infinity-norm and save to file
263  LNorms<> lNorms_vd;
264  lNorms_vd.get_l_norms_vector<Error_vd>(vd_narrow_band);
265  std::cout << lNorms_vd.l2 << ", " << lNorms_vd.linf << std::endl;
266 // int precision = 6;
267 // lNorms_vd.write_to_file(N, precision, "l_norms.csv", path_output);
268 
269 // N = 32, precision = double, 1e3 iterations
270 // Automatically found timestep is 0.00277489
271 // 1000,0.000029667373926,0.203437014506586,6032
272 // 0.0428668, 0.0763498
273 
274  BOOST_CHECK(lNorms_vd.l2 < 0.0428669);
275  BOOST_CHECK(lNorms_vd.linf < 0.0763499);
276  }
277 
278 BOOST_AUTO_TEST_SUITE_END()
279 
280 
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...
Header file containing functions for computing the error and the L_2 / L_infinity norm.
Structure to bundle options for redistancing.
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
void get_l_norms_vector(vectortype &vd)
Computes the L_2 and L_infinity norm on the basis of the precomputed error on a particle vector.
Definition: LNorms.hpp:153
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.
Header file containing functions that compute the analytical solution of the signed distance function...
Class for computing the l2/l_infinity norm for distributed grids and vectors based on given errors.
Definition: LNorms.hpp:103