Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final numba PR #1285

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
8db09cd
Add montecarlo_seed value to global module
arjunsavel Jul 24, 2020
734fd78
Add user-input seed to each seed setting
arjunsavel Jul 24, 2020
e0e67d2
Make sure montecarlo_seed for global module is set during configuration
arjunsavel Jul 24, 2020
b5ee89c
Include packet_seeds within montecarlo_configuration module
arjunsavel Jul 29, 2020
b1a3b6b
Initialize packet seeds
arjunsavel Jul 29, 2020
7c3e5c6
Set user seed before instantiating *any* random process!
arjunsavel Jul 29, 2020
0089ccd
Pass packet seeds to montecarlo_main_loop; use said seeds
arjunsavel Jul 29, 2020
bfbadbb
Bring rpacket creation outside of if/else
arjunsavel Jul 30, 2020
8aa57b0
Initialize seeds within initialize_packets
arjunsavel Jul 30, 2020
f1a1efc
Set mu and nu sampling on a per-seed basis
arjunsavel Jul 30, 2020
341f10e
Use single_packet_seed to index existing packet_seeds array
arjunsavel Jul 30, 2020
e072e14
Perform packet-by-packet creation of trajectories, nu in packet_source
arjunsavel Jul 30, 2020
ecaab5d
Try initializing xis outside of blackbody function
arjunsavel Jul 30, 2020
3b7826f
Remove print statements
arjunsavel Jul 30, 2020
cec0fdb
Do not concatenate arrays in packet_source running; add njit
arjunsavel Jul 31, 2020
9942855
Name create_array more explicitly
arjunsavel Aug 4, 2020
33181f0
Make montecarlo seeds attributes of r_packet
arjunsavel Aug 4, 2020
5ed9fa8
Add rationale for size of random array
arjunsavel Aug 4, 2020
fb67ba9
Clean up documentation of blackbody sampling
arjunsavel Aug 4, 2020
11e9cfc
Remove extraneous numba import
arjunsavel Aug 4, 2020
090f5b2
Remove extra kwarg in basepacket create_packets
arjunsavel Aug 4, 2020
95103dd
Add packet_source docstrings
arjunsavel Aug 4, 2020
de35fff
Justify and change MAX_SEED_VAL
arjunsavel Aug 4, 2020
259a879
Refactor packet_source so that all packet properties are made in the …
arjunsavel Aug 4, 2020
99cd847
Attach seed to r_packet
arjunsavel Aug 4, 2020
b7745d7
Clean up create_packets func
arjunsavel Aug 4, 2020
b675fda
Clean up docstrings for packet_source
arjunsavel Aug 4, 2020
637c9ad
@jitclass BlackBodySimpleSource
arjunsavel Aug 6, 2020
5ac1e56
Ensure that the iteration is added to the seed each time montecarloru…
arjunsavel Aug 10, 2020
dc8290a
jitclass uniform packets
arjunsavel Aug 10, 2020
ae9ef91
Pass iteration to montecarlorunner
arjunsavel Aug 10, 2020
501158a
Remove njit from uniform packet energies
arjunsavel Aug 10, 2020
2e2c4c2
Provide a better-motivated upper bound for the seed value
arjunsavel Aug 10, 2020
379bff5
Use random.sample instead of np.random.randint to prevent possible re…
arjunsavel Aug 10, 2020
fcee808
Raise more explicit error if too many packets
arjunsavel Aug 10, 2020
da4dd7b
Set seed of random module before calling random.sample
arjunsavel Aug 10, 2020
b620f52
Merge branch 'numba_montecarlo' into add_input_seed
arjunsavel Aug 10, 2020
e9f98be
Address merge error in montecarlo_main_loop
arjunsavel Aug 10, 2020
16acf63
Clarify ValueError for seeds
arjunsavel Aug 11, 2020
9519cf1
Do not generate packet properties on a seed-by-seed basis
arjunsavel Aug 11, 2020
bf66971
Remove seeds from create_packets function call
arjunsavel Aug 11, 2020
ee238ba
use new random number generator to make packet seeds
arjunsavel Aug 12, 2020
537ccc2
Bump numpy version to 1.19.0
arjunsavel Aug 12, 2020
75c2a89
Pass rng to packet source functions for randomness
arjunsavel Aug 12, 2020
cefa1ab
actually define rng in montecarlo/base
arjunsavel Aug 12, 2020
dd6af64
Delete error raised for too many packets
arjunsavel Aug 13, 2020
0b47908
Allow packet seeds to overlap
arjunsavel Aug 13, 2020
7f8f331
Remove random module import
arjunsavel Aug 14, 2020
3f30946
Clarify C comparison
arjunsavel Aug 14, 2020
d2a5467
Include numba comparison plots
arjunsavel Aug 14, 2020
3c45e48
Add Testing TARDIS section to docs
arjunsavel Aug 14, 2020
abceef7
Merge branch 'add_input_seed' of https://github.com/arjunsavel/tardis…
arjunsavel Aug 14, 2020
be7aeb5
Make sure custom packet source works
arjunsavel Aug 14, 2020
d36b554
Add disabling to yml file
arjunsavel Aug 20, 2020
ac505e0
Make changes to mc_config_module when necessary
arjunsavel Aug 20, 2020
1df427c
Add scatter-disabling functionality to global module
arjunsavel Aug 20, 2020
6a4f48c
Set tau_sobolev to 0 in plasma setting
arjunsavel Aug 20, 2020
362119c
Add rudimentary last_interaction_type
arjunsavel Aug 20, 2020
3d64c63
Address combined > event for tau_trace
arjunsavel Aug 20, 2020
2ace50a
reference module for sigma_thomson
arjunsavel Aug 20, 2020
6c96456
Explicitly pass sigma_thomson
arjunsavel Aug 21, 2020
0a37d0f
Only reset nu for e-scatter
arjunsavel Aug 24, 2020
f4a90d5
Split thomson_scatter and line_scatter entirely
arjunsavel Aug 24, 2020
004f65a
check doppler factor
arjunsavel Aug 26, 2020
7240e9f
Account for interaction type
arjunsavel Aug 31, 2020
d721854
Do not hardcode the array size anymore
arjunsavel Aug 31, 2020
2881599
Remove extra commented statments
arjunsavel Aug 31, 2020
ae26428
Remove reference to flag
arjunsavel Aug 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/development/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ to the Astropy team for designing it.
.. toctree::
:maxdepth: 2

