Skip to content

Commit

Permalink
Cleaned up some defintions of operators (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed May 13, 2024
1 parent 911579f commit 760e438
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
32 changes: 24 additions & 8 deletions pde/grids/operators/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ def laplace(arr: np.ndarray, out: np.ndarray) -> None:
@CartesianGrid.register_operator("laplace", rank_in=0, rank_out=0)
def make_laplace(
grid: CartesianGrid,
*,
backend: Literal["auto", "numba", "numba-spectral", "scipy"] = "auto",
) -> OperatorType:
"""make a Laplace operator on a Cartesian grid
Expand Down Expand Up @@ -710,6 +711,7 @@ def gradient(arr: np.ndarray, out: np.ndarray) -> None:
@CartesianGrid.register_operator("gradient", rank_in=0, rank_out=1)
def make_gradient(
grid: CartesianGrid,
*,
backend: Literal["auto", "numba", "scipy"] = "auto",
method: Literal["central", "forward", "backward"] = "central",
) -> OperatorType:
Expand Down Expand Up @@ -924,7 +926,7 @@ def gradient_squared(arr: np.ndarray, out: np.ndarray) -> None:


@CartesianGrid.register_operator("gradient_squared", rank_in=0, rank_out=0)
def make_gradient_squared(grid: CartesianGrid, central: bool = True) -> OperatorType:
def make_gradient_squared(grid: CartesianGrid, *, central: bool = True) -> OperatorType:
"""make a gradient operator on a Cartesian grid
Args:
Expand Down Expand Up @@ -1133,6 +1135,7 @@ def divergence(arr: np.ndarray, out: np.ndarray) -> None:
@CartesianGrid.register_operator("divergence", rank_in=1, rank_out=0)
def make_divergence(
grid: CartesianGrid,
*,
backend: Literal["auto", "numba", "scipy"] = "auto",
method: Literal["central", "forward", "backward"] = "central",
) -> OperatorType:
Expand Down Expand Up @@ -1186,6 +1189,7 @@ def _vectorize_operator(
grid: CartesianGrid,
*,
backend: Literal["auto", "numba", "scipy"] = "numba",
**kwargs,
) -> OperatorType:
"""apply an operator to on all dimensions of a vector
Expand All @@ -1201,7 +1205,7 @@ def _vectorize_operator(
A function that can be applied to an array of values
"""
dim = grid.dim
operator = make_operator(grid, backend=backend)
operator = make_operator(grid, backend=backend, **kwargs)

def vectorized_operator(arr: np.ndarray, out: np.ndarray) -> None:
"""apply vector gradient operator to array `arr`"""
Expand All @@ -1216,7 +1220,10 @@ def vectorized_operator(arr: np.ndarray, out: np.ndarray) -> None:

@CartesianGrid.register_operator("vector_gradient", rank_in=1, rank_out=2)
def make_vector_gradient(
grid: CartesianGrid, backend: Literal["auto", "numba", "scipy"] = "numba"
grid: CartesianGrid,
*,
backend: Literal["auto", "numba", "scipy"] = "numba",
method: Literal["central", "forward", "backward"] = "central",
) -> OperatorType:
"""make a vector gradient operator on a Cartesian grid
Expand All @@ -1225,16 +1232,19 @@ def make_vector_gradient(
The grid for which the operator is created
backend (str):
Backend used for calculating the vector gradient operator.
method (str):
The method for calculating the derivative. Possible values are 'central',
'forward', and 'backward'.
Returns:
A function that can be applied to an array of values
"""
return _vectorize_operator(make_gradient, grid, backend=backend)
return _vectorize_operator(make_gradient, grid, backend=backend, method=method)


@CartesianGrid.register_operator("vector_laplace", rank_in=1, rank_out=1)
def make_vector_laplace(
grid: CartesianGrid, backend: Literal["auto", "numba", "scipy"] = "numba"
grid: CartesianGrid, *, backend: Literal["auto", "numba", "scipy"] = "numba"
) -> OperatorType:
"""make a vector Laplacian on a Cartesian grid
Expand All @@ -1252,7 +1262,10 @@ def make_vector_laplace(

@CartesianGrid.register_operator("tensor_divergence", rank_in=2, rank_out=1)
def make_tensor_divergence(
grid: CartesianGrid, backend: Literal["auto", "numba", "scipy"] = "numba"
grid: CartesianGrid,
*,
backend: Literal["auto", "numba", "scipy"] = "numba",
method: Literal["central", "forward", "backward"] = "central",
) -> OperatorType:
"""make a tensor divergence operator on a Cartesian grid
Expand All @@ -1261,16 +1274,19 @@ def make_tensor_divergence(
The grid for which the operator is created
backend (str):
Backend used for calculating the tensor divergence operator.
method (str):
The method for calculating the derivative. Possible values are 'central',
'forward', and 'backward'.
Returns:
A function that can be applied to an array of values
"""
return _vectorize_operator(make_divergence, grid, backend=backend)
return _vectorize_operator(make_divergence, grid, backend=backend, method=method)


@CartesianGrid.register_operator("poisson_solver", rank_in=0, rank_out=0)
def make_poisson_solver(
bcs: Boundaries, method: Literal["auto", "scipy"] = "auto"
bcs: Boundaries, *, method: Literal["auto", "scipy"] = "auto"
) -> OperatorType:
"""make a operator that solves Poisson's equation
Expand Down
2 changes: 1 addition & 1 deletion pde/grids/operators/cylindrical_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def tensor_divergence(arr: np.ndarray, out: np.ndarray) -> None:
@CylindricalSymGrid.register_operator("poisson_solver", rank_in=0, rank_out=0)
@fill_in_docstring
def make_poisson_solver(
bcs: Boundaries, method: Literal["auto", "scipy"] = "auto"
bcs: Boundaries, *, method: Literal["auto", "scipy"] = "auto"
) -> OperatorType:
"""make a operator that solves Poisson's equation
Expand Down
6 changes: 3 additions & 3 deletions pde/grids/operators/polar_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def laplace(arr: np.ndarray, out: np.ndarray) -> None:
@PolarSymGrid.register_operator("gradient", rank_in=0, rank_out=1)
@fill_in_docstring
def make_gradient(
grid: PolarSymGrid, method: Literal["central", "forward", "backward"] = "central"
grid: PolarSymGrid, *, method: Literal["central", "forward", "backward"] = "central"
) -> OperatorType:
"""make a discretized gradient operator for a polar grid
Expand Down Expand Up @@ -107,7 +107,7 @@ def gradient(arr: np.ndarray, out: np.ndarray) -> None:

@PolarSymGrid.register_operator("gradient_squared", rank_in=0, rank_out=0)
@fill_in_docstring
def make_gradient_squared(grid: PolarSymGrid, central: bool = True) -> OperatorType:
def make_gradient_squared(grid: PolarSymGrid, *, central: bool = True) -> OperatorType:
"""make a discretized gradient squared operator for a polar grid
{DESCR_POLAR_GRID}
Expand Down Expand Up @@ -327,7 +327,7 @@ def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
@PolarSymGrid.register_operator("poisson_solver", rank_in=0, rank_out=0)
@fill_in_docstring
def make_poisson_solver(
bcs: Boundaries, method: Literal["auto", "scipy"] = "auto"
bcs: Boundaries, *, method: Literal["auto", "scipy"] = "auto"
) -> OperatorType:
"""make a operator that solves Poisson's equation
Expand Down
15 changes: 10 additions & 5 deletions pde/grids/operators/spherical_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@SphericalSymGrid.register_operator("laplace", rank_in=0, rank_out=0)
@fill_in_docstring
def make_laplace(grid: SphericalSymGrid, conservative: bool = True) -> OperatorType:
def make_laplace(grid: SphericalSymGrid, *, conservative: bool = True) -> OperatorType:
"""make a discretized laplace operator for a spherical grid
{DESCR_SPHERICAL_GRID}
Expand Down Expand Up @@ -89,6 +89,7 @@ def laplace(arr: np.ndarray, out: np.ndarray) -> None:
@fill_in_docstring
def make_gradient(
grid: SphericalSymGrid,
*,
method: Literal["central", "forward", "backward"] = "central",
) -> OperatorType:
"""make a discretized gradient operator for a spherical grid
Expand Down Expand Up @@ -133,7 +134,9 @@ def gradient(arr: np.ndarray, out: np.ndarray) -> None:

@SphericalSymGrid.register_operator("gradient_squared", rank_in=0, rank_out=0)
@fill_in_docstring
def make_gradient_squared(grid: SphericalSymGrid, central: bool = True) -> OperatorType:
def make_gradient_squared(
grid: SphericalSymGrid, *, central: bool = True
) -> OperatorType:
"""make a discretized gradient squared operator for a spherical grid
{DESCR_SPHERICAL_GRID}
Expand Down Expand Up @@ -184,6 +187,7 @@ def gradient_squared(arr: np.ndarray, out: np.ndarray) -> None:
@fill_in_docstring
def make_divergence(
grid: SphericalSymGrid,
*,
safe: bool = True,
conservative: bool = True,
method: Literal["central", "forward", "backward"] = "central",
Expand Down Expand Up @@ -282,6 +286,7 @@ def divergence(arr: np.ndarray, out: np.ndarray) -> None:
@fill_in_docstring
def make_vector_gradient(
grid: SphericalSymGrid,
*,
method: Literal["central", "forward", "backward"] = "central",
safe: bool = True,
) -> OperatorType:
Expand Down Expand Up @@ -358,7 +363,7 @@ def vector_gradient(arr: np.ndarray, out: np.ndarray) -> None:
@SphericalSymGrid.register_operator("tensor_divergence", rank_in=2, rank_out=1)
@fill_in_docstring
def make_tensor_divergence(
grid: SphericalSymGrid, safe: bool = True, conservative: bool = False
grid: SphericalSymGrid, *, safe: bool = True, conservative: bool = False
) -> OperatorType:
"""make a discretized tensor divergence operator for a spherical grid
Expand Down Expand Up @@ -461,7 +466,7 @@ def tensor_divergence(arr: np.ndarray, out: np.ndarray) -> None:
@SphericalSymGrid.register_operator("tensor_double_divergence", rank_in=2, rank_out=0)
@fill_in_docstring
def make_tensor_double_divergence(
grid: SphericalSymGrid, safe: bool = True, conservative: bool = True
grid: SphericalSymGrid, *, safe: bool = True, conservative: bool = True
) -> OperatorType:
"""make a discretized tensor double divergence operator for a spherical grid
Expand Down Expand Up @@ -636,7 +641,7 @@ def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
@SphericalSymGrid.register_operator("poisson_solver", rank_in=0, rank_out=0)
@fill_in_docstring
def make_poisson_solver(
bcs: Boundaries, method: Literal["auto", "scipy"] = "auto"
bcs: Boundaries, *, method: Literal["auto", "scipy"] = "auto"
) -> OperatorType:
"""make a operator that solves Poisson's equation
Expand Down

0 comments on commit 760e438

Please sign in to comment.