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