def test_particle_overrefine(): np.random.seed(int(0x4d3d3d3)) data = {} bbox = [] for i, ax in enumerate('xyz'): DW = DRE[i] - DLE[i] LE = DLE[i] data["particle_position_%s" % ax] = \ np.random.normal(0.5, scale=0.05, size=(NPART)) * DW + LE bbox.append( [DLE[i], DRE[i]] ) bbox = np.array(bbox) _attrs = ('icoords', 'fcoords', 'fwidth', 'ires') for n_ref in [16, 32, 64, 512, 1024]: ds1 = load_particles(data, 1.0, bbox = bbox, n_ref = n_ref) dd1 = ds1.all_data() v1 = dict((a, getattr(dd1, a)) for a in _attrs) cv1 = dd1["cell_volume"].sum(dtype="float64") for over_refine in [1, 2, 3]: f = 1 << (3*(over_refine-1)) ds2 = load_particles(data, 1.0, bbox = bbox, n_ref = n_ref, over_refine_factor = over_refine) dd2 = ds2.all_data() v2 = dict((a, getattr(dd2, a)) for a in _attrs) for a in sorted(v1): assert_equal(v1[a].size * f, v2[a].size) cv2 = dd2["cell_volume"].sum(dtype="float64") assert_equal(cv1, cv2)
def load_particle_fields_mismatch(): x = np.random.uniform(size=100) y = np.random.uniform(size=100) z = np.random.uniform(size=200) data = {"particle_position_x": x, "particle_position_y": y, "particle_position_z": z} load_particles(data)
def do_particles(): f = h5py.File("../s11Qzm1h2_a1.0000.art.h5") #f = h5py.File("../agora.nb_a1.0018.art.h5") data = dict((k, f[k][:].astype("float64")) for k in f) bbox = np.array([[0.0, 128.0], [0.0, 128.0], [0.0, 128.0]]) data[ "particle_mass"] *= 2.2023338912587828e+43 # Convert to grams, only needed for s11Qzm1h2_a1.0000.art.h5 ds_particles = load_particles(data, 2.0604661199638546e+24, bbox=bbox, n_ref=NREF) center = 128 * np.array([0.492470, 0.533444, 0.476942]) #center = np.array([63.05873108, 68.22652435, 61.08340073]) ds_particles.add_particle_filter("finest") particle_vector_functions("all", ["particle_position_%s" % ax for ax in 'xyz'], ["particle_velocity_%s" % ax for ax in 'xyz'], StreamFieldInfo) particle_vector_functions("finest", ["particle_position_%s" % ax for ax in 'xyz'], ["particle_velocity_%s" % ax for ax in 'xyz'], StreamFieldInfo) particle_deposition_functions("all", "Coordinates", "particle_mass", StreamFieldInfo) particle_deposition_functions("finest", "Coordinates", "particle_mass", StreamFieldInfo) process_dataset(ds_particles, center)
def _parse_old_halo_list(data_ds, halo_list): r""" Convert the halo list into a loaded dataset. """ num_halos = len(halo_list) if num_halos == 0: return None # Set up fields that we want to pull from identified halos and their units new_fields = ['particle_identifier', 'particle_mass', 'particle_position_x', 'particle_position_y','particle_position_z', 'virial_radius'] new_units = [ '', 'g', 'cm', 'cm','cm','cm'] # Set up a dictionary based on those fields # with empty arrays where we will fill in their values halo_properties = { f : (np.zeros(num_halos),unit) \ for f, unit in zip(new_fields,new_units)} # Iterate through the halos pulling out their positions and virial quantities # and filling in the properties dictionary for i,halo in enumerate(halo_list): halo_properties['particle_identifier'][0][i] = i halo_properties['particle_mass'][0][i] = halo.virial_mass().in_cgs() halo_properties['virial_radius'][0][i] = halo.virial_radius().in_cgs() com = halo.center_of_mass().in_cgs() halo_properties['particle_position_x'][0][i] = com[0] halo_properties['particle_position_y'][0][i] = com[1] halo_properties['particle_position_z'][0][i] = com[2] # Define a bounding box based on original data ds bbox = np.array([data_ds.domain_left_edge.in_cgs(), data_ds.domain_right_edge.in_cgs()]).T # Create a ds with the halos as particles particle_ds = load_particles(halo_properties, bbox=bbox, length_unit = 1, mass_unit=1) # Create the field info dictionary so we can reference those fields particle_ds.create_field_info() for attr in ["current_redshift", "current_time", "domain_dimensions", "cosmological_simulation", "omega_lambda", "omega_matter", "hubble_constant"]: attr_val = getattr(data_ds, attr) setattr(particle_ds, attr, attr_val) particle_ds.current_time = particle_ds.current_time.in_cgs() particle_ds.unit_registry.modify("h", particle_ds.hubble_constant) # Comoving lengths for my_unit in ["m", "pc", "AU", "au"]: new_unit = "%scm" % my_unit particle_ds.unit_registry.add(new_unit, particle_ds.unit_registry.lut[my_unit][0] / (1 + particle_ds.current_redshift), length, "\\rm{%s}/(1+z)" % my_unit) return particle_ds
def test_arbitrary_grid(): for ncells in [64, 128, 256]: for px in [0.125, 0.25, 0.55519]: particle_data = { 'particle_position_x': np.array([px]), 'particle_position_y': np.array([0.5]), 'particle_position_z': np.array([0.5]), 'particle_mass': np.array([1.0]) } ds = load_particles(particle_data) LE = np.array([0.05, 0.05, 0.05]) RE = np.array([0.95, 0.95, 0.95]) dims = np.array([ncells, ncells, ncells]) dds = (RE - LE) / dims volume = ds.quan(np.product(dds), 'cm**3') obj = ds.arbitrary_grid(LE, RE, dims) deposited_mass = obj["deposit", "all_density"].sum() * volume yield assert_equal, deposited_mass, ds.quan(1.0, 'g') # Test that we get identical results to the covering grid for unigrid data. # Testing AMR data is much harder. for nprocs in [1, 2, 4, 8]: ds = fake_random_ds(32, nprocs=nprocs) for ref_level in [0, 1, 2]: cg = ds.covering_grid(ref_level, [0.0, 0.0, 0.0], 2**ref_level * ds.domain_dimensions) ag = ds.arbitrary_grid([0.0, 0.0, 0.0], [1.0, 1.0, 1.0], 2**ref_level * ds.domain_dimensions) yield assert_almost_equal, cg["density"], ag["density"]
def test_arbitrary_grid(): for ncells in [32, 64]: for px in [0.125, 0.25, 0.55519]: particle_data = { "particle_position_x": np.array([px]), "particle_position_y": np.array([0.5]), "particle_position_z": np.array([0.5]), "particle_mass": np.array([1.0]), } ds = load_particles(particle_data) for dims in ([ncells] * 3, [ncells, ncells / 2, ncells / 4]): LE = np.array([0.05, 0.05, 0.05]) RE = np.array([0.95, 0.95, 0.95]) dims = np.array(dims) dds = (RE - LE) / dims volume = ds.quan(np.product(dds), "cm**3") obj = ds.arbitrary_grid(LE, RE, dims) deposited_mass = obj["deposit", "all_density"].sum() * volume assert_equal(deposited_mass, ds.quan(1.0, "g")) LE = np.array([0.00, 0.00, 0.00]) RE = np.array([0.05, 0.05, 0.05]) obj = ds.arbitrary_grid(LE, RE, dims) deposited_mass = obj["deposit", "all_density"].sum() assert_equal(deposited_mass, 0) # Test that we get identical results to the covering grid for unigrid data. # Testing AMR data is much harder. for nprocs in [1, 2, 4, 8]: ds = fake_random_ds(32, nprocs=nprocs) for ref_level in [0, 1, 2]: cg = ds.covering_grid( ref_level, [0.0, 0.0, 0.0], 2 ** ref_level * ds.domain_dimensions ) ag = ds.arbitrary_grid( [0.0, 0.0, 0.0], [1.0, 1.0, 1.0], 2 ** ref_level * ds.domain_dimensions ) assert_almost_equal(cg["density"], ag["density"])
def test_position_location(): np.random.seed(int(0x4d3d3d3)) pos = np.random.normal(0.5, scale=0.05, size=(NPART,3)) * (DRE-DLE) + DLE # Now convert to integers data = {} bbox = [] for i, ax in enumerate('xyz'): np.clip(pos[:,i], DLE[i], DRE[i], pos[:,i]) bbox.append([DLE[i], DRE[i]]) data["particle_position_%s" % ax] = pos[:,i] bbox = np.array(bbox) ds = load_particles(data, 1.0, bbox = bbox, over_refine_factor = 2) oct_id, all_octs = ds.index.oct_handler.locate_positions(pos) for oi in sorted(all_octs): this_oct = pos[oct_id == oi] assert(np.all(this_oct >= all_octs[oi]["left_edge"])) assert(np.all(this_oct <= all_octs[oi]["right_edge"]))
def test_particle_octree_counts(): np.random.seed(int(0x4d3d3d3)) # Eight times as many! data = {} bbox = [] for i, ax in enumerate('xyz'): DW = DRE[i] - DLE[i] LE = DLE[i] data["particle_position_%s" % ax] = \ np.random.normal(0.5, scale=0.05, size=(NPART*8)) * DW + LE bbox.append( [DLE[i], DRE[i]] ) bbox = np.array(bbox) for n_ref in [16, 32, 64, 512, 1024]: ds = load_particles(data, 1.0, bbox = bbox, n_ref = n_ref) dd = ds.all_data() bi = dd["io","mesh_id"] v = np.bincount(bi.astype("intp")) assert_equal(v.max() <= n_ref, True) bi2 = dd["all","mesh_id"] assert_equal(bi, bi2)
def do_particles(): f = h5py.File("../s11Qzm1h2_a1.0000.art.h5") #f = h5py.File("../agora.nb_a1.0018.art.h5") data = dict((k, f[k][:].astype("float64")) for k in f) bbox = np.array([[0.0, 128.0], [0.0, 128.0], [0.0, 128.0]]) data["particle_mass"] *= 2.2023338912587828e+43 # Convert to grams, only needed for s11Qzm1h2_a1.0000.art.h5 ds_particles = load_particles(data, 2.0604661199638546e+24, bbox=bbox, n_ref = NREF) center = 128 * np.array([0.492470, 0.533444, 0.476942]) #center = np.array([63.05873108, 68.22652435, 61.08340073]) ds_particles.add_particle_filter("finest") particle_vector_functions("all", ["particle_position_%s" % ax for ax in 'xyz'], ["particle_velocity_%s" % ax for ax in 'xyz'], StreamFieldInfo) particle_vector_functions("finest", ["particle_position_%s" % ax for ax in 'xyz'], ["particle_velocity_%s" % ax for ax in 'xyz'], StreamFieldInfo) particle_deposition_functions("all", "Coordinates", "particle_mass", StreamFieldInfo) particle_deposition_functions("finest", "Coordinates", "particle_mass", StreamFieldInfo) process_dataset(ds_particles, center)
def _parse_old_halo_list(data_ds, halo_list): r""" Convert the halo list into a loaded dataset. """ num_halos = len(halo_list) if num_halos == 0: return None # Set up fields that we want to pull from identified halos and their units new_fields = [ 'particle_identifier', 'particle_mass', 'particle_position_x', 'particle_position_y', 'particle_position_z', 'virial_radius' ] new_units = ['', 'g', 'cm', 'cm', 'cm', 'cm'] # Set up a dictionary based on those fields # with empty arrays where we will fill in their values halo_properties = { f : (np.zeros(num_halos),unit) \ for f, unit in zip(new_fields,new_units)} # Iterate through the halos pulling out their positions and virial quantities # and filling in the properties dictionary for i, halo in enumerate(halo_list): halo_properties['particle_identifier'][0][i] = i halo_properties['particle_mass'][0][i] = halo.virial_mass().in_cgs() halo_properties['virial_radius'][0][i] = halo.virial_radius().in_cgs() com = halo.center_of_mass().in_cgs() halo_properties['particle_position_x'][0][i] = com[0] halo_properties['particle_position_y'][0][i] = com[1] halo_properties['particle_position_z'][0][i] = com[2] # Define a bounding box based on original data ds bbox = np.array([ data_ds.domain_left_edge.in_cgs(), data_ds.domain_right_edge.in_cgs() ]).T # Create a ds with the halos as particles particle_ds = load_particles(halo_properties, bbox=bbox, length_unit=1, mass_unit=1) # Create the field info dictionary so we can reference those fields particle_ds.create_field_info() for attr in [ "current_redshift", "current_time", "domain_dimensions", "cosmological_simulation", "omega_lambda", "omega_matter", "hubble_constant" ]: attr_val = getattr(data_ds, attr) setattr(particle_ds, attr, attr_val) particle_ds.current_time = particle_ds.current_time.in_cgs() particle_ds.unit_registry.modify("h", particle_ds.hubble_constant) # Comoving lengths for my_unit in ["m", "pc", "AU", "au"]: new_unit = "%scm" % my_unit particle_ds.unit_registry.add( new_unit, particle_ds.unit_registry.lut[my_unit][0] / (1 + particle_ds.current_redshift), length, "\\rm{%s}/(1+z)" % my_unit) return particle_ds