Skip to content

Commit ac0d7b0

Browse files
committed
added raytracing scattering test
1 parent 9e02e17 commit ac0d7b0

File tree

9 files changed

+135
-22
lines changed

9 files changed

+135
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<common>
2+
3+
<dust_component> "input/dust_nk/silicate_d03.nk" "plaw" 1.0 3800.0 1e-06 1e-06 -3.5
4+
<phase_function> PH_MIE
5+
6+
<mass_fraction> 0.01
7+
8+
<nr_threads> -1
9+
10+
</common>
11+
12+
<task> 1
13+
14+
<cmd> CMD_DUST_EMISSION
15+
16+
<detector_dust nr_pixel = "255*255"> 1e-6 1e-3 4 1 0.0 0.0 4.32e+18
17+
18+
<max_subpixel_lvl> 2
19+
20+
<path_grid> "projects/test/raytracing_scattering/grid_3D_sphere_const_T_m1e-5.dat"
21+
<path_out> "projects/test/raytracing_scattering/dust/"
22+
23+
</task>
24+
25+
<task> 1
26+
27+
<cmd> CMD_DUST_EMISSION
28+
29+
<detector_dust nr_pixel = "255*255"> 1e-6 1e-3 4 1 0.0 0.0 4.32e+18
30+
31+
<source_star nr_photons = "1e6"> 0 0 0 2 4500
32+
33+
<max_subpixel_lvl> 2
34+
35+
<rt_scattering> 1
36+
37+
<path_grid> "projects/test/raytracing_scattering/grid_3D_sphere_const_T_m1e-5.dat"
38+
<path_out> "projects/test/raytracing_scattering/dust_rt/"
39+
40+
</task>
41+
42+
<task> 1
43+
44+
<cmd> CMD_DUST_SCATTERING
45+
46+
<detector_dust_mc nr_pixel = "255*255"> 1e-6 1e-3 4 0.00 0.00 4.32e+18
47+
48+
<source_star nr_photons = "1e6"> 0 0 0 2 4500
49+
<source_dust nr_photons = "2.1e6">
50+
51+
<path_grid> "projects/test/raytracing_scattering/grid_3D_sphere_const_T_m1e-5.dat"
52+
<path_out> "projects/test/raytracing_scattering/dust_mc/"
53+
54+
<peel_off> 1
55+
<enfsca> 1
56+
57+
</task>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from astropy.io import fits
2+
from astropy import units as u
3+
import numpy as np
4+
5+
6+
"""
7+
----------------------------------------------
8+
Raytracing and scattering
9+
----------------------------------------------
10+
11+
Test whether scattering in a dust simulation
12+
(from radiation field) looks the same as
13+
scattering in a dust_mc simulation.
14+
15+
In order to pass this test, all detectors
16+
(with different orientations) have to yield
17+
the same stellar + scattered + emitted flux
18+
for each wavelength.
19+
(Should get better with more photons)
20+
"""
21+
22+
23+
def read_data(sed_fits_file):
24+
fits_header = fits.getheader(sed_fits_file)
25+
fits_data = fits.getdata(sed_fits_file)
26+
27+
nr_wave = np.shape(fits_data)[2]
28+
sed_wavelengths = np.zeros(nr_wave) * u.m
29+
for i_wave in range(nr_wave):
30+
sed_wavelengths[i_wave] = float(fits_header[f'HIERARCH WAVELENGTH{i_wave+1}']) * u.m
31+
32+
_stokes = ['I', 'Q', 'U', 'V']
33+
sed_data = {}
34+
for i_s, i_stokes in enumerate(_stokes):
35+
sed_data[i_stokes] = fits_data[i_s,0,:] * u.Jy
36+
37+
return sed_data
38+
39+
40+
def compare():
41+
dust_sed_data = read_data('projects/test/raytracing_scattering/dust/data/polaris_detector_nr0001_sed.fits.gz')
42+
dust_rt_sed_data = read_data('projects/test/raytracing_scattering/dust_rt/data/polaris_detector_nr0001_sed.fits.gz')
43+
dust_mc_sed_data = read_data('projects/test/raytracing_scattering/dust_mc/data/polaris_detector_nr0001_sed.fits.gz')
44+
45+
max_rel_diff = np.max(np.abs( (dust_sed_data['I'] + dust_mc_sed_data['I']) / dust_rt_sed_data['I'] - 1.0 ))
46+
if max_rel_diff > 1e-1:
47+
raise Exception(f'Test failed: Stokes I does not match (max. relative difference = {max_rel_diff})')
48+
49+
mc_polarization = np.sqrt(dust_mc_sed_data['Q']**2 + dust_mc_sed_data['U']**2 + dust_mc_sed_data['V']**2) / (dust_mc_sed_data['I'] + dust_sed_data['I'])
50+
rt_polarization = np.sqrt(dust_rt_sed_data['Q']**2 + dust_rt_sed_data['U']**2 + dust_rt_sed_data['V']**2) / dust_rt_sed_data['I']
51+
max_abs_diff = np.max(np.abs( mc_polarization - rt_polarization ))
52+
if max_abs_diff > 1e-2:
53+
raise Exception(f'Test failed: Polarization does not match (max. absolute difference = {max_abs_diff})')
54+
55+
max_polarization = np.max(mc_polarization)
56+
if max_polarization > 1e-2:
57+
raise Exception(f'Test failed: Polarization is too large (max. value = {max_polarization})')
58+
59+
return True
60+
61+
62+
if __name__ == '__main__':
63+
res = compare()
64+
if res:
65+
print('Test passed')
Binary file not shown.

