Debian Codegraph @

src/blackbody.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- Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22 module blackbody
23 use physcon, only:c,hplanck,kboltz,cm_to_nm
24 use params, only:doub_prec
25 implicit none
26
27 contains
28 !---------------------------------------------------------
29 ! Planck function
30 ! INPUT:
31 ! temp - temperature [K]
32 ! nu - frequency [Hz]
33 ! OUTPUT:
34 ! B_nu - Planck function erg/s/cm^2/Hz/steradian
35 !---------------------------------------------------------
36 real elemental function B_nu(temp,nu)
37 real, intent(in) :: temp,nu
38 real(doub_prec) :: hnu_on_kT,hnu3_on_c2
39
40 hnu_on_kT = hplanck*nu/(kboltz*temp)
41 hnu3_on_c2 = hplanck*nu**3/c**2
42
43 if (hnu_on_kT < 300.) then
44 B_nu = 2.*hnu3_on_c2/(exp(hnu_on_kT) - 1.d0)
45 else
46 B_nu = tiny(0.)
47 endif
48
49 end function B_nu
50
51 !---------------------------------------------------------
52 ! Planck function derivative
53 ! INPUT:
54 ! temp - temperature [K]
55 ! nu - frequency [Hz]
56 ! OUTPUT:
57 ! dB_nu/dT - Planck function derivative w.r.t. T
58 !---------------------------------------------------------
59 real elemental function dBnu_dT(temp,nu)
60 real, intent(in) :: temp,nu
61 real(doub_prec) :: hnu_on_kT,hnu3_on_c2
62 real :: Bnu,expterm
63
64 hnu_on_kT = hplanck*nu/(kboltz*temp)
65 hnu3_on_c2 = hplanck*nu**3/c**2
66
67 Bnu = B_nu(temp,nu)
68 if (hnu_on_kT < 300.) then
69 expterm = exp(hnu_on_kT)
70 dBnu_dT = Bnu*expterm/(expterm - 1.)*hnu_on_kT/temp
71 else
72 dBnu_dT = -tiny(0.)
73 endif
74
75 end function dBnu_dT
76
77 !---------------------------------------------------------
78 ! Wien's displacement law, in frequency
79 !---------------------------------------------------------
80 real function Wien_T_from_nu(nu) result(T)
81 real, intent(in) :: nu
82
83 T = nu/5.88e10
84
85 end function Wien_T_from_nu
86
87 !---------------------------------------------------------
88 ! Wien's displacement law, frequency from temperature
89 !---------------------------------------------------------
90 real function Wien_nu_from_T(T) result(nu)
91 real, intent(in) :: T
92
93 nu = 5.88e10*T
94
95 end function Wien_nu_from_T
96
97 !---------------------------------------------------------
98 ! Convert frequency in Hz to wavelength in nm
99 !---------------------------------------------------------
100 real function nu_to_lam(nu) result(lam)
101 real, intent(in) :: nu
102
103 lam = (c/nu)*cm_to_nm
104
105 end function nu_to_lam
106
107 !---------------------------------------------------------
108 ! Convert frequency in Hz to wavelength in nm
109 !---------------------------------------------------------
110 real function lam_to_nu(lam) result(nu)
111 real, intent(in) :: lam
112
113 nu = (c/lam)*cm_to_nm
114
115 end function lam_to_nu
116
117 !---------------------------------------------------------
118 ! colour temperature, from fitting peak of blackbody
119 !---------------------------------------------------------
120 subroutine get_colour_temperature(spectrum,freq,Tc,freq_max,bb_scale)
121 real, intent(in) :: spectrum(:),freq(:)
122 real, intent(out) :: Tc,freq_max,bb_scale
123 integer :: imax(1),j
124
125 imax = maxloc(spectrum)
126 j = imax(1)
127 freq_max = freq(j)
128 Tc = Wien_T_from_nu(freq_max)
129
130 ! scaling factor by which to shift Blackbody
131 ! in y direction to match the peak flux
132 bb_scale = spectrum(j)/B_nu(Tc,freq_max)
133
134 end subroutine get_colour_temperature
135
136 !--------------------------------------------------------
137 !+
138 ! Fit blackbody to spectrum over a frequency range
139 ! by least squares minimisation
140 !+
141 !--------------------------------------------------------
142 subroutine bb_fit(spec,freq,fmin,fmax,T,bbscale,min,max)
143 real, intent(in) :: fmin,fmax
144 real, intent(in) :: freq(:),spec(:)
145 real, intent(inout) :: T
146 real, intent(out) :: bbscale
147 real, intent(in), optional :: min,max
148 real :: Tmax,Tmin,func
149 integer :: its
150
151 Tmin = 1e3
152 Tmax = 1e6
153 if (present(min)) Tmin = min
154 if (present(max)) Tmax = max
155 print*,'initial min/max = ',Tmin,Tmax,0.5*(Tmin + Tmax)
156
157 ! rootfind, using bisection method
158 its = 0
159 func = huge(func)
160 do while(abs(func) > 1.e-20 .and. its < 100)
161 T = 0.5*(Tmin + Tmax)
162 func = dchisq_dT(spec,freq,fmin,fmax,T,bbscale)
163 if (func > 0.) then
164 Tmax = T
165 else
166 Tmin = T
167 endif
168 its = its + 1
169 !print*,its,func,'T = ',T,' min/max = ',Tmin,Tmax,bbscale
170 enddo
171 print*,its,func,'T = ',T,' min/max = ',Tmin,Tmax,bbscale
172
173 end subroutine bb_fit
174
175 !--------------------------------------------------------
176 !+
177 ! derivative of the chisquared error between the
178 ! input data (spec) and the blackbody spectrum at T
179 ! this is the function which we find the root of
180 !+
181 !--------------------------------------------------------
182 real function dchisq_dT(spec,freq,fmin,fmax,T,bbscale)
183 real, intent(in) :: fmin,fmax
184 real, intent(in) :: freq(:),spec(:)
185 real, intent(in) :: T
186 real, intent(out) :: bbscale
187 integer :: i,iloc(1)
188 real :: err,Bnu
189
190 ! fit bb_scale to match maximum value of the spectrum
191 ! in the specified frequency range
192 iloc = maxloc(spec,mask=(freq > fmin .and. freq < fmax))
193 i = max(iloc(1),1)
194 !print*,'matching at temperature ',i,Wien_T_from_nu(freq(i)),' spec = ', &
195 ! spec(i),' b_nu = ',B_nu(T,freq(i)),' scale=',bbscale
196
197 bbscale = 1.e-5
198 if (B_nu(T,freq(i)) > epsilon(0.)) bbscale = spec(i)/B_nu(T,freq(i))
199 bbscale = max(bbscale,1e-5)
200
201 dchisq_dT = 0.
202 do i=1,size(freq)
203 if (freq(i) > fmin .and. freq(i) < fmax) then
204 Bnu = B_nu(T,freq(i))
205 err = spec(i)/bbscale - Bnu
206 dchisq_dT = dchisq_dT - 2.*err/dBnu_dT(T,freq(i))
207 endif
208 enddo
209
210 end function dchisq_dT
211
212 !--------------------------------------------------------
213 !+
214 ! Function to fill an array with equally log-spaced points
215 !+
216 !--------------------------------------------------------
217 function logspace(n,xmin,xmax) result(x)
218 integer, intent(in) :: n
219 real, intent(in) :: xmin,xmax
220 real, allocatable :: x(:)
221 integer :: i
222 real :: dx
223
224 allocate(x(n))
225 dx = log10(xmax/xmin)/real(n-1)
226 do i=1,n
227 x(i) = log10(xmin) + (i-1)*dx
228 enddo
229
230 x = 10.**x
231
232 end function logspace
233
234 !--------------------------------------------------------
235 !+
236 ! Integrate function on evenly spaced logarithmic grid
237 ! i.e. \int f(x) dx = \int x f(x) d(ln x)
238 ! using trapezoidal rule
239 !+
240 !--------------------------------------------------------
241 real function integrate_log(f,x,xmin,xmax) result(fint)
242 real, intent(in) :: xmin,xmax
243 real, intent(in) :: x(:),f(:)
244 real :: dlogx
245 integer :: n,i
246
247 n = size(f)
248 dlogx = log(xmax/xmin)/(n-1)
249 fint = 0.
250 do i=2,n
251 if (x(i-1) > xmin .and. x(i) < xmax) then
252 fint = fint + 0.5*(f(i)*x(i) + f(i-1)*x(i-1))*dlogx
253 endif
254 enddo
255
256 end function integrate_log
257
258 end module blackbody