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 #include "timer.hpp"
4 
26 
28 constexpr int U = 0;
29 constexpr int V = 1;
30 
31 constexpr int x = 0;
32 constexpr int y = 1;
33 
35 
53 
55 void init(grid_dist_id<2,double,aggregate<double,double> > & Old, grid_dist_id<2,double,aggregate<double,double> > & New, Box<2,double> & domain)
56 {
57 
59 
72 
74  auto it = Old.getDomainIterator();
75 
76  while (it.isNext())
77  {
78  // Get the local grid key
79  auto key = it.get();
80 
81  // Old values U and V
82  Old.template get<U>(key) = 1.0;
83  Old.template get<V>(key) = 0.0;
84 
85  // Old values U and V
86  New.template get<U>(key) = 0.0;
87  New.template get<V>(key) = 0.0;
88 
89  ++it;
90  }
91 
93 
106 
108  grid_key_dx<2> start({(long int)std::floor(Old.size(0)*1.55f/domain.getHigh(0)),(long int)std::floor(Old.size(1)*1.55f/domain.getHigh(1))});
109  grid_key_dx<2> stop ({(long int)std::ceil (Old.size(0)*1.85f/domain.getHigh(0)),(long int)std::ceil (Old.size(1)*1.85f/domain.getHigh(1))});
110  auto it_init = Old.getSubDomainIterator(start,stop);
111 
112  while (it_init.isNext())
113  {
114  auto key = it_init.get();
115 
116  Old.template get<U>(key) = 0.5 + (((double)std::rand())/RAND_MAX -0.5)/100.0;
117  Old.template get<V>(key) = 0.25 + (((double)std::rand())/RAND_MAX -0.5)/200.0;
118 
119  ++it_init;
120  }
121 
123 
125 
126 }
127 
129 
130 
131 int main(int argc, char* argv[])
132 {
159 
161  openfpm_init(&argc,&argv);
162 
163  // domain
164  Box<2,double> domain({0.0,0.0},{2.5,2.5});
165 
166  // grid size
167  size_t sz[2] = {128,128};
168 
169  // Define periodicity of the grid
170  periodicity<2> bc = {PERIODIC,PERIODIC};
171 
172  // Ghost in grid unit
173  Ghost<2,long int> g(1);
174 
175  // deltaT
176  double deltaT = 1;
177 
178  // Diffusion constant for specie U
179  double du = 2*1e-5;
180 
181  // Diffusion constant for specie V
182  double dv = 1*1e-5;
183 
184  // Number of timesteps
185  size_t timeSteps = 15000;
186 
187  // K and F (Physical constant in the equation)
188  double K = 0.055;
189  double F = 0.03;
190 
192 
205 
208 
209  // New grid with the decomposition of the old grid
210  grid_dist_id<2, double, aggregate<double,double>> New(Old.getDecomposition(),sz,g);
211 
212 
213  // spacing of the grid on x and y
214  double spacing[2] = {Old.spacing(0),Old.spacing(1)};
215 
217 
227 
229  init(Old,New,domain);
230 
232 
267 
269  // sync the ghost
270  size_t count = 0;
271  Old.template ghost_get<U,V>();
272 
273  // because we assume that spacing[x] == spacing[y] we use formula 2
274  // and we calculate the prefactor of Eq 2
275  double uFactor = deltaT * du/(spacing[x]*spacing[x]);
276  double vFactor = deltaT * dv/(spacing[x]*spacing[x]);
277 
278  for (size_t i = 0; i < timeSteps; ++i)
279  {
280  auto it = Old.getDomainIterator();
281 
282  while (it.isNext())
283  {
284  auto key = it.get();
285 
286  // update based on Eq 2
287  New.get<U>(key) = Old.get<U>(key) + uFactor * (
288  Old.get<U>(key.move(x,1)) +
289  Old.get<U>(key.move(x,-1)) +
290  Old.get<U>(key.move(y,1)) +
291  Old.get<U>(key.move(y,-1)) +
292  -4.0*Old.get<U>(key)) +
293  - deltaT * Old.get<U>(key) * Old.get<V>(key) * Old.get<V>(key) +
294  - deltaT * F * (Old.get<U>(key) - 1.0);
295 
296  // update based on Eq 2
297  New.get<V>(key) = Old.get<V>(key) + vFactor * (
298  Old.get<V>(key.move(x,1)) +
299  Old.get<V>(key.move(x,-1)) +
300  Old.get<V>(key.move(y,1)) +
301  Old.get<V>(key.move(y,-1)) -
302  4*Old.get<V>(key)) +
303  deltaT * Old.get<U>(key) * Old.get<V>(key) * Old.get<V>(key) +
304  - deltaT * (F+K) * Old.get<V>(key);
305 
306  // Next point in the grid
307  ++it;
308  }
309 
310  // Here we copy New into the old grid in preparation of the new step
311  // It would be better to alternate, but using this we can show the usage
312  // of the function copy. To note that copy work only on two grid of the same
313  // decomposition. If you want to copy also the decomposition, or force to be
314  // exactly the same, use Old = New
315  Old.copy(New);
316 
317  // After copy we synchronize again the ghost part U and V
318  Old.ghost_get<U,V>();
319 
320  // Every 100 time step we output the configuration for
321  // visualization
322  if (i % 100 == 0)
323  {
324  Old.write_frame("output",count);
325  count++;
326  }
327  }
328 
330 
342 
344  openfpm_finalize();
345 
347 }
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:490
Definition: Ghost.hpp:39
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
St spacing(size_t i) const
Get the spacing of the grid in direction i.
[v_transform metafunction]