OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
main.cpp
1#include "Grid/grid_dist_id.hpp"
2#include "data_type/aggregate.hpp"
3
29
30void init(grid_dist_id<2,double,aggregate<double> > & g_dist, const size_t (& sz)[2])
31{
44
45 // Get the iterator
46 auto it = g_dist.getDomainGhostIterator();
47
48 // For each point in the grid
49 while (it.isNext())
50 {
52
65
66 auto key = it.get();
67
69
85
86 auto key_g = g_dist.getGKey(key);
87
89
105
106 if (key_g.get(0) < 0 || key_g.get(0) == sz[0] ||
107 key_g.get(1) < 0 || key_g.get(1) == sz[1])
108 {
109 // Boundary part
110 g_dist.template get<0>(key) = 0.0;
111 }
112 else
113 {
114 // Internal part
115 g_dist.template get<0>(key) = 0.0;
116 }
117
119
121
122 ++it;
123
124 }
125
127}
128
130
131constexpr int x = 0;
132constexpr int y = 1;
133
134int main(int argc, char* argv[])
135{
150
151 openfpm_init(&argc,&argv);
152
154
170
171 Box<2,double> domain({-1.0,-1.0},{1.0,1.0});
172 size_t sz[2] = {64,64};
173
174 periodicity<2> bc = {NON_PERIODIC,NON_PERIODIC};
175
176 // Ghost in grid unit
178
180
201
202 grid_dist_id<2, double, aggregate<double>> g_dist(sz,domain,g,bc);
203
204 // spacing between two points
205 double spacing[2] = {g_dist.spacing(0),g_dist.spacing(1)};
206
208
221
222 init(g_dist,sz);
223
225
239
240 // sync the ghost property 0
241 g_dist.template ghost_get<0>();
242
244
256
257 // flag that indicate if we are processing red or black
258 // we start from red
259 bool red_black = true;
260
261 // 10000 iterations
262 for (size_t i = 0 ; i < 10000 ; i++)
263 {
278
279 auto dom = g_dist.getDomainIterator();
280
281 // Iterate over all the points
282 while (dom.isNext())
283 {
284
285 // Get the local grid key
286 auto key = dom.get();
287
288 // Here we convert the local grid position, into global position, key_g contain 3 integers that identify the position
289 // of the grid point in global coordinates
290 auto key_g = g_dist.getGKey(key);
291
292
293 //
294 // If we are processing red and is odd jump to the next point
295 // If we are processing black and is even jump to the next point
296 //
297 if (red_black == false && (key_g.get(0) + key_g.get(1)) % 2 == 0)
298 {++dom; continue;}
299 else if (red_black == true && (key_g.get(0) + key_g.get(1)) % 2 == 1)
300 {++dom; continue;}
301
302 //
303 // Update the grid values
304 //
305 // P.S. The keyword template is removed, it is possible only if we are in a function
306 // without template parameters (if you are unsure use the keyword template)
307 //
308 g_dist.get<0>(key) = (g_dist.get<0>(key.move(x,1)) + g_dist.template get<0>(key.move(x,-1)) +
309 g_dist.get<0>(key.move(y,1)) + g_dist.template get<0>(key.move(y,-1)) +
310 - 1.0)/4.0;
311
312 //
313 // next point (red/black)
314 ++dom;
315 }
316
318
334
335 g_dist.template ghost_get<0>();
336
337 // switch from red to black or black to red
338 red_black = !red_black;
339
341 }
342
352
366
367 // It contain the error
368 double error = 0.0;
369
370 // Get the iterator
371 auto dom = g_dist.getDomainIterator();
372
373 // Iterate over all the points
374 while (dom.isNext())
375 {
376 // same the the grid point and the global grid point
377 auto key = dom.get();
378
379 // Calculate the error on each point
380 // The error is how much the solution does not respect the equation
381 double error_tmp = abs((g_dist.get<0>(key.move(x,1)) + g_dist.get<0>(key.move(x,-1)) +
382 g_dist.get<0>(key.move(y,1)) + g_dist.get<0>(key.move(y,-1)) +
383 - 4.0*g_dist.get<0>(key)) - 1.0);
384
385 // In the norm infinity the maximum error across all the point is
386 // important
387 if (error_tmp > error)
388 error = error_tmp;
389
390 // next point
391 ++dom;
392
393 }
394
395 // Get the maximum across processor to calculate the norm infinity of the error
396 // Norm infinity of the error is the maximum error across all the grid points
397 Vcluster<> & v_cl = create_vcluster();
398 v_cl.max(error);
399 v_cl.execute();
400
401 // Only master print the norm infinity
402 if (v_cl.getProcessUnitID() == 0)
403 std::cout << "Error norm infinity: " << error << std::endl;
404
406
423
424 g_dist.write("output");
425
427
428
441
442 openfpm_finalize();
443
445
454}
This class represent an N-dimensional box.
Definition Box.hpp:61
void execute()
Execute all the requests.
size_t getProcessUnitID()
Get the process unit id.
void max(T &num)
Get the maximum number across all processors (or reduction with infinity norm)
Implementation of VCluster class.
Definition VCluster.hpp:59
This is a distributed grid.
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Boundary conditions.
Definition common.hpp:22