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

Refactor and add more benchmarks for montecarlo #2640

Merged
merged 32 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d61d905
benchmark for energy input package
officialasishkumar May 31, 2024
1664b38
refactor benchmark - remove assert
officialasishkumar Jun 2, 2024
d40cfea
remove energy input source
officialasishkumar Jun 2, 2024
bd9a8f6
intensity_black_body and test_reverse_binary_search function benchmark
officialasishkumar Jun 2, 2024
75e2b0e
add name and email in .mailmap
officialasishkumar Jun 3, 2024
ab13842
fix import error of Configuration
officialasishkumar Jun 3, 2024
4c86741
refactor transport_montecarlo_packet.py
officialasishkumar Jun 4, 2024
f5ebda4
refactor transport_montecarlo_numba_formal_integral_p.py
officialasishkumar Jun 4, 2024
48d164d
add FormalIntegrator benchmark
officialasishkumar Jun 6, 2024
edc5dce
add calculate_spectrum benchmark
officialasishkumar Jun 6, 2024
4a909f0
add make_source_function and generate_numba_objects in benchmark
officialasishkumar Jun 6, 2024
76cb1cf
add formal_integral from FormalIntegrator class benchmark
officialasishkumar Jun 6, 2024
247add4
add trace_vpacket_volley benchmark
officialasishkumar Jun 17, 2024
8c2fa52
add single_packet_loop benchmark
officialasishkumar Jun 18, 2024
4974f89
refactor vpacket benchmark
officialasishkumar Jun 18, 2024
e3d52ba
refactor r_packet benchmark
officialasishkumar Jun 18, 2024
293d183
add montecarlo_main_loop benchmark
officialasishkumar Jun 19, 2024
b4e01ce
refactor VPacketCollection_add_packet benchmark
officialasishkumar Jun 19, 2024
70119f4
refactor transport_montecarlo_packet.py
officialasishkumar Jun 19, 2024
3ae39a0
refactor numba_formal_integral benchmark
officialasishkumar Jun 20, 2024
fad45f3
fix benchmark_base path bug
officialasishkumar Jun 20, 2024
e95c3d2
migrate calculate distance functions to a new file
officialasishkumar Jun 20, 2024
72be045
Merge branch 'master' into refactor-benchmarks
officialasishkumar Jun 20, 2024
928c16e
Merge branch 'master' into refactor-benchmarks
officialasishkumar Jun 26, 2024
ed46872
bug fix benchmarking
officialasishkumar Jun 26, 2024
14f29ad
removed duplicate function
officialasishkumar Jun 26, 2024
a245a7d
skip benchmark with GNU error
officialasishkumar Jun 27, 2024
09e3032
benchmark single packet loop
officialasishkumar Jul 9, 2024
ea6175a
fix benchmark trace_vpacket_volley
officialasishkumar Jul 11, 2024
5bae9d4
fix benchmark single_packet_loop
officialasishkumar Jul 11, 2024
72340d1
change docstring
officialasishkumar Jul 12, 2024
fbae4cc
fix benchmark formal_integral functions
officialasishkumar Jul 12, 2024
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: 1 addition & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Alice Harpole <[email protected]>
Alice Harpole <[email protected]> Alice Harpole <[email protected]>

Aman Kumar <[email protected]>
Asish Kumar <[email protected]>

Andreas Flörs <[email protected]>
Andreas Flörs <[email protected]> Andreas Flörs <[email protected]>
Expand Down
84 changes: 69 additions & 15 deletions benchmarks/benchmark_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@

import astropy.units as u
import numpy as np
import pandas as pd
from numba import njit

from benchmarks.util.nlte import NLTE
from tardis import run_tardis
from tardis.io.atom_data import AtomData
from tardis.io.configuration import config_reader
from tardis.io.configuration.config_reader import Configuration
from tardis.io.util import YAMLLoader, yaml_load_file
from tardis.model import SimulationState
from tardis.model.geometry.radial1d import NumbaRadial1DGeometry
from tardis.simulation import Simulation
from tardis.tests.fixtures.atom_data import DEFAULT_ATOM_DATA_UUID
from tardis.tests.fixtures.regression_data import RegressionData
from tardis.transport.montecarlo import RPacket
from tardis.transport.montecarlo.numba_interface import (
opacity_state_initialize,
)
from tardis.transport.montecarlo import RPacket, montecarlo_configuration
from tardis.transport.montecarlo.estimators import radfield_mc_estimators
from tardis.transport.montecarlo.numba_interface import opacity_state_initialize
from tardis.transport.montecarlo.packet_collections import (
VPacketCollection,
)
from tardis.transport.montecarlo.packet_trackers import RPacketTracker


