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