xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision 3b0d37b7b48e6b9e226afa94eeb793b3e14bfbcc)
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 /// Structs and helper functions regarding the state of a newtonian simulation
10 
11 
12 #ifndef newtonian_state_h
13 #define newtonian_state_h
14 
15 #include <math.h>
16 #include <ceed.h>
17 #include "newtonian_types.h"
18 #include "utils.h"
19 
20 typedef struct {
21   CeedScalar pressure;
22   CeedScalar velocity[3];
23   CeedScalar temperature;
24 } StatePrimitive;
25 
26 typedef struct {
27   CeedScalar density;
28   CeedScalar momentum[3];
29   CeedScalar E_total;
30 } StateConservative;
31 
32 typedef struct {
33   StateConservative U;
34   StatePrimitive Y;
35 } State;
36 
37 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
38   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
39   StatePrimitive Y;
40   for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
41   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
42   CeedScalar e_potential = -Dot3(gas->g, x);
43   CeedScalar e_total = U.E_total / U.density;
44   CeedScalar e_internal = e_total - e_kinetic - e_potential;
45   Y.temperature = e_internal / gas->cv;
46   Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal;
47   return Y;
48 }
49 
50 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
51   NewtonianIdealGasContext gas, State s, StateConservative dU,
52   const CeedScalar x[3], const CeedScalar dx[3]) {
53   StatePrimitive dY;
54   for (CeedInt i=0; i<3; i++) {
55     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
56   }
57   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
58   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
59   CeedScalar e_potential = -Dot3(gas->g, x);
60   CeedScalar de_potential = -Dot3(gas->g, dx);
61   CeedScalar e_total = s.U.E_total / s.U.density;
62   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
63   CeedScalar e_internal = e_total - e_kinetic - e_potential;
64   CeedScalar de_internal = de_total - de_kinetic - de_potential;
65   dY.temperature = de_internal / gas->cv;
66   dY.pressure = (gas->cp / gas->cv - 1)
67                 * (dU.density * e_internal + s.U.density * de_internal);
68   return dY;
69 }
70 
71 CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
72                                        const CeedScalar U[5], const CeedScalar x[3]) {
73   State s;
74   s.U.density = U[0];
75   s.U.momentum[0] = U[1];
76   s.U.momentum[1] = U[2];
77   s.U.momentum[2] = U[3];
78   s.U.E_total = U[4];
79   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
80   return s;
81 }
82 
83 CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
84     State s, const CeedScalar dU[5],
85     const CeedScalar x[3], const CeedScalar dx[3]) {
86   State ds;
87   ds.U.density = dU[0];
88   ds.U.momentum[0] = dU[1];
89   ds.U.momentum[1] = dU[2];
90   ds.U.momentum[2] = dU[3];
91   ds.U.E_total = dU[4];
92   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
93   return ds;
94 }
95 
96 CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
97                                         StateConservative Flux[3]) {
98   for (CeedInt i=0; i<3; i++) {
99     Flux[i].density = s.U.momentum[i];
100     for (CeedInt j=0; j<3; j++)
101       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
102                             + s.Y.pressure * (i == j);
103     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
104   }
105 }
106 
107 CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
108     State s, State ds, StateConservative dFlux[3]) {
109   for (CeedInt i=0; i<3; i++) {
110     dFlux[i].density = ds.U.momentum[i];
111     for (CeedInt j=0; j<3; j++)
112       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
113                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
114     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
115                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
116   }
117 }
118 
119 // Kelvin-Mandel notation
120 CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
121                                         CeedScalar strain_rate[6]) {
122   const CeedScalar weight = 1 / sqrt(2.);
123   strain_rate[0] = grad_s[0].Y.velocity[0];
124   strain_rate[1] = grad_s[1].Y.velocity[1];
125   strain_rate[2] = grad_s[2].Y.velocity[2];
126   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
127   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
128   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
129 }
130 
131 CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
132     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
133   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
134   for (CeedInt i=0; i<6; i++) {
135     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
136   }
137 }
138 
139 CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
140     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
141     CeedScalar Fe[3]) {
142   for (CeedInt i=0; i<3; i++) {
143     Fe[i] = - Y.velocity[0] * stress[0][i]
144             - Y.velocity[1] * stress[1][i]
145             - Y.velocity[2] * stress[2][i]
146             - gas->k * grad_s[i].Y.temperature;
147   }
148 }
149 
150 CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
151     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
152     const CeedScalar stress[3][3],
153     const CeedScalar dstress[3][3],
154     CeedScalar dFe[3]) {
155   for (CeedInt i=0; i<3; i++) {
156     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
157              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
158              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
159              - gas->k * grad_ds[i].Y.temperature;
160   }
161 }
162 
163 #endif // newtonian_state_h
164