class BenchmarkBase:
Expand Down Expand Up @@ -350,13 +351,66 @@ def generate_reference(self):
return option

@property
def tardis_ref_data(self):
# TODO: This function is not working in the benchmarks.
if self.generate_reference:
mode = "w"
else:
mode = "r"
with pd.HDFStore(
f"{self.tardis_ref_path}/unit_test_data.h5", mode=mode
) as store:
yield store
def verysimple_radfield_mc_estimators(self):
plasma = self.nb_simulation_verysimple.plasma
return radfield_mc_estimators.initialize_estimator_statistics(
plasma.tau_sobolevs.shape, plasma.gamma.shape
)

@property
def montecarlo_configuration(self):
return montecarlo_configuration.MonteCarloConfiguration()

@property
def rpacket_tracker(self):
return RPacketTracker(0)

@property
def transport_state(self):
return self.nb_simulation_verysimple.transport.transport_state

@property
def simulation_rpacket_tracking_enabled(self):
config_verysimple = self.config_verysimple
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Can be future work) Setting up a separate config property or a yml file is cleaner.

config_verysimple.montecarlo.iterations = 3
config_verysimple.montecarlo.no_of_packets = 4000
config_verysimple.montecarlo.last_no_of_packets = -1
config_verysimple.spectrum.virtual.virtual_packet_logging = True
config_verysimple.montecarlo.no_of_virtual_packets = 1
config_verysimple.montecarlo.tracking.track_rpacket = True
config_verysimple.spectrum.num = 2000
atomic_data = deepcopy(self.atomic_dataset)
sim = run_tardis(
config_verysimple,
atom_data=atomic_data,
show_convergence_plots=False,
)
return sim

@property
def geometry(self):
return NumbaRadial1DGeometry(
r_inner=np.array([6.912e14, 8.64e14], dtype=np.float64),
r_outer=np.array([8.64e14, 1.0368e15], dtype=np.float64),
v_inner=np.array([-1, -1], dtype=np.float64),
v_outer=np.array([-1, -1], dtype=np.float64),
)


@property
def estimators(self):
return radfield_mc_estimators.RadiationFieldMCEstimators(
j_estimator=np.array([0.0, 0.0], dtype=np.float64),
nu_bar_estimator=np.array([0.0, 0.0], dtype=np.float64),
j_blue_estimator=np.array(
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float64
),
Edotlu_estimator=np.array(
[[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]], dtype=np.float64
),
photo_ion_estimator=np.empty((0, 0), dtype=np.float64),
stim_recomb_estimator=np.empty((0, 0), dtype=np.float64),
bf_heating_estimator=np.empty((0, 0), dtype=np.float64),
stim_recomb_cooling_estimator=np.empty((0, 0), dtype=np.float64),
photo_ion_estimator_statistics=np.empty((0, 0), dtype=np.int64),
)
10 changes: 2 additions & 8 deletions benchmarks/run_tardis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from benchmarks.benchmark_base import BenchmarkBase
from tardis import run_tardis
from tardis.io.configuration.config_reader import Configuration


