OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
main_eigen.cpp
1
33
34#include "Grid/grid_dist_id.hpp"
35#include "Matrix/SparseMatrix.hpp"
36#include "Vector/Vector.hpp"
37#include "FiniteDifference/FDScheme.hpp"
38#include "FiniteDifference/util/common.hpp"
39#include "FiniteDifference/eq.hpp"
40#include "Solvers/umfpack_solver.hpp"
41#include "Solvers/petsc_solver.hpp"
42
43struct lid_nn
44{
45 // dimensionaly of the equation ( 3D problem ...)
46 static const unsigned int dims = 3;
47
48 // number of fields in the system v_x, v_y, v_z, P so a total of 4
49 static const unsigned int nvar = 4;
50
51 // boundary conditions PERIODIC OR NON_PERIODIC
52 static const bool boundary[];
53
54 // type of space float, double, ...
55 typedef float stype;
56
57 // type of base grid, it is the distributed grid that will store the result
58 // Note the first property is a 3D vector (velocity), the second is a scalar (Pressure)
60
61 // type of SparseMatrix, for the linear system, this parameter is bounded by the solver
62 // that you are using, in case of umfpack using <double,int> it is the only possible choice
63 // By default SparseMatrix is EIGEN based
64 typedef SparseMatrix<double,int> SparseMatrix_type;
65
66 // type of Vector for the linear system, this parameter is bounded by the solver
67 // that you are using, in case of umfpack using <double> it is the only possible choice
68 typedef Vector<double> Vector_type;
69
70 // Define that the underline grid where we discretize the system of equation is staggered
71 static const int grid_type = STAGGERED_GRID;
72};
73
74const bool lid_nn::boundary[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};
75
77
78
112
113// Constant Field
114struct eta
115{
116 typedef void const_field;
117
118 static float val() {return 1.0;}
119};
120
121// Model the equations
122
123constexpr unsigned int v[] = {0,1,2};
124constexpr unsigned int P = 3;
125constexpr unsigned int ic = 3;
126constexpr int x = 0;
127constexpr int y = 1;
128constexpr int z = 2;
129
130typedef Field<v[x],lid_nn> v_x;
131typedef Field<v[y],lid_nn> v_y;
132typedef Field<v[z],lid_nn> v_z;
133typedef Field<P,lid_nn> Prs;
134
135// Eq1 V_x
136
138typedef D<x,Prs,lid_nn> p_x; // Step 2
139typedef minus<p_x,lid_nn> m_p_x; // Step3
140typedef sum<eta_lap_vx,m_p_x,lid_nn> vx_eq; // Step4
141
142// Eq2 V_y
143
145typedef D<y,Prs,lid_nn> p_y;
148
149// Eq3 V_z
150
152typedef D<z,Prs,lid_nn> p_z;
155
156// Eq4 Incompressibility
157
158typedef D<x,v_x,lid_nn,FORWARD> dx_vx; // Step5
159typedef D<y,v_y,lid_nn,FORWARD> dy_vy; // Step6
160typedef D<z,v_z,lid_nn,FORWARD> dz_vz; // Step 7
161typedef sum<dx_vx,dy_vy,dz_vz,lid_nn> ic_eq; // Step8
162
164
199
200// Directional Avg
203
206
209
212
215
218
219
220#define EQ_1 0
221#define EQ_2 1
222#define EQ_3 2
223#define EQ_4 3
224
226
227#include "Vector/vector_dist.hpp"
228#include "data_type/aggregate.hpp"
229
230int main(int argc, char* argv[])
231{
252
253 // Initialize
254 openfpm_init(&argc,&argv);
255
256 // velocity in the grid is the property 0, pressure is the property 1
257 constexpr int velocity = 0;
258 constexpr int pressure = 1;
259
260 // Domain
261 Box<3,float> domain({0.0,0.0,0.0},{3.0,1.0,1.0});
262
263 // Ghost (Not important in this case but required)
264 Ghost<3,float> g(0.01);
265
266 // Grid points on x=36 and y=12 z=12
267 long int sz[] = {36,12,12};
268 size_t szu[3];
269 szu[0] = (size_t)sz[0];
270 szu[1] = (size_t)sz[1];
271 szu[2] = (size_t)sz[2];
272
273
274 // We need one more point on the left and down part of the domain
275 // This is given by the boundary conditions that we impose.
276 //
277 Padding<3> pd({1,1,1},{0,0,0});
278
280
293
295
297
316
317 // It is the maximum extension of the stencil (order 2 laplacian stencil has extension 1)
318 Ghost<3,long int> stencil_max(1);
319
320 // Finite difference scheme
321 FDScheme<lid_nn> fd(pd, stencil_max, domain, g_dist);
322
324
349
350 // start and end of the bulk
351
352 fd.impose(ic_eq(),0.0, EQ_4, {0,0,0},{sz[0]-2,sz[1]-2,sz[2]-2},true);
353 fd.impose(Prs(), 0.0, EQ_4, {0,0,0},{0,0,0});
354 fd.impose(vx_eq(),0.0, EQ_1, {1,0},{sz[0]-2,sz[1]-2,sz[2]-2});
355 fd.impose(vy_eq(),0.0, EQ_2, {0,1},{sz[0]-2,sz[1]-2,sz[2]-2});
356 fd.impose(vz_eq(),0.0, EQ_3, {0,0,1},{sz[0]-2,sz[1]-2,sz[2]-2});
357
358 // v_x
359 // R L (Right,Left)
360 fd.impose(v_x(),0.0, EQ_1, {0,0,0}, {0,sz[1]-2,sz[2]-2});
361 fd.impose(v_x(),0.0, EQ_1, {sz[0]-1,0,0},{sz[0]-1,sz[1]-2,sz[2]-2});
362
363 // T B (Top,Bottom)
364 fd.impose(avg_y_vx_f(),0.0, EQ_1, {0,-1,0}, {sz[0]-1,-1,sz[2]-2});
365 fd.impose(avg_y_vx(),0.0, EQ_1, {0,sz[1]-1,0},{sz[0]-1,sz[1]-1,sz[2]-2});
366
367 // A F (Forward,Backward)
368 fd.impose(avg_z_vx_f(),0.0, EQ_1, {0,-1,-1}, {sz[0]-1,sz[1]-1,-1});
369 fd.impose(avg_z_vx(),0.0, EQ_1, {0,-1,sz[2]-1},{sz[0]-1,sz[1]-1,sz[2]-1});
370
371 // v_y
372 // R L
373 fd.impose(avg_x_vy_f(),0.0, EQ_2, {-1,0,0}, {-1,sz[1]-1,sz[2]-2});
374 fd.impose(avg_x_vy(),1.0, EQ_2, {sz[0]-1,0,0},{sz[0]-1,sz[1]-1,sz[2]-2});
375
376 // T B
377 fd.impose(v_y(), 0.0, EQ_2, {0,0,0}, {sz[0]-2,0,sz[2]-2});
378 fd.impose(v_y(), 0.0, EQ_2, {0,sz[1]-1,0},{sz[0]-2,sz[1]-1,sz[2]-2});
379
380 // F A
381 fd.impose(avg_z_vy(),0.0, EQ_2, {-1,0,sz[2]-1}, {sz[0]-1,sz[1]-1,sz[2]-1});
382 fd.impose(avg_z_vy_f(),0.0, EQ_2, {-1,0,-1}, {sz[0]-1,sz[1]-1,-1});
383
384 // v_z
385 // R L
386 fd.impose(avg_x_vz_f(),0.0, EQ_3, {-1,0,0}, {-1,sz[1]-2,sz[2]-1});
387 fd.impose(avg_x_vz(),1.0, EQ_3, {sz[0]-1,0,0},{sz[0]-1,sz[1]-2,sz[2]-1});
388
389 // T B
390 fd.impose(avg_y_vz(),0.0, EQ_3, {-1,sz[1]-1,0},{sz[0]-1,sz[1]-1,sz[2]-1});
391 fd.impose(avg_y_vz_f(),0.0, EQ_3, {-1,-1,0}, {sz[0]-1,-1,sz[2]-1});
392
393 // F A
394 fd.impose(v_z(),0.0, EQ_3, {0,0,0}, {sz[0]-2,sz[1]-2,0});
395 fd.impose(v_z(),0.0, EQ_3, {0,0,sz[2]-1},{sz[0]-2,sz[1]-2,sz[2]-1});
396
397 // When we pad the grid, there are points of the grid that are not
398 // touched by the previous condition. Mathematically this lead
399 // to have too many variables for the conditions that we are imposing.
400 // Here we are imposing variables that we do not touch to zero
401 //
402
403 // L R
404 fd.impose(Prs(), 0.0, EQ_4, {-1,-1,-1},{-1,sz[1]-1,sz[2]-1});
405 fd.impose(Prs(), 0.0, EQ_4, {sz[0]-1,-1,-1},{sz[0]-1,sz[1]-1,sz[2]-1});
406
407 // T B
408 fd.impose(Prs(), 0.0, EQ_4, {0,sz[1]-1,-1}, {sz[0]-2,sz[1]-1,sz[2]-1});
409 fd.impose(Prs(), 0.0, EQ_4, {0,-1 ,-1}, {sz[0]-2,-1, sz[2]-1});
410
411 // F A
412 fd.impose(Prs(), 0.0, EQ_4, {0,0,sz[2]-1}, {sz[0]-2,sz[1]-2,sz[2]-1});
413 fd.impose(Prs(), 0.0, EQ_4, {0,0,-1}, {sz[0]-2,sz[1]-2,-1});
414
415 // Impose v_x v_y v_z padding
416 fd.impose(v_x(), 0.0, EQ_1, {-1,-1,-1},{-1,sz[1]-1,sz[2]-1});
417 fd.impose(v_y(), 0.0, EQ_2, {-1,-1,-1},{sz[0]-1,-1,sz[2]-1});
418 fd.impose(v_z(), 0.0, EQ_3, {-1,-1,-1},{sz[0]-1,sz[1]-1,-1});
419
421
436
437 // Create an UMFPACK solver
439
440 // Give to the solver A and b, return x, the solution
441 auto x = solver.solve(fd.getA(),fd.getB());
442
444
457
458 // Bring the solution to grid
459 fd.template copy<velocity,pressure>(x,{0,0},{sz[0]-1,sz[1]-1,sz[2]-1},g_dist);
460
461 g_dist.write("lid_driven_cavity_p_umfpack");
462
464
465
478
479 openfpm_finalize();
480
482
483
492}
Average.
Definition Average.hpp:27
This class represent an N-dimensional box.
Definition Box.hpp:61
This class decompose a space into sub-sub-domains and distribute them across processors.
Derivative second order on h (spacing)
Finite Differences.
Definition FDScheme.hpp:127
Definition eq.hpp:83
Class that contain Padding information on each direction positive and Negative direction.
Definition Ghost.hpp:127
Test structure used for several test.
Sparse Matrix implementation.
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support.
Definition Vector.hpp:40
This is a distributed grid.
stub when library compiled without eigen
static Vector< T > solve(const SparseMatrix< T, id_type, impl > &A, const Vector< T, impl > &b)
stub solve
[Definition of the system]
[Definition of the system]
It ancapsulate the minus operation.
Definition sum.hpp:142
It model an expression expr1 * expr2.
Definition mul.hpp:120
It model an expression expr1 + ... exprn.
Definition sum.hpp:93