1 static char help[] = "Time-dependent reactive low Mach Flow in 2d and 3d channels with finite elements.\n\ 2 We solve the reactive low Mach flow problem in a rectangular domain\n\ 3 using a parallel unstructured mesh (DMPLEX) to discretize the flow\n\ 4 and particles (DWSWARM) to discretize the chemical species.\n\n\n"; 5 6 /*F 7 This low Mach flow is time-dependent isoviscous Navier-Stokes flow. We discretize using the 8 finite element method on an unstructured mesh. The weak form equations are 9 10 \begin{align*} 11 < q, \nabla\cdot u > = 0 12 <v, du/dt> + <v, u \cdot \nabla u> + < \nabla v, \nu (\nabla u + {\nabla u}^T) > - < \nabla\cdot v, p > - < v, f > = 0 13 < w, u \cdot \nabla T > + < \nabla w, \alpha \nabla T > - < w, Q > = 0 14 \end{align*} 15 16 where $\nu$ is the kinematic viscosity and $\alpha$ is thermal diffusivity. 17 18 For visualization, use 19 20 -dm_view hdf5:$PWD/sol.h5 -sol_vec_view hdf5:$PWD/sol.h5::append -exact_vec_view hdf5:$PWD/sol.h5::append 21 22 The particles can be visualized using 23 24 -part_dm_view draw -part_dm_view_swarm_radius 0.03 25 26 F*/ 27 28 #include <petscdmplex.h> 29 #include <petscdmswarm.h> 30 #include <petscts.h> 31 #include <petscds.h> 32 #include <petscbag.h> 33 34 typedef enum {SOL_TRIG_TRIG, NUM_SOL_TYPES} SolType; 35 const char *solTypes[NUM_SOL_TYPES+1] = {"trig_trig", "unknown"}; 36 37 typedef enum {PART_LAYOUT_CELL, PART_LAYOUT_BOX, NUM_PART_LAYOUT_TYPES} PartLayoutType; 38 const char *partLayoutTypes[NUM_PART_LAYOUT_TYPES+1] = {"cell", "box", "unknown"}; 39 40 typedef struct { 41 PetscReal nu; /* Kinematic viscosity */ 42 PetscReal alpha; /* Thermal diffusivity */ 43 PetscReal T_in; /* Inlet temperature*/ 44 PetscReal omega; /* Rotation speed in MMS benchmark */ 45 } Parameter; 46 47 typedef struct { 48 /* Problem definition */ 49 PetscBag bag; /* Holds problem parameters */ 50 SolType solType; /* MMS solution type */ 51 PartLayoutType partLayout; /* Type of particle distribution */ 52 PetscInt Npc; /* The initial number of particles per cell */ 53 PetscReal partLower[3]; /* Lower left corner of particle box */ 54 PetscReal partUpper[3]; /* Upper right corner of particle box */ 55 PetscInt Npb; /* The initial number of particles per box dimension */ 56 } AppCtx; 57 58 typedef struct { 59 PetscReal ti; /* The time for ui, at the beginning of the advection solve */ 60 PetscReal tf; /* The time for uf, at the end of the advection solve */ 61 Vec ui; /* The PDE solution field at ti */ 62 Vec uf; /* The PDE solution field at tf */ 63 Vec x0; /* The initial particle positions at t = 0 */ 64 PetscErrorCode (*exact)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *); 65 AppCtx *ctx; /* Context for exact solution */ 66 } AdvCtx; 67 68 static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx) 69 { 70 PetscInt d; 71 for (d = 0; d < Nc; ++d) u[d] = 0.0; 72 return 0; 73 } 74 75 static PetscErrorCode constant(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx) 76 { 77 PetscInt d; 78 for (d = 0; d < Nc; ++d) u[d] = 1.0; 79 return 0; 80 } 81 82 /* 83 CASE: trigonometric-trigonometric 84 In 2D we use exact solution: 85 86 x = r0 cos(w t + theta0) r0 = sqrt(x0^2 + y0^2) 87 y = r0 sin(w t + theta0) theta0 = arctan(y0/x0) 88 u = -w r0 sin(theta0) = -w y 89 v = w r0 cos(theta0) = w x 90 p = x + y - 1 91 T = t + x + y 92 f = <1, 1> 93 Q = 1 + w (x - y)/r 94 95 so that 96 97 \nabla \cdot u = 0 + 0 = 0 98 99 f = du/dt + u \cdot \nabla u - \nu \Delta u + \nabla p 100 = <0, 0> + u_i d_i u_j - \nu 0 + <1, 1> 101 = <1, 1> + w^2 <-y, x> . <<0, 1>, <-1, 0>> 102 = <1, 1> + w^2 <-x, -y> 103 = <1, 1> - w^2 <x, y> 104 105 Q = dT/dt + u \cdot \nabla T - \alpha \Delta T 106 = 1 + <u, v> . <1, 1> - \alpha 0 107 = 1 + u + v 108 */ 109 static PetscErrorCode trig_trig_x(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *x, void *ctx) 110 { 111 const PetscReal x0 = X[0]; 112 const PetscReal y0 = X[1]; 113 const PetscReal R0 = PetscSqrtReal(x0*x0 + y0*y0); 114 const PetscReal theta0 = PetscAtan2Real(y0, x0); 115 Parameter *p = (Parameter *) ctx; 116 117 x[0] = R0*PetscCosReal(p->omega*time + theta0); 118 x[1] = R0*PetscSinReal(p->omega*time + theta0); 119 return 0; 120 } 121 static PetscErrorCode trig_trig_u(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx) 122 { 123 Parameter *p = (Parameter *) ctx; 124 125 u[0] = -p->omega*X[1]; 126 u[1] = p->omega*X[0]; 127 return 0; 128 } 129 static PetscErrorCode trig_trig_u_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx) 130 { 131 u[0] = 0.0; 132 u[1] = 0.0; 133 return 0; 134 } 135 136 static PetscErrorCode trig_trig_p(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx) 137 { 138 p[0] = X[0] + X[1] - 1.0; 139 return 0; 140 } 141 142 static PetscErrorCode trig_trig_T(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx) 143 { 144 T[0] = time + X[0] + X[1]; 145 return 0; 146 } 147 static PetscErrorCode trig_trig_T_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx) 148 { 149 T[0] = 1.0; 150 return 0; 151 } 152 153 static void f0_trig_trig_v(PetscInt dim, PetscInt Nf, PetscInt NfAux, 154 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 155 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 156 PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 157 { 158 const PetscReal omega = PetscRealPart(constants[3]); 159 PetscInt Nc = dim; 160 PetscInt c, d; 161 162 for (d = 0; d < dim; ++d) f0[d] = u_t[uOff[0]+d]; 163 164 for (c = 0; c < Nc; ++c) { 165 for (d = 0; d < dim; ++d) f0[c] += u[d]*u_x[c*dim+d]; 166 } 167 f0[0] -= 1.0 - omega*omega*X[0]; 168 f0[1] -= 1.0 - omega*omega*X[1]; 169 } 170 171 static void f0_trig_trig_w(PetscInt dim, PetscInt Nf, PetscInt NfAux, 172 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 173 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 174 PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 175 { 176 const PetscReal omega = PetscRealPart(constants[3]); 177 PetscInt d; 178 179 for (d = 0, f0[0] = 0; d < dim; ++d) f0[0] += u[uOff[0]+d]*u_x[uOff_x[2]+d]; 180 f0[0] += u_t[uOff[2]] - (1.0 + omega*(X[0] - X[1])); 181 } 182 183 static void f0_q(PetscInt dim, PetscInt Nf, PetscInt NfAux, 184 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 185 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 186 PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 187 { 188 PetscInt d; 189 for (d = 0, f0[0] = 0.0; d < dim; ++d) f0[0] += u_x[d*dim+d]; 190 } 191 192 /*f1_v = \nu[grad(u) + grad(u)^T] - pI */ 193 static void f1_v(PetscInt dim, PetscInt Nf, PetscInt NfAux, 194 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 195 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 196 PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 197 { 198 const PetscReal nu = PetscRealPart(constants[0]); 199 const PetscInt Nc = dim; 200 PetscInt c, d; 201 202 for (c = 0; c < Nc; ++c) { 203 for (d = 0; d < dim; ++d) { 204 f1[c*dim+d] = nu*(u_x[c*dim+d] + u_x[d*dim+c]); 205 } 206 f1[c*dim+c] -= u[uOff[1]]; 207 } 208 } 209 210 static void f1_w(PetscInt dim, PetscInt Nf, PetscInt NfAux, 211 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 212 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 213 PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 214 { 215 const PetscReal alpha = PetscRealPart(constants[1]); 216 PetscInt d; 217 for (d = 0; d < dim; ++d) f1[d] = alpha*u_x[uOff_x[2]+d]; 218 } 219 220 /*Jacobians*/ 221 static void g1_qu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 222 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 223 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 224 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[]) 225 { 226 PetscInt d; 227 for (d = 0; d < dim; ++d) g1[d*dim+d] = 1.0; 228 } 229 230 static void g0_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 231 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 232 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 233 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 234 { 235 PetscInt c, d; 236 const PetscInt Nc = dim; 237 238 for (d = 0; d < dim; ++d) g0[d*dim+d] = u_tShift; 239 240 for (c = 0; c < Nc; ++c) { 241 for (d = 0; d < dim; ++d) { 242 g0[c*Nc+d] += u_x[c*Nc+d]; 243 } 244 } 245 } 246 247 static void g1_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 248 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 249 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 250 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[]) 251 { 252 PetscInt NcI = dim; 253 PetscInt NcJ = dim; 254 PetscInt c, d, e; 255 256 for (c = 0; c < NcI; ++c) { 257 for (d = 0; d < NcJ; ++d) { 258 for (e = 0; e < dim; ++e) { 259 if (c == d) { 260 g1[(c*NcJ+d)*dim+e] += u[e]; 261 } 262 } 263 } 264 } 265 } 266 267 static void g2_vp(PetscInt dim, PetscInt Nf, PetscInt NfAux, 268 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 269 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 270 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[]) 271 { 272 PetscInt d; 273 for (d = 0; d < dim; ++d) g2[d*dim+d] = -1.0; 274 } 275 276 static void g3_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 277 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 278 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 279 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[]) 280 { 281 const PetscReal nu = PetscRealPart(constants[0]); 282 const PetscInt Nc = dim; 283 PetscInt c, d; 284 285 for (c = 0; c < Nc; ++c) { 286 for (d = 0; d < dim; ++d) { 287 g3[((c*Nc+c)*dim+d)*dim+d] += nu; // gradU 288 g3[((c*Nc+d)*dim+d)*dim+c] += nu; // gradU transpose 289 } 290 } 291 } 292 293 static void g0_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux, 294 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 295 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 296 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 297 { 298 PetscInt d; 299 for (d = 0; d < dim; ++d) g0[d] = u_tShift; 300 } 301 302 static void g0_wu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 303 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 304 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 305 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 306 { 307 PetscInt d; 308 for (d = 0; d < dim; ++d) g0[d] = u_x[uOff_x[2]+d]; 309 } 310 311 static void g1_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux, 312 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 313 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 314 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[]) 315 { 316 PetscInt d; 317 for (d = 0; d < dim; ++d) g1[d] = u[uOff[0]+d]; 318 } 319 320 static void g3_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux, 321 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 322 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 323 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[]) 324 { 325 const PetscReal alpha = PetscRealPart(constants[1]); 326 PetscInt d; 327 328 for (d = 0; d < dim; ++d) g3[d*dim+d] = alpha; 329 } 330 331 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 332 { 333 PetscInt sol, pl, n; 334 335 PetscFunctionBeginUser; 336 options->solType = SOL_TRIG_TRIG; 337 options->partLayout = PART_LAYOUT_CELL; 338 options->Npc = 1; 339 options->Npb = 1; 340 options->partLower[0] = options->partLower[1] = options->partLower[2] = 0.; 341 options->partUpper[0] = options->partUpper[1] = options->partUpper[2] = 1.; 342 PetscOptionsBegin(comm, "", "Low Mach flow Problem Options", "DMPLEX"); 343 sol = options->solType; 344 PetscCall(PetscOptionsEList("-sol_type", "The solution type", "ex77.c", solTypes, NUM_SOL_TYPES, solTypes[options->solType], &sol, NULL)); 345 options->solType = (SolType) sol; 346 pl = options->partLayout; 347 PetscCall(PetscOptionsEList("-part_layout_type", "The particle layout type", "ex77.c", partLayoutTypes, NUM_PART_LAYOUT_TYPES, partLayoutTypes[options->partLayout], &pl, NULL)); 348 options->partLayout = (PartLayoutType) pl; 349 PetscCall(PetscOptionsInt("-Npc", "The initial number of particles per cell", "ex77.c", options->Npc, &options->Npc, NULL)); 350 n = 3; 351 PetscCall(PetscOptionsRealArray("-part_lower", "The lower left corner of the particle box", "ex77.c", options->partLower, &n, NULL)); 352 n = 3; 353 PetscCall(PetscOptionsRealArray("-part_upper", "The upper right corner of the particle box", "ex77.c", options->partUpper, &n, NULL)); 354 PetscCall(PetscOptionsInt("-Npb", "The initial number of particles per box dimension", "ex77.c", options->Npb, &options->Npb, NULL)); 355 PetscOptionsEnd(); 356 PetscFunctionReturn(0); 357 } 358 359 static PetscErrorCode SetupParameters(AppCtx *user) 360 { 361 PetscBag bag; 362 Parameter *p; 363 364 PetscFunctionBeginUser; 365 /* setup PETSc parameter bag */ 366 PetscCall(PetscBagGetData(user->bag, (void **) &p)); 367 PetscCall(PetscBagSetName(user->bag, "par", "Low Mach flow parameters")); 368 bag = user->bag; 369 PetscCall(PetscBagRegisterReal(bag, &p->nu, 1.0, "nu", "Kinematic viscosity")); 370 PetscCall(PetscBagRegisterReal(bag, &p->alpha, 1.0, "alpha", "Thermal diffusivity")); 371 PetscCall(PetscBagRegisterReal(bag, &p->T_in, 1.0, "T_in", "Inlet temperature")); 372 PetscCall(PetscBagRegisterReal(bag, &p->omega, 1.0, "omega", "Rotation speed in MMS benchmark")); 373 PetscFunctionReturn(0); 374 } 375 376 static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) 377 { 378 PetscFunctionBeginUser; 379 PetscCall(DMCreate(comm, dm)); 380 PetscCall(DMSetType(*dm, DMPLEX)); 381 PetscCall(DMSetFromOptions(*dm)); 382 PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view")); 383 PetscFunctionReturn(0); 384 } 385 386 static PetscErrorCode SetupProblem(DM dm, AppCtx *user) 387 { 388 PetscErrorCode (*exactFuncs[3])(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx); 389 PetscErrorCode (*exactFuncs_t[3])(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx); 390 PetscDS prob; 391 DMLabel label; 392 Parameter *ctx; 393 PetscInt id; 394 395 PetscFunctionBeginUser; 396 PetscCall(DMGetLabel(dm, "marker", &label)); 397 PetscCall(DMGetDS(dm, &prob)); 398 switch(user->solType) { 399 case SOL_TRIG_TRIG: 400 PetscCall(PetscDSSetResidual(prob, 0, f0_trig_trig_v, f1_v)); 401 PetscCall(PetscDSSetResidual(prob, 2, f0_trig_trig_w, f1_w)); 402 403 exactFuncs[0] = trig_trig_u; 404 exactFuncs[1] = trig_trig_p; 405 exactFuncs[2] = trig_trig_T; 406 exactFuncs_t[0] = trig_trig_u_t; 407 exactFuncs_t[1] = NULL; 408 exactFuncs_t[2] = trig_trig_T_t; 409 break; 410 default: SETERRQ(PetscObjectComm((PetscObject) prob), PETSC_ERR_ARG_WRONG, "Unsupported solution type: %s (%D)", solTypes[PetscMin(user->solType, NUM_SOL_TYPES)], user->solType); 411 } 412 413 PetscCall(PetscDSSetResidual(prob, 1, f0_q, NULL)); 414 415 PetscCall(PetscDSSetJacobian(prob, 0, 0, g0_vu, g1_vu, NULL, g3_vu)); 416 PetscCall(PetscDSSetJacobian(prob, 0, 1, NULL, NULL, g2_vp, NULL)); 417 PetscCall(PetscDSSetJacobian(prob, 1, 0, NULL, g1_qu, NULL, NULL)); 418 PetscCall(PetscDSSetJacobian(prob, 2, 0, g0_wu, NULL, NULL, NULL)); 419 PetscCall(PetscDSSetJacobian(prob, 2, 2, g0_wT, g1_wT, NULL, g3_wT)); 420 /* Setup constants */ 421 { 422 Parameter *param; 423 PetscScalar constants[4]; 424 425 PetscCall(PetscBagGetData(user->bag, (void **) ¶m)); 426 427 constants[0] = param->nu; 428 constants[1] = param->alpha; 429 constants[2] = param->T_in; 430 constants[3] = param->omega; 431 PetscCall(PetscDSSetConstants(prob, 4, constants)); 432 } 433 /* Setup Boundary Conditions */ 434 PetscCall(PetscBagGetData(user->bag, (void **) &ctx)); 435 id = 3; 436 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "top wall velocity", label, 1, &id, 0, 0, NULL, (void (*)(void)) exactFuncs[0], (void (*)(void)) exactFuncs_t[0], ctx, NULL)); 437 id = 1; 438 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "bottom wall velocity", label, 1, &id, 0, 0, NULL, (void (*)(void)) exactFuncs[0], (void (*)(void)) exactFuncs_t[0], ctx, NULL)); 439 id = 2; 440 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "right wall velocity", label, 1, &id, 0, 0, NULL, (void (*)(void)) exactFuncs[0], (void (*)(void)) exactFuncs_t[0], ctx, NULL)); 441 id = 4; 442 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "left wall velocity", label, 1, &id, 0, 0, NULL, (void (*)(void)) exactFuncs[0], (void (*)(void)) exactFuncs_t[0], ctx, NULL)); 443 id = 3; 444 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "top wall temp", label, 1, &id, 2, 0, NULL, (void (*)(void)) exactFuncs[2], (void (*)(void)) exactFuncs_t[2], ctx, NULL)); 445 id = 1; 446 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "bottom wall temp", label, 1, &id, 2, 0, NULL, (void (*)(void)) exactFuncs[2], (void (*)(void)) exactFuncs_t[2], ctx, NULL)); 447 id = 2; 448 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "right wall temp", label, 1, &id, 2, 0, NULL, (void (*)(void)) exactFuncs[2], (void (*)(void)) exactFuncs_t[2], ctx, NULL)); 449 id = 4; 450 PetscCall(PetscDSAddBoundary(prob, DM_BC_ESSENTIAL, "left wall temp", label, 1, &id, 2, 0, NULL, (void (*)(void)) exactFuncs[2], (void (*)(void)) exactFuncs_t[2], ctx, NULL)); 451 452 /*setup exact solution.*/ 453 PetscCall(PetscDSSetExactSolution(prob, 0, exactFuncs[0], ctx)); 454 PetscCall(PetscDSSetExactSolution(prob, 1, exactFuncs[1], ctx)); 455 PetscCall(PetscDSSetExactSolution(prob, 2, exactFuncs[2], ctx)); 456 PetscCall(PetscDSSetExactSolutionTimeDerivative(prob, 0, exactFuncs_t[0], ctx)); 457 PetscCall(PetscDSSetExactSolutionTimeDerivative(prob, 1, exactFuncs_t[1], ctx)); 458 PetscCall(PetscDSSetExactSolutionTimeDerivative(prob, 2, exactFuncs_t[2], ctx)); 459 PetscFunctionReturn(0); 460 } 461 462 /* x_t = v 463 464 Note that here we use the velocity field at t_{n+1} to advect the particles from 465 t_n to t_{n+1}. If we use both of these fields, we could use Crank-Nicholson or 466 the method of characteristics. 467 */ 468 static PetscErrorCode FreeStreaming(TS ts, PetscReal t, Vec X, Vec F, void *ctx) 469 { 470 AdvCtx *adv = (AdvCtx *) ctx; 471 Vec u = adv->ui; 472 DM sdm, dm, vdm; 473 Vec vel, locvel, pvel; 474 IS vis; 475 DMInterpolationInfo ictx; 476 const PetscScalar *coords, *v; 477 PetscScalar *f; 478 PetscInt vf[1] = {0}; 479 PetscInt dim, Np; 480 481 PetscFunctionBeginUser; 482 PetscCall(TSGetDM(ts, &sdm)); 483 PetscCall(DMSwarmGetCellDM(sdm, &dm)); 484 PetscCall(DMGetGlobalVector(sdm, &pvel)); 485 PetscCall(DMSwarmGetLocalSize(sdm, &Np)); 486 PetscCall(DMGetDimension(dm, &dim)); 487 /* Get local velocity */ 488 PetscCall(DMCreateSubDM(dm, 1, vf, &vis, &vdm)); 489 PetscCall(VecGetSubVector(u, vis, &vel)); 490 PetscCall(DMGetLocalVector(vdm, &locvel)); 491 PetscCall(DMPlexInsertBoundaryValues(vdm, PETSC_TRUE, locvel, adv->ti, NULL, NULL, NULL)); 492 PetscCall(DMGlobalToLocalBegin(vdm, vel, INSERT_VALUES, locvel)); 493 PetscCall(DMGlobalToLocalEnd(vdm, vel, INSERT_VALUES, locvel)); 494 PetscCall(VecRestoreSubVector(u, vis, &vel)); 495 PetscCall(ISDestroy(&vis)); 496 /* Interpolate velocity */ 497 PetscCall(DMInterpolationCreate(PETSC_COMM_SELF, &ictx)); 498 PetscCall(DMInterpolationSetDim(ictx, dim)); 499 PetscCall(DMInterpolationSetDof(ictx, dim)); 500 PetscCall(VecGetArrayRead(X, &coords)); 501 PetscCall(DMInterpolationAddPoints(ictx, Np, (PetscReal *) coords)); 502 PetscCall(VecRestoreArrayRead(X, &coords)); 503 /* Particles that lie outside the domain should be dropped, 504 whereas particles that move to another partition should trigger a migration */ 505 PetscCall(DMInterpolationSetUp(ictx, vdm, PETSC_FALSE, PETSC_TRUE)); 506 PetscCall(VecSet(pvel, 0.)); 507 PetscCall(DMInterpolationEvaluate(ictx, vdm, locvel, pvel)); 508 PetscCall(DMInterpolationDestroy(&ictx)); 509 PetscCall(DMRestoreLocalVector(vdm, &locvel)); 510 PetscCall(DMDestroy(&vdm)); 511 512 PetscCall(VecGetArray(F, &f)); 513 PetscCall(VecGetArrayRead(pvel, &v)); 514 PetscCall(PetscArraycpy(f, v, Np*dim)); 515 PetscCall(VecRestoreArrayRead(pvel, &v)); 516 PetscCall(VecRestoreArray(F, &f)); 517 PetscCall(DMRestoreGlobalVector(sdm, &pvel)); 518 PetscFunctionReturn(0); 519 } 520 521 static PetscErrorCode SetInitialParticleConditions(TS ts, Vec u) 522 { 523 AppCtx *user; 524 void *ctx; 525 DM dm; 526 PetscScalar *coords; 527 PetscReal x[3], dx[3]; 528 PetscInt n[3]; 529 PetscInt dim, d, i, j, k; 530 531 PetscFunctionBegin; 532 PetscCall(TSGetApplicationContext(ts, &ctx)); 533 user = ((AdvCtx *) ctx)->ctx; 534 PetscCall(TSGetDM(ts, &dm)); 535 PetscCall(DMGetDimension(dm, &dim)); 536 switch (user->partLayout) { 537 case PART_LAYOUT_CELL: 538 PetscCall(DMSwarmSetPointCoordinatesRandom(dm, user->Npc)); 539 break; 540 case PART_LAYOUT_BOX: 541 for (d = 0; d < dim; ++d) { 542 n[d] = user->Npb; 543 dx[d] = (user->partUpper[d] - user->partLower[d])/PetscMax(1, n[d] - 1); 544 } 545 PetscCall(VecGetArray(u, &coords)); 546 switch (dim) { 547 case 2: 548 x[0] = user->partLower[0]; 549 for (i = 0; i < n[0]; ++i, x[0] += dx[0]) { 550 x[1] = user->partLower[1]; 551 for (j = 0; j < n[1]; ++j, x[1] += dx[1]) { 552 const PetscInt p = j*n[0] + i; 553 for (d = 0; d < dim; ++d) coords[p*dim + d] = x[d]; 554 } 555 } 556 break; 557 case 3: 558 x[0] = user->partLower[0]; 559 for (i = 0; i < n[0]; ++i, x[0] += dx[0]) { 560 x[1] = user->partLower[1]; 561 for (j = 0; j < n[1]; ++j, x[1] += dx[1]) { 562 x[2] = user->partLower[2]; 563 for (k = 0; k < n[2]; ++k, x[2] += dx[2]) { 564 const PetscInt p = (k*n[1] + j)*n[0] + i; 565 for (d = 0; d < dim; ++d) coords[p*dim + d] = x[d]; 566 } 567 } 568 } 569 break; 570 default: SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_SUP, "Do not support particle layout in dimension %D", dim); 571 } 572 PetscCall(VecRestoreArray(u, &coords)); 573 break; 574 default: SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Invalid particle layout type %s", partLayoutTypes[PetscMin(user->partLayout, NUM_PART_LAYOUT_TYPES)]); 575 } 576 PetscFunctionReturn(0); 577 } 578 579 static PetscErrorCode SetupDiscretization(DM dm, DM sdm, AppCtx *user) 580 { 581 DM cdm = dm; 582 PetscFE fe[3]; 583 Parameter *param; 584 PetscInt *cellid, n[3]; 585 PetscReal x[3], dx[3]; 586 PetscScalar *coords; 587 DMPolytopeType ct; 588 PetscInt dim, d, cStart, cEnd, c, Np, p, i, j, k; 589 PetscBool simplex; 590 MPI_Comm comm; 591 592 PetscFunctionBeginUser; 593 PetscCall(DMGetDimension(dm, &dim)); 594 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 595 PetscCall(DMPlexGetCellType(dm, cStart, &ct)); 596 simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE; 597 /* Create finite element */ 598 PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 599 PetscCall(PetscFECreateDefault(comm, dim, dim, simplex, "vel_", PETSC_DEFAULT, &fe[0])); 600 PetscCall(PetscObjectSetName((PetscObject) fe[0], "velocity")); 601 602 PetscCall(PetscFECreateDefault(comm, dim, 1, simplex, "pres_", PETSC_DEFAULT, &fe[1])); 603 PetscCall(PetscFECopyQuadrature(fe[0], fe[1])); 604 PetscCall(PetscObjectSetName((PetscObject) fe[1], "pressure")); 605 606 PetscCall(PetscFECreateDefault(comm, dim, 1, simplex, "temp_", PETSC_DEFAULT, &fe[2])); 607 PetscCall(PetscFECopyQuadrature(fe[0], fe[2])); 608 PetscCall(PetscObjectSetName((PetscObject) fe[2], "temperature")); 609 610 /* Set discretization and boundary conditions for each mesh */ 611 PetscCall(DMSetField(dm, 0, NULL, (PetscObject) fe[0])); 612 PetscCall(DMSetField(dm, 1, NULL, (PetscObject) fe[1])); 613 PetscCall(DMSetField(dm, 2, NULL, (PetscObject) fe[2])); 614 PetscCall(DMCreateDS(dm)); 615 PetscCall(SetupProblem(dm, user)); 616 PetscCall(PetscBagGetData(user->bag, (void **) ¶m)); 617 while (cdm) { 618 PetscCall(DMCopyDisc(dm, cdm)); 619 PetscCall(DMGetCoarseDM(cdm, &cdm)); 620 } 621 PetscCall(PetscFEDestroy(&fe[0])); 622 PetscCall(PetscFEDestroy(&fe[1])); 623 PetscCall(PetscFEDestroy(&fe[2])); 624 625 { 626 PetscObject pressure; 627 MatNullSpace nullspacePres; 628 629 PetscCall(DMGetField(dm, 1, NULL, &pressure)); 630 PetscCall(MatNullSpaceCreate(PetscObjectComm(pressure), PETSC_TRUE, 0, NULL, &nullspacePres)); 631 PetscCall(PetscObjectCompose(pressure, "nullspace", (PetscObject) nullspacePres)); 632 PetscCall(MatNullSpaceDestroy(&nullspacePres)); 633 } 634 635 /* Setup particle information */ 636 PetscCall(DMSwarmSetType(sdm, DMSWARM_PIC)); 637 PetscCall(DMSwarmRegisterPetscDatatypeField(sdm, "mass", 1, PETSC_REAL)); 638 PetscCall(DMSwarmFinalizeFieldRegister(sdm)); 639 switch (user->partLayout) { 640 case PART_LAYOUT_CELL: 641 PetscCall(DMSwarmSetLocalSizes(sdm, (cEnd - cStart) * user->Npc, 0)); 642 PetscCall(DMSetFromOptions(sdm)); 643 PetscCall(DMSwarmGetField(sdm, DMSwarmPICField_cellid, NULL, NULL, (void **) &cellid)); 644 for (c = cStart; c < cEnd; ++c) { 645 for (p = 0; p < user->Npc; ++p) { 646 const PetscInt n = c*user->Npc + p; 647 648 cellid[n] = c; 649 } 650 } 651 PetscCall(DMSwarmRestoreField(sdm, DMSwarmPICField_cellid, NULL, NULL, (void **) &cellid)); 652 PetscCall(DMSwarmSetPointCoordinatesRandom(sdm, user->Npc)); 653 break; 654 case PART_LAYOUT_BOX: 655 Np = 1; 656 for (d = 0; d < dim; ++d) { 657 n[d] = user->Npb; 658 dx[d] = (user->partUpper[d] - user->partLower[d])/PetscMax(1, n[d] - 1); 659 Np *= n[d]; 660 } 661 PetscCall(DMSwarmSetLocalSizes(sdm, Np, 0)); 662 PetscCall(DMSetFromOptions(sdm)); 663 PetscCall(DMSwarmGetField(sdm, DMSwarmPICField_coor, NULL, NULL, (void **) &coords)); 664 switch (dim) { 665 case 2: 666 x[0] = user->partLower[0]; 667 for (i = 0; i < n[0]; ++i, x[0] += dx[0]) { 668 x[1] = user->partLower[1]; 669 for (j = 0; j < n[1]; ++j, x[1] += dx[1]) { 670 const PetscInt p = j*n[0] + i; 671 for (d = 0; d < dim; ++d) coords[p*dim + d] = x[d]; 672 } 673 } 674 break; 675 case 3: 676 x[0] = user->partLower[0]; 677 for (i = 0; i < n[0]; ++i, x[0] += dx[0]) { 678 x[1] = user->partLower[1]; 679 for (j = 0; j < n[1]; ++j, x[1] += dx[1]) { 680 x[2] = user->partLower[2]; 681 for (k = 0; k < n[2]; ++k, x[2] += dx[2]) { 682 const PetscInt p = (k*n[1] + j)*n[0] + i; 683 for (d = 0; d < dim; ++d) coords[p*dim + d] = x[d]; 684 } 685 } 686 } 687 break; 688 default: SETERRQ(comm, PETSC_ERR_SUP, "Do not support particle layout in dimension %D", dim); 689 } 690 PetscCall(DMSwarmRestoreField(sdm, DMSwarmPICField_coor, NULL, NULL, (void **) &coords)); 691 PetscCall(DMSwarmGetField(sdm, DMSwarmPICField_cellid, NULL, NULL, (void **) &cellid)); 692 for (p = 0; p < Np; ++p) cellid[p] = 0; 693 PetscCall(DMSwarmRestoreField(sdm, DMSwarmPICField_cellid, NULL, NULL, (void **) &cellid)); 694 PetscCall(DMSwarmMigrate(sdm, PETSC_TRUE)); 695 break; 696 default: SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid particle layout type %s", partLayoutTypes[PetscMin(user->partLayout, NUM_PART_LAYOUT_TYPES)]); 697 } 698 PetscCall(PetscObjectSetName((PetscObject) sdm, "Particles")); 699 PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view")); 700 PetscFunctionReturn(0); 701 } 702 703 static PetscErrorCode CreatePressureNullSpace(DM dm, PetscInt ofield, PetscInt nfield, MatNullSpace *nullSpace) 704 { 705 Vec vec; 706 PetscErrorCode (*funcs[3])(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *) = {zero, zero, zero}; 707 708 PetscFunctionBeginUser; 709 PetscCheck(ofield == 1,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Nullspace must be for pressure field at index 1, not %D", ofield); 710 funcs[nfield] = constant; 711 PetscCall(DMCreateGlobalVector(dm, &vec)); 712 PetscCall(DMProjectFunction(dm, 0.0, funcs, NULL, INSERT_ALL_VALUES, vec)); 713 PetscCall(VecNormalize(vec, NULL)); 714 PetscCall(PetscObjectSetName((PetscObject) vec, "Pressure Null Space")); 715 PetscCall(VecViewFromOptions(vec, NULL, "-pressure_nullspace_view")); 716 PetscCall(MatNullSpaceCreate(PetscObjectComm((PetscObject) dm), PETSC_FALSE, 1, &vec, nullSpace)); 717 PetscCall(VecDestroy(&vec)); 718 PetscFunctionReturn(0); 719 } 720 721 static PetscErrorCode RemoveDiscretePressureNullspace_Private(TS ts, Vec u) 722 { 723 DM dm; 724 MatNullSpace nullsp; 725 726 PetscFunctionBegin; 727 PetscCall(TSGetDM(ts, &dm)); 728 PetscCall(CreatePressureNullSpace(dm, 1, 1, &nullsp)); 729 PetscCall(MatNullSpaceRemove(nullsp, u)); 730 PetscCall(MatNullSpaceDestroy(&nullsp)); 731 PetscFunctionReturn(0); 732 } 733 734 /* Make the discrete pressure discretely divergence free */ 735 static PetscErrorCode RemoveDiscretePressureNullspace(TS ts) 736 { 737 Vec u; 738 739 PetscFunctionBegin; 740 PetscCall(TSGetSolution(ts, &u)); 741 PetscCall(RemoveDiscretePressureNullspace_Private(ts, u)); 742 PetscFunctionReturn(0); 743 } 744 745 static PetscErrorCode SetInitialConditions(TS ts, Vec u) 746 { 747 DM dm; 748 PetscReal t; 749 750 PetscFunctionBegin; 751 PetscCall(TSGetDM(ts, &dm)); 752 PetscCall(TSGetTime(ts, &t)); 753 PetscCall(DMComputeExactSolution(dm, t, u, NULL)); 754 PetscCall(RemoveDiscretePressureNullspace_Private(ts, u)); 755 PetscFunctionReturn(0); 756 } 757 758 static PetscErrorCode MonitorError(TS ts, PetscInt step, PetscReal crtime, Vec u, void *ctx) 759 { 760 PetscErrorCode (*exactFuncs[3])(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx); 761 void *ctxs[3]; 762 DM dm; 763 PetscDS ds; 764 Vec v; 765 PetscReal ferrors[3]; 766 PetscInt tl, l, f; 767 768 PetscFunctionBeginUser; 769 PetscCall(TSGetDM(ts, &dm)); 770 PetscCall(DMGetDS(dm, &ds)); 771 772 for (f = 0; f < 3; ++f) PetscCall(PetscDSGetExactSolution(ds, f, &exactFuncs[f], &ctxs[f])); 773 PetscCall(DMComputeL2FieldDiff(dm, crtime, exactFuncs, ctxs, u, ferrors)); 774 PetscCall(PetscObjectGetTabLevel((PetscObject) ts, &tl)); 775 for (l = 0; l < tl; ++l) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\t")); 776 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Timestep: %04d time = %-8.4g \t L_2 Error: [%2.3g, %2.3g, %2.3g]\n", (int) step, (double) crtime, (double) ferrors[0], (double) ferrors[1], (double) ferrors[2])); 777 778 PetscCall(DMGetGlobalVector(dm, &u)); 779 PetscCall(PetscObjectSetName((PetscObject) u, "Numerical Solution")); 780 PetscCall(VecViewFromOptions(u, NULL, "-sol_vec_view")); 781 PetscCall(DMRestoreGlobalVector(dm, &u)); 782 783 PetscCall(DMGetGlobalVector(dm, &v)); 784 PetscCall(DMProjectFunction(dm, 0.0, exactFuncs, ctxs, INSERT_ALL_VALUES, v)); 785 PetscCall(PetscObjectSetName((PetscObject) v, "Exact Solution")); 786 PetscCall(VecViewFromOptions(v, NULL, "-exact_vec_view")); 787 PetscCall(DMRestoreGlobalVector(dm, &v)); 788 789 PetscFunctionReturn(0); 790 } 791 792 /* Note that adv->x0 will not be correct after migration */ 793 static PetscErrorCode ComputeParticleError(TS ts, Vec u, Vec e) 794 { 795 AdvCtx *adv; 796 DM sdm; 797 Parameter *param; 798 const PetscScalar *xp0, *xp; 799 PetscScalar *ep; 800 PetscReal time; 801 PetscInt dim, Np, p; 802 MPI_Comm comm; 803 804 PetscFunctionBeginUser; 805 PetscCall(TSGetTime(ts, &time)); 806 PetscCall(TSGetApplicationContext(ts, &adv)); 807 PetscCall(PetscBagGetData(adv->ctx->bag, (void **) ¶m)); 808 PetscCall(PetscObjectGetComm((PetscObject) ts, &comm)); 809 PetscCall(TSGetDM(ts, &sdm)); 810 PetscCall(DMGetDimension(sdm, &dim)); 811 PetscCall(DMSwarmGetLocalSize(sdm, &Np)); 812 PetscCall(VecGetArrayRead(adv->x0, &xp0)); 813 PetscCall(VecGetArrayRead(u, &xp)); 814 PetscCall(VecGetArrayWrite(e, &ep)); 815 for (p = 0; p < Np; ++p) { 816 PetscScalar x[3]; 817 PetscReal x0[3]; 818 PetscInt d; 819 820 for (d = 0; d < dim; ++d) x0[d] = PetscRealPart(xp0[p*dim+d]); 821 PetscCall(adv->exact(dim, time, x0, 1, x, param)); 822 for (d = 0; d < dim; ++d) ep[p*dim+d] += x[d] - xp[p*dim+d]; 823 } 824 PetscCall(VecRestoreArrayRead(adv->x0, &xp0)); 825 PetscCall(VecRestoreArrayRead(u, &xp)); 826 PetscCall(VecRestoreArrayWrite(e, &ep)); 827 PetscFunctionReturn(0); 828 } 829 830 static PetscErrorCode MonitorParticleError(TS ts, PetscInt step, PetscReal time, Vec u, void *ctx) 831 { 832 AdvCtx *adv = (AdvCtx *) ctx; 833 DM sdm; 834 Parameter *param; 835 const PetscScalar *xp0, *xp; 836 PetscReal error = 0.0; 837 PetscInt dim, tl, l, Np, p; 838 MPI_Comm comm; 839 840 PetscFunctionBeginUser; 841 PetscCall(PetscBagGetData(adv->ctx->bag, (void **) ¶m)); 842 PetscCall(PetscObjectGetComm((PetscObject) ts, &comm)); 843 PetscCall(TSGetDM(ts, &sdm)); 844 PetscCall(DMGetDimension(sdm, &dim)); 845 PetscCall(DMSwarmGetLocalSize(sdm, &Np)); 846 PetscCall(VecGetArrayRead(adv->x0, &xp0)); 847 PetscCall(VecGetArrayRead(u, &xp)); 848 for (p = 0; p < Np; ++p) { 849 PetscScalar x[3]; 850 PetscReal x0[3]; 851 PetscReal perror = 0.0; 852 PetscInt d; 853 854 for (d = 0; d < dim; ++d) x0[d] = PetscRealPart(xp0[p*dim+d]); 855 PetscCall(adv->exact(dim, time, x0, 1, x, param)); 856 for (d = 0; d < dim; ++d) perror += PetscSqr(PetscRealPart(x[d] - xp[p*dim+d])); 857 error += perror; 858 } 859 PetscCall(VecRestoreArrayRead(adv->x0, &xp0)); 860 PetscCall(VecRestoreArrayRead(u, &xp)); 861 PetscCall(PetscObjectGetTabLevel((PetscObject) ts, &tl)); 862 for (l = 0; l < tl; ++l) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\t")); 863 PetscCall(PetscPrintf(comm, "Timestep: %04d time = %-8.4g \t L_2 Particle Error: [%2.3g]\n", (int) step, (double) time, (double) error)); 864 PetscFunctionReturn(0); 865 } 866 867 static PetscErrorCode AdvectParticles(TS ts) 868 { 869 TS sts; 870 DM sdm; 871 Vec coordinates; 872 AdvCtx *adv; 873 PetscReal time; 874 PetscBool lreset, reset; 875 PetscInt dim, n, N, newn, newN; 876 877 PetscFunctionBeginUser; 878 PetscCall(PetscObjectQuery((PetscObject) ts, "_SwarmTS", (PetscObject *) &sts)); 879 PetscCall(TSGetDM(sts, &sdm)); 880 PetscCall(TSGetRHSFunction(sts, NULL, NULL, (void **) &adv)); 881 PetscCall(DMGetDimension(sdm, &dim)); 882 PetscCall(DMSwarmGetSize(sdm, &N)); 883 PetscCall(DMSwarmGetLocalSize(sdm, &n)); 884 PetscCall(DMSwarmCreateGlobalVectorFromField(sdm, DMSwarmPICField_coor, &coordinates)); 885 PetscCall(TSGetTime(ts, &time)); 886 PetscCall(TSSetMaxTime(sts, time)); 887 adv->tf = time; 888 PetscCall(TSSolve(sts, coordinates)); 889 PetscCall(DMSwarmDestroyGlobalVectorFromField(sdm, DMSwarmPICField_coor, &coordinates)); 890 PetscCall(VecCopy(adv->uf, adv->ui)); 891 adv->ti = adv->tf; 892 893 PetscCall(DMSwarmMigrate(sdm, PETSC_TRUE)); 894 PetscCall(DMSwarmGetSize(sdm, &newN)); 895 PetscCall(DMSwarmGetLocalSize(sdm, &newn)); 896 lreset = (n != newn || N != newN) ? PETSC_TRUE : PETSC_FALSE; 897 PetscCallMPI(MPI_Allreduce(&lreset, &reset, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) sts))); 898 if (reset) { 899 PetscCall(TSReset(sts)); 900 PetscCall(DMSwarmVectorDefineField(sdm, DMSwarmPICField_coor)); 901 } 902 PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view")); 903 PetscFunctionReturn(0); 904 } 905 906 int main(int argc, char **argv) 907 { 908 DM dm, sdm; 909 TS ts, sts; 910 Vec u, xtmp; 911 AppCtx user; 912 AdvCtx adv; 913 PetscReal t; 914 PetscInt dim; 915 916 PetscCall(PetscInitialize(&argc, &argv, NULL,help)); 917 PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user)); 918 PetscCall(PetscBagCreate(PETSC_COMM_WORLD, sizeof(Parameter), &user.bag)); 919 PetscCall(SetupParameters(&user)); 920 PetscCall(TSCreate(PETSC_COMM_WORLD, &ts)); 921 PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &dm)); 922 PetscCall(TSSetDM(ts, dm)); 923 PetscCall(DMSetApplicationContext(dm, &user)); 924 /* Discretize chemical species */ 925 PetscCall(DMCreate(PETSC_COMM_WORLD, &sdm)); 926 PetscCall(PetscObjectSetOptionsPrefix((PetscObject) sdm, "part_")); 927 PetscCall(DMSetType(sdm, DMSWARM)); 928 PetscCall(DMGetDimension(dm, &dim)); 929 PetscCall(DMSetDimension(sdm, dim)); 930 PetscCall(DMSwarmSetCellDM(sdm, dm)); 931 /* Setup problem */ 932 PetscCall(SetupDiscretization(dm, sdm, &user)); 933 PetscCall(DMPlexCreateClosureIndex(dm, NULL)); 934 935 PetscCall(DMCreateGlobalVector(dm, &u)); 936 PetscCall(DMSetNullSpaceConstructor(dm, 1, CreatePressureNullSpace)); 937 938 PetscCall(DMTSSetBoundaryLocal(dm, DMPlexTSComputeBoundary, &user)); 939 PetscCall(DMTSSetIFunctionLocal(dm, DMPlexTSComputeIFunctionFEM, &user)); 940 PetscCall(DMTSSetIJacobianLocal(dm, DMPlexTSComputeIJacobianFEM, &user)); 941 PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP)); 942 PetscCall(TSSetPreStep(ts, RemoveDiscretePressureNullspace)); 943 PetscCall(TSMonitorSet(ts, MonitorError, &user, NULL)); 944 PetscCall(TSSetFromOptions(ts)); 945 946 PetscCall(TSSetComputeInitialCondition(ts, SetInitialConditions)); /* Must come after SetFromOptions() */ 947 PetscCall(SetInitialConditions(ts, u)); 948 PetscCall(TSGetTime(ts, &t)); 949 PetscCall(DMSetOutputSequenceNumber(dm, 0, t)); 950 PetscCall(DMTSCheckFromOptions(ts, u)); 951 952 /* Setup particle position integrator */ 953 PetscCall(TSCreate(PETSC_COMM_WORLD, &sts)); 954 PetscCall(PetscObjectSetOptionsPrefix((PetscObject) sts, "part_")); 955 PetscCall(PetscObjectIncrementTabLevel((PetscObject) sts, (PetscObject) ts, 1)); 956 PetscCall(TSSetDM(sts, sdm)); 957 PetscCall(TSSetProblemType(sts, TS_NONLINEAR)); 958 PetscCall(TSSetExactFinalTime(sts, TS_EXACTFINALTIME_MATCHSTEP)); 959 PetscCall(TSMonitorSet(sts, MonitorParticleError, &adv, NULL)); 960 PetscCall(TSSetFromOptions(sts)); 961 PetscCall(TSSetApplicationContext(sts, &adv)); 962 PetscCall(TSSetComputeExactError(sts, ComputeParticleError)); 963 PetscCall(TSSetComputeInitialCondition(sts, SetInitialParticleConditions)); 964 adv.ti = t; 965 adv.uf = u; 966 PetscCall(VecDuplicate(adv.uf, &adv.ui)); 967 PetscCall(VecCopy(u, adv.ui)); 968 PetscCall(TSSetRHSFunction(sts, NULL, FreeStreaming, &adv)); 969 PetscCall(TSSetPostStep(ts, AdvectParticles)); 970 PetscCall(PetscObjectCompose((PetscObject) ts, "_SwarmTS", (PetscObject) sts)); 971 PetscCall(DMSwarmVectorDefineField(sdm, DMSwarmPICField_coor)); 972 PetscCall(DMCreateGlobalVector(sdm, &adv.x0)); 973 PetscCall(DMSwarmCreateGlobalVectorFromField(sdm, DMSwarmPICField_coor, &xtmp)); 974 PetscCall(VecCopy(xtmp, adv.x0)); 975 PetscCall(DMSwarmDestroyGlobalVectorFromField(sdm, DMSwarmPICField_coor, &xtmp)); 976 switch(user.solType) { 977 case SOL_TRIG_TRIG: adv.exact = trig_trig_x;break; 978 default: SETERRQ(PetscObjectComm((PetscObject) sdm), PETSC_ERR_ARG_WRONG, "Unsupported solution type: %s (%D)", solTypes[PetscMin(user.solType, NUM_SOL_TYPES)], user.solType); 979 } 980 adv.ctx = &user; 981 982 PetscCall(TSSolve(ts, u)); 983 PetscCall(DMTSCheckFromOptions(ts, u)); 984 PetscCall(PetscObjectSetName((PetscObject) u, "Numerical Solution")); 985 986 PetscCall(VecDestroy(&u)); 987 PetscCall(VecDestroy(&adv.x0)); 988 PetscCall(VecDestroy(&adv.ui)); 989 PetscCall(DMDestroy(&dm)); 990 PetscCall(DMDestroy(&sdm)); 991 PetscCall(TSDestroy(&ts)); 992 PetscCall(TSDestroy(&sts)); 993 PetscCall(PetscBagDestroy(&user.bag)); 994 PetscCall(PetscFinalize()); 995 return 0; 996 } 997 998 /*TEST 999 1000 # Swarm does not work with complex 1001 test: 1002 suffix: 2d_tri_p2_p1_p1_tconvp 1003 requires: triangle !single !complex 1004 args: -dm_plex_separate_marker -sol_type trig_trig -dm_refine 2 \ 1005 -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \ 1006 -dmts_check .001 -ts_max_steps 4 -ts_dt 0.1 -ts_monitor_cancel \ 1007 -ksp_type fgmres -ksp_gmres_restart 10 -ksp_rtol 1.0e-9 -ksp_error_if_not_converged \ 1008 -pc_type fieldsplit -pc_fieldsplit_0_fields 0,2 -pc_fieldsplit_1_fields 1 -pc_fieldsplit_type schur -pc_fieldsplit_schur_factorization_type full \ 1009 -fieldsplit_0_pc_type lu \ 1010 -fieldsplit_pressure_ksp_rtol 1e-10 -fieldsplit_pressure_pc_type jacobi \ 1011 -omega 0.5 -part_layout_type box -part_lower 0.25,0.25 -part_upper 0.75,0.75 -Npb 5 \ 1012 -part_ts_max_steps 2 -part_ts_dt 0.05 -part_ts_convergence_estimate -convest_num_refine 1 -part_ts_monitor_cancel 1013 test: 1014 suffix: 2d_tri_p2_p1_p1_exit 1015 requires: triangle !single !complex 1016 args: -dm_plex_separate_marker -sol_type trig_trig -dm_refine 1 \ 1017 -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \ 1018 -dmts_check .001 -ts_max_steps 10 -ts_dt 0.1 \ 1019 -ksp_type fgmres -ksp_gmres_restart 10 -ksp_rtol 1.0e-9 -ksp_error_if_not_converged \ 1020 -pc_type fieldsplit -pc_fieldsplit_0_fields 0,2 -pc_fieldsplit_1_fields 1 -pc_fieldsplit_type schur -pc_fieldsplit_schur_factorization_type full \ 1021 -fieldsplit_0_pc_type lu \ 1022 -fieldsplit_pressure_ksp_rtol 1e-10 -fieldsplit_pressure_pc_type jacobi \ 1023 -omega 0.5 -part_layout_type box -part_lower 0.25,0.25 -part_upper 0.75,0.75 -Npb 5 \ 1024 -part_ts_max_steps 20 -part_ts_dt 0.05 1025 1026 TEST*/ 1027