MPI-AMRVAC 3.2
The MPI - Adaptive Mesh Refinement - Versatile Advection Code (development version)
Loading...
Searching...
No Matches
mod_eos_lte.t
Go to the documentation of this file.
1!=============================================================================
2!> LTE (Saha-table) EoS kernels and finalise for the eos% family.
3!>
4!> Carved out of mod_eos.t (the per-type split). Owns everything LTE: the
5!> per-substep update_eos_LTE (caches Te_/Ne_), the cached-Te/Rfactor getters,
6!> the scalar table kernels (T, y, p2eint, gamma1, p/nH, eint, bisections), the
7!> per-method table finalisers, and eos_finalise_LTE (the LTE arm of the
8!> eos_finalise dispatcher). Depends only on the EoS leaf modules + shared
9!> accessors, never on mod_eos -> no circular use. mod_eos re-exports the public
10!> kernels so existing `use mod_eos` callers are unaffected.
11!=============================================================================
22 use mod_comm_lib, only: mpistop
23
24 implicit none
25 private
26
27 !> update_eos_LTE, get_Te_LTE, get_temperature_from_eint_LTE and
28 !> Rfactor_from_LTE are PRIVATE: all bound to eos% pointers inside
29 !> eos_finalise_LTE, so no external module names them.
30 !> get_temperature_from_eint_fast_LTE stays public -- the seam binds it to
31 !> tc_fl%get_temperature_from_eint (fast TC path)
33 !> Scalar table kernels (re-exported by mod_eos for the hd/mhd/ffhd seams).
34 !> T_and_y_from_nH_eint is internal-only (entropy update path).
38 !> LTE arms of the eos_init / eos_finalise dispatchers
41
42contains
43
44 !> LTE arm of eos_init (before units are known): dispatch on eos_method to
45 !> the chosen method's loader, each owned by its method module (analytic loads
46 !> nothing). The LTE temperature-from-pressure getter is a later pass, so
47 !> unlike FI/PI it is left unset here.
48 subroutine eos_init_lte()
49 select case (eos%method_id)
50 case (eos_analytic); call load_analytic_lte()
51 case (eos_entropy); call load_entropy_lte()
52 case default; call load_state_lte() ! EOS_STATE
53 end select
54 end subroutine eos_init_lte
55
56 !> LTE arm of eos_finalise: wire the LTE runtime pointer targets, then
57 !> dispatch on eos_method to the per-method finaliser, each owned by its
58 !> method module (analytic->mod_eos_lte_saha, entropy->mod_eos_lte_entropy,
59 !> tables->mod_eos_lte_tables).
60 subroutine eos_finalise_lte()
61 eos%update_eos => update_eos_lte
62 ! eos%get_thermal_pressure is set by (m)hd_link_eos
63 eos%get_temperature_from_eint => get_temperature_from_eint_lte
64 eos%get_Te => get_te_lte
65 !> R-factor: physics-independent (cached-Ne lookup), bound here rather
66 !> than in the hd/mhd/ffhd link arms so the target stays private.
67 eos%get_Rfactor => rfactor_from_lte
68 select case (eos%method_id)
71 case default; call finalise_state_lte() ! EOS_STATE
72 end select
73 end subroutine eos_finalise_lte
74
75
76 subroutine update_eos_lte(ixI^L, ixO^L, w, x)
77 use mod_physics
78 !> This routine is called before each RK substep
79 integer, intent(in) :: ixi^l,ixo^l
80 double precision, intent(in) :: x(ixi^s,1:ndim)
81 double precision, intent(inout) :: w(ixi^s,1:nw)
82
83 double precision :: wlocal(ixi^s,1:nw)
84 double precision :: new_y(ixi^s)
85 double precision :: pth(ixi^s)
86 double precision :: nh_in(ixi^s), nh(ixi^s), eint_in(ixi^s)
87 double precision :: rfactor_fi
88 double precision :: yy
89 integer :: ix^d
90
91 wlocal(ixi^s,1:nw)=w(ixi^s,1:nw)
92 ! wlocal holds the internal energy, and pth the ideal-gas pressure, valid only without ionE
93 call phys_e_to_ei(ixi^l,ixo^l,wlocal,x)
94 pth(ixo^s) = eos%gamma_minus_1 * wlocal(ixo^s,iw_e)
95
96 call eos%get_nH(w, x, ixi^l, ixo^l, nh)
97 ! Enforce internal energy floor for EoS lookup only.
98 ! Prevents NaN from dlog10(eint<0) after strong rarefactions where
99 ! kinetic energy can numerically exceed total energy.
100 ! The conserved energy w(iw_e) is NOT modified: the physical fluxes
101 ! based on the small positive pressure will naturally restore the cell.
102 ! eint_nH_min is the forward-table edge, or 100 K of neutral gas under analytic
103 wlocal(ixo^s,iw_e) = max(wlocal(ixo^s,iw_e), nh(ixo^s)*eos%eint_nH_min)
104 if (eos%method_id /= eos_analytic) then
105 nh_in(ixo^s) = dlog10(nh(ixo^s))
106 eint_in(ixo^s) = dlog10(wlocal(ixo^s,iw_e)) - nh_in(ixo^s)
107 end if
108
109 !> Constant FI Rfactor for the fully-ionised fast path.
110 !> Must NOT use eos%get_Rfactor which reads iw_ne (uninitialised at IC).
111 rfactor_fi = eos%n_per_nH_FI / eos%nH2rhoFactor
112
113 {do ix^db=ixomin^db,ixomax^db\}
114 if (wlocal(ix^d,iw_e) / w(ix^d,iw_rho) > eos%eint_rho_FI_threshold) then
115 !> Fully ionised: analytical formulae, no table lookups
116 new_y(ix^d) = eos%neOnH_FI
117 if (eos%ionE) then
118 !> T = (eint - eion*nH) * (gamma-1) / (Rfactor_FI * rho)
119 w(ix^d,iw_te) = eos%gamma_minus_1 &
120 * (wlocal(ix^d,iw_e) - eos%eion_per_nH * nh(ix^d)) &
121 / (rfactor_fi * w(ix^d,iw_rho))
122 else
123 w(ix^d,iw_te) = pth(ix^d) / (nh(ix^d) * (1.0d0 + eos%He_abundance + new_y(ix^d)))
124 end if
125 else
126 !> Ionisation zone
127 if (eos%method_id == eos_analytic) then
128 !> Analytical Saha: solve quadratic for T and y
129 call saha_t_from_nh_eint(nh(ix^d), &
130 wlocal(ix^d,iw_e) / nh(ix^d), &
131 w(ix^d,iw_te), yy)
132 new_y(ix^d) = yy
133 else
134 !> Table lookups: fused T+y for shared index computation
135 if (.not. eos%ionE) then
136 new_y(ix^d) = y_from_nh_eint(nh_in(ix^d),eint_in(ix^d))
137 w(ix^d,iw_te) = pth(ix^d) / (nh(ix^d) * (1.0d0 + eos%He_abundance + new_y(ix^d)))
138 else if (eos%method_id == eos_entropy) then
139 !> Entropy method dispatches T_and_y_from_nH_eint to the
140 !> biquintic-Hermite forward at any eint; no FI fallback
141 !> needed (and eos%T isn't loaded so var2_max is 0 here).
142 call t_and_y_from_nh_eint(nh_in(ix^d), eint_in(ix^d), &
143 w(ix^d,iw_te), yy)
144 new_y(ix^d) = yy
145 else
146 if (eint_in(ix^d) < eos%T%var2_max) then
147 call t_and_y_from_nh_eint(nh_in(ix^d), eint_in(ix^d), &
148 w(ix^d,iw_te), yy)
149 new_y(ix^d) = yy
150 else
151 !> Above-table fallback with ionisation energy correction
152 new_y(ix^d) = eos%neOnH_FI
153 w(ix^d,iw_te) = eos%gamma_minus_1 &
154 * (wlocal(ix^d,iw_e) - eos%eion_per_nH * nh(ix^d)) &
155 / (rfactor_fi * w(ix^d,iw_rho))
156 end if
157 end if
158 end if
159 end if
160 {end do\}
161
162 w(ixo^s,iw_ne) = new_y(ixo^s) * nh(ixo^s)
163
164 end subroutine update_eos_lte
165
166 !> Thermodynamic getters shared verbatim by the hd/mhd seams (eos%/phys
167 !> pointer targets). Only routines free of physics-module symbols live here:
168 !> Rfactor_from_LTE uses the cached Ne_ field; get_gamma1_FI returns
169 !> eos%gamma. The FI/LTE variants that read primitive indices (rho_,p_) or
170 !> the physics RR stay in each seam.
171 !> Rfactor from LTE EOS: R = (1 + He + ne/nH) / nH2rhoFactor
172 !> Uses stored Ne_ wextra field from update_eos_LTE.
173 subroutine rfactor_from_lte(w,x,ixI^L,ixO^L,Rfactor)
175 integer, intent(in) :: ixi^l, ixo^l
176 double precision, intent(in) :: w(ixi^s,1:nw)
177 double precision, intent(in) :: x(ixi^s,1:ndim)
178 double precision, intent(out):: rfactor(ixi^s)
179
180 double precision :: y_nh
181 integer :: ix^d
182
183 {do ix^db=ixomin^db,ixomax^db\}
184 y_nh = w(ix^d, iw_ne) * eos%nH2rhoFactor / w(ix^d, iw_rho)
185 rfactor(ix^d) = (1.0d0 + eos%He_abundance + y_nh) / eos%nH2rhoFactor
186 {end do\}
187
188 end subroutine rfactor_from_lte
189
190 !>#######################################################################
191 !> Temperature getters: cached Te accessors and temperature from the
192 !> energy/pressure variable. The from-eint variants are split by eos_type
193 !> (needed by the STS path: dt sees conserved, set_source sees eint -- see
194 !> mod_thermal_conduction).
195 !>#######################################################################
196
197 subroutine get_te_lte(w,x,ixI^L,ixO^L,T)
199 integer, intent(in) :: ixi^l, ixo^l
200 double precision, intent(in) :: w(ixi^s,1:nw)
201 double precision, intent(in) :: x(ixi^s,1:ndim)
202 double precision, intent(out) :: t(ixi^s)
203
204 t(ixo^s) = w(ixo^s,iw_te)
205
206 end subroutine get_te_lte
207
208 subroutine get_temperature_from_eint_lte(w, x, ixI^L, ixO^L, res)
209 !> Assumes input energy is internal energy.
210 !> Includes FI bypass: cells with eint/rho above threshold skip table lookups.
211 use mod_physics
212 integer, intent(in) :: ixi^l,ixo^l
213 double precision, intent(in) :: x(ixi^s,1:ndim)
214 double precision, intent(in) :: w(ixi^s,1:nw)
215 double precision, intent(out) :: res(ixi^s)
216
217 double precision :: nh(ixi^s),nh_in(ixi^s), eint_in(ixi^s)
218 double precision :: rfactor_fi, t_loc, y_loc
219 integer :: ix^d
220
221 rfactor_fi = eos%n_per_nH_FI / (1.0d0 + 4.0d0*eos%He_abundance)
222
223 call eos%get_nH(w, x, ixi^l, ixo^l, nh)
224 nh_in(ixo^s) = dlog10(nh(ixo^s))
225 eint_in(ixo^s) = dlog10(w(ixo^s,iw_e)) - nh_in(ixo^s)
226
227 {do ix^db=ixomin^db,ixomax^db\}
228 if (w(ix^d,iw_e) / w(ix^d,iw_rho) > eos%eint_rho_FI_threshold) then
229 !> Fully ionised: T = (eint - eion*nH) * (gamma-1) / (Rfactor_FI * rho)
230 res(ix^d) = eos%gamma_minus_1 &
231 * (w(ix^d,iw_e) - eos%eion_per_nH * nh(ix^d)) &
232 / (rfactor_fi * w(ix^d,iw_rho))
233 else if (eos%method_id == eos_analytic) then
234 call saha_t_from_nh_eint(nh(ix^d), &
235 w(ix^d,iw_e) / nh(ix^d), t_loc, y_loc)
236 res(ix^d) = t_loc
237 else
238 res(ix^d) = t_from_nh_eint(nh_in(ix^d),eint_in(ix^d))
239 endif
240 {end do\}
241
242 end subroutine get_temperature_from_eint_lte
243
244 subroutine get_temperature_from_eint_fast_lte(w, x, ixI^L, ixO^L, res)
245 !> Fast TC variant: two-pass regime-aware bypass.
246 !>
247 !> Pass 1 (vectorised): compute FI formula for ALL cells as array ops.
248 !> The compiler vectorises this with SVML (no branches, pure arithmetic).
249 !>
250 !> Pass 2 (scalar): overwrite ionisation-zone cells with
251 !> bilinear table lookup + dexp. Threshold check uses multiply
252 !> (eint <= threshold * rho) to avoid a 14-cycle scalar division.
253 use mod_physics
254 integer, intent(in) :: ixi^l,ixo^l
255 double precision, intent(in) :: x(ixi^s,1:ndim)
256 double precision, intent(in) :: w(ixi^s,1:nw)
257 double precision, intent(out) :: res(ixi^s)
258
259 double precision :: inv_rfactor_fi, eion_rho_inv
260 double precision :: log_nh_val, log_eint_nh_val
261 double precision :: fx, fy, rx, ry, nh_loc, t_loc, y_loc
262 integer :: jx, jy, jx1, jy1, ix^d
263 double precision, parameter :: ln10 = 2.302585092994046d0
264
265 if (eos%type_id /= eos_type_lte) call mpistop("get_temperature_from_eint_fast_LTE called outside its eos_type (LTE)")
266 call eos_timer_start()
267
268 !> Precompute scalar constants (avoid per-cell divisions)
269 inv_rfactor_fi = (1.0d0 + 4.0d0*eos%He_abundance) / eos%n_per_nH_FI
270 eion_rho_inv = eos%eion_per_nH / eos%nH2rhoFactor
271
272 !> Pass 1: FI formula for ALL cells (vectorisable array operations).
273 !> T = (gamma-1) * (eint - eion_per_rho * rho) / (Rfactor_FI * rho)
274 res(ixo^s) = eos%gamma_minus_1 * inv_rfactor_fi &
275 * (w(ixo^s,iw_e) - eion_rho_inv * w(ixo^s,iw_rho)) &
276 / w(ixo^s,iw_rho)
277
278 !> Pass 2: overwrite ionisation-zone cells.
279 {do ix^db=ixomin^db,ixomax^db\}
280 if (w(ix^d,iw_e) <= eos%eint_rho_FI_threshold * w(ix^d,iw_rho)) then
281 if (eos%method_id == eos_analytic) then
282 nh_loc = w(ix^d, iw_rho) / eos%nH2rhoFactor
283 call saha_t_from_nh_eint(nh_loc, &
284 w(ix^d,iw_e) / nh_loc, t_loc, y_loc)
285 res(ix^d) = t_loc
286 else if (eos%method_id == eos_entropy) then
287 if (iw_log_nh > 0) then
288 log_nh_val = block%wextra(ix^d, iw_log_nh)
289 else
290 log_nh_val = dlog10(w(ix^d, iw_rho) / eos%nH2rhoFactor)
291 end if
292 log_eint_nh_val = dlog10(w(ix^d, iw_e)) - log_nh_val
293 res(ix^d) = entropy_t_from_nh_eint(log_nh_val, log_eint_nh_val)
294 else
295 if (iw_log_nh > 0) then
296 log_nh_val = block%wextra(ix^d, iw_log_nh)
297 else
298 log_nh_val = dlog10(w(ix^d, iw_rho) / eos%nH2rhoFactor)
299 end if
300 log_eint_nh_val = dlog10(w(ix^d, iw_e)) - log_nh_val
301
302 if (eos%T%is_uniform) then
303 !> Inlined uniform bilinear (hot path: no function call).
304 ry = max(0.0d0, min((log_nh_val - eos%T%var1_min) * eos%T%step_inv_1, &
305 dble(eos%T%dim1-1)))
306 rx = max(0.0d0, min((log_eint_nh_val - eos%T%var2_min) * eos%T%step_inv_2, &
307 dble(eos%T%dim2-1)))
308 jy = int(ry); jx = int(rx)
309 jy1 = min(jy+1, eos%T%dim1-1)
310 jx1 = min(jx+1, eos%T%dim2-1)
311 fy = ry - dble(jy); fx = rx - dble(jx)
312 res(ix^d) = dexp(ln10 * ( &
313 (1.0d0-fy)*((1.0d0-fx)*eos%T%table(jy+1,jx+1) &
314 + fx *eos%T%table(jy+1,jx1+1)) &
315 + fy *((1.0d0-fx)*eos%T%table(jy1+1,jx+1) &
316 + fx *eos%T%table(jy1+1,jx1+1))))
317 else
318 !> Non-uniform: dispatch to the binary-search bilinear.
319 !> Slightly slower per cell (binary search adds ~8 cmps per axis)
320 !> but unavoidable when the grid is non-uniform.
321 res(ix^d) = dexp(ln10 * bilinear_lookup( &
322 log_nh_val, log_eint_nh_val, eos%T))
323 end if
324 end if
325 end if
326 {end do\}
327
328 call eos_timer_stop(timeeos_tfromei)
330
331 !> Scalar EoS kernels: one cell on (log10 nH, log10 eint/nH) or (log10 nH, log10 p/nH), in
332 !> code units, each dispatching to the analytic Saha solve, the entropy tables or the tables
333
334 !> Ionisation fraction ne/nH from (log10 nH, log10 eint/nH)
335 double precision function y_from_nh_eint(nH, eint_nh) result(result_val)
336 double precision, intent(in) :: nh, eint_nh
337 double precision, parameter :: ln10 = 2.302585092994046d0
338 double precision :: t_loc, y_loc
339
340 if (eos%type_id /= eos_type_lte) call mpistop("y_from_nH_eint called outside its eos_type (LTE)")
341 ! A fully ionised cell skips the method, whose tables end above this threshold
342 if (eint_nh > eos%log_eint_nH_FI) then
343 result_val = eos%neOnH_FI
344 return
345 end if
346 if (eos%method_id == eos_analytic) then
347 call saha_t_from_nh_eint(10.0d0**nh, 10.0d0**eint_nh, t_loc, y_loc)
348 result_val = y_loc
349 else if (eos%method_id == eos_entropy) then
350 result_val = entropy_y_from_nh_eint(nh, eint_nh)
351 else
352 result_val = dexp(ln10 * bicubic_lookup(nh, eint_nh, eos%neOnH))
353 end if
354 end function y_from_nh_eint
355
356 !> Temperature from (log10 nH, log10 eint/nH)
357 double precision function t_from_nh_eint(nH, eint_nh) result(result_val)
358 double precision, intent(in) :: nh, eint_nh
359 double precision, parameter :: ln10 = 2.302585092994046d0
360 double precision :: t_loc, y_loc
361
362 if (eos%type_id /= eos_type_lte) call mpistop("T_from_nH_eint called outside its eos_type (LTE)")
363 if (eint_nh > eos%log_eint_nH_FI) then
364 result_val = eos%gamma_minus_1 &
365 * (10.0d0**eint_nh - eos%eion_per_nH) / eos%n_per_nH_FI
366 return
367 end if
368 if (eos%method_id == eos_analytic) then
369 call saha_t_from_nh_eint(10.0d0**nh, 10.0d0**eint_nh, t_loc, y_loc)
370 result_val = t_loc
371 else if (eos%method_id == eos_entropy) then
372 result_val = entropy_t_from_nh_eint(nh, eint_nh)
373 else
374 result_val = dexp(ln10 * bicubic_lookup(nh, eint_nh, eos%T))
375 end if
376 end function t_from_nh_eint
377
378 !> Temperature and ionisation fraction from (log10 nH, log10 eint/nH), sharing one table
379 !> locate when the interleaved table exists
380 subroutine t_and_y_from_nh_eint(log_nH, log_eint_nH, T_out, y_out)
381 double precision, intent(in) :: log_nh, log_eint_nh
382 double precision, intent(out) :: t_out, y_out
383 double precision, parameter :: ln10 = 2.302585092994046d0
384 double precision :: results(3)
385
386 if (eos%method_id == eos_analytic) then
387 call saha_t_from_nh_eint(10.0d0**log_nh, 10.0d0**log_eint_nh, t_out, y_out)
388 else if (eos%method_id == eos_entropy) then
389 call entropy_t_and_y_from_nh_eint(log_nh, log_eint_nh, t_out, y_out)
390 else if (allocated(eos%table_eint_il)) then
391 ! The interleaved table holds (T, ne/nH, p/nH) per node
392 if (eos%T%is_uniform) then
393 call interp_pchip_interleaved(log_nh, log_eint_nh, &
394 eos%table_eint_il, 3, eos%T%dim2, eos%T%dim1, &
395 eos%T%var1_min, eos%T%var1_max, &
396 eos%T%var2_min, eos%T%var2_max, results)
397 else
398 call interp_pchip_interleaved_nu(log_nh, log_eint_nh, &
399 eos%table_eint_il, 3, eos%T%dim2, eos%T%dim1, &
400 eos%T%var1_nodes, eos%T%var2_nodes, &
401 eos%T%guard_1, eos%T%guard_M_1, eos%T%guard_scale_1, &
402 eos%T%guard_2, eos%T%guard_M_2, eos%T%guard_scale_2, &
403 results)
404 end if
405 t_out = dexp(ln10 * results(1))
406 y_out = dexp(ln10 * results(2))
407 else
408 t_out = dexp(ln10 * bicubic_lookup(log_nh, log_eint_nh, eos%T))
409 y_out = dexp(ln10 * bicubic_lookup(log_nh, log_eint_nh, eos%neOnH))
410 end if
411 end subroutine t_and_y_from_nh_eint
412
413 !> Ratio eint/p from (log10 nH, log10 p/nH)
414 double precision function p2eint_from_nh_p(nH, ponH) result(result_val)
415 double precision, intent(in) :: nh, ponh
416 double precision :: nh_code, p_code, t_loc, y_loc, eint_nh_loc
417
418 if (eos%type_id /= eos_type_lte) call mpistop("p2eint_from_nH_p called outside its eos_type (LTE)")
419 if (ponh > eos%log_p_nH_FI) then
420 result_val = eos%inv_gamma_minus_1 + eos%eion_per_nH / 10.0d0**ponh
421 return
422 end if
423 if (eos%method_id == eos_analytic) then
424 nh_code = 10.0d0**nh
425 p_code = nh_code * 10.0d0**ponh
426 call saha_state_from_nh_p(nh_code, p_code, t_loc, y_loc, eint_nh_loc)
427 result_val = eint_nh_loc * nh_code / p_code
428 else if (eos%method_id == eos_entropy) then
429 result_val = entropy_eint_from_nh_p(nh, ponh)
430 else
431 result_val = bicubic_lookup(nh, ponh, eos%p2eint)
432 end if
433 end function p2eint_from_nh_p
434
435 !> Gamma_1 from (log10 nH, log10 p/nH): one lookup on the pressure-indexed table, or for the
436 !> analytic method the closed form after a Saha solve at that pressure
437 double precision function gamma1_from_nh_p(log_nH, log_p_nH) result(g1)
438 double precision, intent(in) :: log_nh, log_p_nh
439 if (eos%type_id /= eos_type_lte) call mpistop("gamma1_from_nH_p called outside its eos_type (LTE)")
440 if (log_p_nh > eos%log_p_nH_FI) then
441 g1 = eos%gamma
442 return
443 end if
444 if (eos%method_id == eos_entropy) then
445 g1 = entropy_gamma1_from_nh_p(log_nh, log_p_nh)
446 else if (eos%method_id == eos_analytic) then
447 g1 = saha_gamma1_from_nh_p(10.0d0**log_nh, 10.0d0**(log_nh + log_p_nh))
448 else
449 g1 = bicubic_lookup(log_nh, log_p_nh, eos%gamma1_p)
450 end if
451 end function gamma1_from_nh_p
452
453 !> Gamma_1 of one cell from (p, rho): the ideal value where fully ionised, else the pressure-
454 !> indexed table of the method
455 double precision function lte_gamma1_from_p(pres, rho_val) result(g1)
456 double precision, intent(in) :: pres, rho_val
457 double precision :: nh_val
458 if (pres/rho_val > eos%p_rho_FI_threshold) then
459 g1 = eos%gamma
460 else
461 nh_val = rho_val/eos%nH2rhoFactor
462 g1 = gamma1_from_nh_p(dlog10(nh_val), dlog10(pres/nh_val))
463 end if
464 end function lte_gamma1_from_p
465
466 !> log10(p/nH) from (log10 nH, log10 eint/nH)
467 double precision function log_p_from_nh_eint(log_nH, log_eint_nH) result(lp)
468 double precision, intent(in) :: log_nh, log_eint_nh
469 lp = bicubic_lookup(log_nh, log_eint_nh, eos%log_p)
470 end function log_p_from_nh_eint
471
472 !> p/nH = (1 + He + y) T from (log10 nH, log10 eint/nH)
473 double precision function p_nh_from_eint(log_nH, log_eint_nH) result(p_nH)
474 double precision, intent(in) :: log_nh, log_eint_nh
475 double precision, parameter :: ln10 = 2.302585092994046d0
476 double precision :: t_loc, y_loc
477
478 if (eos%type_id /= eos_type_lte) call mpistop("p_nH_from_eint called outside its eos_type (LTE)")
479 if (log_eint_nh > eos%log_eint_nH_FI) then
480 p_nh = eos%gamma_minus_1 * (10.0d0**log_eint_nh - eos%eion_per_nH)
481 return
482 end if
483 if (eos%method_id == eos_analytic) then
484 call saha_t_from_nh_eint(10.0d0**log_nh, 10.0d0**log_eint_nh, t_loc, y_loc)
485 p_nh = (1.0d0 + eos%He_abundance + y_loc) * t_loc
486 else if (eos%method_id == eos_entropy) then
487 p_nh = entropy_p_nh_from_eint(log_nh, log_eint_nh)
488 else
489 p_nh = dexp(ln10 * bicubic_lookup(log_nh, log_eint_nh, eos%p_over_nH))
490 end if
491 end function p_nh_from_eint
492
493 !> eint/nH from (log10 nH, log10 T), from the inverse table when one is built and the Saha
494 !> solve otherwise
495 double precision function eint_nh_from_t(log_nH, log_T) result(eint_nH)
496 double precision, intent(in) :: log_nh, log_t
497 double precision, parameter :: ln10 = 2.302585092994046d0
498 double precision :: log_e_nh
499
500 if (eos%type_id /= eos_type_lte) call mpistop("eint_nH_from_T called outside its eos_type (LTE)")
501 if (log_t > eos%log_T_FI) then
502 eint_nh = eos%n_per_nH_FI * eos%inv_gamma_minus_1 * 10.0d0**log_t + eos%eion_per_nH
503 return
504 end if
505 if (eos%method_id == eos_entropy) then
506 log_e_nh = entropy_eint_from_nh_t(log_nh, log_t)
507 eint_nh = 10.0d0**log_e_nh
508 else if (allocated(eos%eint_from_T%table)) then
509 eint_nh = dexp(ln10 * bicubic_lookup(log_nh, log_t, eos%eint_from_T))
510 else
511 eint_nh = saha_eint_from_nh_t(10.0d0**log_nh, 10.0d0**log_t)
512 end if
513 end function eint_nh_from_t
514
515 !>#######################################################################
516 !> Iterative solvers: table-guessed bisection for the WB pressure->eint
517 !> inversion since the method needs high accuracy to not self-seed perturbations.
518 !>#######################################################################
519
520 !> Cached bisection on the log_p table for WB p->eint inversion.
521 !> Precomputes nH-direction indices and table values once, then
522 !> bisects using only cheap PCHIP evaluations with varying tx.
523 !> Expects a narrow initial bracket [lo, hi] (e.g. from p2eint guess).
524 !>
525 !> Adaptive-grid support: when eos%log_p%is_uniform is .false., the
526 !> y- and x-direction index calculations switch to binary search on
527 !> the explicit node arrays. The cached 4x4 stencil mechanism is
528 !> unchanged; only the index-to-cell mapping changes.
529 subroutine log_p_bisect_cached(log_nH, log_p_target, &
530 log_eint_lo, log_eint_hi, max_iter, log_eint_result)
532 double precision, intent(in) :: log_nh, log_p_target
533 double precision, intent(inout) :: log_eint_lo, log_eint_hi
534 integer, intent(in) :: max_iter
535 double precision, intent(out) :: log_eint_result
536
537 integer :: nx, ny, ix, iy, iter, ii
538 integer :: i0, i1, i2, i3, j0, j1, j2, j3
539 double precision :: tx, ty, rx, ry
540 double precision :: xstep_inv, ystep_inv
541 double precision :: vmin_x, vmax_x, vmin_y, vmax_y
542 double precision :: log_eint_mid, log_p_eval
543 logical :: is_unif
544
545 !> Table values: tv(y_row, x_col) for 4 y-rows x 4 x-cols
546 double precision :: tv(4,4)
547 !> Cached ix for detecting grid cell change
548 integer :: ix_cached
549
550 nx = eos%log_p%dim2 ! eint axis (varx)
551 ny = eos%log_p%dim1 ! nH axis (vary)
552 is_unif = eos%log_p%is_uniform
553
554 if (is_unif) then
555 vmin_x = eos%log_p%var2_min
556 vmax_x = eos%log_p%var2_max
557 vmin_y = eos%log_p%var1_min
558 vmax_y = eos%log_p%var1_max
559 xstep_inv = dble(nx-1) / (vmax_x - vmin_x)
560 ystep_inv = dble(ny-1) / (vmax_y - vmin_y)
561 end if
562
563 !> Precompute nH indices (constant across all iterations)
564 if (is_unif) then
565 ry = (log_nh - vmin_y) * ystep_inv
566 ry = max(0.0d0, min(ry, dble(ny-1)))
567 iy = int(ry)
568 ty = ry - dble(iy)
569 else
570 !> Non-uniform: binary search on var1_nodes (length ny = dim1)
571 if (log_nh <= eos%log_p%var1_nodes(1)) then
572 iy = 0; ty = 0.0d0
573 else if (log_nh >= eos%log_p%var1_nodes(ny)) then
574 iy = ny - 2; ty = 1.0d0
575 else
576 ii = find_index_guard(eos%log_p%var1_nodes, ny, log_nh, &
577 eos%log_p%guard_1, eos%log_p%guard_M_1, eos%log_p%guard_scale_1)
578 iy = max(0, min(ii - 2, ny - 2))
579 ty = (log_nh - eos%log_p%var1_nodes(iy+1)) &
580 / (eos%log_p%var1_nodes(iy+2) - eos%log_p%var1_nodes(iy+1))
581 ty = max(0.0d0, min(ty, 1.0d0))
582 end if
583 end if
584
585 !> Clamped y-row indices
586 j0 = max(0, min(ny-1, iy-1))
587 j1 = max(0, min(ny-1, iy ))
588 j2 = max(0, min(ny-1, iy+1))
589 j3 = max(0, min(ny-1, iy+2))
590
591 ix_cached = -1
592
593 do iter = 1, max_iter
594 log_eint_mid = 0.5d0 * (log_eint_lo + log_eint_hi)
595
596 !> Compute eint-direction index
597 if (is_unif) then
598 rx = (log_eint_mid - vmin_x) * xstep_inv
599 rx = max(0.0d0, min(rx, dble(nx-1)))
600 ix = int(rx)
601 tx = rx - dble(ix)
602 else
603 !> Non-uniform: binary search on var2_nodes (length nx = dim2)
604 if (log_eint_mid <= eos%log_p%var2_nodes(1)) then
605 ix = 0; tx = 0.0d0
606 else if (log_eint_mid >= eos%log_p%var2_nodes(nx)) then
607 ix = nx - 2; tx = 1.0d0
608 else
609 ii = find_index_guard(eos%log_p%var2_nodes, nx, log_eint_mid, &
610 eos%log_p%guard_2, eos%log_p%guard_M_2, eos%log_p%guard_scale_2)
611 ix = max(0, min(ii - 2, nx - 2))
612 tx = (log_eint_mid - eos%log_p%var2_nodes(ix+1)) &
613 / (eos%log_p%var2_nodes(ix+2) - eos%log_p%var2_nodes(ix+1))
614 tx = max(0.0d0, min(tx, 1.0d0))
615 end if
616 end if
617
618 !> Load 16 table values only when ix changes
619 if (ix /= ix_cached) then
620 i0 = max(0, min(nx-1, ix-1))
621 i1 = max(0, min(nx-1, ix ))
622 i2 = max(0, min(nx-1, ix+1))
623 i3 = max(0, min(nx-1, ix+2))
624
625 tv(1,1) = eos%log_p%table(j0+1, i0+1)
626 tv(1,2) = eos%log_p%table(j0+1, i1+1)
627 tv(1,3) = eos%log_p%table(j0+1, i2+1)
628 tv(1,4) = eos%log_p%table(j0+1, i3+1)
629 tv(2,1) = eos%log_p%table(j1+1, i0+1)
630 tv(2,2) = eos%log_p%table(j1+1, i1+1)
631 tv(2,3) = eos%log_p%table(j1+1, i2+1)
632 tv(2,4) = eos%log_p%table(j1+1, i3+1)
633 tv(3,1) = eos%log_p%table(j2+1, i0+1)
634 tv(3,2) = eos%log_p%table(j2+1, i1+1)
635 tv(3,3) = eos%log_p%table(j2+1, i2+1)
636 tv(3,4) = eos%log_p%table(j2+1, i3+1)
637 tv(4,1) = eos%log_p%table(j3+1, i0+1)
638 tv(4,2) = eos%log_p%table(j3+1, i1+1)
639 tv(4,3) = eos%log_p%table(j3+1, i2+1)
640 tv(4,4) = eos%log_p%table(j3+1, i3+1)
641
642 ix_cached = ix
643 end if
644
645 !> Evaluate PCHIP: 4 x-rows then 1 y-interp
646 log_p_eval = pchip_2d_from_cache(tv, tx, ty)
647
648 if (log_p_eval < log_p_target) then
649 log_eint_lo = log_eint_mid
650 else
651 log_eint_hi = log_eint_mid
652 end if
653
654 if (dabs(log_eint_hi - log_eint_lo) < 1.0d-14) exit
655 end do
656
657 log_eint_result = 0.5d0 * (log_eint_lo + log_eint_hi)
658
659 contains
660
661 pure double precision function pchip_1d(p0, p1, p2, p3, t) result(v)
662 double precision, intent(in) :: p0, p1, p2, p3, t
663 double precision :: d0, d1, d2, m1, m2, s, a1, a2, lim
664 double precision :: tt, ttt, h00, h10, h01, h11
665
666 d0 = p1 - p0; d1 = p2 - p1; d2 = p3 - p2
667
668 if (d1 == 0.0d0) then
669 m1 = 0.0d0; m2 = 0.0d0
670 else
671 if (d0*d1 <= 0.0d0) then
672 m1 = 0.0d0
673 else
674 m1 = 2.0d0*d0*d1 / (d0 + d1)
675 end if
676 if (d1*d2 <= 0.0d0) then
677 m2 = 0.0d0
678 else
679 m2 = 2.0d0*d1*d2 / (d1 + d2)
680 end if
681 s = sign(1.0d0, d1)
682 a1 = s*m1; a2 = s*m2
683 if (a1 < 0.0d0) a1 = 0.0d0
684 if (a2 < 0.0d0) a2 = 0.0d0
685 lim = 3.0d0*abs(d1)
686 if (a1 > lim) a1 = lim
687 if (a2 > lim) a2 = lim
688 m1 = s*a1; m2 = s*a2
689 end if
690
691 tt = t*t; ttt = tt*t
692 h00 = 2.0d0*ttt - 3.0d0*tt + 1.0d0
693 h10 = ttt - 2.0d0*tt + t
694 h01 = -2.0d0*ttt + 3.0d0*tt
695 h11 = ttt - tt
696 v = h00*p1 + h10*m1 + h01*p2 + h11*m2
697 end function pchip_1d
698
699 pure double precision function pchip_2d_from_cache(c, tx, ty) result(z)
700 double precision, intent(in) :: c(4,4), tx, ty
701 double precision :: g0, g1, g2, g3
702 !> 4 x-direction interpolations (one per y-row)
703 g0 = pchip_1d(c(1,1), c(1,2), c(1,3), c(1,4), tx)
704 g1 = pchip_1d(c(2,1), c(2,2), c(2,3), c(2,4), tx)
705 g2 = pchip_1d(c(3,1), c(3,2), c(3,3), c(3,4), tx)
706 g3 = pchip_1d(c(4,1), c(4,2), c(4,3), c(4,4), tx)
707 !> 1 y-direction interpolation
708 z = pchip_1d(g0, g1, g2, g3, ty)
709 end function pchip_2d_from_cache
710
711 end subroutine log_p_bisect_cached
712
713 !> Given log10(nH) and log10(p), find log10(eint/nH) by table-guessed
714 !> bisection on the forward pressure table. Wraps the bracketing logic
715 !> and log_p_bisect_cached into a single call for use by both HD and MHD
716 !> from_conserved routines.
717 subroutine eint_from_p_bisect(log_nH_val, log_p_val, log_eint_nH_out)
718 double precision, intent(in) :: log_nh_val, log_p_val
719 double precision, intent(out) :: log_eint_nh_out
720
721 double precision :: log_p_target, p2eint_ratio, log_eint_guess
722 double precision :: log_p_at_guess, log_eint_lo, log_eint_hi
723 double precision :: f_bracket, margin
724 double precision :: eint_lim_lo, eint_lim_hi
725 integer :: max_iter
726
727 if (eos%type_id /= eos_type_lte) call mpistop("eint_from_p_bisect called outside its eos_type (LTE)")
728 log_p_target = log_p_val - log_nh_val
729 if (log_p_target > eos%log_p_nH_FI) then
730 log_eint_nh_out = dlog10(eos%inv_gamma_minus_1*10.0d0**log_p_target + eos%eion_per_nH)
731 return
732 end if
733
734 !> eint-axis limits: var2_min/var2_max are set for uniform tables only, so an adaptive
735 !> log_p table takes its end nodes, which keeps the clamps below inside the bicubic stencil
736 if (eos%log_p%is_uniform) then
737 eint_lim_lo = eos%log_p%var2_min
738 eint_lim_hi = eos%log_p%var2_max
739 else
740 eint_lim_lo = eos%log_p%var2_nodes(1)
741 eint_lim_hi = eos%log_p%var2_nodes(eos%log_p%dim2)
742 end if
743
744 ! Initial guess from p2eint table
745 p2eint_ratio = p2eint_from_nh_p(log_nh_val, log_p_target)
746 log_eint_guess = dlog10(p2eint_ratio) + log_p_target
747
748 log_eint_guess = max(log_eint_guess, eint_lim_lo)
749 log_eint_guess = min(log_eint_guess, eint_lim_hi)
750
751 log_p_at_guess = log_p_from_nh_eint(log_nh_val, log_eint_guess)
752
753 ! Establish bracket around the guess
754 margin = 5.0d-4
755 if (log_p_at_guess < log_p_target) then
756 log_eint_lo = log_eint_guess
757 log_eint_hi = min(log_eint_guess + margin, eint_lim_hi)
758 f_bracket = log_p_from_nh_eint(log_nh_val, log_eint_hi) - log_p_target
759 else
760 log_eint_lo = max(log_eint_guess - margin, eint_lim_lo)
761 log_eint_hi = log_eint_guess
762 f_bracket = -(log_p_from_nh_eint(log_nh_val, log_eint_lo) - log_p_target)
763 end if
764
765 if (f_bracket >= 0.0d0) then
766 max_iter = 8
767 else
768 log_eint_lo = eint_lim_lo
769 log_eint_hi = eint_lim_hi
770 max_iter = 20
771 end if
772
773 call log_p_bisect_cached(log_nh_val, log_p_target, &
774 log_eint_lo, log_eint_hi, max_iter, log_eint_nh_out)
775
776 end subroutine eint_from_p_bisect
777
778 !>#######################################################################
779 !> Table-metadata helpers: expose inverse-table grid bounds without
780 !> reaching into EoS table internals.
781 !>#######################################################################
782
783 !> log_nH grid metadata of the (log_nH, log_T) inverse table (eint from T),
784 !> choosing the container by method ('tables'->eint_from_T, 'entropy'->eintT).
785 !> n_nH=0 if no such table (analytic/FI). Lets cooling's build_Y_mod_table
786 !> get its grid without reaching into EoS table internals.
787 subroutine eos_get_eintt_grid(n_nH, lg_nH_min, lg_nH_max)
788 integer, intent(out) :: n_nh
789 double precision, intent(out) :: lg_nh_min, lg_nh_max
790
791 if (eos%type_id /= eos_type_lte) call mpistop("eos_get_eintT_grid called outside its eos_type (LTE)")
792 if (allocated(eos%eint_from_T%table)) then
793 n_nh = eos%eint_from_T%dim1
794 lg_nh_min = eos%eint_from_T%var1_min
795 lg_nh_max = eos%eint_from_T%var1_max
796 else if (allocated(eos%eintT%table)) then
797 n_nh = eos%eintT%dim1
798 lg_nh_min = eos%eintT%var1_min
799 lg_nh_max = eos%eintT%var1_max
800 else
801 n_nh = 0
802 lg_nh_min = 0.0d0
803 lg_nh_max = 0.0d0
804 end if
805 end subroutine eos_get_eintt_grid
806
807 !> LTE pressure from internal energy in one cell: ideal without ionE, else the table floor, the
808 !> FI bypass or p(nH, eint). log_nH_val is read only with ionE
809 double precision function lte_p_from_eint(eint_val,nH_val,log_nH_val,inv_rho) result(pres)
810 double precision, intent(in) :: eint_val, nh_val, log_nh_val, inv_rho
811 double precision :: e_use
812 if (.not. eos%ionE) then
813 pres = eos%gamma_minus_1 * eint_val
814 return
815 end if
816 ! The floor keeps the table logarithms and the Saha solve finite
817 e_use = max(eint_val, nh_val*eos%eint_nH_min, smalldouble)
818 if (e_use*inv_rho > eos%eint_rho_FI_threshold) then
819 pres = eos%gamma_minus_1*(e_use - eos%eion_per_nH*nh_val)
820 else if (eos%method_id == eos_entropy) then
821 pres = entropy_p_from_eint(log_nh_val, dlog10(e_use) - log_nh_val)
822 else
823 pres = nh_val * p_nh_from_eint(log_nh_val, dlog10(e_use) - log_nh_val)
824 end if
825 end function lte_p_from_eint
826
827 !> LTE internal energy from pressure in one cell: FI bypass, else eint(nH, p)
828 double precision function lte_eint_from_p(pres,nH_val,log_nH_val,rho_val) result(eint)
829 double precision, intent(in) :: pres, nh_val, log_nh_val, rho_val
830 double precision :: t_solve, y_solve, eint_nh_solve, log_eint_mid
831 if (.not. eos%ionE) then
832 eint = pres*eos%inv_gamma_minus_1
833 return
834 end if
835 if (pres/rho_val > eos%p_rho_FI_threshold) then
836 eint = pres*eos%inv_gamma_minus_1 + eos%eion_per_nH*nh_val
837 else if (eos%method_id == eos_analytic) then
838 call saha_state_from_nh_p(nh_val, pres, t_solve, y_solve, eint_nh_solve)
839 eint = eint_nh_solve*nh_val
840 else if (eos%p2eint_bisect) then
841 if (eos%method_id == eos_entropy) then
842 call entropy_eint_from_p_bisect(log_nh_val, dlog10(pres) - log_nh_val, log_eint_mid)
843 eint = nh_val*10.0d0**log_eint_mid
844 else
845 call eint_from_p_bisect(log_nh_val, dlog10(pres), log_eint_mid)
846 eint = nh_val*max(10.0d0**log_eint_mid, eos%eint_nH_min)
847 end if
848 else
849 eint = pres*p2eint_from_nh_p(log_nh_val, dlog10(pres) - log_nh_val)
850 end if
851 end function lte_eint_from_p
852
853end module mod_eos_lte
pure double precision function pchip_1d(p0, p1, p2, p3, t)
subroutine, public mpistop(message)
Exit MPI-AMRVAC with an error message.
EoS state container – the single thermodynamic authority for AMRVAC.
integer, parameter, public eos_analytic
integer, parameter, public eos_entropy
type(eos_container), allocatable, public eos
The single EoS state object, allocated in eos_init and shared (read-mostly) across all EoS sub-module...
Interpolation kernels for the EoS tables (pure math; no EoS state).
Entropy-method LTE EoS: every query is ONE bicubic Hermite evaluation.
subroutine, public finalise_entropy_lte()
Entropy-method finalise: shift each loaded table's axes from CGS to code units and run the standard p...
subroutine, public load_entropy_lte()
Forward (log nH, log eint/nH) -> thermodynamic state.
Analytic hydrogen Saha EoS with neutral helium (eos_method == 'analytic').
subroutine, public finalise_analytic_lte()
Analytic-method finalise: there are no tables, only the shared FI-bypass constants.
subroutine, public saha_t_from_nh_eint(nh_code, eint_nh_code, t_out, y_out)
Temperature and ionisation fraction from (nH, eint/nH) in code units, by bisection.
subroutine, public load_analytic_lte()
Analytic-method load: no binary tables (on-the-fly Saha solve); just announce the method.
'state' method (eos_method='state', legacy 'tables') of the LTE EoS family.
subroutine, public finalise_state_lte()
Table-based method finalise: shift the loaded T/neOnH (and ionE-derived) tables to code units,...
subroutine, public load_state_lte()
state arms of the eos_init / eos_finalise dispatchers (mod_eos_lte)
Shared LTE lookup-table infrastructure (build/IO; not on the hot path).
LTE (Saha-table) EoS kernels and finalise for the eos% family.
Definition mod_eos_lte.t:12
double precision function, public lte_eint_from_p(pres, nh_val, log_nh_val, rho_val)
LTE internal energy from pressure in one cell: FI bypass, else eint(nH, p).
double precision function, public p_nh_from_eint(log_nh, log_eint_nh)
p/nH = (1 + He + y) T from (log10 nH, log10 eint/nH)
double precision function, public p2eint_from_nh_p(nh, ponh)
Ratio eint/p from (log10 nH, log10 p/nH).
subroutine, public eos_finalise_lte()
LTE arm of eos_finalise: wire the LTE runtime pointer targets, then dispatch on eos_method to the per...
Definition mod_eos_lte.t:61
double precision function, public eint_nh_from_t(log_nh, log_t)
eint/nH from (log10 nH, log10 T), from the inverse table when one is built and the Saha solve otherwi...
subroutine, public eint_from_p_bisect(log_nh_val, log_p_val, log_eint_nh_out)
Given log10(nH) and log10(p), find log10(eint/nH) by table-guessed bisection on the forward pressure ...
double precision function, public y_from_nh_eint(nh, eint_nh)
Scalar EoS kernels: one cell on (log10 nH, log10 eint/nH) or (log10 nH, log10 p/nH),...
subroutine, public get_temperature_from_eint_fast_lte(w, x, ixil, ixol, res)
subroutine, public eos_get_eintt_grid(n_nh, lg_nh_min, lg_nh_max)
####################################################################### Table-metadata helpers: expos...
double precision function, public lte_gamma1_from_p(pres, rho_val)
Gamma_1 of one cell from (p, rho): the ideal value where fully ionised, else the pressure- indexed ta...
double precision function, public gamma1_from_nh_p(log_nh, log_p_nh)
Gamma_1 from (log10 nH, log10 p/nH): one lookup on the pressure-indexed table, or for the analytic me...
subroutine, public eos_init_lte()
update_eos_LTE, get_Te_LTE, get_temperature_from_eint_LTE and Rfactor_from_LTE are PRIVATE: all bound...
Definition mod_eos_lte.t:49
double precision function, public t_from_nh_eint(nh, eint_nh)
Temperature from (log10 nH, log10 eint/nH).
double precision function, public lte_p_from_eint(eint_val, nh_val, log_nh_val, inv_rho)
LTE pressure from internal energy in one cell: ideal without ionE, else the table floor,...
Shared EoS accessors (EoS-type-agnostic) for the eos% family.
This module contains definitions of global parameters and variables and some generic functions/subrou...
integer, parameter ndim
Number of spatial dimensions for grid variables.
double precision, dimension(:), allocatable, parameter d
A Fortran 90 module for creating 1D and 2D lookup tables. These tables can be used to efficiently int...
pure integer function, public find_index_bsearch(list, val)
Binary search of sorted list for the smallest ix such that list(ix) >= val. On failure,...
This module defines the procedures of a physics module. It contains function pointers for the various...
Definition mod_physics.t:4
procedure(sub_e_to_ei), pointer phys_e_to_ei
Definition mod_physics.t:68
subroutine eos_timer_start()
Open an EoS timing window; inside an OpenMP parallel region the EoS is not timed.
Definition mod_timing.t:45
subroutine eos_timer_stop(acc)
Close an EoS timing window, adding its time to acc when it is the outermost.
Definition mod_timing.t:52
double precision timeeos_tfromei
Definition mod_timing.t:16