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 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 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 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 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 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 __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 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 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 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 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 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 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 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