xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision 5dfaedb85d2aa5da89951bb5d8f41d61be09bbf6)
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 <ceed.h>
16 #include <math.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 void UnpackState_U(StateConservative s, CeedScalar U[5]) {
38   U[0] = s.density;
39   for (int i=0; i<3; i++) U[i+1] = s.momentum[i];
40   U[4] = s.E_total;
41 }
42 
43 CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
44   Y[0] = s.pressure;
45   for (int i=0; i<3; i++) Y[i+1] = s.velocity[i];
46   Y[4] = s.temperature;
47 }
48 
49 CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(
50   NewtonianIdealGasContext gas) {
51   return gas->cp / gas->cv;
52 }
53 
54 CEED_QFUNCTION_HELPER CeedScalar GasConstant(
55   NewtonianIdealGasContext gas) {
56   return gas->cp - gas->cv;
57 }
58 
59 CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) {
60   return gas->cp * gas->mu / gas->k;
61 }
62 
63 CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas,
64     CeedScalar T) {
65   return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T);
66 }
67 
68 CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas,
69                                       CeedScalar T, CeedScalar u) {
70   return u / SoundSpeed(gas, T);
71 }
72 
73 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
74   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
75   StatePrimitive Y;
76   for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
77   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
78   CeedScalar e_potential = -Dot3(gas->g, x);
79   CeedScalar e_total = U.E_total / U.density;
80   CeedScalar e_internal = e_total - e_kinetic - e_potential;
81   Y.temperature = e_internal / gas->cv;
82   Y.pressure = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
83   return Y;
84 }
85 
86 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
87   NewtonianIdealGasContext gas, State s, StateConservative dU,
88   const CeedScalar x[3], const CeedScalar dx[3]) {
89   StatePrimitive dY;
90   for (CeedInt i=0; i<3; i++) {
91     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
92   }
93   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
94   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
95   CeedScalar e_potential = -Dot3(gas->g, x);
96   CeedScalar de_potential = -Dot3(gas->g, dx);
97   CeedScalar e_total = s.U.E_total / s.U.density;
98   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
99   CeedScalar e_internal = e_total - e_kinetic - e_potential;
100   CeedScalar de_internal = de_total - de_kinetic - de_potential;
101   dY.temperature = de_internal / gas->cv;
102   dY.pressure = (HeatCapacityRatio(gas) - 1)
103                 * (dU.density * e_internal + s.U.density * de_internal);
104   return dY;
105 }
106 
107 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(
108   NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) {
109   StateConservative U;
110   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
111   for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i];
112   CeedScalar e_internal = gas->cv * Y.temperature;
113   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
114   CeedScalar e_potential = -Dot3(gas->g, x);
115   CeedScalar e_total = e_internal + e_kinetic + e_potential;
116   U.E_total = U.density*e_total;
117   return U;
118 }
119 
120 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(
121   NewtonianIdealGasContext gas, State s, StatePrimitive dY,
122   const CeedScalar x[3], const CeedScalar dx[3]) {
123   StateConservative dU;
124   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) /
125                (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
126   for (int i=0; i<3; i++) {
127     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
128   }
129   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
130   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
131   CeedScalar e_potential = -Dot3(gas->g, x);
132   CeedScalar de_potential = -Dot3(gas->g, dx);
133   CeedScalar e_internal = gas->cv * s.Y.temperature;
134   CeedScalar de_internal = gas->cv * dY.temperature;
135   CeedScalar e_total = e_internal + e_kinetic + e_potential;
136   CeedScalar de_total = de_internal + de_kinetic + de_potential;
137   dU.E_total = dU.density*e_total + s.U.density*de_total;
138   return dU;
139 }
140 
141 // Function pointer types for generic state array -> State struct functions
142 typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas,
143                                const CeedScalar qi[5], const CeedScalar x[3]);
144 typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas,
145                                    State s, const CeedScalar dqi[5],
146                                    const CeedScalar x[3], const CeedScalar dx[3]);
147 
148 CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
149                                        const CeedScalar U[5], const CeedScalar x[3]) {
150   State s;
151   s.U.density = U[0];
152   s.U.momentum[0] = U[1];
153   s.U.momentum[1] = U[2];
154   s.U.momentum[2] = U[3];
155   s.U.E_total = U[4];
156   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
157   return s;
158 }
159 
160 CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
161     State s, const CeedScalar dU[5],
162     const CeedScalar x[3], const CeedScalar dx[3]) {
163   State ds;
164   ds.U.density = dU[0];
165   ds.U.momentum[0] = dU[1];
166   ds.U.momentum[1] = dU[2];
167   ds.U.momentum[2] = dU[3];
168   ds.U.E_total = dU[4];
169   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
170   return ds;
171 }
172 
173 CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas,
174                                        const CeedScalar Y[5], const CeedScalar x[3]) {
175   State s;
176   s.Y.pressure    = Y[0];
177   s.Y.velocity[0] = Y[1];
178   s.Y.velocity[1] = Y[2];
179   s.Y.velocity[2] = Y[3];
180   s.Y.temperature = Y[4];
181   s.U = StateConservativeFromPrimitive(gas, s.Y, x);
182   return s;
183 }
184 
185 CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas,
186     State s, const CeedScalar dY[5],
187     const CeedScalar x[3], const CeedScalar dx[3]) {
188   State ds;
189   ds.Y.pressure    = dY[0];
190   ds.Y.velocity[0] = dY[1];
191   ds.Y.velocity[1] = dY[2];
192   ds.Y.velocity[2] = dY[3];
193   ds.Y.temperature = dY[4];
194   ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx);
195   return ds;
196 }
197 
198 CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
199                                         StateConservative Flux[3]) {
200   for (CeedInt i=0; i<3; i++) {
201     Flux[i].density = s.U.momentum[i];
202     for (CeedInt j=0; j<3; j++)
203       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
204                             + s.Y.pressure * (i == j);
205     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
206   }
207 }
208 
209 CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
210     State s, State ds, StateConservative dFlux[3]) {
211   for (CeedInt i=0; i<3; i++) {
212     dFlux[i].density = ds.U.momentum[i];
213     for (CeedInt j=0; j<3; j++)
214       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
215                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
216     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
217                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
218   }
219 }
220 
221 CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas,
222     State s, State ds[3], CeedScalar strong_conv[5]) {
223   for (CeedInt i=0; i<5; i++) strong_conv[i] = 0;
224   for (CeedInt i=0; i<3; i++) {
225     StateConservative dF[3];
226     FluxInviscid_fwd(gas, s, ds[i], dF);
227     CeedScalar dF_i[5];
228     UnpackState_U(dF[i], dF_i);
229     for (CeedInt j=0; j<5; j++)
230       strong_conv[j] += dF_i[j];
231   }
232 }
233 
234 CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3],
235                                      CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
236   for (CeedInt j=0; j<3; j++) {
237     Flux[0][j] = F_inviscid[j].density;
238     for (CeedInt k=0; k<3; k++)
239       Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
240     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
241   }
242 }
243 
244 CEED_QFUNCTION_HELPER void FluxTotal_Boundary(
245   const StateConservative F_inviscid[3], const CeedScalar stress[3][3],
246   const CeedScalar Fe[3], const CeedScalar normal[3], CeedScalar Flux[5]) {
247 
248   for(CeedInt j=0; j<5; j++) Flux[j] = 0.;
249   for (CeedInt j=0; j<3; j++) {
250     Flux[0] += F_inviscid[j].density * normal[j];
251     for (CeedInt k=0; k<3; k++) {
252       Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
253     }
254     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
255   }
256 }
257 
258 // Kelvin-Mandel notation
259 CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
260                                         CeedScalar strain_rate[6]) {
261   const CeedScalar weight = 1 / sqrt(2.);
262   strain_rate[0] = grad_s[0].Y.velocity[0];
263   strain_rate[1] = grad_s[1].Y.velocity[1];
264   strain_rate[2] = grad_s[2].Y.velocity[2];
265   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
266   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
267   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
268 }
269 
270 CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
271     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
272   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
273   for (CeedInt i=0; i<6; i++) {
274     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
275   }
276 }
277 
278 CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
279     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
280     CeedScalar Fe[3]) {
281   for (CeedInt i=0; i<3; i++) {
282     Fe[i] = - Y.velocity[0] * stress[0][i]
283             - Y.velocity[1] * stress[1][i]
284             - Y.velocity[2] * stress[2][i]
285             - gas->k * grad_s[i].Y.temperature;
286   }
287 }
288 
289 CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
290     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
291     const CeedScalar stress[3][3],
292     const CeedScalar dstress[3][3],
293     CeedScalar dFe[3]) {
294   for (CeedInt i=0; i<3; i++) {
295     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
296              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
297              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
298              - gas->k * grad_ds[i].Y.temperature;
299   }
300 }
301 
302 #endif // newtonian_state_h
303