From db5e85cc7f06b4d123f1619a8005073d402d55d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Karakaya?= Date: Thu, 19 Nov 2026 10:41:25 +0300 Subject: [PATCH 1/2] perf(solver)!: keep negative thrust in the RPM search The solver treated negative thrust or non-positive torque as "no data" and spent most of a forward-flight solve narrowing the lower RPM bound. Models now return NaN where they have no result; the solver rejects only non-finite values, and a root with negative thrust is still marked invalid_coefficients. Blade-element forward flight drops from about 56 to 13 propeller calls (112 ms to 27 ms) with the same solved RPM. BREAKING CHANGE: TablePropeller.thrust_torque returns (nan, nan) instead of (0, 0) outside its data (Cp <= 0 or Ct < 0). BladeElementPropeller returns (nan, nan) where a station does not converge. Co-Authored-By: Claude Opus 5.5 --- docs/reference/blade_element_propeller.md | 6 +- docs/reference/propellers.md | 7 ++- docs/reference/propulsion_solver.md | 5 ++ docs/reference/table_propeller.md | 1 + pythrust/propeller/blade_element.py | 4 +- pythrust/propeller/model.py | 7 ++- pythrust/propeller/table.py | 8 +++ pythrust/propulsion/solver.py | 12 ++-- tests/test_blade_element.py | 27 +++++++++ tests/test_propeller_model.py | 6 +- tests/test_solver.py | 68 +++++++++++++++++++++++ 11 files changed, 135 insertions(+), 16 deletions(-) diff --git a/docs/reference/blade_element_propeller.md b/docs/reference/blade_element_propeller.md index 2bd5d93..33674ea 100644 --- a/docs/reference/blade_element_propeller.md +++ b/docs/reference/blade_element_propeller.md @@ -109,7 +109,7 @@ thrust_n, torque_nm = propeller.thrust_torque(rho=1.225, airspeed_mps=10.0, rpm= | `mu`, `speed_of_sound` | Air viscosity in kg/(m s) and speed of sound in m/s; the defaults are QPROP's | | `diameter_m` | Twice the tip radius | | `max_advance_ratio` | Advance ratio at which the section at 75% radius has zero lift, an estimate of zero thrust | -| `thrust_torque(rho, airspeed_mps, rpm)` | Thrust in N and torque in N m; zero for both at zero RPM or where a station has no solution | +| `thrust_torque(rho, airspeed_mps, rpm)` | Thrust in N and torque in N m, negative for a windmilling blade; zero at zero RPM, NaN where a station has no solution | ## Theory @@ -246,8 +246,8 @@ $$ Q = \rho B \sum \left(\Gamma W_a + \tfrac{1}{2} W c\, c_d\, W_t\right) r\, \Delta r $$ -At zero RPM, or where a station has no root, the model gives zero thrust and -torque. +At zero RPM the model gives zero thrust and torque; where a station has no +root, it gives NaN. ### Start of the RPM search diff --git a/docs/reference/propellers.md b/docs/reference/propellers.md index 1f3843c..9c10316 100644 --- a/docs/reference/propellers.md +++ b/docs/reference/propellers.md @@ -17,7 +17,12 @@ The solver takes any object that satisfies `pythrust.propeller.PropellerModel`: |---|---| | `diameter_m` | Propeller diameter in metres | | `max_advance_ratio` | Advance ratio up to which the model is expected to give thrust; the RPM search starts there | -| `thrust_torque(rho, airspeed_mps, rpm)` | Thrust in N and shaft torque in N m; zero torque where the model has no data | +| `thrust_torque(rho, airspeed_mps, rpm)` | Thrust in N and shaft torque in N m; NaN where the model has no result | + +Negative thrust or torque, as for a windmilling propeller, is a result: the +solver keeps it in its RPM search and marks an operating point that lands +there infeasible (`invalid_coefficients`). NaN tells the solver that the model +has no answer at that RPM, as past the end of a table. ## Coefficients diff --git a/docs/reference/propulsion_solver.md b/docs/reference/propulsion_solver.md index cce4f12..af0f146 100644 --- a/docs/reference/propulsion_solver.md +++ b/docs/reference/propulsion_solver.md @@ -89,6 +89,11 @@ Before solving, PyThrust builds a search interval: If the residual does not change sign inside the bracket, the point is returned as infeasible with reason `no_bracket`. +Where the propeller model gives NaN, the residual has no value. If it has none +at the lower bound, as past the end of a table, the solver raises the lower +bound to the lowest RPM where it has one. Negative thrust or torque counts as +a value, so a blade-element propeller needs no such search. + ## API ### Solving an Operating Point diff --git a/docs/reference/table_propeller.md b/docs/reference/table_propeller.md index ee81543..c56c92f 100644 --- a/docs/reference/table_propeller.md +++ b/docs/reference/table_propeller.md @@ -23,6 +23,7 @@ ct, cp = propeller.get_coefficients(rpm=6000.0, advance_ratio=0.3) | `TablePropeller(diameter_m, data_by_rpm)` | Build a table from your own data | | `scaled(diameter_m)` | The propeller at another diameter with its coefficients unchanged: a geometrically similar propeller, without the Reynolds number effects of the size change | | `get_coefficients(rpm, advance_ratio)` | Interpolated `Ct` and `Cp`; zero past a band's largest advance ratio | +| `thrust_torque(rho, airspeed_mps, rpm)` | Thrust and torque; NaN where the interpolated `Cp` is not positive or `Ct` is negative, as past the table's data | | `rpm_levels` | Sorted RPM bands | ## APC Performance Files diff --git a/pythrust/propeller/blade_element.py b/pythrust/propeller/blade_element.py index 515b58c..6751c57 100644 --- a/pythrust/propeller/blade_element.py +++ b/pythrust/propeller/blade_element.py @@ -76,7 +76,7 @@ def max_advance_ratio(self) -> float: return math.pi * (r75 / radius) * math.tan(phi) def thrust_torque(self, rho: float, airspeed_mps: float, rpm: float) -> Tuple[float, float]: - """Thrust in N and torque in N m; zero for both where a station has no solution.""" + """Thrust in N and torque in N m; zero at zero RPM, NaN where a station has no solution.""" omega = float(rpm) * math.pi / 30.0 if omega <= 0.0: return 0.0, 0.0 @@ -94,7 +94,7 @@ def thrust_torque(self, rho: float, airspeed_mps: float, rpm: float) -> Tuple[fl self.speed_of_sound, ) if not s.converged.all(): - return 0.0, 0.0 + return math.nan, math.nan w = np.hypot(s.axial_velocity, s.tangential_velocity) # Gamma * eps = Gamma * cd / cl = W c cd / 2, finite where cl is zero. profile = 0.5 * w * self._chord * s.cd diff --git a/pythrust/propeller/model.py b/pythrust/propeller/model.py index 8fc27a8..e3319b9 100644 --- a/pythrust/propeller/model.py +++ b/pythrust/propeller/model.py @@ -25,7 +25,10 @@ def max_advance_ratio(self) -> float: def thrust_torque(self, rho: float, airspeed_mps: float, rpm: float) -> tuple[float, float]: """Return thrust in N and shaft torque in N m. - ``rho`` is the air density in kg/m^3. A model returns zero torque where - it has no data; the solver treats that condition as having no solution. + ``rho`` is the air density in kg/m^3. Where the model has no result, such + as outside a table's data, it returns NaN; the solver treats that as no + solution. Negative thrust or torque, as for a windmilling propeller, is a + result: the solver keeps it in its search and marks an operating point + that lands there infeasible. """ ... diff --git a/pythrust/propeller/table.py b/pythrust/propeller/table.py index 23e2fc4..da81667 100644 --- a/pythrust/propeller/table.py +++ b/pythrust/propeller/table.py @@ -50,9 +50,17 @@ def scaled(self, diameter_m: float) -> "TablePropeller": return TablePropeller(diameter_m, self.data_by_rpm) def thrust_torque(self, rho: float, airspeed_mps: float, rpm: float) -> Tuple[float, float]: + """Thrust in N and torque in N m; NaN for both where the table gives no thrust and power. + + That is where the interpolated power coefficient is not positive or the + thrust coefficient is negative: past the table's data, or in windmilling + rows a table may hold. + """ n = max(rpm / 60.0, 1e-6) j = airspeed_mps / (n * self.diameter_m) if self.diameter_m > 0 else 0.0 ct, cp = self.get_coefficients(rpm, j) + if cp <= 0.0 or ct < 0.0: + return math.nan, math.nan thrust_n = ct * rho * (n**2) * (self.diameter_m**4) torque_nm = cp * rho * (n**2) * (self.diameter_m**5) / (2.0 * math.pi) return thrust_n, torque_nm diff --git a/pythrust/propulsion/solver.py b/pythrust/propulsion/solver.py index d5f8380..6ce2458 100644 --- a/pythrust/propulsion/solver.py +++ b/pythrust/propulsion/solver.py @@ -99,7 +99,7 @@ def g(rpm: float) -> float: airspeed_mps, rpm, ) - if torque_nm <= 0.0 or thrust_n < 0.0 or j < 0.0: + if not (math.isfinite(thrust_n) and math.isfinite(torque_nm)) or j < 0.0: return float("inf") v_motor = v_back + current_a * motor.get_winding_resistance(current_a) battery_current_for_voltage_a = throttle * current_a @@ -173,11 +173,11 @@ def g(rpm: float) -> float: def _lowest_valid_rpm(g: Callable[[float], float], rpm_low: float, rpm_high: float) -> float: """Return the lowest RPM in ``[rpm_low, rpm_high]`` where ``g`` is finite. - The bracket starts at the propeller data's largest advance ratio, where the - coefficients can already describe a windmilling propeller. The coefficients - become valid as the RPM rises, so the search moves the bracket's lower end - onto the first valid RPM. ``g(rpm_low)`` is not finite; the method returns - ``rpm_high`` when no sample is valid. + The bracket starts at the propeller model's largest advance ratio, where a + table can already be out of data. The model gives a result as the RPM + rises, so the search moves the bracket's lower end onto the first RPM + with one. ``g(rpm_low)`` is not finite; the method returns ``rpm_high`` + when no sample is valid. """ samples = [rpm_low + (rpm_high - rpm_low) * index / 32 for index in range(33)] valid = next((index for index, rpm in enumerate(samples) if math.isfinite(g(rpm))), None) diff --git a/tests/test_blade_element.py b/tests/test_blade_element.py index 44be11e..0f33944 100644 --- a/tests/test_blade_element.py +++ b/tests/test_blade_element.py @@ -118,3 +118,30 @@ def test_solver_finds_an_operating_point(propeller_10x5): assert point.thrust_n == pytest.approx(thrust) assert point.torque_nm == pytest.approx(torque) assert 0.0 < point.propeller_efficiency < 1.0 + + +@pytest.mark.parametrize("airspeed", [0.0, 8.0, 15.0]) +def test_solver_needs_few_propeller_evaluations(propeller_10x5, airspeed): + """Negative thrust near the search's start is a result, not a gap to scan past.""" + calls = [] + + class Counted: + diameter_m = propeller_10x5.diameter_m + max_advance_ratio = propeller_10x5.max_advance_ratio + + def thrust_torque(self, rho, airspeed_mps, rpm): + calls.append(rpm) + return propeller_10x5.thrust_torque(rho, airspeed_mps, rpm) + + point = PropulsionSolver().solve_operating_point( + motor=MotorSpec(kv_rpm_per_v=1000.0, resistance_ohm=0.08, no_load_current_a=0.6, current_max_a=40.0), + battery=FixedVoltageBattery(voltage_v=11.1), + system=SystemSpec(resistance_ohm=0.02), + propeller=Counted(), + rho=RHO, + airspeed_mps=airspeed, + throttle=0.8, + ) + + assert point.is_feasible + assert len(calls) <= 20 diff --git a/tests/test_propeller_model.py b/tests/test_propeller_model.py index ea469d5..c7a02e2 100644 --- a/tests/test_propeller_model.py +++ b/tests/test_propeller_model.py @@ -38,11 +38,13 @@ def test_table_propeller_diameter_scales_with_unchanged_coefficients(): assert math.isclose(scaled_torque_nm, 2.0**5 * torque_nm) -def test_table_propeller_has_no_torque_past_its_data(): +def test_table_propeller_has_no_result_past_its_data(): propeller = _propeller() airspeed_mps = 1.0 * (6000.0 / 60.0) * propeller.diameter_m # J = 1.0, past the table's 0.8 - assert propeller.thrust_torque(RHO, airspeed_mps, 6000.0) == (0.0, 0.0) + thrust_n, torque_nm = propeller.thrust_torque(RHO, airspeed_mps, 6000.0) + assert math.isnan(thrust_n) and math.isnan(torque_nm) + assert propeller.get_coefficients(6000.0, 1.0) == (0.0, 0.0) class _ConstantCoefficientPropeller: diff --git a/tests/test_solver.py b/tests/test_solver.py index cb96f15..90ed0b2 100644 --- a/tests/test_solver.py +++ b/tests/test_solver.py @@ -417,3 +417,71 @@ def test_solver_preserves_first_infeasible_reason(test_setup): assert op.is_feasible is False assert op.infeasible_reason == "current_limit" + + +class _LinearPropeller: + """Ct and Cp fall linearly with the advance ratio and turn negative. + + Ct is zero at J = 0.667 and Cp at J = 0.833; the solver's search starts at + J = 1.2, where both are negative, as for a windmilling propeller. + """ + + diameter_m = 0.254 + max_advance_ratio = 1.2 + + def __init__(self): + self.torques = [] + + def thrust_torque(self, rho, airspeed_mps, rpm): + n = rpm / 60.0 + j = airspeed_mps / (n * self.diameter_m) + ct, cp = 0.10 - 0.15 * j, 0.05 - 0.06 * j + torque = cp * rho * n**2 * self.diameter_m**5 / (2.0 * math.pi) + self.torques.append(torque) + return ct * rho * n**2 * self.diameter_m**4, torque + + +_SEARCH_MOTOR = MotorSpec(kv_rpm_per_v=1000.0, resistance_ohm=0.08, no_load_current_a=0.6, current_max_a=40.0) + + +def _rate_map_3s(): + from pythrust import dataset_dir + + return RateMapBattery.from_json(dataset_dir() / "batteries" / "example_liion_cell.json", series=3, parallel=1) + + +def test_search_keeps_negative_torque_and_finds_the_root(): + propeller = _LinearPropeller() + + point = PropulsionSolver().solve_operating_point( + motor=_SEARCH_MOTOR, battery=FixedVoltageBattery(voltage_v=11.1), system=SystemSpec(resistance_ohm=0.02), + propeller=propeller, rho=1.225, airspeed_mps=20.0, throttle=0.9, + ) + + assert any(torque < 0.0 for torque in propeller.torques) + assert point.is_feasible + assert point.thrust_n > 0.0 + + +@pytest.mark.parametrize("throttle", [0.4, 0.6]) +def test_root_with_negative_thrust_is_infeasible(throttle): + point = PropulsionSolver().solve_operating_point( + motor=_SEARCH_MOTOR, battery=FixedVoltageBattery(voltage_v=11.1), system=SystemSpec(resistance_ohm=0.02), + propeller=_LinearPropeller(), rho=1.225, airspeed_mps=20.0, throttle=throttle, + ) + + assert point.thrust_n < 0.0 + assert not point.is_feasible + assert point.infeasible_reason == "invalid_coefficients" + + +def test_rate_map_battery_takes_negative_motor_current_in_the_search(): + point = PropulsionSolver().solve_operating_point( + motor=_SEARCH_MOTOR, battery=_rate_map_3s(), battery_state=BatteryState(soc=0.9), + system=SystemSpec(resistance_ohm=0.02), propeller=_LinearPropeller(), rho=1.225, airspeed_mps=20.0, + throttle=0.4, + ) + + assert point.torque_nm < 0.0 + assert point.motor_current_a < 0.0 + assert point.infeasible_reason == "invalid_coefficients" From ad514ba90aef41cc859861669c40deb4d650f128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Karakaya?= Date: Thu, 19 Nov 2026 10:41:25 +0300 Subject: [PATCH 2/2] docs(agents): point docs updates to the reference pages Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index be394e8..2bcfb1c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -52,7 +52,7 @@ Operating instructions for coding agents. Keep this file short and concrete. - Release: bump `__version__` in `pythrust/__init__.py` in a separate `chore(release): bump version to X.Y.Z` commit. Publishing a GitHub release uploads to PyPI. - SI units, with the unit as a field-name suffix (`resistance_ohm`, `diameter_m`, `kv_rpm_per_v`). - Solvers return an infeasible `OperatingPoint` with `infeasible_reason`; they do not raise for infeasible operating points. - - Every behavior change comes with a test, and with a docs update when it touches the public API (`docs/api_reference.md`, `docs/theory.md`). + - Every behavior change comes with a test, and with a docs update when it touches the public API (the component page under `docs/reference/`). ## Project Learnings