Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/blade_element_propeller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion docs/reference/propellers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions docs/reference/propulsion_solver.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/reference/table_propeller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pythrust/propeller/blade_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pythrust/propeller/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
...
8 changes: 8 additions & 0 deletions pythrust/propeller/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pythrust/propulsion/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_blade_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions tests/test_propeller_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 68 additions & 0 deletions tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading