xref: /libCEED/examples/fluids/qfunctions/densitycurrent.h (revision 78a9fcb6bae7d7246469592ed2b1811dbe8e744f)
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;
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        = 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 
119   // Setup
120   // -- Coordinates
121   const CeedScalar x = X[0];
122   const CeedScalar y = X[1];
123   const CeedScalar z = X[2];
124 
125   // -- Potential temperature, density current
126   CeedScalar rr[3] = {x - center[0], y - center[1], z - center[2]};
127   // (I - q q^T) r: distance from dc_axis (or from center if dc_axis is the zero vector)
128   for (CeedInt i=0; i<3; i++)
129     rr[i] -= dc_axis[i] *
130              (dc_axis[0]*rr[0] + dc_axis[1]*rr[1] + dc_axis[2]*rr[2]);
131   const CeedScalar r = sqrt(rr[0]*rr[0] + rr[1]*rr[1] + rr[2]*rr[2]);
132   const CeedScalar delta_theta = r <= rc ? thetaC*(1. + cos(M_PI*r/rc))/2. : 0.;
133   const CeedScalar theta = theta0*exp(N*N*z/g) + delta_theta;
134 
135   // -- Exner pressure, hydrostatic balance
136   const CeedScalar Pi = 1. + g*g*(exp(-N*N*z/g) - 1.) / (cp*theta0*N*N);
137   // -- Density
138 
139   const CeedScalar rho = P0 * pow(Pi, cv/Rd) / (Rd*theta);
140 
141   // Initial Conditions
142   q[0] = rho;
143   q[1] = 0.0;
144   q[2] = 0.0;
145   q[3] = 0.0;
146   q[4] = rho * (cv*theta*Pi + g*z);
147 
148   return 0;
149 }
150 
151 // *****************************************************************************
152 // This QFunction sets the initial conditions for density current
153 // *****************************************************************************
154 CEED_QFUNCTION(ICsDC)(void *ctx, CeedInt Q,
155                       const CeedScalar *const *in, CeedScalar *const *out) {
156   // Inputs
157   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
158 
159   // Outputs
160   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
161 
162   CeedPragmaSIMD
163   // Quadrature Point Loop
164   for (CeedInt i=0; i<Q; i++) {
165     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
166     CeedScalar q[5] = {0.};
167 
168     Exact_DC(3, 0., x, 5, q, ctx);
169 
170     for (CeedInt j=0; j<5; j++)
171       q0[j][i] = q[j];
172   } // End of Quadrature Point Loop
173 
174   return 0;
175 }
176 
177 #endif // densitycurrent_h
178