projects/test/reemission_sphere/POLARIS.cmd

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
<mass_fraction> 0.01
77

8-
<write_dust_files> 1
9-
108
<nr_threads> -1
119

1210
</common>

projects/test/reemission_sphere/compare.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def calc_flux_ana():
117117
return flux_sphere_ana.to(u.Jy, equivalencies=u.spectral_density(wavelength))
118118

119119

120-
def read_data(sed_fits_file, map_fits_file, stokes='I'):
120+
def read_data(sed_fits_file, map_fits_file):
121121
sed_fits_header = fits.getheader(sed_fits_file)
122122
sed_fits_data = fits.getdata(sed_fits_file)
123123

@@ -126,13 +126,10 @@ def read_data(sed_fits_file, map_fits_file, stokes='I'):
126126
for i_wave in range(nr_wave):
127127
sed_wavelengths[i_wave] = float(sed_fits_header[f'HIERARCH WAVELENGTH{i_wave+1}']) * u.m
128128

129-
_stokes = ['I', 'Q', 'U', 'V', 'TAU']
129+
_stokes = ['I', 'Q', 'U', 'V']
130130
sed_data = {}
131131
for i_s, i_stokes in enumerate(_stokes):
132-
if i_stokes == 'TAU':
133-
sed_data[i_stokes] = sed_fits_data[i_s,0,:] * u.dimensionless_unscaled
134-
else:
135-
sed_data[i_stokes] = sed_fits_data[i_s,0,:] * u.Jy
132+
sed_data[i_stokes] = sed_fits_data[i_s,0,:] * u.Jy
136133

137134
map_fits_header = fits.getheader(map_fits_file)
138135
map_fits_data = fits.getdata(map_fits_file)
@@ -147,7 +144,7 @@ def read_data(sed_fits_file, map_fits_file, stokes='I'):
147144
for i_s, i_stokes in enumerate(_stokes):
148145
map_data[i_stokes] = map_fits_data[i_s,:,:,:] * u.Jy / u.pix
149146

150-
return sed_data[stokes], map_data[stokes]
147+
return sed_data, map_data
151148

152149

153150
def compare():
@@ -159,19 +156,19 @@ def compare():
159156
'projects/test/reemission_sphere/dust/data/polaris_detector_nr0002.fits.gz')
160157
reference = calc_flux_ana()
161158

162-
max_rel_diff = np.max(np.abs( sed_data_pol / reference - 1.0 ))
159+
max_rel_diff = np.max(np.abs( sed_data_pol['I'] / reference - 1.0 ))
163160
if max_rel_diff > 1e-3:
164161
raise Exception(f'Test failed: Polar detector and reference do not match (max. relative difference = {max_rel_diff})')
165162

