PyFinitDiff 2.0.4.post0__py3-none-any.whl → 2.1.3.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/_version.py +2 -2
- PyFinitDiff/finite_difference_1D/boundaries.py +118 -67
- PyFinitDiff/finite_difference_1D/derivative.py +30 -15
- PyFinitDiff/finite_difference_1D/diagonals.py +154 -79
- PyFinitDiff/finite_difference_1D/finite_difference.py +104 -57
- PyFinitDiff/finite_difference_1D/utils.py +36 -18
- PyFinitDiff/finite_difference_2D/boundaries.py +116 -63
- PyFinitDiff/finite_difference_2D/derivative.py +31 -14
- PyFinitDiff/finite_difference_2D/diagonals.py +122 -75
- PyFinitDiff/finite_difference_2D/finite_difference.py +112 -63
- PyFinitDiff/finite_difference_2D/utils.py +46 -26
- PyFinitDiff/triplet.py +312 -33
- {PyFinitDiff-2.0.4.post0.dist-info → PyFinitDiff-2.1.3.post0.dist-info}/METADATA +60 -28
- PyFinitDiff-2.1.3.post0.dist-info/RECORD +26 -0
- {PyFinitDiff-2.0.4.post0.dist-info → PyFinitDiff-2.1.3.post0.dist-info}/WHEEL +1 -1
- PyFinitDiff/tools/__init__.py +0 -0
- PyFinitDiff-2.0.4.post0.dist-info/RECORD +0 -27
- /PyFinitDiff/{tools/directories.py → directories.py} +0 -0
- {PyFinitDiff-2.0.4.post0.dist-info → PyFinitDiff-2.1.3.post0.dist-info}/LICENSE +0 -0
- {PyFinitDiff-2.0.4.post0.dist-info → PyFinitDiff-2.1.3.post0.dist-info}/top_level.txt +0 -0
|
@@ -9,19 +9,26 @@ from PyFinitDiff.triplet import DiagonalTriplet
|
|
|
9
9
|
@dataclass
|
|
10
10
|
class MeshInfo:
|
|
11
11
|
"""
|
|
12
|
-
|
|
12
|
+
Represents the mesh information for a 1D finite-difference grid.
|
|
13
13
|
|
|
14
|
-
Attributes
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
Attributes
|
|
15
|
+
----------
|
|
16
|
+
n_x : int
|
|
17
|
+
Number of points in the x direction.
|
|
18
|
+
dx : float, optional
|
|
19
|
+
Infinitesimal displacement in the x direction. Defaults to 1.
|
|
20
|
+
size : int
|
|
21
|
+
Total number of points in the mesh.
|
|
22
|
+
shape : Tuple[int]
|
|
23
|
+
Shape of the mesh as a tuple.
|
|
19
24
|
"""
|
|
20
25
|
n_x: int
|
|
21
26
|
dx: float = 1
|
|
22
27
|
|
|
23
|
-
def __post_init__(self):
|
|
24
|
-
"""
|
|
28
|
+
def __post_init__(self) -> None:
|
|
29
|
+
"""
|
|
30
|
+
Post-initialization to calculate mesh size and shape.
|
|
31
|
+
"""
|
|
25
32
|
self.size = self.n_x
|
|
26
33
|
self.shape = (self.n_x,)
|
|
27
34
|
|
|
@@ -33,20 +40,31 @@ def get_circular_mesh_triplet(
|
|
|
33
40
|
value_out: float = 0,
|
|
34
41
|
value_in: float = 1) -> DiagonalTriplet:
|
|
35
42
|
"""
|
|
36
|
-
|
|
43
|
+
Generates a DiagonalTriplet representing a 1D mesh with a circular structure.
|
|
37
44
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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.
|
|
45
|
+
The mesh is represented with `value_in` inside a circle of a given `radius`,
|
|
46
|
+
and `value_out` outside the circle. The circular structure is centered with an
|
|
47
|
+
optional x-offset.
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
n_x : int
|
|
52
|
+
The number of points in the x-axis.
|
|
53
|
+
radius : float
|
|
54
|
+
The radius of the circular structure.
|
|
55
|
+
x_offset : float, optional
|
|
56
|
+
The x-offset of the circular structure. Defaults to 0.
|
|
57
|
+
value_out : float, optional
|
|
58
|
+
The value outside the circular structure. Defaults to 0.
|
|
59
|
+
value_in : float, optional
|
|
60
|
+
The value inside the circular structure. Defaults to 1.
|
|
61
|
+
|
|
62
|
+
Returns
|
|
63
|
+
-------
|
|
64
|
+
DiagonalTriplet
|
|
65
|
+
The 1D circular mesh triplet.
|
|
47
66
|
"""
|
|
48
67
|
x = numpy.linspace(-100, 100, n_x)
|
|
49
|
-
|
|
50
68
|
r = numpy.abs(x - x_offset)
|
|
51
69
|
mesh = numpy.ones(x.shape) * value_out
|
|
52
70
|
mesh[r < radius] = value_in
|
|
@@ -17,23 +17,34 @@ config_dict = ConfigDict(
|
|
|
17
17
|
|
|
18
18
|
@dataclass(config=config_dict)
|
|
19
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
20
|
"""
|
|
27
|
-
|
|
21
|
+
Represents a boundary with a specific name, value, and mesh information.
|
|
22
|
+
|
|
23
|
+
Parameters
|
|
24
|
+
----------
|
|
25
|
+
name : str
|
|
26
|
+
The name of the boundary.
|
|
27
|
+
value : Optional[str]
|
|
28
|
+
The value associated with the boundary (e.g., 'symmetric', 'anti-symmetric').
|
|
29
|
+
mesh_info : object
|
|
30
|
+
Mesh information object, used to determine mesh-related properties.
|
|
31
|
+
"""
|
|
28
32
|
name: str
|
|
29
33
|
value: Optional[str]
|
|
30
34
|
mesh_info: object
|
|
31
35
|
|
|
32
36
|
def get_factor(self) -> float:
|
|
33
|
-
"""
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
"""
|
|
38
|
+
Gets the factor associated with the boundary value.
|
|
39
|
+
|
|
40
|
+
Returns
|
|
41
|
+
-------
|
|
42
|
+
float
|
|
43
|
+
The factor corresponding to the boundary value. Possible values are:
|
|
44
|
+
- 1.0 for 'symmetric'
|
|
45
|
+
- -1.0 for 'anti-symmetric'
|
|
46
|
+
- 0.0 for 'zero'
|
|
47
|
+
- numpy.nan for 'none'
|
|
37
48
|
"""
|
|
38
49
|
match self.value:
|
|
39
50
|
case 'symmetric':
|
|
@@ -45,14 +56,19 @@ class Boundary:
|
|
|
45
56
|
case 'none':
|
|
46
57
|
return numpy.nan
|
|
47
58
|
|
|
48
|
-
def get_shift_vector(self, offset: int) -> numpy.ndarray:
|
|
49
|
-
"""
|
|
59
|
+
def get_shift_vector(self, offset: int) -> Optional[numpy.ndarray]:
|
|
60
|
+
"""
|
|
61
|
+
Calculates the shift vector based on the boundary name and offset.
|
|
50
62
|
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
offset : int
|
|
66
|
+
The offset value to be used in the shift vector calculation.
|
|
53
67
|
|
|
54
|
-
Returns
|
|
55
|
-
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
Optional[numpy.ndarray]
|
|
71
|
+
The shift vector as a numpy array, or None if the boundary is 'center'.
|
|
56
72
|
"""
|
|
57
73
|
offset = abs(offset)
|
|
58
74
|
|
|
@@ -79,17 +95,24 @@ class Boundary:
|
|
|
79
95
|
|
|
80
96
|
@dataclass(config=config_dict)
|
|
81
97
|
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
98
|
"""
|
|
92
|
-
|
|
99
|
+
Represents the boundary conditions for a 2D finite-difference mesh.
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
left : str
|
|
104
|
+
Value of the left boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
|
|
105
|
+
right : str
|
|
106
|
+
Value of the right boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
|
|
107
|
+
top : str
|
|
108
|
+
Value of the top boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
|
|
109
|
+
bottom : str
|
|
110
|
+
Value of the bottom boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
|
|
111
|
+
acceptable_boundary : List[str]
|
|
112
|
+
List of acceptable boundary values.
|
|
113
|
+
all_boundaries : List[str]
|
|
114
|
+
List of all boundary names.
|
|
115
|
+
"""
|
|
93
116
|
left: str = 'zero'
|
|
94
117
|
right: str = 'zero'
|
|
95
118
|
top: str = 'zero'
|
|
@@ -99,31 +122,44 @@ class Boundaries:
|
|
|
99
122
|
all_boundaries = ['left', 'right', 'top', 'bottom']
|
|
100
123
|
|
|
101
124
|
def __post_init__(self) -> None:
|
|
102
|
-
"""
|
|
125
|
+
"""
|
|
126
|
+
Validates boundary values after initialization to ensure they are acceptable.
|
|
127
|
+
"""
|
|
103
128
|
for boundary in self.all_boundaries:
|
|
104
129
|
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
105
130
|
|
|
106
131
|
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
107
|
-
"""
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
132
|
+
"""
|
|
133
|
+
Ensures that two boundaries on the same axis are not set to identical symmetry conditions unless they are 'zero'.
|
|
134
|
+
|
|
135
|
+
Parameters
|
|
136
|
+
----------
|
|
137
|
+
boundary_0 : str
|
|
138
|
+
The first boundary value.
|
|
139
|
+
boundary_1 : str
|
|
140
|
+
The second boundary value.
|
|
141
|
+
|
|
142
|
+
Raises
|
|
143
|
+
------
|
|
144
|
+
ValueError
|
|
145
|
+
If both boundaries are set to the same symmetry condition and are not 'zero'.
|
|
115
146
|
"""
|
|
116
147
|
if boundary_0 != 'zero' and boundary_1 != 'zero':
|
|
117
148
|
raise ValueError("Same-axis symmetries shouldn't be set on both ends")
|
|
118
149
|
|
|
119
150
|
def assert_boundary_acceptable(self, boundary_string: str) -> None:
|
|
120
|
-
"""
|
|
151
|
+
"""
|
|
152
|
+
Checks whether the boundary value is acceptable.
|
|
121
153
|
|
|
122
|
-
|
|
123
|
-
|
|
154
|
+
Parameters
|
|
155
|
+
----------
|
|
156
|
+
boundary_string : str
|
|
157
|
+
The name of the boundary to validate.
|
|
124
158
|
|
|
125
|
-
Raises
|
|
126
|
-
|
|
159
|
+
Raises
|
|
160
|
+
------
|
|
161
|
+
AssertionError
|
|
162
|
+
If the boundary value is not within the list of acceptable values.
|
|
127
163
|
"""
|
|
128
164
|
boundary_value = getattr(self, boundary_string)
|
|
129
165
|
assert boundary_value in self.acceptable_boundary, (
|
|
@@ -132,21 +168,29 @@ class Boundaries:
|
|
|
132
168
|
)
|
|
133
169
|
|
|
134
170
|
def get_boundary_pairs(self) -> List[Tuple[str, str]]:
|
|
135
|
-
"""
|
|
171
|
+
"""
|
|
172
|
+
Retrieves pairs of boundaries.
|
|
136
173
|
|
|
137
|
-
Returns
|
|
138
|
-
|
|
174
|
+
Returns
|
|
175
|
+
-------
|
|
176
|
+
List[Tuple[str, str]]
|
|
177
|
+
A list of tuples containing the pairs of boundaries.
|
|
139
178
|
"""
|
|
140
179
|
return [(self.left, self.right), (self.top, self.bottom)]
|
|
141
180
|
|
|
142
181
|
def get_boundary(self, name: str) -> Boundary:
|
|
143
|
-
"""
|
|
182
|
+
"""
|
|
183
|
+
Retrieves a Boundary instance by name.
|
|
144
184
|
|
|
145
|
-
|
|
146
|
-
|
|
185
|
+
Parameters
|
|
186
|
+
----------
|
|
187
|
+
name : str
|
|
188
|
+
The name of the boundary to retrieve.
|
|
147
189
|
|
|
148
|
-
Returns
|
|
149
|
-
|
|
190
|
+
Returns
|
|
191
|
+
-------
|
|
192
|
+
Boundary
|
|
193
|
+
An instance of the Boundary class for the given boundary name.
|
|
150
194
|
"""
|
|
151
195
|
if not hasattr(self, name):
|
|
152
196
|
value = None
|
|
@@ -161,14 +205,19 @@ class Boundaries:
|
|
|
161
205
|
|
|
162
206
|
return boundary
|
|
163
207
|
|
|
164
|
-
def offset_to_boundary(self, offset: int) ->
|
|
165
|
-
"""
|
|
208
|
+
def offset_to_boundary(self, offset: int) -> Boundary:
|
|
209
|
+
"""
|
|
210
|
+
Determines the boundary corresponding to the given offset.
|
|
166
211
|
|
|
167
|
-
|
|
168
|
-
|
|
212
|
+
Parameters
|
|
213
|
+
----------
|
|
214
|
+
offset : int
|
|
215
|
+
The offset value.
|
|
169
216
|
|
|
170
|
-
Returns
|
|
171
|
-
|
|
217
|
+
Returns
|
|
218
|
+
-------
|
|
219
|
+
Boundary
|
|
220
|
+
The boundary instance corresponding to the offset.
|
|
172
221
|
"""
|
|
173
222
|
if offset == 0:
|
|
174
223
|
return self.get_boundary('center')
|
|
@@ -186,10 +235,13 @@ class Boundaries:
|
|
|
186
235
|
return self.get_boundary('bottom')
|
|
187
236
|
|
|
188
237
|
def get_x_parity(self) -> str:
|
|
189
|
-
"""
|
|
238
|
+
"""
|
|
239
|
+
Determines the parity in the x direction based on the left and right boundaries.
|
|
190
240
|
|
|
191
|
-
Returns
|
|
192
|
-
|
|
241
|
+
Returns
|
|
242
|
+
-------
|
|
243
|
+
str
|
|
244
|
+
The parity in the x direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
193
245
|
"""
|
|
194
246
|
if self.left == 'symmetric' or self.right == 'symmetric':
|
|
195
247
|
return 'symmetric'
|
|
@@ -199,10 +251,13 @@ class Boundaries:
|
|
|
199
251
|
return 'zero'
|
|
200
252
|
|
|
201
253
|
def get_y_parity(self) -> str:
|
|
202
|
-
"""
|
|
254
|
+
"""
|
|
255
|
+
Determines the parity in the y direction based on the top and bottom boundaries.
|
|
203
256
|
|
|
204
|
-
Returns
|
|
205
|
-
|
|
257
|
+
Returns
|
|
258
|
+
-------
|
|
259
|
+
str
|
|
260
|
+
The parity in the y direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
206
261
|
"""
|
|
207
262
|
if self.top == 'symmetric' or self.bottom == 'symmetric':
|
|
208
263
|
return 'symmetric'
|
|
@@ -210,5 +265,3 @@ class Boundaries:
|
|
|
210
265
|
return 'anti-symmetric'
|
|
211
266
|
else:
|
|
212
267
|
return 'zero'
|
|
213
|
-
|
|
214
|
-
# -
|
|
@@ -15,20 +15,37 @@ def get_array_derivative(
|
|
|
15
15
|
y_derivative: bool = True,
|
|
16
16
|
boundaries: module.Boundaries = module.Boundaries()) -> np.ndarray:
|
|
17
17
|
"""
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
Compute the 2D gradient of a given array using finite differences.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
array : np.ndarray
|
|
23
|
+
The array for which to compute the nth derivative.
|
|
24
|
+
derivative : int
|
|
25
|
+
The order of the derivative.
|
|
26
|
+
accuracy : int, optional
|
|
27
|
+
The accuracy for the derivative approximation (default is 4).
|
|
28
|
+
dx : float, optional
|
|
29
|
+
The spacing in the x direction (default is 1).
|
|
30
|
+
dy : float, optional
|
|
31
|
+
The spacing in the y direction (default is 1).
|
|
32
|
+
x_derivative : bool, optional
|
|
33
|
+
Whether to compute the derivative in the x direction (default is True).
|
|
34
|
+
y_derivative : bool, optional
|
|
35
|
+
Whether to compute the derivative in the y direction (default is True).
|
|
36
|
+
boundaries : module.Boundaries, optional
|
|
37
|
+
The boundary conditions for the finite difference (default is module.Boundaries()).
|
|
38
|
+
|
|
39
|
+
Returns
|
|
40
|
+
-------
|
|
41
|
+
np.ndarray
|
|
42
|
+
The 2D gradient array computed using finite differences.
|
|
43
|
+
|
|
44
|
+
Examples
|
|
45
|
+
--------
|
|
46
|
+
>>> array = np.array([[1, 2], [3, 4]])
|
|
47
|
+
>>> get_array_derivative(array, derivative=1, dx=0.1, dy=0.1)
|
|
48
|
+
array([[ ... ], [ ... ]])
|
|
32
49
|
"""
|
|
33
50
|
n_x, n_y = array.shape
|
|
34
51
|
|