Example #1
0
def test_is_parent():
    rs = np.random.RandomState(45652)
    for dim in [1, 2, 3]:
        for i in range(6):
            max_n = 2**i

            descriptors = []
            for j in range(dim):
                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)
                descriptors.append("%s:%s" % (b1[1:], b2[1:]))
            block = "B" + "_".join(descriptors)
            # since b2 is computed with max_n=32 in the for-loop, block always
            # has a refined great-great-grandparent
            parent = "B" + "_".join([elem[:-1] for elem in descriptors])
            grandparent = "B" + "_".join([elem[:-2] for elem in descriptors])

            assert is_parent(parent, block)
            assert is_parent(grandparent, parent)
            assert not is_parent(grandparent, block)
            assert not is_parent(block, parent)
            assert not is_parent(flip_random_block_bit(parent, rs), block)
Example #2
0
    def _parse_index(self):
        self.grids = np.empty(self.num_grids, dtype='object')

        pbar = get_pbar("Parsing Hierarchy", self.num_grids)
        f = open(self.ds.parameter_filename, "r")
        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(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