166-
max_rel_diff = np.max(np.abs( sed_data_car / sed_data_pol - 1.0 ))
163+
max_rel_diff = np.max(np.abs( sed_data_car['I'] / sed_data_pol['I'] - 1.0 ))
167164
if max_rel_diff > 1e-3:
168165
raise Exception(f'Test failed: Cartesian and polar detector do not match (max. relative difference = {max_rel_diff})')
169166

170-
max_rel_diff = np.max(np.abs( np.sum(map_data_pol, axis=(1,2)) * u.pix / reference - 1.0 ))
167+
max_rel_diff = np.max(np.abs( np.sum(map_data_pol['I'], axis=(1,2)) * u.pix / reference - 1.0 ))
171168
if max_rel_diff > 1e-3:
172169
raise Exception(f'Test failed: Sum of polar map detector and reference do not match (max. relative difference = {max_rel_diff})')
173170

174-
max_rel_diff = np.max(np.abs( np.sum(map_data_car, axis=(1,2)) / np.sum(map_data_pol, axis=(1,2)) - 1.0 ))
171+
max_rel_diff = np.max(np.abs( np.sum(map_data_car['I'], axis=(1,2)) / np.sum(map_data_pol['I'], axis=(1,2)) - 1.0 ))
175172
if max_rel_diff > 1e-3:
176173
raise Exception(f'Test failed: Sum of cartesian and polar map detector do not match (max. relative difference = {max_rel_diff})')
177174

projects/test/stellar_scattering_sphere/POLARIS.cmd

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
<mass_fraction> 0.01
77

8-
<write_dust_files> 1
9-
108
<nr_threads> -1
119

1210
</common>

projects/test/stellar_scattering_sphere/compare.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def read_data(sed_fits_file):
3636
_stokes = ['I', 'Q', 'U', 'V']
3737
sed_data = {}
3838
for i_s, i_stokes in enumerate(_stokes):
39-
sed_data[i_stokes] = fits_data[i_s,:,:] * u.Jy
39+
sed_data[i_stokes] = fits_data[i_s,0,:] * u.Jy
4040

4141
return sed_data
4242

projects/test/stellar_sed/POLARIS.cmd

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
<mass_fraction> 0.01
77

8-
<write_dust_files> 1
9-
108
<nr_threads> -1
119

1210
</common>

projects/test/stellar_sed/compare.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def flux_planck_law(wavelength, temperature=4500*u.K, radius=2*u.R_sun, distance
2323
return flux.to(u.Jy, equivalencies=u.spectral_density(wavelength))
2424

2525

26-
def read_data(sed_fits_file, stokes='I'):
26+
def read_data(sed_fits_file):
2727
fits_header = fits.getheader(sed_fits_file)
2828
fits_data = fits.getdata(sed_fits_file)
2929

@@ -35,16 +35,16 @@ def read_data(sed_fits_file, stokes='I'):
3535
_stokes = ['I', 'Q', 'U', 'V']
3636
sed_data = {}
3737
for i_s, i_stokes in enumerate(_stokes):
38-
sed_data[i_stokes] = fits_data[i_s,:,:] * u.Jy
38+
sed_data[i_stokes] = fits_data[i_s,0,:] * u.Jy
3939

40-
return sed_wavelengths, sed_data[stokes]
40+
return sed_wavelengths, sed_data
4141

4242

4343
def compare():
4444
sed_wavelengths, sed_data = read_data('projects/test/stellar_sed/dust_mc/data/polaris_detector_nr0001_sed.fits.gz')
4545
reference = flux_planck_law(sed_wavelengths)
4646

47-
max_rel_diff = np.max( sed_data[0] / reference - 1.0 )
47+
max_rel_diff = np.max( sed_data['I'] / reference - 1.0 )
4848
if max_rel_diff > 1e-4:
4949
raise Exception(f'Test failed: POLARIS and reference do not match (max. relative difference = {max_rel_diff})')
5050

0 commit comments

Comments
 (0)