xref: /libCEED/examples/fluids/navierstokes.c (revision 3ab4fca656a1e01d815a06ccfa0fdd49692deced)
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 
59*3ab4fca6SValeria Barra #if PETSC_VERSION_LT(3,14,0)
60*3ab4fca6SValeria Barra #  define DMAddBoundary(a,b,c,d,e,f,g,h,i,j,k,l) DMAddBoundary(a,b,c,d,e,f,g,h,j,k,l)
61*3ab4fca6SValeria Barra #endif
62*3ab4fca6SValeria Barra 
6384d34d69SLeila Ghaffari // MemType Options
6484d34d69SLeila Ghaffari static const char *const memTypes[] = {
6584d34d69SLeila Ghaffari   "host",
6684d34d69SLeila Ghaffari   "device",
6784d34d69SLeila Ghaffari   "memType", "CEED_MEM_", NULL
6884d34d69SLeila Ghaffari };
6984d34d69SLeila Ghaffari 
70ccaff030SJeremy L Thompson // Problem Options
71ccaff030SJeremy L Thompson typedef enum {
72ccaff030SJeremy L Thompson   NS_DENSITY_CURRENT = 0,
73ccaff030SJeremy L Thompson   NS_ADVECTION = 1,
74ccaff030SJeremy L Thompson   NS_ADVECTION2D = 2,
75ccaff030SJeremy L Thompson } problemType;
76ccaff030SJeremy L Thompson static const char *const problemTypes[] = {
77ccaff030SJeremy L Thompson   "density_current",
78ccaff030SJeremy L Thompson   "advection",
79ccaff030SJeremy L Thompson   "advection2d",
8084d34d69SLeila Ghaffari   "problemType", "NS_", NULL
81ccaff030SJeremy L Thompson };
82ccaff030SJeremy L Thompson 
831184866aSLeila Ghaffari // Wind Options for Advection
841184866aSLeila Ghaffari typedef enum {
851184866aSLeila Ghaffari   ADVECTION_WIND_ROTATION = 0,
861184866aSLeila Ghaffari   ADVECTION_WIND_TRANSLATION = 1,
871184866aSLeila Ghaffari } WindType;
881184866aSLeila Ghaffari static const char *const WindTypes[] = {
891184866aSLeila Ghaffari   "rotation",
901184866aSLeila Ghaffari   "translation",
911184866aSLeila Ghaffari   "WindType", "ADVECTION_WIND_", NULL
921184866aSLeila Ghaffari };
931184866aSLeila Ghaffari 
94ccaff030SJeremy L Thompson typedef enum {
95ccaff030SJeremy L Thompson   STAB_NONE = 0,
96ccaff030SJeremy L Thompson   STAB_SU = 1,   // Streamline Upwind
97ccaff030SJeremy L Thompson   STAB_SUPG = 2, // Streamline Upwind Petrov-Galerkin
98ccaff030SJeremy L Thompson } StabilizationType;
99ccaff030SJeremy L Thompson static const char *const StabilizationTypes[] = {
10084d34d69SLeila Ghaffari   "none",
101ccaff030SJeremy L Thompson   "SU",
102ccaff030SJeremy L Thompson   "SUPG",
103ccaff030SJeremy L Thompson   "StabilizationType", "STAB_", NULL
104ccaff030SJeremy L Thompson };
105ccaff030SJeremy L Thompson 
10684d34d69SLeila Ghaffari // Test Options
10784d34d69SLeila Ghaffari typedef enum {
10884d34d69SLeila Ghaffari   TEST_NONE = 0,               // Non test mode
10984d34d69SLeila Ghaffari   TEST_EXPLICIT = 1,           // Explicit test
11084d34d69SLeila Ghaffari   TEST_IMPLICIT_STAB_NONE = 2, // Implicit test no stab
11184d34d69SLeila Ghaffari   TEST_IMPLICIT_STAB_SUPG = 3, // Implicit test supg stab
11284d34d69SLeila Ghaffari } testType;
11384d34d69SLeila Ghaffari static const char *const testTypes[] = {
11484d34d69SLeila Ghaffari   "none",
11584d34d69SLeila Ghaffari   "explicit",
11684d34d69SLeila Ghaffari   "implicit_stab_none",
11784d34d69SLeila Ghaffari   "implicit_stab_supg",
11884d34d69SLeila Ghaffari   "testType", "TEST_", NULL
11984d34d69SLeila Ghaffari };
12084d34d69SLeila Ghaffari 
12184d34d69SLeila Ghaffari // Tests specific data
12284d34d69SLeila Ghaffari typedef struct {
12384d34d69SLeila Ghaffari   PetscScalar testtol;
12484d34d69SLeila Ghaffari   const char *filepath;
12584d34d69SLeila Ghaffari } testData;
12684d34d69SLeila Ghaffari 
12784d34d69SLeila Ghaffari testData testOptions[] = {
12884d34d69SLeila Ghaffari   [TEST_NONE] = {
12984d34d69SLeila Ghaffari     .testtol = 0.,
13084d34d69SLeila Ghaffari     .filepath = NULL
13184d34d69SLeila Ghaffari   },
13284d34d69SLeila Ghaffari   [TEST_EXPLICIT] = {
13384d34d69SLeila Ghaffari     .testtol = 1E-5,
13484d34d69SLeila Ghaffari     .filepath = "examples/fluids/tests-output/fluids-navierstokes-explicit.bin"
13584d34d69SLeila Ghaffari   },
13684d34d69SLeila Ghaffari   [TEST_IMPLICIT_STAB_NONE] = {
13784d34d69SLeila Ghaffari     .testtol = 5E-4,
13884d34d69SLeila Ghaffari     .filepath = "examples/fluids/tests-output/fluids-navierstokes-implicit-stab-none.bin"
13984d34d69SLeila Ghaffari   },
14084d34d69SLeila Ghaffari   [TEST_IMPLICIT_STAB_SUPG] = {
14184d34d69SLeila Ghaffari     .testtol = 5E-4,
14284d34d69SLeila Ghaffari     .filepath = "examples/fluids/tests-output/fluids-navierstokes-implicit-stab-supg.bin"
14384d34d69SLeila Ghaffari   }
14484d34d69SLeila Ghaffari };
14584d34d69SLeila Ghaffari 
146ccaff030SJeremy L Thompson // Problem specific data
147ccaff030SJeremy L Thompson typedef struct {
1488b982baeSLeila Ghaffari   CeedInt dim, qdatasizeVol, qdatasizeSur;
149ebb4b9bdSLeila Ghaffari   CeedQFunctionUser setupVol, setupSur, ics, applyVol_rhs, applyVol_ifunction,
150ebb4b9bdSLeila Ghaffari                     applySur;
151ccaff030SJeremy L Thompson   PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt,
152ccaff030SJeremy L Thompson                        PetscScalar[], void *);
1537659d40cSLeila Ghaffari   const char *setupVol_loc, *setupSur_loc, *ics_loc, *applyVol_rhs_loc,
1547659d40cSLeila Ghaffari         *applyVol_ifunction_loc, *applySur_loc;
155ccaff030SJeremy L Thompson   const bool non_zero_time;
156ccaff030SJeremy L Thompson } problemData;
157ccaff030SJeremy L Thompson 
158ccaff030SJeremy L Thompson problemData problemOptions[] = {
159ccaff030SJeremy L Thompson   [NS_DENSITY_CURRENT] = {
160ccaff030SJeremy L Thompson     .dim                       = 3,
161ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1628b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
163b0137797SLeila Ghaffari     .setupVol                  = Setup,
164b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
165356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
166356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
167ccaff030SJeremy L Thompson     .ics                       = ICsDC,
168ccaff030SJeremy L Thompson     .ics_loc                   = ICsDC_loc,
169c96c872fSLeila Ghaffari     .applyVol_rhs              = DC,
170c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = DC_loc,
171c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_DC,
172c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_DC_loc,
173ccaff030SJeremy L Thompson     .bc                        = Exact_DC,
17484d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
175ccaff030SJeremy L Thompson   },
176ccaff030SJeremy L Thompson   [NS_ADVECTION] = {
177ccaff030SJeremy L Thompson     .dim                       = 3,
178ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1798b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
180b0137797SLeila Ghaffari     .setupVol                  = Setup,
181b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
182356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
183356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
184ccaff030SJeremy L Thompson     .ics                       = ICsAdvection,
185ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection_loc,
186c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection,
187c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection_loc,
188c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection,
189c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection_loc,
1907659d40cSLeila Ghaffari     .applySur                  = Advection_Sur,
1917659d40cSLeila Ghaffari     .applySur_loc              = Advection_Sur_loc,
192ccaff030SJeremy L Thompson     .bc                        = Exact_Advection,
19384d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
194ccaff030SJeremy L Thompson   },
195ccaff030SJeremy L Thompson   [NS_ADVECTION2D] = {
196ccaff030SJeremy L Thompson     .dim                       = 2,
197ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 5,
1988b982baeSLeila Ghaffari     .qdatasizeSur              = 3,
199c96c872fSLeila Ghaffari     .setupVol                  = Setup2d,
200c96c872fSLeila Ghaffari     .setupVol_loc              = Setup2d_loc,
201b0137797SLeila Ghaffari     .setupSur                  = SetupBoundary2d,
202b0137797SLeila Ghaffari     .setupSur_loc              = SetupBoundary2d_loc,
203ccaff030SJeremy L Thompson     .ics                       = ICsAdvection2d,
204ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection2d_loc,
205c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection2d,
206c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection2d_loc,
207c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection2d,
208c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection2d_loc,
2097659d40cSLeila Ghaffari     .applySur                  = Advection2d_Sur,
2107659d40cSLeila Ghaffari     .applySur_loc              = Advection2d_Sur_loc,
211ccaff030SJeremy L Thompson     .bc                        = Exact_Advection2d,
21284d34d69SLeila Ghaffari     .non_zero_time             = PETSC_TRUE,
213ccaff030SJeremy L Thompson   },
214ccaff030SJeremy L Thompson };
215ccaff030SJeremy L Thompson 
216ccaff030SJeremy L Thompson // PETSc user data
217ccaff030SJeremy L Thompson typedef struct User_ *User;
218ccaff030SJeremy L Thompson typedef struct Units_ *Units;
219ccaff030SJeremy L Thompson 
220ccaff030SJeremy L Thompson struct User_ {
221ccaff030SJeremy L Thompson   MPI_Comm comm;
222ccaff030SJeremy L Thompson   PetscInt outputfreq;
223ccaff030SJeremy L Thompson   DM dm;
224ccaff030SJeremy L Thompson   DM dmviz;
225ccaff030SJeremy L Thompson   Mat interpviz;
226ccaff030SJeremy L Thompson   Ceed ceed;
227ccaff030SJeremy L Thompson   Units units;
228ccaff030SJeremy L Thompson   CeedVector qceed, qdotceed, gceed;
2291e150236SLeila Ghaffari   CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction;
230ccaff030SJeremy L Thompson   Vec M;
231ccaff030SJeremy L Thompson   char outputfolder[PETSC_MAX_PATH_LEN];
232ccaff030SJeremy L Thompson   PetscInt contsteps;
233ccaff030SJeremy L Thompson };
234ccaff030SJeremy L Thompson 
235ccaff030SJeremy L Thompson struct Units_ {
236ccaff030SJeremy L Thompson   // fundamental units
237ccaff030SJeremy L Thompson   PetscScalar meter;
238ccaff030SJeremy L Thompson   PetscScalar kilogram;
239ccaff030SJeremy L Thompson   PetscScalar second;
240ccaff030SJeremy L Thompson   PetscScalar Kelvin;
241ccaff030SJeremy L Thompson   // derived units
242ccaff030SJeremy L Thompson   PetscScalar Pascal;
243ccaff030SJeremy L Thompson   PetscScalar JperkgK;
244ccaff030SJeremy L Thompson   PetscScalar mpersquareds;
245ccaff030SJeremy L Thompson   PetscScalar WpermK;
246ccaff030SJeremy L Thompson   PetscScalar kgpercubicm;
247ccaff030SJeremy L Thompson   PetscScalar kgpersquaredms;
248ccaff030SJeremy L Thompson   PetscScalar Joulepercubicm;
24916c0476cSLeila Ghaffari   PetscScalar Joule;
250ccaff030SJeremy L Thompson };
251ccaff030SJeremy L Thompson 
252ccaff030SJeremy L Thompson typedef struct SimpleBC_ *SimpleBC;
253ccaff030SJeremy L Thompson struct SimpleBC_ {
2547659d40cSLeila Ghaffari   PetscInt nwall, nslip[3];
2557659d40cSLeila Ghaffari   PetscInt walls[6], slips[3][6];
25684d34d69SLeila Ghaffari   PetscBool userbc;
257ccaff030SJeremy L Thompson };
258ccaff030SJeremy L Thompson 
259ccaff030SJeremy L Thompson // Essential BC dofs are encoded in closure indices as -(i+1).
260ccaff030SJeremy L Thompson static PetscInt Involute(PetscInt i) {
261ccaff030SJeremy L Thompson   return i >= 0 ? i : -(i+1);
262ccaff030SJeremy L Thompson }
263ccaff030SJeremy L Thompson 
264ccaff030SJeremy L Thompson // Utility function to create local CEED restriction
265ccaff030SJeremy L Thompson static PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt P,
26684d34d69SLeila Ghaffari     CeedInt height, DMLabel domainLabel, CeedInt value,
267ccaff030SJeremy L Thompson     CeedElemRestriction *Erestrict) {
268ccaff030SJeremy L Thompson 
269ccaff030SJeremy L Thompson   PetscSection section;
2701184866aSLeila Ghaffari   PetscInt p, Nelem, Ndof, *erestrict, eoffset, nfields, dim, depth;
2710c6c0b13SLeila Ghaffari   DMLabel depthLabel;
2720c6c0b13SLeila Ghaffari   IS depthIS, iterIS;
27384d34d69SLeila Ghaffari   Vec Uloc;
2740c6c0b13SLeila Ghaffari   const PetscInt *iterIndices;
275ccaff030SJeremy L Thompson   PetscErrorCode ierr;
276ccaff030SJeremy L Thompson 
277ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
278ccaff030SJeremy L Thompson   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
279da51bdd9SLeila Ghaffari   dim -= height;
280ccaff030SJeremy L Thompson   ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
281ccaff030SJeremy L Thompson   ierr = PetscSectionGetNumFields(section, &nfields); CHKERRQ(ierr);
282ccaff030SJeremy L Thompson   PetscInt ncomp[nfields], fieldoff[nfields+1];
283ccaff030SJeremy L Thompson   fieldoff[0] = 0;
284ccaff030SJeremy L Thompson   for (PetscInt f=0; f<nfields; f++) {
285ccaff030SJeremy L Thompson     ierr = PetscSectionGetFieldComponents(section, f, &ncomp[f]); CHKERRQ(ierr);
286ccaff030SJeremy L Thompson     fieldoff[f+1] = fieldoff[f] + ncomp[f];
287ccaff030SJeremy L Thompson   }
288ccaff030SJeremy L Thompson 
2890c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepth(dm, &depth); CHKERRQ(ierr);
2900c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepthLabel(dm, &depthLabel); CHKERRQ(ierr);
2910c6c0b13SLeila Ghaffari   ierr = DMLabelGetStratumIS(depthLabel, depth - height, &depthIS); CHKERRQ(ierr);
2920c6c0b13SLeila Ghaffari   if (domainLabel) {
2930c6c0b13SLeila Ghaffari     IS domainIS;
2940c6c0b13SLeila Ghaffari     ierr = DMLabelGetStratumIS(domainLabel, value, &domainIS); CHKERRQ(ierr);
2951119eeeeSJed Brown     if (domainIS) { // domainIS is non-empty
2960c6c0b13SLeila Ghaffari       ierr = ISIntersect(depthIS, domainIS, &iterIS); CHKERRQ(ierr);
2970c6c0b13SLeila Ghaffari       ierr = ISDestroy(&domainIS); CHKERRQ(ierr);
2981119eeeeSJed Brown     } else { // domainIS is NULL (empty)
2991119eeeeSJed Brown       iterIS = NULL;
3001119eeeeSJed Brown     }
3010c6c0b13SLeila Ghaffari     ierr = ISDestroy(&depthIS); CHKERRQ(ierr);
3020c6c0b13SLeila Ghaffari   } else {
3030c6c0b13SLeila Ghaffari     iterIS = depthIS;
3040c6c0b13SLeila Ghaffari   }
3051119eeeeSJed Brown   if (iterIS) {
3060c6c0b13SLeila Ghaffari     ierr = ISGetLocalSize(iterIS, &Nelem); CHKERRQ(ierr);
3070c6c0b13SLeila Ghaffari     ierr = ISGetIndices(iterIS, &iterIndices); CHKERRQ(ierr);
3081119eeeeSJed Brown   } else {
3091119eeeeSJed Brown     Nelem = 0;
3101119eeeeSJed Brown     iterIndices = NULL;
3111119eeeeSJed Brown   }
312ccaff030SJeremy L Thompson   ierr = PetscMalloc1(Nelem*PetscPowInt(P, dim), &erestrict); CHKERRQ(ierr);
3130c6c0b13SLeila Ghaffari   for (p=0,eoffset=0; p<Nelem; p++) {
3140c6c0b13SLeila Ghaffari     PetscInt c = iterIndices[p];
315ccaff030SJeremy L Thompson     PetscInt numindices, *indices, nnodes;
31684d34d69SLeila Ghaffari     ierr = DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE,
31784d34d69SLeila Ghaffari                                    &numindices, &indices, NULL, NULL);
31884d34d69SLeila Ghaffari     CHKERRQ(ierr);
31932b5ec5fSJed Brown     bool flip = false;
32032b5ec5fSJed Brown     if (height > 0) {
32132b5ec5fSJed Brown       PetscInt numCells, numFaces, start = -1;
32232b5ec5fSJed Brown       const PetscInt *orients, *faces, *cells;
32332b5ec5fSJed Brown       ierr = DMPlexGetSupport(dm, c, &cells); CHKERRQ(ierr);
32432b5ec5fSJed Brown       ierr = DMPlexGetSupportSize(dm, c, &numCells); CHKERRQ(ierr);
325ebb4b9bdSLeila Ghaffari       if (numCells != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
326f259b054Svaleriabarra                                     "Expected one cell in support of exterior face, but got %D cells",
327f259b054Svaleriabarra                                     numCells);
32832b5ec5fSJed Brown       ierr = DMPlexGetCone(dm, cells[0], &faces); CHKERRQ(ierr);
32932b5ec5fSJed Brown       ierr = DMPlexGetConeSize(dm, cells[0], &numFaces); CHKERRQ(ierr);
33032b5ec5fSJed Brown       for (PetscInt i=0; i<numFaces; i++) {if (faces[i] == c) start = i;}
331ebb4b9bdSLeila Ghaffari       if (start < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT,
332f259b054Svaleriabarra                                 "Could not find face %D in cone of its support",
333f259b054Svaleriabarra                                 c);
33432b5ec5fSJed Brown       ierr = DMPlexGetConeOrientation(dm, cells[0], &orients); CHKERRQ(ierr);
33532b5ec5fSJed Brown       if (orients[start] < 0) flip = true;
33632b5ec5fSJed Brown     }
33784d34d69SLeila Ghaffari     if (numindices % fieldoff[nfields]) SETERRQ1(PETSC_COMM_SELF,
33884d34d69SLeila Ghaffari           PETSC_ERR_ARG_INCOMP, "Number of closure indices not compatible with Cell %D",
33984d34d69SLeila Ghaffari           c);
340ccaff030SJeremy L Thompson     nnodes = numindices / fieldoff[nfields];
341ccaff030SJeremy L Thompson     for (PetscInt i=0; i<nnodes; i++) {
34232b5ec5fSJed Brown       PetscInt ii = i;
34332b5ec5fSJed Brown       if (flip) {
34432b5ec5fSJed Brown         if (P == nnodes) ii = nnodes - 1 - i;
34532b5ec5fSJed Brown         else if (P*P == nnodes) {
34632b5ec5fSJed Brown           PetscInt row = i / P, col = i % P;
34732b5ec5fSJed Brown           ii = row + col * P;
348ebb4b9bdSLeila Ghaffari         } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP,
349f259b054Svaleriabarra                           "No support for flipping point with %D nodes != P (%D) or P^2",
350f259b054Svaleriabarra                           nnodes, P);
35132b5ec5fSJed Brown       }
352ccaff030SJeremy L Thompson       // Check that indices are blocked by node and thus can be coalesced as a single field with
353ccaff030SJeremy L Thompson       // fieldoff[nfields] = sum(ncomp) components.
354ccaff030SJeremy L Thompson       for (PetscInt f=0; f<nfields; f++) {
355ccaff030SJeremy L Thompson         for (PetscInt j=0; j<ncomp[f]; j++) {
35632b5ec5fSJed Brown           if (Involute(indices[fieldoff[f]*nnodes + ii*ncomp[f] + j])
35732b5ec5fSJed Brown               != Involute(indices[ii*ncomp[0]]) + fieldoff[f] + j)
358ccaff030SJeremy L Thompson             SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
359ccaff030SJeremy L Thompson                      "Cell %D closure indices not interlaced for node %D field %D component %D",
36032b5ec5fSJed Brown                      c, ii, f, j);
361ccaff030SJeremy L Thompson         }
362ccaff030SJeremy L Thompson       }
363ccaff030SJeremy L Thompson       // Essential boundary conditions are encoded as -(loc+1), but we don't care so we decode.
36432b5ec5fSJed Brown       PetscInt loc = Involute(indices[ii*ncomp[0]]);
3656f55dfd5Svaleriabarra       erestrict[eoffset++] = loc;
366ccaff030SJeremy L Thompson     }
36784d34d69SLeila Ghaffari     ierr = DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE,
36884d34d69SLeila Ghaffari                                        &numindices, &indices, NULL, NULL);
36984d34d69SLeila Ghaffari     CHKERRQ(ierr);
370ccaff030SJeremy L Thompson   }
3710c6c0b13SLeila Ghaffari   if (eoffset != Nelem*PetscPowInt(P, dim))
3720c6c0b13SLeila Ghaffari     SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_LIB,
3730c6c0b13SLeila Ghaffari              "ElemRestriction of size (%D,%D) initialized %D nodes", Nelem,
374ccaff030SJeremy L Thompson              PetscPowInt(P, dim),eoffset);
3751119eeeeSJed Brown   if (iterIS) {
3760c6c0b13SLeila Ghaffari     ierr = ISRestoreIndices(iterIS, &iterIndices); CHKERRQ(ierr);
3771119eeeeSJed Brown   }
3780c6c0b13SLeila Ghaffari   ierr = ISDestroy(&iterIS); CHKERRQ(ierr);
3790c6c0b13SLeila Ghaffari 
380ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Uloc); CHKERRQ(ierr);
381ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(Uloc, &Ndof); CHKERRQ(ierr);
382ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Uloc); CHKERRQ(ierr);
3836f55dfd5Svaleriabarra   CeedElemRestrictionCreate(ceed, Nelem, PetscPowInt(P, dim), fieldoff[nfields],
3846f55dfd5Svaleriabarra                             1, Ndof, CEED_MEM_HOST, CEED_COPY_VALUES, erestrict,
3856f55dfd5Svaleriabarra                             Erestrict);
386ccaff030SJeremy L Thompson   ierr = PetscFree(erestrict); CHKERRQ(ierr);
387ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
388ccaff030SJeremy L Thompson }
389ccaff030SJeremy L Thompson 
390c96c872fSLeila Ghaffari // Utility function to get Ceed Restriction for each domain
3911e150236SLeila Ghaffari static PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
3921e150236SLeila Ghaffari     DMLabel domainLabel, PetscInt value, CeedInt P, CeedInt Q, CeedInt qdatasize,
3931e150236SLeila Ghaffari     CeedElemRestriction *restrictq, CeedElemRestriction *restrictx,
3941e150236SLeila Ghaffari     CeedElemRestriction *restrictqdi) {
395c96c872fSLeila Ghaffari 
396c96c872fSLeila Ghaffari   DM dmcoord;
3971e150236SLeila Ghaffari   CeedInt dim, localNelem;
3981e150236SLeila Ghaffari   CeedInt Qdim;
399c96c872fSLeila Ghaffari   PetscErrorCode ierr;
400c96c872fSLeila Ghaffari 
401c96c872fSLeila Ghaffari   PetscFunctionBeginUser;
4021e150236SLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
4031e150236SLeila Ghaffari   dim -= height;
4041e150236SLeila Ghaffari   Qdim = CeedIntPow(Q, dim);
405c96c872fSLeila Ghaffari   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
406c96c872fSLeila Ghaffari   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
407c96c872fSLeila Ghaffari   CHKERRQ(ierr);
408ebb4b9bdSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dm, P, height, domainLabel, value,
409ebb4b9bdSLeila Ghaffari                                    restrictq);
410c96c872fSLeila Ghaffari   CHKERRQ(ierr);
411ebb4b9bdSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dmcoord, 2, height, domainLabel, value,
412ebb4b9bdSLeila Ghaffari                                    restrictx);
413c96c872fSLeila Ghaffari   CHKERRQ(ierr);
414c96c872fSLeila Ghaffari   CeedElemRestrictionGetNumElements(*restrictq, &localNelem);
415c96c872fSLeila Ghaffari   CeedElemRestrictionCreateStrided(ceed, localNelem, Qdim,
416c96c872fSLeila Ghaffari                                    qdatasize, qdatasize*localNelem*Qdim,
417c96c872fSLeila Ghaffari                                    CEED_STRIDES_BACKEND, restrictqdi);
418c96c872fSLeila Ghaffari   PetscFunctionReturn(0);
419c96c872fSLeila Ghaffari }
420c96c872fSLeila Ghaffari 
4211e150236SLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
4227659d40cSLeila Ghaffari static PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc,
4237659d40cSLeila Ghaffari     WindType wind_type, CeedOperator op_applyVol, CeedQFunction qf_applySur,
4247659d40cSLeila Ghaffari     CeedQFunction qf_setupSur, CeedInt height, CeedInt numP_Sur, CeedInt numQ_Sur,
4257659d40cSLeila Ghaffari     CeedInt qdatasizeSur, CeedInt NqptsSur, CeedBasis basisxSur,
4267659d40cSLeila Ghaffari     CeedBasis basisqSur, CeedOperator *op_apply) {
427ca3ac6ddSLeila Ghaffari 
4287659d40cSLeila Ghaffari   CeedInt dim, nFace;
4297659d40cSLeila Ghaffari   PetscInt lsize, localNelemSur[6];
4301e150236SLeila Ghaffari   Vec Xloc;
4317659d40cSLeila Ghaffari   CeedVector xcorners, qdataSur[6];
4327659d40cSLeila Ghaffari   CeedOperator op_setupSur[6], op_applySur[6];
4337659d40cSLeila Ghaffari   CeedElemRestriction restrictxSur[6], restrictqSur[6], restrictqdiSur[6];
434ca3ac6ddSLeila Ghaffari   DMLabel domainLabel;
4351e150236SLeila Ghaffari   PetscScalar *x;
436ca3ac6ddSLeila Ghaffari   PetscErrorCode ierr;
437ca3ac6ddSLeila Ghaffari 
438ca3ac6ddSLeila Ghaffari   PetscFunctionBeginUser;
439ca3ac6ddSLeila Ghaffari   // Composite Operaters
440ca3ac6ddSLeila Ghaffari   CeedCompositeOperatorCreate(ceed, op_apply);
441ebb4b9bdSLeila Ghaffari   CeedCompositeOperatorAddSub(*op_apply,
442ebb4b9bdSLeila Ghaffari                               op_applyVol); // Apply a Sub-Operator for the volume
443ca3ac6ddSLeila Ghaffari 
4447659d40cSLeila Ghaffari   if (wind_type == ADVECTION_WIND_TRANSLATION) {
4457659d40cSLeila Ghaffari     bc->nwall = 0;
4467659d40cSLeila Ghaffari     bc->nslip[0] = bc->nslip[1] = bc->nslip[2] = 0;
4471e150236SLeila Ghaffari     ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
4481e150236SLeila Ghaffari     ierr = VecGetLocalSize(Xloc, &lsize); CHKERRQ(ierr);
4491e150236SLeila Ghaffari     ierr = CeedVectorCreate(ceed, lsize, &xcorners); CHKERRQ(ierr);
4501e150236SLeila Ghaffari     ierr = VecGetArray(Xloc, &x); CHKERRQ(ierr);
4511e150236SLeila Ghaffari     CeedVectorSetArray(xcorners, CEED_MEM_HOST, CEED_USE_POINTER, x);
452ca3ac6ddSLeila Ghaffari     ierr = DMGetLabel(dm, "Face Sets", &domainLabel); CHKERRQ(ierr);
4537659d40cSLeila Ghaffari     ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
4547659d40cSLeila Ghaffari     if (dim == 2) nFace = 4;
4557659d40cSLeila Ghaffari     if (dim == 3) nFace = 6;
4569fe13df9SLeila Ghaffari 
4577659d40cSLeila Ghaffari     // Create CEED Operator for each boundary face
4587659d40cSLeila Ghaffari     for (CeedInt i=0; i<nFace; i++) {
4597659d40cSLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domainLabel, i+1, numP_Sur,
460f259b054Svaleriabarra                                      numQ_Sur, qdatasizeSur, &restrictqSur[i],
461f259b054Svaleriabarra                                      &restrictxSur[i], &restrictqdiSur[i]);
462f259b054Svaleriabarra       CHKERRQ(ierr);
4637659d40cSLeila Ghaffari       // Create the CEED vectors that will be needed in Boundary setup
4647659d40cSLeila Ghaffari       CeedElemRestrictionGetNumElements(restrictqSur[i], &localNelemSur[i]);
465f259b054Svaleriabarra       CeedVectorCreate(ceed, qdatasizeSur*localNelemSur[i]*NqptsSur,
466f259b054Svaleriabarra                        &qdataSur[i]);
4677659d40cSLeila Ghaffari       // Create the operator that builds the quadrature data for the Boundary operator
4687659d40cSLeila Ghaffari       CeedOperatorCreate(ceed, qf_setupSur, NULL, NULL, &op_setupSur[i]);
469ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "dx", restrictxSur[i], basisxSur,
470ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4717659d40cSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "weight", CEED_ELEMRESTRICTION_NONE,
472ca3ac6ddSLeila Ghaffari                            basisxSur, CEED_VECTOR_NONE);
4737659d40cSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "qdataSur", restrictqdiSur[i],
474ca3ac6ddSLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
4757659d40cSLeila Ghaffari       // Create Boundary operator
4767659d40cSLeila Ghaffari       CeedOperatorCreate(ceed, qf_applySur, NULL, NULL, &op_applySur[i]);
477ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "q", restrictqSur[i], basisqSur,
478ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4797659d40cSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "qdataSur", restrictqdiSur[i],
4807659d40cSLeila Ghaffari                            CEED_BASIS_COLLOCATED, qdataSur[i]);
481f259b054Svaleriabarra       CeedOperatorSetField(op_applySur[i], "x", restrictxSur[i], basisxSur,
482f259b054Svaleriabarra                            xcorners);
483ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "v", restrictqSur[i], basisqSur,
484ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4857659d40cSLeila Ghaffari       // Apply CEED operator for Boundary setup
486ebb4b9bdSLeila Ghaffari       CeedOperatorApply(op_setupSur[i], xcorners, qdataSur[i],
487ebb4b9bdSLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
4887659d40cSLeila Ghaffari       // Apply Sub-Operator for the Boundary
4897659d40cSLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_applySur[i]);
4909fe13df9SLeila Ghaffari     }
4911e150236SLeila Ghaffari     CeedVectorDestroy(&xcorners);
492ca3ac6ddSLeila Ghaffari   }
493ca3ac6ddSLeila Ghaffari   PetscFunctionReturn(0);
494ca3ac6ddSLeila Ghaffari }
495ca3ac6ddSLeila Ghaffari 
496ccaff030SJeremy L Thompson static int CreateVectorFromPetscVec(Ceed ceed, Vec p, CeedVector *v) {
497ccaff030SJeremy L Thompson   PetscErrorCode ierr;
498ccaff030SJeremy L Thompson   PetscInt m;
499ccaff030SJeremy L Thompson 
500ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
501ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &m); CHKERRQ(ierr);
502ccaff030SJeremy L Thompson   ierr = CeedVectorCreate(ceed, m, v); CHKERRQ(ierr);
503ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
504ccaff030SJeremy L Thompson }
505ccaff030SJeremy L Thompson 
506ccaff030SJeremy L Thompson static int VectorPlacePetscVec(CeedVector c, Vec p) {
507ccaff030SJeremy L Thompson   PetscErrorCode ierr;
508ccaff030SJeremy L Thompson   PetscInt mceed, mpetsc;
509ccaff030SJeremy L Thompson   PetscScalar *a;
510ccaff030SJeremy L Thompson 
511ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
512ccaff030SJeremy L Thompson   ierr = CeedVectorGetLength(c, &mceed); CHKERRQ(ierr);
513ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &mpetsc); CHKERRQ(ierr);
514ccaff030SJeremy L Thompson   if (mceed != mpetsc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
51584d34d69SLeila Ghaffari                                   "Cannot place PETSc Vec of length %D in CeedVector of length %D",
51684d34d69SLeila Ghaffari                                   mpetsc, mceed);
517ccaff030SJeremy L Thompson   ierr = VecGetArray(p, &a); CHKERRQ(ierr);
518ccaff030SJeremy L Thompson   CeedVectorSetArray(c, CEED_MEM_HOST, CEED_USE_POINTER, a);
519ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
520ccaff030SJeremy L Thompson }
521ccaff030SJeremy L Thompson 
522ccaff030SJeremy L Thompson static PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm,
523ccaff030SJeremy L Thompson     PetscBool insertEssential, Vec Qloc, PetscReal time, Vec faceGeomFVM,
524ccaff030SJeremy L Thompson     Vec cellGeomFVM, Vec gradFVM) {
525ccaff030SJeremy L Thompson   PetscErrorCode ierr;
526ccaff030SJeremy L Thompson   Vec Qbc;
527ccaff030SJeremy L Thompson 
528ccaff030SJeremy L Thompson   PetscFunctionBegin;
529ccaff030SJeremy L Thompson   ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
530ccaff030SJeremy L Thompson   ierr = VecAXPY(Qloc, 1., Qbc); CHKERRQ(ierr);
531ccaff030SJeremy L Thompson   ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
532ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
533ccaff030SJeremy L Thompson }
534ccaff030SJeremy L Thompson 
535ccaff030SJeremy L Thompson // This is the RHS of the ODE, given as u_t = G(t,u)
536ccaff030SJeremy L Thompson // This function takes in a state vector Q and writes into G
537ccaff030SJeremy L Thompson static PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *userData) {
538ccaff030SJeremy L Thompson   PetscErrorCode ierr;
539ccaff030SJeremy L Thompson   User user = *(User *)userData;
540ccaff030SJeremy L Thompson   PetscScalar *q, *g;
541ccaff030SJeremy L Thompson   Vec Qloc, Gloc;
542ccaff030SJeremy L Thompson 
543ccaff030SJeremy L Thompson   // Global-to-local
544ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
545ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
546ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
547ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
548ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
549ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
550ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
551ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
552ccaff030SJeremy L Thompson 
553ccaff030SJeremy L Thompson   // Ceed Vectors
554ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
555ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
556ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER, q);
557ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
558ccaff030SJeremy L Thompson 
559ccaff030SJeremy L Thompson   // Apply CEED operator
560ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->qceed, user->gceed,
561ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
562ccaff030SJeremy L Thompson 
563ccaff030SJeremy L Thompson   // Restore vectors
564ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
565ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
566ccaff030SJeremy L Thompson 
567ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
568ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
569ccaff030SJeremy L Thompson 
570ccaff030SJeremy L Thompson   // Inverse of the lumped mass matrix
571ccaff030SJeremy L Thompson   ierr = VecPointwiseMult(G, G, user->M); // M is Minv
572ccaff030SJeremy L Thompson   CHKERRQ(ierr);
573ccaff030SJeremy L Thompson 
574ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
575ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
576ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
577ccaff030SJeremy L Thompson }
578ccaff030SJeremy L Thompson 
579ccaff030SJeremy L Thompson static PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Qdot, Vec G,
580ccaff030SJeremy L Thompson                                    void *userData) {
581ccaff030SJeremy L Thompson   PetscErrorCode ierr;
582ccaff030SJeremy L Thompson   User user = *(User *)userData;
583ccaff030SJeremy L Thompson   const PetscScalar *q, *qdot;
584ccaff030SJeremy L Thompson   PetscScalar *g;
585ccaff030SJeremy L Thompson   Vec Qloc, Qdotloc, Gloc;
586ccaff030SJeremy L Thompson 
587ccaff030SJeremy L Thompson   // Global-to-local
588ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
589ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
590ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
591ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
592ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
593ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
594ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
595ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
596ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qdotloc); CHKERRQ(ierr);
597ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Qdot, INSERT_VALUES, Qdotloc); CHKERRQ(ierr);
598ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
599ccaff030SJeremy L Thompson 
600ccaff030SJeremy L Thompson   // Ceed Vectors
601ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, &q); CHKERRQ(ierr);
602ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
603ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
604ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER,
605ccaff030SJeremy L Thompson                      (PetscScalar *)q);
606ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qdotceed, CEED_MEM_HOST, CEED_USE_POINTER,
607ccaff030SJeremy L Thompson                      (PetscScalar *)qdot);
608ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
609ccaff030SJeremy L Thompson 
610ccaff030SJeremy L Thompson   // Apply CEED operator
611ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->qceed, user->gceed,
612ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
613ccaff030SJeremy L Thompson 
614ccaff030SJeremy L Thompson   // Restore vectors
615ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, &q); CHKERRQ(ierr);
616ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
617ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
618ccaff030SJeremy L Thompson 
619ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
620ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
621ccaff030SJeremy L Thompson 
622ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
623ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
624ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
625ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
626ccaff030SJeremy L Thompson }
627ccaff030SJeremy L Thompson 
628ccaff030SJeremy L Thompson // User provided TS Monitor
629ccaff030SJeremy L Thompson static PetscErrorCode TSMonitor_NS(TS ts, PetscInt stepno, PetscReal time,
630ccaff030SJeremy L Thompson                                    Vec Q, void *ctx) {
631ccaff030SJeremy L Thompson   User user = ctx;
632ccaff030SJeremy L Thompson   Vec Qloc;
633ccaff030SJeremy L Thompson   char filepath[PETSC_MAX_PATH_LEN];
634ccaff030SJeremy L Thompson   PetscViewer viewer;
635ccaff030SJeremy L Thompson   PetscErrorCode ierr;
636ccaff030SJeremy L Thompson 
637ccaff030SJeremy L Thompson   // Set up output
638ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
639ccaff030SJeremy L Thompson   // Print every 'outputfreq' steps
640ccaff030SJeremy L Thompson   if (stepno % user->outputfreq != 0)
641ccaff030SJeremy L Thompson     PetscFunctionReturn(0);
642ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
643ccaff030SJeremy L Thompson   ierr = PetscObjectSetName((PetscObject)Qloc, "StateVec"); CHKERRQ(ierr);
644ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
645ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
646ccaff030SJeremy L Thompson 
647ccaff030SJeremy L Thompson   // Output
648ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-%03D.vtu",
649ccaff030SJeremy L Thompson                        user->outputfolder, stepno + user->contsteps);
650ccaff030SJeremy L Thompson   CHKERRQ(ierr);
651ccaff030SJeremy L Thompson   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), filepath,
652ccaff030SJeremy L Thompson                             FILE_MODE_WRITE, &viewer); CHKERRQ(ierr);
653ccaff030SJeremy L Thompson   ierr = VecView(Qloc, viewer); CHKERRQ(ierr);
6549d801c56SJed Brown   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
655ccaff030SJeremy L Thompson   if (user->dmviz) {
656ccaff030SJeremy L Thompson     Vec Qrefined, Qrefined_loc;
657ccaff030SJeremy L Thompson     char filepath_refined[PETSC_MAX_PATH_LEN];
658ccaff030SJeremy L Thompson     PetscViewer viewer_refined;
659ccaff030SJeremy L Thompson 
660ccaff030SJeremy L Thompson     ierr = DMGetGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
661ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
662ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)Qrefined_loc, "Refined");
663ccaff030SJeremy L Thompson     CHKERRQ(ierr);
664ccaff030SJeremy L Thompson     ierr = MatInterpolate(user->interpviz, Q, Qrefined); CHKERRQ(ierr);
665ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qrefined_loc); CHKERRQ(ierr);
666ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(user->dmviz, Qrefined, INSERT_VALUES, Qrefined_loc);
667ccaff030SJeremy L Thompson     CHKERRQ(ierr);
668ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath_refined, sizeof filepath_refined,
669ccaff030SJeremy L Thompson                          "%s/nsrefined-%03D.vtu",
670ccaff030SJeremy L Thompson                          user->outputfolder, stepno + user->contsteps);
671ccaff030SJeremy L Thompson     CHKERRQ(ierr);
672ccaff030SJeremy L Thompson     ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Qrefined),
673ccaff030SJeremy L Thompson                               filepath_refined,
674ccaff030SJeremy L Thompson                               FILE_MODE_WRITE, &viewer_refined); CHKERRQ(ierr);
675ccaff030SJeremy L Thompson     ierr = VecView(Qrefined_loc, viewer_refined); CHKERRQ(ierr);
676ccaff030SJeremy L Thompson     ierr = DMRestoreLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
677ccaff030SJeremy L Thompson     ierr = DMRestoreGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
678ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer_refined); CHKERRQ(ierr);
679ccaff030SJeremy L Thompson   }
680ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
681ccaff030SJeremy L Thompson 
682ccaff030SJeremy L Thompson   // Save data in a binary file for continuation of simulations
683ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
684ccaff030SJeremy L Thompson                        user->outputfolder); CHKERRQ(ierr);
685ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
686ccaff030SJeremy L Thompson   CHKERRQ(ierr);
687ccaff030SJeremy L Thompson   ierr = VecView(Q, viewer); CHKERRQ(ierr);
688ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
689ccaff030SJeremy L Thompson 
690ccaff030SJeremy L Thompson   // Save time stamp
691ccaff030SJeremy L Thompson   // Dimensionalize time back
692ccaff030SJeremy L Thompson   time /= user->units->second;
693ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
694ccaff030SJeremy L Thompson                        user->outputfolder); CHKERRQ(ierr);
695ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
696ccaff030SJeremy L Thompson   CHKERRQ(ierr);
697ccaff030SJeremy L Thompson   #if PETSC_VERSION_GE(3,13,0)
698ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL);
699ccaff030SJeremy L Thompson   #else
700ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL, true);
701ccaff030SJeremy L Thompson   #endif
702ccaff030SJeremy L Thompson   CHKERRQ(ierr);
703ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
704ccaff030SJeremy L Thompson 
705ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
706ccaff030SJeremy L Thompson }
707ccaff030SJeremy L Thompson 
70884d34d69SLeila Ghaffari static PetscErrorCode ICs_FixMultiplicity(CeedOperator op_ics,
709ccaff030SJeremy L Thompson     CeedVector xcorners, CeedVector q0ceed, DM dm, Vec Qloc, Vec Q,
710ccaff030SJeremy L Thompson     CeedElemRestriction restrictq, SetupContext ctxSetup, CeedScalar time) {
711ccaff030SJeremy L Thompson   PetscErrorCode ierr;
712ccaff030SJeremy L Thompson   CeedVector multlvec;
713ccaff030SJeremy L Thompson   Vec Multiplicity, MultiplicityLoc;
714ccaff030SJeremy L Thompson 
715ccaff030SJeremy L Thompson   ctxSetup->time = time;
716ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
717ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
718ccaff030SJeremy L Thompson   CeedOperatorApply(op_ics, xcorners, q0ceed, CEED_REQUEST_IMMEDIATE);
719ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
720ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Qloc, ADD_VALUES, Q); CHKERRQ(ierr);
721ccaff030SJeremy L Thompson 
722ccaff030SJeremy L Thompson   // Fix multiplicity for output of ICs
723ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
724ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &multlvec, NULL);
725ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(multlvec, MultiplicityLoc); CHKERRQ(ierr);
726ccaff030SJeremy L Thompson   CeedElemRestrictionGetMultiplicity(restrictq, multlvec);
727ccaff030SJeremy L Thompson   CeedVectorDestroy(&multlvec);
728ccaff030SJeremy L Thompson   ierr = DMGetGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
729ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Multiplicity); CHKERRQ(ierr);
730ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, MultiplicityLoc, ADD_VALUES, Multiplicity);
731ccaff030SJeremy L Thompson   CHKERRQ(ierr);
732ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Q, Q, Multiplicity); CHKERRQ(ierr);
733ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Qloc, Qloc, MultiplicityLoc); CHKERRQ(ierr);
734ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
735ccaff030SJeremy L Thompson   ierr = DMRestoreGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
736ccaff030SJeremy L Thompson 
737ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
738ccaff030SJeremy L Thompson }
739ccaff030SJeremy L Thompson 
740ccaff030SJeremy L Thompson static PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm,
741ccaff030SJeremy L Thompson     CeedElemRestriction restrictq, CeedBasis basisq,
742ccaff030SJeremy L Thompson     CeedElemRestriction restrictqdi, CeedVector qdata, Vec M) {
743ccaff030SJeremy L Thompson   PetscErrorCode ierr;
744ccaff030SJeremy L Thompson   CeedQFunction qf_mass;
745ccaff030SJeremy L Thompson   CeedOperator op_mass;
746ccaff030SJeremy L Thompson   CeedVector mceed;
747ccaff030SJeremy L Thompson   Vec Mloc;
748ccaff030SJeremy L Thompson   CeedInt ncompq, qdatasize;
749ccaff030SJeremy L Thompson 
750ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
751ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictq, &ncompq);
752ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictqdi, &qdatasize);
753ccaff030SJeremy L Thompson   // Create the Q-function that defines the action of the mass operator
754ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass);
755ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q", ncompq, CEED_EVAL_INTERP);
756ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "qdata", qdatasize, CEED_EVAL_NONE);
757ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", ncompq, CEED_EVAL_INTERP);
758ccaff030SJeremy L Thompson 
759ccaff030SJeremy L Thompson   // Create the mass operator
760ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
761ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
762ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", restrictqdi,
763ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, qdata);
764ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
765ccaff030SJeremy L Thompson 
766ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Mloc); CHKERRQ(ierr);
767ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Mloc); CHKERRQ(ierr);
768ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &mceed, NULL);
769ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(mceed, Mloc); CHKERRQ(ierr);
770ccaff030SJeremy L Thompson 
771ccaff030SJeremy L Thompson   {
772ccaff030SJeremy L Thompson     // Compute a lumped mass matrix
773ccaff030SJeremy L Thompson     CeedVector onesvec;
774ccaff030SJeremy L Thompson     CeedElemRestrictionCreateVector(restrictq, &onesvec, NULL);
775ccaff030SJeremy L Thompson     CeedVectorSetValue(onesvec, 1.0);
776ccaff030SJeremy L Thompson     CeedOperatorApply(op_mass, onesvec, mceed, CEED_REQUEST_IMMEDIATE);
777ccaff030SJeremy L Thompson     CeedVectorDestroy(&onesvec);
778ccaff030SJeremy L Thompson     CeedOperatorDestroy(&op_mass);
779ccaff030SJeremy L Thompson     CeedVectorDestroy(&mceed);
780ccaff030SJeremy L Thompson   }
781ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
782ccaff030SJeremy L Thompson 
783ccaff030SJeremy L Thompson   ierr = VecZeroEntries(M); CHKERRQ(ierr);
784ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Mloc, ADD_VALUES, M); CHKERRQ(ierr);
785ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Mloc); CHKERRQ(ierr);
786ccaff030SJeremy L Thompson 
787ccaff030SJeremy L Thompson   // Invert diagonally lumped mass vector for RHS function
788ccaff030SJeremy L Thompson   ierr = VecReciprocal(M); CHKERRQ(ierr);
789ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
790ccaff030SJeremy L Thompson }
791ccaff030SJeremy L Thompson 
79284d34d69SLeila Ghaffari static PetscErrorCode SetUpDM(DM dm, problemData *problem, PetscInt degree,
793ff6701fcSJed Brown                               SimpleBC bc, void *ctxSetup) {
794ccaff030SJeremy L Thompson   PetscErrorCode ierr;
795ccaff030SJeremy L Thompson 
796ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
797ccaff030SJeremy L Thompson   {
798ccaff030SJeremy L Thompson     // Configure the finite element space and boundary conditions
799ccaff030SJeremy L Thompson     PetscFE fe;
800ccaff030SJeremy L Thompson     PetscInt ncompq = 5;
801ff6701fcSJed Brown     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, ncompq,
802ff6701fcSJed Brown                                  PETSC_FALSE, degree, PETSC_DECIDE,
80332ed2d11SJed Brown                                  &fe); CHKERRQ(ierr);
804ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
805ccaff030SJeremy L Thompson     ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr);
806ccaff030SJeremy L Thompson     ierr = DMCreateDS(dm); CHKERRQ(ierr);
80707af6069Svaleriabarra     {
80807af6069Svaleriabarra       PetscInt comps[1] = {1};
80907af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", "Face Sets", 0,
810*3ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[0],
81107af6069Svaleriabarra                            bc->slips[0], ctxSetup); CHKERRQ(ierr);
81207af6069Svaleriabarra       comps[0] = 2;
81307af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", "Face Sets", 0,
814*3ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[1],
81507af6069Svaleriabarra                            bc->slips[1], ctxSetup); CHKERRQ(ierr);
81607af6069Svaleriabarra       comps[0] = 3;
81707af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", "Face Sets", 0,
818*3ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[2],
81907af6069Svaleriabarra                            bc->slips[2], ctxSetup); CHKERRQ(ierr);
82007af6069Svaleriabarra     }
82184d34d69SLeila Ghaffari     if (bc->userbc == PETSC_TRUE) {
82284d34d69SLeila Ghaffari       for (PetscInt c = 0; c < 3; c++) {
82384d34d69SLeila Ghaffari         for (PetscInt s = 0; s < bc->nslip[c]; s++) {
82484d34d69SLeila Ghaffari           for (PetscInt w = 0; w < bc->nwall; w++) {
82584d34d69SLeila Ghaffari             if (bc->slips[c][s] == bc->walls[w])
82684d34d69SLeila Ghaffari               SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
827f259b054Svaleriabarra                        "Boundary condition already set on face %D!\n",
828f259b054Svaleriabarra                        bc->walls[w]);
82984d34d69SLeila Ghaffari 
83084d34d69SLeila Ghaffari           }
83184d34d69SLeila Ghaffari         }
83284d34d69SLeila Ghaffari       }
83384d34d69SLeila Ghaffari     }
83484d34d69SLeila Ghaffari     // Wall boundary conditions are zero energy density and zero flux for
83584d34d69SLeila Ghaffari     //   velocity in advection/advection2d, and zero velocity and zero flux
83684d34d69SLeila Ghaffari     //   for mass density and energy density in density_current
83784d34d69SLeila Ghaffari     {
83884d34d69SLeila Ghaffari       if (problem->bc == Exact_Advection || problem->bc == Exact_Advection2d) {
83984d34d69SLeila Ghaffari         PetscInt comps[1] = {4};
84084d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
841*3ab4fca6SValeria Barra                              1, comps, (void(*)(void))problem->bc, NULL,
84284d34d69SLeila Ghaffari                              bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr);
84384d34d69SLeila Ghaffari       } else if (problem->bc == Exact_DC) {
84484d34d69SLeila Ghaffari         PetscInt comps[3] = {1, 2, 3};
84584d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
846*3ab4fca6SValeria Barra                              3, comps, (void(*)(void))problem->bc, NULL,
84784d34d69SLeila Ghaffari                              bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr);
84884d34d69SLeila Ghaffari       } else
84984d34d69SLeila Ghaffari         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
85084d34d69SLeila Ghaffari                 "Undefined boundary conditions for this problem");
85184d34d69SLeila Ghaffari     }
852ccaff030SJeremy L Thompson     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
853ccaff030SJeremy L Thompson     CHKERRQ(ierr);
854ccaff030SJeremy L Thompson     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
855ccaff030SJeremy L Thompson   }
856ccaff030SJeremy L Thompson   {
857ccaff030SJeremy L Thompson     // Empty name for conserved field (because there is only one field)
858ccaff030SJeremy L Thompson     PetscSection section;
859ccaff030SJeremy L Thompson     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
860ccaff030SJeremy L Thompson     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
861ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
862ccaff030SJeremy L Thompson     CHKERRQ(ierr);
863ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 1, "MomentumX");
864ccaff030SJeremy L Thompson     CHKERRQ(ierr);
865ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 2, "MomentumY");
866ccaff030SJeremy L Thompson     CHKERRQ(ierr);
867ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 3, "MomentumZ");
868ccaff030SJeremy L Thompson     CHKERRQ(ierr);
869ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 4, "EnergyDensity");
870ccaff030SJeremy L Thompson     CHKERRQ(ierr);
871ccaff030SJeremy L Thompson   }
872ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
873ccaff030SJeremy L Thompson }
874ccaff030SJeremy L Thompson 
875ccaff030SJeremy L Thompson int main(int argc, char **argv) {
876ccaff030SJeremy L Thompson   PetscInt ierr;
877ccaff030SJeremy L Thompson   MPI_Comm comm;
87884d34d69SLeila Ghaffari   DM dm, dmcoord, dmviz;
879ccaff030SJeremy L Thompson   Mat interpviz;
880ccaff030SJeremy L Thompson   TS ts;
881ccaff030SJeremy L Thompson   TSAdapt adapt;
882ccaff030SJeremy L Thompson   User user;
883ccaff030SJeremy L Thompson   Units units;
884ccaff030SJeremy L Thompson   char ceedresource[4096] = "/cpu/self";
88584d34d69SLeila Ghaffari   PetscInt localNelemVol, lnodes, gnodes, steps;
886ccaff030SJeremy L Thompson   const PetscInt ncompq = 5;
887ccaff030SJeremy L Thompson   PetscMPIInt rank;
888ccaff030SJeremy L Thompson   PetscScalar ftime;
889ccaff030SJeremy L Thompson   Vec Q, Qloc, Xloc;
890ccaff030SJeremy L Thompson   Ceed ceed;
89184d34d69SLeila Ghaffari   CeedInt numP, numQ;
892cfa64770SLeila Ghaffari   CeedVector xcorners, qdata, q0ceed;
89384d34d69SLeila Ghaffari   CeedBasis basisx, basisxc, basisq;
89484d34d69SLeila Ghaffari   CeedElemRestriction restrictx, restrictq, restrictqdi;
895cfa64770SLeila Ghaffari   CeedQFunction qf_setupVol, qf_ics, qf_rhsVol, qf_ifunctionVol;
896cfa64770SLeila Ghaffari   CeedOperator op_setupVol, op_ics;
897ccaff030SJeremy L Thompson   CeedScalar Rd;
89884d34d69SLeila Ghaffari   CeedMemType memtyperequested;
899ccaff030SJeremy L Thompson   PetscScalar WpermK, Pascal, JperkgK, mpersquareds, kgpercubicm,
90016c0476cSLeila Ghaffari               kgpersquaredms, Joulepercubicm, Joule;
901ccaff030SJeremy L Thompson   problemType problemChoice;
902ccaff030SJeremy L Thompson   problemData *problem = NULL;
9031184866aSLeila Ghaffari   WindType wind_type;
904ccaff030SJeremy L Thompson   StabilizationType stab;
90584d34d69SLeila Ghaffari   testType testChoice;
90684d34d69SLeila Ghaffari   testData *test = NULL;
90784d34d69SLeila Ghaffari   PetscBool implicit;
908cb3e2689Svaleriabarra   PetscInt    viz_refine = 0;
909ccaff030SJeremy L Thompson   struct SimpleBC_ bc = {
91084d34d69SLeila Ghaffari     .nslip = {2, 2, 2},
91184d34d69SLeila Ghaffari     .slips = {{5, 6}, {3, 4}, {1, 2}}
912ccaff030SJeremy L Thompson   };
913ccaff030SJeremy L Thompson   double start, cpu_time_used;
91484d34d69SLeila Ghaffari   // Check PETSc CUDA support
91584d34d69SLeila Ghaffari   PetscBool petschavecuda, setmemtyperequest = PETSC_FALSE;
91684d34d69SLeila Ghaffari   // *INDENT-OFF*
91784d34d69SLeila Ghaffari   #ifdef PETSC_HAVE_CUDA
91884d34d69SLeila Ghaffari   petschavecuda = PETSC_TRUE;
91984d34d69SLeila Ghaffari   #else
92084d34d69SLeila Ghaffari   petschavecuda = PETSC_FALSE;
92184d34d69SLeila Ghaffari   #endif
92284d34d69SLeila Ghaffari   // *INDENT-ON*
923ccaff030SJeremy L Thompson 
924ccaff030SJeremy L Thompson   // Create the libCEED contexts
925ccaff030SJeremy L Thompson   PetscScalar meter      = 1e-2;     // 1 meter in scaled length units
926ccaff030SJeremy L Thompson   PetscScalar second     = 1e-2;     // 1 second in scaled time units
927ccaff030SJeremy L Thompson   PetscScalar kilogram   = 1e-6;     // 1 kilogram in scaled mass units
928ccaff030SJeremy L Thompson   PetscScalar Kelvin     = 1;        // 1 Kelvin in scaled temperature units
929ccaff030SJeremy L Thompson   CeedScalar theta0      = 300.;     // K
930ccaff030SJeremy L Thompson   CeedScalar thetaC      = -15.;     // K
931ccaff030SJeremy L Thompson   CeedScalar P0          = 1.e5;     // Pa
93216c0476cSLeila Ghaffari   CeedScalar E_wind      = 1.e6;     // J
933ccaff030SJeremy L Thompson   CeedScalar N           = 0.01;     // 1/s
934ccaff030SJeremy L Thompson   CeedScalar cv          = 717.;     // J/(kg K)
935ccaff030SJeremy L Thompson   CeedScalar cp          = 1004.;    // J/(kg K)
936ccaff030SJeremy L Thompson   CeedScalar g           = 9.81;     // m/s^2
937ccaff030SJeremy L Thompson   CeedScalar lambda      = -2./3.;   // -
938ccaff030SJeremy L Thompson   CeedScalar mu          = 75.;      // Pa s, dynamic viscosity
939ccaff030SJeremy L Thompson   // mu = 75 is not physical for air, but is good for numerical stability
940ccaff030SJeremy L Thompson   CeedScalar k           = 0.02638;  // W/(m K)
941ccaff030SJeremy L Thompson   CeedScalar CtauS       = 0.;       // dimensionless
942ccaff030SJeremy L Thompson   CeedScalar strong_form = 0.;       // [0,1]
943ccaff030SJeremy L Thompson   PetscScalar lx         = 8000.;    // m
944ccaff030SJeremy L Thompson   PetscScalar ly         = 8000.;    // m
945ccaff030SJeremy L Thompson   PetscScalar lz         = 4000.;    // m
946ccaff030SJeremy L Thompson   CeedScalar rc          = 1000.;    // m (Radius of bubble)
947ccaff030SJeremy L Thompson   PetscScalar resx       = 1000.;    // m (resolution in x)
948ccaff030SJeremy L Thompson   PetscScalar resy       = 1000.;    // m (resolution in y)
949ccaff030SJeremy L Thompson   PetscScalar resz       = 1000.;    // m (resolution in z)
950ccaff030SJeremy L Thompson   PetscInt outputfreq    = 10;       // -
951ccaff030SJeremy L Thompson   PetscInt contsteps     = 0;        // -
95284d34d69SLeila Ghaffari   PetscInt degree        = 1;        // -
95384d34d69SLeila Ghaffari   PetscInt qextra        = 2;        // -
954ea6e0f84SLeila Ghaffari   PetscInt qextraSur     = 2;        // -
9551184866aSLeila Ghaffari   PetscReal center[3], dc_axis[3] = {0, 0, 0}, wind[3] = {1., 0, 0};
956ccaff030SJeremy L Thompson 
957ccaff030SJeremy L Thompson   ierr = PetscInitialize(&argc, &argv, NULL, help);
958ccaff030SJeremy L Thompson   if (ierr) return ierr;
959ccaff030SJeremy L Thompson 
960ccaff030SJeremy L Thompson   // Allocate PETSc context
961ccaff030SJeremy L Thompson   ierr = PetscCalloc1(1, &user); CHKERRQ(ierr);
962ccaff030SJeremy L Thompson   ierr = PetscMalloc1(1, &units); CHKERRQ(ierr);
963ccaff030SJeremy L Thompson 
964ccaff030SJeremy L Thompson   // Parse command line options
965ccaff030SJeremy L Thompson   comm = PETSC_COMM_WORLD;
966ccaff030SJeremy L Thompson   ierr = PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED",
967ccaff030SJeremy L Thompson                            NULL); CHKERRQ(ierr);
968ccaff030SJeremy L Thompson   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
969ccaff030SJeremy L Thompson                             NULL, ceedresource, ceedresource,
970ccaff030SJeremy L Thompson                             sizeof(ceedresource), NULL); CHKERRQ(ierr);
97184d34d69SLeila Ghaffari   testChoice = TEST_NONE;
97284d34d69SLeila Ghaffari   ierr = PetscOptionsEnum("-test", "Run tests", NULL,
97384d34d69SLeila Ghaffari                           testTypes, (PetscEnum)testChoice,
97484d34d69SLeila Ghaffari                           (PetscEnum *)&testChoice,
97584d34d69SLeila Ghaffari                           NULL); CHKERRQ(ierr);
97684d34d69SLeila Ghaffari   test = &testOptions[testChoice];
977ccaff030SJeremy L Thompson   problemChoice = NS_DENSITY_CURRENT;
978ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-problem", "Problem to solve", NULL,
979ccaff030SJeremy L Thompson                           problemTypes, (PetscEnum)problemChoice,
980ccaff030SJeremy L Thompson                           (PetscEnum *)&problemChoice, NULL); CHKERRQ(ierr);
981ccaff030SJeremy L Thompson   problem = &problemOptions[problemChoice];
9821184866aSLeila Ghaffari   ierr = PetscOptionsEnum("-problem_advection_wind", "Wind type in Advection",
983f259b054Svaleriabarra                           NULL, WindTypes,
984f259b054Svaleriabarra                           (PetscEnum)(wind_type = ADVECTION_WIND_ROTATION),
9851184866aSLeila Ghaffari                           (PetscEnum *)&wind_type, NULL); CHKERRQ(ierr);
9861184866aSLeila Ghaffari   PetscInt n = problem->dim;
98782c09b01SLeila Ghaffari   PetscBool userWind;
988ebb4b9bdSLeila Ghaffari   ierr = PetscOptionsRealArray("-problem_advection_wind_translation",
989ebb4b9bdSLeila Ghaffari                                "Constant wind vector",
99082c09b01SLeila Ghaffari                                NULL, wind, &n, &userWind); CHKERRQ(ierr);
99182c09b01SLeila Ghaffari   if (wind_type == ADVECTION_WIND_ROTATION && userWind) {
992ebb4b9bdSLeila Ghaffari     ierr = PetscPrintf(comm,
993ebb4b9bdSLeila Ghaffari                        "Warning! Use -problem_advection_wind_translation only with -problem_advection_wind translation\n");
99482c09b01SLeila Ghaffari     CHKERRQ(ierr);
9951184866aSLeila Ghaffari   }
996ebb4b9bdSLeila Ghaffari   if (wind_type == ADVECTION_WIND_TRANSLATION
997ebb4b9bdSLeila Ghaffari       && problemChoice == NS_DENSITY_CURRENT) {
99867babd9cSLeila Ghaffari     SETERRQ(comm, PETSC_ERR_ARG_INCOMP,
99967babd9cSLeila Ghaffari             "-problem_advection_wind translation is not defined for -problem density_current");
100067babd9cSLeila Ghaffari   }
1001ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
1002ccaff030SJeremy L Thompson                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
1003ccaff030SJeremy L Thompson                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
1004ccaff030SJeremy L Thompson   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
1005ccaff030SJeremy L Thompson                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
1006ccaff030SJeremy L Thompson   CHKERRQ(ierr);
100784d34d69SLeila Ghaffari   if (!implicit && stab != STAB_NONE) {
100884d34d69SLeila Ghaffari     ierr = PetscPrintf(comm, "Warning! Use -stab only with -implicit\n");
100984d34d69SLeila Ghaffari     CHKERRQ(ierr);
101084d34d69SLeila Ghaffari   }
1011ccaff030SJeremy L Thompson   {
10127573aee6SJed Brown     PetscInt len;
10137573aee6SJed Brown     PetscBool flg;
1014ccaff030SJeremy L Thompson     ierr = PetscOptionsIntArray("-bc_wall",
1015ccaff030SJeremy L Thompson                                 "Use wall boundary conditions on this list of faces",
1016ccaff030SJeremy L Thompson                                 NULL, bc.walls,
1017ccaff030SJeremy L Thompson                                 (len = sizeof(bc.walls) / sizeof(bc.walls[0]),
1018ccaff030SJeremy L Thompson                                  &len), &flg); CHKERRQ(ierr);
10197573aee6SJed Brown     if (flg) {
10207573aee6SJed Brown       bc.nwall = len;
10217573aee6SJed Brown       // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
10227573aee6SJed Brown       bc.nslip[0] = bc.nslip[1] = bc.nslip[2] = 0;
10237573aee6SJed Brown     }
1024ccaff030SJeremy L Thompson     for (PetscInt j=0; j<3; j++) {
1025ccaff030SJeremy L Thompson       const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
1026ccaff030SJeremy L Thompson       ierr = PetscOptionsIntArray(flags[j],
1027ccaff030SJeremy L Thompson                                   "Use slip boundary conditions on this list of faces",
1028ccaff030SJeremy L Thompson                                   NULL, bc.slips[j],
1029ccaff030SJeremy L Thompson                                   (len = sizeof(bc.slips[j]) / sizeof(bc.slips[j][0]),
1030ccaff030SJeremy L Thompson                                    &len), &flg);
1031ccaff030SJeremy L Thompson       CHKERRQ(ierr);
103284d34d69SLeila Ghaffari       if (flg) {
103384d34d69SLeila Ghaffari         bc.nslip[j] = len;
103484d34d69SLeila Ghaffari         bc.userbc = PETSC_TRUE;
103584d34d69SLeila Ghaffari       }
1036ccaff030SJeremy L Thompson     }
1037ccaff030SJeremy L Thompson   }
1038cb3e2689Svaleriabarra   ierr = PetscOptionsInt("-viz_refine",
1039cb3e2689Svaleriabarra                          "Regular refinement levels for visualization",
1040cb3e2689Svaleriabarra                          NULL, viz_refine, &viz_refine, NULL);
1041ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1042ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
1043ccaff030SJeremy L Thompson                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
1044ccaff030SJeremy L Thompson   meter = fabs(meter);
1045ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
1046ccaff030SJeremy L Thompson                             NULL, second, &second, NULL); CHKERRQ(ierr);
1047ccaff030SJeremy L Thompson   second = fabs(second);
1048ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
1049ccaff030SJeremy L Thompson                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
1050ccaff030SJeremy L Thompson   kilogram = fabs(kilogram);
1051ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_Kelvin",
1052ccaff030SJeremy L Thompson                             "1 Kelvin in scaled temperature units",
1053ccaff030SJeremy L Thompson                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
1054ccaff030SJeremy L Thompson   Kelvin = fabs(Kelvin);
1055ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
1056ccaff030SJeremy L Thompson                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
1057ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
1058ccaff030SJeremy L Thompson                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
1059ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
1060ccaff030SJeremy L Thompson                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
106116c0476cSLeila Ghaffari   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
106216c0476cSLeila Ghaffari                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
1063ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
1064ccaff030SJeremy L Thompson                             NULL, N, &N, NULL); CHKERRQ(ierr);
1065ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
1066ccaff030SJeremy L Thompson                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
1067ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
1068ccaff030SJeremy L Thompson                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
1069ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
1070ccaff030SJeremy L Thompson                             NULL, g, &g, NULL); CHKERRQ(ierr);
1071ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lambda",
1072ccaff030SJeremy L Thompson                             "Stokes hypothesis second viscosity coefficient",
1073ccaff030SJeremy L Thompson                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
1074ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
1075ccaff030SJeremy L Thompson                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
1076ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
1077ccaff030SJeremy L Thompson                             NULL, k, &k, NULL); CHKERRQ(ierr);
1078ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-CtauS",
1079ccaff030SJeremy L Thompson                             "Scale coefficient for tau (nondimensional)",
1080ccaff030SJeremy L Thompson                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
108184d34d69SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
108284d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
108384d34d69SLeila Ghaffari                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
108484d34d69SLeila Ghaffari     CHKERRQ(ierr);
108584d34d69SLeila Ghaffari   }
1086ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-strong_form",
1087ccaff030SJeremy L Thompson                             "Strong (1) or weak/integrated by parts (0) advection residual",
1088ccaff030SJeremy L Thompson                             NULL, strong_form, &strong_form, NULL);
1089ccaff030SJeremy L Thompson   CHKERRQ(ierr);
109084d34d69SLeila Ghaffari   if (problemChoice == NS_DENSITY_CURRENT && (CtauS != 0 || strong_form != 0)) {
109184d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
109284d34d69SLeila Ghaffari                        "Warning! Problem density_current does not support -CtauS or -strong_form\n");
109384d34d69SLeila Ghaffari     CHKERRQ(ierr);
109484d34d69SLeila Ghaffari   }
1095ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
1096ccaff030SJeremy L Thompson                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
1097ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
1098ccaff030SJeremy L Thompson                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
1099ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
1100ccaff030SJeremy L Thompson                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
1101ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
1102ccaff030SJeremy L Thompson                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
1103ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resx","Target resolution in x",
1104ccaff030SJeremy L Thompson                             NULL, resx, &resx, NULL); CHKERRQ(ierr);
1105ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resy","Target resolution in y",
1106ccaff030SJeremy L Thompson                             NULL, resy, &resy, NULL); CHKERRQ(ierr);
1107ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resz","Target resolution in z",
1108ccaff030SJeremy L Thompson                             NULL, resz, &resz, NULL); CHKERRQ(ierr);
110982c09b01SLeila Ghaffari   n = problem->dim;
1110ccaff030SJeremy L Thompson   center[0] = 0.5 * lx;
1111ccaff030SJeremy L Thompson   center[1] = 0.5 * ly;
1112ccaff030SJeremy L Thompson   center[2] = 0.5 * lz;
1113ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
1114ccaff030SJeremy L Thompson                                NULL, center, &n, NULL); CHKERRQ(ierr);
1115ccaff030SJeremy L Thompson   n = problem->dim;
1116ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-dc_axis",
1117ccaff030SJeremy L Thompson                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
1118ccaff030SJeremy L Thompson                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
1119ccaff030SJeremy L Thompson   {
1120ccaff030SJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) +
1121ccaff030SJeremy L Thompson                                    PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
1122ccaff030SJeremy L Thompson     if (norm > 0) {
1123ccaff030SJeremy L Thompson       for (int i=0; i<3; i++) dc_axis[i] /= norm;
1124ccaff030SJeremy L Thompson     }
1125ccaff030SJeremy L Thompson   }
1126ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-output_freq",
1127ccaff030SJeremy L Thompson                          "Frequency of output, in number of steps",
1128ccaff030SJeremy L Thompson                          NULL, outputfreq, &outputfreq, NULL); CHKERRQ(ierr);
1129ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-continue", "Continue from previous solution",
1130ccaff030SJeremy L Thompson                          NULL, contsteps, &contsteps, NULL); CHKERRQ(ierr);
113184d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
113284d34d69SLeila Ghaffari                          NULL, degree, &degree, NULL); CHKERRQ(ierr);
113384d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-qextra", "Number of extra quadrature points",
113484d34d69SLeila Ghaffari                          NULL, qextra, &qextra, NULL); CHKERRQ(ierr);
113581f92cf0SLeila Ghaffari   PetscBool userQextraSur;
1136ebb4b9bdSLeila Ghaffari   ierr = PetscOptionsInt("-qextra_boundary",
1137ebb4b9bdSLeila Ghaffari                          "Number of extra quadrature points on in/outflow faces",
1138f259b054Svaleriabarra                          NULL, qextraSur, &qextraSur, &userQextraSur);
1139f259b054Svaleriabarra   CHKERRQ(ierr);
1140ebb4b9bdSLeila Ghaffari   if ((wind_type == ADVECTION_WIND_ROTATION
1141ebb4b9bdSLeila Ghaffari        || problemChoice == NS_DENSITY_CURRENT) && userQextraSur) {
1142ebb4b9bdSLeila Ghaffari     ierr = PetscPrintf(comm,
1143ebb4b9bdSLeila Ghaffari                        "Warning! Use -qextra_boundary only with -problem_advection_wind translation\n");
114481f92cf0SLeila Ghaffari     CHKERRQ(ierr);
114581f92cf0SLeila Ghaffari   }
114684d34d69SLeila Ghaffari   ierr = PetscStrncpy(user->outputfolder, ".", 2); CHKERRQ(ierr);
1147ccaff030SJeremy L Thompson   ierr = PetscOptionsString("-of", "Output folder",
1148ccaff030SJeremy L Thompson                             NULL, user->outputfolder, user->outputfolder,
1149ccaff030SJeremy L Thompson                             sizeof(user->outputfolder), NULL); CHKERRQ(ierr);
115084d34d69SLeila Ghaffari   memtyperequested = petschavecuda ? CEED_MEM_DEVICE : CEED_MEM_HOST;
115184d34d69SLeila Ghaffari   ierr = PetscOptionsEnum("-memtype",
115284d34d69SLeila Ghaffari                           "CEED MemType requested", NULL,
115384d34d69SLeila Ghaffari                           memTypes, (PetscEnum)memtyperequested,
115484d34d69SLeila Ghaffari                           (PetscEnum *)&memtyperequested, &setmemtyperequest);
115584d34d69SLeila Ghaffari   CHKERRQ(ierr);
1156ccaff030SJeremy L Thompson   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
1157ccaff030SJeremy L Thompson 
1158ccaff030SJeremy L Thompson   // Define derived units
1159ccaff030SJeremy L Thompson   Pascal = kilogram / (meter * PetscSqr(second));
1160ccaff030SJeremy L Thompson   JperkgK =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
1161ccaff030SJeremy L Thompson   mpersquareds = meter / PetscSqr(second);
1162ccaff030SJeremy L Thompson   WpermK = kilogram * meter / (pow(second,3) * Kelvin);
1163ccaff030SJeremy L Thompson   kgpercubicm = kilogram / pow(meter,3);
1164ccaff030SJeremy L Thompson   kgpersquaredms = kilogram / (PetscSqr(meter) * second);
1165ccaff030SJeremy L Thompson   Joulepercubicm = kilogram / (meter * PetscSqr(second));
116616c0476cSLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
1167ccaff030SJeremy L Thompson 
1168ccaff030SJeremy L Thompson   // Scale variables to desired units
1169ccaff030SJeremy L Thompson   theta0 *= Kelvin;
1170ccaff030SJeremy L Thompson   thetaC *= Kelvin;
1171ccaff030SJeremy L Thompson   P0 *= Pascal;
117216c0476cSLeila Ghaffari   E_wind *= Joule;
1173ccaff030SJeremy L Thompson   N *= (1./second);
1174ccaff030SJeremy L Thompson   cv *= JperkgK;
1175ccaff030SJeremy L Thompson   cp *= JperkgK;
1176ccaff030SJeremy L Thompson   Rd = cp - cv;
1177ccaff030SJeremy L Thompson   g *= mpersquareds;
1178ccaff030SJeremy L Thompson   mu *= Pascal * second;
1179ccaff030SJeremy L Thompson   k *= WpermK;
1180ccaff030SJeremy L Thompson   lx = fabs(lx) * meter;
1181ccaff030SJeremy L Thompson   ly = fabs(ly) * meter;
1182ccaff030SJeremy L Thompson   lz = fabs(lz) * meter;
1183ccaff030SJeremy L Thompson   rc = fabs(rc) * meter;
1184ccaff030SJeremy L Thompson   resx = fabs(resx) * meter;
1185ccaff030SJeremy L Thompson   resy = fabs(resy) * meter;
1186ccaff030SJeremy L Thompson   resz = fabs(resz) * meter;
1187ccaff030SJeremy L Thompson   for (int i=0; i<3; i++) center[i] *= meter;
1188ccaff030SJeremy L Thompson 
1189ccaff030SJeremy L Thompson   const CeedInt dim = problem->dim, ncompx = problem->dim,
1190cfa64770SLeila Ghaffari                 qdatasizeVol = problem->qdatasizeVol;
1191ccaff030SJeremy L Thompson   // Set up the libCEED context
1192ccaff030SJeremy L Thompson   struct SetupContext_ ctxSetup = {
1193ccaff030SJeremy L Thompson     .theta0 = theta0,
1194ccaff030SJeremy L Thompson     .thetaC = thetaC,
1195ccaff030SJeremy L Thompson     .P0 = P0,
1196ccaff030SJeremy L Thompson     .N = N,
1197ccaff030SJeremy L Thompson     .cv = cv,
1198ccaff030SJeremy L Thompson     .cp = cp,
1199ccaff030SJeremy L Thompson     .Rd = Rd,
1200ccaff030SJeremy L Thompson     .g = g,
1201ccaff030SJeremy L Thompson     .rc = rc,
1202ccaff030SJeremy L Thompson     .lx = lx,
1203ccaff030SJeremy L Thompson     .ly = ly,
1204ccaff030SJeremy L Thompson     .lz = lz,
1205ccaff030SJeremy L Thompson     .center[0] = center[0],
1206ccaff030SJeremy L Thompson     .center[1] = center[1],
1207ccaff030SJeremy L Thompson     .center[2] = center[2],
1208ccaff030SJeremy L Thompson     .dc_axis[0] = dc_axis[0],
1209ccaff030SJeremy L Thompson     .dc_axis[1] = dc_axis[1],
1210ccaff030SJeremy L Thompson     .dc_axis[2] = dc_axis[2],
12111184866aSLeila Ghaffari     .wind[0] = wind[0],
12121184866aSLeila Ghaffari     .wind[1] = wind[1],
12131184866aSLeila Ghaffari     .wind[2] = wind[2],
1214ccaff030SJeremy L Thompson     .time = 0,
12151184866aSLeila Ghaffari     .wind_type = wind_type,
1216ccaff030SJeremy L Thompson   };
1217ccaff030SJeremy L Thompson 
121884d34d69SLeila Ghaffari   // Create the mesh
1219ccaff030SJeremy L Thompson   {
1220ccaff030SJeremy L Thompson     const PetscReal scale[3] = {lx, ly, lz};
1221ccaff030SJeremy L Thompson     ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_FALSE, NULL, NULL, scale,
122284d34d69SLeila Ghaffari                                NULL, PETSC_TRUE, &dm);
1223ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1224ccaff030SJeremy L Thompson   }
122584d34d69SLeila Ghaffari 
122684d34d69SLeila Ghaffari   // Distribute the mesh over processes
122784d34d69SLeila Ghaffari   {
1228ccaff030SJeremy L Thompson     DM               dmDist = NULL;
1229ccaff030SJeremy L Thompson     PetscPartitioner part;
1230ccaff030SJeremy L Thompson 
1231ccaff030SJeremy L Thompson     ierr = DMPlexGetPartitioner(dm, &part); CHKERRQ(ierr);
1232ccaff030SJeremy L Thompson     ierr = PetscPartitionerSetFromOptions(part); CHKERRQ(ierr);
1233ccaff030SJeremy L Thompson     ierr = DMPlexDistribute(dm, 0, NULL, &dmDist); CHKERRQ(ierr);
1234ccaff030SJeremy L Thompson     if (dmDist) {
1235ccaff030SJeremy L Thompson       ierr = DMDestroy(&dm); CHKERRQ(ierr);
1236ccaff030SJeremy L Thompson       dm  = dmDist;
1237ccaff030SJeremy L Thompson     }
1238ccaff030SJeremy L Thompson   }
1239ccaff030SJeremy L Thompson   ierr = DMViewFromOptions(dm, NULL, "-dm_view"); CHKERRQ(ierr);
1240ccaff030SJeremy L Thompson 
124184d34d69SLeila Ghaffari   // Setup DM
1242ccaff030SJeremy L Thompson   ierr = DMLocalizeCoordinates(dm); CHKERRQ(ierr);
1243ccaff030SJeremy L Thompson   ierr = DMSetFromOptions(dm); CHKERRQ(ierr);
124484d34d69SLeila Ghaffari   ierr = SetUpDM(dm, problem, degree, &bc, &ctxSetup); CHKERRQ(ierr);
124584d34d69SLeila Ghaffari 
124684d34d69SLeila Ghaffari   // Refine DM for high-order viz
1247ccaff030SJeremy L Thompson   dmviz = NULL;
1248ccaff030SJeremy L Thompson   interpviz = NULL;
1249ccaff030SJeremy L Thompson   if (viz_refine) {
1250ff6701fcSJed Brown     DM dmhierarchy[viz_refine+1];
1251ff6701fcSJed Brown 
1252ccaff030SJeremy L Thompson     ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
1253ff6701fcSJed Brown     dmhierarchy[0] = dm;
125484d34d69SLeila Ghaffari     for (PetscInt i = 0, d = degree; i < viz_refine; i++) {
1255ff6701fcSJed Brown       Mat interp_next;
1256ff6701fcSJed Brown 
1257ff6701fcSJed Brown       ierr = DMRefine(dmhierarchy[i], MPI_COMM_NULL, &dmhierarchy[i+1]);
1258ccaff030SJeremy L Thompson       CHKERRQ(ierr);
1259bc6a41f7SJed Brown       ierr = DMClearDS(dmhierarchy[i+1]); CHKERRQ(ierr);
1260bc6a41f7SJed Brown       ierr = DMClearFields(dmhierarchy[i+1]); CHKERRQ(ierr);
1261ff6701fcSJed Brown       ierr = DMSetCoarseDM(dmhierarchy[i+1], dmhierarchy[i]); CHKERRQ(ierr);
1262ff6701fcSJed Brown       d = (d + 1) / 2;
1263ff6701fcSJed Brown       if (i + 1 == viz_refine) d = 1;
1264f259b054Svaleriabarra       ierr = SetUpDM(dmhierarchy[i+1], problem, d, &bc, &ctxSetup);
1265f259b054Svaleriabarra       CHKERRQ(ierr);
1266ff6701fcSJed Brown       ierr = DMCreateInterpolation(dmhierarchy[i], dmhierarchy[i+1],
1267ff6701fcSJed Brown                                    &interp_next, NULL); CHKERRQ(ierr);
1268ff6701fcSJed Brown       if (!i) interpviz = interp_next;
1269ff6701fcSJed Brown       else {
1270ff6701fcSJed Brown         Mat C;
1271ff6701fcSJed Brown         ierr = MatMatMult(interp_next, interpviz, MAT_INITIAL_MATRIX,
1272ff6701fcSJed Brown                           PETSC_DECIDE, &C); CHKERRQ(ierr);
1273ff6701fcSJed Brown         ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
1274ff6701fcSJed Brown         ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1275ff6701fcSJed Brown         interpviz = C;
1276ff6701fcSJed Brown       }
1277ff6701fcSJed Brown     }
1278cb3e2689Svaleriabarra     for (PetscInt i=1; i<viz_refine; i++) {
1279ff6701fcSJed Brown       ierr = DMDestroy(&dmhierarchy[i]); CHKERRQ(ierr);
1280cb3e2689Svaleriabarra     }
1281ff6701fcSJed Brown     dmviz = dmhierarchy[viz_refine];
1282ccaff030SJeremy L Thompson   }
1283ccaff030SJeremy L Thompson   ierr = DMCreateGlobalVector(dm, &Q); CHKERRQ(ierr);
1284ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Qloc); CHKERRQ(ierr);
1285ccaff030SJeremy L Thompson   ierr = VecGetSize(Qloc, &lnodes); CHKERRQ(ierr);
1286ccaff030SJeremy L Thompson   lnodes /= ncompq;
1287ccaff030SJeremy L Thompson 
128884d34d69SLeila Ghaffari   // Initialize CEED
128984d34d69SLeila Ghaffari   CeedInit(ceedresource, &ceed);
129084d34d69SLeila Ghaffari   // Set memtype
129184d34d69SLeila Ghaffari   CeedMemType memtypebackend;
129284d34d69SLeila Ghaffari   CeedGetPreferredMemType(ceed, &memtypebackend);
129384d34d69SLeila Ghaffari   // Check memtype compatibility
129484d34d69SLeila Ghaffari   if (!setmemtyperequest)
129584d34d69SLeila Ghaffari     memtyperequested = memtypebackend;
129684d34d69SLeila Ghaffari   else if (!petschavecuda && memtyperequested == CEED_MEM_DEVICE)
129784d34d69SLeila Ghaffari     SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS,
129884d34d69SLeila Ghaffari              "PETSc was not built with CUDA. "
129984d34d69SLeila Ghaffari              "Requested MemType CEED_MEM_DEVICE is not supported.", NULL);
130084d34d69SLeila Ghaffari 
130184d34d69SLeila Ghaffari   // Set number of 1D nodes and quadrature points
130284d34d69SLeila Ghaffari   numP = degree + 1;
130384d34d69SLeila Ghaffari   numQ = numP + qextra;
130484d34d69SLeila Ghaffari 
130584d34d69SLeila Ghaffari   // Print summary
130684d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1307ccaff030SJeremy L Thompson     CeedInt gdofs, odofs;
1308ccaff030SJeremy L Thompson     int comm_size;
1309ccaff030SJeremy L Thompson     char box_faces_str[PETSC_MAX_PATH_LEN] = "NONE";
1310ccaff030SJeremy L Thompson     ierr = VecGetSize(Q, &gdofs); CHKERRQ(ierr);
1311ccaff030SJeremy L Thompson     ierr = VecGetLocalSize(Q, &odofs); CHKERRQ(ierr);
131284d34d69SLeila Ghaffari     gnodes = gdofs/ncompq;
1313ccaff030SJeremy L Thompson     ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr);
1314ccaff030SJeremy L Thompson     ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str,
1315ccaff030SJeremy L Thompson                                  sizeof(box_faces_str), NULL); CHKERRQ(ierr);
131684d34d69SLeila Ghaffari     const char *usedresource;
131784d34d69SLeila Ghaffari     CeedGetResource(ceed, &usedresource);
1318ccaff030SJeremy L Thompson 
131984d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
132084d34d69SLeila Ghaffari                        "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
132184d34d69SLeila Ghaffari                        "  rank(s)                              : %d\n"
132284d34d69SLeila Ghaffari                        "  Problem:\n"
132384d34d69SLeila Ghaffari                        "    Problem Name                       : %s\n"
132484d34d69SLeila Ghaffari                        "    Stabilization                      : %s\n"
132584d34d69SLeila Ghaffari                        "  PETSc:\n"
132684d34d69SLeila Ghaffari                        "    Box Faces                          : %s\n"
132784d34d69SLeila Ghaffari                        "  libCEED:\n"
132884d34d69SLeila Ghaffari                        "    libCEED Backend                    : %s\n"
132984d34d69SLeila Ghaffari                        "    libCEED Backend MemType            : %s\n"
133084d34d69SLeila Ghaffari                        "    libCEED User Requested MemType     : %s\n"
133184d34d69SLeila Ghaffari                        "  Mesh:\n"
133284d34d69SLeila Ghaffari                        "    Number of 1D Basis Nodes (P)       : %d\n"
133384d34d69SLeila Ghaffari                        "    Number of 1D Quadrature Points (Q) : %d\n"
133484d34d69SLeila Ghaffari                        "    Global DoFs                        : %D\n"
133584d34d69SLeila Ghaffari                        "    Owned DoFs                         : %D\n"
133684d34d69SLeila Ghaffari                        "    DoFs per node                      : %D\n"
133784d34d69SLeila Ghaffari                        "    Global nodes                       : %D\n"
133884d34d69SLeila Ghaffari                        "    Owned nodes                        : %D\n",
133984d34d69SLeila Ghaffari                        comm_size, problemTypes[problemChoice],
134084d34d69SLeila Ghaffari                        StabilizationTypes[stab], box_faces_str, usedresource,
134184d34d69SLeila Ghaffari                        CeedMemTypes[memtypebackend],
134284d34d69SLeila Ghaffari                        (setmemtyperequest) ?
134384d34d69SLeila Ghaffari                        CeedMemTypes[memtyperequested] : "none",
134484d34d69SLeila Ghaffari                        numP, numQ, gdofs, odofs, ncompq, gnodes, lnodes);
134584d34d69SLeila Ghaffari     CHKERRQ(ierr);
13460c6c0b13SLeila Ghaffari   }
13470c6c0b13SLeila Ghaffari 
1348ccaff030SJeremy L Thompson   // Set up global mass vector
1349ccaff030SJeremy L Thompson   ierr = VecDuplicate(Q, &user->M); CHKERRQ(ierr);
1350ccaff030SJeremy L Thompson 
135184d34d69SLeila Ghaffari   // Set up libCEED
1352ccaff030SJeremy L Thompson   // CEED Bases
1353ccaff030SJeremy L Thompson   CeedInit(ceedresource, &ceed);
135484d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompq, numP, numQ, CEED_GAUSS,
135584d34d69SLeila Ghaffari                                   &basisq);
135684d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numQ, CEED_GAUSS,
135784d34d69SLeila Ghaffari                                   &basisx);
135884d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numP,
135984d34d69SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxc);
1360ccaff030SJeremy L Thompson   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
1361ccaff030SJeremy L Thompson   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
1362ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1363ccaff030SJeremy L Thompson 
1364ccaff030SJeremy L Thompson   // CEED Restrictions
13651e150236SLeila Ghaffari   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, numP, numQ,
136684d34d69SLeila Ghaffari                                  qdatasizeVol, &restrictq, &restrictx,
136784d34d69SLeila Ghaffari                                  &restrictqdi); CHKERRQ(ierr);
1368ccaff030SJeremy L Thompson 
1369ccaff030SJeremy L Thompson   ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
1370ccaff030SJeremy L Thompson   ierr = CreateVectorFromPetscVec(ceed, Xloc, &xcorners); CHKERRQ(ierr);
1371ccaff030SJeremy L Thompson 
1372ccaff030SJeremy L Thompson   // Create the CEED vectors that will be needed in setup
1373bd910870SLeila Ghaffari   CeedInt NqptsVol;
137484d34d69SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisq, &NqptsVol);
137584d34d69SLeila Ghaffari   CeedElemRestrictionGetNumElements(restrictq, &localNelemVol);
13768b982baeSLeila Ghaffari   CeedVectorCreate(ceed, qdatasizeVol*localNelemVol*NqptsVol, &qdata);
137784d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &q0ceed, NULL);
1378ccaff030SJeremy L Thompson 
1379ccaff030SJeremy L Thompson   // Create the Q-function that builds the quadrature data for the NS operator
1380ea6e0f84SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupVol, problem->setupVol_loc,
1381ea6e0f84SLeila Ghaffari                               &qf_setupVol);
1382ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "dx", ncompx*dim, CEED_EVAL_GRAD);
1383ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "weight", 1, CEED_EVAL_WEIGHT);
13848b982baeSLeila Ghaffari   CeedQFunctionAddOutput(qf_setupVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1385ccaff030SJeremy L Thompson 
1386ccaff030SJeremy L Thompson   // Create the Q-function that sets the ICs of the operator
1387ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, problem->ics, problem->ics_loc, &qf_ics);
1388ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_ics, "x", ncompx, CEED_EVAL_INTERP);
1389ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_ics, "q0", ncompq, CEED_EVAL_NONE);
1390ccaff030SJeremy L Thompson 
1391ea6e0f84SLeila Ghaffari   qf_rhsVol = NULL;
1392ea6e0f84SLeila Ghaffari   if (problem->applyVol_rhs) { // Create the Q-function that defines the action of the RHS operator
1393ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_rhs,
1394ea6e0f84SLeila Ghaffari                                 problem->applyVol_rhs_loc, &qf_rhsVol);
1395ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "q", ncompq, CEED_EVAL_INTERP);
1396ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
13978b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1398ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "x", ncompx, CEED_EVAL_INTERP);
1399ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "v", ncompq, CEED_EVAL_INTERP);
1400ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1401ccaff030SJeremy L Thompson   }
1402ccaff030SJeremy L Thompson 
1403ea6e0f84SLeila Ghaffari   qf_ifunctionVol = NULL;
1404ea6e0f84SLeila Ghaffari   if (problem->applyVol_ifunction) { // Create the Q-function that defines the action of the IFunction
1405ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_ifunction,
1406ea6e0f84SLeila Ghaffari                                 problem->applyVol_ifunction_loc, &qf_ifunctionVol);
1407ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "q", ncompq, CEED_EVAL_INTERP);
1408ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
1409ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdot", ncompq, CEED_EVAL_INTERP);
14108b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1411ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "x", ncompx, CEED_EVAL_INTERP);
1412ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "v", ncompq, CEED_EVAL_INTERP);
1413ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1414ccaff030SJeremy L Thompson   }
1415ccaff030SJeremy L Thompson 
1416ccaff030SJeremy L Thompson   // Create the operator that builds the quadrature data for the NS operator
1417ea6e0f84SLeila Ghaffari   CeedOperatorCreate(ceed, qf_setupVol, NULL, NULL, &op_setupVol);
141884d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "dx", restrictx, basisx, CEED_VECTOR_ACTIVE);
1419ea6e0f84SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "weight", CEED_ELEMRESTRICTION_NONE,
142084d34d69SLeila Ghaffari                        basisx, CEED_VECTOR_NONE);
142184d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "qdata", restrictqdi,
1422ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1423ccaff030SJeremy L Thompson 
1424ccaff030SJeremy L Thompson   // Create the operator that sets the ICs
1425ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics);
142684d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "x", restrictx, basisxc, CEED_VECTOR_ACTIVE);
142784d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "q0", restrictq,
1428ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1429ccaff030SJeremy L Thompson 
143084d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qceed, NULL);
143184d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qdotceed, NULL);
143284d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->gceed, NULL);
1433ccaff030SJeremy L Thompson 
1434ea6e0f84SLeila Ghaffari   if (qf_rhsVol) { // Create the RHS physics operator
1435ccaff030SJeremy L Thompson     CeedOperator op;
1436ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_rhsVol, NULL, NULL, &op);
143784d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
143884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
143984d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
14408b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
144184d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
144284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
144384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1444d3630711SJed Brown     user->op_rhs_vol = op;
1445ccaff030SJeremy L Thompson   }
1446ccaff030SJeremy L Thompson 
1447ea6e0f84SLeila Ghaffari   if (qf_ifunctionVol) { // Create the IFunction operator
1448ccaff030SJeremy L Thompson     CeedOperator op;
1449ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_ifunctionVol, NULL, NULL, &op);
145084d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
145184d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
145284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdot", restrictq, basisq, user->qdotceed);
145384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
14548b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
145584d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
145684d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
145784d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1458d3630711SJed Brown     user->op_ifunction_vol = op;
1459ccaff030SJeremy L Thompson   }
1460ccaff030SJeremy L Thompson 
14616a0edaf9SLeila Ghaffari   // Set up CEED for the boundaries
14626a0edaf9SLeila Ghaffari   CeedInt height = 1;
14636a0edaf9SLeila Ghaffari   CeedInt dimSur = dim - height;
14641e150236SLeila Ghaffari   CeedInt numP_Sur = degree + 1;
14651e150236SLeila Ghaffari   CeedInt numQ_Sur = numP_Sur + qextraSur;
1466cfa64770SLeila Ghaffari   const CeedInt qdatasizeSur = problem->qdatasizeSur;
1467cfa64770SLeila Ghaffari   CeedBasis basisxSur, basisxcSur, basisqSur;
1468cfa64770SLeila Ghaffari   CeedInt NqptsSur;
14697ed8b9c7SLeila Ghaffari   CeedQFunction qf_setupSur, qf_applySur;
1470cfa64770SLeila Ghaffari 
1471cfa64770SLeila Ghaffari   // CEED bases for the boundaries
1472ebb4b9bdSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompq, numP_Sur, numQ_Sur,
1473ebb4b9bdSLeila Ghaffari                                   CEED_GAUSS,
14746a0edaf9SLeila Ghaffari                                   &basisqSur);
14756a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numQ_Sur, CEED_GAUSS,
14766a0edaf9SLeila Ghaffari                                   &basisxSur);
14776a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numP_Sur,
14786a0edaf9SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxcSur);
14796a0edaf9SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisqSur, &NqptsSur);
14806a0edaf9SLeila Ghaffari 
1481cfa64770SLeila Ghaffari   // Create the Q-function that builds the quadrature data for the Surface operator
14826a0edaf9SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupSur, problem->setupSur_loc,
14836a0edaf9SLeila Ghaffari                               &qf_setupSur);
14846a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "dx", ncompx*dimSur, CEED_EVAL_GRAD);
14856a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "weight", 1, CEED_EVAL_WEIGHT);
14866a0edaf9SLeila Ghaffari   CeedQFunctionAddOutput(qf_setupSur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
14876a0edaf9SLeila Ghaffari 
14887659d40cSLeila Ghaffari   // Creat Q-Function for Boundaries
14897ed8b9c7SLeila Ghaffari   qf_applySur = NULL;
14907659d40cSLeila Ghaffari   if (problem->applySur) {
14917659d40cSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applySur,
14927ed8b9c7SLeila Ghaffari                                 problem->applySur_loc, &qf_applySur);
14937ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "q", ncompq, CEED_EVAL_INTERP);
14947ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
14957ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "x", ncompx, CEED_EVAL_INTERP);
14967ed8b9c7SLeila Ghaffari     CeedQFunctionAddOutput(qf_applySur, "v", ncompq, CEED_EVAL_INTERP);
14979fe13df9SLeila Ghaffari   }
14989fe13df9SLeila Ghaffari 
14999fe13df9SLeila Ghaffari   // Create CEED Operator for the whole domain
15009fe13df9SLeila Ghaffari   if (!implicit)
1501ebb4b9bdSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, &bc, wind_type, user->op_rhs_vol,
1502ebb4b9bdSLeila Ghaffari                                    qf_applySur, qf_setupSur,
1503f259b054Svaleriabarra                                    height, numP_Sur, numQ_Sur, qdatasizeSur,
1504f259b054Svaleriabarra                                    NqptsSur, basisxSur, basisqSur,
1505f259b054Svaleriabarra                                    &user->op_rhs); CHKERRQ(ierr);
15069fe13df9SLeila Ghaffari   if (implicit)
1507f259b054Svaleriabarra     ierr = CreateOperatorForDomain(ceed, dm, &bc, wind_type,
1508f259b054Svaleriabarra                                    user->op_ifunction_vol,
1509ebb4b9bdSLeila Ghaffari                                    qf_applySur, qf_setupSur,
1510f259b054Svaleriabarra                                    height, numP_Sur, numQ_Sur, qdatasizeSur,
1511f259b054Svaleriabarra                                    NqptsSur, basisxSur, basisqSur,
1512f259b054Svaleriabarra                                    &user->op_ifunction); CHKERRQ(ierr);
15137659d40cSLeila Ghaffari   // Set up contex for QFunctions
1514ccaff030SJeremy L Thompson   CeedQFunctionSetContext(qf_ics, &ctxSetup, sizeof ctxSetup);
1515ccaff030SJeremy L Thompson   CeedScalar ctxNS[8] = {lambda, mu, k, cv, cp, g, Rd};
1516ccaff030SJeremy L Thompson   struct Advection2dContext_ ctxAdvection2d = {
1517ccaff030SJeremy L Thompson     .CtauS = CtauS,
1518ccaff030SJeremy L Thompson     .strong_form = strong_form,
1519ccaff030SJeremy L Thompson     .stabilization = stab,
1520ccaff030SJeremy L Thompson   };
15217659d40cSLeila Ghaffari   struct SurfaceContext_ ctxSurface = {
152216c0476cSLeila Ghaffari     .E_wind = E_wind,
15237659d40cSLeila Ghaffari     .strong_form = strong_form,
15247659d40cSLeila Ghaffari     .implicit = implicit,
15257659d40cSLeila Ghaffari   };
1526ccaff030SJeremy L Thompson   switch (problemChoice) {
1527ccaff030SJeremy L Thompson   case NS_DENSITY_CURRENT:
1528ea6e0f84SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxNS, sizeof ctxNS);
1529ea6e0f84SLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, &ctxNS,
1530ccaff030SJeremy L Thompson           sizeof ctxNS);
1531ccaff030SJeremy L Thompson     break;
1532ccaff030SJeremy L Thompson   case NS_ADVECTION:
1533ccaff030SJeremy L Thompson   case NS_ADVECTION2D:
1534ea6e0f84SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxAdvection2d,
1535ccaff030SJeremy L Thompson                                              sizeof ctxAdvection2d);
1536ea6e0f84SLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, &ctxAdvection2d,
1537ccaff030SJeremy L Thompson           sizeof ctxAdvection2d);
1538ebb4b9bdSLeila Ghaffari     if (qf_applySur) CeedQFunctionSetContext(qf_applySur, &ctxSurface,
1539ebb4b9bdSLeila Ghaffari           sizeof ctxSurface);
1540ccaff030SJeremy L Thompson   }
1541ccaff030SJeremy L Thompson 
1542ccaff030SJeremy L Thompson   // Set up PETSc context
1543ccaff030SJeremy L Thompson   // Set up units structure
1544ccaff030SJeremy L Thompson   units->meter = meter;
1545ccaff030SJeremy L Thompson   units->kilogram = kilogram;
1546ccaff030SJeremy L Thompson   units->second = second;
1547ccaff030SJeremy L Thompson   units->Kelvin = Kelvin;
1548ccaff030SJeremy L Thompson   units->Pascal = Pascal;
1549ccaff030SJeremy L Thompson   units->JperkgK = JperkgK;
1550ccaff030SJeremy L Thompson   units->mpersquareds = mpersquareds;
1551ccaff030SJeremy L Thompson   units->WpermK = WpermK;
1552ccaff030SJeremy L Thompson   units->kgpercubicm = kgpercubicm;
1553ccaff030SJeremy L Thompson   units->kgpersquaredms = kgpersquaredms;
1554ccaff030SJeremy L Thompson   units->Joulepercubicm = Joulepercubicm;
155516c0476cSLeila Ghaffari   units->Joule = Joule;
1556ccaff030SJeremy L Thompson 
1557ccaff030SJeremy L Thompson   // Set up user structure
1558ccaff030SJeremy L Thompson   user->comm = comm;
1559ccaff030SJeremy L Thompson   user->outputfreq = outputfreq;
1560ccaff030SJeremy L Thompson   user->contsteps = contsteps;
1561ccaff030SJeremy L Thompson   user->units = units;
1562ccaff030SJeremy L Thompson   user->dm = dm;
1563ccaff030SJeremy L Thompson   user->dmviz = dmviz;
1564ccaff030SJeremy L Thompson   user->interpviz = interpviz;
1565ccaff030SJeremy L Thompson   user->ceed = ceed;
1566ccaff030SJeremy L Thompson 
15678b982baeSLeila Ghaffari   // Calculate qdata and ICs
1568ccaff030SJeremy L Thompson   // Set up state global and local vectors
1569ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
1570ccaff030SJeremy L Thompson 
1571cfa64770SLeila Ghaffari   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
1572ccaff030SJeremy L Thompson 
1573ccaff030SJeremy L Thompson   // Apply Setup Ceed Operators
1574ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(xcorners, Xloc); CHKERRQ(ierr);
15758b982baeSLeila Ghaffari   CeedOperatorApply(op_setupVol, xcorners, qdata, CEED_REQUEST_IMMEDIATE);
157684d34d69SLeila Ghaffari   ierr = ComputeLumpedMassMatrix(ceed, dm, restrictq, basisq, restrictqdi, qdata,
1577ccaff030SJeremy L Thompson                                  user->M); CHKERRQ(ierr);
1578ccaff030SJeremy L Thompson 
157984d34d69SLeila Ghaffari   ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qloc, Q, restrictq,
158084d34d69SLeila Ghaffari                              &ctxSetup, 0.0); CHKERRQ(ierr);
1581ccaff030SJeremy L Thompson   if (1) { // Record boundary values from initial condition and override DMPlexInsertBoundaryValues()
1582ccaff030SJeremy L Thompson     // We use this for the main simulation DM because the reference DMPlexInsertBoundaryValues() is very slow.  If we
1583ccaff030SJeremy L Thompson     // disable this, we should still get the same results due to the problem->bc function, but with potentially much
1584ccaff030SJeremy L Thompson     // slower execution.
1585ccaff030SJeremy L Thompson     Vec Qbc;
1586ccaff030SJeremy L Thompson     ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1587ccaff030SJeremy L Thompson     ierr = VecCopy(Qloc, Qbc); CHKERRQ(ierr);
1588ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
1589ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
1590ccaff030SJeremy L Thompson     ierr = VecAXPY(Qbc, -1., Qloc); CHKERRQ(ierr);
1591ccaff030SJeremy L Thompson     ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1592ccaff030SJeremy L Thompson     ierr = PetscObjectComposeFunction((PetscObject)dm,
159384d34d69SLeila Ghaffari                                       "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS);
159484d34d69SLeila Ghaffari     CHKERRQ(ierr);
1595ccaff030SJeremy L Thompson   }
1596ccaff030SJeremy L Thompson 
1597ccaff030SJeremy L Thompson   MPI_Comm_rank(comm, &rank);
1598ccaff030SJeremy L Thompson   if (!rank) {ierr = PetscMkdir(user->outputfolder); CHKERRQ(ierr);}
1599ccaff030SJeremy L Thompson   // Gather initial Q values
1600ccaff030SJeremy L Thompson   // In case of continuation of simulation, set up initial values from binary file
1601ccaff030SJeremy L Thompson   if (contsteps) { // continue from existent solution
1602ccaff030SJeremy L Thompson     PetscViewer viewer;
1603ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1604ccaff030SJeremy L Thompson     // Read input
1605ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
1606ccaff030SJeremy L Thompson                          user->outputfolder);
1607ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1608ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1609ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1610ccaff030SJeremy L Thompson     ierr = VecLoad(Q, viewer); CHKERRQ(ierr);
1611ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1612ccaff030SJeremy L Thompson   }
1613ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Qloc); CHKERRQ(ierr);
1614ccaff030SJeremy L Thompson 
1615ccaff030SJeremy L Thompson // Create and setup TS
1616ccaff030SJeremy L Thompson   ierr = TSCreate(comm, &ts); CHKERRQ(ierr);
1617ccaff030SJeremy L Thompson   ierr = TSSetDM(ts, dm); CHKERRQ(ierr);
1618ccaff030SJeremy L Thompson   if (implicit) {
1619ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSBDF); CHKERRQ(ierr);
1620ccaff030SJeremy L Thompson     if (user->op_ifunction) {
1621ccaff030SJeremy L Thompson       ierr = TSSetIFunction(ts, NULL, IFunction_NS, &user); CHKERRQ(ierr);
1622ccaff030SJeremy L Thompson     } else {                    // Implicit integrators can fall back to using an RHSFunction
1623ccaff030SJeremy L Thompson       ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1624ccaff030SJeremy L Thompson     }
1625ccaff030SJeremy L Thompson   } else {
1626ccaff030SJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL,
1627ccaff030SJeremy L Thompson                                  "Problem does not provide RHSFunction");
1628ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSRK); CHKERRQ(ierr);
1629ccaff030SJeremy L Thompson     ierr = TSRKSetType(ts, TSRK5F); CHKERRQ(ierr);
1630ccaff030SJeremy L Thompson     ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1631ccaff030SJeremy L Thompson   }
1632ccaff030SJeremy L Thompson   ierr = TSSetMaxTime(ts, 500. * units->second); CHKERRQ(ierr);
1633ccaff030SJeremy L Thompson   ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER); CHKERRQ(ierr);
1634ccaff030SJeremy L Thompson   ierr = TSSetTimeStep(ts, 1.e-2 * units->second); CHKERRQ(ierr);
163584d34d69SLeila Ghaffari   if (testChoice != TEST_NONE) {ierr = TSSetMaxSteps(ts, 10); CHKERRQ(ierr);}
1636ccaff030SJeremy L Thompson   ierr = TSGetAdapt(ts, &adapt); CHKERRQ(ierr);
1637ccaff030SJeremy L Thompson   ierr = TSAdaptSetStepLimits(adapt,
1638ccaff030SJeremy L Thompson                               1.e-12 * units->second,
1639ccaff030SJeremy L Thompson                               1.e2 * units->second); CHKERRQ(ierr);
1640ccaff030SJeremy L Thompson   ierr = TSSetFromOptions(ts); CHKERRQ(ierr);
1641ccaff030SJeremy L Thompson   if (!contsteps) { // print initial condition
164284d34d69SLeila Ghaffari     if (testChoice == TEST_NONE) {
1643ccaff030SJeremy L Thompson       ierr = TSMonitor_NS(ts, 0, 0., Q, user); CHKERRQ(ierr);
1644ccaff030SJeremy L Thompson     }
1645ccaff030SJeremy L Thompson   } else { // continue from time of last output
1646ccaff030SJeremy L Thompson     PetscReal time;
1647ccaff030SJeremy L Thompson     PetscInt count;
1648ccaff030SJeremy L Thompson     PetscViewer viewer;
1649ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1650ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
1651ccaff030SJeremy L Thompson                          user->outputfolder); CHKERRQ(ierr);
1652ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1653ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1654ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL);
1655ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1656ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1657ccaff030SJeremy L Thompson     ierr = TSSetTime(ts, time * user->units->second); CHKERRQ(ierr);
1658ccaff030SJeremy L Thompson   }
165984d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1660ccaff030SJeremy L Thompson     ierr = TSMonitorSet(ts, TSMonitor_NS, user, NULL); CHKERRQ(ierr);
1661ccaff030SJeremy L Thompson   }
1662ccaff030SJeremy L Thompson 
1663ccaff030SJeremy L Thompson   // Solve
1664ccaff030SJeremy L Thompson   start = MPI_Wtime();
1665ccaff030SJeremy L Thompson   ierr = PetscBarrier((PetscObject)ts); CHKERRQ(ierr);
1666ccaff030SJeremy L Thompson   ierr = TSSolve(ts, Q); CHKERRQ(ierr);
1667ccaff030SJeremy L Thompson   cpu_time_used = MPI_Wtime() - start;
1668ccaff030SJeremy L Thompson   ierr = TSGetSolveTime(ts, &ftime); CHKERRQ(ierr);
1669ccaff030SJeremy L Thompson   ierr = MPI_Allreduce(MPI_IN_PLACE, &cpu_time_used, 1, MPI_DOUBLE, MPI_MIN,
1670ccaff030SJeremy L Thompson                        comm); CHKERRQ(ierr);
167184d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1672ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
167384d34d69SLeila Ghaffari                        "Time taken for solution (sec): %g\n",
1674ccaff030SJeremy L Thompson                        (double)cpu_time_used); CHKERRQ(ierr);
1675ccaff030SJeremy L Thompson   }
1676ccaff030SJeremy L Thompson 
1677ccaff030SJeremy L Thompson   // Get error
167884d34d69SLeila Ghaffari   if (problem->non_zero_time && testChoice == TEST_NONE) {
1679ccaff030SJeremy L Thompson     Vec Qexact, Qexactloc;
1680ccaff030SJeremy L Thompson     PetscReal norm;
1681ccaff030SJeremy L Thompson     ierr = DMCreateGlobalVector(dm, &Qexact); CHKERRQ(ierr);
1682ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
1683ccaff030SJeremy L Thompson     ierr = VecGetSize(Qexactloc, &lnodes); CHKERRQ(ierr);
1684ccaff030SJeremy L Thompson 
168584d34d69SLeila Ghaffari     ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qexactloc, Qexact,
168684d34d69SLeila Ghaffari                                restrictq, &ctxSetup, ftime); CHKERRQ(ierr);
1687ccaff030SJeremy L Thompson 
1688ccaff030SJeremy L Thompson     ierr = VecAXPY(Q, -1.0, Qexact);  CHKERRQ(ierr);
1689ccaff030SJeremy L Thompson     ierr = VecNorm(Q, NORM_MAX, &norm); CHKERRQ(ierr);
1690cfa64770SLeila Ghaffari     CeedVectorDestroy(&q0ceed);
1691ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1692ccaff030SJeremy L Thompson                        "Max Error: %g\n",
1693ccaff030SJeremy L Thompson                        (double)norm); CHKERRQ(ierr);
169484d34d69SLeila Ghaffari     // Clean up vectors
169584d34d69SLeila Ghaffari     ierr = DMRestoreLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
169684d34d69SLeila Ghaffari     ierr = VecDestroy(&Qexact); CHKERRQ(ierr);
1697ccaff030SJeremy L Thompson   }
1698ccaff030SJeremy L Thompson 
1699ccaff030SJeremy L Thompson   // Output Statistics
1700ccaff030SJeremy L Thompson   ierr = TSGetStepNumber(ts, &steps); CHKERRQ(ierr);
170184d34d69SLeila Ghaffari   if (testChoice == TEST_NONE) {
1702ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1703ccaff030SJeremy L Thompson                        "Time integrator took %D time steps to reach final time %g\n",
1704ccaff030SJeremy L Thompson                        steps, (double)ftime); CHKERRQ(ierr);
1705ccaff030SJeremy L Thompson   }
170684d34d69SLeila Ghaffari   // Output numerical values from command line
170784d34d69SLeila Ghaffari   ierr = VecViewFromOptions(Q, NULL, "-vec_view"); CHKERRQ(ierr);
170884d34d69SLeila Ghaffari 
1709335ea220SLeila Ghaffari   // Compare reference solution values with current test run for CI
171084d34d69SLeila Ghaffari   if (testChoice != TEST_NONE) {
171184d34d69SLeila Ghaffari     PetscViewer viewer;
171284d34d69SLeila Ghaffari     // Read reference file
171384d34d69SLeila Ghaffari     Vec Qref;
171484d34d69SLeila Ghaffari     PetscReal error, Qrefnorm;
171584d34d69SLeila Ghaffari     ierr = VecDuplicate(Q, &Qref); CHKERRQ(ierr);
171684d34d69SLeila Ghaffari     ierr = PetscViewerBinaryOpen(comm, test->filepath, FILE_MODE_READ, &viewer);
171784d34d69SLeila Ghaffari     CHKERRQ(ierr);
171884d34d69SLeila Ghaffari     ierr = VecLoad(Qref, viewer); CHKERRQ(ierr);
171984d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
172084d34d69SLeila Ghaffari 
172184d34d69SLeila Ghaffari     // Compute error with respect to reference solution
172284d34d69SLeila Ghaffari     ierr = VecAXPY(Q, -1.0, Qref);  CHKERRQ(ierr);
172384d34d69SLeila Ghaffari     ierr = VecNorm(Qref, NORM_MAX, &Qrefnorm); CHKERRQ(ierr);
172484d34d69SLeila Ghaffari     ierr = VecScale(Q, 1./Qrefnorm); CHKERRQ(ierr);
172584d34d69SLeila Ghaffari     ierr = VecNorm(Q, NORM_MAX, &error); CHKERRQ(ierr);
172684d34d69SLeila Ghaffari     ierr = VecDestroy(&Qref); CHKERRQ(ierr);
172784d34d69SLeila Ghaffari     // Check error
172884d34d69SLeila Ghaffari     if (error > test->testtol) {
172984d34d69SLeila Ghaffari       ierr = PetscPrintf(PETSC_COMM_WORLD,
173084d34d69SLeila Ghaffari                          "Test failed with error norm %g\n",
173184d34d69SLeila Ghaffari                          (double)error); CHKERRQ(ierr);
173284d34d69SLeila Ghaffari     }
173384d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
173484d34d69SLeila Ghaffari   }
17359cf88b28Svaleriabarra 
1736ccaff030SJeremy L Thompson   // Clean up libCEED
17378b982baeSLeila Ghaffari   CeedVectorDestroy(&qdata);
1738ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qceed);
1739ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qdotceed);
1740ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->gceed);
1741ccaff030SJeremy L Thompson   CeedVectorDestroy(&xcorners);
174284d34d69SLeila Ghaffari   CeedBasisDestroy(&basisq);
174384d34d69SLeila Ghaffari   CeedBasisDestroy(&basisx);
174484d34d69SLeila Ghaffari   CeedBasisDestroy(&basisxc);
174584d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictq);
174684d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictx);
174784d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictqdi);
1748ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupVol);
1749ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_ics);
1750ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_rhsVol);
1751ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_ifunctionVol);
1752ea6e0f84SLeila Ghaffari   CeedOperatorDestroy(&op_setupVol);
1753ccaff030SJeremy L Thompson   CeedOperatorDestroy(&op_ics);
17546a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_rhs_vol);
17556a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_ifunction_vol);
17566a0edaf9SLeila Ghaffari   CeedDestroy(&ceed);
17576a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisqSur);
17586a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxSur);
17596a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxcSur);
17606a0edaf9SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupSur);
17617ed8b9c7SLeila Ghaffari   CeedQFunctionDestroy(&qf_applySur);
1762ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_rhs);
1763ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_ifunction);
1764ccaff030SJeremy L Thompson 
1765ccaff030SJeremy L Thompson   // Clean up PETSc
1766ccaff030SJeremy L Thompson   ierr = VecDestroy(&Q); CHKERRQ(ierr);
1767ccaff030SJeremy L Thompson   ierr = VecDestroy(&user->M); CHKERRQ(ierr);
1768ccaff030SJeremy L Thompson   ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1769ccaff030SJeremy L Thompson   ierr = DMDestroy(&dmviz); CHKERRQ(ierr);
1770ccaff030SJeremy L Thompson   ierr = TSDestroy(&ts); CHKERRQ(ierr);
1771ccaff030SJeremy L Thompson   ierr = DMDestroy(&dm); CHKERRQ(ierr);
1772ccaff030SJeremy L Thompson   ierr = PetscFree(units); CHKERRQ(ierr);
1773ccaff030SJeremy L Thompson   ierr = PetscFree(user); CHKERRQ(ierr);
1774ccaff030SJeremy L Thompson   return PetscFinalize();
1775ccaff030SJeremy L Thompson }
1776ccaff030SJeremy L Thompson 
1777