Skip to content

Commit 85c1988

Browse files
MateusStanoclaude
authored andcommitted
BUG: let the tanks accept in compose the error their own checks accept
The tank checks accept an error of up to 1e-6 m³ in the volumes and 1e-4 m in the heights, but Function.compose only accepted 1e-9 of the domain. An input between the two passed the tank checks and then stopped at the domain error of compose, in all four tank types. The six compose calls now pass the tolerance of the tank checks, so a larger error is still reported by the tank as underfilled or overfilled. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent a46917c commit 85c1988

2 files changed

Lines changed: 121 additions & 7 deletions

File tree

‎rocketpy/motors/tank.py‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,18 @@ def inertia(self):
511511
"""
512512
return self.liquid_inertia + self.gas_inertia
513513

514+
@property
515+
def _volume_tolerance(self):
516+
"""Volume error the tank checks accept (1e-6 m³), as a fraction of the
517+
tank volume. Passed to Function.compose so both accept the same."""
518+
return 1e-6 / self.geometry.total_volume
519+
520+
@property
521+
def _height_tolerance(self):
522+
"""Height error the tank checks accept (1e-4 m), as a fraction of the
523+
tank height. Passed to Function.compose so both accept the same."""
524+
return 1e-4 / (self.geometry.top - self.geometry.bottom)
525+
514526
def _check_volume_bounds(self):
515527
"""Checks if the tank is overfilled or underfilled. Raises a ValueError
516528
if either the `gas_volume` or `liquid_volume` are out of tank geometry
@@ -952,7 +964,9 @@ def liquid_height(self):
952964
Function
953965
Height of the ullage as a function of time.
954966
"""
955-
liquid_height = self.geometry.inverse_volume.compose(self.liquid_volume)
967+
liquid_height = self.geometry.inverse_volume.compose(
968+
self.liquid_volume, tol=self._volume_tolerance
969+
)
956970
diff_bt = liquid_height - self.geometry.bottom
957971
diff_up = liquid_height - self.geometry.top
958972

@@ -990,7 +1004,9 @@ def gas_height(self):
9901004
Height of the ullage as a function of time.
9911005
"""
9921006
fluid_volume = self.gas_volume + self.liquid_volume
993-
gas_height = self.geometry.inverse_volume.compose(fluid_volume)
1007+
gas_height = self.geometry.inverse_volume.compose(
1008+
fluid_volume, tol=self._volume_tolerance
1009+
)
9941010
diff = gas_height - self.geometry.top
9951011
if (diff > 0).any():
9961012
raise ValueError(
@@ -1251,7 +1267,9 @@ def liquid_height(self):
12511267
Function
12521268
Height of the ullage as a function of time.
12531269
"""
1254-
return self.geometry.inverse_volume.compose(self.liquid_volume)
1270+
return self.geometry.inverse_volume.compose(
1271+
self.liquid_volume, tol=self._volume_tolerance
1272+
)
12551273

12561274
@funcify_method("Time (s)", "Gas Height (m)", "linear")
12571275
def gas_height(self):
@@ -1445,7 +1463,9 @@ def liquid_volume(self):
14451463
Function
14461464
Volume of the liquid as a function of time.
14471465
"""
1448-
return self.geometry.volume.compose(self.liquid_height)
1466+
return self.geometry.volume.compose(
1467+
self.liquid_height, tol=self._height_tolerance
1468+
)
14491469

14501470
@funcify_method("Time (s)", "Gas Volume (m³)")
14511471
def gas_volume(self):
@@ -1754,7 +1774,9 @@ def liquid_height(self):
17541774
Function
17551775
Height of the ullage as a function of time.
17561776
"""
1757-
liquid_height = self.geometry.inverse_volume.compose(self.liquid_volume)
1777+
liquid_height = self.geometry.inverse_volume.compose(
1778+
self.liquid_volume, tol=self._volume_tolerance
1779+
)
17581780
diff_bt = liquid_height - self.geometry.bottom
17591781
diff_up = liquid_height - self.geometry.top
17601782

@@ -1790,7 +1812,9 @@ def gas_height(self):
17901812
Height of the ullage as a function of time.
17911813
"""
17921814
fluid_volume = self.gas_volume + self.liquid_volume
1793-
gas_height = self.geometry.inverse_volume.compose(fluid_volume)
1815+
gas_height = self.geometry.inverse_volume.compose(
1816+
fluid_volume, tol=self._volume_tolerance
1817+
)
17941818
diff = gas_height - self.geometry.top
17951819
if (diff > 0).any():
17961820
raise ValueError(

‎tests/unit/motors/test_tank.py‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
import pytest
77
import scipy.integrate as spi
88

9-
from rocketpy import CylindricalTank, Fluid, MassFlowRateBasedTank
9+
from rocketpy import (
10+
CylindricalTank,
11+
Fluid,
12+
LevelBasedTank,
13+
MassBasedTank,
14+
MassFlowRateBasedTank,
15+
UllageBasedTank,
16+
)
1017

1118
BASE_PATH = Path("./data/rockets/berkeley/")
1219

@@ -538,3 +545,86 @@ def test_mass_flow_rate_tank_exact_depletion():
538545

539546
gas_height = tank.gas_height(time_points)
540547
assert np.all(np.isfinite(gas_height))
548+
549+
550+
def _tank_with_error(tank_type, error):
551+
"""Build a 1.2 m tall, 0.1 m radius tank whose input is off by ``error``.
552+
553+
The error is a mass of liquid drained beyond empty (kg), a gas volume
554+
beyond the tank volume (m³), a liquid level above the tank top (m) or a
555+
liquid mass beyond a full tank (kg), depending on the tank type.
556+
"""
557+
geometry = CylindricalTank(radius_function=0.1, height=1.2, spherical_caps=False)
558+
fluids = {
559+
"liquid": Fluid(name="water", density=1000),
560+
"gas": Fluid(name="air", density=1.225),
561+
}
562+
if tank_type == "mass_flow_rate":
563+
return MassFlowRateBasedTank(
564+
name="tank",
565+
geometry=geometry,
566+
flux_time=5.0,
567+
initial_liquid_mass=32.0,
568+
initial_gas_mass=0,
569+
liquid_mass_flow_rate_in=0,
570+
liquid_mass_flow_rate_out=(32.0 + error) / 5.0,
571+
gas_mass_flow_rate_in=0,
572+
gas_mass_flow_rate_out=0,
573+
**fluids,
574+
)
575+
if tank_type == "ullage":
576+
ullage = [[0.0, 0.0], [5.0, geometry.total_volume + error]]
577+
return UllageBasedTank(
578+
name="tank",
579+
geometry=geometry,
580+
flux_time=5.0,
581+
ullage=np.array(ullage),
582+
discretize=11,
583+
**fluids,
584+
)
585+
if tank_type == "level":
586+
level = [[0.0, geometry.top + error], [5.0, 0.0]]
587+
return LevelBasedTank(
588+
name="tank",
589+
geometry=geometry,
590+
flux_time=5.0,
591+
liquid_height=np.array(level),
592+
discretize=11,
593+
**fluids,
594+
)
595+
full_mass = geometry.total_volume * 1000
596+
return MassBasedTank(
597+
name="tank",
598+
geometry=geometry,
599+
flux_time=5.0,
600+
liquid_mass=np.array([[0.0, full_mass + error], [5.0, 0.0]]),
601+
gas_mass=0,
602+
discretize=11,
603+
**fluids,
604+
)
605+
606+
607+
@pytest.mark.parametrize(
608+
"tank_type, error",
609+
[("mass_flow_rate", 5e-7), ("ullage", 5e-7), ("level", 5e-5), ("mass", 5e-4)],
610+
)
611+
def test_tank_accepts_the_error_its_own_checks_accept(tank_type, error):
612+
"""An input off by less than the tank checks accept (1e-6 m³ of volume,
613+
1e-4 m of height) builds the tank. It used to pass those checks and then
614+
stop at the domain check of ``Function.compose``."""
615+
tank = _tank_with_error(tank_type, error)
616+
617+
assert np.all(np.isfinite(tank.liquid_height.y_array))
618+
assert np.all(np.isfinite(tank.liquid_volume.y_array))
619+
assert np.all(np.isfinite(tank.gas_height.y_array))
620+
621+
622+
@pytest.mark.parametrize(
623+
"tank_type, error",
624+
[("mass_flow_rate", 1e-2), ("ullage", 1e-4), ("level", 1e-3), ("mass", 1.0)],
625+
)
626+
def test_tank_names_itself_when_the_error_is_too_large(tank_type, error):
627+
"""A larger error is reported as an underfilled or overfilled tank, not as
628+
a value outside the domain of a Function."""
629+
with pytest.raises(ValueError, match="underfilled|overfilled"):
630+
_tank_with_error(tank_type, error)

0 commit comments

Comments
 (0)