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