Skip to content

Commit

Permalink
[field,geom] Support changing grid dim order
Browse files Browse the repository at this point in the history
  • Loading branch information
holl- committed Jan 31, 2024
1 parent 62dcd87 commit 0d3a0bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion phi/field/_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ def with_boundary(self, boundary):

def with_bounds(self, bounds: Box):
""" Returns a copy of this field with `bounds` replaced. """
return Field(self._geometry, self._values, self._boundary)
order = list(bounds.vector.item_names)
geometry = self._geometry.vector[order]
geometry[order]
new_shape = self._values.shape.without(order) & self._values.shape.only(order, reorder=True)
values = math.transpose(self._values, new_shape)
return Field(geometry, values, self._boundary)

def with_geometry(self, elements: Geometry):
""" Returns a copy of this field with `elements` replaced. """
Expand Down
5 changes: 4 additions & 1 deletion phi/geom/_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def __getitem__(self, item):
bounds = Box(lower, upper)
gather_dict[dim] = slice(start, stop)
resolution = self._resolution.after_gather(gather_dict)
return UniformGrid(resolution, bounds[{d: s for d, s in item.items() if d != 'vector'}])
bounds = bounds[{d: s for d, s in item.items() if d != 'vector'}]
if 'vector' in item:
bounds = bounds[item['vector']] # resolution[item['vector']] will be done automatically
return UniformGrid(resolution, bounds)

def __pack_dims__(self, dims: Tuple[str, ...], packed_dim: Shape, pos: Optional[int], **kwargs) -> 'Cuboid':
return math.pack_dims(self.center_representation(), dims, packed_dim, pos, **kwargs)
Expand Down

0 comments on commit 0d3a0bb

Please sign in to comment.