xref: /libCEED/examples/fluids/qfunctions/densitycurrent.h (revision dc805cc4a09d29f27b3febd084feb659e74b9d08)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Density current initial condition and operator for Navier-Stokes example using PETSc
1077841947SLeila Ghaffari 
1177841947SLeila Ghaffari // Model from:
1277841947SLeila Ghaffari //   Semi-Implicit Formulations of the Navier-Stokes Equations: Application to
1377841947SLeila Ghaffari //   Nonhydrostatic Atmospheric Modeling, Giraldo, Restelli, and Lauter (2010).
1477841947SLeila Ghaffari 
1577841947SLeila Ghaffari #ifndef densitycurrent_h
1677841947SLeila Ghaffari #define densitycurrent_h
1777841947SLeila Ghaffari 
1877841947SLeila Ghaffari #include <math.h>
1988b783a1SJames Wright #include <ceed.h>
20a0add3c9SJed Brown #include "newtonian_types.h"
21*dc805cc4SLeila Ghaffari #include "newtonian_state.h"
2213fa47b2SJames Wright #include "utils.h"
2377841947SLeila Ghaffari 
24*dc805cc4SLeila Ghaffari typedef struct DensityCurrentContext_ *DensityCurrentContext;
25*dc805cc4SLeila Ghaffari struct DensityCurrentContext_ {
26*dc805cc4SLeila Ghaffari   CeedScalar theta0;
27*dc805cc4SLeila Ghaffari   CeedScalar thetaC;
28*dc805cc4SLeila Ghaffari   CeedScalar P0;
29*dc805cc4SLeila Ghaffari   CeedScalar N;
30*dc805cc4SLeila Ghaffari   CeedScalar rc;
31*dc805cc4SLeila Ghaffari   CeedScalar center[3];
32*dc805cc4SLeila Ghaffari   CeedScalar dc_axis[3];
33*dc805cc4SLeila Ghaffari   struct NewtonianIdealGasContext_ newtonian_ctx;
34*dc805cc4SLeila Ghaffari };
35*dc805cc4SLeila Ghaffari 
3677841947SLeila Ghaffari // *****************************************************************************
3777841947SLeila Ghaffari // This function sets the initial conditions and the boundary conditions
3877841947SLeila Ghaffari //
3977841947SLeila Ghaffari // These initial conditions are given in terms of potential temperature and
4077841947SLeila Ghaffari //   Exner pressure and then converted to density and total energy.
4177841947SLeila Ghaffari //   Initial momentum density is zero.
4277841947SLeila Ghaffari //
4377841947SLeila Ghaffari // Initial Conditions:
4477841947SLeila Ghaffari //   Potential Temperature:
4577841947SLeila Ghaffari //     theta = thetabar + delta_theta
4677841947SLeila Ghaffari //       thetabar   = theta0 exp( N**2 z / g )
4777841947SLeila Ghaffari //       delta_theta = r <= rc : thetaC(1 + cos(pi r/rc)) / 2
4877841947SLeila Ghaffari //                     r > rc : 0
4977841947SLeila Ghaffari //         r        = sqrt( (x - xc)**2 + (y - yc)**2 + (z - zc)**2 )
5077841947SLeila Ghaffari //         with (xc,yc,zc) center of domain, rc characteristic radius of thermal bubble
5177841947SLeila Ghaffari //   Exner Pressure:
5277841947SLeila Ghaffari //     Pi = Pibar + deltaPi
5377841947SLeila Ghaffari //       Pibar      = 1. + g**2 (exp( - N**2 z / g ) - 1) / (cp theta0 N**2)
5477841947SLeila Ghaffari //       deltaPi    = 0 (hydrostatic balance)
5577841947SLeila Ghaffari //   Velocity/Momentum Density:
5677841947SLeila Ghaffari //     Ui = ui = 0
5777841947SLeila Ghaffari //
5877841947SLeila Ghaffari // Conversion to Conserved Variables:
5977841947SLeila Ghaffari //   rho = P0 Pi**(cv/Rd) / (Rd theta)
6077841947SLeila Ghaffari //   E   = rho (cv T + (u u)/2 + g z)
6177841947SLeila Ghaffari //
6277841947SLeila Ghaffari //  Boundary Conditions:
6377841947SLeila Ghaffari //    Mass Density:
6477841947SLeila Ghaffari //      0.0 flux
6577841947SLeila Ghaffari //    Momentum Density:
6677841947SLeila Ghaffari //      0.0
6777841947SLeila Ghaffari //    Energy Density:
6877841947SLeila Ghaffari //      0.0 flux
6977841947SLeila Ghaffari //
7077841947SLeila Ghaffari // Constants:
7177841947SLeila Ghaffari //   theta0          ,  Potential temperature constant
7277841947SLeila Ghaffari //   thetaC          ,  Potential temperature perturbation
7377841947SLeila Ghaffari //   P0              ,  Pressure at the surface
7477841947SLeila Ghaffari //   N               ,  Brunt-Vaisala frequency
7577841947SLeila Ghaffari //   cv              ,  Specific heat, constant volume
7677841947SLeila Ghaffari //   cp              ,  Specific heat, constant pressure
7777841947SLeila Ghaffari //   Rd     = cp - cv,  Specific heat difference
7877841947SLeila Ghaffari //   g               ,  Gravity
7977841947SLeila Ghaffari //   rc              ,  Characteristic radius of thermal bubble
8077841947SLeila Ghaffari //   center          ,  Location of bubble center
8177841947SLeila Ghaffari //   dc_axis         ,  Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric
8277841947SLeila Ghaffari // *****************************************************************************
8377841947SLeila Ghaffari 
8477841947SLeila Ghaffari // *****************************************************************************
8577841947SLeila Ghaffari // This helper function provides support for the exact, time-dependent solution
8677841947SLeila Ghaffari //   (currently not implemented) and IC formulation for density current
8777841947SLeila Ghaffari // *****************************************************************************
88*dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State Exact_DC(CeedInt dim, CeedScalar time,
89*dc805cc4SLeila Ghaffari                                      const CeedScalar X[], CeedInt Nf, void *ctx) {
9077841947SLeila Ghaffari   // Context
91*dc805cc4SLeila Ghaffari   const DensityCurrentContext context = (DensityCurrentContext)ctx;
9277841947SLeila Ghaffari   const CeedScalar theta0      = context->theta0;
9377841947SLeila Ghaffari   const CeedScalar thetaC      = context->thetaC;
9477841947SLeila Ghaffari   const CeedScalar P0          = context->P0;
9577841947SLeila Ghaffari   const CeedScalar N           = context->N;
9677841947SLeila Ghaffari   const CeedScalar rc          = context->rc;
9777841947SLeila Ghaffari   const CeedScalar *center     = context->center;
9877841947SLeila Ghaffari   const CeedScalar *dc_axis    = context->dc_axis;
99*dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas = &context->newtonian_ctx;
100*dc805cc4SLeila Ghaffari   const CeedScalar cp          = gas->cp;
101*dc805cc4SLeila Ghaffari   const CeedScalar cv          = gas->cv;
102e6225c47SLeila Ghaffari   const CeedScalar Rd          = cp - cv;
103*dc805cc4SLeila Ghaffari   const CeedScalar *g_vec      = gas->g;
10488626eedSJames Wright   const CeedScalar g           = -g_vec[2];
10577841947SLeila Ghaffari 
10677841947SLeila Ghaffari   // Setup
10777841947SLeila Ghaffari   // -- Coordinates
10877841947SLeila Ghaffari   const CeedScalar x = X[0];
10977841947SLeila Ghaffari   const CeedScalar y = X[1];
11077841947SLeila Ghaffari   const CeedScalar z = X[2];
11177841947SLeila Ghaffari 
11277841947SLeila Ghaffari   // -- Potential temperature, density current
11377841947SLeila Ghaffari   CeedScalar rr[3] = {x - center[0], y - center[1], z - center[2]};
11477841947SLeila Ghaffari   // (I - q q^T) r: distance from dc_axis (or from center if dc_axis is the zero vector)
11577841947SLeila Ghaffari   for (CeedInt i=0; i<3; i++)
11677841947SLeila Ghaffari     rr[i] -= dc_axis[i] *
11777841947SLeila Ghaffari              (dc_axis[0]*rr[0] + dc_axis[1]*rr[1] + dc_axis[2]*rr[2]);
11877841947SLeila Ghaffari   const CeedScalar r = sqrt(rr[0]*rr[0] + rr[1]*rr[1] + rr[2]*rr[2]);
11977841947SLeila Ghaffari   const CeedScalar delta_theta = r <= rc ? thetaC*(1. + cos(M_PI*r/rc))/2. : 0.;
12077841947SLeila Ghaffari   const CeedScalar theta = theta0*exp(N*N*z/g) + delta_theta;
12177841947SLeila Ghaffari 
12277841947SLeila Ghaffari   // -- Exner pressure, hydrostatic balance
12377841947SLeila Ghaffari   const CeedScalar Pi = 1. + g*g*(exp(-N*N*z/g) - 1.) / (cp*theta0*N*N);
12477841947SLeila Ghaffari 
12577841947SLeila Ghaffari   // Initial Conditions
126*dc805cc4SLeila Ghaffari   CeedScalar Y[5] = {0.};
127*dc805cc4SLeila Ghaffari   Y[0] = P0 * pow(Pi, cp/Rd);
128*dc805cc4SLeila Ghaffari   Y[1] = 0.0;
129*dc805cc4SLeila Ghaffari   Y[2] = 0.0;
130*dc805cc4SLeila Ghaffari   Y[3] = 0.0;
131*dc805cc4SLeila Ghaffari   Y[4] = Pi * theta;
13277841947SLeila Ghaffari 
133*dc805cc4SLeila Ghaffari   return StateFromY(gas, Y, X);
13477841947SLeila Ghaffari }
13577841947SLeila Ghaffari 
13677841947SLeila Ghaffari // *****************************************************************************
13777841947SLeila Ghaffari // This QFunction sets the initial conditions for density current
13877841947SLeila Ghaffari // *****************************************************************************
13977841947SLeila Ghaffari CEED_QFUNCTION(ICsDC)(void *ctx, CeedInt Q,
14077841947SLeila Ghaffari                       const CeedScalar *const *in, CeedScalar *const *out) {
14177841947SLeila Ghaffari   // Inputs
14277841947SLeila Ghaffari   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
14377841947SLeila Ghaffari 
14477841947SLeila Ghaffari   // Outputs
14577841947SLeila Ghaffari   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
14677841947SLeila Ghaffari 
147*dc805cc4SLeila Ghaffari   // Context
148*dc805cc4SLeila Ghaffari   const DensityCurrentContext context = (DensityCurrentContext)ctx;
149*dc805cc4SLeila Ghaffari 
15077841947SLeila Ghaffari   CeedPragmaSIMD
15177841947SLeila Ghaffari   // Quadrature Point Loop
15277841947SLeila Ghaffari   for (CeedInt i=0; i<Q; i++) {
15377841947SLeila Ghaffari     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
154*dc805cc4SLeila Ghaffari     State s = Exact_DC(3, 0., x, 5, ctx);
155*dc805cc4SLeila Ghaffari     if (context->newtonian_ctx.primitive) {
156*dc805cc4SLeila Ghaffari       q0[0][i] = s.Y.pressure;
157*dc805cc4SLeila Ghaffari       for (CeedInt j=0; j<3; j++)
158*dc805cc4SLeila Ghaffari         q0[j+1][i] = s.Y.velocity[j];
159*dc805cc4SLeila Ghaffari       q0[4][i] = s.Y.temperature;
160*dc805cc4SLeila Ghaffari     } else {
161*dc805cc4SLeila Ghaffari       q0[0][i] = s.U.density;
162*dc805cc4SLeila Ghaffari       for (CeedInt j=0; j<3; j++)
163*dc805cc4SLeila Ghaffari         q0[j+1][i] = s.U.momentum[j];
164*dc805cc4SLeila Ghaffari       q0[4][i] = s.U.E_total;
165*dc805cc4SLeila Ghaffari     }
16677841947SLeila Ghaffari   } // End of Quadrature Point Loop
16777841947SLeila Ghaffari 
16877841947SLeila Ghaffari   return 0;
16977841947SLeila Ghaffari }
17077841947SLeila Ghaffari 
171*dc805cc4SLeila Ghaffari // *****************************************************************************
172*dc805cc4SLeila Ghaffari 
17377841947SLeila Ghaffari #endif // densitycurrent_h
174