PyFinitDiff 2.0.4.post0__py3-none-any.whl → 2.1.3.post0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,19 +9,26 @@ from PyFinitDiff.triplet import DiagonalTriplet
9
9
  @dataclass
10
10
  class MeshInfo:
11
11
  """
12
- This class represents the mesh information for a 1D finite-difference grid.
12
+ Represents the mesh information for a 1D finite-difference grid.
13
13
 
14
- Attributes:
15
- n_x (int): Number of points in the x direction.
16
- dx (float): Infinitesimal displacement in the x direction. Defaults to 1.
17
- size (int): Total number of points in the mesh.
18
- shape (Tuple[int]): Shape of the mesh as a tuple.
14
+ Attributes
15
+ ----------
16
+ n_x : int
17
+ Number of points in the x direction.
18
+ dx : float, optional
19
+ Infinitesimal displacement in the x direction. Defaults to 1.
20
+ size : int
21
+ Total number of points in the mesh.
22
+ shape : Tuple[int]
23
+ Shape of the mesh as a tuple.
19
24
  """
20
25
  n_x: int
21
26
  dx: float = 1
22
27
 
23
- def __post_init__(self):
24
- """Post-initialization to calculate mesh size and shape."""
28
+ def __post_init__(self) -> None:
29
+ """
30
+ Post-initialization to calculate mesh size and shape.
31
+ """
25
32
  self.size = self.n_x
26
33
  self.shape = (self.n_x,)
27
34
 
@@ -33,20 +40,31 @@ def get_circular_mesh_triplet(
33
40
  value_out: float = 0,
34
41
  value_in: float = 1) -> DiagonalTriplet:
35
42
  """
36
- Gets a Triplet corresponding to a 1D mesh with a circular structure of value_in inside and value_out outside.
43
+ Generates a DiagonalTriplet representing a 1D mesh with a circular structure.
37
44
 
38
- Args:
39
- n_x (int): The number of points in the x-axis.
40
- radius (float): The radius of the circular structure.
41
- x_offset (float, optional): The x offset of the circular structure. Defaults to 0.
42
- value_out (float, optional): The value outside the circular structure. Defaults to 0.
43
- value_in (float, optional): The value inside the circular structure. Defaults to 1.
45
+ The mesh is represented with `value_in` inside a circle of a given `radius`,
46
+ and `value_out` outside the circle. The circular structure is centered with an
47
+ optional x-offset.
44
48
 
45
- Returns:
46
- DiagonalTriplet: The 1D circular mesh triplet.
49
+ Parameters
50
+ ----------
51
+ n_x : int
52
+ The number of points in the x-axis.
53
+ radius : float
54
+ The radius of the circular structure.
55
+ x_offset : float, optional
56
+ The x-offset of the circular structure. Defaults to 0.
57
+ value_out : float, optional
58
+ The value outside the circular structure. Defaults to 0.
59
+ value_in : float, optional
60
+ The value inside the circular structure. Defaults to 1.
61
+
62
+ Returns
63
+ -------
64
+ DiagonalTriplet
65
+ The 1D circular mesh triplet.
47
66
  """
48
67
  x = numpy.linspace(-100, 100, n_x)
49
-
50
68
  r = numpy.abs(x - x_offset)
51
69
  mesh = numpy.ones(x.shape) * value_out
52
70
  mesh[r < radius] = value_in
