xref: /honee/qfunctions/newtonian_state.h (revision e0d1a4dfdb0f42aa761b1f9bd0a13b235fd41f2a)
1475b2820SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2475b2820SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3475b2820SJames Wright //
4475b2820SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5475b2820SJames Wright //
6475b2820SJames Wright // This file is part of CEED:  http://github.com/ceed
7475b2820SJames Wright 
8475b2820SJames Wright /// @file
9475b2820SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation
10475b2820SJames Wright 
11475b2820SJames Wright 
12475b2820SJames Wright #ifndef newtonian_state_h
13475b2820SJames Wright #define newtonian_state_h
14475b2820SJames Wright 
15475b2820SJames Wright #include <ceed.h>
16d0cce58aSJeremy L Thompson #include <math.h>
17475b2820SJames Wright #include "newtonian_types.h"
18704b8bbeSJames Wright #include "utils.h"
19475b2820SJames Wright 
20475b2820SJames Wright typedef struct {
21475b2820SJames Wright   CeedScalar pressure;
22475b2820SJames Wright   CeedScalar velocity[3];
23475b2820SJames Wright   CeedScalar temperature;
24475b2820SJames Wright } StatePrimitive;
25475b2820SJames Wright 
26475b2820SJames Wright typedef struct {
27475b2820SJames Wright   CeedScalar density;
28475b2820SJames Wright   CeedScalar momentum[3];
29475b2820SJames Wright   CeedScalar E_total;
30475b2820SJames Wright } StateConservative;
31475b2820SJames Wright 
32475b2820SJames Wright typedef struct {
33475b2820SJames Wright   StateConservative U;
34475b2820SJames Wright   StatePrimitive Y;
35475b2820SJames Wright } State;
36475b2820SJames Wright 
37d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
38d1b9ef12SLeila Ghaffari   U[0] = s.density;
39d1b9ef12SLeila Ghaffari   for (int i=0; i<3; i++) U[i+1] = s.momentum[i];
40d1b9ef12SLeila Ghaffari   U[4] = s.E_total;
41d1b9ef12SLeila Ghaffari }
42d1b9ef12SLeila Ghaffari 
43d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
44d1b9ef12SLeila Ghaffari   Y[0] = s.pressure;
45d1b9ef12SLeila Ghaffari   for (int i=0; i<3; i++) Y[i+1] = s.velocity[i];
46d1b9ef12SLeila Ghaffari   Y[4] = s.temperature;
47d1b9ef12SLeila Ghaffari }
48d1b9ef12SLeila Ghaffari 
49*e0d1a4dfSLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(
50*e0d1a4dfSLeila Ghaffari   NewtonianIdealGasContext gas) {
51*e0d1a4dfSLeila Ghaffari   return gas->cp / gas->cv;
52*e0d1a4dfSLeila Ghaffari }
53*e0d1a4dfSLeila Ghaffari 
54*e0d1a4dfSLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar GasConstant(
55*e0d1a4dfSLeila Ghaffari   NewtonianIdealGasContext gas) {
56*e0d1a4dfSLeila Ghaffari   return gas->cp - gas->cv;
57*e0d1a4dfSLeila Ghaffari }
58*e0d1a4dfSLeila Ghaffari 
59*e0d1a4dfSLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) {
60*e0d1a4dfSLeila Ghaffari   return gas->cp * gas->mu / gas->k;
61*e0d1a4dfSLeila Ghaffari }
62*e0d1a4dfSLeila Ghaffari 
63*e0d1a4dfSLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas,
64*e0d1a4dfSLeila Ghaffari     CeedScalar T) {
65*e0d1a4dfSLeila Ghaffari   return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T);
66*e0d1a4dfSLeila Ghaffari }
67*e0d1a4dfSLeila Ghaffari 
68*e0d1a4dfSLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas,
69*e0d1a4dfSLeila Ghaffari                                       CeedScalar T, CeedScalar u) {
70*e0d1a4dfSLeila Ghaffari   return u / SoundSpeed(gas, T);
71*e0d1a4dfSLeila Ghaffari }
72*e0d1a4dfSLeila Ghaffari 
73475b2820SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
74475b2820SJames Wright   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
75475b2820SJames Wright   StatePrimitive Y;
76475b2820SJames Wright   for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
77475b2820SJames Wright   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
78475b2820SJames Wright   CeedScalar e_potential = -Dot3(gas->g, x);
79475b2820SJames Wright   CeedScalar e_total = U.E_total / U.density;
80475b2820SJames Wright   CeedScalar e_internal = e_total - e_kinetic - e_potential;
81475b2820SJames Wright   Y.temperature = e_internal / gas->cv;
82*e0d1a4dfSLeila Ghaffari   Y.pressure = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
83475b2820SJames Wright   return Y;
84475b2820SJames Wright }
85475b2820SJames Wright 
86475b2820SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
87475b2820SJames Wright   NewtonianIdealGasContext gas, State s, StateConservative dU,
88475b2820SJames Wright   const CeedScalar x[3], const CeedScalar dx[3]) {
89475b2820SJames Wright   StatePrimitive dY;
90475b2820SJames Wright   for (CeedInt i=0; i<3; i++) {
91475b2820SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
92475b2820SJames Wright   }
93475b2820SJames Wright   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
94475b2820SJames Wright   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
95475b2820SJames Wright   CeedScalar e_potential = -Dot3(gas->g, x);
96475b2820SJames Wright   CeedScalar de_potential = -Dot3(gas->g, dx);
97475b2820SJames Wright   CeedScalar e_total = s.U.E_total / s.U.density;
98475b2820SJames Wright   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
99475b2820SJames Wright   CeedScalar e_internal = e_total - e_kinetic - e_potential;
100475b2820SJames Wright   CeedScalar de_internal = de_total - de_kinetic - de_potential;
101475b2820SJames Wright   dY.temperature = de_internal / gas->cv;
102*e0d1a4dfSLeila Ghaffari   dY.pressure = (HeatCapacityRatio(gas) - 1)
103475b2820SJames Wright                 * (dU.density * e_internal + s.U.density * de_internal);
104475b2820SJames Wright   return dY;
105475b2820SJames Wright }
106475b2820SJames Wright 
107cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(
108cbe60e31SLeila Ghaffari   NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) {
109cbe60e31SLeila Ghaffari   StateConservative U;
110*e0d1a4dfSLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
111cbe60e31SLeila Ghaffari   for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i];
112cbe60e31SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
113cbe60e31SLeila Ghaffari   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
114cbe60e31SLeila Ghaffari   CeedScalar e_potential = -Dot3(gas->g, x);
115cbe60e31SLeila Ghaffari   CeedScalar e_total = e_internal + e_kinetic + e_potential;
116cbe60e31SLeila Ghaffari   U.E_total = U.density*e_total;
117cbe60e31SLeila Ghaffari   return U;
118cbe60e31SLeila Ghaffari }
119cbe60e31SLeila Ghaffari 
120cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(
121cbe60e31SLeila Ghaffari   NewtonianIdealGasContext gas, State s, StatePrimitive dY,
122cbe60e31SLeila Ghaffari   const CeedScalar x[3], const CeedScalar dx[3]) {
123cbe60e31SLeila Ghaffari   StateConservative dU;
124cbe60e31SLeila Ghaffari   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) /
125*e0d1a4dfSLeila Ghaffari                (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
126cbe60e31SLeila Ghaffari   for (int i=0; i<3; i++) {
127cbe60e31SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
128cbe60e31SLeila Ghaffari   }
129cbe60e31SLeila Ghaffari   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
130cbe60e31SLeila Ghaffari   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
131cbe60e31SLeila Ghaffari   CeedScalar e_potential = -Dot3(gas->g, x);
132cbe60e31SLeila Ghaffari   CeedScalar de_potential = -Dot3(gas->g, dx);
133cbe60e31SLeila Ghaffari   CeedScalar e_internal = gas->cv * s.Y.temperature;
134cbe60e31SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
135cbe60e31SLeila Ghaffari   CeedScalar e_total = e_internal + e_kinetic + e_potential;
136cbe60e31SLeila Ghaffari   CeedScalar de_total = de_internal + de_kinetic + de_potential;
137cbe60e31SLeila Ghaffari   dU.E_total = dU.density*e_total + s.U.density*de_total;
138cbe60e31SLeila Ghaffari   return dU;
139cbe60e31SLeila Ghaffari }
140cbe60e31SLeila Ghaffari 
141d4559bbeSJames Wright // Function pointer types for generic state array -> State struct functions
142d4559bbeSJames Wright typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas,
143d4559bbeSJames Wright                                const CeedScalar qi[5], const CeedScalar x[3]);
144d4559bbeSJames Wright typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas,
145d4559bbeSJames Wright                                    State s, const CeedScalar dqi[5],
146d4559bbeSJames Wright                                    const CeedScalar x[3], const CeedScalar dx[3]);
147d4559bbeSJames Wright 
148475b2820SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
149475b2820SJames Wright                                        const CeedScalar U[5], const CeedScalar x[3]) {
150475b2820SJames Wright   State s;
151475b2820SJames Wright   s.U.density = U[0];
152475b2820SJames Wright   s.U.momentum[0] = U[1];
153475b2820SJames Wright   s.U.momentum[1] = U[2];
154475b2820SJames Wright   s.U.momentum[2] = U[3];
155475b2820SJames Wright   s.U.E_total = U[4];
156475b2820SJames Wright   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
157475b2820SJames Wright   return s;
158475b2820SJames Wright }
159475b2820SJames Wright 
160475b2820SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
161475b2820SJames Wright     State s, const CeedScalar dU[5],
162475b2820SJames Wright     const CeedScalar x[3], const CeedScalar dx[3]) {
163475b2820SJames Wright   State ds;
164475b2820SJames Wright   ds.U.density = dU[0];
165475b2820SJames Wright   ds.U.momentum[0] = dU[1];
166475b2820SJames Wright   ds.U.momentum[1] = dU[2];
167475b2820SJames Wright   ds.U.momentum[2] = dU[3];
168475b2820SJames Wright   ds.U.E_total = dU[4];
169475b2820SJames Wright   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
170475b2820SJames Wright   return ds;
171475b2820SJames Wright }
172475b2820SJames Wright 
173cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas,
174cbe60e31SLeila Ghaffari                                        const CeedScalar Y[5], const CeedScalar x[3]) {
175cbe60e31SLeila Ghaffari   State s;
176cbe60e31SLeila Ghaffari   s.Y.pressure    = Y[0];
177cbe60e31SLeila Ghaffari   s.Y.velocity[0] = Y[1];
178cbe60e31SLeila Ghaffari   s.Y.velocity[1] = Y[2];
179cbe60e31SLeila Ghaffari   s.Y.velocity[2] = Y[3];
180cbe60e31SLeila Ghaffari   s.Y.temperature = Y[4];
181cbe60e31SLeila Ghaffari   s.U = StateConservativeFromPrimitive(gas, s.Y, x);
182cbe60e31SLeila Ghaffari   return s;
183cbe60e31SLeila Ghaffari }
184cbe60e31SLeila Ghaffari 
185cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas,
186cbe60e31SLeila Ghaffari     State s, const CeedScalar dY[5],
187cbe60e31SLeila Ghaffari     const CeedScalar x[3], const CeedScalar dx[3]) {
188cbe60e31SLeila Ghaffari   State ds;
189cbe60e31SLeila Ghaffari   ds.Y.pressure    = dY[0];
190cbe60e31SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
191cbe60e31SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
192cbe60e31SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
193cbe60e31SLeila Ghaffari   ds.Y.temperature = dY[4];
194cbe60e31SLeila Ghaffari   ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx);
195cbe60e31SLeila Ghaffari   return ds;
196cbe60e31SLeila Ghaffari }
197cbe60e31SLeila Ghaffari 
198475b2820SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
199475b2820SJames Wright                                         StateConservative Flux[3]) {
200475b2820SJames Wright   for (CeedInt i=0; i<3; i++) {
201475b2820SJames Wright     Flux[i].density = s.U.momentum[i];
202475b2820SJames Wright     for (CeedInt j=0; j<3; j++)
203475b2820SJames Wright       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
204475b2820SJames Wright                             + s.Y.pressure * (i == j);
205475b2820SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
206475b2820SJames Wright   }
207475b2820SJames Wright }
208475b2820SJames Wright 
209475b2820SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
210475b2820SJames Wright     State s, State ds, StateConservative dFlux[3]) {
211475b2820SJames Wright   for (CeedInt i=0; i<3; i++) {
212475b2820SJames Wright     dFlux[i].density = ds.U.momentum[i];
213475b2820SJames Wright     for (CeedInt j=0; j<3; j++)
214475b2820SJames Wright       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
215475b2820SJames Wright                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
216475b2820SJames Wright     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
217475b2820SJames Wright                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
218475b2820SJames Wright   }
219475b2820SJames Wright }
220475b2820SJames Wright 
221d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas,
222d1b9ef12SLeila Ghaffari     State s, State ds[3], CeedScalar strong_conv[5]) {
223d1b9ef12SLeila Ghaffari   for (CeedInt i=0; i<5; i++) strong_conv[i] = 0;
224d1b9ef12SLeila Ghaffari   for (CeedInt i=0; i<3; i++) {
225d1b9ef12SLeila Ghaffari     StateConservative dF[3];
226d1b9ef12SLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
227d1b9ef12SLeila Ghaffari     CeedScalar dF_i[5];
228d1b9ef12SLeila Ghaffari     UnpackState_U(dF[i], dF_i);
229d1b9ef12SLeila Ghaffari     for (CeedInt j=0; j<5; j++)
230d1b9ef12SLeila Ghaffari       strong_conv[j] += dF_i[j];
231d1b9ef12SLeila Ghaffari   }
232d1b9ef12SLeila Ghaffari }
233d1b9ef12SLeila Ghaffari 
234d4559bbeSJames Wright CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3],
235d1b9ef12SLeila Ghaffari                                      CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
236d1b9ef12SLeila Ghaffari   for (CeedInt j=0; j<3; j++) {
237d1b9ef12SLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
238d1b9ef12SLeila Ghaffari     for (CeedInt k=0; k<3; k++)
239d1b9ef12SLeila Ghaffari       Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
240d1b9ef12SLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
241d1b9ef12SLeila Ghaffari   }
242d1b9ef12SLeila Ghaffari }
243d1b9ef12SLeila Ghaffari 
244c5740391SJames Wright CEED_QFUNCTION_HELPER void FluxTotal_Boundary(
245c5740391SJames Wright   const StateConservative F_inviscid[3], const CeedScalar stress[3][3],
246c5740391SJames Wright   const CeedScalar Fe[3], const CeedScalar normal[3], CeedScalar Flux[5]) {
247c5740391SJames Wright 
248c5740391SJames Wright   for(CeedInt j=0; j<5; j++) Flux[j] = 0.;
249c5740391SJames Wright   for (CeedInt j=0; j<3; j++) {
250c5740391SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
251c5740391SJames Wright     for (CeedInt k=0; k<3; k++) {
252c5740391SJames Wright       Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
253c5740391SJames Wright     }
254c5740391SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
255c5740391SJames Wright   }
256c5740391SJames Wright }
257c5740391SJames Wright 
258475b2820SJames Wright // Kelvin-Mandel notation
259475b2820SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
260475b2820SJames Wright                                         CeedScalar strain_rate[6]) {
261475b2820SJames Wright   const CeedScalar weight = 1 / sqrt(2.);
262475b2820SJames Wright   strain_rate[0] = grad_s[0].Y.velocity[0];
263475b2820SJames Wright   strain_rate[1] = grad_s[1].Y.velocity[1];
264475b2820SJames Wright   strain_rate[2] = grad_s[2].Y.velocity[2];
265475b2820SJames Wright   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
266475b2820SJames Wright   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
267475b2820SJames Wright   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
268475b2820SJames Wright }
269475b2820SJames Wright 
270475b2820SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
271475b2820SJames Wright     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
272475b2820SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
273475b2820SJames Wright   for (CeedInt i=0; i<6; i++) {
274475b2820SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
275475b2820SJames Wright   }
276475b2820SJames Wright }
277475b2820SJames Wright 
278475b2820SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
279475b2820SJames Wright     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
280475b2820SJames Wright     CeedScalar Fe[3]) {
281475b2820SJames Wright   for (CeedInt i=0; i<3; i++) {
282475b2820SJames Wright     Fe[i] = - Y.velocity[0] * stress[0][i]
283475b2820SJames Wright             - Y.velocity[1] * stress[1][i]
284475b2820SJames Wright             - Y.velocity[2] * stress[2][i]
285475b2820SJames Wright             - gas->k * grad_s[i].Y.temperature;
286475b2820SJames Wright   }
287475b2820SJames Wright }
288475b2820SJames Wright 
289475b2820SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
290475b2820SJames Wright     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
291475b2820SJames Wright     const CeedScalar stress[3][3],
292475b2820SJames Wright     const CeedScalar dstress[3][3],
293475b2820SJames Wright     CeedScalar dFe[3]) {
294475b2820SJames Wright   for (CeedInt i=0; i<3; i++) {
295475b2820SJames Wright     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
296475b2820SJames Wright              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
297475b2820SJames Wright              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
298475b2820SJames Wright              - gas->k * grad_ds[i].Y.temperature;
299475b2820SJames Wright   }
300475b2820SJames Wright }
301475b2820SJames Wright 
302475b2820SJames Wright #endif // newtonian_state_h
303