class BenchmarkRunTardis(BenchmarkBase):
"""
Expand All @@ -14,12 +12,8 @@ class BenchmarkRunTardis(BenchmarkBase):

def __init__(self):
super().__init__()
self.config = None

def setup(self):
filename = "data/tardis_configv1_benchmark.yml"
path = self.get_relative_path(filename)
self.config = Configuration.from_yaml(path)
self.path = self.get_relative_path(filename)

def time_run_tardis(self):
run_tardis(self.config, log_level="ERROR", show_progress_bars=False)
run_tardis(self.path, log_level="ERROR", show_progress_bars=False)
68 changes: 68 additions & 0 deletions benchmarks/transport_geometry_calculate_distances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from asv_runner.benchmarks.mark import parameterize

import tardis.transport.frame_transformations as frame_transformations
import tardis.transport.geometry.calculate_distances as calculate_distances
from benchmarks.benchmark_base import BenchmarkBase


class BenchmarkTransportGeometryCalculateDistances(BenchmarkBase):
"""
Class to benchmark the calculate distances function.
"""

@property
def model(self):
return 5.2e7

def time_calculate_distance_boundary(self):
mu = 0.3
r = 7.5e14

calculate_distances.calculate_distance_boundary(
r, mu, self.geometry.r_inner[0], self.geometry.r_outer[0]
)

@parameterize(
{
"Parameters": [
{
"packet": {
"nu_line": 0.1,
"is_last_line": True
},
"enable_full_relativity": True,
},
{
"packet": {
"nu_line": 0.2,
"is_last_line": False
},
"enable_full_relativity": True,
}
]
}
)
def time_calculate_distance_line(self, parameters):
packet_params = parameters["packet"]
nu_line = packet_params["nu_line"]
is_last_line = packet_params["is_last_line"]
enable_full_relativity = parameters["enable_full_relativity"]

time_explosion = self.model

doppler_factor = frame_transformations.get_doppler_factor(
self.static_packet.r,
self.static_packet.mu,
time_explosion,
enable_full_relativity
)
comov_nu = self.static_packet.nu * doppler_factor

calculate_distances.calculate_distance_line(
self.static_packet,
comov_nu,
is_last_line,
nu_line,
time_explosion,
enable_full_relativity
)
19 changes: 2 additions & 17 deletions benchmarks/transport_montecarlo_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
Basic TARDIS Benchmark.
"""

import numpy as np
from asv_runner.benchmarks.mark import parameterize, skip_benchmark

import tardis.transport.montecarlo.interaction as interaction
from benchmarks.benchmark_base import BenchmarkBase
from tardis.transport.montecarlo.numba_interface import (
LineInteractionType,
)
from asv_runner.benchmarks.mark import parameterize



@skip_benchmark
class BenchmarkMontecarloMontecarloNumbaInteraction(BenchmarkBase):
"""
Class to benchmark the numba interaction function.
Expand All @@ -30,10 +28,6 @@ def time_thomson_scatter(self):
packet, time_explosion, enable_full_relativity
)

assert np.abs(packet.mu - init_mu) > 1e-7
assert np.abs(packet.nu - init_nu) > 1e-7
assert np.abs(packet.energy - init_energy) > 1e-7

@parameterize(
{
"Line interaction type": [
Expand All @@ -45,9 +39,6 @@ def time_thomson_scatter(self):
)
def time_line_scatter(self, line_interaction_type):
packet = self.packet
init_mu = packet.mu
init_nu = packet.nu
init_energy = packet.energy
packet.initialize_line_id(
self.verysimple_opacity_state,
self.verysimple_time_explosion,
Expand All @@ -64,10 +55,6 @@ def time_line_scatter(self, line_interaction_type):
self.verysimple_continuum_processes_enabled,
)

assert np.abs(packet.mu - init_mu) > 1e-7
assert np.abs(packet.nu - init_nu) > 1e-7
assert np.abs(packet.energy - init_energy) > 1e-7

@parameterize(
{
"Test packet": [
Expand Down Expand Up @@ -109,5 +96,3 @@ def time_line_emission(self, test_packet):
self.verysimple_opacity_state,
self.verysimple_enable_full_relativity,
)

assert packet.next_line_id == emission_line_id + 1
27 changes: 27 additions & 0 deletions benchmarks/transport_montecarlo_main_loop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Basic TARDIS Benchmark.
"""

from benchmarks.benchmark_base import BenchmarkBase
from tardis.transport.montecarlo.montecarlo_main_loop import montecarlo_main_loop


class BenchmarkTransportMontecarloMainLoop(BenchmarkBase):
"""
class to benchmark montecarlo_main_loop function.
"""

def time_montecarlo_main_loop(self):
montecarlo_main_loop(
self.transport_state.packet_collection,
self.transport_state.geometry_state,
self.verysimple_time_explosion,
self.transport_state.opacity_state,
self.montecarlo_configuration,
self.transport_state.radfield_mc_estimators,
self.transport_state.spectrum_frequency.value,
self.montecarlo_configuration.NUMBER_OF_VPACKETS,
iteration=0,
show_progress_bars=False,
total_iterations=0
)
Loading
Loading