def test_input_output_with_complex_functions(): my_particle_distribution = Powerlaw() my_particle_distribution.index = -1.52 electrons = ParticleSource('electrons', distribution_shape=my_particle_distribution) # Now set up the synch. spectrum for our source and the source itself synch_spectrum = _ComplexTestFunction() # Use the particle distribution we created as source for the electrons # producing synch. emission synch_spectrum.particle_distribution = my_particle_distribution synch_source = PointSource('synch_source', ra=12.6, dec=-13.5, spectral_shape=synch_spectrum) my_model = Model(electrons, synch_source) my_model.display() my_model.save("__test.yml") new_model = load_model("__test.yml") assert len(new_model.sources) == len(my_model.sources) assert my_particle_distribution.index.value == new_model.electrons.spectrum.main.shape.index.value
def test_display(): mg = ModelGetter() m = mg.model m.display() m.display(complete=True) # Now display a model without free parameters m = Model(PointSource("test", 0.0, 0.0, Powerlaw())) for parameter in m.parameters.values(): parameter.fix = True m.display() # Now display a model without fixed parameters (very unlikely) for parameter in m.parameters.values(): parameter.free = True m.display()
def test_time_domain_integration(): po = Powerlaw() default_powerlaw = Powerlaw() src = PointSource("test", ra=0.0, dec=0.0, spectral_shape=po) m = Model(src) # type: model.Model # Add time independent variable time = IndependentVariable("time", 0.0, u.s) m.add_independent_variable(time) # Now link one of the parameters with a simple line law line = Line() line.a = 0.0 m.link(po.index, time, line) # Test the display just to make sure it doesn't crash m.display() # Now test the average with the integral energies = np.linspace(1, 10, 10) results = m.get_point_source_fluxes(0, energies, tag=(time, 0, 10)) # type: np.ndarray assert np.all(results == 1.0) # Now test the linking of the normalization, first with a constant then with a line with a certain # angular coefficient m.unlink(po.index) po.index.value = default_powerlaw.index.value line2 = Line() line2.a = 0.0 line2.b = 1.0 m.link(po.K, time, line2) time.value = 1.0 results = m.get_point_source_fluxes(0, energies, tag=(time, 0, 10)) assert np.allclose(results, default_powerlaw(energies)) # Now make something actually happen line2.a = 1.0 line2.b = 1.0 results = m.get_point_source_fluxes(0, energies, tag=(time, 0, 10)) # type: np.ndarray # Compare with analytical result def F(x): return line2.a.value / 2.0 * x**2 + line2.b.value * x effective_norm = (F(10) - F(0)) / 10.0 expected_results = default_powerlaw( energies) * effective_norm # type: np.ndarray assert np.allclose(expected_results, results)