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
|
@@ -2,27 +2,58 @@
|
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
4
|
import numpy
|
|
5
|
-
from
|
|
5
|
+
from typing import Optional, List, Tuple
|
|
6
|
+
from pydantic.dataclasses import dataclass
|
|
7
|
+
from pydantic import ConfigDict
|
|
6
8
|
|
|
9
|
+
config_dict = ConfigDict(
|
|
10
|
+
extra='forbid',
|
|
11
|
+
strict=True,
|
|
12
|
+
arbitrary_types_allowed=True,
|
|
13
|
+
kw_only=True,
|
|
14
|
+
frozen=True
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(config=config_dict)
|
|
19
|
+
class Boundary:
|
|
20
|
+
"""Class representing a boundary with a specific name, value, and mesh information.
|
|
21
|
+
|
|
22
|
+
Attributes:
|
|
23
|
+
name (str): The name of the boundary.
|
|
24
|
+
value (str): The value associated with the boundary.
|
|
25
|
+
mesh_info (object): The mesh information object.
|
|
26
|
+
"""
|
|
7
27
|
|
|
8
|
-
@dataclass(frozen=True)
|
|
9
|
-
class Boundary():
|
|
10
28
|
name: str
|
|
11
|
-
value: str
|
|
29
|
+
value: Optional[str]
|
|
12
30
|
mesh_info: object
|
|
13
31
|
|
|
14
32
|
def get_factor(self) -> float:
|
|
33
|
+
"""Gets the factor associated with the boundary value.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
float: The factor corresponding to the boundary value.
|
|
37
|
+
"""
|
|
15
38
|
match self.value:
|
|
16
39
|
case 'symmetric':
|
|
17
|
-
return
|
|
40
|
+
return 1.0
|
|
18
41
|
case 'anti-symmetric':
|
|
19
|
-
return -1
|
|
42
|
+
return -1.0
|
|
20
43
|
case 'zero':
|
|
21
|
-
return 0
|
|
44
|
+
return 0.0
|
|
22
45
|
case 'none':
|
|
23
46
|
return numpy.nan
|
|
24
47
|
|
|
25
48
|
def get_shift_vector(self, offset: int) -> numpy.ndarray:
|
|
49
|
+
"""Calculates the shift vector based on the boundary name and offset.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
offset (int): The offset value to be used in the shift vector calculation.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
numpy.ndarray: The shift vector as a numpy array.
|
|
56
|
+
"""
|
|
26
57
|
offset = abs(offset)
|
|
27
58
|
|
|
28
59
|
match self.name.lower():
|
|
@@ -46,45 +77,76 @@ class Boundary():
|
|
|
46
77
|
return shift_vector
|
|
47
78
|
|
|
48
79
|
|
|
49
|
-
@dataclass()
|
|
50
|
-
class Boundaries
|
|
80
|
+
@dataclass(config=config_dict)
|
|
81
|
+
class Boundaries:
|
|
82
|
+
"""Class representing the boundaries with left, right, top, and bottom values.
|
|
83
|
+
|
|
84
|
+
Attributes:
|
|
85
|
+
left (str): Value of the left boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
86
|
+
right (str): Value of the right boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
87
|
+
top (str): Value of the top boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
88
|
+
bottom (str): Value of the bottom boundary. Defaults to 'zero' must be either ['zero', 'symmetric', 'anti-symmetric'].
|
|
89
|
+
acceptable_boundary (List[str]): List of acceptable boundary values.
|
|
90
|
+
all_boundaries (List[str]): List of all boundary names.
|
|
91
|
+
"""
|
|
92
|
+
|
|
51
93
|
left: str = 'zero'
|
|
52
|
-
""" Value of the left boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
53
94
|
right: str = 'zero'
|
|
54
|
-
""" Value of the right boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
55
95
|
top: str = 'zero'
|
|
56
|
-
""" Value of the top boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
57
96
|
bottom: str = 'zero'
|
|
58
|
-
""" Value of the bottom boundary, either ['zero', 'symmetric', 'anti-symmetric'] """
|
|
59
97
|
|
|
60
98
|
acceptable_boundary = ['zero', 'symmetric', 'anti-symmetric', 'none']
|
|
61
|
-
|
|
62
99
|
all_boundaries = ['left', 'right', 'top', 'bottom']
|
|
63
100
|
|
|
64
101
|
def __post_init__(self) -> None:
|
|
102
|
+
"""Post-initialization method to assert acceptable boundary values."""
|
|
65
103
|
for boundary in self.all_boundaries:
|
|
66
104
|
self.assert_boundary_acceptable(boundary_string=boundary)
|
|
67
105
|
|
|
68
106
|
def assert_both_boundaries_not_same(self, boundary_0: str, boundary_1: str) -> None:
|
|
107
|
+
"""Asserts that both boundaries are not the same axis symmetries if they are not 'zero'.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
boundary_0 (str): The first boundary value.
|
|
111
|
+
boundary_1 (str): The second boundary value.
|
|
112
|
+
|
|
113
|
+
Raises:
|
|
114
|
+
ValueError: If both boundaries are set to the same axis symmetries.
|
|
115
|
+
"""
|
|
69
116
|
if boundary_0 != 'zero' and boundary_1 != 'zero':
|
|
70
|
-
raise ValueError("Same-axis symmetries shouldn't be set on both
|
|
117
|
+
raise ValueError("Same-axis symmetries shouldn't be set on both ends")
|
|
71
118
|
|
|
72
119
|
def assert_boundary_acceptable(self, boundary_string: str) -> None:
|
|
120
|
+
"""Asserts that a given boundary value is acceptable.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
boundary_string (str): The name of the boundary to check.
|
|
124
|
+
|
|
125
|
+
Raises:
|
|
126
|
+
AssertionError: If the boundary value is not acceptable.
|
|
127
|
+
"""
|
|
73
128
|
boundary_value = getattr(self, boundary_string)
|
|
74
|
-
assert boundary_value in self.acceptable_boundary,
|
|
129
|
+
assert boundary_value in self.acceptable_boundary, (
|
|
130
|
+
f"Error: {boundary_string} boundary: {boundary_value} argument not accepted. "
|
|
131
|
+
f"Input must be in: {self.acceptable_boundary}"
|
|
132
|
+
)
|
|
75
133
|
|
|
76
|
-
def get_boundary_pairs(self) ->
|
|
134
|
+
def get_boundary_pairs(self) -> List[Tuple[str, str]]:
|
|
135
|
+
"""Gets the pairs of boundaries.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
List[Tuple[str, str]]: A list of tuples containing boundary pairs.
|
|
139
|
+
"""
|
|
77
140
|
return [(self.left, self.right), (self.top, self.bottom)]
|
|
78
141
|
|
|
79
142
|
def get_boundary(self, name: str) -> Boundary:
|
|
80
|
-
"""
|
|
81
|
-
Return a specific instance of the boundary
|
|
143
|
+
"""Returns a specific instance of the boundary.
|
|
82
144
|
|
|
83
|
-
:
|
|
84
|
-
|
|
145
|
+
Args:
|
|
146
|
+
name (str): The name of the boundary.
|
|
85
147
|
|
|
86
|
-
:
|
|
87
|
-
|
|
148
|
+
Returns:
|
|
149
|
+
Boundary: The boundary instance.
|
|
88
150
|
"""
|
|
89
151
|
if not hasattr(self, name):
|
|
90
152
|
value = None
|
|
@@ -100,6 +162,14 @@ class Boundaries():
|
|
|
100
162
|
return boundary
|
|
101
163
|
|
|
102
164
|
def offset_to_boundary(self, offset: int) -> str:
|
|
165
|
+
"""Determines the boundary based on the offset.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
offset (int): The offset value.
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
str: The name of the boundary corresponding to the offset.
|
|
172
|
+
"""
|
|
103
173
|
if offset == 0:
|
|
104
174
|
return self.get_boundary('center')
|
|
105
175
|
|
|
@@ -116,6 +186,11 @@ class Boundaries():
|
|
|
116
186
|
return self.get_boundary('bottom')
|
|
117
187
|
|
|
118
188
|
def get_x_parity(self) -> str:
|
|
189
|
+
"""Determines the parity in the x direction based on the left and right boundaries.
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
str: The parity in the x direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
193
|
+
"""
|
|
119
194
|
if self.left == 'symmetric' or self.right == 'symmetric':
|
|
120
195
|
return 'symmetric'
|
|
121
196
|
elif self.left == 'anti-symmetric' or self.right == 'anti-symmetric':
|
|
@@ -124,6 +199,11 @@ class Boundaries():
|
|
|
124
199
|
return 'zero'
|
|
125
200
|
|
|
126
201
|
def get_y_parity(self) -> str:
|
|
202
|
+
"""Determines the parity in the y direction based on the top and bottom boundaries.
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
str: The parity in the y direction ('symmetric', 'anti-symmetric', or 'zero').
|
|
206
|
+
"""
|
|
127
207
|
if self.top == 'symmetric' or self.bottom == 'symmetric':
|
|
128
208
|
return 'symmetric'
|
|
129
209
|
elif self.top == 'anti-symmetric' or self.bottom == 'anti-symmetric':
|
|
@@ -2,26 +2,32 @@ import numpy as np
|
|
|
2
2
|
import numpy
|
|
3
3
|
import matplotlib.pyplot as plt
|
|
4
4
|
from scipy import sparse
|
|
5
|
-
from dataclasses import
|
|
6
|
-
from typing import Dict
|
|
5
|
+
from dataclasses import field
|
|
6
|
+
from typing import Dict, Optional
|
|
7
|
+
|
|
8
|
+
from pydantic.dataclasses import dataclass
|
|
9
|
+
from pydantic import ConfigDict
|
|
7
10
|
|
|
8
11
|
from PyFinitDiff.utils import NameSpace
|
|
9
12
|
from PyFinitDiff.coefficients import FinitCoefficients
|
|
10
13
|
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
config_dict = ConfigDict(extra='forbid', strict=True, kw_only=True, frozen=True)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(config=config_dict)
|
|
13
19
|
class FiniteDifference():
|
|
14
20
|
"""
|
|
15
21
|
Reference : ['math.toronto.edu/mpugh/Teaching/Mat1062/notes2.pdf']
|
|
16
22
|
"""
|
|
17
23
|
n_x: int
|
|
18
24
|
n_y: int
|
|
19
|
-
dx: float = 1
|
|
20
|
-
dy: float = 1
|
|
21
|
-
derivative: int = 1
|
|
22
|
-
accuracy: int = 2
|
|
23
|
-
naive: bool = False
|
|
24
|
-
symmetries: Dict[str,
|
|
25
|
+
dx: Optional[float] = 1
|
|
26
|
+
dy: Optional[float] = 1
|
|
27
|
+
derivative: Optional[int] = 1
|
|
28
|
+
accuracy: Optional[int] = 2
|
|
29
|
+
naive: Optional[bool] = False
|
|
30
|
+
symmetries: Dict[str, int] = field(default_factory=lambda: ({'left': 0, 'right': 0, 'top': 0, 'bottom': 0}))
|
|
25
31
|
|
|
26
32
|
def __post_init__(self):
|
|
27
33
|
self.finit_coefficient = FinitCoefficients(
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env python
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
|
-
import numpy
|
|
5
|
-
|
|
4
|
+
import numpy as np
|
|
6
5
|
import PyFinitDiff.finite_difference_2D as module
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
def get_array_derivative(
|
|
10
|
-
array:
|
|
9
|
+
array: np.ndarray,
|
|
11
10
|
derivative: int,
|
|
12
11
|
accuracy: int = 4,
|
|
13
12
|
coefficient_type: str = 'central',
|
|
@@ -15,31 +14,23 @@ def get_array_derivative(
|
|
|
15
14
|
dy: float = 1,
|
|
16
15
|
x_derivative: bool = True,
|
|
17
16
|
y_derivative: bool = True,
|
|
18
|
-
boundaries: module.Boundaries = module.Boundaries()) ->
|
|
17
|
+
boundaries: module.Boundaries = module.Boundaries()) -> np.ndarray:
|
|
19
18
|
"""
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
:
|
|
34
|
-
|
|
35
|
-
:type x_derivative: bool
|
|
36
|
-
:param y_derivative: Compute the y derivative
|
|
37
|
-
:type y_derivative: bool
|
|
38
|
-
:param boundaries: The boundaries conditions
|
|
39
|
-
:type boundaries: Boundaries
|
|
40
|
-
|
|
41
|
-
:returns: The 2D gradient array.
|
|
42
|
-
:rtype: float
|
|
19
|
+
Computes the 2D gradient of a given array using finite differences.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
array (np.ndarray): The array for which to compute the nth derivative.
|
|
23
|
+
derivative (int): The order of the derivative.
|
|
24
|
+
accuracy (int, optional): The accuracy for the derivative. Defaults to 4.
|
|
25
|
+
coefficient_type (str, optional): The coefficient type. Defaults to 'central'.
|
|
26
|
+
dx (float, optional): The delta value in the x direction. Defaults to 1.
|
|
27
|
+
dy (float, optional): The delta value in the y direction. Defaults to 1.
|
|
28
|
+
x_derivative (bool, optional): Whether to compute the x derivative. Defaults to True.
|
|
29
|
+
y_derivative (bool, optional): Whether to compute the y derivative. Defaults to True.
|
|
30
|
+
boundaries (module.Boundaries, optional): The boundary conditions. Defaults to module.Boundaries().
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
np.ndarray: The 2D gradient array.
|
|
43
34
|
"""
|
|
44
35
|
n_x, n_y = array.shape
|
|
45
36
|
|
|
@@ -60,5 +51,3 @@ def get_array_derivative(
|
|
|
60
51
|
gradient = triplet.to_scipy_sparse() * array.ravel()
|
|
61
52
|
|
|
62
53
|
return gradient.reshape([n_x, n_y])
|
|
63
|
-
|
|
64
|
-
# -
|