Ejemplo n.º 1
0
 def test3(self):
     print("Testing virial radius of an Evrard model.")
     target_number_of_particles = 100
     gas_parts = new_evrard_gas_sphere(target_number_of_particles,
                                       do_scale=True,
                                       seed=1234)
     self.assertAlmostEqual(gas_parts.virial_radius(), 1.00 | nbody.length)
Ejemplo n.º 2
0
def run_evrard(
        hydro_legacy_codes,
        number_of_particles,
        random_seed=None,
        name_of_the_figure="evrard_collapse_test.png"):

    print(
            "\nThe spherical collapse of an initially-cold adiabatic gas cloud,\n",
            "consisting of ",
            str(number_of_particles),
            "particles will be simulated...\n"
            )

    # (3 natural timescales)
    t_end = 3.0 * convert_nbody_units.to_si(1.0 | nbody_system.time)
    print "Evolving to (3 natural timescales): ", t_end.as_quantity_in(
        units.Myr)
    n_steps = 100

    gas = new_evrard_gas_sphere(
        number_of_particles,
        convert_nbody_units,
        do_scale=True,
        seed=random_seed)
    gas.h_smooth = 0.01 | units.kpc

    times = [] | units.Myr
    kinetic_energies = []
    potential_energies = []
    thermal_energies = []

    for hydro_legacy_code in hydro_legacy_codes:
        try:
            hydro_legacy_code.parameters.timestep = t_end / n_steps
        except Exception as exc:
            if "parameter is read-only" not in str(exc):
                raise

        hydro_legacy_code.gas_particles.add_particles(gas)

        kinetic_energies.append([] | units.J)
        potential_energies.append([] | units.J)
        thermal_energies.append([] | units.J)

    for time in [i*t_end/n_steps for i in range(1, n_steps+1)]:
        for i, hydro_legacy_code in enumerate(hydro_legacy_codes):
            hydro_legacy_code.evolve_model(time)
            kinetic_energies[i].append(hydro_legacy_code.kinetic_energy)
            potential_energies[i].append(hydro_legacy_code.potential_energy)
            thermal_energies[i].append(hydro_legacy_code.thermal_energy)
        times.append(time)
    for hydro_legacy_code in hydro_legacy_codes:
        hydro_legacy_code.stop()
    energy_plot(times, kinetic_energies, potential_energies,
                thermal_energies, name_of_the_figure)
    print "All done!\n"
Ejemplo n.º 3
0
 def test2(self):
     print "Testing properties of an Evrard model."
     target_number_of_particles = 1000
     gas_parts = new_evrard_gas_sphere(target_number_of_particles, do_scale=True, seed=1234)
     self.assertEquals(len(gas_parts), 1000)
     self.assertAlmostEqual(gas_parts.kinetic_energy(),             0.00 | nbody.energy)
     self.assertAlmostEqual(gas_parts.potential_energy(G=nbody.G), -0.50 | nbody.energy)
     self.assertAlmostEqual(gas_parts.center_of_mass(),          [0,0,0] | nbody.length)
     self.assertAlmostEqual(gas_parts.center_of_mass_velocity(), [0,0,0] | nbody.speed)
     self.assertAlmostEqual(gas_parts.mass.sum(),                   1.00 | nbody.mass)
Ejemplo n.º 4
0
def run_evrard(hydro_legacy_codes,
               number_of_particles,
               random_seed=None,
               name_of_the_figure="evrard_collapse_test.png"):

    print(
        "\nThe spherical collapse of an initially-cold adiabatic gas cloud,\n",
        "consisting of ", str(number_of_particles),
        "particles will be simulated...\n")

    # (3 natural timescales)
    t_end = 3.0 * convert_nbody_units.to_si(1.0 | nbody_system.time)
    print("Evolving to (3 natural timescales): ",
          t_end.as_quantity_in(units.Myr))
    n_steps = 100

    gas = new_evrard_gas_sphere(number_of_particles,
                                convert_nbody_units,
                                do_scale=True,
                                seed=random_seed)
    gas.h_smooth = 0.01 | units.kpc

    times = [] | units.Myr
    kinetic_energies = []
    potential_energies = []
    thermal_energies = []

    for hydro_legacy_code in hydro_legacy_codes:
        try:
            hydro_legacy_code.parameters.timestep = t_end / n_steps
        except Exception as exc:
            if "parameter is read-only" not in str(exc):
                raise

        hydro_legacy_code.gas_particles.add_particles(gas)

        kinetic_energies.append([] | units.J)
        potential_energies.append([] | units.J)
        thermal_energies.append([] | units.J)

    for time in [i * t_end / n_steps for i in range(1, n_steps + 1)]:
        for i, hydro_legacy_code in enumerate(hydro_legacy_codes):
            hydro_legacy_code.evolve_model(time)
            kinetic_energies[i].append(hydro_legacy_code.kinetic_energy)
            potential_energies[i].append(hydro_legacy_code.potential_energy)
            thermal_energies[i].append(hydro_legacy_code.thermal_energy)
        times.append(time)
    for hydro_legacy_code in hydro_legacy_codes:
        hydro_legacy_code.stop()
    energy_plot(times, kinetic_energies, potential_energies, thermal_energies,
                name_of_the_figure)
    print("All done!\n")
