Skip to content

Commit 84350e6

Browse files
committed
MNT: renamed linear_grid to regular_grid for easy to understand nomenclature
- MNT: added a fallback to reynolds calculation in flight.py to avoid mu is zero case - MNT: updated test_shepard_fallback_warning name and docstring to match implementation in test_function_grid.py
1 parent e3fcad1 commit 84350e6

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

rocketpy/mathutils/function.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"spline": 3,
4545
"shepard": 4,
4646
"rbf": 5,
47-
"linear_grid": 6,
47+
"regular_grid": 6,
4848
}
4949
EXTRAPOLATION_TYPES = {"zero": 0, "natural": 1, "constant": 2}
5050

@@ -463,7 +463,7 @@ def rbf_interpolation(x, x_min, x_max, x_data, y_data, coeffs): # pylint: disab
463463

464464
self._interpolation_func = rbf_interpolation
465465

466-
elif interpolation == 6: # linear_grid (RegularGridInterpolator)
466+
elif interpolation == 6: # regular_grid (RegularGridInterpolator)
467467
# For grid interpolation, the actual interpolator is stored separately
468468
# This function is a placeholder that should not be called directly
469469
# since __get_value_opt_grid is used instead
@@ -4000,11 +4000,16 @@ def __validate_interpolation(self, interpolation):
40004000
elif self.__dom_dim__ > 1:
40014001
if interpolation is None:
40024002
interpolation = "shepard"
4003-
if interpolation.lower() not in ["shepard", "linear", "rbf", "linear_grid"]:
4003+
if interpolation.lower() not in [
4004+
"shepard",
4005+
"linear",
4006+
"rbf",
4007+
"regular_grid",
4008+
]:
40044009
warnings.warn(
40054010
(
40064011
"Interpolation method set to 'shepard'. The methods "
4007-
"'linear', 'shepard', 'rbf' and 'linear_grid' are supported for "
4012+
"'linear', 'shepard', 'rbf' and 'regular_grid' are supported for "
40084013
"multiple dimensions."
40094014
),
40104015
)
@@ -4066,7 +4071,7 @@ def from_grid(
40664071
axes,
40674072
inputs=None,
40684073
outputs=None,
4069-
interpolation="linear_grid",
4074+
interpolation="regular_grid",
40704075
extrapolation="constant",
40714076
**kwargs,
40724077
): # pylint: disable=too-many-statements #TODO: Refactor this method into smaller methods
@@ -4092,8 +4097,8 @@ def from_grid(
40924097
outputs : str, optional
40934098
Name of the output variable. For example: 'Cd'.
40944099
interpolation : str, optional
4095-
Interpolation method. Default is 'linear_grid'.
4096-
Currently only 'linear_grid' is supported for grid data.
4100+
Interpolation method. Default is 'regular_grid'.
4101+
Currently only 'regular_grid' is supported for grid data.
40974102
extrapolation : str, optional
40984103
Extrapolation behavior. Default is ``'constant'`` which clamps to
40994104
edge values. Supported options are::
@@ -4236,8 +4241,21 @@ def from_grid(
42364241
# Set basic array attributes for compatibility
42374242
func.x_array = axes[0]
42384243
func.x_initial, func.x_final = axes[0][0], axes[0][-1]
4239-
func.y_array = func._image[: len(axes[0])] # Placeholder
4240-
func.y_initial, func.y_final = func._image[0], func._image[-1]
4244+
# For grid-based (N-D) functions, a 1-D `y_array` is not a meaningful
4245+
# representation of the function values. Some legacy code paths and
4246+
# serialization expect a `y_array` attribute to exist, so provide the
4247+
# full flattened image for compatibility rather than a truncated slice.
4248+
# Callers should avoid relying on `y_array` for multidimensional
4249+
# Functions; use the interpolator / `get_value_opt` instead.
4250+
func.y_array = func._image
4251+
# Use the global min/max of the flattened image as a sensible
4252+
# `y_initial`/`y_final` for compatibility with code that inspects
4253+
# scalar bounds. These describe the image range, not an ordering
4254+
# along any particular axis.
4255+
func.y_initial, func.y_final = (
4256+
float(func._image.min()),
4257+
float(func._image.max()),
4258+
)
42414259
if len(axes) > 2:
42424260
func.z_array = axes[2]
42434261
func.z_initial, func.z_final = axes[2][0], axes[2][-1]

rocketpy/simulation/flight.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,18 @@ def __get_drag_coefficient(self, drag_function, mach, z, freestream_velocity_bod
14111411
mu = self.env.dynamic_viscosity.get_value_opt(z)
14121412
freestream_speed = np.linalg.norm(freestream_velocity_body)
14131413
characteristic_length = 2 * self.rocket.radius # Diameter
1414-
reynolds = rho * freestream_speed * characteristic_length / mu
1414+
# Defensive: avoid division by zero or non-finite viscosity values.
1415+
# Use a small epsilon fallback if `mu` is zero, negative, NaN or infinite.
1416+
try:
1417+
mu_val = float(mu)
1418+
except Exception:
1419+
mu_val = 0.0
1420+
if not np.isfinite(mu_val) or mu_val <= 0.0:
1421+
mu_safe = 1e-10
1422+
else:
1423+
mu_safe = mu_val
1424+
1425+
reynolds = rho * freestream_speed * characteristic_length / mu_safe
14151426

14161427
# Calculate angle of attack
14171428
# Angle between freestream velocity and rocket axis (z-axis in body frame)

tests/unit/mathutils/test_function_grid.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,26 @@ def test_from_grid_backward_compatibility():
141141
assert func3(2) == 4
142142

143143

144-
def test_shepard_fallback_warning():
145-
"""Test that shepard_fallback is triggered and emits a warning.
146-
147-
When linear_grid interpolation is set but no grid interpolator is available,
148-
the Function class should fall back to shepard interpolation and emit a warning.
144+
def test_regular_grid_without_grid_interpolator_warns():
145+
"""Test that setting `regular_grid` without a grid interpolator warns.
146+
147+
This test constructs a Function from scattered points (no structured
148+
grid). If `regular_grid` interpolation is later selected without a
149+
grid interpolator being configured, the implementation currently
150+
falls back to shepard interpolation and should emit a warning. The
151+
test ensures a warning is raised in this scenario.
149152
"""
150153
# Create a 2D function with scattered points (not structured grid)
151154
source = [(0, 0, 0), (1, 0, 1), (0, 1, 2), (1, 1, 3)]
152155
func = Function(
153156
source=source, inputs=["x", "y"], outputs="z", interpolation="shepard"
154157
)
155158

156-
# Now manually change interpolation to linear_grid without setting up the grid
159+
# Now manually change interpolation to regular_grid without setting up the grid
157160
# This simulates the fallback scenario
158161
with warnings.catch_warnings(record=True) as w:
159162
warnings.simplefilter("always")
160-
func.set_interpolation("linear_grid")
163+
func.set_interpolation("regular_grid")
161164

162165
# Check that a warning was issued
163166
assert len(w) == 1
@@ -168,7 +171,7 @@ def test_shepard_fallback_2d_interpolation():
168171
"""Test that shepard_fallback produces correct interpolation for 2D data.
169172
170173
This test verifies the fallback interpolation works correctly when
171-
linear_grid is set without a grid interpolator.
174+
regular_grid is set without a grid interpolator.
172175
"""
173176
# Create a 2D function: z = x + y
174177
source = [
@@ -191,7 +194,7 @@ def test_shepard_fallback_2d_interpolation():
191194
# Trigger fallback
192195
with warnings.catch_warnings():
193196
warnings.simplefilter("ignore") # Suppress warnings for this test
194-
func_fallback.set_interpolation("linear_grid")
197+
func_fallback.set_interpolation("regular_grid")
195198

196199
# Test that both produce the same results at exact points
197200
assert func_fallback(0, 0) == func_shepard(0, 0)
@@ -237,7 +240,7 @@ def test_shepard_fallback_3d_interpolation():
237240
# Trigger fallback
238241
with warnings.catch_warnings():
239242
warnings.simplefilter("ignore")
240-
func_fallback.set_interpolation("linear_grid")
243+
func_fallback.set_interpolation("regular_grid")
241244

242245
# Test that both produce the same results at exact points
243246
assert func_fallback(0, 0, 0) == func_shepard(0, 0, 0)
@@ -270,7 +273,7 @@ def test_shepard_fallback_at_exact_data_points():
270273
# Trigger fallback
271274
with warnings.catch_warnings():
272275
warnings.simplefilter("ignore")
273-
func.set_interpolation("linear_grid")
276+
func.set_interpolation("regular_grid")
274277

275278
# Test exact data points - should return exact values
276279
assert func(0, 0) == 10

0 commit comments

Comments
 (0)