def test6(self): test_results_path = self.get_path_to_results() output_file = os.path.join( test_results_path, "test6_grid" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) instance = self.store_factory()(output_file) shape = 10, 10, 10 p = Grid(*shape) p.mass = ([x * 2.0 for x in range(p.size)] | units.kg).reshape(shape) p.model_time = 2.0 | units.s instance.store_grid(p.savepoint(1 | units.Myr)) loaded_grid = self.get_version_in_store(instance.load_grid()) self.assertAlmostRelativeEquals(loaded_grid.get_timestamp(), 1 | units.Myr) self.assertEqual(loaded_grid.shape, shape) self.assertEqual(loaded_grid[0][0][0].mass, 0 | units.kg) self.assertAlmostRelativeEquals(loaded_grid[..., 1, 1].mass, p[..., 1, 1].mass) instance.close()
def test12(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test12" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) shape = 10, 10, 10 p = Grid(*shape) p.mass = ([x * 2.0 for x in range(p.size)] | units.kg).reshape(shape) p.model_time = 2.0 | units.s io.write_set_to_file(p.savepoint(timestamp=2 | units.Myr, scale=1 | units.kg), output_file, "hdf5", version=self.store_version()) loaded_particles = io.read_set_from_file(output_file, "hdf5", version=self.store_version()) loaded_particles = self.get_version_in_store(loaded_particles) a = loaded_particles.collection_attributes self.assertAlmostRelativeEquals(a.timestamp, 2 | units.Myr) self.assertAlmostRelativeEquals(a.scale, 1 | units.kg)
def test20(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test20" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) shape = 10, 10, 10 p = Grid(*shape) p.mass = (numpy.asarray([x * 2.0 for x in range(p.size)])).reshape(shape) io.write_set_to_file(p, output_file, "hdf5", version=self.store_version()) loaded = io.read_set_from_file(output_file, "hdf5", version=self.store_version()) loaded = self.get_version_in_store(loaded) self.assertAlmostRelativeEquals(p.mass[0][1][2], 24) self.assertAlmostRelativeEquals(p[0][1][2].mass, 24)
def test27(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test27" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) instance = self.store_factory()(output_file) shape = 10, p = Grid(*shape) p.mass = ([x * 2.0 for x in range(p.size)] | units.kg).reshape(shape) p.model_time = 2.0 | units.s instance.store_grid(p) loaded_grid = instance.load_grid() self.assertEqual(loaded_grid.shape, shape) loaded_mass_in_kg = loaded_grid.mass.value_in(units.kg) previous_mass_in_kg = p.mass.value_in(units.kg) for expected, actual in zip(previous_mass_in_kg, loaded_mass_in_kg): self.assertEqual(expected, actual) instance.close()
def test54(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test23" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) stars = Particles(2) stars[0].x = 1.0 | units.km stars[1].x = 2.0 | units.km gas = Grid(2, 3) gas.y = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] | units.km stars[0].gas = gas[0][0] stars[1].gas = gas[1][0] self.assertAlmostRelativeEquals(stars[0].gas.y, 1.0 | units.km) self.assertAlmostRelativeEquals(stars[1].gas.y, 4.0 | units.km) io.write_set_to_file(stars, output_file, "hdf5", version=self.store_version()) loaded_stars = io.read_set_from_file(output_file, "hdf5", version=self.store_version()) self.assertAlmostRelativeEquals(loaded_stars[0].gas.y, 1.0 | units.km) self.assertAlmostRelativeEquals(loaded_stars[1].gas.y, 4.0 | units.km)
def test54b(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test24" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) stars = Particles(2) stars[0].x = 1.0 | units.km stars[1].x = 2.0 | units.km gas = Grid(2, 3) gas.y = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] | units.km gas[0][0].particle = stars[0] gas[1][2].particle = stars[0] gas[1][0].particle = stars[1] #stars[0].gas = gas[0][0] #stars[1].gas = gas[1][0] self.assertAlmostRelativeEquals(gas[0][0].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(gas[1][2].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(gas[1][0].particle.x, 2.0 | units.km) io.write_set_to_file(gas, output_file, "hdf5", version=self.store_version()) loaded_gas = io.read_set_from_file(output_file, "hdf5", version=self.store_version()) self.assertAlmostRelativeEquals(loaded_gas[0][0].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(loaded_gas[1][2].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(loaded_gas[1][0].particle.x, 2.0 | units.km) self.assertEqual(id(loaded_gas[0][0].particle.get_containing_set()), id(loaded_gas[1][2].particle.get_containing_set())) self.assertEqual(loaded_gas.particle.shape, loaded_gas.shape) gas_copy = loaded_gas.copy() gas_copy[0][0].particle.x = 3 | units.km self.assertAlmostRelativeEquals(loaded_gas[0][0].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(gas_copy[0][0].particle.x, 3.0 | units.km) self.assertAlmostRelativeEquals(gas_copy[1][2].particle.x, 3.0 | units.km)
def test29(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test29"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) particles = Grid(10) particles.attribute1 = "normal" particles.attribute2 = u"unicode" io.write_set_to_file(particles,output_file, format='amuse', version = self.store_version()) output = io.read_set_from_file(output_file, format='amuse') self.assertEquals(output[0].attribute2, u"unicode")
def load(self): dataset = Dataset(self.filename) shape = () order = dict() for i, (key, d) in enumerate(dataset.dimensions.items()): shape += (len(d), ) order[key] = i grid = Grid(*shape[0:2]) for s in dataset.ncattrs(): setattr(grid.collection_attributes, s, getattr(dataset, s)) for name, var in dataset.variables.items(): perm = [order[x] for x in var.dimensions] if hasattr(var, "units"): unit = units_lookup[var.units] else: unit = units.none setattr(grid, name, numpy.array(var).transpose(tuple(perm)) | unit) self.dataset = dataset return grid
def result(self): grid = Grid.create(self.dimensions, self.box_size.as_vector_with_length(3)) zero = numpy.zeros(self.dimensions).flatten() | units.m / units.s rho, rhovx, rhovy, rhovz, rhoe = self.sph_code.get_hydro_state_at_point( grid.x.flatten() - self.box_offset, grid.y.flatten() - self.box_offset, grid.z.flatten() - self.box_offset, zero, zero, zero) grid.rho = rho.reshape(self.dimensions) grid.rhovx = rhovx.reshape(self.dimensions) grid.rhovy = rhovy.reshape(self.dimensions) grid.rhovz = rhovz.reshape(self.dimensions) grid.energy = rhoe.reshape(self.dimensions) if self.do_scale: grid.rho *= self.sph_code.gas_particles.total_mass() / (grid.rho.sum() * grid.cellsize().prod()) total_sph_momentum = (self.sph_code.gas_particles.mass.reshape((-1,1)) * self.sph_code.gas_particles.velocity).sum(axis=0) total_grid_momentum = grid.momentum.reshape((-1,3)).sum(axis=0) grid.momentum += total_sph_momentum / grid.cellsize().prod() - total_grid_momentum grid.energy *= self.sph_code.thermal_energy / (grid.energy.sum() * grid.cellsize().prod()) return grid
def result(self): grid = Grid.create(self.dimensions, self.box_size.as_vector_with_length(3)) grid.add_vector_attribute("momentum", ["rhovx","rhovy","rhovz"]) zero = numpy.zeros(self.dimensions).flatten() | units.m / units.s rho, rhovx, rhovy, rhovz, rhoe = self.sph_code.get_hydro_state_at_point( grid.x.flatten() - self.box_offset, grid.y.flatten() - self.box_offset, grid.z.flatten() - self.box_offset, zero, zero, zero) grid.rho = rho.reshape(self.dimensions) grid.rhovx = rhovx.reshape(self.dimensions) grid.rhovy = rhovy.reshape(self.dimensions) grid.rhovz = rhovz.reshape(self.dimensions) grid.energy = rhoe.reshape(self.dimensions) if self.do_scale: grid.rho *= self.sph_code.gas_particles.total_mass() / (grid.rho.sum() * grid.cellsize().prod()) total_sph_momentum = (self.sph_code.gas_particles.mass.reshape((-1,1)) * self.sph_code.gas_particles.velocity).sum(axis=0) total_grid_momentum = grid.momentum.reshape((-1,3)).sum(axis=0) grid.momentum += total_sph_momentum / grid.cellsize().prod() - total_grid_momentum grid.energy *= self.sph_code.thermal_energy / (grid.energy.sum() * grid.cellsize().prod()) return grid
def __init__(self, filename, nx, ny): self.filename = filename import os.path import sys if not os.path.isfile(filename): sys.exit("Error: No such file " + filename) raw = numpy.fromfile(filename).newbyteorder('S') grid = raw.reshape((7, ny, nx)) # lats=grid[0,:,:]/numpy.pi*180 # lons=grid[1,:,:]/numpy.pi*180 self.lat = lats = grid[0, :, :] self.lon = lons = grid[1, :, :] self.htn = htn = grid[2, :, :] self.hte = hte = grid[3, :, :] self.hus = hus = grid[4, :, :] self.huw = huw = grid[5, :, :] self.angle = angle = grid[6, :, :] #nodes is the U-grid self.nodes = nodes = Grid(ny * nx) nodes.lats = lats | units.rad nodes.lons = lons | units.rad
def setup_simple_grid(self): test_grid = Grid.create((4,3,2), [1.0, 1.0, 1.0] | units.m) test_grid.rho = numpy.linspace(1.0, 2.0, num=24).reshape(test_grid.shape) | units.kg/units.m**3 test_grid.rhovx = test_grid.rho * (3.0 | units.m/units.s) test_grid.rhovy = test_grid.rho * (4.0 | units.m/units.s) test_grid.rhovz = test_grid.rho * (0.0 | units.m/units.s) test_grid.energy = test_grid.rho * ((1.0 | (units.m/units.s)**2) + 0.5 * (5.0 | units.m/units.s)**2) return test_grid
def make_grid(number_of_grid_cells, length, constant_hydrogen_density, inner_radius, outer_radius): grid = Grid.create([number_of_grid_cells] * 3, length.as_vector_with_length(3)) grid.radius = grid.position.lengths() grid.hydrogen_density = constant_hydrogen_density grid.hydrogen_density[grid.radius <= inner_radius] = 0 | units.cm ** -3 grid.hydrogen_density[grid.radius >= outer_radius] = 0 | units.cm ** -3 return grid
def hydro_plot(view, hydro_code, image_size, figname): """ view: the (physical) region to plot [xmin, xmax, ymin, ymax] hydro_code: hydrodynamics code in which the gas to be plotted is defined image_size: size of the output image in pixels (x, y) """ shape = (image_size[0], image_size[1], 1) size = image_size[0] * image_size[1] axis_lengths = [0.0, 0.0, 0.0] | units.m axis_lengths[0] = view[1] - view[0] axis_lengths[1] = view[3] - view[2] grid = Grid.create(shape, axis_lengths) grid.x += view[0] grid.y += view[2] speed = grid.z.reshape(size) * (0 | 1 / units.s) rho, rhovx, rhovy, rhovz, rhoe = \ hydro_code.get_hydro_state_at_point( grid.x.reshape(size), grid.y.reshape(size), grid.z.reshape(size), speed, speed, speed) min_v = 800.0 | units.km / units.s max_v = 3000.0 | units.km / units.s min_rho = 3.0e-4 | units.g / units.cm**3 max_rho = 0.1 | units.g / units.cm**3 min_E = 1.0e11 | units.J / units.kg max_E = 1.0e13 | units.J / units.kg v_sqr = (rhovx**2 + rhovy**2 + rhovz**2) / rho**2 E = rhoe / rho log_v = numpy.log((v_sqr / min_v**2)) / numpy.log((max_v**2 / min_v**2)) log_rho = numpy.log((rho / min_rho)) / numpy.log((max_rho / min_rho)) log_E = numpy.log((E / min_E)) / numpy.log((max_E / min_E)) red = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum(numpy.zeros_like(rho.number), log_rho)).reshape(shape) green = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum(numpy.zeros_like(rho.number), log_v)).reshape(shape) blue = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum(numpy.zeros_like(rho.number), log_E)).reshape(shape) alpha = numpy.minimum( numpy.ones_like(log_v), numpy.maximum(numpy.zeros_like(log_v), numpy.log( (rho / (10 * min_rho))))).reshape(shape) rgba = numpy.concatenate((red, green, blue, alpha), axis=2) pyplot.figure(figsize=(image_size[0] / 100.0, image_size[1] / 100.0), dpi=100) im = pyplot.figimage(rgba, origin='lower') pyplot.savefig(figname, transparent=True, dpi=100) print("\nHydroplot was saved to: ", figname) pyplot.close()
def hydro_plot(view, hydro_code, image_size, figname): """ view: the (physical) region to plot [xmin, xmax, ymin, ymax] hydro_code: hydrodynamics code in which the gas to be plotted is defined image_size: size of the output image in pixels (x, y) """ if not HAS_MATPLOTLIB: return shape = (image_size[0], image_size[1], 1) size = image_size[0] * image_size[1] axis_lengths = [0.0, 0.0, 0.0] | units.m axis_lengths[0] = view[1] - view[0] axis_lengths[1] = view[3] - view[2] grid = Grid.create(shape, axis_lengths) grid.x += view[0] grid.y += view[2] speed = grid.z.reshape(size) * (0 | 1/units.s) rho, rhovx, rhovy, rhovz, rhoe = hydro_code.get_hydro_state_at_point( grid.x.reshape(size), grid.y.reshape(size), grid.z.reshape(size), speed, speed, speed) min_v = 800.0 | units.km / units.s max_v = 3000.0 | units.km / units.s min_rho = 3.0e-9 | units.g / units.cm**3 max_rho = 1.0e-5 | units.g / units.cm**3 min_E = 1.0e11 | units.J / units.kg max_E = 1.0e13 | units.J / units.kg v_sqr = (rhovx**2 + rhovy**2 + rhovz**2) / rho**2 E = rhoe / rho log_v = numpy.log((v_sqr / min_v**2)) / numpy.log((max_v**2 / min_v**2)) log_rho = numpy.log((rho / min_rho)) / numpy.log((max_rho / min_rho)) log_E = numpy.log((E / min_E)) / numpy.log((max_E / min_E)) red = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum( numpy.zeros_like(rho.number), log_rho)).reshape(shape) green = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum( numpy.zeros_like(rho.number), log_v)).reshape(shape) blue = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum( numpy.zeros_like(rho.number), log_E)).reshape(shape) alpha = numpy.minimum( numpy.ones_like(log_v), numpy.maximum( numpy.zeros_like(log_v), numpy.log((rho / (10*min_rho))) ) ).reshape(shape) rgba = numpy.concatenate((red, green, blue, alpha), axis=2) pyplot.figure(figsize=(image_size[0]/100.0, image_size[1]/100.0), dpi=100) im = pyplot.figimage(rgba, origin='lower') pyplot.savefig(figname, transparent=True, dpi=100, facecolor='k', edgecolor='k') print "\nHydroplot was saved to: ", figname pyplot.close()
def setup_particles_in_nbodycode(self): staggered_grid_shape = numpy.asarray(self.gridcode.grid.shape) + 2 corner0 = self.gridcode.grid[0][0][0].position corner1 = self.gridcode.grid[-1][-1][-1].position delta = self.gridcode.grid[1][1][1].position - corner0 print delta.prod() self.volume = delta.prod() staggered_corner0 = corner0 - delta staggered_corner1 = corner1 self.staggered_grid = Grid.create( staggered_grid_shape, staggered_corner1 - staggered_corner0 + delta) # self.staggered_grid.x += staggered_corner0.x # self.staggered_grid.y += staggered_corner0.x # self.staggered_grid.z += staggered_corner0.x # self.staggered_grid.p000 = 0.0 | mass # self.staggered_grid.p100 = 0.0 | mass # self.staggered_grid.p010 = 0.0 | mass # self.staggered_grid.p110 = 0.0 | mass # self.staggered_grid.p000 = 0.0 | mass # self.staggered_grid.p100 = 0.0 | mass # self.staggered_grid.p011 = 0.0 | mass # self.staggered_grid.p111 = 0.0 | mass # self.staggered_grid.p001 = 0.0 | mass # self.staggered_grid.p101 = 0.0 | mass self.normal_grid = Grid.create( self.gridcode.grid.shape, corner1 - corner0 + delta) particles = Particles(self.normal_grid.size) particles.mass = 0.0 | mass particles.x = self.grid.x.flatten() particles.y = self.grid.y.flatten() particles.z = self.grid.z.flatten() particles.vx = 0.0 | speed particles.vy = 0.0 | speed particles.vz = 0.0 | speed particles.radius = 0.0 | length self.nbodycode.particles.add_particles(particles) self.from_model_to_nbody = particles.new_channel_to( self.nbodycode.particles) self.nbodycode.commit_particles() self.particles = particles
def setup_particles_in_nbodycode(self): staggered_grid_shape = numpy.asarray(self.gridcode.grid.shape) + 2 corner0 = self.gridcode.grid[0][0][0].position corner1 = self.gridcode.grid[-1][-1][-1].position delta = self.gridcode.grid[1][1][1].position - corner0 print delta.prod() self.volume = delta.prod() staggered_corner0 = corner0 - delta staggered_corner1 = corner1 self.staggered_grid = Grid.create( staggered_grid_shape, staggered_corner1 - staggered_corner0 + delta) #self.staggered_grid.x += staggered_corner0.x #self.staggered_grid.y += staggered_corner0.x #self.staggered_grid.z += staggered_corner0.x #self.staggered_grid.p000 = 0.0 | mass #self.staggered_grid.p100 = 0.0 | mass #self.staggered_grid.p010 = 0.0 | mass #self.staggered_grid.p110 = 0.0 | mass #self.staggered_grid.p000 = 0.0 | mass #self.staggered_grid.p100 = 0.0 | mass #self.staggered_grid.p011 = 0.0 | mass #self.staggered_grid.p111 = 0.0 | mass #self.staggered_grid.p001 = 0.0 | mass #self.staggered_grid.p101 = 0.0 | mass self.normal_grid = Grid.create(self.gridcode.grid.shape, corner1 - corner0 + delta) particles = Particles(self.normal_grid.size) particles.mass = 0.0 | mass particles.x = self.grid.x.flatten() particles.y = self.grid.y.flatten() particles.z = self.grid.z.flatten() particles.vx = 0.0 | speed particles.vy = 0.0 | speed particles.vz = 0.0 | speed particles.radius = 0.0 | length self.nbodycode.particles.add_particles(particles) self.from_model_to_nbody = particles.new_channel_to( self.nbodycode.particles) self.nbodycode.commit_particles() self.particles = particles
def test20(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test20"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) shape = 10, 10, 10 p = Grid(*shape) p.mass = (numpy.asarray([x * 2.0 for x in range(p.size)])).reshape(shape) io.write_set_to_file(p, output_file, "hdf5", version = self.store_version()) loaded = io.read_set_from_file(output_file, "hdf5", version = self.store_version()) loaded = self.get_version_in_store(loaded) self.assertAlmostRelativeEquals(p.mass[0][1][2], 24) self.assertAlmostRelativeEquals(p[0][1][2].mass, 24)
def test12(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test12"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) shape = 10, 10, 10 p = Grid(*shape) p.mass = ([x * 2.0 for x in range(p.size)] | units.kg).reshape(shape) p.model_time = 2.0 | units.s io.write_set_to_file(p.savepoint(timestamp = 2 | units.Myr, scale = 1 | units.kg), output_file, "hdf5", version = self.store_version()) loaded_particles = io.read_set_from_file(output_file, "hdf5", version = self.store_version()) loaded_particles = self.get_version_in_store(loaded_particles) a = loaded_particles.collection_attributes self.assertAlmostRelativeEquals(a.timestamp, 2 | units.Myr) self.assertAlmostRelativeEquals(a.scale, 1 | units.kg)
def test26(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test26"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) p = Grid(20) io.write_set_to_file(p, output_file, format='amuse', version = self.store_version()) os.remove(output_file)
def setup_simple_grid(self): test_grid = Grid.create((4, 3, 2), [1.0, 1.0, 1.0] | units.m) test_grid.rho = numpy.linspace(1.0, 2.0, num=24).reshape( test_grid.shape) | units.kg / units.m**3 test_grid.rhovx = test_grid.rho * (3.0 | units.m / units.s) test_grid.rhovy = test_grid.rho * (4.0 | units.m / units.s) test_grid.rhovz = test_grid.rho * (0.0 | units.m / units.s) test_grid.energy = test_grid.rho * ((1.0 | (units.m / units.s)**2) + 0.5 * (5.0 | units.m / units.s)**2) return test_grid
def test53(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test53"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) stars = Particles(2) stars[0].x = 1.0 | units.km stars[1].x = 2.0 | units.km gas = Grid(2,3) gas.y = [[1.0, 2.0, 3.0], [4.0,5.0,6.0]] | units.km stars[0].gas = gas io.write_set_to_file(stars, output_file, "hdf5", version = self.store_version()) loaded_stars = io.read_set_from_file(output_file, "hdf5", version = self.store_version()) self.assertAlmostRelativeEquals(loaded_stars[0].gas[0][0].y,1.0 | units.km) self.assertAlmostRelativeEquals(loaded_stars[0].gas[0][2].y,3.0 | units.km) self.assertEquals(loaded_stars[1].gas, None)
def test54b(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test24"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) stars = Particles(2) stars[0].x = 1.0 | units.km stars[1].x = 2.0 | units.km gas = Grid(2,3) gas.y = [[1.0, 2.0, 3.0], [4.0,5.0,6.0]] | units.km gas[0][0].particle = stars[0] gas[1][2].particle = stars[0] gas[1][0].particle = stars[1] #stars[0].gas = gas[0][0] #stars[1].gas = gas[1][0] self.assertAlmostRelativeEquals(gas[0][0].particle.x,1.0 | units.km) self.assertAlmostRelativeEquals(gas[1][2].particle.x,1.0 | units.km) self.assertAlmostRelativeEquals(gas[1][0].particle.x,2.0 | units.km) io.write_set_to_file(gas, output_file, "hdf5", version = self.store_version()) loaded_gas = io.read_set_from_file(output_file, "hdf5", version = self.store_version()) self.assertAlmostRelativeEquals(loaded_gas[0][0].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(loaded_gas[1][2].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(loaded_gas[1][0].particle.x, 2.0 | units.km) self.assertEquals(id(loaded_gas[0][0].particle.get_containing_set()), id(loaded_gas[1][2].particle.get_containing_set())) self.assertEqual(loaded_gas.particle.shape, loaded_gas.shape) gas_copy = loaded_gas.copy() gas_copy[0][0].particle.x = 3 | units.km self.assertAlmostRelativeEquals(loaded_gas[0][0].particle.x, 1.0 | units.km) self.assertAlmostRelativeEquals(gas_copy[0][0].particle.x, 3.0 | units.km) self.assertAlmostRelativeEquals(gas_copy[1][2].particle.x, 3.0 | units.km)
def test56(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test26" + self.store_version() + ".hdf5") if os.path.exists(output_file): os.remove(output_file) number_of_particles = 10 p = Particles(number_of_particles) p.mass = [x * 2.0 for x in range(number_of_particles)] | units.kg p.model_time = 2.0 | units.s gas = Grid(2, 3) gas.y = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] | units.km io.write_set_to_file(p, output_file, "hdf5", particle=p[1], particles=p, gridpoint=gas[0][0], grid=gas, version=self.store_version()) loaded_particles = io.read_set_from_file(output_file, "hdf5", version=self.store_version(), copy_history=False, close_file=False) attributes = loaded_particles.collection_attributes self.assertAlmostRelativeEquals(attributes.particle.mass, loaded_particles[1].mass) self.assertEquals(attributes.particle.key, loaded_particles[1].key) self.assertEquals(id(attributes.particles), id(loaded_particles)) self.assertAlmostRelativeEquals(attributes.gridpoint.y, 1.0 | units.km) self.assertAlmostRelativeEquals(attributes.grid[0][0].y, 1.0 | units.km)
def test6(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test6_grid"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) instance = self.store_factory()(output_file) shape = 10, 10, 10 p = Grid(*shape) p.mass = ([x * 2.0 for x in range(p.size)] | units.kg).reshape(shape) p.model_time = 2.0 | units.s instance.store_grid(p.savepoint(1| units.Myr)) loaded_grid = self.get_version_in_store(instance.load_grid()) self.assertAlmostRelativeEquals(loaded_grid.get_timestamp(), 1| units.Myr) self.assertEquals(loaded_grid.shape, shape) self.assertEquals(loaded_grid[0][0][0].mass, 0 | units.kg) self.assertAlmostRelativeEquals(loaded_grid[...,1,1].mass, p[...,1,1].mass) instance.close()
def test56(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test26"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) number_of_particles = 10 p = Particles(number_of_particles) p.mass = [x * 2.0 for x in range(number_of_particles)] | units.kg p.model_time = 2.0 | units.s gas = Grid(2,3) gas.y = [[1.0, 2.0, 3.0], [4.0,5.0,6.0]] | units.km io.write_set_to_file( p, output_file, "hdf5", particle = p[1], particles = p, gridpoint = gas[0][0], grid = gas, version = self.store_version() ) loaded_particles = io.read_set_from_file( output_file, "hdf5", version = self.store_version() ) attributes = loaded_particles.collection_attributes self.assertAlmostRelativeEquals(attributes.particle.mass, loaded_particles[1].mass) self.assertAlmostRelativeEquals(attributes.particle.key, loaded_particles[1].key) self.assertEquals(id(attributes.particles), id(loaded_particles)) self.assertAlmostRelativeEquals(attributes.gridpoint.y, 1.0 | units.km) self.assertAlmostRelativeEquals(attributes.grid[0][0].y, 1.0 | units.km)
def test27(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test27"+self.store_version()+".hdf5") if os.path.exists(output_file): os.remove(output_file) instance = self.store_factory()(output_file) shape = 10, p = Grid(*shape) p.mass = ([x * 2.0 for x in range(p.size)] | units.kg).reshape(shape) p.model_time = 2.0 | units.s instance.store_grid(p) loaded_grid = instance.load_grid() self.assertEquals(loaded_grid.shape, shape) loaded_mass_in_kg = loaded_grid.mass.value_in(units.kg) previous_mass_in_kg = p.mass.value_in(units.kg) for expected, actual in zip(previous_mass_in_kg, loaded_mass_in_kg): self.assertEquals(expected, actual) instance.close()
def test59(self): test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "test59"+self.store_version()+".h5") if os.path.exists(output_file): os.remove(output_file) g=Grid(10,10) p=Particles(2) p[0].sub=g[0:5] io.write_set_to_file(p, output_file,"amuse", version=self.store_version()) z = io.read_set_from_file(output_file,"amuse") os.remove(output_file)
def new_grid(self): density = mass / length**3 density = density momentum = speed * density energy = mass / (time**2 * length) grid = Grid.create(self.dimensions_of_mesh, (10.0, 10.0, 10.0) | length) grid.rho = self.rho_medium grid.rhovx = 0.0 | momentum grid.rhovy = 0.0 | momentum grid.rhovz = 0.0 | momentum grid.energy = 0.0 | energy return grid
def test_evolve_w_plot(sysfac, tend=1. | units.hour, dt=3600. | units.s, dtplot=None): sys = sysfac() if dtplot is None: dtplot = dt pyplot.ion() f = pyplot.figure(figsize=(10, 10)) pyplot.show() i = 0 Lx = sys.parameters.Lx grid = Grid.create((400, 400), (Lx, Lx)) dx, dy = grid.cellsize() Lx = Lx.value_in(1000 * units.km) x = grid.x.flatten() y = grid.y.flatten() while sys.model_time < tend - dtplot / 2: i = i + 1 sys.evolve_model(sys.model_time + dtplot, dt=dt) psi, dpsi = sys.get_psi_dpsidt(dx + 0. * x, x, y) psi = psi.reshape(grid.shape) # psi=sys.grid[:,:,0].psi f.clf() f1 = pyplot.subplot(111) f1.imshow(psi.transpose() / psi.max(), vmin=0, vmax=1, extent=[0, Lx, 0, Lx], origin="lower") f1.set_xlabel("x (x1000 km)") pyplot.draw() pyplot.savefig("test_bc.png") if i % 100 == 25: print("wait") raw_input() print("done") raw_input()
def derive_stellar_structure(self): sorted = self.sph_particles.pressure.argsort()[::-1] binned = sorted.reshape((-1, self.particles_per_zone)) stellar_model = Grid(binned.shape[0]) stellar_model.dmass = self.sph_particles.mass[binned].sum(axis=1) stellar_model.mass = stellar_model.dmass.accumulate() stellar_model.pressure = self.sph_particles.pressure[binned].sum( axis=1) stellar_model.rho = stellar_model.dmass / ( self.sph_particles.mass / self.sph_particles.density)[binned].sum(axis=1) stellar_model.radius = ( (3 / (4 * numpy.pi)) * stellar_model.dmass / stellar_model.rho).accumulate()**(1.0 / 3.0) * 1 stellar_model.temperature = ( (self.sph_particles.mass * self.sph_particles.u * self.sph_particles.mu)[binned].sum(axis=1) / (1.5 * constants.kB * stellar_model.dmass)).as_quantity_in(units.K) zeros = numpy.zeros(len(stellar_model.dmass)) stellar_model.luminosity = zeros - 1 | units.LSun attribute_names = self.sph_particles.get_attribute_names_defined_in_store( ) for attribute, name in [("h1", "X_H"), ("he4", "X_He"), ("c12", "X_C"), ("n14", "X_N"), ("o16", "X_O"), ("ne20", "X_Ne"), ("mg24", "X_Mg"), ("si28", "X_Si"), ("fe56", "X_Fe")]: if attribute in attribute_names: setattr( stellar_model, name, (self.sph_particles.mass * getattr( self.sph_particles, attribute))[binned].sum(axis=1) / stellar_model.dmass) else: setattr(stellar_model, name, zeros) return stellar_model
def derive_stellar_structure(self): sorted = self.sph_particles.pressure.argsort()[::-1] binned = sorted.reshape((-1, self.particles_per_zone)) stellar_model = Grid(binned.shape[0]) stellar_model.dmass = self.sph_particles.mass[binned].sum(axis=1) stellar_model.mass = stellar_model.dmass.accumulate() stellar_model.pressure= self.sph_particles.pressure[binned].sum(axis=1) stellar_model.rho = stellar_model.dmass / (self.sph_particles.mass / self.sph_particles.density)[binned].sum(axis=1) stellar_model.radius = ((3 / (4 * numpy.pi)) * stellar_model.dmass / stellar_model.rho).accumulate()**(1.0/3.0) * 1 stellar_model.temperature = ((self.sph_particles.mass * self.sph_particles.u * self.sph_particles.mu)[binned].sum(axis=1) / (1.5 * constants.kB * stellar_model.dmass)).as_quantity_in(units.K) zeros = numpy.zeros(len(stellar_model.dmass)) stellar_model.luminosity = zeros - 1 | units.LSun attribute_names = self.sph_particles.get_attribute_names_defined_in_store() for attribute, name in [("h1", "X_H"), ("he4", "X_He"), ("c12", "X_C"), ("n14", "X_N"), ("o16", "X_O"), ("ne20", "X_Ne"), ("mg24", "X_Mg"), ("si28", "X_Si"), ("fe56", "X_Fe")]: if attribute in attribute_names: setattr(stellar_model, name, (self.sph_particles.mass * getattr(self.sph_particles, attribute))[binned].sum(axis=1) / stellar_model.dmass) else: setattr(stellar_model, name, zeros) return stellar_model
def generate_grid(self,n=10,m=10, FI0=-80. | units.deg, FI1=80. | units.deg, L0=0. | units.deg, L1=360. | units.deg, dFI=None, dL=None): if dFI is None: dFI=(FI1-FI0)/n if dL is None: dL=(L1-L0)/m FI,L=numpy.mgrid[in_deg(FI0):in_deg(FI1):in_deg(dFI), in_deg(L0):in_deg(L1):in_deg(dL)] FI=FI|units.deg L=L|units.deg n,m=FI.shape fi,l=self.forward_transform(FI,L) grid=Grid((n,m)) grid.FI=FI grid.L=L grid.lat=to_deg(fi) grid.lon=to_deg(l) return grid
def get_sets(self): nodes=Grid(self.parameters["NP"]) if self.coordinates=="cartesian": nodes.x=self.p[:,0] | units.m nodes.y=self.p[:,1] | units.m else: nodes.lon=self.p[:,0] | units.deg nodes.lat=self.p[:,1] | units.deg nodes.depth=self.p[:,2] | units.m elements=Grid(self.parameters["NE"]) elements.nodes=[(x[1]-1,x[2]-1,x[3]-1) for x in self.t] elev_boundary=[] for type_,seg in self.elev_spec_boundary_seg: indices=seg-1 b=Grid(len(indices)) b.nodes=indices b.type=type_ elev_boundary.append(b) flow_boundary=[] for type_,seg in self.flow_spec_boundary_seg: indices=seg-1 b=Grid(len(indices)) b.nodes=indices b.type=type_ flow_boundary.append(b) return nodes,elements,elev_boundary,flow_boundary
def get_sets(self): nodes = Grid(self.parameters["NP"]) if self.coordinates == "cartesian": nodes.x = self.p[:, 0] | units.m nodes.y = self.p[:, 1] | units.m else: nodes.lon = self.p[:, 0] | units.deg nodes.lat = self.p[:, 1] | units.deg nodes.vmark = self.vmark elements = Grid(self.parameters["NE"]) elements.n1 = [(x[0]) for x in self.t] elements.n2 = [(x[1]) for x in self.t] elements.n3 = [(x[2]) for x in self.t] return nodes, elements #,elev_boundary,flow_boundary
def hydro_plot(view, hydro_code, image_size, time, figname): """ Produce a series of images suitable for conversion into a movie. view: the (physical) region to plot [xmin, xmax, ymin, ymax] hydro_code: hydrodynamics code in which the gas to be plotted is defined image_size: size of the output image in pixels (x, y) time: current hydro code time """ if not HAS_MATPLOTLIB: return shape = (image_size[0], image_size[1], 1) size = image_size[0] * image_size[1] axis_lengths = [0.0, 0.0, 0.0] | units.m axis_lengths[0] = view[1] - view[0] axis_lengths[1] = view[3] - view[2] grid = Grid.create(shape, axis_lengths) grid.x += view[0] grid.y += view[2] speed = grid.z.reshape(size) * (0 | 1 / units.s) rho, rhovx, rhovy, rhovz, rhoe \ = hydro_code.get_hydro_state_at_point( grid.x.reshape(size), grid.y.reshape(size), grid.z.reshape(size), speed, speed, speed) min_v = 800.0 | units.km / units.s max_v = 3000.0 | units.km / units.s min_rho = 3.0e-9 | units.g / units.cm**3 max_rho = 1.0e-5 | units.g / units.cm**3 min_E = 1.0e11 | units.J / units.kg max_E = 1.0e13 | units.J / units.kg v_sqr = (rhovx**2 + rhovy**2 + rhovz**2) / rho**2 E = rhoe / rho log_v = numpy.log((v_sqr / min_v**2)) / numpy.log((max_v**2 / min_v**2)) log_rho = numpy.log((rho / min_rho)) / numpy.log((max_rho / min_rho)) log_E = numpy.log((E / min_E)) / numpy.log((max_E / min_E)) red = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum(numpy.zeros_like(rho.number), log_rho)).reshape(shape) green = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum(numpy.zeros_like(rho.number), log_v)).reshape(shape) blue = numpy.minimum(numpy.ones_like(rho.number), numpy.maximum(numpy.zeros_like(rho.number), log_E)).reshape(shape) alpha = numpy.minimum( numpy.ones_like(log_v), numpy.maximum(numpy.zeros_like(log_v), numpy.log( (rho / (10 * min_rho))))).reshape(shape) rgba = numpy.concatenate((red, green, blue, alpha), axis=2) pyplot.figure(figsize=(image_size[0] / 100.0, image_size[1] / 100.0), dpi=100) im = pyplot.figimage(rgba, origin='lower') pyplot.savefig(figname, transparent=True, dpi=100, facecolor='k', edgecolor='k') print("Saved hydroplot at time", time, "in file") print(' ', figname) pyplot.close()
def new_grid(self): grid = Grid.create(self.dimensions_of_mesh, [1, 1, 1] | length) self.clear_grid(grid) return grid
def new_grid(): grid = Grid.create(DIMENSIONS_OF_MESH, [1, 1, 1] | length) clear_grid(grid) return grid
def initialize_grid(grid, magentic_field_grid): temp = [1,1,1] | length l = 1.0 | length; A_grid = Grid.create(DIMENSIONS_OF_A_MESH, temp + grid.cellsize()*[2,2,0]) A_grid.position -= A_grid.cellsize()*[0.5,0.5,0.0] A_grid.Az = ( B0*l/(4.0*PI)*numpy.cos(4.0*PI/l*A_grid.x) + B0*l/(2.0*PI)*numpy.cos(2.0*PI/l*A_grid.y)) assert grid.cellsize().x == A_grid.cellsize().x assert grid.cellsize().y == A_grid.cellsize().y # assert grid.dim[0] == A_grid.dim[0] magentic_field_grid.B1i = (A_grid.Az[:-1,1:,...] - A_grid.Az[:-1,:-1,...])/A_grid.cellsize().y; magentic_field_grid.B2i = -(A_grid.Az[1:,:-1,...] - A_grid.Az[:-1,:-1,...])/A_grid.cellsize().x; magentic_field_grid.B3i = 0.0 | magnetic_field; #magentic_field_grid.B1i[-1,...,...] = magentic_field_grid.B1i[0,...,...] #magentic_field_grid.B2i[...,-1,...] = magentic_field_grid.B2i[...,0,...] magentic_field_grid.B1c = 0.0 | magnetic_field; magentic_field_grid.B2c = 0.0 | magnetic_field; magentic_field_grid.B3c = 0.0 | magnetic_field; magentic_field_grid.divB = 0.0 | (magnetic_field / length) magentic_field_grid.B1c[:-1,...,...] = (magentic_field_grid.B1i[:-1,...,...] + magentic_field_grid.B1i[1:,...,...])*0.5 magentic_field_grid.B2c[...,:-1,...] = (magentic_field_grid.B2i[...,:-1,...] + magentic_field_grid.B2i[...,1:,...])*0.5 magentic_field_grid.B3c = magentic_field_grid.B3i # magentic_field_grid.B1c[-1,...,...] = magentic_field_grid.B1c[0,...,...] # magentic_field_grid.B2c[...,-1,...] = magentic_field_grid.B2c[...,0,...] mu0 = 1.0 | (mass*length/time**2/current**2) magentic_field_grid.divB[0:-1,0:-1,...] = ( (magentic_field_grid.B1i[:-1,:-1,...] - magentic_field_grid.B1i[1:,:-1,...])/grid.cellsize().x + (magentic_field_grid.B2i[:-1,:-1,...] - magentic_field_grid.B2i[:-1,1:,...])/grid.cellsize().y ) assert (abs(magentic_field_grid.divB[:-1,:-1,:-1]) < 1.0e-10 | magentic_field_grid.divB.unit).all() assert magentic_field_grid.B1i.unit == magnetic_field grid.rho = D0 grid.rhovx = -D0*V0*numpy.sin(2.0*PI/l*grid.y) grid.rhovy = D0*V0*numpy.sin(2.0*PI/l*grid.x) grid.rhovz = 0.0 | momentum print("sum rho vx:", grid.rhovx.sum()) print("sum rho vy:",grid.rhovy.sum()) print("sum rho vz:",grid.rhovz.sum()) grid.energy = ( P0 / (GAMMA - 1) + 0.5 * (grid.rhovx**2 + grid.rhovy**2 + grid.rhovz**2)/grid.rho + 0.5 * (magentic_field_grid.B1c[:-1,:-1,:-1]**2 + magentic_field_grid.B2c[:-1,:-1,:-1]**2 + magentic_field_grid.B3c[:-1,:-1,:-1]**2)/mu0 )
def initialize_grid(grid, magentic_field_grid): temp = [1, 1, 1] | length l = 1.0 | length A_grid = Grid.create(DIMENSIONS_OF_A_MESH, temp + grid.cellsize() * [2, 2, 0]) A_grid.position -= A_grid.cellsize() * [0.5, 0.5, 0.0] A_grid.Az = (B0 * l / (4.0 * PI) * numpy.cos(4.0 * PI / l * A_grid.x) + B0 * l / (2.0 * PI) * numpy.cos(2.0 * PI / l * A_grid.y)) assert grid.cellsize().x == A_grid.cellsize().x assert grid.cellsize().y == A_grid.cellsize().y # assert grid.dim[0] == A_grid.dim[0] magentic_field_grid.B1i = (A_grid.Az[:-1, 1:, ...] - A_grid.Az[:-1, :-1, ...]) / A_grid.cellsize().y magentic_field_grid.B2i = ( -(A_grid.Az[1:, :-1, ...] - A_grid.Az[:-1, :-1, ...]) / A_grid.cellsize().x) magentic_field_grid.B3i = 0.0 | magnetic_field # magentic_field_grid.B1i[-1,...,...] = magentic_field_grid.B1i[0,...,...] # magentic_field_grid.B2i[...,-1,...] = magentic_field_grid.B2i[...,0,...] magentic_field_grid.B1c = 0.0 | magnetic_field magentic_field_grid.B2c = 0.0 | magnetic_field magentic_field_grid.B3c = 0.0 | magnetic_field magentic_field_grid.divB = 0.0 | (magnetic_field / length) magentic_field_grid.B1c[:-1, ..., ...] = ( magentic_field_grid.B1i[:-1, ..., ...] + magentic_field_grid.B1i[1:, ..., ...]) * 0.5 magentic_field_grid.B2c[ ..., :-1, ...] = (magentic_field_grid.B2i[..., :-1, ...] + magentic_field_grid.B2i[..., 1:, ...]) * 0.5 magentic_field_grid.B3c = magentic_field_grid.B3i # magentic_field_grid.B1c[-1,...,...] = magentic_field_grid.B1c[0,...,...] # magentic_field_grid.B2c[...,-1,...] = magentic_field_grid.B2c[...,0,...] mu0 = 1.0 | (mass * length / time**2 / current**2) magentic_field_grid.divB[0:-1, 0:-1, ...] = ( (magentic_field_grid.B1i[:-1, :-1, ...] - magentic_field_grid.B1i[1:, :-1, ...]) / grid.cellsize().x + (magentic_field_grid.B2i[:-1, :-1, ...] - magentic_field_grid.B2i[:-1, 1:, ...]) / grid.cellsize().y) assert (abs(magentic_field_grid.divB[:-1, :-1, :-1]) < 1.0e-10 | magentic_field_grid.divB.unit).all() assert magentic_field_grid.B1i.unit == magnetic_field grid.rho = D0 grid.rhovx = -D0 * V0 * numpy.sin(2.0 * PI / l * grid.y) grid.rhovy = D0 * V0 * numpy.sin(2.0 * PI / l * grid.x) grid.rhovz = 0.0 | momentum print("sum rho vx:", grid.rhovx.sum()) print("sum rho vy:", grid.rhovy.sum()) print("sum rho vz:", grid.rhovz.sum()) grid.energy = (P0 / (GAMMA - 1) + 0.5 * (grid.rhovx**2 + grid.rhovy**2 + grid.rhovz**2) / grid.rho + 0.5 * (magentic_field_grid.B1c[:-1, :-1, :-1]**2 + magentic_field_grid.B2c[:-1, :-1, :-1]**2 + magentic_field_grid.B3c[:-1, :-1, :-1]**2) / mu0)
def create_grid(*arg): grid = Grid.create(*arg) grid.add_vector_attribute("momentum", ["rhovx", "rhovy", "rhovz"]) return grid
def new_grid(self): grid = Grid.create(self.dimensions_of_mesh, [1,1,1] | length) self.clear_grid(grid) return grid
def new_grid(): grid = Grid.create(DIMENSIONS_OF_MESH, [1,1,1] | length) clear_grid(grid) return grid