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

[TEP007] Isotope uniform config option #762

Merged
merged 8 commits into from
Jul 14, 2017
39 changes: 28 additions & 11 deletions tardis/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import logging
import numpy as np
import pandas as pd
from pyne import nucname
from astropy import constants, units as u

from tardis.util import quantity_linspace, element_symbol2atomic_number
from tardis.util import quantity_linspace, element_symbol2atomic_number, MalformedElementSymbolError
from tardis.io.model_reader import read_density_file, read_abundances_file
from tardis.io.util import HDFWriterMixin
from density import HomologousDensity
Expand Down Expand Up @@ -59,11 +60,10 @@ class Radial1DModel(HDFWriterMixin):
"""
hdf_properties = ['t_inner', 'w', 't_radiative', 'v_inner', 'v_outer', 'homologous_density']
hdf_name = 'model'

def __init__(self, velocity, homologous_density, abundance, time_explosion,
t_inner, luminosity_requested=None, t_radiative=None,
dilution_factor=None, v_boundary_inner=None,
v_boundary_outer=None):

def __init__(self, velocity, homologous_density, abundance, isotope_abundance,
time_explosion, t_inner, luminosity_requested=None, t_radiative=None,
dilution_factor=None, v_boundary_inner=None, v_boundary_outer=None):
self._v_boundary_inner = None
self._v_boundary_outer = None
self._velocity = None
Expand All @@ -73,6 +73,10 @@ def __init__(self, velocity, homologous_density, abundance, time_explosion,
self.homologous_density = homologous_density
self._abundance = abundance
self.time_explosion = time_explosion

self.raw_abundance = self._abundance
self.raw_isotope_abundance = isotope_abundance

if t_inner is None:
if luminosity_requested is not None:
self.t_inner = ((luminosity_requested /
Expand Down Expand Up @@ -317,17 +321,29 @@ def from_config(cls, config):
t_inner = config.plasma.initial_t_inner

abundances_section = config.model.abundances
isotope_index = pd.MultiIndex(
[[]] * 2, [[]] * 2, names=['atomic_number', 'mass_number'])
isotope_abundance = pd.DataFrame(columns=np.arange(no_of_shells),
index=isotope_index,
dtype=np.float64)

if abundances_section.type == 'uniform':
abundance = pd.DataFrame(columns=np.arange(no_of_shells),
index=pd.Index(np.arange(1, 120),
name='atomic_number'),
dtype=np.float64)

for element_symbol_string in abundances_section:
if element_symbol_string == 'type':
continue
z = element_symbol2atomic_number(element_symbol_string)
abundance.ix[z] = float(abundances_section[element_symbol_string])
try:
Copy link
Member

Choose a reason for hiding this comment

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

maybe this splitting - might be better performed earlier in the process and not in the init of the model (generally keeping logic out of inits is good practice).

z = element_symbol2atomic_number(element_symbol_string)
abundance.ix[z] = float(
abundances_section[element_symbol_string])
except MalformedElementSymbolError:
mass_no = nucname.anum(element_symbol_string)
z = nucname.znum(element_symbol_string)
isotope_abundance.loc[(z, mass_no), :] = float(
abundances_section[element_symbol_string])

elif abundances_section.type == 'file':
if os.path.isabs(abundances_section.filename):
Expand All @@ -342,17 +358,18 @@ def from_config(cls, config):
abundance = abundance.replace(np.nan, 0.0)
abundance = abundance[abundance.sum(axis=1) > 0]

norm_factor = abundance.sum(axis=0)
norm_factor = abundance.sum(axis=0) + isotope_abundance.sum(axis=0)

if np.any(np.abs(norm_factor - 1) > 1e-12):
logger.warning("Abundances have not been normalized to 1."
" - normalizing")
abundance /= norm_factor

isotope_abundance /= norm_factor

return cls(velocity=velocity,
homologous_density=homologous_density,
abundance=abundance,
isotope_abundance=isotope_abundance,
time_explosion=time_explosion,
t_radiative=t_radiative,
t_inner=t_inner,
Expand Down