def test1(self): options = TestFileFormatProcessor.get_options() self.assertTrue('add_comma' in options) self.assertTrue('save_fast' in options) TestFileFormatProcessor.register() base.write_set_to_file(None, "test.txt", format="123") self.assertEqual(TestFileFormatProcessor.instance.set, None) self.assertEqual(TestFileFormatProcessor.instance.filename, "test.txt") self.assertEqual(TestFileFormatProcessor.instance.format, "123") self.assertTrue(TestFileFormatProcessor.instance.stored) self.assertTrue(TestFileFormatProcessor.instance.add_comma)
def test2(self): TestFileFormatProcessor.register() base.write_set_to_file(None, "test.txt", format="123", add_comma = False) self.assertFalse(TestFileFormatProcessor.instance.add_comma)
def test2(self): TestFileFormatProcessor.register() base.write_set_to_file(None, "test.txt", format="123", add_comma=False) self.assertFalse(TestFileFormatProcessor.instance.add_comma)
def run_hydrodynamics(N=100, Mtot=1|units.MSun, Rvir=1|units.RSun, t_end=0.5|units.day, n_steps=10,\ vx = 0 |(units.RSun/units.day),\ vy = 0 |(units.RSun/units.day),\ vz = 0 |(units.RSun/units.day),\ plummer1=None, plummer2=None,\ bodyname = None): """ Runs the hydrodynamics simulation and returns a HydroResults instance. FUNCTION WALKTHROUGH: In the following explanation 'plummer1' and 'plummer2' are assumed to be hdf5 files written by the function write_set_to_file(). Case 1: If 'plummer1' and 'plummer2' are filenames of hdf5 files, then these two plummer spheres will be smashed together. Case 2: If only plummer1 is supplied, then it will evolve plummer1 with t_end timerange and n_steps steps. Case 3: If no plummers spheres are supplied, then it will generate a new plummer sphere using the default/supplied initial conditions. OUTPUT FILES: If 'results_out' is specified, the HydroResult instance is written to file in HDF5 format. This however does not use write_set_to_file() which writes the entire Particles class and its attributes to file at each dt, but uses write_to_hdf5() from the 'support' module which is tailored to writing HydroResults instances to file. This HDF5 contains all necessary data to plot the required plots of the assignment. In addition, the last snapshot of the Particles instance is written to file using write_set_to_file(), the latter file is written to the 'bodies' directory. Only the last snapshot is written to file because the history of a Particle set is not of interest when reloading them to smash plummer spheres. """ converter = nbody_system.nbody_to_si(Mtot, Rvir) fi = Fi(converter) if plummer1 and plummer2: eta_smash = 0.3 |units.day if plummer1 == plummer2: bodies1 = read_set_from_file(plummer1, format='hdf5') bodies2 = bodies1.copy() bodies2.key += 1 else: bodies1 = read_set_from_file(plummer1, format='hdf5') bodies2 = read_set_from_file(plummer2, format='hdf5') bodies1.move_to_center() bodies2.move_to_center() if vx.value_in(vx.unit) == 0 and vy.value_in(vy.unit) == 0 \ and vz.value_in(vz.unit) == 0: bodies1.x += -1 |units.RSun bodies2.x += 1 |units.RSun else: bodies1.x += (-1)*vx*eta_smash bodies2.x += 1*vx*eta_smash bodies1.vx += vx bodies2.vx += (-1)*vx bodies1.vy += vy bodies2.vy += (-1)*vy bodies1.vz += vz bodies2.vz += (-1)*vz bodies1.add_particles(bodies2) bodies = bodies1 elif plummer1 or plummer2: if plummer1: bodies = read_set_from_file(plummer1) else: bodies = read_set_from_file(plummer2) bodies.move_to_center() else: bodies = new_plummer_gas_model(N, convert_nbody=converter) fi.gas_particles.add_particles(bodies) fi_to_framework = fi.gas_particles.new_channel_to(bodies) fi.parameters.self_gravity_flag = True data = {'lagrangianradii':AdaptingVectorQuantity(),\ 'angular_momentum':AdaptingVectorQuantity(),\ 'time':AdaptingVectorQuantity(),\ 'positions':AdaptingVectorQuantity(),\ 'kinetic_energy':AdaptingVectorQuantity(),\ 'potential_energy':AdaptingVectorQuantity(),\ 'total_energy':AdaptingVectorQuantity() } mass_fractions = [0.10, 0.25, 0.50, 0.75] setattr(data['lagrangianradii'], 'mf', mass_fractions) data['radius_initial'], data['densities_initial'] = radial_density(\ fi.particles.x, fi.particles.mass, N=10, dim=1) timerange = numpy.linspace(0, t_end.value_in(t_end.unit),\ n_steps) | t_end.unit data['time'].extend(timerange) fi.parameters.timestep = t_end/(n_steps+1) widget = drawwidget("Evolving") pbar = pb.ProgressBar(widgets=widget, maxval=len(timerange)).start() for i, t in enumerate(timerange): fi.evolve_model(t) data['kinetic_energy'].append(fi.kinetic_energy) data['potential_energy'].append(fi.potential_energy) data['total_energy'].append(fi.total_energy) data['positions'].append(fi.particles.position) data['angular_momentum'].append(fi.gas_particles.\ total_angular_momentum()) data['lagrangianradii'].append(fi.particles.LagrangianRadii(\ unit_converter=converter,\ mf=mass_fractions)[0]) if t == timerange[-1] and bodyname: if os.path.dirname(bodyname) == '': filename = "bodies/"+bodyname fi_to_framework.copy() write_set_to_file(bodies.savepoint(t), filename, "hdf5") pbar.update(i) pbar.finish() data['radius_final'], data['densities_final'] = radial_density(\ fi.particles.x, fi.particles.mass, N=10, dim=1) fi.stop() results = HydroResults(data) return results
def run_hydrodynamics(N=100, Mtot=1|units.MSun, Rvir=1|units.RSun, t_end=0.5|units.day, n_steps=10,\ vx = 0 |(units.RSun/units.day),\ vy = 0 |(units.RSun/units.day),\ vz = 0 |(units.RSun/units.day),\ plummer1=None, plummer2=None,\ bodyname = None): """ Runs the hydrodynamics simulation and returns a HydroResults instance. FUNCTION WALKTHROUGH: In the following explanation 'plummer1' and 'plummer2' are assumed to be hdf5 files written by the function write_set_to_file(). Case 1: If 'plummer1' and 'plummer2' are filenames of hdf5 files, then these two plummer spheres will be smashed together. Case 2: If only plummer1 is supplied, then it will evolve plummer1 with t_end timerange and n_steps steps. Case 3: If no plummers spheres are supplied, then it will generate a new plummer sphere using the default/supplied initial conditions. OUTPUT FILES: If 'results_out' is specified, the HydroResult instance is written to file in HDF5 format. This however does not use write_set_to_file() which writes the entire Particles class and its attributes to file at each dt, but uses write_to_hdf5() from the 'support' module which is tailored to writing HydroResults instances to file. This HDF5 contains all necessary data to plot the required plots of the assignment. In addition, the last snapshot of the Particles instance is written to file using write_set_to_file(), the latter file is written to the 'bodies' directory. Only the last snapshot is written to file because the history of a Particle set is not of interest when reloading them to smash plummer spheres. """ converter = nbody_system.nbody_to_si(Mtot, Rvir) fi = Fi(converter) if plummer1 and plummer2: eta_smash = 0.3 | units.day if plummer1 == plummer2: bodies1 = read_set_from_file(plummer1, format='hdf5') bodies2 = bodies1.copy() bodies2.key += 1 else: bodies1 = read_set_from_file(plummer1, format='hdf5') bodies2 = read_set_from_file(plummer2, format='hdf5') bodies1.move_to_center() bodies2.move_to_center() if vx.value_in(vx.unit) == 0 and vy.value_in(vy.unit) == 0 \ and vz.value_in(vz.unit) == 0: bodies1.x += -1 | units.RSun bodies2.x += 1 | units.RSun else: bodies1.x += (-1) * vx * eta_smash bodies2.x += 1 * vx * eta_smash bodies1.vx += vx bodies2.vx += (-1) * vx bodies1.vy += vy bodies2.vy += (-1) * vy bodies1.vz += vz bodies2.vz += (-1) * vz bodies1.add_particles(bodies2) bodies = bodies1 elif plummer1 or plummer2: if plummer1: bodies = read_set_from_file(plummer1) else: bodies = read_set_from_file(plummer2) bodies.move_to_center() else: bodies = new_plummer_gas_model(N, convert_nbody=converter) fi.gas_particles.add_particles(bodies) fi_to_framework = fi.gas_particles.new_channel_to(bodies) fi.parameters.self_gravity_flag = True data = {'lagrangianradii':AdaptingVectorQuantity(),\ 'angular_momentum':AdaptingVectorQuantity(),\ 'time':AdaptingVectorQuantity(),\ 'positions':AdaptingVectorQuantity(),\ 'kinetic_energy':AdaptingVectorQuantity(),\ 'potential_energy':AdaptingVectorQuantity(),\ 'total_energy':AdaptingVectorQuantity() } mass_fractions = [0.10, 0.25, 0.50, 0.75] setattr(data['lagrangianradii'], 'mf', mass_fractions) data['radius_initial'], data['densities_initial'] = radial_density(\ fi.particles.x, fi.particles.mass, N=10, dim=1) timerange = numpy.linspace(0, t_end.value_in(t_end.unit),\ n_steps) | t_end.unit data['time'].extend(timerange) fi.parameters.timestep = t_end / (n_steps + 1) widget = drawwidget("Evolving") pbar = pb.ProgressBar(widgets=widget, maxval=len(timerange)).start() for i, t in enumerate(timerange): fi.evolve_model(t) data['kinetic_energy'].append(fi.kinetic_energy) data['potential_energy'].append(fi.potential_energy) data['total_energy'].append(fi.total_energy) data['positions'].append(fi.particles.position) data['angular_momentum'].append(fi.gas_particles.\ total_angular_momentum()) data['lagrangianradii'].append(fi.particles.LagrangianRadii(\ unit_converter=converter,\ mf=mass_fractions)[0]) if t == timerange[-1] and bodyname: if os.path.dirname(bodyname) == '': filename = "bodies/" + bodyname fi_to_framework.copy() write_set_to_file(bodies.savepoint(t), filename, "hdf5") pbar.update(i) pbar.finish() data['radius_final'], data['densities_final'] = radial_density(\ fi.particles.x, fi.particles.mass, N=10, dim=1) fi.stop() results = HydroResults(data) return results