xref: /libCEED/examples/fluids/navierstokes.c (revision e5ed8c30612e91d59941f31c7e7a9ba1dc4ce296)
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",
87*e5ed8c30SLeila 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 
114ccaff030SJeremy L Thompson // Problem specific data
115ccaff030SJeremy L Thompson typedef struct {
1168b982baeSLeila Ghaffari   CeedInt dim, qdatasizeVol, qdatasizeSur;
117ebb4b9bdSLeila Ghaffari   CeedQFunctionUser setupVol, setupSur, ics, applyVol_rhs, applyVol_ifunction,
1184438636fSLeila Ghaffari                     applySur, applyIn, applyOut;
119ccaff030SJeremy L Thompson   PetscErrorCode (*bc)(PetscInt, PetscReal, const PetscReal[], PetscInt,
120ccaff030SJeremy L Thompson                        PetscScalar[], void *);
1217659d40cSLeila Ghaffari   const char *setupVol_loc, *setupSur_loc, *ics_loc, *applyVol_rhs_loc,
1224438636fSLeila Ghaffari         *applyVol_ifunction_loc, *applySur_loc, *applyIn_loc, *applyOut_loc;
123ccaff030SJeremy L Thompson   const bool non_zero_time;
124ccaff030SJeremy L Thompson } problemData;
125ccaff030SJeremy L Thompson 
126ccaff030SJeremy L Thompson problemData problemOptions[] = {
127ccaff030SJeremy L Thompson   [NS_DENSITY_CURRENT] = {
128ccaff030SJeremy L Thompson     .dim                       = 3,
129ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1308b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
131b0137797SLeila Ghaffari     .setupVol                  = Setup,
132b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
133356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
134356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
135ccaff030SJeremy L Thompson     .ics                       = ICsDC,
136ccaff030SJeremy L Thompson     .ics_loc                   = ICsDC_loc,
137c96c872fSLeila Ghaffari     .applyVol_rhs              = DC,
138c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = DC_loc,
139c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_DC,
140c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_DC_loc,
141ccaff030SJeremy L Thompson     .bc                        = Exact_DC,
14284d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
143ccaff030SJeremy L Thompson   },
144ccaff030SJeremy L Thompson   [NS_ADVECTION] = {
145ccaff030SJeremy L Thompson     .dim                       = 3,
146ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 10,
1478b982baeSLeila Ghaffari     .qdatasizeSur              = 4,
148b0137797SLeila Ghaffari     .setupVol                  = Setup,
149b0137797SLeila Ghaffari     .setupVol_loc              = Setup_loc,
150356fbf4bSLeila Ghaffari     .setupSur                  = SetupBoundary,
151356fbf4bSLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
152ccaff030SJeremy L Thompson     .ics                       = ICsAdvection,
153ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection_loc,
154c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection,
155c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection_loc,
156c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection,
157c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection_loc,
1587659d40cSLeila Ghaffari     .applySur                  = Advection_Sur,
1597659d40cSLeila Ghaffari     .applySur_loc              = Advection_Sur_loc,
160ccaff030SJeremy L Thompson     .bc                        = Exact_Advection,
16184d34d69SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,
162ccaff030SJeremy L Thompson   },
163ccaff030SJeremy L Thompson   [NS_ADVECTION2D] = {
164ccaff030SJeremy L Thompson     .dim                       = 2,
165ea6e0f84SLeila Ghaffari     .qdatasizeVol              = 5,
1668b982baeSLeila Ghaffari     .qdatasizeSur              = 3,
167c96c872fSLeila Ghaffari     .setupVol                  = Setup2d,
168c96c872fSLeila Ghaffari     .setupVol_loc              = Setup2d_loc,
169b0137797SLeila Ghaffari     .setupSur                  = SetupBoundary2d,
170b0137797SLeila Ghaffari     .setupSur_loc              = SetupBoundary2d_loc,
171ccaff030SJeremy L Thompson     .ics                       = ICsAdvection2d,
172ccaff030SJeremy L Thompson     .ics_loc                   = ICsAdvection2d_loc,
173c96c872fSLeila Ghaffari     .applyVol_rhs              = Advection2d,
174c96c872fSLeila Ghaffari     .applyVol_rhs_loc          = Advection2d_loc,
175c96c872fSLeila Ghaffari     .applyVol_ifunction        = IFunction_Advection2d,
176c96c872fSLeila Ghaffari     .applyVol_ifunction_loc    = IFunction_Advection2d_loc,
1777659d40cSLeila Ghaffari     .applySur                  = Advection2d_Sur,
1787659d40cSLeila Ghaffari     .applySur_loc              = Advection2d_Sur_loc,
179ccaff030SJeremy L Thompson     .bc                        = Exact_Advection2d,
18084d34d69SLeila Ghaffari     .non_zero_time             = PETSC_TRUE,
181ccaff030SJeremy L Thompson   },
182eb088987SLeila Ghaffari   [NS_EULER_VORTEX] = {
183eb088987SLeila Ghaffari     .dim                       = 3,
184eb088987SLeila Ghaffari     .qdatasizeVol              = 10,
185eb088987SLeila Ghaffari     .qdatasizeSur              = 4,
186eb088987SLeila Ghaffari     .setupVol                  = Setup,
187eb088987SLeila Ghaffari     .setupVol_loc              = Setup_loc,
188eb088987SLeila Ghaffari     .setupSur                  = SetupBoundary,
189eb088987SLeila Ghaffari     .setupSur_loc              = SetupBoundary_loc,
190eb088987SLeila Ghaffari     .ics                       = ICsEuler,
191eb088987SLeila Ghaffari     .ics_loc                   = ICsEuler_loc,
192eb088987SLeila Ghaffari     .applyVol_rhs              = Euler,
193eb088987SLeila Ghaffari     .applyVol_rhs_loc          = Euler_loc,
1944438636fSLeila Ghaffari     .applyIn                   = Euler_In,
1954438636fSLeila Ghaffari     .applyIn_loc               = Euler_In_loc,
1964438636fSLeila Ghaffari     .applyOut                  = Euler_Out,
1974438636fSLeila Ghaffari     .applyOut_loc              = Euler_Out_loc,
198eb088987SLeila Ghaffari     .bc                        = Exact_Euler,
199eb088987SLeila Ghaffari     .non_zero_time             = PETSC_FALSE,  // TODO: this needs to be true
200eb088987SLeila Ghaffari   },
201ccaff030SJeremy L Thompson };
202ccaff030SJeremy L Thompson 
203ccaff030SJeremy L Thompson // PETSc user data
204ccaff030SJeremy L Thompson typedef struct User_ *User;
205ccaff030SJeremy L Thompson typedef struct Units_ *Units;
206ccaff030SJeremy L Thompson 
207ccaff030SJeremy L Thompson struct User_ {
208ccaff030SJeremy L Thompson   MPI_Comm comm;
209ccaff030SJeremy L Thompson   PetscInt outputfreq;
210ccaff030SJeremy L Thompson   DM dm;
211ccaff030SJeremy L Thompson   DM dmviz;
212ccaff030SJeremy L Thompson   Mat interpviz;
213ccaff030SJeremy L Thompson   Ceed ceed;
214ccaff030SJeremy L Thompson   Units units;
215ccaff030SJeremy L Thompson   CeedVector qceed, qdotceed, gceed;
2161e150236SLeila Ghaffari   CeedOperator op_rhs_vol, op_rhs, op_ifunction_vol, op_ifunction;
217ccaff030SJeremy L Thompson   Vec M;
218d99129b9SLeila Ghaffari   char outputdir[PETSC_MAX_PATH_LEN];
219ccaff030SJeremy L Thompson   PetscInt contsteps;
220ccaff030SJeremy L Thompson };
221ccaff030SJeremy L Thompson 
222ccaff030SJeremy L Thompson struct Units_ {
223ccaff030SJeremy L Thompson   // fundamental units
224ccaff030SJeremy L Thompson   PetscScalar meter;
225ccaff030SJeremy L Thompson   PetscScalar kilogram;
226ccaff030SJeremy L Thompson   PetscScalar second;
227ccaff030SJeremy L Thompson   PetscScalar Kelvin;
228ccaff030SJeremy L Thompson   // derived units
229ccaff030SJeremy L Thompson   PetscScalar Pascal;
230ccaff030SJeremy L Thompson   PetscScalar JperkgK;
231ccaff030SJeremy L Thompson   PetscScalar mpersquareds;
232ccaff030SJeremy L Thompson   PetscScalar WpermK;
233ccaff030SJeremy L Thompson   PetscScalar kgpercubicm;
234ccaff030SJeremy L Thompson   PetscScalar kgpersquaredms;
235ccaff030SJeremy L Thompson   PetscScalar Joulepercubicm;
23616c0476cSLeila Ghaffari   PetscScalar Joule;
237ccaff030SJeremy L Thompson };
238ccaff030SJeremy L Thompson 
239ccaff030SJeremy L Thompson typedef struct SimpleBC_ *SimpleBC;
240ccaff030SJeremy L Thompson struct SimpleBC_ {
2417659d40cSLeila Ghaffari   PetscInt nwall, nslip[3];
2427659d40cSLeila Ghaffari   PetscInt walls[6], slips[3][6];
24384d34d69SLeila Ghaffari   PetscBool userbc;
244ccaff030SJeremy L Thompson };
245ccaff030SJeremy L Thompson 
246ccaff030SJeremy L Thompson // Essential BC dofs are encoded in closure indices as -(i+1).
247ccaff030SJeremy L Thompson static PetscInt Involute(PetscInt i) {
248ccaff030SJeremy L Thompson   return i >= 0 ? i : -(i+1);
249ccaff030SJeremy L Thompson }
250ccaff030SJeremy L Thompson 
251ccaff030SJeremy L Thompson // Utility function to create local CEED restriction
252ccaff030SJeremy L Thompson static PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt P,
25384d34d69SLeila Ghaffari     CeedInt height, DMLabel domainLabel, CeedInt value,
254ccaff030SJeremy L Thompson     CeedElemRestriction *Erestrict) {
255ccaff030SJeremy L Thompson 
256ccaff030SJeremy L Thompson   PetscSection section;
2571184866aSLeila Ghaffari   PetscInt p, Nelem, Ndof, *erestrict, eoffset, nfields, dim, depth;
2580c6c0b13SLeila Ghaffari   DMLabel depthLabel;
2590c6c0b13SLeila Ghaffari   IS depthIS, iterIS;
26084d34d69SLeila Ghaffari   Vec Uloc;
2610c6c0b13SLeila Ghaffari   const PetscInt *iterIndices;
262ccaff030SJeremy L Thompson   PetscErrorCode ierr;
263ccaff030SJeremy L Thompson 
264ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
265ccaff030SJeremy L Thompson   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
266da51bdd9SLeila Ghaffari   dim -= height;
267ccaff030SJeremy L Thompson   ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
268ccaff030SJeremy L Thompson   ierr = PetscSectionGetNumFields(section, &nfields); CHKERRQ(ierr);
269ccaff030SJeremy L Thompson   PetscInt ncomp[nfields], fieldoff[nfields+1];
270ccaff030SJeremy L Thompson   fieldoff[0] = 0;
271ccaff030SJeremy L Thompson   for (PetscInt f=0; f<nfields; f++) {
272ccaff030SJeremy L Thompson     ierr = PetscSectionGetFieldComponents(section, f, &ncomp[f]); CHKERRQ(ierr);
273ccaff030SJeremy L Thompson     fieldoff[f+1] = fieldoff[f] + ncomp[f];
274ccaff030SJeremy L Thompson   }
275ccaff030SJeremy L Thompson 
2760c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepth(dm, &depth); CHKERRQ(ierr);
2770c6c0b13SLeila Ghaffari   ierr = DMPlexGetDepthLabel(dm, &depthLabel); CHKERRQ(ierr);
2780c6c0b13SLeila Ghaffari   ierr = DMLabelGetStratumIS(depthLabel, depth - height, &depthIS); CHKERRQ(ierr);
2790c6c0b13SLeila Ghaffari   if (domainLabel) {
2800c6c0b13SLeila Ghaffari     IS domainIS;
2810c6c0b13SLeila Ghaffari     ierr = DMLabelGetStratumIS(domainLabel, value, &domainIS); CHKERRQ(ierr);
2821119eeeeSJed Brown     if (domainIS) { // domainIS is non-empty
2830c6c0b13SLeila Ghaffari       ierr = ISIntersect(depthIS, domainIS, &iterIS); CHKERRQ(ierr);
2840c6c0b13SLeila Ghaffari       ierr = ISDestroy(&domainIS); CHKERRQ(ierr);
2851119eeeeSJed Brown     } else { // domainIS is NULL (empty)
2861119eeeeSJed Brown       iterIS = NULL;
2871119eeeeSJed Brown     }
2880c6c0b13SLeila Ghaffari     ierr = ISDestroy(&depthIS); CHKERRQ(ierr);
2890c6c0b13SLeila Ghaffari   } else {
2900c6c0b13SLeila Ghaffari     iterIS = depthIS;
2910c6c0b13SLeila Ghaffari   }
2921119eeeeSJed Brown   if (iterIS) {
2930c6c0b13SLeila Ghaffari     ierr = ISGetLocalSize(iterIS, &Nelem); CHKERRQ(ierr);
2940c6c0b13SLeila Ghaffari     ierr = ISGetIndices(iterIS, &iterIndices); CHKERRQ(ierr);
2951119eeeeSJed Brown   } else {
2961119eeeeSJed Brown     Nelem = 0;
2971119eeeeSJed Brown     iterIndices = NULL;
2981119eeeeSJed Brown   }
299ccaff030SJeremy L Thompson   ierr = PetscMalloc1(Nelem*PetscPowInt(P, dim), &erestrict); CHKERRQ(ierr);
3000c6c0b13SLeila Ghaffari   for (p=0,eoffset=0; p<Nelem; p++) {
3010c6c0b13SLeila Ghaffari     PetscInt c = iterIndices[p];
302ccaff030SJeremy L Thompson     PetscInt numindices, *indices, nnodes;
30384d34d69SLeila Ghaffari     ierr = DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE,
30484d34d69SLeila Ghaffari                                    &numindices, &indices, NULL, NULL);
30584d34d69SLeila Ghaffari     CHKERRQ(ierr);
30632b5ec5fSJed Brown     bool flip = false;
30732b5ec5fSJed Brown     if (height > 0) {
30832b5ec5fSJed Brown       PetscInt numCells, numFaces, start = -1;
30932b5ec5fSJed Brown       const PetscInt *orients, *faces, *cells;
31032b5ec5fSJed Brown       ierr = DMPlexGetSupport(dm, c, &cells); CHKERRQ(ierr);
31132b5ec5fSJed Brown       ierr = DMPlexGetSupportSize(dm, c, &numCells); CHKERRQ(ierr);
312ebb4b9bdSLeila Ghaffari       if (numCells != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
313f259b054Svaleriabarra                                     "Expected one cell in support of exterior face, but got %D cells",
314f259b054Svaleriabarra                                     numCells);
31532b5ec5fSJed Brown       ierr = DMPlexGetCone(dm, cells[0], &faces); CHKERRQ(ierr);
31632b5ec5fSJed Brown       ierr = DMPlexGetConeSize(dm, cells[0], &numFaces); CHKERRQ(ierr);
31732b5ec5fSJed Brown       for (PetscInt i=0; i<numFaces; i++) {if (faces[i] == c) start = i;}
318ebb4b9bdSLeila Ghaffari       if (start < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT,
319f259b054Svaleriabarra                                 "Could not find face %D in cone of its support",
320f259b054Svaleriabarra                                 c);
32132b5ec5fSJed Brown       ierr = DMPlexGetConeOrientation(dm, cells[0], &orients); CHKERRQ(ierr);
32232b5ec5fSJed Brown       if (orients[start] < 0) flip = true;
32332b5ec5fSJed Brown     }
32484d34d69SLeila Ghaffari     if (numindices % fieldoff[nfields]) SETERRQ1(PETSC_COMM_SELF,
32584d34d69SLeila Ghaffari           PETSC_ERR_ARG_INCOMP, "Number of closure indices not compatible with Cell %D",
32684d34d69SLeila Ghaffari           c);
327ccaff030SJeremy L Thompson     nnodes = numindices / fieldoff[nfields];
328ccaff030SJeremy L Thompson     for (PetscInt i=0; i<nnodes; i++) {
32932b5ec5fSJed Brown       PetscInt ii = i;
33032b5ec5fSJed Brown       if (flip) {
33132b5ec5fSJed Brown         if (P == nnodes) ii = nnodes - 1 - i;
33232b5ec5fSJed Brown         else if (P*P == nnodes) {
33332b5ec5fSJed Brown           PetscInt row = i / P, col = i % P;
33432b5ec5fSJed Brown           ii = row + col * P;
335ebb4b9bdSLeila Ghaffari         } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_SUP,
336f259b054Svaleriabarra                           "No support for flipping point with %D nodes != P (%D) or P^2",
337f259b054Svaleriabarra                           nnodes, P);
33832b5ec5fSJed Brown       }
339ccaff030SJeremy L Thompson       // Check that indices are blocked by node and thus can be coalesced as a single field with
340ccaff030SJeremy L Thompson       // fieldoff[nfields] = sum(ncomp) components.
341ccaff030SJeremy L Thompson       for (PetscInt f=0; f<nfields; f++) {
342ccaff030SJeremy L Thompson         for (PetscInt j=0; j<ncomp[f]; j++) {
34332b5ec5fSJed Brown           if (Involute(indices[fieldoff[f]*nnodes + ii*ncomp[f] + j])
34432b5ec5fSJed Brown               != Involute(indices[ii*ncomp[0]]) + fieldoff[f] + j)
345ccaff030SJeremy L Thompson             SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
346ccaff030SJeremy L Thompson                      "Cell %D closure indices not interlaced for node %D field %D component %D",
34732b5ec5fSJed Brown                      c, ii, f, j);
348ccaff030SJeremy L Thompson         }
349ccaff030SJeremy L Thompson       }
350ccaff030SJeremy L Thompson       // Essential boundary conditions are encoded as -(loc+1), but we don't care so we decode.
35132b5ec5fSJed Brown       PetscInt loc = Involute(indices[ii*ncomp[0]]);
3526f55dfd5Svaleriabarra       erestrict[eoffset++] = loc;
353ccaff030SJeremy L Thompson     }
35484d34d69SLeila Ghaffari     ierr = DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE,
35584d34d69SLeila Ghaffari                                        &numindices, &indices, NULL, NULL);
35684d34d69SLeila Ghaffari     CHKERRQ(ierr);
357ccaff030SJeremy L Thompson   }
3580c6c0b13SLeila Ghaffari   if (eoffset != Nelem*PetscPowInt(P, dim))
3590c6c0b13SLeila Ghaffari     SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_LIB,
3600c6c0b13SLeila Ghaffari              "ElemRestriction of size (%D,%D) initialized %D nodes", Nelem,
361ccaff030SJeremy L Thompson              PetscPowInt(P, dim),eoffset);
3621119eeeeSJed Brown   if (iterIS) {
3630c6c0b13SLeila Ghaffari     ierr = ISRestoreIndices(iterIS, &iterIndices); CHKERRQ(ierr);
3641119eeeeSJed Brown   }
3650c6c0b13SLeila Ghaffari   ierr = ISDestroy(&iterIS); CHKERRQ(ierr);
3660c6c0b13SLeila Ghaffari 
367ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Uloc); CHKERRQ(ierr);
368ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(Uloc, &Ndof); CHKERRQ(ierr);
369ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Uloc); CHKERRQ(ierr);
3706f55dfd5Svaleriabarra   CeedElemRestrictionCreate(ceed, Nelem, PetscPowInt(P, dim), fieldoff[nfields],
3716f55dfd5Svaleriabarra                             1, Ndof, CEED_MEM_HOST, CEED_COPY_VALUES, erestrict,
3726f55dfd5Svaleriabarra                             Erestrict);
373ccaff030SJeremy L Thompson   ierr = PetscFree(erestrict); CHKERRQ(ierr);
374ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
375ccaff030SJeremy L Thompson }
376ccaff030SJeremy L Thompson 
377c96c872fSLeila Ghaffari // Utility function to get Ceed Restriction for each domain
3781e150236SLeila Ghaffari static PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
3791e150236SLeila Ghaffari     DMLabel domainLabel, PetscInt value, CeedInt P, CeedInt Q, CeedInt qdatasize,
3801e150236SLeila Ghaffari     CeedElemRestriction *restrictq, CeedElemRestriction *restrictx,
3811e150236SLeila Ghaffari     CeedElemRestriction *restrictqdi) {
382c96c872fSLeila Ghaffari 
383c96c872fSLeila Ghaffari   DM dmcoord;
3841e150236SLeila Ghaffari   CeedInt dim, localNelem;
3851e150236SLeila Ghaffari   CeedInt Qdim;
386c96c872fSLeila Ghaffari   PetscErrorCode ierr;
387c96c872fSLeila Ghaffari 
388c96c872fSLeila Ghaffari   PetscFunctionBeginUser;
3891e150236SLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
3901e150236SLeila Ghaffari   dim -= height;
3911e150236SLeila Ghaffari   Qdim = CeedIntPow(Q, dim);
392c96c872fSLeila Ghaffari   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
393c96c872fSLeila Ghaffari   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
394c96c872fSLeila Ghaffari   CHKERRQ(ierr);
395ebb4b9bdSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dm, P, height, domainLabel, value,
396ebb4b9bdSLeila Ghaffari                                    restrictq);
397c96c872fSLeila Ghaffari   CHKERRQ(ierr);
398ebb4b9bdSLeila Ghaffari   ierr = CreateRestrictionFromPlex(ceed, dmcoord, 2, height, domainLabel, value,
399ebb4b9bdSLeila Ghaffari                                    restrictx);
400c96c872fSLeila Ghaffari   CHKERRQ(ierr);
401c96c872fSLeila Ghaffari   CeedElemRestrictionGetNumElements(*restrictq, &localNelem);
402c96c872fSLeila Ghaffari   CeedElemRestrictionCreateStrided(ceed, localNelem, Qdim,
403c96c872fSLeila Ghaffari                                    qdatasize, qdatasize*localNelem*Qdim,
404c96c872fSLeila Ghaffari                                    CEED_STRIDES_BACKEND, restrictqdi);
405c96c872fSLeila Ghaffari   PetscFunctionReturn(0);
406c96c872fSLeila Ghaffari }
407c96c872fSLeila Ghaffari 
4081e150236SLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
4097659d40cSLeila Ghaffari static PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc,
4104438636fSLeila Ghaffari     problemType problemChoice, WindType wind_type, CeedOperator op_applyVol,
4114438636fSLeila Ghaffari     CeedQFunction qf_applySur, CeedQFunction qf_setupSur,
4124438636fSLeila Ghaffari     CeedQFunction qf_applyIn, CeedQFunction qf_applyOut, CeedInt height,
4134438636fSLeila Ghaffari     CeedInt numP_Sur, CeedInt numQ_Sur, CeedInt qdatasizeSur, CeedInt NqptsSur,
4144438636fSLeila Ghaffari     CeedBasis basisxSur, CeedBasis basisqSur, CeedOperator *op_apply) {
415ca3ac6ddSLeila Ghaffari 
4167659d40cSLeila Ghaffari   CeedInt dim, nFace;
4174438636fSLeila Ghaffari   PetscInt lsize;
4181e150236SLeila Ghaffari   Vec Xloc;
4194438636fSLeila Ghaffari   CeedVector xcorners;
420ca3ac6ddSLeila Ghaffari   DMLabel domainLabel;
4211e150236SLeila Ghaffari   PetscScalar *x;
422ca3ac6ddSLeila Ghaffari   PetscErrorCode ierr;
423ca3ac6ddSLeila Ghaffari 
424ca3ac6ddSLeila Ghaffari   PetscFunctionBeginUser;
425ca3ac6ddSLeila Ghaffari   // Composite Operaters
426ca3ac6ddSLeila Ghaffari   CeedCompositeOperatorCreate(ceed, op_apply);
4274438636fSLeila Ghaffari   // --Apply a Sub-Operator for the volume
4284438636fSLeila Ghaffari   CeedCompositeOperatorAddSub(*op_apply, op_applyVol);
429ca3ac6ddSLeila Ghaffari 
4304438636fSLeila Ghaffari   // Required data for in/outflow BCs
4311e150236SLeila Ghaffari   ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
4321e150236SLeila Ghaffari   ierr = VecGetLocalSize(Xloc, &lsize); CHKERRQ(ierr);
4331e150236SLeila Ghaffari   ierr = CeedVectorCreate(ceed, lsize, &xcorners); CHKERRQ(ierr);
4341e150236SLeila Ghaffari   ierr = VecGetArray(Xloc, &x); CHKERRQ(ierr);
4351e150236SLeila Ghaffari   CeedVectorSetArray(xcorners, CEED_MEM_HOST, CEED_USE_POINTER, x);
436ca3ac6ddSLeila Ghaffari   ierr = DMGetLabel(dm, "Face Sets", &domainLabel); CHKERRQ(ierr);
4377659d40cSLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
4384438636fSLeila Ghaffari 
4394438636fSLeila Ghaffari   if (wind_type == ADVECTION_WIND_TRANSLATION ) {
4404438636fSLeila Ghaffari     // Ignore wall and slip BCs
4414438636fSLeila Ghaffari     bc->nwall = 0;
4424438636fSLeila Ghaffari     bc->nslip[0] = bc->nslip[1] = bc->nslip[2] = 0;
4434438636fSLeila Ghaffari 
4444438636fSLeila Ghaffari     // Set number of faces
4457659d40cSLeila Ghaffari     if (dim == 2) nFace = 4;
4467659d40cSLeila Ghaffari     if (dim == 3) nFace = 6;
4479fe13df9SLeila Ghaffari 
4487659d40cSLeila Ghaffari     // Create CEED Operator for each boundary face
4494438636fSLeila Ghaffari     PetscInt localNelemSur[6];
4504438636fSLeila Ghaffari     CeedVector qdataSur[6];
4514438636fSLeila Ghaffari     CeedOperator op_setupSur[6], op_applySur[6];
4524438636fSLeila Ghaffari     CeedElemRestriction restrictxSur[6], restrictqSur[6], restrictqdiSur[6];
4537659d40cSLeila Ghaffari     for (CeedInt i=0; i<nFace; i++) {
4547659d40cSLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domainLabel, i+1, numP_Sur,
455f259b054Svaleriabarra                                      numQ_Sur, qdatasizeSur, &restrictqSur[i],
456f259b054Svaleriabarra                                      &restrictxSur[i], &restrictqdiSur[i]);
457f259b054Svaleriabarra       CHKERRQ(ierr);
4587659d40cSLeila Ghaffari       // Create the CEED vectors that will be needed in Boundary setup
4597659d40cSLeila Ghaffari       CeedElemRestrictionGetNumElements(restrictqSur[i], &localNelemSur[i]);
460f259b054Svaleriabarra       CeedVectorCreate(ceed, qdatasizeSur*localNelemSur[i]*NqptsSur,
461f259b054Svaleriabarra                        &qdataSur[i]);
4627659d40cSLeila Ghaffari       // Create the operator that builds the quadrature data for the Boundary operator
4637659d40cSLeila Ghaffari       CeedOperatorCreate(ceed, qf_setupSur, NULL, NULL, &op_setupSur[i]);
464ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "dx", restrictxSur[i], basisxSur,
465ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4667659d40cSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "weight", CEED_ELEMRESTRICTION_NONE,
467ca3ac6ddSLeila Ghaffari                            basisxSur, CEED_VECTOR_NONE);
4687659d40cSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "qdataSur", restrictqdiSur[i],
469ca3ac6ddSLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
4707659d40cSLeila Ghaffari       // Create Boundary operator
4717659d40cSLeila Ghaffari       CeedOperatorCreate(ceed, qf_applySur, NULL, NULL, &op_applySur[i]);
472ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "q", restrictqSur[i], basisqSur,
473ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4747659d40cSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "qdataSur", restrictqdiSur[i],
4757659d40cSLeila Ghaffari                            CEED_BASIS_COLLOCATED, qdataSur[i]);
476f259b054Svaleriabarra       CeedOperatorSetField(op_applySur[i], "x", restrictxSur[i], basisxSur,
477f259b054Svaleriabarra                            xcorners);
478ebb4b9bdSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "v", restrictqSur[i], basisqSur,
479ebb4b9bdSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
4807659d40cSLeila Ghaffari       // Apply CEED operator for Boundary setup
481ebb4b9bdSLeila Ghaffari       CeedOperatorApply(op_setupSur[i], xcorners, qdataSur[i],
482ebb4b9bdSLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
4834438636fSLeila Ghaffari       // --Apply Sub-Operator for the Boundary
4847659d40cSLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_applySur[i]);
4859fe13df9SLeila Ghaffari     }
4861e150236SLeila Ghaffari     CeedVectorDestroy(&xcorners);
487ca3ac6ddSLeila Ghaffari   }
4884438636fSLeila Ghaffari 
4894438636fSLeila Ghaffari   if (problemChoice == NS_EULER_VORTEX) {
490fae0d315SLeila Ghaffari     // All faces are slip, except for 5 and 6
4914438636fSLeila Ghaffari     bc->nwall = 0;
4924438636fSLeila Ghaffari     bc->nslip[0] = 0;
4934438636fSLeila Ghaffari     bc->nslip[1] = 2;     bc->slips[1][0] = 3; bc->slips[1][1] = 4;
4944438636fSLeila Ghaffari     bc->nslip[2] = 2;     bc->slips[2][0] = 1; bc->slips[2][1] = 2;
4954438636fSLeila Ghaffari 
4964438636fSLeila Ghaffari     // Create CEED Operator for each boundary face
4974438636fSLeila Ghaffari     PetscInt localNelemSur[2];
4984438636fSLeila Ghaffari     CeedVector qdataSur[2];
4994438636fSLeila Ghaffari     CeedQFunction qf_apply;
5004438636fSLeila Ghaffari     CeedOperator op_setupSur[2], op_applySur[2];
5014438636fSLeila Ghaffari     CeedElemRestriction restrictxSur[2], restrictqSur[2], restrictqdiSur[2];
5024438636fSLeila Ghaffari     for (CeedInt i=0; i<2; i++) {
5034438636fSLeila Ghaffari       if (i == 0) qf_apply = qf_applyIn;
5044438636fSLeila Ghaffari       if (i == 1) qf_apply = qf_applyOut;
5054438636fSLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domainLabel, i+5, numP_Sur,
5064438636fSLeila Ghaffari                                      numQ_Sur, qdatasizeSur, &restrictqSur[i],
5074438636fSLeila Ghaffari                                      &restrictxSur[i], &restrictqdiSur[i]);
5084438636fSLeila Ghaffari       CHKERRQ(ierr);
5094438636fSLeila Ghaffari       // Create the CEED vectors that will be needed in Boundary setup
5104438636fSLeila Ghaffari       CeedElemRestrictionGetNumElements(restrictqSur[i], &localNelemSur[i]);
5114438636fSLeila Ghaffari       CeedVectorCreate(ceed, qdatasizeSur*localNelemSur[i]*NqptsSur,
5124438636fSLeila Ghaffari                        &qdataSur[i]);
5134438636fSLeila Ghaffari       // Create the operator that builds the quadrature data for the Boundary operator
5144438636fSLeila Ghaffari       CeedOperatorCreate(ceed, qf_setupSur, NULL, NULL, &op_setupSur[i]);
5154438636fSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "dx", restrictxSur[i], basisxSur,
5164438636fSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
5174438636fSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "weight", CEED_ELEMRESTRICTION_NONE,
5184438636fSLeila Ghaffari                            basisxSur, CEED_VECTOR_NONE);
5194438636fSLeila Ghaffari       CeedOperatorSetField(op_setupSur[i], "qdataSur", restrictqdiSur[i],
5204438636fSLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
5214438636fSLeila Ghaffari       // Create Boundary operator
5224438636fSLeila Ghaffari       CeedOperatorCreate(ceed, qf_apply, NULL, NULL, &op_applySur[i]);
5234438636fSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "q", restrictqSur[i], basisqSur,
5244438636fSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
5254438636fSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "qdataSur", restrictqdiSur[i],
5264438636fSLeila Ghaffari                            CEED_BASIS_COLLOCATED, qdataSur[i]);
5274438636fSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "x", restrictxSur[i], basisxSur,
5284438636fSLeila Ghaffari                            xcorners);
5294438636fSLeila Ghaffari       CeedOperatorSetField(op_applySur[i], "v", restrictqSur[i], basisqSur,
5304438636fSLeila Ghaffari                            CEED_VECTOR_ACTIVE);
5314438636fSLeila Ghaffari       // Apply CEED operator for Boundary setup
5324438636fSLeila Ghaffari       CeedOperatorApply(op_setupSur[i], xcorners, qdataSur[i],
5334438636fSLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
5344438636fSLeila Ghaffari       // --Apply Sub-Operator for the Boundary
5354438636fSLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_applySur[i]);
5364438636fSLeila Ghaffari     }
5374438636fSLeila Ghaffari     CeedVectorDestroy(&xcorners);
5384438636fSLeila Ghaffari 
5394438636fSLeila Ghaffari   }
540ca3ac6ddSLeila Ghaffari   PetscFunctionReturn(0);
541ca3ac6ddSLeila Ghaffari }
542ca3ac6ddSLeila Ghaffari 
543ccaff030SJeremy L Thompson static int CreateVectorFromPetscVec(Ceed ceed, Vec p, CeedVector *v) {
544ccaff030SJeremy L Thompson   PetscErrorCode ierr;
545ccaff030SJeremy L Thompson   PetscInt m;
546ccaff030SJeremy L Thompson 
547ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
548ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &m); CHKERRQ(ierr);
549ccaff030SJeremy L Thompson   ierr = CeedVectorCreate(ceed, m, v); CHKERRQ(ierr);
550ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
551ccaff030SJeremy L Thompson }
552ccaff030SJeremy L Thompson 
553ccaff030SJeremy L Thompson static int VectorPlacePetscVec(CeedVector c, Vec p) {
554ccaff030SJeremy L Thompson   PetscErrorCode ierr;
555ccaff030SJeremy L Thompson   PetscInt mceed, mpetsc;
556ccaff030SJeremy L Thompson   PetscScalar *a;
557ccaff030SJeremy L Thompson 
558ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
559ccaff030SJeremy L Thompson   ierr = CeedVectorGetLength(c, &mceed); CHKERRQ(ierr);
560ccaff030SJeremy L Thompson   ierr = VecGetLocalSize(p, &mpetsc); CHKERRQ(ierr);
561ccaff030SJeremy L Thompson   if (mceed != mpetsc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
56284d34d69SLeila Ghaffari                                   "Cannot place PETSc Vec of length %D in CeedVector of length %D",
56384d34d69SLeila Ghaffari                                   mpetsc, mceed);
564ccaff030SJeremy L Thompson   ierr = VecGetArray(p, &a); CHKERRQ(ierr);
565ccaff030SJeremy L Thompson   CeedVectorSetArray(c, CEED_MEM_HOST, CEED_USE_POINTER, a);
566ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
567ccaff030SJeremy L Thompson }
568ccaff030SJeremy L Thompson 
569ccaff030SJeremy L Thompson static PetscErrorCode DMPlexInsertBoundaryValues_NS(DM dm,
570ccaff030SJeremy L Thompson     PetscBool insertEssential, Vec Qloc, PetscReal time, Vec faceGeomFVM,
571ccaff030SJeremy L Thompson     Vec cellGeomFVM, Vec gradFVM) {
572ccaff030SJeremy L Thompson   PetscErrorCode ierr;
573ccaff030SJeremy L Thompson   Vec Qbc;
574ccaff030SJeremy L Thompson 
575ccaff030SJeremy L Thompson   PetscFunctionBegin;
576ccaff030SJeremy L Thompson   ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
577ccaff030SJeremy L Thompson   ierr = VecAXPY(Qloc, 1., Qbc); CHKERRQ(ierr);
578ccaff030SJeremy L Thompson   ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
579ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
580ccaff030SJeremy L Thompson }
581ccaff030SJeremy L Thompson 
582ccaff030SJeremy L Thompson // This is the RHS of the ODE, given as u_t = G(t,u)
583ccaff030SJeremy L Thompson // This function takes in a state vector Q and writes into G
584ccaff030SJeremy L Thompson static PetscErrorCode RHS_NS(TS ts, PetscReal t, Vec Q, Vec G, void *userData) {
585ccaff030SJeremy L Thompson   PetscErrorCode ierr;
586ccaff030SJeremy L Thompson   User user = *(User *)userData;
587ccaff030SJeremy L Thompson   PetscScalar *q, *g;
588ccaff030SJeremy L Thompson   Vec Qloc, Gloc;
589ccaff030SJeremy L Thompson 
590ccaff030SJeremy L Thompson   // Global-to-local
591ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
592ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
593ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
594ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
595ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
596ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
597ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
598ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
599ccaff030SJeremy L Thompson 
600ccaff030SJeremy L Thompson   // Ceed Vectors
601ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
602ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
603ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER, q);
604ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
605ccaff030SJeremy L Thompson 
606ccaff030SJeremy L Thompson   // Apply CEED operator
607ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_rhs, user->qceed, user->gceed,
608ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
609ccaff030SJeremy L Thompson 
610ccaff030SJeremy L Thompson   // Restore vectors
611ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, (const PetscScalar **)&q); CHKERRQ(ierr);
612ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
613ccaff030SJeremy L Thompson 
614ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
615ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
616ccaff030SJeremy L Thompson 
617ccaff030SJeremy L Thompson   // Inverse of the lumped mass matrix
618ccaff030SJeremy L Thompson   ierr = VecPointwiseMult(G, G, user->M); // M is Minv
619ccaff030SJeremy L Thompson   CHKERRQ(ierr);
620ccaff030SJeremy L Thompson 
621ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
622ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
623ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
624ccaff030SJeremy L Thompson }
625ccaff030SJeremy L Thompson 
626ccaff030SJeremy L Thompson static PetscErrorCode IFunction_NS(TS ts, PetscReal t, Vec Q, Vec Qdot, Vec G,
627ccaff030SJeremy L Thompson                                    void *userData) {
628ccaff030SJeremy L Thompson   PetscErrorCode ierr;
629ccaff030SJeremy L Thompson   User user = *(User *)userData;
630ccaff030SJeremy L Thompson   const PetscScalar *q, *qdot;
631ccaff030SJeremy L Thompson   PetscScalar *g;
632ccaff030SJeremy L Thompson   Vec Qloc, Qdotloc, Gloc;
633ccaff030SJeremy L Thompson 
634ccaff030SJeremy L Thompson   // Global-to-local
635ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
636ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
637ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
638ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
639ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
640ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
641ccaff030SJeremy L Thompson   ierr = DMPlexInsertBoundaryValues(user->dm, PETSC_TRUE, Qloc, 0.0,
642ccaff030SJeremy L Thompson                                     NULL, NULL, NULL); CHKERRQ(ierr);
643ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qdotloc); CHKERRQ(ierr);
644ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Qdot, INSERT_VALUES, Qdotloc); CHKERRQ(ierr);
645ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Gloc); CHKERRQ(ierr);
646ccaff030SJeremy L Thompson 
647ccaff030SJeremy L Thompson   // Ceed Vectors
648ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qloc, &q); CHKERRQ(ierr);
649ccaff030SJeremy L Thompson   ierr = VecGetArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
650ccaff030SJeremy L Thompson   ierr = VecGetArray(Gloc, &g); CHKERRQ(ierr);
651ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qceed, CEED_MEM_HOST, CEED_USE_POINTER,
652ccaff030SJeremy L Thompson                      (PetscScalar *)q);
653ccaff030SJeremy L Thompson   CeedVectorSetArray(user->qdotceed, CEED_MEM_HOST, CEED_USE_POINTER,
654ccaff030SJeremy L Thompson                      (PetscScalar *)qdot);
655ccaff030SJeremy L Thompson   CeedVectorSetArray(user->gceed, CEED_MEM_HOST, CEED_USE_POINTER, g);
656ccaff030SJeremy L Thompson 
657ccaff030SJeremy L Thompson   // Apply CEED operator
658ccaff030SJeremy L Thompson   CeedOperatorApply(user->op_ifunction, user->qceed, user->gceed,
659ccaff030SJeremy L Thompson                     CEED_REQUEST_IMMEDIATE);
660ccaff030SJeremy L Thompson 
661ccaff030SJeremy L Thompson   // Restore vectors
662ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qloc, &q); CHKERRQ(ierr);
663ccaff030SJeremy L Thompson   ierr = VecRestoreArrayRead(Qdotloc, &qdot); CHKERRQ(ierr);
664ccaff030SJeremy L Thompson   ierr = VecRestoreArray(Gloc, &g); CHKERRQ(ierr);
665ccaff030SJeremy L Thompson 
666ccaff030SJeremy L Thompson   ierr = VecZeroEntries(G); CHKERRQ(ierr);
667ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(user->dm, Gloc, ADD_VALUES, G); CHKERRQ(ierr);
668ccaff030SJeremy L Thompson 
669ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
670ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qdotloc); CHKERRQ(ierr);
671ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Gloc); CHKERRQ(ierr);
672ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
673ccaff030SJeremy L Thompson }
674ccaff030SJeremy L Thompson 
675ccaff030SJeremy L Thompson // User provided TS Monitor
676ccaff030SJeremy L Thompson static PetscErrorCode TSMonitor_NS(TS ts, PetscInt stepno, PetscReal time,
677ccaff030SJeremy L Thompson                                    Vec Q, void *ctx) {
678ccaff030SJeremy L Thompson   User user = ctx;
679ccaff030SJeremy L Thompson   Vec Qloc;
680ccaff030SJeremy L Thompson   char filepath[PETSC_MAX_PATH_LEN];
681ccaff030SJeremy L Thompson   PetscViewer viewer;
682ccaff030SJeremy L Thompson   PetscErrorCode ierr;
683ccaff030SJeremy L Thompson 
684ccaff030SJeremy L Thompson   // Set up output
685ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
686ccaff030SJeremy L Thompson   // Print every 'outputfreq' steps
687ccaff030SJeremy L Thompson   if (stepno % user->outputfreq != 0)
688ccaff030SJeremy L Thompson     PetscFunctionReturn(0);
689ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
690ccaff030SJeremy L Thompson   ierr = PetscObjectSetName((PetscObject)Qloc, "StateVec"); CHKERRQ(ierr);
691ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
692ccaff030SJeremy L Thompson   ierr = DMGlobalToLocal(user->dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
693ccaff030SJeremy L Thompson 
694ccaff030SJeremy L Thompson   // Output
695ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-%03D.vtu",
696d99129b9SLeila Ghaffari                        user->outputdir, stepno + user->contsteps);
697ccaff030SJeremy L Thompson   CHKERRQ(ierr);
698ccaff030SJeremy L Thompson   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Q), filepath,
699ccaff030SJeremy L Thompson                             FILE_MODE_WRITE, &viewer); CHKERRQ(ierr);
700ccaff030SJeremy L Thompson   ierr = VecView(Qloc, viewer); CHKERRQ(ierr);
7019d801c56SJed Brown   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
702ccaff030SJeremy L Thompson   if (user->dmviz) {
703ccaff030SJeremy L Thompson     Vec Qrefined, Qrefined_loc;
704ccaff030SJeremy L Thompson     char filepath_refined[PETSC_MAX_PATH_LEN];
705ccaff030SJeremy L Thompson     PetscViewer viewer_refined;
706ccaff030SJeremy L Thompson 
707ccaff030SJeremy L Thompson     ierr = DMGetGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
708ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
709ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)Qrefined_loc, "Refined");
710ccaff030SJeremy L Thompson     CHKERRQ(ierr);
711ccaff030SJeremy L Thompson     ierr = MatInterpolate(user->interpviz, Q, Qrefined); CHKERRQ(ierr);
712ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qrefined_loc); CHKERRQ(ierr);
713ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(user->dmviz, Qrefined, INSERT_VALUES, Qrefined_loc);
714ccaff030SJeremy L Thompson     CHKERRQ(ierr);
715ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath_refined, sizeof filepath_refined,
716ccaff030SJeremy L Thompson                          "%s/nsrefined-%03D.vtu",
717d99129b9SLeila Ghaffari                          user->outputdir, stepno + user->contsteps);
718ccaff030SJeremy L Thompson     CHKERRQ(ierr);
719ccaff030SJeremy L Thompson     ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)Qrefined),
720ccaff030SJeremy L Thompson                               filepath_refined,
721ccaff030SJeremy L Thompson                               FILE_MODE_WRITE, &viewer_refined); CHKERRQ(ierr);
722ccaff030SJeremy L Thompson     ierr = VecView(Qrefined_loc, viewer_refined); CHKERRQ(ierr);
723ccaff030SJeremy L Thompson     ierr = DMRestoreLocalVector(user->dmviz, &Qrefined_loc); CHKERRQ(ierr);
724ccaff030SJeremy L Thompson     ierr = DMRestoreGlobalVector(user->dmviz, &Qrefined); CHKERRQ(ierr);
725ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer_refined); CHKERRQ(ierr);
726ccaff030SJeremy L Thompson   }
727ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(user->dm, &Qloc); CHKERRQ(ierr);
728ccaff030SJeremy L Thompson 
729ccaff030SJeremy L Thompson   // Save data in a binary file for continuation of simulations
730ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
731d99129b9SLeila Ghaffari                        user->outputdir); CHKERRQ(ierr);
732ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
733ccaff030SJeremy L Thompson   CHKERRQ(ierr);
734ccaff030SJeremy L Thompson   ierr = VecView(Q, viewer); CHKERRQ(ierr);
735ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
736ccaff030SJeremy L Thompson 
737ccaff030SJeremy L Thompson   // Save time stamp
738ccaff030SJeremy L Thompson   // Dimensionalize time back
739ccaff030SJeremy L Thompson   time /= user->units->second;
740ccaff030SJeremy L Thompson   ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
741d99129b9SLeila Ghaffari                        user->outputdir); CHKERRQ(ierr);
742ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryOpen(user->comm, filepath, FILE_MODE_WRITE, &viewer);
743ccaff030SJeremy L Thompson   CHKERRQ(ierr);
744ccaff030SJeremy L Thompson   #if PETSC_VERSION_GE(3,13,0)
745ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL);
746ccaff030SJeremy L Thompson   #else
747ccaff030SJeremy L Thompson   ierr = PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL, true);
748ccaff030SJeremy L Thompson   #endif
749ccaff030SJeremy L Thompson   CHKERRQ(ierr);
750ccaff030SJeremy L Thompson   ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
751ccaff030SJeremy L Thompson 
752ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
753ccaff030SJeremy L Thompson }
754ccaff030SJeremy L Thompson 
75584d34d69SLeila Ghaffari static PetscErrorCode ICs_FixMultiplicity(CeedOperator op_ics,
756ccaff030SJeremy L Thompson     CeedVector xcorners, CeedVector q0ceed, DM dm, Vec Qloc, Vec Q,
757777ff853SJeremy L Thompson     CeedElemRestriction restrictq, CeedQFunctionContext ctxSetup, CeedScalar time) {
758ccaff030SJeremy L Thompson   PetscErrorCode ierr;
759ccaff030SJeremy L Thompson   CeedVector multlvec;
760ccaff030SJeremy L Thompson   Vec Multiplicity, MultiplicityLoc;
761ccaff030SJeremy L Thompson 
762777ff853SJeremy L Thompson   SetupContext ctxSetupData;
763777ff853SJeremy L Thompson   CeedQFunctionContextGetData(ctxSetup, CEED_MEM_HOST, (void **)&ctxSetupData);
764777ff853SJeremy L Thompson   ctxSetupData->time = time;
765777ff853SJeremy L Thompson   CeedQFunctionContextRestoreData(ctxSetup, (void **)&ctxSetupData);
766777ff853SJeremy L Thompson 
767ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
768ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
769ccaff030SJeremy L Thompson   CeedOperatorApply(op_ics, xcorners, q0ceed, CEED_REQUEST_IMMEDIATE);
770ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
771ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Qloc, ADD_VALUES, Q); CHKERRQ(ierr);
772ccaff030SJeremy L Thompson 
773ccaff030SJeremy L Thompson   // Fix multiplicity for output of ICs
774ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
775ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &multlvec, NULL);
776ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(multlvec, MultiplicityLoc); CHKERRQ(ierr);
777ccaff030SJeremy L Thompson   CeedElemRestrictionGetMultiplicity(restrictq, multlvec);
778ccaff030SJeremy L Thompson   CeedVectorDestroy(&multlvec);
779ccaff030SJeremy L Thompson   ierr = DMGetGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
780ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Multiplicity); CHKERRQ(ierr);
781ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, MultiplicityLoc, ADD_VALUES, Multiplicity);
782ccaff030SJeremy L Thompson   CHKERRQ(ierr);
783ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Q, Q, Multiplicity); CHKERRQ(ierr);
784ccaff030SJeremy L Thompson   ierr = VecPointwiseDivide(Qloc, Qloc, MultiplicityLoc); CHKERRQ(ierr);
785ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &MultiplicityLoc); CHKERRQ(ierr);
786ccaff030SJeremy L Thompson   ierr = DMRestoreGlobalVector(dm, &Multiplicity); CHKERRQ(ierr);
787ccaff030SJeremy L Thompson 
788ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
789ccaff030SJeremy L Thompson }
790ccaff030SJeremy L Thompson 
791ccaff030SJeremy L Thompson static PetscErrorCode ComputeLumpedMassMatrix(Ceed ceed, DM dm,
792ccaff030SJeremy L Thompson     CeedElemRestriction restrictq, CeedBasis basisq,
793ccaff030SJeremy L Thompson     CeedElemRestriction restrictqdi, CeedVector qdata, Vec M) {
794ccaff030SJeremy L Thompson   PetscErrorCode ierr;
795ccaff030SJeremy L Thompson   CeedQFunction qf_mass;
796ccaff030SJeremy L Thompson   CeedOperator op_mass;
797ccaff030SJeremy L Thompson   CeedVector mceed;
798ccaff030SJeremy L Thompson   Vec Mloc;
799ccaff030SJeremy L Thompson   CeedInt ncompq, qdatasize;
800ccaff030SJeremy L Thompson 
801ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
802ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictq, &ncompq);
803ccaff030SJeremy L Thompson   CeedElemRestrictionGetNumComponents(restrictqdi, &qdatasize);
804ccaff030SJeremy L Thompson   // Create the Q-function that defines the action of the mass operator
805ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, Mass, Mass_loc, &qf_mass);
806ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q", ncompq, CEED_EVAL_INTERP);
807ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "qdata", qdatasize, CEED_EVAL_NONE);
808ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", ncompq, CEED_EVAL_INTERP);
809ccaff030SJeremy L Thompson 
810ccaff030SJeremy L Thompson   // Create the mass operator
811ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
812ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
813ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "qdata", restrictqdi,
814ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, qdata);
815ccaff030SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
816ccaff030SJeremy L Thompson 
817ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Mloc); CHKERRQ(ierr);
818ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Mloc); CHKERRQ(ierr);
819ccaff030SJeremy L Thompson   CeedElemRestrictionCreateVector(restrictq, &mceed, NULL);
820ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(mceed, Mloc); CHKERRQ(ierr);
821ccaff030SJeremy L Thompson 
822ccaff030SJeremy L Thompson   {
823ccaff030SJeremy L Thompson     // Compute a lumped mass matrix
824ccaff030SJeremy L Thompson     CeedVector onesvec;
825ccaff030SJeremy L Thompson     CeedElemRestrictionCreateVector(restrictq, &onesvec, NULL);
826ccaff030SJeremy L Thompson     CeedVectorSetValue(onesvec, 1.0);
827ccaff030SJeremy L Thompson     CeedOperatorApply(op_mass, onesvec, mceed, CEED_REQUEST_IMMEDIATE);
828ccaff030SJeremy L Thompson     CeedVectorDestroy(&onesvec);
829ccaff030SJeremy L Thompson     CeedOperatorDestroy(&op_mass);
830ccaff030SJeremy L Thompson     CeedVectorDestroy(&mceed);
831ccaff030SJeremy L Thompson   }
832ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
833ccaff030SJeremy L Thompson 
834ccaff030SJeremy L Thompson   ierr = VecZeroEntries(M); CHKERRQ(ierr);
835ccaff030SJeremy L Thompson   ierr = DMLocalToGlobal(dm, Mloc, ADD_VALUES, M); CHKERRQ(ierr);
836ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Mloc); CHKERRQ(ierr);
837ccaff030SJeremy L Thompson 
838ccaff030SJeremy L Thompson   // Invert diagonally lumped mass vector for RHS function
839ccaff030SJeremy L Thompson   ierr = VecReciprocal(M); CHKERRQ(ierr);
840ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
841ccaff030SJeremy L Thompson }
842ccaff030SJeremy L Thompson 
84384d34d69SLeila Ghaffari static PetscErrorCode SetUpDM(DM dm, problemData *problem, PetscInt degree,
844777ff853SJeremy L Thompson                               SimpleBC bc, void *ctxSetupData) {
845ccaff030SJeremy L Thompson   PetscErrorCode ierr;
846ccaff030SJeremy L Thompson 
847ccaff030SJeremy L Thompson   PetscFunctionBeginUser;
848ccaff030SJeremy L Thompson   {
849ccaff030SJeremy L Thompson     // Configure the finite element space and boundary conditions
850ccaff030SJeremy L Thompson     PetscFE fe;
851ccaff030SJeremy L Thompson     PetscInt ncompq = 5;
852ff6701fcSJed Brown     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, ncompq,
853ff6701fcSJed Brown                                  PETSC_FALSE, degree, PETSC_DECIDE,
85432ed2d11SJed Brown                                  &fe); CHKERRQ(ierr);
855ccaff030SJeremy L Thompson     ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
856ccaff030SJeremy L Thompson     ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr);
857ccaff030SJeremy L Thompson     ierr = DMCreateDS(dm); CHKERRQ(ierr);
85807af6069Svaleriabarra     {
85907af6069Svaleriabarra       PetscInt comps[1] = {1};
86007af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", "Face Sets", 0,
8613ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[0],
862777ff853SJeremy L Thompson                            bc->slips[0], ctxSetupData); CHKERRQ(ierr);
86307af6069Svaleriabarra       comps[0] = 2;
86407af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", "Face Sets", 0,
8653ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[1],
866777ff853SJeremy L Thompson                            bc->slips[1], ctxSetupData); CHKERRQ(ierr);
86707af6069Svaleriabarra       comps[0] = 3;
86807af6069Svaleriabarra       ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", "Face Sets", 0,
8693ab4fca6SValeria Barra                            1, comps, (void(*)(void))NULL, NULL, bc->nslip[2],
870777ff853SJeremy L Thompson                            bc->slips[2], ctxSetupData); CHKERRQ(ierr);
87107af6069Svaleriabarra     }
87284d34d69SLeila Ghaffari     if (bc->userbc == PETSC_TRUE) {
87384d34d69SLeila Ghaffari       for (PetscInt c = 0; c < 3; c++) {
87484d34d69SLeila Ghaffari         for (PetscInt s = 0; s < bc->nslip[c]; s++) {
87584d34d69SLeila Ghaffari           for (PetscInt w = 0; w < bc->nwall; w++) {
87684d34d69SLeila Ghaffari             if (bc->slips[c][s] == bc->walls[w])
87784d34d69SLeila Ghaffari               SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
878f259b054Svaleriabarra                        "Boundary condition already set on face %D!\n",
879f259b054Svaleriabarra                        bc->walls[w]);
88084d34d69SLeila Ghaffari 
88184d34d69SLeila Ghaffari           }
88284d34d69SLeila Ghaffari         }
88384d34d69SLeila Ghaffari       }
88484d34d69SLeila Ghaffari     }
88584d34d69SLeila Ghaffari     // Wall boundary conditions are zero energy density and zero flux for
88684d34d69SLeila Ghaffari     //   velocity in advection/advection2d, and zero velocity and zero flux
88784d34d69SLeila Ghaffari     //   for mass density and energy density in density_current
88884d34d69SLeila Ghaffari     {
88984d34d69SLeila Ghaffari       if (problem->bc == Exact_Advection || problem->bc == Exact_Advection2d) {
89084d34d69SLeila Ghaffari         PetscInt comps[1] = {4};
89184d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
8923ab4fca6SValeria Barra                              1, comps, (void(*)(void))problem->bc, NULL,
893*e5ed8c30SLeila Ghaffari                              bc->nwall, bc->walls, ctxSetup); CHKERRQ(ierr);
894*e5ed8c30SLeila Ghaffari       } else if (problem->bc == Exact_DC || problem->bc == Exact_Euler) {
89584d34d69SLeila Ghaffari         PetscInt comps[3] = {1, 2, 3};
89684d34d69SLeila Ghaffari         ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", "Face Sets", 0,
8973ab4fca6SValeria Barra                              3, comps, (void(*)(void))problem->bc, NULL,
898777ff853SJeremy L Thompson                              bc->nwall, bc->walls, ctxSetupData); CHKERRQ(ierr);
89984d34d69SLeila Ghaffari       } else
90084d34d69SLeila Ghaffari         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
90184d34d69SLeila Ghaffari                 "Undefined boundary conditions for this problem");
90284d34d69SLeila Ghaffari     }
903ccaff030SJeremy L Thompson     ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
904ccaff030SJeremy L Thompson     CHKERRQ(ierr);
905ccaff030SJeremy L Thompson     ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
906ccaff030SJeremy L Thompson   }
907ccaff030SJeremy L Thompson   {
908ccaff030SJeremy L Thompson     // Empty name for conserved field (because there is only one field)
909ccaff030SJeremy L Thompson     PetscSection section;
910ccaff030SJeremy L Thompson     ierr = DMGetLocalSection(dm, &section); CHKERRQ(ierr);
911ccaff030SJeremy L Thompson     ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
912ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 0, "Density");
913ccaff030SJeremy L Thompson     CHKERRQ(ierr);
914ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 1, "MomentumX");
915ccaff030SJeremy L Thompson     CHKERRQ(ierr);
916ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 2, "MomentumY");
917ccaff030SJeremy L Thompson     CHKERRQ(ierr);
918ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 3, "MomentumZ");
919ccaff030SJeremy L Thompson     CHKERRQ(ierr);
920ccaff030SJeremy L Thompson     ierr = PetscSectionSetComponentName(section, 0, 4, "EnergyDensity");
921ccaff030SJeremy L Thompson     CHKERRQ(ierr);
922ccaff030SJeremy L Thompson   }
923ccaff030SJeremy L Thompson   PetscFunctionReturn(0);
924ccaff030SJeremy L Thompson }
925ccaff030SJeremy L Thompson 
926ccaff030SJeremy L Thompson int main(int argc, char **argv) {
927ccaff030SJeremy L Thompson   PetscInt ierr;
928ccaff030SJeremy L Thompson   MPI_Comm comm;
92984d34d69SLeila Ghaffari   DM dm, dmcoord, dmviz;
930ccaff030SJeremy L Thompson   Mat interpviz;
931ccaff030SJeremy L Thompson   TS ts;
932ccaff030SJeremy L Thompson   TSAdapt adapt;
933ccaff030SJeremy L Thompson   User user;
934ccaff030SJeremy L Thompson   Units units;
935ccaff030SJeremy L Thompson   char ceedresource[4096] = "/cpu/self";
93684d34d69SLeila Ghaffari   PetscInt localNelemVol, lnodes, gnodes, steps;
937ccaff030SJeremy L Thompson   const PetscInt ncompq = 5;
938ccaff030SJeremy L Thompson   PetscMPIInt rank;
939ccaff030SJeremy L Thompson   PetscScalar ftime;
940ccaff030SJeremy L Thompson   Vec Q, Qloc, Xloc;
941ccaff030SJeremy L Thompson   Ceed ceed;
94284d34d69SLeila Ghaffari   CeedInt numP, numQ;
943cfa64770SLeila Ghaffari   CeedVector xcorners, qdata, q0ceed;
94484d34d69SLeila Ghaffari   CeedBasis basisx, basisxc, basisq;
94584d34d69SLeila Ghaffari   CeedElemRestriction restrictx, restrictq, restrictqdi;
946cfa64770SLeila Ghaffari   CeedQFunction qf_setupVol, qf_ics, qf_rhsVol, qf_ifunctionVol;
947777ff853SJeremy L Thompson   CeedQFunctionContext ctxSetup, ctxNS, ctxAdvection2d, ctxSurface;
948cfa64770SLeila Ghaffari   CeedOperator op_setupVol, op_ics;
949ccaff030SJeremy L Thompson   CeedScalar Rd;
95084d34d69SLeila Ghaffari   CeedMemType memtyperequested;
951ccaff030SJeremy L Thompson   PetscScalar WpermK, Pascal, JperkgK, mpersquareds, kgpercubicm,
95216c0476cSLeila Ghaffari               kgpersquaredms, Joulepercubicm, Joule;
953ccaff030SJeremy L Thompson   problemType problemChoice;
954ccaff030SJeremy L Thompson   problemData *problem = NULL;
9551184866aSLeila Ghaffari   WindType wind_type;
956ccaff030SJeremy L Thompson   StabilizationType stab;
95784d34d69SLeila Ghaffari   PetscBool implicit;
958cb3e2689Svaleriabarra   PetscInt    viz_refine = 0;
959ccaff030SJeremy L Thompson   struct SimpleBC_ bc = {
96084d34d69SLeila Ghaffari     .nslip = {2, 2, 2},
96184d34d69SLeila Ghaffari     .slips = {{5, 6}, {3, 4}, {1, 2}}
962ccaff030SJeremy L Thompson   };
963ccaff030SJeremy L Thompson   double start, cpu_time_used;
964dc8efd83SLeila Ghaffari   // Test variables
965dc8efd83SLeila Ghaffari   PetscBool test;
966dc8efd83SLeila Ghaffari   PetscScalar testtol = 0.;
967dc8efd83SLeila Ghaffari   char filepath[PETSC_MAX_PATH_LEN];
96884d34d69SLeila Ghaffari   // Check PETSc CUDA support
96984d34d69SLeila Ghaffari   PetscBool petschavecuda, setmemtyperequest = PETSC_FALSE;
97084d34d69SLeila Ghaffari   // *INDENT-OFF*
97184d34d69SLeila Ghaffari   #ifdef PETSC_HAVE_CUDA
97284d34d69SLeila Ghaffari   petschavecuda = PETSC_TRUE;
97384d34d69SLeila Ghaffari   #else
97484d34d69SLeila Ghaffari   petschavecuda = PETSC_FALSE;
97584d34d69SLeila Ghaffari   #endif
97684d34d69SLeila Ghaffari   // *INDENT-ON*
977ccaff030SJeremy L Thompson 
978ccaff030SJeremy L Thompson   // Create the libCEED contexts
979ccaff030SJeremy L Thompson   PetscScalar meter          = 1e-2;     // 1 meter in scaled length units
980ccaff030SJeremy L Thompson   PetscScalar second         = 1e-2;     // 1 second in scaled time units
981ccaff030SJeremy L Thompson   PetscScalar kilogram       = 1e-6;     // 1 kilogram in scaled mass units
982ccaff030SJeremy L Thompson   PetscScalar Kelvin         = 1;        // 1 Kelvin in scaled temperature units
983ccaff030SJeremy L Thompson   CeedScalar theta0          = 300.;     // K
984ccaff030SJeremy L Thompson   CeedScalar thetaC          = -15.;     // K
985ccaff030SJeremy L Thompson   CeedScalar P0              = 1.e5;     // Pa
98616c0476cSLeila Ghaffari   CeedScalar E_wind          = 1.e6;     // J
987ccaff030SJeremy L Thompson   CeedScalar N               = 0.01;     // 1/s
988ccaff030SJeremy L Thompson   CeedScalar cv              = 717.;     // J/(kg K)
989ccaff030SJeremy L Thompson   CeedScalar cp              = 1004.;    // J/(kg K)
990011314a7SLeila Ghaffari   CeedScalar vortex_strength = 5.;       // -
991b9c3f7b3SLeila Ghaffari   CeedScalar rho_enter       = 1.2;      // Kg/m^3
992ccaff030SJeremy L Thompson   CeedScalar g               = 9.81;     // m/s^2
993ccaff030SJeremy L Thompson   CeedScalar lambda          = -2./3.;   // -
994ccaff030SJeremy L Thompson   CeedScalar mu              = 75.;      // Pa s, dynamic viscosity
995ccaff030SJeremy L Thompson   // mu = 75 is not physical for air, but is good for numerical stability
996ccaff030SJeremy L Thompson   CeedScalar k               = 0.02638;  // W/(m K)
997ccaff030SJeremy L Thompson   CeedScalar CtauS           = 0.;       // dimensionless
998ccaff030SJeremy L Thompson   CeedScalar strong_form     = 0.;       // [0,1]
999ccaff030SJeremy L Thompson   PetscScalar lx             = 8000.;    // m
1000ccaff030SJeremy L Thompson   PetscScalar ly             = 8000.;    // m
1001ccaff030SJeremy L Thompson   PetscScalar lz             = 4000.;    // m
1002ccaff030SJeremy L Thompson   CeedScalar rc              = 1000.;    // m (Radius of bubble)
1003ccaff030SJeremy L Thompson   PetscScalar resx           = 1000.;    // m (resolution in x)
1004ccaff030SJeremy L Thompson   PetscScalar resy           = 1000.;    // m (resolution in y)
1005ccaff030SJeremy L Thompson   PetscScalar resz           = 1000.;    // m (resolution in z)
1006ccaff030SJeremy L Thompson   PetscInt outputfreq        = 10;       // -
1007ccaff030SJeremy L Thompson   PetscInt contsteps         = 0;        // -
100884d34d69SLeila Ghaffari   PetscInt degree            = 1;        // -
100984d34d69SLeila Ghaffari   PetscInt qextra            = 2;        // -
1010ea6e0f84SLeila Ghaffari   PetscInt qextraSur         = 2;        // -
1011b9c3f7b3SLeila Ghaffari   PetscReal center[3], dc_axis[3] = {0, 0, 0}, wind[3] = {1., 0, 0},
1012*e5ed8c30SLeila Ghaffari             u_enter[3] = {2., 0, 0};
1013ccaff030SJeremy L Thompson 
1014ccaff030SJeremy L Thompson   ierr = PetscInitialize(&argc, &argv, NULL, help);
1015ccaff030SJeremy L Thompson   if (ierr) return ierr;
1016ccaff030SJeremy L Thompson 
1017ccaff030SJeremy L Thompson   // Allocate PETSc context
1018ccaff030SJeremy L Thompson   ierr = PetscCalloc1(1, &user); CHKERRQ(ierr);
1019ccaff030SJeremy L Thompson   ierr = PetscMalloc1(1, &units); CHKERRQ(ierr);
1020ccaff030SJeremy L Thompson 
1021ccaff030SJeremy L Thompson   // Parse command line options
1022ccaff030SJeremy L Thompson   comm = PETSC_COMM_WORLD;
1023ccaff030SJeremy L Thompson   ierr = PetscOptionsBegin(comm, NULL, "Navier-Stokes in PETSc with libCEED",
1024ccaff030SJeremy L Thompson                            NULL); CHKERRQ(ierr);
1025ccaff030SJeremy L Thompson   ierr = PetscOptionsString("-ceed", "CEED resource specifier",
1026ccaff030SJeremy L Thompson                             NULL, ceedresource, ceedresource,
1027ccaff030SJeremy L Thompson                             sizeof(ceedresource), NULL); CHKERRQ(ierr);
1028dc8efd83SLeila Ghaffari   ierr = PetscOptionsBool("-test", "Run in test mode",
1029dc8efd83SLeila Ghaffari                           NULL, test=PETSC_FALSE, &test, NULL); CHKERRQ(ierr);
1030dc8efd83SLeila Ghaffari   ierr = PetscOptionsScalar("-compare_final_state_atol",
1031dc8efd83SLeila Ghaffari                             "Test absolute tolerance",
1032dc8efd83SLeila Ghaffari                             NULL, testtol, &testtol, NULL); CHKERRQ(ierr);
1033dc8efd83SLeila Ghaffari   ierr = PetscOptionsString("-compare_final_state_filename", "Test filename",
1034dc8efd83SLeila Ghaffari                             NULL, filepath, filepath,
1035dc8efd83SLeila Ghaffari                             sizeof(filepath), NULL); CHKERRQ(ierr);
1036ccaff030SJeremy L Thompson   problemChoice = NS_DENSITY_CURRENT;
1037ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-problem", "Problem to solve", NULL,
1038ccaff030SJeremy L Thompson                           problemTypes, (PetscEnum)problemChoice,
1039ccaff030SJeremy L Thompson                           (PetscEnum *)&problemChoice, NULL); CHKERRQ(ierr);
1040ccaff030SJeremy L Thompson   problem = &problemOptions[problemChoice];
10411184866aSLeila Ghaffari   ierr = PetscOptionsEnum("-problem_advection_wind", "Wind type in Advection",
1042f259b054Svaleriabarra                           NULL, WindTypes,
1043f259b054Svaleriabarra                           (PetscEnum)(wind_type = ADVECTION_WIND_ROTATION),
10441184866aSLeila Ghaffari                           (PetscEnum *)&wind_type, NULL); CHKERRQ(ierr);
10451184866aSLeila Ghaffari   PetscInt n = problem->dim;
104682c09b01SLeila Ghaffari   PetscBool userWind;
1047ebb4b9bdSLeila Ghaffari   ierr = PetscOptionsRealArray("-problem_advection_wind_translation",
1048ebb4b9bdSLeila Ghaffari                                "Constant wind vector",
104982c09b01SLeila Ghaffari                                NULL, wind, &n, &userWind); CHKERRQ(ierr);
105082c09b01SLeila Ghaffari   if (wind_type == ADVECTION_WIND_ROTATION && userWind) {
1051ebb4b9bdSLeila Ghaffari     ierr = PetscPrintf(comm,
1052ebb4b9bdSLeila Ghaffari                        "Warning! Use -problem_advection_wind_translation only with -problem_advection_wind translation\n");
105382c09b01SLeila Ghaffari     CHKERRQ(ierr);
10541184866aSLeila Ghaffari   }
1055b9c3f7b3SLeila Ghaffari   if (wind_type == ADVECTION_WIND_TRANSLATION
1056b9c3f7b3SLeila Ghaffari       && (problemChoice == NS_DENSITY_CURRENT ||
1057b9c3f7b3SLeila Ghaffari           problemChoice == NS_EULER_VORTEX)) {
105867babd9cSLeila Ghaffari     SETERRQ(comm, PETSC_ERR_ARG_INCOMP,
1059b9c3f7b3SLeila Ghaffari             "-problem_advection_wind translation is not defined for -problem density_current or -problem euler_vortex");
106067babd9cSLeila Ghaffari   }
1061b9c3f7b3SLeila Ghaffari   ierr = PetscOptionsRealArray("-problem_euler_vortex_velocity",
1062b9c3f7b3SLeila Ghaffari                                "Incoming velocity vector",
1063b9c3f7b3SLeila Ghaffari                                NULL, u_enter, &n, NULL); CHKERRQ(ierr);
1064ccaff030SJeremy L Thompson   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
1065ccaff030SJeremy L Thompson                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
1066ccaff030SJeremy L Thompson                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
1067ccaff030SJeremy L Thompson   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
1068ccaff030SJeremy L Thompson                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
1069ccaff030SJeremy L Thompson   CHKERRQ(ierr);
107084d34d69SLeila Ghaffari   if (!implicit && stab != STAB_NONE) {
107184d34d69SLeila Ghaffari     ierr = PetscPrintf(comm, "Warning! Use -stab only with -implicit\n");
107284d34d69SLeila Ghaffari     CHKERRQ(ierr);
107384d34d69SLeila Ghaffari   }
1074ccaff030SJeremy L Thompson   {
10757573aee6SJed Brown     PetscInt len;
10767573aee6SJed Brown     PetscBool flg;
1077ccaff030SJeremy L Thompson     ierr = PetscOptionsIntArray("-bc_wall",
1078ccaff030SJeremy L Thompson                                 "Use wall boundary conditions on this list of faces",
1079ccaff030SJeremy L Thompson                                 NULL, bc.walls,
1080ccaff030SJeremy L Thompson                                 (len = sizeof(bc.walls) / sizeof(bc.walls[0]),
1081ccaff030SJeremy L Thompson                                  &len), &flg); CHKERRQ(ierr);
10827573aee6SJed Brown     if (flg) {
10837573aee6SJed Brown       bc.nwall = len;
10847573aee6SJed Brown       // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
10857573aee6SJed Brown       bc.nslip[0] = bc.nslip[1] = bc.nslip[2] = 0;
10867573aee6SJed Brown     }
1087ccaff030SJeremy L Thompson     for (PetscInt j=0; j<3; j++) {
1088ccaff030SJeremy L Thompson       const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
1089ccaff030SJeremy L Thompson       ierr = PetscOptionsIntArray(flags[j],
1090ccaff030SJeremy L Thompson                                   "Use slip boundary conditions on this list of faces",
1091ccaff030SJeremy L Thompson                                   NULL, bc.slips[j],
1092ccaff030SJeremy L Thompson                                   (len = sizeof(bc.slips[j]) / sizeof(bc.slips[j][0]),
1093ccaff030SJeremy L Thompson                                    &len), &flg);
1094ccaff030SJeremy L Thompson       CHKERRQ(ierr);
109584d34d69SLeila Ghaffari       if (flg) {
109684d34d69SLeila Ghaffari         bc.nslip[j] = len;
109784d34d69SLeila Ghaffari         bc.userbc = PETSC_TRUE;
109884d34d69SLeila Ghaffari       }
1099ccaff030SJeremy L Thompson     }
1100ccaff030SJeremy L Thompson   }
1101cb3e2689Svaleriabarra   ierr = PetscOptionsInt("-viz_refine",
1102cb3e2689Svaleriabarra                          "Regular refinement levels for visualization",
1103cb3e2689Svaleriabarra                          NULL, viz_refine, &viz_refine, NULL);
1104ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1105ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
1106ccaff030SJeremy L Thompson                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
1107ccaff030SJeremy L Thompson   meter = fabs(meter);
1108ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
1109ccaff030SJeremy L Thompson                             NULL, second, &second, NULL); CHKERRQ(ierr);
1110ccaff030SJeremy L Thompson   second = fabs(second);
1111ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
1112ccaff030SJeremy L Thompson                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
1113ccaff030SJeremy L Thompson   kilogram = fabs(kilogram);
1114ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-units_Kelvin",
1115ccaff030SJeremy L Thompson                             "1 Kelvin in scaled temperature units",
1116ccaff030SJeremy L Thompson                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
1117ccaff030SJeremy L Thompson   Kelvin = fabs(Kelvin);
1118ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
1119ccaff030SJeremy L Thompson                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
1120ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
1121ccaff030SJeremy L Thompson                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
1122ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
1123ccaff030SJeremy L Thompson                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
112416c0476cSLeila Ghaffari   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
112516c0476cSLeila Ghaffari                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
1126ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
1127ccaff030SJeremy L Thompson                             NULL, N, &N, NULL); CHKERRQ(ierr);
1128ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
1129ccaff030SJeremy L Thompson                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
1130ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
1131ccaff030SJeremy L Thompson                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
1132011314a7SLeila Ghaffari   PetscBool userVortex;
1133011314a7SLeila Ghaffari   ierr = PetscOptionsScalar("-vortex_strength", "Strength of Vortex",
1134011314a7SLeila Ghaffari                             NULL, vortex_strength, &vortex_strength, &userVortex);
1135011314a7SLeila Ghaffari   CHKERRQ(ierr);
1136011314a7SLeila Ghaffari   if (problemChoice != NS_EULER_VORTEX && userVortex) {
1137011314a7SLeila Ghaffari     ierr = PetscPrintf(comm,
1138011314a7SLeila Ghaffari                        "Warning! Use -vortex_strength only with -problem euler_vortex\n");
1139011314a7SLeila Ghaffari     CHKERRQ(ierr);
1140011314a7SLeila Ghaffari   }
1141b9c3f7b3SLeila Ghaffari   ierr = PetscOptionsScalar("-problem_euler_vortex_rho", "Incoming density",
1142b9c3f7b3SLeila Ghaffari                             NULL, rho_enter, &rho_enter, NULL);
1143b9c3f7b3SLeila Ghaffari   CHKERRQ(ierr);
1144ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
1145ccaff030SJeremy L Thompson                             NULL, g, &g, NULL); CHKERRQ(ierr);
1146ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lambda",
1147ccaff030SJeremy L Thompson                             "Stokes hypothesis second viscosity coefficient",
1148ccaff030SJeremy L Thompson                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
1149ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
1150ccaff030SJeremy L Thompson                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
1151ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
1152ccaff030SJeremy L Thompson                             NULL, k, &k, NULL); CHKERRQ(ierr);
1153ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-CtauS",
1154ccaff030SJeremy L Thompson                             "Scale coefficient for tau (nondimensional)",
1155ccaff030SJeremy L Thompson                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
115684d34d69SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
115784d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
115884d34d69SLeila Ghaffari                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
115984d34d69SLeila Ghaffari     CHKERRQ(ierr);
116084d34d69SLeila Ghaffari   }
1161ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-strong_form",
1162ccaff030SJeremy L Thompson                             "Strong (1) or weak/integrated by parts (0) advection residual",
1163ccaff030SJeremy L Thompson                             NULL, strong_form, &strong_form, NULL);
1164ccaff030SJeremy L Thompson   CHKERRQ(ierr);
116584d34d69SLeila Ghaffari   if (problemChoice == NS_DENSITY_CURRENT && (CtauS != 0 || strong_form != 0)) {
116684d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
116784d34d69SLeila Ghaffari                        "Warning! Problem density_current does not support -CtauS or -strong_form\n");
116884d34d69SLeila Ghaffari     CHKERRQ(ierr);
116984d34d69SLeila Ghaffari   }
1170ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
1171ccaff030SJeremy L Thompson                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
1172ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
1173ccaff030SJeremy L Thompson                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
1174ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
1175ccaff030SJeremy L Thompson                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
1176ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
1177ccaff030SJeremy L Thompson                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
1178ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resx","Target resolution in x",
1179ccaff030SJeremy L Thompson                             NULL, resx, &resx, NULL); CHKERRQ(ierr);
1180ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resy","Target resolution in y",
1181ccaff030SJeremy L Thompson                             NULL, resy, &resy, NULL); CHKERRQ(ierr);
1182ccaff030SJeremy L Thompson   ierr = PetscOptionsScalar("-resz","Target resolution in z",
1183ccaff030SJeremy L Thompson                             NULL, resz, &resz, NULL); CHKERRQ(ierr);
118482c09b01SLeila Ghaffari   n = problem->dim;
1185ccaff030SJeremy L Thompson   center[0] = 0.5 * lx;
1186ccaff030SJeremy L Thompson   center[1] = 0.5 * ly;
1187ccaff030SJeremy L Thompson   center[2] = 0.5 * lz;
1188ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
1189ccaff030SJeremy L Thompson                                NULL, center, &n, NULL); CHKERRQ(ierr);
1190ccaff030SJeremy L Thompson   n = problem->dim;
1191ccaff030SJeremy L Thompson   ierr = PetscOptionsRealArray("-dc_axis",
1192ccaff030SJeremy L Thompson                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
1193ccaff030SJeremy L Thompson                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
1194ccaff030SJeremy L Thompson   {
1195ccaff030SJeremy L Thompson     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) +
1196ccaff030SJeremy L Thompson                                    PetscSqr(dc_axis[1]) + PetscSqr(dc_axis[2]));
1197ccaff030SJeremy L Thompson     if (norm > 0) {
1198ccaff030SJeremy L Thompson       for (int i=0; i<3; i++) dc_axis[i] /= norm;
1199ccaff030SJeremy L Thompson     }
1200ccaff030SJeremy L Thompson   }
1201ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-output_freq",
1202ccaff030SJeremy L Thompson                          "Frequency of output, in number of steps",
1203ccaff030SJeremy L Thompson                          NULL, outputfreq, &outputfreq, NULL); CHKERRQ(ierr);
1204ccaff030SJeremy L Thompson   ierr = PetscOptionsInt("-continue", "Continue from previous solution",
1205ccaff030SJeremy L Thompson                          NULL, contsteps, &contsteps, NULL); CHKERRQ(ierr);
120684d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-degree", "Polynomial degree of finite elements",
120784d34d69SLeila Ghaffari                          NULL, degree, &degree, NULL); CHKERRQ(ierr);
120884d34d69SLeila Ghaffari   ierr = PetscOptionsInt("-qextra", "Number of extra quadrature points",
120984d34d69SLeila Ghaffari                          NULL, qextra, &qextra, NULL); CHKERRQ(ierr);
121081f92cf0SLeila Ghaffari   PetscBool userQextraSur;
1211ebb4b9bdSLeila Ghaffari   ierr = PetscOptionsInt("-qextra_boundary",
1212ebb4b9bdSLeila Ghaffari                          "Number of extra quadrature points on in/outflow faces",
1213f259b054Svaleriabarra                          NULL, qextraSur, &qextraSur, &userQextraSur);
1214f259b054Svaleriabarra   CHKERRQ(ierr);
1215ebb4b9bdSLeila Ghaffari   if ((wind_type == ADVECTION_WIND_ROTATION
1216ebb4b9bdSLeila Ghaffari        || problemChoice == NS_DENSITY_CURRENT) && userQextraSur) {
1217ebb4b9bdSLeila Ghaffari     ierr = PetscPrintf(comm,
1218ebb4b9bdSLeila Ghaffari                        "Warning! Use -qextra_boundary only with -problem_advection_wind translation\n");
121981f92cf0SLeila Ghaffari     CHKERRQ(ierr);
122081f92cf0SLeila Ghaffari   }
1221d99129b9SLeila Ghaffari   ierr = PetscStrncpy(user->outputdir, ".", 2); CHKERRQ(ierr);
1222d99129b9SLeila Ghaffari   ierr = PetscOptionsString("-output_dir", "Output directory",
1223d99129b9SLeila Ghaffari                             NULL, user->outputdir, user->outputdir,
1224d99129b9SLeila Ghaffari                             sizeof(user->outputdir), NULL); CHKERRQ(ierr);
122584d34d69SLeila Ghaffari   memtyperequested = petschavecuda ? CEED_MEM_DEVICE : CEED_MEM_HOST;
122684d34d69SLeila Ghaffari   ierr = PetscOptionsEnum("-memtype",
122784d34d69SLeila Ghaffari                           "CEED MemType requested", NULL,
122884d34d69SLeila Ghaffari                           memTypes, (PetscEnum)memtyperequested,
122984d34d69SLeila Ghaffari                           (PetscEnum *)&memtyperequested, &setmemtyperequest);
123084d34d69SLeila Ghaffari   CHKERRQ(ierr);
1231ccaff030SJeremy L Thompson   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
1232ccaff030SJeremy L Thompson 
1233ccaff030SJeremy L Thompson   // Define derived units
1234ccaff030SJeremy L Thompson   Pascal = kilogram / (meter * PetscSqr(second));
1235ccaff030SJeremy L Thompson   JperkgK =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
1236ccaff030SJeremy L Thompson   mpersquareds = meter / PetscSqr(second);
1237ccaff030SJeremy L Thompson   WpermK = kilogram * meter / (pow(second,3) * Kelvin);
1238ccaff030SJeremy L Thompson   kgpercubicm = kilogram / pow(meter,3);
1239ccaff030SJeremy L Thompson   kgpersquaredms = kilogram / (PetscSqr(meter) * second);
1240ccaff030SJeremy L Thompson   Joulepercubicm = kilogram / (meter * PetscSqr(second));
124116c0476cSLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
1242ccaff030SJeremy L Thompson 
1243ccaff030SJeremy L Thompson   // Scale variables to desired units
1244ccaff030SJeremy L Thompson   theta0 *= Kelvin;
1245ccaff030SJeremy L Thompson   thetaC *= Kelvin;
1246ccaff030SJeremy L Thompson   P0 *= Pascal;
124716c0476cSLeila Ghaffari   E_wind *= Joule;
1248b9c3f7b3SLeila Ghaffari   rho_enter *= kgpercubicm;
1249ccaff030SJeremy L Thompson   N *= (1./second);
1250ccaff030SJeremy L Thompson   cv *= JperkgK;
1251ccaff030SJeremy L Thompson   cp *= JperkgK;
1252ccaff030SJeremy L Thompson   Rd = cp - cv;
1253ccaff030SJeremy L Thompson   g *= mpersquareds;
1254ccaff030SJeremy L Thompson   mu *= Pascal * second;
1255ccaff030SJeremy L Thompson   k *= WpermK;
1256ccaff030SJeremy L Thompson   lx = fabs(lx) * meter;
1257ccaff030SJeremy L Thompson   ly = fabs(ly) * meter;
1258ccaff030SJeremy L Thompson   lz = fabs(lz) * meter;
1259ccaff030SJeremy L Thompson   rc = fabs(rc) * meter;
1260ccaff030SJeremy L Thompson   resx = fabs(resx) * meter;
1261ccaff030SJeremy L Thompson   resy = fabs(resy) * meter;
1262ccaff030SJeremy L Thompson   resz = fabs(resz) * meter;
1263ccaff030SJeremy L Thompson   for (int i=0; i<3; i++) center[i] *= meter;
1264ccaff030SJeremy L Thompson 
1265ccaff030SJeremy L Thompson   const CeedInt dim = problem->dim, ncompx = problem->dim,
1266cfa64770SLeila Ghaffari                 qdatasizeVol = problem->qdatasizeVol;
1267ccaff030SJeremy L Thompson   // Set up the libCEED context
1268777ff853SJeremy L Thompson   struct SetupContext_ ctxSetupData = {
1269ccaff030SJeremy L Thompson     .theta0 = theta0,
1270ccaff030SJeremy L Thompson     .thetaC = thetaC,
1271ccaff030SJeremy L Thompson     .P0 = P0,
1272ccaff030SJeremy L Thompson     .N = N,
1273ccaff030SJeremy L Thompson     .cv = cv,
1274ccaff030SJeremy L Thompson     .cp = cp,
1275ccaff030SJeremy L Thompson     .Rd = Rd,
1276ccaff030SJeremy L Thompson     .g = g,
1277ccaff030SJeremy L Thompson     .rc = rc,
1278ccaff030SJeremy L Thompson     .lx = lx,
1279ccaff030SJeremy L Thompson     .ly = ly,
1280ccaff030SJeremy L Thompson     .lz = lz,
1281ccaff030SJeremy L Thompson     .center[0] = center[0],
1282ccaff030SJeremy L Thompson     .center[1] = center[1],
1283ccaff030SJeremy L Thompson     .center[2] = center[2],
1284ccaff030SJeremy L Thompson     .dc_axis[0] = dc_axis[0],
1285ccaff030SJeremy L Thompson     .dc_axis[1] = dc_axis[1],
1286ccaff030SJeremy L Thompson     .dc_axis[2] = dc_axis[2],
12871184866aSLeila Ghaffari     .wind[0] = wind[0],
12881184866aSLeila Ghaffari     .wind[1] = wind[1],
12891184866aSLeila Ghaffari     .wind[2] = wind[2],
1290ccaff030SJeremy L Thompson     .time = 0,
1291011314a7SLeila Ghaffari     .vortex_strength = vortex_strength,
12921184866aSLeila Ghaffari     .wind_type = wind_type,
1293ccaff030SJeremy L Thompson   };
1294ccaff030SJeremy L Thompson 
129584d34d69SLeila Ghaffari   // Create the mesh
1296ccaff030SJeremy L Thompson   {
1297ccaff030SJeremy L Thompson     const PetscReal scale[3] = {lx, ly, lz};
1298ccaff030SJeremy L Thompson     ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_FALSE, NULL, NULL, scale,
129984d34d69SLeila Ghaffari                                NULL, PETSC_TRUE, &dm);
1300ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1301ccaff030SJeremy L Thompson   }
130284d34d69SLeila Ghaffari 
130384d34d69SLeila Ghaffari   // Distribute the mesh over processes
130484d34d69SLeila Ghaffari   {
1305ccaff030SJeremy L Thompson     DM               dmDist = NULL;
1306ccaff030SJeremy L Thompson     PetscPartitioner part;
1307ccaff030SJeremy L Thompson 
1308ccaff030SJeremy L Thompson     ierr = DMPlexGetPartitioner(dm, &part); CHKERRQ(ierr);
1309ccaff030SJeremy L Thompson     ierr = PetscPartitionerSetFromOptions(part); CHKERRQ(ierr);
1310ccaff030SJeremy L Thompson     ierr = DMPlexDistribute(dm, 0, NULL, &dmDist); CHKERRQ(ierr);
1311ccaff030SJeremy L Thompson     if (dmDist) {
1312ccaff030SJeremy L Thompson       ierr = DMDestroy(&dm); CHKERRQ(ierr);
1313ccaff030SJeremy L Thompson       dm  = dmDist;
1314ccaff030SJeremy L Thompson     }
1315ccaff030SJeremy L Thompson   }
1316ccaff030SJeremy L Thompson   ierr = DMViewFromOptions(dm, NULL, "-dm_view"); CHKERRQ(ierr);
1317ccaff030SJeremy L Thompson 
131884d34d69SLeila Ghaffari   // Setup DM
1319ccaff030SJeremy L Thompson   ierr = DMLocalizeCoordinates(dm); CHKERRQ(ierr);
1320ccaff030SJeremy L Thompson   ierr = DMSetFromOptions(dm); CHKERRQ(ierr);
1321777ff853SJeremy L Thompson   ierr = SetUpDM(dm, problem, degree, &bc, &ctxSetupData); CHKERRQ(ierr);
132284d34d69SLeila Ghaffari 
132384d34d69SLeila Ghaffari   // Refine DM for high-order viz
1324ccaff030SJeremy L Thompson   dmviz = NULL;
1325ccaff030SJeremy L Thompson   interpviz = NULL;
1326ccaff030SJeremy L Thompson   if (viz_refine) {
1327ff6701fcSJed Brown     DM dmhierarchy[viz_refine+1];
1328ff6701fcSJed Brown 
1329ccaff030SJeremy L Thompson     ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr);
1330ff6701fcSJed Brown     dmhierarchy[0] = dm;
133184d34d69SLeila Ghaffari     for (PetscInt i = 0, d = degree; i < viz_refine; i++) {
1332ff6701fcSJed Brown       Mat interp_next;
1333ff6701fcSJed Brown 
1334ff6701fcSJed Brown       ierr = DMRefine(dmhierarchy[i], MPI_COMM_NULL, &dmhierarchy[i+1]);
1335ccaff030SJeremy L Thompson       CHKERRQ(ierr);
1336bc6a41f7SJed Brown       ierr = DMClearDS(dmhierarchy[i+1]); CHKERRQ(ierr);
1337bc6a41f7SJed Brown       ierr = DMClearFields(dmhierarchy[i+1]); CHKERRQ(ierr);
1338ff6701fcSJed Brown       ierr = DMSetCoarseDM(dmhierarchy[i+1], dmhierarchy[i]); CHKERRQ(ierr);
1339ff6701fcSJed Brown       d = (d + 1) / 2;
1340ff6701fcSJed Brown       if (i + 1 == viz_refine) d = 1;
1341777ff853SJeremy L Thompson       ierr = SetUpDM(dmhierarchy[i+1], problem, d, &bc, &ctxSetupData);
1342f259b054Svaleriabarra       CHKERRQ(ierr);
1343ff6701fcSJed Brown       ierr = DMCreateInterpolation(dmhierarchy[i], dmhierarchy[i+1],
1344ff6701fcSJed Brown                                    &interp_next, NULL); CHKERRQ(ierr);
1345ff6701fcSJed Brown       if (!i) interpviz = interp_next;
1346ff6701fcSJed Brown       else {
1347ff6701fcSJed Brown         Mat C;
1348ff6701fcSJed Brown         ierr = MatMatMult(interp_next, interpviz, MAT_INITIAL_MATRIX,
1349ff6701fcSJed Brown                           PETSC_DECIDE, &C); CHKERRQ(ierr);
1350ff6701fcSJed Brown         ierr = MatDestroy(&interp_next); CHKERRQ(ierr);
1351ff6701fcSJed Brown         ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1352ff6701fcSJed Brown         interpviz = C;
1353ff6701fcSJed Brown       }
1354ff6701fcSJed Brown     }
1355cb3e2689Svaleriabarra     for (PetscInt i=1; i<viz_refine; i++) {
1356ff6701fcSJed Brown       ierr = DMDestroy(&dmhierarchy[i]); CHKERRQ(ierr);
1357cb3e2689Svaleriabarra     }
1358ff6701fcSJed Brown     dmviz = dmhierarchy[viz_refine];
1359ccaff030SJeremy L Thompson   }
1360ccaff030SJeremy L Thompson   ierr = DMCreateGlobalVector(dm, &Q); CHKERRQ(ierr);
1361ccaff030SJeremy L Thompson   ierr = DMGetLocalVector(dm, &Qloc); CHKERRQ(ierr);
1362ccaff030SJeremy L Thompson   ierr = VecGetSize(Qloc, &lnodes); CHKERRQ(ierr);
1363ccaff030SJeremy L Thompson   lnodes /= ncompq;
1364ccaff030SJeremy L Thompson 
136584d34d69SLeila Ghaffari   // Initialize CEED
136684d34d69SLeila Ghaffari   CeedInit(ceedresource, &ceed);
136784d34d69SLeila Ghaffari   // Set memtype
136884d34d69SLeila Ghaffari   CeedMemType memtypebackend;
136984d34d69SLeila Ghaffari   CeedGetPreferredMemType(ceed, &memtypebackend);
137084d34d69SLeila Ghaffari   // Check memtype compatibility
137184d34d69SLeila Ghaffari   if (!setmemtyperequest)
137284d34d69SLeila Ghaffari     memtyperequested = memtypebackend;
137384d34d69SLeila Ghaffari   else if (!petschavecuda && memtyperequested == CEED_MEM_DEVICE)
137484d34d69SLeila Ghaffari     SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS,
137584d34d69SLeila Ghaffari              "PETSc was not built with CUDA. "
137684d34d69SLeila Ghaffari              "Requested MemType CEED_MEM_DEVICE is not supported.", NULL);
137784d34d69SLeila Ghaffari 
137884d34d69SLeila Ghaffari   // Set number of 1D nodes and quadrature points
137984d34d69SLeila Ghaffari   numP = degree + 1;
138084d34d69SLeila Ghaffari   numQ = numP + qextra;
138184d34d69SLeila Ghaffari 
138284d34d69SLeila Ghaffari   // Print summary
1383dc8efd83SLeila Ghaffari   if (!test) {
1384ccaff030SJeremy L Thompson     CeedInt gdofs, odofs;
1385ccaff030SJeremy L Thompson     int comm_size;
1386ccaff030SJeremy L Thompson     char box_faces_str[PETSC_MAX_PATH_LEN] = "NONE";
1387ccaff030SJeremy L Thompson     ierr = VecGetSize(Q, &gdofs); CHKERRQ(ierr);
1388ccaff030SJeremy L Thompson     ierr = VecGetLocalSize(Q, &odofs); CHKERRQ(ierr);
138984d34d69SLeila Ghaffari     gnodes = gdofs/ncompq;
1390ccaff030SJeremy L Thompson     ierr = MPI_Comm_size(comm, &comm_size); CHKERRQ(ierr);
1391ccaff030SJeremy L Thompson     ierr = PetscOptionsGetString(NULL, NULL, "-dm_plex_box_faces", box_faces_str,
1392ccaff030SJeremy L Thompson                                  sizeof(box_faces_str), NULL); CHKERRQ(ierr);
139384d34d69SLeila Ghaffari     const char *usedresource;
139484d34d69SLeila Ghaffari     CeedGetResource(ceed, &usedresource);
1395ccaff030SJeremy L Thompson 
139684d34d69SLeila Ghaffari     ierr = PetscPrintf(comm,
139784d34d69SLeila Ghaffari                        "\n-- Navier-Stokes solver - libCEED + PETSc --\n"
139884d34d69SLeila Ghaffari                        "  rank(s)                              : %d\n"
139984d34d69SLeila Ghaffari                        "  Problem:\n"
140084d34d69SLeila Ghaffari                        "    Problem Name                       : %s\n"
140184d34d69SLeila Ghaffari                        "    Stabilization                      : %s\n"
140284d34d69SLeila Ghaffari                        "  PETSc:\n"
140384d34d69SLeila Ghaffari                        "    Box Faces                          : %s\n"
140484d34d69SLeila Ghaffari                        "  libCEED:\n"
140584d34d69SLeila Ghaffari                        "    libCEED Backend                    : %s\n"
140684d34d69SLeila Ghaffari                        "    libCEED Backend MemType            : %s\n"
140784d34d69SLeila Ghaffari                        "    libCEED User Requested MemType     : %s\n"
140884d34d69SLeila Ghaffari                        "  Mesh:\n"
140984d34d69SLeila Ghaffari                        "    Number of 1D Basis Nodes (P)       : %d\n"
141084d34d69SLeila Ghaffari                        "    Number of 1D Quadrature Points (Q) : %d\n"
141184d34d69SLeila Ghaffari                        "    Global DoFs                        : %D\n"
141284d34d69SLeila Ghaffari                        "    Owned DoFs                         : %D\n"
141384d34d69SLeila Ghaffari                        "    DoFs per node                      : %D\n"
141484d34d69SLeila Ghaffari                        "    Global nodes                       : %D\n"
141584d34d69SLeila Ghaffari                        "    Owned nodes                        : %D\n",
141684d34d69SLeila Ghaffari                        comm_size, problemTypes[problemChoice],
141784d34d69SLeila Ghaffari                        StabilizationTypes[stab], box_faces_str, usedresource,
141884d34d69SLeila Ghaffari                        CeedMemTypes[memtypebackend],
141984d34d69SLeila Ghaffari                        (setmemtyperequest) ?
142084d34d69SLeila Ghaffari                        CeedMemTypes[memtyperequested] : "none",
142184d34d69SLeila Ghaffari                        numP, numQ, gdofs, odofs, ncompq, gnodes, lnodes);
142284d34d69SLeila Ghaffari     CHKERRQ(ierr);
14230c6c0b13SLeila Ghaffari   }
14240c6c0b13SLeila Ghaffari 
1425ccaff030SJeremy L Thompson   // Set up global mass vector
1426ccaff030SJeremy L Thompson   ierr = VecDuplicate(Q, &user->M); CHKERRQ(ierr);
1427ccaff030SJeremy L Thompson 
142884d34d69SLeila Ghaffari   // Set up libCEED
1429ccaff030SJeremy L Thompson   // CEED Bases
1430ccaff030SJeremy L Thompson   CeedInit(ceedresource, &ceed);
143184d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompq, numP, numQ, CEED_GAUSS,
143284d34d69SLeila Ghaffari                                   &basisq);
143384d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numQ, CEED_GAUSS,
143484d34d69SLeila Ghaffari                                   &basisx);
143584d34d69SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, ncompx, 2, numP,
143684d34d69SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxc);
1437ccaff030SJeremy L Thompson   ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
1438ccaff030SJeremy L Thompson   ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
1439ccaff030SJeremy L Thompson   CHKERRQ(ierr);
1440ccaff030SJeremy L Thompson 
1441ccaff030SJeremy L Thompson   // CEED Restrictions
14421e150236SLeila Ghaffari   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, numP, numQ,
144384d34d69SLeila Ghaffari                                  qdatasizeVol, &restrictq, &restrictx,
144484d34d69SLeila Ghaffari                                  &restrictqdi); CHKERRQ(ierr);
1445ccaff030SJeremy L Thompson 
1446ccaff030SJeremy L Thompson   ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
1447ccaff030SJeremy L Thompson   ierr = CreateVectorFromPetscVec(ceed, Xloc, &xcorners); CHKERRQ(ierr);
1448ccaff030SJeremy L Thompson 
1449ccaff030SJeremy L Thompson   // Create the CEED vectors that will be needed in setup
1450bd910870SLeila Ghaffari   CeedInt NqptsVol;
145184d34d69SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisq, &NqptsVol);
145284d34d69SLeila Ghaffari   CeedElemRestrictionGetNumElements(restrictq, &localNelemVol);
14538b982baeSLeila Ghaffari   CeedVectorCreate(ceed, qdatasizeVol*localNelemVol*NqptsVol, &qdata);
145484d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &q0ceed, NULL);
1455ccaff030SJeremy L Thompson 
1456ccaff030SJeremy L Thompson   // Create the Q-function that builds the quadrature data for the NS operator
1457ea6e0f84SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupVol, problem->setupVol_loc,
1458ea6e0f84SLeila Ghaffari                               &qf_setupVol);
1459ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "dx", ncompx*dim, CEED_EVAL_GRAD);
1460ea6e0f84SLeila Ghaffari   CeedQFunctionAddInput(qf_setupVol, "weight", 1, CEED_EVAL_WEIGHT);
14618b982baeSLeila Ghaffari   CeedQFunctionAddOutput(qf_setupVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1462ccaff030SJeremy L Thompson 
1463ccaff030SJeremy L Thompson   // Create the Q-function that sets the ICs of the operator
1464ccaff030SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, problem->ics, problem->ics_loc, &qf_ics);
1465ccaff030SJeremy L Thompson   CeedQFunctionAddInput(qf_ics, "x", ncompx, CEED_EVAL_INTERP);
1466ccaff030SJeremy L Thompson   CeedQFunctionAddOutput(qf_ics, "q0", ncompq, CEED_EVAL_NONE);
1467ccaff030SJeremy L Thompson 
1468ea6e0f84SLeila Ghaffari   qf_rhsVol = NULL;
1469ea6e0f84SLeila Ghaffari   if (problem->applyVol_rhs) { // Create the Q-function that defines the action of the RHS operator
1470ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_rhs,
1471ea6e0f84SLeila Ghaffari                                 problem->applyVol_rhs_loc, &qf_rhsVol);
1472ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "q", ncompq, CEED_EVAL_INTERP);
1473ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
14748b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1475ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_rhsVol, "x", ncompx, CEED_EVAL_INTERP);
1476ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "v", ncompq, CEED_EVAL_INTERP);
1477ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_rhsVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1478ccaff030SJeremy L Thompson   }
1479ccaff030SJeremy L Thompson 
1480ea6e0f84SLeila Ghaffari   qf_ifunctionVol = NULL;
1481ea6e0f84SLeila Ghaffari   if (problem->applyVol_ifunction) { // Create the Q-function that defines the action of the IFunction
1482ea6e0f84SLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyVol_ifunction,
1483ea6e0f84SLeila Ghaffari                                 problem->applyVol_ifunction_loc, &qf_ifunctionVol);
1484ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "q", ncompq, CEED_EVAL_INTERP);
1485ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "dq", ncompq*dim, CEED_EVAL_GRAD);
1486ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdot", ncompq, CEED_EVAL_INTERP);
14878b982baeSLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "qdata", qdatasizeVol, CEED_EVAL_NONE);
1488ea6e0f84SLeila Ghaffari     CeedQFunctionAddInput(qf_ifunctionVol, "x", ncompx, CEED_EVAL_INTERP);
1489ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "v", ncompq, CEED_EVAL_INTERP);
1490ea6e0f84SLeila Ghaffari     CeedQFunctionAddOutput(qf_ifunctionVol, "dv", ncompq*dim, CEED_EVAL_GRAD);
1491ccaff030SJeremy L Thompson   }
1492ccaff030SJeremy L Thompson 
1493ccaff030SJeremy L Thompson   // Create the operator that builds the quadrature data for the NS operator
1494ea6e0f84SLeila Ghaffari   CeedOperatorCreate(ceed, qf_setupVol, NULL, NULL, &op_setupVol);
149584d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "dx", restrictx, basisx, CEED_VECTOR_ACTIVE);
1496ea6e0f84SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "weight", CEED_ELEMRESTRICTION_NONE,
149784d34d69SLeila Ghaffari                        basisx, CEED_VECTOR_NONE);
149884d34d69SLeila Ghaffari   CeedOperatorSetField(op_setupVol, "qdata", restrictqdi,
1499ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1500ccaff030SJeremy L Thompson 
1501ccaff030SJeremy L Thompson   // Create the operator that sets the ICs
1502ccaff030SJeremy L Thompson   CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics);
150384d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "x", restrictx, basisxc, CEED_VECTOR_ACTIVE);
150484d34d69SLeila Ghaffari   CeedOperatorSetField(op_ics, "q0", restrictq,
1505ccaff030SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1506ccaff030SJeremy L Thompson 
150784d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qceed, NULL);
150884d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->qdotceed, NULL);
150984d34d69SLeila Ghaffari   CeedElemRestrictionCreateVector(restrictq, &user->gceed, NULL);
1510ccaff030SJeremy L Thompson 
1511ea6e0f84SLeila Ghaffari   if (qf_rhsVol) { // Create the RHS physics operator
1512ccaff030SJeremy L Thompson     CeedOperator op;
1513ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_rhsVol, NULL, NULL, &op);
151484d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
151584d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
151684d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
15178b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
151884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
151984d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
152084d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1521d3630711SJed Brown     user->op_rhs_vol = op;
1522ccaff030SJeremy L Thompson   }
1523ccaff030SJeremy L Thompson 
1524ea6e0f84SLeila Ghaffari   if (qf_ifunctionVol) { // Create the IFunction operator
1525ccaff030SJeremy L Thompson     CeedOperator op;
1526ea6e0f84SLeila Ghaffari     CeedOperatorCreate(ceed, qf_ifunctionVol, NULL, NULL, &op);
152784d34d69SLeila Ghaffari     CeedOperatorSetField(op, "q", restrictq, basisq, CEED_VECTOR_ACTIVE);
152884d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dq", restrictq, basisq, CEED_VECTOR_ACTIVE);
152984d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdot", restrictq, basisq, user->qdotceed);
153084d34d69SLeila Ghaffari     CeedOperatorSetField(op, "qdata", restrictqdi,
15318b982baeSLeila Ghaffari                          CEED_BASIS_COLLOCATED, qdata);
153284d34d69SLeila Ghaffari     CeedOperatorSetField(op, "x", restrictx, basisx, xcorners);
153384d34d69SLeila Ghaffari     CeedOperatorSetField(op, "v", restrictq, basisq, CEED_VECTOR_ACTIVE);
153484d34d69SLeila Ghaffari     CeedOperatorSetField(op, "dv", restrictq, basisq, CEED_VECTOR_ACTIVE);
1535d3630711SJed Brown     user->op_ifunction_vol = op;
1536ccaff030SJeremy L Thompson   }
1537ccaff030SJeremy L Thompson 
15386a0edaf9SLeila Ghaffari   // Set up CEED for the boundaries
15396a0edaf9SLeila Ghaffari   CeedInt height = 1;
15406a0edaf9SLeila Ghaffari   CeedInt dimSur = dim - height;
15411e150236SLeila Ghaffari   CeedInt numP_Sur = degree + 1;
15421e150236SLeila Ghaffari   CeedInt numQ_Sur = numP_Sur + qextraSur;
1543cfa64770SLeila Ghaffari   const CeedInt qdatasizeSur = problem->qdatasizeSur;
1544cfa64770SLeila Ghaffari   CeedBasis basisxSur, basisxcSur, basisqSur;
1545cfa64770SLeila Ghaffari   CeedInt NqptsSur;
15464438636fSLeila Ghaffari   CeedQFunction qf_setupSur, qf_applySur, qf_applyIn, qf_applyOut;
1547cfa64770SLeila Ghaffari 
1548cfa64770SLeila Ghaffari   // CEED bases for the boundaries
1549ebb4b9bdSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompq, numP_Sur, numQ_Sur,
1550ebb4b9bdSLeila Ghaffari                                   CEED_GAUSS,
15516a0edaf9SLeila Ghaffari                                   &basisqSur);
15526a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numQ_Sur, CEED_GAUSS,
15536a0edaf9SLeila Ghaffari                                   &basisxSur);
15546a0edaf9SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dimSur, ncompx, 2, numP_Sur,
15556a0edaf9SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &basisxcSur);
15566a0edaf9SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(basisqSur, &NqptsSur);
15576a0edaf9SLeila Ghaffari 
1558cfa64770SLeila Ghaffari   // Create the Q-function that builds the quadrature data for the Surface operator
15596a0edaf9SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, problem->setupSur, problem->setupSur_loc,
15606a0edaf9SLeila Ghaffari                               &qf_setupSur);
15616a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "dx", ncompx*dimSur, CEED_EVAL_GRAD);
15626a0edaf9SLeila Ghaffari   CeedQFunctionAddInput(qf_setupSur, "weight", 1, CEED_EVAL_WEIGHT);
15636a0edaf9SLeila Ghaffari   CeedQFunctionAddOutput(qf_setupSur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
15646a0edaf9SLeila Ghaffari 
15657659d40cSLeila Ghaffari   // Creat Q-Function for Boundaries
15667ed8b9c7SLeila Ghaffari   qf_applySur = NULL;
15677659d40cSLeila Ghaffari   if (problem->applySur) {
15687659d40cSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applySur,
15697ed8b9c7SLeila Ghaffari                                 problem->applySur_loc, &qf_applySur);
15707ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "q", ncompq, CEED_EVAL_INTERP);
15717ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
15727ed8b9c7SLeila Ghaffari     CeedQFunctionAddInput(qf_applySur, "x", ncompx, CEED_EVAL_INTERP);
15737ed8b9c7SLeila Ghaffari     CeedQFunctionAddOutput(qf_applySur, "v", ncompq, CEED_EVAL_INTERP);
15749fe13df9SLeila Ghaffari   }
15754438636fSLeila Ghaffari   qf_applyIn = NULL;
15764438636fSLeila Ghaffari   if (problem->applyIn) {
15774438636fSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyIn,
15784438636fSLeila Ghaffari                                 problem->applyIn_loc, &qf_applyIn);
15794438636fSLeila Ghaffari     CeedQFunctionAddInput(qf_applyIn, "q", ncompq, CEED_EVAL_INTERP);
15804438636fSLeila Ghaffari     CeedQFunctionAddInput(qf_applyIn, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
15814438636fSLeila Ghaffari     CeedQFunctionAddInput(qf_applyIn, "x", ncompx, CEED_EVAL_INTERP);
15824438636fSLeila Ghaffari     CeedQFunctionAddOutput(qf_applyIn, "v", ncompq, CEED_EVAL_INTERP);
15834438636fSLeila Ghaffari   }
15844438636fSLeila Ghaffari   qf_applyOut = NULL;
15854438636fSLeila Ghaffari   if (problem->applyOut) {
15864438636fSLeila Ghaffari     CeedQFunctionCreateInterior(ceed, 1, problem->applyOut,
15874438636fSLeila Ghaffari                                 problem->applyOut_loc, &qf_applyOut);
15884438636fSLeila Ghaffari     CeedQFunctionAddInput(qf_applyOut, "q", ncompq, CEED_EVAL_INTERP);
15894438636fSLeila Ghaffari     CeedQFunctionAddInput(qf_applyOut, "qdataSur", qdatasizeSur, CEED_EVAL_NONE);
15904438636fSLeila Ghaffari     CeedQFunctionAddInput(qf_applyOut, "x", ncompx, CEED_EVAL_INTERP);
15914438636fSLeila Ghaffari     CeedQFunctionAddOutput(qf_applyOut, "v", ncompq, CEED_EVAL_INTERP);
15924438636fSLeila Ghaffari   }
15939fe13df9SLeila Ghaffari 
15949fe13df9SLeila Ghaffari   // Create CEED Operator for the whole domain
15959fe13df9SLeila Ghaffari   if (!implicit)
15964438636fSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, &bc, problemChoice, wind_type,
15974438636fSLeila Ghaffari                                    user->op_rhs_vol,qf_applySur,
15984438636fSLeila Ghaffari                                    qf_setupSur, qf_applyIn, qf_applyOut,
15994438636fSLeila Ghaffari                                    height, numP_Sur, numQ_Sur,
16004438636fSLeila Ghaffari                                    qdatasizeSur, NqptsSur, basisxSur,
16014438636fSLeila Ghaffari                                    basisqSur, &user->op_rhs);
16024438636fSLeila Ghaffari                                    CHKERRQ(ierr);
16039fe13df9SLeila Ghaffari   if (implicit)
16044438636fSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, &bc, problemChoice, wind_type,
16054438636fSLeila Ghaffari                                    user->op_ifunction_vol, qf_applySur,
16064438636fSLeila Ghaffari                                    qf_setupSur, NULL, NULL,
16074438636fSLeila Ghaffari                                    height, numP_Sur, numQ_Sur,
16084438636fSLeila Ghaffari                                    qdatasizeSur, NqptsSur, basisxSur,
16094438636fSLeila Ghaffari                                    basisqSur, &user->op_ifunction);
16104438636fSLeila Ghaffari                                    CHKERRQ(ierr);
16117659d40cSLeila Ghaffari   // Set up contex for QFunctions
1612777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxSetup);
1613777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxSetup, CEED_MEM_HOST, CEED_USE_POINTER,
1614777ff853SJeremy L Thompson                               sizeof ctxSetupData, &ctxSetupData);
1615777ff853SJeremy L Thompson   CeedQFunctionSetContext(qf_ics, ctxSetup);
1616777ff853SJeremy L Thompson 
1617777ff853SJeremy L Thompson   CeedScalar ctxNSData[8] = {lambda, mu, k, cv, cp, g, Rd};
1618777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxNS);
1619777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxNS, CEED_MEM_HOST, CEED_USE_POINTER,
1620777ff853SJeremy L Thompson                               sizeof ctxNSData, &ctxNSData);
1621777ff853SJeremy L Thompson 
1622777ff853SJeremy L Thompson   struct Advection2dContext_ ctxAdvection2dData = {
1623ccaff030SJeremy L Thompson     .CtauS = CtauS,
1624ccaff030SJeremy L Thompson     .strong_form = strong_form,
1625ccaff030SJeremy L Thompson     .stabilization = stab,
1626ccaff030SJeremy L Thompson   };
1627777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxAdvection2d);
1628777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxAdvection2d, CEED_MEM_HOST, CEED_USE_POINTER,
1629777ff853SJeremy L Thompson                               sizeof ctxAdvection2dData, &ctxAdvection2dData);
1630777ff853SJeremy L Thompson 
1631777ff853SJeremy L Thompson   struct SurfaceContext_ ctxSurfaceData = {
163216c0476cSLeila Ghaffari     .E_wind = E_wind,
16337659d40cSLeila Ghaffari     .strong_form = strong_form,
1634*e5ed8c30SLeila Ghaffari     .implicit = implicit,
1635*e5ed8c30SLeila Ghaffari   };
1636*e5ed8c30SLeila Ghaffari   struct EulerContext_ ctxEuler = {
1637*e5ed8c30SLeila Ghaffari     .cv = cv,
1638*e5ed8c30SLeila Ghaffari     .cp = cp,
1639*e5ed8c30SLeila Ghaffari     .Rd = Rd,
1640*e5ed8c30SLeila Ghaffari     .g = g,
1641b9c3f7b3SLeila Ghaffari     .rho_enter = rho_enter,
1642b9c3f7b3SLeila Ghaffari     .u_enter[0] = u_enter[0],
1643b9c3f7b3SLeila Ghaffari     .u_enter[1] = u_enter[1],
1644b9c3f7b3SLeila Ghaffari     .u_enter[2] = u_enter[2],
16457659d40cSLeila Ghaffari     .implicit = implicit,
16467659d40cSLeila Ghaffari   };
1647777ff853SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctxSurface);
1648777ff853SJeremy L Thompson   CeedQFunctionContextSetData(ctxSurface, CEED_MEM_HOST, CEED_USE_POINTER,
1649777ff853SJeremy L Thompson                               sizeof ctxSurfaceData, &ctxSurfaceData);
1650777ff853SJeremy L Thompson 
1651ccaff030SJeremy L Thompson   switch (problemChoice) {
1652ccaff030SJeremy L Thompson   case NS_DENSITY_CURRENT:
1653*e5ed8c30SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxNS, sizeof ctxNS);
1654*e5ed8c30SLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, &ctxNS,
1655*e5ed8c30SLeila Ghaffari           sizeof ctxNS);
1656ccaff030SJeremy L Thompson     break;
1657ccaff030SJeremy L Thompson   case NS_ADVECTION:
1658ccaff030SJeremy L Thompson   case NS_ADVECTION2D:
1659*e5ed8c30SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxAdvection2d,
1660*e5ed8c30SLeila Ghaffari           sizeof ctxAdvection2d);
1661*e5ed8c30SLeila Ghaffari     if (qf_ifunctionVol) CeedQFunctionSetContext(qf_ifunctionVol, &ctxAdvection2d,
1662*e5ed8c30SLeila Ghaffari           sizeof ctxAdvection2d);
1663*e5ed8c30SLeila Ghaffari     if (qf_applySur) CeedQFunctionSetContext(qf_applySur, &ctxSurface,
1664*e5ed8c30SLeila Ghaffari           sizeof ctxSurface);
1665*e5ed8c30SLeila Ghaffari   case NS_EULER_VORTEX:
1666*e5ed8c30SLeila Ghaffari     if (qf_rhsVol) CeedQFunctionSetContext(qf_rhsVol, &ctxEuler,
1667*e5ed8c30SLeila Ghaffari           sizeof ctxEuler);
1668*e5ed8c30SLeila Ghaffari     if (qf_applyOut) CeedQFunctionSetContext(qf_applyOut, &ctxEuler,
1669*e5ed8c30SLeila Ghaffari           sizeof ctxEuler);
1670*e5ed8c30SLeila Ghaffari     if (qf_applyIn) CeedQFunctionSetContext(qf_applyIn, &ctxEuler,
1671*e5ed8c30SLeila Ghaffari           sizeof ctxEuler);
1672ccaff030SJeremy L Thompson   }
1673ccaff030SJeremy L Thompson 
1674ccaff030SJeremy L Thompson   // Set up PETSc context
1675ccaff030SJeremy L Thompson   // Set up units structure
1676ccaff030SJeremy L Thompson   units->meter = meter;
1677ccaff030SJeremy L Thompson   units->kilogram = kilogram;
1678ccaff030SJeremy L Thompson   units->second = second;
1679ccaff030SJeremy L Thompson   units->Kelvin = Kelvin;
1680ccaff030SJeremy L Thompson   units->Pascal = Pascal;
1681ccaff030SJeremy L Thompson   units->JperkgK = JperkgK;
1682ccaff030SJeremy L Thompson   units->mpersquareds = mpersquareds;
1683ccaff030SJeremy L Thompson   units->WpermK = WpermK;
1684ccaff030SJeremy L Thompson   units->kgpercubicm = kgpercubicm;
1685ccaff030SJeremy L Thompson   units->kgpersquaredms = kgpersquaredms;
1686ccaff030SJeremy L Thompson   units->Joulepercubicm = Joulepercubicm;
168716c0476cSLeila Ghaffari   units->Joule = Joule;
1688ccaff030SJeremy L Thompson 
1689ccaff030SJeremy L Thompson   // Set up user structure
1690ccaff030SJeremy L Thompson   user->comm = comm;
1691ccaff030SJeremy L Thompson   user->outputfreq = outputfreq;
1692ccaff030SJeremy L Thompson   user->contsteps = contsteps;
1693ccaff030SJeremy L Thompson   user->units = units;
1694ccaff030SJeremy L Thompson   user->dm = dm;
1695ccaff030SJeremy L Thompson   user->dmviz = dmviz;
1696ccaff030SJeremy L Thompson   user->interpviz = interpviz;
1697ccaff030SJeremy L Thompson   user->ceed = ceed;
1698ccaff030SJeremy L Thompson 
16998b982baeSLeila Ghaffari   // Calculate qdata and ICs
1700ccaff030SJeremy L Thompson   // Set up state global and local vectors
1701ccaff030SJeremy L Thompson   ierr = VecZeroEntries(Q); CHKERRQ(ierr);
1702ccaff030SJeremy L Thompson 
1703cfa64770SLeila Ghaffari   ierr = VectorPlacePetscVec(q0ceed, Qloc); CHKERRQ(ierr);
1704ccaff030SJeremy L Thompson 
1705ccaff030SJeremy L Thompson   // Apply Setup Ceed Operators
1706ccaff030SJeremy L Thompson   ierr = VectorPlacePetscVec(xcorners, Xloc); CHKERRQ(ierr);
17078b982baeSLeila Ghaffari   CeedOperatorApply(op_setupVol, xcorners, qdata, CEED_REQUEST_IMMEDIATE);
170884d34d69SLeila Ghaffari   ierr = ComputeLumpedMassMatrix(ceed, dm, restrictq, basisq, restrictqdi, qdata,
1709ccaff030SJeremy L Thompson                                  user->M); CHKERRQ(ierr);
1710ccaff030SJeremy L Thompson 
171184d34d69SLeila Ghaffari   ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qloc, Q, restrictq,
1712777ff853SJeremy L Thompson                              ctxSetup, 0.0); CHKERRQ(ierr);
1713ccaff030SJeremy L Thompson   if (1) { // Record boundary values from initial condition and override DMPlexInsertBoundaryValues()
1714ccaff030SJeremy L Thompson     // We use this for the main simulation DM because the reference DMPlexInsertBoundaryValues() is very slow.  If we
1715ccaff030SJeremy L Thompson     // disable this, we should still get the same results due to the problem->bc function, but with potentially much
1716ccaff030SJeremy L Thompson     // slower execution.
1717ccaff030SJeremy L Thompson     Vec Qbc;
1718ccaff030SJeremy L Thompson     ierr = DMGetNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1719ccaff030SJeremy L Thompson     ierr = VecCopy(Qloc, Qbc); CHKERRQ(ierr);
1720ccaff030SJeremy L Thompson     ierr = VecZeroEntries(Qloc); CHKERRQ(ierr);
1721ccaff030SJeremy L Thompson     ierr = DMGlobalToLocal(dm, Q, INSERT_VALUES, Qloc); CHKERRQ(ierr);
1722ccaff030SJeremy L Thompson     ierr = VecAXPY(Qbc, -1., Qloc); CHKERRQ(ierr);
1723ccaff030SJeremy L Thompson     ierr = DMRestoreNamedLocalVector(dm, "Qbc", &Qbc); CHKERRQ(ierr);
1724ccaff030SJeremy L Thompson     ierr = PetscObjectComposeFunction((PetscObject)dm,
172584d34d69SLeila Ghaffari                                       "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_NS);
172684d34d69SLeila Ghaffari     CHKERRQ(ierr);
1727ccaff030SJeremy L Thompson   }
1728ccaff030SJeremy L Thompson 
1729ccaff030SJeremy L Thompson   MPI_Comm_rank(comm, &rank);
1730d99129b9SLeila Ghaffari   if (!rank) {ierr = PetscMkdir(user->outputdir); CHKERRQ(ierr);}
1731ccaff030SJeremy L Thompson   // Gather initial Q values
1732ccaff030SJeremy L Thompson   // In case of continuation of simulation, set up initial values from binary file
1733ccaff030SJeremy L Thompson   if (contsteps) { // continue from existent solution
1734ccaff030SJeremy L Thompson     PetscViewer viewer;
1735ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1736ccaff030SJeremy L Thompson     // Read input
1737ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-solution.bin",
1738d99129b9SLeila Ghaffari                          user->outputdir);
1739ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1740ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1741ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1742ccaff030SJeremy L Thompson     ierr = VecLoad(Q, viewer); CHKERRQ(ierr);
1743ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1744ccaff030SJeremy L Thompson   }
1745ccaff030SJeremy L Thompson   ierr = DMRestoreLocalVector(dm, &Qloc); CHKERRQ(ierr);
1746ccaff030SJeremy L Thompson 
1747ccaff030SJeremy L Thompson // Create and setup TS
1748ccaff030SJeremy L Thompson   ierr = TSCreate(comm, &ts); CHKERRQ(ierr);
1749ccaff030SJeremy L Thompson   ierr = TSSetDM(ts, dm); CHKERRQ(ierr);
1750ccaff030SJeremy L Thompson   if (implicit) {
1751ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSBDF); CHKERRQ(ierr);
1752ccaff030SJeremy L Thompson     if (user->op_ifunction) {
1753ccaff030SJeremy L Thompson       ierr = TSSetIFunction(ts, NULL, IFunction_NS, &user); CHKERRQ(ierr);
1754ccaff030SJeremy L Thompson     } else {                    // Implicit integrators can fall back to using an RHSFunction
1755ccaff030SJeremy L Thompson       ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1756ccaff030SJeremy L Thompson     }
1757ccaff030SJeremy L Thompson   } else {
1758ccaff030SJeremy L Thompson     if (!user->op_rhs) SETERRQ(comm, PETSC_ERR_ARG_NULL,
1759ccaff030SJeremy L Thompson                                  "Problem does not provide RHSFunction");
1760ccaff030SJeremy L Thompson     ierr = TSSetType(ts, TSRK); CHKERRQ(ierr);
1761ccaff030SJeremy L Thompson     ierr = TSRKSetType(ts, TSRK5F); CHKERRQ(ierr);
1762ccaff030SJeremy L Thompson     ierr = TSSetRHSFunction(ts, NULL, RHS_NS, &user); CHKERRQ(ierr);
1763ccaff030SJeremy L Thompson   }
1764ccaff030SJeremy L Thompson   ierr = TSSetMaxTime(ts, 500. * units->second); CHKERRQ(ierr);
1765ccaff030SJeremy L Thompson   ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER); CHKERRQ(ierr);
1766ccaff030SJeremy L Thompson   ierr = TSSetTimeStep(ts, 1.e-2 * units->second); CHKERRQ(ierr);
1767dc8efd83SLeila Ghaffari   if (test) {ierr = TSSetMaxSteps(ts, 10); CHKERRQ(ierr);}
1768ccaff030SJeremy L Thompson   ierr = TSGetAdapt(ts, &adapt); CHKERRQ(ierr);
1769ccaff030SJeremy L Thompson   ierr = TSAdaptSetStepLimits(adapt,
1770ccaff030SJeremy L Thompson                               1.e-12 * units->second,
1771ccaff030SJeremy L Thompson                               1.e2 * units->second); CHKERRQ(ierr);
1772ccaff030SJeremy L Thompson   ierr = TSSetFromOptions(ts); CHKERRQ(ierr);
1773ccaff030SJeremy L Thompson   if (!contsteps) { // print initial condition
1774dc8efd83SLeila Ghaffari     if (!test) {
1775ccaff030SJeremy L Thompson       ierr = TSMonitor_NS(ts, 0, 0., Q, user); CHKERRQ(ierr);
1776ccaff030SJeremy L Thompson     }
1777ccaff030SJeremy L Thompson   } else { // continue from time of last output
1778ccaff030SJeremy L Thompson     PetscReal time;
1779ccaff030SJeremy L Thompson     PetscInt count;
1780ccaff030SJeremy L Thompson     PetscViewer viewer;
1781ccaff030SJeremy L Thompson     char filepath[PETSC_MAX_PATH_LEN];
1782ccaff030SJeremy L Thompson     ierr = PetscSNPrintf(filepath, sizeof filepath, "%s/ns-time.bin",
1783d99129b9SLeila Ghaffari                          user->outputdir); CHKERRQ(ierr);
1784ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
1785ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1786ccaff030SJeremy L Thompson     ierr = PetscViewerBinaryRead(viewer, &time, 1, &count, PETSC_REAL);
1787ccaff030SJeremy L Thompson     CHKERRQ(ierr);
1788ccaff030SJeremy L Thompson     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
1789ccaff030SJeremy L Thompson     ierr = TSSetTime(ts, time * user->units->second); CHKERRQ(ierr);
1790ccaff030SJeremy L Thompson   }
1791dc8efd83SLeila Ghaffari   if (!test) {
1792ccaff030SJeremy L Thompson     ierr = TSMonitorSet(ts, TSMonitor_NS, user, NULL); CHKERRQ(ierr);
1793ccaff030SJeremy L Thompson   }
1794ccaff030SJeremy L Thompson 
1795ccaff030SJeremy L Thompson   // Solve
1796ccaff030SJeremy L Thompson   start = MPI_Wtime();
1797ccaff030SJeremy L Thompson   ierr = PetscBarrier((PetscObject)ts); CHKERRQ(ierr);
1798ccaff030SJeremy L Thompson   ierr = TSSolve(ts, Q); CHKERRQ(ierr);
1799ccaff030SJeremy L Thompson   cpu_time_used = MPI_Wtime() - start;
1800ccaff030SJeremy L Thompson   ierr = TSGetSolveTime(ts, &ftime); CHKERRQ(ierr);
1801ccaff030SJeremy L Thompson   ierr = MPI_Allreduce(MPI_IN_PLACE, &cpu_time_used, 1, MPI_DOUBLE, MPI_MIN,
1802ccaff030SJeremy L Thompson                        comm); CHKERRQ(ierr);
1803dc8efd83SLeila Ghaffari   if (!test) {
1804ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
180584d34d69SLeila Ghaffari                        "Time taken for solution (sec): %g\n",
1806ccaff030SJeremy L Thompson                        (double)cpu_time_used); CHKERRQ(ierr);
1807ccaff030SJeremy L Thompson   }
1808ccaff030SJeremy L Thompson 
1809ccaff030SJeremy L Thompson   // Get error
1810dc8efd83SLeila Ghaffari   if (problem->non_zero_time && !test) {
1811ccaff030SJeremy L Thompson     Vec Qexact, Qexactloc;
1812ccaff030SJeremy L Thompson     PetscReal norm;
1813ccaff030SJeremy L Thompson     ierr = DMCreateGlobalVector(dm, &Qexact); CHKERRQ(ierr);
1814ccaff030SJeremy L Thompson     ierr = DMGetLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
1815ccaff030SJeremy L Thompson     ierr = VecGetSize(Qexactloc, &lnodes); CHKERRQ(ierr);
1816ccaff030SJeremy L Thompson 
181784d34d69SLeila Ghaffari     ierr = ICs_FixMultiplicity(op_ics, xcorners, q0ceed, dm, Qexactloc, Qexact,
1818777ff853SJeremy L Thompson                                restrictq, ctxSetup, ftime); CHKERRQ(ierr);
1819ccaff030SJeremy L Thompson 
1820ccaff030SJeremy L Thompson     ierr = VecAXPY(Q, -1.0, Qexact);  CHKERRQ(ierr);
1821ccaff030SJeremy L Thompson     ierr = VecNorm(Q, NORM_MAX, &norm); CHKERRQ(ierr);
1822cfa64770SLeila Ghaffari     CeedVectorDestroy(&q0ceed);
1823ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1824ccaff030SJeremy L Thompson                        "Max Error: %g\n",
1825ccaff030SJeremy L Thompson                        (double)norm); CHKERRQ(ierr);
182684d34d69SLeila Ghaffari     // Clean up vectors
182784d34d69SLeila Ghaffari     ierr = DMRestoreLocalVector(dm, &Qexactloc); CHKERRQ(ierr);
182884d34d69SLeila Ghaffari     ierr = VecDestroy(&Qexact); CHKERRQ(ierr);
1829ccaff030SJeremy L Thompson   }
1830ccaff030SJeremy L Thompson 
1831ccaff030SJeremy L Thompson   // Output Statistics
1832ccaff030SJeremy L Thompson   ierr = TSGetStepNumber(ts, &steps); CHKERRQ(ierr);
1833dc8efd83SLeila Ghaffari   if (!test) {
1834ccaff030SJeremy L Thompson     ierr = PetscPrintf(PETSC_COMM_WORLD,
1835ccaff030SJeremy L Thompson                        "Time integrator took %D time steps to reach final time %g\n",
1836ccaff030SJeremy L Thompson                        steps, (double)ftime); CHKERRQ(ierr);
1837ccaff030SJeremy L Thompson   }
183884d34d69SLeila Ghaffari   // Output numerical values from command line
183984d34d69SLeila Ghaffari   ierr = VecViewFromOptions(Q, NULL, "-vec_view"); CHKERRQ(ierr);
184084d34d69SLeila Ghaffari 
1841335ea220SLeila Ghaffari   // Compare reference solution values with current test run for CI
1842dc8efd83SLeila Ghaffari   if (test) {
184384d34d69SLeila Ghaffari     PetscViewer viewer;
184484d34d69SLeila Ghaffari     // Read reference file
184584d34d69SLeila Ghaffari     Vec Qref;
184684d34d69SLeila Ghaffari     PetscReal error, Qrefnorm;
184784d34d69SLeila Ghaffari     ierr = VecDuplicate(Q, &Qref); CHKERRQ(ierr);
1848dc8efd83SLeila Ghaffari     ierr = PetscViewerBinaryOpen(comm, filepath, FILE_MODE_READ, &viewer);
184984d34d69SLeila Ghaffari     CHKERRQ(ierr);
185084d34d69SLeila Ghaffari     ierr = VecLoad(Qref, viewer); CHKERRQ(ierr);
185184d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
185284d34d69SLeila Ghaffari 
185384d34d69SLeila Ghaffari     // Compute error with respect to reference solution
185484d34d69SLeila Ghaffari     ierr = VecAXPY(Q, -1.0, Qref);  CHKERRQ(ierr);
185584d34d69SLeila Ghaffari     ierr = VecNorm(Qref, NORM_MAX, &Qrefnorm); CHKERRQ(ierr);
185684d34d69SLeila Ghaffari     ierr = VecScale(Q, 1./Qrefnorm); CHKERRQ(ierr);
185784d34d69SLeila Ghaffari     ierr = VecNorm(Q, NORM_MAX, &error); CHKERRQ(ierr);
185884d34d69SLeila Ghaffari     ierr = VecDestroy(&Qref); CHKERRQ(ierr);
185984d34d69SLeila Ghaffari     // Check error
1860dc8efd83SLeila Ghaffari     if (error > testtol) {
186184d34d69SLeila Ghaffari       ierr = PetscPrintf(PETSC_COMM_WORLD,
186284d34d69SLeila Ghaffari                          "Test failed with error norm %g\n",
186384d34d69SLeila Ghaffari                          (double)error); CHKERRQ(ierr);
186484d34d69SLeila Ghaffari     }
186584d34d69SLeila Ghaffari     ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);
186684d34d69SLeila Ghaffari   }
18679cf88b28Svaleriabarra 
1868ccaff030SJeremy L Thompson   // Clean up libCEED
18698b982baeSLeila Ghaffari   CeedVectorDestroy(&qdata);
1870ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qceed);
1871ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->qdotceed);
1872ccaff030SJeremy L Thompson   CeedVectorDestroy(&user->gceed);
1873ccaff030SJeremy L Thompson   CeedVectorDestroy(&xcorners);
187484d34d69SLeila Ghaffari   CeedBasisDestroy(&basisq);
187584d34d69SLeila Ghaffari   CeedBasisDestroy(&basisx);
187684d34d69SLeila Ghaffari   CeedBasisDestroy(&basisxc);
187784d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictq);
187884d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictx);
187984d34d69SLeila Ghaffari   CeedElemRestrictionDestroy(&restrictqdi);
1880ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupVol);
1881ccaff030SJeremy L Thompson   CeedQFunctionDestroy(&qf_ics);
1882ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_rhsVol);
1883ea6e0f84SLeila Ghaffari   CeedQFunctionDestroy(&qf_ifunctionVol);
1884777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxSetup);
1885777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxNS);
1886777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxAdvection2d);
1887777ff853SJeremy L Thompson   CeedQFunctionContextDestroy(&ctxSurface);
1888ea6e0f84SLeila Ghaffari   CeedOperatorDestroy(&op_setupVol);
1889ccaff030SJeremy L Thompson   CeedOperatorDestroy(&op_ics);
18906a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_rhs_vol);
18916a0edaf9SLeila Ghaffari   CeedOperatorDestroy(&user->op_ifunction_vol);
18926a0edaf9SLeila Ghaffari   CeedDestroy(&ceed);
18936a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisqSur);
18946a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxSur);
18956a0edaf9SLeila Ghaffari   CeedBasisDestroy(&basisxcSur);
18966a0edaf9SLeila Ghaffari   CeedQFunctionDestroy(&qf_setupSur);
18977ed8b9c7SLeila Ghaffari   CeedQFunctionDestroy(&qf_applySur);
1898ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_rhs);
1899ccaff030SJeremy L Thompson   CeedOperatorDestroy(&user->op_ifunction);
1900ccaff030SJeremy L Thompson 
1901ccaff030SJeremy L Thompson   // Clean up PETSc
1902ccaff030SJeremy L Thompson   ierr = VecDestroy(&Q); CHKERRQ(ierr);
1903ccaff030SJeremy L Thompson   ierr = VecDestroy(&user->M); CHKERRQ(ierr);
1904ccaff030SJeremy L Thompson   ierr = MatDestroy(&interpviz); CHKERRQ(ierr);
1905ccaff030SJeremy L Thompson   ierr = DMDestroy(&dmviz); CHKERRQ(ierr);
1906ccaff030SJeremy L Thompson   ierr = TSDestroy(&ts); CHKERRQ(ierr);
1907ccaff030SJeremy L Thompson   ierr = DMDestroy(&dm); CHKERRQ(ierr);
1908ccaff030SJeremy L Thompson   ierr = PetscFree(units); CHKERRQ(ierr);
1909ccaff030SJeremy L Thompson   ierr = PetscFree(user); CHKERRQ(ierr);
1910ccaff030SJeremy L Thompson   return PetscFinalize();
1911ccaff030SJeremy L Thompson }
1912