Skip to content

Commit

Permalink
Restructure radiation field (#2474)
Browse files Browse the repository at this point in the history
* restructure of geometry

* add radial1d boundary logic

* black format

* several fixes

* fix epsilon

* add testing of boundaries

* change the r_inner_active

* first integration with `from_config` working

* hunting down density indexing bug

* all model tests (without csvy) pass

* more fixes

* fix of model to simulation_state

* fix inner boundary packet error

* fix some leftovers

* final fix for csvy

* blackify

* restructure to readers and remove some leftover code

* further cleanup

* first start of the restructure

* add comment about removing quantitiness

* add velocity check

* add new abundance functions

* remove default units

* add new matter module

* several restructures. move decay from io to tardis/model/matter

* mid restructure

* slow progress on abundance refactor

* include effective_element_masses

* further updates

* fix of csvy readers

* last fixes to custom abundance widget

* restructuring the radiation field

* some cleanup

* cleanup

* further cleanup

* clearnup

* changes

* remove matter base.py

* removing matter

* fixed last tests

* add several parsers

* add composition

* remove isotopemassfractions

* some small fixes

* fixed w, t_rad to dilution_factor, t_radiative

* add setters for w, t_rad

* several fixes for instantiating the right t_rads.
also ensuring that they are positive and len(t_rad) == dilution factor

* add the from_config atom_data

* fixing model.ipynb

* fix some more tests

* fix tests

* fix some of the issues wdigets

* assert mass fractions positive.
Change IsotopeAbundances to Massfractions

* add radiation_field_state

* Fix isotope mass fraction typo and t_radiatve duplicate definition

* Document remove functionality for effective element masses

* Fix variable name in
convert_to_nuclide_mass_fraction function

* Refactor test names and variable names in
test_base.py

* Refactor gamma ray simulation setup and test
functions

* fix gamma ray tests

* Add model_isotope_time_0 property to abundances
schema

* Add test for montecarlo main loop with vpacket
tracking

* Refactor config_reader and config_validator
modules

* Fix model_isotope_time_0 property in
model_definitions.yml

* Refactor simulation module to use pathlib

* remove bad comment and refactor with ruff

* Fix variable name in test_simulation_state_mass()
function

* Fix unit conversion in calculate_cell_masses
function

* Fix logger debug message in CSV reader and update
method names in Composition class

* black formatting

* Refactor code to use composition variable in
test_gamma_ray_transport.py

* Refactor imports in atom_web_download.py and
update return statement in config_internal.py

* fix for documentation not builiding

* add fix for model_isotope_time_0

* fix grid test

* restructure the grid

* final change

* add stuff

* add vpacket_log

* Refactor model_reader.py and shell_info.py
changing w to dilution_factor

* Update damping constants and variable names
in doc notebook

* fix w in several places in tardis

* slowly fixing radiation_field changes

* last fix

* change hdf from model to simulation_state

* fixing model->simulation-state

* Fix model_reader.py imports and formatting issues

* Refactor variable names for clarity and
consistency and being correct

* Update variable names in model.ipynb

* Update variable name for average temperature
calculation

* Refactor variable names in model reader and
simulation base

* Refactor store_model_to_hdf to
store_simulation_state_to_hdf

* Refactor HDF file handling and model reader
imports

* add docstr
  • Loading branch information
wkerzendorf authored Dec 11, 2023
1 parent b68e542 commit df439d8
Show file tree
Hide file tree
Showing 19 changed files with 633 additions and 502 deletions.
2 changes: 1 addition & 1 deletion docs/io/optional/callback_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"outputs": [],
"source": [
"def average_temp(sim):\n",
" t_rads = sim.simulation_state.t_rad\n",
" t_rads = sim.simulation_state.t_radiative\n",
" volumes = sim.simulation_state.volume\n",
" avg = sum(t_rads*volumes) / sum(volumes)\n",
" print(f\"Average temperature for iteration {sim.iterations_executed}: {avg}\")"
Expand Down
38 changes: 19 additions & 19 deletions docs/physics/setup/model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@
" 'Ni57':.1,\n",
" 'Cr51':.4}\n",
"\n",
"abund_isotopes_model = SimulationState.from_config(abund_isotopes_config, atom_data=atom_data)\n",
"abund_isotopes_simulation_state = SimulationState.from_config(abund_isotopes_config, atom_data=atom_data)\n",
"\n",
"abund_isotopes_model.abundance"
"abund_isotopes_simulation_state.abundance"
]
},
{
Expand Down Expand Up @@ -529,15 +529,15 @@
"t_rad_config.model.structure.velocity.stop = 2000 * u.km/u.s\n",
"t_rad_config.model.structure.velocity.num = 20\n",
"\n",
"t_rad_model = SimulationState.from_config(t_rad_config, atom_data=atom_data)\n",
"t_radiative_simulation_state = SimulationState.from_config(t_rad_config, atom_data=atom_data)\n",
"\n",
"print('t_inner:\\n', t_rad_model.t_inner)\n",
"print('t_rad:\\n', t_rad_model.t_rad)\n",
"print('t_inner:\\n', t_radiative_simulation_state.t_inner)\n",
"print('t_rad:\\n', t_radiative_simulation_state.t_radiative)\n",
"\n",
"plt.plot(t_rad_model.r_middle, t_rad_model.t_rad)\n",
"plt.scatter(t_rad_model.radius[0], t_rad_model.t_inner)\n",
"plt.xlabel(f'Radius ({t_rad_model.r_middle.unit})')\n",
"plt.ylabel(f'Radiative Temperature ({t_rad_model.t_rad.unit})');"
"plt.plot(t_radiative_simulation_state.r_middle, t_radiative_simulation_state.t_radiative)\n",
"plt.scatter(t_radiative_simulation_state.radius[0], t_radiative_simulation_state.t_inner)\n",
"plt.xlabel(f'Radius ({t_radiative_simulation_state.r_middle.unit})')\n",
"plt.ylabel(f'Radiative Temperature ({t_radiative_simulation_state.t_radiative.unit})');"
]
},
{
Expand Down Expand Up @@ -580,24 +580,24 @@
},
"outputs": [],
"source": [
"w_config = copy.deepcopy(base_config)\n",
"dilution_factor_config = copy.deepcopy(base_config)\n",
"\n",
"w_config.supernova.time_explosion = 10 * u.day\n",
"dilution_factor_config.supernova.time_explosion = 10 * u.day\n",
"\n",
"# This line is necessary to indicate we are using a built-in shell structure\n",
"# and density. Do not change it.\n",
"w_config.model.structure.type = 'specific'\n",
"dilution_factor_config.model.structure.type = 'specific'\n",
"\n",
"w_config.model.structure.velocity.start = 1000 * u.km/u.s\n",
"w_config.model.structure.velocity.stop = 2000 * u.km/u.s\n",
"w_config.model.structure.velocity.num = 20\n",
"dilution_factor_config.model.structure.velocity.start = 1000 * u.km/u.s\n",
"dilution_factor_config.model.structure.velocity.stop = 2000 * u.km/u.s\n",
"dilution_factor_config.model.structure.velocity.num = 20\n",
"\n",
"w_model = SimulationState.from_config(w_config, atom_data=atom_data)\n",
"dilution_factor_simulation_state = SimulationState.from_config(dilution_factor_config, atom_data=atom_data)\n",
"\n",
"print('w:\\n', w_model.w)\n",
"print(f'dilution_factor: {dilution_factor_simulation_state.dilution_factor}')\n",
"\n",
"plt.plot(w_model.r_middle, w_model.w)\n",
"plt.xlabel(f'Radius ({w_model.r_middle.unit})')\n",
"plt.plot(dilution_factor_simulation_state.r_middle, dilution_factor_simulation_state.dilution_factor)\n",
"plt.xlabel(f'Radius ({dilution_factor_simulation_state.r_middle.unit})')\n",
"plt.ylabel(f'Dilution Factor');"
]
}
Expand Down
44 changes: 22 additions & 22 deletions docs/physics/update_and_conv/update_and_conv.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@
"\n",
"# We manually put in the damping constants and t_inner_update_exponent for\n",
"# illustrative purposes:\n",
"d_t_rad = 0.5\n",
"d_w = 0.3\n",
"d_t_inner = 0.7\n",
"damping_t_radiative = 0.5\n",
"damping_dilution_factor = 0.3\n",
"damping_t_inner = 0.7\n",
"t_inner_update_exponent = -0.5\n",
"\n",
"# We set the above values into the configuration:\n",
"tardis_config.montecarlo.convergence_strategy.t_rad.damping_constant = d_t_rad\n",
"tardis_config.montecarlo.convergence_strategy.w.damping_constant = d_w\n",
"tardis_config.montecarlo.convergence_strategy.t_inner.damping_constant = d_t_inner\n",
"tardis_config.montecarlo.convergence_strategy.t_rad.damping_constant = damping_t_radiative\n",
"tardis_config.montecarlo.convergence_strategy.w.damping_constant = damping_dilution_factor\n",
"tardis_config.montecarlo.convergence_strategy.t_inner.damping_constant = damping_t_inner\n",
"tardis_config.montecarlo.convergence_strategy.t_inner_update_exponent = t_inner_update_exponent"
]
},
Expand All @@ -212,7 +212,7 @@
"source": [
"sim = Simulation.from_config(tardis_config)\n",
"\n",
"model = sim.simulation_state\n",
"simulation_state = sim.simulation_state\n",
"plasma = sim.plasma\n",
"transport = sim.transport"
]
Expand All @@ -233,7 +233,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.t_rad"
"simulation_state.t_radiative"
]
},
{
Expand All @@ -243,7 +243,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.w"
"simulation_state.dilution_factor"
]
},
{
Expand All @@ -253,7 +253,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.t_inner"
"simulation_state.t_inner"
]
},
{
Expand Down Expand Up @@ -344,8 +344,8 @@
"metadata": {},
"outputs": [],
"source": [
"V = model.volume\n",
"Delta_t = transport.calculate_time_of_simulation(model)\n",
"V = simulation_state.volume\n",
"Delta_t = transport.calculate_time_of_simulation(simulation_state)\n",
"prefactor = 1 / (4 * np.pi * V * Delta_t)\n",
"J = prefactor * j_estimator\n",
"J"
Expand Down Expand Up @@ -410,7 +410,7 @@
"metadata": {},
"outputs": [],
"source": [
"t_rad_updated = model.t_rad + d_t_rad * (t_rad_estimated - model.t_rad)\n",
"t_rad_updated = simulation_state.t_radiative + damping_t_radiative * (t_rad_estimated - simulation_state.t_radiative)\n",
"t_rad_updated"
]
},
Expand All @@ -421,8 +421,8 @@
"metadata": {},
"outputs": [],
"source": [
"w_estimated = ( j_estimator / (4 * const.sigma_sb.cgs * t_rad_estimated**4 * V * Delta_t) ).decompose()\n",
"w_estimated"
"dilution_factor_estimated = ( j_estimator / (4 * const.sigma_sb.cgs * t_rad_estimated**4 * V * Delta_t) ).decompose()\n",
"dilution_factor_estimated"
]
},
{
Expand All @@ -432,8 +432,8 @@
"metadata": {},
"outputs": [],
"source": [
"w_updated = model.w + d_w * (w_estimated - model.w)\n",
"w_updated"
"dilution_factor_updated = simulation_state.dilution_factor + damping_dilution_factor * (dilution_factor_estimated - simulation_state.dilution_factor)\n",
"dilution_factor_updated"
]
},
{
Expand Down Expand Up @@ -484,7 +484,7 @@
"metadata": {},
"outputs": [],
"source": [
"t_inner_estimated = model.t_inner * (L_output / L_requested)**t_inner_update_exponent\n",
"t_inner_estimated = simulation_state.t_inner * (L_output / L_requested)**t_inner_update_exponent\n",
"t_inner_estimated"
]
},
Expand All @@ -495,7 +495,7 @@
"metadata": {},
"outputs": [],
"source": [
"t_inner_updated = model.t_inner + d_t_inner * (t_inner_estimated - model.t_inner)\n",
"t_inner_updated = simulation_state.t_inner + damping_t_inner * (t_inner_estimated - simulation_state.t_inner)\n",
"t_inner_updated"
]
},
Expand Down Expand Up @@ -536,7 +536,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.t_rad"
"simulation_state.t_radiative"
]
},
{
Expand All @@ -548,7 +548,7 @@
},
"outputs": [],
"source": [
"model.w"
"simulation_state.dilution_factor"
]
},
{
Expand All @@ -558,7 +558,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.t_inner"
"simulation_state.t_inner"
]
},
{
Expand Down
27 changes: 14 additions & 13 deletions tardis/io/model/hdf.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
from tardis.io.model.model_reader import simulation_state_to_dict


import h5py

from tardis.io.model.model_reader import simulation_state_to_dict


def store_model_to_hdf(model, fname):
def store_simulation_state_to_hdf(simulation_state, fname):
"""
Stores data from SimulationState object into a hdf file.
Parameters
----------
model : tardis.model.SimulationState
filename : str
simulation_state : tardis.model.SimulationState
fname : str
"""
with h5py.File(fname, "a") as f:
model_group = f.require_group("model")
model_group.clear()
simulation_state_group = f.require_group("simulation_state")
simulation_state_group.clear()

model_dict = simulation_state_to_dict(model)
simulation_state_dict = simulation_state_to_dict(simulation_state)

for key, value in model_dict.items():
for key, value in simulation_state_dict.items():
if key.endswith("_cgs"):
model_group.create_dataset(key, data=value[0])
model_group.create_dataset(key + "_unit", data=value[1])
simulation_state_group.create_dataset(key, data=value[0])
simulation_state_group.create_dataset(
key + "_unit", data=value[1]
)
else:
model_group.create_dataset(key, data=value)
simulation_state_group.create_dataset(key, data=value)
Loading

0 comments on commit df439d8

Please sign in to comment.