PyFinitDiff 1.0.14__py3-none-any.whl → 1.1.1__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/__init__.py +35 -27
- 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/coefficients/central.py +0 -4
- PyFinitDiff/finite_difference_1D/__init__.py +6 -0
- PyFinitDiff/finite_difference_1D/__pycache__/__init__.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/boundaries.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/derivatives.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/diagonals.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/finite_difference.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/meshes.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/__pycache__/utils.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_1D/boundaries.py +103 -0
- PyFinitDiff/{tools → finite_difference_1D}/derivatives.py +7 -6
- PyFinitDiff/finite_difference_1D/diagonals.py +248 -0
- PyFinitDiff/finite_difference_1D/finite_difference.py +175 -0
- PyFinitDiff/finite_difference_1D/meshes.py +41 -0
- PyFinitDiff/finite_difference_1D/utils.py +18 -0
- PyFinitDiff/finite_difference_2D/__init__.py +8 -0
- PyFinitDiff/finite_difference_2D/__pycache__/__init__.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/boundaries.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/derivative.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/diagonals.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/finite_difference.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/meshes.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/__pycache__/utils.cpython-311.pyc +0 -0
- PyFinitDiff/finite_difference_2D/boundaries.py +134 -0
- PyFinitDiff/{dense.py → finite_difference_2D/dense.py} +1 -1
- PyFinitDiff/finite_difference_2D/derivative.py +64 -0
- PyFinitDiff/finite_difference_2D/diagonals.py +256 -0
- PyFinitDiff/finite_difference_2D/finite_difference.py +188 -0
- PyFinitDiff/finite_difference_2D/meshes.py +50 -0
- PyFinitDiff/finite_difference_2D/utils.py +22 -0
- PyFinitDiff/tools/directories.py +0 -3
- PyFinitDiff/triplet.py +85 -26
- PyFinitDiff/utils.py +2 -2
- {PyFinitDiff-1.0.14.dist-info → PyFinitDiff-1.1.1.dist-info}/METADATA +6 -7
- PyFinitDiff-1.1.1.dist-info/RECORD +65 -0
- {PyFinitDiff-1.0.14.dist-info → PyFinitDiff-1.1.1.dist-info}/top_level.txt +1 -0
- tests/1D/__init__.py +0 -0
- tests/1D/__pycache__/__init__.cpython-311.pyc +0 -0
- tests/1D/__pycache__/test_api.cpython-311-pytest-8.0.0.pyc +0 -0
- tests/1D/__pycache__/test_derivative.cpython-311-pytest-8.0.0.pyc +0 -0
- tests/1D/test_api.py +29 -0
- tests/1D/test_derivative.py +39 -0
- tests/2D/__init__.py +0 -0
- tests/2D/__pycache__/__init__.cpython-311.pyc +0 -0
- tests/2D/__pycache__/test_api.cpython-311-pytest-8.0.0.pyc +0 -0
- tests/2D/__pycache__/test_derivative.cpython-311-pytest-8.0.0.pyc +0 -0
- tests/2D/__pycache__/test_eigen.cpython-311-pytest-8.0.0.pyc +0 -0
- tests/2D/test_api.py +38 -0
- tests/2D/test_derivative.py +69 -0
- tests/2D/test_eigen.py +43 -0
- PyFinitDiff/__pycache__/__init__.cpython-310.pyc +0 -0
- PyFinitDiff/__pycache__/boundaries.cpython-310.pyc +0 -0
- PyFinitDiff/__pycache__/sparse1D.cpython-310.pyc +0 -0
- PyFinitDiff/__pycache__/sparse2D.cpython-310.pyc +0 -0
- PyFinitDiff/__pycache__/triplet.cpython-310.pyc +0 -0
- PyFinitDiff/__pycache__/utils.cpython-310.pyc +0 -0
- PyFinitDiff/boundaries.py +0 -90
- PyFinitDiff/coefficients/__pycache__/__init__.cpython-310.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/backward.cpython-310.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/central.cpython-310.pyc +0 -0
- PyFinitDiff/coefficients/__pycache__/forward.cpython-310.pyc +0 -0
- PyFinitDiff/sparse1D.py +0 -212
- PyFinitDiff/sparse2D.py +0 -256
- PyFinitDiff/tools/__pycache__/__init__.cpython-310.pyc +0 -0
- PyFinitDiff/tools/__pycache__/derivatives.cpython-310.pyc +0 -0
- PyFinitDiff-1.0.14.dist-info/RECORD +0 -32
- {PyFinitDiff-1.0.14.dist-info → PyFinitDiff-1.1.1.dist-info}/LICENSE +0 -0
- {PyFinitDiff-1.0.14.dist-info → PyFinitDiff-1.1.1.dist-info}/WHEEL +0 -0
PyFinitDiff/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.
|
|
1
|
+
1.1.1
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -15,24 +15,13 @@ __accuracy_list__ = [2, 4, 6]
|
|
|
15
15
|
__derivative_list__ = [1, 2]
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
class
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
self.central = central_coefficent[f"d{self.derivative}"][f"a{self.accuracy}"]
|
|
27
|
-
self.forward = forward_coefficent[f"d{self.derivative}"][f"a{self.accuracy}"]
|
|
28
|
-
self.backward = backward_coefficent[f"d{self.derivative}"][f"a{self.accuracy}"]
|
|
29
|
-
|
|
30
|
-
def __repr__(self):
|
|
31
|
-
return f""" \
|
|
32
|
-
\rcentral coefficients: {self.central}\
|
|
33
|
-
\rforward coefficients: {self.forward}\
|
|
34
|
-
\rbackward coefficients: {self.backward}\
|
|
35
|
-
"""
|
|
18
|
+
class Singleton(type):
|
|
19
|
+
_instances = {}
|
|
20
|
+
|
|
21
|
+
def __call__(cls, *args, **kwargs):
|
|
22
|
+
if cls not in cls._instances:
|
|
23
|
+
cls._instances[cls] = super().__call__(*args, **kwargs)
|
|
24
|
+
return cls._instances[cls]
|
|
36
25
|
|
|
37
26
|
|
|
38
27
|
@dataclass
|
|
@@ -44,10 +33,6 @@ class FiniteCoefficients():
|
|
|
44
33
|
coefficient_type: str = 'central'
|
|
45
34
|
""" Type of coefficient, has to be either 'central', 'forward' or 'backward' """
|
|
46
35
|
|
|
47
|
-
def __post_init__(self):
|
|
48
|
-
self.derivative_string = f'd{self.derivative}'
|
|
49
|
-
self.accuracy_string = f'a{self.accuracy}'
|
|
50
|
-
|
|
51
36
|
def __setattr__(self, attribute, value):
|
|
52
37
|
if attribute == "coefficient_type":
|
|
53
38
|
assert value in ['central', 'forward', 'backward']
|
|
@@ -61,6 +46,26 @@ class FiniteCoefficients():
|
|
|
61
46
|
assert value in self.module.__derivative_list__, f"Derivative: {value} is not avaible for this configuration. Valid in put: {self.module.__derivative_list__}"
|
|
62
47
|
super().__setattr__(attribute, value)
|
|
63
48
|
|
|
49
|
+
@property
|
|
50
|
+
def derivative_string(self) -> str:
|
|
51
|
+
return f'd{self.derivative}'
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def accuracy_string(self) -> str:
|
|
55
|
+
return f'a{self.accuracy}'
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def central(self) -> dict:
|
|
59
|
+
return central_coefficent[self.derivative_string][self.accuracy_string]
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def forward(self) -> dict:
|
|
63
|
+
return forward_coefficent[self.derivative_string][self.accuracy_string]
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def backward(self) -> dict:
|
|
67
|
+
return backward_coefficent[self.derivative_string][self.accuracy_string]
|
|
68
|
+
|
|
64
69
|
def get_coeffcient(self) -> numpy.ndarray:
|
|
65
70
|
"""
|
|
66
71
|
Gets the finit difference coeffcients.
|
|
@@ -70,7 +75,7 @@ class FiniteCoefficients():
|
|
|
70
75
|
"""
|
|
71
76
|
coefficients_dictionnary = self.module.coefficients
|
|
72
77
|
|
|
73
|
-
coefficients_array = coefficients_dictionnary[
|
|
78
|
+
coefficients_array = coefficients_dictionnary[self.derivative_string][self.accuracy_string]
|
|
74
79
|
|
|
75
80
|
coefficients_array = numpy.array(coefficients_array)
|
|
76
81
|
|
|
@@ -85,9 +90,8 @@ class FiniteCoefficients():
|
|
|
85
90
|
@property
|
|
86
91
|
def module(self) -> object:
|
|
87
92
|
"""
|
|
88
|
-
Returns the right module depending on which
|
|
89
|
-
|
|
90
|
-
that the right accuracy and derivative exists on that module.
|
|
93
|
+
Returns the right module depending on which type of coefficient ones need.
|
|
94
|
+
The method also asserts that the right accuracy and derivative exists on that module.
|
|
91
95
|
|
|
92
96
|
:returns: The module.
|
|
93
97
|
:rtype: object
|
|
@@ -107,6 +111,10 @@ class FiniteCoefficients():
|
|
|
107
111
|
def index(self) -> numpy.ndarray:
|
|
108
112
|
return self.array[:, 0]
|
|
109
113
|
|
|
114
|
+
@property
|
|
115
|
+
def max_central_index(self) -> int:
|
|
116
|
+
return self.index.max()
|
|
117
|
+
|
|
110
118
|
@property
|
|
111
119
|
def values(self) -> numpy.ndarray:
|
|
112
120
|
return self.array[:, 1]
|
|
@@ -116,7 +124,7 @@ class FiniteCoefficients():
|
|
|
116
124
|
yield index, values
|
|
117
125
|
|
|
118
126
|
def print(self):
|
|
119
|
-
table = tabulate(self.array, headers=['
|
|
127
|
+
table = tabulate(self.array, headers=['index', 'value'])
|
|
120
128
|
print(table)
|
|
121
129
|
|
|
122
130
|
# -
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
import numpy
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class Boundary():
|
|
10
|
+
name: str
|
|
11
|
+
value: str
|
|
12
|
+
mesh_info: object
|
|
13
|
+
|
|
14
|
+
def get_factor(self) -> float:
|
|
15
|
+
match self.value:
|
|
16
|
+
case 'symmetric':
|
|
17
|
+
return +1
|
|
18
|
+
case 'anti-symmetric':
|
|
19
|
+
return -1
|
|
20
|
+
case 'zero':
|
|
21
|
+
return 0
|
|
22
|
+
case 'none':
|
|
23
|
+
return numpy.nan
|
|
24
|
+
|
|
25
|
+
def get_shift_vector(self, offset: int) -> numpy.ndarray:
|
|
26
|
+
offset = abs(offset)
|
|
27
|
+
|
|
28
|
+
match self.name.lower():
|
|
29
|
+
case 'center':
|
|
30
|
+
shift_vector = None
|
|
31
|
+
case 'left':
|
|
32
|
+
shift_vector = numpy.zeros(self.mesh_info.size)
|
|
33
|
+
shift_vector[:offset] = numpy.arange(offset)[::-1] + 1
|
|
34
|
+
case 'right':
|
|
35
|
+
shift_vector = numpy.zeros(self.mesh_info.size)
|
|
36
|
+
shift_vector[-offset - 1:] = - numpy.arange(offset + 1)
|
|
37
|
+
|
|
38
|
+
return shift_vector
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass()
|
|
42
|
+
class Boundaries():
|
|
43
|
+
left: str = 'zero'
|
|
44
|
+
""" Value of the left boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
45
|
+
right: str = 'zero'
|
|
46
|
+
""" Value of the right boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
47
|
+
|
|
48
|
+
acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
|
|
49
|
+
|
|
50
|
+
all_boundaries = ['left', 'right']
|
|
51
|
+
|
|
52
|
+
def __post_init__(self) -> None:
|
|
53
|
+
for boundary in self.all_boundaries:
|
|
54
|
+
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
55
|
+
|
|
56
|
+
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
57
|
+
if boundary_0 != 'zero' and boundary_1 != 'zero':
|
|
58
|
+
raise ValueError("Same-axis symmetries shouldn't be set on both end")
|
|
59
|
+
|
|
60
|
+
def assert_boundary_acceptable(self, boundary_string: str) -> None:
|
|
61
|
+
boundary_value = getattr(self, boundary_string)
|
|
62
|
+
assert boundary_value in self.acceptable_boundary, f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. Input must be in: {self.acceptable_boundary}"
|
|
63
|
+
|
|
64
|
+
def get_boundary_pairs(self) -> list:
|
|
65
|
+
return [(self.left, self.right), (self.top, self.bottom)]
|
|
66
|
+
|
|
67
|
+
def get_boundary(self, name: str) -> Boundary:
|
|
68
|
+
"""
|
|
69
|
+
Return a specific instance of the boundary
|
|
70
|
+
|
|
71
|
+
:param name: The name
|
|
72
|
+
:type name: str
|
|
73
|
+
|
|
74
|
+
:returns: The boundary.
|
|
75
|
+
:rtype: Boundary
|
|
76
|
+
"""
|
|
77
|
+
if not hasattr(self, name):
|
|
78
|
+
value = None
|
|
79
|
+
else:
|
|
80
|
+
value = getattr(self, name)
|
|
81
|
+
|
|
82
|
+
boundary = Boundary(
|
|
83
|
+
name=name,
|
|
84
|
+
value=value,
|
|
85
|
+
mesh_info=self.mesh_info
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
return boundary
|
|
89
|
+
|
|
90
|
+
def offset_to_boundary(self, offset: int) -> str:
|
|
91
|
+
if offset == 0:
|
|
92
|
+
return self.get_boundary('center')
|
|
93
|
+
|
|
94
|
+
if offset > 0:
|
|
95
|
+
if offset < self.mesh_info.n_x:
|
|
96
|
+
return self.get_boundary('right')
|
|
97
|
+
|
|
98
|
+
if offset < 0:
|
|
99
|
+
if offset > -self.mesh_info.n_x:
|
|
100
|
+
return self.get_boundary('left')
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
# -
|
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
from PyFinitDiff.coefficients import FiniteCoefficients
|
|
5
5
|
|
|
6
|
+
import numpy
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
def get_function_derivative(
|
|
10
|
+
function: numpy.ndarray,
|
|
9
11
|
x_eval: float,
|
|
10
|
-
|
|
12
|
+
derivative: int,
|
|
11
13
|
delta: float,
|
|
12
14
|
function_kwargs: dict,
|
|
13
15
|
accuracy: int = 4,
|
|
@@ -32,7 +34,7 @@ def get_derivative_1d(
|
|
|
32
34
|
:rtype: float
|
|
33
35
|
"""
|
|
34
36
|
coefficients = FiniteCoefficients(
|
|
35
|
-
derivative=
|
|
37
|
+
derivative=derivative,
|
|
36
38
|
accuracy=accuracy,
|
|
37
39
|
coefficient_type=coefficient_type
|
|
38
40
|
)
|
|
@@ -46,7 +48,6 @@ def get_derivative_1d(
|
|
|
46
48
|
|
|
47
49
|
summation += value * y
|
|
48
50
|
|
|
49
|
-
return summation / delta**
|
|
50
|
-
|
|
51
|
+
return summation / delta ** derivative
|
|
51
52
|
|
|
52
53
|
# -
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# Built-in imports
|
|
2
|
+
from typing import Self
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Local imports
|
|
7
|
+
import numpy
|
|
8
|
+
from PyFinitDiff.triplet import Triplet
|
|
9
|
+
import PyFinitDiff.finite_difference_1D as module
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Diagonal():
|
|
14
|
+
"""
|
|
15
|
+
This class is a construction of diagonals element of the finit-difference method.
|
|
16
|
+
The class can be initialized with different parameters suchs as it's offset or
|
|
17
|
+
boundary condition.
|
|
18
|
+
|
|
19
|
+
"""
|
|
20
|
+
mesh_info: object
|
|
21
|
+
""" Instance describing the meta info on the mesh to be considered. """
|
|
22
|
+
offset: int
|
|
23
|
+
""" Offset of the column index for the diagonal. """
|
|
24
|
+
values: float
|
|
25
|
+
""" Value associated with the diagonal. """
|
|
26
|
+
boundary: module.Boundary
|
|
27
|
+
""" Instance of the boundary used for that diagonal. """
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def triplet(self) -> Triplet:
|
|
31
|
+
"""
|
|
32
|
+
Return the Triplet instance of the diagonal.
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
|
|
36
|
+
|
|
37
|
+
triplet = Triplet(
|
|
38
|
+
array=self.array,
|
|
39
|
+
shape=self.mesh_info.shape
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
return triplet
|
|
43
|
+
|
|
44
|
+
def compute_triplet(self) -> None:
|
|
45
|
+
"""
|
|
46
|
+
Compute the diagonal index and generate a Triplet instance out of it.
|
|
47
|
+
The value of the third triplet column depends on the boundary condition.
|
|
48
|
+
|
|
49
|
+
:returns: No return
|
|
50
|
+
:rtype: None
|
|
51
|
+
"""
|
|
52
|
+
self.rows: numpy.ndarray = numpy.arange(0, self.mesh_info.size)
|
|
53
|
+
|
|
54
|
+
self.columns: numpy.ndarray = self.rows + self.offset
|
|
55
|
+
|
|
56
|
+
self.apply_symmetry()
|
|
57
|
+
|
|
58
|
+
self.array: numpy.ndarray = numpy.c_[self.rows, self.columns, self.values]
|
|
59
|
+
|
|
60
|
+
def apply_symmetry(self) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Return the value of the diabonal index as defined by the boundary condition.
|
|
63
|
+
If boundary is symmetric the value stays the same, if anti-symmetric a minus sign
|
|
64
|
+
is added, if zero it returns zero.
|
|
65
|
+
|
|
66
|
+
:param values: The values
|
|
67
|
+
:type values: The initial value
|
|
68
|
+
:param shift_array: The shift array
|
|
69
|
+
:type shift_array: numpy.ndarray
|
|
70
|
+
|
|
71
|
+
:returns: The symmetrized value
|
|
72
|
+
:rtype: float
|
|
73
|
+
"""
|
|
74
|
+
shift_array = self.boundary.get_shift_vector(self.offset)
|
|
75
|
+
|
|
76
|
+
if shift_array is None:
|
|
77
|
+
return
|
|
78
|
+
|
|
79
|
+
self.columns = self.columns + 2 * shift_array
|
|
80
|
+
|
|
81
|
+
self.values[shift_array != 0] *= self.boundary.get_factor()
|
|
82
|
+
|
|
83
|
+
def validate_index(self) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Removes all negative index
|
|
86
|
+
|
|
87
|
+
:returns: No return
|
|
88
|
+
:rtype: None
|
|
89
|
+
"""
|
|
90
|
+
valid_index = self.columns >= 0
|
|
91
|
+
|
|
92
|
+
self.columns = self.columns[valid_index]
|
|
93
|
+
|
|
94
|
+
self.rows = self.rows[valid_index]
|
|
95
|
+
|
|
96
|
+
self.values = self.values[valid_index]
|
|
97
|
+
|
|
98
|
+
def plot(self) -> None:
|
|
99
|
+
"""
|
|
100
|
+
Plots the Triplet instance.
|
|
101
|
+
|
|
102
|
+
"""
|
|
103
|
+
return self.triplet.plot()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class ConstantDiagonal(Diagonal):
|
|
107
|
+
def __init__(self, offset: int, value: float, mesh_info: list, boundary: module.Boundary):
|
|
108
|
+
super().__init__(
|
|
109
|
+
offset=offset,
|
|
110
|
+
mesh_info=mesh_info,
|
|
111
|
+
values=numpy.ones(mesh_info.size) * value,
|
|
112
|
+
boundary=boundary,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass
|
|
117
|
+
class DiagonalSet():
|
|
118
|
+
mesh_info: object
|
|
119
|
+
diagonals: list = field(default_factory=list)
|
|
120
|
+
|
|
121
|
+
def append(self, diagonal: Diagonal) -> Self:
|
|
122
|
+
self.diagonals.append(diagonal)
|
|
123
|
+
return self
|
|
124
|
+
|
|
125
|
+
def concatenate(self, other_diagonal_set: Self) -> Self:
|
|
126
|
+
self.diagonals += other_diagonal_set.diagonals
|
|
127
|
+
|
|
128
|
+
return self
|
|
129
|
+
|
|
130
|
+
def get_row_nan_bool(self) -> numpy.ndarray:
|
|
131
|
+
"""
|
|
132
|
+
Returns a bool array with True where the associated rows has a numpy.nan value.
|
|
133
|
+
|
|
134
|
+
:returns: The nan index.
|
|
135
|
+
:rtype: numpy.ndarray
|
|
136
|
+
"""
|
|
137
|
+
nan_index = self.get_nan_index()
|
|
138
|
+
|
|
139
|
+
nan_rows = self.triplet.rows[nan_index]
|
|
140
|
+
nan_index = numpy.isin(self.triplet.rows, nan_rows)
|
|
141
|
+
|
|
142
|
+
return nan_index
|
|
143
|
+
|
|
144
|
+
def get_list_of_nan_rows(self) -> numpy.ndarray:
|
|
145
|
+
nan_index = numpy.isnan(self.triplet.values)
|
|
146
|
+
|
|
147
|
+
nan_rows = self.triplet.rows[nan_index]
|
|
148
|
+
|
|
149
|
+
return numpy.unique(nan_rows)
|
|
150
|
+
|
|
151
|
+
def get_list_of_not_nan_rows(self) -> numpy.ndarray:
|
|
152
|
+
"""
|
|
153
|
+
Gets the row list where no nan value is associated.
|
|
154
|
+
|
|
155
|
+
:returns: The list of not nan rows.
|
|
156
|
+
:rtype: numpy.ndarray
|
|
157
|
+
"""
|
|
158
|
+
nan_rows = self.get_list_of_nan_rows()
|
|
159
|
+
|
|
160
|
+
non_nan_index = ~numpy.isin(self.triplet.rows, nan_rows)
|
|
161
|
+
|
|
162
|
+
non_nan_rows = self.triplet.rows[non_nan_index]
|
|
163
|
+
|
|
164
|
+
return non_nan_rows
|
|
165
|
+
|
|
166
|
+
def get_nan_index(self) -> numpy.ndarray:
|
|
167
|
+
"""
|
|
168
|
+
Return a boolean list of True/False depending of the values of the triplet
|
|
169
|
+
|
|
170
|
+
:returns: The nan index.
|
|
171
|
+
:rtype: numpy.ndarray
|
|
172
|
+
"""
|
|
173
|
+
return numpy.isnan(self.triplet.values)
|
|
174
|
+
|
|
175
|
+
def get_array_from_rows(self, rows: numpy.ndarray) -> numpy.ndarray:
|
|
176
|
+
"""
|
|
177
|
+
Returns the array element which for which the rows is in the given rows input
|
|
178
|
+
|
|
179
|
+
:param rows: The rows to search for
|
|
180
|
+
:type rows: numpy.ndarray
|
|
181
|
+
|
|
182
|
+
:returns: The array from rows.
|
|
183
|
+
:rtype: numpy.ndarray
|
|
184
|
+
"""
|
|
185
|
+
rows_index = numpy.isin(self.triplet.rows, rows)
|
|
186
|
+
|
|
187
|
+
return self.triplet.array[rows_index]
|
|
188
|
+
|
|
189
|
+
def remove_nan_rows(self) -> Self:
|
|
190
|
+
nan_rows = self.get_row_nan_bool()
|
|
191
|
+
|
|
192
|
+
return self.remove_rows(rows=nan_rows)
|
|
193
|
+
|
|
194
|
+
def remove_rows(self, rows: numpy.ndarray) -> Self:
|
|
195
|
+
index_to_remove = numpy.isin(self.triplet.rows, rows)
|
|
196
|
+
|
|
197
|
+
self.triplet.array = numpy.delete(
|
|
198
|
+
arr=self.triplet.array,
|
|
199
|
+
obj=index_to_remove,
|
|
200
|
+
axis=0
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
return self
|
|
204
|
+
|
|
205
|
+
def replace_nan_rows_with(self, other: Self) -> Self:
|
|
206
|
+
"""
|
|
207
|
+
Replace the nan rows in self for the equivalent rows in the other_diagonal_set if any.
|
|
208
|
+
|
|
209
|
+
:param other_diagonal_set: The other diagonal set
|
|
210
|
+
:type other_diagonal_set: Self
|
|
211
|
+
|
|
212
|
+
:returns: The self instance
|
|
213
|
+
:rtype: Self
|
|
214
|
+
"""
|
|
215
|
+
self_nan_rows = self.get_list_of_nan_rows()
|
|
216
|
+
|
|
217
|
+
other_not_nan_rows = other.get_list_of_not_nan_rows()
|
|
218
|
+
|
|
219
|
+
replace_rows = numpy.intersect1d(self_nan_rows, other_not_nan_rows)
|
|
220
|
+
|
|
221
|
+
self.remove_rows(replace_rows)
|
|
222
|
+
|
|
223
|
+
add_array = other.get_array_from_rows(self_nan_rows)
|
|
224
|
+
|
|
225
|
+
self.triplet.append_array(add_array)
|
|
226
|
+
|
|
227
|
+
def initialize_triplet(self) -> Self:
|
|
228
|
+
triplet = Triplet(
|
|
229
|
+
array=[0, 0, 0],
|
|
230
|
+
shape=self.mesh_info.shape
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
for diagonal in self.diagonals:
|
|
234
|
+
diagonal.compute_triplet()
|
|
235
|
+
triplet += diagonal.triplet
|
|
236
|
+
|
|
237
|
+
self.triplet = triplet
|
|
238
|
+
|
|
239
|
+
return self
|
|
240
|
+
|
|
241
|
+
def __add__(self, other: Self) -> Self:
|
|
242
|
+
self.diagonals += other.diagonals
|
|
243
|
+
|
|
244
|
+
return self
|
|
245
|
+
|
|
246
|
+
def plot(self):
|
|
247
|
+
return self.triplet.plot()
|
|
248
|
+
# -
|