OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
mainDCPSE.cpp
Go to the documentation of this file.
1//
2// Created by Abhinav Singh on 15.11.2021.
3//
38
39// 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
48int 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 Derivative_x Dx(Particles, 2, rCut,sampling_factor, support_options::RADIUS);
195 Derivative_xx Dxx(Particles, 2, rCut,sampling_factor2, support_options::RADIUS);
196 Derivative_yy Dyy(Particles, 2, rCut,sampling_factor2, support_options::RADIUS);
197 Derivative_y Dy(Particles, 2, rCut,sampling_factor, support_options::RADIUS);
198 Derivative_x Bulk_Dx(Particles_bulk, 2, rCut,sampling_factor, support_options::RADIUS);
199 Derivative_y Bulk_Dy(Particles_bulk, 2, rCut,sampling_factor, support_options::RADIUS);
201
212 int n = 0, nmax = 300, ctr = 0, errctr=1, Vreset = 0;
213 double V_err=1;
214 if (Vreset == 1) {
215 P_bulk = 0;
216 P = 0;
217 Vreset = 0;
218 }
219 P=0;
220
221 eq_id vx,vy;
222 vx.setId(0);
223 vy.setId(1);
224
225 double sum, sum1, sum_k,V_err_old;
226 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]));
227 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]));
228 petsc_solver<double> solverPetsc;
229 solverPetsc.setSolver(KSPGMRES);
230 RHS[x] = 0.0;
231 RHS[y] = 0.0;
232 dV=0;
233 P=0;
234 timer tt;
235 while (V_err >= V_err_eps && n <= nmax) {
236 if (n%5==0){
237 Particles.ghost_get<0,1,2,3,4,5,6,7>(SKIP_LABELLING);
238 Particles.deleteGhost();
239 Particles.write_frame("LID",n,BINARY);
240 Particles.ghost_get<0>();
241 }
242 tt.start();
243 Particles.ghost_get<0>(SKIP_LABELLING);
244 RHS_bulk[x] = -Bulk_Dx(P);
245 RHS_bulk[y] = -Bulk_Dy(P);
246 DCPSE_scheme<equations2d2, decltype(Particles)> Solver(Particles);
247 Solver.impose(StokesX, bulk, RHS[x], vx);
248 Solver.impose(StokesY, bulk, RHS[y], vy);
249 Solver.impose(V_star[x], up_p, 1.0, vx);
250 Solver.impose(V_star[y], up_p, 0.0, vy);
251 Solver.impose(V_star[x], l_p, 0.0, vx);
252 Solver.impose(V_star[y], l_p, 0.0, vy);
253 Solver.impose(V_star[x], r_p, 0.0, vx);
254 Solver.impose(V_star[y], r_p, 0.0, vy);
255 Solver.impose(V_star[x], dw_p, 0.0, vx);
256 Solver.impose(V_star[y], dw_p, 0.0, vy);
257 Solver.solve_with_solver(solverPetsc,V_star[x], V_star[y]);
258
259 Particles.ghost_get<5>(SKIP_LABELLING);
260 div = (Dx(V_star[x]) + Dy(V_star[y]));
261
262 DCPSE_scheme<equations2d1E,decltype(Particles)> SolverH(Particles,options_solver::LAGRANGE_MULTIPLIER);
263 auto Helmholtz = Dxx(H)+Dyy(H);
264 SolverH.impose(Helmholtz,bulk,prop_id<4>());
265 SolverH.impose(Dy(H), up_p,0);
266 SolverH.impose(Dx(H), l_p,0);
267 SolverH.impose(Dx(H), r_p,0);
268 SolverH.impose(Dy(H), dw_p,0);
269 //SolverH.solve_with_solver(solverPetsc2,H);
270 SolverH.solve(H);
271 Particles.ghost_get<6>(SKIP_LABELLING);
272 Particles.ghost_get<6>(SKIP_LABELLING);
273 P_bulk = P - alpha*(div-0.5*(V[x]*Bulk_Dx(H)+V[y]*Bulk_Dy(H)));
274 V_star_bulk[0] = V_star[0] - Bulk_Dx(H);
275 V_star_bulk[1] = V_star[1] - Bulk_Dy(H);
276 sum = 0;
277 sum1 = 0;
278
279 for (int j = 0; j < bulk.size(); j++) {
280 auto p = bulk.get<0>(j);
281 sum += (Particles.getProp<5>(p)[0] - Particles.getProp<1>(p)[0]) *
282 (Particles.getProp<5>(p)[0] - Particles.getProp<1>(p)[0]) +
283 (Particles.getProp<5>(p)[1] - Particles.getProp<1>(p)[1]) *
284 (Particles.getProp<5>(p)[1] - Particles.getProp<1>(p)[1]);
285 sum1 += Particles.getProp<5>(p)[0] * Particles.getProp<5>(p)[0] +
286 Particles.getProp<5>(p)[1] * Particles.getProp<5>(p)[1];
287 }
288
289 V = V_star;
290 v_cl.sum(sum);
291 v_cl.sum(sum1);
292 v_cl.execute();
293 sum = sqrt(sum);
294 sum1 = sqrt(sum1);
295 V_err_old = V_err;
296 V_err = sum / sum1;
297 if (V_err > V_err_old || abs(V_err_old - V_err) < 1e-8) {
298 errctr++;
299 //alpha_P -= 0.1;
300 } else {
301 errctr = 0;
302 }
303 if (n > 3) {
304 if (errctr > 5) {
305 Vreset = 1;
306 errctr=0;
307 //break;
308 } else {
309 Vreset = 0;
310 }
311 }
312 n++;
313 tt.stop();
314 if (v_cl.rank() == 0) {
315 std::cout << "Rel l2 cgs err in V = " << V_err << " at " << n << " and took " <<tt.getwct() <<"("<<tt.getcputime()<<") seconds(CPU)." << std::endl;
316 }
317 }
318 Particles.deleteGhost();
319 Particles.write("LID");
320 tt2.stop();
321 if (v_cl.rank() == 0) {
322 std::cout << "The simulation took " << tt2.getcputime() << "(CPU) ------ " << tt2.getwct()
323 << "(Wall) Seconds.";
324 }
325 }
326 openfpm_finalize();
328
339}
This class represent an N-dimensional box.
Definition Box.hpp:61
Test structure used for several test.
This class implement the point shape in an N-dimensional space.
Definition Point.hpp:28
In case T does not match the PETSC precision compilation create a stub structure.
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...
Specify the general characteristic of system to solve.
It model an expression expr1 + ... exprn.
Definition sum.hpp:93