Skip to content
Open
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
17 changes: 15 additions & 2 deletions rocketpy/rocket/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,8 +1048,10 @@ def evaluate_com_to_cdm_function(self):
-----
1. The `com_to_cdm_function` plus `center_of_mass` should be equal
to `center_of_dry_mass_position` at every time step.
2. The `com_to_cdm_function` is a function of time and will usually
already be discretized.
2. The function uses 1000 linear samples over the burn interval,
sampled mass/position domains and declared tank-flux intervals.
Constant extrapolation applies outside that range. Its second
derivative between samples is zero.

Returns
-------
Expand All @@ -1066,6 +1068,17 @@ def evaluate_com_to_cdm_function(self):
* self.motor.propellant_mass
/ self.total_mass
)
bounds = list(self.motor.burn_time)
for function in (
self.motor.propellant_mass,
self.center_of_propellant_position,
):
if function.is_array_source():
bounds.extend(function.x_array[[0, -1]])
for positioned_tank in getattr(self.motor, "positioned_tanks", []):
bounds.extend(positioned_tank["tank"].flux_time)
start, end = min(bounds), max(bounds)
self.com_to_cdm_function.set_discrete(start, end, 1000, "linear", "constant")
self.com_to_cdm_function.set_inputs("Time (s)")
self.com_to_cdm_function.set_outputs("Z Coordinate COM to CDM (m)")
self.com_to_cdm_function.set_title("Z Coordinate COM to CDM")
Expand Down
73 changes: 73 additions & 0 deletions tests/integration/simulation/test_cm_rate_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Airborne ignition/burnout checks for the sampled CM-offset approach."""

import numpy as np
import pytest

from rocketpy import CylindricalTank, Flight, Fluid, HybridMotor, MassFlowRateBasedTank


@pytest.fixture
def near_boundary_rocket(calisto_motorless, request):
"""Build a hybrid whose tank and motor endpoint differ by one ULP."""
endpoint, direction = request.param
times = [1.0, 11.0]
times[endpoint] = np.nextafter(times[endpoint], direction)
tank = MassFlowRateBasedTank(
name="review tank",
geometry=CylindricalTank(0.1, 1),
flux_time=times,
liquid=Fluid("liquid", 1000),
gas=Fluid("gas", 1),
initial_liquid_mass=3,
initial_gas_mass=0.01,
liquid_mass_flow_rate_in=0,
liquid_mass_flow_rate_out=0.1,
gas_mass_flow_rate_in=0,
gas_mass_flow_rate_out=0,
)
motor = HybridMotor(
thrust_source=lambda t: 2000 - 100 * (t - 1),
burn_time=(1, 11),
dry_mass=8,
dry_inertia=(4, 4, 0.1),
center_of_dry_mass_position=0,
grain_density=1700,
grain_number=4,
grain_initial_height=0.1,
grain_separation=0,
grain_initial_inner_radius=0.04,
grain_outer_radius=0.1,
grains_center_of_mass_position=-0.1,
nozzle_position=-0.4,
nozzle_radius=0.07,
)
motor.add_tank(tank, position=0.3)
calisto_motorless.add_motor(motor, position=-1.373)
return calisto_motorless


@pytest.mark.parametrize(
"near_boundary_rocket",
[(0, -np.inf), (0, np.inf), (1, -np.inf), (1, np.inf)],
indirect=True,
)
def test_flight_near_equal_boundaries(near_boundary_rocket, example_plain_env):
"""Cross both endpoints in flight without the former acceleration spikes."""
state = [0, 0, 100, 0, 0, 30, 1, 0, 0, 0, 0, 0, 0]
flight = Flight(
rocket=near_boundary_rocket,
environment=example_plain_env,
rail_length=1,
initial_solution=[0.99, *state],
max_time=11.5,
verbose=False,
)

assert flight.solution[-1][0] == pytest.approx(11.5)
assert np.isfinite(flight.solution).all()
# About 2 kN thrust and at least 22 kg dry mass in this fixture: this
# allows its normal flight but rejects the former thousands of m/s².
assert flight.max_acceleration < 100
for time in near_boundary_rocket.motor.burn_time:
rhs = flight.u_dot_generalized(time, state)
assert np.linalg.norm(rhs[3:6]) < 100
52 changes: 50 additions & 2 deletions tests/unit/rocket/test_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,61 @@ def test_evaluate_nozzle_gyration_tensor_parallel_axis_term(calisto):

def test_evaluate_com_to_cdm_function(calisto):
atol = 1e-3 # Equivalent to 1mm
times = calisto.com_to_cdm_function.x_array
expected = calisto.center_of_dry_mass_position - calisto.center_of_mass
assert np.allclose(
(calisto.center_of_dry_mass_position - calisto.center_of_mass).source,
calisto.com_to_cdm_function.source,
expected(times),
calisto.com_to_cdm_function(times),
atol=atol,
)


@pytest.mark.parametrize(
"rocket_fixture",
["calisto_motorless", "calisto", "calisto_liquid_modded", "calisto_hybrid_modded"],
)
def test_com_to_cdm_function_linear_samples(request, rocket_fixture):
"""Use a finite linear table for empty, solid, liquid and hybrid motors."""
rocket = request.getfixturevalue(rocket_fixture)
offset = rocket.com_to_cdm_function

assert offset.source.shape == (1000, 2)
assert offset.get_interpolation_method() == "linear"
assert offset.get_extrapolation_method() == "constant"
assert np.isfinite(offset.source).all()
for time in rocket.motor.burn_time:
assert np.isfinite(offset.differentiate_complex_step(time))
assert offset.differentiate(time, order=2) == 0


def test_com_to_cdm_function_covers_tank_flux(calisto_hybrid_modded, oxidizer_tank):
"""Do not freeze the offset while a tank continues flowing after burnout."""
rocket = calisto_hybrid_modded
offset = rocket.com_to_cdm_function
end = oxidizer_tank.flux_time[-1]
times = np.linspace(rocket.motor.burn_out_time, end, 20)

assert offset.x_array[-1] == end
np.testing.assert_allclose(
offset(times),
rocket.center_of_dry_mass_position - rocket.center_of_mass(times),
rtol=0,
atol=1e-3,
)


def test_com_to_cdm_function_covers_sampled_mass(calisto_motorless):
"""Include declared mass data outside the thrust interval in the table."""
rocket = calisto_motorless
rocket.motor.propellant_mass = Function([[-1, 4], [2, 2]])
rocket.total_mass = rocket.dry_mass + rocket.motor.propellant_mass
rocket.center_of_propellant_position = Function(-0.3)

offset = rocket.evaluate_com_to_cdm_function()

np.testing.assert_array_equal(offset.x_array[[0, -1]], [-1, 2])


def test_get_inertia_tensor_at_time(calisto):
# Expected values (for t = 0)
# TODO: compute these values by hand or using CAD.
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/simulation/test_udot_rail2_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ def test_udot_rail2_reports_the_accelerations_it_integrates():
assert abs(u_dot[10]) > 0.1
assert flight.alpha1(time) == pytest.approx(u_dot[10], rel=1e-9)
assert flight.alpha2(time) == pytest.approx(u_dot[11], rel=1e-9)
assert flight.alpha3(time) == 0
assert u_dot[12] == 0
# Interpolation may leave roundoff even at a stored zero sample.
assert flight.alpha3(time) == pytest.approx(u_dot[12], abs=1e-12)
assert flight.ax(time) == pytest.approx(u_dot[3], abs=1e-9)
assert flight.ay(time) == pytest.approx(u_dot[4], rel=1e-9)
assert flight.az(time) == pytest.approx(u_dot[5], rel=1e-9)
Expand Down