diff --git a/docs/user/rocket/rocket_usage.rst b/docs/user/rocket/rocket_usage.rst index efaccfb96..3adf35fe6 100644 --- a/docs/user/rocket/rocket_usage.rst +++ b/docs/user/rocket/rocket_usage.rst @@ -165,9 +165,9 @@ defines drag as a function of angle of attack and Mach number: Fill in every combination of the values in the table, and write each value the same way in every row. If a combination is missing, a row is repeated or a value differs in any digit (0.3 and 0.30000000000000004), the file - still loads and no warning is given, but RocketPy interpolates between - scattered points instead of along the grid, and the drag coefficient - changes both between and outside the points. + still loads, but RocketPy warns and interpolates between scattered points + instead of along the grid, and the drag coefficient changes both between + and outside the points. For backward compatibility, ``rocket.power_off_drag`` and ``rocket.power_on_drag`` expose the Mach-only slice of each model, with the diff --git a/rocketpy/rocket/rocket.py b/rocketpy/rocket/rocket.py index 75369af50..a7e790813 100644 --- a/rocketpy/rocket/rocket.py +++ b/rocketpy/rocket/rocket.py @@ -2612,6 +2612,13 @@ def mach_wrapper( extrapolation="constant", ) if csv_func is None: + warnings.warn( + f"{coeff_name} CSV {file_path!r} does not form a full regular " + "grid; using scattered interpolation. Drag coefficient values " + "may differ both between and outside the tabulated points.", + UserWarning, + stacklevel=4, + ) csv_func = Function( file_path, interpolation="linear", diff --git a/tests/unit/rocket/test_rocket.py b/tests/unit/rocket/test_rocket.py index b713226a6..b14d086e3 100644 --- a/tests/unit/rocket/test_rocket.py +++ b/tests/unit/rocket/test_rocket.py @@ -887,6 +887,59 @@ def test_drag_csv_header_order_independent_for_multivariable_input(tmp_path): assert swapped_csv_function.get_interpolation_method() == "regular_grid" +def test_load_rocket_drag_csv_missing_combination_warns_and_uses_scattered_interpolation( + tmp_path, +): + """Non-grid drag CSVs must warn and still load with scattered interpolation. + + A multi-variable CSV missing one input combination cannot form a regular + grid, so ``Rocket.__load_rocket_drag_csv`` falls back to interpolating + scattered points. The warning names the drag file and points at the + caller's ``Rocket(...)`` line. The rocket must still construct and + evaluate the table. + """ + irregular_csv = tmp_path / "drag_missing_combination.csv" + irregular_csv.write_text( + "alpha,mach,cd\n0.0,0.5,0.30\n0.0,1.0,0.45\n0.1,0.5,0.32\n", + encoding="utf-8", + ) + + with pytest.warns( + UserWarning, + match="does not form a full regular grid.*scattered interpolation", + ) as caught: + rocket = Rocket( + radius=0.05, + mass=1.0, + inertia=(1.0, 1.0, 1.0), + power_off_drag=str(irregular_csv), + power_on_drag=0.3, + center_of_mass_without_motor=0.0, + ) + + warning = caught[0] + assert warning.filename == __file__ + warning_text = str(warning.message) + assert irregular_csv.name in warning_text + path_str = str(irregular_csv) + assert path_str in warning_text or repr(path_str) in warning_text + + closure = rocket.power_off_drag_7d.source.__closure__ + csv_function = next( + cell.cell_contents + for cell in closure + if isinstance(cell.cell_contents, Function) + ) + + assert csv_function.get_interpolation_method() != "regular_grid" + assert rocket.power_off_drag_7d(0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0) == pytest.approx( + 0.30 + ) + assert rocket.power_off_drag_7d(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0) == pytest.approx( + 0.45 + ) + + def test_drag_input_types_supported_for_power_on_and_power_off(tmp_path): """Ensure drag input processing accepts all supported input types.