def fake_amr_ds(fields=("Density", ), geometry="cartesian", particles=0): from yt.frontends.stream.api import load_amr_grids prng = RandomState(0x4d3d3d3) LE, RE = _geom_transforms[geometry] LE = np.array(LE) RE = np.array(RE) data = [] for gspec in _amr_grid_index: level, left_edge, right_edge, dims = gspec left_edge = left_edge * (RE - LE) + LE right_edge = right_edge * (RE - LE) + LE gdata = dict(level=level, left_edge=left_edge, right_edge=right_edge, dimensions=dims) for f in fields: gdata[f] = prng.random_sample(dims) if particles: for i, f in enumerate('particle_position_%s' % ax for ax in 'xyz'): pdata = prng.random_sample(particles) pdata /= (right_edge[i] - left_edge[i]) pdata += left_edge[i] gdata['io', f] = (pdata, 'code_length') for f in ('particle_velocity_%s' % ax for ax in 'xyz'): gdata['io', f] = (prng.random_sample(particles) - 0.5, 'cm/s') gdata['io', 'particle_mass'] = (prng.random_sample(particles), 'g') data.append(gdata) bbox = np.array([LE, RE]).T return load_amr_grids(data, [32, 32, 32], geometry=geometry, bbox=bbox)
def setup(): """Prepare setup specific environment""" global test_ds grid_data = [ dict(left_edge=[0.0, 0.0, 0.0], right_edge=[1.0, 1.0, 1.], level=0, dimensions=[16, 16, 16]), dict(left_edge=[0.25, 0.25, 0.25], right_edge=[0.75, 0.75, 0.75], level=1, dimensions=[16, 16, 16]), dict(left_edge=[0.25, 0.25, 0.375], right_edge=[0.5, 0.5, 0.625], level=2, dimensions=[16, 16, 16]), dict(left_edge=[0.5, 0.5, 0.375], right_edge=[0.75, 0.75, 0.625], level=2, dimensions=[16, 16, 16]), dict(left_edge=[0.3125, 0.3125, 0.4375], right_edge=[0.4375, 0.4375, 0.5625], level=3, dimensions=[16, 16, 16]), dict(left_edge=[0.5625, 0.5625, 0.4375], right_edge=[0.6875, 0.6875, 0.5625], level=3, dimensions=[16, 16, 16]) ] for grid in grid_data: grid["density"] = \ np.random.random(grid["dimensions"]) * 2 ** grid["level"] test_ds = load_amr_grids(grid_data, [16, 16, 16], 1.0)
def setup_test_ds(): """Prepare setup specific environment""" grid_data = [ dict(left_edge=[0.0, 0.0, 0.0], right_edge=[1.0, 1.0, 1.], level=0, dimensions=[16, 16, 16]), dict(left_edge=[0.25, 0.25, 0.25], right_edge=[0.75, 0.75, 0.75], level=1, dimensions=[16, 16, 16]), dict(left_edge=[0.25, 0.25, 0.375], right_edge=[0.5, 0.5, 0.625], level=2, dimensions=[16, 16, 16]), dict(left_edge=[0.5, 0.5, 0.375], right_edge=[0.75, 0.75, 0.625], level=2, dimensions=[16, 16, 16]), dict(left_edge=[0.3125, 0.3125, 0.4375], right_edge=[0.4375, 0.4375, 0.5625], level=3, dimensions=[16, 16, 16]), dict(left_edge=[0.5625, 0.5625, 0.4375], right_edge=[0.6875, 0.6875, 0.5625], level=3, dimensions=[16, 16, 16]) ] for grid in grid_data: grid["density"] = \ (np.random.random(grid["dimensions"]) * 2 ** grid["level"], "g/cm**3") return load_amr_grids(grid_data, [16, 16, 16])
def fake_amr_ds( fields=("Density", ), geometry="cartesian", particles=0, length_unit=None): from yt.frontends.stream.api import load_amr_grids prng = RandomState(0x4D3D3D3) LE, RE = _geom_transforms[geometry] LE = np.array(LE) RE = np.array(RE) data = [] for gspec in _amr_grid_index: level, left_edge, right_edge, dims = gspec left_edge = left_edge * (RE - LE) + LE right_edge = right_edge * (RE - LE) + LE gdata = dict(level=level, left_edge=left_edge, right_edge=right_edge, dimensions=dims) for f in fields: gdata[f] = prng.random_sample(dims) if particles: for i, f in enumerate(f"particle_position_{ax}" for ax in "xyz"): pdata = prng.random_sample(particles) pdata /= right_edge[i] - left_edge[i] pdata += left_edge[i] gdata["io", f] = (pdata, "code_length") for f in (f"particle_velocity_{ax}" for ax in "xyz"): gdata["io", f] = (prng.random_sample(particles) - 0.5, "cm/s") gdata["io", "particle_mass"] = (prng.random_sample(particles), "g") data.append(gdata) bbox = np.array([LE, RE]).T return load_amr_grids(data, [32, 32, 32], geometry=geometry, bbox=bbox, length_unit=length_unit)
def fake_amr_ds(fields = ("Density",)): from yt.frontends.stream.api import load_amr_grids data = [] for gspec in _amr_grid_index: level, left_edge, right_edge, dims = gspec gdata = dict(level = level, left_edge = left_edge, right_edge = right_edge, dimensions = dims) for f in fields: gdata[f] = np.random.random(dims) data.append(gdata) return load_amr_grids(data, [32, 32, 32], 1.0)
def fake_amr_ds(fields=("Density", ), geometry="cartesian"): from yt.frontends.stream.api import load_amr_grids LE, RE = _geom_transforms[geometry] LE = np.array(LE) RE = np.array(RE) data = [] for gspec in _amr_grid_index: level, left_edge, right_edge, dims = gspec left_edge = left_edge * (RE - LE) + LE right_edge = right_edge * (RE - LE) + LE gdata = dict(level=level, left_edge=left_edge, right_edge=right_edge, dimensions=dims) for f in fields: gdata[f] = np.random.random(dims) data.append(gdata) bbox = np.array([LE, RE]).T return load_amr_grids(data, [32, 32, 32], geometry=geometry, bbox=bbox)
def fake_amr_ds(fields = ("Density",), geometry = "cartesian"): from yt.frontends.stream.api import load_amr_grids LE, RE = _geom_transforms[geometry] LE = np.array(LE) RE = np.array(RE) data = [] for gspec in _amr_grid_index: level, left_edge, right_edge, dims = gspec left_edge = left_edge * (RE - LE) + LE right_edge = right_edge * (RE - LE) + LE gdata = dict(level = level, left_edge = left_edge, right_edge = right_edge, dimensions = dims) for f in fields: gdata[f] = np.random.random(dims) data.append(gdata) bbox = np.array([LE, RE]).T return load_amr_grids(data, [32, 32, 32], 1.0, geometry=geometry, bbox=bbox)
def test_stream_particles(): num_particles = 100000 domain_dims = (64, 64, 64) dens = np.random.random(domain_dims) x = np.random.uniform(size=num_particles) y = np.random.uniform(size=num_particles) z = np.random.uniform(size=num_particles) m = np.ones(num_particles) # Field operators and cell flagging methods fo = [] fo.append(ic.TopHatSphere(0.1, [0.2, 0.3, 0.4], {"density": 2.0})) fo.append(ic.TopHatSphere(0.05, [0.7, 0.4, 0.75], {"density": 20.0})) rc = [fm.flagging_method_registry["overdensity"](1.0)] # Check that all of this runs ok without particles ug0 = load_uniform_grid({"density": dens}, domain_dims, 1.0, nprocs=8) amr0 = refine_amr(ug0, rc, fo, 3) grid_data = [] for grid in amr0.index.grids: data = dict( left_edge=grid.LeftEdge, right_edge=grid.RightEdge, level=grid.Level, dimensions=grid.ActiveDimensions, ) for field in amr0.field_list: data[field] = grid[field] grid_data.append(data) amr0 = load_amr_grids(grid_data, domain_dims) # Now add particles fields1 = { "density": dens, "particle_position_x": x, "particle_position_y": y, "particle_position_z": z, "particle_mass": m, } fields2 = fields1.copy() ug1 = load_uniform_grid(fields1, domain_dims, 1.0) ug2 = load_uniform_grid(fields2, domain_dims, 1.0, nprocs=8) # Check to make sure the number of particles is the same number_of_particles1 = np.sum([grid.NumberOfParticles for grid in ug1.index.grids]) number_of_particles2 = np.sum([grid.NumberOfParticles for grid in ug2.index.grids]) assert_equal(number_of_particles1, num_particles) assert_equal(number_of_particles1, number_of_particles2) for grid in ug2.index.grids: tot_parts = grid["io", "particle_position_x"].size tot_all_parts = grid["all", "particle_position_x"].size assert tot_parts == grid.NumberOfParticles assert tot_all_parts == grid.NumberOfParticles # Check to make sure the fields have been defined correctly for ptype in ("all", "io"): assert ( ug1._get_field_info(ptype, "particle_position_x").sampling_type == "particle" ) assert ( ug1._get_field_info(ptype, "particle_position_y").sampling_type == "particle" ) assert ( ug1._get_field_info(ptype, "particle_position_z").sampling_type == "particle" ) assert ug1._get_field_info(ptype, "particle_mass").sampling_type == "particle" assert not ug1._get_field_info("gas", "density").sampling_type == "particle" for ptype in ("all", "io"): assert ( ug2._get_field_info(ptype, "particle_position_x").sampling_type == "particle" ) assert ( ug2._get_field_info(ptype, "particle_position_y").sampling_type == "particle" ) assert ( ug2._get_field_info(ptype, "particle_position_z").sampling_type == "particle" ) assert ug2._get_field_info(ptype, "particle_mass").sampling_type == "particle" assert not ug2._get_field_info("gas", "density").sampling_type == "particle" # Now refine this amr1 = refine_amr(ug1, rc, fo, 3) for field in sorted(ug1.field_list): assert field in amr1.field_list grid_data = [] for grid in amr1.index.grids: data = dict( left_edge=grid.LeftEdge, right_edge=grid.RightEdge, level=grid.Level, dimensions=grid.ActiveDimensions, ) for field in amr1.field_list: if field[0] not in ("all", "nbody"): data[field] = grid[field] grid_data.append(data) amr2 = load_amr_grids(grid_data, domain_dims) # Check everything again number_of_particles1 = [grid.NumberOfParticles for grid in amr1.index.grids] number_of_particles2 = [grid.NumberOfParticles for grid in amr2.index.grids] assert_equal(np.sum(number_of_particles1), num_particles) assert_equal(number_of_particles1, number_of_particles2) for grid in amr1.index.grids: tot_parts = grid["io", "particle_position_x"].size tot_all_parts = grid["all", "particle_position_x"].size assert tot_parts == grid.NumberOfParticles assert tot_all_parts == grid.NumberOfParticles for grid in amr2.index.grids: tot_parts = grid["io", "particle_position_x"].size tot_all_parts = grid["all", "particle_position_x"].size assert tot_parts == grid.NumberOfParticles assert tot_all_parts == grid.NumberOfParticles assert ( amr1._get_field_info("all", "particle_position_x").sampling_type == "particle" ) assert ( amr1._get_field_info("all", "particle_position_y").sampling_type == "particle" ) assert ( amr1._get_field_info("all", "particle_position_z").sampling_type == "particle" ) assert amr1._get_field_info("all", "particle_mass").sampling_type == "particle" assert not amr1._get_field_info("gas", "density").sampling_type == "particle" assert ( amr2._get_field_info("all", "particle_position_x").sampling_type == "particle" ) assert ( amr2._get_field_info("all", "particle_position_y").sampling_type == "particle" ) assert ( amr2._get_field_info("all", "particle_position_z").sampling_type == "particle" ) assert amr2._get_field_info("all", "particle_mass").sampling_type == "particle" assert not amr2._get_field_info("gas", "density").sampling_type == "particle" # Now perform similar checks, but with multiple particle types num_dm_particles = 30000 xd = np.random.uniform(size=num_dm_particles) yd = np.random.uniform(size=num_dm_particles) zd = np.random.uniform(size=num_dm_particles) md = np.ones(num_dm_particles) num_star_particles = 20000 xs = np.random.uniform(size=num_star_particles) ys = np.random.uniform(size=num_star_particles) zs = np.random.uniform(size=num_star_particles) ms = 2.0 * np.ones(num_star_particles) dens = np.random.random(domain_dims) fields3 = { "density": dens, ("dm", "particle_position_x"): xd, ("dm", "particle_position_y"): yd, ("dm", "particle_position_z"): zd, ("dm", "particle_mass"): md, ("star", "particle_position_x"): xs, ("star", "particle_position_y"): ys, ("star", "particle_position_z"): zs, ("star", "particle_mass"): ms, } fields4 = fields3.copy() ug3 = load_uniform_grid(fields3, domain_dims, 1.0) ug4 = load_uniform_grid(fields4, domain_dims, 1.0, nprocs=8) # Check to make sure the number of particles is the same number_of_particles3 = np.sum([grid.NumberOfParticles for grid in ug3.index.grids]) number_of_particles4 = np.sum([grid.NumberOfParticles for grid in ug4.index.grids]) assert_equal(number_of_particles3, num_dm_particles + num_star_particles) assert_equal(number_of_particles3, number_of_particles4) for grid in ug4.index.grids: tot_parts = grid["dm", "particle_position_x"].size tot_parts += grid["star", "particle_position_x"].size tot_all_parts = grid["all", "particle_position_x"].size assert tot_parts == grid.NumberOfParticles assert tot_all_parts == grid.NumberOfParticles # Check to make sure the fields have been defined correctly for ptype in ("dm", "star"): assert ( ug3._get_field_info(ptype, "particle_position_x").sampling_type == "particle" ) assert ( ug3._get_field_info(ptype, "particle_position_y").sampling_type == "particle" ) assert ( ug3._get_field_info(ptype, "particle_position_z").sampling_type == "particle" ) assert ug3._get_field_info(ptype, "particle_mass").sampling_type == "particle" assert ( ug4._get_field_info(ptype, "particle_position_x").sampling_type == "particle" ) assert ( ug4._get_field_info(ptype, "particle_position_y").sampling_type == "particle" ) assert ( ug4._get_field_info(ptype, "particle_position_z").sampling_type == "particle" ) assert ug4._get_field_info(ptype, "particle_mass").sampling_type == "particle" # Now refine this amr3 = refine_amr(ug3, rc, fo, 3) for field in sorted(ug3.field_list): assert field in amr3.field_list grid_data = [] for grid in amr3.index.grids: data = dict( left_edge=grid.LeftEdge, right_edge=grid.RightEdge, level=grid.Level, dimensions=grid.ActiveDimensions, ) for field in amr3.field_list: if field[0] not in ("all", "nbody"): data[field] = grid[field] grid_data.append(data) amr4 = load_amr_grids(grid_data, domain_dims) # Check everything again number_of_particles3 = [grid.NumberOfParticles for grid in amr3.index.grids] number_of_particles4 = [grid.NumberOfParticles for grid in amr4.index.grids] assert_equal(np.sum(number_of_particles3), num_star_particles + num_dm_particles) assert_equal(number_of_particles3, number_of_particles4) for ptype in ("dm", "star"): assert ( amr3._get_field_info(ptype, "particle_position_x").sampling_type == "particle" ) assert ( amr3._get_field_info(ptype, "particle_position_y").sampling_type == "particle" ) assert ( amr3._get_field_info(ptype, "particle_position_z").sampling_type == "particle" ) assert amr3._get_field_info(ptype, "particle_mass").sampling_type == "particle" assert ( amr4._get_field_info(ptype, "particle_position_x").sampling_type == "particle" ) assert ( amr4._get_field_info(ptype, "particle_position_y").sampling_type == "particle" ) assert ( amr4._get_field_info(ptype, "particle_position_z").sampling_type == "particle" ) assert amr4._get_field_info(ptype, "particle_mass").sampling_type == "particle" for grid in amr3.index.grids: tot_parts = grid["dm", "particle_position_x"].size tot_parts += grid["star", "particle_position_x"].size tot_all_parts = grid["all", "particle_position_x"].size assert tot_parts == grid.NumberOfParticles assert tot_all_parts == grid.NumberOfParticles for grid in amr4.index.grids: tot_parts = grid["dm", "particle_position_x"].size tot_parts += grid["star", "particle_position_x"].size tot_all_parts = grid["all", "particle_position_x"].size assert tot_parts == grid.NumberOfParticles assert tot_all_parts == grid.NumberOfParticles
def test_stream_particles() : num_particles = 100000 domain_dims = (64, 64, 64) dens = np.random.random(domain_dims) x = np.random.uniform(size=num_particles) y = np.random.uniform(size=num_particles) z = np.random.uniform(size=num_particles) m = np.ones((num_particles)) # Field operators and cell flagging methods fo = [] fo.append(ic.TopHatSphere(0.1, [0.2,0.3,0.4],{"density": 2.0})) fo.append(ic.TopHatSphere(0.05, [0.7,0.4,0.75],{"density": 20.0})) rc = [fm.flagging_method_registry["overdensity"](1.0)] # Check that all of this runs ok without particles ug0 = load_uniform_grid({"density": dens}, domain_dims, 1.0, nprocs=8) amr0 = refine_amr(ug0, rc, fo, 3) grid_data = [] for grid in amr0.index.grids : data = dict(left_edge = grid.LeftEdge, right_edge = grid.RightEdge, level = grid.Level, dimensions = grid.ActiveDimensions, number_of_particles = grid.NumberOfParticles) for field in amr0.field_list : data[field] = grid[field] grid_data.append(data) amr0 = load_amr_grids(grid_data, domain_dims, 1.0) # Now add particles fields1 = {"density": dens, "particle_position_x": x, "particle_position_y": y, "particle_position_z": z, "particle_mass": m, "number_of_particles": num_particles} fields2 = fields1.copy() ug1 = load_uniform_grid(fields1, domain_dims, 1.0) ug2 = load_uniform_grid(fields2, domain_dims, 1.0, nprocs=8) # Check to make sure the number of particles is the same number_of_particles1 = np.sum([grid.NumberOfParticles for grid in ug1.index.grids]) number_of_particles2 = np.sum([grid.NumberOfParticles for grid in ug2.index.grids]) yield assert_equal, number_of_particles1, num_particles yield assert_equal, number_of_particles1, number_of_particles2 # Check to make sure the fields have been defined correctly for ptype in ("all", "io"): assert ug1._get_field_info(ptype, "particle_position_x").particle_type assert ug1._get_field_info(ptype, "particle_position_y").particle_type assert ug1._get_field_info(ptype, "particle_position_z").particle_type assert ug1._get_field_info(ptype, "particle_mass").particle_type assert not ug1._get_field_info("gas", "density").particle_type for ptype in ("all", "io"): assert ug2._get_field_info(ptype, "particle_position_x").particle_type assert ug2._get_field_info(ptype, "particle_position_y").particle_type assert ug2._get_field_info(ptype, "particle_position_z").particle_type assert ug2._get_field_info(ptype, "particle_mass").particle_type assert not ug2._get_field_info("gas", "density").particle_type # Now refine this amr1 = refine_amr(ug1, rc, fo, 3) for field in sorted(ug1.field_list): yield assert_equal, (field in amr1.field_list), True grid_data = [] for grid in amr1.index.grids : data = dict(left_edge = grid.LeftEdge, right_edge = grid.RightEdge, level = grid.Level, dimensions = grid.ActiveDimensions, number_of_particles = grid.NumberOfParticles) for field in amr1.field_list : data[field] = grid[field] grid_data.append(data) amr2 = load_amr_grids(grid_data, domain_dims, 1.0) # Check everything again number_of_particles1 = [grid.NumberOfParticles for grid in amr1.index.grids] number_of_particles2 = [grid.NumberOfParticles for grid in amr2.index.grids] yield assert_equal, np.sum(number_of_particles1), num_particles yield assert_equal, number_of_particles1, number_of_particles2 assert amr1._get_field_info("all", "particle_position_x").particle_type assert amr1._get_field_info("all", "particle_position_y").particle_type assert amr1._get_field_info("all", "particle_position_z").particle_type assert amr1._get_field_info("all", "particle_mass").particle_type assert not amr1._get_field_info("gas", "density").particle_type assert amr2._get_field_info("all", "particle_position_x").particle_type assert amr2._get_field_info("all", "particle_position_y").particle_type assert amr2._get_field_info("all", "particle_position_z").particle_type assert amr2._get_field_info("all", "particle_mass").particle_type assert not amr2._get_field_info("gas", "density").particle_type
def test_stream_particles(): num_particles = 100000 domain_dims = (64, 64, 64) dens = np.random.random(domain_dims) x = np.random.uniform(size=num_particles) y = np.random.uniform(size=num_particles) z = np.random.uniform(size=num_particles) m = np.ones((num_particles)) # Field operators and cell flagging methods fo = [] fo.append(ic.TopHatSphere(0.1, [0.2, 0.3, 0.4], {"density": 2.0})) fo.append(ic.TopHatSphere(0.05, [0.7, 0.4, 0.75], {"density": 20.0})) rc = [fm.flagging_method_registry["overdensity"](1.0)] # Check that all of this runs ok without particles ug0 = load_uniform_grid({"density": dens}, domain_dims, 1.0, nprocs=8) amr0 = refine_amr(ug0, rc, fo, 3) grid_data = [] for grid in amr0.index.grids: data = dict(left_edge=grid.LeftEdge, right_edge=grid.RightEdge, level=grid.Level, dimensions=grid.ActiveDimensions, number_of_particles=grid.NumberOfParticles) for field in amr0.field_list: data[field] = grid[field] grid_data.append(data) amr0 = load_amr_grids(grid_data, domain_dims, 1.0) # Now add particles fields1 = { "density": dens, "particle_position_x": x, "particle_position_y": y, "particle_position_z": z, "particle_mass": m, "number_of_particles": num_particles } fields2 = fields1.copy() ug1 = load_uniform_grid(fields1, domain_dims, 1.0) ug2 = load_uniform_grid(fields2, domain_dims, 1.0, nprocs=8) # Check to make sure the number of particles is the same number_of_particles1 = np.sum( [grid.NumberOfParticles for grid in ug1.index.grids]) number_of_particles2 = np.sum( [grid.NumberOfParticles for grid in ug2.index.grids]) yield assert_equal, number_of_particles1, num_particles yield assert_equal, number_of_particles1, number_of_particles2 # Check to make sure the fields have been defined correctly for ptype in ("all", "io"): assert ug1._get_field_info(ptype, "particle_position_x").particle_type assert ug1._get_field_info(ptype, "particle_position_y").particle_type assert ug1._get_field_info(ptype, "particle_position_z").particle_type assert ug1._get_field_info(ptype, "particle_mass").particle_type assert not ug1._get_field_info("gas", "density").particle_type for ptype in ("all", "io"): assert ug2._get_field_info(ptype, "particle_position_x").particle_type assert ug2._get_field_info(ptype, "particle_position_y").particle_type assert ug2._get_field_info(ptype, "particle_position_z").particle_type assert ug2._get_field_info(ptype, "particle_mass").particle_type assert not ug2._get_field_info("gas", "density").particle_type # Now refine this amr1 = refine_amr(ug1, rc, fo, 3) for field in sorted(ug1.field_list): yield assert_equal, (field in amr1.field_list), True grid_data = [] for grid in amr1.index.grids: data = dict(left_edge=grid.LeftEdge, right_edge=grid.RightEdge, level=grid.Level, dimensions=grid.ActiveDimensions, number_of_particles=grid.NumberOfParticles) for field in amr1.field_list: data[field] = grid[field] grid_data.append(data) amr2 = load_amr_grids(grid_data, domain_dims, 1.0) # Check everything again number_of_particles1 = [ grid.NumberOfParticles for grid in amr1.index.grids ] number_of_particles2 = [ grid.NumberOfParticles for grid in amr2.index.grids ] yield assert_equal, np.sum(number_of_particles1), num_particles yield assert_equal, number_of_particles1, number_of_particles2 assert amr1._get_field_info("all", "particle_position_x").particle_type assert amr1._get_field_info("all", "particle_position_y").particle_type assert amr1._get_field_info("all", "particle_position_z").particle_type assert amr1._get_field_info("all", "particle_mass").particle_type assert not amr1._get_field_info("gas", "density").particle_type assert amr2._get_field_info("all", "particle_position_x").particle_type assert amr2._get_field_info("all", "particle_position_y").particle_type assert amr2._get_field_info("all", "particle_position_z").particle_type assert amr2._get_field_info("all", "particle_mass").particle_type assert not amr2._get_field_info("gas", "density").particle_type