Skip to content

Commit

Permalink
Fixed a bug in vector field conversion (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker authored Jan 30, 2024
1 parent 839f39e commit 56227fd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
18 changes: 10 additions & 8 deletions pde/fields/vectorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ def from_expression(

def __getitem__(self, key: int | str) -> ScalarField:
"""extract a component of the VectorField"""
axis = self.grid.get_axis_index(key)
comp_name = self.grid.c.axes[axis]
if self.label:
label = self.label + f"_{comp_name}"
else:
label = f"{comp_name} component"
return ScalarField(
self.grid,
data=self._data_full[self.grid.get_axis_index(key)],
with_ghost_cells=True,
self.grid, data=self._data_full[axis], label=label, with_ghost_cells=True
)

def __setitem__(self, key: int | str, value: NumberOrArray | ScalarField):
Expand Down Expand Up @@ -590,14 +594,12 @@ def interpolate_to_grid(
# determine the points at which data needs to be calculated
if isinstance(grid, CartesianGrid):
# convert Cartesian coordinates to coordinates in current grid
points = self.grid.transform(
grid.cell_coords, "cartesian", "grid", full=True
)
# interpolate the data to the grid; this gives the vector in the grid basis
points = self.grid.c.pos_from_cart(grid.cell_coords)
points_grid_sym = self.grid._coords_symmetric(points)
# interpolate the data to the grid; this gives the vector in the grid basis
data_grid = self.interpolate(points_grid_sym, bc=bc, fill=fill)
# convert the vector to the cartesian basis
data = self.grid._vector_to_cartesian(points, data_grid, coords="grid")
data = self.grid._vector_to_cartesian(points, data_grid)

elif (
self.grid.__class__ is grid.__class__
Expand Down
15 changes: 7 additions & 8 deletions pde/grids/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,18 +722,16 @@ def point_from_cartesian(
return self._coords_symmetric(points_sph)

def _vector_to_cartesian(
self, points: ArrayLike, components: ArrayLike, *, coords: CoordsType = "grid"
self, points: ArrayLike, components: ArrayLike
) -> np.ndarray:
"""convert the vectors at given points into a Cartesian basis
Args:
points (:class:`~numpy.ndarray`):
The coordinates of the point(s) where the vectors are specified.
The coordinates of the point(s) where the vectors are specified. These
need to be given in grid coordinates.
components (:class:`~numpy.ndarray`):
The components of the vectors at the given points
coords (str):
The coordinate system in which the point is specified. Valid values are
`cartesian`, `cell`, and `grid`; see :meth:`~pde.grids.base.GridBase.transform`.
Returns:
The vectors specified at the same position but with components given in
Expand Down Expand Up @@ -1118,12 +1116,13 @@ def get_vector_data(self, data: np.ndarray, **kwargs) -> dict[str, Any]:
img_coord0 = self.get_image_data(data[0], **kwargs)
img_coord1 = self.get_image_data(data[1], **kwargs)

points_cart = np.stack((img_coord0["xs"], img_coord0["ys"]), axis=-1)
points = self.c._pos_from_cart(points_cart)

# convert vectors to cartesian coordinates
img_data = img_coord0
img_data["data_x"], img_data["data_y"] = self._vector_to_cartesian(
np.stack((img_coord0["xs"], img_coord0["ys"]), axis=-1),
[img_coord0["data"], img_coord1["data"]],
coords="cartesian",
points, [img_coord0["data"], img_coord1["data"]]
)
img_data.pop("data")
return img_data
Expand Down

0 comments on commit 56227fd

Please sign in to comment.