Skip to content

Commit

Permalink
Improved the ASV framework; Added benchmarks taking the unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
airvzxf committed Mar 28, 2024
1 parent edb0e8f commit 65ae093
Show file tree
Hide file tree
Showing 57 changed files with 5,797 additions and 68 deletions.
453 changes: 453 additions & 0 deletions benchmarks/benchmark_base.py

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions benchmarks/benchmark_run_tardis.py

This file was deleted.

18 changes: 18 additions & 0 deletions benchmarks/benchmark_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Basic TARDIS Benchmark.
"""
from asv_runner.benchmarks.mark import skip_benchmark
from benchmarks.benchmark_base import BenchmarkBase


@skip_benchmark
class BenchmarkXx(BenchmarkBase):
"""
Class to benchmark the Xx function.
"""

def __init__(self):
pass

def time_template(self):
pass
2 changes: 1 addition & 1 deletion benchmarks/data/tardis_configv1_benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ supernova:
luminosity_requested: 2.8e9 solLum
time_explosion: 13 day

atom_data: kurucz_atom_pure_simple.h5
atom_data: kurucz_cd23_chianti_H_He.h5

model:
structure:
Expand Down
28 changes: 28 additions & 0 deletions benchmarks/energy_input_energy_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Basic TARDIS Benchmark.
"""
import numpy as np
from asv_runner.benchmarks.mark import parameterize, skip_benchmark

from benchmarks.benchmark_base import BenchmarkBase
from tardis.energy_input.samplers import create_energy_cdf


# @skip_benchmark
class BenchmarkEnergyInputEnergySource(BenchmarkBase):
"""
Class to benchmark the run_tardis function.
"""

@parameterize({"Energy CDF": [
{
"energy": np.array([100.0, 50.0]),
"intensity": np.array([1.0, 1.0])
},
{
"energy": np.array([50.0, 100.0]),
"intensity": np.array([0.0, 1.0])
},
]})
def time_create_energy_cdf(self, values: dict):
create_energy_cdf(values.get('energy'), values.get('intensity'))
32 changes: 32 additions & 0 deletions benchmarks/energy_input_gamma_ray_grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Basic TARDIS Benchmark.
"""
import numpy as np
from asv_runner.benchmarks.mark import skip_benchmark

from tardis.energy_input import H_CGS_KEV
from tardis.energy_input.GXPacket import GXPacketStatus, GXPacket
from tardis.energy_input.gamma_ray_grid import move_packet


# @skip_benchmark
class BenchmarkEnergyInputGammaRayGrid:
"""
Class to benchmark the gamma ray grid function.
"""

def time_move_packet(self):
packet = GXPacket(
location=np.array([1.36375693e13, 4.10589818e14, 9.11718168e14]),
direction=np.array([-0.97113853, 0.23134328, -0.05805379]),
energy_rf=1e52,
energy_cmf=1e52,
nu_rf=1000.0e3 / H_CGS_KEV,
nu_cmf=1000.0e3 / H_CGS_KEV,
status=GXPacketStatus.IN_PROCESS,
shell=1,
time_current=1000,
)
distance = 1.0e15

move_packet(packet, distance)
57 changes: 57 additions & 0 deletions benchmarks/energy_input_gamma_ray_interactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Basic TARDIS Benchmark.
"""
import numpy as np
from asv_runner.benchmarks.mark import parameterize, skip_benchmark

from benchmarks.benchmark_base import BenchmarkBase
from tardis.energy_input import H_CGS_KEV, ELECTRON_MASS_ENERGY_KEV
from tardis.energy_input.GXPacket import GXPacketStatus, GXPacket
from tardis.energy_input.gamma_ray_interactions import pair_creation_packet, scatter_type


# @skip_benchmark
class BenchmarkEnergyInputGammaRayInteractions(BenchmarkBase):
"""
Class to benchmark the gamma ray interactions function.
"""

def __init__(self):
self.basic_gamma_ray = GXPacket(
location=np.array([1.36375693e13, 4.10589818e14, 9.11718168e14]),
direction=np.array([-0.97113853, 0.23134328, -0.05805379]),
energy_rf=1e52,
energy_cmf=1e52,
nu_rf=1000.0e3 / H_CGS_KEV,
nu_cmf=1000.0e3 / H_CGS_KEV,
status=GXPacketStatus.IN_PROCESS,
shell=1,
time_current=1000,
)

def time_pair_creation(self):
np.random.seed(2)
self.basic_gamma_ray.nu_cmf = 2 * ELECTRON_MASS_ENERGY_KEV / H_CGS_KEV
pair_creation_packet(self.basic_gamma_ray)

