-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added from __future__ import annotations to all files
- Loading branch information
1 parent
9c21af6
commit 5355226
Showing
26 changed files
with
111 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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: | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.. codeauthor:: David Zwicker <[email protected]> | ||
""" | ||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.. codeauthor:: David Zwicker <[email protected]> | ||
""" | ||
from __future__ import annotations | ||
|
||
from ..fields import ScalarField | ||
from ..grids.base import GridBase | ||
|
@@ -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: | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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")} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Oops, something went wrong.