Skip to content

Commit 56227fd

Browse files
Fixed a bug in vector field conversion (#532)
1 parent 839f39e commit 56227fd

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

pde/fields/vectorial.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@ def from_expression(
147147

148148
def __getitem__(self, key: int | str) -> ScalarField:
149149
"""extract a component of the VectorField"""
150+
axis = self.grid.get_axis_index(key)
151+
comp_name = self.grid.c.axes[axis]
152+
if self.label:
153+
label = self.label + f"_{comp_name}"
154+
else:
155+
label = f"{comp_name} component"
150156
return ScalarField(
151-
self.grid,
152-
data=self._data_full[self.grid.get_axis_index(key)],
153-
with_ghost_cells=True,
157+
self.grid, data=self._data_full[axis], label=label, with_ghost_cells=True
154158
)
155159

156160
def __setitem__(self, key: int | str, value: NumberOrArray | ScalarField):
@@ -590,14 +594,12 @@ def interpolate_to_grid(
590594
# determine the points at which data needs to be calculated
591595
if isinstance(grid, CartesianGrid):
592596
# convert Cartesian coordinates to coordinates in current grid
593-
points = self.grid.transform(
594-
grid.cell_coords, "cartesian", "grid", full=True
595-
)
596-
# interpolate the data to the grid; this gives the vector in the grid basis
597+
points = self.grid.c.pos_from_cart(grid.cell_coords)
597598
points_grid_sym = self.grid._coords_symmetric(points)
599+
# interpolate the data to the grid; this gives the vector in the grid basis
598600
data_grid = self.interpolate(points_grid_sym, bc=bc, fill=fill)
599601
# convert the vector to the cartesian basis
600-
data = self.grid._vector_to_cartesian(points, data_grid, coords="grid")
602+
data = self.grid._vector_to_cartesian(points, data_grid)
601603

602604
elif (
603605
self.grid.__class__ is grid.__class__

pde/grids/base.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,18 +722,16 @@ def point_from_cartesian(
722722
return self._coords_symmetric(points_sph)
723723

724724
def _vector_to_cartesian(
725-
self, points: ArrayLike, components: ArrayLike, *, coords: CoordsType = "grid"
725+
self, points: ArrayLike, components: ArrayLike
726726
) -> np.ndarray:
727727
"""convert the vectors at given points into a Cartesian basis
728728
729729
Args:
730730
points (:class:`~numpy.ndarray`):
731-
The coordinates of the point(s) where the vectors are specified.
731+
The coordinates of the point(s) where the vectors are specified. These
732+
need to be given in grid coordinates.
732733
components (:class:`~numpy.ndarray`):
733734
The components of the vectors at the given points
734-
coords (str):
735-
The coordinate system in which the point is specified. Valid values are
736-
`cartesian`, `cell`, and `grid`; see :meth:`~pde.grids.base.GridBase.transform`.
737735
738736
Returns:
739737
The vectors specified at the same position but with components given in
@@ -1118,12 +1116,13 @@ def get_vector_data(self, data: np.ndarray, **kwargs) -> dict[str, Any]:
11181116
img_coord0 = self.get_image_data(data[0], **kwargs)
11191117
img_coord1 = self.get_image_data(data[1], **kwargs)
11201118

1119+
points_cart = np.stack((img_coord0["xs"], img_coord0["ys"]), axis=-1)
1120+
points = self.c._pos_from_cart(points_cart)
1121+
11211122
# convert vectors to cartesian coordinates
11221123
img_data = img_coord0
11231124
img_data["data_x"], img_data["data_y"] = self._vector_to_cartesian(
1124-
np.stack((img_coord0["xs"], img_coord0["ys"]), axis=-1),
1125-
[img_coord0["data"], img_coord1["data"]],
1126-
coords="cartesian",
1125+
points, [img_coord0["data"], img_coord1["data"]]
11271126
)
11281127
img_data.pop("data")
11291128
return img_data

0 commit comments

Comments
 (0)