def from_config(cls, config): """ Create a new Radial1DModel instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration Returns ------- Radial1DModel """ time_explosion = config.supernova.time_explosion.cgs structure = config.model.structure electron_densities = None temperature = None if structure.type == 'specific': velocity = quantity_linspace(structure.velocity.start, structure.velocity.stop, structure.velocity.num + 1).cgs homologous_density = HomologousDensity.from_config(config) elif structure.type == 'file': if os.path.isabs(structure.filename): structure_fname = structure.filename else: structure_fname = os.path.join(config.config_dirname, structure.filename) time_0, velocity, density_0, electron_densities, temperature = read_density_file( structure_fname, structure.filetype) density_0 = density_0.insert(0, 0) homologous_density = HomologousDensity(density_0, time_0) else: raise NotImplementedError # Note: This is the number of shells *without* taking in mind the # v boundaries. no_of_shells = len(velocity) - 1 if temperature: t_radiative = temperature elif config.plasma.initial_t_rad > 0 * u.K: t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad else: t_radiative = None if config.plasma.initial_t_inner < 0.0 * u.K: luminosity_requested = config.supernova.luminosity_requested t_inner = None else: luminosity_requested = None t_inner = config.plasma.initial_t_inner abundances_section = config.model.abundances isotope_abundance = pd.DataFrame() if abundances_section.type == 'uniform': abundance, isotope_abundance = read_uniform_abundances( abundances_section, no_of_shells) elif abundances_section.type == 'file': if os.path.isabs(abundances_section.filename): abundances_fname = abundances_section.filename else: abundances_fname = os.path.join(config.config_dirname, abundances_section.filename) index, abundance, isotope_abundance = read_abundances_file( abundances_fname, abundances_section.filetype) abundance = abundance.replace(np.nan, 0.0) abundance = abundance[abundance.sum(axis=1) > 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 isotope_abundance = IsotopeAbundances(isotope_abundance) 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, luminosity_requested=luminosity_requested, dilution_factor=None, v_boundary_inner=structure.get('v_inner_boundary', None), v_boundary_outer=structure.get('v_outer_boundary', None), electron_densities=electron_densities)
def from_csvy(cls, config): """ Create a new Radial1DModel instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration Returns ------- Radial1DModel """ if os.path.isabs(config.csvy_model): csvy_model_fname = config.csvy_model else: csvy_model_fname = os.path.join(config.config_dirname, config.csvy_model) csvy_model_config, csvy_model_data = load_csvy(csvy_model_fname) base_dir = os.path.abspath(os.path.dirname(__file__)) schema_dir = os.path.join(base_dir, '..', 'io', 'schemas') csvy_schema_file = os.path.join(schema_dir, 'csvy_model.yml') csvy_model_config = Configuration( validate_dict(csvy_model_config, schemapath=csvy_schema_file)) time_explosion = config.supernova.time_explosion.cgs electron_densities = None temperature = None if hasattr(csvy_model_config, 'v_inner_boundary'): v_boundary_inner = csvy_model_config.v_inner_boundary else: v_boundary_inner = None if hasattr(csvy_model_config, 'v_outer_boundary'): v_boundary_outer = csvy_model_config.v_outer_boundary else: v_boundary_outer = None if hasattr(csvy_model_config, 'velocity'): velocity = quantity_linspace(csvy_model_config.velocity.start, csvy_model_config.velocity.stop, csvy_model_config.velocity.num + 1).cgs else: velocity_field_index = [ field['name'] for field in csvy_model_config.datatype.fields ].index('velocity') velocity_unit = u.Unit( csvy_model_config.datatype.fields[velocity_field_index] ['unit']) velocity = csvy_model_data['velocity'].values * velocity_unit velocity = velocity.to('cm/s') if hasattr(csvy_model_config, 'density'): homologous_density = HomologousDensity.from_csvy( config, csvy_model_config) else: time_0 = csvy_model_config.model_density_time_0 density_field_index = [ field['name'] for field in csvy_model_config.datatype.fields ].index('density') density_unit = u.Unit( csvy_model_config.datatype.fields[density_field_index]['unit']) density_0 = csvy_model_data['density'].values * density_unit density_0 = density_0.to('g/cm^3')[1:] density_0 = density_0.insert(0, 0) homologous_density = HomologousDensity(density_0, time_0) no_of_shells = len(velocity) - 1 # TODO -- implement t_radiative #t_radiative = None if temperature: t_radiative = temperature elif hasattr(csvy_model_data, 'columns'): if 't_rad' in csvy_model_data.columns: t_rad_field_index = [ field['name'] for field in csvy_model_config.datatype.fields ].index('t_rad') t_rad_unit = u.Unit( csvy_model_config.datatype.fields[t_rad_field_index] ['unit']) t_radiative = csvy_model_data['t_rad'].iloc[ 0:].values * t_rad_unit else: t_radiative = None dilution_factor = None if hasattr(csvy_model_data, 'columns'): if 'w' in csvy_model_data.columns: dilution_factor = csvy_model_data['w'].iloc[0:].to_numpy() elif config.plasma.initial_t_rad > 0 * u.K: t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad else: t_radiative = None if config.plasma.initial_t_inner < 0.0 * u.K: luminosity_requested = config.supernova.luminosity_requested t_inner = None else: luminosity_requested = None t_inner = config.plasma.initial_t_inner if hasattr(csvy_model_config, 'abundance'): abundances_section = csvy_model_config.abundance abundance, isotope_abundance = read_uniform_abundances( abundances_section, no_of_shells) else: index, abundance, isotope_abundance = parse_csv_abundances( csvy_model_data) abundance = abundance.replace(np.nan, 0.0) abundance = abundance[abundance.sum(axis=1) > 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 #isotope_abundance = IsotopeAbundances(isotope_abundance) isotope_abundance = IsotopeAbundances( isotope_abundance, time_0=csvy_model_config.model_isotope_time_0) #isotope_abundance.time_0 = csvy_model_config.model_isotope_time_0 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, luminosity_requested=luminosity_requested, dilution_factor=dilution_factor, v_boundary_inner=v_boundary_inner, v_boundary_outer=v_boundary_outer, electron_densities=electron_densities)
def from_config(cls, config): """ Create a new Radial1DModel instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration Returns ------- Radial1DModel """ time_explosion = config.supernova.time_explosion.cgs structure = config.model.structure electron_densities = None temperature = None if structure.type == 'specific': velocity = quantity_linspace(structure.velocity.start, structure.velocity.stop, structure.velocity.num + 1).cgs homologous_density = HomologousDensity.from_config(config) elif structure.type == 'file': if os.path.isabs(structure.filename): structure_fname = structure.filename else: structure_fname = os.path.join(config.config_dirname, structure.filename) time_0, velocity, density_0, electron_densities, temperature = read_density_file( structure_fname, structure.filetype) density_0 = density_0.insert(0, 0) homologous_density = HomologousDensity(density_0, time_0) else: raise NotImplementedError # Note: This is the number of shells *without* taking in mind the # v boundaries. no_of_shells = len(velocity) - 1 if temperature: t_radiative = temperature elif config.plasma.initial_t_rad > 0 * u.K: t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad else: t_radiative = None if config.plasma.initial_t_inner < 0.0 * u.K: luminosity_requested = config.supernova.luminosity_requested t_inner = None else: luminosity_requested = None t_inner = config.plasma.initial_t_inner abundances_section = config.model.abundances isotope_abundance = pd.DataFrame() if abundances_section.type == 'uniform': abundance, isotope_abundance = read_uniform_abundances( abundances_section, no_of_shells) elif abundances_section.type == 'file': if os.path.isabs(abundances_section.filename): abundances_fname = abundances_section.filename else: abundances_fname = os.path.join(config.config_dirname, abundances_section.filename) index, abundance, isotope_abundance = read_abundances_file(abundances_fname, abundances_section.filetype) abundance = abundance.replace(np.nan, 0.0) abundance = abundance[abundance.sum(axis=1) > 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 isotope_abundance = IsotopeAbundances(isotope_abundance) 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, luminosity_requested=luminosity_requested, dilution_factor=None, v_boundary_inner=structure.get('v_inner_boundary', None), v_boundary_outer=structure.get('v_outer_boundary', None), electron_densities=electron_densities)
def from_csvy(cls, config): """ Create a new Radial1DModel instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration Returns ------- Radial1DModel """ CSVY_SUPPORTED_COLUMNS = { 'velocity', 'density', 't_rad', 'dilution_factor' } if os.path.isabs(config.csvy_model): csvy_model_fname = config.csvy_model else: csvy_model_fname = os.path.join(config.config_dirname, config.csvy_model) csvy_model_config, csvy_model_data = load_csvy(csvy_model_fname) base_dir = os.path.abspath(os.path.dirname(__file__)) schema_dir = os.path.join(base_dir, '..', 'io', 'schemas') csvy_schema_file = os.path.join(schema_dir, 'csvy_model.yml') csvy_model_config = Configuration( validate_dict(csvy_model_config, schemapath=csvy_schema_file)) if hasattr(csvy_model_data, 'columns'): abund_names = set([ name for name in csvy_model_data.columns if nucname.iselement(name) or nucname.isnuclide(name) ]) unsupported_columns = set( csvy_model_data.columns) - abund_names - CSVY_SUPPORTED_COLUMNS field_names = set( [field['name'] for field in csvy_model_config.datatype.fields]) assert set(csvy_model_data.columns) - field_names == set(),\ 'CSVY columns exist without field descriptions' assert field_names - set(csvy_model_data.columns) == set(),\ 'CSVY field descriptions exist without corresponding csv data' if unsupported_columns != set(): logger.warning( "The following columns are specified in the csvy" "model file, but are IGNORED by TARDIS: %s" % (str(unsupported_columns))) time_explosion = config.supernova.time_explosion.cgs electron_densities = None temperature = None #if hasattr(csvy_model_config, 'v_inner_boundary'): # v_boundary_inner = csvy_model_config.v_inner_boundary #else: # v_boundary_inner = None #if hasattr(csvy_model_config, 'v_outer_boundary'): # v_boundary_outer = csvy_model_config.v_outer_boundary #else: # v_boundary_outer = None if hasattr(config, 'model'): if hasattr(config.model, 'v_inner_boundary'): v_boundary_inner = config.model.v_inner_boundary else: v_boundary_inner = None if hasattr(config.model, 'v_outer_boundary'): v_boundary_outer = config.model.v_outer_boundary else: v_boundary_outer = None else: v_boundary_inner = None v_boundary_outer = None if hasattr(csvy_model_config, 'velocity'): velocity = quantity_linspace(csvy_model_config.velocity.start, csvy_model_config.velocity.stop, csvy_model_config.velocity.num + 1).cgs else: velocity_field_index = [ field['name'] for field in csvy_model_config.datatype.fields ].index('velocity') velocity_unit = u.Unit( csvy_model_config.datatype.fields[velocity_field_index] ['unit']) velocity = csvy_model_data['velocity'].values * velocity_unit velocity = velocity.to('cm/s') if hasattr(csvy_model_config, 'density'): homologous_density = HomologousDensity.from_csvy( config, csvy_model_config) else: time_0 = csvy_model_config.model_density_time_0 density_field_index = [ field['name'] for field in csvy_model_config.datatype.fields ].index('density') density_unit = u.Unit( csvy_model_config.datatype.fields[density_field_index]['unit']) density_0 = csvy_model_data['density'].values * density_unit density_0 = density_0.to('g/cm^3')[1:] density_0 = density_0.insert(0, 0) homologous_density = HomologousDensity(density_0, time_0) no_of_shells = len(velocity) - 1 # TODO -- implement t_radiative #t_radiative = None if temperature: t_radiative = temperature elif hasattr(csvy_model_data, 'columns'): if 't_rad' in csvy_model_data.columns: t_rad_field_index = [ field['name'] for field in csvy_model_config.datatype.fields ].index('t_rad') t_rad_unit = u.Unit( csvy_model_config.datatype.fields[t_rad_field_index] ['unit']) t_radiative = csvy_model_data['t_rad'].iloc[ 0:].values * t_rad_unit else: t_radiative = None dilution_factor = None if hasattr(csvy_model_data, 'columns'): if 'dilution_factor' in csvy_model_data.columns: dilution_factor = csvy_model_data['dilution_factor'].iloc[ 0:].to_numpy() elif config.plasma.initial_t_rad > 0 * u.K: t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad else: t_radiative = None if config.plasma.initial_t_inner < 0.0 * u.K: luminosity_requested = config.supernova.luminosity_requested t_inner = None else: luminosity_requested = None t_inner = config.plasma.initial_t_inner if hasattr(csvy_model_config, 'abundance'): abundances_section = csvy_model_config.abundance abundance, isotope_abundance = read_uniform_abundances( abundances_section, no_of_shells) else: index, abundance, isotope_abundance = parse_csv_abundances( csvy_model_data) abundance = abundance.replace(np.nan, 0.0) abundance = abundance[abundance.sum(axis=1) > 0] abundance = abundance.loc[:, 1:] abundance.columns = np.arange(abundance.shape[1]) 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 #isotope_abundance = IsotopeAbundances(isotope_abundance) isotope_abundance = IsotopeAbundances( isotope_abundance, time_0=csvy_model_config.model_isotope_time_0) #isotope_abundance.time_0 = csvy_model_config.model_isotope_time_0 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, luminosity_requested=luminosity_requested, dilution_factor=dilution_factor, v_boundary_inner=v_boundary_inner, v_boundary_outer=v_boundary_outer, electron_densities=electron_densities)
def from_csvy(cls, config): """ Create a new Radial1DModel instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration Returns ------- Radial1DModel """ CSVY_SUPPORTED_COLUMNS = { "velocity", "density", "t_rad", "dilution_factor", } if os.path.isabs(config.csvy_model): csvy_model_fname = config.csvy_model else: csvy_model_fname = os.path.join( config.config_dirname, config.csvy_model ) csvy_model_config, csvy_model_data = load_csvy(csvy_model_fname) base_dir = os.path.abspath(os.path.dirname(__file__)) schema_dir = os.path.join(base_dir, "..", "io", "schemas") csvy_schema_file = os.path.join(schema_dir, "csvy_model.yml") csvy_model_config = Configuration( validate_dict(csvy_model_config, schemapath=csvy_schema_file) ) if hasattr(csvy_model_data, "columns"): abund_names = set( [ name for name in csvy_model_data.columns if is_valid_nuclide_or_elem(name) ] ) unsupported_columns = ( set(csvy_model_data.columns) - abund_names - CSVY_SUPPORTED_COLUMNS ) field_names = set( [field["name"] for field in csvy_model_config.datatype.fields] ) assert ( set(csvy_model_data.columns) - field_names == set() ), "CSVY columns exist without field descriptions" assert ( field_names - set(csvy_model_data.columns) == set() ), "CSVY field descriptions exist without corresponding csv data" if unsupported_columns != set(): logger.warning( "The following columns are " "specified in the csvy model file," f" but are IGNORED by TARDIS: {str(unsupported_columns)}" ) time_explosion = config.supernova.time_explosion.cgs electron_densities = None temperature = None # if hasattr(csvy_model_config, 'v_inner_boundary'): # v_boundary_inner = csvy_model_config.v_inner_boundary # else: # v_boundary_inner = None # if hasattr(csvy_model_config, 'v_outer_boundary'): # v_boundary_outer = csvy_model_config.v_outer_boundary # else: # v_boundary_outer = None if hasattr(config, "model"): if hasattr(config.model, "v_inner_boundary"): v_boundary_inner = config.model.v_inner_boundary else: v_boundary_inner = None if hasattr(config.model, "v_outer_boundary"): v_boundary_outer = config.model.v_outer_boundary else: v_boundary_outer = None else: v_boundary_inner = None v_boundary_outer = None if hasattr(csvy_model_config, "velocity"): velocity = quantity_linspace( csvy_model_config.velocity.start, csvy_model_config.velocity.stop, csvy_model_config.velocity.num + 1, ).cgs else: velocity_field_index = [ field["name"] for field in csvy_model_config.datatype.fields ].index("velocity") velocity_unit = u.Unit( csvy_model_config.datatype.fields[velocity_field_index]["unit"] ) velocity = csvy_model_data["velocity"].values * velocity_unit velocity = velocity.to("cm/s") if hasattr(csvy_model_config, "density"): homologous_density = HomologousDensity.from_csvy( config, csvy_model_config ) else: time_0 = csvy_model_config.model_density_time_0 density_field_index = [ field["name"] for field in csvy_model_config.datatype.fields ].index("density") density_unit = u.Unit( csvy_model_config.datatype.fields[density_field_index]["unit"] ) density_0 = csvy_model_data["density"].values * density_unit density_0 = density_0.to("g/cm^3")[1:] density_0 = density_0.insert(0, 0) homologous_density = HomologousDensity(density_0, time_0) no_of_shells = len(velocity) - 1 # TODO -- implement t_radiative # t_radiative = None if temperature: t_radiative = temperature elif hasattr(csvy_model_data, "columns"): if "t_rad" in csvy_model_data.columns: t_rad_field_index = [ field["name"] for field in csvy_model_config.datatype.fields ].index("t_rad") t_rad_unit = u.Unit( csvy_model_config.datatype.fields[t_rad_field_index]["unit"] ) t_radiative = ( csvy_model_data["t_rad"].iloc[0:].values * t_rad_unit ) else: t_radiative = None dilution_factor = None if hasattr(csvy_model_data, "columns"): if "dilution_factor" in csvy_model_data.columns: dilution_factor = ( csvy_model_data["dilution_factor"].iloc[0:].to_numpy() ) elif config.plasma.initial_t_rad > 0 * u.K: t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad else: t_radiative = None if config.plasma.initial_t_inner < 0.0 * u.K: luminosity_requested = config.supernova.luminosity_requested t_inner = None else: luminosity_requested = None t_inner = config.plasma.initial_t_inner if hasattr(csvy_model_config, "abundance"): abundances_section = csvy_model_config.abundance abundance, isotope_abundance = read_uniform_abundances( abundances_section, no_of_shells ) else: index, abundance, isotope_abundance = parse_csv_abundances( csvy_model_data ) abundance = abundance.loc[:, 1:] abundance.columns = np.arange(abundance.shape[1]) isotope_abundance = isotope_abundance.loc[:, 1:] isotope_abundance.columns = np.arange(isotope_abundance.shape[1]) abundance = abundance.replace(np.nan, 0.0) abundance = abundance[abundance.sum(axis=1) > 0] isotope_abundance = isotope_abundance.replace(np.nan, 0.0) isotope_abundance = isotope_abundance[isotope_abundance.sum(axis=1) > 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 # isotope_abundance = IsotopeAbundances(isotope_abundance) isotope_abundance = IsotopeAbundances( isotope_abundance, time_0=csvy_model_config.model_isotope_time_0 ) # isotope_abundance.time_0 = csvy_model_config.model_isotope_time_0 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, luminosity_requested=luminosity_requested, dilution_factor=dilution_factor, v_boundary_inner=v_boundary_inner, v_boundary_outer=v_boundary_outer, electron_densities=electron_densities, )