OpenFPM  5.2.0
Project that contain the implementation of distributed structures
main.cpp
1 
16 #include "Grid/grid_dist_id.hpp"
17 #include "data_type/aggregate.hpp"
18 #include "timer.hpp"
19 
48 constexpr int U = 0;
49 constexpr int V = 1;
50 
51 constexpr int x = 0;
52 constexpr int y = 1;
53 constexpr int z = 2;
54 
55 void init(sgrid_dist_id<3,double,aggregate<double,double> > & Old, sgrid_dist_id<3,double,aggregate<double,double> > & New, Box<3,double> & domain)
56 {
57  auto it = Old.getGridIterator();
58 
59  while (it.isNext())
60  {
61  // Get the local grid key
62  auto key = it.get_dist();
63 
64  // Old values U and V
65  Old.template insert<U>(key) = 1.0;
66  Old.template insert<V>(key) = 0.0;
67 
68  // Old values U and V
69  New.template insert<U>(key) = 0.0;
70  New.template insert<V>(key) = 0.0;
71 
72  ++it;
73  }
74 
75  long int x_start = Old.size(0)*1.55f/domain.getHigh(0);
76  long int y_start = Old.size(1)*1.55f/domain.getHigh(1);
77  long int z_start = Old.size(1)*1.55f/domain.getHigh(2);
78 
79  long int x_stop = Old.size(0)*1.85f/domain.getHigh(0);
80  long int y_stop = Old.size(1)*1.85f/domain.getHigh(1);
81  long int z_stop = Old.size(1)*1.85f/domain.getHigh(2);
82 
83  grid_key_dx<3> start({x_start,y_start,z_start});
84  grid_key_dx<3> stop ({x_stop,y_stop,z_stop});
85  auto it_init = Old.getGridIterator(start,stop);
86 
87  while (it_init.isNext())
88  {
89  auto key = it_init.get_dist();
90 
91  Old.template insert<U>(key) = 0.5 + (((double)std::rand())/RAND_MAX -0.5)/10.0;
92  Old.template insert<V>(key) = 0.25 + (((double)std::rand())/RAND_MAX -0.5)/20.0;
93 
94  ++it_init;
95  }
96 }
97 
98 
99 int main(int argc, char* argv[])
100 {
101  openfpm_init(&argc,&argv);
102 
103  // domain
104  Box<3,double> domain({0.0,0.0,0.0},{2.5,2.5,2.5});
105 
106  // grid size
107  size_t sz[3] = {128,128,128};
108 
109  // Define periodicity of the grid
110  periodicity<3> bc = {PERIODIC,PERIODIC,PERIODIC};
111 
112  // Ghost in grid unit
113  Ghost<3,long int> g(1);
114 
115  // deltaT
116  double deltaT = 1;
117 
118  // Diffusion constant for specie U
119  double du = 2*1e-5;
120 
121  // Diffusion constant for specie V
122  double dv = 1*1e-5;
123 
124  // Number of timesteps
125 #ifdef TEST_RUN
126  size_t timeSteps = 200;
127 #else
128  size_t timeSteps = 5000;
129 #endif
130 
131  // K and F (Physical constant in the equation)
132  double K = 0.053;
133  double F = 0.014;
134 
136 
137  // New grid with the decomposition of the old grid
138  sgrid_dist_id<3, double, aggregate<double,double>> New(Old.getDecomposition(),sz,g);
139 
140 
141  // spacing of the grid on x and y
142  double spacing[3] = {Old.spacing(0),Old.spacing(1),Old.spacing(2)};
143 
144  init(Old,New,domain);
145 
146  // sync the ghost
147  size_t count = 0;
148  Old.template ghost_get<U,V>();
149 
150  // because we assume that spacing[x] == spacing[y] we use formula 2
151  // and we calculate the prefactor of Eq 2
152  double uFactor = deltaT * du/(spacing[x]*spacing[x]);
153  double vFactor = deltaT * dv/(spacing[x]*spacing[x]);
154 
155  Old.write("Init_condition");
156 
157  timer tot_sim;
158  tot_sim.start();
159 
160  auto & v_cl = create_vcluster();
161 
162  for (size_t i = 0; i < timeSteps; ++i)
163  {
164  if (v_cl.rank() == 0)
165  {std::cout << "STEP: " << i << std::endl;}
166 /* if (i % 300 == 0)
167  {
168  std::cout << "STEP: " << i << std::endl;
169  Old.write_frame("out",i);
170  }*/
171 
173 
174  auto it = Old.getDomainIterator();
175 
176  while (it.isNext())
177  {
178  // center point
179  auto Cp = it.get();
180 
181  // plus,minus X,Y,Z
182  auto mx = Cp.move(0,-1);
183  auto px = Cp.move(0,+1);
184  auto my = Cp.move(1,-1);
185  auto py = Cp.move(1,1);
186  auto mz = Cp.move(2,-1);
187  auto pz = Cp.move(2,1);
188 
189  // update based on Eq 2
190  New.insert<U>(Cp) = Old.get<U>(Cp) + uFactor * (
191  Old.get<U>(mz) +
192  Old.get<U>(pz) +
193  Old.get<U>(my) +
194  Old.get<U>(py) +
195  Old.get<U>(mx) +
196  Old.get<U>(px) -
197  6.0*Old.get<U>(Cp)) +
198  - deltaT * Old.get<U>(Cp) * Old.get<V>(Cp) * Old.get<V>(Cp) +
199  - deltaT * F * (Old.get<U>(Cp) - 1.0);
200 
201 
202  // update based on Eq 2
203  New.insert<V>(Cp) = Old.get<V>(Cp) + vFactor * (
204  Old.get<V>(mz) +
205  Old.get<V>(pz) +
206  Old.get<V>(my) +
207  Old.get<V>(py) +
208  Old.get<V>(mx) +
209  Old.get<V>(px) -
210  6*Old.get<V>(Cp)) +
211  deltaT * Old.get<U>(Cp) * Old.get<V>(Cp) * Old.get<V>(Cp) +
212  - deltaT * (F+K) * Old.get<V>(Cp);
213 
214  // Next point in the grid
215  ++it;
216  }
217 
219 
220  // Here we copy New into the old grid in preparation of the new step
221  // It would be better to alternate, but using this we can show the usage
222  // of the function copy. To note that copy work only on two grid of the same
223  // decomposition. If you want to copy also the decomposition, or force to be
224  // exactly the same, use Old = New
225  Old.copy_sparse(New);
226 
227  // After copy we synchronize again the ghost part U and V
228  Old.ghost_get<U,V>();
229 
230  // Every 500 time step we output the configuration for
231  // visualization
232  if (i % 500 == 0)
233  {
234  Old.save("output_" + std::to_string(count));
235  count++;
236  }
237  }
238 
239  tot_sim.stop();
240  std::cout << "Total simulation: " << tot_sim.getwct() << std::endl;
241 
243 
256 
257  openfpm_finalize();
258 
260 
269 }
This class represent an N-dimensional box.
Definition: Box.hpp:60
Definition: Ghost.hpp:40
This is a distributed grid.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:19
Class for cpu time benchmarking.
Definition: timer.hpp:28
void stop()
Stop the timer.
Definition: timer.hpp:119
void start()
Start the timer.
Definition: timer.hpp:90
double getwct()
Return the elapsed real time.
Definition: timer.hpp:130
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
[v_transform metafunction]
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:221
Boundary conditions.
Definition: common.hpp:22