@parameterize({"Scatter type": [
{
"compton_opacity": 1,
"photoabsorption_opacity": 0,
"total_opacity": 1,
},
{
"compton_opacity": 0,
"photoabsorption_opacity": 1,
"total_opacity": 1,
},
{
"compton_opacity": 0,
"photoabsorption_opacity": 0,
"total_opacity": 1,
},
]})
def time_scatter_type(self, values: dict):
scatter_type(
values.get('compton_opacity'), values.get('photoabsorption_opacity'), values.get('total_opacity')
)
136 changes: 136 additions & 0 deletions benchmarks/energy_input_gamma_ray_transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""
Basic TARDIS Benchmark.
"""
import astropy.units as u
from asv_runner.benchmarks.mark import skip_benchmark

from benchmarks.benchmark_base import BenchmarkBase
from tardis.energy_input.energy_source import get_all_isotopes, setup_input_energy
from tardis.energy_input.gamma_ray_transport import create_isotope_dicts, create_inventories_dict, \
calculate_total_decays, decay_chain_energies, calculate_average_energies, calculate_energy_per_mass


# @skip_benchmark
class BenchmarkEnergyInputGammaRayTransport(BenchmarkBase):
"""
Class to benchmark the gamma ray transport function.
"""

def __init__(self):
pass

def time_calculate_cell_masses(self):
"""
Function to test calculation of shell masses.
"""
self.gamma_ray_simulation_state.composition.calculate_cell_masses(
self.gamma_ray_simulation_state.geometry.volume
)

def time_calculate_total_decays_activity(self):
# setup of decay test
time_delta = 1.0 * u.s

# calculating necessary values
composition = self.gamma_ray_simulation_state.composition
cell_masses = composition.calculate_cell_masses(
self.gamma_ray_simulation_state.geometry.volume
)
isotopic_mass_fractions = (
self.gamma_ray_simulation_state.composition.isotopic_mass_fraction
)
iso_dict = create_isotope_dicts(isotopic_mass_fractions, cell_masses)
inv_dict = create_inventories_dict(iso_dict)

calculate_total_decays(inv_dict, time_delta)

def time_calculate_total_decays_activity_chain(self):
time_delta = 1.0 * u.d.to(u.s)

composition = self.gamma_ray_simulation_state.composition
cell_masses = composition.calculate_cell_masses(
self.gamma_ray_simulation_state.geometry.volume
)
isotopic_mass_fractions = (
self.gamma_ray_simulation_state.composition.isotopic_mass_fraction
)
iso_dict = create_isotope_dicts(isotopic_mass_fractions, cell_masses)
inv_dict = create_inventories_dict(iso_dict)

calculate_total_decays(inv_dict, time_delta)

def time_isotope_dicts(self):
isotopic_mass_fractions = (
self.gamma_ray_simulation_state.composition.isotopic_mass_fraction
)
composition = self.gamma_ray_simulation_state.composition
cell_masses = composition.calculate_cell_masses(
self.gamma_ray_simulation_state.geometry.volume
)
create_isotope_dicts(isotopic_mass_fractions, cell_masses)

def time_average_energies(self):
isotopic_mass_fraction = (
self.gamma_ray_simulation_state.composition.isotopic_mass_fraction
)
gamma_ray_lines = self.atomic_dataset.decay_radiation_data

all_isotope_names = get_all_isotopes(isotopic_mass_fraction)

for isotope_name in all_isotope_names:
setup_input_energy(
gamma_ray_lines[
gamma_ray_lines.index == isotope_name.replace("-", "")
],
"g",
)

def time_decay_energy_chain(self):
isotopic_mass_fractions = (
self.gamma_ray_simulation_state.composition.isotopic_mass_fraction
)

composition = self.gamma_ray_simulation_state.composition
cell_masses = composition.calculate_cell_masses(
self.gamma_ray_simulation_state.geometry.volume
)
iso_dict = create_isotope_dicts(isotopic_mass_fractions, cell_masses)
inventories_dict = create_inventories_dict(iso_dict)
gamma_ray_lines = self.atomic_dataset.decay_radiation_data

total_decays = calculate_total_decays(inventories_dict, 1.0 * u.s)

(
average_energies,
_,
_,
) = calculate_average_energies(isotopic_mass_fractions, gamma_ray_lines)

decay_chain_energies(
average_energies,
total_decays,
)

def time_energy_per_mass(self):
isotopic_mass_fractions = (
self.gamma_ray_simulation_state.composition.isotopic_mass_fraction
)
composition = self.gamma_ray_simulation_state.composition
cell_masses = composition.calculate_cell_masses(
self.gamma_ray_simulation_state.geometry.volume
)
iso_dict = create_isotope_dicts(isotopic_mass_fractions, cell_masses)
inventories_dict = create_inventories_dict(iso_dict)
total_decays = calculate_total_decays(inventories_dict, 1.0 * u.s)

gamma_ray_lines = self.atomic_dataset.decay_radiation_data
average_energies = calculate_average_energies(
isotopic_mass_fractions, gamma_ray_lines
)
decay_energy = decay_chain_energies(
average_energies[0],
total_decays,
)
calculate_energy_per_mass(
decay_energy, isotopic_mass_fractions, cell_masses
)
Loading

0 comments on commit 65ae093

Please sign in to comment.