def _parse_parameter_file(self): # Read all parameters. simu = self._read_log_simu() param = self._read_parameter() # Set up general information. self.filename_template = self.parameter_filename self.file_count = 1 self.parameters.update(param) self.particle_types = ('halos') self.particle_types_raw = ('halos') self.unique_identifier = \ int(os.stat(self.parameter_filename)[stat.ST_CTIME]) # Set up geometrical information. self.refine_by = 2 self.dimensionality = 3 nz = 1 << self.over_refine_factor self.domain_dimensions = np.ones(self.dimensionality, "int32") * nz self.domain_left_edge = np.array([0.0, 0.0, 0.0]) # Note that boxsize is in Mpc but particle positions are in kpc. self.domain_right_edge = np.array([simu['boxsize']] * 3) * 1000 self.periodicity = (True, True, True) # Set up cosmological information. self.cosmological_simulation = 1 self.current_redshift = param['z'] self.omega_lambda = simu['lambda0'] self.omega_matter = simu['omega0'] cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time(param['z']).in_units('s')
def read_caesar(c_file, selected_gals): print('Reading caesar file...') sim = caesar.load(c_file, LoadHalo=False) redshift = np.round(sim.simulation.redshift, decimals=2) h = sim.simulation.hubble_constant cosmo = Cosmology(hubble_constant=sim.simulation.hubble_constant, omega_matter=sim.simulation.omega_matter, omega_lambda=sim.simulation.omega_lambda, omega_curvature=0) thubble = cosmo.hubble_time(redshift).in_units("Gyr") H0 = (100 * sim.simulation.hubble_constant) * km / s / Mpc rhocrit = 3. * H0.to('1/s')**2 / (8 * np.pi * sim.simulation.G) mlim = 32 * rhocrit.to('Msun/kpc**3') * sim.simulation.boxsize.to( 'kpc' )**3 * sim.simulation.omega_baryon / sim.simulation.effective_resolution**3 / sim.simulation.scale_factor**3 # galaxy mass resolution limit: 32 gas particle masses gals = np.array([]) # read galaxy particle data for the selected galaxies if isinstance(selected_gals, np.ndarray): gals = np.asarray([ i for i in sim.galaxies if i.GroupID in selected_gals and i.masses['stellar'] > mlim ]) # select resolved galaxies print('Galaxy data from caesar file extracted and saved.') return sim, redshift, h, cosmo, thubble, H0, rhocrit, mlim, gals
def _parse_parameter_file(self): with open(self.parameter_filename, "rb") as f: hvals = fpu.read_cattrs(f, header_dt) hvals.pop("unused") self.dimensionality = 3 self.refine_by = 2 self.unique_identifier = \ int(os.stat(self.parameter_filename)[stat.ST_CTIME]) prefix = ".".join(self.parameter_filename.rsplit(".", 2)[:-2]) self.filename_template = "%s.%%(num)s%s" % (prefix, self._suffix) self.file_count = len(glob.glob(prefix + ".*" + self._suffix)) # Now we can set up things we already know. self.cosmological_simulation = 1 self.current_redshift = (1.0 / hvals['scale']) - 1.0 self.hubble_constant = hvals['h0'] self.omega_lambda = hvals['Ol'] self.omega_matter = hvals['Om'] cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time( self.current_redshift).in_units("s") self.periodicity = (True, True, True) self.particle_types = ("halos") self.particle_types_raw = ("halos") self.domain_left_edge = np.array([0.0, 0.0, 0.0]) self.domain_right_edge = np.array([hvals['box_size']] * 3) nz = 1 << self.over_refine_factor self.domain_dimensions = np.ones(3, "int32") * nz self.parameters.update(hvals)
def _parse_parameter_file(self): hvals = self._get_hvals() self.dimensionality = 3 self.refine_by = 2 self.parameters["HydroMethod"] = "sph" self.unique_identifier = \ int(os.stat(self.parameter_filename)[stat.ST_CTIME]) # Set standard values # We may have an overridden bounding box. if self.domain_left_edge is None: self.domain_left_edge = np.zeros(3, "float64") self.domain_right_edge = np.ones(3, "float64") * hvals["BoxSize"] nz = 1 << self.over_refine_factor self.domain_dimensions = np.ones(3, "int32") * nz self.periodicity = (True, True, True) self.cosmological_simulation = 1 self.current_redshift = hvals["Redshift"] self.omega_lambda = hvals["OmegaLambda"] self.omega_matter = hvals["Omega0"] self.hubble_constant = hvals["HubbleParam"] # According to the Gadget manual, OmegaLambda will be zero for # non-cosmological datasets. However, it may be the case that # individuals are running cosmological simulations *without* Lambda, in # which case we may be doing something incorrect here. # It may be possible to deduce whether ComovingIntegration is on # somehow, but opinions on this vary. if self.omega_lambda == 0.0: mylog.info("Omega Lambda is 0.0, so we are turning off Cosmology.") self.hubble_constant = 1.0 # So that scaling comes out correct self.cosmological_simulation = 0 self.current_redshift = 0.0 # This may not be correct. self.current_time = hvals["Time"] else: # Now we calculate our time based on the cosmology, because in # ComovingIntegration hvals["Time"] will in fact be the expansion # factor, not the actual integration time, so we re-calculate # global time from our Cosmology. cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time(self.current_redshift) mylog.info("Calculating time from %0.3e to be %0.3e seconds", hvals["Time"], self.current_time) self.parameters = hvals prefix = os.path.abspath( os.path.join(os.path.dirname(self.parameter_filename), os.path.basename(self.parameter_filename).split(".", 1)[0])) if hvals["NumFiles"] > 1: self.filename_template = "%s.%%(num)s%s" % (prefix, self._suffix) else: self.filename_template = self.parameter_filename self.file_count = hvals["NumFiles"]
def _parse_parameter_file(self): with open(self.parameter_filename, "rb") as f: hvals = fpu.read_cattrs(f, header_dt) hvals.pop("unused") self.dimensionality = 3 self.refine_by = 2 self.unique_identifier = int(os.stat(self.parameter_filename)[stat.ST_CTIME]) prefix = ".".join(self.parameter_filename.rsplit(".", 2)[:-2]) self.filename_template = "%s.%%(num)s%s" % (prefix, self._suffix) self.file_count = len(glob.glob(prefix + ".*" + self._suffix)) # Now we can set up things we already know. self.cosmological_simulation = 1 self.current_redshift = (1.0 / hvals["scale"]) - 1.0 self.hubble_constant = hvals["h0"] self.omega_lambda = hvals["Ol"] self.omega_matter = hvals["Om"] cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time(self.current_redshift).in_units("s") self.periodicity = (True, True, True) self.particle_types = "halos" self.particle_types_raw = "halos" self.domain_left_edge = np.array([0.0, 0.0, 0.0]) self.domain_right_edge = np.array([hvals["box_size"]] * 3) nz = 1 << self.over_refine_factor self.domain_dimensions = np.ones(3, "int32") * nz self.parameters.update(hvals)
def test_hubble_time(): """ Make sure hubble_time and t_from_z functions agree. """ for i in range(10): co = Cosmology() # random sample over interval (-1,100] z = -101 * np.random.random() + 100 assert_rel_equal(co.hubble_time(z), co.t_from_z(z), 5)
def test_hubble_time(): """ Make sure hubble_time and t_from_z functions agree. """ for i in range(10): co = Cosmology() # random sample over interval (-1,100] z = -101 * np.random.random() + 100 yield assert_rel_equal, co.hubble_time(z), co.t_from_z(z), 5
def _set_code_unit_attributes(self): # First try to set units based on parameter file if self.cosmological_simulation: mu = self.parameters.get('dMsolUnit', 1.) self.mass_unit = self.quan(mu, 'Msun') lu = self.parameters.get('dKpcUnit', 1000.) # In cosmological runs, lengths are stored as length*scale_factor self.length_unit = self.quan(lu, 'kpc') * self.scale_factor density_unit = self.mass_unit / (self.length_unit / self.scale_factor)**3 if 'dHubble0' in self.parameters: # Gasoline's internal hubble constant, dHubble0, is stored in # units of proper code time self.hubble_constant *= np.sqrt(G * density_unit) # Finally, we scale the hubble constant by 100 km/s/Mpc self.hubble_constant /= self.quan(100, 'km/s/Mpc') # If we leave it as a YTQuantity, the cosmology object # used below will add units back on. self.hubble_constant = self.hubble_constant.in_units("").d else: mu = self.parameters.get('dMsolUnit', 1.0) self.mass_unit = self.quan(mu, 'Msun') lu = self.parameters.get('dKpcUnit', 1.0) self.length_unit = self.quan(lu, 'kpc') # If unit base is defined by the user, override all relevant units if self._unit_base is not None: for my_unit in ["length", "mass", "time"]: if my_unit in self._unit_base: my_val = self._unit_base[my_unit] my_val = \ self.quan(*my_val) if isinstance(my_val, tuple) \ else self.quan(my_val) setattr(self, "%s_unit" % my_unit, my_val) # Finally, set the dependent units if self.cosmological_simulation: cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time(self.current_redshift) # mass units are rho_crit(z=0) * domain volume mu = cosmo.critical_density(0.0) * \ (1 + self.current_redshift)**3 * self.length_unit**3 self.mass_unit = self.quan(mu.in_units("Msun"), "Msun") density_unit = self.mass_unit / (self.length_unit / self.scale_factor)**3 # need to do this again because we've modified the hubble constant self.unit_registry.modify("h", self.hubble_constant) else: density_unit = self.mass_unit / self.length_unit**3 if not hasattr(self, "time_unit"): self.time_unit = 1.0 / np.sqrt(G * density_unit)
def _set_code_unit_attributes(self): if self.cosmological_simulation: mu = self.parameters.get('dMsolUnit', 1.) lu = self.parameters.get('dKpcUnit', 1000.) # In cosmological runs, lengths are stored as length*scale_factor self.length_unit = self.quan(lu, 'kpc')*self.scale_factor self.mass_unit = self.quan(mu, 'Msun') density_unit = self.mass_unit/ (self.length_unit/self.scale_factor)**3 # Gasoline's hubble constant, dHubble0, is stored units of proper code time. self.hubble_constant *= np.sqrt(G.in_units('kpc**3*Msun**-1*s**-2')*density_unit).value/(3.2407793e-18) cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time(self.current_redshift) else: mu = self.parameters.get('dMsolUnit', 1.0) self.mass_unit = self.quan(mu, 'Msun') lu = self.parameters.get('dKpcUnit', 1.0) self.length_unit = self.quan(lu, 'kpc') density_unit = self.mass_unit / self.length_unit**3 self.time_unit = 1.0 / np.sqrt(G * density_unit)
def _set_code_unit_attributes(self): if self.cosmological_simulation: mu = self.parameters.get('dMsolUnit', 1.) lu = self.parameters.get('dKpcUnit', 1000.) # In cosmological runs, lengths are stored as length*scale_factor self.length_unit = self.quan(lu, 'kpc') * self.scale_factor self.mass_unit = self.quan(mu, 'Msun') density_unit = self.mass_unit / (self.length_unit / self.scale_factor)**3 # Gasoline's hubble constant, dHubble0, is stored units of proper code time. self.hubble_constant *= np.sqrt( G.in_units('kpc**3*Msun**-1*s**-2') * density_unit).value / (3.2407793e-18) cosmo = Cosmology(self.hubble_constant, self.omega_matter, self.omega_lambda) self.current_time = cosmo.hubble_time(self.current_redshift) else: mu = self.parameters.get('dMsolUnit', 1.0) self.mass_unit = self.quan(mu, 'Msun') lu = self.parameters.get('dKpcUnit', 1.0) self.length_unit = self.quan(lu, 'kpc') density_unit = self.mass_unit / self.length_unit**3 self.time_unit = 1.0 / np.sqrt(G * density_unit)