Skip to content

Commit

Permalink
Re-introduce type hints from typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgensd committed Apr 25, 2024
1 parent bc312f5 commit 20f4286
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/adios4dolfinx/adios2_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from contextlib import contextmanager
from pathlib import Path
from typing import NamedTuple
from typing import NamedTuple, Union

from mpi4py import MPI

Expand Down Expand Up @@ -44,7 +44,7 @@ class AdiosFile(NamedTuple):
@contextmanager
def ADIOSFile(
adios: adios2.ADIOS,
filename: Path | str,
filename: Union[Path, str],
engine: str,
mode: adios2.Mode,
io_name: str,
Expand All @@ -62,7 +62,7 @@ def ADIOSFile(
def read_cell_perms(
adios: adios2.ADIOS,
comm: MPI.Intracomm,
filename: Path | str,
filename: Union[Path, str],
variable: str,
num_cells_global: np.int64,
engine: str,
Expand Down Expand Up @@ -126,12 +126,12 @@ def read_cell_perms(
def read_adjacency_list(
adios: adios2.ADIOS,
comm: MPI.Intracomm,
filename: Path | str,
filename: Union[Path, str],
dofmap: str,
dofmap_offsets: str,
num_cells_global: np.int64,
engine: str,
) -> dolfinx.cpp.graph.AdjacencyList_int64 | dolfinx.cpp.graph.AdjacencyList_int32:
) -> Union[dolfinx.cpp.graph.AdjacencyList_int64, dolfinx.cpp.graph.AdjacencyList_int32]:
"""
Read an adjacency-list from an ADIOS file with given communicator.
The adjancency list is split in to a flat array (data) and its corresponding offset.
Expand Down Expand Up @@ -205,7 +205,7 @@ def read_adjacency_list(

def read_array(
adios: adios2.ADIOS,
filename: Path | str,
filename: Union[Path, str],
array_name: str,
engine: str,
comm: MPI.Intracomm,
Expand Down
22 changes: 11 additions & 11 deletions src/adios4dolfinx/checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@


def write_attributes(
filename: Path | str,
filename: typing.Union[Path, str],
comm: MPI.Intracomm,
name: str,
attributes: dict[str, np.ndarray],
Expand Down Expand Up @@ -91,7 +91,7 @@ def write_attributes(


def read_attributes(
filename: Path | str,
filename: typing.Union[Path, str],
comm: MPI.Intracomm,
name: str,
engine: str = "BP4",
Expand Down Expand Up @@ -125,11 +125,11 @@ def read_attributes(


def write_meshtags(
filename: Path | str,
filename: typing.Union[Path, str],
mesh: dolfinx.mesh.Mesh,
meshtags: dolfinx.mesh.MeshTags,
engine: str = "BP4",
meshtag_name: str | None = None,
meshtag_name: typing.Optional[str] = None,
):
"""
Write meshtags associated with input mesh to file.
Expand Down Expand Up @@ -204,7 +204,7 @@ def write_meshtags(


def read_meshtags(
filename: Path | str,
filename: typing.Union[Path, str],
mesh: dolfinx.mesh.Mesh,
meshtag_name: str,
engine: str = "BP4",
Expand Down Expand Up @@ -301,12 +301,12 @@ def read_meshtags(


def read_function(
filename: Path | str,
filename: typing.Union[Path, str],
u: dolfinx.fem.Function,
engine: str = "BP4",
time: float = 0.0,
legacy: bool = False,
name: str | None = None,
name: typing.Optional[str] = None,
):
"""
Read checkpoint from file and fill it into `u`.
Expand Down Expand Up @@ -437,7 +437,7 @@ def read_function(


def read_mesh_data(
filename: Path | str,
filename: typing.Union[Path, str],
comm: MPI.Intracomm,
engine: str = "BP4",
ghost_mode: dolfinx.mesh.GhostMode = dolfinx.mesh.GhostMode.shared_facet,
Expand Down Expand Up @@ -573,7 +573,7 @@ def partitioner(comm: MPI.Intracomm, n, m, topo):


def read_mesh(
filename: Path | str,
filename: typing.Union[Path, str],
comm: MPI.Intracomm,
engine: str = "BP4",
ghost_mode: dolfinx.mesh.GhostMode = dolfinx.mesh.GhostMode.shared_facet,
Expand Down Expand Up @@ -709,12 +709,12 @@ def write_mesh(


def write_function(
filename: Path | str,
filename: typing.Union[Path, str],
u: dolfinx.fem.Function,
engine: str = "BP4",
mode: adios2.Mode = adios2.Mode.Append,
time: float = 0.0,
name: str | None = None,
name: typing.Optional[str] = None,
):
"""
Write function checkpoint to file.
Expand Down
3 changes: 2 additions & 1 deletion src/adios4dolfinx/legacy_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import pathlib
import typing

from mpi4py import MPI

Expand Down Expand Up @@ -329,7 +330,7 @@ def read_function_from_legacy_h5(
comm: MPI.Intracomm,
u: dolfinx.fem.Function,
group: str = "mesh",
step: int | None = None,
step: typing.Optional[int] = None,
):
"""
Read function from a `h5`-file generated by legacy DOLFIN `HDF5File.write`
Expand Down
11 changes: 7 additions & 4 deletions src/adios4dolfinx/original_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import typing
from pathlib import Path

from mpi4py import MPI
Expand Down Expand Up @@ -199,7 +200,7 @@ def create_original_mesh_data(mesh: dolfinx.mesh.Mesh) -> MeshData:


def create_function_data_on_original_mesh(
u: dolfinx.fem.Function, name: str | None = None
u: dolfinx.fem.Function, name: typing.Optional[str] = None
) -> FunctionData:
"""
Create data object to save with ADIOS2
Expand Down Expand Up @@ -327,12 +328,12 @@ def create_function_data_on_original_mesh(


def write_function_on_input_mesh(
filename: Path | str,
filename: typing.Union[Path, str],
u: dolfinx.fem.Function,
engine: str = "BP4",
mode: adios2.Mode = adios2.Mode.Append,
time: float = 0.0,
name: str | None = None,
name: typing.Optional[str] = None,
):
"""
Write function checkpoint (to be read with the input mesh).
Expand All @@ -359,7 +360,9 @@ def write_function_on_input_mesh(
)


def write_mesh_input_order(filename: Path | str, mesh: dolfinx.mesh.Mesh, engine: str = "BP4"):
def write_mesh_input_order(
filename: typing.Union[Path, str], mesh: dolfinx.mesh.Mesh, engine: str = "BP4"
):
"""
Write mesh to checkpoint file in original input ordering
"""
Expand Down
13 changes: 8 additions & 5 deletions src/adios4dolfinx/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import typing
from dataclasses import dataclass

import numpy as np
Expand Down Expand Up @@ -33,11 +34,13 @@ class MeshData:

# Partitioning_information
store_partition: bool
partition_processes: int | None # Number of processes in partition
ownership_array: npt.NDArray[np.int32] | None # Ownership array for cells
ownership_offset: npt.NDArray[np.int32] | None # Ownership offset for cells
partition_range: tuple[int, int] | None # Local insert position for partitioning information
partition_global: int | None
partition_processes: typing.Optional[int] # Number of processes in partition
ownership_array: typing.Optional[npt.NDArray[np.int32]] # Ownership array for cells
ownership_offset: typing.Optional[npt.NDArray[np.int32]] # Ownership offset for cells
partition_range: typing.Optional[
tuple[int, int]
] # Local insert position for partitioning information
partition_global: typing.Optional[int]


@dataclass
Expand Down
6 changes: 4 additions & 2 deletions src/adios4dolfinx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from __future__ import annotations

import typing

from mpi4py import MPI

import dolfinx
Expand All @@ -25,8 +27,8 @@
"unroll_insert_position",
]

valid_function_types = np.float32 | np.float64 | np.complex64 | np.complex128
valid_real_types = np.float32 | np.float64
valid_function_types = typing.Union[np.float32, np.float64, np.complex64, np.complex128]
valid_real_types = typing.Union[np.float32, np.float64]


def compute_insert_position(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_meshtags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import itertools
import typing
from collections import ChainMap

from mpi4py import MPI
Expand Down Expand Up @@ -67,7 +68,7 @@ def generate_reference_map(
meshtag: dolfinx.mesh.MeshTags,
comm: MPI.Intracomm,
root: int,
) -> None | dict[str, tuple[int, npt.NDArray]]:
) -> typing.Optional[dict[str, tuple[int, npt.NDArray]]]:
"""
Helper function to generate map from meshtag value to its corresponding index and midpoint.
Expand Down

0 comments on commit 20f4286

Please sign in to comment.