Ejemplo n.º 5
0
    def test3(self):
        print("Demonstrate new_sink_particles usage (using Gadget2)")
        UnitLength = 1.0 | units.kpc
        UnitMass = 1.0e10 | units.MSun
        UnitVelocity = 1.0 | units.km / units.s
        convert_nbody = nbody_system.nbody_to_si(UnitLength, UnitMass)
        converter = ConvertBetweenGenericAndSiUnits(UnitLength, UnitMass, UnitVelocity)
        number_gas_particles = 1000
        gas = new_evrard_gas_sphere(number_gas_particles, convert_nbody, do_scale=True, seed=12345)

        sph_code = Gadget2(converter)
        sph_code.initialize_code()
        sph_code.parameters.stopping_condition_maximum_density = 10 * UnitMass / UnitLength**3
        sph_code.gas_particles.add_particles(gas)
        self.assertIsOfOrder(max(sph_code.gas_particles.density), UnitMass / UnitLength**3)

        density_limit_detection = sph_code.stopping_conditions.density_limit_detection
        density_limit_detection.enable()

        sph_code.evolve_model(10.0 | units.Myr)
        self.assertTrue(density_limit_detection.is_set())
        self.assertTrue(sph_code.model_time < 10.0 | units.Myr)
        print("density_limit exceeded at t =", sph_code.model_time.as_quantity_in(units.Myr))
        self.assertEqual(len(density_limit_detection.particles()), 1)
        self.assertTrue(density_limit_detection.particles().density >
                10 * UnitMass / UnitLength**3)

        clumps = density_limit_detection.particles().copy()
        sph_code.gas_particles.remove_particles(clumps)
        clumps_in_code = sph_code.dm_particles.add_particles(clumps)

        sinks = new_sink_particles(clumps_in_code,looping_over=self.looping_over)
        self.assertEqual(sinks.sink_radius, clumps.radius)
        self.assertAlmostRelativeEqual(sinks.mass, UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sinks.position, clumps.position, 10)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 1)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)
        self.assertEqual(set(sinks.get_attribute_names_defined_in_store()) - set(["sink_radius","lx","ly","lz"]),
            set(sph_code.particles.get_attribute_names_defined_in_store()))

        sinks.accrete(sph_code.gas_particles)
        self.assertAlmostRelativeEqual(sinks.mass, 3 * UnitMass / number_gas_particles, 10)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 3)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)

        sinks.accrete(sph_code.particles) # Nothing happens: gas already gone, and cannot accrete itself
        self.assertAlmostRelativeEqual(sinks.mass, 3 * UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)

        steps = 0
        while True:
            sph_code.evolve_model(sph_code.model_time + (0.1 | units.Myr))
            sinks.sink_radius = 4 * clumps_in_code.radius
            sinks.accrete(sph_code.gas_particles)
            steps += 1
            if density_limit_detection.is_set():
                break

        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 7)
        self.assertAlmostRelativeEqual(sinks.mass, 7 * UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)

        self.assertTrue(density_limit_detection.is_set())
        self.assertEqual(steps, 5)
        self.assertTrue(sph_code.model_time < 10.0 | units.Myr)
        print("density_limit exceeded at t =", sph_code.model_time.as_quantity_in(units.Myr))
        self.assertEqual(len(density_limit_detection.particles()), 5)
        self.assertTrue((density_limit_detection.particles().density >
                10 * UnitMass / UnitLength**3).all())

        clumps = density_limit_detection.particles().copy()
        sph_code.gas_particles.remove_particles(clumps)
        clumps_in_code = sph_code.dm_particles.add_particles(clumps)

        sinks.add_sinks(clumps_in_code, sink_radius=0.1|units.kpc)
        self.assertEqual(sinks[1:].sink_radius, 0.1 | units.kpc)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 12)
        self.assertAlmostRelativeEqual(sinks.mass[1:], UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sinks.position[1:], clumps.position, 10)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)

        sinks.accrete(sph_code.gas_particles)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 66)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass().as_quantity_in(units.MSun), UnitMass, 10)
        self.assertAlmostRelativeEqual(sinks.mass, [7.0, 13.0, 15.0, 9.0, 11.0, 11.0] * UnitMass / number_gas_particles, 10)
Ejemplo n.º 6
0
 def test1(self):
     print("First test: making an Evrard gas sphere model.")
     target_number_of_particles = 1000
     gas_parts = new_evrard_gas_sphere(target_number_of_particles,
                                       seed=1234)
     self.assertEqual(len(gas_parts), 1000)
