def test_write(): """ Creates a sample dataset. """ # Box is 100 Mpc boxsize = 100 * unyt.Mpc # Generate object. cosmo_units corresponds to default Gadget-oid units # of 10^10 Msun, Mpc, and km/s x = Writer("galactic", boxsize) # 32^3 particles. n_p = 32**3 # Randomly spaced coordinates from 0, 100 Mpc in each direction x.gas.coordinates = np.random.rand(n_p, 3) * (100 * unyt.Mpc) # Random velocities from 0 to 1 km/s x.gas.velocities = np.random.rand(n_p, 3) * (unyt.km / unyt.s) # Generate uniform masses as 10^6 solar masses for each particle x.gas.masses = np.ones(n_p, dtype=float) * (1e6 * unyt.msun) # Generate internal energy corresponding to 10^4 K x.gas.internal_energy = np.ones( n_p, dtype=float) * (1e4 * unyt.kb * unyt.K) / (1e6 * unyt.msun) # Generate initial guess for smoothing lengths based on MIPS x.gas.generate_smoothing_lengths(boxsize=boxsize, dimension=3) # If IDs are not present, this automatically generates x.write("test.hdf5")
def generate_ics(redshift: float, filename: str, glass_filename: str) -> None: """ Generate initial conditions for the CoolingRedshiftDependence example. """ scale_factor = 1 / (1 + redshift) comoving_boxsize = boxsize / scale_factor glass_coordinates = get_coordinates(glass_filename) number_of_particles = len(glass_coordinates) gas_particle_mass = physical_density * (boxsize ** 3) / number_of_particles writer = Writer(cosmo_units, comoving_boxsize) writer.gas.coordinates = glass_coordinates * comoving_boxsize writer.gas.velocities = np.zeros_like(glass_coordinates) * cm / s writer.gas.masses = np.ones(number_of_particles, dtype=float) * gas_particle_mass # Leave in physical units; handled by boxsize change. writer.gas.internal_energy = ( np.ones(number_of_particles, dtype=float) * 3.0 / 2.0 * (temperature * kb) / (mu_hydrogen * mh) ) writer.gas.generate_smoothing_lengths(boxsize=comoving_boxsize, dimension=3) writer.write(filename) return
def write_out_ics( filename, num_on_side, side_length=1.0, duplications=4, blob_radius=0.1, blob_location_x=0.5, inside_density=10, velocity_outside=2.7, # Actually the mach number ): x = Writer(cgs_unit_system, [side_length * duplications, side_length, side_length] * cm) positions_outside, velocities_outside = generate_outside_of_blob( num_on_side, duplications, side_length, blob_location_x, blob_radius, initial_velocity=1.0, ) positions_inside, velocities_inside = generate_blob( int(num_on_side * np.cbrt(inside_density)), side_length, blob_location_x, blob_radius, ) coordinates = np.concatenate([positions_inside, positions_outside]) velocities = np.concatenate([velocities_inside, velocities_outside]) x.gas.coordinates = coordinates * cm x.gas.velocities = velocities * cm / s x.gas.masses = (np.ones(coordinates.shape[0], dtype=float) * (side_length / num_on_side) * g) # Set to be (1 / n) c_s x.gas.internal_energy = (np.ones(coordinates.shape[0], dtype=float) * ( (1.0 / velocity_outside) * x.gas.velocities[-1][0])**2 / (5.0 / 3.0 - 1)) # Need to correct the particles inside the sphere so that we are in pressure equilibrium everywhere x.gas.internal_energy[:len(positions_inside)] /= inside_density x.gas.generate_smoothing_lengths(boxsize=(duplications / 2) * side_length * cm, dimension=3) x.write(filename) return
def test_reading_ic_units(): """ Test to ensure we are able to correctly read ICs created with swiftsimio """ # File we're writing to test_filename = "test_write_output_units.hdf5" # Box is 100 Mpc boxsize = 100 * unyt.Mpc # Generate object. cosmo_units corresponds to default Gadget-oid units # of 10^10 Msun, Mpc, and km/s x = Writer(cosmo_units, boxsize) # 32^3 particles. n_p = 32 ** 3 # Randomly spaced coordinates from 0, 100 Mpc in each direction x.gas.coordinates = np.random.rand(n_p, 3) * (100 * unyt.Mpc) # Random velocities from 0 to 1 km/s x.gas.velocities = np.random.rand(n_p, 3) * (unyt.km / unyt.s) # Generate uniform masses as 10^6 solar masses for each particle x.gas.masses = np.ones(n_p, dtype=float) * (1e6 * unyt.msun) # Generate internal energy corresponding to 10^4 K x.gas.internal_energy = ( np.ones(n_p, dtype=float) * (1e4 * unyt.kb * unyt.K) / (1e6 * unyt.msun) ) # Generate initial guess for smoothing lengths based on MIPS x.gas.generate_smoothing_lengths(boxsize=boxsize, dimension=3) # If IDs are not present, this automatically generates x.write(test_filename) data = load(test_filename) assert np.array_equal(data.gas.coordinates, x.gas.coordinates) assert np.array_equal(data.gas.velocities, x.gas.velocities) assert np.array_equal(data.gas.masses, x.gas.masses) assert np.array_equal(data.gas.internal_energy, x.gas.internal_energy) assert np.array_equal(data.gas.smoothing_length, x.gas.smoothing_length) remove(test_filename) return
def write_out_glass(filename, cube, side_length=1.0): x = Writer(cgs_unit_system, side_length * cm) x.gas.coordinates = cube * cm x.gas.velocities = np.zeros_like(cube) * cm / s x.gas.masses = np.ones(cube.shape[0], dtype=float) * g x.gas.internal_energy = np.ones(cube.shape[0], dtype=float) * erg / g x.gas.generate_smoothing_lengths(boxsize=side_length * cm, dimension=3) x.write(filename) return
def test_write(): """ Tests whether swiftsimio can handle a new particle type. If the test doesn't crash this is a success. """ # Use default units, i.e. cm, grams, seconds, Ampere, Kelvin unit_system = unyt.UnitSystem(name="default", length_unit=unyt.cm, mass_unit=unyt.g, time_unit=unyt.s) # Specify a new type in the metadata - currently done by editing the dictionaries directly. # TODO: Remove this terrible way of setting up different particle types. swp.particle_name_underscores[6] = "extratype" swp.particle_name_class[6] = "Extratype" swp.particle_name_text[6] = "Extratype" swmw.extratype = {"smoothing_length": "SmoothingLength", **swmw.shared} boxsize = 10 * unyt.cm x = Writer(unit_system, boxsize, unit_fields_generate_units=generate_units) x.extratype.coordinates = np.zeros((10, 3)) * unyt.cm for i in range(0, 10): x.extratype.coordinates[i][0] = float(i) x.extratype.velocities = np.zeros((10, 3)) * unyt.cm / unyt.s x.extratype.masses = np.ones(10, dtype=float) * unyt.g x.extratype.smoothing_length = np.ones(10, dtype=float) * (5.0 * unyt.cm) x.write("extra_test.hdf5") # Clean up these global variables we screwed around with... swp.particle_name_underscores.pop(6) swp.particle_name_class.pop(6) swp.particle_name_text.pop(6)
unitL = unyt.cm t_end = 1e-3 * unyt.s edgelen = unyt.c.to("cm/s") * t_end * 2.0 edgelen = edgelen.to(unitL) boxsize = unyt.unyt_array([edgelen.v, edgelen.v, 0.0], unitL) xs = unyt.unyt_array( [np.array([xs[0] * edgelen, xs[1] * edgelen, 0.0 * edgelen])], unitL ) xp *= edgelen h *= edgelen w = Writer(unit_system=unyt.unit_systems.cgs_unit_system, box_size=boxsize, dimension=2) w.gas.coordinates = xp w.stars.coordinates = xs w.gas.velocities = np.zeros(xp.shape) * (unyt.cm / unyt.s) w.stars.velocities = np.zeros(xs.shape) * (unyt.cm / unyt.s) w.gas.masses = np.ones(xp.shape[0], dtype=np.float64) * 100 * unyt.g w.stars.masses = np.ones(xs.shape[0], dtype=np.float64) * 100 * unyt.g w.gas.internal_energy = ( np.ones(xp.shape[0], dtype=np.float64) * (300.0 * unyt.kb * unyt.K) / (unyt.g) ) w.gas.smoothing_length = h w.stars.smoothing_length = w.gas.smoothing_length[:1] w.write("propagationTest-2D.hdf5")
w = Writer(unit_system=cosmo_units, box_size=boxsize, dimension=2) # write particle positions ans smoothing lengths w.gas.coordinates = xp w.stars.coordinates = xs w.gas.velocities = np.zeros(xp.shape) * (unitL / unyt.Myr) w.stars.velocities = np.zeros(xs.shape) * (unitL / unyt.Myr) w.gas.smoothing_length = h w.stars.smoothing_length = w.gas.smoothing_length[:1] # get gas masses nH = 1e-3 * unyt.cm ** (-3) rhoH = nH * unyt.proton_mass Mtot = rhoH * edgelen ** 3 mpart = Mtot / xp.shape[0] mpart = mpart.to(cosmo_units["mass"]) w.gas.masses = np.ones(xp.shape[0], dtype=np.float64) * mpart w.stars.masses = np.ones(xs.shape[0], dtype=np.float64) * mpart # get gas internal energy for a given temperature and composition XH = 1.0 # hydrogen mass fraction XHe = 0.0 # hydrogen mass fraction T = 100 * unyt.K XHI, XHII, XHeI, XHeII, XHeIII = get_mass_fractions(T, XH, XHe) mu = mean_molecular_weight(XHI, XHII, XHeI, XHeII, XHeIII) internal_energy = internal_energy(T, mu) w.gas.internal_energy = np.ones(xp.shape[0], dtype=np.float64) * internal_energy w.write("stromgrenSphere-2D.hdf5")
type=float, default=1.0) args = vars(parser.parse_args()) boxsize = args["boxsize"] * unyt.cm n_part = args["npart"] mass = args["mass"] * unyt.g low = args["low"] * unyt.erg / unyt.g high = args["high"] * unyt.erg / unyt.g if not (n_part % 2 == 0): raise AttributeError("Please ensure --npart is even.") cgs = unyt.unit_systems.cgs_unit_system boxsize = 1.0 * unyt.cm writer = Writer(cgs, boxsize, dimension=1) coordinates = np.zeros((n_part, 3), dtype=float) * unyt.cm coordinates[:, 0] = get_particle_positions(boxsize, n_part) writer.gas.coordinates = coordinates writer.gas.velocities = np.zeros( (n_part, 3), dtype=float) * unyt.cm / unyt.s writer.gas.masses = get_particle_masses(mass, n_part) writer.gas.internal_energy = get_particle_u(low, high, n_part) writer.gas.smoothing_length = get_particle_hsml(boxsize, n_part) writer.write("diffusion.hdf5")
w.gas.coordinates = xp w.stars.coordinates = xs w.gas.velocities = np.zeros(xp.shape) * (unyt.cm / unyt.s) w.stars.velocities = np.zeros(xs.shape) * (unyt.cm / unyt.s) w.gas.masses = np.ones(xp.shape[0], dtype=np.float64) * 1000 * unyt.g w.stars.masses = np.ones(xs.shape[0], dtype=np.float64) * 1000 * unyt.g w.gas.internal_energy = (np.ones(xp.shape[0], dtype=np.float64) * (300.0 * unyt.kb * unyt.K) / unyt.g) # Generate initial guess for smoothing lengths based on MIPS w.gas.generate_smoothing_lengths(boxsize=boxsize, dimension=3) w.stars.generate_smoothing_lengths(boxsize=boxsize, dimension=3) # If IDs are not present, this automatically generates w.write("uniformBox-rt.hdf5") # Writing Photon Initial Conditions # -------------------------------------- # This part is specific for the GEAR RT scheme, assuming we're # running with nPhotonGroups photon groups. nPhotonGroups = 4 # Now open file back up again and add photon groups # you can make them unitless, the units have already been # written down in the writer. In this case, it's in cgs. F = h5py.File("uniformBox-rt.hdf5", "r+") header = F["Header"]
xs = unyt.unyt_array(xs_tot, boxsize.units) vp_tot = np.vstack((velp, velp_sampled)) vp = unyt.unyt_array(vp_tot, unyt.km / unyt.s) vs_tot = np.vstack((vels, vels_sampled)) vs = unyt.unyt_array(vs_tot, unyt.km / unyt.s) w = Writer(cosmo_units, boxsize) w.gas.coordinates = xp w.stars.coordinates = xs w.gas.velocities = vp w.stars.velocities = vs w.gas.masses = np.ones(xp.shape[0], dtype=np.float64) * 1e6 * unyt.msun w.stars.masses = np.random.uniform(1e8, 1e10, size=xs.shape[0]) * unyt.msun # Generate internal energy corresponding to 10^4 K w.gas.internal_energy = ( np.ones(xp.shape[0], dtype=np.float64) * (1e4 * unyt.kb * unyt.K) / (1e6 * unyt.msun) ) # Generate initial guess for smoothing lengths based on MIPS w.gas.generate_smoothing_lengths(boxsize=boxsize, dimension=3) w.stars.generate_smoothing_lengths(boxsize=boxsize, dimension=3) # If IDs are not present, this automatically generates w.write("randomized-sine.hdf5")
number_of_particles = len(h) side_length = (number_of_particles * particle_mass / initial_density)**(1 / 3) side_length.convert_to_base(unit_system) print(f"Your box has a side length of {side_length}") # Find the central particle central_particle = np.sum((positions - 0.5)**2, axis=1).argmin() # Inject the feedback into that central particle background_internal_energy = ((1.0 / (mu * mh)) * (kb / (gamma - 1.0)) * initial_temperature) heated_internal_energy = (1.0 / (mu * mh)) * (kb / (gamma - 1.0)) * inject_temperature internal_energy = np.ones_like(h) * background_internal_energy internal_energy[central_particle] = heated_internal_energy # Now we have all the information we need to set up the initial conditions! output = Writer(unit_system=unit_system, box_size=side_length) output.gas.coordinates = positions * side_length output.gas.velocities = np.zeros_like(positions) * cm / s output.gas.smoothing_length = h * side_length output.gas.internal_energy = internal_energy output.gas.masses = np.ones_like(h) * particle_mass output.write(file_name)
mean_interparticle_sep = x_range / n_part**(1 / 3) writer.gas.smoothing_length = np.ones(n_part, dtype=float) * mean_interparticle_sep writer.gas.particle_ids = unyt.unyt_array(readsnap(filename, "pid", 0), None) def read_dm_quantity(name, unit, parttype): """ The DM particles are in three sets because of their different masses. In SWIFT we have to combine these. """ out = np.concatenate([readsnap(filename, name, p) for p in parttype]) * unit return out for name, parttype in {"dark_matter": [1], "boundary": [2, 3, 5]}.items(): writer_value = getattr(writer, name) writer_value.coordinates = read_dm_quantity("pos", length, parttype) writer_value.velocities = read_dm_quantity("vel", velocity, parttype) writer_value.masses = read_dm_quantity("mass", mass, parttype) writer_value.particle_ids = read_dm_quantity("pid", 1, parttype) writer.write("nifty.hdf5")
rho_bar_0 = Omega_bar * rho_crit_0 # From this, we calculate the mass of the gas particles gas_particle_mass = rho_bar_0 * boxsize**3 / (n_p_1D**3) # Generate object. cosmo_units corresponds to default Gadget-oid units # of 10^10 Msun, Mpc, and km/s x = Writer(cosmo_units, boxsize) # 32^3 particles. n_p = 32**3 # Make gas coordinates from 0, 100 Mpc in each direction x.gas.coordinates = glass_pos * boxsize # Random velocities from 0 to 1 km/s x.gas.velocities = np.zeros((n_p, 3)) * (unyt.km / unyt.s) # Generate uniform masses as 10^6 solar masses for each particle x.gas.masses = np.ones(n_p, dtype=float) * gas_particle_mass # Generate internal energy corresponding to 10^4 K x.gas.internal_energy = (np.ones(n_p, dtype=float) * (T_i * unyt.kb * unyt.K) / (1e6 * unyt.msun)) # Generate initial guess for smoothing lengths based on MIPS x.gas.generate_smoothing_lengths(boxsize=boxsize, dimension=3) # If IDs are not present, this automatically generates x.write(filename)
# generate the coordinates dist = np.random.rand(num_part) * (max_r - min_r) + min_r angle = np.random.rand(num_part) * 2 * np.pi # more easy to do it in 2D, therefore coords[:, 2] == 0 coords = np.zeros((num_part, 3)) coords[:, 0] = dist * np.sin(angle) coords[:, 1] = dist * np.cos(angle) coords += boxsize * 0.5 # generate the masses m = np.ones(num_part) * masses # generate the velocities sign = np.random.rand(num_part) sign[sign < 0.5] = -1 sign[sign >= 0.5] = 1 v = np.zeros((num_part, 3)) v[:, 0] = sign * np.sqrt(G * M / (dist * (1 + np.tan(angle)**2))) v[:, 1] = - np.tan(angle) * v[:, 0] # Write the snapshot units = unyt.UnitSystem("Planets", unyt.AU, unyt.mearth, unyt.yr) snapshot = Writer(units, boxsize * unyt.AU) snapshot.dark_matter.coordinates = coords * unyt.AU snapshot.dark_matter.velocities = v * unyt.AU / unyt.yr snapshot.dark_matter.masses = m * unyt.mearth snapshot.write(filename)
masses = np.ones(xp.shape[0], dtype=np.float64) * mpart # change some gas masses mask = xp[:, 0] > 0.5 * edgelen masses[mask] *= 3 w.gas.masses = masses w.gas.internal_energy = (np.ones(xp.shape[0], dtype=np.float64) * 1.25e6 * unyt.m**2 / unyt.s**2) # get velocities vels = np.zeros((xp.shape[0], 3)) vels[:, 0] = -1.0 vels[:, 1] = +1.0 w.gas.velocities = vels * 1000 * cosmo_units["length"] / cosmo_units["time"] w.write(outputfilename) # Now open file back up again and add RT data. F = h5py.File(outputfilename, "r+") header = F["Header"] nparts = header.attrs["NumPart_ThisFile"][0] parts = F["/PartType0"] pos = parts["Coordinates"] # Create initial ionization species mass fractions. HIdata = np.ones((nparts), dtype=np.float32) * 0.76 HIIdata = np.zeros((nparts), dtype=np.float32) HeIdata = np.ones((nparts), dtype=np.float32) * 0.24 HeIIdata = np.zeros((nparts), dtype=np.float32) HeIIIdata = np.zeros((nparts), dtype=np.float32)
[x.flatten() for x in np.meshgrid(*[base_coordinates] * 3)], "cm").T dataset.gas.generate_smoothing_lengths(boxsize=dataset.box_size, dimension=3) base_velocities = np.zeros_like(base_coordinates) dataset.gas.velocities = unyt.unyt_array( [x.flatten() for x in np.meshgrid(*[base_velocities] * 3)], "cm/s").T # Set the particle with the highest mass to be in the centre of # the volume for easier plotting later special_particle = (np.linalg.norm(dataset.gas.coordinates - unyt.unyt_quantity(0.5, "cm"), axis=1)).argmin() base_masses = np.ones(TOTAL_NUMBER_OF_PARTICLES, dtype=np.float32) base_masses[special_particle] = np.float32(PARTICLE_OVER_MASS_FACTOR) dataset.gas.masses = unyt.unyt_array(base_masses, "g") # Set internal energy to be consistent with a CFL time-step of the # required length internal_energy = (dataset.gas.smoothing_length[0] * 0.1 / TIME_STEP)**2 / (5 / 3 * (5 / 3 - 1)) internal_energies = (np.ones(TOTAL_NUMBER_OF_PARTICLES, dtype=np.float32) * internal_energy) dataset.gas.internal_energy = unyt.unyt_array(internal_energies, "cm**2 / s**2") dataset.gas.particle_ids = unyt.unyt_array( np.arange(TOTAL_NUMBER_OF_PARTICLES), "dimensionless") dataset.write("particleSplitting.hdf5")