def test_root_blocks(): rs = np.random.RandomState(45652) for i in range(6): max_n = 2 ** i n1, l1, b1 = get_random_block_string(max_n=max_n, random_state=rs, level=0) n2, l2, b2 = get_random_block_string(max_n=32, random_state=rs, level=0) block = f"{b1}:{b2[1:]}" nrb = get_root_blocks(block, min_dim=1) assert nrb == max_n rbid = get_root_block_id(block, min_dim=1) assert rbid == n1
def _parse_parameter_file(self): """ Parses the parameter file and establishes the various dictionaries. """ f = open(self.parameter_filename) # 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 = tuple(np.ones(self.dimensionality, dtype=bool)) lcfn = self.parameter_filename[:-len(self._suffix)] + ".libconfig" if os.path.exists(lcfn): with open(lcfn) 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 = { 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[f"{attr}_now"]) # 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] self.parameters["current_cycle"] = ablock.attrs["cycle"][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"))
def _parse_index(self): self.grids = np.empty(self.num_grids, dtype="object") c = 1 pbar = get_pbar("Parsing Hierarchy", self.num_grids) f = open(self.ds.parameter_filename) fblock_size = 32768 f.seek(0, 2) file_size = f.tell() nblocks = np.ceil(float(file_size) / fblock_size).astype(np.int64) f.seek(0) offset = f.tell() lstr = "" # place child blocks after the root blocks rbdim = self.ds.root_block_dimensions nroot_blocks = rbdim.prod() child_id = nroot_blocks last_pid = None for _ib in range(nblocks): fblock = min(fblock_size, file_size - offset) buff = lstr + f.read(fblock) bnl = 0 for _inl in range(buff.count("\n")): nnl = buff.find("\n", bnl) line = buff[bnl:nnl] block_name, block_file = line.split() # Handling of the B, B_, and B__ blocks is consistent with # other unrefined blocks level, left, right = get_block_info(block_name) rbindex = get_root_block_id(block_name) rbid = (rbindex[0] * rbdim[1:].prod() + rbindex[1] * rbdim[2:].prod() + rbindex[2]) # There are also blocks at lower level than the # real root blocks. These can be ignored. if level == 0: check_root = get_root_blocks(block_name).prod() if check_root < nroot_blocks: level = -1 if level == -1: grid_id = child_id parent_id = -1 child_id += 1 elif level == 0: grid_id = rbid parent_id = -1 else: grid_id = child_id # Try the last parent_id first if last_pid is not None and is_parent( self.grids[last_pid].block_name, block_name): parent_id = last_pid else: parent_id = self.grids[rbid].get_parent_id(block_name) last_pid = parent_id child_id += 1 my_grid = self.grid( grid_id, self, block_name, filename=os.path.join(self.directory, block_file), ) my_grid.Level = level my_grid._parent_id = parent_id self.grids[grid_id] = my_grid self.grid_levels[grid_id] = level self.grid_left_edge[grid_id] = left self.grid_right_edge[grid_id] = right self.grid_dimensions[grid_id] = self.ds.active_grid_dimensions if level > 0: self.grids[parent_id].add_child(my_grid) bnl = nnl + 1 pbar.update(c) c += 1 lstr = buff[bnl:] offset += fblock f.close() pbar.finish() slope = self.ds.domain_width / self.ds.arr(np.ones(3), "code_length") self.grid_left_edge = self.grid_left_edge * slope + self.ds.domain_left_edge self.grid_right_edge = self.grid_right_edge * slope + self.ds.domain_left_edge