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#include "timer.hpp"
4
45
46constexpr int U = 0;
47constexpr int V = 1;
48
49constexpr int x = 0;
50constexpr int y = 1;
51constexpr int z = 2;
52
54
56{
57 auto it = Old.getDomainIterator();
58
59 while (it.isNext())
60 {
61 // Get the local grid key
62 auto key = it.get();
63
64 // Old values U and V
65 Old.template get<U>(key) = 1.0;
66 Old.template get<V>(key) = 0.0;
67
68 // Old values U and V
69 New.template get<U>(key) = 0.0;
70 New.template get<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.getSubDomainIterator(start,stop);
86
87 while (it_init.isNext())
88 {
89 auto key = it_init.get();
90
91 Old.template get<U>(key) = 0.5 + (((double)std::rand())/RAND_MAX -0.5)/10.0;
92 Old.template get<V>(key) = 0.25 + (((double)std::rand())/RAND_MAX -0.5)/20.0;
93
94 ++it_init;
95 }
96}
97
98
99int 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
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 size_t timeSteps = 5000;
126
127 // K and F (Physical constant in the equation)
128 double K = 0.053;
129 double F = 0.014;
130
132
133 // New grid with the decomposition of the old grid
134 grid_dist_id<3, double, aggregate<double,double>> New(Old.getDecomposition(),sz,g);
135
136
137 // spacing of the grid on x and y
138 double spacing[3] = {Old.spacing(0),Old.spacing(1),Old.spacing(2)};
139
140 init(Old,New,domain);
141
142 // sync the ghost
143 size_t count = 0;
144 Old.template ghost_get<U,V>();
145
146 // because we assume that spacing[x] == spacing[y] we use formula 2
147 // and we calculate the prefactor of Eq 2
148 double uFactor = deltaT * du/(spacing[x]*spacing[x]);
149 double vFactor = deltaT * dv/(spacing[x]*spacing[x]);
150
151 timer tot_sim;
152 tot_sim.start();
153
155
156 static grid_key_dx<3> star_stencil_3D[7] = {{0,0,0},
157 {0,0,-1},
158 {0,0,1},
159 {0,-1,0},
160 {0,1,0},
161 {-1,0,0},
162 {1,0,0}};
163
165
166 for (size_t i = 0; i < timeSteps; ++i)
167 {
168 if (i % 300 == 0)
169 {std::cout << "STEP: " << i << std::endl;}
170
172
173 auto it = Old.getDomainIteratorStencil(star_stencil_3D);
174
175 while (it.isNext())
176 {
177 // center point
178 auto Cp = it.getStencil<0>();
179
180 // plus,minus X,Y,Z
181 auto mx = it.getStencil<1>();
182 auto px = it.getStencil<2>();
183 auto my = it.getStencil<3>();
184 auto py = it.getStencil<4>();
185 auto mz = it.getStencil<5>();
186 auto pz = it.getStencil<6>();
187
188 // update based on Eq 2
189 New.get<U>(Cp) = Old.get<U>(Cp) + uFactor * (
190 Old.get<U>(mz) +
191 Old.get<U>(pz) +
192 Old.get<U>(my) +
193 Old.get<U>(py) +
194 Old.get<U>(mx) +
195 Old.get<U>(px) -
196 6.0*Old.get<U>(Cp)) +
197 - deltaT * Old.get<U>(Cp) * Old.get<V>(Cp) * Old.get<V>(Cp) +
198 - deltaT * F * (Old.get<U>(Cp) - 1.0);
199
200
201 // update based on Eq 2
202 New.get<V>(Cp) = Old.get<V>(Cp) + vFactor * (
203 Old.get<V>(mz) +
204 Old.get<V>(pz) +
205 Old.get<V>(my) +
206 Old.get<V>(py) +
207 Old.get<V>(mx) +
208 Old.get<V>(px) -
209 6*Old.get<V>(Cp)) +
210 deltaT * Old.get<U>(Cp) * Old.get<V>(Cp) * Old.get<V>(Cp) +
211 - deltaT * (F+K) * Old.get<V>(Cp);
212
213 // Next point in the grid
214 ++it;
215 }
216
218
219 // Here we copy New into the old grid in preparation of the new step
220 // It would be better to alternate, but using this we can show the usage
221 // of the function copy. To note that copy work only on two grid of the same
222 // decomposition. If you want to copy also the decomposition, or force to be
223 // exactly the same, use Old = New
224 Old.copy(New);
225
226 // After copy we synchronize again the ghost part U and V
227 Old.ghost_get<U,V>();
228
229 // Every 500 time step we output the configuration for
230 // visualization
231 if (i % 500 == 0)
232 {
233// Old.save("output_" + std::to_string(count));
234 count++;
235 }
236 }
237
238 tot_sim.stop();
239 std::cout << "Total simulation: " << tot_sim.getwct() << std::endl;
240
242
255
256 openfpm_finalize();
257
259
268}
This class represent an N-dimensional box.
Definition Box.hpp:61
This is a distributed grid.
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition grid_key.hpp:503
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...
Boundary conditions.
Definition common.hpp:22