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 "Vector/vector_dist.hpp"
2 #include "Operators/Vector/vector_dist_operators.hpp"
3 
22 
24 constexpr int A = 0;
25 constexpr int B = 1;
26 constexpr int C = 2;
27 constexpr int D = 3;
28 
30 
31 
57 
59 // Kernel are structures
60 struct exp_kernel
61 {
62  // variance of the exponential
63  float sigma;
64 
65  // Constructor require to define the variance
66  exp_kernel(float sigma)
67  :sigma(sigma)
68  {}
69 
70  // The kernel function itself K(x_p,x_q,A_p,A_q). The name MUST be value and require 4 arguments.
71  // p,q,A_p,A_q Position of p, position of q, value of the property A on p, value of the property A on q
72  // and it return the same type of the property A
74  {
75  // calculate the distance between p and q
76  float dist = norm(p-q);
77 
78  // Calculate the exponential
79  return Pq * exp(- dist * dist / sigma);
80  }
81 
82  // The kernel function itself K(p,q,A_p,A_q,particles). The name MUST be value and require 5 arguments.
83  // p,q,A_p,A_q,particles size_t index of p, size_t index of q, value of the property A on p, value of the property A on q,
84  // original vector of the particles p, and it return the same type of the property A
85  inline Point<3,double> value(size_t p, size_t q, Point<3,double> & Pp, Point<3,double> & Pq, const vector_dist<3,double, aggregate<double,double,Point<3,double>,Point<3,double>> > & v)
86  {
87  // calculate the distance between p and q
88  float dist = norm(p-q);
89 
90  // Calculate the exponential
91  return Pq * exp(- dist * dist / sigma);
92  }
93 };
94 
96 
97 int main(int argc, char* argv[])
98 {
112 
114  // initialize the library
115  openfpm_init(&argc,&argv);
116 
117  // Here we define our domain a 2D box with intervals from 0 to 1.0
118  Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});
119 
120  // Here we define the boundary conditions of our problem
121  size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};
122 
123  // extended boundary around the domain, and the processor domain
124  Ghost<3,float> g(0.01);
125 
126  // Delta t
127  double dt = 0.01;
128 
130 
144 
146  // distributed vectors
148  vector_dist<3,double, aggregate<double> > vd2(4096,domain,bc,g);
149 
151 
165 
167  // Assign random position to the vector dist
168  auto it = vd.getDomainIterator();
169 
170  while (it.isNext())
171  {
172  auto p = it.get();
173 
174  vd.getPos(p)[0] = (double)rand() / RAND_MAX;
175  vd.getPos(p)[1] = (double)rand() / RAND_MAX;
176  vd.getPos(p)[2] = (double)rand() / RAND_MAX;
177 
178  vd2.getPos(p)[0] = (double)rand() / RAND_MAX;
179  vd2.getPos(p)[1] = (double)rand() / RAND_MAX;
180  vd2.getPos(p)[2] = (double)rand() / RAND_MAX;
181 
182  ++it;
183  }
184 
186 
200 
202  vd.map();
203  vd.map();
204 
206 
219 
221  // vA is an alias for the property A of the vector vd
222  // vB is an alias for the property V of the vector vd
223  // vC is ...
224  auto vA = getV<A>(vd);
225  auto vB = getV<B>(vd);
226  auto vC = getV<C>(vd);
227  auto vD = getV<D>(vd);
228 
229  // This indicate the particle position
230  auto vPOS = getV<PROP_POS>(vd);
231 
232  // same concept for the vector v2
233  auto v2A = getV<0>(vd);
234  auto v2POS = getV<PROP_POS>(vd);
235 
237 
250 
252  // Assign 1 to the property A
253  vA = 1;
254 
255  // Equal the property A to B
256  vB = vA;
257 
258  // Assign to the property C and for each component 4.0
259  vC = 4;
260 
261  // Assign the position of the particle to the property D
262  vD = vPOS;
263 
265 
282 
284  // In vA we set the distance from the origin for each particle
285  vA = norm(vPOS);
286 
287  //
288  // For each particle p calculate the expression under
289  //
290  // NOTE sin(2.0 * vD) and exp(5.0 * vD) are both vector
291  //
292  // so this is a scalar product
293  // |
294  // V
295  vA = vA + sin(2.0 * vD) * exp(1.2 * vD);
296  // |_____________________________|
297  // |
298  // V
299  // and this is a number
300  //
301  //
302 
303 
304  // pmul indicate component-wise multiplication the same as .* in Matlab
305  vC = pmul(vD,vD) * dt;
306 
307  // Normalization of the vector vD
308  vD = vD / sqrt( vD * vD );
309 
311 
325 
327  // we write the result on file
328  vd.write("output");
329 
331 
345 
347  // Constant point
348  Point<3,double> p0({0.5,0.5,0.5});
349 
350  // we cannot use p0 directly we have to create an expression from it
351  auto p0_e = getVExpr(p0);
352 
353  // here we are creating an expression, expr1 collect the full expression but does not produce any
354  // code execution
355  auto expr1 = 1.0/2.0/sqrt(M_PI)*exp(-(v2POS-p0_e)*(v2POS-p0_e)/2.0);
356 
357  // here the expression is executed on all particles of v2 and the result assigned to the property A of v2
358  v2A = expr1;
359 
361 
373 
375  assign(vA,1.0, // vA = 1
376  vB,expr1, // vB = 1.0/2.0/sqrt(M_PI)*exp(-(v2POS-p0_e)*(v2POS-p0_e)/2.0)
377  vC,vD/norm(vD), // vC = vD/norm(vD)
378  vD,2.0*vC); // vD = 2.0*vC // here vC = vD/norm(vD)
379 
381 
418 
420  // Create a kernel with sigma 0.5
421  exp_kernel ker(0.5);
422 
423  // Get a Cell list with 0.1 of cutoff-radius
424  auto cl = vd.getCellList(0.1);
425 
426  // We are going to do some calculation that require ghost, so sync it 3 == vD
427  vd.ghost_get<3>();
428 
430 
462 
464  vC = applyKernel_in( vD / norm(vD) ,vd,cl,ker) + vD;
465 
467 
469 
470  // Second version in this case the kernel is called with the particles id for p
471  // and
472  vC = applyKernel_in_gen( vD / norm(vD) ,vd,cl,ker) + vD;
473 
475 
484  // ok, because does not use applyKernel
485  vD = vD / norm(vD);
486 
488 
504 
506  vd.deleteGhost();
507  vd.write("output2");
508 
510 
522 
524  openfpm_finalize();
525 
527 }
Derivative second order on h (spacing)
Definition: Derivative.hpp:28
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
Definition: Ghost.hpp:39
exp_kernel(float var)
Exponential kernel giving variance.
float value(const Point< 3, float > &p, const Point< 3, float > &q, float pA, float pB)
Result of the exponential kernel.
Distributed vector.
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:81