OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
HelpFunctionsForGrid.hpp
Go to the documentation of this file.
1 //
2 // Created by jstark on 2020-05-12.
3 //
12 #ifndef REDISTANCING_SUSSMAN_HELPFUNCTIONSFORGRID_HPP
13 #define REDISTANCING_SUSSMAN_HELPFUNCTIONSFORGRID_HPP
14 
15 #include <iostream>
16 #include <limits>
17 
18 #include "HelpFunctions.hpp"
19 #include "VCluster/VCluster.hpp"
20 #include "Grid/grid_dist_id.hpp"
21 
28 template <typename grid_type>
30 {
31  typename grid_type::stype sum = 0.0;
32  for (size_t d = 0; d < grid_type::dims; d++)
33  {
34  sum += 1.0 / (grid.spacing(d) * grid.spacing(d));
35  }
36  return 0.5 / sum;
37 }
38 
39 #if 0
40 
49 template <typename grid_type>
51 {
52  typename grid_type::stype sum = 0;
53  for (size_t d = 0; d < grid_type::dims; d++)
54  {
55  sum += u[d] / grid.spacing(d);
56  }
57  return C / sum;
58 }
68 template <typename grid_type>
70 {
71  typename grid_type::stype sum = 0;
72  for (size_t d = 0; d < grid_type::dims; d++)
73  {
74  sum += u / grid.spacing(d);
75  }
76  return C / sum;
77 }
78 #endif
79 
88 template <size_t Prop, typename grid_type, typename T>
90 {
91  auto dom_ghost = grid.getDomainGhostIterator();
92  while(dom_ghost.isNext())
93  {
94  auto key = dom_ghost.get();
95  grid.template get<Prop>(key) = init_value;
96  ++dom_ghost;
97  }
98 }
99 
111 template <size_t attr_sc, size_t attr_ds, typename grid_source_type, typename grid_dest_type>
112 void copy_gridTogrid(const grid_source_type & grid_sc, grid_dest_type & grid_ds, bool include_ghost=false)
113 {
114  assert(grid_source_type::dims == grid_dest_type::dims);
115  assert(grid_sc.size() == grid_ds.size());
116  if (include_ghost)
117  {
118  auto dom_sc = grid_sc.getDomainGhostIterator();
119  auto dom_ds = grid_ds.getDomainGhostIterator();
120  while (dom_sc.isNext())
121  {
122  auto key_sc = dom_sc.get();
123  auto key_ds = dom_ds.get();
124  grid_ds.template get<attr_ds>(key_ds) = grid_sc.template get<attr_sc>(key_sc);
125  ++dom_sc;
126  ++dom_ds;
127  }
128  }
129  else
130  {
131  auto dom_sc = grid_sc.getDomainIterator();
132  auto dom_ds = grid_ds.getDomainIterator();
133  while (dom_sc.isNext())
134  {
135  auto key_sc = dom_sc.get();
136  auto key_ds = dom_ds.get();
137  grid_ds.template get<attr_ds>(key_ds) = grid_sc.template get<attr_sc>(key_sc);
138  ++dom_sc;
139  ++dom_ds;
140  }
141  }
142 }
143 
151 template <typename grid_type>
153 {
154  typename grid_type::stype h_max = 0;
155  for (size_t d = 0; d < grid_type::dims; d++)
156  {
157  if (grid.spacing(d) > h_max) h_max = grid.spacing(d);
158  }
159  return h_max;
160 }
161 
169 template <typename grid_type>
171 {
172  typename grid_type::stype spacing [grid_type::dims];
173  for (size_t d = 0; d < grid_type::dims; d++)
174  {
175  spacing[d] = grid.spacing(d);
176  }
177  std::sort(std::begin(spacing), std::end(spacing));
178  return spacing[0];
179 }
180 
191 template <size_t Prop1, size_t Prop2, typename grid_type>
193 {
194  auto dom = grid.getDomainIterator();
195  typedef typename std::decay_t<decltype(grid.template get<Prop1>(dom.get()))> prop_type;
196  prop_type total_diff = 0;
197  while (dom.isNext())
198  {
199  auto key = dom.get();
200  total_diff += abs( grid.template get<Prop1>(key) - grid.template get<Prop2>(key) );
201  ++dom;
202  }
203  return total_diff / grid.size();
204 }
205 
213 template <size_t Prop, typename grid_type>
215 {
216  auto dom = grid.getDomainIterator();
217  typedef typename std::decay_t<decltype(grid.template get<Prop>(dom.get()))> prop_type;
218  prop_type max_value = std::numeric_limits<prop_type>::lowest();
219  while(dom.isNext())
220  {
221  auto key = dom.get();
222  if (grid.template get<Prop>(key) > max_value) max_value = grid.template get<Prop>(key);
223  ++dom;
224  }
225  auto & v_cl = create_vcluster();
226  v_cl.max(max_value);
227  v_cl.execute();
228  return max_value;
229 }
230 
238 template <size_t Prop, typename grid_type>
240 {
241  auto dom = grid.getDomainIterator();
242  typedef typename std::decay_t<decltype(grid.template get<Prop>(dom.get()))> prop_type;
243  prop_type min_value = std::numeric_limits<prop_type>::max();
244  while(dom.isNext())
245  {
246  auto key = dom.get();
247  if (grid.template get<Prop>(key) < min_value) min_value = grid.template get<Prop>(key);
248  ++dom;
249  }
250  auto & v_cl = create_vcluster();
251  v_cl.min(min_value);
252  v_cl.execute();
253  return min_value;
254 }
255 
263 template <size_t Prop_in, size_t Prop_out, typename grid_type>
265 {
266  auto dom = grid.getDomainIterator();
267  while(dom.isNext())
268  {
269  auto key = dom.get();
270  grid.template get<Prop_out>(key) = sgn(grid.template get<Prop_in>(key));
271  ++dom;
272  }
273 }
274 
275 
283 template <size_t Vector_in, size_t Magnitude_out, typename magnitude_type, typename gridtype>
284 void get_vector_magnitude(gridtype & grid)
285 {
286  grid.template ghost_get<Vector_in>();
287  auto dom = grid.getDomainGhostIterator();
288  while(dom.isNext())
289  {
290  magnitude_type sum = 0;
291  auto key = dom.get();
292  for(size_t d = 0; d < gridtype::dims; d++)
293  {
294  sum += grid.template get<Vector_in> (key)[d] * grid.template get<Vector_in> (key)[d];
295  }
296  grid.template get<Magnitude_out> (key) = sqrt(sum);
297  ++dom;
298  }
299 }
300 
307 template <size_t Vector_in, typename key_type, typename gridtype>
308 auto get_vector_magnitude(gridtype & grid, key_type & key)
309 {
310  typedef typename std::remove_const_t<std::remove_reference_t<decltype(grid.template get<Vector_in>(key)[0])>>
311  prop_type;
312  prop_type sum = 0;
313  for(size_t d = 0; d < gridtype::dims; d++)
314  {
315  sum += grid.template get<Vector_in> (key)[d] * grid.template get<Vector_in> (key)[d];
316  }
317  return sqrt(sum);
318 }
319 
320 
321 #endif //REDISTANCING_SUSSMAN_HELPFUNCTIONSFORGRID_HPP
static const unsigned int dims
Number of dimensions.
auto get_max_val(grid_type &grid)
Determines the maximum value stored on a given grid at a given property.
void init_grid_and_ghost(grid_type &grid, T init_value)
Initializes given property Prop of an OpenFPM grid including ghost layer with a given value from init...
auto get_min_val(grid_type &grid)
Determines the minimum value stored on a given grid at a given property.
grid_type::stype get_smallest_spacing(grid_type &grid)
Determines the smallest spacing of a grid which is potentially anisotropic when comparing x,...
int sgn(T val)
Gets the sign of a variable.
St stype
Type of space.
auto average_difference(grid_type &grid)
Computes the average difference between the values stored at two different properties of the same gri...
This is a distributed grid.
void get_vector_magnitude(gridtype &grid)
Computes the magnitude of the gradient (L2-norm of gradient vector).
void copy_gridTogrid(const grid_source_type &grid_sc, grid_dest_type &grid_ds, bool include_ghost=false)
Copies the value stored in a given property from a given source grid to a given destination grid.
grid_type::stype get_biggest_spacing(grid_type &grid)
Determines the biggest spacing of a grid which is potentially anisotropic when comparing x,...
OutputIteratorT ScanTileStateT int ScanOpT InitValueT init_value
Initial value to seed the exclusive scan.
void init_sign_prop(grid_type &grid)
Determines the sign of a value stored at a given property and stores it in another property.
It model an expression expr1 + ... exprn.
Definition: sum.hpp:92
grid_type::stype get_time_step_CFL(grid_type &grid)
Computes the time step for the iterative re-distancing fulfilling CFL condition.
Header file containing small mathematical help-functions that are needed e.g. for the Sussman redista...