def setup_particle_mass_field(self, ptype): name = "particle_mass" if ptype in self.ds.particle_unions: add_union_field(self, ptype, name, "code_mass") return constants = nested_dict_get(self.ds.parameters, ("Particle", ptype, "constants"), default=[]) if not constants: names = [] else: if not isinstance(constants[0], tuple): constants = (constants, ) names = [c[0] for c in constants] if "mass" in names: val = constants[names.index("mass")][2] val = self.ds.quan(val, self.ds.mass_unit) if self.ds.cosmological_simulation: val = val / self.ds.domain_dimensions.prod() def _pmass(field, data): return val * data[ptype, "particle_ones"] self.add_field( (ptype, name), function=_pmass, units="code_mass", sampling_type="particle", )
def test_nested_dict_get(): rs = np.random.RandomState(47988) keys = [] my_dict = None for i in range(5): k = str(rs.randint(0, high=1000000)) if my_dict is None: v = str(rs.randint(0, high=1000000)) keys.append(v) my_dict = {k: v} else: my_dict = {k: my_dict} keys.append(k) keys.reverse() assert nested_dict_get(my_dict, keys[:-1]) == keys[-1] my_def = "devron" assert nested_dict_get(my_dict, "system", default=my_def) == my_def
def _set_code_unit_attributes(self): if self.cosmological_simulation: box_size = self.parameters["Physics"]["cosmology"][ "comoving_box_size"] k = cosmology_get_units( self.hubble_constant, self.omega_matter, box_size, self.parameters["Physics"]["cosmology"]["initial_redshift"], self.current_redshift, ) # Now some CGS values setdefaultattr(self, "length_unit", self.quan(box_size, "Mpccm/h")) setdefaultattr( self, "mass_unit", self.quan(k["urho"], "g/cm**3") * (self.length_unit.in_cgs())**3, ) setdefaultattr(self, "velocity_unit", self.quan(k["uvel"], "cm/s")) else: p = self.parameters for d, u in zip(("length", "time"), ("cm", "s")): val = nested_dict_get(p, ("Units", d), default=1) setdefaultattr(self, f"{d}_unit", self.quan(val, u)) mass = nested_dict_get(p, ("Units", "mass")) if mass is None: density = nested_dict_get(p, ("Units", "density")) if density is not None: mass = density * self.length_unit**3 else: mass = 1 setdefaultattr(self, "mass_unit", self.quan(mass, "g")) setdefaultattr(self, "velocity_unit", self.length_unit / self.time_unit) magnetic_unit = np.sqrt(4 * np.pi * self.mass_unit / (self.time_unit**2 * self.length_unit)) magnetic_unit = np.float64(magnetic_unit.in_cgs()) setdefaultattr(self, "magnetic_unit", self.quan(magnetic_unit, "gauss"))
def _parse_parameter_file(self): """ Parses the parameter file and establishes the various dictionaries. """ f = open(self.parameter_filename, "r") # get dimension from first block name b0, fn0 = f.readline().strip().split() level0, left0, right0 = get_block_info(b0, min_dim=0) root_blocks = get_root_blocks(b0) f.close() self.dimensionality = left0.size self.periodicity = \ ensure_tuple(np.ones(self.dimensionality, dtype=bool)) lcfn = self.parameter_filename[:-len(self._suffix)] + ".libconfig" if os.path.exists(lcfn): with io.open(lcfn, "r") as lf: self.parameters = libconf.load(lf) cosmo = nested_dict_get( self.parameters, ("Physics", "cosmology")) if cosmo is not None: self.cosmological_simulation = 1 co_pars = ["hubble_constant_now", "omega_matter_now", "omega_lambda_now", "comoving_box_size", "initial_redshift"] co_dict = \ dict((attr, nested_dict_get(self.parameters, ("Physics", "cosmology", attr))) for attr in co_pars) for attr in ["hubble_constant", "omega_matter", "omega_lambda"]: setattr(self, attr, co_dict["%s_now" % attr]) # Current redshift is not stored, so it's not possible # to set all cosmological units yet. # Get the time units and use that to figure out redshift. k = cosmology_get_units( self.hubble_constant, self.omega_matter, co_dict["comoving_box_size"], co_dict["initial_redshift"], 0) setdefaultattr( self, 'time_unit', self.quan(k['utim'], 's')) co = Cosmology(hubble_constant=self.hubble_constant, omega_matter=self.omega_matter, omega_lambda=self.omega_lambda) else: self.cosmological_simulation = 0 else: self.cosmological_simulation = 0 fh = h5py.File(os.path.join(self.directory, fn0), "r") self.domain_left_edge = fh.attrs["lower"] self.domain_right_edge = fh.attrs["upper"] # all blocks are the same size ablock = fh[list(fh.keys())[0]] self.current_time = ablock.attrs["time"][0] gsi = ablock.attrs["enzo_GridStartIndex"] gei = ablock.attrs["enzo_GridEndIndex"] self.ghost_zones = gsi[0] self.root_block_dimensions = root_blocks self.active_grid_dimensions = gei - gsi + 1 self.grid_dimensions = ablock.attrs["enzo_GridDimension"] self.domain_dimensions = root_blocks * self.active_grid_dimensions fh.close() if self.cosmological_simulation: self.current_redshift = \ co.z_from_t(self.current_time * self.time_unit) self.periodicity += (False, ) * (3 - self.dimensionality) self.gamma = nested_dict_get(self.parameters, ("Field", "gamma")) self.unique_identifier = \ str(int(os.stat(self.parameter_filename)[stat.ST_CTIME]))
def test_nested_dict_get_real_none(): my_dict = {"a": None} response = nested_dict_get(my_dict, "a", default="fail") assert response is None