Skip to content

Commit

Permalink
Added from __future__ import annotations to all files
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed Jan 3, 2024
1 parent 9c21af6 commit 5355226
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 89 deletions.
11 changes: 6 additions & 5 deletions pde/grids/operators/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable, Literal, Tuple
from typing import Callable, Literal

import numba as nb
import numpy as np
Expand All @@ -35,7 +36,7 @@
# deprecated since 2023-12-06


def _get_laplace_matrix_1d(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix_1d(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for Laplace operator on a 1d Cartesian grid
Args:
Expand Down Expand Up @@ -77,7 +78,7 @@ def _get_laplace_matrix_1d(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
return matrix, vector


def _get_laplace_matrix_2d(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix_2d(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for Laplace operator on a 2d Cartesian grid
Args:
Expand Down Expand Up @@ -146,7 +147,7 @@ def i(x, y):
return matrix, vector


def _get_laplace_matrix_3d(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix_3d(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for Laplace operator on a 3d Cartesian grid
Args:
Expand Down Expand Up @@ -233,7 +234,7 @@ def i(x, y, z):
return matrix, vector


def _get_laplace_matrix(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for Laplace operator on a Cartesian grid
Args:
Expand Down
7 changes: 4 additions & 3 deletions pde/grids/operators/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

import logging
import warnings
from typing import Callable, Literal, Optional
from typing import Callable, Literal

import numpy as np

Expand Down Expand Up @@ -198,7 +199,7 @@ def uniform_discretization(grid: GridBase) -> float:

def make_laplace_from_matrix(
matrix, vector
) -> Callable[[np.ndarray, Optional[np.ndarray]], np.ndarray]:
) -> Callable[[np.ndarray, np.ndarray | None], np.ndarray]:
"""make a Laplace operator using matrix vector products
Args:
Expand All @@ -214,7 +215,7 @@ def make_laplace_from_matrix(
mat = matrix.tocsc()
vec = vector.toarray()[:, 0]

def laplace(arr: np.ndarray, out: Optional[np.ndarray] = None) -> np.ndarray:
def laplace(arr: np.ndarray, out: np.ndarray | None = None) -> np.ndarray:
"""apply the laplace operator to `arr`"""
result = mat.dot(arr.flat) + vec
if out is None:
Expand Down
5 changes: 3 additions & 2 deletions pde/grids/operators/cylindrical_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Literal, Tuple
from typing import Literal

import numba as nb
import numpy as np
Expand All @@ -29,7 +30,7 @@
from .common import make_general_poisson_solver


def _get_laplace_matrix(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for Laplace operator on a cylindrical grid
Args:
Expand Down
6 changes: 4 additions & 2 deletions pde/grids/operators/polar_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""

from typing import Literal, Tuple
from __future__ import annotations

from typing import Literal

import numpy as np

Expand Down Expand Up @@ -251,7 +253,7 @@ def tensor_divergence(arr: np.ndarray, out: np.ndarray) -> None:


@fill_in_docstring
def _get_laplace_matrix(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for laplace operator on a polar grid
Args:
Expand Down
5 changes: 3 additions & 2 deletions pde/grids/operators/spherical_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Literal, Tuple
from typing import Literal

import numpy as np

Expand Down Expand Up @@ -523,7 +524,7 @@ def tensor_double_divergence(arr: np.ndarray, out: np.ndarray) -> None:


@fill_in_docstring
def _get_laplace_matrix(bcs: Boundaries) -> Tuple[np.ndarray, np.ndarray]:
def _get_laplace_matrix(bcs: Boundaries) -> tuple[np.ndarray, np.ndarray]:
"""get sparse matrix for laplace operator on a polar grid
Args:
Expand Down
3 changes: 2 additions & 1 deletion pde/pdes/allen_cahn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable # @UnusedImport
from typing import Callable

import numba as nb
import numpy as np
Expand Down
1 change: 1 addition & 0 deletions pde/pdes/cahn_hilliard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable

Expand Down
5 changes: 3 additions & 2 deletions pde/pdes/diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable, Optional
from typing import Callable

import numba as nb
import numpy as np
Expand Down Expand Up @@ -36,7 +37,7 @@ def __init__(
*,
bc: BoundariesData = "auto_periodic_neumann",
noise: float = 0,
rng: Optional[np.random.Generator] = None,
rng: np.random.Generator | None = None,
):
"""
Args:
Expand Down
5 changes: 3 additions & 2 deletions pde/pdes/kpz_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable, Optional
from typing import Callable

import numba as nb
import numpy as np
Expand Down Expand Up @@ -41,7 +42,7 @@ def __init__(
*,
bc: BoundariesData = "auto_periodic_neumann",
noise: float = 0,
rng: Optional[np.random.Generator] = None,
rng: np.random.Generator | None = None,
):
r"""
Args:
Expand Down
7 changes: 4 additions & 3 deletions pde/pdes/kuramoto_sivashinsky.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable, Optional
from typing import Callable

import numba as nb
import numpy as np
Expand Down Expand Up @@ -38,9 +39,9 @@ def __init__(
nu: float = 1,
*,
bc: BoundariesData = "auto_periodic_neumann",
bc_lap: Optional[BoundariesData] = None,
bc_lap: BoundariesData | None = None,
noise: float = 0,
rng: Optional[np.random.Generator] = None,
rng: np.random.Generator | None = None,
):
r"""
Args:
Expand Down
5 changes: 3 additions & 2 deletions pde/pdes/laplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from ..fields import ScalarField
from ..grids.base import GridBase
Expand All @@ -13,7 +14,7 @@
@fill_in_docstring
def solve_poisson_equation(
rhs: ScalarField,
bc: "BoundariesData",
bc: BoundariesData,
label: str = "Solution to Poisson's equation",
**kwargs,
) -> ScalarField:
Expand Down Expand Up @@ -80,7 +81,7 @@ def solve_poisson_equation(

@fill_in_docstring
def solve_laplace_equation(
grid: GridBase, bc: "BoundariesData", label: str = "Solution to Laplace's equation"
grid: GridBase, bc: BoundariesData, label: str = "Solution to Laplace's equation"
) -> ScalarField:
"""Solve Laplace's equation on a given grid.
Expand Down
5 changes: 3 additions & 2 deletions pde/pdes/swift_hohenberg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable, Optional
from typing import Callable

import numba as nb
import numpy as np
Expand Down Expand Up @@ -40,7 +41,7 @@ def __init__(
delta: float = 1.0,
*,
bc: BoundariesData = "auto_periodic_neumann",
bc_lap: Optional[BoundariesData] = None,
bc_lap: BoundariesData | None = None,
):
r"""
Args:
Expand Down
7 changes: 4 additions & 3 deletions pde/pdes/wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

from typing import Callable, Dict, Optional
from typing import Callable

import numba as nb
import numpy as np
Expand Down Expand Up @@ -49,7 +50,7 @@ def __init__(self, speed: float = 1, bc: BoundariesData = "auto_periodic_neumann
self.speed = speed
self.bc = bc

def get_initial_condition(self, u: ScalarField, v: Optional[ScalarField] = None):
def get_initial_condition(self, u: ScalarField, v: ScalarField | None = None):
"""create a suitable initial condition
Args:
Expand All @@ -68,7 +69,7 @@ def get_initial_condition(self, u: ScalarField, v: Optional[ScalarField] = None)
return FieldCollection([u, v], labels=["u", "v"])

@property
def expressions(self) -> Dict[str, str]:
def expressions(self) -> dict[str, str]:
"""dict: the expressions of the right hand side of this PDE"""
return {"u": "v", "v": expr_prod(self.speed**2, "∇²u")}

Expand Down
23 changes: 11 additions & 12 deletions pde/solvers/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
.. codeauthor:: David Zwicker <[email protected]>
"""
from __future__ import annotations

import datetime
import logging
import time
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, TypeVar, Union
from typing import TYPE_CHECKING, Any, Callable, Tuple, TypeVar, Union

from .. import __version__
from ..tools import mpi
Expand All @@ -34,7 +35,7 @@ class Controller:
care of trackers that analyze and modify the state periodically.
"""

diagnostics: Dict[str, Any]
diagnostics: dict[str, Any]
"""dict: diagnostic information (available after simulation finished)"""

_get_current_time: Callable = time.process_time
Expand Down Expand Up @@ -74,15 +75,15 @@ def __init__(
self.trackers = TrackerCollection.from_data(tracker)

# initialize some diagnostic information
self.info: Dict[str, Any] = {}
self.info: dict[str, Any] = {}
self.diagnostics = {
"controller": self.info,
"package_version": __version__,
}
self._logger = logging.getLogger(self.__class__.__name__)

@property
def t_range(self) -> Tuple[float, float]:
def t_range(self) -> tuple[float, float]:
"""tuple: start and end time of the simulation"""
return self._t_range

Expand All @@ -98,7 +99,7 @@ def t_range(self, value: TRangeType):
"""
# determine time range
try:
self._t_range: Tuple[float, float] = (0, float(value)) # type: ignore
self._t_range: tuple[float, float] = (0, float(value)) # type: ignore
except TypeError: # assume a single number was given
if len(value) == 2: # type: ignore
self._t_range = tuple(value) # type: ignore
Expand All @@ -107,10 +108,10 @@ def t_range(self, value: TRangeType):
"t_range must be set to a single number or a tuple of two numbers"
)

def _get_stop_handler(self) -> Callable[[Exception, float], Tuple[int, str]]:
def _get_stop_handler(self) -> Callable[[Exception, float], tuple[int, str]]:
"""return function that handles messaging"""

def _handle_stop_iteration(err: Exception, t: float) -> Tuple[int, str]:
def _handle_stop_iteration(err: Exception, t: float) -> tuple[int, str]:
"""helper function for handling interrupts raised by trackers"""
if isinstance(err, FinishedSimulation):
# tracker determined that the simulation finished
Expand Down Expand Up @@ -138,7 +139,7 @@ def _handle_stop_iteration(err: Exception, t: float) -> Tuple[int, str]:

return _handle_stop_iteration

def _run_single(self, state: TState, dt: Optional[float] = None) -> None:
def _run_single(self, state: TState, dt: float | None = None) -> None:
"""run the simulation
Diagnostic information about the solver procedure are available in the
Expand Down Expand Up @@ -261,7 +262,7 @@ def _run_single(self, state: TState, dt: Optional[float] = None) -> None:
"Consider reducing time step."
)

def _run_mpi_client(self, state: TState, dt: Optional[float] = None) -> None:
def _run_mpi_client(self, state: TState, dt: float | None = None) -> None:
"""loop for run the simulation on client nodes during an MPI run
This function just loops the stepper advancing the sub field of the current node
Expand Down Expand Up @@ -292,9 +293,7 @@ def _run_mpi_client(self, state: TState, dt: Optional[float] = None) -> None:
while t < t_end:
t = stepper(state, t, t_end)

def run(
self, initial_state: TState, dt: Optional[float] = None
) -> Optional[TState]:
def run(self, initial_state: TState, dt: float | None = None) -> TState | None:
"""run the simulation
Diagnostic information about the solver are available in the
Expand Down
Loading

0 comments on commit 5355226

Please sign in to comment.