PyFinitDiff 1.2.3__py3-none-any.whl → 1.3.1.post0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyFinitDiff/__init__.py +8 -0
- PyFinitDiff/_version.py +16 -0
- PyFinitDiff/finite_difference_1D/__init__.py +6 -6
- PyFinitDiff/finite_difference_1D/boundaries.py +113 -30
- PyFinitDiff/finite_difference_1D/derivative.py +44 -0
- PyFinitDiff/finite_difference_1D/diagonals.py +4 -3
- PyFinitDiff/finite_difference_1D/finite_difference.py +21 -7
- PyFinitDiff/finite_difference_1D/utils.py +40 -4
- PyFinitDiff/finite_difference_2D/__init__.py +5 -7
- PyFinitDiff/finite_difference_2D/boundaries.py +103 -23
- PyFinitDiff/finite_difference_2D/dense.py +15 -9
- PyFinitDiff/finite_difference_2D/derivative.py +18 -29
- PyFinitDiff/finite_difference_2D/diagonals.py +147 -91
- PyFinitDiff/finite_difference_2D/finite_difference.py +67 -56
- PyFinitDiff/finite_difference_2D/utils.py +51 -5
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.1.post0.dist-info}/METADATA +31 -11
- PyFinitDiff-1.3.1.post0.dist-info/RECORD +28 -0
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.1.post0.dist-info}/WHEEL +1 -1
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.1.post0.dist-info}/top_level.txt +0 -1
- PyFinitDiff/VERSION +0 -1
- PyFinitDiff/__pycache__/__init__.cpython-311.pyc +0 -0
- PyFinitDiff/__pycache__/triplet.cpython-311.pyc +0 -0
- PyFinitDiff/__pycache__/utils.cpython-311.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/__init__.cpython-311.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/backward.cpython-311.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/central.cpython-311.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/forward.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/__init__.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/boundaries.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/derivatives.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/diagonals.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/finite_difference.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/meshes.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/utils.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/derivatives.py +0 -53
- PyFinitDiff/finite_difference_1D/meshes.py +0 -41
- PyFinitDiff/finite_difference_2D/__pycache__/__init__.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/boundaries.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/derivative.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/diagonals.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/finite_difference.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/meshes.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/utils.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/meshes.py +0 -50
- PyFinitDiff-1.2.3.dist-info/RECORD +0 -65
- tests/1D/__init__.py +0 -0
- tests/1D/__pycache__/__init__.cpython-311.pyc +0 -0
- tests/1D/__pycache__/test_api.cpython-311-pytest-8.2.2.pyc +0 -0
- tests/1D/__pycache__/test_derivative.cpython-311-pytest-8.2.2.pyc +0 -0
- tests/1D/test_api.py +0 -36
- tests/1D/test_derivative.py +0 -60
- tests/2D/__init__.py +0 -0
- tests/2D/__pycache__/__init__.cpython-311.pyc +0 -0
- tests/2D/__pycache__/test_api.cpython-311-pytest-8.2.2.pyc +0 -0
- tests/2D/__pycache__/test_derivative.cpython-311-pytest-8.2.2.pyc +0 -0
- tests/2D/__pycache__/test_eigen.cpython-311-pytest-8.2.2.pyc +0 -0
- tests/2D/test_api.py +0 -44
- tests/2D/test_derivative.py +0 -76
- tests/2D/test_eigen.py +0 -58
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.1.post0.dist-info}/LICENSE +0 -0
PyFinitDiff/__init__.py
CHANGED
PyFinitDiff/_version.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# file generated by setuptools_scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
TYPE_CHECKING = False
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from typing import Tuple, Union
|
|
6
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
7
|
+
else:
|
|
8
|
+
VERSION_TUPLE = object
|
|
9
|
+
|
|
10
|
+
version: str
|
|
11
|
+
__version__: str
|
|
12
|
+
__version_tuple__: VERSION_TUPLE
|
|
13
|
+
version_tuple: VERSION_TUPLE
|
|
14
|
+
|
|
15
|
+
__version__ = version = '1.3.1.post0'
|
|
16
|
+
__version_tuple__ = version_tuple = (1, 3, 1)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
from .
|
|
2
|
-
from .
|
|
3
|
-
from .
|
|
4
|
-
from .
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
from .finite_difference import FiniteDifference # noqa: F401
|
|
2
|
+
from .boundaries import Boundaries # noqa: F401
|
|
3
|
+
from .utils import get_circular_mesh_triplet # noqa: F401
|
|
4
|
+
from .derivative import get_function_derivative # noqa: F401
|
|
5
|
+
|
|
6
|
+
# -
|
|
@@ -3,81 +3,151 @@
|
|
|
3
3
|
|
|
4
4
|
import numpy
|
|
5
5
|
|
|
6
|
-
from typing import Optional,
|
|
6
|
+
from typing import Optional, List, Tuple
|
|
7
7
|
from pydantic.dataclasses import dataclass
|
|
8
8
|
from pydantic import ConfigDict
|
|
9
9
|
|
|
10
|
-
config_dict = ConfigDict(
|
|
10
|
+
config_dict = ConfigDict(
|
|
11
|
+
extra='forbid',
|
|
12
|
+
strict=True,
|
|
13
|
+
arbitrary_types_allowed=True,
|
|
14
|
+
kw_only=True,
|
|
15
|
+
frozen=True
|
|
16
|
+
)
|
|
11
17
|
|
|
12
18
|
|
|
13
|
-
@dataclass(config=config_dict
|
|
14
|
-
class Boundary
|
|
19
|
+
@dataclass(config=config_dict)
|
|
20
|
+
class Boundary:
|
|
21
|
+
"""Class representing a boundary with a specific name, value, and mesh information.
|
|
22
|
+
|
|
23
|
+
Attributes:
|
|
24
|
+
name (str): The name of the boundary.
|
|
25
|
+
value (Optional[str]): The value associated with the boundary.
|
|
26
|
+
mesh_info (object): The mesh information object.
|
|
27
|
+
"""
|
|
28
|
+
|
|
15
29
|
name: str
|
|
16
|
-
value:
|
|
30
|
+
value: Optional[str]
|
|
17
31
|
mesh_info: object
|
|
18
32
|
|
|
19
33
|
def get_factor(self) -> float:
|
|
34
|
+
"""Gets the factor associated with the boundary value.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
float: The factor corresponding to the boundary value.
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
ValueError: If the boundary value is unexpected.
|
|
41
|
+
"""
|
|
20
42
|
match self.value:
|
|
21
43
|
case 'symmetric':
|
|
22
|
-
return
|
|
44
|
+
return 1.0
|
|
23
45
|
case 'anti-symmetric':
|
|
24
|
-
return -1
|
|
46
|
+
return -1.0
|
|
25
47
|
case 'zero':
|
|
26
|
-
return 0
|
|
48
|
+
return 0.0
|
|
27
49
|
case 'none':
|
|
28
50
|
return numpy.nan
|
|
51
|
+
case _:
|
|
52
|
+
raise ValueError(f"Unexpected boundary value: {self.value}")
|
|
53
|
+
|
|
54
|
+
def get_shift_vector(self, offset: int) -> Optional[numpy.ndarray]:
|
|
55
|
+
"""Calculates the shift vector based on the boundary name and offset.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
offset (int): The offset value to be used in the shift vector calculation.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
Optional[numpy.ndarray]: The shift vector as a numpy array, or None if the boundary name is 'center'.
|
|
29
62
|
|
|
30
|
-
|
|
63
|
+
Raises:
|
|
64
|
+
ValueError: If the boundary name is unexpected.
|
|
65
|
+
"""
|
|
31
66
|
offset = abs(offset)
|
|
32
67
|
|
|
33
68
|
match self.name.lower():
|
|
34
69
|
case 'center':
|
|
35
|
-
|
|
70
|
+
return None
|
|
36
71
|
case 'left':
|
|
37
72
|
shift_vector = numpy.zeros(self.mesh_info.size)
|
|
38
73
|
shift_vector[:offset] = numpy.arange(offset)[::-1] + 1
|
|
74
|
+
return shift_vector
|
|
39
75
|
case 'right':
|
|
40
76
|
shift_vector = numpy.zeros(self.mesh_info.size)
|
|
41
|
-
shift_vector[-offset - 1:] = -
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
77
|
+
shift_vector[-offset - 1:] = -numpy.arange(offset + 1)
|
|
78
|
+
return shift_vector
|
|
79
|
+
case _:
|
|
80
|
+
raise ValueError(f"Unexpected boundary name: {self.name}")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass(config=config_dict)
|
|
84
|
+
class Boundaries:
|
|
85
|
+
"""Class representing the boundaries with left and right values.
|
|
86
|
+
|
|
87
|
+
Attributes:
|
|
88
|
+
left (Optional[str]): Value of the left boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
89
|
+
right (Optional[str]): Value of the right boundary. Defaults to 'zero' either ['zero', 'symmetric', 'anti-symmetric'].
|
|
90
|
+
acceptable_boundary (List[str]): List of acceptable boundary values.
|
|
91
|
+
all_boundaries (List[str]): List of all boundary names.
|
|
92
|
+
"""
|
|
48
93
|
left: Optional[str] = 'zero'
|
|
49
|
-
""" Value of the left boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
50
94
|
right: Optional[str] = 'zero'
|
|
51
|
-
""" Value of the right boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
52
95
|
|
|
53
96
|
acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
|
|
54
|
-
|
|
55
97
|
all_boundaries = ['left', 'right']
|
|
56
98
|
|
|
57
99
|
def __post_init__(self) -> None:
|
|
100
|
+
"""Post-initialization method to assert acceptable boundary values."""
|
|
58
101
|
for boundary in self.all_boundaries:
|
|
59
102
|
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
60
103
|
|
|
61
104
|
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
105
|
+
"""Asserts that both boundaries are not the same axis symmetries if they are not 'zero'.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
boundary_0 (str): The first boundary value.
|
|
109
|
+
boundary_1 (str): The second boundary value.
|
|
110
|
+
|
|
111
|
+
Raises:
|
|
112
|
+
ValueError: If both boundaries are set to the same axis symmetries.
|
|
113
|
+
"""
|
|
62
114
|
if boundary_0 != 'zero' and boundary_1 != 'zero':
|
|
63
|
-
raise ValueError("Same-axis symmetries shouldn't be set on both
|
|
115
|
+
raise ValueError("Same-axis symmetries shouldn't be set on both ends")
|
|
64
116
|
|
|
65
117
|
def assert_boundary_acceptable(self, boundary_string: str) -> None:
|
|
118
|
+
"""Asserts that a given boundary value is acceptable.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
boundary_string (str): The name of the boundary to check.
|
|
122
|
+
|
|
123
|
+
Raises:
|
|
124
|
+
AssertionError: If the boundary value is not acceptable.
|
|
125
|
+
"""
|
|
66
126
|
boundary_value = getattr(self, boundary_string)
|
|
67
|
-
assert boundary_value in self.acceptable_boundary,
|
|
127
|
+
assert boundary_value in self.acceptable_boundary, (
|
|
128
|
+
f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. "
|
|
129
|
+
f"Input must be in: {self.acceptable_boundary}"
|
|
130
|
+
)
|
|
68
131
|
|
|
69
|
-
def get_boundary_pairs(self) ->
|
|
70
|
-
|
|
132
|
+
def get_boundary_pairs(self) -> List[Tuple[str, str]]:
|
|
133
|
+
"""Gets the pairs of boundaries.
|
|
71
134
|
|
|
72
|
-
|
|
135
|
+
Returns:
|
|
136
|
+
List[Tuple[str, str]]: A list of tuples containing boundary pairs.
|
|
73
137
|
"""
|
|
74
|
-
|
|
138
|
+
return [(self.left, self.right)]
|
|
139
|
+
|
|
140
|
+
def get_boundary(self, name: str) -> Boundary:
|
|
141
|
+
"""Returns a specific instance of the boundary.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
name (str): The name of the boundary.
|
|
75
145
|
|
|
76
|
-
:
|
|
77
|
-
|
|
146
|
+
Returns:
|
|
147
|
+
Boundary: The boundary instance.
|
|
78
148
|
|
|
79
|
-
:
|
|
80
|
-
|
|
149
|
+
Raises:
|
|
150
|
+
AttributeError: If the boundary name is not an attribute of the instance.
|
|
81
151
|
"""
|
|
82
152
|
if not hasattr(self, name):
|
|
83
153
|
value = None
|
|
@@ -93,6 +163,17 @@ class Boundaries():
|
|
|
93
163
|
return boundary
|
|
94
164
|
|
|
95
165
|
def offset_to_boundary(self, offset: int) -> str:
|
|
166
|
+
"""Determines the boundary based on the offset.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
offset (int): The offset value.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
str: The name of the boundary corresponding to the offset.
|
|
173
|
+
|
|
174
|
+
Raises:
|
|
175
|
+
ValueError: If the offset does not correspond to a valid boundary.
|
|
176
|
+
"""
|
|
96
177
|
if offset == 0:
|
|
97
178
|
return self.get_boundary('center')
|
|
98
179
|
|
|
@@ -104,4 +185,6 @@ class Boundaries():
|
|
|
104
185
|
if offset > -self.mesh_info.n_x:
|
|
105
186
|
return self.get_boundary('left')
|
|
106
187
|
|
|
188
|
+
raise ValueError("Offset does not correspond to a valid boundary.")
|
|
189
|
+
|
|
107
190
|
# -
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
from PyFinitDiff.coefficients import FiniteCoefficients
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_function_derivative(
|
|
8
|
+
function: callable,
|
|
9
|
+
x_eval: float,
|
|
10
|
+
derivative: int,
|
|
11
|
+
delta: float,
|
|
12
|
+
function_kwargs: dict,
|
|
13
|
+
accuracy: int = 4,
|
|
14
|
+
coefficient_type: str = 'central') -> float:
|
|
15
|
+
"""
|
|
16
|
+
Computes the derivative of a given function at a specified point using finite differences.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
function (callable): The function to differentiate.
|
|
20
|
+
x_eval (float): The point at which to evaluate the derivative.
|
|
21
|
+
derivative (int): The order of the derivative.
|
|
22
|
+
delta (float): The distance between points.
|
|
23
|
+
function_kwargs (dict): Additional keyword arguments to pass to the function.
|
|
24
|
+
accuracy (int, optional): The accuracy of the finite difference approximation. Defaults to 4.
|
|
25
|
+
coefficient_type (str, optional): The type of finite difference coefficients ('central', 'forward', 'backward'). Defaults to 'central'.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
float: The value of the derivative at the specified point.
|
|
29
|
+
"""
|
|
30
|
+
coefficients = FiniteCoefficients(
|
|
31
|
+
derivative=derivative,
|
|
32
|
+
accuracy=accuracy,
|
|
33
|
+
coefficient_type=coefficient_type
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
summation = 0
|
|
37
|
+
for index, value in coefficients:
|
|
38
|
+
x = x_eval + index * delta
|
|
39
|
+
y = function(x, **function_kwargs)
|
|
40
|
+
summation += value * y
|
|
41
|
+
|
|
42
|
+
return summation / delta ** derivative
|
|
43
|
+
|
|
44
|
+
# -
|
|
@@ -7,7 +7,8 @@ from dataclasses import dataclass, field
|
|
|
7
7
|
# Local imports
|
|
8
8
|
import numpy
|
|
9
9
|
from PyFinitDiff.triplet import Triplet
|
|
10
|
-
|
|
10
|
+
from PyFinitDiff.finite_difference_1D.utils import MeshInfo
|
|
11
|
+
from PyFinitDiff.finite_difference_1D.boundaries import Boundary
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
@dataclass
|
|
@@ -24,7 +25,7 @@ class Diagonal():
|
|
|
24
25
|
""" Offset of the column index for the diagonal. """
|
|
25
26
|
values: float
|
|
26
27
|
""" Value associated with the diagonal. """
|
|
27
|
-
boundary:
|
|
28
|
+
boundary: Boundary
|
|
28
29
|
""" Instance of the boundary used for that diagonal. """
|
|
29
30
|
|
|
30
31
|
@property
|
|
@@ -105,7 +106,7 @@ class Diagonal():
|
|
|
105
106
|
|
|
106
107
|
|
|
107
108
|
class ConstantDiagonal(Diagonal):
|
|
108
|
-
def __init__(self, offset: int, value: float, mesh_info: list, boundary:
|
|
109
|
+
def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
|
|
109
110
|
super().__init__(
|
|
110
111
|
offset=offset,
|
|
111
112
|
mesh_info=mesh_info,
|
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env python
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
|
-
from dataclasses import dataclass
|
|
5
|
-
|
|
4
|
+
from pydantic.dataclasses import dataclass
|
|
5
|
+
from pydantic import ConfigDict
|
|
6
|
+
from dataclasses import field
|
|
7
|
+
|
|
6
8
|
from PyFinitDiff.coefficients import FiniteCoefficients
|
|
9
|
+
from PyFinitDiff.finite_difference_1D.boundaries import Boundaries
|
|
10
|
+
from PyFinitDiff.finite_difference_1D.utils import MeshInfo
|
|
11
|
+
from PyFinitDiff.finite_difference_1D.diagonals import DiagonalSet, ConstantDiagonal
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
config_dict = ConfigDict(
|
|
15
|
+
extra='forbid',
|
|
16
|
+
strict=True,
|
|
17
|
+
arbitrary_types_allowed=True,
|
|
18
|
+
kw_only=True,
|
|
19
|
+
frozen=True
|
|
20
|
+
)
|
|
7
21
|
|
|
8
22
|
|
|
9
|
-
@dataclass
|
|
23
|
+
@dataclass(config=config_dict)
|
|
10
24
|
class FiniteDifference():
|
|
11
25
|
"""
|
|
12
26
|
This class represent a specific finit difference configuration, which is defined with the descretization of the mesh, the derivative order,
|
|
@@ -20,13 +34,13 @@ class FiniteDifference():
|
|
|
20
34
|
""" Derivative order to convert into finit-difference matrix. """
|
|
21
35
|
accuracy: int = 2
|
|
22
36
|
""" Accuracy of the derivative approximation [error is inversly proportional to the power of that value]. """
|
|
23
|
-
boundaries:
|
|
37
|
+
boundaries: Boundaries = field(default_factory=Boundaries)
|
|
24
38
|
""" Values of the four possible boundaries of the system. """
|
|
25
39
|
x_derivative: bool = True
|
|
26
40
|
""" Add the x derivative """
|
|
27
41
|
|
|
28
42
|
def __post_init__(self):
|
|
29
|
-
self.mesh_info =
|
|
43
|
+
self.mesh_info = MeshInfo(
|
|
30
44
|
n_x=self.n_x,
|
|
31
45
|
dx=self.dx,
|
|
32
46
|
)
|
|
@@ -103,7 +117,7 @@ class FiniteDifference():
|
|
|
103
117
|
:returns: No return
|
|
104
118
|
:rtype: None
|
|
105
119
|
"""
|
|
106
|
-
diagonal_set =
|
|
120
|
+
diagonal_set = DiagonalSet(mesh_info=self.mesh_info)
|
|
107
121
|
|
|
108
122
|
coefficients = getattr(self.finit_coefficient, coefficient_type)
|
|
109
123
|
|
|
@@ -113,7 +127,7 @@ class FiniteDifference():
|
|
|
113
127
|
)
|
|
114
128
|
|
|
115
129
|
for offset, value, boundary in iterator:
|
|
116
|
-
diagonal =
|
|
130
|
+
diagonal = ConstantDiagonal(
|
|
117
131
|
mesh_info=self.mesh_info,
|
|
118
132
|
offset=offset,
|
|
119
133
|
boundary=boundary,
|
|
@@ -2,17 +2,53 @@
|
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
4
|
from dataclasses import dataclass
|
|
5
|
+
import numpy
|
|
6
|
+
from PyFinitDiff.triplet import DiagonalTriplet
|
|
5
7
|
|
|
6
8
|
|
|
7
9
|
@dataclass
|
|
8
|
-
class MeshInfo
|
|
10
|
+
class MeshInfo:
|
|
11
|
+
"""
|
|
12
|
+
This class represents the mesh information for a 1D finite-difference grid.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
n_x (int): Number of points in the x direction.
|
|
16
|
+
dx (float): Infinitesimal displacement in the x direction. Defaults to 1.
|
|
17
|
+
size (int): Total number of points in the mesh.
|
|
18
|
+
shape (Tuple[int]): Shape of the mesh as a tuple.
|
|
19
|
+
"""
|
|
9
20
|
n_x: int
|
|
10
|
-
""" Number of point in the x direction """
|
|
11
21
|
dx: float = 1
|
|
12
|
-
""" Infinetisemal displacement in x direction """
|
|
13
22
|
|
|
14
23
|
def __post_init__(self):
|
|
24
|
+
"""Post-initialization to calculate mesh size and shape."""
|
|
15
25
|
self.size = self.n_x
|
|
16
26
|
self.shape = (self.n_x,)
|
|
17
27
|
|
|
18
|
-
|
|
28
|
+
|
|
29
|
+
def get_circular_mesh_triplet(
|
|
30
|
+
n_x: int,
|
|
31
|
+
radius: float,
|
|
32
|
+
x_offset: float = 0,
|
|
33
|
+
value_out: float = 0,
|
|
34
|
+
value_in: float = 1) -> DiagonalTriplet:
|
|
35
|
+
"""
|
|
36
|
+
Gets a Triplet corresponding to a 1D mesh with a circular structure of value_in inside and value_out outside.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
n_x (int): The number of points in the x-axis.
|
|
40
|
+
radius (float): The radius of the circular structure.
|
|
41
|
+
x_offset (float, optional): The x offset of the circular structure. Defaults to 0.
|
|
42
|
+
value_out (float, optional): The value outside the circular structure. Defaults to 0.
|
|
43
|
+
value_in (float, optional): The value inside the circular structure. Defaults to 1.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
DiagonalTriplet: The 1D circular mesh triplet.
|
|
47
|
+
"""
|
|
48
|
+
x = numpy.linspace(-100, 100, n_x)
|
|
49
|
+
|
|
50
|
+
r = numpy.abs(x - x_offset)
|
|
51
|
+
mesh = numpy.ones(x.shape) * value_out
|
|
52
|
+
mesh[r < radius] = value_in
|
|
53
|
+
|
|
54
|
+
return DiagonalTriplet(mesh, shape=(n_x,))
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
from .
|
|
2
|
-
from .
|
|
3
|
-
from .
|
|
4
|
-
from .
|
|
5
|
-
from .derivative import *
|
|
6
|
-
from .meshes import *
|
|
1
|
+
from .finite_difference import FiniteDifference # noqa: F401
|
|
2
|
+
from .boundaries import Boundaries # noqa: F401
|
|
3
|
+
from .utils import get_circular_mesh_triplet # noqa: F401
|
|
4
|
+
from .derivative import get_array_derivative # noqa: F401
|
|
7
5
|
|
|
8
|
-
#
|
|
6
|
+
# -
|
|
@@ -2,27 +2,58 @@
|
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
4
|
import numpy
|
|
5
|
-
from
|
|
5
|
+
from typing import Optional, List, Tuple
|
|
6
|
+
from pydantic.dataclasses import dataclass
|
|
7
|
+
from pydantic import ConfigDict
|
|
6
8
|
|
|
9
|
+
config_dict = ConfigDict(
|
|
10
|
+
extra='forbid',
|
|
11
|
+
strict=True,
|
|
12
|
+
arbitrary_types_allowed=True,
|
|
13
|
+
kw_only=True,
|
|
14
|
+
frozen=True
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(config=config_dict)
|
|
19
|
+
class Boundary:
|
|
20
|
+
"""Class representing a boundary with a specific name, value, and mesh information.
|
|
21
|
+
|
|
22
|
+
Attributes:
|
|
23
|
+
name (str): The name of the boundary.
|
|
24
|
+
value (str): The value associated with the boundary.
|
|
25
|
+
mesh_info (object): The mesh information object.
|
|
26
|
+
"""
|
|
7
27
|
|
|
8
|
-
@dataclass(frozen=True)
|
|
9
|
-
class Boundary():
|
|
10
28
|
name: str
|
|
11
|
-
value: str
|
|
29
|
+
value: Optional[str]
|
|
12
30
|
mesh_info: object
|
|
13
31
|
|
|
14
32
|
def get_factor(self) -> float:
|
|
33
|
+
"""Gets the factor associated with the boundary value.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
float: The factor corresponding to the boundary value.
|
|
37
|
+
"""
|
|
15
38
|
match self.value:
|
|
16
39
|
case 'symmetric':
|
|
17
|
-
return
|
|
40
|
+
return 1.0
|
|
18
41
|
case 'anti-symmetric':
|
|
19
|
-
return -1
|
|
42
|
+
return -1.0
|
|
20
43
|
case 'zero':
|
|
21
|
-
return 0
|
|
44
|
+
return 0.0
|
|
22
45
|
case 'none':
|
|
23
46
|
return numpy.nan
|
|
24
47
|
|
|
25
48
|
def get_shift_vector(self, offset: int) -> numpy.ndarray:
|
|
49
|
+
"""Calculates the shift vector based on the boundary name and offset.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
offset (int): The offset value to be used in the shift vector calculation.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
numpy.ndarray: The shift vector as a numpy array.
|
|
56
|
+
"""
|
|
26
57
|
offset = abs(offset)
|
|
27
58
|
|
|
28
59
|
match self.name.lower():
|
|
@@ -46,45 +77,76 @@ class Boundary():
|
|
|
46
77
|
return shift_vector
|
|
47
78
|
|
|
48
79
|
|
|
49
|
-
@dataclass()
|
|
50
|
-
class Boundaries
|
|
80
|
+
@dataclass(config=config_dict)
|
|
81
|
+
class Boundaries:
|
|
82
|
+
"""Class representing the boundaries with left, right, top, and bottom values.
|
|
83
|
+
|
|
84
|
+
Attributes:
|
|
85
|
+
left (str): Value of the left boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
86
|
+
right (str): Value of the right boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
87
|
+
top (str): Value of the top boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
88
|
+
bottom (str): Value of the bottom boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
89
|
+
acceptable_boundary (List[str]): List of acceptable boundary values.
|
|
90
|
+
all_boundaries (List[str]): List of all boundary names.
|
|
91
|
+
"""
|
|
92
|
+
|
|
51
93
|
left: str = 'zero'
|
|
52
|
-
""" Value of the left boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
53
94
|
right: str = 'zero'
|
|
54
|
-
""" Value of the right boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
55
95
|
top: str = 'zero'
|
|
56
|
-
""" Value of the top boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
57
96
|
bottom: str = 'zero'
|
|
58
|
-
""" Value of the bottom boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
59
97
|
|
|
60
98
|
acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
|
|
61
|
-
|
|
62
99
|
all_boundaries = ['left', 'right', 'top', 'bottom']
|
|
63
100
|
|
|
64
101
|
def __post_init__(self) -> None:
|
|
102
|
+
"""Post-initialization method to assert acceptable boundary values."""
|
|
65
103
|
for boundary in self.all_boundaries:
|
|
66
104
|
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
67
105
|
|
|
68
106
|
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
107
|
+
"""Asserts that both boundaries are not the same axis symmetries if they are not 'zero'.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
boundary_0 (str): The first boundary value.
|
|
111
|
+
boundary_1 (str): The second boundary value.
|
|
112
|
+
|
|
113
|
+
Raises:
|
|
114
|
+
ValueError: If both boundaries are set to the same axis symmetries.
|
|
115
|
+
"""
|
|
69
116
|
if boundary_0 != 'zero' and boundary_1 != 'zero':
|
|
70
|
-
raise ValueError("Same-axis symmetries shouldn't be set on both
|
|
117
|
+
raise ValueError("Same-axis symmetries shouldn't be set on both ends")
|
|
71
118
|
|
|
72
119
|
def assert_boundary_acceptable(self, boundary_string: str) -> None:
|
|
120
|
+
"""Asserts that a given boundary value is acceptable.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
boundary_string (str): The name of the boundary to check.
|
|
124
|
+
|
|
125
|
+
Raises:
|
|
126
|
+
AssertionError: If the boundary value is not acceptable.
|
|
127
|
+
"""
|
|
73
128
|
boundary_value = getattr(self, boundary_string)
|
|
74
|
-
assert boundary_value in self.acceptable_boundary,
|
|
129
|
+
assert boundary_value in self.acceptable_boundary, (
|
|
130
|
+
f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. "
|
|
131
|
+
f"Input must be in: {self.acceptable_boundary}"
|
|
132
|
+
)
|
|
75
133
|
|
|
76
|
-
def get_boundary_pairs(self) ->
|
|
134
|
+
def get_boundary_pairs(self) -> List[Tuple[str, str]]:
|
|
135
|
+
"""Gets the pairs of boundaries.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
List[Tuple[str, str]]: A list of tuples containing boundary pairs.
|
|
139
|
+
"""
|
|
77
140
|
return [(self.left, self.right), (self.top, self.bottom)]
|
|
78
141
|
|
|
79
142
|
def get_boundary(self, name: str) -> Boundary:
|
|
80
|
-
"""
|
|
81
|
-
Return a specific instance of the boundary
|
|
143
|
+
"""Returns a specific instance of the boundary.
|
|
82
144
|
|
|
83
|
-
:
|
|
84
|
-
|
|
145
|
+
Args:
|
|
146
|
+
name (str): The name of the boundary.
|
|
85
147
|
|
|
86
|
-
:
|
|
87
|
-
|
|
148
|
+
Returns:
|
|
149
|
+
Boundary: The boundary instance.
|
|
88
150
|
"""
|
|
89
151
|
if not hasattr(self, name):
|
|
90
152
|
value = None
|
|
@@ -100,6 +162,14 @@ class Boundaries():
|
|
|
100
162
|
return boundary
|
|
101
163
|
|
|
102
164
|
def offset_to_boundary(self, offset: int) -> str:
|
|
165
|
+
"""Determines the boundary based on the offset.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
offset (int): The offset value.
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
str: The name of the boundary corresponding to the offset.
|
|
172
|
+
"""
|
|
103
173
|
if offset == 0:
|
|
104
174
|
return self.get_boundary('center')
|
|
105
175
|
|
|
@@ -116,6 +186,11 @@ class Boundaries():
|
|
|
116
186
|
return self.get_boundary('bottom')
|
|
117
187
|
|
|
118
188
|
def get_x_parity(self) -> str:
|
|
189
|
+
"""Determines the parity in the x direction based on the left and right boundaries.
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
str: The parity in the x direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
193
|
+
"""
|
|
119
194
|
if self.left == 'symmetric' or self.right == 'symmetric':
|
|
120
195
|
return 'symmetric'
|
|
121
196
|
elif self.left == 'anti-symmetric' or self.right == 'anti-symmetric':
|
|
@@ -124,6 +199,11 @@ class Boundaries():
|
|
|
124
199
|
return 'zero'
|
|
125
200
|
|
|
126
201
|
def get_y_parity(self) -> str:
|
|
202
|
+
"""Determines the parity in the y direction based on the top and bottom boundaries.
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
str: The parity in the y direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
206
|
+
"""
|
|
127
207
|
if self.top == 'symmetric' or self.bottom == 'symmetric':
|
|
128
208
|
return 'symmetric'
|
|
129
209
|
elif self.top == 'anti-symmetric' or self.bottom == 'anti-symmetric':
|