PyFinitDiff 1.2.3__py3-none-any.whl → 1.3.0__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 +1 -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/__init__.py +6 -6
- 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__/derivative.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__/utils.cpython-311.pyc +0 -0
- 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/__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__/utils.cpython-311.pyc +0 -0
- 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.0.dist-info}/METADATA +1 -1
- PyFinitDiff-1.3.0.dist-info/RECORD +61 -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/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
- PyFinitDiff/finite_difference_1D/__pycache__/derivatives.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/meshes.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__/meshes.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/meshes.py +0 -50
- PyFinitDiff-1.2.3.dist-info/RECORD +0 -65
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.0.dist-info}/LICENSE +0 -0
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.0.dist-info}/WHEEL +0 -0
- {PyFinitDiff-1.2.3.dist-info → PyFinitDiff-1.3.0.dist-info}/top_level.txt +0 -0
|
@@ -1,39 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
2
6
|
from dataclasses import field
|
|
3
|
-
from typing import Optional
|
|
7
|
+
from typing import Optional, List
|
|
4
8
|
from pydantic.dataclasses import dataclass
|
|
5
9
|
from pydantic import ConfigDict
|
|
6
10
|
|
|
7
11
|
|
|
8
|
-
# Local imports
|
|
9
12
|
import numpy
|
|
10
13
|
from PyFinitDiff.triplet import Triplet
|
|
11
|
-
import PyFinitDiff.finite_difference_2D as module
|
|
12
14
|
from PyFinitDiff.finite_difference_2D.utils import MeshInfo
|
|
15
|
+
from PyFinitDiff.finite_difference_2D.boundaries import Boundary
|
|
16
|
+
|
|
17
|
+
config_dict = ConfigDict(
|
|
18
|
+
extra='forbid',
|
|
19
|
+
strict=True,
|
|
20
|
+
kw_only=True,
|
|
21
|
+
frozen=True,
|
|
22
|
+
arbitrary_types_allowed=True
|
|
23
|
+
)
|
|
13
24
|
|
|
14
25
|
|
|
15
|
-
@dataclass(config=
|
|
26
|
+
@dataclass(config=config_dict)
|
|
16
27
|
class Diagonal():
|
|
17
28
|
"""
|
|
18
|
-
This class
|
|
19
|
-
The class can be initialized with different parameters
|
|
29
|
+
This class represents the diagonal elements of the finite-difference method.
|
|
30
|
+
The class can be initialized with different parameters such as its offset or
|
|
20
31
|
boundary condition.
|
|
21
32
|
|
|
33
|
+
Attributes:
|
|
34
|
+
mesh_info (MeshInfo): Instance describing the meta info on the mesh to be considered.
|
|
35
|
+
offset (int): Offset of the column index for the diagonal.
|
|
36
|
+
values (numpy.ndarray): Values associated with the diagonal.
|
|
37
|
+
boundary (Optional[Boundary]): Instance of the boundary used for that diagonal.
|
|
22
38
|
"""
|
|
23
39
|
mesh_info: MeshInfo
|
|
24
|
-
""" Instance describing the meta info on the mesh to be considered. """
|
|
25
40
|
offset: int
|
|
26
|
-
""" Offset of the column index for the diagonal. """
|
|
27
41
|
values: numpy.ndarray
|
|
28
|
-
|
|
29
|
-
boundary: Optional[module.Boundary] = None
|
|
30
|
-
""" Instance of the boundary used for that diagonal. """
|
|
42
|
+
boundary: Optional[Boundary] = None
|
|
31
43
|
|
|
32
44
|
@property
|
|
33
45
|
def triplet(self) -> Triplet:
|
|
34
46
|
"""
|
|
35
|
-
|
|
47
|
+
Returns the Triplet instance of the diagonal.
|
|
36
48
|
|
|
49
|
+
Returns:
|
|
50
|
+
Triplet: The Triplet instance containing the array and shape.
|
|
37
51
|
"""
|
|
38
52
|
self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
|
|
39
53
|
|
|
@@ -46,14 +60,10 @@ class Diagonal():
|
|
|
46
60
|
|
|
47
61
|
def compute_triplet(self) -> None:
|
|
48
62
|
"""
|
|
49
|
-
|
|
63
|
+
Computes the diagonal index and generates a Triplet instance out of it.
|
|
50
64
|
The value of the third triplet column depends on the boundary condition.
|
|
51
|
-
|
|
52
|
-
:returns: No return
|
|
53
|
-
:rtype: None
|
|
54
65
|
"""
|
|
55
66
|
self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
|
|
56
|
-
|
|
57
67
|
self.columns: numpy.ndarray = self.rows + self.offset
|
|
58
68
|
|
|
59
69
|
self.apply_symmetry()
|
|
@@ -62,17 +72,10 @@ class Diagonal():
|
|
|
62
72
|
|
|
63
73
|
def apply_symmetry(self) -> None:
|
|
64
74
|
"""
|
|
65
|
-
|
|
75
|
+
Adjusts the values of the diagonal index as defined by the boundary condition.
|
|
66
76
|
If boundary is symmetric the value stays the same, if anti-symmetric a minus sign
|
|
67
77
|
is added, if zero it returns zero.
|
|
68
78
|
|
|
69
|
-
:param values: The values
|
|
70
|
-
:type values: The initial value
|
|
71
|
-
:param shift_array: The shift array
|
|
72
|
-
:type shift_array: numpy.ndarray
|
|
73
|
-
|
|
74
|
-
:returns: The symmetrized value
|
|
75
|
-
:rtype: float
|
|
76
79
|
"""
|
|
77
80
|
shift_array = self.boundary.get_shift_vector(self.offset)
|
|
78
81
|
|
|
@@ -80,22 +83,17 @@ class Diagonal():
|
|
|
80
83
|
return
|
|
81
84
|
|
|
82
85
|
self.columns = self.columns + 2 * shift_array
|
|
83
|
-
|
|
84
86
|
self.values[shift_array != 0] *= self.boundary.get_factor()
|
|
85
87
|
|
|
86
88
|
def validate_index(self) -> None:
|
|
87
89
|
"""
|
|
88
|
-
Removes all negative
|
|
90
|
+
Removes all negative indices.
|
|
89
91
|
|
|
90
|
-
:returns: No return
|
|
91
|
-
:rtype: None
|
|
92
92
|
"""
|
|
93
93
|
valid_index = self.columns >= 0
|
|
94
94
|
|
|
95
95
|
self.columns = self.columns[valid_index]
|
|
96
|
-
|
|
97
96
|
self.rows = self.rows[valid_index]
|
|
98
|
-
|
|
99
97
|
self.values = self.values[valid_index]
|
|
100
98
|
|
|
101
99
|
def plot(self) -> None:
|
|
@@ -107,7 +105,7 @@ class Diagonal():
|
|
|
107
105
|
|
|
108
106
|
|
|
109
107
|
class ConstantDiagonal(Diagonal):
|
|
110
|
-
def __init__(self, offset: int, value: float, mesh_info: list, boundary:
|
|
108
|
+
def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
|
|
111
109
|
super().__init__(
|
|
112
110
|
offset=offset,
|
|
113
111
|
mesh_info=mesh_info,
|
|
@@ -116,144 +114,202 @@ class ConstantDiagonal(Diagonal):
|
|
|
116
114
|
)
|
|
117
115
|
|
|
118
116
|
|
|
119
|
-
@dataclass
|
|
117
|
+
@dataclass(config=config_dict)
|
|
120
118
|
class DiagonalSet():
|
|
121
|
-
|
|
122
|
-
diagonals
|
|
119
|
+
"""
|
|
120
|
+
This class represents a set of diagonals for the finite-difference method. It provides
|
|
121
|
+
various utilities for manipulating and analyzing the set of diagonals.
|
|
123
122
|
|
|
124
|
-
|
|
123
|
+
Attributes:
|
|
124
|
+
mesh_info (object): Meta information about the mesh.
|
|
125
|
+
diagonals (List[Diagonal]): List of Diagonal objects.
|
|
126
|
+
"""
|
|
127
|
+
mesh_info: MeshInfo
|
|
128
|
+
diagonals: List[Diagonal] = field(default_factory=list)
|
|
129
|
+
|
|
130
|
+
def append(self, diagonal: Diagonal) -> DiagonalSet:
|
|
131
|
+
"""
|
|
132
|
+
Appends a Diagonal to the set.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
diagonal (Diagonal): The diagonal to append.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
DiagonalSet: The updated DiagonalSet.
|
|
139
|
+
"""
|
|
125
140
|
self.diagonals.append(diagonal)
|
|
126
141
|
return self
|
|
127
142
|
|
|
128
|
-
def concatenate(self, other_diagonal_set:
|
|
129
|
-
|
|
143
|
+
def concatenate(self, other_diagonal_set: DiagonalSet) -> DiagonalSet:
|
|
144
|
+
"""
|
|
145
|
+
Concatenates another DiagonalSet to this one.
|
|
130
146
|
|
|
147
|
+
Args:
|
|
148
|
+
other_diagonal_set (DiagonalSet): The other DiagonalSet to concatenate.
|
|
149
|
+
|
|
150
|
+
Returns:
|
|
151
|
+
DiagonalSet: The updated DiagonalSet.
|
|
152
|
+
"""
|
|
153
|
+
self.diagonals += other_diagonal_set.diagonals
|
|
131
154
|
return self
|
|
132
155
|
|
|
133
156
|
def get_row_nan_bool(self) -> numpy.ndarray:
|
|
134
157
|
"""
|
|
135
|
-
Returns a
|
|
158
|
+
Returns a boolean array with True where the associated rows have a NaN value.
|
|
136
159
|
|
|
137
|
-
:
|
|
138
|
-
|
|
160
|
+
Returns:
|
|
161
|
+
numpy.ndarray: Boolean array indicating NaN rows.
|
|
139
162
|
"""
|
|
140
163
|
nan_index = self.get_nan_index()
|
|
141
|
-
|
|
142
164
|
nan_rows = self.triplet.rows[nan_index]
|
|
143
165
|
nan_index = numpy.isin(self.triplet.rows, nan_rows)
|
|
144
|
-
|
|
145
166
|
return nan_index
|
|
146
167
|
|
|
147
168
|
def get_list_of_nan_rows(self) -> numpy.ndarray:
|
|
148
|
-
|
|
169
|
+
"""
|
|
170
|
+
Gets a list of rows containing NaN values.
|
|
149
171
|
|
|
172
|
+
Returns:
|
|
173
|
+
numpy.ndarray: Array of rows with NaN values.
|
|
174
|
+
"""
|
|
175
|
+
nan_index = numpy.isnan(self.triplet.values)
|
|
150
176
|
nan_rows = self.triplet.rows[nan_index]
|
|
151
|
-
|
|
152
177
|
return numpy.unique(nan_rows)
|
|
153
178
|
|
|
154
179
|
def get_list_of_not_nan_rows(self) -> numpy.ndarray:
|
|
155
180
|
"""
|
|
156
|
-
Gets
|
|
181
|
+
Gets a list of rows without NaN values.
|
|
157
182
|
|
|
158
|
-
:
|
|
159
|
-
|
|
183
|
+
Returns:
|
|
184
|
+
numpy.ndarray: Array of rows without NaN values.
|
|
160
185
|
"""
|
|
161
186
|
nan_rows = self.get_list_of_nan_rows()
|
|
162
|
-
|
|
163
187
|
non_nan_index = ~numpy.isin(self.triplet.rows, nan_rows)
|
|
164
|
-
|
|
165
188
|
non_nan_rows = self.triplet.rows[non_nan_index]
|
|
166
|
-
|
|
167
189
|
return non_nan_rows
|
|
168
190
|
|
|
169
191
|
def get_nan_index(self) -> numpy.ndarray:
|
|
192
|
+
"""
|
|
193
|
+
Gets a boolean array indicating the indices of NaN values in the triplet.
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
numpy.ndarray: Boolean array indicating NaN indices.
|
|
197
|
+
"""
|
|
170
198
|
return numpy.isnan(self.triplet.values)
|
|
171
199
|
|
|
172
200
|
def get_array_from_rows(self, rows: numpy.ndarray) -> numpy.ndarray:
|
|
173
201
|
"""
|
|
174
|
-
Returns the array
|
|
202
|
+
Returns the array elements for the specified rows.
|
|
175
203
|
|
|
176
|
-
:
|
|
177
|
-
|
|
204
|
+
Args:
|
|
205
|
+
rows (numpy.ndarray): The rows to search for.
|
|
178
206
|
|
|
179
|
-
:
|
|
180
|
-
|
|
207
|
+
Returns:
|
|
208
|
+
numpy.ndarray: The array elements corresponding to the specified rows.
|
|
181
209
|
"""
|
|
182
210
|
rows_index = numpy.isin(self.triplet.rows, rows)
|
|
183
|
-
|
|
184
211
|
return self.triplet.array[rows_index]
|
|
185
212
|
|
|
186
|
-
def remove_nan_rows(self) ->
|
|
187
|
-
|
|
213
|
+
def remove_nan_rows(self) -> DiagonalSet:
|
|
214
|
+
"""
|
|
215
|
+
Removes rows containing NaN values from the triplet.
|
|
188
216
|
|
|
217
|
+
Returns:
|
|
218
|
+
DiagonalSet: The updated DiagonalSet with NaN rows removed.
|
|
219
|
+
"""
|
|
220
|
+
nan_rows = self.get_row_nan_bool()
|
|
189
221
|
return self.remove_rows(rows=nan_rows)
|
|
190
222
|
|
|
191
|
-
def remove_rows(self, rows: numpy.ndarray) ->
|
|
192
|
-
|
|
223
|
+
def remove_rows(self, rows: numpy.ndarray) -> DiagonalSet:
|
|
224
|
+
"""
|
|
225
|
+
Removes the specified rows from the triplet.
|
|
193
226
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
obj=index_to_remove,
|
|
197
|
-
axis=0
|
|
198
|
-
)
|
|
227
|
+
Args:
|
|
228
|
+
rows (numpy.ndarray): The rows to remove.
|
|
199
229
|
|
|
230
|
+
Returns:
|
|
231
|
+
DiagonalSet: The updated DiagonalSet with the specified rows removed.
|
|
232
|
+
"""
|
|
233
|
+
index_to_remove = numpy.isin(self.triplet.rows, rows)
|
|
234
|
+
self.triplet.array = numpy.delete(self.triplet.array, index_to_remove, axis=0)
|
|
200
235
|
return self
|
|
201
236
|
|
|
202
|
-
def replace_nan_rows_with(self, other:
|
|
237
|
+
def replace_nan_rows_with(self, other: DiagonalSet) -> DiagonalSet:
|
|
203
238
|
"""
|
|
204
|
-
|
|
239
|
+
Replaces NaN rows in this DiagonalSet with the equivalent rows from another DiagonalSet.
|
|
205
240
|
|
|
206
|
-
:
|
|
207
|
-
|
|
241
|
+
Args:
|
|
242
|
+
other (DiagonalSet): The other DiagonalSet to source non-NaN rows from.
|
|
208
243
|
|
|
209
|
-
:
|
|
210
|
-
|
|
244
|
+
Returns:
|
|
245
|
+
DiagonalSet: The updated DiagonalSet with NaN rows replaced.
|
|
211
246
|
"""
|
|
212
247
|
self_nan_rows = self.get_list_of_nan_rows()
|
|
213
|
-
|
|
214
248
|
other_not_nan_rows = other.get_list_of_not_nan_rows()
|
|
215
|
-
|
|
216
249
|
replace_rows = numpy.intersect1d(self_nan_rows, other_not_nan_rows)
|
|
217
|
-
|
|
218
250
|
self.remove_rows(replace_rows)
|
|
219
|
-
|
|
220
251
|
add_array = other.get_array_from_rows(self_nan_rows)
|
|
221
|
-
|
|
222
252
|
self.triplet.append_array(add_array)
|
|
253
|
+
return self
|
|
254
|
+
|
|
255
|
+
def initialize_triplet(self) -> DiagonalSet:
|
|
256
|
+
"""
|
|
257
|
+
Initializes the triplet by combining all diagonals in the set.
|
|
223
258
|
|
|
224
|
-
|
|
259
|
+
Returns:
|
|
260
|
+
DiagonalSet: The initialized DiagonalSet.
|
|
261
|
+
"""
|
|
225
262
|
triplet = Triplet(
|
|
226
|
-
array=[0, 0, 0],
|
|
263
|
+
array=numpy.array([0, 0, 0]),
|
|
227
264
|
shape=self.mesh_info.shape
|
|
228
265
|
)
|
|
229
|
-
|
|
230
266
|
for diagonal in self.diagonals:
|
|
231
267
|
diagonal.compute_triplet()
|
|
232
268
|
triplet += diagonal.triplet
|
|
233
|
-
|
|
234
269
|
self.triplet = triplet
|
|
235
|
-
|
|
236
270
|
return self
|
|
237
271
|
|
|
238
272
|
def get_lowest_nan(self) -> int:
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
rows = self.triplet[nan_index]
|
|
273
|
+
"""
|
|
274
|
+
Gets the lowest row index containing a NaN value.
|
|
242
275
|
|
|
276
|
+
Returns:
|
|
277
|
+
int: The lowest row index with a NaN value.
|
|
278
|
+
"""
|
|
279
|
+
nan_index = self.get_nan_index()
|
|
280
|
+
rows = self.triplet.rows[nan_index]
|
|
243
281
|
return rows.min()
|
|
244
282
|
|
|
245
283
|
def get_highest_nan(self) -> int:
|
|
284
|
+
"""
|
|
285
|
+
Gets the highest row index containing a NaN value.
|
|
286
|
+
|
|
287
|
+
Returns:
|
|
288
|
+
int: The highest row index with a NaN value.
|
|
289
|
+
"""
|
|
246
290
|
nan_index = self.get_nan_index()
|
|
291
|
+
rows = self.triplet.rows[nan_index]
|
|
292
|
+
return rows.max()
|
|
247
293
|
|
|
248
|
-
|
|
294
|
+
def __add__(self, other: DiagonalSet) -> DiagonalSet:
|
|
295
|
+
"""
|
|
296
|
+
Adds another DiagonalSet to this one.
|
|
249
297
|
|
|
250
|
-
|
|
298
|
+
Args:
|
|
299
|
+
other (DiagonalSet): The other DiagonalSet to add.
|
|
251
300
|
|
|
252
|
-
|
|
301
|
+
Returns:
|
|
302
|
+
DiagonalSet: The updated DiagonalSet.
|
|
303
|
+
"""
|
|
253
304
|
self.diagonals += other.diagonals
|
|
254
|
-
|
|
255
305
|
return self
|
|
256
306
|
|
|
257
|
-
def plot(self):
|
|
307
|
+
def plot(self) -> None:
|
|
308
|
+
"""
|
|
309
|
+
Plots the Triplet instance.
|
|
310
|
+
|
|
311
|
+
Returns:
|
|
312
|
+
None
|
|
313
|
+
"""
|
|
258
314
|
return self.triplet.plot()
|
|
259
315
|
# -
|
|
@@ -1,39 +1,58 @@
|
|
|
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
|
|
7
9
|
from PyFinitDiff.triplet import Triplet
|
|
10
|
+
from PyFinitDiff.finite_difference_2D.boundaries import Boundaries
|
|
11
|
+
from PyFinitDiff.finite_difference_2D.utils import MeshInfo
|
|
12
|
+
from PyFinitDiff.finite_difference_2D.diagonals import DiagonalSet, ConstantDiagonal
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
config_dict = ConfigDict(
|
|
16
|
+
extra='forbid',
|
|
17
|
+
strict=True,
|
|
18
|
+
arbitrary_types_allowed=True,
|
|
19
|
+
kw_only=True,
|
|
20
|
+
frozen=True
|
|
21
|
+
)
|
|
8
22
|
|
|
9
23
|
|
|
10
|
-
@dataclass
|
|
24
|
+
@dataclass(config=config_dict)
|
|
11
25
|
class FiniteDifference():
|
|
12
26
|
"""
|
|
13
|
-
This class
|
|
14
|
-
|
|
27
|
+
This class represents a specific finite difference configuration, which is defined
|
|
28
|
+
with the discretization of the mesh, the derivative order, accuracy, and the boundary
|
|
29
|
+
conditions that are defined. More information is provided at the following link:
|
|
30
|
+
'math.toronto.edu/mpugh/Teaching/Mat1062/notes2.pdf'
|
|
31
|
+
|
|
32
|
+
Attributes:
|
|
33
|
+
n_x (int): Number of points in the x direction.
|
|
34
|
+
n_y (int): Number of points in the y direction.
|
|
35
|
+
dx (float): Infinitesimal displacement in the x direction. Defaults to 1.
|
|
36
|
+
dy (float): Infinitesimal displacement in the y direction. Defaults to 1.
|
|
37
|
+
derivative (int): Derivative order to convert into finite-difference matrix. Defaults to 1.
|
|
38
|
+
accuracy (int): Accuracy of the derivative approximation (error is inversely proportional to the power of this value). Defaults to 2.
|
|
39
|
+
boundaries (Boundaries): Values of the four possible boundaries of the system.
|
|
40
|
+
x_derivative (bool): Add the x derivative. Defaults to True.
|
|
41
|
+
y_derivative (bool): Add the y derivative. Defaults to True.
|
|
15
42
|
"""
|
|
16
43
|
n_x: int
|
|
17
|
-
""" Number of point in the x direction """
|
|
18
44
|
n_y: int
|
|
19
|
-
""" Number of point in the y direction """
|
|
20
45
|
dx: float = 1
|
|
21
|
-
""" Infinetisemal displacement in x direction """
|
|
22
46
|
dy: float = 1
|
|
23
|
-
""" Infinetisemal displacement in y direction """
|
|
24
47
|
derivative: int = 1
|
|
25
|
-
""" Derivative order to convert into finit-difference matrix. """
|
|
26
48
|
accuracy: int = 2
|
|
27
|
-
|
|
28
|
-
boundaries: module.Boundaries = field(default_factory=module.Boundaries())
|
|
29
|
-
""" Values of the four possible boundaries of the system. """
|
|
49
|
+
boundaries: Boundaries = field(default_factory=Boundaries)
|
|
30
50
|
x_derivative: bool = True
|
|
31
|
-
""" Add the x derivative """
|
|
32
51
|
y_derivative: bool = True
|
|
33
|
-
""" Add the y derivative """
|
|
34
52
|
|
|
35
53
|
def __post_init__(self):
|
|
36
|
-
|
|
54
|
+
"""Post-initialization to set up mesh info and finite coefficients."""
|
|
55
|
+
self.mesh_info = MeshInfo(
|
|
37
56
|
n_x=self.n_x,
|
|
38
57
|
n_y=self.n_y,
|
|
39
58
|
dx=self.dx,
|
|
@@ -41,7 +60,7 @@ class FiniteDifference():
|
|
|
41
60
|
)
|
|
42
61
|
self.boundaries.mesh_info = self.mesh_info
|
|
43
62
|
|
|
44
|
-
self.
|
|
63
|
+
self.finite_coefficient = FiniteCoefficients(
|
|
45
64
|
derivative=self.derivative,
|
|
46
65
|
accuracy=self.accuracy
|
|
47
66
|
)
|
|
@@ -49,14 +68,17 @@ class FiniteDifference():
|
|
|
49
68
|
|
|
50
69
|
@property
|
|
51
70
|
def shape(self):
|
|
71
|
+
"""Returns the shape of the mesh as a tuple."""
|
|
52
72
|
return (self.n_x, self.n_y)
|
|
53
73
|
|
|
54
74
|
@property
|
|
55
75
|
def triplet(self):
|
|
56
76
|
"""
|
|
57
|
-
Triplet representing the non-
|
|
77
|
+
Triplet representing the non-null values of the specific
|
|
58
78
|
finite-difference configuration.
|
|
59
79
|
|
|
80
|
+
Returns:
|
|
81
|
+
Triplet: The Triplet instance containing the array and shape.
|
|
60
82
|
"""
|
|
61
83
|
if not self._triplet:
|
|
62
84
|
self.construct_triplet()
|
|
@@ -64,10 +86,12 @@ class FiniteDifference():
|
|
|
64
86
|
|
|
65
87
|
@property
|
|
66
88
|
def _dx(self) -> float:
|
|
89
|
+
"""Returns the dx value raised to the power of the derivative order."""
|
|
67
90
|
return self.dx ** self.derivative
|
|
68
91
|
|
|
69
92
|
@property
|
|
70
93
|
def _dy(self) -> float:
|
|
94
|
+
"""Returns the dy value raised to the power of the derivative order."""
|
|
71
95
|
return self.dy ** self.derivative
|
|
72
96
|
|
|
73
97
|
def iterate_central_coefficient(
|
|
@@ -75,46 +99,38 @@ class FiniteDifference():
|
|
|
75
99
|
coefficients: str,
|
|
76
100
|
offset_multiplier: int) -> tuple:
|
|
77
101
|
"""
|
|
78
|
-
Iterate
|
|
102
|
+
Iterate through the given type coefficients.
|
|
79
103
|
|
|
80
|
-
:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
:type offset_multiplier: int
|
|
104
|
+
Args:
|
|
105
|
+
coefficients (str): The coefficient type.
|
|
106
|
+
offset_multiplier (int): The offset multiplier.
|
|
84
107
|
|
|
85
|
-
:
|
|
86
|
-
|
|
108
|
+
Yields:
|
|
109
|
+
tuple: The offset, value, and boundary.
|
|
87
110
|
"""
|
|
88
111
|
for offset, value in coefficients:
|
|
89
112
|
offset *= offset_multiplier
|
|
90
|
-
|
|
91
113
|
boundary = self.boundaries.offset_to_boundary(offset=offset)
|
|
92
|
-
|
|
93
114
|
yield offset, value, boundary
|
|
94
115
|
|
|
95
116
|
def _add_diagonal_coefficient(
|
|
96
117
|
self,
|
|
97
118
|
coefficient_type: str,
|
|
98
119
|
offset_multiplier: int,
|
|
99
|
-
delta: float) ->
|
|
120
|
+
delta: float) -> 'DiagonalSet':
|
|
100
121
|
"""
|
|
101
122
|
Adds a diagonal coefficient to the list of diagonals.
|
|
102
123
|
|
|
103
|
-
:
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
:param offset_multiplier: The offset multiplier
|
|
108
|
-
:type offset_multiplier: int
|
|
109
|
-
:param delta: The delta
|
|
110
|
-
:type delta: float
|
|
111
|
-
|
|
112
|
-
:returns: No return
|
|
113
|
-
:rtype: None
|
|
114
|
-
"""
|
|
115
|
-
diagonal_set = module.DiagonalSet(mesh_info=self.mesh_info)
|
|
124
|
+
Args:
|
|
125
|
+
coefficient_type (str): The coefficient type.
|
|
126
|
+
offset_multiplier (int): The offset multiplier.
|
|
127
|
+
delta (float): The delta value.
|
|
116
128
|
|
|
117
|
-
|
|
129
|
+
Returns:
|
|
130
|
+
DiagonalSet: The diagonal set with added coefficients.
|
|
131
|
+
"""
|
|
132
|
+
diagonal_set = DiagonalSet(mesh_info=self.mesh_info)
|
|
133
|
+
coefficients = getattr(self.finite_coefficient, coefficient_type)
|
|
118
134
|
|
|
119
135
|
iterator = self.iterate_central_coefficient(
|
|
120
136
|
coefficients=coefficients,
|
|
@@ -122,31 +138,28 @@ class FiniteDifference():
|
|
|
122
138
|
)
|
|
123
139
|
|
|
124
140
|
for offset, value, boundary in iterator:
|
|
125
|
-
diagonal =
|
|
141
|
+
diagonal = ConstantDiagonal(
|
|
126
142
|
mesh_info=self.mesh_info,
|
|
127
143
|
offset=offset,
|
|
128
144
|
boundary=boundary,
|
|
129
145
|
value=value / delta,
|
|
130
146
|
)
|
|
131
|
-
|
|
132
147
|
diagonal_set.append(diagonal)
|
|
133
148
|
|
|
134
149
|
diagonal_set.initialize_triplet()
|
|
135
|
-
|
|
136
150
|
return diagonal_set
|
|
137
151
|
|
|
138
|
-
def get_diagonal_set_full(self, offset_multiplier: int, delta: float) ->
|
|
152
|
+
def get_diagonal_set_full(self, offset_multiplier: int, delta: float) -> 'DiagonalSet':
|
|
139
153
|
"""
|
|
140
|
-
Constructs and returns the central
|
|
154
|
+
Constructs and returns the central coefficients diagonals which is completed with
|
|
141
155
|
forward and backward coefficients if some 'nan' values are left.
|
|
142
156
|
|
|
143
|
-
:
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
:type delta: float
|
|
157
|
+
Args:
|
|
158
|
+
offset_multiplier (int): The offset multiplier.
|
|
159
|
+
delta (float): The delta value.
|
|
147
160
|
|
|
148
|
-
:
|
|
149
|
-
|
|
161
|
+
Returns:
|
|
162
|
+
DiagonalSet: The full set of diagonal coefficients.
|
|
150
163
|
"""
|
|
151
164
|
central_diagonal = self._add_diagonal_coefficient(
|
|
152
165
|
coefficient_type='central',
|
|
@@ -167,12 +180,12 @@ class FiniteDifference():
|
|
|
167
180
|
)
|
|
168
181
|
|
|
169
182
|
central_diagonal.replace_nan_rows_with(forward_diagonal)
|
|
170
|
-
|
|
171
183
|
central_diagonal.replace_nan_rows_with(backward_diagonal)
|
|
172
184
|
|
|
173
185
|
return central_diagonal
|
|
174
186
|
|
|
175
187
|
def construct_triplet(self) -> None:
|
|
188
|
+
"""Constructs the Triplet instance for the finite-difference configuration."""
|
|
176
189
|
self._triplet = Triplet(array=[[0, 0, 0]], shape=self.mesh_info.shape)
|
|
177
190
|
|
|
178
191
|
if self.x_derivative:
|
|
@@ -180,7 +193,6 @@ class FiniteDifference():
|
|
|
180
193
|
offset_multiplier=1,
|
|
181
194
|
delta=self._dx
|
|
182
195
|
)
|
|
183
|
-
|
|
184
196
|
self._triplet += x_diagonals.triplet
|
|
185
197
|
|
|
186
198
|
if self.y_derivative:
|
|
@@ -188,7 +200,6 @@ class FiniteDifference():
|
|
|
188
200
|
offset_multiplier=self.n_x,
|
|
189
201
|
delta=self._dy
|
|
190
202
|
)
|
|
191
|
-
|
|
192
203
|
self._triplet += y_diagonals.triplet
|
|
193
204
|
|
|
194
205
|
|