Skip to content

Commit

Permalink
Add synonym support for axis names in boundary conditions and update …
Browse files Browse the repository at this point in the history
…tests (#645)

* Add synonym support for axis names in boundary conditions and update tests
* Closes Issue #643
  • Loading branch information
david-zwicker authored Jan 28, 2025
1 parent cb73a6f commit de052cf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ venv.bak/
*.code-workspace
examples/output/allen_cahn.avi
examples/output/allen_cahn.hdf
debug
8 changes: 8 additions & 0 deletions pde/grids/boundaries/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ def _parse_from_dict(
bc_data = [[bc_all, bc_all] for _ in range(grid.num_axes)]
bc_seen = [[False, False] for _ in range(grid.num_axes)]

# replace synonymous axes names
for pattern, repl in grid.c._axes_alt_repl.items(): # iterate replacements
for ext in ["", "-", "+"]: # iterate all variants
if pattern + ext in data:
if repl + ext in data:
raise KeyError(f"Key `{repl + ext}` is specified twice")
data[repl + ext] = data.pop(pattern + ext)

# check specific boundary conditions for all axes
for ax, ax_name in enumerate(grid.axes):
# overwrite boundaries whose axes are given
Expand Down
2 changes: 1 addition & 1 deletion pde/grids/boundaries/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def from_data(
if isinstance(data, BCBase):
# already in the correct format
if data.grid._mesh is not None:
# we need to exclude this case since otherwise we get into a rabit hole
# we need to exclude this case since otherwise we get into a rabbit hole
# where it is not clear what grid boundary conditions belong to. The
# idea is that users only create boundary conditions for the full grid
# and that the splitting onto subgrids is only done once, automatically,
Expand Down
2 changes: 1 addition & 1 deletion pde/grids/coordinates/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PolarCoordinates(CoordinatesBase):

dim = 2
axes = ["r", "φ"]
_axes_alt = {"φ": ["phi"]}
_axes_alt = {"r": ["radius"], "φ": ["phi"]}
coordinate_limits = [(0, np.inf), (0, 2 * np.pi)]

_singleton: PolarCoordinates | None = None
Expand Down
2 changes: 1 addition & 1 deletion pde/grids/coordinates/spherical.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SphericalCoordinates(CoordinatesBase):

dim = 3
axes = ["r", "θ", "φ"]
_axes_alt = {"θ": ["theta"], "φ": ["phi"]}
_axes_alt = {"r": ["radius"], "θ": ["theta"], "φ": ["phi"]}
coordinate_limits = [(0, np.inf), (0, np.pi), (0, 2 * np.pi)]
major_axis = 0

Expand Down
24 changes: 23 additions & 1 deletion tests/grids/boundaries/test_axes_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import pytest

from pde import ScalarField, UnitGrid
from pde import PolarSymGrid, ScalarField, UnitGrid
from pde.grids.base import PeriodicityError
from pde.grids.boundaries.axes import (
BCDataError,
Expand Down Expand Up @@ -255,3 +255,25 @@ def setter(data, args=None):
mask = np.ones((6, 6), dtype=bool)
mask[0, 0] = mask[-1, 0] = mask[0, -1] = mask[-1, -1] = False
np.testing.assert_allclose(f1._data_full[mask], f2._data_full[mask])


def test_boundaries_axis_synonyms():
"""Test whether we can use synonyms of the axis to set boundaries."""
grid = PolarSymGrid([1, 2], 3)
expect = grid.get_boundary_conditions({"r": "value"})

gbc = grid.get_boundary_conditions
assert expect == gbc({"radius": "value"})
assert expect == gbc({"r-": "value", "r+": "value"})
assert expect == gbc({"radius-": "value", "radius+": "value"})
assert expect == gbc({"r-": "value", "radius+": "value"})
assert expect == gbc({"radius-": "value", "r+": "value"})

with pytest.raises(KeyError):
gbc({"radius": "value", "r": "value"})
with pytest.raises(KeyError):
gbc({"r": "value", "radius": "value"})
with pytest.raises(KeyError):
gbc({"radius+": "value", "r+": "value"})
with pytest.raises(KeyError):
gbc({"r-": "value", "radius-": "value"})

0 comments on commit de052cf

Please sign in to comment.