xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision dc805cc4a09d29f27b3febd084feb659e74b9d08)
1c6e8c570SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2c6e8c570SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3c6e8c570SJames Wright //
4c6e8c570SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5c6e8c570SJames Wright //
6c6e8c570SJames Wright // This file is part of CEED:  http://github.com/ceed
7c6e8c570SJames Wright 
8c6e8c570SJames Wright /// @file
9c6e8c570SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation
10c6e8c570SJames Wright 
11c6e8c570SJames Wright 
12c6e8c570SJames Wright #ifndef newtonian_state_h
13c6e8c570SJames Wright #define newtonian_state_h
14c6e8c570SJames Wright 
15c6e8c570SJames Wright #include <math.h>
16c6e8c570SJames Wright #include <ceed.h>
17c6e8c570SJames Wright #include "newtonian_types.h"
1813fa47b2SJames Wright #include "utils.h"
19c6e8c570SJames Wright 
20c6e8c570SJames Wright typedef struct {
21c6e8c570SJames Wright   CeedScalar pressure;
22c6e8c570SJames Wright   CeedScalar velocity[3];
23c6e8c570SJames Wright   CeedScalar temperature;
24c6e8c570SJames Wright } StatePrimitive;
25c6e8c570SJames Wright 
26c6e8c570SJames Wright typedef struct {
27c6e8c570SJames Wright   CeedScalar density;
28c6e8c570SJames Wright   CeedScalar momentum[3];
29c6e8c570SJames Wright   CeedScalar E_total;
30c6e8c570SJames Wright } StateConservative;
31c6e8c570SJames Wright 
32c6e8c570SJames Wright typedef struct {
33c6e8c570SJames Wright   StateConservative U;
34c6e8c570SJames Wright   StatePrimitive Y;
35c6e8c570SJames Wright } State;
36c6e8c570SJames Wright 
37c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
38c6e8c570SJames Wright   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
39c6e8c570SJames Wright   StatePrimitive Y;
40c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
41c6e8c570SJames Wright   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
42c6e8c570SJames Wright   CeedScalar e_potential = -Dot3(gas->g, x);
43c6e8c570SJames Wright   CeedScalar e_total = U.E_total / U.density;
44c6e8c570SJames Wright   CeedScalar e_internal = e_total - e_kinetic - e_potential;
45c6e8c570SJames Wright   Y.temperature = e_internal / gas->cv;
46c6e8c570SJames Wright   Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal;
47c6e8c570SJames Wright   return Y;
48c6e8c570SJames Wright }
49c6e8c570SJames Wright 
50c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
51c6e8c570SJames Wright   NewtonianIdealGasContext gas, State s, StateConservative dU,
52c6e8c570SJames Wright   const CeedScalar x[3], const CeedScalar dx[3]) {
53c6e8c570SJames Wright   StatePrimitive dY;
54c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
55c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
56c6e8c570SJames Wright   }
57c6e8c570SJames Wright   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
58c6e8c570SJames Wright   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
59c6e8c570SJames Wright   CeedScalar e_potential = -Dot3(gas->g, x);
60c6e8c570SJames Wright   CeedScalar de_potential = -Dot3(gas->g, dx);
61c6e8c570SJames Wright   CeedScalar e_total = s.U.E_total / s.U.density;
62c6e8c570SJames Wright   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
63c6e8c570SJames Wright   CeedScalar e_internal = e_total - e_kinetic - e_potential;
64c6e8c570SJames Wright   CeedScalar de_internal = de_total - de_kinetic - de_potential;
65c6e8c570SJames Wright   dY.temperature = de_internal / gas->cv;
66c6e8c570SJames Wright   dY.pressure = (gas->cp / gas->cv - 1)
67c6e8c570SJames Wright                 * (dU.density * e_internal + s.U.density * de_internal);
68c6e8c570SJames Wright   return dY;
69c6e8c570SJames Wright }
70c6e8c570SJames Wright 
71*dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(
72*dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) {
73*dc805cc4SLeila Ghaffari   StateConservative U;
74*dc805cc4SLeila Ghaffari   CeedScalar R = gas->cp - gas->cv;
75*dc805cc4SLeila Ghaffari   U.density = Y.pressure / (R * Y.temperature);
76*dc805cc4SLeila Ghaffari   for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i];
77*dc805cc4SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
78*dc805cc4SLeila Ghaffari   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
79*dc805cc4SLeila Ghaffari   CeedScalar e_potential = -Dot3(gas->g, x);
80*dc805cc4SLeila Ghaffari   CeedScalar e_total = e_internal + e_kinetic + e_potential;
81*dc805cc4SLeila Ghaffari   U.E_total = U.density*e_total;
82*dc805cc4SLeila Ghaffari   return U;
83*dc805cc4SLeila Ghaffari }
84*dc805cc4SLeila Ghaffari 
85*dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(
86*dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas, State s, StatePrimitive dY,
87*dc805cc4SLeila Ghaffari   const CeedScalar x[3], const CeedScalar dx[3]) {
88*dc805cc4SLeila Ghaffari   StateConservative dU;
89*dc805cc4SLeila Ghaffari   CeedScalar R = gas->cp - gas->cv;
90*dc805cc4SLeila Ghaffari   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) /
91*dc805cc4SLeila Ghaffari                (R * s.Y.temperature * s.Y.temperature);
92*dc805cc4SLeila Ghaffari   for (int i=0; i<3; i++) {
93*dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
94*dc805cc4SLeila Ghaffari   }
95*dc805cc4SLeila Ghaffari   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
96*dc805cc4SLeila Ghaffari   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
97*dc805cc4SLeila Ghaffari   CeedScalar e_potential = -Dot3(gas->g, x);
98*dc805cc4SLeila Ghaffari   CeedScalar de_potential = -Dot3(gas->g, dx);
99*dc805cc4SLeila Ghaffari   CeedScalar e_internal = gas->cv * s.Y.temperature;
100*dc805cc4SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
101*dc805cc4SLeila Ghaffari   CeedScalar e_total = e_internal + e_kinetic + e_potential;
102*dc805cc4SLeila Ghaffari   CeedScalar de_total = de_internal + de_kinetic + de_potential;
103*dc805cc4SLeila Ghaffari   dU.E_total = dU.density*e_total + s.U.density*de_total;
104*dc805cc4SLeila Ghaffari   return dU;
105*dc805cc4SLeila Ghaffari }
106*dc805cc4SLeila Ghaffari 
107c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
108c6e8c570SJames Wright                                        const CeedScalar U[5], const CeedScalar x[3]) {
109c6e8c570SJames Wright   State s;
110c6e8c570SJames Wright   s.U.density = U[0];
111c6e8c570SJames Wright   s.U.momentum[0] = U[1];
112c6e8c570SJames Wright   s.U.momentum[1] = U[2];
113c6e8c570SJames Wright   s.U.momentum[2] = U[3];
114c6e8c570SJames Wright   s.U.E_total = U[4];
115c6e8c570SJames Wright   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
116c6e8c570SJames Wright   return s;
117c6e8c570SJames Wright }
118c6e8c570SJames Wright 
119c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
120c6e8c570SJames Wright     State s, const CeedScalar dU[5],
121c6e8c570SJames Wright     const CeedScalar x[3], const CeedScalar dx[3]) {
122c6e8c570SJames Wright   State ds;
123c6e8c570SJames Wright   ds.U.density = dU[0];
124c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
125c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
126c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
127c6e8c570SJames Wright   ds.U.E_total = dU[4];
128c6e8c570SJames Wright   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
129c6e8c570SJames Wright   return ds;
130c6e8c570SJames Wright }
131c6e8c570SJames Wright 
132*dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas,
133*dc805cc4SLeila Ghaffari                                        const CeedScalar Y[5], const CeedScalar x[3]) {
134*dc805cc4SLeila Ghaffari   State s;
135*dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
136*dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
137*dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
138*dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
139*dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
140*dc805cc4SLeila Ghaffari   s.U = StateConservativeFromPrimitive(gas, s.Y, x);
141*dc805cc4SLeila Ghaffari   return s;
142*dc805cc4SLeila Ghaffari }
143*dc805cc4SLeila Ghaffari 
144*dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas,
145*dc805cc4SLeila Ghaffari     State s, const CeedScalar dY[5],
146*dc805cc4SLeila Ghaffari     const CeedScalar x[3], const CeedScalar dx[3]) {
147*dc805cc4SLeila Ghaffari   State ds;
148*dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
149*dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
150*dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
151*dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
152*dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
153*dc805cc4SLeila Ghaffari   ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx);
154*dc805cc4SLeila Ghaffari   return ds;
155*dc805cc4SLeila Ghaffari }
156*dc805cc4SLeila Ghaffari 
157c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
158c6e8c570SJames Wright                                         StateConservative Flux[3]) {
159c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
160c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
161c6e8c570SJames Wright     for (CeedInt j=0; j<3; j++)
162c6e8c570SJames Wright       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
163c6e8c570SJames Wright                             + s.Y.pressure * (i == j);
164c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
165c6e8c570SJames Wright   }
166c6e8c570SJames Wright }
167c6e8c570SJames Wright 
168c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
169c6e8c570SJames Wright     State s, State ds, StateConservative dFlux[3]) {
170c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
171c6e8c570SJames Wright     dFlux[i].density = ds.U.momentum[i];
172c6e8c570SJames Wright     for (CeedInt j=0; j<3; j++)
173c6e8c570SJames Wright       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
174c6e8c570SJames Wright                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
175c6e8c570SJames Wright     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
176c6e8c570SJames Wright                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
177c6e8c570SJames Wright   }
178c6e8c570SJames Wright }
179c6e8c570SJames Wright 
180c6e8c570SJames Wright // Kelvin-Mandel notation
181c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
182c6e8c570SJames Wright                                         CeedScalar strain_rate[6]) {
183c6e8c570SJames Wright   const CeedScalar weight = 1 / sqrt(2.);
184c6e8c570SJames Wright   strain_rate[0] = grad_s[0].Y.velocity[0];
185c6e8c570SJames Wright   strain_rate[1] = grad_s[1].Y.velocity[1];
186c6e8c570SJames Wright   strain_rate[2] = grad_s[2].Y.velocity[2];
187c6e8c570SJames Wright   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
188c6e8c570SJames Wright   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
189c6e8c570SJames Wright   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
190c6e8c570SJames Wright }
191c6e8c570SJames Wright 
192c6e8c570SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
193c6e8c570SJames Wright     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
194c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
195c6e8c570SJames Wright   for (CeedInt i=0; i<6; i++) {
196c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
197c6e8c570SJames Wright   }
198c6e8c570SJames Wright }
199c6e8c570SJames Wright 
200c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
201c6e8c570SJames Wright     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
202c6e8c570SJames Wright     CeedScalar Fe[3]) {
203c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
204c6e8c570SJames Wright     Fe[i] = - Y.velocity[0] * stress[0][i]
205c6e8c570SJames Wright             - Y.velocity[1] * stress[1][i]
206c6e8c570SJames Wright             - Y.velocity[2] * stress[2][i]
207c6e8c570SJames Wright             - gas->k * grad_s[i].Y.temperature;
208c6e8c570SJames Wright   }
209c6e8c570SJames Wright }
210c6e8c570SJames Wright 
211c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
212c6e8c570SJames Wright     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
213c6e8c570SJames Wright     const CeedScalar stress[3][3],
214c6e8c570SJames Wright     const CeedScalar dstress[3][3],
215c6e8c570SJames Wright     CeedScalar dFe[3]) {
216c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
217c6e8c570SJames Wright     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
218c6e8c570SJames Wright              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
219c6e8c570SJames Wright              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
220c6e8c570SJames Wright              - gas->k * grad_ds[i].Y.temperature;
221c6e8c570SJames Wright   }
222c6e8c570SJames Wright }
223c6e8c570SJames Wright 
224c6e8c570SJames Wright #endif // newtonian_state_h
225