Debian Codegraph @

src/lightcurve_utils.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2021-2022 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22 module lightcurve_utils
23 use params, only:doub_prec
24 implicit none
25
26 public :: get_temp_from_u,ionisation_fraction,ionisation_fraction_Honly,get_opacity
27
28 private
29
30 contains
31 !---------------------------------------------------------
32 ! routine to to compute temperature from
33 ! internal energy assuming a mix of gas and radiation
34 ! pressure, where Trad = Tgas. That is, we solve the
35 ! quartic equation
36 !
37 ! a*T^4 + 3/2*rho*kb*T/mu = rho*u
38 !
39 ! to determine the temperature from the supplied density
40 ! and internal energy (rho, u).
41 ! INPUT:
42 ! rho - density [g/cm^3]
43 ! u - internal energy [erg/g]
44 ! OUTPUT:
45 ! temp - temperature [K]
46 !---------------------------------------------------------
47 real elemental function get_temp_from_u(rho,u) result(temp)
48 use physcon, only:kb_on_mh,radconst
49 real(doub_prec), intent(in) :: rho,u
50 real(doub_prec) :: ft,dft,dt
51 real(doub_prec), parameter :: tol = 1.e-8
52 real(doub_prec), parameter :: mu = 0.6
53 integer :: its
54
55 ! Take minimum of gas and radiation temperatures as initial guess
56 temp = min(u*mu/(1.5*kb_on_mh),(u*rho/radconst)**0.25)
57
58 dt = huge(0.)
59 its = 0
60 do while (abs(dt) > tol*temp .and. its < 500)
61 its = its + 1
62 ft = u*rho - 1.5*kb_on_mh*temp*rho/mu - radconst*temp**4
63 dft = - 1.5*kb_on_mh*rho/mu - 4.*radconst*temp**3
64 dt = ft/dft ! Newton-Raphson
65 if (temp - dt > 1.2*temp) then
66 temp = 1.2*temp
67 elseif (temp - dt < 0.8*temp) then
68 temp = 0.8*temp
69 else
70 temp = temp - dt
71 endif
72 enddo
73
74 end function get_temp_from_u
75
76 !---------------------------------------------------------
77 ! routine to return simple gas opacities
78 ! from electron scattering and free-free absorption
79 !
80 ! INPUT:
81 ! rho - density [g/cm^3]
82 ! T - temperature [K]
83 ! OUTPUT:
84 ! kappa - cm^2/g
85 !---------------------------------------------------------
86
87 real elemental function get_opacity(rho,T,X,Y,use_all) result(kappa)
88 use physcon, only:sigma_e
89 real(doub_prec), intent(in) :: rho,T,X,Y
90 real(doub_prec) :: kappa_K,kappa_es,kappa_H,kappa_mol,kappa_abs
91 real(doub_prec) :: Z
92 real(doub_prec) :: xfrac,ne,xh0,xh1,xhe0,xhe1,xhe2
93 logical, intent(in) :: use_all
94
95 call ionisation_fraction_Honly(rho,T,xfrac,ne)
96 !call ionisation_fraction(rho,T,X,Y,xh0,xh1,xhe0,xhe1,xhe2,ne)
97
98 Z = max(1. - X - Y,0.) ! metallicity
99
100 ! opacity due to Kramer's law (free-free and bound-free transitions)
101 !kappa_K = 4e25*Z*(1.+X)*rho*T**(-3.5) ! Metzger & Pejcha (2017)
102 kappa_K = 1.2e26*Z*(1.+X)*rho*T**(-3.5) ! from Matsumoto & Metzger (2022)
103
104 ! opacity due to negative Hydrogen
105 kappa_H = 1.1e-25*sqrt(Z)*sqrt(rho)*T**7.7
106
107 ! opacity due to molecules
108 kappa_mol = 0.1*Z
109
110 ! electron scattering
111 kappa_es = sigma_e*ne/rho ! 0.2*(1 + X) if fully ionised
112
113 if (use_all) then
114 ! total opacity, valid between T = 1.5 x 10^3 and 10^9 K
115 !kappa = kappa_mol + 1./(1./kappa_H + 1./(kappa_es + kappa_K)) ! Metzger & Pejcha (2017)
116 kappa = kappa_mol + kappa_es + 1./(1./kappa_H + 1./kappa_K) ! MM22
117 else
118 kappa = kappa_es
119 endif
120
121 ! absorption opacity
122 !kappa_abs = kappa_ff + kappa_H !+ kappa_ff
123 !kappa = sqrt((kappa_ff + kappa_es + kappa_H)*kappa_abs)
124 !kappa = sqrt(kappa*(kappa_abs))
125
126 end function get_opacity
127
128 !----------------------------------------------------------------
129 !+
130 ! Solves three Saha equations simultaneously to return ion
131 ! fractions of hydrogen and helium. Assumes inputs in cgs units
132 !+
133 !----------------------------------------------------------------
134 pure subroutine ionisation_fraction(dens,temp,X,Y,xh0,xh1,xhe0,xhe1,xhe2,ne)
135 use vectorutils, only:matrixinvert3D
136 use physcon, only:pi,kboltz,hplanck,mh
137 real, intent(in) :: dens,temp,X,Y
138 real, intent(out):: xh0,xh1,xhe0,xhe1,xhe2,ne
139 real :: n,nh,nhe,A,B,C,const,xh1g,xhe1g,xhe2g,f,g,h
140 real, dimension(3,3) :: M,M_inv
141 real, dimension(3) :: dx
142 integer :: i,ierr
143 real, parameter :: twopi=2.*pi,eV=1.60219d-12,mass_electron_cgs=9.10938291d-28,&
144 chih0=13.6,chihe0=24.6,chihe1=54.4
145
146 nh = X * dens / mh
147 nhe = Y * dens / (4. * mh)
148 n = nh + nhe
149
150 const = (sqrt(twopi * mass_electron_cgs * kboltz) / hplanck)**3 / n
151
152 A = 1. * const * temp**(1.5) * exp(-chih0 * eV / (kboltz * temp))
153 B = 4. * const * temp**(1.5) * exp(-chihe0 * eV / (kboltz * temp))
154 C = 1. * const * temp**(1.5) * exp(-chihe1 * eV / (kboltz * temp))
155
156 xh1g = 0.4
157 xhe1g = 0.3
158 xhe2g = 0.2
159
160 do i=1,50
161 f = xh1g * (xh1g + xhe1g + 2*xhe2g) - A * ((nh/n) - xh1g)
162 g = xhe1g * (xh1g + xhe1g + 2*xhe2g) - B * ((nhe/n) - xhe1g - xhe2g)
163 h = xhe2g * (xh1g + xhe1g + 2*xhe2g) - C * xhe1g
164
165 M(1,:) = (/ 2*xh1g + xhe1g + 2*xhe2g + A, xh1g, 2*xh1g /)
166 M(2,:) = (/ xhe1g, xh1g + 2*xhe1g + 2*xhe2g + B, 2*xhe1g + B /)
167 M(3,:) = (/ xhe2g, xhe2g - C, xh1g + xhe1g + 4*xhe2g /)
168
169 call matrixinvert3D(M,M_inv,ierr)
170 dx = matmul(M_inv, (/ -f, -g, -h/))
171
172 xh1g = xh1g + dx(1)
173 xhe1g = xhe1g + dx(2)
174 xhe2g = xhe2g + dx(3)
175 enddo
176
177 xh1 = xh1g * n / nh
178 xhe1 = xhe1g * n / nhe
179 xhe2 = xhe2g * n / nhe
180 xh0 = ((nh/n) - xh1g) * n / nh
181 xhe0 = ((nhe/n) - xhe1g - xhe2g) * n / nhe
182
183 ne = xh1*nh + xhe1*nhe + xhe2*nhe
184
185 end subroutine ionisation_fraction
186
187 !----------------------------------------------------------------
188 !+
189 ! Same as above but for H-only, can be solved analytically
190 ! without any matrix inversion or iterations.
191 ! Assumes inputs in cgs units.
192 !+
193 !----------------------------------------------------------------
194 pure subroutine ionisation_fraction_Honly(dens,temp,xh1,ne)
195 use physcon, only:pi,kboltz,hplanck,mh
196 real(doub_prec), intent(in) :: dens,temp
197 real(doub_prec), intent(out):: xh1,ne
198 real(doub_prec) :: n,nh,nhe,A,const
199 real(doub_prec), parameter :: twopi=2.*pi,eV=1.60219d-12,mass_electron_cgs=9.10938291d-28,&
200 chih0=13.6
201
202 nh = dens / mh
203 n = nh
204
205 const = (sqrt(twopi * mass_electron_cgs * kboltz) / hplanck)**3 / n
206 A = const * temp**(1.5) * exp(-chih0 * eV / (kboltz * temp))
207
208 ! solve quadratic equation x^2/(1-x) = A, i.e. x^2 + Ax - A = 0
209 xh1 = 0.5*(-A + sqrt(A**2 + 4.*A))
210
211 ! electron density
212 ne = xh1*n
213
214 end subroutine ionisation_fraction_Honly
215
216 end module lightcurve_utils