Ejemplo n.º 7
0
    def test3(self):
        print "Demonstrate new_sink_particles usage (using Gadget2)"
        UnitLength = 1.0 | units.kpc
        UnitMass = 1.0e10 | units.MSun
        UnitVelocity = 1.0 | units.km / units.s
        convert_nbody = nbody_system.nbody_to_si(UnitLength, UnitMass)
        converter = ConvertBetweenGenericAndSiUnits(UnitLength, UnitMass, UnitVelocity)
        number_gas_particles = 1000
        gas = new_evrard_gas_sphere(number_gas_particles, convert_nbody, do_scale=True, seed=12345)

        sph_code = Gadget2(converter)
        sph_code.initialize_code()
        sph_code.parameters.stopping_condition_maximum_density = 10 * UnitMass / UnitLength**3
        sph_code.gas_particles.add_particles(gas)
        self.assertIsOfOrder(max(sph_code.gas_particles.density), UnitMass / UnitLength**3)

        density_limit_detection = sph_code.stopping_conditions.density_limit_detection
        density_limit_detection.enable()

        sph_code.evolve_model(10.0 | units.Myr)
        self.assertTrue(density_limit_detection.is_set())
        self.assertTrue(sph_code.model_time < 10.0 | units.Myr)
        print "density_limit exceeded at t =", sph_code.model_time.as_quantity_in(units.Myr)
        self.assertEquals(len(density_limit_detection.particles()), 1)
        self.assertTrue(density_limit_detection.particles().density >
                10 * UnitMass / UnitLength**3)

        clumps = density_limit_detection.particles().copy()
        sph_code.gas_particles.remove_particles(clumps)
        clumps_in_code = sph_code.dm_particles.add_particles(clumps)

        sinks = new_sink_particles(clumps_in_code,looping_over=self.looping_over)
        self.assertEqual(sinks.sink_radius, clumps.radius)
        self.assertAlmostRelativeEqual(sinks.mass, UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sinks.position, clumps.position, 10)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 1)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)
        self.assertEqual(set(sinks.get_attribute_names_defined_in_store()) - set(["sink_radius","lx","ly","lz"]),
            set(sph_code.particles.get_attribute_names_defined_in_store()))

        sinks.accrete(sph_code.gas_particles)
        self.assertAlmostRelativeEqual(sinks.mass, 3 * UnitMass / number_gas_particles, 10)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 3)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)

        sinks.accrete(sph_code.particles) # Nothing happens: gas already gone, and cannot accrete itself
        self.assertAlmostRelativeEqual(sinks.mass, 3 * UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)

        steps = 0
        while True:
            sph_code.evolve_model(sph_code.model_time + (0.1 | units.Myr))
            sinks.sink_radius = 4 * clumps_in_code.radius
            sinks.accrete(sph_code.gas_particles)
            steps += 1
            if density_limit_detection.is_set():
                break

        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 7)
        self.assertAlmostRelativeEqual(sinks.mass, 7 * UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)

        self.assertTrue(density_limit_detection.is_set())
        self.assertEqual(steps, 5)
        self.assertTrue(sph_code.model_time < 10.0 | units.Myr)
        print "density_limit exceeded at t =", sph_code.model_time.as_quantity_in(units.Myr)
        self.assertEquals(len(density_limit_detection.particles()), 5)
        self.assertTrue((density_limit_detection.particles().density >
                10 * UnitMass / UnitLength**3).all())

        clumps = density_limit_detection.particles().copy()
        sph_code.gas_particles.remove_particles(clumps)
        clumps_in_code = sph_code.dm_particles.add_particles(clumps)

        sinks.add_sinks(clumps_in_code, sink_radius=0.1|units.kpc)
        self.assertEqual(sinks[1:].sink_radius, 0.1 | units.kpc)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 12)
        self.assertAlmostRelativeEqual(sinks.mass[1:], UnitMass / number_gas_particles, 10)
        self.assertAlmostRelativeEqual(sinks.position[1:], clumps.position, 10)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass(), UnitMass, 10)
        self.assertAlmostRelativeEqual(sph_code.gas_particles.total_mass(), UnitMass - sinks.total_mass(), 10)

        sinks.accrete(sph_code.gas_particles)
        self.assertEqual(len(sph_code.gas_particles), number_gas_particles - 66)
        self.assertAlmostRelativeEqual(sph_code.particles.total_mass().as_quantity_in(units.MSun), UnitMass, 10)
        self.assertAlmostRelativeEqual(sinks.mass, [7.0, 13.0, 15.0, 9.0, 11.0, 11.0] * UnitMass / number_gas_particles, 10)
Ejemplo n.º 8
0
 def test1(self):
     print "First test: making an Evrard gas sphere model."
     target_number_of_particles = 1000
     gas_parts = new_evrard_gas_sphere(target_number_of_particles, seed=1234)
     self.assertEquals(len(gas_parts), 1000)
Ejemplo n.º 9
0
 def test3(self):
     print "Testing virial radius of an Evrard model."
     target_number_of_particles = 100
     gas_parts = new_evrard_gas_sphere(target_number_of_particles, do_scale=True, seed=1234)
     self.assertAlmostEqual(gas_parts.virial_radius(),             1.00 | nbody.length)