MPI-AMRVAC 3.2
The MPI - Adaptive Mesh Refinement - Versatile Advection Code (development version)
Loading...
Searching...
No Matches
mod_eos_lte_saha.t
Go to the documentation of this file.
1!=============================================================================
2!> Analytic hydrogen Saha EoS with neutral helium (eos_method == 'analytic')
3!>
4!> Ground-state Saha for hydrogen, solved per call without tables:
5!> X(T) = saha_pf * T^{3/2} * exp(-chi/kB/T)
6!> y(=ne/nH) = 2X / (X + sqrt(X^2 + 4 nH X))
7!> Public entry points take and return code units; the CGS conversion is internal.
8!> T(nH, eint/nH) is found by bisection and p(nH, T) closes the state.
9!> saha_gamma1_from_nH_p evaluates Gamma_1 in closed form at (nH, p), after a bisection for T, so
10!> that the method uses no table of any kind
11!=============================================================================
15 use mod_comm_lib, only: mpistop
17 implicit none
18 private
19
23
24 ! Prefactor (2 pi m_e k_B/h^2)^{3/2} in CGS (cm^-3)
25 ! SI value is 2.4146830395719654e21 m^-3; divide by 1e6 for CGS
26 double precision, parameter :: saha_pf = 2.4146830395719654d15
27 double precision, parameter :: saha_kB_cgs = 1.380649d-16
28 ! One ionisation energy for the Saha exponent and the energy stored in ionisation
29 double precision, parameter :: saha_chi_H_cgs = const_chi_h * const_ev
30 double precision, parameter :: saha_chi_kB = saha_chi_h_cgs / saha_kb_cgs
31
32 !> One-shot guard: a bisection that exhausts max_iter without meeting the
33 !> tolerance warns once (rank 0) rather than silently returning an
34 !> under-converged T every offending cell.
35 logical :: saha_nonconv_warned = .false.
36
37contains
38
39 !> Analytic-method load: no binary tables (on-the-fly Saha solve); just
40 !> announce the method.
41 subroutine load_analytic_lte()
42 if (mype == 0) then
43 write(*,*) 'EoS method: analytic (hydrogen Saha, helium neutral)'
44 write(*,*) ' gamma1_method: ', trim(eos%gamma1_method)
45 end if
46 end subroutine load_analytic_lte
47
48 !> Analytic-method finalise: there are no tables, only the shared FI-bypass constants
50 call precompute_fi_bypass_constants(saha_chi_h_cgs)
51 end subroutine finalise_analytic_lte
52
53 !> Ionization fraction from Saha in CGS (no unit conversions).
54 !> Use this inside bisection loops where nH_cgs is constant.
55 double precision function saha_y_cgs(nH_cgs, T_cgs) result(y)
56 double precision, intent(in) :: nh_cgs, t_cgs
57 double precision :: sqx
58
59 ! The quadratic root in the form y = 2 sqrt(X)/(sqrt(X) + sqrt(X + 4 nH)), which avoids the
60 ! cancellation of -X + sqrt(X^2 + 4 nH X) as X grows and, unlike 2X/(X + sqrt(X^2 + 4 nH X)),
61 ! does not become 0/0 when X underflows in cold gas. sqrt(X) is evaluated directly, which
62 ! halves the exponent and moves the underflow from about 210 K to about 105 K (about 220 K
63 ! and 110 K where subnormals are flushed to zero), below which y returns 0
64 ! sqrt(X) = sqrt(saha_pf) T**(3/4) exp(-chi_H/(2 kB T))
65 sqx = dsqrt(saha_pf) * t_cgs**0.75d0 * dexp(-0.5d0 * saha_chi_kb / t_cgs)
66 y = 2.0d0 * sqx / (sqx + dsqrt(sqx * sqx + 4.0d0 * nh_cgs))
67
68 end function saha_y_cgs
69
70 !> Ionisation fraction y = ne/nH from Saha at (nH, T) in code units
71 double precision function saha_y_from_nh_t(nH_code, T_code) result(y)
72 double precision, intent(in) :: nh_code, t_code
73
74 y = saha_y_cgs(nh_code * unit_numberdensity, t_code * unit_temperature)
75
76 end function saha_y_from_nh_t
77
78 !> eint/nH in CGS with helium neutral, 1.5 (1+He+y) kB T, plus y chi_H when ionE
79 double precision function saha_eint_cgs(y, T_cgs) result(e)
80 double precision, intent(in) :: y, t_cgs
81 e = 1.5d0 * (1.0d0 + eos%He_abundance + y) * saha_kb_cgs * t_cgs
82 if (eos%ionE) e = e + y * saha_chi_h_cgs
83 end function saha_eint_cgs
84
85 !> eint/nH from Saha at (nH, T), in code units
86 double precision function saha_eint_from_nh_t(nH_code, T_code) result(eint_nH_code)
87 double precision, intent(in) :: nh_code, t_code
88
89 if (eos%method_id /= eos_analytic) call mpistop("saha_eint_from_nH_T requires eos_method=analytic")
90 eint_nh_code = saha_eint_cgs(saha_y_from_nh_t(nh_code, t_code), t_code * unit_temperature) &
92
93 end function saha_eint_from_nh_t
94
95 !> Given (nH, p) in code units, T, y and optionally eint/nH: bisection on
96 !> p = nH*(1+He+y(T))*T, with kB absorbed in the code units
97 subroutine saha_state_from_nh_p(nH_code, p_code, T_out, y_out, eint_nH_out)
98 double precision, intent(in) :: nh_code, p_code
99 double precision, intent(out) :: t_out, y_out
100 double precision, intent(out), optional :: eint_nh_out
101
102 double precision :: nh_cgs, ut
103 double precision :: t_lo, t_hi, t_mid, y_mid, p_eval
104 integer :: iter
105 integer, parameter :: max_iter = 30
106 double precision, parameter :: tol = 1.0d-8
107
108 if (eos%method_id /= eos_analytic) call mpistop("saha_state_from_nH_p requires eos_method=analytic")
109 nh_cgs = nh_code * unit_numberdensity
111
112 ! brackets: y = 0 gives T = p/(nH (1+He)), y = 1 gives T = p/(nH (2+He))
113 t_hi = p_code / (nh_code * (1.0d0 + eos%He_abundance))
114 t_lo = p_code / (nh_code * (2.0d0 + eos%He_abundance))
115 t_lo = max(t_lo, 100.0d0 / ut)
116
117 do iter = 1, max_iter
118 t_mid = 0.5d0 * (t_lo + t_hi)
119 y_mid = saha_y_cgs(nh_cgs, t_mid * ut)
120 p_eval = nh_code * (1.0d0 + eos%He_abundance + y_mid) * t_mid
121
122 if (p_eval < p_code) then
123 t_lo = t_mid
124 else
125 t_hi = t_mid
126 end if
127
128 if (dabs(t_hi - t_lo) < tol * t_mid) exit
129 end do
130
131 if (iter > max_iter .and. .not. saha_nonconv_warned .and. mype == 0) then
132 saha_nonconv_warned = .true.
133 write(*,*) "WARNING: saha_state_from_nH_p bisection reached max_iter ", &
134 "without converging; using last iterate. Not reported again."
135 end if
136
137 t_out = t_mid
138 y_out = y_mid
139
140 if (present(eint_nh_out)) &
141 eint_nh_out = saha_eint_cgs(y_mid, t_mid * ut) * unit_numberdensity / unit_pressure
142
143 end subroutine saha_state_from_nh_p
144
145 !> Temperature and ionisation fraction from (nH, eint/nH) in code units, by bisection
146 subroutine saha_t_from_nh_eint(nH_code, eint_nH_code, T_out, y_out)
147 double precision, intent(in) :: nh_code, eint_nh_code
148 double precision, intent(out) :: t_out, y_out
149
150 if (eos%method_id /= eos_analytic) call mpistop("saha_T_from_nH_eint requires eos_method=analytic")
151 call saha_t_bisection(nh_code, eint_nh_code, t_out, y_out)
152
153 end subroutine saha_t_from_nh_eint
154
155 !> Bisection for T(nH, eint/nH) between the fully ionised (y = 1) and neutral (y = 0) limits
156 subroutine saha_t_bisection(nH_code, eint_nH_code, T_out, y_out)
157 double precision, intent(in) :: nh_code, eint_nh_code
158 double precision, intent(out) :: t_out, y_out
159
160 double precision :: nh_cgs, eint_nh_cgs, t_lo, t_hi, t_mid
161 double precision :: y_mid, cv_ion, cv_neu
162 integer :: iter
163 integer, parameter :: max_iter = 30
164 double precision, parameter :: tol = 1.0d-8
165
166 nh_cgs = nh_code * unit_numberdensity
167 eint_nh_cgs = eint_nh_code * unit_pressure / unit_numberdensity
168
169 ! temperature bounds from the fully ionised (y = 1) and neutral (y = 0) limits
170 cv_ion = 1.5d0 * (2.0d0 + eos%He_abundance) * saha_kb_cgs
171 cv_neu = 1.5d0 * (1.0d0 + eos%He_abundance) * saha_kb_cgs
172 if (eos%ionE) then
173 t_lo = (eint_nh_cgs - saha_chi_h_cgs) / cv_ion
174 else
175 t_lo = eint_nh_cgs / cv_ion
176 end if
177 t_lo = max(t_lo, 100.0d0)
178 t_hi = max(eint_nh_cgs / cv_neu, t_lo + 1.0d0)
179
180 ! eint(T) is monotonic, so the bracket always holds; where hydrogen is already fully
181 ! ionised at T_lo (to 1e-10), T_lo = (eint - chi_H)/cv_ion is the solution
182 if (eos%ionE) then
183 if (saha_y_cgs(nh_cgs, t_lo) > 1.0d0 - 1.0d-10) then
184 t_out = t_lo / unit_temperature
185 y_out = 1.0d0
186 return
187 end if
188 end if
189
190 ! Bisect in CGS
191 do iter = 1, max_iter
192 t_mid = 0.5d0 * (t_lo + t_hi)
193 y_mid = saha_y_cgs(nh_cgs, t_mid)
194 if (saha_eint_cgs(y_mid, t_mid) < eint_nh_cgs) then
195 t_lo = t_mid
196 else
197 t_hi = t_mid
198 end if
199
200 if (dabs(t_hi - t_lo) < tol * t_mid) exit
201 end do
202
203 if (iter > max_iter .and. .not. saha_nonconv_warned .and. mype == 0) then
204 saha_nonconv_warned = .true.
205 write(*,*) "WARNING: saha_T_bisection reached max_iter without ", &
206 "converging; using last iterate. Not reported again."
207 end if
208
209 t_out = t_mid / unit_temperature
210 y_out = y_mid
211
212 end subroutine saha_t_bisection
213
214 !> Gamma_1 at (nH, p) in code units, in closed form from the hydrogen Saha balance. T and y come
215 !> from the bisection at that pressure, and with N = 1+He+y and theta = chi_H/kT
216 !> dln y/dln T = (1-y)/(2-y) (3/2 + theta), chi_T = 1 + y dln y/dln T / N,
217 !> chi_rho = 1 - y (1-y)/((2-y) N), c_v/kB = 3/2 N + (3/2 + theta) y dln y/dln T,
218 !> Gamma_1 = chi_rho + chi_T**2 N kB/c_v,
219 !> where the theta term of c_v is the heat taken up by ionisation. It is linked only with ionE,
220 !> since without the ionisation energy p = (gamma-1) eint and the sound speed is the ideal one
221 double precision function saha_gamma1_from_nh_p(nH_code, p_code) result(g1)
222 double precision, intent(in) :: nh_code, p_code
223 double precision :: t_code, y, theta, dlny, npart, cv, chi_t, chi_rho
224 call saha_state_from_nh_p(nh_code, p_code, t_code, y)
225 theta = saha_chi_kb / (t_code * unit_temperature)
226 dlny = (1.0d0 - y) / (2.0d0 - y) * (1.5d0 + theta)
227 npart = 1.0d0 + eos%He_abundance + y
228 cv = 1.5d0 * npart + (1.5d0 + theta) * y * dlny
229 chi_t = 1.0d0 + y * dlny / npart
230 chi_rho = 1.0d0 - y * (1.0d0 - y) / ((2.0d0 - y) * npart)
231 g1 = chi_rho + chi_t**2 * npart / cv
232 end function saha_gamma1_from_nh_p
233
234end module mod_eos_lte_saha
235!> Needs a line after to pass the preprocessor
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
type(eos_container), allocatable, public eos
The single EoS state object, allocated in eos_init and shared (read-mostly) across all EoS sub-module...
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.
double precision function, public saha_gamma1_from_nh_p(nh_code, p_code)
Gamma_1 at (nH, p) in code units, in closed form from the hydrogen Saha balance. T and y come from th...
double precision function, public saha_eint_from_nh_t(nh_code, t_code)
eint/nH from Saha at (nH, T), in code units
subroutine, public saha_state_from_nh_p(nh_code, p_code, t_out, y_out, eint_nh_out)
Given (nH, p) in code units, T, y and optionally eint/nH: bisection on p = nH*(1+He+y(T))*T,...
Shared LTE lookup-table infrastructure (build/IO; not on the hot path).
subroutine, public precompute_fi_bypass_constants(chi_h_only)
Precompute constants for bypassing table lookups when gas is fully ionised. Physics: above T ~ 50 kK,...
This module contains definitions of global parameters and variables and some generic functions/subrou...
double precision unit_numberdensity
Physical scaling factor for number density.
double precision unit_pressure
Physical scaling factor for pressure.
integer mype
The rank of the current MPI task.
double precision, dimension(:), allocatable, parameter d
double precision unit_temperature
Physical scaling factor for temperature.