1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 // libCEED + PETSc Example: Navier-Stokes 18 // 19 // This example demonstrates a simple usage of libCEED with PETSc to solve a 20 // Navier-Stokes problem. 21 // 22 // The code is intentionally "raw", using only low-level communication 23 // primitives. 24 // 25 // Build with: 26 // 27 // make [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>] navierstokes 28 // 29 // Sample runs: 30 // 31 // ./navierstokes -ceed /cpu/self -problem density_current -degree 1 32 // ./navierstokes -ceed /gpu/occa -problem advection -degree 1 33 // 34 //TESTARGS -ceed {ceed_resource} -test -degree 1 35 36 /// @file 37 /// Navier-Stokes example using PETSc 38 39 const char help[] = "Solve Navier-Stokes using PETSc and libCEED\n"; 40 41 #include <petscts.h> 42 #include <petscdmplex.h> 43 #include <ceed.h> 44 #include <stdbool.h> 45 #include <petscsys.h> 46 #include "common.h" 47 #include "advection.h" 48 #include "advection2d.h" 49 #include "densitycurrent.h" 50 51 // Problem Options 52 typedef enum { 53 NS_DENSITY_CURRENT = 0, 54 NS_ADVECTION = 1, 55 NS_ADVECTION2D = 2, 56 } problemType; 57 static const char *const problemTypes[] = { 58 "density_current", 59 "advection", 60 "advection2d", 61 "problemType","NS_",0 62 }; 63 64 typedef enum { 65 STAB_NONE = 0, 66 STAB_SU = 1, // Streamline Upwind 67 STAB_SUPG = 2, // Streamline Upwind Petrov-Galerkin 68 } StabilizationType; 69 static const char *const StabilizationTypes[] = { 70 "NONE", 71 "SU", 72 "SUPG", 73 "StabilizationType", "STAB_", NULL 74 }; 75 76 // Problem specific data 77 typedef struct { 78 CeedInt dim, qdatasize; 79 CeedQFunctionUser setup, ics, apply_rhs, apply_ifunction; 80 PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt, 81 PetscScalar[], void *); 82 const char *setup_loc, *ics_loc, *apply_rhs_loc, *apply_ifunction_loc; 83 const bool non_zero_time; 84 } problemData; 85 86 problemData problemOptions[] = { 87 [NS_DENSITY_CURRENT] = { 88 .dim = 3, 89 .qdatasize = 10, 90 .setup = Setup, 91 .setup_loc = Setup_loc, 92 .ics = ICsDC, 93 .ics_loc = ICsDC_loc, 94 .apply_rhs = DC, 95 .apply_rhs_loc = DC_loc, 96 .apply_ifunction = IFunction_DC, 97 .apply_ifunction_loc = IFunction_DC_loc, 98 .bc = Exact_DC, 99 .non_zero_time = false, 100 }, 101 [NS_ADVECTION] = { 102 .dim = 3, 103 .qdatasize = 10, 104 .setup = Setup, 105 .setup_loc = Setup_loc, 106 .ics = ICsAdvection, 107 .ics_loc = ICsAdvection_loc, 108 .apply_rhs = Advection, 109 .apply_rhs_loc = Advection_loc, 110 .apply_ifunction = IFunction_Advection, 111 .apply_ifunction_loc = IFunction_Advection_loc, 112 .bc = Exact_Advection, 113 .non_zero_time = false, 114 }, 115 [NS_ADVECTION2D] = { 116 .dim = 2, 117 .qdatasize = 5, 118 .setup = Setup2d, 119 .setup_loc = Setup2d_loc, 120 .ics = ICsAdvection2d, 121 .ics_loc = ICsAdvection2d_loc, 122 .apply_rhs = Advection2d, 123 .apply_rhs_loc = Advection2d_loc, 124 .apply_ifunction = IFunction_Advection2d, 125 .apply_ifunction_loc = IFunction_Advection2d_loc, 126 .bc = Exact_Advection2d, 127 .non_zero_time = true, 128 }, 129 }; 130 131 // PETSc user data 132 typedef struct User_ *User; 133 typedef struct Units_ *Units; 134 135 struct User_ { 136 MPI_Comm comm; 137 PetscInt outputfreq; 138 DM dm; 139 DM dmviz; 140 Mat interpviz; 141 Ceed ceed; 142 Units units; 143 CeedVector qceed, qdotceed, gceed; 144 CeedOperator op_rhs, op_ifunction; 145 Vec M; 146 char outputfolder[PETSC_MAX_PATH_LEN]; 147 PetscInt contsteps; 148 }; 149 150 struct Units_ { 151 // fundamental units 152 PetscScalar meter; 153 PetscScalar kilogram; 154 PetscScalar second; 155 PetscScalar Kelvin; 156 // derived units 157 PetscScalar Pascal; 158 PetscScalar JperkgK; 159 PetscScalar mpersquareds; 160 PetscScalar WpermK; 161 PetscScalar kgpercubicm; 162 PetscScalar kgpersquaredms; 163 PetscScalar Joulepercubicm; 164 }; 165 166 typedef struct SimpleBC_ *SimpleBC; 167 struct SimpleBC_ { 168 PetscInt nwall, nslip[3]; 169 PetscInt walls[10], slips[3][10]; 170 }; 171 172 // Essential BC dofs are encoded in closure indices as -(i+1). 173 static PetscInt Involute(PetscInt i) { 174 return i >= 0 ? i : -(i+1); 175 } 176 177 // Utility function to create local CEED restriction 178 static PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt P, 179 CeedElemRestriction *Erestrict) { 180 181 PetscSection section; 182 PetscInt c, cStart, cEnd, Nelem, Ndof, *erestrict, eoffset, nfields, dim; 183 PetscErrorCode ierr; 184 Vec Uloc; 185 186 PetscFunctionBeginUser; 187 ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr); 188 ierr = DMGetLocalSection(dm,§ion); CHKERRQ(ierr); 189 ierr = PetscSectionGetNumFields(section, &nfields); CHKERRQ(ierr); 190 PetscInt ncomp[nfields], fieldoff[nfields+1]; 191 fieldoff[0] = 0; 192 for (PetscInt f=0; f<nfields; f++) { 193 ierr = PetscSectionGetFieldComponents(section, f, &ncomp[f]); CHKERRQ(ierr); 194 fieldoff[f+1] = fieldoff[f] + ncomp[f]; 195 } 196 197 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd); CHKERRQ(ierr); 198 Nelem = cEnd - cStart; 199 ierr = PetscMalloc1(Nelem*PetscPowInt(P, dim), &erestrict); CHKERRQ(ierr); 200 for (c=cStart,eoffset=0; c<cEnd; c++) { 201 PetscInt numindices, *indices, nnodes; 202 ierr = DMPlexGetClosureIndices(dm, section, section, c, &numindices, 203 &indices, NULL); CHKERRQ(ierr); 204 if (numindices % fieldoff[nfields]) SETERRQ1(PETSC_COMM_SELF, 205 PETSC_ERR_ARG_INCOMP, "Number of closure indices not compatible with Cell %D", 206 c); 207 nnodes = numindices / fieldoff[nfields]; 208 for (PetscInt i=0; i<nnodes; i++) { 209 // Check that indices are blocked by node and thus can be coalesced as a single field with 210 // fieldoff[nfields] = sum(ncomp) components. 211 for (PetscInt f=0; f<nfields; f++) { 212 for (PetscInt j=0; j<ncomp[f]; j++) { 213 if (Involute(indices[fieldoff[f]*nnodes + i*ncomp[f] + j]) 214 != Involute(indices[i*ncomp[0]]) + fieldoff[f] + j) 215 SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP, 216 "Cell %D closure indices not interlaced for node %D field %D component %D", 217 c, i, f, j); 218 } 219 } 220 // Essential boundary conditions are encoded as -(loc+1), but we don't care so we decode. 221 PetscInt loc = Involute(indices[i*ncomp[0]]); 222 erestrict[eoffset++] = loc / fieldoff[nfields]; 223 } 224 ierr = DMPlexRestoreClosureIndices(dm, section, section, c, &numindices, 225 &indices, NULL); CHKERRQ(ierr); 226 } 227 if (eoffset != Nelem*PetscPowInt(P, dim)) SETERRQ3(PETSC_COMM_SELF, 228 PETSC_ERR_LIB, "ElemRestriction of size (%D,%D) initialized %D nodes", Nelem, 229 PetscPowInt(P, dim),eoffset); 230 ierr = DMGetLocalVector(dm, &Uloc); CHKERRQ(ierr); 231 ierr = VecGetLocalSize(Uloc, &Ndof); CHKERRQ(ierr); 232 ierr = DMRestoreLocalVector(dm, &Uloc); CHKERRQ(ierr); 233 CeedElemRestrictionCreate(ceed, CEED_INTERLACED, Nelem, PetscPowInt(P, dim), 234 Ndof/fieldoff[nfields], fieldoff[nfields], 235 CEED_MEM_HOST, CEED_COPY_VALUES, erestrict, Erestrict); 236 ierr = PetscFree(erestrict); CHKERRQ(ierr); 237 PetscFunctionReturn(0); 238 } 239 240 static int CreateVectorFromPetscVec(Ceed ceed, Vec p, CeedVector *v) { 241 PetscErrorCode ierr; 242 PetscInt m; 243 244 PetscFunctionBeginUser; 245 ierr = VecGetLocalSize(p, &m); CHKERRQ(ierr); 246 ierr = CeedVectorCreate(ceed, m, v); CHKERRQ(ierr); 247 PetscFunctionReturn(0); 248 } 249 250 static int VectorPlacePetscVec(CeedVector c, Vec p) { 251 PetscErrorCode ierr; 252 PetscInt mceed,mpetsc; 253 PetscScalar *a; 254 255 PetscFunctionBeginUser; 256 ierr = CeedVectorGetLength(c, &mceed); CHKERRQ(ierr); 257 ierr = VecGetLocalSize(p, &mpetsc); CHKERRQ(ierr); 258 if (mceed != mpetsc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP, 259 "Cannot place PETSc Vec of length %D in CeedVector of length %D", 260 mpetsc, mceed); 261 ierr = VecGetArray(p, &a); CHKERRQ(ierr); 262 CeedVectorSetArray(c, CEED_MEM_HOST, CEED_USE_POINTER, a); 263 PetscFunctionReturn(0); 264 } 265 266 static PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm, 267 PetscBool insertEssential, Vec Qloc, PetscReal time, Vec faceGeomFVM, 268 Vec cellGeomFVM, Vec gradFVM) { 269 PetscErrorCode ierr; 270 Vec Qbc; 271 272 PetscFunctionBegin; 273 ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr); 274 ierr = VecAXPY(Qloc, 1., Qbc); CHKERRQ(ierr); 275 ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr); 276 PetscFunctionReturn(0); 277 } 278 279 // This is the RHS of the ODE, given as u_t = G(t,u) 280 // This function takes in a state vector Q and writes into G 281 static PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *userData) { 282 PetscErrorCode ierr; 283 User user = *(User *)userData; 284 PetscScalar *q, *g; 285 Vec Qloc, Gloc; 286 287 // Global-to-local 288 PetscFunctionBeginUser; 289 ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr); 290 ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr); 291 ierr = VecZeroEntries(Qloc); CHKERRQ(ierr); 292 ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr); 293 ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0, 294 NULL, NULL, NULL); CHKERRQ(ierr); 295 ierr = VecZeroEntries(Gloc); CHKERRQ(ierr); 296 297 // Ceed Vectors 298 ierr = VecGetArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr); 299 ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr); 300 CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER, q); 301 CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g); 302 303 // Apply CEED operator 304 CeedOperatorApply(user->op_rhs, user->qceed, user->gceed, 305 CEED_REQUEST_IMMEDIATE); 306 307 // Restore vectors 308 ierr = VecRestoreArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr); 309 ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr); 310 311 ierr = VecZeroEntries(G); CHKERRQ(ierr); 312 ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr); 313 314 // Inverse of the lumped mass matrix 315 ierr = VecPointwiseMult(G, G, user->M); // M is Minv 316 CHKERRQ(ierr); 317 318 ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr); 319 ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr); 320 PetscFunctionReturn(0); 321 } 322 323 static PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Qdot, Vec G, 324 void *userData) { 325 PetscErrorCode ierr; 326 User user = *(User *)userData; 327 const PetscScalar *q, *qdot; 328 PetscScalar *g; 329 Vec Qloc, Qdotloc, Gloc; 330 331 // Global-to-local 332 PetscFunctionBeginUser; 333 ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr); 334 ierr = DMGetLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr); 335 ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr); 336 ierr = VecZeroEntries(Qloc); CHKERRQ(ierr); 337 ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr); 338 ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0, 339 NULL, NULL, NULL); CHKERRQ(ierr); 340 ierr = VecZeroEntries(Qdotloc); CHKERRQ(ierr); 341 ierr = DMGlobalToLocal(user->dm, Qdot, INSERT_VALUES, Qdotloc); CHKERRQ(ierr); 342 ierr = VecZeroEntries(Gloc); CHKERRQ(ierr); 343 344 // Ceed Vectors 345 ierr = VecGetArrayRead(Qloc, &q); CHKERRQ(ierr); 346 ierr = VecGetArrayRead(Qdotloc, &qdot); CHKERRQ(ierr); 347 ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr); 348 CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER, 349 (PetscScalar *)q); 350 CeedVectorSetArray(user->qdotceed, CEED_MEM_HOST, CEED_USE_POINTER, 351 (PetscScalar *)qdot); 352 CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g); 353 354 // Apply CEED operator 355 CeedOperatorApply(user->op_ifunction, user->qceed, user->gceed, 356 CEED_REQUEST_IMMEDIATE); 357 358 // Restore vectors 359 ierr = VecRestoreArrayRead(Qloc, &q); CHKERRQ(ierr); 360 ierr = VecRestoreArrayRead(Qdotloc, &qdot); CHKERRQ(ierr); 361 ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr); 362 363 ierr = VecZeroEntries(G); CHKERRQ(ierr); 364 ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr); 365 366 ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr); 367 ierr = DMRestoreLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr); 368 ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr); 369 PetscFunctionReturn(0); 370 } 371 372 // User provided TS Monitor 373 static PetscErrorCode TSMonitor_NS(TS ts, PetscInt stepno, PetscReal time, 374 Vec Q, void *ctx) { 375 User user = ctx; 376 Vec Qloc; 377 char filepath[PETSC_MAX_PATH_LEN]; 378 PetscViewer viewer; 379 PetscErrorCode ierr; 380 381 // Set up output 382 PetscFunctionBeginUser; 383 // Print every 'outputfreq' steps 384 if (stepno % user->outputfreq != 0) 385 PetscFunctionReturn(0); 386 ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr); 387 ierr = PetscObjectSetName((PetscObject)Qloc, "StateVec"); CHKERRQ(ierr); 388 ierr = VecZeroEntries(Qloc); CHKERRQ(ierr); 389 ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr); 390 391 // Output 392 ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-%03D.vtu", 393 user->outputfolder, stepno + user->contsteps); 394 CHKERRQ(ierr); 395 ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), filepath, 396 FILE_MODE_WRITE, &viewer); CHKERRQ(ierr); 397 ierr = VecView(Qloc, viewer); CHKERRQ(ierr); 398 if (user->dmviz) { 399 Vec Qrefined, Qrefined_loc; 400 char filepath_refined[PETSC_MAX_PATH_LEN]; 401 PetscViewer viewer_refined; 402 403 ierr = DMGetGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr); 404 ierr = DMGetLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr); 405 ierr = PetscObjectSetName((PetscObject)Qrefined_loc, "Refined"); 406 CHKERRQ(ierr); 407 ierr = MatInterpolate(user->interpviz, Q, Qrefined); CHKERRQ(ierr); 408 ierr = VecZeroEntries(Qrefined_loc); CHKERRQ(ierr); 409 ierr = DMGlobalToLocal(user->dmviz, Qrefined, INSERT_VALUES, Qrefined_loc); 410 CHKERRQ(ierr); 411 ierr = PetscSNPrintf(filepath_refined, sizeof filepath_refined, 412 "%s/nsrefined-%03D.vtu", 413 user->outputfolder, stepno + user->contsteps); 414 CHKERRQ(ierr); 415 ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Qrefined), 416 filepath_refined, 417 FILE_MODE_WRITE, &viewer_refined); CHKERRQ(ierr); 418 ierr = VecView(Qrefined_loc, viewer_refined); CHKERRQ(ierr); 419 ierr = DMRestoreLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr); 420 ierr = DMRestoreGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr); 421 ierr = PetscViewerDestroy(&viewer_refined); CHKERRQ(ierr); 422 } 423 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); 424 ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr); 425 426 // Save data in a binary file for continuation of simulations 427 ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin", 428 user->outputfolder); CHKERRQ(ierr); 429 ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer); 430 CHKERRQ(ierr); 431 ierr = VecView(Q, viewer); CHKERRQ(ierr); 432 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); 433 434 // Save time stamp 435 // Dimensionalize time back 436 time /= user->units->second; 437 ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin", 438 user->outputfolder); CHKERRQ(ierr); 439 ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer); 440 CHKERRQ(ierr); 441 #if PETSC_VERSION_GE(3,13,0) 442 ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL); 443 #else 444 ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL, true); 445 #endif 446 CHKERRQ(ierr); 447 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); 448 449 PetscFunctionReturn(0); 450 } 451 452 static PetscErrorCode ICs_FixMultiplicity(CeedOperator op_ics, 453 CeedVector xcorners, CeedVector q0ceed, DM dm, Vec Qloc, Vec Q, 454 CeedElemRestriction restrictq, SetupContext ctxSetup, CeedScalar time) { 455 PetscErrorCode ierr; 456 CeedVector multlvec; 457 Vec Multiplicity, MultiplicityLoc; 458 459 ctxSetup->time = time; 460 ierr = VecZeroEntries(Qloc); CHKERRQ(ierr); 461 ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr); 462 CeedOperatorApply(op_ics, xcorners, q0ceed, CEED_REQUEST_IMMEDIATE); 463 ierr = VecZeroEntries(Q); CHKERRQ(ierr); 464 ierr = DMLocalToGlobal(dm, Qloc, ADD_VALUES, Q); CHKERRQ(ierr); 465 466 // Fix multiplicity for output of ICs 467 ierr = DMGetLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr); 468 CeedElemRestrictionCreateVector(restrictq, &multlvec, NULL); 469 ierr = VectorPlacePetscVec(multlvec, MultiplicityLoc); CHKERRQ(ierr); 470 CeedElemRestrictionGetMultiplicity(restrictq, multlvec); 471 CeedVectorDestroy(&multlvec); 472 ierr = DMGetGlobalVector(dm, &Multiplicity); CHKERRQ(ierr); 473 ierr = VecZeroEntries(Multiplicity); CHKERRQ(ierr); 474 ierr = DMLocalToGlobal(dm, MultiplicityLoc, ADD_VALUES, Multiplicity); 475 CHKERRQ(ierr); 476 ierr = VecPointwiseDivide(Q, Q, Multiplicity); CHKERRQ(ierr); 477 ierr = VecPointwiseDivide(Qloc, Qloc, MultiplicityLoc); CHKERRQ(ierr); 478 ierr = DMRestoreLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr); 479 ierr = DMRestoreGlobalVector(dm, &Multiplicity); CHKERRQ(ierr); 480 481 PetscFunctionReturn(0); 482 } 483 484 static PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm, 485 CeedElemRestriction restrictq, CeedBasis basisq, 486 CeedElemRestriction restrictqdi, CeedVector qdata, Vec M) { 487 PetscErrorCode ierr; 488 CeedQFunction qf_mass; 489 CeedOperator op_mass; 490 CeedVector mceed; 491 Vec Mloc; 492 CeedInt ncompq, qdatasize; 493 494 PetscFunctionBeginUser; 495 CeedElemRestrictionGetNumComponents(restrictq, &ncompq); 496 CeedElemRestrictionGetNumComponents(restrictqdi, &qdatasize); 497 // Create the Q-function that defines the action of the mass operator 498 CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass); 499 CeedQFunctionAddInput(qf_mass, "q", ncompq, CEED_EVAL_INTERP); 500 CeedQFunctionAddInput(qf_mass, "qdata", qdatasize, CEED_EVAL_NONE); 501 CeedQFunctionAddOutput(qf_mass, "v", ncompq, CEED_EVAL_INTERP); 502 503 // Create the mass operator 504 CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass); 505 CeedOperatorSetField(op_mass, "q", restrictq, basisq, CEED_VECTOR_ACTIVE); 506 CeedOperatorSetField(op_mass, "qdata", restrictqdi, 507 CEED_BASIS_COLLOCATED, qdata); 508 CeedOperatorSetField(op_mass, "v", restrictq, basisq, CEED_VECTOR_ACTIVE); 509 510 ierr = DMGetLocalVector(dm, &Mloc); CHKERRQ(ierr); 511 ierr = VecZeroEntries(Mloc); CHKERRQ(ierr); 512 CeedElemRestrictionCreateVector(restrictq, &mceed, NULL); 513 ierr = VectorPlacePetscVec(mceed, Mloc); CHKERRQ(ierr); 514 515 { 516 // Compute a lumped mass matrix 517 CeedVector onesvec; 518 CeedElemRestrictionCreateVector(restrictq, &onesvec, NULL); 519 CeedVectorSetValue(onesvec, 1.0); 520 CeedOperatorApply(op_mass, onesvec, mceed, CEED_REQUEST_IMMEDIATE); 521 CeedVectorDestroy(&onesvec); 522 CeedOperatorDestroy(&op_mass); 523 CeedVectorDestroy(&mceed); 524 } 525 CeedQFunctionDestroy(&qf_mass); 526 527 ierr = VecZeroEntries(M); CHKERRQ(ierr); 528 ierr = DMLocalToGlobal(dm, Mloc, ADD_VALUES, M); CHKERRQ(ierr); 529 ierr = DMRestoreLocalVector(dm, &Mloc); CHKERRQ(ierr); 530 531 // Invert diagonally lumped mass vector for RHS function 532 ierr = VecReciprocal(M); CHKERRQ(ierr); 533 PetscFunctionReturn(0); 534 } 535 536 PetscErrorCode SetUpDM(DM dm, problemData *problem, PetscInt degree, 537 SimpleBC bc, void *ctxSetup) { 538 PetscErrorCode ierr; 539 540 PetscFunctionBeginUser; 541 { 542 // Configure the finite element space and boundary conditions 543 PetscFE fe; 544 PetscSpace fespace; 545 PetscInt ncompq = 5; 546 ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, ncompq, 547 PETSC_FALSE, degree, PETSC_DECIDE, 548 &fe); 549 ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr); 550 ierr = DMAddField(dm,NULL,(PetscObject)fe); CHKERRQ(ierr); 551 ierr = DMCreateDS(dm); CHKERRQ(ierr); 552 // Wall boundary conditions are zero energy density and zero flux for 553 // velocity in advection/advection2d, and zero velocity and zero flux 554 // for mass density and energy density in density_current 555 { 556 if (problem->bc == Exact_Advection || problem->bc == Exact_Advection2d) { 557 PetscInt comps[1] = {4}; 558 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0, 559 1, comps, (void(*)(void))problem->bc, 560 bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr); 561 } else if (problem->bc == Exact_DC) { 562 PetscInt comps[3] = {1, 2, 3}; 563 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0, 564 3, comps, (void(*)(void))problem->bc, 565 bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr); 566 } 567 } 568 { 569 PetscInt comps[1] = {1}; 570 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", "Face Sets", 0, 571 1, comps, (void(*)(void))NULL, bc->nslip[0], 572 bc->slips[0], ctxSetup); CHKERRQ(ierr); 573 comps[0] = 2; 574 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", "Face Sets", 0, 575 1, comps, (void(*)(void))NULL, bc->nslip[1], 576 bc->slips[1], ctxSetup); CHKERRQ(ierr); 577 comps[0] = 3; 578 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", "Face Sets", 0, 579 1, comps, (void(*)(void))NULL, bc->nslip[2], 580 bc->slips[2], ctxSetup); CHKERRQ(ierr); 581 } 582 ierr = DMPlexSetClosurePermutationTensor(dm,PETSC_DETERMINE,NULL); 583 CHKERRQ(ierr); 584 ierr = PetscFEGetBasisSpace(fe, &fespace); CHKERRQ(ierr); 585 ierr = PetscFEDestroy(&fe); CHKERRQ(ierr); 586 } 587 { 588 // Empty name for conserved field (because there is only one field) 589 PetscSection section; 590 ierr = DMGetLocalSection(dm, §ion); CHKERRQ(ierr); 591 ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr); 592 ierr = PetscSectionSetComponentName(section, 0, 0, "Density"); 593 CHKERRQ(ierr); 594 ierr = PetscSectionSetComponentName(section, 0, 1, "MomentumX"); 595 CHKERRQ(ierr); 596 ierr = PetscSectionSetComponentName(section, 0, 2, "MomentumY"); 597 CHKERRQ(ierr); 598 ierr = PetscSectionSetComponentName(section, 0, 3, "MomentumZ"); 599 CHKERRQ(ierr); 600 ierr = PetscSectionSetComponentName(section, 0, 4, "EnergyDensity"); 601 CHKERRQ(ierr); 602 } 603 PetscFunctionReturn(0); 604 } 605 606 int main(int argc, char **argv) { 607 PetscInt ierr; 608 MPI_Comm comm; 609 DM dm, dmcoord, dmviz; 610 Mat interpviz; 611 TS ts; 612 TSAdapt adapt; 613 User user; 614 Units units; 615 char ceedresource[4096] = "/cpu/self"; 616 PetscInt cStart, cEnd, localNelem, lnodes, steps; 617 const PetscInt ncompq = 5; 618 PetscMPIInt rank; 619 PetscScalar ftime; 620 Vec Q, Qloc, Xloc; 621 Ceed ceed; 622 CeedInt numP, numQ; 623 CeedVector xcorners, qdata, q0ceed; 624 CeedBasis basisx, basisxc, basisq; 625 CeedElemRestriction restrictx, restrictxcoord, restrictq, restrictqdi; 626 CeedQFunction qf_setup, qf_ics, qf_rhs, qf_ifunction; 627 CeedOperator op_setup, op_ics; 628 CeedScalar Rd; 629 PetscScalar WpermK, Pascal, JperkgK, mpersquareds, kgpercubicm, 630 kgpersquaredms, Joulepercubicm; 631 problemType problemChoice; 632 problemData *problem = NULL; 633 StabilizationType stab; 634 PetscBool test, implicit; 635 PetscInt viz_refine = 0; 636 struct SimpleBC_ bc = { 637 .nwall = 6, 638 .walls = {1,2,3,4,5,6}, 639 }; 640 double start, cpu_time_used; 641 642 // Create the libCEED contexts 643 PetscScalar meter = 1e-2; // 1 meter in scaled length units 644 PetscScalar second = 1e-2; // 1 second in scaled time units 645 PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units 646 PetscScalar Kelvin = 1; // 1 Kelvin in scaled temperature units 647 CeedScalar theta0 = 300.; // K 648 CeedScalar thetaC = -15.; // K 649 CeedScalar P0 = 1.e5; // Pa 650 CeedScalar N = 0.01; // 1/s 651 CeedScalar cv = 717.; // J/(kg K) 652 CeedScalar cp = 1004.; // J/(kg K) 653 CeedScalar g = 9.81; // m/s^2 654 CeedScalar lambda = -2./3.; // - 655 CeedScalar mu = 75.; // Pa s, dynamic viscosity 656 // mu = 75 is not physical for air, but is good for numerical stability 657 CeedScalar k = 0.02638; // W/(m K) 658 CeedScalar CtauS = 0.; // dimensionless 659 CeedScalar strong_form = 0.; // [0,1] 660 PetscScalar lx = 8000.; // m 661 PetscScalar ly = 8000.; // m 662 PetscScalar lz = 4000.; // m 663 CeedScalar rc = 1000.; // m (Radius of bubble) 664 PetscScalar resx = 1000.; // m (resolution in x) 665 PetscScalar resy = 1000.; // m (resolution in y) 666 PetscScalar resz = 1000.; // m (resolution in z) 667 PetscInt outputfreq = 10; // - 668 PetscInt contsteps = 0; // - 669 PetscInt degree = 1; // - 670 PetscInt qextra = 2; // - 671 PetscReal center[3], dc_axis[3] = {0, 0, 0}; 672 673 ierr = PetscInitialize(&argc, &argv, NULL, help); 674 if (ierr) return ierr; 675 676 // Allocate PETSc context 677 ierr = PetscCalloc1(1, &user); CHKERRQ(ierr); 678 ierr = PetscMalloc1(1, &units); CHKERRQ(ierr); 679 680 // Parse command line options 681 comm = PETSC_COMM_WORLD; 682 ierr = PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED", 683 NULL); CHKERRQ(ierr); 684 ierr = PetscOptionsString("-ceed", "CEED resource specifier", 685 NULL, ceedresource, ceedresource, 686 sizeof(ceedresource), NULL); CHKERRQ(ierr); 687 ierr = PetscOptionsBool("-test", "Run in test mode", 688 NULL, test=PETSC_FALSE, &test, NULL); CHKERRQ(ierr); 689 problemChoice = NS_DENSITY_CURRENT; 690 ierr = PetscOptionsEnum("-problem", "Problem to solve", NULL, 691 problemTypes, (PetscEnum)problemChoice, 692 (PetscEnum *)&problemChoice, NULL); CHKERRQ(ierr); 693 problem = &problemOptions[problemChoice]; 694 ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL, 695 StabilizationTypes, (PetscEnum)(stab = STAB_NONE), 696 (PetscEnum *)&stab, NULL); CHKERRQ(ierr); 697 ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", 698 NULL, implicit=PETSC_FALSE, &implicit, NULL); 699 CHKERRQ(ierr); 700 if (!implicit && stab != STAB_NONE) { 701 ierr = PetscPrintf(comm, "Warning! Use -stab only with -implicit\n"); 702 CHKERRQ(ierr); 703 } 704 { 705 PetscInt len; 706 PetscBool flg; 707 ierr = PetscOptionsIntArray("-bc_wall", 708 "Use wall boundary conditions on this list of faces", 709 NULL, bc.walls, 710 (len = sizeof(bc.walls) / sizeof(bc.walls[0]), 711 &len), &flg); CHKERRQ(ierr); 712 if (flg) bc.nwall = len; 713 for (PetscInt j=0; j<3; j++) { 714 const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"}; 715 ierr = PetscOptionsIntArray(flags[j], 716 "Use slip boundary conditions on this list of faces", 717 NULL, bc.slips[j], 718 (len = sizeof(bc.slips[j]) / sizeof(bc.slips[j][0]), 719 &len), &flg); 720 CHKERRQ(ierr); 721 if (flg) bc.nslip[j] = len; 722 } 723 } 724 ierr = PetscOptionsInt("-viz_refine", 725 "Regular refinement levels for visualization", 726 NULL, viz_refine, &viz_refine, NULL); 727 CHKERRQ(ierr); 728 ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units", 729 NULL, meter, &meter, NULL); CHKERRQ(ierr); 730 meter = fabs(meter); 731 ierr = PetscOptionsScalar("-units_second","1 second in scaled time units", 732 NULL, second, &second, NULL); CHKERRQ(ierr); 733 second = fabs(second); 734 ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units", 735 NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr); 736 kilogram = fabs(kilogram); 737 ierr = PetscOptionsScalar("-units_Kelvin", 738 "1 Kelvin in scaled temperature units", 739 NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr); 740 Kelvin = fabs(Kelvin); 741 ierr = PetscOptionsScalar("-theta0", "Reference potential temperature", 742 NULL, theta0, &theta0, NULL); CHKERRQ(ierr); 743 ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature", 744 NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr); 745 ierr = PetscOptionsScalar("-P0", "Atmospheric pressure", 746 NULL, P0, &P0, NULL); CHKERRQ(ierr); 747 ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency", 748 NULL, N, &N, NULL); CHKERRQ(ierr); 749 ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume", 750 NULL, cv, &cv, NULL); CHKERRQ(ierr); 751 ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure", 752 NULL, cp, &cp, NULL); CHKERRQ(ierr); 753 ierr = PetscOptionsScalar("-g", "Gravitational acceleration", 754 NULL, g, &g, NULL); CHKERRQ(ierr); 755 ierr = PetscOptionsScalar("-lambda", 756 "Stokes hypothesis second viscosity coefficient", 757 NULL, lambda, &lambda, NULL); CHKERRQ(ierr); 758 ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", 759 NULL, mu, &mu, NULL); CHKERRQ(ierr); 760 ierr = PetscOptionsScalar("-k", "Thermal conductivity", 761 NULL, k, &k, NULL); CHKERRQ(ierr); 762 ierr = PetscOptionsScalar("-CtauS", 763 "Scale coefficient for tau (nondimensional)", 764 NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr); 765 if (stab == STAB_NONE && CtauS != 0) { 766 ierr = PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"); 767 CHKERRQ(ierr); 768 } 769 ierr = PetscOptionsScalar("-strong_form", 770 "Strong (1) or weak/integrated by parts (0) advection residual", 771 NULL, strong_form, &strong_form, NULL); 772 CHKERRQ(ierr); 773 ierr = PetscOptionsScalar("-lx", "Length scale in x direction", 774 NULL, lx, &lx, NULL); CHKERRQ(ierr); 775 ierr = PetscOptionsScalar("-ly", "Length scale in y direction", 776 NULL, ly, &ly, NULL); CHKERRQ(ierr); 777 ierr = PetscOptionsScalar("-lz", "Length scale in z direction", 778 NULL, lz, &lz, NULL); CHKERRQ(ierr); 779 ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", 780 NULL, rc, &rc, NULL); CHKERRQ(ierr); 781 ierr = PetscOptionsScalar("-resx","Target resolution in x", 782 NULL, resx, &resx, NULL); CHKERRQ(ierr); 783 ierr = PetscOptionsScalar("-resy","Target resolution in y", 784 NULL, resy, &resy, NULL); CHKERRQ(ierr); 785 ierr = PetscOptionsScalar("-resz","Target resolution in z", 786 NULL, resz, &resz, NULL); CHKERRQ(ierr); 787 PetscInt n = problem->dim; 788 center[0] = 0.5 * lx; 789 center[1] = 0.5 * ly; 790 center[2] = 0.5 * lz; 791 ierr = PetscOptionsRealArray("-center", "Location of bubble center", 792 NULL, center, &n, NULL); CHKERRQ(ierr); 793 n = problem->dim; 794 ierr = PetscOptionsRealArray("-dc_axis", 795 "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric", 796 NULL, dc_axis, &n, NULL); CHKERRQ(ierr); 797 { 798 PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + 799 PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2])); 800 if (norm > 0) { 801 for (int i=0; i<3; i++) dc_axis[i] /= norm; 802 } 803 } 804 ierr = PetscOptionsInt("-output_freq", 805 "Frequency of output, in number of steps", 806 NULL, outputfreq, &outputfreq, NULL); CHKERRQ(ierr); 807 ierr = PetscOptionsInt("-continue", "Continue from previous solution", 808 NULL, contsteps, &contsteps, NULL); CHKERRQ(ierr); 809 ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements", 810 NULL, degree, °ree, NULL); CHKERRQ(ierr); 811 ierr = PetscOptionsInt("-qextra", "Number of extra quadrature points", 812 NULL, qextra, &qextra, NULL); CHKERRQ(ierr); 813 PetscStrncpy(user->outputfolder, ".", 2); 814 ierr = PetscOptionsString("-of", "Output folder", 815 NULL, user->outputfolder, user->outputfolder, 816 sizeof(user->outputfolder), NULL); CHKERRQ(ierr); 817 ierr = PetscOptionsEnd(); CHKERRQ(ierr); 818 819 // Define derived units 820 Pascal = kilogram / (meter * PetscSqr(second)); 821 JperkgK = PetscSqr(meter) / (PetscSqr(second) * Kelvin); 822 mpersquareds = meter / PetscSqr(second); 823 WpermK = kilogram * meter / (pow(second,3) * Kelvin); 824 kgpercubicm = kilogram / pow(meter,3); 825 kgpersquaredms = kilogram / (PetscSqr(meter) * second); 826 Joulepercubicm = kilogram / (meter * PetscSqr(second)); 827 828 // Scale variables to desired units 829 theta0 *= Kelvin; 830 thetaC *= Kelvin; 831 P0 *= Pascal; 832 N *= (1./second); 833 cv *= JperkgK; 834 cp *= JperkgK; 835 Rd = cp - cv; 836 g *= mpersquareds; 837 mu *= Pascal * second; 838 k *= WpermK; 839 lx = fabs(lx) * meter; 840 ly = fabs(ly) * meter; 841 lz = fabs(lz) * meter; 842 rc = fabs(rc) * meter; 843 resx = fabs(resx) * meter; 844 resy = fabs(resy) * meter; 845 resz = fabs(resz) * meter; 846 for (int i=0; i<3; i++) center[i] *= meter; 847 848 const CeedInt dim = problem->dim, ncompx = problem->dim, 849 qdatasize = problem->qdatasize; 850 // Set up the libCEED context 851 struct SetupContext_ ctxSetup = { 852 .theta0 = theta0, 853 .thetaC = thetaC, 854 .P0 = P0, 855 .N = N, 856 .cv = cv, 857 .cp = cp, 858 .Rd = Rd, 859 .g = g, 860 .rc = rc, 861 .lx = lx, 862 .ly = ly, 863 .lz = lz, 864 .center[0] = center[0], 865 .center[1] = center[1], 866 .center[2] = center[2], 867 .dc_axis[0] = dc_axis[0], 868 .dc_axis[1] = dc_axis[1], 869 .dc_axis[2] = dc_axis[2], 870 .time = 0, 871 }; 872 873 // Create the mesh 874 { 875 const PetscReal scale[3] = {lx, ly, lz}; 876 ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_FALSE, NULL, NULL, scale, 877 NULL, PETSC_TRUE, &dm); 878 CHKERRQ(ierr); 879 } 880 881 // Distribute the mesh over processes 882 { 883 DM dmDist = NULL; 884 PetscPartitioner part; 885 886 ierr = DMPlexGetPartitioner(dm, &part); CHKERRQ(ierr); 887 ierr = PetscPartitionerSetFromOptions(part); CHKERRQ(ierr); 888 ierr = DMPlexDistribute(dm, 0, NULL, &dmDist); CHKERRQ(ierr); 889 if (dmDist) { 890 ierr = DMDestroy(&dm); CHKERRQ(ierr); 891 dm = dmDist; 892 } 893 } 894 ierr = DMViewFromOptions(dm, NULL, "-dm_view"); CHKERRQ(ierr); 895 896 // Setup DM 897 ierr = DMLocalizeCoordinates(dm); CHKERRQ(ierr); 898 ierr = DMSetFromOptions(dm); CHKERRQ(ierr); 899 ierr = SetUpDM(dm, problem, degree, &bc, &ctxSetup); CHKERRQ(ierr); 900 901 // Print FEM space information 902 if (!test) { 903 ierr = PetscPrintf(PETSC_COMM_WORLD, 904 "Degree of FEM space: %D\n", 905 degree); CHKERRQ(ierr); 906 } 907 908 // Refine DM for high-order viz 909 dmviz = NULL; 910 interpviz = NULL; 911 if (viz_refine) { 912 DM dmhierarchy[viz_refine+1]; 913 914 ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr); 915 dmhierarchy[0] = dm; 916 for (PetscInt i = 0, d = degree; i < viz_refine; i++) { 917 Mat interp_next; 918 919 ierr = DMRefine(dmhierarchy[i], MPI_COMM_NULL, &dmhierarchy[i+1]); 920 CHKERRQ(ierr); 921 ierr = DMSetCoarseDM(dmhierarchy[i+1], dmhierarchy[i]); CHKERRQ(ierr); 922 d = (d + 1) / 2; 923 if (i + 1 == viz_refine) d = 1; 924 ierr = SetUpDM(dmhierarchy[i+1], problem, d, &bc, &ctxSetup); CHKERRQ(ierr); 925 ierr = DMCreateInterpolation(dmhierarchy[i], dmhierarchy[i+1], 926 &interp_next, NULL); CHKERRQ(ierr); 927 if (!i) interpviz = interp_next; 928 else { 929 Mat C; 930 ierr = MatMatMult(interp_next, interpviz, MAT_INITIAL_MATRIX, 931 PETSC_DECIDE, &C); CHKERRQ(ierr); 932 ierr = MatDestroy(&interp_next); CHKERRQ(ierr); 933 ierr = MatDestroy(&interpviz); CHKERRQ(ierr); 934 interpviz = C; 935 } 936 } 937 for (PetscInt i=1; i<viz_refine; i++) { 938 ierr = DMDestroy(&dmhierarchy[i]); CHKERRQ(ierr); 939 } 940 dmviz = dmhierarchy[viz_refine]; 941 } 942 ierr = DMCreateGlobalVector(dm, &Q); CHKERRQ(ierr); 943 ierr = DMGetLocalVector(dm, &Qloc); CHKERRQ(ierr); 944 ierr = VecGetSize(Qloc, &lnodes); CHKERRQ(ierr); 945 lnodes /= ncompq; 946 947 { 948 // Print grid information 949 CeedInt gdofs, odofs; 950 int comm_size; 951 char box_faces_str[PETSC_MAX_PATH_LEN] = "NONE"; 952 ierr = VecGetSize(Q, &gdofs); CHKERRQ(ierr); 953 ierr = VecGetLocalSize(Q, &odofs); CHKERRQ(ierr); 954 ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr); 955 ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str, 956 sizeof(box_faces_str), NULL); CHKERRQ(ierr); 957 if (!test) { 958 ierr = PetscPrintf(comm, "Global FEM dofs: %D (%D owned) on %d rank(s)\n", 959 gdofs, odofs, comm_size); CHKERRQ(ierr); 960 ierr = PetscPrintf(comm, "Local FEM nodes: %D\n", lnodes); CHKERRQ(ierr); 961 ierr = PetscPrintf(comm, "dm_plex_box_faces: %s\n", box_faces_str); 962 CHKERRQ(ierr); 963 } 964 965 } 966 967 // Set up global mass vector 968 ierr = VecDuplicate(Q, &user->M); CHKERRQ(ierr); 969 970 // Set up CEED 971 // CEED Bases 972 CeedInit(ceedresource, &ceed); 973 numP = degree + 1; 974 numQ = numP + qextra; 975 CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompq, numP, numQ, CEED_GAUSS, 976 &basisq); 977 CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numQ, CEED_GAUSS, 978 &basisx); 979 CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numP, 980 CEED_GAUSS_LOBATTO, &basisxc); 981 982 ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr); 983 ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL); 984 CHKERRQ(ierr); 985 986 // CEED Restrictions 987 ierr = CreateRestrictionFromPlex(ceed, dm, degree+1, &restrictq); 988 CHKERRQ(ierr); 989 ierr = CreateRestrictionFromPlex(ceed, dmcoord, 2, &restrictx); CHKERRQ(ierr); 990 DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd); CHKERRQ(ierr); 991 localNelem = cEnd - cStart; 992 CeedInt numQdim = CeedIntPow(numQ, dim); 993 CeedElemRestrictionCreateStrided(ceed, localNelem, numQdim, 994 localNelem*numQdim, qdatasize, 995 CEED_STRIDES_BACKEND, &restrictqdi); 996 CeedElemRestrictionCreateStrided(ceed, localNelem, PetscPowInt(numP, dim), 997 localNelem*PetscPowInt(numP, dim), ncompx, 998 CEED_STRIDES_BACKEND, &restrictxcoord); 999 1000 ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr); 1001 ierr = CreateVectorFromPetscVec(ceed, Xloc, &xcorners); CHKERRQ(ierr); 1002 1003 // Create the CEED vectors that will be needed in setup 1004 CeedInt Nqpts; 1005 CeedBasisGetNumQuadraturePoints(basisq, &Nqpts); 1006 CeedVectorCreate(ceed, qdatasize*localNelem*Nqpts, &qdata); 1007 CeedElemRestrictionCreateVector(restrictq, &q0ceed, NULL); 1008 1009 // Create the Q-function that builds the quadrature data for the NS operator 1010 CeedQFunctionCreateInterior(ceed, 1, problem->setup, problem->setup_loc, 1011 &qf_setup); 1012 CeedQFunctionAddInput(qf_setup, "dx", ncompx*dim, CEED_EVAL_GRAD); 1013 CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 1014 CeedQFunctionAddOutput(qf_setup, "qdata", qdatasize, CEED_EVAL_NONE); 1015 1016 // Create the Q-function that sets the ICs of the operator 1017 CeedQFunctionCreateInterior(ceed, 1, problem->ics, problem->ics_loc, &qf_ics); 1018 CeedQFunctionAddInput(qf_ics, "x", ncompx, CEED_EVAL_INTERP); 1019 CeedQFunctionAddOutput(qf_ics, "q0", ncompq, CEED_EVAL_NONE); 1020 1021 qf_rhs = NULL; 1022 if (problem->apply_rhs) { // Create the Q-function that defines the action of the RHS operator 1023 CeedQFunctionCreateInterior(ceed, 1, problem->apply_rhs, 1024 problem->apply_rhs_loc, &qf_rhs); 1025 CeedQFunctionAddInput(qf_rhs, "q", ncompq, CEED_EVAL_INTERP); 1026 CeedQFunctionAddInput(qf_rhs, "dq", ncompq*dim, CEED_EVAL_GRAD); 1027 CeedQFunctionAddInput(qf_rhs, "qdata", qdatasize, CEED_EVAL_NONE); 1028 CeedQFunctionAddInput(qf_rhs, "x", ncompx, CEED_EVAL_INTERP); 1029 CeedQFunctionAddOutput(qf_rhs, "v", ncompq, CEED_EVAL_INTERP); 1030 CeedQFunctionAddOutput(qf_rhs, "dv", ncompq*dim, CEED_EVAL_GRAD); 1031 } 1032 1033 qf_ifunction = NULL; 1034 if (problem->apply_ifunction) { // Create the Q-function that defines the action of the IFunction 1035 CeedQFunctionCreateInterior(ceed, 1, problem->apply_ifunction, 1036 problem->apply_ifunction_loc, &qf_ifunction); 1037 CeedQFunctionAddInput(qf_ifunction, "q", ncompq, CEED_EVAL_INTERP); 1038 CeedQFunctionAddInput(qf_ifunction, "dq", ncompq*dim, CEED_EVAL_GRAD); 1039 CeedQFunctionAddInput(qf_ifunction, "qdot", ncompq, CEED_EVAL_INTERP); 1040 CeedQFunctionAddInput(qf_ifunction, "qdata", qdatasize, CEED_EVAL_NONE); 1041 CeedQFunctionAddInput(qf_ifunction, "x", ncompx, CEED_EVAL_INTERP); 1042 CeedQFunctionAddOutput(qf_ifunction, "v", ncompq, CEED_EVAL_INTERP); 1043 CeedQFunctionAddOutput(qf_ifunction, "dv", ncompq*dim, CEED_EVAL_GRAD); 1044 } 1045 1046 // Create the operator that builds the quadrature data for the NS operator 1047 CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup); 1048 CeedOperatorSetField(op_setup, "dx", restrictx, basisx, CEED_VECTOR_ACTIVE); 1049 CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, 1050 basisx, CEED_VECTOR_NONE); 1051 CeedOperatorSetField(op_setup, "qdata", restrictqdi, 1052 CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 1053 1054 // Create the operator that sets the ICs 1055 CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics); 1056 CeedOperatorSetField(op_ics, "x", restrictx, basisxc, CEED_VECTOR_ACTIVE); 1057 CeedOperatorSetField(op_ics, "q0", restrictq, 1058 CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 1059 1060 CeedElemRestrictionCreateVector(restrictq, &user->qceed, NULL); 1061 CeedElemRestrictionCreateVector(restrictq, &user->qdotceed, NULL); 1062 CeedElemRestrictionCreateVector(restrictq, &user->gceed, NULL); 1063 1064 if (qf_rhs) { // Create the RHS physics operator 1065 CeedOperator op; 1066 CeedOperatorCreate(ceed, qf_rhs, NULL, NULL, &op); 1067 CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE); 1068 CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE); 1069 CeedOperatorSetField(op, "qdata", restrictqdi, 1070 CEED_BASIS_COLLOCATED, qdata); 1071 CeedOperatorSetField(op, "x", restrictx, basisx, xcorners); 1072 CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE); 1073 CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE); 1074 user->op_rhs = op; 1075 } 1076 1077 if (qf_ifunction) { // Create the IFunction operator 1078 CeedOperator op; 1079 CeedOperatorCreate(ceed, qf_ifunction, NULL, NULL, &op); 1080 CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE); 1081 CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE); 1082 CeedOperatorSetField(op, "qdot", restrictq, basisq, user->qdotceed); 1083 CeedOperatorSetField(op, "qdata", restrictqdi, 1084 CEED_BASIS_COLLOCATED, qdata); 1085 CeedOperatorSetField(op, "x", restrictx, basisx, xcorners); 1086 CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE); 1087 CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE); 1088 user->op_ifunction = op; 1089 } 1090 1091 CeedQFunctionSetContext(qf_ics, &ctxSetup, sizeof ctxSetup); 1092 CeedScalar ctxNS[8] = {lambda, mu, k, cv, cp, g, Rd}; 1093 struct Advection2dContext_ ctxAdvection2d = { 1094 .CtauS = CtauS, 1095 .strong_form = strong_form, 1096 .stabilization = stab, 1097 }; 1098 switch (problemChoice) { 1099 case NS_DENSITY_CURRENT: 1100 if (qf_rhs) CeedQFunctionSetContext(qf_rhs, &ctxNS, sizeof ctxNS); 1101 if (qf_ifunction) CeedQFunctionSetContext(qf_ifunction, &ctxNS, 1102 sizeof ctxNS); 1103 break; 1104 case NS_ADVECTION: 1105 case NS_ADVECTION2D: 1106 if (qf_rhs) CeedQFunctionSetContext(qf_rhs, &ctxAdvection2d, 1107 sizeof ctxAdvection2d); 1108 if (qf_ifunction) CeedQFunctionSetContext(qf_ifunction, &ctxAdvection2d, 1109 sizeof ctxAdvection2d); 1110 } 1111 1112 // Set up PETSc context 1113 // Set up units structure 1114 units->meter = meter; 1115 units->kilogram = kilogram; 1116 units->second = second; 1117 units->Kelvin = Kelvin; 1118 units->Pascal = Pascal; 1119 units->JperkgK = JperkgK; 1120 units->mpersquareds = mpersquareds; 1121 units->WpermK = WpermK; 1122 units->kgpercubicm = kgpercubicm; 1123 units->kgpersquaredms = kgpersquaredms; 1124 units->Joulepercubicm = Joulepercubicm; 1125 1126 // Set up user structure 1127 user->comm = comm; 1128 user->outputfreq = outputfreq; 1129 user->contsteps = contsteps; 1130 user->units = units; 1131 user->dm = dm; 1132 user->dmviz = dmviz; 1133 user->interpviz = interpviz; 1134 user->ceed = ceed; 1135 1136 // Calculate qdata and ICs 1137 // Set up state global and local vectors 1138 ierr = VecZeroEntries(Q); CHKERRQ(ierr); 1139 1140 ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr); 1141 1142 // Apply Setup Ceed Operators 1143 ierr = VectorPlacePetscVec(xcorners, Xloc); CHKERRQ(ierr); 1144 CeedOperatorApply(op_setup, xcorners, qdata, CEED_REQUEST_IMMEDIATE); 1145 ierr = ComputeLumpedMassMatrix(ceed, dm, restrictq, basisq, restrictqdi, qdata, 1146 user->M); CHKERRQ(ierr); 1147 1148 ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qloc, Q, restrictq, 1149 &ctxSetup, 0.0); 1150 if (1) { // Record boundary values from initial condition and override DMPlexInsertBoundaryValues() 1151 // We use this for the main simulation DM because the reference DMPlexInsertBoundaryValues() is very slow. If we 1152 // disable this, we should still get the same results due to the problem->bc function, but with potentially much 1153 // slower execution. 1154 Vec Qbc; 1155 ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr); 1156 ierr = VecCopy(Qloc, Qbc); CHKERRQ(ierr); 1157 ierr = VecZeroEntries(Qloc); CHKERRQ(ierr); 1158 ierr = DMGlobalToLocal(dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr); 1159 ierr = VecAXPY(Qbc, -1., Qloc); CHKERRQ(ierr); 1160 ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr); 1161 ierr = PetscObjectComposeFunction((PetscObject)dm, 1162 "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS); 1163 CHKERRQ(ierr); 1164 } 1165 1166 MPI_Comm_rank(comm, &rank); 1167 if (!rank) {ierr = PetscMkdir(user->outputfolder); CHKERRQ(ierr);} 1168 // Gather initial Q values 1169 // In case of continuation of simulation, set up initial values from binary file 1170 if (contsteps) { // continue from existent solution 1171 PetscViewer viewer; 1172 char filepath[PETSC_MAX_PATH_LEN]; 1173 // Read input 1174 ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin", 1175 user->outputfolder); 1176 CHKERRQ(ierr); 1177 ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer); 1178 CHKERRQ(ierr); 1179 ierr = VecLoad(Q, viewer); CHKERRQ(ierr); 1180 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); 1181 } 1182 ierr = DMRestoreLocalVector(dm, &Qloc); CHKERRQ(ierr); 1183 1184 // Create and setup TS 1185 ierr = TSCreate(comm, &ts); CHKERRQ(ierr); 1186 ierr = TSSetDM(ts, dm); CHKERRQ(ierr); 1187 if (implicit) { 1188 ierr = TSSetType(ts, TSBDF); CHKERRQ(ierr); 1189 if (user->op_ifunction) { 1190 ierr = TSSetIFunction(ts, NULL, IFunction_NS, &user); CHKERRQ(ierr); 1191 } else { // Implicit integrators can fall back to using an RHSFunction 1192 ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr); 1193 } 1194 } else { 1195 if (!user->op_rhs) SETERRQ(comm,PETSC_ERR_ARG_NULL, 1196 "Problem does not provide RHSFunction"); 1197 ierr = TSSetType(ts, TSRK); CHKERRQ(ierr); 1198 ierr = TSRKSetType(ts, TSRK5F); CHKERRQ(ierr); 1199 ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr); 1200 } 1201 ierr = TSSetMaxTime(ts, 500. * units->second); CHKERRQ(ierr); 1202 ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER); CHKERRQ(ierr); 1203 ierr = TSSetTimeStep(ts, 1.e-2 * units->second); CHKERRQ(ierr); 1204 if (test) {ierr = TSSetMaxSteps(ts, 1); CHKERRQ(ierr);} 1205 ierr = TSGetAdapt(ts, &adapt); CHKERRQ(ierr); 1206 ierr = TSAdaptSetStepLimits(adapt, 1207 1.e-12 * units->second, 1208 1.e2 * units->second); CHKERRQ(ierr); 1209 ierr = TSSetFromOptions(ts); CHKERRQ(ierr); 1210 if (!contsteps) { // print initial condition 1211 if (!test) { 1212 ierr = TSMonitor_NS(ts, 0, 0., Q, user); CHKERRQ(ierr); 1213 } 1214 } else { // continue from time of last output 1215 PetscReal time; 1216 PetscInt count; 1217 PetscViewer viewer; 1218 char filepath[PETSC_MAX_PATH_LEN]; 1219 ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin", 1220 user->outputfolder); CHKERRQ(ierr); 1221 ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer); 1222 CHKERRQ(ierr); 1223 ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL); 1224 CHKERRQ(ierr); 1225 ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); 1226 ierr = TSSetTime(ts, time * user->units->second); CHKERRQ(ierr); 1227 } 1228 if (!test) { 1229 ierr = TSMonitorSet(ts, TSMonitor_NS, user, NULL); CHKERRQ(ierr); 1230 } 1231 1232 // Solve 1233 start = MPI_Wtime(); 1234 ierr = PetscBarrier((PetscObject)ts); CHKERRQ(ierr); 1235 ierr = TSSolve(ts, Q); CHKERRQ(ierr); 1236 cpu_time_used = MPI_Wtime() - start; 1237 ierr = TSGetSolveTime(ts, &ftime); CHKERRQ(ierr); 1238 ierr = MPI_Allreduce(MPI_IN_PLACE, &cpu_time_used, 1, MPI_DOUBLE, MPI_MIN, 1239 comm); CHKERRQ(ierr); 1240 if (!test) { 1241 ierr = PetscPrintf(PETSC_COMM_WORLD, 1242 "Time taken for solution (sec): %g\n", 1243 (double)cpu_time_used); CHKERRQ(ierr); 1244 } 1245 1246 // Get error 1247 if (problem->non_zero_time && !test) { 1248 Vec Qexact, Qexactloc; 1249 PetscReal norm; 1250 ierr = DMCreateGlobalVector(dm, &Qexact); CHKERRQ(ierr); 1251 ierr = DMGetLocalVector(dm, &Qexactloc); CHKERRQ(ierr); 1252 ierr = VecGetSize(Qexactloc, &lnodes); CHKERRQ(ierr); 1253 1254 ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qexactloc, Qexact, 1255 restrictq, &ctxSetup, ftime); CHKERRQ(ierr); 1256 1257 ierr = VecAXPY(Q, -1.0, Qexact); CHKERRQ(ierr); 1258 ierr = VecNorm(Q, NORM_MAX, &norm); CHKERRQ(ierr); 1259 CeedVectorDestroy(&q0ceed); 1260 ierr = PetscPrintf(PETSC_COMM_WORLD, 1261 "Max Error: %g\n", 1262 (double)norm); CHKERRQ(ierr); 1263 // Clean up vectors 1264 ierr = DMRestoreLocalVector(dm, &Qexactloc); CHKERRQ(ierr); 1265 ierr = VecDestroy(&Qexact); CHKERRQ(ierr); 1266 } 1267 1268 // Output Statistics 1269 ierr = TSGetStepNumber(ts,&steps); CHKERRQ(ierr); 1270 if (!test) { 1271 ierr = PetscPrintf(PETSC_COMM_WORLD, 1272 "Time integrator took %D time steps to reach final time %g\n", 1273 steps, (double)ftime); CHKERRQ(ierr); 1274 } 1275 1276 // Clean up libCEED 1277 CeedVectorDestroy(&qdata); 1278 CeedVectorDestroy(&user->qceed); 1279 CeedVectorDestroy(&user->qdotceed); 1280 CeedVectorDestroy(&user->gceed); 1281 CeedVectorDestroy(&xcorners); 1282 CeedBasisDestroy(&basisq); 1283 CeedBasisDestroy(&basisx); 1284 CeedBasisDestroy(&basisxc); 1285 CeedElemRestrictionDestroy(&restrictq); 1286 CeedElemRestrictionDestroy(&restrictx); 1287 CeedElemRestrictionDestroy(&restrictqdi); 1288 CeedElemRestrictionDestroy(&restrictxcoord); 1289 CeedQFunctionDestroy(&qf_setup); 1290 CeedQFunctionDestroy(&qf_ics); 1291 CeedQFunctionDestroy(&qf_rhs); 1292 CeedQFunctionDestroy(&qf_ifunction); 1293 CeedOperatorDestroy(&op_setup); 1294 CeedOperatorDestroy(&op_ics); 1295 CeedOperatorDestroy(&user->op_rhs); 1296 CeedOperatorDestroy(&user->op_ifunction); 1297 CeedDestroy(&ceed); 1298 1299 // Clean up PETSc 1300 ierr = VecDestroy(&Q); CHKERRQ(ierr); 1301 ierr = VecDestroy(&user->M); CHKERRQ(ierr); 1302 ierr = MatDestroy(&interpviz); CHKERRQ(ierr); 1303 ierr = DMDestroy(&dmviz); CHKERRQ(ierr); 1304 ierr = TSDestroy(&ts); CHKERRQ(ierr); 1305 ierr = DMDestroy(&dm); CHKERRQ(ierr); 1306 ierr = PetscFree(units); CHKERRQ(ierr); 1307 ierr = PetscFree(user); CHKERRQ(ierr); 1308 return PetscFinalize(); 1309 } 1310 1311