forked from AMReX-Astro/Microphysics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnse_eos.H
376 lines (284 loc) · 9.59 KB
/
nse_eos.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#ifndef NSE_EOS_H
#define NSE_EOS_H
#include <AMReX_REAL.H>
#include <eos.H>
#include <extern_parameters.H>
#include <nse_table_type.H>
#include <nse_table.H>
///
/// This function inverts this form of the EOS to find the T
/// that satisfies the EOS and NSE given an input e and rho.
///
/// if we are in NSE, then the entire thermodynamic state is just
/// a function of rho, T, Ye. We can write the energy as:
///
/// e = e(rho, T, Y_e, Abar(rho, T, Ye))
///
/// where we note that Abar is a function of those same inputs.
///
/// The basic idea is that Abar and Zbar are both functions of
/// rho, T, Ye through the NSE table, so we express the energy
/// as:
///
/// e = e(rho, T, Abar(rho, T, Ye), Zbar(rho, T, Ye)
///
/// and NR on that. Note that Zbar = Ye Abar, so we can group
/// those derivative terms together.
///
/// T and abar come in as initial guesses and are updated
/// on output
///
AMREX_GPU_HOST_DEVICE AMREX_INLINE
void
nse_T_abar_from_e(const Real rho, const Real e_in, const Real Ye,
Real& T, Real& abar) {
using namespace amrex;
using namespace AuxZero;
const Real ttol{1.e-6_rt};
const int max_iter{100};
// we need the full EOS type, since we need de/dA
//eos_extra_t eos_state;
bool converged{false};
int iter{};
nse_table_t nse_state;
while (not converged && iter < max_iter) {
// call NSE table to get Abar
nse_state.T = T;
nse_state.rho = rho;
nse_state.Ye = Ye;
constexpr bool skip_X_fill{true};
nse_interp(nse_state, skip_X_fill);
Real abar_old = nse_state.abar;
// call the EOS with the initial guess for T
eos_re_extra_t eos_state;
eos_state.rho = rho;
eos_state.T = T;
eos_state.aux[iye] = Ye;
eos_state.aux[iabar] = abar_old;
eos(eos_input_rt, eos_state);
// f is the quantity we want to zero
Real f = eos_state.e - e_in;
Real dabar_dT = nse_interp_dT(T, rho, Ye, nse_table::abartab);
// compute the correction to our guess
Real dT = -f / (eos_state.dedT + eos_state.dedA * dabar_dT
+ Ye * eos_state.dedZ * dabar_dT);
// update the temperature
T = std::clamp(T + dT, 0.25 * T, 4.0 * T);
// check convergence
if (std::abs(dT) < ttol * T) {
converged = true;
}
iter++;
}
// T is set to the last T
// we just need to save abar for output
abar = nse_state.abar;
}
///
/// This function inverts this form of the EOS to find the rho
/// that satisfies the EOS and NSE given an input e and T.
///
/// if we are in NSE, then the entire thermodynamic state is just
/// a function of rho, T, Ye. We can write the energy as:
///
/// e = e(rho, T, Y_e, Abar(rho, T, Ye))
///
/// where we note that Abar is a function of those same inputs.
///
/// The basic idea is that Abar and Zbar are both functions of
/// rho, T, Ye through the NSE table, so we express the energy
/// as:
///
/// e = e(rho, T, Abar(rho, T, Ye), Zbar(rho, T, Ye)
///
/// and NR on that. Note that Zbar = Ye Abar, so we can group
/// those derivative terms together.
///
/// rho and abar come in as initial guesses and are updated
/// on output
///
AMREX_GPU_HOST_DEVICE AMREX_INLINE
void
nse_rho_abar_from_e(const Real T, const Real e_in, const Real Ye,
Real& rho, Real& abar) {
using namespace amrex;
using namespace AuxZero;
const Real dtol{1.e-6_rt};
const int max_iter{100};
// we need the full EOS type, since we need de/dA
//eos_extra_t eos_state;
bool converged{false};
int iter{};
nse_table_t nse_state;
while (not converged && iter < max_iter) {
// call NSE table to get Abar
nse_state.T = T;
nse_state.rho = rho;
nse_state.Ye = Ye;
constexpr bool skip_X_fill{true};
nse_interp(nse_state, skip_X_fill);
Real abar_old = nse_state.abar;
// call the EOS with the initial guess for rho
eos_re_extra_t eos_state;
eos_state.rho = rho;
eos_state.T = T;
eos_state.aux[iye] = Ye;
eos_state.aux[iabar] = abar_old;
eos(eos_input_rt, eos_state);
// f is the quantity we want to zero
Real f = eos_state.e - e_in;
Real dabar_drho = nse_interp_drho(T, rho, Ye, nse_table::abartab);
// compute the correction to our guess
Real drho = -f / (eos_state.dedr + eos_state.dedA * dabar_drho
+ Ye * eos_state.dedZ * dabar_drho);
// update the density
rho = std::clamp(rho + drho, 0.25 * rho, 4.0 * rho);
// check convergence
if (std::abs(drho) < dtol * rho) {
converged = true;
}
iter++;
}
// rho is set to the last rho
// we just need to save abar for output
abar = nse_state.abar;
}
///
/// This function inverts this form of the EOS to find the T
/// that satisfies the EOS and NSE given an input p and rho.
///
/// if we are in NSE, then the entire thermodynamic state is just
/// a function of rho, T, Ye. We can write the pressure as:
///
/// p = [(rho, T, Y_e, Abar(rho, T, Ye))
///
/// where we note that Abar is a function of those same inputs.
///
/// The basic idea is that Abar and Zbar are both functions of
/// rho, T, Ye through the NSE table, so we express the pressure
/// as:
///
/// p = p(rho, T, Abar(rho, T, Ye), Zbar(rho, T, Ye)
///
/// and NR on that. Note that Zbar = Ye Abar, so we can group
/// those derivative terms together.
///
/// T and abar come in as initial guesses and are updated
/// on output
///
AMREX_GPU_HOST_DEVICE AMREX_INLINE
void
nse_T_abar_from_p(const Real rho, const Real p_in, const Real Ye,
Real& T, Real& abar) {
using namespace amrex;
using namespace AuxZero;
const Real ttol{1.e-6_rt};
const int max_iter{100};
// we need the full EOS type, since we need de/dA
//eos_extra_t eos_state;
bool converged{false};
int iter{};
nse_table_t nse_state;
while (not converged && iter < max_iter) {
// call NSE table to get Abar
nse_state.T = T;
nse_state.rho = rho;
nse_state.Ye = Ye;
constexpr bool skip_X_fill{true};
nse_interp(nse_state, skip_X_fill);
Real abar_old = nse_state.abar;
// call the EOS with the initial guess for T
eos_rep_extra_t eos_state;
eos_state.rho = rho;
eos_state.T = T;
eos_state.aux[iye] = Ye;
eos_state.aux[iabar] = abar_old;
eos(eos_input_rt, eos_state);
// f is the quantity we want to zero
Real f = eos_state.p - p_in;
Real dabar_dT = nse_interp_dT(T, rho, Ye, nse_table::abartab);
// compute the correction to our guess
Real dT = -f / (eos_state.dpdT + eos_state.dpdA * dabar_dT
+ Ye * eos_state.dpdZ * dabar_dT);
// update the temperature
T = std::clamp(T + dT, 0.25 * T, 4.0 * T);
// check convergence
if (std::abs(dT) < ttol * T) {
converged = true;
}
iter++;
}
// T is set to the last T
// we just need to save abar for output
abar = nse_state.abar;
}
///
/// This function inverts this form of the EOS to find the rho
/// that satisfies the EOS and NSE given an input p and T.
///
/// if we are in NSE, then the entire thermodynamic state is just
/// a function of rho, T, Ye. We can write the pressure as:
///
/// p = [(rho, T, Y_e, Abar(rho, T, Ye))
///
/// where we note that Abar is a function of those same inputs.
///
/// The basic idea is that Abar and Zbar are both functions of
/// rho, T, Ye through the NSE table, so we express the pressure
/// as:
///
/// p = p(rho, T, Abar(rho, T, Ye), Zbar(rho, T, Ye)
///
/// and NR on that. Note that Zbar = Ye Abar, so we can group
/// those derivative terms together.
///
/// rho and abar come in as initial guesses and are updated
/// on output
///
AMREX_GPU_HOST_DEVICE AMREX_INLINE
void
nse_rho_abar_from_p(const Real T, const Real p_in, const Real Ye,
Real& rho, Real& abar) {
using namespace amrex;
using namespace AuxZero;
const Real dtol{1.e-6_rt};
const int max_iter{100};
// we need the full EOS type, since we need de/dA
//eos_extra_t eos_state;
bool converged{false};
int iter{};
nse_table_t nse_state;
while (not converged && iter < max_iter) {
// call NSE table to get Abar
nse_state.T = T;
nse_state.rho = rho;
nse_state.Ye = Ye;
constexpr bool skip_X_fill{true};
nse_interp(nse_state, skip_X_fill);
Real abar_old = nse_state.abar;
// call the EOS with the initial guess for rho
eos_rep_extra_t eos_state;
eos_state.rho = rho;
eos_state.T = T;
eos_state.aux[iye] = Ye;
eos_state.aux[iabar] = abar_old;
eos(eos_input_rt, eos_state);
// f is the quantity we want to zero
Real f = eos_state.p - p_in;
Real dabar_drho = nse_interp_drho(T, rho, Ye, nse_table::abartab);
// compute the correction to our guess
Real drho = -f / (eos_state.dpdr + eos_state.dpdA * dabar_drho
+ Ye * eos_state.dpdZ * dabar_drho);
// update the density
rho = std::clamp(rho + drho, 0.25 * rho, 4.0 * rho);
// check convergence
if (std::abs(drho) < dtol * rho) {
converged = true;
}
iter++;
}
// rho is set to the last rho
// we just need to save abar for output
abar = nse_state.abar;
}
#endif