Skip to content

Commit

Permalink
Allow multiple parents for grid tree
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturk committed Jun 23, 2023
1 parent 075db4b commit bd55726
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
10 changes: 6 additions & 4 deletions yt/data_objects/index_subobjects/grid_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List

import numpy as np
from more_itertools import always_iterable

import yt.geometry.particle_deposit as particle_deposit
from yt._typing import FieldKey
Expand Down Expand Up @@ -127,10 +128,11 @@ def _setup_dx(self):
id = self.id - self._id_offset
ds = self.ds
index = self.index
if self.Parent is not None:
if not hasattr(self.Parent, "dds"):
self.Parent._setup_dx()
self.dds = self.Parent.dds.d / self.ds.refine_by
parents = list(always_iterable(self.Parent))
if len(parents) > 0:
if not hasattr(parents[0], "dds"):
parents[0]._setup_dx()
self.dds = parents[0].dds.d / self.ds.refine_by
else:
LE, RE = (index.grid_left_edge[id, :].d, index.grid_right_edge[id, :].d)
self.dds = (RE - LE) / self.ActiveDimensions
Expand Down
25 changes: 13 additions & 12 deletions yt/geometry/grid_container.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ cdef class GridTree:
np.ndarray[np.float64_t, ndim=2] left_edge,
np.ndarray[np.float64_t, ndim=2] right_edge,
np.ndarray[np.int32_t, ndim=2] dimensions,
np.ndarray[np.int64_t, ndim=1] parent_ind,
list parent_ind, # Not an array anymore!
np.ndarray[np.int64_t, ndim=1] level,
np.ndarray[np.int64_t, ndim=1] num_children,
refine_by = 2):
int refine_by = 2,
int min_level = 0):

cdef int i, j, k
cdef np.ndarray[np.int_t, ndim=1] child_ptr
Expand Down Expand Up @@ -87,7 +88,7 @@ cdef class GridTree:
dimensions[i,:],
num_children[i],
level[i], i)
if level[i] == 0:
if level[i] == min_level:
self.num_root_grids += 1
if num_children[i] == 0:
self.num_leaf_grids += 1
Expand All @@ -96,15 +97,15 @@ cdef class GridTree:
sizeof(GridTreeNode) * self.num_root_grids)
k = 0
for i in range(num_grids):
j = parent_ind[i]
if j >= 0:
self.grids[j].children[child_ptr[j]] = &self.grids[i]
child_ptr[j] += 1
else:
if k >= self.num_root_grids:
raise RuntimeError
self.root_grids[k] = self.grids[i]
k = k + 1
for j in parent_ind[i]:
if j >= 0:
self.grids[j].children[child_ptr[j]] = &self.grids[i]
child_ptr[j] += 1
else:
if k >= self.num_root_grids:
raise RuntimeError(k, self.num_root_grids)
self.root_grids[k] = self.grids[i]
k = k + 1

def __iter__(self):
yield self
Expand Down
15 changes: 11 additions & 4 deletions yt/geometry/grid_geometry_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional, Tuple

import numpy as np
from more_itertools import always_iterable

from yt.arraytypes import blankRecordArray
from yt.config import ytcfg
Expand Down Expand Up @@ -297,20 +298,25 @@ def grid_tree(self):
left_edge = self.ds.arr(np.zeros((self.num_grids, 3)), "code_length")
right_edge = self.ds.arr(np.zeros((self.num_grids, 3)), "code_length")
level = np.zeros((self.num_grids), dtype="int64")
parent_ind = np.zeros((self.num_grids), dtype="int64")
parent_ind = []
num_children = np.zeros((self.num_grids), dtype="int64")
dimensions = np.zeros((self.num_grids, 3), dtype="int32")
# Some things become slightly more complicated because in some
# frontends, the parent relationship can be 1:N. So we need to
# construct a list of lists.

for i, grid in enumerate(self.grids):
left_edge[i, :] = grid.LeftEdge
right_edge[i, :] = grid.RightEdge
level[i] = grid.Level
if grid.Parent is None:
parent_ind[i] = -1
parents = list(always_iterable(grid.Parent))
if len(parents) == 0 or parents[0] is None:
parent_ind.append([-1])
else:
parent_ind[i] = grid.Parent.id - grid.Parent._id_offset
parent_ind.append([_.id - _._id_offset for _ in parents])
num_children[i] = np.int64(len(grid.Children))
dimensions[i, :] = grid.ActiveDimensions
# Now we need to convert the list of parents into an array

return GridTree(
self.num_grids,
Expand All @@ -321,6 +327,7 @@ def grid_tree(self):
level,
num_children,
self.ds.refine_by,
self.min_level,
)

def convert(self, unit):
Expand Down

0 comments on commit bd55726

Please sign in to comment.