running_tests
issues
debug_numba

Expand Down
98 changes: 0 additions & 98 deletions docs/development/running_tests.rst

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ assistance.
physics/plasma/index
physics/montecarlo/index

.. toctree::
:maxdepth: 2
:caption: Testing TARDIS
:hidden:

testing/automated_tests
testing/numba


.. toctree::
:maxdepth: 2
Expand Down
16 changes: 8 additions & 8 deletions docs/running/custom_source.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
" \n",
" def __init__(self, seed, truncation_wavelength):\n",
" super().__init__(seed)\n",
" self.rng = np.random.default_rng(seed=seed)\n",
" self.truncation_wavelength = truncation_wavelength\n",
" \n",
" def create_packets(self, T, no_of_packets,\n",
" def create_packets(self, T, no_of_packets, rng,\n",
" drawing_sample_size=None):\n",
" \"\"\"\n",
" Packet source that generates a truncated Blackbody source.\n",
Expand All @@ -70,11 +71,10 @@
" Only wavelengths higher than the truncation wavelength\n",
" will be sampled.\n",
" \"\"\"\n",
" \n",
" \n",
"\n",
" # Use mus and energies from normal blackbody source.\n",
" mus = self.create_zero_limb_darkening_packet_mus(no_of_packets)\n",
" energies = self.create_uniform_packet_energies(no_of_packets)\n",
" mus = self.create_zero_limb_darkening_packet_mus(no_of_packets, self.rng)\n",
" energies = self.create_uniform_packet_energies(no_of_packets, self.rng)\n",
"\n",
" # If not specified, draw 2 times as many packets and reject any beyond no_of_packets.\n",
" if drawing_sample_size is None:\n",
Expand All @@ -86,15 +86,15 @@
" \n",
" # Draw nus from blackbody distribution and reject based on truncation_frequency.\n",
" # If more nus.shape[0] > no_of_packets use only the first no_of_packets.\n",
" nus = self.create_blackbody_packet_nus(T, drawing_sample_size)\n",
" nus = self.create_blackbody_packet_nus(T, drawing_sample_size, self.rng)\n",
" nus = nus[nus<truncation_frequency][:no_of_packets]\n",
" \n",
" \n",
" # Only required if the truncation wavelength is too big compared to the maximum \n",
" # of the blackbody distribution. Keep sampling until nus.shape[0] > no_of_packets.\n",
" while nus.shape[0] < no_of_packets:\n",
" additional_nus = self.create_blackbody_packet_nus(\n",
" T, drawing_sample_size\n",
" T, drawing_sample_size, self.rng\n",
" )\n",
" mask = additional_nus < truncation_frequency\n",
" additional_nus = additional_nus[mask][:no_of_packets]\n",
Expand Down Expand Up @@ -170,4 +170,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
5 changes: 5 additions & 0 deletions tardis/io/schemas/plasma.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ properties:
default: false
description: disable electron scattering process in montecarlo part - non-physical
only for tests
disable_line_scattering:
type: boolean
default: false
description: disable line scattering process in montecarlo part - non-physical
only for tests
ionization:
type: string
enum:
Expand Down
40 changes: 32 additions & 8 deletions tardis/montecarlo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
logger = logging.getLogger(__name__)
TARDIS_PATH = TARDIS_PATH[0]

MAX_SEED_VAL = 2**32 - 1

