Skip to content

Commit 46725b9

Browse files
Merge pull request #441 from RocketPy-Team/enh/get-solution-at-time
ENH: new Flight.get_solution_at_time() method
2 parents eb44ff8 + 422bb51 commit 46725b9

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

rocketpy/simulation/flight.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,37 @@ def time(self):
19341934
"""Returns time array from solution."""
19351935
return self.solution_array[:, 0]
19361936

1937+
def get_solution_at_time(self, t, atol=1e-3):
1938+
"""Returns the solution state vector at a given time. If the time is
1939+
not found in the solution, the closest time is used and a warning is
1940+
raised.
1941+
1942+
Parameters
1943+
----------
1944+
t : float
1945+
Time in seconds.
1946+
atol : float, optional
1947+
Absolute tolerance for time comparison. Default is 1e-3. If the
1948+
difference between the time and the closest time in the solution
1949+
is greater than this value, a warning is raised.
1950+
1951+
Returns
1952+
-------
1953+
solution : np.array
1954+
Solution state at time t. The array follows the format of the
1955+
solution array, with the first column being time like this:
1956+
[t, x, y, z, vx, vy, vz, e0, e1, e2, e3, omega1, omega2, omega3].
1957+
1958+
"""
1959+
time_index = find_closest(self.time, t)
1960+
if abs(self.time[time_index] - t) > atol:
1961+
warnings.warn(
1962+
f"Time {t} not found in solution. Closest time is "
1963+
f"{self.time[time_index]}. Using closest time.",
1964+
UserWarning,
1965+
)
1966+
return self.solution_array[time_index, :]
1967+
19371968
# Process first type of outputs - state vector
19381969
# Transform solution array into Functions
19391970
@funcify_method("Time (s)", "X (m)", "spline", "constant")

tests/test_flight.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,48 @@ def test_initial_solution(mock_show, example_env, calisto_robust):
141141
assert test_flight.all_info() == None
142142

143143

144+
def test_get_solution_at_time(flight_calisto):
145+
"""Test the get_solution_at_time method of the Flight class. This test
146+
simply calls the method at the initial and final time and checks if the
147+
returned values are correct.
148+
149+
Parameters
150+
----------
151+
flight_calisto : rocketpy.Flight
152+
Flight object to be tested. See the conftest.py file for more info
153+
regarding this pytest fixture.
154+
"""
155+
assert np.allclose(
156+
flight_calisto.get_solution_at_time(0),
157+
np.array([0, 0, 0, 0, 0, 0, 0, 0.99904822, -0.04361939, 0, 0, 0, 0, 0]),
158+
rtol=1e-05,
159+
atol=1e-08,
160+
)
161+
assert np.allclose(
162+
flight_calisto.get_solution_at_time(flight_calisto.t_final),
163+
np.array(
164+
[
165+
48.4313533,
166+
0.0,
167+
985.7665845,
168+
-0.00000229951048,
169+
0.0,
170+
11.2223284,
171+
-341.028803,
172+
0.999048222,
173+
-0.0436193874,
174+
0.0,
175+
0.0,
176+
0.0,
177+
0.0,
178+
0.0,
179+
]
180+
),
181+
rtol=1e-02,
182+
atol=5e-03,
183+
)
184+
185+
144186
@pytest.mark.parametrize("wind_u, wind_v", [(0, 10), (0, -10), (10, 0), (-10, 0)])
145187
@pytest.mark.parametrize(
146188
"static_margin, max_time",

0 commit comments

Comments
 (0)