xref: /libCEED/examples/fluids/qfunctions/densitycurrent.h (revision 88626eed6564cd43033d3137230605fb5f962840)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Density current initial condition and operator for Navier-Stokes example using PETSc
10 
11 // Model from:
12 //   Semi-Implicit Formulations of the Navier-Stokes Equations: Application to
13 //   Nonhydrostatic Atmospheric Modeling, Giraldo, Restelli, and Lauter (2010).
14 
15 #ifndef densitycurrent_h
16 #define densitycurrent_h
17 
18 #include <math.h>
19 #include <ceed.h>
20 
21 #ifndef M_PI
22 #define M_PI    3.14159265358979323846
23 #endif
24 
25 #ifndef setup_context_struct
26 #define setup_context_struct
27 typedef struct SetupContext_ *SetupContext;
28 struct SetupContext_ {
29   CeedScalar theta0;
30   CeedScalar thetaC;
31   CeedScalar P0;
32   CeedScalar N;
33   CeedScalar cv;
34   CeedScalar cp;
35   CeedScalar g[3];
36   CeedScalar rc;
37   CeedScalar lx;
38   CeedScalar ly;
39   CeedScalar lz;
40   CeedScalar center[3];
41   CeedScalar dc_axis[3];
42   CeedScalar wind[3];
43   CeedScalar time;
44   int wind_type;              // See WindType: 0=ROTATION, 1=TRANSLATION
45   int bubble_type;            // See BubbleType: 0=SPHERE, 1=CYLINDER
46   int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
47 };
48 #endif
49 
50 // *****************************************************************************
51 // This function sets the initial conditions and the boundary conditions
52 //
53 // These initial conditions are given in terms of potential temperature and
54 //   Exner pressure and then converted to density and total energy.
55 //   Initial momentum density is zero.
56 //
57 // Initial Conditions:
58 //   Potential Temperature:
59 //     theta = thetabar + delta_theta
60 //       thetabar   = theta0 exp( N**2 z / g )
61 //       delta_theta = r <= rc : thetaC(1 + cos(pi r/rc)) / 2
62 //                     r > rc : 0
63 //         r        = sqrt( (x - xc)**2 + (y - yc)**2 + (z - zc)**2 )
64 //         with (xc,yc,zc) center of domain, rc characteristic radius of thermal bubble
65 //   Exner Pressure:
66 //     Pi = Pibar + deltaPi
67 //       Pibar      = 1. + g**2 (exp( - N**2 z / g ) - 1) / (cp theta0 N**2)
68 //       deltaPi    = 0 (hydrostatic balance)
69 //   Velocity/Momentum Density:
70 //     Ui = ui = 0
71 //
72 // Conversion to Conserved Variables:
73 //   rho = P0 Pi**(cv/Rd) / (Rd theta)
74 //   E   = rho (cv T + (u u)/2 + g z)
75 //
76 //  Boundary Conditions:
77 //    Mass Density:
78 //      0.0 flux
79 //    Momentum Density:
80 //      0.0
81 //    Energy Density:
82 //      0.0 flux
83 //
84 // Constants:
85 //   theta0          ,  Potential temperature constant
86 //   thetaC          ,  Potential temperature perturbation
87 //   P0              ,  Pressure at the surface
88 //   N               ,  Brunt-Vaisala frequency
89 //   cv              ,  Specific heat, constant volume
90 //   cp              ,  Specific heat, constant pressure
91 //   Rd     = cp - cv,  Specific heat difference
92 //   g               ,  Gravity
93 //   rc              ,  Characteristic radius of thermal bubble
94 //   center          ,  Location of bubble center
95 //   dc_axis         ,  Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric
96 // *****************************************************************************
97 
98 // *****************************************************************************
99 // This helper function provides support for the exact, time-dependent solution
100 //   (currently not implemented) and IC formulation for density current
101 // *****************************************************************************
102 CEED_QFUNCTION_HELPER int Exact_DC(CeedInt dim, CeedScalar time,
103                                    const CeedScalar X[], CeedInt Nf, CeedScalar q[],
104                                    void *ctx) {
105   // Context
106   const SetupContext context = (SetupContext)ctx;
107   const CeedScalar theta0   = context->theta0;
108   const CeedScalar thetaC   = context->thetaC;
109   const CeedScalar P0       = context->P0;
110   const CeedScalar N        = context->N;
111   const CeedScalar cv       = context->cv;
112   const CeedScalar cp       = context->cp;
113   const CeedScalar *g_vec   = context->g;
114   const CeedScalar rc       = context->rc;
115   const CeedScalar *center  = context->center;
116   const CeedScalar *dc_axis = context->dc_axis;
117   const CeedScalar Rd       = cp - cv;
118   const CeedScalar g = -g_vec[2];
119 
120   // Setup
121   // -- Coordinates
122   const CeedScalar x = X[0];
123   const CeedScalar y = X[1];
124   const CeedScalar z = X[2];
125 
126   // -- Potential temperature, density current
127   CeedScalar rr[3] = {x - center[0], y - center[1], z - center[2]};
128   // (I - q q^T) r: distance from dc_axis (or from center if dc_axis is the zero vector)
129   for (CeedInt i=0; i<3; i++)
130     rr[i] -= dc_axis[i] *
131              (dc_axis[0]*rr[0] + dc_axis[1]*rr[1] + dc_axis[2]*rr[2]);
132   const CeedScalar r = sqrt(rr[0]*rr[0] + rr[1]*rr[1] + rr[2]*rr[2]);
133   const CeedScalar delta_theta = r <= rc ? thetaC*(1. + cos(M_PI*r/rc))/2. : 0.;
134   const CeedScalar theta = theta0*exp(N*N*z/g) + delta_theta;
135 
136   // -- Exner pressure, hydrostatic balance
137   const CeedScalar Pi = 1. + g*g*(exp(-N*N*z/g) - 1.) / (cp*theta0*N*N);
138   // -- Density
139 
140   const CeedScalar rho = P0 * pow(Pi, cv/Rd) / (Rd*theta);
141 
142   // Initial Conditions
143   q[0] = rho;
144   q[1] = 0.0;
145   q[2] = 0.0;
146   q[3] = 0.0;
147   q[4] = rho * (cv*theta*Pi + g*z);
148 
149   return 0;
150 }
151 
152 // *****************************************************************************
153 // This QFunction sets the initial conditions for density current
154 // *****************************************************************************
155 CEED_QFUNCTION(ICsDC)(void *ctx, CeedInt Q,
156                       const CeedScalar *const *in, CeedScalar *const *out) {
157   // Inputs
158   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
159 
160   // Outputs
161   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
162 
163   CeedPragmaSIMD
164   // Quadrature Point Loop
165   for (CeedInt i=0; i<Q; i++) {
166     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
167     CeedScalar q[5] = {0.};
168 
169     Exact_DC(3, 0., x, 5, q, ctx);
170 
171     for (CeedInt j=0; j<5; j++)
172       q0[j][i] = q[j];
173   } // End of Quadrature Point Loop
174 
175   return 0;
176 }
177 
178 #endif // densitycurrent_h
179