xref: /petsc/src/ts/tutorials/ex76.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
1649ef022SMatthew Knepley static char help[] = "Time-dependent Low Mach Flow in 2d and 3d channels with finite elements.\n\
2444129b9SMatthew G. Knepley We solve the Low Mach flow problem for both conducting and non-conducting fluids,\n\
3444129b9SMatthew G. Knepley using a parallel unstructured mesh (DMPLEX) to discretize it.\n\n\n";
4649ef022SMatthew Knepley 
5649ef022SMatthew Knepley /*F
6444129b9SMatthew G. Knepley The non-conducting Low Mach flow is time-dependent isoviscous Navier-Stokes flow. We discretize using the
7649ef022SMatthew Knepley finite element method on an unstructured mesh. The weak form equations are
8649ef022SMatthew Knepley 
9649ef022SMatthew Knepley \begin{align*}
10649ef022SMatthew Knepley     < q, \nabla\cdot u > = 0
11649ef022SMatthew Knepley     <v, du/dt> + <v, u \cdot \nabla u> + < \nabla v, \nu (\nabla u + {\nabla u}^T) > - < \nabla\cdot v, p >  - < v, f  >  = 0
12649ef022SMatthew Knepley     < w, u \cdot \nabla T > + < \nabla w, \alpha \nabla T > - < w, Q > = 0
13649ef022SMatthew Knepley \end{align*}
14649ef022SMatthew Knepley 
15649ef022SMatthew Knepley where $\nu$ is the kinematic viscosity and $\alpha$ is thermal diffusivity.
16649ef022SMatthew Knepley 
17444129b9SMatthew G. Knepley The conducting form is given in the ABLATE documentation [1,2] and derived in Principe and Codina [2].
18444129b9SMatthew G. Knepley 
19649ef022SMatthew Knepley For visualization, use
20649ef022SMatthew Knepley 
21649ef022SMatthew Knepley   -dm_view hdf5:$PWD/sol.h5 -sol_vec_view hdf5:$PWD/sol.h5::append -exact_vec_view hdf5:$PWD/sol.h5::append
22444129b9SMatthew G. Knepley 
234e6a9dc0SMatthew Knepley To look at nonlinear solver convergence, use
244e6a9dc0SMatthew Knepley 
254e6a9dc0SMatthew Knepley   -dm_refine <k> -ts_max_steps 1 \
264e6a9dc0SMatthew Knepley   -ts_view -ts_monitor -snes_monitor -snes_converged_reason -ksp_converged_reason -fieldsplit_pressure_ksp_converged_reason
274e6a9dc0SMatthew Knepley 
28444129b9SMatthew G. Knepley [1] https://ubchrest.github.io/ablate/content/formulations/lowMachFlow/
29444129b9SMatthew G. Knepley [2] https://github.com/UBCHREST/ablate/blob/main/ablateCore/flow/lowMachFlow.c
30444129b9SMatthew G. Knepley [3] J. Principe and R. Codina, "Mathematical models for thermally coupled low speed flows", Adv. in Theo. and App. Mech., 2(1), pp.93--112, 2009.
31649ef022SMatthew Knepley F*/
32649ef022SMatthew Knepley 
33649ef022SMatthew Knepley #include <petscdmplex.h>
34649ef022SMatthew Knepley #include <petscsnes.h>
35649ef022SMatthew Knepley #include <petscts.h>
36649ef022SMatthew Knepley #include <petscds.h>
37649ef022SMatthew Knepley #include <petscbag.h>
38649ef022SMatthew Knepley 
39444129b9SMatthew G. Knepley typedef enum {MOD_INCOMPRESSIBLE, MOD_CONDUCTING, NUM_MOD_TYPES} ModType;
40444129b9SMatthew G. Knepley const char *modTypes[NUM_MOD_TYPES+1] = {"incompressible", "conducting", "unknown"};
41444129b9SMatthew G. Knepley 
42367970cfSMatthew G. Knepley typedef enum {SOL_QUADRATIC, SOL_CUBIC, SOL_CUBIC_TRIG, SOL_TAYLOR_GREEN, SOL_PIPE, SOL_PIPE_WIGGLY, NUM_SOL_TYPES} SolType;
43367970cfSMatthew G. Knepley const char *solTypes[NUM_SOL_TYPES+1] = {"quadratic", "cubic", "cubic_trig", "taylor_green", "pipe", "pipe_wiggly", "unknown"};
44444129b9SMatthew G. Knepley 
45444129b9SMatthew G. Knepley /* Fields */
46444129b9SMatthew G. Knepley const PetscInt VEL      = 0;
47444129b9SMatthew G. Knepley const PetscInt PRES     = 1;
48444129b9SMatthew G. Knepley const PetscInt TEMP     = 2;
49444129b9SMatthew G. Knepley /* Sources */
50444129b9SMatthew G. Knepley const PetscInt MOMENTUM = 0;
51444129b9SMatthew G. Knepley const PetscInt MASS     = 1;
52444129b9SMatthew G. Knepley const PetscInt ENERGY   = 2;
53444129b9SMatthew G. Knepley /* Constants */
54444129b9SMatthew G. Knepley const PetscInt STROUHAL = 0;
55444129b9SMatthew G. Knepley const PetscInt FROUDE   = 1;
56444129b9SMatthew G. Knepley const PetscInt REYNOLDS = 2;
57444129b9SMatthew G. Knepley const PetscInt PECLET   = 3;
58444129b9SMatthew G. Knepley const PetscInt P_TH     = 4;
59444129b9SMatthew G. Knepley const PetscInt MU       = 5;
60444129b9SMatthew G. Knepley const PetscInt NU       = 6;
61444129b9SMatthew G. Knepley const PetscInt C_P      = 7;
62444129b9SMatthew G. Knepley const PetscInt K        = 8;
63444129b9SMatthew G. Knepley const PetscInt ALPHA    = 9;
64444129b9SMatthew G. Knepley const PetscInt T_IN     = 10;
65444129b9SMatthew G. Knepley const PetscInt G_DIR    = 11;
66367970cfSMatthew G. Knepley const PetscInt EPSILON  = 12;
67649ef022SMatthew Knepley 
68649ef022SMatthew Knepley typedef struct {
69444129b9SMatthew G. Knepley   PetscReal Strouhal; /* Strouhal number */
70444129b9SMatthew G. Knepley   PetscReal Froude;   /* Froude number */
71444129b9SMatthew G. Knepley   PetscReal Reynolds; /* Reynolds number */
72444129b9SMatthew G. Knepley   PetscReal Peclet;   /* Peclet number */
73444129b9SMatthew G. Knepley   PetscReal p_th;     /* Thermodynamic pressure */
74444129b9SMatthew G. Knepley   PetscReal mu;       /* Dynamic viscosity */
75649ef022SMatthew Knepley   PetscReal nu;       /* Kinematic viscosity */
76444129b9SMatthew G. Knepley   PetscReal c_p;      /* Specific heat at constant pressure */
77444129b9SMatthew G. Knepley   PetscReal k;        /* Thermal conductivity */
78649ef022SMatthew Knepley   PetscReal alpha;    /* Thermal diffusivity */
79649ef022SMatthew Knepley   PetscReal T_in;     /* Inlet temperature */
80444129b9SMatthew G. Knepley   PetscReal g_dir;    /* Gravity direction */
81367970cfSMatthew G. Knepley   PetscReal epsilon;  /* Strength of perturbation */
82649ef022SMatthew Knepley } Parameter;
83649ef022SMatthew Knepley 
84649ef022SMatthew Knepley typedef struct {
85649ef022SMatthew Knepley   /* Problem definition */
86649ef022SMatthew Knepley   PetscBag  bag;          /* Holds problem parameters */
87444129b9SMatthew G. Knepley   ModType   modType;      /* Model type */
88649ef022SMatthew Knepley   SolType   solType;      /* MMS solution type */
89444129b9SMatthew G. Knepley   PetscBool hasNullSpace; /* Problem has the constant null space for pressure */
90a712f3bbSMatthew G. Knepley   /* Flow diagnostics */
91a712f3bbSMatthew G. Knepley   DM        dmCell;       /* A DM with piecewise constant discretization */
92649ef022SMatthew Knepley } AppCtx;
93649ef022SMatthew Knepley 
94649ef022SMatthew Knepley static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
95649ef022SMatthew Knepley {
96649ef022SMatthew Knepley   PetscInt d;
97649ef022SMatthew Knepley   for (d = 0; d < Nc; ++d) u[d] = 0.0;
98649ef022SMatthew Knepley   return 0;
99649ef022SMatthew Knepley }
100649ef022SMatthew Knepley 
101649ef022SMatthew Knepley static PetscErrorCode constant(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
102649ef022SMatthew Knepley {
103649ef022SMatthew Knepley   PetscInt d;
104649ef022SMatthew Knepley   for (d = 0; d < Nc; ++d) u[d] = 1.0;
105649ef022SMatthew Knepley   return 0;
106649ef022SMatthew Knepley }
107649ef022SMatthew Knepley 
108649ef022SMatthew Knepley /*
109649ef022SMatthew Knepley   CASE: quadratic
110649ef022SMatthew Knepley   In 2D we use exact solution:
111649ef022SMatthew Knepley 
112649ef022SMatthew Knepley     u = t + x^2 + y^2
113649ef022SMatthew Knepley     v = t + 2x^2 - 2xy
114649ef022SMatthew Knepley     p = x + y - 1
115444129b9SMatthew G. Knepley     T = t + x + y + 1
116649ef022SMatthew Knepley     f = <t (2x + 2y) + 2x^3 + 4x^2y - 2xy^2 -4\nu + 2, t (2x - 2y) + 4xy^2 + 2x^2y - 2y^3 -4\nu + 2>
117649ef022SMatthew Knepley     Q = 1 + 2t + 3x^2 - 2xy + y^2
118649ef022SMatthew Knepley 
119649ef022SMatthew Knepley   so that
120649ef022SMatthew Knepley 
121649ef022SMatthew Knepley     \nabla \cdot u = 2x - 2x = 0
122649ef022SMatthew Knepley 
123649ef022SMatthew Knepley   f = du/dt + u \cdot \nabla u - \nu \Delta u + \nabla p
124649ef022SMatthew Knepley     = <1, 1> + <t + x^2 + y^2, t + 2x^2 - 2xy> . <<2x, 4x - 2y>, <2y, -2x>> - \nu <4, 4> + <1, 1>
125649ef022SMatthew Knepley     = <t (2x + 2y) + 2x^3 + 4x^2y - 2xy^2, t (2x - 2y) + 2x^2y + 4xy^2 - 2y^3> + <-4 \nu + 2, -4\nu + 2>
126649ef022SMatthew Knepley     = <t (2x + 2y) + 2x^3 + 4x^2y - 2xy^2 - 4\nu + 2, t (2x - 2y) + 4xy^2 + 2x^2y - 2y^3 - 4\nu + 2>
127649ef022SMatthew Knepley 
128649ef022SMatthew Knepley   Q = dT/dt + u \cdot \nabla T - \alpha \Delta T
129649ef022SMatthew Knepley     = 1 + <t + x^2 + y^2, t + 2x^2 - 2xy> . <1, 1> - \alpha 0
130649ef022SMatthew Knepley     = 1 + 2t + 3x^2 - 2xy + y^2
131649ef022SMatthew Knepley */
132649ef022SMatthew Knepley 
133649ef022SMatthew Knepley static PetscErrorCode quadratic_u(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
134649ef022SMatthew Knepley {
135649ef022SMatthew Knepley   u[0] = time + X[0]*X[0] + X[1]*X[1];
136649ef022SMatthew Knepley   u[1] = time + 2.0*X[0]*X[0] - 2.0*X[0]*X[1];
137649ef022SMatthew Knepley   return 0;
138649ef022SMatthew Knepley }
139649ef022SMatthew Knepley static PetscErrorCode quadratic_u_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
140649ef022SMatthew Knepley {
141649ef022SMatthew Knepley   u[0] = 1.0;
142649ef022SMatthew Knepley   u[1] = 1.0;
143649ef022SMatthew Knepley   return 0;
144649ef022SMatthew Knepley }
145649ef022SMatthew Knepley 
146649ef022SMatthew Knepley static PetscErrorCode quadratic_p(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
147649ef022SMatthew Knepley {
148649ef022SMatthew Knepley   p[0] = X[0] + X[1] - 1.0;
149649ef022SMatthew Knepley   return 0;
150649ef022SMatthew Knepley }
151649ef022SMatthew Knepley 
152649ef022SMatthew Knepley static PetscErrorCode quadratic_T(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
153649ef022SMatthew Knepley {
154444129b9SMatthew G. Knepley   T[0] = time + X[0] + X[1] + 1.0;
155649ef022SMatthew Knepley   return 0;
156649ef022SMatthew Knepley }
157649ef022SMatthew Knepley static PetscErrorCode quadratic_T_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
158649ef022SMatthew Knepley {
159649ef022SMatthew Knepley   T[0] = 1.0;
160649ef022SMatthew Knepley   return 0;
161649ef022SMatthew Knepley }
162649ef022SMatthew Knepley 
163649ef022SMatthew Knepley static void f0_quadratic_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
164649ef022SMatthew Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
165649ef022SMatthew Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
166649ef022SMatthew Knepley                            PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
167649ef022SMatthew Knepley {
168444129b9SMatthew G. Knepley   const PetscReal nu = PetscRealPart(constants[NU]);
169649ef022SMatthew Knepley 
170444129b9SMatthew G. Knepley   f0[0] -= t*(2*X[0] + 2*X[1]) + 2*X[0]*X[0]*X[0] + 4*X[0]*X[0]*X[1] - 2*X[0]*X[1]*X[1] - 4.0*nu + 2;
171444129b9SMatthew G. Knepley   f0[1] -= t*(2*X[0] - 2*X[1]) + 4*X[0]*X[1]*X[1] + 2*X[0]*X[0]*X[1] - 2*X[1]*X[1]*X[1] - 4.0*nu + 2;
172649ef022SMatthew Knepley }
173649ef022SMatthew Knepley 
174649ef022SMatthew Knepley static void f0_quadratic_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
175649ef022SMatthew Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
176649ef022SMatthew Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
177649ef022SMatthew Knepley                            PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
178649ef022SMatthew Knepley {
179444129b9SMatthew G. Knepley   f0[0] -= 2*t + 1 + 3*X[0]*X[0] - 2*X[0]*X[1] + X[1]*X[1];
180444129b9SMatthew G. Knepley }
181444129b9SMatthew G. Knepley 
182444129b9SMatthew G. Knepley /*
183444129b9SMatthew G. Knepley   CASE: quadratic
184444129b9SMatthew G. Knepley   In 2D we use exact solution:
185444129b9SMatthew G. Knepley 
186444129b9SMatthew G. Knepley     u = t + x^2 + y^2
187444129b9SMatthew G. Knepley     v = t + 2x^2 - 2xy
188444129b9SMatthew G. Knepley     p = x + y - 1
189444129b9SMatthew G. Knepley     T = t + x + y + 1
190444129b9SMatthew G. Knepley   rho = p^{th} / T
191444129b9SMatthew G. Knepley 
192444129b9SMatthew G. Knepley   so that
193444129b9SMatthew G. Knepley 
194444129b9SMatthew G. Knepley     \nabla \cdot u = 2x - 2x = 0
195444129b9SMatthew G. Knepley     grad u = <<2 x, 4x - 2y>, <2 y, -2x>>
196444129b9SMatthew G. Knepley     epsilon(u) = 1/2 (grad u + grad u^T) = <<2x, 2x>, <2x, -2x>>
197444129b9SMatthew G. Knepley     epsilon'(u) = epsilon(u) - 1/3 (div u) I = epsilon(u)
198444129b9SMatthew G. Knepley     div epsilon'(u) = <2, 2>
199444129b9SMatthew G. Knepley 
200444129b9SMatthew G. Knepley   f = rho S du/dt + rho u \cdot \nabla u - 2\mu/Re div \epsilon'(u) + \nabla p + rho / F^2 \hat y
201444129b9SMatthew G. Knepley     = rho S <1, 1> + rho <t + x^2 + y^2, t + 2x^2 - 2xy> . <<2x, 4x - 2y>, <2y, -2x>> - 2\mu/Re <2, 2> + <1, 1> + rho/F^2 <0, 1>
202444129b9SMatthew G. Knepley     = rho S <1, 1> + rho <t (2x + 2y) + 2x^3 + 4x^2y - 2xy^2, t (2x - 2y) + 2x^2y + 4xy^2 - 2y^3> - mu/Re <4, 4> + <1, 1> + rho/F^2 <0, 1>
203444129b9SMatthew G. Knepley 
204444129b9SMatthew G. Knepley   g = S rho_t + div (rho u)
205444129b9SMatthew G. Knepley     = -S pth T_t/T^2 + rho div (u) + u . grad rho
206444129b9SMatthew G. Knepley     = -S pth 1/T^2 - pth u . grad T / T^2
207444129b9SMatthew G. Knepley     = -pth / T^2 (S + 2t + 3 x^2 - 2xy + y^2)
208444129b9SMatthew G. Knepley 
209444129b9SMatthew G. Knepley   Q = rho c_p S dT/dt + rho c_p u . grad T - 1/Pe div k grad T
210444129b9SMatthew G. Knepley     = c_p S pth / T + c_p pth (2t + 3 x^2 - 2xy + y^2) / T - k/Pe 0
211444129b9SMatthew G. Knepley     = c_p pth / T (S + 2t + 3 x^2 - 2xy + y^2)
212444129b9SMatthew G. Knepley */
213444129b9SMatthew G. Knepley static void f0_conduct_quadratic_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
214444129b9SMatthew G. Knepley                                    const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
215444129b9SMatthew G. Knepley                                    const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
216444129b9SMatthew G. Knepley                                    PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
217444129b9SMatthew G. Knepley {
218444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
219444129b9SMatthew G. Knepley   const PetscReal F    = PetscRealPart(constants[FROUDE]);
220444129b9SMatthew G. Knepley   const PetscReal Re   = PetscRealPart(constants[REYNOLDS]);
221444129b9SMatthew G. Knepley   const PetscReal mu   = PetscRealPart(constants[MU]);
222444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
223444129b9SMatthew G. Knepley   const PetscReal rho  = p_th / (t + X[0] + X[1] + 1.);
224444129b9SMatthew G. Knepley   const PetscInt  gd   = (PetscInt) PetscRealPart(constants[G_DIR]);
225444129b9SMatthew G. Knepley 
226444129b9SMatthew G. Knepley   f0[0]  -= rho * S + rho * (2.*t*(X[0] + X[1]) + 2.*X[0]*X[0]*X[0] + 4.*X[0]*X[0]*X[1] - 2.*X[0]*X[1]*X[1]) - 4.*mu/Re + 1.;
227444129b9SMatthew G. Knepley   f0[1]  -= rho * S + rho * (2.*t*(X[0] - X[1]) + 2.*X[0]*X[0]*X[1] + 4.*X[0]*X[1]*X[1] - 2.*X[1]*X[1]*X[1]) - 4.*mu/Re + 1.;
228444129b9SMatthew G. Knepley   f0[gd] -= rho/PetscSqr(F);
229444129b9SMatthew G. Knepley }
230444129b9SMatthew G. Knepley 
231444129b9SMatthew G. Knepley static void f0_conduct_quadratic_q(PetscInt dim, PetscInt Nf, PetscInt NfAux,
232444129b9SMatthew G. Knepley                                    const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
233444129b9SMatthew G. Knepley                                    const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
234444129b9SMatthew G. Knepley                                    PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
235444129b9SMatthew G. Knepley {
236444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
237444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
238444129b9SMatthew G. Knepley 
239444129b9SMatthew G. Knepley   f0[0] += p_th * (S + 2.*t + 3.*X[0]*X[0] - 2.*X[0]*X[1] + X[1]*X[1]) / PetscSqr(t + X[0] + X[1] + 1.);
240444129b9SMatthew G. Knepley }
241444129b9SMatthew G. Knepley 
242444129b9SMatthew G. Knepley static void f0_conduct_quadratic_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
243444129b9SMatthew G. Knepley                                    const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
244444129b9SMatthew G. Knepley                                    const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
245444129b9SMatthew G. Knepley                                    PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
246444129b9SMatthew G. Knepley {
247444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
248444129b9SMatthew G. Knepley   const PetscReal c_p  = PetscRealPart(constants[C_P]);
249444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
250444129b9SMatthew G. Knepley 
251444129b9SMatthew G. Knepley   f0[0] -= c_p*p_th * (S + 2.*t + 3.*X[0]*X[0] - 2.*X[0]*X[1] + X[1]*X[1]) / (t + X[0] + X[1] + 1.);
252649ef022SMatthew Knepley }
253649ef022SMatthew Knepley 
254649ef022SMatthew Knepley /*
255649ef022SMatthew Knepley   CASE: cubic
256649ef022SMatthew Knepley   In 2D we use exact solution:
257649ef022SMatthew Knepley 
258649ef022SMatthew Knepley     u = t + x^3 + y^3
259649ef022SMatthew Knepley     v = t + 2x^3 - 3x^2y
260649ef022SMatthew Knepley     p = 3/2 x^2 + 3/2 y^2 - 1
261649ef022SMatthew Knepley     T = t + 1/2 x^2 + 1/2 y^2
262649ef022SMatthew Knepley     f = < t(3x^2 + 3y^2) + 3x^5 + 6x^3y^2 - 6x^2y^3 - \nu(6x + 6y) + 3x + 1,
263649ef022SMatthew Knepley           t(3x^2 - 6xy) + 6x^2y^3 + 3x^4y - 6xy^4 - \nu(12x - 6y) + 3y + 1>
264649ef022SMatthew Knepley     Q = x^4 + xy^3 + 2x^3y - 3x^2y^2 + xt + yt - 2\alpha + 1
265649ef022SMatthew Knepley 
266649ef022SMatthew Knepley   so that
267649ef022SMatthew Knepley 
268649ef022SMatthew Knepley     \nabla \cdot u = 3x^2 - 3x^2 = 0
269649ef022SMatthew Knepley 
270649ef022SMatthew Knepley   du/dt + u \cdot \nabla u - \nu \Delta u + \nabla p - f
271649ef022SMatthew Knepley   = <1,1> + <t(3x^2 + 3y^2) + 3x^5 + 6x^3y^2 - 6x^2y^3, t(3x^2 - 6xy) + 6x^2y^3 + 3x^4y - 6xy^4> - \nu<6x + 6y, 12x - 6y> + <3x, 3y> - <t(3x^2 + 3y^2) + 3x^5 + 6x^3y^2 - 6x^2y^3 - \nu(6x + 6y) + 3x + 1, t(3x^2 - 6xy) + 6x^2y^3 + 3x^4y - 6xy^4 - \nu(12x - 6y) + 3y + 1>  = 0
272649ef022SMatthew Knepley 
273649ef022SMatthew Knepley   dT/dt + u \cdot \nabla T - \alpha \Delta T - Q = 1 + (x^3 + y^3) x + (2x^3 - 3x^2y) y - 2*\alpha - (x^4 + xy^3 + 2x^3y - 3x^2y^2 - 2*\alpha +1)   = 0
274649ef022SMatthew Knepley */
275649ef022SMatthew Knepley static PetscErrorCode cubic_u(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
276649ef022SMatthew Knepley {
277649ef022SMatthew Knepley   u[0] = time + X[0]*X[0]*X[0] + X[1]*X[1]*X[1];
278649ef022SMatthew Knepley   u[1] = time + 2.0*X[0]*X[0]*X[0] - 3.0*X[0]*X[0]*X[1];
279649ef022SMatthew Knepley   return 0;
280649ef022SMatthew Knepley }
281649ef022SMatthew Knepley static PetscErrorCode cubic_u_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
282649ef022SMatthew Knepley {
283649ef022SMatthew Knepley   u[0] = 1.0;
284649ef022SMatthew Knepley   u[1] = 1.0;
285649ef022SMatthew Knepley   return 0;
286649ef022SMatthew Knepley }
287649ef022SMatthew Knepley 
288649ef022SMatthew Knepley static PetscErrorCode cubic_p(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
289649ef022SMatthew Knepley {
290649ef022SMatthew Knepley   p[0] = 3.0*X[0]*X[0]/2.0 + 3.0*X[1]*X[1]/2.0 - 1.0;
291649ef022SMatthew Knepley   return 0;
292649ef022SMatthew Knepley }
293649ef022SMatthew Knepley 
294649ef022SMatthew Knepley static PetscErrorCode cubic_T(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
295649ef022SMatthew Knepley {
296649ef022SMatthew Knepley   T[0] = time + X[0]*X[0]/2.0 + X[1]*X[1]/2.0;
297649ef022SMatthew Knepley   return 0;
298649ef022SMatthew Knepley }
299649ef022SMatthew Knepley static PetscErrorCode cubic_T_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
300649ef022SMatthew Knepley {
301649ef022SMatthew Knepley   T[0] = 1.0;
302649ef022SMatthew Knepley   return 0;
303649ef022SMatthew Knepley }
304649ef022SMatthew Knepley 
305649ef022SMatthew Knepley static void f0_cubic_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
306649ef022SMatthew Knepley                        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
307649ef022SMatthew Knepley                        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
308649ef022SMatthew Knepley                        PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
309649ef022SMatthew Knepley {
310444129b9SMatthew G. Knepley   const PetscReal nu = PetscRealPart(constants[NU]);
311649ef022SMatthew Knepley 
312649ef022SMatthew Knepley   f0[0] -= (t*(3*X[0]*X[0] + 3*X[1]*X[1]) + 3*X[0]*X[0]*X[0]*X[0]*X[0] + 6*X[0]*X[0]*X[0]*X[1]*X[1] - 6*X[0]*X[0]*X[1]*X[1]*X[1] - ( 6*X[0] + 6*X[1])*nu + 3*X[0] + 1);
313649ef022SMatthew Knepley   f0[1] -= (t*(3*X[0]*X[0] - 6*X[0]*X[1]) + 3*X[0]*X[0]*X[0]*X[0]*X[1] + 6*X[0]*X[0]*X[1]*X[1]*X[1] - 6*X[0]*X[1]*X[1]*X[1]*X[1] - (12*X[0] - 6*X[1])*nu + 3*X[1] + 1);
314649ef022SMatthew Knepley }
315649ef022SMatthew Knepley 
316649ef022SMatthew Knepley static void f0_cubic_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
317649ef022SMatthew Knepley                        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
318649ef022SMatthew Knepley                        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
319649ef022SMatthew Knepley                        PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
320649ef022SMatthew Knepley {
321444129b9SMatthew G. Knepley   const PetscReal alpha = PetscRealPart(constants[ALPHA]);
322649ef022SMatthew Knepley 
323444129b9SMatthew G. Knepley   f0[0] -= X[0]*X[0]*X[0]*X[0] + 2.0*X[0]*X[0]*X[0]*X[1] - 3.0*X[0]*X[0]*X[1]*X[1] + X[0]*X[1]*X[1]*X[1] + X[0]*t + X[1]*t - 2.0*alpha + 1;
324649ef022SMatthew Knepley }
325649ef022SMatthew Knepley 
326649ef022SMatthew Knepley /*
327649ef022SMatthew Knepley   CASE: cubic-trigonometric
328649ef022SMatthew Knepley   In 2D we use exact solution:
329649ef022SMatthew Knepley 
330649ef022SMatthew Knepley     u = beta cos t + x^3 + y^3
331649ef022SMatthew Knepley     v = beta sin t + 2x^3 - 3x^2y
332649ef022SMatthew Knepley     p = 3/2 x^2 + 3/2 y^2 - 1
333649ef022SMatthew Knepley     T = 20 cos t + 1/2 x^2 + 1/2 y^2
334649ef022SMatthew Knepley     f = < beta cos t 3x^2         + beta sin t (3y^2 - 1) + 3x^5 + 6x^3y^2 - 6x^2y^3 - \nu(6x + 6y)  + 3x,
335649ef022SMatthew Knepley           beta cos t (6x^2 - 6xy) - beta sin t (3x^2)     + 3x^4y + 6x^2y^3 - 6xy^4  - \nu(12x - 6y) + 3y>
336649ef022SMatthew Knepley     Q = beta cos t x + beta sin t (y - 1) + x^4 + 2x^3y - 3x^2y^2 + xy^3 - 2\alpha
337649ef022SMatthew Knepley 
338649ef022SMatthew Knepley   so that
339649ef022SMatthew Knepley 
340649ef022SMatthew Knepley     \nabla \cdot u = 3x^2 - 3x^2 = 0
341649ef022SMatthew Knepley 
342649ef022SMatthew Knepley   f = du/dt + u \cdot \nabla u - \nu \Delta u + \nabla p
343649ef022SMatthew Knepley     = <-sin t, cos t> + <cos t + x^3 + y^3, sin t + 2x^3 - 3x^2y> <<3x^2, 6x^2 - 6xy>, <3y^2, -3x^2>> - \nu <6x + 6y, 12x - 6y> + <3x, 3y>
344649ef022SMatthew Knepley     = <-sin t, cos t> + <cos t 3x^2 + 3x^5 + 3x^2y^3 + sin t 3y^2 + 6x^3y^2 - 9x^2y^3, cos t (6x^2 - 6xy) + 6x^5 - 6x^4y + 6x^2y^3 - 6xy^4 + sin t (-3x^2) - 6x^5 + 9x^4y> - \nu <6x + 6y, 12x - 6y> + <3x, 3y>
345649ef022SMatthew Knepley     = <cos t (3x^2)       + sin t (3y^2 - 1) + 3x^5 + 6x^3y^2 - 6x^2y^3 - \nu (6x + 6y)  + 3x,
346649ef022SMatthew Knepley        cos t (6x^2 - 6xy) - sin t (3x^2)     + 3x^4y + 6x^2y^3 - 6xy^4  - \nu (12x - 6y) + 3y>
347649ef022SMatthew Knepley 
348649ef022SMatthew Knepley   Q = dT/dt + u \cdot \nabla T - \alpha \Delta T
349649ef022SMatthew Knepley     = -sin t + <cos t + x^3 + y^3, sin t + 2x^3 - 3x^2y> . <x, y> - 2 \alpha
350649ef022SMatthew Knepley     = -sin t + cos t (x) + x^4 + xy^3 + sin t (y) + 2x^3y - 3x^2y^2 - 2 \alpha
351649ef022SMatthew Knepley     = cos t x + sin t (y - 1) + (x^4 + 2x^3y - 3x^2y^2 + xy^3 - 2 \alpha)
352649ef022SMatthew Knepley */
353649ef022SMatthew Knepley static PetscErrorCode cubic_trig_u(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
354649ef022SMatthew Knepley {
355649ef022SMatthew Knepley   u[0] = 100.*PetscCosReal(time) + X[0]*X[0]*X[0] + X[1]*X[1]*X[1];
356649ef022SMatthew Knepley   u[1] = 100.*PetscSinReal(time) + 2.0*X[0]*X[0]*X[0] - 3.0*X[0]*X[0]*X[1];
357649ef022SMatthew Knepley   return 0;
358649ef022SMatthew Knepley }
359649ef022SMatthew Knepley static PetscErrorCode cubic_trig_u_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
360649ef022SMatthew Knepley {
361649ef022SMatthew Knepley   u[0] = -100.*PetscSinReal(time);
362649ef022SMatthew Knepley   u[1] =  100.*PetscCosReal(time);
363649ef022SMatthew Knepley   return 0;
364649ef022SMatthew Knepley }
365649ef022SMatthew Knepley 
366649ef022SMatthew Knepley static PetscErrorCode cubic_trig_p(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
367649ef022SMatthew Knepley {
368649ef022SMatthew Knepley   p[0] = 3.0*X[0]*X[0]/2.0 + 3.0*X[1]*X[1]/2.0 - 1.0;
369649ef022SMatthew Knepley   return 0;
370649ef022SMatthew Knepley }
371649ef022SMatthew Knepley 
372649ef022SMatthew Knepley static PetscErrorCode cubic_trig_T(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
373649ef022SMatthew Knepley {
374649ef022SMatthew Knepley   T[0] = 100.*PetscCosReal(time) + X[0]*X[0]/2.0 + X[1]*X[1]/2.0;
375649ef022SMatthew Knepley   return 0;
376649ef022SMatthew Knepley }
377649ef022SMatthew Knepley static PetscErrorCode cubic_trig_T_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
378649ef022SMatthew Knepley {
379649ef022SMatthew Knepley   T[0] = -100.*PetscSinReal(time);
380649ef022SMatthew Knepley   return 0;
381649ef022SMatthew Knepley }
382649ef022SMatthew Knepley 
383649ef022SMatthew Knepley static void f0_cubic_trig_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
384649ef022SMatthew Knepley                             const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
385649ef022SMatthew Knepley                             const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
386649ef022SMatthew Knepley                             PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
387649ef022SMatthew Knepley {
388444129b9SMatthew G. Knepley   const PetscReal nu = PetscRealPart(constants[NU]);
389649ef022SMatthew Knepley 
390649ef022SMatthew Knepley   f0[0] -= 100.*PetscCosReal(t)*(3*X[0]*X[0])               + 100.*PetscSinReal(t)*(3*X[1]*X[1] - 1.) + 3*X[0]*X[0]*X[0]*X[0]*X[0] + 6*X[0]*X[0]*X[0]*X[1]*X[1] - 6*X[0]*X[0]*X[1]*X[1]*X[1] - ( 6*X[0] + 6*X[1])*nu + 3*X[0];
391649ef022SMatthew Knepley   f0[1] -= 100.*PetscCosReal(t)*(6*X[0]*X[0] - 6*X[0]*X[1]) - 100.*PetscSinReal(t)*(3*X[0]*X[0])      + 3*X[0]*X[0]*X[0]*X[0]*X[1] + 6*X[0]*X[0]*X[1]*X[1]*X[1] - 6*X[0]*X[1]*X[1]*X[1]*X[1] - (12*X[0] - 6*X[1])*nu + 3*X[1];
392649ef022SMatthew Knepley }
393649ef022SMatthew Knepley 
394649ef022SMatthew Knepley static void f0_cubic_trig_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
395649ef022SMatthew Knepley                             const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
396649ef022SMatthew Knepley                             const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
397649ef022SMatthew Knepley                             PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
398649ef022SMatthew Knepley {
399444129b9SMatthew G. Knepley   const PetscReal alpha = PetscRealPart(constants[ALPHA]);
400649ef022SMatthew Knepley 
401444129b9SMatthew G. Knepley   f0[0] -= 100.*PetscCosReal(t)*X[0] + 100.*PetscSinReal(t)*(X[1] - 1.) + X[0]*X[0]*X[0]*X[0] + 2.0*X[0]*X[0]*X[0]*X[1] - 3.0*X[0]*X[0]*X[1]*X[1] + X[0]*X[1]*X[1]*X[1] - 2.0*alpha;
402649ef022SMatthew Knepley }
403649ef022SMatthew Knepley 
404606d57d4SMatthew G. Knepley /*
405444129b9SMatthew G. Knepley   CASE: Taylor-Green vortex
406606d57d4SMatthew G. Knepley   In 2D we use exact solution:
407606d57d4SMatthew G. Knepley 
408606d57d4SMatthew G. Knepley     u = 1 - cos(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t)
409606d57d4SMatthew G. Knepley     v = 1 + sin(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)
410606d57d4SMatthew G. Knepley     p = -1/4 [cos(2 \pi(x - t)) + cos(2 \pi(y - t))] exp(-4 \pi^2 \nu t)
411606d57d4SMatthew G. Knepley     T = t + x + y
412606d57d4SMatthew G. Knepley     f = <\nu \pi^2 exp(-2\nu \pi^2 t) cos(\pi(x-t)) sin(\pi(y-t)), -\nu \pi^2 exp(-2\nu \pi^2 t) sin(\pi(x-t)) cos(\pi(y-t))  >
413606d57d4SMatthew G. Knepley     Q = 3 + sin(\pi(x-y)) exp(-2\nu \pi^2 t)
414606d57d4SMatthew G. Knepley 
415606d57d4SMatthew G. Knepley   so that
416606d57d4SMatthew G. Knepley 
417606d57d4SMatthew G. Knepley   \nabla \cdot u = \pi sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t) - \pi sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t) = 0
418606d57d4SMatthew G. Knepley 
419606d57d4SMatthew G. Knepley   f = du/dt + u \cdot \nabla u - \nu \Delta u + \nabla p
420606d57d4SMatthew G. Knepley     = <-\pi (sin(\pi(x - t)) sin(\pi(y - t)) - cos(\pi(x - t)) cos(\pi(y - t)) - 2\pi cos(\pi(x - t)) sin(\pi(y - t))) exp(-2 \pi^2 \nu t),
421606d57d4SMatthew G. Knepley         \pi (sin(\pi(x - t)) sin(\pi(y - t)) - cos(\pi(x - t)) cos(\pi(y - t)) - 2\pi sin(\pi(x - t)) cos(\pi(y - t))) exp(-2 \pi^2 \nu t)>
422606d57d4SMatthew G. Knepley     + < \pi (1 - cos(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t)) sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t),
423606d57d4SMatthew G. Knepley         \pi (1 - cos(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t)) cos(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)>
424606d57d4SMatthew G. Knepley     + <-\pi (1 + sin(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)) cos(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t),
425606d57d4SMatthew G. Knepley        -\pi (1 + sin(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)) sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t)>
426606d57d4SMatthew G. Knepley     + <-2\pi^2 cos(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t),
427606d57d4SMatthew G. Knepley         2\pi^2 sin(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)>
428606d57d4SMatthew G. Knepley     + < \pi/2 sin(2\pi(x - t)) exp(-4 \pi^2 \nu t),
429606d57d4SMatthew G. Knepley         \pi/2 sin(2\pi(y - t)) exp(-4 \pi^2 \nu t)>
430606d57d4SMatthew G. Knepley     = <-\pi (sin(\pi(x - t)) sin(\pi(y - t)) - cos(\pi(x - t)) cos(\pi(y - t)) - 2\pi cos(\pi(x - t)) sin(\pi(y - t))) exp(-2 \pi^2 \nu t),
431606d57d4SMatthew G. Knepley         \pi (sin(\pi(x - t)) sin(\pi(y - t)) - cos(\pi(x - t)) cos(\pi(y - t)) - 2\pi sin(\pi(x - t)) cos(\pi(y - t))) exp(-2 \pi^2 \nu t)>
432606d57d4SMatthew G. Knepley     + < \pi sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t),
433606d57d4SMatthew G. Knepley         \pi cos(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)>
434606d57d4SMatthew G. Knepley     + <-\pi cos(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t),
435606d57d4SMatthew G. Knepley        -\pi sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t)>
436606d57d4SMatthew G. Knepley     + <-\pi/2 sin(2\pi(x - t)) exp(-4 \pi^2 \nu t),
437606d57d4SMatthew G. Knepley        -\pi/2 sin(2\pi(y - t)) exp(-4 \pi^2 \nu t)>
438606d57d4SMatthew G. Knepley     + <-2\pi^2 cos(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t),
439606d57d4SMatthew G. Knepley         2\pi^2 sin(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)>
440606d57d4SMatthew G. Knepley     + < \pi/2 sin(2\pi(x - t)) exp(-4 \pi^2 \nu t),
441606d57d4SMatthew G. Knepley         \pi/2 sin(2\pi(y - t)) exp(-4 \pi^2 \nu t)>
442606d57d4SMatthew G. Knepley     = <-\pi (sin(\pi(x - t)) sin(\pi(y - t)) - cos(\pi(x - t)) cos(\pi(y - t))) exp(-2 \pi^2 \nu t),
443606d57d4SMatthew G. Knepley         \pi (sin(\pi(x - t)) sin(\pi(y - t)) - cos(\pi(x - t)) cos(\pi(y - t))) exp(-2 \pi^2 \nu t)>
444606d57d4SMatthew G. Knepley     + < \pi sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t),
445606d57d4SMatthew G. Knepley         \pi cos(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t)>
446606d57d4SMatthew G. Knepley     + <-\pi cos(\pi(x - t)) cos(\pi(y - t)) exp(-2 \pi^2 \nu t),
447606d57d4SMatthew G. Knepley        -\pi sin(\pi(x - t)) sin(\pi(y - t)) exp(-2 \pi^2 \nu t)>
448606d57d4SMatthew G. Knepley     = < \pi cos(\pi(x - t)) cos(\pi(y - t)),
449606d57d4SMatthew G. Knepley         \pi sin(\pi(x - t)) sin(\pi(y - t))>
450606d57d4SMatthew G. Knepley     + <-\pi cos(\pi(x - t)) cos(\pi(y - t)),
451606d57d4SMatthew G. Knepley        -\pi sin(\pi(x - t)) sin(\pi(y - t))> = 0
452606d57d4SMatthew G. Knepley   Q = dT/dt + u \cdot \nabla T - \alpha \Delta T
453606d57d4SMatthew G. Knepley     = 1 + u \cdot <1, 1> - 0
454606d57d4SMatthew G. Knepley     = 1 + u + v
455606d57d4SMatthew G. Knepley */
456606d57d4SMatthew G. Knepley 
457606d57d4SMatthew G. Knepley static PetscErrorCode taylor_green_u(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
458606d57d4SMatthew G. Knepley {
459606d57d4SMatthew G. Knepley   u[0] = 1 - PetscCosReal(PETSC_PI*(X[0]-time))*PetscSinReal(PETSC_PI*(X[1]-time))*PetscExpReal(-2*PETSC_PI*PETSC_PI*time);
460606d57d4SMatthew G. Knepley   u[1] = 1 + PetscSinReal(PETSC_PI*(X[0]-time))*PetscCosReal(PETSC_PI*(X[1]-time))*PetscExpReal(-2*PETSC_PI*PETSC_PI*time);
461606d57d4SMatthew G. Knepley   return 0;
462606d57d4SMatthew G. Knepley }
463606d57d4SMatthew G. Knepley static PetscErrorCode taylor_green_u_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
464606d57d4SMatthew G. Knepley {
465606d57d4SMatthew G. Knepley   u[0] = -PETSC_PI*(PetscSinReal(PETSC_PI*(X[0]-time))*PetscSinReal(PETSC_PI*(X[1]-time))
466606d57d4SMatthew G. Knepley                   - PetscCosReal(PETSC_PI*(X[0]-time))*PetscCosReal(PETSC_PI*(X[1]-time))
467606d57d4SMatthew G. Knepley                   - 2*PETSC_PI*PetscCosReal(PETSC_PI*(X[0]-time))*PetscSinReal(PETSC_PI*(X[1]-time)))*PetscExpReal(-2*PETSC_PI*PETSC_PI*time);
468606d57d4SMatthew G. Knepley   u[1] =  PETSC_PI*(PetscSinReal(PETSC_PI*(X[0]-time))*PetscSinReal(PETSC_PI*(X[1]-time))
469606d57d4SMatthew G. Knepley                   - PetscCosReal(PETSC_PI*(X[0]-time))*PetscCosReal(PETSC_PI*(X[1]-time))
470606d57d4SMatthew G. Knepley                   - 2*PETSC_PI*PetscSinReal(PETSC_PI*(X[0]-time))*PetscCosReal(PETSC_PI*(X[1]-time)))*PetscExpReal(-2*PETSC_PI*PETSC_PI*time);
471606d57d4SMatthew G. Knepley   return 0;
472606d57d4SMatthew G. Knepley }
473606d57d4SMatthew G. Knepley 
474606d57d4SMatthew G. Knepley static PetscErrorCode taylor_green_p(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
475606d57d4SMatthew G. Knepley {
476606d57d4SMatthew G. Knepley   p[0] = -0.25*(PetscCosReal(2*PETSC_PI*(X[0]-time)) + PetscCosReal(2*PETSC_PI*(X[1]-time)))*PetscExpReal(-4*PETSC_PI*PETSC_PI*time);
477606d57d4SMatthew G. Knepley   return 0;
478606d57d4SMatthew G. Knepley }
479606d57d4SMatthew G. Knepley 
480606d57d4SMatthew G. Knepley static PetscErrorCode taylor_green_p_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
481606d57d4SMatthew G. Knepley {
482606d57d4SMatthew G. Knepley   p[0] = PETSC_PI*(0.5*(PetscSinReal(2*PETSC_PI*(X[0]-time)) + PetscSinReal(2*PETSC_PI*(X[1]-time)))
483606d57d4SMatthew G. Knepley                  + PETSC_PI*(PetscCosReal(2*PETSC_PI*(X[0]-time)) + PetscCosReal(2*PETSC_PI*(X[1]-time))))*PetscExpReal(-4*PETSC_PI*PETSC_PI*time);
484606d57d4SMatthew G. Knepley   return 0;
485606d57d4SMatthew G. Knepley }
486606d57d4SMatthew G. Knepley 
487606d57d4SMatthew G. Knepley static PetscErrorCode taylor_green_T(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
488606d57d4SMatthew G. Knepley {
489606d57d4SMatthew G. Knepley   T[0] = time + X[0] + X[1];
490606d57d4SMatthew G. Knepley   return 0;
491606d57d4SMatthew G. Knepley }
492606d57d4SMatthew G. Knepley static PetscErrorCode taylor_green_T_t(PetscInt Dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
493606d57d4SMatthew G. Knepley {
494606d57d4SMatthew G. Knepley   T[0] = 1.0;
495606d57d4SMatthew G. Knepley   return 0;
496606d57d4SMatthew G. Knepley }
497606d57d4SMatthew G. Knepley 
498606d57d4SMatthew G. Knepley static void f0_taylor_green_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
499606d57d4SMatthew G. Knepley                             const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
500606d57d4SMatthew G. Knepley                             const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
501606d57d4SMatthew G. Knepley                             PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
502606d57d4SMatthew G. Knepley {
503606d57d4SMatthew G. Knepley   PetscScalar vel[2];
504606d57d4SMatthew G. Knepley 
505606d57d4SMatthew G. Knepley   taylor_green_u(dim, t, X, Nf, vel, NULL);
506444129b9SMatthew G. Knepley   f0[0] -= 1.0 + vel[0] + vel[1];
507606d57d4SMatthew G. Knepley }
508606d57d4SMatthew G. Knepley 
509444129b9SMatthew G. Knepley /*
510444129b9SMatthew G. Knepley   CASE: Pipe flow
511444129b9SMatthew G. Knepley   Poiseuille flow, with the incoming fluid having a parabolic temperature profile and the side walls being held at T_in
512444129b9SMatthew G. Knepley 
513444129b9SMatthew G. Knepley     u = \Delta Re/(2 mu) y (1 - y)
514444129b9SMatthew G. Knepley     v = 0
515444129b9SMatthew G. Knepley     p = -\Delta x
516444129b9SMatthew G. Knepley     T = y (1 - y) + T_in
517444129b9SMatthew G. Knepley   rho = p^{th} / T
518444129b9SMatthew G. Knepley 
519444129b9SMatthew G. Knepley   so that
520444129b9SMatthew G. Knepley 
521444129b9SMatthew G. Knepley     \nabla \cdot u = 0 - 0 = 0
522444129b9SMatthew G. Knepley     grad u = \Delta Re/(2 mu) <<0, 0>, <1 - 2y, 0>>
523444129b9SMatthew G. Knepley     epsilon(u) = 1/2 (grad u + grad u^T) = \Delta Re/(4 mu) <<0, 1 - 2y>, <<1 - 2y, 0>>
524444129b9SMatthew G. Knepley     epsilon'(u) = epsilon(u) - 1/3 (div u) I = epsilon(u)
525444129b9SMatthew G. Knepley     div epsilon'(u) = -\Delta Re/(2 mu) <1, 0>
526444129b9SMatthew G. Knepley 
527444129b9SMatthew G. Knepley   f = rho S du/dt + rho u \cdot \nabla u - 2\mu/Re div \epsilon'(u) + \nabla p + rho / F^2 \hat y
528444129b9SMatthew G. Knepley     = 0 + 0 - div (2\mu/Re \epsilon'(u) - pI) + rho / F^2 \hat y
529444129b9SMatthew G. Knepley     = -\Delta div <<x, (1 - 2y)/2>, <<(1 - 2y)/2, x>> + rho / F^2 \hat y
530444129b9SMatthew G. Knepley     = \Delta <1, 0> - \Delta <1, 0> + rho/F^2 <0, 1>
531444129b9SMatthew G. Knepley     = rho/F^2 <0, 1>
532444129b9SMatthew G. Knepley 
533444129b9SMatthew G. Knepley   g = S rho_t + div (rho u)
534444129b9SMatthew G. Knepley     = 0 + rho div (u) + u . grad rho
535444129b9SMatthew G. Knepley     = 0 + 0 - pth u . grad T / T^2
536444129b9SMatthew G. Knepley     = 0
537444129b9SMatthew G. Knepley 
538444129b9SMatthew G. Knepley   Q = rho c_p S dT/dt + rho c_p u . grad T - 1/Pe div k grad T
539444129b9SMatthew G. Knepley     = 0 + c_p pth / T 0 + 2 k/Pe
540444129b9SMatthew G. Knepley     = 2 k/Pe
541444129b9SMatthew G. Knepley 
542444129b9SMatthew G. Knepley   The boundary conditions on the top and bottom are zero velocity and T_in temperature. The boundary term is
543444129b9SMatthew G. Knepley 
544444129b9SMatthew G. Knepley     (2\mu/Re \epsilon'(u) - p I) . n = \Delta <<x, (1 - 2y)/2>, <<(1 - 2y)/2, x>> . n
545444129b9SMatthew G. Knepley 
546444129b9SMatthew G. Knepley   so that
547444129b9SMatthew G. Knepley 
548444129b9SMatthew G. Knepley     x = 0: \Delta <<0, (1 - 2y)/2>, <<(1 - 2y)/2, 0>> . <-1, 0> = <0, (2y - 1)/2>
549444129b9SMatthew G. Knepley     x = 1: \Delta <<1, (1 - 2y)/2>, <<(1 - 2y)/2, 1>> . <1, 0> = <1, (1 - 2y)/2>
550444129b9SMatthew G. Knepley */
551444129b9SMatthew G. Knepley static PetscErrorCode pipe_u(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
552444129b9SMatthew G. Knepley {
553444129b9SMatthew G. Knepley   Parameter *param = (Parameter *) ctx;
554444129b9SMatthew G. Knepley 
555444129b9SMatthew G. Knepley   u[0] = (0.5*param->Reynolds / param->mu) * X[1]*(1.0 - X[1]);
556444129b9SMatthew G. Knepley   u[1] = 0.0;
557444129b9SMatthew G. Knepley   return 0;
558444129b9SMatthew G. Knepley }
559444129b9SMatthew G. Knepley static PetscErrorCode pipe_u_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
560444129b9SMatthew G. Knepley {
561444129b9SMatthew G. Knepley   u[0] = 0.0;
562444129b9SMatthew G. Knepley   u[1] = 0.0;
563444129b9SMatthew G. Knepley   return 0;
564444129b9SMatthew G. Knepley }
565444129b9SMatthew G. Knepley 
566444129b9SMatthew G. Knepley static PetscErrorCode pipe_p(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
567444129b9SMatthew G. Knepley {
568444129b9SMatthew G. Knepley   p[0] = -X[0];
569444129b9SMatthew G. Knepley   return 0;
570444129b9SMatthew G. Knepley }
571444129b9SMatthew G. Knepley static PetscErrorCode pipe_p_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
572444129b9SMatthew G. Knepley {
573444129b9SMatthew G. Knepley   p[0] = 0.0;
574444129b9SMatthew G. Knepley   return 0;
575444129b9SMatthew G. Knepley }
576444129b9SMatthew G. Knepley 
577444129b9SMatthew G. Knepley static PetscErrorCode pipe_T(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
578444129b9SMatthew G. Knepley {
579444129b9SMatthew G. Knepley   Parameter *param = (Parameter *) ctx;
580444129b9SMatthew G. Knepley 
581444129b9SMatthew G. Knepley   T[0] = X[1]*(1.0 - X[1]) + param->T_in;
582444129b9SMatthew G. Knepley   return 0;
583444129b9SMatthew G. Knepley }
584444129b9SMatthew G. Knepley static PetscErrorCode pipe_T_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
585444129b9SMatthew G. Knepley {
586444129b9SMatthew G. Knepley   T[0] = 0.0;
587444129b9SMatthew G. Knepley   return 0;
588444129b9SMatthew G. Knepley }
589444129b9SMatthew G. Knepley 
590444129b9SMatthew G. Knepley static void f0_conduct_pipe_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
591444129b9SMatthew G. Knepley                               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
592444129b9SMatthew G. Knepley                               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
593444129b9SMatthew G. Knepley                               PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
594444129b9SMatthew G. Knepley {
595444129b9SMatthew G. Knepley   const PetscReal F    = PetscRealPart(constants[FROUDE]);
596444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
597444129b9SMatthew G. Knepley   const PetscReal T_in = PetscRealPart(constants[T_IN]);
598444129b9SMatthew G. Knepley   const PetscReal rho  = p_th / (X[1]*(1. - X[1]) + T_in);
599444129b9SMatthew G. Knepley   const PetscInt  gd   = (PetscInt) PetscRealPart(constants[G_DIR]);
600444129b9SMatthew G. Knepley 
601444129b9SMatthew G. Knepley   f0[gd] -= rho/PetscSqr(F);
602444129b9SMatthew G. Knepley }
603444129b9SMatthew G. Knepley 
604444129b9SMatthew G. Knepley static void f0_conduct_bd_pipe_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
605444129b9SMatthew G. Knepley                                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
606444129b9SMatthew G. Knepley                                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
607444129b9SMatthew G. Knepley                                  PetscReal t, const PetscReal X[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
608444129b9SMatthew G. Knepley {
609444129b9SMatthew G. Knepley   PetscReal sigma[4] = {X[0], 0.5*(1. - 2.*X[1]), 0.5*(1. - 2.*X[1]), X[0]};
610444129b9SMatthew G. Knepley   PetscInt  d, e;
611444129b9SMatthew G. Knepley 
612444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
613444129b9SMatthew G. Knepley     for (e = 0; e < dim; ++e) {
614444129b9SMatthew G. Knepley       f0[d] -= sigma[d*dim + e] * n[e];
615444129b9SMatthew G. Knepley     }
616444129b9SMatthew G. Knepley   }
617444129b9SMatthew G. Knepley }
618444129b9SMatthew G. Knepley 
619444129b9SMatthew G. Knepley static void f0_conduct_pipe_q(PetscInt dim, PetscInt Nf, PetscInt NfAux,
620444129b9SMatthew G. Knepley                               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
621444129b9SMatthew G. Knepley                               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
622444129b9SMatthew G. Knepley                               PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
623444129b9SMatthew G. Knepley {
624444129b9SMatthew G. Knepley   f0[0] += 0.0;
625444129b9SMatthew G. Knepley }
626444129b9SMatthew G. Knepley 
627444129b9SMatthew G. Knepley static void f0_conduct_pipe_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
628444129b9SMatthew G. Knepley                               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
629444129b9SMatthew G. Knepley                               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
630444129b9SMatthew G. Knepley                               PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
631444129b9SMatthew G. Knepley {
632444129b9SMatthew G. Knepley   const PetscReal k  = PetscRealPart(constants[K]);
633444129b9SMatthew G. Knepley   const PetscReal Pe = PetscRealPart(constants[PECLET]);
634444129b9SMatthew G. Knepley 
635444129b9SMatthew G. Knepley   f0[0] -= 2*k/Pe;
636444129b9SMatthew G. Knepley }
637444129b9SMatthew G. Knepley 
638367970cfSMatthew G. Knepley /*
639367970cfSMatthew G. Knepley   CASE: Wiggly pipe flow
640367970cfSMatthew G. Knepley   Perturbed Poiseuille flow, with the incoming fluid having a perturbed parabolic temperature profile and the side walls being held at T_in
641367970cfSMatthew G. Knepley 
642367970cfSMatthew G. Knepley     u = \Delta Re/(2 mu) [y (1 - y) + a sin(pi y)]
643367970cfSMatthew G. Knepley     v = 0
644367970cfSMatthew G. Knepley     p = -\Delta x
645367970cfSMatthew G. Knepley     T = y (1 - y) + a sin(pi y) + T_in
646367970cfSMatthew G. Knepley   rho = p^{th} / T
647367970cfSMatthew G. Knepley 
648367970cfSMatthew G. Knepley   so that
649367970cfSMatthew G. Knepley 
650367970cfSMatthew G. Knepley     \nabla \cdot u = 0 - 0 = 0
651367970cfSMatthew G. Knepley     grad u = \Delta Re/(2 mu) <<0, 0>, <1 - 2y + a pi cos(pi y), 0>>
652367970cfSMatthew G. Knepley     epsilon(u) = 1/2 (grad u + grad u^T) = \Delta Re/(4 mu) <<0, 1 - 2y + a pi cos(pi y)>, <<1 - 2y + a pi cos(pi y), 0>>
653367970cfSMatthew G. Knepley     epsilon'(u) = epsilon(u) - 1/3 (div u) I = epsilon(u)
654367970cfSMatthew G. Knepley     div epsilon'(u) = -\Delta Re/(2 mu) <1 + a pi^2/2 sin(pi y), 0>
655367970cfSMatthew G. Knepley 
656367970cfSMatthew G. Knepley   f = rho S du/dt + rho u \cdot \nabla u - 2\mu/Re div \epsilon'(u) + \nabla p + rho / F^2 \hat y
657367970cfSMatthew G. Knepley     = 0 + 0 - div (2\mu/Re \epsilon'(u) - pI) + rho / F^2 \hat y
658367970cfSMatthew G. Knepley     = -\Delta div <<x, (1 - 2y)/2 + a pi/2 cos(pi y)>, <<(1 - 2y)/2 + a pi/2 cos(pi y), x>> + rho / F^2 \hat y
659367970cfSMatthew G. Knepley     = -\Delta <1 - 1 - a pi^2/2 sin(pi y), 0> + rho/F^2 <0, 1>
660367970cfSMatthew G. Knepley     = a \Delta pi^2/2 sin(pi y) <1, 0> + rho/F^2 <0, 1>
661367970cfSMatthew G. Knepley 
662367970cfSMatthew G. Knepley   g = S rho_t + div (rho u)
663367970cfSMatthew G. Knepley     = 0 + rho div (u) + u . grad rho
664367970cfSMatthew G. Knepley     = 0 + 0 - pth u . grad T / T^2
665367970cfSMatthew G. Knepley     = 0
666367970cfSMatthew G. Knepley 
667367970cfSMatthew G. Knepley   Q = rho c_p S dT/dt + rho c_p u . grad T - 1/Pe div k grad T
668367970cfSMatthew G. Knepley     = 0 + c_p pth / T 0 - k/Pe div <0, 1 - 2y + a pi cos(pi y)>
669367970cfSMatthew G. Knepley     = - k/Pe (-2 - a pi^2 sin(pi y))
670367970cfSMatthew G. Knepley     = 2 k/Pe (1 + a pi^2/2 sin(pi y))
671367970cfSMatthew G. Knepley 
672367970cfSMatthew G. Knepley   The boundary conditions on the top and bottom are zero velocity and T_in temperature. The boundary term is
673367970cfSMatthew G. Knepley 
674367970cfSMatthew G. Knepley     (2\mu/Re \epsilon'(u) - p I) . n = \Delta <<x, (1 - 2y)/2 + a pi/2 cos(pi y)>, <<(1 - 2y)/2 + a pi/2 cos(pi y), x>> . n
675367970cfSMatthew G. Knepley 
676367970cfSMatthew G. Knepley   so that
677367970cfSMatthew G. Knepley 
678367970cfSMatthew G. Knepley     x = 0: \Delta <<0, (1 - 2y)/2>, <<(1 - 2y)/2, 0>> . <-1, 0> = <0, (2y - 1)/2 - a pi/2 cos(pi y)>
679367970cfSMatthew G. Knepley     x = 1: \Delta <<1, (1 - 2y)/2>, <<(1 - 2y)/2, 1>> . < 1, 0> = <1, (1 - 2y)/2 + a pi/2 cos(pi y)>
680367970cfSMatthew G. Knepley */
681367970cfSMatthew G. Knepley static PetscErrorCode pipe_wiggly_u(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
682367970cfSMatthew G. Knepley {
683367970cfSMatthew G. Knepley   Parameter *param = (Parameter *) ctx;
684367970cfSMatthew G. Knepley 
685367970cfSMatthew G. Knepley   u[0] = (0.5*param->Reynolds / param->mu) * (X[1]*(1.0 - X[1]) + param->epsilon*PetscSinReal(PETSC_PI*X[1]));
686367970cfSMatthew G. Knepley   u[1] = 0.0;
687367970cfSMatthew G. Knepley   return 0;
688367970cfSMatthew G. Knepley }
689367970cfSMatthew G. Knepley static PetscErrorCode pipe_wiggly_u_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *u, void *ctx)
690367970cfSMatthew G. Knepley {
691367970cfSMatthew G. Knepley   u[0] = 0.0;
692367970cfSMatthew G. Knepley   u[1] = 0.0;
693367970cfSMatthew G. Knepley   return 0;
694367970cfSMatthew G. Knepley }
695367970cfSMatthew G. Knepley 
696367970cfSMatthew G. Knepley static PetscErrorCode pipe_wiggly_p(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
697367970cfSMatthew G. Knepley {
698367970cfSMatthew G. Knepley   p[0] = -X[0];
699367970cfSMatthew G. Knepley   return 0;
700367970cfSMatthew G. Knepley }
701367970cfSMatthew G. Knepley static PetscErrorCode pipe_wiggly_p_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *p, void *ctx)
702367970cfSMatthew G. Knepley {
703367970cfSMatthew G. Knepley   p[0] = 0.0;
704367970cfSMatthew G. Knepley   return 0;
705367970cfSMatthew G. Knepley }
706367970cfSMatthew G. Knepley 
707367970cfSMatthew G. Knepley static PetscErrorCode pipe_wiggly_T(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
708367970cfSMatthew G. Knepley {
709367970cfSMatthew G. Knepley   Parameter *param = (Parameter *) ctx;
710367970cfSMatthew G. Knepley 
711367970cfSMatthew G. Knepley   T[0] = X[1]*(1.0 - X[1]) + param->epsilon*PetscSinReal(PETSC_PI*X[1]) + param->T_in;
712367970cfSMatthew G. Knepley   return 0;
713367970cfSMatthew G. Knepley }
714367970cfSMatthew G. Knepley static PetscErrorCode pipe_wiggly_T_t(PetscInt dim, PetscReal time, const PetscReal X[], PetscInt Nf, PetscScalar *T, void *ctx)
715367970cfSMatthew G. Knepley {
716367970cfSMatthew G. Knepley   T[0] = 0.0;
717367970cfSMatthew G. Knepley   return 0;
718367970cfSMatthew G. Knepley }
719367970cfSMatthew G. Knepley 
720367970cfSMatthew G. Knepley static void f0_conduct_pipe_wiggly_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
721367970cfSMatthew G. Knepley                                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
722367970cfSMatthew G. Knepley                                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
723367970cfSMatthew G. Knepley                                      PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
724367970cfSMatthew G. Knepley {
725367970cfSMatthew G. Knepley   const PetscReal F    = PetscRealPart(constants[FROUDE]);
726367970cfSMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
727367970cfSMatthew G. Knepley   const PetscReal T_in = PetscRealPart(constants[T_IN]);
728367970cfSMatthew G. Knepley   const PetscReal eps  = PetscRealPart(constants[EPSILON]);
729367970cfSMatthew G. Knepley   const PetscReal rho  = p_th / (X[1]*(1. - X[1]) + T_in);
730367970cfSMatthew G. Knepley   const PetscInt  gd   = (PetscInt) PetscRealPart(constants[G_DIR]);
731367970cfSMatthew G. Knepley 
732367970cfSMatthew G. Knepley   f0[0]  -= eps*0.5*PetscSqr(PETSC_PI)*PetscSinReal(PETSC_PI*X[1]);
733367970cfSMatthew G. Knepley   f0[gd] -= rho/PetscSqr(F);
734367970cfSMatthew G. Knepley }
735367970cfSMatthew G. Knepley 
736367970cfSMatthew G. Knepley static void f0_conduct_bd_pipe_wiggly_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
737367970cfSMatthew G. Knepley                                         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
738367970cfSMatthew G. Knepley                                         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
739367970cfSMatthew G. Knepley                                         PetscReal t, const PetscReal X[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
740367970cfSMatthew G. Knepley {
741367970cfSMatthew G. Knepley   const PetscReal eps = PetscRealPart(constants[EPSILON]);
742367970cfSMatthew G. Knepley   PetscReal sigma[4] = {X[0], 0.5*(1. - 2.*X[1]) + eps*0.5*PETSC_PI*PetscCosReal(PETSC_PI*X[1]), 0.5*(1. - 2.*X[1]) + eps*0.5*PETSC_PI*PetscCosReal(PETSC_PI*X[1]), X[0]};
743367970cfSMatthew G. Knepley   PetscInt  d, e;
744367970cfSMatthew G. Knepley 
745367970cfSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
746367970cfSMatthew G. Knepley     for (e = 0; e < dim; ++e) {
747367970cfSMatthew G. Knepley       f0[d] -= sigma[d*dim + e] * n[e];
748367970cfSMatthew G. Knepley     }
749367970cfSMatthew G. Knepley   }
750367970cfSMatthew G. Knepley }
751367970cfSMatthew G. Knepley 
752367970cfSMatthew G. Knepley static void f0_conduct_pipe_wiggly_q(PetscInt dim, PetscInt Nf, PetscInt NfAux,
753367970cfSMatthew G. Knepley                                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
754367970cfSMatthew G. Knepley                                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
755367970cfSMatthew G. Knepley                                      PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
756367970cfSMatthew G. Knepley {
757367970cfSMatthew G. Knepley   f0[0] += 0.0;
758367970cfSMatthew G. Knepley }
759367970cfSMatthew G. Knepley 
760367970cfSMatthew G. Knepley static void f0_conduct_pipe_wiggly_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
761367970cfSMatthew G. Knepley                                      const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
762367970cfSMatthew G. Knepley                                      const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
763367970cfSMatthew G. Knepley                                      PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
764367970cfSMatthew G. Knepley {
765367970cfSMatthew G. Knepley   const PetscReal k  = PetscRealPart(constants[K]);
766367970cfSMatthew G. Knepley   const PetscReal Pe = PetscRealPart(constants[PECLET]);
767367970cfSMatthew G. Knepley   const PetscReal eps = PetscRealPart(constants[EPSILON]);
768367970cfSMatthew G. Knepley 
769367970cfSMatthew G. Knepley   f0[0] -= 2*k/Pe*(1.0 + eps*0.5*PetscSqr(PETSC_PI)*PetscSinReal(PETSC_PI*X[1]));
770367970cfSMatthew G. Knepley }
771367970cfSMatthew G. Knepley 
772444129b9SMatthew G. Knepley /*      Physics Kernels      */
773444129b9SMatthew G. Knepley 
774649ef022SMatthew Knepley static void f0_q(PetscInt dim, PetscInt Nf, PetscInt NfAux,
775649ef022SMatthew Knepley                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
776649ef022SMatthew Knepley                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
777649ef022SMatthew Knepley                  PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
778649ef022SMatthew Knepley {
779649ef022SMatthew Knepley   PetscInt d;
780649ef022SMatthew Knepley   for (d = 0, f0[0] = 0.0; d < dim; ++d) f0[0] += u_x[d*dim+d];
781649ef022SMatthew Knepley }
782649ef022SMatthew Knepley 
783444129b9SMatthew G. Knepley /* -\frac{Sp^{th}}{T^2} \frac{\partial T}{\partial t} + \frac{p^{th}}{T} \nabla \cdot \vb{u} - \frac{p^{th}}{T^2} \vb{u} \cdot \nabla T */
784444129b9SMatthew G. Knepley static void f0_conduct_q(PetscInt dim, PetscInt Nf, PetscInt NfAux,
785444129b9SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
786444129b9SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
787444129b9SMatthew G. Knepley                          PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
788444129b9SMatthew G. Knepley {
789444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
790444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
791444129b9SMatthew G. Knepley   PetscInt        d;
792444129b9SMatthew G. Knepley 
793444129b9SMatthew G. Knepley   // -\frac{S p^{th}}{T^2} \frac{\partial T}{\partial t}
794444129b9SMatthew G. Knepley   f0[0] += -u_t[uOff[TEMP]] * S * p_th / PetscSqr(u[uOff[TEMP]]);
795444129b9SMatthew G. Knepley 
796444129b9SMatthew G. Knepley   // \frac{p^{th}}{T} \nabla \cdot \vb{u}
797444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
798444129b9SMatthew G. Knepley     f0[0] += p_th / u[uOff[TEMP]] * u_x[uOff_x[VEL] + d*dim + d];
799444129b9SMatthew G. Knepley   }
800444129b9SMatthew G. Knepley 
801444129b9SMatthew G. Knepley   // - \frac{p^{th}}{T^2} \vb{u} \cdot \nabla T
802444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
803444129b9SMatthew G. Knepley     f0[0] -= p_th / (u[uOff[TEMP]] * u[uOff[TEMP]]) * u[uOff[VEL] + d] * u_x[uOff_x[TEMP] + d];
804444129b9SMatthew G. Knepley   }
805444129b9SMatthew G. Knepley 
806444129b9SMatthew G. Knepley   // Add in any fixed source term
807444129b9SMatthew G. Knepley   if (NfAux > 0) {
808444129b9SMatthew G. Knepley     f0[0] += a[aOff[MASS]];
809444129b9SMatthew G. Knepley   }
810444129b9SMatthew G. Knepley }
811444129b9SMatthew G. Knepley 
812444129b9SMatthew G. Knepley /* \vb{u}_t + \vb{u} \cdot \nabla\vb{u} */
813444129b9SMatthew G. Knepley static void f0_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
814444129b9SMatthew G. Knepley                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
815444129b9SMatthew G. Knepley                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
816444129b9SMatthew G. Knepley                  PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
817444129b9SMatthew G. Knepley {
818444129b9SMatthew G. Knepley   const PetscInt Nc = dim;
819444129b9SMatthew G. Knepley   PetscInt       c, d;
820444129b9SMatthew G. Knepley 
821444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
822444129b9SMatthew G. Knepley     /* \vb{u}_t */
823444129b9SMatthew G. Knepley     f0[c] += u_t[uOff[VEL] + c];
824444129b9SMatthew G. Knepley     /* \vb{u} \cdot \nabla\vb{u} */
825444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) f0[c] += u[uOff[VEL] + d]*u_x[uOff_x[VEL] + c*dim + d];
826444129b9SMatthew G. Knepley   }
827444129b9SMatthew G. Knepley }
828444129b9SMatthew G. Knepley 
829444129b9SMatthew G. Knepley /* \rho S \frac{\partial \vb{u}}{\partial t} + \rho \vb{u} \cdot \nabla \vb{u} + \rho \frac{\hat{\vb{z}}}{F^2} */
830444129b9SMatthew G. Knepley static void f0_conduct_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
831444129b9SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
832444129b9SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
833444129b9SMatthew G. Knepley                          PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
834444129b9SMatthew G. Knepley {
835444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
836444129b9SMatthew G. Knepley   const PetscReal F    = PetscRealPart(constants[FROUDE]);
837444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
838444129b9SMatthew G. Knepley   const PetscReal rho  = p_th / PetscRealPart(u[uOff[TEMP]]);
839444129b9SMatthew G. Knepley   const PetscInt  gdir = (PetscInt) PetscRealPart(constants[G_DIR]);
840444129b9SMatthew G. Knepley   PetscInt        Nc   = dim;
841444129b9SMatthew G. Knepley   PetscInt        c, d;
842444129b9SMatthew G. Knepley 
843444129b9SMatthew G. Knepley   // \rho S \frac{\partial \vb{u}}{\partial t}
844444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
845444129b9SMatthew G. Knepley     f0[d] = rho * S * u_t[uOff[VEL] + d];
846444129b9SMatthew G. Knepley   }
847444129b9SMatthew G. Knepley 
848444129b9SMatthew G. Knepley   // \rho \vb{u} \cdot \nabla \vb{u}
849444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
850444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
851444129b9SMatthew G. Knepley       f0[c] += rho * u[uOff[VEL] + d] * u_x[uOff_x[VEL] + c*dim + d];
852444129b9SMatthew G. Knepley     }
853444129b9SMatthew G. Knepley   }
854444129b9SMatthew G. Knepley 
855444129b9SMatthew G. Knepley   // rho \hat{z}/F^2
856444129b9SMatthew G. Knepley   f0[gdir] += rho / (F*F);
857444129b9SMatthew G. Knepley 
858444129b9SMatthew G. Knepley   // Add in any fixed source term
859444129b9SMatthew G. Knepley   if (NfAux > 0) {
860444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
861444129b9SMatthew G. Knepley       f0[d] += a[aOff[MOMENTUM] + d];
862444129b9SMatthew G. Knepley     }
863444129b9SMatthew G. Knepley   }
864444129b9SMatthew G. Knepley }
865444129b9SMatthew G. Knepley 
866649ef022SMatthew Knepley /*f1_v = \nu[grad(u) + grad(u)^T] - pI */
867649ef022SMatthew Knepley static void f1_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
868649ef022SMatthew Knepley                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
869649ef022SMatthew Knepley                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
870649ef022SMatthew Knepley                  PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
871649ef022SMatthew Knepley {
872444129b9SMatthew G. Knepley   const PetscReal nu = PetscRealPart(constants[NU]);
873649ef022SMatthew Knepley   const PetscInt  Nc = dim;
874649ef022SMatthew Knepley   PetscInt        c, d;
875649ef022SMatthew Knepley 
876649ef022SMatthew Knepley   for (c = 0; c < Nc; ++c) {
877649ef022SMatthew Knepley     for (d = 0; d < dim; ++d) {
878649ef022SMatthew Knepley       f1[c*dim+d] = nu*(u_x[c*dim+d] + u_x[d*dim+c]);
879649ef022SMatthew Knepley     }
880649ef022SMatthew Knepley     f1[c*dim+c] -= u[uOff[1]];
881649ef022SMatthew Knepley   }
882649ef022SMatthew Knepley }
883649ef022SMatthew Knepley 
884444129b9SMatthew G. Knepley /* 2 \mu/Re (1/2 (\nabla \vb{u} + \nabla \vb{u}^T) - 1/3 (\nabla \cdot \vb{u}) I) - p I */
885444129b9SMatthew G. Knepley static void f1_conduct_v(PetscInt dim, PetscInt Nf, PetscInt NfAux,
886444129b9SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
887444129b9SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
888444129b9SMatthew G. Knepley                          PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
889444129b9SMatthew G. Knepley {
890444129b9SMatthew G. Knepley   const PetscReal Re    = PetscRealPart(constants[REYNOLDS]);
891444129b9SMatthew G. Knepley   const PetscReal mu    = PetscRealPart(constants[MU]);
892444129b9SMatthew G. Knepley   const PetscReal coef  = mu / Re;
893444129b9SMatthew G. Knepley   PetscReal       u_div = 0.0;
894444129b9SMatthew G. Knepley   const PetscInt  Nc    = dim;
895444129b9SMatthew G. Knepley   PetscInt        c, d;
896444129b9SMatthew G. Knepley 
897444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
898444129b9SMatthew G. Knepley     u_div += PetscRealPart(u_x[uOff_x[VEL] + c*dim + c]);
899444129b9SMatthew G. Knepley   }
900444129b9SMatthew G. Knepley 
901444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
902444129b9SMatthew G. Knepley     // 2 \mu/Re 1/2 (\nabla \vb{u} + \nabla \vb{u}^T
903444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
904444129b9SMatthew G. Knepley       f1[c*dim + d] += coef * (u_x[uOff_x[VEL] + c*dim + d] + u_x[uOff_x[VEL] + d*dim + c]);
905444129b9SMatthew G. Knepley     }
906444129b9SMatthew G. Knepley     // -2/3 \mu/Re (\nabla \cdot \vb{u}) I
907444129b9SMatthew G. Knepley     f1[c * dim + c] -= 2.0 * coef / 3.0 * u_div;
908444129b9SMatthew G. Knepley   }
909444129b9SMatthew G. Knepley 
910444129b9SMatthew G. Knepley   // -p I
911444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
912444129b9SMatthew G. Knepley     f1[c*dim + c] -= u[uOff[PRES]];
913444129b9SMatthew G. Knepley   }
914444129b9SMatthew G. Knepley }
915444129b9SMatthew G. Knepley 
916444129b9SMatthew G. Knepley /* T_t + \vb{u} \cdot \nabla T */
917444129b9SMatthew G. Knepley static void f0_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
918444129b9SMatthew G. Knepley                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
919444129b9SMatthew G. Knepley                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
920444129b9SMatthew G. Knepley                  PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
921444129b9SMatthew G. Knepley {
922444129b9SMatthew G. Knepley   PetscInt d;
923444129b9SMatthew G. Knepley 
924444129b9SMatthew G. Knepley   /* T_t */
925444129b9SMatthew G. Knepley   f0[0] += u_t[uOff[TEMP]];
926444129b9SMatthew G. Knepley   /* \vb{u} \cdot \nabla T */
927444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) f0[0] += u[uOff[VEL] + d] * u_x[uOff_x[TEMP] + d];
928444129b9SMatthew G. Knepley }
929444129b9SMatthew G. Knepley 
930444129b9SMatthew G. Knepley /* \frac{C_p S p^{th}}{T} \frac{\partial T}{\partial t} + \frac{C_p p^{th}}{T} \vb{u} \cdot \nabla T */
931444129b9SMatthew G. Knepley static void f0_conduct_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
932444129b9SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
933444129b9SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
934444129b9SMatthew G. Knepley                          PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
935444129b9SMatthew G. Knepley {
936444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
937444129b9SMatthew G. Knepley   const PetscReal c_p  = PetscRealPart(constants[C_P]);
938444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
939444129b9SMatthew G. Knepley   PetscInt        d;
940444129b9SMatthew G. Knepley 
941444129b9SMatthew G. Knepley   // \frac{C_p S p^{th}}{T} \frac{\partial T}{\partial t}
942444129b9SMatthew G. Knepley   f0[0] = c_p * S * p_th / u[uOff[TEMP]] * u_t[uOff[TEMP]];
943444129b9SMatthew G. Knepley 
944444129b9SMatthew G. Knepley   // \frac{C_p p^{th}}{T} \vb{u} \cdot \nabla T
945444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
946444129b9SMatthew G. Knepley     f0[0] += c_p * p_th / u[uOff[TEMP]] * u[uOff[VEL] + d] * u_x[uOff_x[TEMP] + d];
947444129b9SMatthew G. Knepley   }
948444129b9SMatthew G. Knepley 
949444129b9SMatthew G. Knepley   // Add in any fixed source term
950444129b9SMatthew G. Knepley   if (NfAux > 0) {
951444129b9SMatthew G. Knepley     f0[0] += a[aOff[ENERGY]];
952444129b9SMatthew G. Knepley   }
953444129b9SMatthew G. Knepley }
954444129b9SMatthew G. Knepley 
955649ef022SMatthew Knepley static void f1_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
956649ef022SMatthew Knepley                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
957649ef022SMatthew Knepley                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
958649ef022SMatthew Knepley                  PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
959649ef022SMatthew Knepley {
960444129b9SMatthew G. Knepley   const PetscReal alpha = PetscRealPart(constants[ALPHA]);
961649ef022SMatthew Knepley   PetscInt        d;
962444129b9SMatthew G. Knepley 
963649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) f1[d] = alpha*u_x[uOff_x[2]+d];
964649ef022SMatthew Knepley }
965649ef022SMatthew Knepley 
966444129b9SMatthew G. Knepley /* \frac{k}{Pe} \nabla T */
967444129b9SMatthew G. Knepley static void f1_conduct_w(PetscInt dim, PetscInt Nf, PetscInt NfAux,
968444129b9SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
969444129b9SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
970444129b9SMatthew G. Knepley                          PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
971444129b9SMatthew G. Knepley {
972444129b9SMatthew G. Knepley   const PetscReal Pe = PetscRealPart(constants[PECLET]);
973444129b9SMatthew G. Knepley   const PetscReal k  = PetscRealPart(constants[K]);
974444129b9SMatthew G. Knepley   PetscInt        d;
975444129b9SMatthew G. Knepley 
976444129b9SMatthew G. Knepley   // \frac{k}{Pe} \nabla T
977444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
978444129b9SMatthew G. Knepley     f1[d] = k / Pe * u_x[uOff_x[TEMP] + d];
979444129b9SMatthew G. Knepley   }
980444129b9SMatthew G. Knepley }
981444129b9SMatthew G. Knepley 
982649ef022SMatthew Knepley static void g1_qu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
983649ef022SMatthew Knepley                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
984649ef022SMatthew Knepley                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
985649ef022SMatthew Knepley                  PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
986649ef022SMatthew Knepley {
987649ef022SMatthew Knepley   PetscInt d;
988649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) g1[d*dim+d] = 1.0;
989649ef022SMatthew Knepley }
990649ef022SMatthew Knepley 
991649ef022SMatthew Knepley static void g0_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
992649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
993649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
994649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
995649ef022SMatthew Knepley {
996649ef022SMatthew Knepley   PetscInt c, d;
997649ef022SMatthew Knepley   const PetscInt  Nc = dim;
998649ef022SMatthew Knepley 
999649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) g0[d*dim+d] = u_tShift;
1000649ef022SMatthew Knepley 
1001649ef022SMatthew Knepley   for (c = 0; c < Nc; ++c) {
1002649ef022SMatthew Knepley     for (d = 0; d < dim; ++d) {
1003649ef022SMatthew Knepley       g0[c*Nc+d] += u_x[ c*Nc+d];
1004649ef022SMatthew Knepley     }
1005649ef022SMatthew Knepley   }
1006649ef022SMatthew Knepley }
1007649ef022SMatthew Knepley 
1008649ef022SMatthew Knepley static void g1_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1009649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1010649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1011649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
1012649ef022SMatthew Knepley {
1013649ef022SMatthew Knepley   PetscInt NcI = dim;
1014649ef022SMatthew Knepley   PetscInt NcJ = dim;
1015649ef022SMatthew Knepley   PetscInt c, d, e;
1016649ef022SMatthew Knepley 
1017649ef022SMatthew Knepley   for (c = 0; c < NcI; ++c) {
1018649ef022SMatthew Knepley     for (d = 0; d < NcJ; ++d) {
1019649ef022SMatthew Knepley       for (e = 0; e < dim; ++e) {
1020649ef022SMatthew Knepley         if (c == d) {
1021649ef022SMatthew Knepley           g1[(c*NcJ+d)*dim+e] += u[e];
1022649ef022SMatthew Knepley         }
1023649ef022SMatthew Knepley       }
1024649ef022SMatthew Knepley     }
1025649ef022SMatthew Knepley   }
1026649ef022SMatthew Knepley }
1027649ef022SMatthew Knepley 
1028444129b9SMatthew G. Knepley static void g0_conduct_qu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1029444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1030444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1031444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1032444129b9SMatthew G. Knepley {
1033444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1034444129b9SMatthew G. Knepley   PetscInt        d;
1035444129b9SMatthew G. Knepley 
1036444129b9SMatthew G. Knepley   // - \phi_i \frac{p^{th}}{T^2} \frac{\partial T}{\partial x_c} \psi_{j, u_c}
1037444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1038444129b9SMatthew G. Knepley     g0[d] = -p_th / PetscSqr(u[uOff[TEMP]]) * u_x[uOff_x[TEMP] + d];
1039444129b9SMatthew G. Knepley   }
1040444129b9SMatthew G. Knepley }
1041444129b9SMatthew G. Knepley 
1042444129b9SMatthew G. Knepley static void g1_conduct_qu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1043444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1044444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1045444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
1046444129b9SMatthew G. Knepley {
1047444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1048444129b9SMatthew G. Knepley   PetscInt        d;
1049444129b9SMatthew G. Knepley 
1050444129b9SMatthew G. Knepley   // \phi_i \frac{p^{th}}{T} \frac{\partial \psi_{u_c,j}}{\partial x_c}
1051444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1052444129b9SMatthew G. Knepley     g1[d * dim + d] = p_th / u[uOff[TEMP]];
1053444129b9SMatthew G. Knepley   }
1054444129b9SMatthew G. Knepley }
1055444129b9SMatthew G. Knepley 
1056444129b9SMatthew G. Knepley static void g0_conduct_qT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1057444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1058444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1059444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1060444129b9SMatthew G. Knepley {
1061444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
1062444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1063444129b9SMatthew G. Knepley   PetscInt        d;
1064444129b9SMatthew G. Knepley 
1065444129b9SMatthew G. Knepley   // - \phi_i \frac{S p^{th}}{T^2} \psi_j
1066444129b9SMatthew G. Knepley   g0[0] -= S * p_th / PetscSqr(u[uOff[TEMP]]) * u_tShift;
1067444129b9SMatthew G. Knepley   // \phi_i 2 \frac{S p^{th}}{T^3} T_t \psi_j
1068444129b9SMatthew G. Knepley   g0[0] += 2.0 * S * p_th / PetscPowScalarInt(u[uOff[TEMP]], 3) * u_t[uOff[TEMP]];
1069444129b9SMatthew G. Knepley   // \phi_i \frac{p^{th}}{T^2} \left( - \nabla \cdot \vb{u} \psi_j + \frac{2}{T} \vb{u} \cdot \nabla T \psi_j \right)
1070444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1071444129b9SMatthew G. Knepley     g0[0] += p_th / PetscSqr(u[uOff[TEMP]]) * (-u_x[uOff_x[VEL] + d * dim + d] + 2.0 / u[uOff[TEMP]] * u[uOff[VEL] + d] * u_x[uOff_x[TEMP] + d]);
1072444129b9SMatthew G. Knepley   }
1073444129b9SMatthew G. Knepley }
1074444129b9SMatthew G. Knepley 
1075444129b9SMatthew G. Knepley static void g1_conduct_qT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1076444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1077444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1078444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
1079444129b9SMatthew G. Knepley {
1080444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1081444129b9SMatthew G. Knepley   PetscInt        d;
1082444129b9SMatthew G. Knepley 
1083444129b9SMatthew G. Knepley   // - \phi_i \frac{p^{th}}{T^2} \vb{u} \cdot \nabla \psi_j
1084444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1085444129b9SMatthew G. Knepley     g1[d] = -p_th / PetscSqr(u[uOff[TEMP]]) * u[uOff[VEL] + d];
1086444129b9SMatthew G. Knepley   }
1087444129b9SMatthew G. Knepley }
1088444129b9SMatthew G. Knepley 
1089649ef022SMatthew Knepley static void g2_vp(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1090649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1091649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1092649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[])
1093649ef022SMatthew Knepley {
1094649ef022SMatthew Knepley   PetscInt d;
1095649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) g2[d*dim+d] = -1.0;
1096649ef022SMatthew Knepley }
1097649ef022SMatthew Knepley 
1098649ef022SMatthew Knepley static void g3_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1099649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1100649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1101649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
1102649ef022SMatthew Knepley {
1103444129b9SMatthew G. Knepley    const PetscReal nu = PetscRealPart(constants[NU]);
1104649ef022SMatthew Knepley    const PetscInt  Nc = dim;
1105649ef022SMatthew Knepley    PetscInt        c, d;
1106649ef022SMatthew Knepley 
1107649ef022SMatthew Knepley   for (c = 0; c < Nc; ++c) {
1108649ef022SMatthew Knepley     for (d = 0; d < dim; ++d) {
1109606d57d4SMatthew G. Knepley       g3[((c*Nc+c)*dim+d)*dim+d] += nu;
1110606d57d4SMatthew G. Knepley       g3[((c*Nc+d)*dim+d)*dim+c] += nu;
1111649ef022SMatthew Knepley     }
1112649ef022SMatthew Knepley   }
1113649ef022SMatthew Knepley }
1114649ef022SMatthew Knepley 
1115444129b9SMatthew G. Knepley static void g0_conduct_vT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1116444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1117444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1118444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1119444129b9SMatthew G. Knepley {
1120444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
1121444129b9SMatthew G. Knepley   const PetscReal F    = PetscRealPart(constants[FROUDE]);
1122444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1123444129b9SMatthew G. Knepley   const PetscInt  gdir = (PetscInt) PetscRealPart(constants[G_DIR]);
1124444129b9SMatthew G. Knepley   const PetscInt  Nc = dim;
1125444129b9SMatthew G. Knepley   PetscInt        c, d;
1126444129b9SMatthew G. Knepley 
1127444129b9SMatthew G. Knepley   // - \vb{\phi}_i \cdot \vb{u}_t \frac{p^{th} S}{T^2} \psi_j
1128444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1129444129b9SMatthew G. Knepley     g0[d] -= p_th * S / PetscSqr(u[uOff[TEMP]]) * u_t[uOff[VEL] + d];
1130444129b9SMatthew G. Knepley   }
1131444129b9SMatthew G. Knepley 
1132444129b9SMatthew G. Knepley   // - \vb{\phi}_i \cdot \vb{u} \cdot \nabla \vb{u} \frac{p^{th}}{T^2} \psi_j
1133444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
1134444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1135444129b9SMatthew G. Knepley       g0[c] -= p_th / PetscSqr(u[uOff[TEMP]]) * u[uOff[VEL] + d] * u_x[uOff_x[VEL] + c * dim + d];
1136444129b9SMatthew G. Knepley     }
1137444129b9SMatthew G. Knepley   }
1138444129b9SMatthew G. Knepley 
1139444129b9SMatthew G. Knepley   // - \vb{\phi}_i \cdot \vu{z} \frac{p^{th}}{T^2 F^2} \psi_j
1140444129b9SMatthew G. Knepley   g0[gdir] -= p_th / PetscSqr(u[uOff[TEMP]] * F);
1141444129b9SMatthew G. Knepley }
1142444129b9SMatthew G. Knepley 
1143444129b9SMatthew G. Knepley static void g0_conduct_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1144444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1145444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1146444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1147444129b9SMatthew G. Knepley {
1148444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
1149444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1150444129b9SMatthew G. Knepley   const PetscInt  Nc   = dim;
1151444129b9SMatthew G. Knepley   PetscInt        c, d;
1152444129b9SMatthew G. Knepley 
1153444129b9SMatthew G. Knepley   // \vb{\phi}_i \cdot S \rho \psi_j
1154444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1155444129b9SMatthew G. Knepley     g0[d * dim + d] = S * p_th / u[uOff[TEMP]] * u_tShift;
1156444129b9SMatthew G. Knepley   }
1157444129b9SMatthew G. Knepley 
1158444129b9SMatthew G. Knepley   // \phi^c_i \cdot \rho \frac{\partial u^c}{\partial x^d} \psi^d_j
1159444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
1160444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1161444129b9SMatthew G. Knepley       g0[c * Nc + d] += p_th / u[uOff[TEMP]] * u_x[uOff_x[VEL] + c * Nc + d];
1162444129b9SMatthew G. Knepley     }
1163444129b9SMatthew G. Knepley   }
1164444129b9SMatthew G. Knepley }
1165444129b9SMatthew G. Knepley 
1166444129b9SMatthew G. Knepley static void g1_conduct_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1167444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1168444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1169444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
1170444129b9SMatthew G. Knepley {
1171444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1172444129b9SMatthew G. Knepley   const PetscInt  NcI = dim;
1173444129b9SMatthew G. Knepley   const PetscInt  NcJ = dim;
1174444129b9SMatthew G. Knepley   PetscInt        c, d, e;
1175444129b9SMatthew G. Knepley 
1176444129b9SMatthew G. Knepley   // \phi^c_i \rho u^e \frac{\partial \psi^d_j}{\partial x^e}
1177444129b9SMatthew G. Knepley   for (c = 0; c < NcI; ++c) {
1178444129b9SMatthew G. Knepley     for (d = 0; d < NcJ; ++d) {
1179444129b9SMatthew G. Knepley       for (e = 0; e < dim; ++e) {
1180444129b9SMatthew G. Knepley         if (c == d) {
1181444129b9SMatthew G. Knepley           g1[(c * NcJ + d) * dim + e] += p_th / u[uOff[TEMP]] * u[uOff[VEL] + e];
1182444129b9SMatthew G. Knepley         }
1183444129b9SMatthew G. Knepley       }
1184444129b9SMatthew G. Knepley     }
1185444129b9SMatthew G. Knepley   }
1186444129b9SMatthew G. Knepley }
1187444129b9SMatthew G. Knepley 
1188444129b9SMatthew G. Knepley static void g3_conduct_vu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1189444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1190444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1191444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
1192444129b9SMatthew G. Knepley {
1193444129b9SMatthew G. Knepley   const PetscReal Re = PetscRealPart(constants[REYNOLDS]);
1194444129b9SMatthew G. Knepley   const PetscReal mu = PetscRealPart(constants[MU]);
1195444129b9SMatthew G. Knepley   const PetscInt  Nc = dim;
1196444129b9SMatthew G. Knepley   PetscInt        c, d;
1197444129b9SMatthew G. Knepley 
1198444129b9SMatthew G. Knepley   for (c = 0; c < Nc; ++c) {
1199444129b9SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1200444129b9SMatthew G. Knepley       // \frac{\partial \phi^c_i}{\partial x^d} \mu/Re \frac{\partial \psi^c_i}{\partial x^d}
1201444129b9SMatthew G. Knepley       g3[((c * Nc + c) * dim + d) * dim + d] += mu / Re;  // gradU
1202444129b9SMatthew G. Knepley       // \frac{\partial \phi^c_i}{\partial x^d} \mu/Re \frac{\partial \psi^d_i}{\partial x^c}
1203444129b9SMatthew G. Knepley       g3[((c * Nc + d) * dim + d) * dim + c] += mu / Re;  // gradU transpose
1204444129b9SMatthew G. Knepley       // \frac{\partial \phi^c_i}{\partial x^d} -2/3 \mu/Re \frac{\partial \psi^d_i}{\partial x^c}
1205444129b9SMatthew G. Knepley       g3[((c * Nc + d) * dim + c) * dim + d] -= 2.0 / 3.0 * mu / Re;
1206444129b9SMatthew G. Knepley     }
1207444129b9SMatthew G. Knepley   }
1208444129b9SMatthew G. Knepley }
1209444129b9SMatthew G. Knepley 
1210444129b9SMatthew G. Knepley static void g2_conduct_vp(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1211444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1212444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1213444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[])
1214444129b9SMatthew G. Knepley {
1215444129b9SMatthew G. Knepley   PetscInt d;
1216444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1217444129b9SMatthew G. Knepley     g2[d * dim + d] = -1.0;
1218444129b9SMatthew G. Knepley   }
1219444129b9SMatthew G. Knepley }
1220444129b9SMatthew G. Knepley 
1221649ef022SMatthew Knepley static void g0_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1222649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1223649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1224649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1225649ef022SMatthew Knepley {
1226a712f3bbSMatthew G. Knepley   g0[0] = u_tShift;
1227649ef022SMatthew Knepley }
1228649ef022SMatthew Knepley 
1229649ef022SMatthew Knepley static void g0_wu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1230649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1231649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1232649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1233649ef022SMatthew Knepley {
1234649ef022SMatthew Knepley   PetscInt d;
1235649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) g0[d] = u_x[uOff_x[2]+d];
1236649ef022SMatthew Knepley }
1237649ef022SMatthew Knepley 
1238649ef022SMatthew Knepley static void g1_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1239649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1240649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1241649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
1242649ef022SMatthew Knepley {
1243649ef022SMatthew Knepley   PetscInt d;
1244649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) g1[d] = u[uOff[0]+d];
1245649ef022SMatthew Knepley }
1246649ef022SMatthew Knepley 
1247649ef022SMatthew Knepley static void g3_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1248649ef022SMatthew Knepley                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1249649ef022SMatthew Knepley                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1250649ef022SMatthew Knepley                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
1251649ef022SMatthew Knepley {
1252444129b9SMatthew G. Knepley   const PetscReal alpha = PetscRealPart(constants[ALPHA]);
1253649ef022SMatthew Knepley   PetscInt        d;
1254649ef022SMatthew Knepley 
1255649ef022SMatthew Knepley   for (d = 0; d < dim; ++d) g3[d*dim+d] = alpha;
1256649ef022SMatthew Knepley }
1257649ef022SMatthew Knepley 
1258444129b9SMatthew G. Knepley static void g0_conduct_wu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1259444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1260444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1261444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1262444129b9SMatthew G. Knepley {
1263444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1264444129b9SMatthew G. Knepley   const PetscReal c_p  = PetscRealPart(constants[C_P]);
1265444129b9SMatthew G. Knepley   PetscInt        d;
1266444129b9SMatthew G. Knepley 
1267444129b9SMatthew G. Knepley   // \phi_i \frac{C_p p^{th}}{T} \nabla T \cdot \psi_j
1268444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1269444129b9SMatthew G. Knepley     g0[d] = c_p * p_th / u[uOff[TEMP]] * u_x[uOff_x[TEMP] + d];
1270444129b9SMatthew G. Knepley   }
1271444129b9SMatthew G. Knepley }
1272444129b9SMatthew G. Knepley 
1273444129b9SMatthew G. Knepley static void g0_conduct_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1274444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1275444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1276444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
1277444129b9SMatthew G. Knepley {
1278444129b9SMatthew G. Knepley   const PetscReal S    = PetscRealPart(constants[STROUHAL]);
1279444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1280444129b9SMatthew G. Knepley   const PetscReal c_p  = PetscRealPart(constants[C_P]);
1281444129b9SMatthew G. Knepley   PetscInt        d;
1282444129b9SMatthew G. Knepley 
1283444129b9SMatthew G. Knepley   // \psi_i C_p S p^{th}\T \psi_{j}
1284444129b9SMatthew G. Knepley   g0[0] += c_p * S * p_th / u[uOff[TEMP]] * u_tShift;
1285444129b9SMatthew G. Knepley   // - \phi_i C_p S p^{th}/T^2 T_t \psi_j
1286444129b9SMatthew G. Knepley   g0[0] -= c_p * S * p_th / PetscSqr(u[uOff[TEMP]]) * u_t[uOff[TEMP]];
1287444129b9SMatthew G. Knepley   // - \phi_i C_p p^{th}/T^2 \vb{u} \cdot \nabla T \psi_j
1288444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1289444129b9SMatthew G. Knepley     g0[0] -= c_p * p_th / PetscSqr(u[uOff[TEMP]]) * u[uOff[VEL] + d] * u_x[uOff_x[TEMP] + d];
1290444129b9SMatthew G. Knepley   }
1291444129b9SMatthew G. Knepley }
1292444129b9SMatthew G. Knepley 
1293444129b9SMatthew G. Knepley static void g1_conduct_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1294444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1295444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1296444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
1297444129b9SMatthew G. Knepley {
1298444129b9SMatthew G. Knepley   const PetscReal p_th = PetscRealPart(constants[P_TH]);
1299444129b9SMatthew G. Knepley   const PetscReal c_p  = PetscRealPart(constants[C_P]);
1300444129b9SMatthew G. Knepley   PetscInt        d;
1301444129b9SMatthew G. Knepley 
1302444129b9SMatthew G. Knepley   // \phi_i C_p p^{th}/T \vb{u} \cdot \nabla \psi_j
1303444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1304444129b9SMatthew G. Knepley     g1[d] += c_p * p_th / u[uOff[TEMP]] * u[uOff[VEL] + d];
1305444129b9SMatthew G. Knepley   }
1306444129b9SMatthew G. Knepley }
1307444129b9SMatthew G. Knepley 
1308444129b9SMatthew G. Knepley static void g3_conduct_wT(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1309444129b9SMatthew G. Knepley                           const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1310444129b9SMatthew G. Knepley                           const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1311444129b9SMatthew G. Knepley                           PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
1312444129b9SMatthew G. Knepley {
1313444129b9SMatthew G. Knepley   const PetscReal Pe = PetscRealPart(constants[PECLET]);
1314444129b9SMatthew G. Knepley   const PetscReal k  = PetscRealPart(constants[K]);
1315444129b9SMatthew G. Knepley   PetscInt        d;
1316444129b9SMatthew G. Knepley 
1317444129b9SMatthew G. Knepley   // \nabla \phi_i \frac{k}{Pe} \nabla \phi_j
1318444129b9SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1319444129b9SMatthew G. Knepley     g3[d * dim + d] = k / Pe;
1320444129b9SMatthew G. Knepley   }
1321444129b9SMatthew G. Knepley }
1322444129b9SMatthew G. Knepley 
1323649ef022SMatthew Knepley static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
1324649ef022SMatthew Knepley {
1325444129b9SMatthew G. Knepley   PetscInt       mod, sol;
1326649ef022SMatthew Knepley   PetscErrorCode ierr;
1327649ef022SMatthew Knepley 
1328649ef022SMatthew Knepley   PetscFunctionBeginUser;
1329444129b9SMatthew G. Knepley   options->modType      = MOD_INCOMPRESSIBLE;
1330649ef022SMatthew Knepley   options->solType      = SOL_QUADRATIC;
1331444129b9SMatthew G. Knepley   options->hasNullSpace = PETSC_TRUE;
1332367970cfSMatthew G. Knepley   options->dmCell       = NULL;
1333649ef022SMatthew Knepley 
1334649ef022SMatthew Knepley   ierr = PetscOptionsBegin(comm, "", "Low Mach flow Problem Options", "DMPLEX");CHKERRQ(ierr);
1335444129b9SMatthew G. Knepley   mod = options->modType;
13365f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsEList("-mod_type", "The model type", "ex76.c", modTypes, NUM_MOD_TYPES, modTypes[options->modType], &mod, NULL));
1337444129b9SMatthew G. Knepley   options->modType = (ModType) mod;
1338649ef022SMatthew Knepley   sol = options->solType;
13395f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsEList("-sol_type", "The solution type", "ex76.c", solTypes, NUM_SOL_TYPES, solTypes[options->solType], &sol, NULL));
1340649ef022SMatthew Knepley   options->solType = (SolType) sol;
13411e1ea65dSPierre Jolivet   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1342649ef022SMatthew Knepley   PetscFunctionReturn(0);
1343649ef022SMatthew Knepley }
1344649ef022SMatthew Knepley 
1345444129b9SMatthew G. Knepley static PetscErrorCode SetupParameters(DM dm, AppCtx *user)
1346649ef022SMatthew Knepley {
1347649ef022SMatthew Knepley   PetscBag       bag;
1348649ef022SMatthew Knepley   Parameter     *p;
1349444129b9SMatthew G. Knepley   PetscReal      dir;
1350444129b9SMatthew G. Knepley   PetscInt       dim;
1351649ef022SMatthew Knepley 
1352649ef022SMatthew Knepley   PetscFunctionBeginUser;
13535f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
1354444129b9SMatthew G. Knepley   dir  = (PetscReal) (dim-1);
1355649ef022SMatthew Knepley   /* setup PETSc parameter bag */
13565f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagGetData(user->bag, (void **) &p));
13575f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagSetName(user->bag, "par", "Low Mach flow parameters"));
1358649ef022SMatthew Knepley   bag  = user->bag;
13595f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->Strouhal, 1.0, "S",     "Strouhal number"));
13605f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->Froude,   1.0, "Fr",    "Froude number"));
13615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->Reynolds, 1.0, "Re",    "Reynolds number"));
13625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->Peclet,   1.0, "Pe",    "Peclet number"));
13635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->p_th,     1.0, "p_th",  "Thermodynamic pressure"));
13645f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->mu,       1.0, "mu",    "Dynamic viscosity"));
13655f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->nu,       1.0, "nu",    "Kinematic viscosity"));
13665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->c_p,      1.0, "c_p",   "Specific heat at constant pressure"));
13675f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->k,        1.0, "k",     "Thermal conductivity"));
13685f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->alpha,    1.0, "alpha", "Thermal diffusivity"));
13695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->T_in,     1.0, "T_in",  "Inlet temperature"));
13705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->g_dir,    dir, "g_dir", "Gravity direction"));
13715f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagRegisterReal(bag, &p->epsilon,  1.0, "epsilon", "Perturbation strength"));
1372649ef022SMatthew Knepley   PetscFunctionReturn(0);
1373649ef022SMatthew Knepley }
1374649ef022SMatthew Knepley 
1375649ef022SMatthew Knepley static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
1376649ef022SMatthew Knepley {
1377649ef022SMatthew Knepley   PetscFunctionBeginUser;
13785f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(comm, dm));
13795f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(*dm, DMPLEX));
13805f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetFromOptions(*dm));
13815f80ce2aSJacob Faibussowitsch   CHKERRQ(DMViewFromOptions(*dm, NULL, "-dm_view"));
1382649ef022SMatthew Knepley   PetscFunctionReturn(0);
1383649ef022SMatthew Knepley }
1384649ef022SMatthew Knepley 
1385444129b9SMatthew G. Knepley static PetscErrorCode UniformBoundaryConditions(DM dm, DMLabel label, PetscSimplePointFunc exactFuncs[], PetscSimplePointFunc exactFuncs_t[], AppCtx *user)
1386444129b9SMatthew G. Knepley {
1387444129b9SMatthew G. Knepley   PetscDS        ds;
1388444129b9SMatthew G. Knepley   PetscInt       id;
1389444129b9SMatthew G. Knepley   void          *ctx;
1390444129b9SMatthew G. Knepley 
1391444129b9SMatthew G. Knepley   PetscFunctionBeginUser;
13925f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDS(dm, &ds));
13935f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagGetData(user->bag, &ctx));
1394444129b9SMatthew G. Knepley   id   = 3;
13955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "top wall velocity",    label, 1, &id, VEL, 0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
1396444129b9SMatthew G. Knepley   id   = 1;
13975f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "bottom wall velocity", label, 1, &id, VEL, 0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
1398444129b9SMatthew G. Knepley   id   = 2;
13995f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "right wall velocity",  label, 1, &id, VEL, 0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
1400444129b9SMatthew G. Knepley   id   = 4;
14015f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "left wall velocity",   label, 1, &id, VEL, 0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
1402444129b9SMatthew G. Knepley   id   = 3;
14035f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "top wall temp",    label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1404444129b9SMatthew G. Knepley   id   = 1;
14055f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "bottom wall temp", label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1406444129b9SMatthew G. Knepley   id   = 2;
14075f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "right wall temp",  label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1408444129b9SMatthew G. Knepley   id   = 4;
14095f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "left wall temp",   label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1410444129b9SMatthew G. Knepley   PetscFunctionReturn(0);
1411444129b9SMatthew G. Knepley }
1412444129b9SMatthew G. Knepley 
1413649ef022SMatthew Knepley static PetscErrorCode SetupProblem(DM dm, AppCtx *user)
1414649ef022SMatthew Knepley {
141545480ffeSMatthew G. Knepley   PetscSimplePointFunc exactFuncs[3];
141645480ffeSMatthew G. Knepley   PetscSimplePointFunc exactFuncs_t[3];
1417444129b9SMatthew G. Knepley   PetscDS              ds;
1418444129b9SMatthew G. Knepley   PetscWeakForm        wf;
141945480ffeSMatthew G. Knepley   DMLabel              label;
1420649ef022SMatthew Knepley   Parameter           *ctx;
1421444129b9SMatthew G. Knepley   PetscInt             id, bd;
1422649ef022SMatthew Knepley 
1423649ef022SMatthew Knepley   PetscFunctionBeginUser;
14245f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, "marker", &label));
14255f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDS(dm, &ds));
14265f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSGetWeakForm(ds, &wf));
1427444129b9SMatthew G. Knepley 
1428444129b9SMatthew G. Knepley   switch(user->modType) {
1429444129b9SMatthew G. Knepley     case MOD_INCOMPRESSIBLE:
14305f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetResidual(ds, VEL,  f0_v, f1_v));
14315f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetResidual(ds, PRES, f0_q, NULL));
14325f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetResidual(ds, TEMP, f0_w, f1_w));
1433444129b9SMatthew G. Knepley 
14345f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, VEL,  VEL,  g0_vu, g1_vu, NULL,  g3_vu));
14355f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, VEL,  PRES, NULL,  NULL,  g2_vp, NULL));
14365f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, PRES, VEL,  NULL,  g1_qu, NULL,  NULL));
14375f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, TEMP, VEL,  g0_wu, NULL,  NULL,  NULL));
14385f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, TEMP, TEMP, g0_wT, g1_wT, NULL,  g3_wT));
1439444129b9SMatthew G. Knepley 
1440649ef022SMatthew Knepley       switch(user->solType) {
1441649ef022SMatthew Knepley       case SOL_QUADRATIC:
14425f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, VEL,  0, 1, f0_quadratic_v, 0, NULL));
14435f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_quadratic_w, 0, NULL));
1444649ef022SMatthew Knepley 
1445444129b9SMatthew G. Knepley         exactFuncs[VEL]    = quadratic_u;
1446444129b9SMatthew G. Knepley         exactFuncs[PRES]   = quadratic_p;
1447444129b9SMatthew G. Knepley         exactFuncs[TEMP]   = quadratic_T;
1448444129b9SMatthew G. Knepley         exactFuncs_t[VEL]  = quadratic_u_t;
1449444129b9SMatthew G. Knepley         exactFuncs_t[PRES] = NULL;
1450444129b9SMatthew G. Knepley         exactFuncs_t[TEMP] = quadratic_T_t;
1451444129b9SMatthew G. Knepley 
14525f80ce2aSJacob Faibussowitsch         CHKERRQ(UniformBoundaryConditions(dm, label, exactFuncs, exactFuncs_t, user));
1453649ef022SMatthew Knepley         break;
1454649ef022SMatthew Knepley       case SOL_CUBIC:
14555f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, VEL,  0, 1, f0_cubic_v, 0, NULL));
14565f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_cubic_w, 0, NULL));
1457649ef022SMatthew Knepley 
1458444129b9SMatthew G. Knepley         exactFuncs[VEL]    = cubic_u;
1459444129b9SMatthew G. Knepley         exactFuncs[PRES]   = cubic_p;
1460444129b9SMatthew G. Knepley         exactFuncs[TEMP]   = cubic_T;
1461444129b9SMatthew G. Knepley         exactFuncs_t[VEL]  = cubic_u_t;
1462444129b9SMatthew G. Knepley         exactFuncs_t[PRES] = NULL;
1463444129b9SMatthew G. Knepley         exactFuncs_t[TEMP] = cubic_T_t;
1464444129b9SMatthew G. Knepley 
14655f80ce2aSJacob Faibussowitsch         CHKERRQ(UniformBoundaryConditions(dm, label, exactFuncs, exactFuncs_t, user));
1466649ef022SMatthew Knepley         break;
1467649ef022SMatthew Knepley       case SOL_CUBIC_TRIG:
14685f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, VEL,  0, 1, f0_cubic_trig_v, 0, NULL));
14695f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_cubic_trig_w, 0, NULL));
1470649ef022SMatthew Knepley 
1471444129b9SMatthew G. Knepley         exactFuncs[VEL]    = cubic_trig_u;
1472444129b9SMatthew G. Knepley         exactFuncs[PRES]   = cubic_trig_p;
1473444129b9SMatthew G. Knepley         exactFuncs[TEMP]   = cubic_trig_T;
1474444129b9SMatthew G. Knepley         exactFuncs_t[VEL]  = cubic_trig_u_t;
1475444129b9SMatthew G. Knepley         exactFuncs_t[PRES] = NULL;
1476444129b9SMatthew G. Knepley         exactFuncs_t[TEMP] = cubic_trig_T_t;
1477444129b9SMatthew G. Knepley 
14785f80ce2aSJacob Faibussowitsch         CHKERRQ(UniformBoundaryConditions(dm, label, exactFuncs, exactFuncs_t, user));
1479649ef022SMatthew Knepley         break;
1480606d57d4SMatthew G. Knepley       case SOL_TAYLOR_GREEN:
14815f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_taylor_green_w, 0, NULL));
1482606d57d4SMatthew G. Knepley 
1483444129b9SMatthew G. Knepley         exactFuncs[VEL]    = taylor_green_u;
1484444129b9SMatthew G. Knepley         exactFuncs[PRES]   = taylor_green_p;
1485444129b9SMatthew G. Knepley         exactFuncs[TEMP]   = taylor_green_T;
1486444129b9SMatthew G. Knepley         exactFuncs_t[VEL]  = taylor_green_u_t;
1487444129b9SMatthew G. Knepley         exactFuncs_t[PRES] = taylor_green_p_t;
1488444129b9SMatthew G. Knepley         exactFuncs_t[TEMP] = taylor_green_T_t;
1489444129b9SMatthew G. Knepley 
14905f80ce2aSJacob Faibussowitsch         CHKERRQ(UniformBoundaryConditions(dm, label, exactFuncs, exactFuncs_t, user));
1491606d57d4SMatthew G. Knepley         break;
149298921bdaSJacob Faibussowitsch        default: SETERRQ(PetscObjectComm((PetscObject) ds), PETSC_ERR_ARG_WRONG, "Unsupported solution type: %s (%D)", solTypes[PetscMin(user->solType, NUM_SOL_TYPES)], user->solType);
1493649ef022SMatthew Knepley       }
1494444129b9SMatthew G. Knepley       break;
1495444129b9SMatthew G. Knepley     case MOD_CONDUCTING:
14965f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetResidual(ds, VEL,  f0_conduct_v, f1_conduct_v));
14975f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetResidual(ds, PRES, f0_conduct_q, NULL));
14985f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetResidual(ds, TEMP, f0_conduct_w, f1_conduct_w));
1499649ef022SMatthew Knepley 
15005f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, VEL,  VEL,  g0_conduct_vu, g1_conduct_vu, NULL,          g3_conduct_vu));
15015f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, VEL,  PRES, NULL,          NULL,          g2_conduct_vp, NULL));
15025f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, VEL,  TEMP, g0_conduct_vT, NULL,          NULL,          NULL));
15035f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, PRES, VEL,  g0_conduct_qu, g1_conduct_qu, NULL,          NULL));
15045f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, PRES, TEMP, g0_conduct_qT, g1_conduct_qT, NULL,          NULL));
15055f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, TEMP, VEL,  g0_conduct_wu, NULL,          NULL,          NULL));
15065f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetJacobian(ds, TEMP, TEMP, g0_conduct_wT, g1_conduct_wT, NULL,          g3_conduct_wT));
1507649ef022SMatthew Knepley 
1508444129b9SMatthew G. Knepley       switch(user->solType) {
1509444129b9SMatthew G. Knepley       case SOL_QUADRATIC:
15105f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, VEL,  0, 1, f0_conduct_quadratic_v, 0, NULL));
15115f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, PRES, 0, 1, f0_conduct_quadratic_q, 0, NULL));
15125f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_conduct_quadratic_w, 0, NULL));
1513444129b9SMatthew G. Knepley 
1514444129b9SMatthew G. Knepley         exactFuncs[VEL]    = quadratic_u;
1515444129b9SMatthew G. Knepley         exactFuncs[PRES]   = quadratic_p;
1516444129b9SMatthew G. Knepley         exactFuncs[TEMP]   = quadratic_T;
1517444129b9SMatthew G. Knepley         exactFuncs_t[VEL]  = quadratic_u_t;
1518444129b9SMatthew G. Knepley         exactFuncs_t[PRES] = NULL;
1519444129b9SMatthew G. Knepley         exactFuncs_t[TEMP] = quadratic_T_t;
1520444129b9SMatthew G. Knepley 
15215f80ce2aSJacob Faibussowitsch         CHKERRQ(UniformBoundaryConditions(dm, label, exactFuncs, exactFuncs_t, user));
1522444129b9SMatthew G. Knepley         break;
1523444129b9SMatthew G. Knepley       case SOL_PIPE:
1524444129b9SMatthew G. Knepley         user->hasNullSpace = PETSC_FALSE;
15255f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, VEL,  0, 1, f0_conduct_pipe_v, 0, NULL));
15265f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, PRES, 0, 1, f0_conduct_pipe_q, 0, NULL));
15275f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_conduct_pipe_w, 0, NULL));
1528444129b9SMatthew G. Knepley 
1529444129b9SMatthew G. Knepley         exactFuncs[VEL]    = pipe_u;
1530444129b9SMatthew G. Knepley         exactFuncs[PRES]   = pipe_p;
1531444129b9SMatthew G. Knepley         exactFuncs[TEMP]   = pipe_T;
1532444129b9SMatthew G. Knepley         exactFuncs_t[VEL]  = pipe_u_t;
1533444129b9SMatthew G. Knepley         exactFuncs_t[PRES] = pipe_p_t;
1534444129b9SMatthew G. Knepley         exactFuncs_t[TEMP] = pipe_T_t;
1535444129b9SMatthew G. Knepley 
15365f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscBagGetData(user->bag, (void **) &ctx));
1537444129b9SMatthew G. Knepley         id   = 2;
15385f80ce2aSJacob Faibussowitsch         CHKERRQ(DMAddBoundary(dm, DM_BC_NATURAL, "right wall", label, 1, &id, 0, 0, NULL, NULL, NULL, ctx, &bd));
15395f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSGetBoundary(ds, bd, &wf, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
15405f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexBdResidual(wf, label, id, VEL, 0, 0, f0_conduct_bd_pipe_v, 0, NULL));
1541444129b9SMatthew G. Knepley         id   = 4;
15425f80ce2aSJacob Faibussowitsch         CHKERRQ(DMAddBoundary(dm, DM_BC_NATURAL, "left wall", label, 1, &id, 0, 0, NULL, NULL, NULL, ctx, &bd));
15435f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSGetBoundary(ds, bd, &wf, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
15445f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexBdResidual(wf, label, id, VEL, 0, 0, f0_conduct_bd_pipe_v, 0, NULL));
1545444129b9SMatthew G. Knepley         id   = 4;
15465f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "left wall temperature",   label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1547444129b9SMatthew G. Knepley         id   = 3;
15485f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "top wall velocity",       label, 1, &id, VEL,  0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
15495f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "top wall temperature",    label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1550444129b9SMatthew G. Knepley         id   = 1;
15515f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "bottom wall velocity",    label, 1, &id, VEL,  0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
15525f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "bottom wall temperature", label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1553444129b9SMatthew G. Knepley         break;
1554367970cfSMatthew G. Knepley       case SOL_PIPE_WIGGLY:
1555367970cfSMatthew G. Knepley         user->hasNullSpace = PETSC_FALSE;
15565f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, VEL,  0, 1, f0_conduct_pipe_wiggly_v, 0, NULL));
15575f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, PRES, 0, 1, f0_conduct_pipe_wiggly_q, 0, NULL));
15585f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexResidual(wf, NULL, 0, TEMP, 0, 1, f0_conduct_pipe_wiggly_w, 0, NULL));
1559367970cfSMatthew G. Knepley 
1560367970cfSMatthew G. Knepley         exactFuncs[VEL]    = pipe_wiggly_u;
1561367970cfSMatthew G. Knepley         exactFuncs[PRES]   = pipe_wiggly_p;
1562367970cfSMatthew G. Knepley         exactFuncs[TEMP]   = pipe_wiggly_T;
1563367970cfSMatthew G. Knepley         exactFuncs_t[VEL]  = pipe_wiggly_u_t;
1564367970cfSMatthew G. Knepley         exactFuncs_t[PRES] = pipe_wiggly_p_t;
1565367970cfSMatthew G. Knepley         exactFuncs_t[TEMP] = pipe_wiggly_T_t;
1566367970cfSMatthew G. Knepley 
15675f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscBagGetData(user->bag, (void **) &ctx));
1568367970cfSMatthew G. Knepley         id   = 2;
15695f80ce2aSJacob Faibussowitsch         CHKERRQ(DMAddBoundary(dm, DM_BC_NATURAL, "right wall", label, 1, &id, 0, 0, NULL, NULL, NULL, ctx, &bd));
15705f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSGetBoundary(ds, bd, &wf, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
15715f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexBdResidual(wf, label, id, VEL, 0, 0, f0_conduct_bd_pipe_wiggly_v, 0, NULL));
1572367970cfSMatthew G. Knepley         id   = 4;
15735f80ce2aSJacob Faibussowitsch         CHKERRQ(DMAddBoundary(dm, DM_BC_NATURAL, "left wall", label, 1, &id, 0, 0, NULL, NULL, NULL, ctx, &bd));
15745f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSGetBoundary(ds, bd, &wf, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
15755f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscWeakFormSetIndexBdResidual(wf, label, id, VEL, 0, 0, f0_conduct_bd_pipe_wiggly_v, 0, NULL));
1576367970cfSMatthew G. Knepley         id   = 4;
15775f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "left wall temperature",   label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1578367970cfSMatthew G. Knepley         id   = 3;
15795f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "top wall velocity",       label, 1, &id, VEL,  0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
15805f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "top wall temperature",    label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1581367970cfSMatthew G. Knepley         id   = 1;
15825f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "bottom wall velocity",    label, 1, &id, VEL,  0, NULL, (void (*)(void)) exactFuncs[VEL], (void (*)(void)) exactFuncs_t[VEL], ctx, NULL));
15835f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSAddBoundary(ds, DM_BC_ESSENTIAL, "bottom wall temperature", label, 1, &id, TEMP, 0, NULL, (void (*)(void)) exactFuncs[TEMP], (void (*)(void)) exactFuncs_t[TEMP], ctx, NULL));
1584367970cfSMatthew G. Knepley         break;
158598921bdaSJacob Faibussowitsch        default: SETERRQ(PetscObjectComm((PetscObject) ds), PETSC_ERR_ARG_WRONG, "Unsupported solution type: %s (%D)", solTypes[PetscMin(user->solType, NUM_SOL_TYPES)], user->solType);
1586444129b9SMatthew G. Knepley       }
1587444129b9SMatthew G. Knepley       break;
1588b009a0cbSMatthew G. Knepley     default: SETERRQ(PetscObjectComm((PetscObject) ds), PETSC_ERR_ARG_WRONG, "Unsupported model type: %s (%D)", modTypes[PetscMin(user->modType, NUM_MOD_TYPES)], user->modType);
1589444129b9SMatthew G. Knepley   }
1590649ef022SMatthew Knepley   /* Setup constants */
1591649ef022SMatthew Knepley   {
1592649ef022SMatthew Knepley     Parameter  *param;
1593367970cfSMatthew G. Knepley     PetscScalar constants[13];
1594649ef022SMatthew Knepley 
15955f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscBagGetData(user->bag, (void **) &param));
1596649ef022SMatthew Knepley 
1597444129b9SMatthew G. Knepley     constants[STROUHAL] = param->Strouhal;
1598444129b9SMatthew G. Knepley     constants[FROUDE]   = param->Froude;
1599444129b9SMatthew G. Knepley     constants[REYNOLDS] = param->Reynolds;
1600444129b9SMatthew G. Knepley     constants[PECLET]   = param->Peclet;
1601444129b9SMatthew G. Knepley     constants[P_TH]     = param->p_th;
1602444129b9SMatthew G. Knepley     constants[MU]       = param->mu;
1603444129b9SMatthew G. Knepley     constants[NU]       = param->nu;
1604444129b9SMatthew G. Knepley     constants[C_P]      = param->c_p;
1605444129b9SMatthew G. Knepley     constants[K]        = param->k;
1606444129b9SMatthew G. Knepley     constants[ALPHA]    = param->alpha;
1607444129b9SMatthew G. Knepley     constants[T_IN]     = param->T_in;
1608444129b9SMatthew G. Knepley     constants[G_DIR]    = param->g_dir;
1609367970cfSMatthew G. Knepley     constants[EPSILON]  = param->epsilon;
16105f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSSetConstants(ds, 13, constants));
1611649ef022SMatthew Knepley   }
1612649ef022SMatthew Knepley 
16135f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagGetData(user->bag, (void **) &ctx));
16145f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSetExactSolution(ds, VEL,  exactFuncs[VEL],  ctx));
16155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSetExactSolution(ds, PRES, exactFuncs[PRES], ctx));
16165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSetExactSolution(ds, TEMP, exactFuncs[TEMP], ctx));
16175f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSetExactSolutionTimeDerivative(ds, VEL,  exactFuncs_t[VEL],  ctx));
16185f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSetExactSolutionTimeDerivative(ds, PRES, exactFuncs_t[PRES], ctx));
16195f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSetExactSolutionTimeDerivative(ds, TEMP, exactFuncs_t[TEMP], ctx));
1620649ef022SMatthew Knepley   PetscFunctionReturn(0);
1621649ef022SMatthew Knepley }
1622649ef022SMatthew Knepley 
1623367970cfSMatthew G. Knepley static PetscErrorCode CreateCellDM(DM dm, AppCtx *user)
1624367970cfSMatthew G. Knepley {
1625367970cfSMatthew G. Knepley   PetscFE        fe, fediv;
1626367970cfSMatthew G. Knepley   DMPolytopeType ct;
1627367970cfSMatthew G. Knepley   PetscInt       dim, cStart;
1628367970cfSMatthew G. Knepley   PetscBool      simplex;
1629367970cfSMatthew G. Knepley 
1630367970cfSMatthew G. Knepley   PetscFunctionBeginUser;
16315f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
16325f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
16335f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetCellType(dm, cStart, &ct));
1634367970cfSMatthew G. Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
1635367970cfSMatthew G. Knepley 
16365f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetField(dm, VEL,  NULL, (PetscObject *) &fe));
16375f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "div_", PETSC_DEFAULT, &fediv));
16385f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECopyQuadrature(fe, fediv));
16395f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) fediv, "divergence"));
1640367970cfSMatthew G. Knepley 
16415f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&user->dmCell));
16425f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClone(dm, &user->dmCell));
16435f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetField(user->dmCell, 0, NULL, (PetscObject) fediv));
16445f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateDS(user->dmCell));
16455f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFEDestroy(&fediv));
1646367970cfSMatthew G. Knepley   PetscFunctionReturn(0);
1647367970cfSMatthew G. Knepley }
1648367970cfSMatthew G. Knepley 
1649367970cfSMatthew G. Knepley static PetscErrorCode GetCellDM(DM dm, AppCtx *user, DM *dmCell)
1650367970cfSMatthew G. Knepley {
1651367970cfSMatthew G. Knepley   PetscInt       cStart, cEnd, cellStart = -1, cellEnd = -1;
1652367970cfSMatthew G. Knepley 
1653367970cfSMatthew G. Knepley   PetscFunctionBeginUser;
16545f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
16555f80ce2aSJacob Faibussowitsch   if (user->dmCell) CHKERRQ(DMPlexGetSimplexOrBoxCells(user->dmCell, 0, &cellStart, &cellEnd));
16565f80ce2aSJacob Faibussowitsch   if (cStart != cellStart || cEnd != cellEnd) CHKERRQ(CreateCellDM(dm, user));
1657367970cfSMatthew G. Knepley   *dmCell = user->dmCell;
1658367970cfSMatthew G. Knepley   PetscFunctionReturn(0);
1659367970cfSMatthew G. Knepley }
1660367970cfSMatthew G. Knepley 
1661649ef022SMatthew Knepley static PetscErrorCode SetupDiscretization(DM dm, AppCtx *user)
1662649ef022SMatthew Knepley {
1663649ef022SMatthew Knepley   DM              cdm = dm;
1664367970cfSMatthew G. Knepley   PetscFE         fe[3];
1665649ef022SMatthew Knepley   Parameter      *param;
1666649ef022SMatthew Knepley   DMPolytopeType  ct;
1667649ef022SMatthew Knepley   PetscInt        dim, cStart;
1668649ef022SMatthew Knepley   PetscBool       simplex;
1669649ef022SMatthew Knepley 
1670649ef022SMatthew Knepley   PetscFunctionBeginUser;
16715f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
16725f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
16735f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetCellType(dm, cStart, &ct));
1674649ef022SMatthew Knepley   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
1675649ef022SMatthew Knepley   /* Create finite element */
16765f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECreateDefault(PETSC_COMM_SELF, dim, dim, simplex, "vel_", PETSC_DEFAULT, &fe[0]));
16775f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) fe[0], "velocity"));
1678649ef022SMatthew Knepley 
16795f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "pres_", PETSC_DEFAULT, &fe[1]));
16805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECopyQuadrature(fe[0], fe[1]));
16815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) fe[1], "pressure"));
1682649ef022SMatthew Knepley 
16835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "temp_", PETSC_DEFAULT, &fe[2]));
16845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECopyQuadrature(fe[0], fe[2]));
16855f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) fe[2], "temperature"));
1686649ef022SMatthew Knepley 
1687649ef022SMatthew Knepley   /* Set discretization and boundary conditions for each mesh */
16885f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetField(dm, VEL,  NULL, (PetscObject) fe[VEL]));
16895f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetField(dm, PRES, NULL, (PetscObject) fe[PRES]));
16905f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetField(dm, TEMP, NULL, (PetscObject) fe[TEMP]));
16915f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateDS(dm));
16925f80ce2aSJacob Faibussowitsch   CHKERRQ(SetupProblem(dm, user));
16935f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagGetData(user->bag, (void **) &param));
1694649ef022SMatthew Knepley   while (cdm) {
16955f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCopyDisc(dm, cdm));
16965f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoarseDM(cdm, &cdm));
1697649ef022SMatthew Knepley   }
16985f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFEDestroy(&fe[VEL]));
16995f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFEDestroy(&fe[PRES]));
17005f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFEDestroy(&fe[TEMP]));
1701649ef022SMatthew Knepley 
1702444129b9SMatthew G. Knepley   if (user->hasNullSpace) {
1703649ef022SMatthew Knepley     PetscObject  pressure;
1704649ef022SMatthew Knepley     MatNullSpace nullspacePres;
1705649ef022SMatthew Knepley 
17065f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetField(dm, PRES, NULL, &pressure));
17075f80ce2aSJacob Faibussowitsch     CHKERRQ(MatNullSpaceCreate(PetscObjectComm(pressure), PETSC_TRUE, 0, NULL, &nullspacePres));
17085f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectCompose(pressure, "nullspace", (PetscObject) nullspacePres));
17095f80ce2aSJacob Faibussowitsch     CHKERRQ(MatNullSpaceDestroy(&nullspacePres));
1710649ef022SMatthew Knepley   }
1711649ef022SMatthew Knepley   PetscFunctionReturn(0);
1712649ef022SMatthew Knepley }
1713649ef022SMatthew Knepley 
1714649ef022SMatthew Knepley static PetscErrorCode CreatePressureNullSpace(DM dm, PetscInt ofield, PetscInt nfield, MatNullSpace *nullSpace)
1715649ef022SMatthew Knepley {
1716649ef022SMatthew Knepley   Vec              vec;
1717649ef022SMatthew Knepley   PetscErrorCode (*funcs[3])(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *) = {zero, zero, zero};
1718649ef022SMatthew Knepley 
1719649ef022SMatthew Knepley   PetscFunctionBeginUser;
17203c633725SBarry Smith   PetscCheck(ofield == PRES,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Nullspace must be for pressure field at index %D, not %D", PRES, ofield);
1721649ef022SMatthew Knepley   funcs[nfield] = constant;
17225f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateGlobalVector(dm, &vec));
17235f80ce2aSJacob Faibussowitsch   CHKERRQ(DMProjectFunction(dm, 0.0, funcs, NULL, INSERT_ALL_VALUES, vec));
17245f80ce2aSJacob Faibussowitsch   CHKERRQ(VecNormalize(vec, NULL));
17255f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) vec, "Pressure Null Space"));
17265f80ce2aSJacob Faibussowitsch   CHKERRQ(VecViewFromOptions(vec, NULL, "-pressure_nullspace_view"));
17275f80ce2aSJacob Faibussowitsch   CHKERRQ(MatNullSpaceCreate(PetscObjectComm((PetscObject) dm), PETSC_FALSE, 1, &vec, nullSpace));
17285f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&vec));
1729649ef022SMatthew Knepley   PetscFunctionReturn(0);
1730649ef022SMatthew Knepley }
1731649ef022SMatthew Knepley 
1732649ef022SMatthew Knepley static PetscErrorCode RemoveDiscretePressureNullspace_Private(TS ts, Vec u)
1733649ef022SMatthew Knepley {
1734649ef022SMatthew Knepley   DM             dm;
1735444129b9SMatthew G. Knepley   AppCtx        *user;
1736649ef022SMatthew Knepley   MatNullSpace   nullsp;
1737649ef022SMatthew Knepley 
1738649ef022SMatthew Knepley   PetscFunctionBegin;
17395f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetDM(ts, &dm));
17405f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetApplicationContext(dm, &user));
1741444129b9SMatthew G. Knepley   if (!user->hasNullSpace) PetscFunctionReturn(0);
17425f80ce2aSJacob Faibussowitsch   CHKERRQ(CreatePressureNullSpace(dm, 1, 1, &nullsp));
17435f80ce2aSJacob Faibussowitsch   CHKERRQ(MatNullSpaceRemove(nullsp, u));
17445f80ce2aSJacob Faibussowitsch   CHKERRQ(MatNullSpaceDestroy(&nullsp));
1745649ef022SMatthew Knepley   PetscFunctionReturn(0);
1746649ef022SMatthew Knepley }
1747649ef022SMatthew Knepley 
1748649ef022SMatthew Knepley /* Make the discrete pressure discretely divergence free */
1749649ef022SMatthew Knepley static PetscErrorCode RemoveDiscretePressureNullspace(TS ts)
1750649ef022SMatthew Knepley {
1751649ef022SMatthew Knepley   Vec            u;
1752649ef022SMatthew Knepley 
1753649ef022SMatthew Knepley   PetscFunctionBegin;
17545f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetSolution(ts, &u));
17555f80ce2aSJacob Faibussowitsch   CHKERRQ(RemoveDiscretePressureNullspace_Private(ts, u));
1756649ef022SMatthew Knepley   PetscFunctionReturn(0);
1757649ef022SMatthew Knepley }
1758649ef022SMatthew Knepley 
1759367970cfSMatthew G. Knepley static void divergence(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1760367970cfSMatthew G. Knepley                        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1761367970cfSMatthew G. Knepley                        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1762367970cfSMatthew G. Knepley                        PetscReal t, const PetscReal X[], PetscInt numConstants, const PetscScalar constants[], PetscScalar divu[])
1763367970cfSMatthew G. Knepley {
1764367970cfSMatthew G. Knepley   PetscInt d;
1765367970cfSMatthew G. Knepley 
1766367970cfSMatthew G. Knepley   divu[0] = 0.;
1767367970cfSMatthew G. Knepley   for (d = 0; d < dim; ++d) divu[0] += u_x[d*dim+d];
1768367970cfSMatthew G. Knepley }
1769367970cfSMatthew G. Knepley 
1770649ef022SMatthew Knepley static PetscErrorCode SetInitialConditions(TS ts, Vec u)
1771649ef022SMatthew Knepley {
1772444129b9SMatthew G. Knepley   AppCtx        *user;
1773649ef022SMatthew Knepley   DM             dm;
1774649ef022SMatthew Knepley   PetscReal      t;
1775649ef022SMatthew Knepley 
1776649ef022SMatthew Knepley   PetscFunctionBegin;
17775f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetDM(ts, &dm));
17785f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetTime(ts, &t));
17795f80ce2aSJacob Faibussowitsch   CHKERRQ(DMComputeExactSolution(dm, t, u, NULL));
17805f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetApplicationContext(dm, &user));
17815f80ce2aSJacob Faibussowitsch   CHKERRQ(RemoveDiscretePressureNullspace_Private(ts, u));
1782649ef022SMatthew Knepley   PetscFunctionReturn(0);
1783649ef022SMatthew Knepley }
1784649ef022SMatthew Knepley 
1785649ef022SMatthew Knepley static PetscErrorCode MonitorError(TS ts, PetscInt step, PetscReal crtime, Vec u, void *ctx)
1786649ef022SMatthew Knepley {
1787649ef022SMatthew Knepley   PetscErrorCode (*exactFuncs[3])(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx);
1788649ef022SMatthew Knepley   void            *ctxs[3];
1789a712f3bbSMatthew G. Knepley   PetscPointFunc   diagnostics[1] = {divergence};
1790367970cfSMatthew G. Knepley   DM               dm, dmCell = NULL;
1791649ef022SMatthew Knepley   PetscDS          ds;
1792a712f3bbSMatthew G. Knepley   Vec              v, divu;
1793a712f3bbSMatthew G. Knepley   PetscReal        ferrors[3], massFlux;
1794649ef022SMatthew Knepley   PetscInt         f;
1795649ef022SMatthew Knepley 
1796649ef022SMatthew Knepley   PetscFunctionBeginUser;
17975f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetDM(ts, &dm));
17985f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDS(dm, &ds));
1799649ef022SMatthew Knepley 
18005f80ce2aSJacob Faibussowitsch   for (f = 0; f < 3; ++f) CHKERRQ(PetscDSGetExactSolution(ds, f, &exactFuncs[f], &ctxs[f]));
18015f80ce2aSJacob Faibussowitsch   CHKERRQ(DMComputeL2FieldDiff(dm, crtime, exactFuncs, ctxs, u, ferrors));
18025f80ce2aSJacob Faibussowitsch   CHKERRQ(GetCellDM(dm, (AppCtx *) ctx, &dmCell));
18035f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetGlobalVector(dmCell, &divu));
18045f80ce2aSJacob Faibussowitsch   CHKERRQ(DMProjectField(dmCell, crtime, u, diagnostics, INSERT_VALUES, divu));
18055f80ce2aSJacob Faibussowitsch   CHKERRQ(VecViewFromOptions(divu, NULL, "-divu_vec_view"));
18065f80ce2aSJacob Faibussowitsch   CHKERRQ(VecNorm(divu, NORM_2, &massFlux));
18075f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD, "Timestep: %04d time = %-8.4g \t L_2 Error: [%2.3g, %2.3g, %2.3g] ||div u||: %2.3g\n", (int) step, (double) crtime, (double) ferrors[0], (double) ferrors[1], (double) ferrors[2], (double) massFlux));
1808649ef022SMatthew Knepley 
18095f80ce2aSJacob Faibussowitsch   CHKERRQ(VecViewFromOptions(u, NULL, "-sol_vec_view"));
1810649ef022SMatthew Knepley 
18115f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetGlobalVector(dm, &v));
18125f80ce2aSJacob Faibussowitsch   CHKERRQ(DMProjectFunction(dm, crtime, exactFuncs, ctxs, INSERT_ALL_VALUES, v));
18135f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) v, "Exact Solution"));
18145f80ce2aSJacob Faibussowitsch   CHKERRQ(VecViewFromOptions(v, NULL, "-exact_vec_view"));
18155f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreGlobalVector(dm, &v));
1816649ef022SMatthew Knepley 
18175f80ce2aSJacob Faibussowitsch   CHKERRQ(VecViewFromOptions(divu, NULL, "-div_vec_view"));
18185f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreGlobalVector(dmCell, &divu));
1819a712f3bbSMatthew G. Knepley 
1820649ef022SMatthew Knepley   PetscFunctionReturn(0);
1821649ef022SMatthew Knepley }
1822649ef022SMatthew Knepley 
1823649ef022SMatthew Knepley int main(int argc, char **argv)
1824649ef022SMatthew Knepley {
1825649ef022SMatthew Knepley   DM              dm;   /* problem definition */
1826649ef022SMatthew Knepley   TS              ts;   /* timestepper */
1827649ef022SMatthew Knepley   Vec             u;    /* solution */
1828649ef022SMatthew Knepley   AppCtx          user; /* user-defined work context */
1829649ef022SMatthew Knepley   PetscReal       t;
1830649ef022SMatthew Knepley 
1831*b122ec5aSJacob Faibussowitsch   CHKERRQ(PetscInitialize(&argc, &argv, NULL,help));
18325f80ce2aSJacob Faibussowitsch   CHKERRQ(ProcessOptions(PETSC_COMM_WORLD, &user));
18335f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagCreate(PETSC_COMM_WORLD, sizeof(Parameter), &user.bag));
18345f80ce2aSJacob Faibussowitsch   CHKERRQ(CreateMesh(PETSC_COMM_WORLD, &user, &dm));
18355f80ce2aSJacob Faibussowitsch   CHKERRQ(SetupParameters(dm, &user));
18365f80ce2aSJacob Faibussowitsch   CHKERRQ(TSCreate(PETSC_COMM_WORLD, &ts));
18375f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetDM(ts, dm));
18385f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetApplicationContext(dm, &user));
1839649ef022SMatthew Knepley   /* Setup problem */
18405f80ce2aSJacob Faibussowitsch   CHKERRQ(SetupDiscretization(dm, &user));
18415f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateClosureIndex(dm, NULL));
1842649ef022SMatthew Knepley 
18435f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateGlobalVector(dm, &u));
18445f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) u, "Numerical Solution"));
18455f80ce2aSJacob Faibussowitsch   if (user.hasNullSpace) CHKERRQ(DMSetNullSpaceConstructor(dm, 1, CreatePressureNullSpace));
1846649ef022SMatthew Knepley 
18475f80ce2aSJacob Faibussowitsch   CHKERRQ(DMTSSetBoundaryLocal(dm, DMPlexTSComputeBoundary, &user));
18485f80ce2aSJacob Faibussowitsch   CHKERRQ(DMTSSetIFunctionLocal(dm, DMPlexTSComputeIFunctionFEM, &user));
18495f80ce2aSJacob Faibussowitsch   CHKERRQ(DMTSSetIJacobianLocal(dm, DMPlexTSComputeIJacobianFEM, &user));
18505f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP));
18515f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetPreStep(ts, RemoveDiscretePressureNullspace));
18525f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetFromOptions(ts));
1853649ef022SMatthew Knepley 
18545f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetComputeInitialCondition(ts, SetInitialConditions)); /* Must come after SetFromOptions() */
18555f80ce2aSJacob Faibussowitsch   CHKERRQ(SetInitialConditions(ts, u));
18565f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetTime(ts, &t));
18575f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetOutputSequenceNumber(dm, 0, t));
18585f80ce2aSJacob Faibussowitsch   CHKERRQ(DMTSCheckFromOptions(ts, u));
18595f80ce2aSJacob Faibussowitsch   CHKERRQ(TSMonitorSet(ts, MonitorError, &user, NULL));
1860649ef022SMatthew Knepley 
18615f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSolve(ts, u));
18625f80ce2aSJacob Faibussowitsch   CHKERRQ(DMTSCheckFromOptions(ts, u));
1863649ef022SMatthew Knepley 
18645f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&u));
18655f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&user.dmCell));
18665f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm));
18675f80ce2aSJacob Faibussowitsch   CHKERRQ(TSDestroy(&ts));
18685f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscBagDestroy(&user.bag));
1869*b122ec5aSJacob Faibussowitsch   CHKERRQ(PetscFinalize());
1870*b122ec5aSJacob Faibussowitsch   return 0;
1871649ef022SMatthew Knepley }
1872649ef022SMatthew Knepley 
1873649ef022SMatthew Knepley /*TEST
1874649ef022SMatthew Knepley 
1875444129b9SMatthew G. Knepley   testset:
1876649ef022SMatthew Knepley     requires: triangle !single
1877444129b9SMatthew G. Knepley     args: -dm_plex_separate_marker \
1878a712f3bbSMatthew G. Knepley           -div_petscdualspace_lagrange_use_moments -div_petscdualspace_lagrange_moment_order 3 \
1879444129b9SMatthew G. Knepley           -snes_error_if_not_converged -snes_convergence_test correct_pressure \
1880649ef022SMatthew Knepley           -ksp_type fgmres -ksp_gmres_restart 10 -ksp_rtol 1.0e-9 -ksp_error_if_not_converged \
1881444129b9SMatthew G. Knepley           -pc_type fieldsplit -pc_fieldsplit_0_fields 0,2 -pc_fieldsplit_1_fields 1 \
1882444129b9SMatthew G. Knepley           -pc_fieldsplit_type schur -pc_fieldsplit_schur_factorization_type full \
1883649ef022SMatthew Knepley             -fieldsplit_0_pc_type lu \
1884649ef022SMatthew Knepley             -fieldsplit_pressure_ksp_rtol 1e-10 -fieldsplit_pressure_pc_type jacobi
1885649ef022SMatthew Knepley 
1886444129b9SMatthew G. Knepley     test:
1887444129b9SMatthew G. Knepley       suffix: 2d_tri_p2_p1_p1
1888444129b9SMatthew G. Knepley       args: -sol_type quadratic \
1889444129b9SMatthew G. Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \
1890444129b9SMatthew G. Knepley             -dmts_check .001 -ts_max_steps 4 -ts_dt 0.1
1891444129b9SMatthew G. Knepley 
1892649ef022SMatthew Knepley     test:
1893649ef022SMatthew Knepley       # Using -dm_refine 5 -convest_num_refine 2 gives L_2 convergence rate: [0.89, 0.011, 1.0]
1894649ef022SMatthew Knepley       suffix: 2d_tri_p2_p1_p1_tconv
1895444129b9SMatthew G. Knepley       args: -sol_type cubic_trig \
1896649ef022SMatthew Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \
1897444129b9SMatthew G. Knepley             -ts_max_steps 4 -ts_dt 0.1 -ts_convergence_estimate -convest_num_refine 1
1898649ef022SMatthew Knepley 
1899649ef022SMatthew Knepley     test:
1900649ef022SMatthew Knepley       # Using -dm_refine 3 -convest_num_refine 3 gives L_2 convergence rate: [3.0, 2.5, 1.9]
1901649ef022SMatthew Knepley       suffix: 2d_tri_p2_p1_p1_sconv
1902444129b9SMatthew G. Knepley       args: -sol_type cubic \
1903649ef022SMatthew Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \
1904444129b9SMatthew G. Knepley             -ts_max_steps 1 -ts_dt 1e-4 -ts_convergence_estimate -ts_convergence_temporal 0 -convest_num_refine 1
1905649ef022SMatthew Knepley 
1906649ef022SMatthew Knepley     test:
1907649ef022SMatthew Knepley       suffix: 2d_tri_p3_p2_p2
1908444129b9SMatthew G. Knepley       args: -sol_type cubic \
1909649ef022SMatthew Knepley             -vel_petscspace_degree 3 -pres_petscspace_degree 2 -temp_petscspace_degree 2 \
1910444129b9SMatthew G. Knepley             -dmts_check .001 -ts_max_steps 4 -ts_dt 0.1
1911649ef022SMatthew Knepley 
1912606d57d4SMatthew G. Knepley     test:
1913606d57d4SMatthew G. Knepley       # Using -dm_refine 3 -convest_num_refine 3 gives L_2 convergence rate: [3.0, 2.1, 3.1]
1914606d57d4SMatthew G. Knepley       suffix: 2d_tri_p2_p1_p1_tg_sconv
1915444129b9SMatthew G. Knepley       args: -sol_type taylor_green \
1916606d57d4SMatthew G. Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \
1917444129b9SMatthew G. Knepley             -ts_max_steps 1 -ts_dt 1e-8 -ts_convergence_estimate -ts_convergence_temporal 0 -convest_num_refine 1
1918606d57d4SMatthew G. Knepley 
1919606d57d4SMatthew G. Knepley     test:
1920606d57d4SMatthew G. Knepley       # Using -dm_refine 3 -convest_num_refine 2 gives L_2 convergence rate: [1.2, 1.5, 1.2]
1921606d57d4SMatthew G. Knepley       suffix: 2d_tri_p2_p1_p1_tg_tconv
1922444129b9SMatthew G. Knepley       args: -sol_type taylor_green \
1923606d57d4SMatthew G. Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \
1924444129b9SMatthew G. Knepley             -ts_max_steps 4 -ts_dt 0.1 -ts_convergence_estimate -convest_num_refine 1
1925444129b9SMatthew G. Knepley 
1926444129b9SMatthew G. Knepley   testset:
1927444129b9SMatthew G. Knepley     requires: triangle !single
1928444129b9SMatthew G. Knepley     args: -dm_plex_separate_marker -mod_type conducting \
1929a712f3bbSMatthew G. Knepley           -div_petscdualspace_lagrange_use_moments -div_petscdualspace_lagrange_moment_order 3 \
1930444129b9SMatthew G. Knepley           -snes_error_if_not_converged -snes_max_linear_solve_fail 5 \
193182894d03SBarry Smith           -ksp_type fgmres -ksp_max_it 2 -ksp_gmres_restart 10 -ksp_rtol 1.0e-9 \
1932444129b9SMatthew G. Knepley           -pc_type fieldsplit -pc_fieldsplit_0_fields 0,2 -pc_fieldsplit_1_fields 1 \
1933444129b9SMatthew G. Knepley           -pc_fieldsplit_type schur -pc_fieldsplit_schur_factorization_type full \
1934606d57d4SMatthew G. Knepley             -fieldsplit_0_pc_type lu \
1935606d57d4SMatthew G. Knepley             -fieldsplit_pressure_ksp_rtol 1e-10 -fieldsplit_pressure_pc_type jacobi
1936606d57d4SMatthew G. Knepley 
1937444129b9SMatthew G. Knepley     test:
1938444129b9SMatthew G. Knepley       # At this resolution, the rhs is inconsistent on some Newton steps
1939444129b9SMatthew G. Knepley       suffix: 2d_tri_p2_p1_p1_conduct
1940444129b9SMatthew G. Knepley       args: -sol_type quadratic \
1941444129b9SMatthew G. Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 1 \
1942444129b9SMatthew G. Knepley             -dmts_check .001 -ts_max_steps 4 -ts_dt 0.1 \
1943444129b9SMatthew G. Knepley             -pc_fieldsplit_schur_precondition full \
1944444129b9SMatthew G. Knepley               -fieldsplit_pressure_ksp_max_it 2 -fieldsplit_pressure_pc_type svd
1945444129b9SMatthew G. Knepley 
1946444129b9SMatthew G. Knepley     test:
1947444129b9SMatthew G. Knepley       suffix: 2d_tri_p2_p1_p2_conduct_pipe
1948444129b9SMatthew G. Knepley       args: -sol_type pipe \
1949444129b9SMatthew G. Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 2 \
1950444129b9SMatthew G. Knepley             -dmts_check .001 -ts_max_steps 4 -ts_dt 0.1
1951444129b9SMatthew G. Knepley 
1952367970cfSMatthew G. Knepley     test:
1953367970cfSMatthew G. Knepley       suffix: 2d_tri_p2_p1_p2_conduct_pipe_wiggly_sconv
1954367970cfSMatthew G. Knepley       args: -sol_type pipe_wiggly -Fr 1e10 \
1955367970cfSMatthew G. Knepley             -vel_petscspace_degree 2 -pres_petscspace_degree 1 -temp_petscspace_degree 2 \
1956367970cfSMatthew G. Knepley             -ts_convergence_estimate -ts_convergence_temporal 0 -convest_num_refine 1 \
1957367970cfSMatthew G. Knepley             -ts_max_steps 1 -ts_dt 1e10 \
1958367970cfSMatthew G. Knepley             -ksp_atol 1e-12 -ksp_max_it 300 \
1959367970cfSMatthew G. Knepley               -fieldsplit_pressure_ksp_atol 1e-14
1960367970cfSMatthew G. Knepley 
1961649ef022SMatthew Knepley TEST*/
1962