xref: /libCEED/examples/fluids/navierstokes.c (revision 9fe13df93aae2cbae6e91241e43d140b3fba2734)
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 //
3184d34d69SLeila Ghaffari //     ./navierstokes -ceed /cpu/self -problem density_current -degree 1
3284d34d69SLeila Ghaffari //     ./navierstokes -ceed /gpu/occa -problem advection -degree 1
33ccaff030SJeremy L Thompson //
3484d34d69SLeila Ghaffari //TESTARGS -ceed {ceed_resource} -test explicit -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -center 62.5,62.5,187.5 -rc 100. -thetaC -35. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3
3584d34d69SLeila Ghaffari //TESTARGS -ceed {ceed_resource} -test implicit_stab_none -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -center 62.5,62.5,187.5 -rc 100. -thetaC -35. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha
3684d34d69SLeila Ghaffari //TESTARGS -ceed {ceed_resource} -test implicit_stab_supg -degree 3 -dm_plex_box_faces 1,1,2 -units_kilogram 1e-9 -lx 125 -ly 125 -lz 250 -center 62.5,62.5,187.5 -rc 100. -thetaC -35. -ksp_atol 1e-4 -ksp_rtol 1e-3 -ksp_type bcgs -snes_atol 1e-3 -snes_lag_jacobian 100 -snes_lag_jacobian_persists -snes_mf_operator -ts_dt 1e-3 -implicit -ts_type alpha -stab supg
37ccaff030SJeremy L Thompson 
38ccaff030SJeremy L Thompson /// @file
39ccaff030SJeremy L Thompson /// Navier-Stokes example using PETSc
40ccaff030SJeremy L Thompson 
41ccaff030SJeremy L Thompson const char help[] = "Solve Navier-Stokes using PETSc and libCEED\n";
42ccaff030SJeremy L Thompson 
43ccaff030SJeremy L Thompson #include <petscts.h>
44ccaff030SJeremy L Thompson #include <petscdmplex.h>
45ccaff030SJeremy L Thompson #include <ceed.h>
46ccaff030SJeremy L Thompson #include <stdbool.h>
47ccaff030SJeremy L Thompson #include <petscsys.h>
48ccaff030SJeremy L Thompson #include "common.h"
49b0137797SLeila Ghaffari #include "setup-boundary.h"
50ccaff030SJeremy L Thompson #include "advection.h"
51ccaff030SJeremy L Thompson #include "advection2d.h"
52ccaff030SJeremy L Thompson #include "densitycurrent.h"
53ccaff030SJeremy L Thompson 
5484d34d69SLeila Ghaffari #if PETSC_VERSION_LT(3,14,0)
5584d34d69SLeila Ghaffari #  define DMPlexGetClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexGetClosureIndices(a,b,c,d,f,g,i)
5684d34d69SLeila Ghaffari #  define DMPlexRestoreClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexRestoreClosureIndices(a,b,c,d,f,g,i)
5784d34d69SLeila Ghaffari #endif
5884d34d69SLeila Ghaffari 
5984d34d69SLeila Ghaffari // MemType Options
6084d34d69SLeila Ghaffari static const char *const memTypes[] = {
6184d34d69SLeila Ghaffari   "host",
6284d34d69SLeila Ghaffari   "device",
6384d34d69SLeila Ghaffari   "memType", "CEED_MEM_", NULL
6484d34d69SLeila Ghaffari };
6584d34d69SLeila Ghaffari 
66ccaff030SJeremy L Thompson // Problem Options
67ccaff030SJeremy L Thompson typedef enum {
68ccaff030SJeremy L Thompson   NS_DENSITY_CURRENT = 0,
69ccaff030SJeremy L Thompson   NS_ADVECTION = 1,
70ccaff030SJeremy L Thompson   NS_ADVECTION2D = 2,
71ccaff030SJeremy L Thompson } problemType;
72ccaff030SJeremy L Thompson static const char *const problemTypes[] = {
73ccaff030SJeremy L Thompson   "density_current",
74ccaff030SJeremy L Thompson   "advection",
75ccaff030SJeremy L Thompson   "advection2d",
7684d34d69SLeila Ghaffari   "problemType", "NS_", NULL
77ccaff030SJeremy L Thompson };
78ccaff030SJeremy L Thompson 
79ccaff030SJeremy L Thompson typedef enum {
80ccaff030SJeremy L Thompson   STAB_NONE = 0,
81ccaff030SJeremy L Thompson   STAB_SU = 1,   // Streamline Upwind
82ccaff030SJeremy L Thompson   STAB_SUPG = 2, // Streamline Upwind Petrov-Galerkin
83ccaff030SJeremy L Thompson } StabilizationType;
84ccaff030SJeremy L Thompson static const char *const StabilizationTypes[] = {
8584d34d69SLeila Ghaffari   "none",
86ccaff030SJeremy L Thompson   "SU",
87ccaff030SJeremy L Thompson   "SUPG",
88ccaff030SJeremy L Thompson   "StabilizationType", "STAB_", NULL
89ccaff030SJeremy L Thompson };
90ccaff030SJeremy L Thompson 
9184d34d69SLeila Ghaffari // Test Options
9284d34d69SLeila Ghaffari typedef enum {
9384d34d69SLeila Ghaffari   TEST_NONE = 0,               // Non test mode
9484d34d69SLeila Ghaffari   TEST_EXPLICIT = 1,           // Explicit test
9584d34d69SLeila Ghaffari   TEST_IMPLICIT_STAB_NONE = 2, // Implicit test no stab
9684d34d69SLeila Ghaffari   TEST_IMPLICIT_STAB_SUPG = 3, // Implicit test supg stab
9784d34d69SLeila Ghaffari } testType;
9884d34d69SLeila Ghaffari static const char *const testTypes[] = {
9984d34d69SLeila Ghaffari   "none",
10084d34d69SLeila Ghaffari   "explicit",
10184d34d69SLeila Ghaffari   "implicit_stab_none",
10284d34d69SLeila Ghaffari   "implicit_stab_supg",
10384d34d69SLeila Ghaffari   "testType", "TEST_", NULL
10484d34d69SLeila Ghaffari };
10584d34d69SLeila Ghaffari 
10684d34d69SLeila Ghaffari // Tests specific data
10784d34d69SLeila Ghaffari typedef struct {
10884d34d69SLeila Ghaffari   PetscScalar testtol;
10984d34d69SLeila Ghaffari   const char *filepath;
11084d34d69SLeila Ghaffari } testData;
11184d34d69SLeila Ghaffari 
11284d34d69SLeila Ghaffari testData testOptions[] = {
11384d34d69SLeila Ghaffari   [TEST_NONE] = {
11484d34d69SLeila Ghaffari     .testtol = 0.,
11584d34d69SLeila Ghaffari     .filepath = NULL
11684d34d69SLeila Ghaffari   },
11784d34d69SLeila Ghaffari   [TEST_EXPLICIT] = {
11884d34d69SLeila Ghaffari     .testtol = 1E-5,
11984d34d69SLeila Ghaffari     .filepath = "examples/fluids/tests-output/fluids-navierstokes-explicit.bin"
12084d34d69SLeila Ghaffari   },
12184d34d69SLeila Ghaffari   [TEST_IMPLICIT_STAB_NONE] = {
12284d34d69SLeila Ghaffari     .testtol = 5E-4,
12384d34d69SLeila Ghaffari     .filepath = "examples/fluids/tests-output/fluids-navierstokes-implicit-stab-none.bin"
12484d34d69SLeila Ghaffari   },
12584d34d69SLeila Ghaffari   [TEST_IMPLICIT_STAB_SUPG] = {
12684d34d69SLeila Ghaffari     .testtol = 5E-4,
12784d34d69SLeila Ghaffari     .filepath = "examples/fluids/tests-output/fluids-navierstokes-implicit-stab-supg.bin"
12884d34d69SLeila Ghaffari   }
12984d34d69SLeila Ghaffari };
13084d34d69SLeila Ghaffari 
131ccaff030SJeremy L Thompson // Problem specific data
132ccaff030SJeremy L Thompson typedef struct {
1338b982baeSLeila Ghaffari   CeedInt dim, qdatasizeVol, qdatasizeSur;
1345603407fSLeila Ghaffari   CeedQFunctionUser setupVol, setupSur, ics, applyVol_rhs, applyVol_ifunction,
135*9fe13df9SLeila Ghaffari                     applyOut_rhs, applyOut_ifunction, applyIn_rhs, applyIn_ifunction;
136ccaff030SJeremy L Thompson   PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt,
137ccaff030SJeremy L Thompson                        PetscScalar[], void *);
1385603407fSLeila Ghaffari   const char *setupVol_loc, *setupSur_loc, *ics_loc, *applyVol_rhs_loc, *applyVol_ifunction_loc,
139*9fe13df9SLeila Ghaffari              *applyOut_rhs_loc, *applyOut_ifunction_loc, *applyIn_rhs_loc, *applyIn_ifunction_loc;
140ccaff030SJeremy L Thompson   const bool non_zero_time;
141ccaff030SJeremy L Thompson } problemData;
142ccaff030SJeremy L Thompson 
143ccaff030SJeremy L Thompson problemData problemOptions[] = {
144ccaff030SJeremy L Thompson   [NS_DENSITY_CURRENT] = {
145ccaff030SJeremy L Thompson     .dim                       = 3,
146ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1478b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
148b0137797SLeila Ghaffari     .setupVol                  = Setup,
149b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
150356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
151356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
152ccaff030SJeremy L Thompson     .ics                       = ICsDC,
153ccaff030SJeremy L Thompson     .ics_loc                   = ICsDC_loc,
154c96c872fSLeila Ghaffari     .applyVol_rhs              = DC,
155c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = DC_loc,
1565603407fSLeila Ghaffari   //.applyOut_rhs              = DC_Out,
1575603407fSLeila Ghaffari   //.applyOut_rhs_loc          = DC_Out_loc,
158*9fe13df9SLeila Ghaffari   //.applyIn_rhs               = DC_In,
159*9fe13df9SLeila Ghaffari   //.applyIn_rhs_loc           = DC_In_loc,
160c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_DC,
161c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_DC_loc,
1625603407fSLeila Ghaffari   //.applyOut_ifunction        = IFunction_DC_Out,
1635603407fSLeila Ghaffari   //.applyOut_ifunction_loc    = IFunction_DC_Out_loc,
164*9fe13df9SLeila Ghaffari   //.applyIn_ifunction         = IFunction_DC_In,
165*9fe13df9SLeila Ghaffari   //.applyIn_ifunction_loc     = IFunction_DC_In_loc,
166ccaff030SJeremy L Thompson     .bc                        = Exact_DC,
16784d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
168ccaff030SJeremy L Thompson   },
169ccaff030SJeremy L Thompson   [NS_ADVECTION] = {
170ccaff030SJeremy L Thompson     .dim                       = 3,
171ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1728b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
173b0137797SLeila Ghaffari     .setupVol                  = Setup,
174b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
175356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
176356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
177ccaff030SJeremy L Thompson     .ics                       = ICsAdvection,
178ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection_loc,
179c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection,
180c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection_loc,
1815603407fSLeila Ghaffari     .applyOut_rhs              = Advection_Out,
1825603407fSLeila Ghaffari     .applyOut_rhs_loc          = Advection_Out_loc,
183*9fe13df9SLeila Ghaffari     .applyIn_rhs               = Advection_In,
184*9fe13df9SLeila Ghaffari     .applyIn_rhs_loc           = Advection_In_loc,
185c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection,
186c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection_loc,
1875603407fSLeila Ghaffari     .applyOut_ifunction        = IFunction_Advection_Out,
1885603407fSLeila Ghaffari     .applyOut_ifunction_loc    = IFunction_Advection_Out_loc,
189*9fe13df9SLeila Ghaffari     .applyIn_ifunction         = IFunction_Advection_In,
190*9fe13df9SLeila Ghaffari     .applyIn_ifunction_loc     = IFunction_Advection_In_loc,
191ccaff030SJeremy L Thompson     .bc                        = Exact_Advection,
19284d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
193ccaff030SJeremy L Thompson   },
194ccaff030SJeremy L Thompson   [NS_ADVECTION2D] = {
195ccaff030SJeremy L Thompson     .dim                       = 2,
196ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 5,
1978b982baeSLeila Ghaffari     .qdatasizeSur              = 3,
198c96c872fSLeila Ghaffari     .setupVol                  = Setup2d,
199c96c872fSLeila Ghaffari     .setupVol_loc              = Setup2d_loc,
200b0137797SLeila Ghaffari     .setupSur                  = SetupBoundary2d,
201b0137797SLeila Ghaffari     .setupSur_loc              = SetupBoundary2d_loc,
202ccaff030SJeremy L Thompson     .ics                       = ICsAdvection2d,
203ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection2d_loc,
204c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection2d,
205c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection2d_loc,
2065603407fSLeila Ghaffari     .applyOut_rhs              = Advection2d_Out,
2075603407fSLeila Ghaffari     .applyOut_rhs_loc          = Advection2d_Out_loc,
208*9fe13df9SLeila Ghaffari     .applyIn_rhs               = Advection2d_In,
209*9fe13df9SLeila Ghaffari     .applyIn_rhs_loc           = Advection2d_In_loc,
210c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection2d,
211c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection2d_loc,
2125603407fSLeila Ghaffari     .applyOut_ifunction        = IFunction_Advection2d_Out,
2135603407fSLeila Ghaffari     .applyOut_ifunction_loc    = IFunction_Advection2d_Out_loc,
214*9fe13df9SLeila Ghaffari     .applyIn_ifunction         = IFunction_Advection2d_In,
215*9fe13df9SLeila Ghaffari     .applyIn_ifunction_loc     = IFunction_Advection2d_In_loc,
216ccaff030SJeremy L Thompson     .bc                        = Exact_Advection2d,
21784d34d69SLeila Ghaffari     .non_zero_time             = PETSC_TRUE,
218ccaff030SJeremy L Thompson   },
219ccaff030SJeremy L Thompson };
220ccaff030SJeremy L Thompson 
221ccaff030SJeremy L Thompson // PETSc user data
222ccaff030SJeremy L Thompson typedef struct User_ *User;
223ccaff030SJeremy L Thompson typedef struct Units_ *Units;
224ccaff030SJeremy L Thompson 
225ccaff030SJeremy L Thompson struct User_ {
226ccaff030SJeremy L Thompson   MPI_Comm comm;
227ccaff030SJeremy L Thompson   PetscInt outputfreq;
228ccaff030SJeremy L Thompson   DM dm;
229ccaff030SJeremy L Thompson   DM dmviz;
230ccaff030SJeremy L Thompson   Mat interpviz;
231ccaff030SJeremy L Thompson   Ceed ceed;
232ccaff030SJeremy L Thompson   Units units;
233ccaff030SJeremy L Thompson   CeedVector qceed, qdotceed, gceed;
2341e150236SLeila Ghaffari   CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction;
235ccaff030SJeremy L Thompson   Vec M;
236ccaff030SJeremy L Thompson   char outputfolder[PETSC_MAX_PATH_LEN];
237ccaff030SJeremy L Thompson   PetscInt contsteps;
238ccaff030SJeremy L Thompson };
239ccaff030SJeremy L Thompson 
240ccaff030SJeremy L Thompson struct Units_ {
241ccaff030SJeremy L Thompson   // fundamental units
242ccaff030SJeremy L Thompson   PetscScalar meter;
243ccaff030SJeremy L Thompson   PetscScalar kilogram;
244ccaff030SJeremy L Thompson   PetscScalar second;
245ccaff030SJeremy L Thompson   PetscScalar Kelvin;
246ccaff030SJeremy L Thompson   // derived units
247ccaff030SJeremy L Thompson   PetscScalar Pascal;
248ccaff030SJeremy L Thompson   PetscScalar JperkgK;
249ccaff030SJeremy L Thompson   PetscScalar mpersquareds;
250ccaff030SJeremy L Thompson   PetscScalar WpermK;
251ccaff030SJeremy L Thompson   PetscScalar kgpercubicm;
252ccaff030SJeremy L Thompson   PetscScalar kgpersquaredms;
253ccaff030SJeremy L Thompson   PetscScalar Joulepercubicm;
254ccaff030SJeremy L Thompson };
255ccaff030SJeremy L Thompson 
256ccaff030SJeremy L Thompson typedef struct SimpleBC_ *SimpleBC;
257ccaff030SJeremy L Thompson struct SimpleBC_ {
258*9fe13df9SLeila Ghaffari   PetscInt nwall, nslip[3], noutflow, ninflow;
259*9fe13df9SLeila Ghaffari   PetscInt walls[6], slips[3][6], outflow[6], inflow[6];
26084d34d69SLeila Ghaffari   PetscBool userbc;
261ccaff030SJeremy L Thompson };
262ccaff030SJeremy L Thompson 
263ccaff030SJeremy L Thompson // Essential BC dofs are encoded in closure indices as -(i+1).
264ccaff030SJeremy L Thompson static PetscInt Involute(PetscInt i) {
265ccaff030SJeremy L Thompson   return i >= 0 ? i : -(i+1);
266ccaff030SJeremy L Thompson }
267ccaff030SJeremy L Thompson 
268ccaff030SJeremy L Thompson // Utility function to create local CEED restriction
269ccaff030SJeremy L Thompson static PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt P,
27084d34d69SLeila Ghaffari     CeedInt height, DMLabel domainLabel, CeedInt value,
271ccaff030SJeremy L Thompson     CeedElemRestriction *Erestrict) {
272ccaff030SJeremy L Thompson 
273ccaff030SJeremy L Thompson   PetscSection   section;
2740c6c0b13SLeila Ghaffari   PetscInt       p, Nelem, Ndof, *erestrict, eoffset, nfields, dim,
2750c6c0b13SLeila Ghaffari                  depth;
2760c6c0b13SLeila Ghaffari   DMLabel        depthLabel;
2770c6c0b13SLeila Ghaffari   IS             depthIS, iterIS;
27884d34d69SLeila Ghaffari   Vec            Uloc;
2790c6c0b13SLeila Ghaffari   const PetscInt *iterIndices;
280ccaff030SJeremy L Thompson   PetscErrorCode ierr;
281ccaff030SJeremy L Thompson 
282ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
283ccaff030SJeremy L Thompson   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
284da51bdd9SLeila Ghaffari   dim -= height;
285ccaff030SJeremy L Thompson   ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
286ccaff030SJeremy L Thompson   ierr = PetscSectionGetNumFields(section, &nfields); CHKERRQ(ierr);
287ccaff030SJeremy L Thompson   PetscInt ncomp[nfields], fieldoff[nfields+1];
288ccaff030SJeremy L Thompson   fieldoff[0] = 0;
289ccaff030SJeremy L Thompson   for (PetscInt f=0; f<nfields; f++) {
290ccaff030SJeremy L Thompson     ierr = PetscSectionGetFieldComponents(section, f, &ncomp[f]); CHKERRQ(ierr);
291ccaff030SJeremy L Thompson     fieldoff[f+1] = fieldoff[f] + ncomp[f];
292ccaff030SJeremy L Thompson   }
293ccaff030SJeremy L Thompson 
2940c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepth(dm, &depth); CHKERRQ(ierr);
2950c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepthLabel(dm, &depthLabel); CHKERRQ(ierr);
2960c6c0b13SLeila Ghaffari   ierr = DMLabelGetStratumIS(depthLabel, depth - height, &depthIS); CHKERRQ(ierr);
2970c6c0b13SLeila Ghaffari   if (domainLabel) {
2980c6c0b13SLeila Ghaffari     IS domainIS;
2990c6c0b13SLeila Ghaffari     ierr = DMLabelGetStratumIS(domainLabel, value, &domainIS); CHKERRQ(ierr);
3000c6c0b13SLeila Ghaffari     ierr = ISIntersect(depthIS, domainIS, &iterIS); CHKERRQ(ierr);
3010c6c0b13SLeila Ghaffari     ierr = ISDestroy(&domainIS); CHKERRQ(ierr);
3020c6c0b13SLeila Ghaffari     ierr = ISDestroy(&depthIS); CHKERRQ(ierr);
3030c6c0b13SLeila Ghaffari   } else {
3040c6c0b13SLeila Ghaffari     iterIS = depthIS;
3050c6c0b13SLeila Ghaffari   }
3060c6c0b13SLeila Ghaffari   ierr = ISGetLocalSize(iterIS, &Nelem); CHKERRQ(ierr);
3070c6c0b13SLeila Ghaffari   ierr = ISGetIndices(iterIS, &iterIndices); CHKERRQ(ierr);
308ccaff030SJeremy L Thompson   ierr = PetscMalloc1(Nelem*PetscPowInt(P, dim), &erestrict); CHKERRQ(ierr);
3090c6c0b13SLeila Ghaffari   for (p=0,eoffset=0; p<Nelem; p++) {
3100c6c0b13SLeila Ghaffari     PetscInt c = iterIndices[p];
311ccaff030SJeremy L Thompson     PetscInt numindices, *indices, nnodes;
31284d34d69SLeila Ghaffari     ierr = DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE,
31384d34d69SLeila Ghaffari                                    &numindices, &indices, NULL, NULL);
31484d34d69SLeila Ghaffari     CHKERRQ(ierr);
31532b5ec5fSJed Brown     bool flip = false;
31632b5ec5fSJed Brown     if (height > 0) {
31732b5ec5fSJed Brown       PetscInt numCells, numFaces, start = -1;
31832b5ec5fSJed Brown       const PetscInt *orients, *faces, *cells;
31932b5ec5fSJed Brown       ierr = DMPlexGetSupport(dm, c, &cells); CHKERRQ(ierr);
32032b5ec5fSJed Brown       ierr = DMPlexGetSupportSize(dm, c, &numCells); CHKERRQ(ierr);
32132b5ec5fSJed Brown       if (numCells != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Expected one cell in support of exterior face, but got %D cells", numCells);
32232b5ec5fSJed Brown       ierr = DMPlexGetCone(dm, cells[0], &faces); CHKERRQ(ierr);
32332b5ec5fSJed Brown       ierr = DMPlexGetConeSize(dm, cells[0], &numFaces); CHKERRQ(ierr);
32432b5ec5fSJed Brown       for (PetscInt i=0; i<numFaces; i++) {if (faces[i] == c) start = i;}
32532b5ec5fSJed Brown       if (start < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Could not find face %D in cone of its support", c);
32632b5ec5fSJed Brown       ierr = DMPlexGetConeOrientation(dm, cells[0], &orients); CHKERRQ(ierr);
32732b5ec5fSJed Brown       if (orients[start] < 0) flip = true;
32832b5ec5fSJed Brown     }
32984d34d69SLeila Ghaffari     if (numindices % fieldoff[nfields]) SETERRQ1(PETSC_COMM_SELF,
33084d34d69SLeila Ghaffari           PETSC_ERR_ARG_INCOMP, "Number of closure indices not compatible with Cell %D",
33184d34d69SLeila Ghaffari           c);
332ccaff030SJeremy L Thompson     nnodes = numindices / fieldoff[nfields];
333ccaff030SJeremy L Thompson     for (PetscInt i=0; i<nnodes; i++) {
33432b5ec5fSJed Brown       PetscInt ii = i;
33532b5ec5fSJed Brown       if (flip) {
33632b5ec5fSJed Brown     	  if (P == nnodes) ii = nnodes - 1 - i;
33732b5ec5fSJed Brown         else if (P*P == nnodes) {
33832b5ec5fSJed Brown           PetscInt row = i / P, col = i % P;
33932b5ec5fSJed Brown 	        ii = row + col * P;
34032b5ec5fSJed Brown 	      } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for flipping point with %D nodes != P (%D) or P^2", nnodes, P);
34132b5ec5fSJed Brown       }
342ccaff030SJeremy L Thompson       // Check that indices are blocked by node and thus can be coalesced as a single field with
343ccaff030SJeremy L Thompson       // fieldoff[nfields] = sum(ncomp) components.
344ccaff030SJeremy L Thompson       for (PetscInt f=0; f<nfields; f++) {
345ccaff030SJeremy L Thompson         for (PetscInt j=0; j<ncomp[f]; j++) {
34632b5ec5fSJed Brown           if (Involute(indices[fieldoff[f]*nnodes + ii*ncomp[f] + j])
34732b5ec5fSJed Brown               != Involute(indices[ii*ncomp[0]]) + fieldoff[f] + j)
348ccaff030SJeremy L Thompson             SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,
349ccaff030SJeremy L Thompson                      "Cell %D closure indices not interlaced for node %D field %D component %D",
35032b5ec5fSJed Brown                      c, ii, f, j);
351ccaff030SJeremy L Thompson         }
352ccaff030SJeremy L Thompson       }
353ccaff030SJeremy L Thompson       // Essential boundary conditions are encoded as -(loc+1), but we don't care so we decode.
35432b5ec5fSJed Brown       PetscInt loc = Involute(indices[ii*ncomp[0]]);
3556f55dfd5Svaleriabarra       erestrict[eoffset++] = loc;
356ccaff030SJeremy L Thompson     }
35784d34d69SLeila Ghaffari     ierr = DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE,
35884d34d69SLeila Ghaffari                                        &numindices, &indices, NULL, NULL);
35984d34d69SLeila Ghaffari     CHKERRQ(ierr);
360ccaff030SJeremy L Thompson   }
3610c6c0b13SLeila Ghaffari   if (eoffset != Nelem*PetscPowInt(P, dim))
3620c6c0b13SLeila Ghaffari     SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_LIB,
3630c6c0b13SLeila Ghaffari              "ElemRestriction of size (%D,%D) initialized %D nodes", Nelem,
364ccaff030SJeremy L Thompson              PetscPowInt(P, dim),eoffset);
3650c6c0b13SLeila Ghaffari   ierr = ISRestoreIndices(iterIS, &iterIndices); CHKERRQ(ierr);
3660c6c0b13SLeila Ghaffari   ierr = ISDestroy(&iterIS); CHKERRQ(ierr);
3670c6c0b13SLeila Ghaffari 
368ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Uloc); CHKERRQ(ierr);
369ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(Uloc, &Ndof); CHKERRQ(ierr);
370ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Uloc); CHKERRQ(ierr);
3716f55dfd5Svaleriabarra   CeedElemRestrictionCreate(ceed, Nelem, PetscPowInt(P, dim), fieldoff[nfields],
3726f55dfd5Svaleriabarra                             1, Ndof, CEED_MEM_HOST, CEED_COPY_VALUES, erestrict,
3736f55dfd5Svaleriabarra                             Erestrict);
374ccaff030SJeremy L Thompson   ierr = PetscFree(erestrict); CHKERRQ(ierr);
375ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
376ccaff030SJeremy L Thompson }
377ccaff030SJeremy L Thompson 
378c96c872fSLeila Ghaffari // Utility function to get Ceed Restriction for each domain
3791e150236SLeila Ghaffari static PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
3801e150236SLeila Ghaffari     DMLabel domainLabel, PetscInt value, CeedInt P, CeedInt Q, CeedInt qdatasize,
3811e150236SLeila Ghaffari     CeedElemRestriction *restrictq, CeedElemRestriction *restrictx,
3821e150236SLeila Ghaffari     CeedElemRestriction *restrictqdi) {
383c96c872fSLeila Ghaffari 
384c96c872fSLeila Ghaffari   DM dmcoord;
3851e150236SLeila Ghaffari   CeedInt dim, localNelem;
3861e150236SLeila Ghaffari   CeedInt Qdim;
387c96c872fSLeila Ghaffari   PetscErrorCode ierr;
388c96c872fSLeila Ghaffari 
389c96c872fSLeila Ghaffari   PetscFunctionBeginUser;
3901e150236SLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
3911e150236SLeila Ghaffari   dim -= height;
3921e150236SLeila Ghaffari   Qdim = CeedIntPow(Q, dim);
393c96c872fSLeila Ghaffari   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
394c96c872fSLeila Ghaffari   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
395c96c872fSLeila Ghaffari   CHKERRQ(ierr);
396c96c872fSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dm, P, height, domainLabel, value, restrictq);
397c96c872fSLeila Ghaffari   CHKERRQ(ierr);
398c96c872fSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dmcoord, 2, height, domainLabel, value, restrictx);
399c96c872fSLeila Ghaffari   CHKERRQ(ierr);
400c96c872fSLeila Ghaffari   CeedElemRestrictionGetNumElements(*restrictq, &localNelem);
401c96c872fSLeila Ghaffari   CeedElemRestrictionCreateStrided(ceed, localNelem, Qdim,
402c96c872fSLeila Ghaffari                                    qdatasize, qdatasize*localNelem*Qdim,
403c96c872fSLeila Ghaffari                                    CEED_STRIDES_BACKEND, restrictqdi);
404c96c872fSLeila Ghaffari   PetscFunctionReturn(0);
405c96c872fSLeila Ghaffari }
406c96c872fSLeila Ghaffari 
4071e150236SLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
4081e150236SLeila Ghaffari static PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, CeedOperator op_applyVol,
409*9fe13df9SLeila Ghaffari     CeedQFunction qf_applyOut, CeedQFunction qf_applyIn, CeedQFunction qf_setupSur, CeedInt height,
410*9fe13df9SLeila Ghaffari     PetscInt nOut, PetscInt valueOut[6], PetscInt nIn, PetscInt valueIn[6], CeedInt numP_Sur,
411*9fe13df9SLeila Ghaffari     CeedInt numQ_Sur, CeedInt qdatasizeSur, CeedInt NqptsSur,
4121e150236SLeila Ghaffari     CeedBasis basisxSur, CeedBasis basisqSur, CeedOperator *op_apply) {
413ca3ac6ddSLeila Ghaffari 
414*9fe13df9SLeila Ghaffari   CeedElemRestriction restrictxOut[6], restrictqOut[6], restrictqdiOut[6],
415*9fe13df9SLeila Ghaffari                       restrictxIn[6], restrictqIn[6], restrictqdiIn[6];
416*9fe13df9SLeila Ghaffari   PetscInt localNelemOut[6], localNelemIn[6];
4171e150236SLeila Ghaffari   Vec Xloc;
418*9fe13df9SLeila Ghaffari   CeedVector xcorners, qdataOut[6], qdataIn[6];
419*9fe13df9SLeila Ghaffari   CeedOperator op_setupOut[6], op_applyOut[6], op_setupIn[6], op_applyIn[6];
420ca3ac6ddSLeila Ghaffari   DMLabel domainLabel;
4211e150236SLeila Ghaffari   PetscScalar *x;
4221e150236SLeila Ghaffari   PetscInt lsize;
423ca3ac6ddSLeila Ghaffari   PetscErrorCode ierr;
424ca3ac6ddSLeila Ghaffari 
425ca3ac6ddSLeila Ghaffari   PetscFunctionBeginUser;
426ca3ac6ddSLeila Ghaffari   // Composite Operaters
427ca3ac6ddSLeila Ghaffari   CeedCompositeOperatorCreate(ceed, op_apply);
4281e150236SLeila Ghaffari   CeedCompositeOperatorAddSub(*op_apply, op_applyVol); // Apply a Sub-Operator for the volume
429ca3ac6ddSLeila Ghaffari 
430*9fe13df9SLeila Ghaffari   if (nOut || nIn) {
4311e150236SLeila Ghaffari     ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
4321e150236SLeila Ghaffari     ierr = VecGetLocalSize(Xloc, &lsize); CHKERRQ(ierr);
4331e150236SLeila Ghaffari     ierr = CeedVectorCreate(ceed, lsize, &xcorners); CHKERRQ(ierr);
4341e150236SLeila Ghaffari     ierr = VecGetArray(Xloc, &x); CHKERRQ(ierr);
4351e150236SLeila Ghaffari     CeedVectorSetArray(xcorners, CEED_MEM_HOST, CEED_USE_POINTER, x);
436ca3ac6ddSLeila Ghaffari     ierr = DMGetLabel(dm, "Face Sets", &domainLabel); CHKERRQ(ierr);
437*9fe13df9SLeila Ghaffari 
438*9fe13df9SLeila Ghaffari     // Create CEED Operator for each OutFlow faces
439*9fe13df9SLeila Ghaffari     if (nOut) {
4405603407fSLeila Ghaffari       for (CeedInt i=0; i<nOut; i++) {
4415603407fSLeila Ghaffari         ierr = GetRestrictionForDomain(ceed, dm, height, domainLabel, valueOut[i], numP_Sur,
4425603407fSLeila Ghaffari                                        numQ_Sur, qdatasizeSur, &restrictqOut[i], &restrictxOut[i],
4435603407fSLeila Ghaffari                                        &restrictqdiOut[i]); CHKERRQ(ierr);
4441e150236SLeila Ghaffari         // Create the CEED vectors that will be needed in boundary setup
4455603407fSLeila Ghaffari         CeedElemRestrictionGetNumElements(restrictqOut[i], &localNelemOut[i]);
4465603407fSLeila Ghaffari         CeedVectorCreate(ceed, qdatasizeSur*localNelemOut[i]*NqptsSur, &qdataOut[i]);
447*9fe13df9SLeila Ghaffari         // Create the operator that builds the quadrature data for the OutFlow operator
4485603407fSLeila Ghaffari         CeedOperatorCreate(ceed, qf_setupSur, NULL, NULL, &op_setupOut[i]);
4495603407fSLeila Ghaffari         CeedOperatorSetField(op_setupOut[i], "dx", restrictxOut[i], basisxSur, CEED_VECTOR_ACTIVE);
4505603407fSLeila Ghaffari         CeedOperatorSetField(op_setupOut[i], "weight", CEED_ELEMRESTRICTION_NONE,
451ca3ac6ddSLeila Ghaffari                              basisxSur, CEED_VECTOR_NONE);
4525603407fSLeila Ghaffari         CeedOperatorSetField(op_setupOut[i], "qdataSur", restrictqdiOut[i],
453ca3ac6ddSLeila Ghaffari                              CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
454*9fe13df9SLeila Ghaffari         // Create OutFlow operator
4555603407fSLeila Ghaffari         CeedOperatorCreate(ceed, qf_applyOut, NULL, NULL, &op_applyOut[i]);
4565603407fSLeila Ghaffari         CeedOperatorSetField(op_applyOut[i], "q", restrictqOut[i], basisqSur, CEED_VECTOR_ACTIVE);
4575603407fSLeila Ghaffari         CeedOperatorSetField(op_applyOut[i], "qdataSur", restrictqdiOut[i],
4585603407fSLeila Ghaffari                              CEED_BASIS_COLLOCATED, qdataOut[i]);
4595603407fSLeila Ghaffari         CeedOperatorSetField(op_applyOut[i], "x", restrictxOut[i], basisxSur, xcorners);
4605603407fSLeila Ghaffari         CeedOperatorSetField(op_applyOut[i], "v", restrictqOut[i], basisqSur, CEED_VECTOR_ACTIVE);
461*9fe13df9SLeila Ghaffari         // Apply CEED operator for OutFlow setup
4625603407fSLeila Ghaffari         CeedOperatorApply(op_setupOut[i], xcorners, qdataOut[i], CEED_REQUEST_IMMEDIATE);
463*9fe13df9SLeila Ghaffari         // Apply Sub-Operator for OutFlow BCs
4645603407fSLeila Ghaffari         CeedCompositeOperatorAddSub(*op_apply, op_applyOut[i]);
465ca3ac6ddSLeila Ghaffari       }
466*9fe13df9SLeila Ghaffari     }
467*9fe13df9SLeila Ghaffari     // Create CEED Operator for each InFlow faces
468*9fe13df9SLeila Ghaffari     if (nIn) {
469*9fe13df9SLeila Ghaffari       for (CeedInt i=0; i<nIn; i++) {
470*9fe13df9SLeila Ghaffari         ierr = GetRestrictionForDomain(ceed, dm, height, domainLabel, valueIn[i], numP_Sur,
471*9fe13df9SLeila Ghaffari                                        numQ_Sur, qdatasizeSur, &restrictqIn[i], &restrictxIn[i],
472*9fe13df9SLeila Ghaffari                                        &restrictqdiIn[i]); CHKERRQ(ierr);
473*9fe13df9SLeila Ghaffari         // Create the CEED vectors that will be needed in boundary setup
474*9fe13df9SLeila Ghaffari         CeedElemRestrictionGetNumElements(restrictqIn[i], &localNelemIn[i]);
475*9fe13df9SLeila Ghaffari         CeedVectorCreate(ceed, qdatasizeSur*localNelemIn[i]*NqptsSur, &qdataIn[i]);
476*9fe13df9SLeila Ghaffari         // Create the operator that builds the quadrature data for the InFlow operator
477*9fe13df9SLeila Ghaffari         CeedOperatorCreate(ceed, qf_setupSur, NULL, NULL, &op_setupIn[i]);
478*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_setupIn[i], "dx", restrictxIn[i], basisxSur, CEED_VECTOR_ACTIVE);
479*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_setupIn[i], "weight", CEED_ELEMRESTRICTION_NONE,
480*9fe13df9SLeila Ghaffari                              basisxSur, CEED_VECTOR_NONE);
481*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_setupIn[i], "qdataSur", restrictqdiIn[i],
482*9fe13df9SLeila Ghaffari                              CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
483*9fe13df9SLeila Ghaffari         // Create InFlow operator
484*9fe13df9SLeila Ghaffari         CeedOperatorCreate(ceed, qf_applyIn, NULL, NULL, &op_applyIn[i]);
485*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_applyIn[i], "q", restrictqIn[i], basisqSur, CEED_VECTOR_ACTIVE);
486*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_applyIn[i], "qdataSur", restrictqdiIn[i],
487*9fe13df9SLeila Ghaffari                              CEED_BASIS_COLLOCATED, qdataIn[i]);
488*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_applyIn[i], "x", restrictxIn[i], basisxSur, xcorners);
489*9fe13df9SLeila Ghaffari         CeedOperatorSetField(op_applyIn[i], "v", restrictqIn[i], basisqSur, CEED_VECTOR_ACTIVE);
490*9fe13df9SLeila Ghaffari         // Apply CEED operator for InFlow setup
491*9fe13df9SLeila Ghaffari         CeedOperatorApply(op_setupIn[i], xcorners, qdataIn[i], CEED_REQUEST_IMMEDIATE);
492*9fe13df9SLeila Ghaffari         // Apply Sub-Operator for InFlow BCs
493*9fe13df9SLeila Ghaffari         CeedCompositeOperatorAddSub(*op_apply, op_applyIn[i]);
494*9fe13df9SLeila Ghaffari       }
495*9fe13df9SLeila Ghaffari     }
4961e150236SLeila Ghaffari     CeedVectorDestroy(&xcorners);
497ca3ac6ddSLeila Ghaffari   }
498ca3ac6ddSLeila Ghaffari   PetscFunctionReturn(0);
499ca3ac6ddSLeila Ghaffari }
500ca3ac6ddSLeila Ghaffari 
501ccaff030SJeremy L Thompson static int CreateVectorFromPetscVec(Ceed ceed, Vec p, CeedVector *v) {
502ccaff030SJeremy L Thompson   PetscErrorCode ierr;
503ccaff030SJeremy L Thompson   PetscInt m;
504ccaff030SJeremy L Thompson 
505ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
506ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &m); CHKERRQ(ierr);
507ccaff030SJeremy L Thompson   ierr = CeedVectorCreate(ceed, m, v); CHKERRQ(ierr);
508ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
509ccaff030SJeremy L Thompson }
510ccaff030SJeremy L Thompson 
511ccaff030SJeremy L Thompson static int VectorPlacePetscVec(CeedVector c, Vec p) {
512ccaff030SJeremy L Thompson   PetscErrorCode ierr;
513ccaff030SJeremy L Thompson   PetscInt mceed,mpetsc;
514ccaff030SJeremy L Thompson   PetscScalar *a;
515ccaff030SJeremy L Thompson 
516ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
517ccaff030SJeremy L Thompson   ierr = CeedVectorGetLength(c, &mceed); CHKERRQ(ierr);
518ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &mpetsc); CHKERRQ(ierr);
519ccaff030SJeremy L Thompson   if (mceed != mpetsc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,
52084d34d69SLeila Ghaffari                                   "Cannot place PETSc Vec of length %D in CeedVector of length %D",
52184d34d69SLeila Ghaffari                                   mpetsc, mceed);
522ccaff030SJeremy L Thompson   ierr = VecGetArray(p, &a); CHKERRQ(ierr);
523ccaff030SJeremy L Thompson   CeedVectorSetArray(c, CEED_MEM_HOST, CEED_USE_POINTER, a);
524ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
525ccaff030SJeremy L Thompson }
526ccaff030SJeremy L Thompson 
527ccaff030SJeremy L Thompson static PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm,
528ccaff030SJeremy L Thompson     PetscBool insertEssential, Vec Qloc, PetscReal time, Vec faceGeomFVM,
529ccaff030SJeremy L Thompson     Vec cellGeomFVM, Vec gradFVM) {
530ccaff030SJeremy L Thompson   PetscErrorCode ierr;
531ccaff030SJeremy L Thompson   Vec Qbc;
532ccaff030SJeremy L Thompson 
533ccaff030SJeremy L Thompson   PetscFunctionBegin;
534ccaff030SJeremy L Thompson   ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
535ccaff030SJeremy L Thompson   ierr = VecAXPY(Qloc, 1., Qbc); CHKERRQ(ierr);
536ccaff030SJeremy L Thompson   ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
537ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
538ccaff030SJeremy L Thompson }
539ccaff030SJeremy L Thompson 
540ccaff030SJeremy L Thompson // This is the RHS of the ODE, given as u_t = G(t,u)
541ccaff030SJeremy L Thompson // This function takes in a state vector Q and writes into G
542ccaff030SJeremy L Thompson static PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *userData) {
543ccaff030SJeremy L Thompson   PetscErrorCode ierr;
544ccaff030SJeremy L Thompson   User user = *(User *)userData;
545ccaff030SJeremy L Thompson   PetscScalar *q, *g;
546ccaff030SJeremy L Thompson   Vec Qloc, Gloc;
547ccaff030SJeremy L Thompson 
548ccaff030SJeremy L Thompson   // Global-to-local
549ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
550ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
551ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
552ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
553ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
554ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
555ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
556ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
557ccaff030SJeremy L Thompson 
558ccaff030SJeremy L Thompson   // Ceed Vectors
559ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
560ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
561ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER, q);
562ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
563ccaff030SJeremy L Thompson 
564ccaff030SJeremy L Thompson   // Apply CEED operator
565ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->qceed, user->gceed,
566ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
567ccaff030SJeremy L Thompson 
568ccaff030SJeremy L Thompson   // Restore vectors
569ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
570ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
571ccaff030SJeremy L Thompson 
572ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
573ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
574ccaff030SJeremy L Thompson 
575ccaff030SJeremy L Thompson   // Inverse of the lumped mass matrix
576ccaff030SJeremy L Thompson   ierr = VecPointwiseMult(G, G, user->M); // M is Minv
577ccaff030SJeremy L Thompson   CHKERRQ(ierr);
578ccaff030SJeremy L Thompson 
579ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
580ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
581ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
582ccaff030SJeremy L Thompson }
583ccaff030SJeremy L Thompson 
584ccaff030SJeremy L Thompson static PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Qdot, Vec G,
585ccaff030SJeremy L Thompson                                    void *userData) {
586ccaff030SJeremy L Thompson   PetscErrorCode ierr;
587ccaff030SJeremy L Thompson   User user = *(User *)userData;
588ccaff030SJeremy L Thompson   const PetscScalar *q, *qdot;
589ccaff030SJeremy L Thompson   PetscScalar *g;
590ccaff030SJeremy L Thompson   Vec Qloc, Qdotloc, Gloc;
591ccaff030SJeremy L Thompson 
592ccaff030SJeremy L Thompson   // Global-to-local
593ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
594ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
595ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
596ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
597ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
598ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
599ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
600ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
601ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qdotloc); CHKERRQ(ierr);
602ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Qdot, INSERT_VALUES, Qdotloc); CHKERRQ(ierr);
603ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
604ccaff030SJeremy L Thompson 
605ccaff030SJeremy L Thompson   // Ceed Vectors
606ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, &q); CHKERRQ(ierr);
607ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
608ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
609ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER,
610ccaff030SJeremy L Thompson                      (PetscScalar *)q);
611ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qdotceed, CEED_MEM_HOST, CEED_USE_POINTER,
612ccaff030SJeremy L Thompson                      (PetscScalar *)qdot);
613ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
614ccaff030SJeremy L Thompson 
615ccaff030SJeremy L Thompson   // Apply CEED operator
616ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->qceed, user->gceed,
617ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
618ccaff030SJeremy L Thompson 
619ccaff030SJeremy L Thompson   // Restore vectors
620ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, &q); CHKERRQ(ierr);
621ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
622ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
623ccaff030SJeremy L Thompson 
624ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
625ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
626ccaff030SJeremy L Thompson 
627ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
628ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
629ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
630ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
631ccaff030SJeremy L Thompson }
632ccaff030SJeremy L Thompson 
633ccaff030SJeremy L Thompson // User provided TS Monitor
634ccaff030SJeremy L Thompson static PetscErrorCode TSMonitor_NS(TS ts, PetscInt stepno, PetscReal time,
635ccaff030SJeremy L Thompson                                    Vec Q, void *ctx) {
636ccaff030SJeremy L Thompson   User user = ctx;
637ccaff030SJeremy L Thompson   Vec Qloc;
638ccaff030SJeremy L Thompson   char filepath[PETSC_MAX_PATH_LEN];
639ccaff030SJeremy L Thompson   PetscViewer viewer;
640ccaff030SJeremy L Thompson   PetscErrorCode ierr;
641ccaff030SJeremy L Thompson 
642ccaff030SJeremy L Thompson   // Set up output
643ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
644ccaff030SJeremy L Thompson   // Print every 'outputfreq' steps
645ccaff030SJeremy L Thompson   if (stepno % user->outputfreq != 0)
646ccaff030SJeremy L Thompson     PetscFunctionReturn(0);
647ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
648ccaff030SJeremy L Thompson   ierr = PetscObjectSetName((PetscObject)Qloc, "StateVec"); CHKERRQ(ierr);
649ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
650ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
651ccaff030SJeremy L Thompson 
652ccaff030SJeremy L Thompson   // Output
653ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-%03D.vtu",
654ccaff030SJeremy L Thompson                        user->outputfolder, stepno + user->contsteps);
655ccaff030SJeremy L Thompson   CHKERRQ(ierr);
656ccaff030SJeremy L Thompson   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), filepath,
657ccaff030SJeremy L Thompson                             FILE_MODE_WRITE, &viewer); CHKERRQ(ierr);
658ccaff030SJeremy L Thompson   ierr = VecView(Qloc, viewer); CHKERRQ(ierr);
6599d801c56SJed Brown   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
660ccaff030SJeremy L Thompson   if (user->dmviz) {
661ccaff030SJeremy L Thompson     Vec Qrefined, Qrefined_loc;
662ccaff030SJeremy L Thompson     char filepath_refined[PETSC_MAX_PATH_LEN];
663ccaff030SJeremy L Thompson     PetscViewer viewer_refined;
664ccaff030SJeremy L Thompson 
665ccaff030SJeremy L Thompson     ierr = DMGetGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
666ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
667ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)Qrefined_loc, "Refined");
668ccaff030SJeremy L Thompson     CHKERRQ(ierr);
669ccaff030SJeremy L Thompson     ierr = MatInterpolate(user->interpviz, Q, Qrefined); CHKERRQ(ierr);
670ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qrefined_loc); CHKERRQ(ierr);
671ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(user->dmviz, Qrefined, INSERT_VALUES, Qrefined_loc);
672ccaff030SJeremy L Thompson     CHKERRQ(ierr);
673ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath_refined, sizeof filepath_refined,
674ccaff030SJeremy L Thompson                          "%s/nsrefined-%03D.vtu",
675ccaff030SJeremy L Thompson                          user->outputfolder, stepno + user->contsteps);
676ccaff030SJeremy L Thompson     CHKERRQ(ierr);
677ccaff030SJeremy L Thompson     ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Qrefined),
678ccaff030SJeremy L Thompson                               filepath_refined,
679ccaff030SJeremy L Thompson                               FILE_MODE_WRITE, &viewer_refined); CHKERRQ(ierr);
680ccaff030SJeremy L Thompson     ierr = VecView(Qrefined_loc, viewer_refined); CHKERRQ(ierr);
681ccaff030SJeremy L Thompson     ierr = DMRestoreLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
682ccaff030SJeremy L Thompson     ierr = DMRestoreGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
683ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer_refined); CHKERRQ(ierr);
684ccaff030SJeremy L Thompson   }
685ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
686ccaff030SJeremy L Thompson 
687ccaff030SJeremy L Thompson   // Save data in a binary file for continuation of simulations
688ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
689ccaff030SJeremy L Thompson                        user->outputfolder); CHKERRQ(ierr);
690ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
691ccaff030SJeremy L Thompson   CHKERRQ(ierr);
692ccaff030SJeremy L Thompson   ierr = VecView(Q, viewer); CHKERRQ(ierr);
693ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
694ccaff030SJeremy L Thompson 
695ccaff030SJeremy L Thompson   // Save time stamp
696ccaff030SJeremy L Thompson   // Dimensionalize time back
697ccaff030SJeremy L Thompson   time /= user->units->second;
698ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
699ccaff030SJeremy L Thompson                        user->outputfolder); CHKERRQ(ierr);
700ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
701ccaff030SJeremy L Thompson   CHKERRQ(ierr);
702ccaff030SJeremy L Thompson   #if PETSC_VERSION_GE(3,13,0)
703ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL);
704ccaff030SJeremy L Thompson   #else
705ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL, true);
706ccaff030SJeremy L Thompson   #endif
707ccaff030SJeremy L Thompson   CHKERRQ(ierr);
708ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
709ccaff030SJeremy L Thompson 
710ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
711ccaff030SJeremy L Thompson }
712ccaff030SJeremy L Thompson 
71384d34d69SLeila Ghaffari static PetscErrorCode ICs_FixMultiplicity(CeedOperator op_ics,
714ccaff030SJeremy L Thompson     CeedVector xcorners, CeedVector q0ceed, DM dm, Vec Qloc, Vec Q,
715ccaff030SJeremy L Thompson     CeedElemRestriction restrictq, SetupContext ctxSetup, CeedScalar time) {
716ccaff030SJeremy L Thompson   PetscErrorCode ierr;
717ccaff030SJeremy L Thompson   CeedVector multlvec;
718ccaff030SJeremy L Thompson   Vec Multiplicity, MultiplicityLoc;
719ccaff030SJeremy L Thompson 
720ccaff030SJeremy L Thompson   ctxSetup->time = time;
721ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
722ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
723ccaff030SJeremy L Thompson   CeedOperatorApply(op_ics, xcorners, q0ceed, CEED_REQUEST_IMMEDIATE);
724ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
725ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Qloc, ADD_VALUES, Q); CHKERRQ(ierr);
726ccaff030SJeremy L Thompson 
727ccaff030SJeremy L Thompson   // Fix multiplicity for output of ICs
728ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
729ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &multlvec, NULL);
730ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(multlvec, MultiplicityLoc); CHKERRQ(ierr);
731ccaff030SJeremy L Thompson   CeedElemRestrictionGetMultiplicity(restrictq, multlvec);
732ccaff030SJeremy L Thompson   CeedVectorDestroy(&multlvec);
733ccaff030SJeremy L Thompson   ierr = DMGetGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
734ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Multiplicity); CHKERRQ(ierr);
735ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, MultiplicityLoc, ADD_VALUES, Multiplicity);
736ccaff030SJeremy L Thompson   CHKERRQ(ierr);
737ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Q, Q, Multiplicity); CHKERRQ(ierr);
738ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Qloc, Qloc, MultiplicityLoc); CHKERRQ(ierr);
739ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
740ccaff030SJeremy L Thompson   ierr = DMRestoreGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
741ccaff030SJeremy L Thompson 
742ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
743ccaff030SJeremy L Thompson }
744ccaff030SJeremy L Thompson 
745ccaff030SJeremy L Thompson static PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm,
746ccaff030SJeremy L Thompson     CeedElemRestriction restrictq, CeedBasis basisq,
747ccaff030SJeremy L Thompson     CeedElemRestriction restrictqdi, CeedVector qdata, Vec M) {
748ccaff030SJeremy L Thompson   PetscErrorCode ierr;
749ccaff030SJeremy L Thompson   CeedQFunction qf_mass;
750ccaff030SJeremy L Thompson   CeedOperator op_mass;
751ccaff030SJeremy L Thompson   CeedVector mceed;
752ccaff030SJeremy L Thompson   Vec Mloc;
753ccaff030SJeremy L Thompson   CeedInt ncompq, qdatasize;
754ccaff030SJeremy L Thompson 
755ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
756ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictq, &ncompq);
757ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictqdi, &qdatasize);
758ccaff030SJeremy L Thompson   // Create the Q-function that defines the action of the mass operator
759ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass);
760ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q", ncompq, CEED_EVAL_INTERP);
761ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "qdata", qdatasize, CEED_EVAL_NONE);
762ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", ncompq, CEED_EVAL_INTERP);
763ccaff030SJeremy L Thompson 
764ccaff030SJeremy L Thompson   // Create the mass operator
765ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
766ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
767ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", restrictqdi,
768ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, qdata);
769ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
770ccaff030SJeremy L Thompson 
771ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Mloc); CHKERRQ(ierr);
772ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Mloc); CHKERRQ(ierr);
773ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &mceed, NULL);
774ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(mceed, Mloc); CHKERRQ(ierr);
775ccaff030SJeremy L Thompson 
776ccaff030SJeremy L Thompson   {
777ccaff030SJeremy L Thompson     // Compute a lumped mass matrix
778ccaff030SJeremy L Thompson     CeedVector onesvec;
779ccaff030SJeremy L Thompson     CeedElemRestrictionCreateVector(restrictq, &onesvec, NULL);
780ccaff030SJeremy L Thompson     CeedVectorSetValue(onesvec, 1.0);
781ccaff030SJeremy L Thompson     CeedOperatorApply(op_mass, onesvec, mceed, CEED_REQUEST_IMMEDIATE);
782ccaff030SJeremy L Thompson     CeedVectorDestroy(&onesvec);
783ccaff030SJeremy L Thompson     CeedOperatorDestroy(&op_mass);
784ccaff030SJeremy L Thompson     CeedVectorDestroy(&mceed);
785ccaff030SJeremy L Thompson   }
786ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
787ccaff030SJeremy L Thompson 
788ccaff030SJeremy L Thompson   ierr = VecZeroEntries(M); CHKERRQ(ierr);
789ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Mloc, ADD_VALUES, M); CHKERRQ(ierr);
790ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Mloc); CHKERRQ(ierr);
791ccaff030SJeremy L Thompson 
792ccaff030SJeremy L Thompson   // Invert diagonally lumped mass vector for RHS function
793ccaff030SJeremy L Thompson   ierr = VecReciprocal(M); CHKERRQ(ierr);
794ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
795ccaff030SJeremy L Thompson }
796ccaff030SJeremy L Thompson 
79784d34d69SLeila Ghaffari static PetscErrorCode SetUpDM(DM dm, problemData *problem, PetscInt degree,
798ff6701fcSJed Brown                               SimpleBC bc, void *ctxSetup) {
799ccaff030SJeremy L Thompson   PetscErrorCode ierr;
800ccaff030SJeremy L Thompson 
801ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
802ccaff030SJeremy L Thompson   {
803ccaff030SJeremy L Thompson     // Configure the finite element space and boundary conditions
804ccaff030SJeremy L Thompson     PetscFE fe;
805ccaff030SJeremy L Thompson     PetscInt ncompq = 5;
806ff6701fcSJed Brown     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, ncompq,
807ff6701fcSJed Brown                                  PETSC_FALSE, degree, PETSC_DECIDE,
80832ed2d11SJed Brown                                  &fe); CHKERRQ(ierr);
809ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
810ccaff030SJeremy L Thompson     ierr = DMAddField(dm,NULL,(PetscObject)fe); CHKERRQ(ierr);
811ccaff030SJeremy L Thompson     ierr = DMCreateDS(dm); CHKERRQ(ierr);
81207af6069Svaleriabarra     {
81307af6069Svaleriabarra       PetscInt comps[1] = {1};
81407af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", "Face Sets", 0,
81507af6069Svaleriabarra                            1, comps, (void(*)(void))NULL, bc->nslip[0],
81607af6069Svaleriabarra                            bc->slips[0], ctxSetup); CHKERRQ(ierr);
81707af6069Svaleriabarra       comps[0] = 2;
81807af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", "Face Sets", 0,
81907af6069Svaleriabarra                            1, comps, (void(*)(void))NULL, bc->nslip[1],
82007af6069Svaleriabarra                            bc->slips[1], ctxSetup); CHKERRQ(ierr);
82107af6069Svaleriabarra       comps[0] = 3;
82207af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", "Face Sets", 0,
82307af6069Svaleriabarra                            1, comps, (void(*)(void))NULL, bc->nslip[2],
82407af6069Svaleriabarra                            bc->slips[2], ctxSetup); CHKERRQ(ierr);
82507af6069Svaleriabarra     }
82684d34d69SLeila Ghaffari     if (bc->userbc == PETSC_TRUE) {
82784d34d69SLeila Ghaffari       for (PetscInt c = 0; c < 3; c++) {
82884d34d69SLeila Ghaffari         for (PetscInt s = 0; s < bc->nslip[c]; s++) {
82984d34d69SLeila Ghaffari           for (PetscInt w = 0; w < bc->nwall; w++) {
83084d34d69SLeila Ghaffari             if (bc->slips[c][s] == bc->walls[w])
83184d34d69SLeila Ghaffari               SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
83284d34d69SLeila Ghaffari                        "Boundary condition already set on face %D!\n", bc->walls[w]);
83384d34d69SLeila Ghaffari 
83484d34d69SLeila Ghaffari           }
83584d34d69SLeila Ghaffari         }
83684d34d69SLeila Ghaffari       }
83784d34d69SLeila Ghaffari     }
83884d34d69SLeila Ghaffari     // Wall boundary conditions are zero energy density and zero flux for
83984d34d69SLeila Ghaffari     //   velocity in advection/advection2d, and zero velocity and zero flux
84084d34d69SLeila Ghaffari     //   for mass density and energy density in density_current
84184d34d69SLeila Ghaffari     {
84284d34d69SLeila Ghaffari       if (problem->bc == Exact_Advection || problem->bc == Exact_Advection2d) {
84384d34d69SLeila Ghaffari         PetscInt comps[1] = {4};
84484d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
84584d34d69SLeila Ghaffari                              1, comps, (void(*)(void))problem->bc,
84684d34d69SLeila Ghaffari                              bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr);
84784d34d69SLeila Ghaffari       } else if (problem->bc == Exact_DC) {
84884d34d69SLeila Ghaffari         PetscInt comps[3] = {1, 2, 3};
84984d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
85084d34d69SLeila Ghaffari                              3, comps, (void(*)(void))problem->bc,
85184d34d69SLeila Ghaffari                              bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr);
85284d34d69SLeila Ghaffari       } else
85384d34d69SLeila Ghaffari         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
85484d34d69SLeila Ghaffari                 "Undefined boundary conditions for this problem");
85584d34d69SLeila Ghaffari     }
856ccaff030SJeremy L Thompson     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
857ccaff030SJeremy L Thompson     CHKERRQ(ierr);
858ccaff030SJeremy L Thompson     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
859ccaff030SJeremy L Thompson   }
860ccaff030SJeremy L Thompson   {
861ccaff030SJeremy L Thompson     // Empty name for conserved field (because there is only one field)
862ccaff030SJeremy L Thompson     PetscSection section;
863ccaff030SJeremy L Thompson     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
864ccaff030SJeremy L Thompson     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
865ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
866ccaff030SJeremy L Thompson     CHKERRQ(ierr);
867ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 1, "MomentumX");
868ccaff030SJeremy L Thompson     CHKERRQ(ierr);
869ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 2, "MomentumY");
870ccaff030SJeremy L Thompson     CHKERRQ(ierr);
871ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 3, "MomentumZ");
872ccaff030SJeremy L Thompson     CHKERRQ(ierr);
873ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 4, "EnergyDensity");
874ccaff030SJeremy L Thompson     CHKERRQ(ierr);
875ccaff030SJeremy L Thompson   }
876ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
877ccaff030SJeremy L Thompson }
878ccaff030SJeremy L Thompson 
879ccaff030SJeremy L Thompson int main(int argc, char **argv) {
880ccaff030SJeremy L Thompson   PetscInt ierr;
881ccaff030SJeremy L Thompson   MPI_Comm comm;
88284d34d69SLeila Ghaffari   DM dm, dmcoord, dmviz;
883ccaff030SJeremy L Thompson   Mat interpviz;
884ccaff030SJeremy L Thompson   TS ts;
885ccaff030SJeremy L Thompson   TSAdapt adapt;
886ccaff030SJeremy L Thompson   User user;
887ccaff030SJeremy L Thompson   Units units;
888ccaff030SJeremy L Thompson   char ceedresource[4096] = "/cpu/self";
88984d34d69SLeila Ghaffari   PetscInt localNelemVol, lnodes, gnodes, steps;
890ccaff030SJeremy L Thompson   const PetscInt ncompq = 5;
891ccaff030SJeremy L Thompson   PetscMPIInt rank;
892ccaff030SJeremy L Thompson   PetscScalar ftime;
893ccaff030SJeremy L Thompson   Vec Q, Qloc, Xloc;
894ccaff030SJeremy L Thompson   Ceed ceed;
89584d34d69SLeila Ghaffari   CeedInt numP, numQ;
896cfa64770SLeila Ghaffari   CeedVector xcorners, qdata, q0ceed;
89784d34d69SLeila Ghaffari   CeedBasis basisx, basisxc, basisq;
89884d34d69SLeila Ghaffari   CeedElemRestriction restrictx, restrictq, restrictqdi;
899cfa64770SLeila Ghaffari   CeedQFunction qf_setupVol, qf_ics, qf_rhsVol, qf_ifunctionVol;
900cfa64770SLeila Ghaffari   CeedOperator op_setupVol, op_ics;
901ccaff030SJeremy L Thompson   CeedScalar Rd;
90284d34d69SLeila Ghaffari   CeedMemType memtyperequested;
903ccaff030SJeremy L Thompson   PetscScalar WpermK, Pascal, JperkgK, mpersquareds, kgpercubicm,
904ccaff030SJeremy L Thompson               kgpersquaredms, Joulepercubicm;
905ccaff030SJeremy L Thompson   problemType problemChoice;
906ccaff030SJeremy L Thompson   problemData *problem = NULL;
907ccaff030SJeremy L Thompson   StabilizationType stab;
90884d34d69SLeila Ghaffari   testType testChoice;
90984d34d69SLeila Ghaffari   testData *test = NULL;
91084d34d69SLeila Ghaffari   PetscBool implicit;
911cb3e2689Svaleriabarra   PetscInt    viz_refine = 0;
912ccaff030SJeremy L Thompson   struct SimpleBC_ bc = {
91384d34d69SLeila Ghaffari     .nslip = {2, 2, 2},
91484d34d69SLeila Ghaffari     .slips = {{5, 6}, {3, 4}, {1, 2}}
915ccaff030SJeremy L Thompson   };
916ccaff030SJeremy L Thompson   double start, cpu_time_used;
91784d34d69SLeila Ghaffari   // Check PETSc CUDA support
91884d34d69SLeila Ghaffari   PetscBool petschavecuda, setmemtyperequest = PETSC_FALSE;
91984d34d69SLeila Ghaffari   // *INDENT-OFF*
92084d34d69SLeila Ghaffari   #ifdef PETSC_HAVE_CUDA
92184d34d69SLeila Ghaffari   petschavecuda = PETSC_TRUE;
92284d34d69SLeila Ghaffari   #else
92384d34d69SLeila Ghaffari   petschavecuda = PETSC_FALSE;
92484d34d69SLeila Ghaffari   #endif
92584d34d69SLeila Ghaffari   // *INDENT-ON*
926ccaff030SJeremy L Thompson 
927ccaff030SJeremy L Thompson   // Create the libCEED contexts
928ccaff030SJeremy L Thompson   PetscScalar meter      = 1e-2;     // 1 meter in scaled length units
929ccaff030SJeremy L Thompson   PetscScalar second     = 1e-2;     // 1 second in scaled time units
930ccaff030SJeremy L Thompson   PetscScalar kilogram   = 1e-6;     // 1 kilogram in scaled mass units
931ccaff030SJeremy L Thompson   PetscScalar Kelvin     = 1;        // 1 Kelvin in scaled temperature units
932ccaff030SJeremy L Thompson   CeedScalar theta0      = 300.;     // K
933ccaff030SJeremy L Thompson   CeedScalar thetaC      = -15.;     // K
934ccaff030SJeremy L Thompson   CeedScalar P0          = 1.e5;     // Pa
935*9fe13df9SLeila Ghaffari   CeedScalar P_left      = 1.5e5;    // Pa
936*9fe13df9SLeila Ghaffari   CeedScalar rho_left    = 1.2;      // Kg/m^3
937ccaff030SJeremy L Thompson   CeedScalar N           = 0.01;     // 1/s
938ccaff030SJeremy L Thompson   CeedScalar cv          = 717.;     // J/(kg K)
939ccaff030SJeremy L Thompson   CeedScalar cp          = 1004.;    // J/(kg K)
940ccaff030SJeremy L Thompson   CeedScalar g           = 9.81;     // m/s^2
941ccaff030SJeremy L Thompson   CeedScalar lambda      = -2./3.;   // -
942ccaff030SJeremy L Thompson   CeedScalar mu          = 75.;      // Pa s, dynamic viscosity
943ccaff030SJeremy L Thompson   // mu = 75 is not physical for air, but is good for numerical stability
944ccaff030SJeremy L Thompson   CeedScalar k           = 0.02638;  // W/(m K)
945ccaff030SJeremy L Thompson   CeedScalar CtauS       = 0.;       // dimensionless
946ccaff030SJeremy L Thompson   CeedScalar strong_form = 0.;       // [0,1]
947ccaff030SJeremy L Thompson   PetscScalar lx         = 8000.;    // m
948ccaff030SJeremy L Thompson   PetscScalar ly         = 8000.;    // m
949ccaff030SJeremy L Thompson   PetscScalar lz         = 4000.;    // m
950ccaff030SJeremy L Thompson   CeedScalar rc          = 1000.;    // m (Radius of bubble)
951ccaff030SJeremy L Thompson   PetscScalar resx       = 1000.;    // m (resolution in x)
952ccaff030SJeremy L Thompson   PetscScalar resy       = 1000.;    // m (resolution in y)
953ccaff030SJeremy L Thompson   PetscScalar resz       = 1000.;    // m (resolution in z)
954ccaff030SJeremy L Thompson   PetscInt outputfreq    = 10;       // -
955ccaff030SJeremy L Thompson   PetscInt contsteps     = 0;        // -
95684d34d69SLeila Ghaffari   PetscInt degree        = 1;        // -
95784d34d69SLeila Ghaffari   PetscInt qextra        = 2;        // -
958ea6e0f84SLeila Ghaffari   PetscInt qextraSur     = 2;        // -
959ccaff030SJeremy L Thompson   PetscReal center[3], dc_axis[3] = {0, 0, 0};
960ccaff030SJeremy L Thompson 
961ccaff030SJeremy L Thompson   ierr = PetscInitialize(&argc, &argv, NULL, help);
962ccaff030SJeremy L Thompson   if (ierr) return ierr;
963ccaff030SJeremy L Thompson 
964ccaff030SJeremy L Thompson   // Allocate PETSc context
965ccaff030SJeremy L Thompson   ierr = PetscCalloc1(1, &user); CHKERRQ(ierr);
966ccaff030SJeremy L Thompson   ierr = PetscMalloc1(1, &units); CHKERRQ(ierr);
967ccaff030SJeremy L Thompson 
968ccaff030SJeremy L Thompson   // Parse command line options
969ccaff030SJeremy L Thompson   comm = PETSC_COMM_WORLD;
970ccaff030SJeremy L Thompson   ierr = PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED",
971ccaff030SJeremy L Thompson                            NULL); CHKERRQ(ierr);
972ccaff030SJeremy L Thompson   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
973ccaff030SJeremy L Thompson                             NULL, ceedresource, ceedresource,
974ccaff030SJeremy L Thompson                             sizeof(ceedresource), NULL); CHKERRQ(ierr);
97584d34d69SLeila Ghaffari   testChoice = TEST_NONE;
97684d34d69SLeila Ghaffari   ierr = PetscOptionsEnum("-test", "Run tests", NULL,
97784d34d69SLeila Ghaffari                           testTypes, (PetscEnum)testChoice,
97884d34d69SLeila Ghaffari                           (PetscEnum *)&testChoice,
97984d34d69SLeila Ghaffari                           NULL); CHKERRQ(ierr);
98084d34d69SLeila Ghaffari   test = &testOptions[testChoice];
981ccaff030SJeremy L Thompson   problemChoice = NS_DENSITY_CURRENT;
982ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-problem", "Problem to solve", NULL,
983ccaff030SJeremy L Thompson                           problemTypes, (PetscEnum)problemChoice,
984ccaff030SJeremy L Thompson                           (PetscEnum *)&problemChoice, NULL); CHKERRQ(ierr);
985ccaff030SJeremy L Thompson   problem = &problemOptions[problemChoice];
986ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
987ccaff030SJeremy L Thompson                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
988ccaff030SJeremy L Thompson                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
989ccaff030SJeremy L Thompson   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
990ccaff030SJeremy L Thompson                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
991ccaff030SJeremy L Thompson   CHKERRQ(ierr);
99284d34d69SLeila Ghaffari   if (!implicit && stab != STAB_NONE) {
99384d34d69SLeila Ghaffari     ierr = PetscPrintf(comm, "Warning! Use -stab only with -implicit\n");
99484d34d69SLeila Ghaffari     CHKERRQ(ierr);
99584d34d69SLeila Ghaffari   }
996ccaff030SJeremy L Thompson   {
9977573aee6SJed Brown     PetscInt len;
9987573aee6SJed Brown     PetscBool flg;
9997573aee6SJed Brown     ierr = PetscOptionsIntArray("-bc_outflow",
10007573aee6SJed Brown                               "Use outflow boundary conditions on this list of faces",
10017573aee6SJed Brown                               NULL, bc.outflow,
10027573aee6SJed Brown                               (len = sizeof(bc.outflow) / sizeof(bc.outflow[0]),
10037573aee6SJed Brown                               &len), &flg); CHKERRQ(ierr);
10047573aee6SJed Brown     if (flg) {
10057573aee6SJed Brown       bc.noutflow = len;
10067573aee6SJed Brown       // Using outflow boundaries disables automatic wall/slip boundaries (they must be set explicitly)
10077573aee6SJed Brown       bc.nwall = 0;
10087573aee6SJed Brown       bc.nslip[0] = bc.nslip[1] = bc.nslip[2] = 0;
10097573aee6SJed Brown     }
1010*9fe13df9SLeila Ghaffari     ierr = PetscOptionsIntArray("-bc_inflow",
1011*9fe13df9SLeila Ghaffari                               "Use inflow boundary conditions on this list of faces",
1012*9fe13df9SLeila Ghaffari                               NULL, bc.inflow,
1013*9fe13df9SLeila Ghaffari                               (len = sizeof(bc.inflow) / sizeof(bc.inflow[0]),
1014*9fe13df9SLeila Ghaffari                               &len), &flg); CHKERRQ(ierr);
1015*9fe13df9SLeila Ghaffari     if (flg) {
1016*9fe13df9SLeila Ghaffari       bc.ninflow = len;
1017*9fe13df9SLeila Ghaffari       // Using inflow boundaries disables automatic wall/slip boundaries (they must be set explicitly)
1018*9fe13df9SLeila Ghaffari       bc.nwall = 0;
1019*9fe13df9SLeila Ghaffari       bc.nslip[0] = bc.nslip[1] = bc.nslip[2] = 0;
1020*9fe13df9SLeila Ghaffari     }
1021ccaff030SJeremy L Thompson     ierr = PetscOptionsIntArray("-bc_wall",
1022ccaff030SJeremy L Thompson                                 "Use wall boundary conditions on this list of faces",
1023ccaff030SJeremy L Thompson                                 NULL, bc.walls,
1024ccaff030SJeremy L Thompson                                 (len = sizeof(bc.walls) / sizeof(bc.walls[0]),
1025ccaff030SJeremy L Thompson                                  &len), &flg); CHKERRQ(ierr);
10267573aee6SJed Brown     if (flg) {
10277573aee6SJed Brown       bc.nwall = len;
10287573aee6SJed Brown       // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
10297573aee6SJed Brown       bc.nslip[0] = bc.nslip[1] = bc.nslip[2] = 0;
10307573aee6SJed Brown     }
1031ccaff030SJeremy L Thompson     for (PetscInt j=0; j<3; j++) {
1032ccaff030SJeremy L Thompson       const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
1033ccaff030SJeremy L Thompson       ierr = PetscOptionsIntArray(flags[j],
1034ccaff030SJeremy L Thompson                                   "Use slip boundary conditions on this list of faces",
1035ccaff030SJeremy L Thompson                                   NULL, bc.slips[j],
1036ccaff030SJeremy L Thompson                                   (len = sizeof(bc.slips[j]) / sizeof(bc.slips[j][0]),
1037ccaff030SJeremy L Thompson                                    &len), &flg);
1038ccaff030SJeremy L Thompson       CHKERRQ(ierr);
103984d34d69SLeila Ghaffari       if (flg) {
104084d34d69SLeila Ghaffari         bc.nslip[j] = len;
104184d34d69SLeila Ghaffari         bc.userbc = PETSC_TRUE;
104284d34d69SLeila Ghaffari       }
1043ccaff030SJeremy L Thompson     }
1044ccaff030SJeremy L Thompson   }
1045cb3e2689Svaleriabarra   ierr = PetscOptionsInt("-viz_refine",
1046cb3e2689Svaleriabarra                          "Regular refinement levels for visualization",
1047cb3e2689Svaleriabarra                          NULL, viz_refine, &viz_refine, NULL);
1048ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1049ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
1050ccaff030SJeremy L Thompson                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
1051ccaff030SJeremy L Thompson   meter = fabs(meter);
1052ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
1053ccaff030SJeremy L Thompson                             NULL, second, &second, NULL); CHKERRQ(ierr);
1054ccaff030SJeremy L Thompson   second = fabs(second);
1055ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
1056ccaff030SJeremy L Thompson                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
1057ccaff030SJeremy L Thompson   kilogram = fabs(kilogram);
1058ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_Kelvin",
1059ccaff030SJeremy L Thompson                             "1 Kelvin in scaled temperature units",
1060ccaff030SJeremy L Thompson                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
1061ccaff030SJeremy L Thompson   Kelvin = fabs(Kelvin);
1062ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
1063ccaff030SJeremy L Thompson                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
1064ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
1065ccaff030SJeremy L Thompson                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
1066ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
1067ccaff030SJeremy L Thompson                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
1068*9fe13df9SLeila Ghaffari   ierr = PetscOptionsScalar("-P_left", "Inflow pressure",
1069*9fe13df9SLeila Ghaffari                             NULL, P_left, &P_left, NULL); CHKERRQ(ierr);
1070*9fe13df9SLeila Ghaffari   ierr = PetscOptionsScalar("-rho_left", "Inflow density",
1071*9fe13df9SLeila Ghaffari                             NULL, rho_left, &rho_left, NULL); CHKERRQ(ierr);
1072ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
1073ccaff030SJeremy L Thompson                             NULL, N, &N, NULL); CHKERRQ(ierr);
1074ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
1075ccaff030SJeremy L Thompson                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
1076ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
1077ccaff030SJeremy L Thompson                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
1078ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
1079ccaff030SJeremy L Thompson                             NULL, g, &g, NULL); CHKERRQ(ierr);
1080ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lambda",
1081ccaff030SJeremy L Thompson                             "Stokes hypothesis second viscosity coefficient",
1082ccaff030SJeremy L Thompson                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
1083ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
1084ccaff030SJeremy L Thompson                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
1085ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
1086ccaff030SJeremy L Thompson                             NULL, k, &k, NULL); CHKERRQ(ierr);
1087ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-CtauS",
1088ccaff030SJeremy L Thompson                             "Scale coefficient for tau (nondimensional)",
1089ccaff030SJeremy L Thompson                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
109084d34d69SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
109184d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
109284d34d69SLeila Ghaffari                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
109384d34d69SLeila Ghaffari     CHKERRQ(ierr);
109484d34d69SLeila Ghaffari   }
1095ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-strong_form",
1096ccaff030SJeremy L Thompson                             "Strong (1) or weak/integrated by parts (0) advection residual",
1097ccaff030SJeremy L Thompson                             NULL, strong_form, &strong_form, NULL);
1098ccaff030SJeremy L Thompson   CHKERRQ(ierr);
109984d34d69SLeila Ghaffari   if (problemChoice == NS_DENSITY_CURRENT && (CtauS != 0 || strong_form != 0)) {
110084d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
110184d34d69SLeila Ghaffari                        "Warning! Problem density_current does not support -CtauS or -strong_form\n");
110284d34d69SLeila Ghaffari     CHKERRQ(ierr);
110384d34d69SLeila Ghaffari   }
1104ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
1105ccaff030SJeremy L Thompson                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
1106ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
1107ccaff030SJeremy L Thompson                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
1108ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
1109ccaff030SJeremy L Thompson                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
1110ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
1111ccaff030SJeremy L Thompson                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
1112ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resx","Target resolution in x",
1113ccaff030SJeremy L Thompson                             NULL, resx, &resx, NULL); CHKERRQ(ierr);
1114ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resy","Target resolution in y",
1115ccaff030SJeremy L Thompson                             NULL, resy, &resy, NULL); CHKERRQ(ierr);
1116ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resz","Target resolution in z",
1117ccaff030SJeremy L Thompson                             NULL, resz, &resz, NULL); CHKERRQ(ierr);
1118ccaff030SJeremy L Thompson   PetscInt n = problem->dim;
1119ccaff030SJeremy L Thompson   center[0] = 0.5 * lx;
1120ccaff030SJeremy L Thompson   center[1] = 0.5 * ly;
1121ccaff030SJeremy L Thompson   center[2] = 0.5 * lz;
1122ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
1123ccaff030SJeremy L Thompson                                NULL, center, &n, NULL); CHKERRQ(ierr);
1124ccaff030SJeremy L Thompson   n = problem->dim;
1125ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-dc_axis",
1126ccaff030SJeremy L Thompson                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
1127ccaff030SJeremy L Thompson                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
1128ccaff030SJeremy L Thompson   {
1129ccaff030SJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) +
1130ccaff030SJeremy L Thompson                                    PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
1131ccaff030SJeremy L Thompson     if (norm > 0) {
1132ccaff030SJeremy L Thompson       for (int i=0; i<3; i++) dc_axis[i] /= norm;
1133ccaff030SJeremy L Thompson     }
1134ccaff030SJeremy L Thompson   }
1135ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-output_freq",
1136ccaff030SJeremy L Thompson                          "Frequency of output, in number of steps",
1137ccaff030SJeremy L Thompson                          NULL, outputfreq, &outputfreq, NULL); CHKERRQ(ierr);
1138ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-continue", "Continue from previous solution",
1139ccaff030SJeremy L Thompson                          NULL, contsteps, &contsteps, NULL); CHKERRQ(ierr);
114084d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
114184d34d69SLeila Ghaffari                          NULL, degree, &degree, NULL); CHKERRQ(ierr);
114284d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-qextra", "Number of extra quadrature points",
114384d34d69SLeila Ghaffari                          NULL, qextra, &qextra, NULL); CHKERRQ(ierr);
114484d34d69SLeila Ghaffari   ierr = PetscStrncpy(user->outputfolder, ".", 2); CHKERRQ(ierr);
1145ccaff030SJeremy L Thompson   ierr = PetscOptionsString("-of", "Output folder",
1146ccaff030SJeremy L Thompson                             NULL, user->outputfolder, user->outputfolder,
1147ccaff030SJeremy L Thompson                             sizeof(user->outputfolder), NULL); CHKERRQ(ierr);
114884d34d69SLeila Ghaffari   memtyperequested = petschavecuda ? CEED_MEM_DEVICE : CEED_MEM_HOST;
114984d34d69SLeila Ghaffari   ierr = PetscOptionsEnum("-memtype",
115084d34d69SLeila Ghaffari                           "CEED MemType requested", NULL,
115184d34d69SLeila Ghaffari                           memTypes, (PetscEnum)memtyperequested,
115284d34d69SLeila Ghaffari                           (PetscEnum *)&memtyperequested, &setmemtyperequest);
115384d34d69SLeila Ghaffari   CHKERRQ(ierr);
1154ccaff030SJeremy L Thompson   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
1155ccaff030SJeremy L Thompson 
1156ccaff030SJeremy L Thompson   // Define derived units
1157ccaff030SJeremy L Thompson   Pascal = kilogram / (meter * PetscSqr(second));
1158ccaff030SJeremy L Thompson   JperkgK =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
1159ccaff030SJeremy L Thompson   mpersquareds = meter / PetscSqr(second);
1160ccaff030SJeremy L Thompson   WpermK = kilogram * meter / (pow(second,3) * Kelvin);
1161ccaff030SJeremy L Thompson   kgpercubicm = kilogram / pow(meter,3);
1162ccaff030SJeremy L Thompson   kgpersquaredms = kilogram / (PetscSqr(meter) * second);
1163ccaff030SJeremy L Thompson   Joulepercubicm = kilogram / (meter * PetscSqr(second));
1164ccaff030SJeremy L Thompson 
1165ccaff030SJeremy L Thompson   // Scale variables to desired units
1166ccaff030SJeremy L Thompson   theta0 *= Kelvin;
1167ccaff030SJeremy L Thompson   thetaC *= Kelvin;
1168ccaff030SJeremy L Thompson   P0 *= Pascal;
1169*9fe13df9SLeila Ghaffari   P_left *= Pascal;
1170*9fe13df9SLeila Ghaffari   rho_left *= kgpercubicm;
1171ccaff030SJeremy L Thompson   N *= (1./second);
1172ccaff030SJeremy L Thompson   cv *= JperkgK;
1173ccaff030SJeremy L Thompson   cp *= JperkgK;
1174ccaff030SJeremy L Thompson   Rd = cp - cv;
1175ccaff030SJeremy L Thompson   g *= mpersquareds;
1176ccaff030SJeremy L Thompson   mu *= Pascal * second;
1177ccaff030SJeremy L Thompson   k *= WpermK;
1178ccaff030SJeremy L Thompson   lx = fabs(lx) * meter;
1179ccaff030SJeremy L Thompson   ly = fabs(ly) * meter;
1180ccaff030SJeremy L Thompson   lz = fabs(lz) * meter;
1181ccaff030SJeremy L Thompson   rc = fabs(rc) * meter;
1182ccaff030SJeremy L Thompson   resx = fabs(resx) * meter;
1183ccaff030SJeremy L Thompson   resy = fabs(resy) * meter;
1184ccaff030SJeremy L Thompson   resz = fabs(resz) * meter;
1185ccaff030SJeremy L Thompson   for (int i=0; i<3; i++) center[i] *= meter;
1186ccaff030SJeremy L Thompson 
1187ccaff030SJeremy L Thompson   const CeedInt dim = problem->dim, ncompx = problem->dim,
1188cfa64770SLeila Ghaffari                 qdatasizeVol = problem->qdatasizeVol;
1189ccaff030SJeremy L Thompson   // Set up the libCEED context
1190ccaff030SJeremy L Thompson   struct SetupContext_ ctxSetup = {
1191ccaff030SJeremy L Thompson     .theta0 = theta0,
1192ccaff030SJeremy L Thompson     .thetaC = thetaC,
1193ccaff030SJeremy L Thompson     .P0 = P0,
1194ccaff030SJeremy L Thompson     .N = N,
1195ccaff030SJeremy L Thompson     .cv = cv,
1196ccaff030SJeremy L Thompson     .cp = cp,
1197ccaff030SJeremy L Thompson     .Rd = Rd,
1198ccaff030SJeremy L Thompson     .g = g,
1199ccaff030SJeremy L Thompson     .rc = rc,
1200ccaff030SJeremy L Thompson     .lx = lx,
1201ccaff030SJeremy L Thompson     .ly = ly,
1202ccaff030SJeremy L Thompson     .lz = lz,
1203ccaff030SJeremy L Thompson     .center[0] = center[0],
1204ccaff030SJeremy L Thompson     .center[1] = center[1],
1205ccaff030SJeremy L Thompson     .center[2] = center[2],
1206ccaff030SJeremy L Thompson     .dc_axis[0] = dc_axis[0],
1207ccaff030SJeremy L Thompson     .dc_axis[1] = dc_axis[1],
1208ccaff030SJeremy L Thompson     .dc_axis[2] = dc_axis[2],
1209ccaff030SJeremy L Thompson     .time = 0,
1210ccaff030SJeremy L Thompson   };
1211ccaff030SJeremy L Thompson 
121284d34d69SLeila Ghaffari   // Create the mesh
1213ccaff030SJeremy L Thompson   {
1214ccaff030SJeremy L Thompson     const PetscReal scale[3] = {lx, ly, lz};
1215ccaff030SJeremy L Thompson     ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_FALSE, NULL, NULL, scale,
121684d34d69SLeila Ghaffari                                NULL, PETSC_TRUE, &dm);
1217ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1218ccaff030SJeremy L Thompson   }
121984d34d69SLeila Ghaffari 
122084d34d69SLeila Ghaffari   // Distribute the mesh over processes
122184d34d69SLeila Ghaffari   {
1222ccaff030SJeremy L Thompson     DM               dmDist = NULL;
1223ccaff030SJeremy L Thompson     PetscPartitioner part;
1224ccaff030SJeremy L Thompson 
1225ccaff030SJeremy L Thompson     ierr = DMPlexGetPartitioner(dm, &part); CHKERRQ(ierr);
1226ccaff030SJeremy L Thompson     ierr = PetscPartitionerSetFromOptions(part); CHKERRQ(ierr);
1227ccaff030SJeremy L Thompson     ierr = DMPlexDistribute(dm, 0, NULL, &dmDist); CHKERRQ(ierr);
1228ccaff030SJeremy L Thompson     if (dmDist) {
1229ccaff030SJeremy L Thompson       ierr = DMDestroy(&dm); CHKERRQ(ierr);
1230ccaff030SJeremy L Thompson       dm  = dmDist;
1231ccaff030SJeremy L Thompson     }
1232ccaff030SJeremy L Thompson   }
1233ccaff030SJeremy L Thompson   ierr = DMViewFromOptions(dm, NULL, "-dm_view"); CHKERRQ(ierr);
1234ccaff030SJeremy L Thompson 
123584d34d69SLeila Ghaffari   // Setup DM
1236ccaff030SJeremy L Thompson   ierr = DMLocalizeCoordinates(dm); CHKERRQ(ierr);
1237ccaff030SJeremy L Thompson   ierr = DMSetFromOptions(dm); CHKERRQ(ierr);
123884d34d69SLeila Ghaffari   ierr = SetUpDM(dm, problem, degree, &bc, &ctxSetup); CHKERRQ(ierr);
123984d34d69SLeila Ghaffari 
124084d34d69SLeila Ghaffari   // Refine DM for high-order viz
1241ccaff030SJeremy L Thompson   dmviz = NULL;
1242ccaff030SJeremy L Thompson   interpviz = NULL;
1243ccaff030SJeremy L Thompson   if (viz_refine) {
1244ff6701fcSJed Brown     DM dmhierarchy[viz_refine+1];
1245ff6701fcSJed Brown 
1246ccaff030SJeremy L Thompson     ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
1247ff6701fcSJed Brown     dmhierarchy[0] = dm;
124884d34d69SLeila Ghaffari     for (PetscInt i = 0, d = degree; i < viz_refine; i++) {
1249ff6701fcSJed Brown       Mat interp_next;
1250ff6701fcSJed Brown 
1251ff6701fcSJed Brown       ierr = DMRefine(dmhierarchy[i], MPI_COMM_NULL, &dmhierarchy[i+1]);
1252ccaff030SJeremy L Thompson       CHKERRQ(ierr);
1253ff6701fcSJed Brown       ierr = DMSetCoarseDM(dmhierarchy[i+1], dmhierarchy[i]); CHKERRQ(ierr);
1254ff6701fcSJed Brown       d = (d + 1) / 2;
1255ff6701fcSJed Brown       if (i + 1 == viz_refine) d = 1;
1256ff6701fcSJed Brown       ierr = SetUpDM(dmhierarchy[i+1], problem, d, &bc, &ctxSetup); CHKERRQ(ierr);
1257ff6701fcSJed Brown       ierr = DMCreateInterpolation(dmhierarchy[i], dmhierarchy[i+1],
1258ff6701fcSJed Brown                                    &interp_next, NULL); CHKERRQ(ierr);
1259ff6701fcSJed Brown       if (!i) interpviz = interp_next;
1260ff6701fcSJed Brown       else {
1261ff6701fcSJed Brown         Mat C;
1262ff6701fcSJed Brown         ierr = MatMatMult(interp_next, interpviz, MAT_INITIAL_MATRIX,
1263ff6701fcSJed Brown                           PETSC_DECIDE, &C); CHKERRQ(ierr);
1264ff6701fcSJed Brown         ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
1265ff6701fcSJed Brown         ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1266ff6701fcSJed Brown         interpviz = C;
1267ff6701fcSJed Brown       }
1268ff6701fcSJed Brown     }
1269cb3e2689Svaleriabarra     for (PetscInt i=1; i<viz_refine; i++) {
1270ff6701fcSJed Brown       ierr = DMDestroy(&dmhierarchy[i]); CHKERRQ(ierr);
1271cb3e2689Svaleriabarra     }
1272ff6701fcSJed Brown     dmviz = dmhierarchy[viz_refine];
1273ccaff030SJeremy L Thompson   }
1274ccaff030SJeremy L Thompson   ierr = DMCreateGlobalVector(dm, &Q); CHKERRQ(ierr);
1275ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Qloc); CHKERRQ(ierr);
1276ccaff030SJeremy L Thompson   ierr = VecGetSize(Qloc, &lnodes); CHKERRQ(ierr);
1277ccaff030SJeremy L Thompson   lnodes /= ncompq;
1278ccaff030SJeremy L Thompson 
127984d34d69SLeila Ghaffari   // Initialize CEED
128084d34d69SLeila Ghaffari   CeedInit(ceedresource, &ceed);
128184d34d69SLeila Ghaffari   // Set memtype
128284d34d69SLeila Ghaffari   CeedMemType memtypebackend;
128384d34d69SLeila Ghaffari   CeedGetPreferredMemType(ceed, &memtypebackend);
128484d34d69SLeila Ghaffari   // Check memtype compatibility
128584d34d69SLeila Ghaffari   if (!setmemtyperequest)
128684d34d69SLeila Ghaffari     memtyperequested = memtypebackend;
128784d34d69SLeila Ghaffari   else if (!petschavecuda && memtyperequested == CEED_MEM_DEVICE)
128884d34d69SLeila Ghaffari     SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS,
128984d34d69SLeila Ghaffari              "PETSc was not built with CUDA. "
129084d34d69SLeila Ghaffari              "Requested MemType CEED_MEM_DEVICE is not supported.", NULL);
129184d34d69SLeila Ghaffari 
129284d34d69SLeila Ghaffari   // Set number of 1D nodes and quadrature points
129384d34d69SLeila Ghaffari   numP = degree + 1;
129484d34d69SLeila Ghaffari   numQ = numP + qextra;
129584d34d69SLeila Ghaffari 
129684d34d69SLeila Ghaffari     // Print summary
129784d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1298ccaff030SJeremy L Thompson     CeedInt gdofs, odofs;
1299ccaff030SJeremy L Thompson     int comm_size;
1300ccaff030SJeremy L Thompson     char box_faces_str[PETSC_MAX_PATH_LEN] = "NONE";
1301ccaff030SJeremy L Thompson     ierr = VecGetSize(Q, &gdofs); CHKERRQ(ierr);
1302ccaff030SJeremy L Thompson     ierr = VecGetLocalSize(Q, &odofs); CHKERRQ(ierr);
130384d34d69SLeila Ghaffari     gnodes = gdofs/ncompq;
1304ccaff030SJeremy L Thompson     ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr);
1305ccaff030SJeremy L Thompson     ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str,
1306ccaff030SJeremy L Thompson                                  sizeof(box_faces_str), NULL); CHKERRQ(ierr);
130784d34d69SLeila Ghaffari     const char *usedresource;
130884d34d69SLeila Ghaffari     CeedGetResource(ceed, &usedresource);
1309ccaff030SJeremy L Thompson 
131084d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
131184d34d69SLeila Ghaffari                        "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
131284d34d69SLeila Ghaffari                        "  rank(s)                              : %d\n"
131384d34d69SLeila Ghaffari                        "  Problem:\n"
131484d34d69SLeila Ghaffari                        "    Problem Name                       : %s\n"
131584d34d69SLeila Ghaffari                        "    Stabilization                      : %s\n"
131684d34d69SLeila Ghaffari                        "  PETSc:\n"
131784d34d69SLeila Ghaffari                        "    Box Faces                          : %s\n"
131884d34d69SLeila Ghaffari                        "  libCEED:\n"
131984d34d69SLeila Ghaffari                        "    libCEED Backend                    : %s\n"
132084d34d69SLeila Ghaffari                        "    libCEED Backend MemType            : %s\n"
132184d34d69SLeila Ghaffari                        "    libCEED User Requested MemType     : %s\n"
132284d34d69SLeila Ghaffari                        "  Mesh:\n"
132384d34d69SLeila Ghaffari                        "    Number of 1D Basis Nodes (P)       : %d\n"
132484d34d69SLeila Ghaffari                        "    Number of 1D Quadrature Points (Q) : %d\n"
132584d34d69SLeila Ghaffari                        "    Global DoFs                        : %D\n"
132684d34d69SLeila Ghaffari                        "    Owned DoFs                         : %D\n"
132784d34d69SLeila Ghaffari                        "    DoFs per node                      : %D\n"
132884d34d69SLeila Ghaffari                        "    Global nodes                       : %D\n"
132984d34d69SLeila Ghaffari                        "    Owned nodes                        : %D\n",
133084d34d69SLeila Ghaffari                        comm_size, problemTypes[problemChoice],
133184d34d69SLeila Ghaffari                        StabilizationTypes[stab], box_faces_str, usedresource,
133284d34d69SLeila Ghaffari                        CeedMemTypes[memtypebackend],
133384d34d69SLeila Ghaffari                        (setmemtyperequest) ?
133484d34d69SLeila Ghaffari                        CeedMemTypes[memtyperequested] : "none",
133584d34d69SLeila Ghaffari                        numP, numQ, gdofs, odofs, ncompq, gnodes, lnodes);
133684d34d69SLeila Ghaffari     CHKERRQ(ierr);
13370c6c0b13SLeila Ghaffari   }
13380c6c0b13SLeila Ghaffari 
1339ccaff030SJeremy L Thompson   // Set up global mass vector
1340ccaff030SJeremy L Thompson   ierr = VecDuplicate(Q, &user->M); CHKERRQ(ierr);
1341ccaff030SJeremy L Thompson 
134284d34d69SLeila Ghaffari   // Set up libCEED
1343ccaff030SJeremy L Thompson   // CEED Bases
1344ccaff030SJeremy L Thompson   CeedInit(ceedresource, &ceed);
134584d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompq, numP, numQ, CEED_GAUSS,
134684d34d69SLeila Ghaffari                                   &basisq);
134784d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numQ, CEED_GAUSS,
134884d34d69SLeila Ghaffari                                   &basisx);
134984d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numP,
135084d34d69SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxc);
1351ccaff030SJeremy L Thompson   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
1352ccaff030SJeremy L Thompson   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
1353ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1354ccaff030SJeremy L Thompson 
1355ccaff030SJeremy L Thompson   // CEED Restrictions
13561e150236SLeila Ghaffari   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, numP, numQ,
135784d34d69SLeila Ghaffari                                  qdatasizeVol, &restrictq, &restrictx,
135884d34d69SLeila Ghaffari                                  &restrictqdi); CHKERRQ(ierr);
1359ccaff030SJeremy L Thompson 
1360ccaff030SJeremy L Thompson   ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
1361ccaff030SJeremy L Thompson   ierr = CreateVectorFromPetscVec(ceed, Xloc, &xcorners); CHKERRQ(ierr);
1362ccaff030SJeremy L Thompson 
1363ccaff030SJeremy L Thompson   // Create the CEED vectors that will be needed in setup
1364bd910870SLeila Ghaffari   CeedInt NqptsVol;
136584d34d69SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisq, &NqptsVol);
136684d34d69SLeila Ghaffari   CeedElemRestrictionGetNumElements(restrictq, &localNelemVol);
13678b982baeSLeila Ghaffari   CeedVectorCreate(ceed, qdatasizeVol*localNelemVol*NqptsVol, &qdata);
136884d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &q0ceed, NULL);
1369ccaff030SJeremy L Thompson 
1370ccaff030SJeremy L Thompson   // Create the Q-function that builds the quadrature data for the NS operator
1371ea6e0f84SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupVol, problem->setupVol_loc,
1372ea6e0f84SLeila Ghaffari                               &qf_setupVol);
1373ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "dx", ncompx*dim, CEED_EVAL_GRAD);
1374ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "weight", 1, CEED_EVAL_WEIGHT);
13758b982baeSLeila Ghaffari   CeedQFunctionAddOutput(qf_setupVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1376ccaff030SJeremy L Thompson 
1377ccaff030SJeremy L Thompson   // Create the Q-function that sets the ICs of the operator
1378ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, problem->ics, problem->ics_loc, &qf_ics);
1379ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_ics, "x", ncompx, CEED_EVAL_INTERP);
1380ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_ics, "q0", ncompq, CEED_EVAL_NONE);
1381ccaff030SJeremy L Thompson 
1382ea6e0f84SLeila Ghaffari   qf_rhsVol = NULL;
1383ea6e0f84SLeila Ghaffari   if (problem->applyVol_rhs) { // Create the Q-function that defines the action of the RHS operator
1384ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_rhs,
1385ea6e0f84SLeila Ghaffari                                 problem->applyVol_rhs_loc, &qf_rhsVol);
1386ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "q", ncompq, CEED_EVAL_INTERP);
1387ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
13888b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1389ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "x", ncompx, CEED_EVAL_INTERP);
1390ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "v", ncompq, CEED_EVAL_INTERP);
1391ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1392ccaff030SJeremy L Thompson   }
1393ccaff030SJeremy L Thompson 
1394ea6e0f84SLeila Ghaffari   qf_ifunctionVol = NULL;
1395ea6e0f84SLeila Ghaffari   if (problem->applyVol_ifunction) { // Create the Q-function that defines the action of the IFunction
1396ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_ifunction,
1397ea6e0f84SLeila Ghaffari                                 problem->applyVol_ifunction_loc, &qf_ifunctionVol);
1398ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "q", ncompq, CEED_EVAL_INTERP);
1399ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
1400ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdot", ncompq, CEED_EVAL_INTERP);
14018b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1402ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "x", ncompx, CEED_EVAL_INTERP);
1403ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "v", ncompq, CEED_EVAL_INTERP);
1404ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1405ccaff030SJeremy L Thompson   }
1406ccaff030SJeremy L Thompson 
1407ccaff030SJeremy L Thompson   // Create the operator that builds the quadrature data for the NS operator
1408ea6e0f84SLeila Ghaffari   CeedOperatorCreate(ceed, qf_setupVol, NULL, NULL, &op_setupVol);
140984d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "dx", restrictx, basisx, CEED_VECTOR_ACTIVE);
1410ea6e0f84SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "weight", CEED_ELEMRESTRICTION_NONE,
141184d34d69SLeila Ghaffari                        basisx, CEED_VECTOR_NONE);
141284d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "qdata", restrictqdi,
1413ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1414ccaff030SJeremy L Thompson 
1415ccaff030SJeremy L Thompson   // Create the operator that sets the ICs
1416ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics);
141784d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "x", restrictx, basisxc, CEED_VECTOR_ACTIVE);
141884d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "q0", restrictq,
1419ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1420ccaff030SJeremy L Thompson 
142184d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qceed, NULL);
142284d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qdotceed, NULL);
142384d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->gceed, NULL);
1424ccaff030SJeremy L Thompson 
1425ea6e0f84SLeila Ghaffari   if (qf_rhsVol) { // Create the RHS physics operator
1426ccaff030SJeremy L Thompson     CeedOperator op;
1427ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_rhsVol, NULL, NULL, &op);
142884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
142984d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
143084d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
14318b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
143284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
143384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
143484d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1435d3630711SJed Brown     user->op_rhs_vol = op;
1436ccaff030SJeremy L Thompson   }
1437ccaff030SJeremy L Thompson 
1438ea6e0f84SLeila Ghaffari   if (qf_ifunctionVol) { // Create the IFunction operator
1439ccaff030SJeremy L Thompson     CeedOperator op;
1440ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_ifunctionVol, NULL, NULL, &op);
144184d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
144284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
144384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdot", restrictq, basisq, user->qdotceed);
144484d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
14458b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
144684d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
144784d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
144884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1449d3630711SJed Brown     user->op_ifunction_vol = op;
1450ccaff030SJeremy L Thompson   }
1451ccaff030SJeremy L Thompson 
1452cfa64770SLeila Ghaffari   //--------------------------------------------------------------------------------------//
1453*9fe13df9SLeila Ghaffari   // In/OutFlow Boundary Conditions
14546a0edaf9SLeila Ghaffari   //--------------------------------------------------------------------------------------//
14556a0edaf9SLeila Ghaffari   // Set up CEED for the boundaries
14566a0edaf9SLeila Ghaffari   CeedInt height = 1;
14576a0edaf9SLeila Ghaffari   CeedInt dimSur = dim - height;
14581e150236SLeila Ghaffari   CeedInt numP_Sur = degree + 1;
14591e150236SLeila Ghaffari   CeedInt numQ_Sur = numP_Sur + qextraSur;
1460cfa64770SLeila Ghaffari   const CeedInt qdatasizeSur = problem->qdatasizeSur;
1461cfa64770SLeila Ghaffari   CeedBasis basisxSur, basisxcSur, basisqSur;
1462cfa64770SLeila Ghaffari   CeedInt NqptsSur;
1463*9fe13df9SLeila Ghaffari   CeedQFunction qf_setupSur, qf_rhsOut, qf_ifunctionOut, qf_rhsIn, qf_ifunctionIn;
1464cfa64770SLeila Ghaffari 
1465cfa64770SLeila Ghaffari   // CEED bases for the boundaries
14666a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompq, numP_Sur, numQ_Sur, CEED_GAUSS,
14676a0edaf9SLeila Ghaffari                                   &basisqSur);
14686a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numQ_Sur, CEED_GAUSS,
14696a0edaf9SLeila Ghaffari                                   &basisxSur);
14706a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numP_Sur,
14716a0edaf9SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxcSur);
14726a0edaf9SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisqSur, &NqptsSur);
14736a0edaf9SLeila Ghaffari 
1474cfa64770SLeila Ghaffari   // Create the Q-function that builds the quadrature data for the Surface operator
14756a0edaf9SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupSur, problem->setupSur_loc,
14766a0edaf9SLeila Ghaffari                               &qf_setupSur);
14776a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "dx", ncompx*dimSur, CEED_EVAL_GRAD);
14786a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "weight", 1, CEED_EVAL_WEIGHT);
14796a0edaf9SLeila Ghaffari   CeedQFunctionAddOutput(qf_setupSur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
14806a0edaf9SLeila Ghaffari 
1481*9fe13df9SLeila Ghaffari   // Creat Q-Function for OutFlow BCs
14825603407fSLeila Ghaffari   qf_rhsOut = NULL;
1483*9fe13df9SLeila Ghaffari   if (problem->applyOut_rhs) { // Create the Q-function that defines the action of the RHS operator
14845603407fSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyOut_rhs,
14855603407fSLeila Ghaffari                                 problem->applyOut_rhs_loc, &qf_rhsOut);
14865603407fSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsOut, "q", ncompq, CEED_EVAL_INTERP);
14875603407fSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsOut, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
14885603407fSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsOut, "x", ncompx, CEED_EVAL_INTERP);
14895603407fSLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsOut, "v", ncompq, CEED_EVAL_INTERP);
14906a0edaf9SLeila Ghaffari   }
14915603407fSLeila Ghaffari   qf_ifunctionOut = NULL;
14925603407fSLeila Ghaffari   if (problem->applyOut_ifunction) { // Create the Q-function that defines the action of the IFunction
14935603407fSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyOut_ifunction,
14945603407fSLeila Ghaffari                                 problem->applyOut_ifunction_loc, &qf_ifunctionOut);
14955603407fSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionOut, "q", ncompq, CEED_EVAL_INTERP);
14965603407fSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionOut, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
14975603407fSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionOut, "x", ncompx, CEED_EVAL_INTERP);
14985603407fSLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionOut, "v", ncompq, CEED_EVAL_INTERP);
14996a0edaf9SLeila Ghaffari   }
1500*9fe13df9SLeila Ghaffari 
1501*9fe13df9SLeila Ghaffari   // Creat Q-Function for InFlow BCs
1502*9fe13df9SLeila Ghaffari   qf_rhsIn = NULL;
1503*9fe13df9SLeila Ghaffari   if (problem->applyIn_rhs) { // Create the Q-function that defines the action of the RHS operator
1504*9fe13df9SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyIn_rhs,
1505*9fe13df9SLeila Ghaffari                                 problem->applyIn_rhs_loc, &qf_rhsIn);
1506*9fe13df9SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsIn, "q", ncompq, CEED_EVAL_INTERP);
1507*9fe13df9SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsIn, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
1508*9fe13df9SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsIn, "x", ncompx, CEED_EVAL_INTERP);
1509*9fe13df9SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsIn, "v", ncompq, CEED_EVAL_INTERP);
1510*9fe13df9SLeila Ghaffari   }
1511*9fe13df9SLeila Ghaffari   qf_ifunctionIn = NULL;
1512*9fe13df9SLeila Ghaffari   if (problem->applyIn_ifunction) { // Create the Q-function that defines the action of the IFunction
1513*9fe13df9SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyIn_ifunction,
1514*9fe13df9SLeila Ghaffari                                 problem->applyIn_ifunction_loc, &qf_ifunctionIn);
1515*9fe13df9SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionIn, "q", ncompq, CEED_EVAL_INTERP);
1516*9fe13df9SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionIn, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
1517*9fe13df9SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionIn, "x", ncompx, CEED_EVAL_INTERP);
1518*9fe13df9SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionIn, "v", ncompq, CEED_EVAL_INTERP);
1519*9fe13df9SLeila Ghaffari   }
1520*9fe13df9SLeila Ghaffari 
1521*9fe13df9SLeila Ghaffari   // Create CEED Operator for the whole domain
1522*9fe13df9SLeila Ghaffari   if (!implicit)
1523*9fe13df9SLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, user->op_rhs_vol, qf_rhsOut, qf_rhsIn, qf_setupSur, height,
1524*9fe13df9SLeila Ghaffari                                   bc.noutflow, bc.outflow, bc.ninflow, bc.inflow, numP_Sur, numQ_Sur, qdatasizeSur,
15251e150236SLeila Ghaffari                                   NqptsSur, basisxSur, basisqSur, &user->op_rhs);
1526ca3ac6ddSLeila Ghaffari                                   CHKERRQ(ierr);
1527*9fe13df9SLeila Ghaffari   if (implicit)
1528*9fe13df9SLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, user->op_ifunction_vol, qf_ifunctionOut, qf_ifunctionIn, qf_setupSur, height,
1529*9fe13df9SLeila Ghaffari                                   bc.noutflow, bc.outflow, bc.ninflow, bc.inflow, numP_Sur, numQ_Sur, qdatasizeSur,
15301e150236SLeila Ghaffari                                   NqptsSur, basisxSur, basisqSur, &user->op_ifunction);
1531ca3ac6ddSLeila Ghaffari                                   CHKERRQ(ierr);
1532*9fe13df9SLeila Ghaffari 
1533cfa64770SLeila Ghaffari   //--------------------------------------------------------------------------------------//
1534ccaff030SJeremy L Thompson   CeedQFunctionSetContext(qf_ics, &ctxSetup, sizeof ctxSetup);
1535ccaff030SJeremy L Thompson   CeedScalar ctxNS[8] = {lambda, mu, k, cv, cp, g, Rd};
1536*9fe13df9SLeila Ghaffari   CeedScalar ctxIn[5] = {cv, cp, Rd, P_left, rho_left};
1537ccaff030SJeremy L Thompson   struct Advection2dContext_ ctxAdvection2d = {
1538ccaff030SJeremy L Thompson     .CtauS = CtauS,
1539ccaff030SJeremy L Thompson     .strong_form = strong_form,
1540ccaff030SJeremy L Thompson     .stabilization = stab,
1541ccaff030SJeremy L Thompson   };
1542ccaff030SJeremy L Thompson   switch (problemChoice) {
1543ccaff030SJeremy L Thompson   case NS_DENSITY_CURRENT:
1544ea6e0f84SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxNS, sizeof ctxNS);
1545ea6e0f84SLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, &ctxNS,
1546ccaff030SJeremy L Thompson           sizeof ctxNS);
15475603407fSLeila Ghaffari     if (qf_rhsOut) CeedQFunctionSetContext(qf_rhsOut, &ctxNS, sizeof ctxNS);
15485603407fSLeila Ghaffari     if (qf_ifunctionOut) CeedQFunctionSetContext(qf_ifunctionOut, &ctxNS,
15496a0edaf9SLeila Ghaffari           sizeof ctxNS);
1550*9fe13df9SLeila Ghaffari     if (qf_rhsIn) CeedQFunctionSetContext(qf_rhsIn, &ctxIn, sizeof ctxIn);
1551*9fe13df9SLeila Ghaffari     if (qf_ifunctionIn) CeedQFunctionSetContext(qf_ifunctionIn, &ctxIn, sizeof ctxIn);
1552ccaff030SJeremy L Thompson     break;
1553ccaff030SJeremy L Thompson   case NS_ADVECTION:
1554ccaff030SJeremy L Thompson   case NS_ADVECTION2D:
1555ea6e0f84SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxAdvection2d,
1556ccaff030SJeremy L Thompson           sizeof ctxAdvection2d);
1557ea6e0f84SLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, &ctxAdvection2d,
1558ccaff030SJeremy L Thompson           sizeof ctxAdvection2d);
15595603407fSLeila Ghaffari     if (qf_rhsOut) CeedQFunctionSetContext(qf_rhsOut, &ctxAdvection2d,
15606a0edaf9SLeila Ghaffari           sizeof ctxAdvection2d);
15615603407fSLeila Ghaffari     if (qf_ifunctionOut) CeedQFunctionSetContext(qf_ifunctionOut, &ctxAdvection2d,
15626a0edaf9SLeila Ghaffari           sizeof ctxAdvection2d);
1563*9fe13df9SLeila Ghaffari     if (qf_rhsIn) CeedQFunctionSetContext(qf_rhsIn, &ctxIn, sizeof ctxIn);
1564*9fe13df9SLeila Ghaffari     if (qf_ifunctionIn) CeedQFunctionSetContext(qf_ifunctionIn, &ctxIn, sizeof ctxIn);
1565ccaff030SJeremy L Thompson   }
1566ccaff030SJeremy L Thompson 
1567ccaff030SJeremy L Thompson   // Set up PETSc context
1568ccaff030SJeremy L Thompson   // Set up units structure
1569ccaff030SJeremy L Thompson   units->meter = meter;
1570ccaff030SJeremy L Thompson   units->kilogram = kilogram;
1571ccaff030SJeremy L Thompson   units->second = second;
1572ccaff030SJeremy L Thompson   units->Kelvin = Kelvin;
1573ccaff030SJeremy L Thompson   units->Pascal = Pascal;
1574ccaff030SJeremy L Thompson   units->JperkgK = JperkgK;
1575ccaff030SJeremy L Thompson   units->mpersquareds = mpersquareds;
1576ccaff030SJeremy L Thompson   units->WpermK = WpermK;
1577ccaff030SJeremy L Thompson   units->kgpercubicm = kgpercubicm;
1578ccaff030SJeremy L Thompson   units->kgpersquaredms = kgpersquaredms;
1579ccaff030SJeremy L Thompson   units->Joulepercubicm = Joulepercubicm;
1580ccaff030SJeremy L Thompson 
1581ccaff030SJeremy L Thompson   // Set up user structure
1582ccaff030SJeremy L Thompson   user->comm = comm;
1583ccaff030SJeremy L Thompson   user->outputfreq = outputfreq;
1584ccaff030SJeremy L Thompson   user->contsteps = contsteps;
1585ccaff030SJeremy L Thompson   user->units = units;
1586ccaff030SJeremy L Thompson   user->dm = dm;
1587ccaff030SJeremy L Thompson   user->dmviz = dmviz;
1588ccaff030SJeremy L Thompson   user->interpviz = interpviz;
1589ccaff030SJeremy L Thompson   user->ceed = ceed;
1590ccaff030SJeremy L Thompson 
15918b982baeSLeila Ghaffari   // Calculate qdata and ICs
1592ccaff030SJeremy L Thompson   // Set up state global and local vectors
1593ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
1594ccaff030SJeremy L Thompson 
1595cfa64770SLeila Ghaffari   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
1596ccaff030SJeremy L Thompson 
1597ccaff030SJeremy L Thompson   // Apply Setup Ceed Operators
1598ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(xcorners, Xloc); CHKERRQ(ierr);
15998b982baeSLeila Ghaffari   CeedOperatorApply(op_setupVol, xcorners, qdata, CEED_REQUEST_IMMEDIATE);
160084d34d69SLeila Ghaffari   ierr = ComputeLumpedMassMatrix(ceed, dm, restrictq, basisq, restrictqdi, qdata,
1601ccaff030SJeremy L Thompson                                  user->M); CHKERRQ(ierr);
1602ccaff030SJeremy L Thompson 
160384d34d69SLeila Ghaffari   ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qloc, Q, restrictq,
160484d34d69SLeila Ghaffari                              &ctxSetup, 0.0); CHKERRQ(ierr);
1605ccaff030SJeremy L Thompson   if (1) { // Record boundary values from initial condition and override DMPlexInsertBoundaryValues()
1606ccaff030SJeremy L Thompson     // We use this for the main simulation DM because the reference DMPlexInsertBoundaryValues() is very slow.  If we
1607ccaff030SJeremy L Thompson     // disable this, we should still get the same results due to the problem->bc function, but with potentially much
1608ccaff030SJeremy L Thompson     // slower execution.
1609ccaff030SJeremy L Thompson     Vec Qbc;
1610ccaff030SJeremy L Thompson     ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1611ccaff030SJeremy L Thompson     ierr = VecCopy(Qloc, Qbc); CHKERRQ(ierr);
1612ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
1613ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
1614ccaff030SJeremy L Thompson     ierr = VecAXPY(Qbc, -1., Qloc); CHKERRQ(ierr);
1615ccaff030SJeremy L Thompson     ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1616ccaff030SJeremy L Thompson     ierr = PetscObjectComposeFunction((PetscObject)dm,
161784d34d69SLeila Ghaffari                                       "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS);
161884d34d69SLeila Ghaffari     CHKERRQ(ierr);
1619ccaff030SJeremy L Thompson   }
1620ccaff030SJeremy L Thompson 
1621ccaff030SJeremy L Thompson   MPI_Comm_rank(comm, &rank);
1622ccaff030SJeremy L Thompson   if (!rank) {ierr = PetscMkdir(user->outputfolder); CHKERRQ(ierr);}
1623ccaff030SJeremy L Thompson   // Gather initial Q values
1624ccaff030SJeremy L Thompson   // In case of continuation of simulation, set up initial values from binary file
1625ccaff030SJeremy L Thompson   if (contsteps) { // continue from existent solution
1626ccaff030SJeremy L Thompson     PetscViewer viewer;
1627ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1628ccaff030SJeremy L Thompson     // Read input
1629ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
1630ccaff030SJeremy L Thompson                          user->outputfolder);
1631ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1632ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1633ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1634ccaff030SJeremy L Thompson     ierr = VecLoad(Q, viewer); CHKERRQ(ierr);
1635ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1636ccaff030SJeremy L Thompson   }
1637ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Qloc); CHKERRQ(ierr);
1638ccaff030SJeremy L Thompson 
1639ccaff030SJeremy L Thompson // Create and setup TS
1640ccaff030SJeremy L Thompson   ierr = TSCreate(comm, &ts); CHKERRQ(ierr);
1641ccaff030SJeremy L Thompson   ierr = TSSetDM(ts, dm); CHKERRQ(ierr);
1642ccaff030SJeremy L Thompson   if (implicit) {
1643ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSBDF); CHKERRQ(ierr);
1644ccaff030SJeremy L Thompson     if (user->op_ifunction) {
1645ccaff030SJeremy L Thompson       ierr = TSSetIFunction(ts, NULL, IFunction_NS, &user); CHKERRQ(ierr);
1646ccaff030SJeremy L Thompson     } else {                    // Implicit integrators can fall back to using an RHSFunction
1647ccaff030SJeremy L Thompson       ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1648ccaff030SJeremy L Thompson     }
1649ccaff030SJeremy L Thompson   } else {
1650ccaff030SJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm,PETSC_ERR_ARG_NULL,
1651ccaff030SJeremy L Thompson                                  "Problem does not provide RHSFunction");
1652ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSRK); CHKERRQ(ierr);
1653ccaff030SJeremy L Thompson     ierr = TSRKSetType(ts, TSRK5F); CHKERRQ(ierr);
1654ccaff030SJeremy L Thompson     ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1655ccaff030SJeremy L Thompson   }
1656ccaff030SJeremy L Thompson   ierr = TSSetMaxTime(ts, 500. * units->second); CHKERRQ(ierr);
1657ccaff030SJeremy L Thompson   ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER); CHKERRQ(ierr);
1658ccaff030SJeremy L Thompson   ierr = TSSetTimeStep(ts, 1.e-2 * units->second); CHKERRQ(ierr);
165984d34d69SLeila Ghaffari   if (testChoice != TEST_NONE) {ierr = TSSetMaxSteps(ts, 10); CHKERRQ(ierr);}
1660ccaff030SJeremy L Thompson   ierr = TSGetAdapt(ts, &adapt); CHKERRQ(ierr);
1661ccaff030SJeremy L Thompson   ierr = TSAdaptSetStepLimits(adapt,
1662ccaff030SJeremy L Thompson                               1.e-12 * units->second,
1663ccaff030SJeremy L Thompson                               1.e2 * units->second); CHKERRQ(ierr);
1664ccaff030SJeremy L Thompson   ierr = TSSetFromOptions(ts); CHKERRQ(ierr);
1665ccaff030SJeremy L Thompson   if (!contsteps) { // print initial condition
166684d34d69SLeila Ghaffari     if (testChoice == TEST_NONE) {
1667ccaff030SJeremy L Thompson       ierr = TSMonitor_NS(ts, 0, 0., Q, user); CHKERRQ(ierr);
1668ccaff030SJeremy L Thompson     }
1669ccaff030SJeremy L Thompson   } else { // continue from time of last output
1670ccaff030SJeremy L Thompson     PetscReal time;
1671ccaff030SJeremy L Thompson     PetscInt count;
1672ccaff030SJeremy L Thompson     PetscViewer viewer;
1673ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1674ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
1675ccaff030SJeremy L Thompson                          user->outputfolder); CHKERRQ(ierr);
1676ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1677ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1678ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL);
1679ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1680ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1681ccaff030SJeremy L Thompson     ierr = TSSetTime(ts, time * user->units->second); CHKERRQ(ierr);
1682ccaff030SJeremy L Thompson   }
168384d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1684ccaff030SJeremy L Thompson     ierr = TSMonitorSet(ts, TSMonitor_NS, user, NULL); CHKERRQ(ierr);
1685ccaff030SJeremy L Thompson   }
1686ccaff030SJeremy L Thompson 
1687ccaff030SJeremy L Thompson   // Solve
1688ccaff030SJeremy L Thompson   start = MPI_Wtime();
1689ccaff030SJeremy L Thompson   ierr = PetscBarrier((PetscObject)ts); CHKERRQ(ierr);
1690ccaff030SJeremy L Thompson   ierr = TSSolve(ts, Q); CHKERRQ(ierr);
1691ccaff030SJeremy L Thompson   cpu_time_used = MPI_Wtime() - start;
1692ccaff030SJeremy L Thompson   ierr = TSGetSolveTime(ts, &ftime); CHKERRQ(ierr);
1693ccaff030SJeremy L Thompson   ierr = MPI_Allreduce(MPI_IN_PLACE, &cpu_time_used, 1, MPI_DOUBLE, MPI_MIN,
1694ccaff030SJeremy L Thompson                        comm); CHKERRQ(ierr);
169584d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1696ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
169784d34d69SLeila Ghaffari                        "Time taken for solution (sec): %g\n",
1698ccaff030SJeremy L Thompson                        (double)cpu_time_used); CHKERRQ(ierr);
1699ccaff030SJeremy L Thompson   }
1700ccaff030SJeremy L Thompson 
1701ccaff030SJeremy L Thompson   // Get error
170284d34d69SLeila Ghaffari   if (problem->non_zero_time && testChoice == TEST_NONE) {
1703ccaff030SJeremy L Thompson     Vec Qexact, Qexactloc;
1704ccaff030SJeremy L Thompson     PetscReal norm;
1705ccaff030SJeremy L Thompson     ierr = DMCreateGlobalVector(dm, &Qexact); CHKERRQ(ierr);
1706ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
1707ccaff030SJeremy L Thompson     ierr = VecGetSize(Qexactloc, &lnodes); CHKERRQ(ierr);
1708ccaff030SJeremy L Thompson 
170984d34d69SLeila Ghaffari     ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qexactloc, Qexact,
171084d34d69SLeila Ghaffari                                restrictq, &ctxSetup, ftime); CHKERRQ(ierr);
1711ccaff030SJeremy L Thompson 
1712ccaff030SJeremy L Thompson     ierr = VecAXPY(Q, -1.0, Qexact);  CHKERRQ(ierr);
1713ccaff030SJeremy L Thompson     ierr = VecNorm(Q, NORM_MAX, &norm); CHKERRQ(ierr);
1714cfa64770SLeila Ghaffari     CeedVectorDestroy(&q0ceed);
1715ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1716ccaff030SJeremy L Thompson                        "Max Error: %g\n",
1717ccaff030SJeremy L Thompson                        (double)norm); CHKERRQ(ierr);
171884d34d69SLeila Ghaffari     // Clean up vectors
171984d34d69SLeila Ghaffari     ierr = DMRestoreLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
172084d34d69SLeila Ghaffari     ierr = VecDestroy(&Qexact); CHKERRQ(ierr);
1721ccaff030SJeremy L Thompson   }
1722ccaff030SJeremy L Thompson 
1723ccaff030SJeremy L Thompson   // Output Statistics
1724ccaff030SJeremy L Thompson   ierr = TSGetStepNumber(ts,&steps); CHKERRQ(ierr);
172584d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1726ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1727ccaff030SJeremy L Thompson                        "Time integrator took %D time steps to reach final time %g\n",
1728ccaff030SJeremy L Thompson                        steps, (double)ftime); CHKERRQ(ierr);
1729ccaff030SJeremy L Thompson   }
173084d34d69SLeila Ghaffari   // Output numerical values from command line
173184d34d69SLeila Ghaffari   ierr = VecViewFromOptions(Q, NULL, "-vec_view"); CHKERRQ(ierr);
173284d34d69SLeila Ghaffari 
173384d34d69SLeila Ghaffari   // compare reference solution values with current run
173484d34d69SLeila Ghaffari   if (testChoice != TEST_NONE) {
173584d34d69SLeila Ghaffari     PetscViewer viewer;
173684d34d69SLeila Ghaffari     // Read reference file
173784d34d69SLeila Ghaffari     Vec Qref;
173884d34d69SLeila Ghaffari     PetscReal error, Qrefnorm;
173984d34d69SLeila Ghaffari     ierr = VecDuplicate(Q, &Qref); CHKERRQ(ierr);
174084d34d69SLeila Ghaffari     ierr = PetscViewerBinaryOpen(comm, test->filepath, FILE_MODE_READ, &viewer);
174184d34d69SLeila Ghaffari     CHKERRQ(ierr);
174284d34d69SLeila Ghaffari     ierr = VecLoad(Qref, viewer); CHKERRQ(ierr);
174384d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
174484d34d69SLeila Ghaffari 
174584d34d69SLeila Ghaffari     // Compute error with respect to reference solution
174684d34d69SLeila Ghaffari     ierr = VecAXPY(Q, -1.0, Qref);  CHKERRQ(ierr);
174784d34d69SLeila Ghaffari     ierr = VecNorm(Qref, NORM_MAX, &Qrefnorm); CHKERRQ(ierr);
174884d34d69SLeila Ghaffari     ierr = VecScale(Q, 1./Qrefnorm); CHKERRQ(ierr);
174984d34d69SLeila Ghaffari     ierr = VecNorm(Q, NORM_MAX, &error); CHKERRQ(ierr);
175084d34d69SLeila Ghaffari     ierr = VecDestroy(&Qref); CHKERRQ(ierr);
175184d34d69SLeila Ghaffari     // Check error
175284d34d69SLeila Ghaffari     if (error > test->testtol) {
175384d34d69SLeila Ghaffari       ierr = PetscPrintf(PETSC_COMM_WORLD,
175484d34d69SLeila Ghaffari                          "Test failed with error norm %g\n",
175584d34d69SLeila Ghaffari                          (double)error); CHKERRQ(ierr);
175684d34d69SLeila Ghaffari     }
175784d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
175884d34d69SLeila Ghaffari   }
17599cf88b28Svaleriabarra 
1760ccaff030SJeremy L Thompson   // Clean up libCEED
17618b982baeSLeila Ghaffari   CeedVectorDestroy(&qdata);
1762ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qceed);
1763ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qdotceed);
1764ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->gceed);
1765ccaff030SJeremy L Thompson   CeedVectorDestroy(&xcorners);
176684d34d69SLeila Ghaffari   CeedBasisDestroy(&basisq);
176784d34d69SLeila Ghaffari   CeedBasisDestroy(&basisx);
176884d34d69SLeila Ghaffari   CeedBasisDestroy(&basisxc);
176984d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictq);
177084d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictx);
177184d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictqdi);
1772ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupVol);
1773ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_ics);
1774ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_rhsVol);
1775ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_ifunctionVol);
1776ea6e0f84SLeila Ghaffari   CeedOperatorDestroy(&op_setupVol);
1777ccaff030SJeremy L Thompson   CeedOperatorDestroy(&op_ics);
17786a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_rhs_vol);
17796a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_ifunction_vol);
17806a0edaf9SLeila Ghaffari   CeedDestroy(&ceed);
17816a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisqSur);
17826a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxSur);
17836a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxcSur);
17846a0edaf9SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupSur);
17855603407fSLeila Ghaffari   CeedQFunctionDestroy(&qf_rhsOut);
17865603407fSLeila Ghaffari   CeedQFunctionDestroy(&qf_ifunctionOut);
1787*9fe13df9SLeila Ghaffari   CeedQFunctionDestroy(&qf_rhsIn);
1788*9fe13df9SLeila Ghaffari   CeedQFunctionDestroy(&qf_ifunctionIn);
1789ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_rhs);
1790ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_ifunction);
1791ccaff030SJeremy L Thompson 
1792ccaff030SJeremy L Thompson   // Clean up PETSc
1793ccaff030SJeremy L Thompson   ierr = VecDestroy(&Q); CHKERRQ(ierr);
1794ccaff030SJeremy L Thompson   ierr = VecDestroy(&user->M); CHKERRQ(ierr);
1795ccaff030SJeremy L Thompson   ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1796ccaff030SJeremy L Thompson   ierr = DMDestroy(&dmviz); CHKERRQ(ierr);
1797ccaff030SJeremy L Thompson   ierr = TSDestroy(&ts); CHKERRQ(ierr);
1798ccaff030SJeremy L Thompson   ierr = DMDestroy(&dm); CHKERRQ(ierr);
1799ccaff030SJeremy L Thompson   ierr = PetscFree(units); CHKERRQ(ierr);
1800ccaff030SJeremy L Thompson   ierr = PetscFree(user); CHKERRQ(ierr);
1801ccaff030SJeremy L Thompson   return PetscFinalize();
1802ccaff030SJeremy L Thompson }
1803ccaff030SJeremy L Thompson 
1804