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