PyFinitDiff 2.2.0__py3-none-any.whl → 2.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/__init__.py +1 -0
- PyFinitDiff/_version.py +16 -3
- PyFinitDiff/boundary_values.py +43 -0
- PyFinitDiff/{finite_difference_2D/diagonals.py → diagonal.py} +48 -52
- PyFinitDiff/finite_difference_1D/boundaries.py +25 -53
- PyFinitDiff/finite_difference_1D/finite_difference.py +1 -1
- PyFinitDiff/finite_difference_2D/boundaries.py +42 -68
- PyFinitDiff/finite_difference_2D/finite_difference.py +1 -1
- {pyfinitdiff-2.2.0.dist-info → pyfinitdiff-2.3.0.dist-info}/METADATA +1 -1
- {pyfinitdiff-2.2.0.dist-info → pyfinitdiff-2.3.0.dist-info}/RECORD +13 -14
- PyFinitDiff/finite_difference_1D/diagonals.py +0 -324
- PyFinitDiff/utils.py +0 -106
- {pyfinitdiff-2.2.0.dist-info → pyfinitdiff-2.3.0.dist-info}/WHEEL +0 -0
- {pyfinitdiff-2.2.0.dist-info → pyfinitdiff-2.3.0.dist-info}/licenses/LICENSE +0 -0
- {pyfinitdiff-2.2.0.dist-info → pyfinitdiff-2.3.0.dist-info}/top_level.txt +0 -0
PyFinitDiff/__init__.py
CHANGED
PyFinitDiff/_version.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
# file generated by setuptools-scm
|
|
2
2
|
# don't change, don't track in version control
|
|
3
3
|
|
|
4
|
-
__all__ = [
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
5
12
|
|
|
6
13
|
TYPE_CHECKING = False
|
|
7
14
|
if TYPE_CHECKING:
|
|
@@ -9,13 +16,19 @@ if TYPE_CHECKING:
|
|
|
9
16
|
from typing import Union
|
|
10
17
|
|
|
11
18
|
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
12
20
|
else:
|
|
13
21
|
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
14
23
|
|
|
15
24
|
version: str
|
|
16
25
|
__version__: str
|
|
17
26
|
__version_tuple__: VERSION_TUPLE
|
|
18
27
|
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
19
30
|
|
|
20
|
-
__version__ = version = '2.
|
|
21
|
-
__version_tuple__ = version_tuple = (2,
|
|
31
|
+
__version__ = version = '2.3.0'
|
|
32
|
+
__version_tuple__ = version_tuple = (2, 3, 0)
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = None
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
from enum import Enum
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BoundaryValue(Enum):
|
|
8
|
+
"""
|
|
9
|
+
Enumeration for boundary values.
|
|
10
|
+
"""
|
|
11
|
+
SYMMETRIC = 'symmetric'
|
|
12
|
+
ANTI_SYMMETRIC = 'anti-symmetric'
|
|
13
|
+
ZERO = 'zero'
|
|
14
|
+
NONE = 'none'
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def from_string(cls, value: str) -> 'BoundaryValue':
|
|
18
|
+
"""
|
|
19
|
+
Convert a string to a BoundaryValue enum.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
value : str
|
|
24
|
+
String value to convert.
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
BoundaryValue
|
|
29
|
+
The corresponding enum value.
|
|
30
|
+
|
|
31
|
+
Raises
|
|
32
|
+
------
|
|
33
|
+
ValueError
|
|
34
|
+
If the string doesn't correspond to a valid BoundaryValue.
|
|
35
|
+
"""
|
|
36
|
+
for boundary_val in cls:
|
|
37
|
+
if boundary_val.value == value:
|
|
38
|
+
return boundary_val
|
|
39
|
+
raise ValueError(f"No BoundaryValue found for string: '{value}'")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
@@ -1,59 +1,45 @@
|
|
|
1
1
|
#!/usr/bin/env python
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
|
-
from
|
|
5
|
-
|
|
6
|
-
from dataclasses import field
|
|
7
|
-
from typing import Optional, List
|
|
8
|
-
from pydantic.dataclasses import dataclass
|
|
9
|
-
from pydantic import ConfigDict
|
|
4
|
+
from dataclasses import dataclass
|
|
10
5
|
|
|
6
|
+
# Local imports
|
|
11
7
|
import numpy
|
|
12
8
|
from PyFinitDiff.triplet import Triplet
|
|
13
|
-
from PyFinitDiff.finite_difference_2D.utils import MeshInfo
|
|
14
|
-
from PyFinitDiff.finite_difference_2D.boundaries import Boundary
|
|
15
|
-
|
|
16
|
-
config_dict = ConfigDict(
|
|
17
|
-
extra='forbid',
|
|
18
|
-
strict=True,
|
|
19
|
-
kw_only=True,
|
|
20
|
-
frozen=False,
|
|
21
|
-
arbitrary_types_allowed=True
|
|
22
|
-
)
|
|
23
9
|
|
|
24
10
|
|
|
25
|
-
@dataclass
|
|
11
|
+
@dataclass
|
|
26
12
|
class Diagonal:
|
|
27
13
|
"""
|
|
28
|
-
Represents
|
|
14
|
+
Represents a diagonal element of the finite-difference method.
|
|
29
15
|
|
|
30
|
-
This class
|
|
16
|
+
This class constructs a diagonal with parameters such as its offset and boundary condition.
|
|
31
17
|
|
|
32
18
|
Parameters
|
|
33
19
|
----------
|
|
34
|
-
mesh_info :
|
|
20
|
+
mesh_info : object
|
|
35
21
|
Instance describing the meta information on the mesh to be considered.
|
|
36
22
|
offset : int
|
|
37
23
|
Offset of the column index for the diagonal.
|
|
38
|
-
values :
|
|
39
|
-
|
|
40
|
-
boundary :
|
|
41
|
-
Instance of the boundary used for
|
|
24
|
+
values : float
|
|
25
|
+
Value associated with the diagonal.
|
|
26
|
+
boundary : object
|
|
27
|
+
Instance of the boundary used for the diagonal.
|
|
42
28
|
"""
|
|
43
|
-
mesh_info:
|
|
29
|
+
mesh_info: object
|
|
44
30
|
offset: int
|
|
45
|
-
values:
|
|
46
|
-
boundary:
|
|
31
|
+
values: float
|
|
32
|
+
boundary: object
|
|
47
33
|
|
|
48
34
|
@property
|
|
49
35
|
def triplet(self) -> Triplet:
|
|
50
36
|
"""
|
|
51
|
-
|
|
37
|
+
Return the Triplet instance of the diagonal.
|
|
52
38
|
|
|
53
39
|
Returns
|
|
54
40
|
-------
|
|
55
41
|
Triplet
|
|
56
|
-
|
|
42
|
+
A Triplet instance representing the non-zero values in the diagonal.
|
|
57
43
|
"""
|
|
58
44
|
self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
|
|
59
45
|
|
|
@@ -66,9 +52,9 @@ class Diagonal:
|
|
|
66
52
|
|
|
67
53
|
def compute_triplet(self) -> None:
|
|
68
54
|
"""
|
|
69
|
-
|
|
55
|
+
Compute the diagonal indices and generate a Triplet instance out of it.
|
|
70
56
|
|
|
71
|
-
The value of the third triplet
|
|
57
|
+
The value of the third column in the triplet depends on the boundary condition.
|
|
72
58
|
"""
|
|
73
59
|
self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
|
|
74
60
|
self.columns: numpy.ndarray = self.rows + self.offset
|
|
@@ -77,10 +63,10 @@ class Diagonal:
|
|
|
77
63
|
|
|
78
64
|
def apply_symmetry(self) -> None:
|
|
79
65
|
"""
|
|
80
|
-
|
|
66
|
+
Apply boundary symmetry conditions to the diagonal.
|
|
81
67
|
|
|
82
|
-
If the boundary is symmetric, the value stays the same. If anti-symmetric,
|
|
83
|
-
If zero,
|
|
68
|
+
If the boundary is symmetric, the value stays the same. If it is anti-symmetric,
|
|
69
|
+
a negative sign is added. If it is zero, the value becomes zero.
|
|
84
70
|
"""
|
|
85
71
|
shift_array = self.boundary.get_shift_vector(self.offset)
|
|
86
72
|
|
|
@@ -92,7 +78,7 @@ class Diagonal:
|
|
|
92
78
|
|
|
93
79
|
def validate_index(self) -> None:
|
|
94
80
|
"""
|
|
95
|
-
|
|
81
|
+
Remove all negative indices from the diagonal.
|
|
96
82
|
"""
|
|
97
83
|
valid_index = self.columns >= 0
|
|
98
84
|
self.columns = self.columns[valid_index]
|
|
@@ -101,18 +87,17 @@ class Diagonal:
|
|
|
101
87
|
|
|
102
88
|
def plot(self) -> None:
|
|
103
89
|
"""
|
|
104
|
-
|
|
90
|
+
Plot the Triplet instance representing the diagonal.
|
|
105
91
|
"""
|
|
106
92
|
return self.triplet.plot()
|
|
107
93
|
|
|
108
|
-
|
|
109
94
|
class ConstantDiagonal(Diagonal):
|
|
110
95
|
"""
|
|
111
|
-
Represents a diagonal with constant values
|
|
96
|
+
Represents a diagonal with constant values.
|
|
112
97
|
|
|
113
98
|
This subclass of Diagonal is initialized with constant values for the entire diagonal.
|
|
114
99
|
"""
|
|
115
|
-
def __init__(self, offset: int, value: float, mesh_info: list, boundary:
|
|
100
|
+
def __init__(self, offset: int, value: float, mesh_info: list, boundary: object):
|
|
116
101
|
super().__init__(
|
|
117
102
|
offset=offset,
|
|
118
103
|
mesh_info=mesh_info,
|
|
@@ -121,7 +106,6 @@ class ConstantDiagonal(Diagonal):
|
|
|
121
106
|
)
|
|
122
107
|
|
|
123
108
|
|
|
124
|
-
@dataclass(config=config_dict)
|
|
125
109
|
class DiagonalSet:
|
|
126
110
|
"""
|
|
127
111
|
Represents a set of diagonals for the finite-difference method.
|
|
@@ -135,10 +119,22 @@ class DiagonalSet:
|
|
|
135
119
|
diagonals : List[Diagonal]
|
|
136
120
|
List of Diagonal objects.
|
|
137
121
|
"""
|
|
138
|
-
mesh_info: MeshInfo
|
|
139
|
-
diagonals: List[Diagonal] = field(default_factory=list)
|
|
140
122
|
|
|
141
|
-
def
|
|
123
|
+
def __init__(self, mesh_info: object, diagonals: list = None) -> None:
|
|
124
|
+
"""
|
|
125
|
+
Initializes the DiagonalSet with mesh information and an optional list of diagonals.
|
|
126
|
+
|
|
127
|
+
Parameters
|
|
128
|
+
----------
|
|
129
|
+
mesh_info : object
|
|
130
|
+
Meta information about the mesh.
|
|
131
|
+
diagonals : list, optional
|
|
132
|
+
List of Diagonal objects, defaults to an empty list.
|
|
133
|
+
"""
|
|
134
|
+
self.mesh_info = mesh_info
|
|
135
|
+
self.diagonals = diagonals if diagonals is not None else []
|
|
136
|
+
|
|
137
|
+
def append(self, diagonal: Diagonal) -> 'DiagonalSet':
|
|
142
138
|
"""
|
|
143
139
|
Appends a Diagonal to the set.
|
|
144
140
|
|
|
@@ -155,13 +151,13 @@ class DiagonalSet:
|
|
|
155
151
|
self.diagonals.append(diagonal)
|
|
156
152
|
return self
|
|
157
153
|
|
|
158
|
-
def concatenate(self, other_diagonal_set: DiagonalSet) -> DiagonalSet:
|
|
154
|
+
def concatenate(self, other_diagonal_set: 'DiagonalSet') -> 'DiagonalSet':
|
|
159
155
|
"""
|
|
160
156
|
Concatenates another DiagonalSet to this one.
|
|
161
157
|
|
|
162
158
|
Parameters
|
|
163
159
|
----------
|
|
164
|
-
other_diagonal_set : DiagonalSet
|
|
160
|
+
other_diagonal_set : 'DiagonalSet'
|
|
165
161
|
The other DiagonalSet to concatenate.
|
|
166
162
|
|
|
167
163
|
Returns
|
|
@@ -241,7 +237,7 @@ class DiagonalSet:
|
|
|
241
237
|
rows_index = numpy.isin(self.triplet.rows, rows)
|
|
242
238
|
return self.triplet.array[rows_index]
|
|
243
239
|
|
|
244
|
-
def remove_nan_rows(self) -> DiagonalSet:
|
|
240
|
+
def remove_nan_rows(self) -> 'DiagonalSet':
|
|
245
241
|
"""
|
|
246
242
|
Removes rows containing NaN values from the triplet.
|
|
247
243
|
|
|
@@ -253,7 +249,7 @@ class DiagonalSet:
|
|
|
253
249
|
nan_rows = self.get_row_nan_bool()
|
|
254
250
|
return self.remove_rows(rows=nan_rows)
|
|
255
251
|
|
|
256
|
-
def remove_rows(self, rows: numpy.ndarray) -> DiagonalSet:
|
|
252
|
+
def remove_rows(self, rows: numpy.ndarray) -> 'DiagonalSet':
|
|
257
253
|
"""
|
|
258
254
|
Removes the specified rows from the triplet.
|
|
259
255
|
|
|
@@ -271,13 +267,13 @@ class DiagonalSet:
|
|
|
271
267
|
self.triplet.array = numpy.delete(self.triplet.array, index_to_remove, axis=0)
|
|
272
268
|
return self
|
|
273
269
|
|
|
274
|
-
def replace_nan_rows_with(self, other: DiagonalSet) -> DiagonalSet:
|
|
270
|
+
def replace_nan_rows_with(self, other: 'DiagonalSet') -> 'DiagonalSet':
|
|
275
271
|
"""
|
|
276
272
|
Replaces NaN rows in this DiagonalSet with the equivalent rows from another DiagonalSet.
|
|
277
273
|
|
|
278
274
|
Parameters
|
|
279
275
|
----------
|
|
280
|
-
other : DiagonalSet
|
|
276
|
+
other : 'DiagonalSet'
|
|
281
277
|
The other DiagonalSet to source non-NaN rows from.
|
|
282
278
|
|
|
283
279
|
Returns
|
|
@@ -293,7 +289,7 @@ class DiagonalSet:
|
|
|
293
289
|
self.triplet.append_array(add_array)
|
|
294
290
|
return self
|
|
295
291
|
|
|
296
|
-
def initialize_triplet(self) -> DiagonalSet:
|
|
292
|
+
def initialize_triplet(self) -> 'DiagonalSet':
|
|
297
293
|
"""
|
|
298
294
|
Initializes the triplet by combining all diagonals in the set.
|
|
299
295
|
|
|
@@ -338,13 +334,13 @@ class DiagonalSet:
|
|
|
338
334
|
rows = self.triplet.rows[nan_index]
|
|
339
335
|
return rows.max()
|
|
340
336
|
|
|
341
|
-
def __add__(self, other: DiagonalSet) -> DiagonalSet:
|
|
337
|
+
def __add__(self, other: 'DiagonalSet') -> 'DiagonalSet':
|
|
342
338
|
"""
|
|
343
339
|
Adds another DiagonalSet to this one.
|
|
344
340
|
|
|
345
341
|
Parameters
|
|
346
342
|
----------
|
|
347
|
-
other : DiagonalSet
|
|
343
|
+
other : 'DiagonalSet'
|
|
348
344
|
The other DiagonalSet to add.
|
|
349
345
|
|
|
350
346
|
Returns
|
|
@@ -6,6 +6,7 @@ import numpy
|
|
|
6
6
|
from typing import Optional, List, Tuple
|
|
7
7
|
from pydantic.dataclasses import dataclass
|
|
8
8
|
from pydantic import ConfigDict
|
|
9
|
+
from PyFinitDiff.boundary_values import BoundaryValue
|
|
9
10
|
|
|
10
11
|
config_dict = ConfigDict(
|
|
11
12
|
extra='forbid',
|
|
@@ -16,7 +17,6 @@ config_dict = ConfigDict(
|
|
|
16
17
|
)
|
|
17
18
|
|
|
18
19
|
|
|
19
|
-
@dataclass(config=config_dict)
|
|
20
20
|
class Boundary:
|
|
21
21
|
"""
|
|
22
22
|
Class representing a boundary with a specific name, value, and mesh information.
|
|
@@ -25,15 +25,16 @@ class Boundary:
|
|
|
25
25
|
----------
|
|
26
26
|
name : str
|
|
27
27
|
The name of the boundary.
|
|
28
|
-
value : Optional[
|
|
29
|
-
The value associated with the boundary, such as
|
|
28
|
+
value : Optional[BoundaryValue]
|
|
29
|
+
The value associated with the boundary, such as BoundaryValue.SYMMETRIC, BoundaryValue.ANTI_SYMMETRIC, BoundaryValue.ZERO, or BoundaryValue.NONE.
|
|
30
30
|
mesh_info : object
|
|
31
31
|
The mesh information object containing information about the mesh size and structure.
|
|
32
32
|
"""
|
|
33
33
|
|
|
34
|
-
name: str
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
def __init__(self, name: str, value: Optional[BoundaryValue], mesh_info: object) -> None:
|
|
35
|
+
self.name = name
|
|
36
|
+
self.value = value
|
|
37
|
+
self.mesh_info = mesh_info
|
|
37
38
|
|
|
38
39
|
def get_factor(self) -> float:
|
|
39
40
|
"""
|
|
@@ -50,13 +51,13 @@ class Boundary:
|
|
|
50
51
|
If the boundary value is unexpected.
|
|
51
52
|
"""
|
|
52
53
|
match self.value:
|
|
53
|
-
case
|
|
54
|
+
case BoundaryValue.SYMMETRIC:
|
|
54
55
|
return 1.0
|
|
55
|
-
case
|
|
56
|
+
case BoundaryValue.ANTI_SYMMETRIC:
|
|
56
57
|
return -1.0
|
|
57
|
-
case
|
|
58
|
+
case BoundaryValue.ZERO:
|
|
58
59
|
return 0.0
|
|
59
|
-
case
|
|
60
|
+
case BoundaryValue.NONE:
|
|
60
61
|
return numpy.nan
|
|
61
62
|
case _:
|
|
62
63
|
raise ValueError(f"Unexpected boundary value: {self.value}")
|
|
@@ -104,37 +105,28 @@ class Boundaries:
|
|
|
104
105
|
|
|
105
106
|
Parameters
|
|
106
107
|
----------
|
|
107
|
-
left : Optional[
|
|
108
|
-
Value of the left boundary. Defaults to
|
|
109
|
-
right : Optional[
|
|
110
|
-
Value of the right boundary. Defaults to
|
|
111
|
-
|
|
112
|
-
List of acceptable boundary values.
|
|
108
|
+
left : Optional[BoundaryValue]
|
|
109
|
+
Value of the left boundary. Defaults to BoundaryValue.ZERO.
|
|
110
|
+
right : Optional[BoundaryValue]
|
|
111
|
+
Value of the right boundary. Defaults to BoundaryValue.ZERO.
|
|
112
|
+
|
|
113
113
|
all_boundaries : List[str]
|
|
114
114
|
List of all boundary names.
|
|
115
115
|
"""
|
|
116
|
-
left: Optional[
|
|
117
|
-
right: Optional[
|
|
116
|
+
left: Optional[BoundaryValue] = BoundaryValue.ZERO
|
|
117
|
+
right: Optional[BoundaryValue] = BoundaryValue.ZERO
|
|
118
118
|
|
|
119
|
-
acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
|
|
120
119
|
all_boundaries = ['left', 'right']
|
|
121
120
|
|
|
122
|
-
def
|
|
123
|
-
"""
|
|
124
|
-
Post-initialization method to assert acceptable boundary values.
|
|
125
|
-
"""
|
|
126
|
-
for boundary in self.all_boundaries:
|
|
127
|
-
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
128
|
-
|
|
129
|
-
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
121
|
+
def assert_both_boundaries_not_same(self, boundary_0: BoundaryValue, boundary_1: BoundaryValue) -> None:
|
|
130
122
|
"""
|
|
131
|
-
Assert that both boundaries are not the same axis symmetries if they are not
|
|
123
|
+
Assert that both boundaries are not the same axis symmetries if they are not BoundaryValue.ZERO.
|
|
132
124
|
|
|
133
125
|
Parameters
|
|
134
126
|
----------
|
|
135
|
-
boundary_0 :
|
|
127
|
+
boundary_0 : BoundaryValue
|
|
136
128
|
The first boundary value.
|
|
137
|
-
boundary_1 :
|
|
129
|
+
boundary_1 : BoundaryValue
|
|
138
130
|
The second boundary value.
|
|
139
131
|
|
|
140
132
|
Raises
|
|
@@ -142,36 +134,16 @@ class Boundaries:
|
|
|
142
134
|
ValueError
|
|
143
135
|
If both boundaries are set to the same axis symmetries.
|
|
144
136
|
"""
|
|
145
|
-
if boundary_0 !=
|
|
137
|
+
if boundary_0 != BoundaryValue.ZERO and boundary_1 != BoundaryValue.ZERO:
|
|
146
138
|
raise ValueError("Same-axis symmetries shouldn't be set on both ends")
|
|
147
139
|
|
|
148
|
-
def
|
|
149
|
-
"""
|
|
150
|
-
Assert that a given boundary value is acceptable.
|
|
151
|
-
|
|
152
|
-
Parameters
|
|
153
|
-
----------
|
|
154
|
-
boundary_string : str
|
|
155
|
-
The name of the boundary to check.
|
|
156
|
-
|
|
157
|
-
Raises
|
|
158
|
-
------
|
|
159
|
-
AssertionError
|
|
160
|
-
If the boundary value is not acceptable.
|
|
161
|
-
"""
|
|
162
|
-
boundary_value = getattr(self, boundary_string)
|
|
163
|
-
assert boundary_value in self.acceptable_boundary, (
|
|
164
|
-
f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. "
|
|
165
|
-
f"Input must be in: {self.acceptable_boundary}"
|
|
166
|
-
)
|
|
167
|
-
|
|
168
|
-
def get_boundary_pairs(self) -> List[Tuple[str, str]]:
|
|
140
|
+
def get_boundary_pairs(self) -> List[Tuple[BoundaryValue, BoundaryValue]]:
|
|
169
141
|
"""
|
|
170
142
|
Get the pairs of boundaries.
|
|
171
143
|
|
|
172
144
|
Returns
|
|
173
145
|
-------
|
|
174
|
-
List[Tuple[
|
|
146
|
+
List[Tuple[BoundaryValue, BoundaryValue]]
|
|
175
147
|
A list of tuples containing boundary pairs.
|
|
176
148
|
"""
|
|
177
149
|
return [(self.left, self.right)]
|
|
@@ -7,7 +7,7 @@ from dataclasses import field
|
|
|
7
7
|
from PyFinitDiff.coefficients import FiniteCoefficients
|
|
8
8
|
from PyFinitDiff.finite_difference_1D.boundaries import Boundaries
|
|
9
9
|
from PyFinitDiff.finite_difference_1D.utils import MeshInfo
|
|
10
|
-
from PyFinitDiff.
|
|
10
|
+
from PyFinitDiff.diagonal import ConstantDiagonal, DiagonalSet
|
|
11
11
|
|
|
12
12
|
config_dict = ConfigDict(
|
|
13
13
|
extra='forbid',
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
4
|
import numpy
|
|
5
|
+
|
|
5
6
|
from typing import Optional, List, Tuple
|
|
6
7
|
from pydantic.dataclasses import dataclass
|
|
7
8
|
from pydantic import ConfigDict
|
|
9
|
+
from PyFinitDiff.boundary_values import BoundaryValue
|
|
8
10
|
|
|
9
11
|
config_dict = ConfigDict(
|
|
10
12
|
extra='forbid',
|
|
@@ -15,7 +17,7 @@ config_dict = ConfigDict(
|
|
|
15
17
|
)
|
|
16
18
|
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
|
|
19
21
|
class Boundary:
|
|
20
22
|
"""
|
|
21
23
|
Represents a boundary with a specific name, value, and mesh information.
|
|
@@ -24,14 +26,16 @@ class Boundary:
|
|
|
24
26
|
----------
|
|
25
27
|
name : str
|
|
26
28
|
The name of the boundary.
|
|
27
|
-
value : Optional[
|
|
28
|
-
The value associated with the boundary (e.g.,
|
|
29
|
+
value : Optional[BoundaryValue]
|
|
30
|
+
The value associated with the boundary (e.g., BoundaryValue.SYMMETRIC, BoundaryValue.ANTI_SYMMETRIC).
|
|
29
31
|
mesh_info : object
|
|
30
32
|
Mesh information object, used to determine mesh-related properties.
|
|
31
33
|
"""
|
|
32
|
-
|
|
33
|
-
value: Optional[
|
|
34
|
-
|
|
34
|
+
|
|
35
|
+
def __init__(self, name: str, value: Optional[BoundaryValue], mesh_info: object) -> None:
|
|
36
|
+
self.name = name
|
|
37
|
+
self.value = value
|
|
38
|
+
self.mesh_info = mesh_info
|
|
35
39
|
|
|
36
40
|
def get_factor(self) -> float:
|
|
37
41
|
"""
|
|
@@ -41,19 +45,19 @@ class Boundary:
|
|
|
41
45
|
-------
|
|
42
46
|
float
|
|
43
47
|
The factor corresponding to the boundary value. Possible values are:
|
|
44
|
-
- 1.0 for
|
|
45
|
-
- -1.0 for
|
|
46
|
-
- 0.0 for
|
|
47
|
-
- numpy.nan for
|
|
48
|
+
- 1.0 for BoundaryValue.SYMMETRIC
|
|
49
|
+
- -1.0 for BoundaryValue.ANTI_SYMMETRIC
|
|
50
|
+
- 0.0 for BoundaryValue.ZERO
|
|
51
|
+
- numpy.nan for BoundaryValue.NONE
|
|
48
52
|
"""
|
|
49
53
|
match self.value:
|
|
50
|
-
case
|
|
54
|
+
case BoundaryValue.SYMMETRIC:
|
|
51
55
|
return 1.0
|
|
52
|
-
case
|
|
56
|
+
case BoundaryValue.ANTI_SYMMETRIC:
|
|
53
57
|
return -1.0
|
|
54
|
-
case
|
|
58
|
+
case BoundaryValue.ZERO:
|
|
55
59
|
return 0.0
|
|
56
|
-
case
|
|
60
|
+
case BoundaryValue.NONE:
|
|
57
61
|
return numpy.nan
|
|
58
62
|
|
|
59
63
|
def get_shift_vector(self, offset: int) -> Optional[numpy.ndarray]:
|
|
@@ -100,80 +104,50 @@ class Boundaries:
|
|
|
100
104
|
|
|
101
105
|
Parameters
|
|
102
106
|
----------
|
|
103
|
-
left : str
|
|
104
|
-
Value of the left boundary.
|
|
105
|
-
right : str
|
|
106
|
-
Value of the right boundary.
|
|
107
|
-
top : str
|
|
108
|
-
Value of the top boundary.
|
|
109
|
-
bottom : str
|
|
110
|
-
Value of the bottom boundary.
|
|
111
|
-
acceptable_boundary : List[str]
|
|
112
|
-
List of acceptable boundary values.
|
|
107
|
+
left : Union[str, BoundaryValue]
|
|
108
|
+
Value of the left boundary.
|
|
109
|
+
right : Union[str, BoundaryValue]
|
|
110
|
+
Value of the right boundary.
|
|
111
|
+
top : Union[str, BoundaryValue]
|
|
112
|
+
Value of the top boundary.
|
|
113
|
+
bottom : Union[str, BoundaryValue]
|
|
114
|
+
Value of the bottom boundary.
|
|
113
115
|
all_boundaries : List[str]
|
|
114
116
|
List of all boundary names.
|
|
115
117
|
"""
|
|
116
|
-
left:
|
|
117
|
-
right:
|
|
118
|
-
top:
|
|
119
|
-
bottom:
|
|
118
|
+
left: Optional[BoundaryValue] = BoundaryValue.ZERO
|
|
119
|
+
right: Optional[BoundaryValue] = BoundaryValue.ZERO
|
|
120
|
+
top: Optional[BoundaryValue] = BoundaryValue.ZERO
|
|
121
|
+
bottom: Optional[BoundaryValue] = BoundaryValue.ZERO
|
|
120
122
|
|
|
121
|
-
acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
|
|
122
123
|
all_boundaries = ['left', 'right', 'top', 'bottom']
|
|
123
124
|
|
|
124
|
-
def
|
|
125
|
-
"""
|
|
126
|
-
Validates boundary values after initialization to ensure they are acceptable.
|
|
125
|
+
def assert_both_boundaries_not_same(self, boundary_0: BoundaryValue, boundary_1: BoundaryValue) -> None:
|
|
127
126
|
"""
|
|
128
|
-
|
|
129
|
-
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
130
|
-
|
|
131
|
-
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
132
|
-
"""
|
|
133
|
-
Ensures that two boundaries on the same axis are not set to identical symmetry conditions unless they are 'zero'.
|
|
127
|
+
Ensures that two boundaries on the same axis are not set to identical symmetry conditions unless they are BoundaryValue.ZERO.
|
|
134
128
|
|
|
135
129
|
Parameters
|
|
136
130
|
----------
|
|
137
|
-
boundary_0 :
|
|
131
|
+
boundary_0 : BoundaryValue
|
|
138
132
|
The first boundary value.
|
|
139
|
-
boundary_1 :
|
|
133
|
+
boundary_1 : BoundaryValue
|
|
140
134
|
The second boundary value.
|
|
141
135
|
|
|
142
136
|
Raises
|
|
143
137
|
------
|
|
144
138
|
ValueError
|
|
145
|
-
If both boundaries are set to the same symmetry condition and are not
|
|
139
|
+
If both boundaries are set to the same symmetry condition and are not BoundaryValue.ZERO.
|
|
146
140
|
"""
|
|
147
|
-
if boundary_0 !=
|
|
141
|
+
if boundary_0 != BoundaryValue.ZERO and boundary_1 != BoundaryValue.ZERO:
|
|
148
142
|
raise ValueError("Same-axis symmetries shouldn't be set on both ends")
|
|
149
143
|
|
|
150
|
-
def
|
|
151
|
-
"""
|
|
152
|
-
Checks whether the boundary value is acceptable.
|
|
153
|
-
|
|
154
|
-
Parameters
|
|
155
|
-
----------
|
|
156
|
-
boundary_string : str
|
|
157
|
-
The name of the boundary to validate.
|
|
158
|
-
|
|
159
|
-
Raises
|
|
160
|
-
------
|
|
161
|
-
AssertionError
|
|
162
|
-
If the boundary value is not within the list of acceptable values.
|
|
163
|
-
"""
|
|
164
|
-
boundary_value = getattr(self, boundary_string)
|
|
165
|
-
assert boundary_value in self.acceptable_boundary, (
|
|
166
|
-
f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. "
|
|
167
|
-
f"Input must be in: {self.acceptable_boundary}"
|
|
168
|
-
)
|
|
169
|
-
|
|
170
|
-
def get_boundary_pairs(self) -> List[Tuple[str, str]]:
|
|
144
|
+
def get_boundary_pairs(self) -> List[Tuple[BoundaryValue, BoundaryValue]]:
|
|
171
145
|
"""
|
|
172
146
|
Retrieves pairs of boundaries.
|
|
173
147
|
|
|
174
148
|
Returns
|
|
175
149
|
-------
|
|
176
|
-
List[Tuple[
|
|
150
|
+
List[Tuple[BoundaryValue, BoundaryValue]]
|
|
177
151
|
A list of tuples containing the pairs of boundaries.
|
|
178
152
|
"""
|
|
179
153
|
return [(self.left, self.right), (self.top, self.bottom)]
|
|
@@ -243,9 +217,9 @@ class Boundaries:
|
|
|
243
217
|
str
|
|
244
218
|
The parity in the x direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
245
219
|
"""
|
|
246
|
-
if self.left ==
|
|
220
|
+
if self.left == BoundaryValue.SYMMETRIC or self.right == BoundaryValue.SYMMETRIC:
|
|
247
221
|
return 'symmetric'
|
|
248
|
-
elif self.left ==
|
|
222
|
+
elif self.left == BoundaryValue.ANTI_SYMMETRIC or self.right == BoundaryValue.ANTI_SYMMETRIC:
|
|
249
223
|
return 'anti-symmetric'
|
|
250
224
|
else:
|
|
251
225
|
return 'zero'
|
|
@@ -259,9 +233,9 @@ class Boundaries:
|
|
|
259
233
|
str
|
|
260
234
|
The parity in the y direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
261
235
|
"""
|
|
262
|
-
if self.top ==
|
|
236
|
+
if self.top == BoundaryValue.SYMMETRIC or self.bottom == BoundaryValue.SYMMETRIC:
|
|
263
237
|
return 'symmetric'
|
|
264
|
-
elif self.top ==
|
|
238
|
+
elif self.top == BoundaryValue.ANTI_SYMMETRIC or self.bottom == BoundaryValue.ANTI_SYMMETRIC:
|
|
265
239
|
return 'anti-symmetric'
|
|
266
240
|
else:
|
|
267
241
|
return 'zero'
|
|
@@ -8,7 +8,7 @@ from PyFinitDiff.coefficients import FiniteCoefficients
|
|
|
8
8
|
from PyFinitDiff.triplet import Triplet
|
|
9
9
|
from PyFinitDiff.finite_difference_2D.boundaries import Boundaries
|
|
10
10
|
from PyFinitDiff.finite_difference_2D.utils import MeshInfo
|
|
11
|
-
from PyFinitDiff.
|
|
11
|
+
from PyFinitDiff.diagonal import ConstantDiagonal, DiagonalSet
|
|
12
12
|
|
|
13
13
|
config_dict = ConfigDict(
|
|
14
14
|
extra='forbid',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: PyFinitDiff
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.3.0
|
|
4
4
|
Summary: A package for generating finite-difference matrices, particularly suited for physics and engineering applications.
|
|
5
5
|
Author-email: Martin Poinsinet de Sivry-Houle <martin.poinsinet.de.sivry@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
PyFinitDiff/__init__.py,sha256=
|
|
2
|
-
PyFinitDiff/_version.py,sha256=
|
|
1
|
+
PyFinitDiff/__init__.py,sha256=2W6LdHIO-28A_9Y5iNndtZ0Dd53aMF4VR1041JgLQXQ,175
|
|
2
|
+
PyFinitDiff/_version.py,sha256=KNFYe-Vtdt7Z-oHyl8jmDAQ9qXoCNMAEXigj6BR1QUI,704
|
|
3
|
+
PyFinitDiff/boundary_values.py,sha256=SPqckrx85N7aDoM8G9xfp01-f7zUQ3iRN-3fMXL5YWI,907
|
|
4
|
+
PyFinitDiff/diagonal.py,sha256=mHPEniSMiRppjGjIgolkmxVCylD4MWphyTgxroB3ISI,10296
|
|
3
5
|
PyFinitDiff/directories.py,sha256=CjG37eY-Z68Czo5L7MzHJpu8KEJNhZPnh5VB5MTQ2Tg,837
|
|
4
6
|
PyFinitDiff/triplet.py,sha256=rlNFtfXea3sQGSACAGenV_J7HiIFV3_utlPEfOxiA1E,13878
|
|
5
|
-
PyFinitDiff/utils.py,sha256=oKaaG1qbAjJe3Kd9rS86LuylEuoYu2oPeKgTjKtdxME,3175
|
|
6
7
|
PyFinitDiff/coefficients/__init__.py,sha256=D6leOHW6065G9S6rdcQLmTc99bhWJ9OldfR8lWYQJIU,4179
|
|
7
8
|
PyFinitDiff/coefficients/backward.py,sha256=cTti5MkcrhvuJVNURLJqnTcMmSQhdPvRPImu6ZSEsfY,1977
|
|
8
9
|
PyFinitDiff/coefficients/central.py,sha256=Xq1W-yXnTFLNugYwHajl8XynHPWdV7jH6ll2fy_NVSI,2377
|
|
9
10
|
PyFinitDiff/coefficients/forward.py,sha256=znCszjYptZpQiINx_VQXFeLTNKhntm3MV1g7CNOW67E,1973
|
|
10
11
|
PyFinitDiff/finite_difference_1D/__init__.py,sha256=VIxWZB2VhtmtnU-Qz-VDGLIz1V1ufqXHyUxlIAYKgW4,237
|
|
11
|
-
PyFinitDiff/finite_difference_1D/boundaries.py,sha256=
|
|
12
|
+
PyFinitDiff/finite_difference_1D/boundaries.py,sha256=yA5RsLDQA0DJQq1a9Ks4ujlKU9f5fpXOH6pxTEfF8E4,6093
|
|
12
13
|
PyFinitDiff/finite_difference_1D/derivative.py,sha256=DjJD_b9GBJdjaIyeET2WgcPT18CeCJ9rnxq3igVX5LY,1698
|
|
13
|
-
PyFinitDiff/finite_difference_1D/
|
|
14
|
-
PyFinitDiff/finite_difference_1D/finite_difference.py,sha256=hcHawdh5BJjYooM4Un-P7iwvRCUQ0WbXHX5qywjDCZ8,7577
|
|
14
|
+
PyFinitDiff/finite_difference_1D/finite_difference.py,sha256=4fJImie1arwaFX_7qSWDf1cQOLR28YlDHL0IEbePiL4,7555
|
|
15
15
|
PyFinitDiff/finite_difference_1D/utils.py,sha256=NKRognj1v5asGubHBV0uMgY-gJlloVFWE-1N3hsNxTg,1963
|
|
16
16
|
PyFinitDiff/finite_difference_2D/__init__.py,sha256=TIijlPbN-SiZ9UsaO5zVSZrb5ZsKVm0P63TZ_rjEySU,234
|
|
17
|
-
PyFinitDiff/finite_difference_2D/boundaries.py,sha256=
|
|
17
|
+
PyFinitDiff/finite_difference_2D/boundaries.py,sha256=5etdP_mD8-BKtmGu1oIKy46KJ2dWJSfwKhmpDVhmmpc,7597
|
|
18
18
|
PyFinitDiff/finite_difference_2D/derivative.py,sha256=5BvZl7gImgGHMxAkAdNrtofGrv9m9e2BIYR4Uuz08HI,2013
|
|
19
|
-
PyFinitDiff/finite_difference_2D/
|
|
20
|
-
PyFinitDiff/finite_difference_2D/finite_difference.py,sha256=L9VIPF3NbhZ2YnkO3PBcgxbgg3HHPBhmq0k7w7wkeMc,8403
|
|
19
|
+
PyFinitDiff/finite_difference_2D/finite_difference.py,sha256=bVzpGZGXDnmBERT6laWa_1wOok7JrBQ71N-1htKJGxU,8381
|
|
21
20
|
PyFinitDiff/finite_difference_2D/utils.py,sha256=299k0sFfGRw5l0f8nFQef9x5J1zFMoD-IaifdX6LLhs,2454
|
|
22
|
-
pyfinitdiff-2.
|
|
23
|
-
pyfinitdiff-2.
|
|
24
|
-
pyfinitdiff-2.
|
|
25
|
-
pyfinitdiff-2.
|
|
26
|
-
pyfinitdiff-2.
|
|
21
|
+
pyfinitdiff-2.3.0.dist-info/licenses/LICENSE,sha256=U6YG4UEW9XJzHa53oijR0qw6JNN_SZ38WOBmYn3NPd0,1072
|
|
22
|
+
pyfinitdiff-2.3.0.dist-info/METADATA,sha256=HDTA9dxxd-hFKpXh6IxgmhWRSUPhN0IasU83j-R1dhk,7976
|
|
23
|
+
pyfinitdiff-2.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
24
|
+
pyfinitdiff-2.3.0.dist-info/top_level.txt,sha256=yl_A1Z9VT4HxBK5PTOM4ZyRcXZb-VtbQo1TzRUc1mW4,12
|
|
25
|
+
pyfinitdiff-2.3.0.dist-info/RECORD,,
|
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
|
|
4
|
-
from dataclasses import dataclass, field
|
|
5
|
-
|
|
6
|
-
# Local imports
|
|
7
|
-
import numpy
|
|
8
|
-
from PyFinitDiff.triplet import Triplet
|
|
9
|
-
from PyFinitDiff.finite_difference_1D.boundaries import Boundary
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
@dataclass
|
|
13
|
-
class Diagonal:
|
|
14
|
-
"""
|
|
15
|
-
Represents a diagonal element of the finite-difference method.
|
|
16
|
-
|
|
17
|
-
This class constructs a diagonal with parameters such as its offset and boundary condition.
|
|
18
|
-
|
|
19
|
-
Parameters
|
|
20
|
-
----------
|
|
21
|
-
mesh_info : object
|
|
22
|
-
Instance describing the meta information on the mesh to be considered.
|
|
23
|
-
offset : int
|
|
24
|
-
Offset of the column index for the diagonal.
|
|
25
|
-
values : float
|
|
26
|
-
Value associated with the diagonal.
|
|
27
|
-
boundary : PyFinitDiff.finite_difference_1D.boundaries.Boundary
|
|
28
|
-
Instance of the boundary used for the diagonal.
|
|
29
|
-
"""
|
|
30
|
-
mesh_info: object
|
|
31
|
-
offset: int
|
|
32
|
-
values: float
|
|
33
|
-
boundary: Boundary
|
|
34
|
-
|
|
35
|
-
@property
|
|
36
|
-
def triplet(self) -> Triplet:
|
|
37
|
-
"""
|
|
38
|
-
Return the Triplet instance of the diagonal.
|
|
39
|
-
|
|
40
|
-
Returns
|
|
41
|
-
-------
|
|
42
|
-
Triplet
|
|
43
|
-
A Triplet instance representing the non-zero values in the diagonal.
|
|
44
|
-
"""
|
|
45
|
-
self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
|
|
46
|
-
|
|
47
|
-
triplet = Triplet(
|
|
48
|
-
array=self.array,
|
|
49
|
-
shape=self.mesh_info.shape
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
return triplet
|
|
53
|
-
|
|
54
|
-
def compute_triplet(self) -> None:
|
|
55
|
-
"""
|
|
56
|
-
Compute the diagonal indices and generate a Triplet instance out of it.
|
|
57
|
-
|
|
58
|
-
The value of the third column in the triplet depends on the boundary condition.
|
|
59
|
-
"""
|
|
60
|
-
self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
|
|
61
|
-
self.columns: numpy.ndarray = self.rows + self.offset
|
|
62
|
-
self.apply_symmetry()
|
|
63
|
-
self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
|
|
64
|
-
|
|
65
|
-
def apply_symmetry(self) -> None:
|
|
66
|
-
"""
|
|
67
|
-
Apply boundary symmetry conditions to the diagonal.
|
|
68
|
-
|
|
69
|
-
If the boundary is symmetric, the value stays the same. If it is anti-symmetric,
|
|
70
|
-
a negative sign is added. If it is zero, the value becomes zero.
|
|
71
|
-
"""
|
|
72
|
-
shift_array = self.boundary.get_shift_vector(self.offset)
|
|
73
|
-
|
|
74
|
-
if shift_array is None:
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
self.columns = self.columns + 2 * shift_array
|
|
78
|
-
self.values[shift_array != 0] *= self.boundary.get_factor()
|
|
79
|
-
|
|
80
|
-
def validate_index(self) -> None:
|
|
81
|
-
"""
|
|
82
|
-
Remove all negative indices from the diagonal.
|
|
83
|
-
"""
|
|
84
|
-
valid_index = self.columns >= 0
|
|
85
|
-
self.columns = self.columns[valid_index]
|
|
86
|
-
self.rows = self.rows[valid_index]
|
|
87
|
-
self.values = self.values[valid_index]
|
|
88
|
-
|
|
89
|
-
def plot(self) -> None:
|
|
90
|
-
"""
|
|
91
|
-
Plot the Triplet instance representing the diagonal.
|
|
92
|
-
"""
|
|
93
|
-
return self.triplet.plot()
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
class ConstantDiagonal(Diagonal):
|
|
97
|
-
"""
|
|
98
|
-
Represents a diagonal with constant values.
|
|
99
|
-
|
|
100
|
-
This subclass of Diagonal is initialized with constant values for the entire diagonal.
|
|
101
|
-
"""
|
|
102
|
-
def __init__(self, offset: int, value: float, mesh_info: list, boundary: Boundary):
|
|
103
|
-
super().__init__(
|
|
104
|
-
offset=offset,
|
|
105
|
-
mesh_info=mesh_info,
|
|
106
|
-
values=numpy.ones(mesh_info.size) * value,
|
|
107
|
-
boundary=boundary,
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
@dataclass
|
|
112
|
-
class DiagonalSet:
|
|
113
|
-
"""
|
|
114
|
-
Represents a set of diagonals for the finite-difference matrix.
|
|
115
|
-
|
|
116
|
-
Parameters
|
|
117
|
-
----------
|
|
118
|
-
mesh_info : object
|
|
119
|
-
Instance describing the meta information on the mesh.
|
|
120
|
-
diagonals : list of Diagonal
|
|
121
|
-
List containing diagonal elements.
|
|
122
|
-
"""
|
|
123
|
-
mesh_info: object
|
|
124
|
-
diagonals: list = field(default_factory=list)
|
|
125
|
-
|
|
126
|
-
def append(self, diagonal: Diagonal) -> 'DiagonalSet':
|
|
127
|
-
"""
|
|
128
|
-
Append a diagonal to the set.
|
|
129
|
-
|
|
130
|
-
Parameters
|
|
131
|
-
----------
|
|
132
|
-
diagonal : Diagonal
|
|
133
|
-
The diagonal to be added to the set.
|
|
134
|
-
|
|
135
|
-
Returns
|
|
136
|
-
-------
|
|
137
|
-
DiagonalSet
|
|
138
|
-
The modified DiagonalSet instance.
|
|
139
|
-
"""
|
|
140
|
-
self.diagonals.append(diagonal)
|
|
141
|
-
return self
|
|
142
|
-
|
|
143
|
-
def concatenate(self, other_diagonal_set: 'DiagonalSet') -> 'DiagonalSet':
|
|
144
|
-
"""
|
|
145
|
-
Concatenate another DiagonalSet to this one.
|
|
146
|
-
|
|
147
|
-
Parameters
|
|
148
|
-
----------
|
|
149
|
-
other_diagonal_set : DiagonalSet
|
|
150
|
-
The other DiagonalSet to concatenate.
|
|
151
|
-
|
|
152
|
-
Returns
|
|
153
|
-
-------
|
|
154
|
-
DiagonalSet
|
|
155
|
-
The modified DiagonalSet instance.
|
|
156
|
-
"""
|
|
157
|
-
self.diagonals += other_diagonal_set.diagonals
|
|
158
|
-
return self
|
|
159
|
-
|
|
160
|
-
def get_row_nan_bool(self) -> numpy.ndarray:
|
|
161
|
-
"""
|
|
162
|
-
Get a boolean array indicating which rows have NaN values.
|
|
163
|
-
|
|
164
|
-
Returns
|
|
165
|
-
-------
|
|
166
|
-
numpy.ndarray
|
|
167
|
-
Boolean array with True where rows have NaN values.
|
|
168
|
-
"""
|
|
169
|
-
nan_index = self.get_nan_index()
|
|
170
|
-
nan_rows = self.triplet.rows[nan_index]
|
|
171
|
-
nan_index = numpy.isin(self.triplet.rows, nan_rows)
|
|
172
|
-
return nan_index
|
|
173
|
-
|
|
174
|
-
def get_list_of_nan_rows(self) -> numpy.ndarray:
|
|
175
|
-
"""
|
|
176
|
-
Get a list of rows containing NaN values.
|
|
177
|
-
|
|
178
|
-
Returns
|
|
179
|
-
-------
|
|
180
|
-
numpy.ndarray
|
|
181
|
-
Unique list of rows with NaN values.
|
|
182
|
-
"""
|
|
183
|
-
nan_index = numpy.isnan(self.triplet.values)
|
|
184
|
-
nan_rows = self.triplet.rows[nan_index]
|
|
185
|
-
return numpy.unique(nan_rows)
|
|
186
|
-
|
|
187
|
-
def get_list_of_not_nan_rows(self) -> numpy.ndarray:
|
|
188
|
-
"""
|
|
189
|
-
Get a list of rows without NaN values.
|
|
190
|
-
|
|
191
|
-
Returns
|
|
192
|
-
-------
|
|
193
|
-
numpy.ndarray
|
|
194
|
-
List of rows that do not have NaN values.
|
|
195
|
-
"""
|
|
196
|
-
nan_rows = self.get_list_of_nan_rows()
|
|
197
|
-
non_nan_index = ~numpy.isin(self.triplet.rows, nan_rows)
|
|
198
|
-
non_nan_rows = self.triplet.rows[non_nan_index]
|
|
199
|
-
return non_nan_rows
|
|
200
|
-
|
|
201
|
-
def get_nan_index(self) -> numpy.ndarray:
|
|
202
|
-
"""
|
|
203
|
-
Get a boolean array indicating which entries have NaN values.
|
|
204
|
-
|
|
205
|
-
Returns
|
|
206
|
-
-------
|
|
207
|
-
numpy.ndarray
|
|
208
|
-
Boolean array indicating which entries in the triplet have NaN values.
|
|
209
|
-
"""
|
|
210
|
-
return numpy.isnan(self.triplet.values)
|
|
211
|
-
|
|
212
|
-
def get_array_from_rows(self, rows: numpy.ndarray) -> numpy.ndarray:
|
|
213
|
-
"""
|
|
214
|
-
Get the array elements for the specified rows.
|
|
215
|
-
|
|
216
|
-
Parameters
|
|
217
|
-
----------
|
|
218
|
-
rows : numpy.ndarray
|
|
219
|
-
Rows to search for in the triplet.
|
|
220
|
-
|
|
221
|
-
Returns
|
|
222
|
-
-------
|
|
223
|
-
numpy.ndarray
|
|
224
|
-
Array elements corresponding to the specified rows.
|
|
225
|
-
"""
|
|
226
|
-
rows_index = numpy.isin(self.triplet.rows, rows)
|
|
227
|
-
return self.triplet.array[rows_index]
|
|
228
|
-
|
|
229
|
-
def remove_nan_rows(self) -> 'DiagonalSet':
|
|
230
|
-
"""
|
|
231
|
-
Remove rows with NaN values from the DiagonalSet.
|
|
232
|
-
|
|
233
|
-
Returns
|
|
234
|
-
-------
|
|
235
|
-
DiagonalSet
|
|
236
|
-
The modified DiagonalSet instance with NaN rows removed.
|
|
237
|
-
"""
|
|
238
|
-
nan_rows = self.get_row_nan_bool()
|
|
239
|
-
return self.remove_rows(rows=nan_rows)
|
|
240
|
-
|
|
241
|
-
def remove_rows(self, rows: numpy.ndarray) -> 'DiagonalSet':
|
|
242
|
-
"""
|
|
243
|
-
Remove specified rows from the DiagonalSet.
|
|
244
|
-
|
|
245
|
-
Parameters
|
|
246
|
-
----------
|
|
247
|
-
rows : numpy.ndarray
|
|
248
|
-
Rows to be removed.
|
|
249
|
-
|
|
250
|
-
Returns
|
|
251
|
-
-------
|
|
252
|
-
DiagonalSet
|
|
253
|
-
The modified DiagonalSet instance with specified rows removed.
|
|
254
|
-
"""
|
|
255
|
-
index_to_remove = numpy.isin(self.triplet.rows, rows)
|
|
256
|
-
self.triplet.array = numpy.delete(
|
|
257
|
-
arr=self.triplet.array,
|
|
258
|
-
obj=index_to_remove,
|
|
259
|
-
axis=0
|
|
260
|
-
)
|
|
261
|
-
return self
|
|
262
|
-
|
|
263
|
-
def replace_nan_rows_with(self, other: 'DiagonalSet') -> 'DiagonalSet':
|
|
264
|
-
"""
|
|
265
|
-
Replace NaN rows in this DiagonalSet with corresponding rows from another DiagonalSet.
|
|
266
|
-
|
|
267
|
-
Parameters
|
|
268
|
-
----------
|
|
269
|
-
other : DiagonalSet
|
|
270
|
-
The other DiagonalSet whose non-NaN rows will replace NaN rows in this set.
|
|
271
|
-
|
|
272
|
-
Returns
|
|
273
|
-
-------
|
|
274
|
-
DiagonalSet
|
|
275
|
-
The modified DiagonalSet instance with replaced rows.
|
|
276
|
-
"""
|
|
277
|
-
self_nan_rows = self.get_list_of_nan_rows()
|
|
278
|
-
other_not_nan_rows = other.get_list_of_not_nan_rows()
|
|
279
|
-
replace_rows = numpy.intersect1d(self_nan_rows, other_not_nan_rows)
|
|
280
|
-
self.remove_rows(replace_rows)
|
|
281
|
-
add_array = other.get_array_from_rows(self_nan_rows)
|
|
282
|
-
self.triplet.append_array(add_array)
|
|
283
|
-
|
|
284
|
-
def initialize_triplet(self) -> 'DiagonalSet':
|
|
285
|
-
"""
|
|
286
|
-
Initialize the Triplet instance by concatenating all diagonal triplets.
|
|
287
|
-
|
|
288
|
-
Returns
|
|
289
|
-
-------
|
|
290
|
-
DiagonalSet
|
|
291
|
-
The modified DiagonalSet instance with initialized Triplet.
|
|
292
|
-
"""
|
|
293
|
-
triplet = Triplet(
|
|
294
|
-
array=[0, 0, 0],
|
|
295
|
-
shape=self.mesh_info.shape
|
|
296
|
-
)
|
|
297
|
-
for diagonal in self.diagonals:
|
|
298
|
-
diagonal.compute_triplet()
|
|
299
|
-
triplet += diagonal.triplet
|
|
300
|
-
self.triplet = triplet
|
|
301
|
-
return self
|
|
302
|
-
|
|
303
|
-
def __add__(self, other: 'DiagonalSet') -> 'DiagonalSet':
|
|
304
|
-
"""
|
|
305
|
-
Add another DiagonalSet to this one.
|
|
306
|
-
|
|
307
|
-
Parameters
|
|
308
|
-
----------
|
|
309
|
-
other : DiagonalSet
|
|
310
|
-
The other DiagonalSet to add.
|
|
311
|
-
|
|
312
|
-
Returns
|
|
313
|
-
-------
|
|
314
|
-
DiagonalSet
|
|
315
|
-
The combined DiagonalSet instance.
|
|
316
|
-
"""
|
|
317
|
-
self.diagonals += other.diagonals
|
|
318
|
-
return self
|
|
319
|
-
|
|
320
|
-
def plot(self):
|
|
321
|
-
"""
|
|
322
|
-
Plot the Triplet instance representing the entire DiagonalSet.
|
|
323
|
-
"""
|
|
324
|
-
return self.triplet.plot()
|
PyFinitDiff/utils.py
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import matplotlib.pyplot as plt
|
|
2
|
-
from collections.abc import Iterable
|
|
3
|
-
import numpy
|
|
4
|
-
import scipy
|
|
5
|
-
|
|
6
|
-
from PyFinitDiff.triplet import DiagonalTriplet
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def plot_mesh(*meshes, text=False, title=''):
|
|
10
|
-
figure, axes = plt.subplots(1, len(meshes), figsize=(5 * len(meshes), 5))
|
|
11
|
-
|
|
12
|
-
if not isinstance(axes, Iterable):
|
|
13
|
-
axes = [axes]
|
|
14
|
-
|
|
15
|
-
for ax, mesh in zip(axes, meshes):
|
|
16
|
-
|
|
17
|
-
if isinstance(mesh, scipy.sparse._csr.csr_matrix):
|
|
18
|
-
mesh = mesh.todense()
|
|
19
|
-
|
|
20
|
-
im0 = ax.imshow(mesh, cmap='viridis')
|
|
21
|
-
plt.colorbar(im0, ax=ax)
|
|
22
|
-
ax.set_title(f'FD: {title}')
|
|
23
|
-
ax.grid(True)
|
|
24
|
-
|
|
25
|
-
if text:
|
|
26
|
-
for (i, j), z in numpy.ndenumerate(mesh.astype(float)):
|
|
27
|
-
ax.text(j, i, f'{z:.0f}', ha='center', va='center', size=8)
|
|
28
|
-
|
|
29
|
-
plt.show()
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def get_2D_circular_mesh_triplet(
|
|
33
|
-
n_x: int,
|
|
34
|
-
n_y: int,
|
|
35
|
-
radius: float,
|
|
36
|
-
x_offset: float = 0,
|
|
37
|
-
y_offset: float = 0,
|
|
38
|
-
value_out: float = 0,
|
|
39
|
-
value_in: float = 1) -> DiagonalTriplet:
|
|
40
|
-
"""
|
|
41
|
-
Gets a Triplet corresponding to a 2d mesh with
|
|
42
|
-
circular structure of value_in inside and value_out outside.
|
|
43
|
-
|
|
44
|
-
:param n_x: The number of point in the x-axis
|
|
45
|
-
:type n_x: int
|
|
46
|
-
:param n_y: The number of point in the y-axis
|
|
47
|
-
:type n_y: int
|
|
48
|
-
:param radius: The radius of the structure
|
|
49
|
-
:type radius: float
|
|
50
|
-
:param x_offset: The x offset of the circular structure
|
|
51
|
-
:type x_offset: float
|
|
52
|
-
:param y_offset: The y offset of the circular structure
|
|
53
|
-
:type y_offset: float
|
|
54
|
-
:param value_in: The value inside
|
|
55
|
-
:type value_in: float
|
|
56
|
-
:param value_out: The value outside
|
|
57
|
-
:type value_out: float
|
|
58
|
-
|
|
59
|
-
:returns: The 2d circular mesh triplet.
|
|
60
|
-
:rtype: DiagonalTriplet
|
|
61
|
-
"""
|
|
62
|
-
y, x = numpy.mgrid[
|
|
63
|
-
-100:100:complex(n_y),
|
|
64
|
-
-100:100:complex(n_x)
|
|
65
|
-
]
|
|
66
|
-
|
|
67
|
-
r = numpy.sqrt((x - x_offset)**2 + (y - y_offset)**2)
|
|
68
|
-
mesh = numpy.ones(x.shape) * value_out
|
|
69
|
-
mesh[r < radius] = value_in
|
|
70
|
-
|
|
71
|
-
return DiagonalTriplet(mesh, shape=x.shape)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def get_1D_circular_mesh_triplet(
|
|
75
|
-
n_x: int,
|
|
76
|
-
radius: float,
|
|
77
|
-
x_offset: float = 0,
|
|
78
|
-
value_out: float = 0,
|
|
79
|
-
value_in: float = 1) -> DiagonalTriplet:
|
|
80
|
-
"""
|
|
81
|
-
Gets a Triplet corresponding to a 1d mesh with
|
|
82
|
-
circular structure of value_in inside and value_out outside.
|
|
83
|
-
|
|
84
|
-
:param n_x: The number of point in the x-axis
|
|
85
|
-
:type n_x: int
|
|
86
|
-
:param radius: The radius of the structure
|
|
87
|
-
:type radius: float
|
|
88
|
-
:param x_offset: The x offset of the circular structure
|
|
89
|
-
:type x_offset: float
|
|
90
|
-
:param value_out: The value inside
|
|
91
|
-
:type value_out: float
|
|
92
|
-
:param value_in: The value outside
|
|
93
|
-
:type value_in: float
|
|
94
|
-
|
|
95
|
-
:returns: The 1d circular mesh triplet.
|
|
96
|
-
:rtype: DiagonalTriplet
|
|
97
|
-
"""
|
|
98
|
-
x = numpy.linspace(-100, 100, n_x)
|
|
99
|
-
|
|
100
|
-
r = numpy.sqrt((x - x_offset)**2)
|
|
101
|
-
mesh = numpy.ones(x.shape) * value_out
|
|
102
|
-
mesh[r < radius] = value_in
|
|
103
|
-
|
|
104
|
-
return DiagonalTriplet(mesh, shape=x.shape)
|
|
105
|
-
|
|
106
|
-
# -
|
|
File without changes
|
|
File without changes
|
|
File without changes
|