def setup(self): filename = 'tardis_configv1_artis_density.yml' self.config = Configuration.from_yaml(data_path(filename)) self.config.model.abundances.type = 'file' self.config.model.abundances.filename = 'artis_abundances.dat' self.config.model.abundances.filetype = 'artis' self.model = Radial1DModel.from_config(self.config)
def setup(self): filename = "tardis_configv1_artis_density_v_slice.yml" self.config = Configuration.from_yaml(data_path(filename)) self.config.model.abundances.type = "file" self.config.model.abundances.filename = "artis_abundances.dat" self.config.model.abundances.filetype = "artis" self.model = Radial1DModel.from_config(self.config)
def test_ascii_reader_exponential_law(): filename = "tardis_configv1_density_exponential_test.yml" config = Configuration.from_yaml(data_path(filename)) model = Radial1DModel.from_config(config) expected_densites = [ 5.18114795e-14, 4.45945537e-14, 3.83828881e-14, 3.30364579e-14, 2.84347428e-14, 2.44740100e-14, 2.10649756e-14, 1.81307925e-14, 1.56053177e-14, 1.34316215e-14, 1.15607037e-14, 9.95038990e-15, 8.56437996e-15, 7.37143014e-15, 6.34464872e-15, 5.46088976e-15, 4.70023138e-15, 4.04552664e-15, 3.48201705e-15, 2.99699985e-15, ] expected_unit = "g / (cm3)" assert model.no_of_shells == 20 for i, mdens in enumerate(expected_densites): assert_almost_equal(model.density[i].value, mdens) assert model.density[i].unit == u.Unit(expected_unit)
def test_ascii_reader_power_law(): filename = "tardis_configv1_density_power_law_test.yml" config = Configuration.from_yaml(data_path(filename)) model = Radial1DModel.from_config(config) expected_densites = [ 3.29072513e-14, 2.70357804e-14, 2.23776573e-14, 1.86501954e-14, 1.56435277e-14, 1.32001689e-14, 1.12007560e-14, 9.55397475e-15, 8.18935779e-15, 7.05208050e-15, 6.09916083e-15, 5.29665772e-15, 4.61758699e-15, 4.04035750e-15, 3.54758837e-15, 3.12520752e-15, 2.76175961e-15, 2.44787115e-15, 2.17583442e-15, 1.93928168e-15, ] assert model.no_of_shells == 20 for i, mdens in enumerate(expected_densites): assert_almost_equal(model.density[i].to(u.Unit("g / (cm3)")).value, mdens)
def from_config(cls, config, **kwargs): """ Create a new Simulation instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration **kwargs Allow overriding some structures, such as model, plasma, atomic data and the runner, instead of creating them from the configuration object. Returns ------- Simulation """ # Allow overriding some config structures. This is useful in some # unit tests, and could be extended in all the from_config classmethods. if 'model' in kwargs: model = kwargs['model'] else: model = Radial1DModel.from_config(config) if 'plasma' in kwargs: plasma = kwargs['plasma'] else: plasma = assemble_plasma(config, model, atom_data=kwargs.get('atom_data', None)) if 'runner' in kwargs: runner = kwargs['runner'] else: runner = MontecarloRunner.from_config(config) luminosity_nu_start = config.supernova.luminosity_wavelength_end.to( u.Hz, u.spectral()) try: luminosity_nu_end = config.supernova.luminosity_wavelength_start.to( u.Hz, u.spectral()) except ZeroDivisionError: luminosity_nu_end = np.inf * u.Hz last_no_of_packets = config.montecarlo.last_no_of_packets if last_no_of_packets is None or last_no_of_packets < 0: last_no_of_packets = config.montecarlo.no_of_packets last_no_of_packets = int(last_no_of_packets) return cls(iterations=config.montecarlo.iterations, model=model, plasma=plasma, runner=runner, no_of_packets=int(config.montecarlo.no_of_packets), no_of_virtual_packets=int( config.montecarlo.no_of_virtual_packets), luminosity_nu_start=luminosity_nu_start, luminosity_nu_end=luminosity_nu_end, last_no_of_packets=last_no_of_packets, luminosity_requested=config.supernova.luminosity_requested.cgs, convergence_strategy=config.montecarlo.convergence_strategy, nthreads=config.montecarlo.nthreads)
def test_model_decay(simple_isotope_abundance): filename = "tardis_configv1_verysimple.yml" config = Configuration.from_yaml(data_path(filename)) model = Radial1DModel.from_config(config) model.raw_isotope_abundance = simple_isotope_abundance decayed = simple_isotope_abundance.decay(model.time_explosion).as_atoms() norm_factor = 1.4 assert_almost_equal( model.abundance.loc[8][0], model.raw_abundance.loc[8][0] / norm_factor, decimal=4, ) assert_almost_equal( model.abundance.loc[14][0], (model.raw_abundance.loc[14][0] + decayed.loc[14][0]) / norm_factor, decimal=4, ) assert_almost_equal( model._abundance.loc[12][5], (model.raw_abundance.loc[12][5] + decayed.loc[12][5]) / norm_factor, decimal=4, ) assert_almost_equal( model.abundance.loc[6][12], (decayed.loc[6][12]) / norm_factor, decimal=4, )
def setup(self): filename = 'tardis_configv1_artis_density_v_slice.yml' self.config = Configuration.from_yaml(data_path(filename)) self.config.model.abundances.type = 'file' self.config.model.abundances.filename = 'artis_abundances.dat' self.config.model.abundances.filetype = 'artis' self.model = Radial1DModel.from_config(self.config)
def test_compare_models(model_config_fnames): """Compare identical models produced by .from_config and .from_csvy to check that velocities, densities and abundances (pre and post decay) are the same""" csvy_config_file, old_config_file = model_config_fnames tardis_config = Configuration.from_yaml(csvy_config_file) tardis_config_old = Configuration.from_yaml(old_config_file) csvy_model = Radial1DModel.from_csvy(tardis_config) config_model = Radial1DModel.from_config(tardis_config_old) csvy_model_props = csvy_model.get_properties().keys() config_model_props = config_model.get_properties().keys() npt.assert_array_equal(csvy_model_props, config_model_props) for prop in config_model_props: csvy_model_val = csvy_model.get_properties()[prop] config_model_val = config_model.get_properties()[prop] if prop == "homologous_density": npt.assert_array_almost_equal( csvy_model_val.density_0.value, config_model_val.density_0.value ) npt.assert_array_almost_equal( csvy_model_val.time_0.value, config_model_val.time_0.value ) else: if hasattr(config_model_val, "value"): config_model_val = config_model_val.value csvy_model_val = csvy_model_val.value npt.assert_array_almost_equal(csvy_model_val, config_model_val) assert csvy_model.raw_abundance.shape == config_model.raw_abundance.shape assert ( csvy_model.raw_isotope_abundance.shape == config_model.raw_isotope_abundance.shape ) assert csvy_model.abundance.shape == config_model.abundance.shape npt.assert_array_almost_equal( csvy_model.raw_abundance.to_numpy(), config_model.raw_abundance.to_numpy(), ) npt.assert_array_almost_equal( csvy_model.raw_isotope_abundance.to_numpy(), config_model.raw_isotope_abundance.to_numpy(), ) npt.assert_array_almost_equal( csvy_model.abundance.to_numpy(), config_model.abundance.to_numpy() )
def test_compare_models(full_filename): tardis_config = Configuration.from_yaml(full_filename) csvy_model = Radial1DModel.from_csvy(tardis_config) config_model = Radial1DModel.from_config(tardis_config) csvy_model_props = csvy_model.get_properties().keys() config_model_props = config_model.get_properties().keys() npt.assert_array_equal(csvy_model_props, config_model_props) for prop in config_model_props: csvy_model_val = csvy_model.get_properties()[prop] config_model_val = config_model.get_properties()[prop] if prop == 'homologous_density': npt.assert_array_almost_equal(csvy_model_val.density_0.value, config_model_val.density_0.value) npt.assert_array_almost_equal(csvy_model_val.time_0.value, config_model_val.time_0.value) else: if hasattr(config_model_val, 'value'): config_model_val = config_model_val.value csvy_model_val = csvy_model_val.value npt.assert_array_almost_equal(csvy_model_val, config_model_val)
def test_csvy_abundance(): csvypath = os.path.join(DATA_PATH, 'config_v_filter.yml') config = Configuration.from_yaml(csvypath) csvy_model = Radial1DModel.from_csvy(config) csvy_abund = csvy_model.abundance ref_abund = pd.DataFrame( np.array([[0.35, 0.3, 0.6, 0.4], [0.65, 0.7, 0.4, 0.6]])) ref_abund.index.name = 'atomic_number' ref_abund.index = np.array([1, 2]) assert csvy_abund.equals(ref_abund)
def setup(self): self.atom_data_filename = os.path.expanduser( os.path.expandvars(pytest.config.getvalue('atomic-dataset'))) assert os.path.exists( self.atom_data_filename), ("{0} atomic datafiles" " does not seem to " "exist".format(self.atom_data_filename)) self.config_yaml = yaml.load( open('tardis/io/tests/data/tardis_configv1_verysimple.yml')) self.config_yaml['atom_data'] = self.atom_data_filename tardis_config = Configuration.from_config_dict(self.config_yaml) self.model = Radial1DModel(tardis_config) self.simulation = Simulation(tardis_config) self.simulation.legacy_run_simulation(self.model)
def test_ascii_reader_power_law(): filename = 'tardis_configv1_density_power_law_test.yml' config = Configuration.from_yaml(data_path(filename)) model = Radial1DModel.from_config(config) expected_densites = [3.29072513e-14, 2.70357804e-14, 2.23776573e-14, 1.86501954e-14, 1.56435277e-14, 1.32001689e-14, 1.12007560e-14, 9.55397475e-15, 8.18935779e-15, 7.05208050e-15, 6.09916083e-15, 5.29665772e-15, 4.61758699e-15, 4.04035750e-15, 3.54758837e-15, 3.12520752e-15, 2.76175961e-15, 2.44787115e-15, 2.17583442e-15, 1.93928168e-15] assert model.no_of_shells == 20 for i, mdens in enumerate(expected_densites): assert_almost_equal(model.density[i].to(u.Unit('g / (cm3)')).value, mdens)
def grid_row_to_model(self, row_index): """ Generates a TARDIS Radial1DModel object using the base self.config modified by the specified grid row. Parameters ---------- row_index : int Row index in grid. Returns ------- model : tardis.model.base.Radial1DModel """ rowconfig = self.grid_row_to_config(row_index) model = Radial1DModel.from_config(rowconfig) return model
def test_ascii_reader_exponential_law(): filename = 'tardis_configv1_density_exponential_test.yml' config = Configuration.from_yaml(data_path(filename)) model = Radial1DModel.from_config(config) expected_densites = [5.18114795e-14, 4.45945537e-14, 3.83828881e-14, 3.30364579e-14, 2.84347428e-14, 2.44740100e-14, 2.10649756e-14, 1.81307925e-14, 1.56053177e-14, 1.34316215e-14, 1.15607037e-14, 9.95038990e-15, 8.56437996e-15, 7.37143014e-15, 6.34464872e-15, 5.46088976e-15, 4.70023138e-15, 4.04552664e-15, 3.48201705e-15, 2.99699985e-15] expected_unit = 'g / (cm3)' assert model.no_of_shells == 20 for i, mdens in enumerate(expected_densites): assert_almost_equal(model.density[i].value, mdens) assert model.density[i].unit == u.Unit(expected_unit)
def test_model_decay(simple_isotope_abundance): filename = 'tardis_configv1_verysimple.yml' config = Configuration.from_yaml(data_path(filename)) model = Radial1DModel.from_config(config) model.raw_isotope_abundance = simple_isotope_abundance decayed = simple_isotope_abundance.decay( model.time_explosion).as_atoms() norm_factor = 1.4 assert_almost_equal( model.abundance.loc[8][0], model.raw_abundance.loc[8][0] / norm_factor, decimal=4) assert_almost_equal(model.abundance.loc[14][0], ( model.raw_abundance.loc[14][0] + decayed.loc[14][0]) / norm_factor, decimal=4) assert_almost_equal(model._abundance.loc[12][5], ( model.raw_abundance.loc[12][5] + decayed.loc[12][5]) / norm_factor, decimal=4) assert_almost_equal( model.abundance.loc[6][12], (decayed.loc[6][12]) / norm_factor, decimal=4)
def setup(self): filename = "tardis_configv1_artis_density.yml" self.config = Configuration.from_yaml(data_path(filename)) self.model = Radial1DModel.from_config(self.config)
def raw_model(tardis_config): return Radial1DModel.from_config(tardis_config)
def setup(self): filename = 'tardis_configv1_uniform_density.yml' self.config = Configuration.from_yaml(data_path(filename)) self.config.plasma.initial_t_inner = 2508 * u.K self.model = Radial1DModel.from_config(self.config)
def raw_model(tardis_model_density_config): return Radial1DModel.from_config(tardis_model_density_config)
def setup(self): filename = "paper1_tardis_configv1.yml" self.config = Configuration.from_yaml(data_path(filename)) self.model = Radial1DModel.from_config(self.config)
def setup(self): filename = 'paper1_tardis_configv1.yml' self.config = Configuration.from_yaml(data_path(filename)) self.model = Radial1DModel.from_config(self.config)
def setup(self): filename = "tardis_configv1_uniform_density.yml" self.config = Configuration.from_yaml(data_path(filename)) self.config.plasma.initial_t_inner = 2508 * u.K self.model = Radial1DModel.from_config(self.config)
def setup(self): filename = "tardis_configv1_ascii_density_abund.yml" self.config = Configuration.from_yaml(data_path(filename)) self.config.model.structure.filename = "density.dat" self.config.model.abundances.filename = "abund.dat" self.model = Radial1DModel.from_config(self.config)
def csvy_model_test_abundances(): """Returns Radial1DModel to use to test abundances dataframes""" csvypath = os.path.join(DATA_PATH, "csvy_model_to_test_abundances.yml") config = Configuration.from_yaml(csvypath) csvy_model_test_abundances = Radial1DModel.from_csvy(config) return csvy_model_test_abundances
def setup(self): filename = 'tardis_configv1_ascii_density_abund.yml' self.config = Configuration.from_yaml(data_path(filename)) self.config.model.structure.filename = 'density.dat' self.config.model.abundances.filename = 'abund.dat' self.model = Radial1DModel.from_config(self.config)
def raw_model(tardis_config): return Radial1DModel(tardis_config)
def from_config(cls, config, packet_source=None, virtual_packet_logging=False, **kwargs): """ Create a new Simulation instance from a Configuration object. Parameters ---------- config : tardis.io.config_reader.Configuration **kwargs Allow overriding some structures, such as model, plasma, atomic data and the runner, instead of creating them from the configuration object. Returns ------- Simulation """ # Allow overriding some config structures. This is useful in some # unit tests, and could be extended in all the from_config classmethods. if "model" in kwargs: model = kwargs["model"] else: if hasattr(config, "csvy_model"): model = Radial1DModel.from_csvy(config) else: model = Radial1DModel.from_config(config) if "plasma" in kwargs: plasma = kwargs["plasma"] else: plasma = assemble_plasma(config, model, atom_data=kwargs.get("atom_data", None)) if "runner" in kwargs: if packet_source is not None: raise ConfigurationError( "Cannot specify packet_source and runner at the same time." ) runner = kwargs["runner"] else: runner = MontecarloRunner.from_config( config, packet_source=packet_source, virtual_packet_logging=virtual_packet_logging, ) luminosity_nu_start = config.supernova.luminosity_wavelength_end.to( u.Hz, u.spectral()) if u.isclose(config.supernova.luminosity_wavelength_start, 0 * u.angstrom): luminosity_nu_end = np.inf * u.Hz else: luminosity_nu_end = ( const.c / config.supernova.luminosity_wavelength_start).to( u.Hz) last_no_of_packets = config.montecarlo.last_no_of_packets if last_no_of_packets is None or last_no_of_packets < 0: last_no_of_packets = config.montecarlo.no_of_packets last_no_of_packets = int(last_no_of_packets) return cls( iterations=config.montecarlo.iterations, model=model, plasma=plasma, runner=runner, no_of_packets=int(config.montecarlo.no_of_packets), no_of_virtual_packets=int(config.montecarlo.no_of_virtual_packets), luminosity_nu_start=luminosity_nu_start, luminosity_nu_end=luminosity_nu_end, last_no_of_packets=last_no_of_packets, luminosity_requested=config.supernova.luminosity_requested.cgs, convergence_strategy=config.montecarlo.convergence_strategy, nthreads=config.montecarlo.nthreads, )
def setup(self): filename = 'tardis_configv1_artis_density.yml' self.config = Configuration.from_yaml(data_path(filename)) self.model = Radial1DModel.from_config(self.config)
def setup(self): """Initialize config and model.""" filename = "tardis_configv1_verysimple.yml" self.config = Configuration.from_yaml(data_path(filename)) self.model = Radial1DModel.from_config(self.config)
def setup(self, request, reference, data_path): """ This method does initial setup of creating configuration and performing a single run of integration test. """ # The last component in dirpath can be extracted as name of setup. self.name = data_path['setup_name'] self.config_file = os.path.join(data_path['config_dirpath'], "config.yml") # A quick hack to use atom data per setup. Atom data is ingested from # local HDF or downloaded and cached from a url, depending on data_path # keys. atom_data_name = yaml.load(open(self.config_file))['atom_data'] # Get the path to HDF file: if 'atom_data_url' in data_path: # If the atom data is to be ingested from url: atom_data_filepath = download_file(urlparse.urljoin( base=data_path['atom_data_url'], url=atom_data_name), cache=True ) else: # If the atom data is to be ingested from local file: atom_data_filepath = os.path.join( data_path['atom_data_dirpath'], atom_data_name ) # Load atom data file separately, pass it for forming tardis config. self.atom_data = AtomData.from_hdf5(atom_data_filepath) # Check whether the atom data file in current run and the atom data # file used in obtaining the reference data are same. # TODO: hard coded UUID for kurucz atom data file, generalize it later. # kurucz_data_file_uuid1 = "5ca3035ca8b311e3bb684437e69d75d7" # assert self.atom_data.uuid1 == kurucz_data_file_uuid1 # Create a Configuration through yaml file and atom data. tardis_config = Configuration.from_yaml( self.config_file, atom_data=self.atom_data) # Check whether current run is with less packets. if request.config.getoption("--less-packets"): less_packets = request.config.integration_tests_config['less_packets'] tardis_config['montecarlo']['no_of_packets'] = ( less_packets['no_of_packets'] ) tardis_config['montecarlo']['last_no_of_packets'] = ( less_packets['last_no_of_packets'] ) # We now do a run with prepared config and get radial1d model. self.result = Radial1DModel(tardis_config) # If current test run is just for collecting reference data, store the # output model to HDF file, save it at specified path. Skip all tests. # Else simply perform the run and move further for performing # assertions. if request.config.getoption("--generate-reference"): run_radial1d(self.result, hdf_path_or_buf=os.path.join( data_path['gen_ref_dirpath'], "{0}.h5".format(self.name) )) pytest.skip("Reference data saved at {0}".format( data_path['gen_ref_dirpath'] )) else: run_radial1d(self.result) # Get the reference data through the fixture. self.reference = reference