@@ -17,23 +17,34 @@ config_dict = ConfigDict(
17
17
 
18
18
  @dataclass(config=config_dict)
19
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
20
  """
27
-
21
+ Represents a boundary with a specific name, value, and mesh information.
22
+
23
+ Parameters
24
+ ----------
25
+ name : str
26
+ The name of the boundary.
27
+ value : Optional[str]
28
+ The value associated with the boundary (e.g., 'symmetric', 'anti-symmetric').
29
+ mesh_info : object
30
+ Mesh information object, used to determine mesh-related properties.
31
+ """
28
32
  name: str
29
33
  value: Optional[str]
30
34
  mesh_info: object
31
35
 
32
36
  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
+ """
38
+ Gets the factor associated with the boundary value.
39
+
40
+ Returns
41
+ -------
42
+ float
43
+ The factor corresponding to the boundary value. Possible values are:
44
+ - 1.0 for 'symmetric'
45
+ - -1.0 for 'anti-symmetric'
46
+ - 0.0 for 'zero'
47
+ - numpy.nan for 'none'
37
48
  """
38
49
  match self.value:
39
50
  case 'symmetric':
@@ -45,14 +56,19 @@ class Boundary:
45
56
  case 'none':
46
57
  return numpy.nan
47
58
 
48
- def get_shift_vector(self, offset: int) -> numpy.ndarray:
49
- """Calculates the shift vector based on the boundary name and offset.
59
+ def get_shift_vector(self, offset: int) -> Optional[numpy.ndarray]:
60
+ """
61
+ Calculates the shift vector based on the boundary name and offset.
50
62
 
51
- Args:
52
- offset (int): The offset value to be used in the shift vector calculation.
63
+ Parameters
64
+ ----------
65
+ offset : int
66
+ The offset value to be used in the shift vector calculation.
53
67
 
54
- Returns:
55
- numpy.ndarray: The shift vector as a numpy array.
68
+ Returns
69
+ -------
70
+ Optional[numpy.ndarray]
71
+ The shift vector as a numpy array, or None if the boundary is 'center'.
56
72
  """
57
73
  offset = abs(offset)
58
74
 
@@ -79,17 +95,24 @@ class Boundary:
79
95
 
80
96
  @dataclass(config=config_dict)
81
97
  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
98
  """
92
-
99
+ Represents the boundary conditions for a 2D finite-difference mesh.
100
+
101
+ Parameters
102
+ ----------
103
+ left : str
104
+ Value of the left boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
105
+ right : str
106
+ Value of the right boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
107
+ top : str
108
+ Value of the top boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
109
+ bottom : str
110
+ Value of the bottom boundary. Must be one of ['zero', 'symmetric', 'anti-symmetric', 'none'].
111
+ acceptable_boundary : List[str]
112
+ List of acceptable boundary values.
113
+ all_boundaries : List[str]
114
+ List of all boundary names.
115
+ """
93
116
  left: str = 'zero'
94
117
  right: str = 'zero'
95
118
  top: str = 'zero'
@@ -99,31 +122,44 @@ class Boundaries:
99
122
  all_boundaries = ['left', 'right', 'top', 'bottom']
100
123
 
101
124
  def __post_init__(self) -> None:
102
- """Post-initialization method to assert acceptable boundary values."""
125
+ """
126
+ Validates boundary values after initialization to ensure they are acceptable.
127
+ """
103
128
  for boundary in self.all_boundaries:
104
129
  self.assert_boundary_acceptable(boundary_string=boundary)
105
130
 
106
131
  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.
132
+ """
133
+ Ensures that two boundaries on the same axis are not set to identical symmetry conditions unless they are 'zero'.
134
+
135
+ Parameters
136
+ ----------
137
+ boundary_0 : str
138
+ The first boundary value.
139
+ boundary_1 : str
140
+ The second boundary value.
141
+
142
+ Raises
143
+ ------
144
+ ValueError
145
+ If both boundaries are set to the same symmetry condition and are not 'zero'.
115
146
  """
116
147
  if boundary_0 != 'zero' and boundary_1 != 'zero':
117
148
  raise ValueError("Same-axis symmetries shouldn't be set on both ends")
118
149
 
119
150
  def assert_boundary_acceptable(self, boundary_string: str) -> None:
120
- """Asserts that a given boundary value is acceptable.
151
+ """
152
+ Checks whether the boundary value is acceptable.
121
153
 
122
- Args:
123
- boundary_string (str): The name of the boundary to check.
154
+ Parameters
155
+ ----------
156
+ boundary_string : str
157
+ The name of the boundary to validate.
124
158
 
125
- Raises:
126
- AssertionError: If the boundary value is not acceptable.
159
+ Raises
160
+ ------
161
+ AssertionError
162
+ If the boundary value is not within the list of acceptable values.
127
163
  """
128
164
  boundary_value = getattr(self, boundary_string)
129
165
  assert boundary_value in self.acceptable_boundary, (
@@ -132,21 +168,29 @@ class Boundaries:
132
168
  )
133
169
 
134
170
  def get_boundary_pairs(self) -> List[Tuple[str, str]]:
135
- """Gets the pairs of boundaries.
171
+ """
172
+ Retrieves pairs of boundaries.
136
173
 
137
- Returns:
138
- List[Tuple[str, str]]: A list of tuples containing boundary pairs.
174
+ Returns
175
+ -------
176
+ List[Tuple[str, str]]
177
+ A list of tuples containing the pairs of boundaries.
139
178
  """
140
179
  return [(self.left, self.right), (self.top, self.bottom)]
141
180
 
142
181
  def get_boundary(self, name: str) -> Boundary:
143
- """Returns a specific instance of the boundary.
182
+ """
183
+ Retrieves a Boundary instance by name.
144
184
 
145
- Args:
146
- name (str): The name of the boundary.
185
+ Parameters
186
+ ----------
187
+ name : str
188
+ The name of the boundary to retrieve.
147
189
 
148
- Returns:
149
- Boundary: The boundary instance.
190
+ Returns
191
+ -------
192
+ Boundary
193
+ An instance of the Boundary class for the given boundary name.
150
194
  """
151
195
  if not hasattr(self, name):
152
196
  value = None
@@ -161,14 +205,19 @@ class Boundaries:
161
205
 
162
206
  return boundary
163
207
 
164
- def offset_to_boundary(self, offset: int) -> str:
165
- """Determines the boundary based on the offset.
208
+ def offset_to_boundary(self, offset: int) -> Boundary:
209
+ """
210
+ Determines the boundary corresponding to the given offset.
166
211
 
167
- Args:
168
- offset (int): The offset value.
212
+ Parameters
213
+ ----------
214
+ offset : int
215
+ The offset value.
169
216
 
170
- Returns:
171
- str: The name of the boundary corresponding to the offset.
217
+ Returns
218
+ -------
219
+ Boundary
220
+ The boundary instance corresponding to the offset.
172
221
  """
173
222
  if offset == 0:
174
223
  return self.get_boundary('center')
@@ -186,10 +235,13 @@ class Boundaries:
186
235
  return self.get_boundary('bottom')
187
236
 
188
237
  def get_x_parity(self) -> str:
189
- """Determines the parity in the x direction based on the left and right boundaries.
238
+ """
239
+ Determines the parity in the x direction based on the left and right boundaries.
190
240
 
191
- Returns:
192
- str: The parity in the x direction ('symmetric', 'anti-symmetric', or 'zero').
241
+ Returns
242
+ -------
243
+ str
244
+ The parity in the x direction ('symmetric', 'anti-symmetric', or 'zero').
193
245
  """
194
246
  if self.left == 'symmetric' or self.right == 'symmetric':
195
247
  return 'symmetric'
@@ -199,10 +251,13 @@ class Boundaries:
199
251
  return 'zero'
200
252
 
201
253
  def get_y_parity(self) -> str:
202
- """Determines the parity in the y direction based on the top and bottom boundaries.
254
+ """
255
+ Determines the parity in the y direction based on the top and bottom boundaries.
203
256
 
204
- Returns:
205
- str: The parity in the y direction ('symmetric', 'anti-symmetric', or 'zero').
257
+ Returns
258
+ -------
259
+ str
260
+ The parity in the y direction ('symmetric', 'anti-symmetric', or 'zero').
206
261
  """
207
262
  if self.top == 'symmetric' or self.bottom == 'symmetric':
208
263
  return 'symmetric'
@@ -210,5 +265,3 @@ class Boundaries:
210
265
  return 'anti-symmetric'
211
266
  else:
212
267
  return 'zero'
213
-
214
- # -
@@ -15,20 +15,37 @@ def get_array_derivative(
15
15
  y_derivative: bool = True,
16
16
  boundaries: module.Boundaries = module.Boundaries()) -> np.ndarray:
17
17
  """
18
- Computes the 2D gradient of a given array using finite differences.
19
-
20
- Args:
21
- array (np.ndarray): The array for which to compute the nth derivative.
22
- derivative (int): The order of the derivative.
23
- accuracy (int, optional): The accuracy for the derivative. Defaults to 4.
24
- dx (float, optional): The delta value in the x direction. Defaults to 1.
25
- dy (float, optional): The delta value in the y direction. Defaults to 1.
26
- x_derivative (bool, optional): Whether to compute the x derivative. Defaults to True.
27
- y_derivative (bool, optional): Whether to compute the y derivative. Defaults to True.
28
- boundaries (module.Boundaries, optional): The boundary conditions. Defaults to module.Boundaries().
29
-
30
- Returns:
31
- np.ndarray: The 2D gradient array.
18
+ Compute the 2D gradient of a given array using finite differences.
19
+
20
+ Parameters
21
+ ----------
22
+ array : np.ndarray
23
+ The array for which to compute the nth derivative.
24
+ derivative : int
25
+ The order of the derivative.
26
+ accuracy : int, optional
27
+ The accuracy for the derivative approximation (default is 4).
28
+ dx : float, optional
29
+ The spacing in the x direction (default is 1).
30
+ dy : float, optional
31
+ The spacing in the y direction (default is 1).
32
+ x_derivative : bool, optional
33
+ Whether to compute the derivative in the x direction (default is True).
34
+ y_derivative : bool, optional
35
+ Whether to compute the derivative in the y direction (default is True).
36
+ boundaries : module.Boundaries, optional
37
+ The boundary conditions for the finite difference (default is module.Boundaries()).
38
+
39
+ Returns
40
+ -------
41
+ np.ndarray
42
+ The 2D gradient array computed using finite differences.
43
+
44
+ Examples
45
+ --------
46
+ >>> array = np.array([[1, 2], [3, 4]])
47
+ >>> get_array_derivative(array, derivative=1, dx=0.1, dy=0.1)
48
+ array([[ ... ], [ ... ]])
32
49
  """
33
50
  n_x, n_y = array.shape
34
51