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 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 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 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 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: 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: 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)