def slowtest8(self): print "Test for obtaining the stellar composition structure - evolved star" stars = Particles(1) stars.mass = 1.0 | units.MSun instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.particles.add_particles(stars) instance.commit_particles() instance.evolve_model(11.7 | units.Gyr) self.assertTrue(instance.particles[0].age >= 11.7 | units.Gyr) print instance.particles[0].stellar_type #~ self.assertTrue(str(instance.particles[0].stellar_type) == "First Giant Branch") number_of_zones = instance.particles.get_number_of_zones()[0] number_of_species = instance.particles.get_number_of_species()[0] composition = instance.particles[0].get_chemical_abundance_profiles() species_names = instance.particles[0].get_names_of_species() self.assertEquals(number_of_zones, 199) self.assertEquals(number_of_species, 9) self.assertEquals(len(species_names), number_of_species) self.assertEquals(len(composition), number_of_species) self.assertEquals(len(composition[0]), number_of_zones) self.assertEquals(species_names, ['h1', 'he4', 'c12', 'n14', 'o16', 'ne20', 'mg24', 'si28', 'fe56']) self.assertAlmostRelativeEquals(composition[0, -1], 0.7 | units.none, 1) self.assertAlmostRelativeEquals(composition[1, -1], 0.3 - instance.parameters.metallicity, 1) self.assertAlmostRelativeEquals(composition[2:,-1].sum(), instance.parameters.metallicity, 1) self.assertAlmostEquals(composition.sum(axis=0), [1.0]*number_of_zones | units.none) self.assertAlmostEquals(composition[0, 0], 0.00 | units.none) self.assertAlmostEquals(composition[1, 0], 1.00 - instance.parameters.metallicity, 3) self.assertAlmostEquals(composition[2:,0].sum(), instance.parameters.metallicity, 3) instance.stop()
def xtest9(self): print "Test for changing the stellar structure model (not yet implemented)" star = Particles(1) star.mass = 1.0 | units.MSun instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.particles.add_particles(star) instance.commit_particles() instance.evolve_model() density_profile = instance.particles[0].get_density_profile() self.assertRaises(AmuseException, instance.particles[0].set_density_profile, density_profile[2:], expected_message = "The length of the supplied vector (197) does not match the number of " "mesh zones of the star (199).") mass_factor = 1.1 instance.particles[0].set_density_profile(mass_factor*density_profile) self.assertAlmostRelativeEqual(instance.particles[0].get_density_profile(), density_profile*mass_factor, places=10) instance.particles.mass *= mass_factor instance.evolve_model() outer_radius = instance.particles[0].get_radius_profile() inner_radius = outer_radius[:-1] inner_radius.prepend(0|units.m) delta_radius_cubed = (outer_radius**3 - inner_radius**3) integrated_mass = (4./3.*pi*delta_radius_cubed*instance.particles[0].get_density_profile()).sum() self.assertAlmostRelativeEqual(integrated_mass, star.mass*mass_factor, places = 3) instance.stop() del instance
def test6(self): print "Test for obtaining the stellar structure model" stars = Particles(2) stars.mass = [1.0, 10.0] | units.MSun instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.particles.add_particles(stars) instance.commit_particles() self.assertEquals(instance.particles.get_number_of_zones(), [199, 199]) self.assertEquals(len(instance.particles[0].get_radius_profile()), 199) self.assertRaises(AmuseException, instance.particles.get_radius_profile, expected_message = "Querying radius profiles of more than one particle at a time is not supported.") self.assertEquals(len(instance.particles[1].get_density_profile()), 199) self.assertIsOfOrder(instance.particles[0].get_radius_profile()[-1], 1.0 | units.RSun) self.assertIsOfOrder(instance.particles[0].get_temperature_profile()[0], 1.0e7 | units.K) self.assertIsOfOrder(instance.particles[0].get_temperature_profile()[-1], 5.0e3 | units.K) radius1 = instance.particles[0].get_radius_profile() radius2 = radius1[:-1] radius2.prepend(0|units.m) delta_radius_cubed = (radius1**3 - radius2**3) total_mass = (4./3. * pi * instance.particles[0].get_density_profile() * delta_radius_cubed).sum() self.assertAlmostRelativeEqual(total_mass, stars[0].mass, places = 1) self.assertAlmostEquals(instance.particles[0].get_mu_profile()[:100], [0.62]*100 | units.amu, places=1) instance.stop()
def test5(self): print "Testing adding and removing particles from stellar evolution code..." particles = Particles(3) particles.mass = 0.3 | units.MSun instance = EVtwin()#redirection="none") instance.initialize_code() instance.parameters.verbosity = True instance.commit_parameters() stars = instance.particles self.assertEquals(len(stars), 0) # before creation stars.add_particles(particles[:-1]) instance.commit_particles() instance.evolve_model(1.0 | units.Myr) self.assertEquals(len(stars), 2) # before remove self.assertAlmostEqual(stars.age, 1.0 | units.Myr) stars.remove_particle(particles[0]) self.assertEquals(len(stars), 1) self.assertEquals(instance.get_number_of_particles(), 1) instance.evolve_model(2.0 | units.Myr) self.assertAlmostEqual(stars[0].age, 2.0 | units.Myr) stars.add_particles(particles[::2]) self.assertEquals(len(stars), 3) # it's back... self.assertAlmostEqual(stars[0].age, 2.0 | units.Myr) self.assertAlmostEqual(stars[1].age, 0.0 | units.Myr) self.assertAlmostEqual(stars[2].age, 0.0 | units.Myr) # ... and rejuvenated. instance.evolve_model(3.0 | units.Myr) # The young stars keep their age offset from the old star self.assertAlmostEqual(stars.age, [3.0, 1.0, 1.0] | units.Myr) instance.evolve_model(4.0 | units.Myr) self.assertAlmostEqual(stars.age, [4.0, 2.0, 2.0] | units.Myr) instance.stop()
def test2(self): print "Testing basic operations" instance = EVtwin() instance.initialize_code() instance.commit_parameters() stars = Particles(1) stars.mass = 10 | units.MSun instance.particles.add_particles(stars) instance.commit_particles() self.assertEquals(instance.particles.mass, 10 | units.MSun) self.assertAlmostEquals(instance.particles.luminosity, 5695.19757302 | units.LSun, 6) self.assertAlmostEquals(instance.particles.radius, 4.07378590 | units.RSun, 6) instance.stop()
def test4(self): print "Testing max age stop condition..." # masses = [.5, 1.0, 1.5] | units.MSun # Test with fewer particles for speed-up. masses = [0.5] | units.MSun max_age = 9.0 | units.Myr number_of_stars = len(masses) stars = Particles(number_of_stars) for i, star in enumerate(stars): star.mass = masses[i] star.radius = 0.0 | units.RSun # Initialize stellar evolution code instance = EVtwin() instance.initialize_code() instance.parameters.verbosity = True if instance.parameters.maximum_number_of_stars < number_of_stars: instance.parameters.maximum_number_of_stars = number_of_stars self.assertEqual(instance.parameters.max_age_stop_condition, 2e6 | units.Myr) instance.parameters.max_age_stop_condition = max_age self.assertEqual(instance.parameters.max_age_stop_condition, max_age) instance.commit_parameters() instance.particles.add_particles(stars) # Let the code perform initialization actions after all particles have been created. instance.commit_particles() from_code_to_model = instance.particles.new_channel_to(stars) instance.evolve_model(end_time=8.0 | units.Myr) from_code_to_model.copy() for i in range(number_of_stars): print stars[i].age.as_quantity_in(units.Myr) self.assertTrue(stars[i].age >= 8.0 | units.Myr) self.assertTrue(stars[i].age <= max_age) self.assertTrue(stars[i].mass <= masses[i]) self.assertTrue(stars[i].time_step <= max_age) self.assertRaises( AmuseException, instance.evolve_model, end_time=2 * max_age, expected_message="Error when calling 'evolve_for' of a 'EVtwin', errorcode " "is 5, error is 'Age greater than maximum age limit.'", ) instance.stop()
def test4(self): print "Testing max age stop condition..." #masses = [.5, 1.0, 1.5] | units.MSun # Test with fewer particles for speed-up. masses = [.5] | units.MSun max_age = 9.0 | units.Myr number_of_stars=len(masses) stars = Particles(number_of_stars) for i, star in enumerate(stars): star.mass = masses[i] star.radius = 0.0 | units.RSun # Initialize stellar evolution code instance = EVtwin() instance.initialize_code() instance.parameters.verbosity = True if instance.parameters.maximum_number_of_stars < number_of_stars: instance.parameters.maximum_number_of_stars = number_of_stars self.assertEqual(instance.parameters.max_age_stop_condition, 2e6 | units.Myr) instance.parameters.max_age_stop_condition = max_age self.assertEqual(instance.parameters.max_age_stop_condition, max_age) instance.commit_parameters() instance.particles.add_particles(stars) # Let the code perform initialization actions after all particles have been created. instance.commit_particles() from_code_to_model = instance.particles.new_channel_to(stars) instance.evolve_model(end_time = 8.0 | units.Myr) from_code_to_model.copy() for i in range(number_of_stars): print stars[i].age.as_quantity_in(units.Myr) self.assertTrue(stars[i].age >= 8.0 | units.Myr) self.assertTrue(stars[i].age <= max_age) self.assertTrue(stars[i].mass <= masses[i]) self.assertTrue(stars[i].time_step <= max_age) self.assertRaises(AmuseException, instance.evolve_model, end_time = 2*max_age, expected_message = "Error when calling 'evolve_for' of a 'EVtwin', errorcode " "is 5, error is 'Age greater than maximum age limit.'") instance.stop()
def xtest10(self): print "Test for changing the stellar composition (not yet implemented)" star = Particles(1) star.mass = 1.0 | units.MSun instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.particles.add_particles(star) instance.commit_particles() instance.evolve_model() composition = instance.particles[0].get_chemical_abundance_profiles() h1_profile = composition[0] * 1 he4_profile = composition[1] * 1 k_surface = -1 # index to the outer mesh cell (surface) self.assertAlmostEquals(composition[0, k_surface], 0.7 | units.none, 4) self.assertAlmostEquals(composition[1, k_surface], (0.3 | units.none) - instance.parameters.metallicity, 4) self.assertAlmostEquals(composition[2:, k_surface].sum(), instance.parameters.metallicity, 4) composition[0] = he4_profile composition[1] = h1_profile instance.particles[0].set_chemical_abundance_profiles(composition) instance.evolve_model() composition = instance.particles[0].get_chemical_abundance_profiles() self.assertAlmostEquals(composition[0, k_surface], (0.3 | units.none) - instance.parameters.metallicity, 4) self.assertAlmostEquals(composition[1, k_surface], 0.7 | units.none, 4) self.assertAlmostEquals(composition[2:, k_surface].sum(), instance.parameters.metallicity, 4) self.assertAlmostEquals(composition.sum(axis=0), 1.0 | units.none) self.assertRaises( AmuseException, instance.particles[0].set_chemical_abundance_profiles, composition[:7], expected_message="The length of the supplied vector (7) does not match the number of " "chemical species of the star (8).", ) instance.stop() del instance
def test3(self): print "Testing sun recreation" instance = EVtwin() instance.initialize_code() instance.commit_parameters() stars = Particles(1) stars.mass = 1 | units.MSun instance.particles.add_particles(stars) instance.commit_particles() self.assertEquals(instance.particles.mass, 1 | units.MSun) self.assertAlmostEquals(instance.particles.luminosity, 0.7098065 | units.LSun, 6) self.assertAlmostEquals(instance.particles.radius, 0.8892833 | units.RSun, 6) instance.evolve_model(4.8|units.Gyr) self.assertAlmostEquals(instance.particles.mass, 0.999921335 | units.MSun, 6) self.assertAlmostEquals(instance.particles.luminosity, 1.04448714 | units.LSun, 6) self.assertAlmostEquals(instance.particles.radius, 1.02061451 | units.RSun, 6) instance.stop()
def test14(self): print "Testing EVtwin states" stars = Particles(2) stars.mass = 1.0 | units.MSun instance = EVtwin() print "First do everything manually:", self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED') instance.initialize_code() self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED') instance.commit_parameters() self.assertEquals(instance.get_name_of_current_state(), 'EDIT') instance.particles.add_particle(stars[0]) instance.commit_particles() self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.cleanup_code() self.assertEquals(instance.get_name_of_current_state(), 'END') instance.stop() print "ok" print "initialize_code(), commit_parameters(), (re)commit_particles(), " \ "and cleanup_code() should be called automatically:", instance = EVtwin() self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED') instance.parameters.RGB_wind_setting = -0.5 self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED') instance.particles.add_particle(stars[0]) self.assertEquals(instance.get_name_of_current_state(), 'EDIT') mass = instance.particles[0].mass self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.particles.add_particle(stars[1]) self.assertEquals(instance.get_name_of_current_state(), 'UPDATE') mass = instance.particles[0].mass self.assertEquals(instance.get_name_of_current_state(), 'RUN') instance.stop() self.assertEquals(instance.get_name_of_current_state(), 'STOPPED') print "ok"
def xslowtest11(self): print "Test 11: Continue the stellar evolution of a 'merger product' - WIP" instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.parameters.min_timestep_stop_condition = 1.0 | units.s stars = Particles(3) stars.mass = [1.0, 2.0, 1.0] | units.MSun instance.particles.add_particles(stars) instance.commit_particles() instance.evolve_model(1.0 | units.Myr) stellar_models = instance.native_stars.get_internal_structure() self.assertEqual(len(stellar_models), 3) self.assertEqual(len(stellar_models[0]), 199) self.assertEqual(len(stellar_models[1]), 199) self.assertAlmostEqual(stellar_models[0].mass[198], 1.0 | units.MSun, 2) self.assertAlmostEqual(stellar_models[1].mass[198], 2.0 | units.MSun, 2) self.assertAlmostEqual(stellar_models[0].mass[0], 0.0 | units.MSun, 2) instance.new_particle_from_model(stellar_models[0], instance.particles[0].age) self.assertEqual(len(instance.particles), 4) self.assertEqual(len(instance.imported_stars), 1) imported_stellar_model = instance.imported_stars[0].get_internal_structure() self.assertEqual(len(imported_stellar_model), 199) self.assertAlmostEqual(imported_stellar_model.mass[198], 1.0 | units.MSun, 2) self.assertAlmostEqual(imported_stellar_model.mass[0], 0.0 | units.MSun, 2) self.assertAlmostRelativeEqual(imported_stellar_model.X_H, stellar_models[0].X_H, 5) self.assertAlmostRelativeEqual(imported_stellar_model.X_He, stellar_models[0].X_He, 5) self.assertAlmostRelativeEqual(imported_stellar_model.mass, stellar_models[0].mass, 2) self.assertAlmostRelativeEqual(imported_stellar_model.radius[1:], stellar_models[0].radius[1:], 2) # instance.evolve_model(2.0 | units.Myr) print instance.particles instance.stop() del instance
def xtest10(self): print "Test for changing the stellar composition (not yet implemented)" star = Particles(1) star.mass = 1.0 | units.MSun instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.particles.add_particles(star) instance.commit_particles() instance.evolve_model() composition = instance.particles[0].get_chemical_abundance_profiles() h1_profile = composition[0] * 1 he4_profile = composition[1] * 1 k_surface = -1 # index to the outer mesh cell (surface) self.assertAlmostEquals(composition[0, k_surface], 0.7 | units.none, 4) self.assertAlmostEquals(composition[1, k_surface], (0.3 | units.none) - instance.parameters.metallicity, 4) self.assertAlmostEquals(composition[2: , k_surface].sum(), instance.parameters.metallicity, 4) composition[0] = he4_profile composition[1] = h1_profile instance.particles[0].set_chemical_abundance_profiles(composition) instance.evolve_model() composition = instance.particles[0].get_chemical_abundance_profiles() self.assertAlmostEquals(composition[0, k_surface], (0.3 | units.none) - instance.parameters.metallicity, 4) self.assertAlmostEquals(composition[1, k_surface], 0.7 | units.none, 4) self.assertAlmostEquals(composition[2: , k_surface].sum(), instance.parameters.metallicity, 4) self.assertAlmostEquals(composition.sum(axis=0), 1.0 | units.none) self.assertRaises(AmuseException, instance.particles[0].set_chemical_abundance_profiles, composition[:7], expected_message = "The length of the supplied vector (7) does not match the number of " "chemical species of the star (8).") instance.stop() del instance
def test7(self): print "Test for obtaining the stellar composition structure" stars = Particles(1) stars.mass = 1.0 | units.MSun instance = EVtwin() instance.initialize_code() instance.commit_parameters() instance.particles.add_particles(stars) instance.commit_particles() number_of_zones = instance.particles.get_number_of_zones()[0] number_of_species = instance.particles.get_number_of_species()[0] composition = instance.particles[0].get_chemical_abundance_profiles() species_names = instance.particles[0].get_names_of_species() self.assertEquals(number_of_zones, 199) self.assertEquals(number_of_species, 9) self.assertEquals(len(species_names), number_of_species) self.assertEquals(len(composition), number_of_species) self.assertEquals(len(composition[0]), number_of_zones) self.assertEquals(species_names, ['h1', 'he4', 'c12', 'n14', 'o16', 'ne20', 'mg24', 'si28', 'fe56']) self.assertAlmostEquals(composition[0, -1], 0.7, 3) self.assertAlmostEquals(composition[1, -1], 0.3 - instance.parameters.metallicity, 3) self.assertAlmostEquals(composition[2:,-1].sum(), instance.parameters.metallicity, 3) self.assertAlmostEquals(composition.sum(axis=0), [1.0]*number_of_zones) instance.stop()
def xtest3(self): instance = EVtwin() instance.initialize_code() instance.commit_parameters() stars = Particles(1) star = stars[0] star.mass = 5.0 | units.MSun star.radius = 0.0 | units.RSun instance.particles.add_particles(stars) instance.commit_particles() from_code_to_model = instance.particles.new_channel_to(stars) from_code_to_model.copy() previous_type = star.stellar_type results = [] t0 = 0 | units.Myr current_time = t0 while current_time < (115 | units.Myr): instance.evolve_model() from_code_to_model.copy() current_time = star.age print (star.age, star.mass, star.stellar_type) if not star.stellar_type == previous_type: results.append((star.age, star.mass, star.stellar_type)) previous_type = star.stellar_type print results self.assertEqual(len(results), 6) times = ( 104.0 | units.Myr, 104.4 | units.Myr, 104.7 | units.Myr, 120.1 | units.Myr, 120.9 | units.Myr, 121.5 | units.Myr ) for result, expected in zip(results, times): self.assertAlmostEqual(result[0].value_in(units.Myr), expected.value_in(units.Myr), 1) masses = ( 5.000 | units.MSun, 5.000 | units.MSun, 4.998 | units.MSun, 4.932 | units.MSun, 4.895 | units.MSun, 0.997 | units.MSun ) for result, expected in zip(results, masses): self.assertAlmostEqual(result[1].value_in(units.MSun), expected.value_in(units.MSun), 3) types = ( "Hertzsprung Gap", "First Giant Branch", "Core Helium Burning", "First Asymptotic Giant Branch", "Second Asymptotic Giant Branch", "Carbon/Oxygen White Dwarf", ) for result, expected in zip(results, types): self.assertEquals(str(result[2]), expected) instance.stop()