Skip to content

Commit 1380e73

Browse files
committed
Add typing generics dirichletbc
1 parent b3072ab commit 1380e73

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

python/dolfinx/fem/bcs.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import numbers
1616
from collections.abc import Callable, Iterable
17+
from typing import Generic, TypeVar
1718

1819
import numpy as np
1920
import numpy.typing as npt
@@ -91,7 +92,10 @@ def locate_dofs_topological(
9192
return _cpp.fem.locate_dofs_topological(_V, entity_dim, _entities, remote)
9293

9394

94-
class DirichletBC:
95+
_T = TypeVar("_T", np.float32, np.float64, np.complex64, np.complex128)
96+
97+
98+
class DirichletBC(Generic[_T]):
9599
"""Representation of Dirichlet boundary conditions.
96100
97101
The conditions are imposed on a linear system.
@@ -120,18 +124,17 @@ class initialiser. This class is combined with different
120124
self._cpp_object = bc
121125

122126
@property
123-
def g(self) -> Function | Constant | np.ndarray:
127+
def g(self) -> Function | Constant:
124128
"""The boundary condition value(s)."""
129+
# TODO: needs to be wrapped into Function or Constant
125130
return self._cpp_object.value
126131

127132
@property
128133
def function_space(self) -> dolfinx.fem.FunctionSpace:
129134
"""Function space on which the boundary condition is defined."""
130135
return self._cpp_object.function_space
131136

132-
def set(
133-
self, x: npt.NDArray, x0: npt.NDArray[np.int32] | None = None, alpha: float = 1
134-
) -> None:
137+
def set(self, x: npt.NDArray[_T], x0: npt.NDArray[_T] | None = None, alpha: float = 1) -> None:
135138
"""Set array entries that are constrained by a Dirichlet condition.
136139
137140
Entries in ``x`` that are constrained by a Dirichlet boundary
@@ -172,10 +175,10 @@ def dof_indices(self) -> tuple[npt.NDArray[np.int32], int]:
172175

173176

174177
def dirichletbc(
175-
value: Function | Constant | np.ndarray,
178+
value: Function | Constant | npt.NDArray[_T],
176179
dofs: npt.NDArray[np.int32],
177180
V: dolfinx.fem.FunctionSpace | None = None,
178-
) -> DirichletBC:
181+
) -> DirichletBC[_T]:
179182
"""Representation of Dirichlet boundary condition.
180183
181184
Args:
@@ -232,8 +235,8 @@ def dirichletbc(
232235

233236

234237
def bcs_by_block(
235-
spaces: Iterable[FunctionSpace | None], bcs: Iterable[DirichletBC]
236-
) -> list[list[DirichletBC]]:
238+
spaces: Iterable[FunctionSpace | None], bcs: Iterable[DirichletBC[_T]]
239+
) -> list[list[DirichletBC[_T]]]:
237240
"""Arrange boundary conditions by the space that they constrain.
238241
239242
Given a sequence of function spaces ``spaces`` and a sequence of

0 commit comments

Comments
 (0)