OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
main.cpp
1 #include "Grid/grid_dist_id.hpp"
2 #include "data_type/aggregate.hpp"
3 
28 
30 void init(grid_dist_id<2,double,aggregate<double> > & g_dist, const size_t (& sz)[2])
31 {
43 
45  // Get the iterator
46  auto it = g_dist.getDomainGhostIterator();
47 
48  // For each point in the grid
49  while (it.isNext())
50  {
52 
64 
66  auto key = it.get();
67 
69 
84 
86  auto key_g = g_dist.getGKey(key);
87 
89 
103 
105  if (key_g.get(0) == 0 || key_g.get(0) == sz[0] ||
106  key_g.get(1) == 0 || key_g.get(1) == sz[1])
107  {
108  // Boundary part
109  g_dist.template get<0>(key) = 0.0;
110  }
111  else
112  {
113  // Internal part
114  g_dist.template get<0>(key) = 0.0;
115  }
116 
118 
120 
121  ++it;
122 
123  }
124 
126 }
127 
129 
130 constexpr int x = 0;
131 constexpr int y = 1;
132 
133 int main(int argc, char* argv[])
134 {
148 
150  openfpm_init(&argc,&argv);
151 
153 
168 
170  Box<2,double> domain({-1.0,-1.0},{1.0,1.0});
171  size_t sz[2] = {64,64};
172 
173  periodicity<2> bc = {NON_PERIODIC,NON_PERIODIC};
174 
175  // Ghost in grid unit
176  Ghost<2,long int> g(1);
177 
179 
199 
201  grid_dist_id<2, double, aggregate<double>> g_dist(sz,domain,g,bc);
202 
203  // spacing between two points
204  double spacing[2] = {g_dist.spacing(0),g_dist.spacing(1)};
205 
207 
219 
221  init(g_dist,sz);
222 
224 
237 
239  // sync the ghost property 0
240  g_dist.template ghost_get<0>();
241 
243 
254 
256  // flag that indicate if we are processing red or black
257  // we start from red
258  bool red_black = true;
259 
260  // 10000 iterations
261  for (size_t i = 0 ; i < 10000 ; i++)
262  {
276 
278  auto dom = g_dist.getDomainIterator();
279 
280  // Iterate over all the points
281  while (dom.isNext())
282  {
283 
284  // Get the local grid key
285  auto key = dom.get();
286 
287  // Here we convert the local grid position, into global position, key_g contain 3 integers that identify the position
288  // of the grid point in global coordinates
289  auto key_g = g_dist.getGKey(key);
290 
291 
292  //
293  // If we are processing red and is odd jump to the next point
294  // If we are processing black and is even jump to the next point
295  //
296  if (red_black == false && (key_g.get(0) + key_g.get(1)) % 2 == 0)
297  {++dom; continue;}
298  else if (red_black == true && (key_g.get(0) + key_g.get(1)) % 2 == 1)
299  {++dom; continue;}
300 
301  //
302  // Update the grid values
303  //
304  // P.S. The keyword template is removed, it is possible only if we are in a function
305  // without template parameters (if you are unsure use the keyword template)
306  //
307  g_dist.get<0>(key) = (g_dist.get<0>(key.move(x,1)) + g_dist.template get<0>(key.move(x,-1)) +
308  g_dist.get<0>(key.move(y,1)) + g_dist.template get<0>(key.move(y,-1)) +
309  - 1.0)/4.0;
310 
311  //
312  // next point (red/black)
313  ++dom;
314  }
315 
317 
332 
334  g_dist.template ghost_get<0>();
335 
336  // switch from red to black or black to red
337  red_black = !red_black;
338 
340  }
341 
350 
364 
366  // It contain the error
367  double error = 0.0;
368 
369  // Get the iterator
370  auto dom = g_dist.getDomainIterator();
371 
372  // Iterate over all the points
373  while (dom.isNext())
374  {
375  // same the the grid point and the global grid point
376  auto key = dom.get();
377 
378  // Calculate the error on each point
379  // The error is how much the solution does not respect the equation
380  double error_tmp = abs((g_dist.get<0>(key.move(x,1)) + g_dist.get<0>(key.move(x,-1)) +
381  g_dist.get<0>(key.move(y,1)) + g_dist.get<0>(key.move(y,-1)) +
382  - 4.0*g_dist.get<0>(key)) - 1.0);
383 
384  // In the norm infinity the maximum error across all the point is
385  // important
386  if (error_tmp > error)
387  error = error_tmp;
388 
389  // next point
390  ++dom;
391 
392  }
393 
394  // Get the maximum across processor to calculate the norm infinity of the error
395  // Norm infinity of the error is the maximum error across all the grid points
396  Vcluster & v_cl = create_vcluster();
397  v_cl.max(error);
398  v_cl.execute();
399 
400  // Only master print the norm infinity
401  if (v_cl.getProcessUnitID() == 0)
402  std::cout << "Error norm infinity: " << error << std::endl;
403 
405 
421 
423  g_dist.write("output");
424 
426 
427 
439 
441  openfpm_finalize();
442 
444 
453 }
size_t getProcessUnitID()
Get the process unit id.
void execute()
Execute all the requests.
void max(T &num)
Get the maximum number across all processors (or reduction with infinity norm)
Definition: Ghost.hpp:39
Implementation of VCluster class.
Definition: VCluster.hpp:36
This is a distributed grid.
This class represent an N-dimensional box.
Definition: Box.hpp:56
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:81