Debian Codegraph @

src/lightcurve.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 lightcurve
23 use params, only:int1,doub_prec
24 implicit none
25
26 public :: get_lightcurve
27
28 private
29
30 contains
31
32 !---------------------------------------------------------
33 ! routine to to compute luminosity, effective temperature
34 ! effective blackbody radius, Reff, vs time from
35 ! SPH particle data
36 !
37 ! We solve the equation of radiative transfer along a
38 ! ray for each pixel in the image, performing the ray
39 ! trace through particles assuming grey blackbody
40 ! emission (i.e. each particle emits sigma*T^4)
41 !
42 ! We then compute the emitting area (area of optically
43 ! thick material) and the effective temperature, putting
44 ! these together to give a total luminosity
45 !
46 ! Used to generate synthetic lightcurves
47 !---------------------------------------------------------
48 subroutine get_lightcurve(ncolumns,dat,npartoftype,masstype,itype,ndim,ntypes,&
49 lum,rphoto,temp,lum_bb,r_bb,Tc,badfrac,specfile,ierr)
50 use labels, only:ix,ih,irho,ipmass,itemp,ikappa,ivx,ipmomx
51 use limits, only:lim,get_particle_subset
52 use lightcurve_utils, only:get_temp_from_u
53 use interpolate3D_opacity, only:interp3D_proj_opacity
54 use particle_data, only:icolourme
55 use interpolation, only:get_n_interp,set_interpolation_weights
56 use settings_data, only:iRescale,iverbose,required,UseTypeInRenderings,ndimV
57 use settings_part, only:iplotpartoftype
58 use settings_render, only:npix,inormalise=>inormalise_interpolations,&
59 idensityweightedinterpolation,exact_rendering
60 use settings_units, only:units,unit_interp
61 use physcon, only:steboltz,pi,au,rsun=>solarrcgs,Lsun,c,cm_to_nm
62 use write_pixmap, only:write_pixmap_ascii
63 use filenames, only:tagline
64 use blackbody, only:B_nu,logspace,Wien_nu_from_T,nu_to_lam,&
65 integrate_log,get_colour_temperature
66 use settings_xsecrot, only:anglex,angley,anglez,taupartdepth
67 use rotation, only:rotate_particles
68 use system_utils, only:get_command_flag,renvironment
69 use readwrite_fits, only:write_fits_cube,write_fits_image,write_fits_image
70 integer, intent(in) :: ncolumns,ntypes,ndim
71 integer, intent(in) :: npartoftype(:)
72 integer(kind=int1), intent(in) :: itype(:)
73 real, intent(in) :: masstype(:)
74 real, intent(in) :: dat(:,:)
75 real, intent(out) :: lum,rphoto,temp,lum_bb,r_bb,Tc,badfrac
76 integer, intent(out) :: ierr
77 character(len=*), intent(in) :: specfile
78 integer :: n,isinktype,npixx,npixy,j,i,nfreq
79 integer, parameter :: iu1 = 45
80 real, dimension(3) :: xmin,xmax
81 real, dimension(:), allocatable :: weight,x,y,z,flux,opacity
82 real, dimension(:), allocatable :: freq,spectrum,bb_spectrum
83 real, dimension(:,:), allocatable :: img,taupix,flux_nu,v_on_c,badpix
84 real, dimension(:,:,:), allocatable :: img_nu,img_tmp
85 real :: zobs,dzobs,dx,dy,area,freqmin,freqmax,lam_max,freq_max,bb_scale,opacity_factor,f_col
86 real :: betaz,lorentz,doppler_factor,doppler_factor_max,tempi,badarea
87 logical :: relativistic
88
89 lum = 0.
90 rphoto = 0.
91 temp = 0.
92 ierr = 0
93 if (ndim /= 3) then
94 print "(a)",' ERROR: lightcurve only works with 3 dimensional data'
95 ierr = 1
96 return
97 endif
98 if (.not.(ih > 0 .and. ipmass > 0 .and. irho > 0 .and. itemp > 0)) then
99 if (ih <= 0) print "(a)",' ERROR: could not locate smoothing length in data'
100 if (ipmass <= 0) print "(a)",' ERROR: could not locate particle mass in data'
101 if (irho <= 0) print "(a)",' ERROR: could not locate density in data'
102 if (itemp <= 0) print "(a)",' ERROR: could not locate temperature in data'
103 ierr = 2
104 return
105 endif
106 !
107 !--allow for reduction in opacity by factor of v/c for photon trapping
108 !
109 relativistic = .not.get_command_flag('nonrel') .and. (ivx > 0 .and. ndimV >= 3 .and. ipmomx > 0)
110 if (relativistic) print "(a)",' Relativistic corrections ON (use --nonrel to disable)'
111
112 !--allow for colour correction to temperature by factor of f_col
113 f_col = renvironment('fcol', -1.0)
114 if (f_col < 0.0) f_col = renvironment('f_col', 1.0)
115 print "(/,a,f4.2,/,a,/)",' SPECTRAL HARDENING FACTOR f_col = ',f_col, &
116 ' (use --fcol=1.7 for radiation-pressure dominated flows; Shimura & Takahara 1995)'
117 xmin(1:ndim) = lim(ix(1:ndim),1)
118 xmax(1:ndim) = lim(ix(1:ndim),2)
119 !
120 !--set number of particles to use in the interpolation routines
121 ! and allocate memory for weights
122 !
123 n = get_n_interp(ntypes,npartoftype,UseTypeInRenderings,iplotpartoftype,size(itype),.false.)
124 allocate(weight(n),x(n),y(n),z(n),flux(n),opacity(n),stat=ierr)
125 if (ierr /= 0) then
126 print*,' ERROR allocating memory for interpolation weights, aborting...'
127 ierr = 3
128 return
129 endif
130 x(1:n) = dat(1:n,ix(1))
131 y(1:n) = dat(1:n,ix(2))
132 z(1:n) = dat(1:n,ix(3))
133
134 if (relativistic) then
135 allocate(v_on_c(3,n),stat=ierr)
136 do i=1,3
137 v_on_c(i,:) = dat(1:n,ivx+i-1)/c ! velocity in units of speed of light
138 enddo
139 endif
140 if (allocated(v_on_c)) then
141 call rotate_particles(n,x,y,z,anglex,angley,anglez,v=v_on_c)
142 else
143 call rotate_particles(n,x,y,z,anglex,angley,anglez)
144 endif
145
146 !
147 !--set number of pixels and pixel scale in each direction
148 !
149 npixx = npix
150 if (npixx < 8) npixx = 1024
151 dx = (xmax(1)-xmin(1))/npixx
152 npixy = int((xmax(2)-xmin(2) - 0.5*dx)/dx) + 1
153 dy = (xmax(2)-xmin(2))/npixy
154 print "(a,i0,a,i0,a)",' Using ',npixx,' x ',npixy,' pixels'
155 print "(2(1x,a,es10.3,'->',es10.3,a,/))",'x = [',xmin(1),xmax(1),']','y = [',xmin(2),xmax(2),']'
156 !
157 !--allocate memory for image
158 !
159 if (allocated(img) .or. allocated(taupix)) deallocate(img,taupix)
160 allocate(img(npixx,npixy),taupix(npixx,npixy),badpix(npixx,npixy))
161 !
162 !--set interpolation weights (w = m/(rho*h^ndim)
163 !
164 isinktype = 0 !get_sink_type(ntypes)
165 call set_interpolation_weights(weight,dat,itype,(iplotpartoftype .and. UseTypeInRenderings),&
166 n,npartoftype,masstype,ntypes,ncolumns,irho,ipmass,ih,ndim,iRescale,&
167 idensityweightedinterpolation,inormalise,units,unit_interp,required,.false.,isinktype)
168 !
169 !--set default mask and apply range restrictions to data
170 !
171 icolourme(:) = 1
172 call get_particle_subset(icolourme,dat,ncolumns)
173
174 !
175 ! specify opacity
176 !
177 if (ikappa > 0) then
178 opacity = dat(1:n,ikappa)
179 if (abs(taupartdepth - 1.0) > tiny(0.) .and. taupartdepth > 0.) then
180 print "(a,es10.2)",' opacity = opacity x ',taupartdepth
181 opacity = opacity*taupartdepth
182 endif
183 else
184 opacity = 0.35
185 if (taupartdepth > 0.) opacity = taupartdepth
186 print "(a,1pg10.2,a)",' WARNING: using fixed opacity kappa = ',maxval(opacity),' cm^2/g for lightcurve'
187 endif
188 !
189 ! specify source function for each particle
190 !
191 flux = steboltz*dat(1:n,itemp)**4 ! grey version
192
193 ! frequency-dependent version
194 nfreq = 128
195 freqmin = 1e8
196 freqmax = 1e22
197 freq = logspace(nfreq,freqmin,freqmax) ! frequency grid in Hz
198 if (allocated(flux_nu)) deallocate(flux_nu)
199 allocate(flux_nu(nfreq,n))
200 doppler_factor = 1.
201 opacity_factor = 1.
202 doppler_factor_max = 0.
203 do i=1,n
204 if (relativistic) then
205 betaz = 1. + v_on_c(3,i)
206 lorentz = 1./sqrt(1. - dot_product(v_on_c(:,i),v_on_c(:,i)))
207 doppler_factor = betaz*lorentz ! nu / nu_0, Lorentz transform
208 opacity_factor = lorentz*(1. - v_on_c(3,i)) ! nu_0 / nu, inverse Lorentz transform
209 doppler_factor_max = max(doppler_factor,doppler_factor_max)
210 endif
211 opacity(i) = opacity(i)*opacity_factor
212 tempi = dat(i,itemp)*f_col
213 !call get_opacity_nongrey(nfreq,freq,dat(i,temp),dat(i,rho),opacity_nu(:,i))
214 flux_nu(:,i) = B_nu(tempi,freq*doppler_factor)
215 enddo
216 if (relativistic) print*,' max relativistic correction=',doppler_factor_max
217
218 if (allocated(img_nu)) deallocate(img_nu)
219 allocate(img_nu(nfreq,npixx,npixy))
220 !
221 ! raytrace SPH data to 2D image to get flux
222 !
223 zobs = huge(zobs) ! no 3D perspective
224 dzobs = 0.
225 call interp3D_proj_opacity(x,y,z,&
226 dat(1:n,ipmass),n,dat(1:n,ih),weight, &
227 flux,z,icolourme(1:n), &
228 n,xmin(1),xmin(2),img,taupix,npixx,npixy,&
229 dx,dy,zobs,dzobs,opacity,huge(zobs),iverbose,.false.,datv=flux_nu,datvpix=img_nu,badpix=badpix)
230
231 ! integrate flux over all frequencies to give Flux = \int F_\nu d\nu = pi \int B_nu dnu
232 do j=1,npixy
233 do i=1,npixx
234 img(i,j) = pi*integrate_log(img_nu(1:nfreq,i,j),freq,freqmin,freqmax)
235 enddo
236 enddo
237
238 lum = 4.*sum(img)*dx*dy
239
240 ! luminosity is integrated flux
241 print "(/,a,2(es10.3,a))",' L_bol = ',lum,' erg/s = ',lum/Lsun,' L_sun'
242
243 area = count(taupix >= 1.)*dx*dy
244 badarea = count(badpix > 0.)*dx*dy
245 badfrac = 0.
246 if (area > 0.) badfrac = badarea/area
247 print "(a,1pg10.3,a)",' emitting area = ',area/au**2,' au^2 (pixels where dtau > 1/3)'
248 print "(a,1pg10.3,a,2pf6.2,a)",' unresolved area = ',badarea/au**2,' au^2 (',badfrac,'%)'
249 if (badarea/area > 0.05) print "(/,1x,a,2pf6.2,a)",'WARNING: ',badfrac,'% of photosphere is UNRESOLVED!'
250
251 print "(/,a,1pg10.3,a)",' Tmax = ',(maxval(img)/steboltz)**0.25,' K'
252
253 ! effective temperature: total flux equals that of a blackbody at T=Teff
254 temp = (lum/area/(4.*steboltz))**0.25
255 freq_max = Wien_nu_from_T(temp)
256 lam_max = c/freq_max*cm_to_nm
257 print "(a,3(1pg10.3,a))",' Teff = ',temp,' K: Blackbody peak at ',freq_max,' Hz / ',lam_max,' nm'
258
259 ! get integrated spectrum from integrating over all pixels in the image
260 if (allocated(spectrum)) deallocate(spectrum,bb_spectrum)
261 allocate(spectrum(nfreq),bb_spectrum(nfreq))
262 !$omp parallel do default(none) private(i) &
263 !$omp shared(spectrum,img_nu,npixx,npixy,nfreq,dx,dy)
264 do i=1,nfreq
265 spectrum(i) = sum(img_nu(i,1:npixx,1:npixy))*dx*dy
266 enddo
267 !$omp end parallel do
268
269 ! get colour temperature by fitting the blackbody peak
270 call get_colour_temperature(spectrum,freq,Tc,freq_max,bb_scale)
271 print "(a,3(1pg10.3,a))",' Tc = ',Tc,' K: Blackbody peak at ',freq_max,' Hz / ',nu_to_lam(freq_max),' nm'
272
273 ! effective photospheric radius, using Teff
274 rphoto = sqrt(lum/(4.*pi*steboltz*temp**4))
275 print "(a,2(es10.3,a))",' R_eff = ',rphoto/au,' au = ',rphoto/rsun,' rsun'
276
277 ! L_bb and effective photospheric radius, using blackbody at T=Tc
278 bb_spectrum = B_nu(Tc,freq(:))*bb_scale
279 lum_bb = 4.*pi*integrate_log(bb_spectrum(1:nfreq),freq,freqmin,freqmax)
280 print "(a,2(es10.3,a))",' L_bb = ',lum_bb
281 r_bb = sqrt(lum_bb/(4.*pi*steboltz*Tc**4))
282 print "(a,2(es10.3,a))",' R_bb = ',r_bb/au,' au = ',r_bb/rsun,' rsun'
283
284 print "(a)",' WRITING '//trim(specfile)//'.spec'
285 open(unit=iu1,file=trim(specfile)//'.spec',status='replace',iostat=ierr)
286 write(iu1,"(a)") '# model spectrum, computed with '//trim(tagline)
287 write(iu1,"(a)") '# frequency [Hz], F_\nu'
288 do i=1,nfreq
289 write(iu1,*) freq(i),spectrum(i)
290 enddo
291 close(iu1)
292
293 if (get_command_flag('writepix')) then
294 call write_pixmap_ascii(img,npixx,npixy,xmin(1),xmin(2),dx,&
295 minval(img),maxval(img),'intensity','lum.pix',0.)
296 endif
297
298 if (get_command_flag('writefits')) then
299 write(*,"(/,a)") 'Writing total intensity to fits image ...'
300 call write_fits_image('img_'//trim(specfile)//'_mom0.fits',img,(/npixx,npixy/),ierr)
301
302 write(*,"(/,a)") 'Writing image into FITS cube ...'
303 allocate(img_tmp(nfreq,npixx,npixy),stat=ierr)
304 if (ierr == 0) then
305 !$omp parallel do default(none) private(i) &
306 !$omp shared(nfreq,npixx,npixy,img_nu,img_tmp)
307 do i=1,nfreq
308 img_tmp(1:npixx,1:npixy,i) = img_nu(i,1:npixx,1:npixy)
309 enddo
310 !$omp end parallel do
311 call write_fits_cube('img_'//trim(specfile)//'.fits',img_tmp,(/npixx,npixy,nfreq/),ierr)
312 endif
313 if (ierr /= 0) write(*,*) 'Error writing to FITS cube !!!'
314 if (allocated(img_tmp)) deallocate(img_tmp)
315 endif
316
317 end subroutine get_lightcurve
318
319 end module lightcurve