def result(self): masses = numpy.ones( self.number_of_particles) / self.number_of_particles radius, theta, phi = self.new_positions_spherical_coordinates() x, y, z = self.coordinates_from_spherical(radius, theta, phi) radius, theta, phi = self.new_velocities_spherical_coordinates(radius) vx, vy, vz = self.coordinates_from_spherical(radius, theta, phi) result = datamodel.Particles(self.number_of_particles) result.mass = nbody_system.mass.new_quantity(masses) result.x = nbody_system.length.new_quantity( x.reshape(self.number_of_particles) / 1.695) result.y = nbody_system.length.new_quantity( y.reshape(self.number_of_particles) / 1.695) result.z = nbody_system.length.new_quantity( z.reshape(self.number_of_particles) / 1.695) result.vx = nbody_system.speed.new_quantity( vx.reshape(self.number_of_particles) / sqrt(1 / 1.695)) result.vy = nbody_system.speed.new_quantity( vy.reshape(self.number_of_particles) / sqrt(1 / 1.695)) result.vz = nbody_system.speed.new_quantity( vz.reshape(self.number_of_particles) / sqrt(1 / 1.695)) result.radius = 0 | nbody_system.length result.move_to_center() if self.do_scale: result.scale_to_standard() if not self.convert_nbody is None: result = datamodel.ParticlesWithUnitsConverted( result, self.convert_nbody.as_converter_from_si_to_generic()) result = result.copy() return result
def write_time_step(master_set, converter, current_time, file_prefix): ''' Writes out necessary information for a time step. master_set: The Master AMUSE Particle Set used in Tycho multiples_code: The Multiples Instance for Tycho current_time: The Simulations Current Time file_prefix: String Value for a Prefix to the Saved File ''' # First, Define/Make the Directory for the Time Step to be Stored file_dir_MS = os.getcwd() + "/Run/MasterParticleSet" file_dir_CoM = os.getcwd() + "/Run/CoMSet" if not os.path.exists(file_dir_MS): os.makedirs(file_dir_MS) if not os.path.exists(file_dir_CoM): os.makedirs(file_dir_CoM) file_base_MS = file_dir_MS + "/" + file_prefix file_base_CoM = file_dir_CoM + "/" + file_prefix # Second, Create the CoM Tree Particle Set from Multiples # Third, Convert from NBody to SI Before Writing MS_SI = datamodel.ParticlesWithUnitsConverted( master_set, converter.as_converter_from_nbody_to_si()) # CoM_SI = datamodel.ParticlesWithUnitsConverted(CoM_Set, converter.as_converter_from_nbody_to_si()) # Fourth, Write the Master AMUSE Particle Set to a HDF5 File file_format = "hdf5" write_set_to_file(MS_SI, file_base_MS+"_MS_t%.3f.hdf5" %(current_time.number), \ format=file_format, close_file=True)
def convert_to_string(self, particles, converter=None): if not converter is None: particles = datamodel.ParticlesWithUnitsConverted( particles, converter.as_converter_from_generic_to_si()) self.time = (particles.get_timestamp() or (0.0 | nbody_system.time)).value_in(nbody_system.time) self.particles = particles self.number_of_particles = len(particles) masses = particles.mass.value_in(nbody_system.mass) velocities = particles.velocity.value_in(nbody_system.speed) positions = particles.position.value_in(nbody_system.length) for i, mass in enumerate(masses): self.mass_paragraph += ' ' + str(mass) if (i % 5) == 0: self.mass_paragraph += '\n ' for i, phase in enumerate(zip(positions, velocities)): self.phase_paragraph += ' '.join([ str(j) for j in phase[0] ]) + ' ' + ' '.join([str(k) for k in phase[1]]) if (i % 1) == 0: self.phase_paragraph += '\n ' return TEMPLATE.format(self)
def test2(self): particles = datamodel.Particles(3) particles.mass = [2, 3, 4] | units.kg parent = particles[0] child1 = particles[1] child2 = particles[2] parent.child1 = child1 parent.child2 = child2 child1.sibling = child2 convert_nbody = nbody_system.nbody_to_si(10 | units.kg, 5 | units.m) particles = datamodel.ParticlesWithUnitsConverted( particles, convert_nbody.as_converter_from_generic_to_si()) self.assertEqual(len(particles.child1), 3) self.assertEqual(len(particles.child1.as_set().compressed()), 1) self.assertEqual(len(particles.child2), 3) self.assertEqual(len(particles.child2.as_set().compressed()), 1) self.assertAlmostRelativeEqual(particles[0].child1.mass, 0.3 | nbody_system.mass) self.assertAlmostRelativeEqual(particles[0].child1.mass, 0.3 | nbody_system.mass)
def result(self): mass, x, y, z, vx, vy, vz, u = self.new_model() result = datamodel.Particles(self.actualN) result.mass = nbody_system.mass.new_quantity(mass) result.x = nbody_system.length.new_quantity(x) result.y = nbody_system.length.new_quantity(y) result.z = nbody_system.length.new_quantity(z) result.vx = nbody_system.speed.new_quantity(vx) result.vy = nbody_system.speed.new_quantity(vy) result.vz = nbody_system.speed.new_quantity(vz) result.u = (nbody_system.speed**2).new_quantity(u) result.move_to_center() if self.do_scale: potential_energy = result.potential_energy(G=nbody_system.G) result.position *= potential_energy / (-0.5 | nbody_system.energy) internal_energy = result.thermal_energy() result.u *= ((0.25 | nbody_system.energy) / internal_energy) if not self.convert_nbody is None: result = datamodel.ParticlesWithUnitsConverted( result, self.convert_nbody.as_converter_from_si_to_generic()) result = result.copy() return result
def result(self): """Result. Returns ------- result: Particles Particles datamodel """ num_objs = self.number_of_particles masses = np.ones(num_objs) / num_objs print("made masses") radius, theta, phi = self.new_positions_spherical_coordinates() x, y, z = self.coordinates_from_spherical(radius, theta, phi) print("made coordinates") speed, theta, phi = self.new_velocities_spherical_coordinates(x, y, z) vx, vy, vz = self.coordinates_from_spherical(speed, theta, phi) print("made velocities") # --------------- # build Particles result = datamodel.Particles(num_objs) # result.mass = nbody_system.mass.new_quantity(masses) result.mass = masses | u.MSun # TODO FIX # spatial # result.x = nbody_system.length.new_quantity(x.reshape(num_objs)) # result.y = nbody_system.length.new_quantity(y.reshape(num_objs)) # result.z = nbody_system.length.new_quantity(z.reshape(num_objs)) result.x = x.reshape(num_objs) result.y = y.reshape(num_objs) result.z = z.reshape(num_objs) # velocity # result.vx = nbody_system.speed.new_quantity(vx.reshape(num_objs)) # result.vy = nbody_system.speed.new_quantity(vy.reshape(num_objs)) # result.vz = nbody_system.speed.new_quantity(vz.reshape(num_objs)) result.vx = vx.reshape(num_objs) result.vy = vy.reshape(num_objs) result.vz = vz.reshape(num_objs) # radius # result.radius = 0 | nbody_system.length result.radius = 0 | u.AU # --------------- result.move_to_center() if self.do_scale: result.scale_to_standard() if self.convert_nbody is not None: converter = self.convert_nbody.as_converter_from_si_to_generic() result = datamodel.ParticlesWithUnitsConverted(result, converter) result = result.copy() return result
def result(self): particles = self.new_model() particles.move_to_center() if self.do_scale: particles.scale_to_standard(virial_ratio=self.virial_ratio) if not self.convert_nbody is None: return datamodel.ParticlesWithUnitsConverted(particles, self.convert_nbody.as_converter_from_si_to_generic()).copy() return particles
def store_string(self): if not self.nbody_to_si_converter is None: particles = datamodel.ParticlesWithUnitsConverted( self.set, self.nbody_to_si_converter.as_converter_from_generic_to_si()) else: particles = self.set x = Particles2Dyn() return x.convert_to_string(particles)
def convert_to_particles(self, string, converter=None): self.read_to_ram(string) result = datamodel.Particles(self.number_of_particles) result.mass = self.masses | nbody_system.mass result.position = [i[0] for i in self.phases] | nbody_system.length result.velocity = [i[1] for i in self.phases] | nbody_system.speed if not self.timestamp is None: result.savepoint(self.timestamp) if not converter is None: result = datamodel.ParticlesWithUnitsConverted( result, converter.as_converter_from_si_to_generic()) return result
def __init__(self, dyn_filename=None, convert_nbody=None): dyn2xml = Dyn2Xml() xml_string = dyn2xml.convert_to_xml(dyn_filename) xml2particles = Xml2Particles() err1 = xml2particles.parse_xml(xml_string) if convert_nbody is None: self.convert_nbody = None self.Particles = xml2particles.system return else: self.convert_nbody = convert_nbody self.Particles = datamodel.ParticlesWithUnitsConverted( xml2particles.system, self.convert_nbody.as_converter_from_si_to_generic())
def result(self): mass, x, y, z, vx, vy, vz, u = self.new_model() result = datamodel.Particles(self.actualN) result.mass = nbody_system.mass.new_quantity(mass) result.x = nbody_system.length.new_quantity(x) result.y = nbody_system.length.new_quantity(y) result.z = nbody_system.length.new_quantity(z) result.vx = nbody_system.speed.new_quantity(vx) result.vy = nbody_system.speed.new_quantity(vy) result.vz = nbody_system.speed.new_quantity(vz) result.u = (nbody_system.speed**2).new_quantity(u) if not self.convert is None: result = datamodel.ParticlesWithUnitsConverted( result, self.convert.as_converter_from_si_to_generic()) result = result.copy() return result
def result(self): masses, positions, velocities = self.makeking() result = datamodel.Particles(self.number_of_particles) result.mass = nbody_system.mass.new_quantity(masses) result.position = nbody_system.length.new_quantity(positions) result.velocity = nbody_system.speed.new_quantity(velocities) result.radius = 0 | nbody_system.length if self.center_model: result.move_to_center() if self.do_scale: result.scale_to_standard() if not self.convert_nbody is None: result = datamodel.ParticlesWithUnitsConverted( result, self.convert_nbody.as_converter_from_si_to_generic()) result = result.copy() return result
def load_string(self, string): x = Dyn2Xml() xml_string = x.convert_starlab_string_to_xml_string(string) xml2particles = Xml2Particles() xml2particles.dynamics_mass_units = self.dynamics_mass_units xml2particles.dynamics_time_units = self.dynamics_time_units xml2particles.dynamics_length_units = self.dynamics_length_units xml2particles.parse_xml(xml_string) unit_converter = None if not self.nbody_to_si_converter is None: unit_converter = self.nbody_to_si_converter elif self.must_scale: if not self._is_valid_scaling_factor(xml2particles.mass_scale): unit_converter = None elif not self._is_valid_scaling_factor(xml2particles.time_scale): unit_converter = nbody_system.nbody_to_si( (1.0 / xml2particles.mass_scale) | units.MSun, (1.0 / xml2particles.size_scale) | units.RSun, ) else: unit_converter = generic_unit_converter.ConvertBetweenGenericAndSiUnits( (1.0 / xml2particles.mass_scale) | units.MSun, (1.0 / xml2particles.size_scale) | units.RSun, (1.0 / xml2particles.time_scale) | units.Myr, ) if unit_converter is None: result = xml2particles.system else: result = datamodel.ParticlesWithUnitsConverted( xml2particles.system, unit_converter.as_converter_from_si_to_generic() ) if self.return_children: if self.return_converter: return result[0].children(), unit_converter else: return result[0].children() else: if self.return_converter: return result[0], unit_converter else: return result[0]
p = temp[0] # Moves the Planet to Orbit the Host Star p.position = p.position + host_star.position p.velocity = p.velocity + host_star.velocity # Returns the Created AMUSE Particle return p MasterSet = datamodel.Particles() # Create the Stellar Cluster, Shift from SI to NBody, Add the Particles to MS, & Open a Channel to the MS stars_SI, converter = king_cluster(num_stars, 'test_stars', rand_seed=0, IBF=0.5) stars_NB = datamodel.ParticlesWithUnitsConverted( stars_SI, converter.as_converter_from_nbody_to_si()) MasterSet.add_particles(stars_NB) #channel_stars_master = stars_NB.new_channel_to(MasterSet) # Create Planetary Systems, Shift from SI to NBody, Add the Particles to MS, & Open a Channel to the MS systems_SI = planetary_systems(stars_SI, converter, num_psys, 'test_planets', Jupiter=True) systems_NB = datamodel.ParticlesWithUnitsConverted( systems_SI, converter.as_converter_from_nbody_to_si()) MasterSet.add_particles(systems_NB) print MasterSet time = 0.0 | nbody_system.time
def convert_to_particles(self, inputfile, converter=None): self.Particles = self.read_to_ram(inputfile) if not converter == None: self.Particles = datamodel.ParticlesWithUnitsConverted( self.Particles, converter.as_converter_from_si_to_generic())