-
Notifications
You must be signed in to change notification settings - Fork 894
Add typing and type annotations for icbc #1453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
7d82528
25cd004
68a85fa
74cee5b
45e8ac9
9f4c722
a491700
3eacc22
f8c2d99
19fdaed
f81f4be
2ef9831
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,27 +14,37 @@ | |||||
| import numbers | ||||||
| from abc import ABC, abstractmethod | ||||||
| from functools import wraps | ||||||
| from typing import Any, Callable, List, Optional, overload, Tuple, Union | ||||||
|
|
||||||
| import numpy as np | ||||||
| from numpy.typing import NDArray, ArrayLike | ||||||
|
|
||||||
| from .. import backend as bkd | ||||||
| from .. import config | ||||||
| from .. import data | ||||||
| from .. import gradients as grad | ||||||
| from .. import utils | ||||||
| from ..backend import backend_name | ||||||
| from ..geometry import Geometry | ||||||
| from ..types import Tensor, TensorOrTensors | ||||||
|
|
||||||
|
|
||||||
| class BC(ABC): | ||||||
| """Boundary condition base class. | ||||||
|
|
||||||
| Args: | ||||||
| geom: A ``deepxde.geometry.Geometry`` instance. | ||||||
| on_boundary: A function: (x, Geometry.on_boundary(x)) -> True/False. | ||||||
| component: The output component satisfying this BC. | ||||||
| on_boundary: A function: `(x, Geometry.on_boundary(x))` -> True/False. | ||||||
| component: The output component satisfying this BC, should be provided | ||||||
| if ``BC.error`` involves derivatives and the output has multiple components. | ||||||
| """ | ||||||
|
|
||||||
| def __init__(self, geom, on_boundary, component): | ||||||
| def __init__( | ||||||
| self, | ||||||
| geom: Geometry, | ||||||
| on_boundary: Callable[[NDArray[Any], NDArray[Any]], NDArray[np.bool_]], | ||||||
| component: Union[List[int], int], | ||||||
| ): | ||||||
| self.geom = geom | ||||||
| self.on_boundary = lambda x, on: np.array( | ||||||
| [on_boundary(x[i], on[i]) for i in range(len(x))] | ||||||
|
|
@@ -45,28 +55,57 @@ def __init__(self, geom, on_boundary, component): | |||||
| utils.return_tensor(self.geom.boundary_normal) | ||||||
| ) | ||||||
|
|
||||||
| def filter(self, X): | ||||||
| def filter(self, X: NDArray[Any]) -> NDArray[np.bool_]: | ||||||
| return X[self.on_boundary(X, self.geom.on_boundary(X))] | ||||||
|
|
||||||
| def collocation_points(self, X): | ||||||
| def collocation_points(self, X: NDArray[Any]) -> NDArray[Any]: | ||||||
| return self.filter(X) | ||||||
|
|
||||||
| def normal_derivative(self, X, inputs, outputs, beg, end): | ||||||
| def normal_derivative( | ||||||
| self, | ||||||
| X: NDArray[Any], | ||||||
| inputs: TensorOrTensors, | ||||||
| outputs: Tensor, | ||||||
| beg: int, | ||||||
| end: int, | ||||||
| ) -> Tensor: | ||||||
| dydx = grad.jacobian(outputs, inputs, i=self.component, j=None)[beg:end] | ||||||
| n = self.boundary_normal(X, beg, end, None) | ||||||
| return bkd.sum(dydx * n, 1, keepdims=True) | ||||||
|
|
||||||
| @abstractmethod | ||||||
| def error(self, X, inputs, outputs, beg, end, aux_var=None): | ||||||
| def error( | ||||||
| self, | ||||||
| X: NDArray[Any], | ||||||
| inputs: TensorOrTensors, | ||||||
| outputs: Tensor, | ||||||
| beg: int, | ||||||
| end: int, | ||||||
| aux_var: Union[NDArray[np.float_], None] = None, | ||||||
|
||||||
| aux_var: Union[NDArray[np.float_], None] = None, | |
| aux_var: NDArray[np.float_] | None = None, |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| component: Union[List[int], int] = 0, | |
| component: list[int] | int = 0, |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not call this file as |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,7 @@ | ||||||||
| import numpy as np | ||||||||
| from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union, TypeVar | ||||||||
|
||||||||
| from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union, TypeVar | |
| from __future__ import annotations | |
| from typing import Sequence, TypeVar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestions, I will change them later 👍
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| TensorOrTensors = Union[Tensor, Sequence[Tensor]] | |
| TensorOrTensors = Tensor | Sequence[Tensor] | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that
DeepXDEsupports Python 3.8+, you should use -