xref: /libCEED/examples/fluids/navierstokes.c (revision a45a933fa78da9b71b55603518f17a332153126f)
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
3228688798Sjeremylt //     ./navierstokes -ceed /gpu/cuda -problem advection -degree 1
33ccaff030SJeremy L Thompson //
34dc8efd83SLeila Ghaffari //TESTARGS(name="dc_explicit") -ceed {ceed_resource} -test -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. -ts_dt 1e-3 -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-dc-explicit.bin
35dc8efd83SLeila Ghaffari //TESTARGS(name="dc_implicit_stab_none") -ceed {ceed_resource} -test -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 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-dc-implicit-stab-none.bin
36dc8efd83SLeila Ghaffari //TESTARGS(name="adv_rotation_explicit_strong") -ceed {ceed_resource} -test -problem advection -strong_form 1 -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. -ts_dt 1e-3 -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-rotation-explicit-strong.bin
37dc8efd83SLeila Ghaffari //TESTARGS(name="adv_rotation_implicit_stab_supg") -ceed {ceed_resource} -test -problem advection -CtauS .3 -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. -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 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-rotation-implicit-stab-supg.bin
38dc8efd83SLeila Ghaffari //TESTARGS(name="adv_translation_implicit_stab_su") -ceed {ceed_resource} -test -problem advection -CtauS .3 -stab su -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. -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 -problem_advection_wind translation -problem_advection_wind_translation .53,-1.33,-2.65 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv-translation-implicit-stab-su.bin
39dc8efd83SLeila Ghaffari //TESTARGS(name="adv2d_rotation_explicit_strong") -ceed {ceed_resource} -test -problem advection2d -strong_form 1 -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. -ts_dt 1e-3 -compare_final_state_atol 1E-11 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv2d-rotation-explicit-strong.bin
40dc8efd83SLeila Ghaffari //TESTARGS(name="adv2d_rotation_implicit_stab_supg") -ceed {ceed_resource} -test -problem advection2d -CtauS .3 -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. -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 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv2d-rotation-implicit-stab-supg.bin
41dc8efd83SLeila Ghaffari //TESTARGS(name="adv2d_translation_implicit_stab_su") -ceed {ceed_resource} -test -problem advection2d -CtauS .3 -stab su -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. -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 -problem_advection_wind translation -problem_advection_wind_translation .53,-1.33,0 -compare_final_state_atol 5E-4 -compare_final_state_filename examples/fluids/tests-output/fluids-navierstokes-adv2d-translation-implicit-stab-su.bin
42ccaff030SJeremy L Thompson 
43ccaff030SJeremy L Thompson /// @file
44ccaff030SJeremy L Thompson /// Navier-Stokes example using PETSc
45ccaff030SJeremy L Thompson 
46ccaff030SJeremy L Thompson const char help[] = "Solve Navier-Stokes using PETSc and libCEED\n";
47ccaff030SJeremy L Thompson 
48ccaff030SJeremy L Thompson #include <ceed.h>
493d576824SJeremy L Thompson #include <petscdmplex.h>
50ccaff030SJeremy L Thompson #include <petscsys.h>
513d576824SJeremy L Thompson #include <petscts.h>
523d576824SJeremy L Thompson #include <stdbool.h>
53ccaff030SJeremy L Thompson #include "advection.h"
54ccaff030SJeremy L Thompson #include "advection2d.h"
553d576824SJeremy L Thompson #include "common.h"
56eb088987SLeila Ghaffari #include "euler-vortex.h"
57ccaff030SJeremy L Thompson #include "densitycurrent.h"
583d576824SJeremy L Thompson #include "setup-boundary.h"
59ccaff030SJeremy L Thompson 
6084d34d69SLeila Ghaffari #if PETSC_VERSION_LT(3,14,0)
6184d34d69SLeila Ghaffari #  define DMPlexGetClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexGetClosureIndices(a,b,c,d,f,g,i)
6284d34d69SLeila Ghaffari #  define DMPlexRestoreClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexRestoreClosureIndices(a,b,c,d,f,g,i)
6384d34d69SLeila Ghaffari #endif
6484d34d69SLeila Ghaffari 
653ab4fca6SValeria Barra #if PETSC_VERSION_LT(3,14,0)
663ab4fca6SValeria 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)
673ab4fca6SValeria Barra #endif
683ab4fca6SValeria Barra 
6984d34d69SLeila Ghaffari // MemType Options
7084d34d69SLeila Ghaffari static const char *const memTypes[] = {
7184d34d69SLeila Ghaffari   "host",
7284d34d69SLeila Ghaffari   "device",
7384d34d69SLeila Ghaffari   "memType", "CEED_MEM_", NULL
7484d34d69SLeila Ghaffari };
7584d34d69SLeila Ghaffari 
76ccaff030SJeremy L Thompson // Problem Options
77ccaff030SJeremy L Thompson typedef enum {
78ccaff030SJeremy L Thompson   NS_DENSITY_CURRENT = 0,
79ccaff030SJeremy L Thompson   NS_ADVECTION = 1,
80ccaff030SJeremy L Thompson   NS_ADVECTION2D = 2,
81eb088987SLeila Ghaffari   NS_EULER_VORTEX = 3,
82ccaff030SJeremy L Thompson } problemType;
83ccaff030SJeremy L Thompson static const char *const problemTypes[] = {
84ccaff030SJeremy L Thompson   "density_current",
85ccaff030SJeremy L Thompson   "advection",
86ccaff030SJeremy L Thompson   "advection2d",
87e5ed8c30SLeila Ghaffari   "euler_vortex",
8884d34d69SLeila Ghaffari   "problemType", "NS_", NULL
89ccaff030SJeremy L Thompson };
90ccaff030SJeremy L Thompson 
911184866aSLeila Ghaffari // Wind Options for Advection
921184866aSLeila Ghaffari typedef enum {
931184866aSLeila Ghaffari   ADVECTION_WIND_ROTATION = 0,
941184866aSLeila Ghaffari   ADVECTION_WIND_TRANSLATION = 1,
951184866aSLeila Ghaffari } WindType;
961184866aSLeila Ghaffari static const char *const WindTypes[] = {
971184866aSLeila Ghaffari   "rotation",
981184866aSLeila Ghaffari   "translation",
991184866aSLeila Ghaffari   "WindType", "ADVECTION_WIND_", NULL
1001184866aSLeila Ghaffari };
1011184866aSLeila Ghaffari 
102ccaff030SJeremy L Thompson typedef enum {
103ccaff030SJeremy L Thompson   STAB_NONE = 0,
104ccaff030SJeremy L Thompson   STAB_SU = 1,   // Streamline Upwind
105ccaff030SJeremy L Thompson   STAB_SUPG = 2, // Streamline Upwind Petrov-Galerkin
106ccaff030SJeremy L Thompson } StabilizationType;
107ccaff030SJeremy L Thompson static const char *const StabilizationTypes[] = {
10884d34d69SLeila Ghaffari   "none",
109ccaff030SJeremy L Thompson   "SU",
110ccaff030SJeremy L Thompson   "SUPG",
111ccaff030SJeremy L Thompson   "StabilizationType", "STAB_", NULL
112ccaff030SJeremy L Thompson };
113ccaff030SJeremy L Thompson 
114a3ddc43aSLeila Ghaffari typedef enum {
115a3ddc43aSLeila Ghaffari   EULER_TEST_NONE = 0,
116a3ddc43aSLeila Ghaffari   EULER_TEST_1 = 1,
117a3ddc43aSLeila Ghaffari   EULER_TEST_2 = 2,
118a3ddc43aSLeila Ghaffari   EULER_TEST_3 = 3,
119a3ddc43aSLeila Ghaffari   EULER_TEST_4 = 4,
120a3ddc43aSLeila Ghaffari } EulerTestType;
121a3ddc43aSLeila Ghaffari static const char *const EulerTestTypes[] = {
122a3ddc43aSLeila Ghaffari   "none",
123a3ddc43aSLeila Ghaffari   "t1",
124a3ddc43aSLeila Ghaffari   "t2",
125a3ddc43aSLeila Ghaffari   "t3",
126a3ddc43aSLeila Ghaffari   "t4",
127a3ddc43aSLeila Ghaffari   "EulerTestType", "EULER_TEST_", NULL
128a3ddc43aSLeila Ghaffari };
129a3ddc43aSLeila Ghaffari 
130ccaff030SJeremy L Thompson // Problem specific data
131ccaff030SJeremy L Thompson typedef struct {
1328b982baeSLeila Ghaffari   CeedInt dim, qdatasizeVol, qdatasizeSur;
133ebb4b9bdSLeila Ghaffari   CeedQFunctionUser setupVol, setupSur, ics, applyVol_rhs, applyVol_ifunction,
134e2043960SLeila Ghaffari                     applySur;
135ccaff030SJeremy L Thompson   PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt,
136ccaff030SJeremy L Thompson                        PetscScalar[], void *);
1377659d40cSLeila Ghaffari   const char *setupVol_loc, *setupSur_loc, *ics_loc, *applyVol_rhs_loc,
138e2043960SLeila Ghaffari         *applyVol_ifunction_loc, *applySur_loc;
139ccaff030SJeremy L Thompson   const bool non_zero_time;
140ccaff030SJeremy L Thompson } problemData;
141ccaff030SJeremy L Thompson 
142ccaff030SJeremy L Thompson problemData problemOptions[] = {
143ccaff030SJeremy L Thompson   [NS_DENSITY_CURRENT] = {
144ccaff030SJeremy L Thompson     .dim                       = 3,
145ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1468b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
147b0137797SLeila Ghaffari     .setupVol                  = Setup,
148b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
149356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
150356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
151ccaff030SJeremy L Thompson     .ics                       = ICsDC,
152ccaff030SJeremy L Thompson     .ics_loc                   = ICsDC_loc,
153c96c872fSLeila Ghaffari     .applyVol_rhs              = DC,
154c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = DC_loc,
155c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_DC,
156c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_DC_loc,
157ccaff030SJeremy L Thompson     .bc                        = Exact_DC,
15884d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
159ccaff030SJeremy L Thompson   },
160ccaff030SJeremy L Thompson   [NS_ADVECTION] = {
161ccaff030SJeremy L Thompson     .dim                       = 3,
162ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1638b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
164b0137797SLeila Ghaffari     .setupVol                  = Setup,
165b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
166356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
167356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
168ccaff030SJeremy L Thompson     .ics                       = ICsAdvection,
169ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection_loc,
170c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection,
171c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection_loc,
172c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection,
173c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection_loc,
1747659d40cSLeila Ghaffari     .applySur                  = Advection_Sur,
1757659d40cSLeila Ghaffari     .applySur_loc              = Advection_Sur_loc,
176ccaff030SJeremy L Thompson     .bc                        = Exact_Advection,
17784d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
178ccaff030SJeremy L Thompson   },
179ccaff030SJeremy L Thompson   [NS_ADVECTION2D] = {
180ccaff030SJeremy L Thompson     .dim                       = 2,
181ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 5,
1828b982baeSLeila Ghaffari     .qdatasizeSur              = 3,
183c96c872fSLeila Ghaffari     .setupVol                  = Setup2d,
184c96c872fSLeila Ghaffari     .setupVol_loc              = Setup2d_loc,
185b0137797SLeila Ghaffari     .setupSur                  = SetupBoundary2d,
186b0137797SLeila Ghaffari     .setupSur_loc              = SetupBoundary2d_loc,
187ccaff030SJeremy L Thompson     .ics                       = ICsAdvection2d,
188ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection2d_loc,
189c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection2d,
190c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection2d_loc,
191c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection2d,
192c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection2d_loc,
1937659d40cSLeila Ghaffari     .applySur                  = Advection2d_Sur,
1947659d40cSLeila Ghaffari     .applySur_loc              = Advection2d_Sur_loc,
195ccaff030SJeremy L Thompson     .bc                        = Exact_Advection2d,
19684d34d69SLeila Ghaffari     .non_zero_time             = PETSC_TRUE,
197ccaff030SJeremy L Thompson   },
198eb088987SLeila Ghaffari   [NS_EULER_VORTEX] = {
199eb088987SLeila Ghaffari     .dim                       = 3,
200eb088987SLeila Ghaffari     .qdatasizeVol              = 10,
201eb088987SLeila Ghaffari     .qdatasizeSur              = 4,
202eb088987SLeila Ghaffari     .setupVol                  = Setup,
203eb088987SLeila Ghaffari     .setupVol_loc              = Setup_loc,
204eb088987SLeila Ghaffari     .setupSur                  = SetupBoundary,
205eb088987SLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
206eb088987SLeila Ghaffari     .ics                       = ICsEuler,
207eb088987SLeila Ghaffari     .ics_loc                   = ICsEuler_loc,
208eb088987SLeila Ghaffari     .applyVol_rhs              = Euler,
209eb088987SLeila Ghaffari     .applyVol_rhs_loc          = Euler_loc,
210b6df2e11SLeila Ghaffari     .applySur                  = Euler_Sur,
211b6df2e11SLeila Ghaffari     .applySur_loc              = Euler_Sur_loc,
212eb088987SLeila Ghaffari     .bc                        = Exact_Euler,
21347ff3025SLeila Ghaffari     .non_zero_time             = PETSC_TRUE,
214eb088987SLeila Ghaffari   },
215ccaff030SJeremy L Thompson };
216ccaff030SJeremy L Thompson 
217ccaff030SJeremy L Thompson // PETSc user data
218ccaff030SJeremy L Thompson typedef struct User_ *User;
219ccaff030SJeremy L Thompson typedef struct Units_ *Units;
220ccaff030SJeremy L Thompson 
221ccaff030SJeremy L Thompson struct User_ {
222ccaff030SJeremy L Thompson   MPI_Comm comm;
223ccaff030SJeremy L Thompson   PetscInt outputfreq;
224ccaff030SJeremy L Thompson   DM dm;
225ccaff030SJeremy L Thompson   DM dmviz;
226ccaff030SJeremy L Thompson   Mat interpviz;
227ccaff030SJeremy L Thompson   Ceed ceed;
228ccaff030SJeremy L Thompson   Units units;
229ccaff030SJeremy L Thompson   CeedVector qceed, qdotceed, gceed;
2301e150236SLeila Ghaffari   CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction;
231ccaff030SJeremy L Thompson   Vec M;
232d99129b9SLeila Ghaffari   char outputdir[PETSC_MAX_PATH_LEN];
233ccaff030SJeremy L Thompson   PetscInt contsteps;
2345bf10c6fSLeila Ghaffari   EulerContext ctxEulerData;
235ccaff030SJeremy L Thompson };
236ccaff030SJeremy L Thompson 
237ccaff030SJeremy L Thompson struct Units_ {
238ccaff030SJeremy L Thompson   // fundamental units
239ccaff030SJeremy L Thompson   PetscScalar meter;
240ccaff030SJeremy L Thompson   PetscScalar kilogram;
241ccaff030SJeremy L Thompson   PetscScalar second;
242ccaff030SJeremy L Thompson   PetscScalar Kelvin;
243ccaff030SJeremy L Thompson   // derived units
244ccaff030SJeremy L Thompson   PetscScalar Pascal;
245ccaff030SJeremy L Thompson   PetscScalar JperkgK;
246ccaff030SJeremy L Thompson   PetscScalar mpersquareds;
247ccaff030SJeremy L Thompson   PetscScalar WpermK;
248ccaff030SJeremy L Thompson   PetscScalar kgpercubicm;
249ccaff030SJeremy L Thompson   PetscScalar kgpersquaredms;
250ccaff030SJeremy L Thompson   PetscScalar Joulepercubicm;
25116c0476cSLeila Ghaffari   PetscScalar Joule;
252ccaff030SJeremy L Thompson };
253ccaff030SJeremy L Thompson 
254ccaff030SJeremy L Thompson typedef struct SimpleBC_ *SimpleBC;
255ccaff030SJeremy L Thompson struct SimpleBC_ {
2567659d40cSLeila Ghaffari   PetscInt nwall, nslip[3];
2577659d40cSLeila Ghaffari   PetscInt walls[6], slips[3][6];
25884d34d69SLeila Ghaffari   PetscBool userbc;
259ccaff030SJeremy L Thompson };
260ccaff030SJeremy L Thompson 
261ccaff030SJeremy L Thompson // Essential BC dofs are encoded in closure indices as -(i+1).
262ccaff030SJeremy L Thompson static PetscInt Involute(PetscInt i) {
263ccaff030SJeremy L Thompson   return i >= 0 ? i : -(i+1);
264ccaff030SJeremy L Thompson }
265ccaff030SJeremy L Thompson 
266ccaff030SJeremy L Thompson // Utility function to create local CEED restriction
267ccaff030SJeremy L Thompson static PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt P,
26884d34d69SLeila Ghaffari     CeedInt height, DMLabel domainLabel, CeedInt value,
269ccaff030SJeremy L Thompson     CeedElemRestriction *Erestrict) {
270ccaff030SJeremy L Thompson 
271ccaff030SJeremy L Thompson   PetscSection section;
2721184866aSLeila Ghaffari   PetscInt p, Nelem, Ndof, *erestrict, eoffset, nfields, dim, depth;
2730c6c0b13SLeila Ghaffari   DMLabel depthLabel;
2740c6c0b13SLeila Ghaffari   IS depthIS, iterIS;
27584d34d69SLeila Ghaffari   Vec Uloc;
2760c6c0b13SLeila Ghaffari   const PetscInt *iterIndices;
277ccaff030SJeremy L Thompson   PetscErrorCode ierr;
278ccaff030SJeremy L Thompson 
279ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
280ccaff030SJeremy L Thompson   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
281da51bdd9SLeila Ghaffari   dim -= height;
282ccaff030SJeremy L Thompson   ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
283ccaff030SJeremy L Thompson   ierr = PetscSectionGetNumFields(section, &nfields); CHKERRQ(ierr);
284ccaff030SJeremy L Thompson   PetscInt ncomp[nfields], fieldoff[nfields+1];
285ccaff030SJeremy L Thompson   fieldoff[0] = 0;
286ccaff030SJeremy L Thompson   for (PetscInt f=0; f<nfields; f++) {
287ccaff030SJeremy L Thompson     ierr = PetscSectionGetFieldComponents(section, f, &ncomp[f]); CHKERRQ(ierr);
288ccaff030SJeremy L Thompson     fieldoff[f+1] = fieldoff[f] + ncomp[f];
289ccaff030SJeremy L Thompson   }
290ccaff030SJeremy L Thompson 
2910c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepth(dm, &depth); CHKERRQ(ierr);
2920c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepthLabel(dm, &depthLabel); CHKERRQ(ierr);
2930c6c0b13SLeila Ghaffari   ierr = DMLabelGetStratumIS(depthLabel, depth - height, &depthIS); CHKERRQ(ierr);
2940c6c0b13SLeila Ghaffari   if (domainLabel) {
2950c6c0b13SLeila Ghaffari     IS domainIS;
2960c6c0b13SLeila Ghaffari     ierr = DMLabelGetStratumIS(domainLabel, value, &domainIS); CHKERRQ(ierr);
2971119eeeeSJed Brown     if (domainIS) { // domainIS is non-empty
2980c6c0b13SLeila Ghaffari       ierr = ISIntersect(depthIS, domainIS, &iterIS); CHKERRQ(ierr);
2990c6c0b13SLeila Ghaffari       ierr = ISDestroy(&domainIS); CHKERRQ(ierr);
3001119eeeeSJed Brown     } else { // domainIS is NULL (empty)
3011119eeeeSJed Brown       iterIS = NULL;
3021119eeeeSJed Brown     }
3030c6c0b13SLeila Ghaffari     ierr = ISDestroy(&depthIS); CHKERRQ(ierr);
3040c6c0b13SLeila Ghaffari   } else {
3050c6c0b13SLeila Ghaffari     iterIS = depthIS;
3060c6c0b13SLeila Ghaffari   }
3071119eeeeSJed Brown   if (iterIS) {
3080c6c0b13SLeila Ghaffari     ierr = ISGetLocalSize(iterIS, &Nelem); CHKERRQ(ierr);
3090c6c0b13SLeila Ghaffari     ierr = ISGetIndices(iterIS, &iterIndices); CHKERRQ(ierr);
3101119eeeeSJed Brown   } else {
3111119eeeeSJed Brown     Nelem = 0;
3121119eeeeSJed Brown     iterIndices = NULL;
3131119eeeeSJed Brown   }
314ccaff030SJeremy L Thompson   ierr = PetscMalloc1(Nelem*PetscPowInt(P, dim), &erestrict); CHKERRQ(ierr);
3150c6c0b13SLeila Ghaffari   for (p=0,eoffset=0; p<Nelem; p++) {
3160c6c0b13SLeila Ghaffari     PetscInt c = iterIndices[p];
317ccaff030SJeremy L Thompson     PetscInt numindices, *indices, nnodes;
31884d34d69SLeila Ghaffari     ierr = DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE,
31984d34d69SLeila Ghaffari                                    &numindices, &indices, NULL, NULL);
32084d34d69SLeila Ghaffari     CHKERRQ(ierr);
32132b5ec5fSJed Brown     bool flip = false;
32232b5ec5fSJed Brown     if (height > 0) {
32332b5ec5fSJed Brown       PetscInt numCells, numFaces, start = -1;
32432b5ec5fSJed Brown       const PetscInt *orients, *faces, *cells;
32532b5ec5fSJed Brown       ierr = DMPlexGetSupport(dm, c, &cells); CHKERRQ(ierr);
32632b5ec5fSJed Brown       ierr = DMPlexGetSupportSize(dm, c, &numCells); CHKERRQ(ierr);
327ebb4b9bdSLeila Ghaffari       if (numCells != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
328f259b054Svaleriabarra                                     "Expected one cell in support of exterior face, but got %D cells",
329f259b054Svaleriabarra                                     numCells);
33032b5ec5fSJed Brown       ierr = DMPlexGetCone(dm, cells[0], &faces); CHKERRQ(ierr);
33132b5ec5fSJed Brown       ierr = DMPlexGetConeSize(dm, cells[0], &numFaces); CHKERRQ(ierr);
33232b5ec5fSJed Brown       for (PetscInt i=0; i<numFaces; i++) {if (faces[i] == c) start = i;}
333ebb4b9bdSLeila Ghaffari       if (start < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT,
334f259b054Svaleriabarra                                 "Could not find face %D in cone of its support",
335f259b054Svaleriabarra                                 c);
33632b5ec5fSJed Brown       ierr = DMPlexGetConeOrientation(dm, cells[0], &orients); CHKERRQ(ierr);
33732b5ec5fSJed Brown       if (orients[start] < 0) flip = true;
33832b5ec5fSJed Brown     }
33984d34d69SLeila Ghaffari     if (numindices % fieldoff[nfields]) SETERRQ1(PETSC_COMM_SELF,
34084d34d69SLeila Ghaffari           PETSC_ERR_ARG_INCOMP, "Number of closure indices not compatible with Cell %D",
34184d34d69SLeila Ghaffari           c);
342ccaff030SJeremy L Thompson     nnodes = numindices / fieldoff[nfields];
343ccaff030SJeremy L Thompson     for (PetscInt i=0; i<nnodes; i++) {
34432b5ec5fSJed Brown       PetscInt ii = i;
34532b5ec5fSJed Brown       if (flip) {
34632b5ec5fSJed Brown         if (P == nnodes) ii = nnodes - 1 - i;
34732b5ec5fSJed Brown         else if (P*P == nnodes) {
34832b5ec5fSJed Brown           PetscInt row = i / P, col = i % P;
34932b5ec5fSJed Brown           ii = row + col * P;
350ebb4b9bdSLeila Ghaffari         } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP,
351f259b054Svaleriabarra                           "No support for flipping point with %D nodes != P (%D) or P^2",
352f259b054Svaleriabarra                           nnodes, P);
35332b5ec5fSJed Brown       }
354ccaff030SJeremy L Thompson       // Check that indices are blocked by node and thus can be coalesced as a single field with
355ccaff030SJeremy L Thompson       // fieldoff[nfields] = sum(ncomp) components.
356ccaff030SJeremy L Thompson       for (PetscInt f=0; f<nfields; f++) {
357ccaff030SJeremy L Thompson         for (PetscInt j=0; j<ncomp[f]; j++) {
35832b5ec5fSJed Brown           if (Involute(indices[fieldoff[f]*nnodes + ii*ncomp[f] + j])
35932b5ec5fSJed Brown               != Involute(indices[ii*ncomp[0]]) + fieldoff[f] + j)
360ccaff030SJeremy L Thompson             SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
361ccaff030SJeremy L Thompson                      "Cell %D closure indices not interlaced for node %D field %D component %D",
36232b5ec5fSJed Brown                      c, ii, f, j);
363ccaff030SJeremy L Thompson         }
364ccaff030SJeremy L Thompson       }
365ccaff030SJeremy L Thompson       // Essential boundary conditions are encoded as -(loc+1), but we don't care so we decode.
36632b5ec5fSJed Brown       PetscInt loc = Involute(indices[ii*ncomp[0]]);
3676f55dfd5Svaleriabarra       erestrict[eoffset++] = loc;
368ccaff030SJeremy L Thompson     }
36984d34d69SLeila Ghaffari     ierr = DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE,
37084d34d69SLeila Ghaffari                                        &numindices, &indices, NULL, NULL);
37184d34d69SLeila Ghaffari     CHKERRQ(ierr);
372ccaff030SJeremy L Thompson   }
3730c6c0b13SLeila Ghaffari   if (eoffset != Nelem*PetscPowInt(P, dim))
3740c6c0b13SLeila Ghaffari     SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_LIB,
3750c6c0b13SLeila Ghaffari              "ElemRestriction of size (%D,%D) initialized %D nodes", Nelem,
376ccaff030SJeremy L Thompson              PetscPowInt(P, dim),eoffset);
3771119eeeeSJed Brown   if (iterIS) {
3780c6c0b13SLeila Ghaffari     ierr = ISRestoreIndices(iterIS, &iterIndices); CHKERRQ(ierr);
3791119eeeeSJed Brown   }
3800c6c0b13SLeila Ghaffari   ierr = ISDestroy(&iterIS); CHKERRQ(ierr);
3810c6c0b13SLeila Ghaffari 
382ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Uloc); CHKERRQ(ierr);
383ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(Uloc, &Ndof); CHKERRQ(ierr);
384ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Uloc); CHKERRQ(ierr);
3856f55dfd5Svaleriabarra   CeedElemRestrictionCreate(ceed, Nelem, PetscPowInt(P, dim), fieldoff[nfields],
3866f55dfd5Svaleriabarra                             1, Ndof, CEED_MEM_HOST, CEED_COPY_VALUES, erestrict,
3876f55dfd5Svaleriabarra                             Erestrict);
388ccaff030SJeremy L Thompson   ierr = PetscFree(erestrict); CHKERRQ(ierr);
389ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
390ccaff030SJeremy L Thompson }
391ccaff030SJeremy L Thompson 
392c96c872fSLeila Ghaffari // Utility function to get Ceed Restriction for each domain
3931e150236SLeila Ghaffari static PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
3941e150236SLeila Ghaffari     DMLabel domainLabel, PetscInt value, CeedInt P, CeedInt Q, CeedInt qdatasize,
3951e150236SLeila Ghaffari     CeedElemRestriction *restrictq, CeedElemRestriction *restrictx,
3961e150236SLeila Ghaffari     CeedElemRestriction *restrictqdi) {
397c96c872fSLeila Ghaffari 
398c96c872fSLeila Ghaffari   DM dmcoord;
3991e150236SLeila Ghaffari   CeedInt dim, localNelem;
4001e150236SLeila Ghaffari   CeedInt Qdim;
401c96c872fSLeila Ghaffari   PetscErrorCode ierr;
402c96c872fSLeila Ghaffari 
403c96c872fSLeila Ghaffari   PetscFunctionBeginUser;
4041e150236SLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
4051e150236SLeila Ghaffari   dim -= height;
4061e150236SLeila Ghaffari   Qdim = CeedIntPow(Q, dim);
407c96c872fSLeila Ghaffari   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
408c96c872fSLeila Ghaffari   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
409c96c872fSLeila Ghaffari   CHKERRQ(ierr);
410ebb4b9bdSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dm, P, height, domainLabel, value,
411ebb4b9bdSLeila Ghaffari                                    restrictq);
412c96c872fSLeila Ghaffari   CHKERRQ(ierr);
413ebb4b9bdSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dmcoord, 2, height, domainLabel, value,
414ebb4b9bdSLeila Ghaffari                                    restrictx);
415c96c872fSLeila Ghaffari   CHKERRQ(ierr);
416c96c872fSLeila Ghaffari   CeedElemRestrictionGetNumElements(*restrictq, &localNelem);
417c96c872fSLeila Ghaffari   CeedElemRestrictionCreateStrided(ceed, localNelem, Qdim,
418c96c872fSLeila Ghaffari                                    qdatasize, qdatasize*localNelem*Qdim,
419c96c872fSLeila Ghaffari                                    CEED_STRIDES_BACKEND, restrictqdi);
420c96c872fSLeila Ghaffari   PetscFunctionReturn(0);
421c96c872fSLeila Ghaffari }
422c96c872fSLeila Ghaffari 
4231e150236SLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
4247659d40cSLeila Ghaffari static PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc,
4254438636fSLeila Ghaffari     problemType problemChoice, WindType wind_type, CeedOperator op_applyVol,
426e2043960SLeila Ghaffari     CeedQFunction qf_applySur, CeedQFunction qf_setupSur,CeedInt height,
4274438636fSLeila Ghaffari     CeedInt numP_Sur, CeedInt numQ_Sur, CeedInt qdatasizeSur, CeedInt NqptsSur,
4284438636fSLeila Ghaffari     CeedBasis basisxSur, CeedBasis basisqSur, CeedOperator *op_apply) {
429ca3ac6ddSLeila Ghaffari 
4307659d40cSLeila Ghaffari   CeedInt dim, nFace;
4314438636fSLeila Ghaffari   PetscInt lsize;
4321e150236SLeila Ghaffari   Vec Xloc;
4334438636fSLeila Ghaffari   CeedVector xcorners;
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);
4414438636fSLeila Ghaffari   // --Apply a Sub-Operator for the volume
4424438636fSLeila Ghaffari   CeedCompositeOperatorAddSub(*op_apply, op_applyVol);
443ca3ac6ddSLeila Ghaffari 
4444438636fSLeila Ghaffari   // Required data for in/outflow BCs
4451e150236SLeila Ghaffari   ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
4461e150236SLeila Ghaffari   ierr = VecGetLocalSize(Xloc, &lsize); CHKERRQ(ierr);
4471e150236SLeila Ghaffari   ierr = CeedVectorCreate(ceed, lsize, &xcorners); CHKERRQ(ierr);
4481e150236SLeila Ghaffari   ierr = VecGetArray(Xloc, &x); CHKERRQ(ierr);
4491e150236SLeila Ghaffari   CeedVectorSetArray(xcorners, CEED_MEM_HOST, CEED_USE_POINTER, x);
450ca3ac6ddSLeila Ghaffari   ierr = DMGetLabel(dm, "Face Sets", &domainLabel); CHKERRQ(ierr);
4517659d40cSLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
4524438636fSLeila Ghaffari 
45326aa2eeeSLeila Ghaffari   if (wind_type == ADVECTION_WIND_TRANSLATION
45426aa2eeeSLeila Ghaffari       || problemChoice == NS_EULER_VORTEX) {
4554438636fSLeila Ghaffari     // Ignore wall and slip BCs
456b6df2e11SLeila Ghaffari     bc->nwall = bc->nslip[0] = bc->nslip[1] = 0;
457b6df2e11SLeila Ghaffari     if (wind_type == ADVECTION_WIND_TRANSLATION) bc->nslip[2] = 0;
4584438636fSLeila Ghaffari 
4594438636fSLeila Ghaffari     // Set number of faces
4607659d40cSLeila Ghaffari     if (dim == 2) nFace = 4;
4617659d40cSLeila Ghaffari     if (dim == 3) nFace = 6;
4629fe13df9SLeila Ghaffari 
4637659d40cSLeila Ghaffari     // Create CEED Operator for each boundary face
4644438636fSLeila Ghaffari     PetscInt localNelemSur[6];
4654438636fSLeila Ghaffari     CeedVector qdataSur[6];
4664438636fSLeila Ghaffari     CeedOperator op_setupSur[6], op_applySur[6];
4674438636fSLeila Ghaffari     CeedElemRestriction restrictxSur[6], restrictqSur[6], restrictqdiSur[6];
46886ba6f09SLeila Ghaffari 
4697659d40cSLeila Ghaffari     for (CeedInt i=0; i<nFace; i++) {
4707659d40cSLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domainLabel, i+1, numP_Sur,
471f259b054Svaleriabarra                                      numQ_Sur, qdatasizeSur, &restrictqSur[i],
472f259b054Svaleriabarra                                      &restrictxSur[i], &restrictqdiSur[i]);
473f259b054Svaleriabarra       CHKERRQ(ierr);
4747659d40cSLeila Ghaffari       // Create the CEED vectors that will be needed in Boundary setup
4757659d40cSLeila Ghaffari       CeedElemRestrictionGetNumElements(restrictqSur[i], &localNelemSur[i]);
476f259b054Svaleriabarra       CeedVectorCreate(ceed, qdatasizeSur*localNelemSur[i]*NqptsSur,
477f259b054Svaleriabarra                        &qdataSur[i]);
4787659d40cSLeila Ghaffari       // Create the operator that builds the quadrature data for the Boundary operator
4797659d40cSLeila Ghaffari       CeedOperatorCreate(ceed, qf_setupSur, NULL, NULL, &op_setupSur[i]);
480ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "dx", restrictxSur[i], basisxSur,
481ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4827659d40cSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "weight", CEED_ELEMRESTRICTION_NONE,
483ca3ac6ddSLeila Ghaffari                            basisxSur, CEED_VECTOR_NONE);
4847659d40cSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "qdataSur", restrictqdiSur[i],
485ca3ac6ddSLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
4867659d40cSLeila Ghaffari       // Create Boundary operator
4877659d40cSLeila Ghaffari       CeedOperatorCreate(ceed, qf_applySur, NULL, NULL, &op_applySur[i]);
488ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "q", restrictqSur[i], basisqSur,
489ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4907659d40cSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "qdataSur", restrictqdiSur[i],
4917659d40cSLeila Ghaffari                            CEED_BASIS_COLLOCATED, qdataSur[i]);
492f259b054Svaleriabarra       CeedOperatorSetField(op_applySur[i], "x", restrictxSur[i], basisxSur,
493f259b054Svaleriabarra                            xcorners);
494ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "v", restrictqSur[i], basisqSur,
495ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4967659d40cSLeila Ghaffari       // Apply CEED operator for Boundary setup
497ebb4b9bdSLeila Ghaffari       CeedOperatorApply(op_setupSur[i], xcorners, qdataSur[i],
498ebb4b9bdSLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
4994438636fSLeila Ghaffari       // --Apply Sub-Operator for the Boundary
5007659d40cSLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_applySur[i]);
5019fe13df9SLeila Ghaffari     }
5021e150236SLeila Ghaffari     CeedVectorDestroy(&xcorners);
503ca3ac6ddSLeila Ghaffari   }
504ca3ac6ddSLeila Ghaffari   PetscFunctionReturn(0);
505ca3ac6ddSLeila Ghaffari }
506ca3ac6ddSLeila Ghaffari 
507ccaff030SJeremy L Thompson static int CreateVectorFromPetscVec(Ceed ceed, Vec p, CeedVector *v) {
508ccaff030SJeremy L Thompson   PetscErrorCode ierr;
509ccaff030SJeremy L Thompson   PetscInt m;
510ccaff030SJeremy L Thompson 
511ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
512ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &m); CHKERRQ(ierr);
513ccaff030SJeremy L Thompson   ierr = CeedVectorCreate(ceed, m, v); CHKERRQ(ierr);
514ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
515ccaff030SJeremy L Thompson }
516ccaff030SJeremy L Thompson 
517ccaff030SJeremy L Thompson static int VectorPlacePetscVec(CeedVector c, Vec p) {
518ccaff030SJeremy L Thompson   PetscErrorCode ierr;
519ccaff030SJeremy L Thompson   PetscInt mceed, mpetsc;
520ccaff030SJeremy L Thompson   PetscScalar *a;
521ccaff030SJeremy L Thompson 
522ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
523ccaff030SJeremy L Thompson   ierr = CeedVectorGetLength(c, &mceed); CHKERRQ(ierr);
524ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &mpetsc); CHKERRQ(ierr);
525ccaff030SJeremy L Thompson   if (mceed != mpetsc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
52684d34d69SLeila Ghaffari                                   "Cannot place PETSc Vec of length %D in CeedVector of length %D",
52784d34d69SLeila Ghaffari                                   mpetsc, mceed);
528ccaff030SJeremy L Thompson   ierr = VecGetArray(p, &a); CHKERRQ(ierr);
529ccaff030SJeremy L Thompson   CeedVectorSetArray(c, CEED_MEM_HOST, CEED_USE_POINTER, a);
530ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
531ccaff030SJeremy L Thompson }
532ccaff030SJeremy L Thompson 
533ccaff030SJeremy L Thompson static PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm,
534ccaff030SJeremy L Thompson     PetscBool insertEssential, Vec Qloc, PetscReal time, Vec faceGeomFVM,
535ccaff030SJeremy L Thompson     Vec cellGeomFVM, Vec gradFVM) {
536ccaff030SJeremy L Thompson   PetscErrorCode ierr;
537ccaff030SJeremy L Thompson   Vec Qbc;
538ccaff030SJeremy L Thompson 
539ccaff030SJeremy L Thompson   PetscFunctionBegin;
540ccaff030SJeremy L Thompson   ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
541ccaff030SJeremy L Thompson   ierr = VecAXPY(Qloc, 1., Qbc); CHKERRQ(ierr);
542ccaff030SJeremy L Thompson   ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
543ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
544ccaff030SJeremy L Thompson }
545ccaff030SJeremy L Thompson 
546ccaff030SJeremy L Thompson // This is the RHS of the ODE, given as u_t = G(t,u)
547ccaff030SJeremy L Thompson // This function takes in a state vector Q and writes into G
548ccaff030SJeremy L Thompson static PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *userData) {
549ccaff030SJeremy L Thompson   PetscErrorCode ierr;
550ccaff030SJeremy L Thompson   User user = *(User *)userData;
551ccaff030SJeremy L Thompson   PetscScalar *q, *g;
552ccaff030SJeremy L Thompson   Vec Qloc, Gloc;
553ccaff030SJeremy L Thompson 
554ccaff030SJeremy L Thompson   // Global-to-local
555ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
5565bf10c6fSLeila Ghaffari   user->ctxEulerData->currentTime = t;
557ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
558ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
559ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
560ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
561ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
562ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
563ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
564ccaff030SJeremy L Thompson 
565ccaff030SJeremy L Thompson   // Ceed Vectors
566ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
567ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
568ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER, q);
569ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
570ccaff030SJeremy L Thompson 
571ccaff030SJeremy L Thompson   // Apply CEED operator
572ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->qceed, user->gceed,
573ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
574ccaff030SJeremy L Thompson 
575ccaff030SJeremy L Thompson   // Restore vectors
576ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
577ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
578ccaff030SJeremy L Thompson 
579ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
580ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
581ccaff030SJeremy L Thompson 
582ccaff030SJeremy L Thompson   // Inverse of the lumped mass matrix
583ccaff030SJeremy L Thompson   ierr = VecPointwiseMult(G, G, user->M); // M is Minv
584ccaff030SJeremy L Thompson   CHKERRQ(ierr);
585ccaff030SJeremy L Thompson 
586ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
587ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
588ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
589ccaff030SJeremy L Thompson }
590ccaff030SJeremy L Thompson 
591ccaff030SJeremy L Thompson static PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Qdot, Vec G,
592ccaff030SJeremy L Thompson                                    void *userData) {
593ccaff030SJeremy L Thompson   PetscErrorCode ierr;
594ccaff030SJeremy L Thompson   User user = *(User *)userData;
595ccaff030SJeremy L Thompson   const PetscScalar *q, *qdot;
596ccaff030SJeremy L Thompson   PetscScalar *g;
597ccaff030SJeremy L Thompson   Vec Qloc, Qdotloc, Gloc;
598ccaff030SJeremy L Thompson 
599ccaff030SJeremy L Thompson   // Global-to-local
600ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
601ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
602ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
603ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
604ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
605ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
606ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
607ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
608ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qdotloc); CHKERRQ(ierr);
609ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Qdot, INSERT_VALUES, Qdotloc); CHKERRQ(ierr);
610ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
611ccaff030SJeremy L Thompson 
612ccaff030SJeremy L Thompson   // Ceed Vectors
613ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, &q); CHKERRQ(ierr);
614ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
615ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
616ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER,
617ccaff030SJeremy L Thompson                      (PetscScalar *)q);
618ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qdotceed, CEED_MEM_HOST, CEED_USE_POINTER,
619ccaff030SJeremy L Thompson                      (PetscScalar *)qdot);
620ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
621ccaff030SJeremy L Thompson 
622ccaff030SJeremy L Thompson   // Apply CEED operator
623ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->qceed, user->gceed,
624ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
625ccaff030SJeremy L Thompson 
626ccaff030SJeremy L Thompson   // Restore vectors
627ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, &q); CHKERRQ(ierr);
628ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
629ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
630ccaff030SJeremy L Thompson 
631ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
632ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
633ccaff030SJeremy L Thompson 
634ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
635ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
636ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
637ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
638ccaff030SJeremy L Thompson }
639ccaff030SJeremy L Thompson 
640ccaff030SJeremy L Thompson // User provided TS Monitor
641ccaff030SJeremy L Thompson static PetscErrorCode TSMonitor_NS(TS ts, PetscInt stepno, PetscReal time,
642ccaff030SJeremy L Thompson                                    Vec Q, void *ctx) {
643ccaff030SJeremy L Thompson   User user = ctx;
644ccaff030SJeremy L Thompson   Vec Qloc;
645ccaff030SJeremy L Thompson   char filepath[PETSC_MAX_PATH_LEN];
646ccaff030SJeremy L Thompson   PetscViewer viewer;
647ccaff030SJeremy L Thompson   PetscErrorCode ierr;
648ccaff030SJeremy L Thompson 
649ccaff030SJeremy L Thompson   // Set up output
650ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
651ccaff030SJeremy L Thompson   // Print every 'outputfreq' steps
652ccaff030SJeremy L Thompson   if (stepno % user->outputfreq != 0)
653ccaff030SJeremy L Thompson     PetscFunctionReturn(0);
654ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
655ccaff030SJeremy L Thompson   ierr = PetscObjectSetName((PetscObject)Qloc, "StateVec"); CHKERRQ(ierr);
656ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
657ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
658ccaff030SJeremy L Thompson 
659ccaff030SJeremy L Thompson   // Output
660ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-%03D.vtu",
661d99129b9SLeila Ghaffari                        user->outputdir, stepno + user->contsteps);
662ccaff030SJeremy L Thompson   CHKERRQ(ierr);
663ccaff030SJeremy L Thompson   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), filepath,
664ccaff030SJeremy L Thompson                             FILE_MODE_WRITE, &viewer); CHKERRQ(ierr);
665ccaff030SJeremy L Thompson   ierr = VecView(Qloc, viewer); CHKERRQ(ierr);
6669d801c56SJed Brown   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
667ccaff030SJeremy L Thompson   if (user->dmviz) {
668ccaff030SJeremy L Thompson     Vec Qrefined, Qrefined_loc;
669ccaff030SJeremy L Thompson     char filepath_refined[PETSC_MAX_PATH_LEN];
670ccaff030SJeremy L Thompson     PetscViewer viewer_refined;
671ccaff030SJeremy L Thompson 
672ccaff030SJeremy L Thompson     ierr = DMGetGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
673ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
674ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)Qrefined_loc, "Refined");
675ccaff030SJeremy L Thompson     CHKERRQ(ierr);
676ccaff030SJeremy L Thompson     ierr = MatInterpolate(user->interpviz, Q, Qrefined); CHKERRQ(ierr);
677ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qrefined_loc); CHKERRQ(ierr);
678ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(user->dmviz, Qrefined, INSERT_VALUES, Qrefined_loc);
679ccaff030SJeremy L Thompson     CHKERRQ(ierr);
680ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath_refined, sizeof filepath_refined,
681ccaff030SJeremy L Thompson                          "%s/nsrefined-%03D.vtu",
682d99129b9SLeila Ghaffari                          user->outputdir, stepno + user->contsteps);
683ccaff030SJeremy L Thompson     CHKERRQ(ierr);
684ccaff030SJeremy L Thompson     ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Qrefined),
685ccaff030SJeremy L Thompson                               filepath_refined,
686ccaff030SJeremy L Thompson                               FILE_MODE_WRITE, &viewer_refined); CHKERRQ(ierr);
687ccaff030SJeremy L Thompson     ierr = VecView(Qrefined_loc, viewer_refined); CHKERRQ(ierr);
688ccaff030SJeremy L Thompson     ierr = DMRestoreLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
689ccaff030SJeremy L Thompson     ierr = DMRestoreGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
690ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer_refined); CHKERRQ(ierr);
691ccaff030SJeremy L Thompson   }
692ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
693ccaff030SJeremy L Thompson 
694ccaff030SJeremy L Thompson   // Save data in a binary file for continuation of simulations
695ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
696d99129b9SLeila Ghaffari                        user->outputdir); CHKERRQ(ierr);
697ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
698ccaff030SJeremy L Thompson   CHKERRQ(ierr);
699ccaff030SJeremy L Thompson   ierr = VecView(Q, viewer); CHKERRQ(ierr);
700ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
701ccaff030SJeremy L Thompson 
702ccaff030SJeremy L Thompson   // Save time stamp
703ccaff030SJeremy L Thompson   // Dimensionalize time back
704ccaff030SJeremy L Thompson   time /= user->units->second;
705ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
706d99129b9SLeila Ghaffari                        user->outputdir); CHKERRQ(ierr);
707ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
708ccaff030SJeremy L Thompson   CHKERRQ(ierr);
709ccaff030SJeremy L Thompson   #if PETSC_VERSION_GE(3,13,0)
710ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL);
711ccaff030SJeremy L Thompson   #else
712ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL, true);
713ccaff030SJeremy L Thompson   #endif
714ccaff030SJeremy L Thompson   CHKERRQ(ierr);
715ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
716ccaff030SJeremy L Thompson 
717ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
718ccaff030SJeremy L Thompson }
719ccaff030SJeremy L Thompson 
72084d34d69SLeila Ghaffari static PetscErrorCode ICs_FixMultiplicity(CeedOperator op_ics,
721ccaff030SJeremy L Thompson     CeedVector xcorners, CeedVector q0ceed, DM dm, Vec Qloc, Vec Q,
722777ff853SJeremy L Thompson     CeedElemRestriction restrictq, CeedQFunctionContext ctxSetup, CeedScalar time) {
723ccaff030SJeremy L Thompson   PetscErrorCode ierr;
724ccaff030SJeremy L Thompson   CeedVector multlvec;
725ccaff030SJeremy L Thompson   Vec Multiplicity, MultiplicityLoc;
726ccaff030SJeremy L Thompson 
727777ff853SJeremy L Thompson   SetupContext ctxSetupData;
728777ff853SJeremy L Thompson   CeedQFunctionContextGetData(ctxSetup, CEED_MEM_HOST, (void **)&ctxSetupData);
729777ff853SJeremy L Thompson   ctxSetupData->time = time;
730777ff853SJeremy L Thompson   CeedQFunctionContextRestoreData(ctxSetup, (void **)&ctxSetupData);
731777ff853SJeremy L Thompson 
732ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
733ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
734ccaff030SJeremy L Thompson   CeedOperatorApply(op_ics, xcorners, q0ceed, CEED_REQUEST_IMMEDIATE);
735ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
736ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Qloc, ADD_VALUES, Q); CHKERRQ(ierr);
737ccaff030SJeremy L Thompson 
738ccaff030SJeremy L Thompson   // Fix multiplicity for output of ICs
739ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
740ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &multlvec, NULL);
741ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(multlvec, MultiplicityLoc); CHKERRQ(ierr);
742ccaff030SJeremy L Thompson   CeedElemRestrictionGetMultiplicity(restrictq, multlvec);
743ccaff030SJeremy L Thompson   CeedVectorDestroy(&multlvec);
744ccaff030SJeremy L Thompson   ierr = DMGetGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
745ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Multiplicity); CHKERRQ(ierr);
746ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, MultiplicityLoc, ADD_VALUES, Multiplicity);
747ccaff030SJeremy L Thompson   CHKERRQ(ierr);
748ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Q, Q, Multiplicity); CHKERRQ(ierr);
749ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Qloc, Qloc, MultiplicityLoc); CHKERRQ(ierr);
750ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
751ccaff030SJeremy L Thompson   ierr = DMRestoreGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
752ccaff030SJeremy L Thompson 
753ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
754ccaff030SJeremy L Thompson }
755ccaff030SJeremy L Thompson 
756ccaff030SJeremy L Thompson static PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm,
757ccaff030SJeremy L Thompson     CeedElemRestriction restrictq, CeedBasis basisq,
758ccaff030SJeremy L Thompson     CeedElemRestriction restrictqdi, CeedVector qdata, Vec M) {
759ccaff030SJeremy L Thompson   PetscErrorCode ierr;
760ccaff030SJeremy L Thompson   CeedQFunction qf_mass;
761ccaff030SJeremy L Thompson   CeedOperator op_mass;
762ccaff030SJeremy L Thompson   CeedVector mceed;
763ccaff030SJeremy L Thompson   Vec Mloc;
764ccaff030SJeremy L Thompson   CeedInt ncompq, qdatasize;
765ccaff030SJeremy L Thompson 
766ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
767ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictq, &ncompq);
768ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictqdi, &qdatasize);
769ccaff030SJeremy L Thompson   // Create the Q-function that defines the action of the mass operator
770ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass);
771ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q", ncompq, CEED_EVAL_INTERP);
772ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "qdata", qdatasize, CEED_EVAL_NONE);
773ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", ncompq, CEED_EVAL_INTERP);
774ccaff030SJeremy L Thompson 
775ccaff030SJeremy L Thompson   // Create the mass operator
776ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
777ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
778ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", restrictqdi,
779ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, qdata);
780ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
781ccaff030SJeremy L Thompson 
782ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Mloc); CHKERRQ(ierr);
783ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Mloc); CHKERRQ(ierr);
784ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &mceed, NULL);
785ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(mceed, Mloc); CHKERRQ(ierr);
786ccaff030SJeremy L Thompson 
787ccaff030SJeremy L Thompson   {
788ccaff030SJeremy L Thompson     // Compute a lumped mass matrix
789ccaff030SJeremy L Thompson     CeedVector onesvec;
790ccaff030SJeremy L Thompson     CeedElemRestrictionCreateVector(restrictq, &onesvec, NULL);
791ccaff030SJeremy L Thompson     CeedVectorSetValue(onesvec, 1.0);
792ccaff030SJeremy L Thompson     CeedOperatorApply(op_mass, onesvec, mceed, CEED_REQUEST_IMMEDIATE);
793ccaff030SJeremy L Thompson     CeedVectorDestroy(&onesvec);
794ccaff030SJeremy L Thompson     CeedOperatorDestroy(&op_mass);
795ccaff030SJeremy L Thompson     CeedVectorDestroy(&mceed);
796ccaff030SJeremy L Thompson   }
797ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
798ccaff030SJeremy L Thompson 
799ccaff030SJeremy L Thompson   ierr = VecZeroEntries(M); CHKERRQ(ierr);
800ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Mloc, ADD_VALUES, M); CHKERRQ(ierr);
801ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Mloc); CHKERRQ(ierr);
802ccaff030SJeremy L Thompson 
803ccaff030SJeremy L Thompson   // Invert diagonally lumped mass vector for RHS function
804ccaff030SJeremy L Thompson   ierr = VecReciprocal(M); CHKERRQ(ierr);
805ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
806ccaff030SJeremy L Thompson }
807ccaff030SJeremy L Thompson 
80884d34d69SLeila Ghaffari static PetscErrorCode SetUpDM(DM dm, problemData *problem, PetscInt degree,
8095bf10c6fSLeila Ghaffari                               SimpleBC bc, void *ctxSetupData, void *ctxMMS) {
810ccaff030SJeremy L Thompson   PetscErrorCode ierr;
811ccaff030SJeremy L Thompson 
812ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
813ccaff030SJeremy L Thompson   {
814ccaff030SJeremy L Thompson     // Configure the finite element space and boundary conditions
815ccaff030SJeremy L Thompson     PetscFE fe;
816ccaff030SJeremy L Thompson     PetscInt ncompq = 5;
817ff6701fcSJed Brown     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, ncompq,
818ff6701fcSJed Brown                                  PETSC_FALSE, degree, PETSC_DECIDE,
81932ed2d11SJed Brown                                  &fe); CHKERRQ(ierr);
820ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
821ccaff030SJeremy L Thompson     ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr);
822ccaff030SJeremy L Thompson     ierr = DMCreateDS(dm); CHKERRQ(ierr);
82307af6069Svaleriabarra     {
82407af6069Svaleriabarra       PetscInt comps[1] = {1};
82507af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", "Face Sets", 0,
8263ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[0],
827777ff853SJeremy L Thompson                            bc->slips[0], ctxSetupData); CHKERRQ(ierr);
82807af6069Svaleriabarra       comps[0] = 2;
82907af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", "Face Sets", 0,
8303ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[1],
831777ff853SJeremy L Thompson                            bc->slips[1], ctxSetupData); CHKERRQ(ierr);
83207af6069Svaleriabarra       comps[0] = 3;
83307af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", "Face Sets", 0,
8343ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[2],
835777ff853SJeremy L Thompson                            bc->slips[2], ctxSetupData); CHKERRQ(ierr);
83607af6069Svaleriabarra     }
83784d34d69SLeila Ghaffari     if (bc->userbc == PETSC_TRUE) {
83884d34d69SLeila Ghaffari       for (PetscInt c = 0; c < 3; c++) {
83984d34d69SLeila Ghaffari         for (PetscInt s = 0; s < bc->nslip[c]; s++) {
84084d34d69SLeila Ghaffari           for (PetscInt w = 0; w < bc->nwall; w++) {
84184d34d69SLeila Ghaffari             if (bc->slips[c][s] == bc->walls[w])
84284d34d69SLeila Ghaffari               SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
843f259b054Svaleriabarra                        "Boundary condition already set on face %D!\n",
844f259b054Svaleriabarra                        bc->walls[w]);
84584d34d69SLeila Ghaffari           }
84684d34d69SLeila Ghaffari         }
84784d34d69SLeila Ghaffari       }
84884d34d69SLeila Ghaffari     }
84984d34d69SLeila Ghaffari     // Wall boundary conditions are zero energy density and zero flux for
85084d34d69SLeila Ghaffari     //   velocity in advection/advection2d, and zero velocity and zero flux
85184d34d69SLeila Ghaffari     //   for mass density and energy density in density_current
85284d34d69SLeila Ghaffari     {
85384d34d69SLeila Ghaffari       if (problem->bc == Exact_Advection || problem->bc == Exact_Advection2d) {
85484d34d69SLeila Ghaffari         PetscInt comps[1] = {4};
85584d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
8563ab4fca6SValeria Barra                              1, comps, (void(*)(void))problem->bc, NULL,
8570da62991SLeila Ghaffari                              bc->nwall, bc->walls, ctxSetupData); CHKERRQ(ierr);
858753d00aaSLeila Ghaffari       } else if (problem->bc == Exact_DC) {
85984d34d69SLeila Ghaffari         PetscInt comps[3] = {1, 2, 3};
86084d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
8613ab4fca6SValeria Barra                              3, comps, (void(*)(void))problem->bc, NULL,
862777ff853SJeremy L Thompson                              bc->nwall, bc->walls, ctxSetupData); CHKERRQ(ierr);
863753d00aaSLeila Ghaffari       } else if (problem->bc == Exact_Euler) {
8644a452432SLeila Ghaffari         PetscInt comps[3] = {1, 2, 3};
8654a452432SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
8664a452432SLeila Ghaffari                              3, comps, (void(*)(void))problem->bc, NULL,
8674a452432SLeila Ghaffari                              bc->nwall, bc->walls, ctxMMS); CHKERRQ(ierr);
86884d34d69SLeila Ghaffari       } else
86984d34d69SLeila Ghaffari         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
87084d34d69SLeila Ghaffari                 "Undefined boundary conditions for this problem");
87184d34d69SLeila Ghaffari     }
872ccaff030SJeremy L Thompson     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
873ccaff030SJeremy L Thompson     CHKERRQ(ierr);
874ccaff030SJeremy L Thompson     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
875ccaff030SJeremy L Thompson   }
876ccaff030SJeremy L Thompson   {
877ccaff030SJeremy L Thompson     // Empty name for conserved field (because there is only one field)
878ccaff030SJeremy L Thompson     PetscSection section;
879ccaff030SJeremy L Thompson     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
880ccaff030SJeremy L Thompson     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
881ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
882ccaff030SJeremy L Thompson     CHKERRQ(ierr);
883ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 1, "MomentumX");
884ccaff030SJeremy L Thompson     CHKERRQ(ierr);
885ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 2, "MomentumY");
886ccaff030SJeremy L Thompson     CHKERRQ(ierr);
887ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 3, "MomentumZ");
888ccaff030SJeremy L Thompson     CHKERRQ(ierr);
889ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 4, "EnergyDensity");
890ccaff030SJeremy L Thompson     CHKERRQ(ierr);
891ccaff030SJeremy L Thompson   }
892ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
893ccaff030SJeremy L Thompson }
894ccaff030SJeremy L Thompson 
895ccaff030SJeremy L Thompson int main(int argc, char **argv) {
896ccaff030SJeremy L Thompson   PetscInt ierr;
897ccaff030SJeremy L Thompson   MPI_Comm comm;
89884d34d69SLeila Ghaffari   DM dm, dmcoord, dmviz;
899ccaff030SJeremy L Thompson   Mat interpviz;
900ccaff030SJeremy L Thompson   TS ts;
901ccaff030SJeremy L Thompson   TSAdapt adapt;
902ccaff030SJeremy L Thompson   User user;
903ccaff030SJeremy L Thompson   Units units;
9045bf10c6fSLeila Ghaffari   EulerContext ctxEulerData;
905ccaff030SJeremy L Thompson   char ceedresource[4096] = "/cpu/self";
90684d34d69SLeila Ghaffari   PetscInt localNelemVol, lnodes, gnodes, steps;
907ccaff030SJeremy L Thompson   const PetscInt ncompq = 5;
908ccaff030SJeremy L Thompson   PetscMPIInt rank;
909ccaff030SJeremy L Thompson   PetscScalar ftime;
910ccaff030SJeremy L Thompson   Vec Q, Qloc, Xloc;
911ccaff030SJeremy L Thompson   Ceed ceed;
91284d34d69SLeila Ghaffari   CeedInt numP, numQ;
913cfa64770SLeila Ghaffari   CeedVector xcorners, qdata, q0ceed;
91484d34d69SLeila Ghaffari   CeedBasis basisx, basisxc, basisq;
91584d34d69SLeila Ghaffari   CeedElemRestriction restrictx, restrictq, restrictqdi;
916cfa64770SLeila Ghaffari   CeedQFunction qf_setupVol, qf_ics, qf_rhsVol, qf_ifunctionVol;
9175bf10c6fSLeila Ghaffari   CeedQFunctionContext ctxSetup, ctxNS, ctxAdvection2d, ctxSurface, ctxEuler;
918cfa64770SLeila Ghaffari   CeedOperator op_setupVol, op_ics;
919ccaff030SJeremy L Thompson   CeedScalar Rd;
92084d34d69SLeila Ghaffari   CeedMemType memtyperequested;
921ccaff030SJeremy L Thompson   PetscScalar WpermK, Pascal, JperkgK, mpersquareds, kgpercubicm,
92216c0476cSLeila Ghaffari               kgpersquaredms, Joulepercubicm, Joule;
923ccaff030SJeremy L Thompson   problemType problemChoice;
924ccaff030SJeremy L Thompson   problemData *problem = NULL;
9251184866aSLeila Ghaffari   WindType wind_type;
926ccaff030SJeremy L Thompson   StabilizationType stab;
927a3ddc43aSLeila Ghaffari   EulerTestType euler_test;
92884d34d69SLeila Ghaffari   PetscBool implicit;
929cb3e2689Svaleriabarra   PetscInt    viz_refine = 0;
930ccaff030SJeremy L Thompson   struct SimpleBC_ bc = {
93184d34d69SLeila Ghaffari     .nslip = {2, 2, 2},
93284d34d69SLeila Ghaffari     .slips = {{5, 6}, {3, 4}, {1, 2}}
933ccaff030SJeremy L Thompson   };
934ccaff030SJeremy L Thompson   double start, cpu_time_used;
935dc8efd83SLeila Ghaffari   // Test variables
936dc8efd83SLeila Ghaffari   PetscBool test;
937dc8efd83SLeila Ghaffari   PetscScalar testtol = 0.;
938dc8efd83SLeila Ghaffari   char filepath[PETSC_MAX_PATH_LEN];
93984d34d69SLeila Ghaffari   // Check PETSc CUDA support
94084d34d69SLeila Ghaffari   PetscBool petschavecuda, setmemtyperequest = PETSC_FALSE;
94184d34d69SLeila Ghaffari   // *INDENT-OFF*
94284d34d69SLeila Ghaffari   #ifdef PETSC_HAVE_CUDA
94384d34d69SLeila Ghaffari   petschavecuda = PETSC_TRUE;
94484d34d69SLeila Ghaffari   #else
94584d34d69SLeila Ghaffari   petschavecuda = PETSC_FALSE;
94684d34d69SLeila Ghaffari   #endif
94784d34d69SLeila Ghaffari   // *INDENT-ON*
948ccaff030SJeremy L Thompson 
949ccaff030SJeremy L Thompson   // Create the libCEED contexts
950ccaff030SJeremy L Thompson   PetscScalar meter          = 1e-2;     // 1 meter in scaled length units
951ccaff030SJeremy L Thompson   PetscScalar second         = 1e-2;     // 1 second in scaled time units
952ccaff030SJeremy L Thompson   PetscScalar kilogram       = 1e-6;     // 1 kilogram in scaled mass units
953ccaff030SJeremy L Thompson   PetscScalar Kelvin         = 1;        // 1 Kelvin in scaled temperature units
954ccaff030SJeremy L Thompson   CeedScalar theta0          = 300.;     // K
955ccaff030SJeremy L Thompson   CeedScalar thetaC          = -15.;     // K
956ccaff030SJeremy L Thompson   CeedScalar P0              = 1.e5;     // Pa
95716c0476cSLeila Ghaffari   CeedScalar E_wind          = 1.e6;     // J
958ccaff030SJeremy L Thompson   CeedScalar N               = 0.01;     // 1/s
959ccaff030SJeremy L Thompson   CeedScalar cv              = 717.;     // J/(kg K)
960ccaff030SJeremy L Thompson   CeedScalar cp              = 1004.;    // J/(kg K)
961011314a7SLeila Ghaffari   CeedScalar vortex_strength = 5.;       // -
96235e1ec25SLeila Ghaffari   CeedScalar T_inlet         = 1.;       // K
963cfc558c5SLeila Ghaffari   CeedScalar P_inlet         = 1.e3;     // Pa
964ccaff030SJeremy L Thompson   CeedScalar g               = 9.81;     // m/s^2
965ccaff030SJeremy L Thompson   CeedScalar lambda          = -2./3.;   // -
966ccaff030SJeremy L Thompson   CeedScalar mu              = 75.;      // Pa s, dynamic viscosity
967ccaff030SJeremy L Thompson   // mu = 75 is not physical for air, but is good for numerical stability
968ccaff030SJeremy L Thompson   CeedScalar k               = 0.02638;  // W/(m K)
969ccaff030SJeremy L Thompson   CeedScalar CtauS           = 0.;       // dimensionless
970ccaff030SJeremy L Thompson   CeedScalar strong_form     = 0.;       // [0,1]
971ccaff030SJeremy L Thompson   PetscScalar lx             = 8000.;    // m
972ccaff030SJeremy L Thompson   PetscScalar ly             = 8000.;    // m
973ccaff030SJeremy L Thompson   PetscScalar lz             = 4000.;    // m
974ccaff030SJeremy L Thompson   CeedScalar rc              = 1000.;    // m (Radius of bubble)
975ccaff030SJeremy L Thompson   PetscScalar resx           = 1000.;    // m (resolution in x)
976ccaff030SJeremy L Thompson   PetscScalar resy           = 1000.;    // m (resolution in y)
977ccaff030SJeremy L Thompson   PetscScalar resz           = 1000.;    // m (resolution in z)
978ccaff030SJeremy L Thompson   PetscInt outputfreq        = 10;       // -
979ccaff030SJeremy L Thompson   PetscInt contsteps         = 0;        // -
98084d34d69SLeila Ghaffari   PetscInt degree            = 1;        // -
98184d34d69SLeila Ghaffari   PetscInt qextra            = 2;        // -
982ea6e0f84SLeila Ghaffari   PetscInt qextraSur         = 2;        // -
983e8107fdaSLeila Ghaffari   PetscReal center[3], dc_axis[3] = {0, 0, 0}, wind[3] = {1., 0, 0},
9846bd16babSLeila Ghaffari                                     etv_mean_velocity[3] = {1., 1., 0};
985ccaff030SJeremy L Thompson   ierr = PetscInitialize(&argc, &argv, NULL, help);
986ccaff030SJeremy L Thompson   if (ierr) return ierr;
987ccaff030SJeremy L Thompson 
988ccaff030SJeremy L Thompson   // Allocate PETSc context
989ccaff030SJeremy L Thompson   ierr = PetscCalloc1(1, &user); CHKERRQ(ierr);
990ccaff030SJeremy L Thompson   ierr = PetscMalloc1(1, &units); CHKERRQ(ierr);
9915bf10c6fSLeila Ghaffari   ierr = PetscMalloc1(1, &ctxEulerData); CHKERRQ(ierr);
992ccaff030SJeremy L Thompson 
993ccaff030SJeremy L Thompson   // Parse command line options
994ccaff030SJeremy L Thompson   comm = PETSC_COMM_WORLD;
995ccaff030SJeremy L Thompson   ierr = PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED",
996ccaff030SJeremy L Thompson                            NULL); CHKERRQ(ierr);
997ccaff030SJeremy L Thompson   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
998ccaff030SJeremy L Thompson                             NULL, ceedresource, ceedresource,
999ccaff030SJeremy L Thompson                             sizeof(ceedresource), NULL); CHKERRQ(ierr);
1000dc8efd83SLeila Ghaffari   ierr = PetscOptionsBool("-test", "Run in test mode",
1001dc8efd83SLeila Ghaffari                           NULL, test=PETSC_FALSE, &test, NULL); CHKERRQ(ierr);
1002dc8efd83SLeila Ghaffari   ierr = PetscOptionsScalar("-compare_final_state_atol",
1003dc8efd83SLeila Ghaffari                             "Test absolute tolerance",
1004dc8efd83SLeila Ghaffari                             NULL, testtol, &testtol, NULL); CHKERRQ(ierr);
1005dc8efd83SLeila Ghaffari   ierr = PetscOptionsString("-compare_final_state_filename", "Test filename",
1006dc8efd83SLeila Ghaffari                             NULL, filepath, filepath,
1007dc8efd83SLeila Ghaffari                             sizeof(filepath), NULL); CHKERRQ(ierr);
1008ccaff030SJeremy L Thompson   problemChoice = NS_DENSITY_CURRENT;
1009ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-problem", "Problem to solve", NULL,
1010ccaff030SJeremy L Thompson                           problemTypes, (PetscEnum)problemChoice,
1011ccaff030SJeremy L Thompson                           (PetscEnum *)&problemChoice, NULL); CHKERRQ(ierr);
1012ccaff030SJeremy L Thompson   problem = &problemOptions[problemChoice];
10131184866aSLeila Ghaffari   ierr = PetscOptionsEnum("-problem_advection_wind", "Wind type in Advection",
1014f259b054Svaleriabarra                           NULL, WindTypes,
1015f259b054Svaleriabarra                           (PetscEnum)(wind_type = ADVECTION_WIND_ROTATION),
10161184866aSLeila Ghaffari                           (PetscEnum *)&wind_type, NULL); CHKERRQ(ierr);
10171184866aSLeila Ghaffari   PetscInt n = problem->dim;
101882c09b01SLeila Ghaffari   PetscBool userWind;
1019ebb4b9bdSLeila Ghaffari   ierr = PetscOptionsRealArray("-problem_advection_wind_translation",
1020ebb4b9bdSLeila Ghaffari                                "Constant wind vector",
102182c09b01SLeila Ghaffari                                NULL, wind, &n, &userWind); CHKERRQ(ierr);
102282c09b01SLeila Ghaffari   if (wind_type == ADVECTION_WIND_ROTATION && userWind) {
1023ebb4b9bdSLeila Ghaffari     ierr = PetscPrintf(comm,
1024ebb4b9bdSLeila Ghaffari                        "Warning! Use -problem_advection_wind_translation only with -problem_advection_wind translation\n");
102582c09b01SLeila Ghaffari     CHKERRQ(ierr);
10261184866aSLeila Ghaffari   }
1027b9c3f7b3SLeila Ghaffari   if (wind_type == ADVECTION_WIND_TRANSLATION
1028b9c3f7b3SLeila Ghaffari       && (problemChoice == NS_DENSITY_CURRENT ||
1029b9c3f7b3SLeila Ghaffari           problemChoice == NS_EULER_VORTEX)) {
103067babd9cSLeila Ghaffari     SETERRQ(comm, PETSC_ERR_ARG_INCOMP,
1031b9c3f7b3SLeila Ghaffari             "-problem_advection_wind translation is not defined for -problem density_current or -problem euler_vortex");
103267babd9cSLeila Ghaffari   }
10330975b6e1SLeila Ghaffari   n = problem->dim;
1034e8107fdaSLeila Ghaffari   ierr = PetscOptionsRealArray("-problem_euler_mean_velocity",
1035e8107fdaSLeila Ghaffari                                "Mean velocity vector",
1036e8107fdaSLeila Ghaffari                                NULL, etv_mean_velocity, &n, NULL);
1037e8107fdaSLeila Ghaffari   CHKERRQ(ierr);
1038ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
1039ccaff030SJeremy L Thompson                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
1040ccaff030SJeremy L Thompson                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
1041a3ddc43aSLeila Ghaffari   ierr = PetscOptionsEnum("-euler_test", "Euler test option", NULL,
1042a3ddc43aSLeila Ghaffari                           EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_NONE),
1043a3ddc43aSLeila Ghaffari                           (PetscEnum *)&euler_test, NULL); CHKERRQ(ierr);
1044ccaff030SJeremy L Thompson   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
1045ccaff030SJeremy L Thompson                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
1046ccaff030SJeremy L Thompson   CHKERRQ(ierr);
104784d34d69SLeila Ghaffari   if (!implicit && stab != STAB_NONE) {
104884d34d69SLeila Ghaffari     ierr = PetscPrintf(comm, "Warning! Use -stab only with -implicit\n");
104984d34d69SLeila Ghaffari     CHKERRQ(ierr);
105084d34d69SLeila Ghaffari   }
1051ccaff030SJeremy L Thompson   {
10527573aee6SJed Brown     PetscInt len;
10537573aee6SJed Brown     PetscBool flg;
1054ccaff030SJeremy L Thompson     ierr = PetscOptionsIntArray("-bc_wall",
1055ccaff030SJeremy L Thompson                                 "Use wall boundary conditions on this list of faces",
1056ccaff030SJeremy L Thompson                                 NULL, bc.walls,
1057ccaff030SJeremy L Thompson                                 (len = sizeof(bc.walls) / sizeof(bc.walls[0]),
1058ccaff030SJeremy L Thompson                                  &len), &flg); CHKERRQ(ierr);
10597573aee6SJed Brown     if (flg) {
10607573aee6SJed Brown       bc.nwall = len;
10617573aee6SJed Brown       // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
10627573aee6SJed Brown       bc.nslip[0] = bc.nslip[1] = bc.nslip[2] = 0;
10637573aee6SJed Brown     }
1064ccaff030SJeremy L Thompson     for (PetscInt j=0; j<3; j++) {
1065ccaff030SJeremy L Thompson       const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
1066ccaff030SJeremy L Thompson       ierr = PetscOptionsIntArray(flags[j],
1067ccaff030SJeremy L Thompson                                   "Use slip boundary conditions on this list of faces",
1068ccaff030SJeremy L Thompson                                   NULL, bc.slips[j],
1069ccaff030SJeremy L Thompson                                   (len = sizeof(bc.slips[j]) / sizeof(bc.slips[j][0]),
1070ccaff030SJeremy L Thompson                                    &len), &flg);
1071ccaff030SJeremy L Thompson       CHKERRQ(ierr);
107284d34d69SLeila Ghaffari       if (flg) {
107384d34d69SLeila Ghaffari         bc.nslip[j] = len;
107484d34d69SLeila Ghaffari         bc.userbc = PETSC_TRUE;
107584d34d69SLeila Ghaffari       }
1076ccaff030SJeremy L Thompson     }
1077ccaff030SJeremy L Thompson   }
1078cb3e2689Svaleriabarra   ierr = PetscOptionsInt("-viz_refine",
1079cb3e2689Svaleriabarra                          "Regular refinement levels for visualization",
1080cb3e2689Svaleriabarra                          NULL, viz_refine, &viz_refine, NULL);
1081ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1082ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
1083ccaff030SJeremy L Thompson                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
1084ccaff030SJeremy L Thompson   meter = fabs(meter);
1085ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
1086ccaff030SJeremy L Thompson                             NULL, second, &second, NULL); CHKERRQ(ierr);
1087ccaff030SJeremy L Thompson   second = fabs(second);
1088ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
1089ccaff030SJeremy L Thompson                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
1090ccaff030SJeremy L Thompson   kilogram = fabs(kilogram);
1091ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_Kelvin",
1092ccaff030SJeremy L Thompson                             "1 Kelvin in scaled temperature units",
1093ccaff030SJeremy L Thompson                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
1094ccaff030SJeremy L Thompson   Kelvin = fabs(Kelvin);
1095ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
1096ccaff030SJeremy L Thompson                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
1097ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
1098ccaff030SJeremy L Thompson                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
1099ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
1100ccaff030SJeremy L Thompson                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
110116c0476cSLeila Ghaffari   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
110216c0476cSLeila Ghaffari                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
1103ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
1104ccaff030SJeremy L Thompson                             NULL, N, &N, NULL); CHKERRQ(ierr);
1105ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
1106ccaff030SJeremy L Thompson                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
1107ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
1108ccaff030SJeremy L Thompson                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
1109011314a7SLeila Ghaffari   PetscBool userVortex;
1110011314a7SLeila Ghaffari   ierr = PetscOptionsScalar("-vortex_strength", "Strength of Vortex",
1111011314a7SLeila Ghaffari                             NULL, vortex_strength, &vortex_strength, &userVortex);
1112011314a7SLeila Ghaffari   CHKERRQ(ierr);
1113011314a7SLeila Ghaffari   if (problemChoice != NS_EULER_VORTEX && userVortex) {
1114011314a7SLeila Ghaffari     ierr = PetscPrintf(comm,
1115011314a7SLeila Ghaffari                        "Warning! Use -vortex_strength only with -problem euler_vortex\n");
1116011314a7SLeila Ghaffari     CHKERRQ(ierr);
1117011314a7SLeila Ghaffari   }
1118c8cb9b1fSLeila Ghaffari   PetscBool userTinlet;
1119c8cb9b1fSLeila Ghaffari   ierr = PetscOptionsScalar("-T_inlet", "Incoming Temperature",
1120c8cb9b1fSLeila Ghaffari                             NULL, T_inlet, &T_inlet, &userTinlet);
1121c8cb9b1fSLeila Ghaffari   CHKERRQ(ierr);
1122c8cb9b1fSLeila Ghaffari   if (problemChoice != NS_EULER_VORTEX && userTinlet) {
1123c8cb9b1fSLeila Ghaffari     ierr = PetscPrintf(comm,
1124c8cb9b1fSLeila Ghaffari                        "Warning! Use -T_inlet only with -problem euler_vortex\n");
1125c8cb9b1fSLeila Ghaffari     CHKERRQ(ierr);
1126c8cb9b1fSLeila Ghaffari   }
1127c8cb9b1fSLeila Ghaffari   PetscBool userPinlet;
1128c8cb9b1fSLeila Ghaffari   ierr = PetscOptionsScalar("-P_inlet", "Incoming Pressure",
1129c8cb9b1fSLeila Ghaffari                             NULL, P_inlet, &P_inlet, &userPinlet);
1130c8cb9b1fSLeila Ghaffari   CHKERRQ(ierr);
1131c8cb9b1fSLeila Ghaffari   if (problemChoice != NS_EULER_VORTEX && userPinlet) {
1132c8cb9b1fSLeila Ghaffari     ierr = PetscPrintf(comm,
1133c8cb9b1fSLeila Ghaffari                        "Warning! Use -P_inlet only with -problem euler_vortex\n");
1134c8cb9b1fSLeila Ghaffari     CHKERRQ(ierr);
1135c8cb9b1fSLeila Ghaffari   }
1136ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
1137ccaff030SJeremy L Thompson                             NULL, g, &g, NULL); CHKERRQ(ierr);
1138ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lambda",
1139ccaff030SJeremy L Thompson                             "Stokes hypothesis second viscosity coefficient",
1140ccaff030SJeremy L Thompson                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
1141ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
1142ccaff030SJeremy L Thompson                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
1143ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
1144ccaff030SJeremy L Thompson                             NULL, k, &k, NULL); CHKERRQ(ierr);
1145ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-CtauS",
1146ccaff030SJeremy L Thompson                             "Scale coefficient for tau (nondimensional)",
1147ccaff030SJeremy L Thompson                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
114884d34d69SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
114984d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
115084d34d69SLeila Ghaffari                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
115184d34d69SLeila Ghaffari     CHKERRQ(ierr);
115284d34d69SLeila Ghaffari   }
1153ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-strong_form",
1154ccaff030SJeremy L Thompson                             "Strong (1) or weak/integrated by parts (0) advection residual",
1155ccaff030SJeremy L Thompson                             NULL, strong_form, &strong_form, NULL);
1156ccaff030SJeremy L Thompson   CHKERRQ(ierr);
115784d34d69SLeila Ghaffari   if (problemChoice == NS_DENSITY_CURRENT && (CtauS != 0 || strong_form != 0)) {
115884d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
115984d34d69SLeila Ghaffari                        "Warning! Problem density_current does not support -CtauS or -strong_form\n");
116084d34d69SLeila Ghaffari     CHKERRQ(ierr);
116184d34d69SLeila Ghaffari   }
1162ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
1163ccaff030SJeremy L Thompson                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
1164ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
1165ccaff030SJeremy L Thompson                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
1166ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
1167ccaff030SJeremy L Thompson                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
1168ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
1169ccaff030SJeremy L Thompson                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
1170ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resx","Target resolution in x",
1171ccaff030SJeremy L Thompson                             NULL, resx, &resx, NULL); CHKERRQ(ierr);
1172ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resy","Target resolution in y",
1173ccaff030SJeremy L Thompson                             NULL, resy, &resy, NULL); CHKERRQ(ierr);
1174ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resz","Target resolution in z",
1175ccaff030SJeremy L Thompson                             NULL, resz, &resz, NULL); CHKERRQ(ierr);
117682c09b01SLeila Ghaffari   n = problem->dim;
1177ccaff030SJeremy L Thompson   center[0] = 0.5 * lx;
1178ccaff030SJeremy L Thompson   center[1] = 0.5 * ly;
1179ccaff030SJeremy L Thompson   center[2] = 0.5 * lz;
1180ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
1181ccaff030SJeremy L Thompson                                NULL, center, &n, NULL); CHKERRQ(ierr);
1182ccaff030SJeremy L Thompson   n = problem->dim;
1183ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-dc_axis",
1184ccaff030SJeremy L Thompson                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
1185ccaff030SJeremy L Thompson                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
1186ccaff030SJeremy L Thompson   {
1187ccaff030SJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) +
1188ccaff030SJeremy L Thompson                                    PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
1189ccaff030SJeremy L Thompson     if (norm > 0) {
1190ccaff030SJeremy L Thompson       for (int i=0; i<3; i++) dc_axis[i] /= norm;
1191ccaff030SJeremy L Thompson     }
1192ccaff030SJeremy L Thompson   }
1193ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-output_freq",
1194ccaff030SJeremy L Thompson                          "Frequency of output, in number of steps",
1195ccaff030SJeremy L Thompson                          NULL, outputfreq, &outputfreq, NULL); CHKERRQ(ierr);
1196ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-continue", "Continue from previous solution",
1197ccaff030SJeremy L Thompson                          NULL, contsteps, &contsteps, NULL); CHKERRQ(ierr);
119884d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
119984d34d69SLeila Ghaffari                          NULL, degree, &degree, NULL); CHKERRQ(ierr);
120084d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-qextra", "Number of extra quadrature points",
120184d34d69SLeila Ghaffari                          NULL, qextra, &qextra, NULL); CHKERRQ(ierr);
120281f92cf0SLeila Ghaffari   PetscBool userQextraSur;
1203ebb4b9bdSLeila Ghaffari   ierr = PetscOptionsInt("-qextra_boundary",
1204ebb4b9bdSLeila Ghaffari                          "Number of extra quadrature points on in/outflow faces",
1205f259b054Svaleriabarra                          NULL, qextraSur, &qextraSur, &userQextraSur);
1206f259b054Svaleriabarra   CHKERRQ(ierr);
1207ebb4b9bdSLeila Ghaffari   if ((wind_type == ADVECTION_WIND_ROTATION
1208ebb4b9bdSLeila Ghaffari        || problemChoice == NS_DENSITY_CURRENT) && userQextraSur) {
1209ebb4b9bdSLeila Ghaffari     ierr = PetscPrintf(comm,
1210ebb4b9bdSLeila Ghaffari                        "Warning! Use -qextra_boundary only with -problem_advection_wind translation\n");
121181f92cf0SLeila Ghaffari     CHKERRQ(ierr);
121281f92cf0SLeila Ghaffari   }
1213d99129b9SLeila Ghaffari   ierr = PetscStrncpy(user->outputdir, ".", 2); CHKERRQ(ierr);
1214d99129b9SLeila Ghaffari   ierr = PetscOptionsString("-output_dir", "Output directory",
1215d99129b9SLeila Ghaffari                             NULL, user->outputdir, user->outputdir,
1216d99129b9SLeila Ghaffari                             sizeof(user->outputdir), NULL); CHKERRQ(ierr);
121784d34d69SLeila Ghaffari   memtyperequested = petschavecuda ? CEED_MEM_DEVICE : CEED_MEM_HOST;
121884d34d69SLeila Ghaffari   ierr = PetscOptionsEnum("-memtype",
121984d34d69SLeila Ghaffari                           "CEED MemType requested", NULL,
122084d34d69SLeila Ghaffari                           memTypes, (PetscEnum)memtyperequested,
122184d34d69SLeila Ghaffari                           (PetscEnum *)&memtyperequested, &setmemtyperequest);
122284d34d69SLeila Ghaffari   CHKERRQ(ierr);
1223ccaff030SJeremy L Thompson   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
1224ccaff030SJeremy L Thompson 
1225ccaff030SJeremy L Thompson   // Define derived units
1226ccaff030SJeremy L Thompson   Pascal = kilogram / (meter * PetscSqr(second));
1227ccaff030SJeremy L Thompson   JperkgK =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
1228ccaff030SJeremy L Thompson   mpersquareds = meter / PetscSqr(second);
1229ccaff030SJeremy L Thompson   WpermK = kilogram * meter / (pow(second,3) * Kelvin);
1230ccaff030SJeremy L Thompson   kgpercubicm = kilogram / pow(meter,3);
1231ccaff030SJeremy L Thompson   kgpersquaredms = kilogram / (PetscSqr(meter) * second);
1232ccaff030SJeremy L Thompson   Joulepercubicm = kilogram / (meter * PetscSqr(second));
123316c0476cSLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
1234ccaff030SJeremy L Thompson 
1235ccaff030SJeremy L Thompson   // Scale variables to desired units
1236ccaff030SJeremy L Thompson   theta0 *= Kelvin;
1237ccaff030SJeremy L Thompson   thetaC *= Kelvin;
1238ccaff030SJeremy L Thompson   P0 *= Pascal;
123916c0476cSLeila Ghaffari   E_wind *= Joule;
1240ccaff030SJeremy L Thompson   N *= (1./second);
1241ccaff030SJeremy L Thompson   cv *= JperkgK;
1242ccaff030SJeremy L Thompson   cp *= JperkgK;
1243ccaff030SJeremy L Thompson   Rd = cp - cv;
1244c8cb9b1fSLeila Ghaffari   T_inlet *= Kelvin;
1245c8cb9b1fSLeila Ghaffari   P_inlet *= Pascal;
1246ccaff030SJeremy L Thompson   g *= mpersquareds;
1247ccaff030SJeremy L Thompson   mu *= Pascal * second;
1248ccaff030SJeremy L Thompson   k *= WpermK;
1249ccaff030SJeremy L Thompson   lx = fabs(lx) * meter;
1250ccaff030SJeremy L Thompson   ly = fabs(ly) * meter;
1251ccaff030SJeremy L Thompson   lz = fabs(lz) * meter;
1252ccaff030SJeremy L Thompson   rc = fabs(rc) * meter;
1253ccaff030SJeremy L Thompson   resx = fabs(resx) * meter;
1254ccaff030SJeremy L Thompson   resy = fabs(resy) * meter;
1255ccaff030SJeremy L Thompson   resz = fabs(resz) * meter;
1256ccaff030SJeremy L Thompson   for (int i=0; i<3; i++) center[i] *= meter;
1257ccaff030SJeremy L Thompson 
1258ccaff030SJeremy L Thompson   const CeedInt dim = problem->dim, ncompx = problem->dim,
1259cfa64770SLeila Ghaffari                 qdatasizeVol = problem->qdatasizeVol;
1260ccaff030SJeremy L Thompson   // Set up the libCEED context
1261777ff853SJeremy L Thompson   struct SetupContext_ ctxSetupData = {
1262ccaff030SJeremy L Thompson     .theta0 = theta0,
1263ccaff030SJeremy L Thompson     .thetaC = thetaC,
1264ccaff030SJeremy L Thompson     .P0 = P0,
1265ccaff030SJeremy L Thompson     .N = N,
1266ccaff030SJeremy L Thompson     .cv = cv,
1267ccaff030SJeremy L Thompson     .cp = cp,
1268ccaff030SJeremy L Thompson     .Rd = Rd,
1269ccaff030SJeremy L Thompson     .g = g,
1270ccaff030SJeremy L Thompson     .rc = rc,
1271ccaff030SJeremy L Thompson     .lx = lx,
1272ccaff030SJeremy L Thompson     .ly = ly,
1273ccaff030SJeremy L Thompson     .lz = lz,
1274ccaff030SJeremy L Thompson     .center[0] = center[0],
1275ccaff030SJeremy L Thompson     .center[1] = center[1],
1276ccaff030SJeremy L Thompson     .center[2] = center[2],
1277ccaff030SJeremy L Thompson     .dc_axis[0] = dc_axis[0],
1278ccaff030SJeremy L Thompson     .dc_axis[1] = dc_axis[1],
1279ccaff030SJeremy L Thompson     .dc_axis[2] = dc_axis[2],
12801184866aSLeila Ghaffari     .wind[0] = wind[0],
12811184866aSLeila Ghaffari     .wind[1] = wind[1],
12821184866aSLeila Ghaffari     .wind[2] = wind[2],
12833a9f53b0SLeila Ghaffari     .time = 0,
1284011314a7SLeila Ghaffari     .vortex_strength = vortex_strength,
12851184866aSLeila Ghaffari     .wind_type = wind_type,
1286ccaff030SJeremy L Thompson   };
1287ccaff030SJeremy L Thompson 
128884d34d69SLeila Ghaffari   // Create the mesh
1289ccaff030SJeremy L Thompson   {
1290ccaff030SJeremy L Thompson     const PetscReal scale[3] = {lx, ly, lz};
1291ccaff030SJeremy L Thompson     ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_FALSE, NULL, NULL, scale,
12922561194eSLeila Ghaffari                                NULL, PETSC_TRUE, &dm);
1293ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1294ccaff030SJeremy L Thompson   }
129584d34d69SLeila Ghaffari 
129684d34d69SLeila Ghaffari   // Distribute the mesh over processes
129784d34d69SLeila Ghaffari   {
1298ccaff030SJeremy L Thompson     DM               dmDist = NULL;
1299ccaff030SJeremy L Thompson     PetscPartitioner part;
1300ccaff030SJeremy L Thompson 
1301ccaff030SJeremy L Thompson     ierr = DMPlexGetPartitioner(dm, &part); CHKERRQ(ierr);
1302ccaff030SJeremy L Thompson     ierr = PetscPartitionerSetFromOptions(part); CHKERRQ(ierr);
1303ccaff030SJeremy L Thompson     ierr = DMPlexDistribute(dm, 0, NULL, &dmDist); CHKERRQ(ierr);
1304ccaff030SJeremy L Thompson     if (dmDist) {
1305ccaff030SJeremy L Thompson       ierr = DMDestroy(&dm); CHKERRQ(ierr);
1306ccaff030SJeremy L Thompson       dm  = dmDist;
1307ccaff030SJeremy L Thompson     }
1308ccaff030SJeremy L Thompson   }
1309ccaff030SJeremy L Thompson   ierr = DMViewFromOptions(dm, NULL, "-dm_view"); CHKERRQ(ierr);
1310ccaff030SJeremy L Thompson 
131184d34d69SLeila Ghaffari   // Setup DM
1312ccaff030SJeremy L Thompson   ierr = DMLocalizeCoordinates(dm); CHKERRQ(ierr);
1313ccaff030SJeremy L Thompson   ierr = DMSetFromOptions(dm); CHKERRQ(ierr);
13145bf10c6fSLeila Ghaffari   ierr = SetUpDM(dm, problem, degree, &bc, &ctxSetupData, ctxEulerData);
13155bf10c6fSLeila Ghaffari   CHKERRQ(ierr);
131684d34d69SLeila Ghaffari 
131784d34d69SLeila Ghaffari   // Refine DM for high-order viz
1318ccaff030SJeremy L Thompson   dmviz = NULL;
1319ccaff030SJeremy L Thompson   interpviz = NULL;
1320ccaff030SJeremy L Thompson   if (viz_refine) {
1321ff6701fcSJed Brown     DM dmhierarchy[viz_refine+1];
1322ff6701fcSJed Brown 
1323ccaff030SJeremy L Thompson     ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
1324ff6701fcSJed Brown     dmhierarchy[0] = dm;
132584d34d69SLeila Ghaffari     for (PetscInt i = 0, d = degree; i < viz_refine; i++) {
1326ff6701fcSJed Brown       Mat interp_next;
1327ff6701fcSJed Brown 
1328ff6701fcSJed Brown       ierr = DMRefine(dmhierarchy[i], MPI_COMM_NULL, &dmhierarchy[i+1]);
1329ccaff030SJeremy L Thompson       CHKERRQ(ierr);
1330bc6a41f7SJed Brown       ierr = DMClearDS(dmhierarchy[i+1]); CHKERRQ(ierr);
1331bc6a41f7SJed Brown       ierr = DMClearFields(dmhierarchy[i+1]); CHKERRQ(ierr);
1332ff6701fcSJed Brown       ierr = DMSetCoarseDM(dmhierarchy[i+1], dmhierarchy[i]); CHKERRQ(ierr);
1333ff6701fcSJed Brown       d = (d + 1) / 2;
1334ff6701fcSJed Brown       if (i + 1 == viz_refine) d = 1;
13355bf10c6fSLeila Ghaffari       ierr = SetUpDM(dmhierarchy[i+1], problem, d, &bc, &ctxSetupData,
13365bf10c6fSLeila Ghaffari                      ctxEulerData); CHKERRQ(ierr);
1337ff6701fcSJed Brown       ierr = DMCreateInterpolation(dmhierarchy[i], dmhierarchy[i+1],
1338ff6701fcSJed Brown                                    &interp_next, NULL); CHKERRQ(ierr);
1339ff6701fcSJed Brown       if (!i) interpviz = interp_next;
1340ff6701fcSJed Brown       else {
1341ff6701fcSJed Brown         Mat C;
1342ff6701fcSJed Brown         ierr = MatMatMult(interp_next, interpviz, MAT_INITIAL_MATRIX,
1343ff6701fcSJed Brown                           PETSC_DECIDE, &C); CHKERRQ(ierr);
1344ff6701fcSJed Brown         ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
1345ff6701fcSJed Brown         ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1346ff6701fcSJed Brown         interpviz = C;
1347ff6701fcSJed Brown       }
1348ff6701fcSJed Brown     }
1349cb3e2689Svaleriabarra     for (PetscInt i=1; i<viz_refine; i++) {
1350ff6701fcSJed Brown       ierr = DMDestroy(&dmhierarchy[i]); CHKERRQ(ierr);
1351cb3e2689Svaleriabarra     }
1352ff6701fcSJed Brown     dmviz = dmhierarchy[viz_refine];
1353ccaff030SJeremy L Thompson   }
1354ccaff030SJeremy L Thompson   ierr = DMCreateGlobalVector(dm, &Q); CHKERRQ(ierr);
1355ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Qloc); CHKERRQ(ierr);
1356ccaff030SJeremy L Thompson   ierr = VecGetSize(Qloc, &lnodes); CHKERRQ(ierr);
1357ccaff030SJeremy L Thompson   lnodes /= ncompq;
1358ccaff030SJeremy L Thompson 
135984d34d69SLeila Ghaffari   // Initialize CEED
136084d34d69SLeila Ghaffari   CeedInit(ceedresource, &ceed);
136184d34d69SLeila Ghaffari   // Set memtype
136284d34d69SLeila Ghaffari   CeedMemType memtypebackend;
136384d34d69SLeila Ghaffari   CeedGetPreferredMemType(ceed, &memtypebackend);
136484d34d69SLeila Ghaffari   // Check memtype compatibility
136584d34d69SLeila Ghaffari   if (!setmemtyperequest)
136684d34d69SLeila Ghaffari     memtyperequested = memtypebackend;
136784d34d69SLeila Ghaffari   else if (!petschavecuda && memtyperequested == CEED_MEM_DEVICE)
136884d34d69SLeila Ghaffari     SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS,
136984d34d69SLeila Ghaffari              "PETSc was not built with CUDA. "
137084d34d69SLeila Ghaffari              "Requested MemType CEED_MEM_DEVICE is not supported.", NULL);
137184d34d69SLeila Ghaffari 
137284d34d69SLeila Ghaffari   // Set number of 1D nodes and quadrature points
137384d34d69SLeila Ghaffari   numP = degree + 1;
137484d34d69SLeila Ghaffari   numQ = numP + qextra;
137584d34d69SLeila Ghaffari 
137684d34d69SLeila Ghaffari   // Print summary
1377dc8efd83SLeila Ghaffari   if (!test) {
1378ccaff030SJeremy L Thompson     CeedInt gdofs, odofs;
1379ccaff030SJeremy L Thompson     int comm_size;
1380ccaff030SJeremy L Thompson     char box_faces_str[PETSC_MAX_PATH_LEN] = "NONE";
1381ccaff030SJeremy L Thompson     ierr = VecGetSize(Q, &gdofs); CHKERRQ(ierr);
1382ccaff030SJeremy L Thompson     ierr = VecGetLocalSize(Q, &odofs); CHKERRQ(ierr);
138384d34d69SLeila Ghaffari     gnodes = gdofs/ncompq;
1384ccaff030SJeremy L Thompson     ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr);
1385ccaff030SJeremy L Thompson     ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str,
1386ccaff030SJeremy L Thompson                                  sizeof(box_faces_str), NULL); CHKERRQ(ierr);
138784d34d69SLeila Ghaffari     const char *usedresource;
138884d34d69SLeila Ghaffari     CeedGetResource(ceed, &usedresource);
1389ccaff030SJeremy L Thompson 
139084d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
139184d34d69SLeila Ghaffari                        "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
139284d34d69SLeila Ghaffari                        "  rank(s)                              : %d\n"
139384d34d69SLeila Ghaffari                        "  Problem:\n"
139484d34d69SLeila Ghaffari                        "    Problem Name                       : %s\n"
139584d34d69SLeila Ghaffari                        "    Stabilization                      : %s\n"
139684d34d69SLeila Ghaffari                        "  PETSc:\n"
139784d34d69SLeila Ghaffari                        "    Box Faces                          : %s\n"
139884d34d69SLeila Ghaffari                        "  libCEED:\n"
139984d34d69SLeila Ghaffari                        "    libCEED Backend                    : %s\n"
140084d34d69SLeila Ghaffari                        "    libCEED Backend MemType            : %s\n"
140184d34d69SLeila Ghaffari                        "    libCEED User Requested MemType     : %s\n"
140284d34d69SLeila Ghaffari                        "  Mesh:\n"
140384d34d69SLeila Ghaffari                        "    Number of 1D Basis Nodes (P)       : %d\n"
140484d34d69SLeila Ghaffari                        "    Number of 1D Quadrature Points (Q) : %d\n"
140584d34d69SLeila Ghaffari                        "    Global DoFs                        : %D\n"
140684d34d69SLeila Ghaffari                        "    Owned DoFs                         : %D\n"
140784d34d69SLeila Ghaffari                        "    DoFs per node                      : %D\n"
140884d34d69SLeila Ghaffari                        "    Global nodes                       : %D\n"
140984d34d69SLeila Ghaffari                        "    Owned nodes                        : %D\n",
141084d34d69SLeila Ghaffari                        comm_size, problemTypes[problemChoice],
141184d34d69SLeila Ghaffari                        StabilizationTypes[stab], box_faces_str, usedresource,
141284d34d69SLeila Ghaffari                        CeedMemTypes[memtypebackend],
141384d34d69SLeila Ghaffari                        (setmemtyperequest) ?
141484d34d69SLeila Ghaffari                        CeedMemTypes[memtyperequested] : "none",
141584d34d69SLeila Ghaffari                        numP, numQ, gdofs, odofs, ncompq, gnodes, lnodes);
141684d34d69SLeila Ghaffari     CHKERRQ(ierr);
14170c6c0b13SLeila Ghaffari   }
14180c6c0b13SLeila Ghaffari 
1419ccaff030SJeremy L Thompson   // Set up global mass vector
1420ccaff030SJeremy L Thompson   ierr = VecDuplicate(Q, &user->M); CHKERRQ(ierr);
1421ccaff030SJeremy L Thompson 
142284d34d69SLeila Ghaffari   // Set up libCEED
1423ccaff030SJeremy L Thompson   // CEED Bases
1424ccaff030SJeremy L Thompson   CeedInit(ceedresource, &ceed);
142584d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompq, numP, numQ, CEED_GAUSS,
142684d34d69SLeila Ghaffari                                   &basisq);
142784d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numQ, CEED_GAUSS,
142884d34d69SLeila Ghaffari                                   &basisx);
142984d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numP,
143084d34d69SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxc);
1431ccaff030SJeremy L Thompson   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
1432ccaff030SJeremy L Thompson   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
1433ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1434ccaff030SJeremy L Thompson 
1435ccaff030SJeremy L Thompson   // CEED Restrictions
14361e150236SLeila Ghaffari   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, numP, numQ,
143784d34d69SLeila Ghaffari                                  qdatasizeVol, &restrictq, &restrictx,
143884d34d69SLeila Ghaffari                                  &restrictqdi); CHKERRQ(ierr);
1439ccaff030SJeremy L Thompson 
1440ccaff030SJeremy L Thompson   ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
1441ccaff030SJeremy L Thompson   ierr = CreateVectorFromPetscVec(ceed, Xloc, &xcorners); CHKERRQ(ierr);
1442ccaff030SJeremy L Thompson 
1443ccaff030SJeremy L Thompson   // Create the CEED vectors that will be needed in setup
1444bd910870SLeila Ghaffari   CeedInt NqptsVol;
144584d34d69SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisq, &NqptsVol);
144684d34d69SLeila Ghaffari   CeedElemRestrictionGetNumElements(restrictq, &localNelemVol);
14478b982baeSLeila Ghaffari   CeedVectorCreate(ceed, qdatasizeVol*localNelemVol*NqptsVol, &qdata);
144884d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &q0ceed, NULL);
1449ccaff030SJeremy L Thompson 
1450ccaff030SJeremy L Thompson   // Create the Q-function that builds the quadrature data for the NS operator
1451ea6e0f84SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupVol, problem->setupVol_loc,
1452ea6e0f84SLeila Ghaffari                               &qf_setupVol);
1453ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "dx", ncompx*dim, CEED_EVAL_GRAD);
1454ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "weight", 1, CEED_EVAL_WEIGHT);
14558b982baeSLeila Ghaffari   CeedQFunctionAddOutput(qf_setupVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1456ccaff030SJeremy L Thompson 
1457ccaff030SJeremy L Thompson   // Create the Q-function that sets the ICs of the operator
1458ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, problem->ics, problem->ics_loc, &qf_ics);
1459ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_ics, "x", ncompx, CEED_EVAL_INTERP);
1460ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_ics, "q0", ncompq, CEED_EVAL_NONE);
1461ccaff030SJeremy L Thompson 
1462ea6e0f84SLeila Ghaffari   qf_rhsVol = NULL;
1463ea6e0f84SLeila Ghaffari   if (problem->applyVol_rhs) { // Create the Q-function that defines the action of the RHS operator
1464ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_rhs,
1465ea6e0f84SLeila Ghaffari                                 problem->applyVol_rhs_loc, &qf_rhsVol);
1466ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "q", ncompq, CEED_EVAL_INTERP);
1467ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
14688b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1469ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "x", ncompx, CEED_EVAL_INTERP);
1470ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "v", ncompq, CEED_EVAL_INTERP);
1471ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1472ccaff030SJeremy L Thompson   }
1473ccaff030SJeremy L Thompson 
1474ea6e0f84SLeila Ghaffari   qf_ifunctionVol = NULL;
1475ea6e0f84SLeila Ghaffari   if (problem->applyVol_ifunction) { // Create the Q-function that defines the action of the IFunction
1476ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_ifunction,
1477ea6e0f84SLeila Ghaffari                                 problem->applyVol_ifunction_loc, &qf_ifunctionVol);
1478ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "q", ncompq, CEED_EVAL_INTERP);
1479ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
1480ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdot", ncompq, CEED_EVAL_INTERP);
14818b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1482ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "x", ncompx, CEED_EVAL_INTERP);
1483ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "v", ncompq, CEED_EVAL_INTERP);
1484ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1485ccaff030SJeremy L Thompson   }
1486ccaff030SJeremy L Thompson 
1487ccaff030SJeremy L Thompson   // Create the operator that builds the quadrature data for the NS operator
1488ea6e0f84SLeila Ghaffari   CeedOperatorCreate(ceed, qf_setupVol, NULL, NULL, &op_setupVol);
148984d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "dx", restrictx, basisx, CEED_VECTOR_ACTIVE);
1490ea6e0f84SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "weight", CEED_ELEMRESTRICTION_NONE,
149184d34d69SLeila Ghaffari                        basisx, CEED_VECTOR_NONE);
149284d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "qdata", restrictqdi,
1493ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1494ccaff030SJeremy L Thompson 
1495ccaff030SJeremy L Thompson   // Create the operator that sets the ICs
1496ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics);
149784d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "x", restrictx, basisxc, CEED_VECTOR_ACTIVE);
149884d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "q0", restrictq,
1499ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1500ccaff030SJeremy L Thompson 
150184d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qceed, NULL);
150284d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qdotceed, NULL);
150384d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->gceed, NULL);
1504ccaff030SJeremy L Thompson 
1505ea6e0f84SLeila Ghaffari   if (qf_rhsVol) { // Create the RHS physics operator
1506ccaff030SJeremy L Thompson     CeedOperator op;
1507ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_rhsVol, NULL, NULL, &op);
150884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
150984d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
151084d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
15118b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
151284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
151384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
151484d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1515d3630711SJed Brown     user->op_rhs_vol = op;
1516ccaff030SJeremy L Thompson   }
1517ccaff030SJeremy L Thompson 
1518ea6e0f84SLeila Ghaffari   if (qf_ifunctionVol) { // Create the IFunction operator
1519ccaff030SJeremy L Thompson     CeedOperator op;
1520ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_ifunctionVol, NULL, NULL, &op);
152184d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
152284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
152384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdot", restrictq, basisq, user->qdotceed);
152484d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
15258b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
152684d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
152784d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
152884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1529d3630711SJed Brown     user->op_ifunction_vol = op;
1530ccaff030SJeremy L Thompson   }
1531ccaff030SJeremy L Thompson 
15326a0edaf9SLeila Ghaffari   // Set up CEED for the boundaries
15336a0edaf9SLeila Ghaffari   CeedInt height = 1;
15346a0edaf9SLeila Ghaffari   CeedInt dimSur = dim - height;
15351e150236SLeila Ghaffari   CeedInt numP_Sur = degree + 1;
15361e150236SLeila Ghaffari   CeedInt numQ_Sur = numP_Sur + qextraSur;
1537cfa64770SLeila Ghaffari   const CeedInt qdatasizeSur = problem->qdatasizeSur;
1538cfa64770SLeila Ghaffari   CeedBasis basisxSur, basisxcSur, basisqSur;
1539cfa64770SLeila Ghaffari   CeedInt NqptsSur;
1540e2043960SLeila Ghaffari   CeedQFunction qf_setupSur, qf_applySur;
1541cfa64770SLeila Ghaffari 
1542cfa64770SLeila Ghaffari   // CEED bases for the boundaries
1543ebb4b9bdSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompq, numP_Sur, numQ_Sur,
1544ebb4b9bdSLeila Ghaffari                                   CEED_GAUSS,
15456a0edaf9SLeila Ghaffari                                   &basisqSur);
15466a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numQ_Sur, CEED_GAUSS,
15476a0edaf9SLeila Ghaffari                                   &basisxSur);
15486a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numP_Sur,
15496a0edaf9SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxcSur);
15506a0edaf9SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisqSur, &NqptsSur);
15516a0edaf9SLeila Ghaffari 
1552cfa64770SLeila Ghaffari   // Create the Q-function that builds the quadrature data for the Surface operator
15536a0edaf9SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupSur, problem->setupSur_loc,
15546a0edaf9SLeila Ghaffari                               &qf_setupSur);
15556a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "dx", ncompx*dimSur, CEED_EVAL_GRAD);
15566a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "weight", 1, CEED_EVAL_WEIGHT);
15576a0edaf9SLeila Ghaffari   CeedQFunctionAddOutput(qf_setupSur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
15586a0edaf9SLeila Ghaffari 
15597659d40cSLeila Ghaffari   // Creat Q-Function for Boundaries
156086ba6f09SLeila Ghaffari   // -- Defined for Advection(2d) test cases
15617ed8b9c7SLeila Ghaffari   qf_applySur = NULL;
15627659d40cSLeila Ghaffari   if (problem->applySur) {
15637659d40cSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applySur,
15647ed8b9c7SLeila Ghaffari                                 problem->applySur_loc, &qf_applySur);
15657ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "q", ncompq, CEED_EVAL_INTERP);
15667ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
15677ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "x", ncompx, CEED_EVAL_INTERP);
15687ed8b9c7SLeila Ghaffari     CeedQFunctionAddOutput(qf_applySur, "v", ncompq, CEED_EVAL_INTERP);
15699fe13df9SLeila Ghaffari   }
15709fe13df9SLeila Ghaffari 
15719fe13df9SLeila Ghaffari   // Create CEED Operator for the whole domain
15729fe13df9SLeila Ghaffari   if (!implicit)
15734438636fSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, &bc, problemChoice, wind_type,
15744438636fSLeila Ghaffari                                    user->op_rhs_vol, qf_applySur,
1575e2043960SLeila Ghaffari                                    qf_setupSur, height, numP_Sur, numQ_Sur,
15764438636fSLeila Ghaffari                                    qdatasizeSur, NqptsSur, basisxSur,
15774438636fSLeila Ghaffari                                    basisqSur, &user->op_rhs);
15784438636fSLeila Ghaffari   CHKERRQ(ierr);
15799fe13df9SLeila Ghaffari   if (implicit)
15804438636fSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, &bc, problemChoice, wind_type,
15814438636fSLeila Ghaffari                                    user->op_ifunction_vol, qf_applySur,
1582e2043960SLeila Ghaffari                                    qf_setupSur, height, numP_Sur, numQ_Sur,
15834438636fSLeila Ghaffari                                    qdatasizeSur, NqptsSur, basisxSur,
15844438636fSLeila Ghaffari                                    basisqSur, &user->op_ifunction);
15854438636fSLeila Ghaffari   CHKERRQ(ierr);
15867659d40cSLeila Ghaffari   // Set up contex for QFunctions
1587777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxSetup);
1588777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxSetup, CEED_MEM_HOST, CEED_USE_POINTER,
1589777ff853SJeremy L Thompson                               sizeof ctxSetupData, &ctxSetupData);
15905bf10c6fSLeila Ghaffari   if (qf_ics && problemChoice != NS_EULER_VORTEX)
1591777ff853SJeremy L Thompson     CeedQFunctionSetContext(qf_ics, ctxSetup);
1592777ff853SJeremy L Thompson 
15935bf10c6fSLeila Ghaffari   CeedScalar ctxNSData[8] = {lambda, mu, k, cv, cp, g, Rd};
1594777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxNS);
1595777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxNS, CEED_MEM_HOST, CEED_USE_POINTER,
1596777ff853SJeremy L Thompson                               sizeof ctxNSData, &ctxNSData);
1597777ff853SJeremy L Thompson 
15980da62991SLeila Ghaffari   struct Advection2dContext_ ctxAdvection2dData = {
1599ccaff030SJeremy L Thompson     .CtauS = CtauS,
1600ccaff030SJeremy L Thompson     .strong_form = strong_form,
1601ccaff030SJeremy L Thompson     .stabilization = stab,
1602ccaff030SJeremy L Thompson   };
1603777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxAdvection2d);
1604777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxAdvection2d, CEED_MEM_HOST, CEED_USE_POINTER,
1605777ff853SJeremy L Thompson                               sizeof ctxAdvection2dData, &ctxAdvection2dData);
1606777ff853SJeremy L Thompson 
1607777ff853SJeremy L Thompson   struct SurfaceContext_ ctxSurfaceData = {
160816c0476cSLeila Ghaffari     .E_wind = E_wind,
16097659d40cSLeila Ghaffari     .strong_form = strong_form,
1610e5ed8c30SLeila Ghaffari     .implicit = implicit,
1611e5ed8c30SLeila Ghaffari   };
1612777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxSurface);
1613777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxSurface, CEED_MEM_HOST, CEED_USE_POINTER,
1614777ff853SJeremy L Thompson                               sizeof ctxSurfaceData, &ctxSurfaceData);
1615777ff853SJeremy L Thompson 
16165bf10c6fSLeila Ghaffari   // Set up ctxEulerData structure
16175bf10c6fSLeila Ghaffari   ctxEulerData->time = 0.;
16185bf10c6fSLeila Ghaffari   ctxEulerData->currentTime = 0.;
1619a3ddc43aSLeila Ghaffari   ctxEulerData->euler_test = euler_test;
16205bf10c6fSLeila Ghaffari   ctxEulerData->center[0] = center[0];
16215bf10c6fSLeila Ghaffari   ctxEulerData->center[1] = center[1];
16225bf10c6fSLeila Ghaffari   ctxEulerData->center[2] = center[2];
16235bf10c6fSLeila Ghaffari   ctxEulerData->vortex_strength = vortex_strength;
16240da62991SLeila Ghaffari   ctxEulerData->etv_mean_velocity[0] = etv_mean_velocity[0];
16250da62991SLeila Ghaffari   ctxEulerData->etv_mean_velocity[1] = etv_mean_velocity[1];
16260da62991SLeila Ghaffari   ctxEulerData->etv_mean_velocity[2] = etv_mean_velocity[2];
1627*a45a933fSLeila Ghaffari   ctxEulerData->T_inlet = T_inlet;
1628*a45a933fSLeila Ghaffari   ctxEulerData->P_inlet = P_inlet;
16295bf10c6fSLeila Ghaffari   user->ctxEulerData = ctxEulerData;
16305bf10c6fSLeila Ghaffari 
16315bf10c6fSLeila Ghaffari   CeedQFunctionContextCreate(ceed, &ctxEuler);
16325bf10c6fSLeila Ghaffari   CeedQFunctionContextSetData(ctxEuler, CEED_MEM_HOST, CEED_USE_POINTER,
16330da62991SLeila Ghaffari                               sizeof *ctxEulerData, ctxEulerData);
16345bf10c6fSLeila Ghaffari 
1635ccaff030SJeremy L Thompson   switch (problemChoice) {
1636ccaff030SJeremy L Thompson   case NS_DENSITY_CURRENT:
16375bf10c6fSLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, ctxNS);
16385bf10c6fSLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, ctxNS);
1639ccaff030SJeremy L Thompson     break;
1640ccaff030SJeremy L Thompson   case NS_ADVECTION:
1641ccaff030SJeremy L Thompson   case NS_ADVECTION2D:
16425bf10c6fSLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, ctxAdvection2d);
16435bf10c6fSLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, ctxAdvection2d);
16445bf10c6fSLeila Ghaffari     if (qf_applySur) CeedQFunctionSetContext(qf_applySur, ctxSurface);
1645e5ed8c30SLeila Ghaffari   case NS_EULER_VORTEX:
16460da62991SLeila Ghaffari     if (qf_ics) CeedQFunctionSetContext(qf_ics, ctxEuler);
16475bf10c6fSLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, ctxEuler);
1648*a45a933fSLeila Ghaffari     if (qf_applySur) CeedQFunctionSetContext(qf_applySur, ctxEuler);
1649ccaff030SJeremy L Thompson   }
1650ccaff030SJeremy L Thompson 
1651ccaff030SJeremy L Thompson   // Set up PETSc context
1652ccaff030SJeremy L Thompson   // Set up units structure
1653ccaff030SJeremy L Thompson   units->meter = meter;
1654ccaff030SJeremy L Thompson   units->kilogram = kilogram;
1655ccaff030SJeremy L Thompson   units->second = second;
1656ccaff030SJeremy L Thompson   units->Kelvin = Kelvin;
1657ccaff030SJeremy L Thompson   units->Pascal = Pascal;
1658ccaff030SJeremy L Thompson   units->JperkgK = JperkgK;
1659ccaff030SJeremy L Thompson   units->mpersquareds = mpersquareds;
1660ccaff030SJeremy L Thompson   units->WpermK = WpermK;
1661ccaff030SJeremy L Thompson   units->kgpercubicm = kgpercubicm;
1662ccaff030SJeremy L Thompson   units->kgpersquaredms = kgpersquaredms;
1663ccaff030SJeremy L Thompson   units->Joulepercubicm = Joulepercubicm;
166416c0476cSLeila Ghaffari   units->Joule = Joule;
1665ccaff030SJeremy L Thompson 
1666ccaff030SJeremy L Thompson   // Set up user structure
1667ccaff030SJeremy L Thompson   user->comm = comm;
1668ccaff030SJeremy L Thompson   user->outputfreq = outputfreq;
1669ccaff030SJeremy L Thompson   user->contsteps = contsteps;
1670ccaff030SJeremy L Thompson   user->units = units;
1671ccaff030SJeremy L Thompson   user->dm = dm;
1672ccaff030SJeremy L Thompson   user->dmviz = dmviz;
1673ccaff030SJeremy L Thompson   user->interpviz = interpviz;
1674ccaff030SJeremy L Thompson   user->ceed = ceed;
1675ccaff030SJeremy L Thompson 
16768b982baeSLeila Ghaffari   // Calculate qdata and ICs
1677ccaff030SJeremy L Thompson   // Set up state global and local vectors
1678ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
1679ccaff030SJeremy L Thompson 
1680cfa64770SLeila Ghaffari   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
1681ccaff030SJeremy L Thompson 
1682ccaff030SJeremy L Thompson   // Apply Setup Ceed Operators
1683ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(xcorners, Xloc); CHKERRQ(ierr);
16848b982baeSLeila Ghaffari   CeedOperatorApply(op_setupVol, xcorners, qdata, CEED_REQUEST_IMMEDIATE);
168584d34d69SLeila Ghaffari   ierr = ComputeLumpedMassMatrix(ceed, dm, restrictq, basisq, restrictqdi, qdata,
1686ccaff030SJeremy L Thompson                                  user->M); CHKERRQ(ierr);
1687ccaff030SJeremy L Thompson 
168884d34d69SLeila Ghaffari   ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qloc, Q, restrictq,
1689777ff853SJeremy L Thompson                              ctxSetup, 0.0); CHKERRQ(ierr);
1690ccaff030SJeremy L Thompson   if (1) { // Record boundary values from initial condition and override DMPlexInsertBoundaryValues()
1691ccaff030SJeremy L Thompson     // We use this for the main simulation DM because the reference DMPlexInsertBoundaryValues() is very slow.  If we
1692ccaff030SJeremy L Thompson     // disable this, we should still get the same results due to the problem->bc function, but with potentially much
1693ccaff030SJeremy L Thompson     // slower execution.
1694ccaff030SJeremy L Thompson     Vec Qbc;
1695ccaff030SJeremy L Thompson     ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1696ccaff030SJeremy L Thompson     ierr = VecCopy(Qloc, Qbc); CHKERRQ(ierr);
1697ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
1698ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
1699ccaff030SJeremy L Thompson     ierr = VecAXPY(Qbc, -1., Qloc); CHKERRQ(ierr);
1700ccaff030SJeremy L Thompson     ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1701ccaff030SJeremy L Thompson     ierr = PetscObjectComposeFunction((PetscObject)dm,
170284d34d69SLeila Ghaffari                                       "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS);
170384d34d69SLeila Ghaffari     CHKERRQ(ierr);
1704ccaff030SJeremy L Thompson   }
1705ccaff030SJeremy L Thompson 
1706ccaff030SJeremy L Thompson   MPI_Comm_rank(comm, &rank);
1707d99129b9SLeila Ghaffari   if (!rank) {ierr = PetscMkdir(user->outputdir); CHKERRQ(ierr);}
1708ccaff030SJeremy L Thompson   // Gather initial Q values
1709ccaff030SJeremy L Thompson   // In case of continuation of simulation, set up initial values from binary file
1710ccaff030SJeremy L Thompson   if (contsteps) { // continue from existent solution
1711ccaff030SJeremy L Thompson     PetscViewer viewer;
1712ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1713ccaff030SJeremy L Thompson     // Read input
1714ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
1715d99129b9SLeila Ghaffari                          user->outputdir);
1716ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1717ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1718ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1719ccaff030SJeremy L Thompson     ierr = VecLoad(Q, viewer); CHKERRQ(ierr);
1720ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1721ccaff030SJeremy L Thompson   }
1722ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Qloc); CHKERRQ(ierr);
1723ccaff030SJeremy L Thompson 
1724ccaff030SJeremy L Thompson   // Create and setup TS
1725ccaff030SJeremy L Thompson   ierr = TSCreate(comm, &ts); CHKERRQ(ierr);
1726ccaff030SJeremy L Thompson   ierr = TSSetDM(ts, dm); CHKERRQ(ierr);
1727ccaff030SJeremy L Thompson   if (implicit) {
1728ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSBDF); CHKERRQ(ierr);
1729ccaff030SJeremy L Thompson     if (user->op_ifunction) {
1730ccaff030SJeremy L Thompson       ierr = TSSetIFunction(ts, NULL, IFunction_NS, &user); CHKERRQ(ierr);
1731ccaff030SJeremy L Thompson     } else {                    // Implicit integrators can fall back to using an RHSFunction
1732ccaff030SJeremy L Thompson       ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1733ccaff030SJeremy L Thompson     }
1734ccaff030SJeremy L Thompson   } else {
1735ccaff030SJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL,
1736ccaff030SJeremy L Thompson                                  "Problem does not provide RHSFunction");
1737ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSRK); CHKERRQ(ierr);
1738ccaff030SJeremy L Thompson     ierr = TSRKSetType(ts, TSRK5F); CHKERRQ(ierr);
1739ccaff030SJeremy L Thompson     ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1740ccaff030SJeremy L Thompson   }
1741ccaff030SJeremy L Thompson   ierr = TSSetMaxTime(ts, 500. * units->second); CHKERRQ(ierr);
1742ccaff030SJeremy L Thompson   ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER); CHKERRQ(ierr);
1743ccaff030SJeremy L Thompson   ierr = TSSetTimeStep(ts, 1.e-2 * units->second); CHKERRQ(ierr);
1744dc8efd83SLeila Ghaffari   if (test) {ierr = TSSetMaxSteps(ts, 10); CHKERRQ(ierr);}
1745ccaff030SJeremy L Thompson   ierr = TSGetAdapt(ts, &adapt); CHKERRQ(ierr);
1746ccaff030SJeremy L Thompson   ierr = TSAdaptSetStepLimits(adapt,
1747ccaff030SJeremy L Thompson                               1.e-12 * units->second,
1748ccaff030SJeremy L Thompson                               1.e2 * units->second); CHKERRQ(ierr);
1749ccaff030SJeremy L Thompson   ierr = TSSetFromOptions(ts); CHKERRQ(ierr);
1750ccaff030SJeremy L Thompson   if (!contsteps) { // print initial condition
1751dc8efd83SLeila Ghaffari     if (!test) {
1752ccaff030SJeremy L Thompson       ierr = TSMonitor_NS(ts, 0, 0., Q, user); CHKERRQ(ierr);
1753ccaff030SJeremy L Thompson     }
1754ccaff030SJeremy L Thompson   } else { // continue from time of last output
1755ccaff030SJeremy L Thompson     PetscReal time;
1756ccaff030SJeremy L Thompson     PetscInt count;
1757ccaff030SJeremy L Thompson     PetscViewer viewer;
1758ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1759ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
1760d99129b9SLeila Ghaffari                          user->outputdir); CHKERRQ(ierr);
1761ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1762ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1763ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL);
1764ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1765ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1766ccaff030SJeremy L Thompson     ierr = TSSetTime(ts, time * user->units->second); CHKERRQ(ierr);
1767ccaff030SJeremy L Thompson   }
1768dc8efd83SLeila Ghaffari   if (!test) {
1769ccaff030SJeremy L Thompson     ierr = TSMonitorSet(ts, TSMonitor_NS, user, NULL); CHKERRQ(ierr);
1770ccaff030SJeremy L Thompson   }
1771ccaff030SJeremy L Thompson 
1772ccaff030SJeremy L Thompson   // Solve
1773ccaff030SJeremy L Thompson   start = MPI_Wtime();
1774ccaff030SJeremy L Thompson   ierr = PetscBarrier((PetscObject)ts); CHKERRQ(ierr);
1775ccaff030SJeremy L Thompson   ierr = TSSolve(ts, Q); CHKERRQ(ierr);
1776ccaff030SJeremy L Thompson   cpu_time_used = MPI_Wtime() - start;
1777ccaff030SJeremy L Thompson   ierr = TSGetSolveTime(ts, &ftime); CHKERRQ(ierr);
1778ccaff030SJeremy L Thompson   ierr = MPI_Allreduce(MPI_IN_PLACE, &cpu_time_used, 1, MPI_DOUBLE, MPI_MIN,
1779ccaff030SJeremy L Thompson                        comm); CHKERRQ(ierr);
1780dc8efd83SLeila Ghaffari   if (!test) {
1781ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
178284d34d69SLeila Ghaffari                        "Time taken for solution (sec): %g\n",
1783ccaff030SJeremy L Thompson                        (double)cpu_time_used); CHKERRQ(ierr);
1784ccaff030SJeremy L Thompson   }
1785ccaff030SJeremy L Thompson 
1786ccaff030SJeremy L Thompson   // Get error
1787dc8efd83SLeila Ghaffari   if (problem->non_zero_time && !test) {
1788ccaff030SJeremy L Thompson     Vec Qexact, Qexactloc;
1789ccaff030SJeremy L Thompson     PetscReal norm;
1790ccaff030SJeremy L Thompson     ierr = DMCreateGlobalVector(dm, &Qexact); CHKERRQ(ierr);
1791ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
1792ccaff030SJeremy L Thompson     ierr = VecGetSize(Qexactloc, &lnodes); CHKERRQ(ierr);
1793ccaff030SJeremy L Thompson 
179484d34d69SLeila Ghaffari     ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qexactloc, Qexact,
1795777ff853SJeremy L Thompson                                restrictq, ctxSetup, ftime); CHKERRQ(ierr);
1796ccaff030SJeremy L Thompson 
1797ccaff030SJeremy L Thompson     ierr = VecAXPY(Q, -1.0, Qexact);  CHKERRQ(ierr);
1798ccaff030SJeremy L Thompson     ierr = VecNorm(Q, NORM_MAX, &norm); CHKERRQ(ierr);
1799cfa64770SLeila Ghaffari     CeedVectorDestroy(&q0ceed);
1800ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1801ccaff030SJeremy L Thompson                        "Max Error: %g\n",
1802ccaff030SJeremy L Thompson                        (double)norm); CHKERRQ(ierr);
180384d34d69SLeila Ghaffari     // Clean up vectors
180484d34d69SLeila Ghaffari     ierr = DMRestoreLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
180584d34d69SLeila Ghaffari     ierr = VecDestroy(&Qexact); CHKERRQ(ierr);
1806ccaff030SJeremy L Thompson   }
1807ccaff030SJeremy L Thompson 
1808ccaff030SJeremy L Thompson   // Output Statistics
1809ccaff030SJeremy L Thompson   ierr = TSGetStepNumber(ts, &steps); CHKERRQ(ierr);
1810dc8efd83SLeila Ghaffari   if (!test) {
1811ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1812ccaff030SJeremy L Thompson                        "Time integrator took %D time steps to reach final time %g\n",
1813ccaff030SJeremy L Thompson                        steps, (double)ftime); CHKERRQ(ierr);
1814ccaff030SJeremy L Thompson   }
181584d34d69SLeila Ghaffari   // Output numerical values from command line
181684d34d69SLeila Ghaffari   ierr = VecViewFromOptions(Q, NULL, "-vec_view"); CHKERRQ(ierr);
181784d34d69SLeila Ghaffari 
1818335ea220SLeila Ghaffari   // Compare reference solution values with current test run for CI
1819dc8efd83SLeila Ghaffari   if (test) {
182084d34d69SLeila Ghaffari     PetscViewer viewer;
182184d34d69SLeila Ghaffari     // Read reference file
182284d34d69SLeila Ghaffari     Vec Qref;
182384d34d69SLeila Ghaffari     PetscReal error, Qrefnorm;
182484d34d69SLeila Ghaffari     ierr = VecDuplicate(Q, &Qref); CHKERRQ(ierr);
1825dc8efd83SLeila Ghaffari     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
182684d34d69SLeila Ghaffari     CHKERRQ(ierr);
182784d34d69SLeila Ghaffari     ierr = VecLoad(Qref, viewer); CHKERRQ(ierr);
182884d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
182984d34d69SLeila Ghaffari 
183084d34d69SLeila Ghaffari     // Compute error with respect to reference solution
183184d34d69SLeila Ghaffari     ierr = VecAXPY(Q, -1.0, Qref);  CHKERRQ(ierr);
183284d34d69SLeila Ghaffari     ierr = VecNorm(Qref, NORM_MAX, &Qrefnorm); CHKERRQ(ierr);
183384d34d69SLeila Ghaffari     ierr = VecScale(Q, 1./Qrefnorm); CHKERRQ(ierr);
183484d34d69SLeila Ghaffari     ierr = VecNorm(Q, NORM_MAX, &error); CHKERRQ(ierr);
183584d34d69SLeila Ghaffari     ierr = VecDestroy(&Qref); CHKERRQ(ierr);
183684d34d69SLeila Ghaffari     // Check error
1837dc8efd83SLeila Ghaffari     if (error > testtol) {
183884d34d69SLeila Ghaffari       ierr = PetscPrintf(PETSC_COMM_WORLD,
183984d34d69SLeila Ghaffari                          "Test failed with error norm %g\n",
184084d34d69SLeila Ghaffari                          (double)error); CHKERRQ(ierr);
184184d34d69SLeila Ghaffari     }
184284d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
184384d34d69SLeila Ghaffari   }
18449cf88b28Svaleriabarra 
1845ccaff030SJeremy L Thompson   // Clean up libCEED
18468b982baeSLeila Ghaffari   CeedVectorDestroy(&qdata);
1847ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qceed);
1848ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qdotceed);
1849ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->gceed);
1850ccaff030SJeremy L Thompson   CeedVectorDestroy(&xcorners);
185184d34d69SLeila Ghaffari   CeedBasisDestroy(&basisq);
185284d34d69SLeila Ghaffari   CeedBasisDestroy(&basisx);
185384d34d69SLeila Ghaffari   CeedBasisDestroy(&basisxc);
185484d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictq);
185584d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictx);
185684d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictqdi);
1857ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupVol);
1858ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_ics);
1859ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_rhsVol);
1860ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_ifunctionVol);
1861777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxSetup);
1862777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxNS);
1863777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxAdvection2d);
1864777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxSurface);
18650da62991SLeila Ghaffari   CeedQFunctionContextDestroy(&ctxEuler);
1866ea6e0f84SLeila Ghaffari   CeedOperatorDestroy(&op_setupVol);
1867ccaff030SJeremy L Thompson   CeedOperatorDestroy(&op_ics);
18686a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_rhs_vol);
18696a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_ifunction_vol);
18706a0edaf9SLeila Ghaffari   CeedDestroy(&ceed);
18716a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisqSur);
18726a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxSur);
18736a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxcSur);
18746a0edaf9SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupSur);
18757ed8b9c7SLeila Ghaffari   CeedQFunctionDestroy(&qf_applySur);
1876ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_rhs);
1877ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_ifunction);
1878ccaff030SJeremy L Thompson 
1879ccaff030SJeremy L Thompson   // Clean up PETSc
1880ccaff030SJeremy L Thompson   ierr = VecDestroy(&Q); CHKERRQ(ierr);
1881ccaff030SJeremy L Thompson   ierr = VecDestroy(&user->M); CHKERRQ(ierr);
1882ccaff030SJeremy L Thompson   ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1883ccaff030SJeremy L Thompson   ierr = DMDestroy(&dmviz); CHKERRQ(ierr);
1884ccaff030SJeremy L Thompson   ierr = TSDestroy(&ts); CHKERRQ(ierr);
1885ccaff030SJeremy L Thompson   ierr = DMDestroy(&dm); CHKERRQ(ierr);
1886ccaff030SJeremy L Thompson   ierr = PetscFree(units); CHKERRQ(ierr);
1887ccaff030SJeremy L Thompson   ierr = PetscFree(user); CHKERRQ(ierr);
18885bf10c6fSLeila Ghaffari   ierr = PetscFree(ctxEulerData); CHKERRQ(ierr);
1889ccaff030SJeremy L Thompson   return PetscFinalize();
1890ccaff030SJeremy L Thompson }
1891