# MAX_SEED_VAL must be multiple orders of magnitude larger than no_of_packets;
# otherwise, each packet would not have its own seed. Here, we set the max
# seed val to the maximum allowed by numpy.

class MontecarloRunner(HDFWriterMixin):
"""
This class is designed as an interface between the Python part and the
Expand Down Expand Up @@ -61,7 +67,7 @@ class MontecarloRunner(HDFWriterMixin):
(const.h / const.k_B)).cgs.value

def __init__(self, seed, spectrum_frequency, virtual_spectrum_range,
sigma_thomson, enable_reflective_inner_boundary,
sigma_thomson, disable_electron_scattering, enable_reflective_inner_boundary,
enable_full_relativity, inner_boundary_albedo,
line_interaction_type, integrator_settings,
v_packet_settings, spectrum_method,
Expand All @@ -74,6 +80,7 @@ def __init__(self, seed, spectrum_frequency, virtual_spectrum_range,
else:
self.packet_source = packet_source
# inject different packets
self.disable_electron_scattering = disable_electron_scattering
self.spectrum_frequency = spectrum_frequency
self.virtual_spectrum_range = virtual_spectrum_range
self.sigma_thomson = sigma_thomson
Expand All @@ -85,6 +92,7 @@ def __init__(self, seed, spectrum_frequency, virtual_spectrum_range,
self.integrator_settings = integrator_settings
self.v_packet_settings = v_packet_settings
self.spectrum_method = spectrum_method
self.seed = seed
self._integrator = None
self._spectrum_integrated = None

Expand Down Expand Up @@ -127,11 +135,23 @@ def _initialize_geometry_arrays(self, model):
self.r_outer_cgs = model.r_outer.to('cm').value
self.v_inner_cgs = model.v_inner.to('cm/s').value

def _initialize_packets(self, T, no_of_packets):
def _initialize_packets(self, T, no_of_packets, iteration):
# the iteration is added each time to preserve randomness
# across different simulations with the same temperature,
# for example. We seed the random module instead of the numpy module
# because we call random.sample, which references a different internal
# state than in the numpy.random module.
seed = self.seed + iteration
rng = np.random.default_rng(seed=seed)
seeds = rng.choice(MAX_SEED_VAL,
no_of_packets,
replace=True
)
nus, mus, energies = self.packet_source.create_packets(
T,
no_of_packets
)
no_of_packets,
rng)
mc_config_module.packet_seeds = seeds
self.input_nu = nus
self.input_mu = mus
self.input_energy = energies
Expand Down Expand Up @@ -206,7 +226,7 @@ def integrator(self):

def run(self, model, plasma, no_of_packets,
no_of_virtual_packets=0, nthreads=1,
last_run=False):
last_run=False, iteration=0):
"""
Run the montecarlo calculation

Expand Down Expand Up @@ -236,7 +256,7 @@ def run(self, model, plasma, no_of_packets,
self._initialize_geometry_arrays(model)

self._initialize_packets(model.t_inner.value,
no_of_packets)
no_of_packets, iteration)

montecarlo_configuration = configuration_initialize(self,
no_of_virtual_packets)
Expand Down Expand Up @@ -439,15 +459,18 @@ def from_config(cls, config, packet_source=None):
"""
if config.plasma.disable_electron_scattering:
logger.warn('Disabling electron scattering - this is not physical')
sigma_thomson = 1e-200 * (u.cm ** 2)
sigma_thomson = 1e-200
# mc_config_module.disable_electron_scattering = True
else:
logger.debug("Electron scattering switched on")
sigma_thomson = const.sigma_T.cgs
sigma_thomson = const.sigma_T.to('cm^2').value
# mc_config_module.disable_electron_scattering = False

spectrum_frequency = quantity_linspace(
config.spectrum.stop.to('Hz', u.spectral()),
config.spectrum.start.to('Hz', u.spectral()),
num=config.spectrum.num + 1)
mc_config_module.disable_line_scattering = config.plasma.disable_line_scattering

return cls(seed=config.montecarlo.seed,
spectrum_frequency=spectrum_frequency,
Expand All @@ -460,6 +483,7 @@ def from_config(cls, config, packet_source=None):
integrator_settings=config.spectrum.integrated,
v_packet_settings=config.spectrum.virtual,
spectrum_method=config.spectrum.method,
disable_electron_scattering=config.plasma.disable_electron_scattering,
packet_source=packet_source,
debug_packets=config.montecarlo.debug_packets,
logger_buffer=config.montecarlo.logger_buffer,
Expand Down
8 changes: 7 additions & 1 deletion tardis/montecarlo/montecarlo_configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from tardis import constants as const

full_relativity = True
single_packet_seed = -1
temporary_v_packet_bins = None
number_of_vpackets = 0
line_interaction_type = None
montecarlo_seed = 0
line_interaction_type = None
packet_seeds = []
disable_electron_scattering = False
disable_line_scattering = False
Loading