OpenFPM  5.2.0
Project that contain the implementation of distributed structures
mainDCPSE.cpp
Go to the documentation of this file.
1 //
2 // Created by Abhinav Singh on 15.11.2021.
3 //
38 // Include Vector Expression,Vector Expressions for Subset,DCPSE , and Solver header files
40 #include "config.h"
41 #include <iostream>
42 #include "DCPSE/DCPSE_op/DCPSE_op.hpp"
43 #include "DCPSE/DCPSE_op/DCPSE_Solver.hpp"
44 #include "Operators/Vector/vector_dist_operators.hpp"
45 #include "Vector/vector_dist_subset.hpp"
47 
48 int main(int argc, char* argv[])
49 {
50 
51  {
66  openfpm_init(&argc,&argv);
67  timer tt2;
68  tt2.start();
69  size_t gd_sz = 81;
70  double Re=100;
71  double V_err_eps = 1e-2;
72  double alpha=0.0125;
73 
74  constexpr int x = 0;
75  constexpr int y = 1;
76  const size_t sz[2] = {gd_sz,gd_sz};
77  Box<2, double> box({0, 0}, {1,1});
78  size_t bc[2] = {NON_PERIODIC, NON_PERIODIC};
79  double spacing;
80  spacing = 1.0 / (sz[0] - 1);
81  double rCut = 3.1 * spacing;
82  int ord = 2;
83 
84  Ghost<2, double> ghost(rCut);
85  auto &v_cl = create_vcluster();
87 
88  vector_dist_ws<2, double, LidCavity> Particles(0, box,bc,ghost);
89  Particles.setPropNames({"00-Pressure","01-Velocity","02-RHS","03-dV","04-Divergence","05-Divergence","06-H","07-dP"});
90 
91  double x0, y0, x1, y1;
92  x0 = box.getLow(0);
93  y0 = box.getLow(1);
94  x1 = box.getHigh(0);
95  y1 = box.getHigh(1);
97 
111 
112  auto it = Particles.getGridIterator(sz);
113  while (it.isNext())
114  {
115  Particles.add();
116  auto key = it.get();
117  mem_id k0 = key.get(0);
118  double xp = k0 * spacing;
119  Particles.getLastPos()[0] = xp;
120  mem_id k1 = key.get(1);
121  double yp = k1 * spacing;
122  Particles.getLastPos()[1] = yp;
123  Particles.getLastProp<1>()[0]=0.0;
124  Particles.getLastProp<1>()[1]=0.0;
125  if (xp != x0 && yp != y0 && xp != x1 && yp != y1)
126  {
127  Particles.getLastSubset(0);
128  }
129  else if(yp==y1 && xp>x0 && xp<x1)
130  {
131  Particles.getLastSubset(1);
132  Particles.getLastProp<1>()[0]=1.0;
133  }
134  else if(xp==0)
135  {
136  Particles.getLastSubset(3);
137  }
138  else if(xp==x1)
139  {
140  Particles.getLastSubset(4);
141  }
142  else
143  {
144  Particles.getLastSubset(2);
145  }
146  ++it;
147  }
148 
149  Particles.map();
150  Particles.ghost_get<0>();
152 
163  vector_dist_subset<2, double, LidCavity> Particles_bulk(Particles,0);
164  vector_dist_subset<2, double, LidCavity> Particles_up(Particles,1);
165  vector_dist_subset<2, double, LidCavity> Particles_down(Particles,2);
166  vector_dist_subset<2, double, LidCavity> Particles_left(Particles,3);
167  vector_dist_subset<2, double, LidCavity> Particles_right(Particles,4);
168  auto &bulk=Particles_bulk.getIds();
169  auto &up_p=Particles_up.getIds();
170  auto &dw_p=Particles_down.getIds();
171  auto &l_p=Particles_left.getIds();
172  auto &r_p=Particles_right.getIds();
173 
174 
175  auto P = getV<0>(Particles);
176  auto V = getV<1>(Particles);
177  auto RHS = getV<2>(Particles);
178  auto dV = getV<3>(Particles);
179  auto div = getV<4>(Particles);
180  auto V_star = getV<5>(Particles);
181  auto H = getV<6>(Particles);
182  auto dP = getV<7>(Particles);
183 
184 
185 
186  auto P_bulk = getV<0>(Particles_bulk);
187  auto V_bulk = getV<1>(Particles_bulk);
188  auto RHS_bulk =getV<2>(Particles_bulk);
189  auto V_star_bulk = getV<5>(Particles_bulk);
190  auto dP_bulk = getV<7>(Particles_bulk);
191 
192 
193  P_bulk = 0;
194 
195  auto verletList = Particles.template getVerlet<VL_NON_SYMMETRIC|VL_SKIP_REF_PART>(rCut);
196  auto verletList_bulk = Particles_bulk.template getVerlet<VL_NON_SYMMETRIC|VL_SKIP_REF_PART>(rCut);
197 
198  Derivative_x<decltype(verletList)> Dx(Particles, verletList, 2, rCut);
199  Derivative_xx<decltype(verletList)> Dxx(Particles, verletList, 2, rCut);
200  Derivative_yy<decltype(verletList)> Dyy(Particles, verletList, 2, rCut);
201  Derivative_y<decltype(verletList)> Dy(Particles, verletList, 2, rCut);
202  Derivative_x<decltype(verletList_bulk)> Bulk_Dx(Particles, Particles_bulk, verletList_bulk, 2, rCut);
203  Derivative_y<decltype(verletList_bulk)> Bulk_Dy(Particles, Particles_bulk, verletList_bulk, 2, rCut);
205 
216  int n = 0, nmax = 300, ctr = 0, errctr=1, Vreset = 0;
217  double V_err=1;
218  if (Vreset == 1) {
219  P_bulk = 0;
220  P = 0;
221  Vreset = 0;
222  }
223  P=0;
224 
225  eq_id vx,vy;
226  vx.setId(0);
227  vy.setId(1);
228 
229  double sum, sum1, sum_k,V_err_old;
230  auto StokesX=(V[x]*Dx(V_star[x])+V[y]*Dy(V_star[x]))-(1.0/Re)*(Dxx(V_star[x])+Dyy(V_star[x]));
231  auto StokesY=(V[x]*Dx(V_star[y])+V[y]*Dy(V_star[y]))-(1.0/Re)*(Dxx(V_star[y])+Dyy(V_star[y]));
232  petsc_solver<double> solverPetsc;
233  solverPetsc.setSolver(KSPGMRES);
234  RHS[x] = 0.0;
235  RHS[y] = 0.0;
236  dV=0;
237  P=0;
238  timer tt;
239  while (V_err >= V_err_eps && n <= nmax) {
240  if (n%5==0){
241  Particles.ghost_get<0,1,2,3,4,5,6,7>(SKIP_LABELLING);
242  Particles.deleteGhost();
243  Particles.write_frame("LID",n,BINARY);
244  Particles.ghost_get<0>();
245  }
246  tt.start();
247  Particles.ghost_get<0>(SKIP_LABELLING);
248  RHS_bulk[x] = -Bulk_Dx(P);
249  RHS_bulk[y] = -Bulk_Dy(P);
250  DCPSE_scheme<equations2d2, decltype(Particles)> Solver(Particles);
251  Solver.impose(StokesX, bulk, RHS[x], vx);
252  Solver.impose(StokesY, bulk, RHS[y], vy);
253  Solver.impose(V_star[x], up_p, 1.0, vx);
254  Solver.impose(V_star[y], up_p, 0.0, vy);
255  Solver.impose(V_star[x], l_p, 0.0, vx);
256  Solver.impose(V_star[y], l_p, 0.0, vy);
257  Solver.impose(V_star[x], r_p, 0.0, vx);
258  Solver.impose(V_star[y], r_p, 0.0, vy);
259  Solver.impose(V_star[x], dw_p, 0.0, vx);
260  Solver.impose(V_star[y], dw_p, 0.0, vy);
261  Solver.solve_with_solver(solverPetsc,V_star[x], V_star[y]);
262 
263  Particles.ghost_get<5>(SKIP_LABELLING);
264  div = (Dx(V_star[x]) + Dy(V_star[y]));
265 
266  DCPSE_scheme<equations2d1E,decltype(Particles)> SolverH(Particles,options_solver::LAGRANGE_MULTIPLIER);
267  auto Helmholtz = Dxx(H)+Dyy(H);
268  SolverH.impose(Helmholtz,bulk,prop_id<4>());
269  SolverH.impose(Dy(H), up_p,0);
270  SolverH.impose(Dx(H), l_p,0);
271  SolverH.impose(Dx(H), r_p,0);
272  SolverH.impose(Dy(H), dw_p,0);
273  //SolverH.solve_with_solver(solverPetsc2,H);
274  SolverH.solve(H);
275  Particles.ghost_get<6>(SKIP_LABELLING);
276  Particles.ghost_get<6>(SKIP_LABELLING);
277  P_bulk = P - alpha*(div-0.5*(V[x]*Bulk_Dx(H)+V[y]*Bulk_Dy(H)));
278  V_star_bulk[0] = V_star[0] - Bulk_Dx(H);
279  V_star_bulk[1] = V_star[1] - Bulk_Dy(H);
280  sum = 0;
281  sum1 = 0;
282 
283  for (int j = 0; j < bulk.size(); j++) {
284  auto p = bulk.get<0>(j);
285  sum += (Particles.getProp<5>(p)[0] - Particles.getProp<1>(p)[0]) *
286  (Particles.getProp<5>(p)[0] - Particles.getProp<1>(p)[0]) +
287  (Particles.getProp<5>(p)[1] - Particles.getProp<1>(p)[1]) *
288  (Particles.getProp<5>(p)[1] - Particles.getProp<1>(p)[1]);
289  sum1 += Particles.getProp<5>(p)[0] * Particles.getProp<5>(p)[0] +
290  Particles.getProp<5>(p)[1] * Particles.getProp<5>(p)[1];
291  }
292 
293  V = V_star;
294  v_cl.sum(sum);
295  v_cl.sum(sum1);
296  v_cl.execute();
297  sum = sqrt(sum);
298  sum1 = sqrt(sum1);
299  V_err_old = V_err;
300  V_err = sum / sum1;
301  if (V_err > V_err_old || abs(V_err_old - V_err) < 1e-8) {
302  errctr++;
303  //alpha_P -= 0.1;
304  } else {
305  errctr = 0;
306  }
307  if (n > 3) {
308  if (errctr > 5) {
309  Vreset = 1;
310  errctr=0;
311  //break;
312  } else {
313  Vreset = 0;
314  }
315  }
316  n++;
317  tt.stop();
318  if (v_cl.rank() == 0) {
319  std::cout << "Rel l2 cgs err in V = " << V_err << " at " << n << " and took " <<tt.getwct() <<"("<<tt.getcputime()<<") seconds(CPU)." << std::endl;
320  }
321  }
322  Particles.deleteGhost();
323  Particles.write("LID");
324  tt2.stop();
325  if (v_cl.rank() == 0) {
326  std::cout << "The simulation took " << tt2.getcputime() << "(CPU) ------ " << tt2.getwct()
327  << "(Wall) Seconds.";
328  }
329  }
330  openfpm_finalize();
332 
343 }
This class represent an N-dimensional box.
Definition: Box.hpp:60
Definition: Ghost.hpp:40
Test structure used for several test.
Definition: Point_test.hpp:106
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:28
This class is able to do Matrix inversion in parallel with PETSC solvers.
void setSolver(KSPType type)
Set the Petsc solver.
Class for cpu time benchmarking.
Definition: timer.hpp:28
void stop()
Stop the timer.
Definition: timer.hpp:119
double getcputime()
Return the cpu time.
Definition: timer.hpp:142
void start()
Start the timer.
Definition: timer.hpp:90
double getwct()
Return the elapsed real time.
Definition: timer.hpp:130
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:221
Specify the general characteristic of system to solve.
Definition: EqnsStruct.hpp:648
It model an expression expr1 + ... exprn.
Definition: sum.hpp:93