Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Precalculate Barometric Height #511

Merged
merged 14 commits into from
Jan 25, 2024
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
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function-naming-style=snake_case
# Good variable names which should always be accepted, separated by a comma.
good-names=FlightPhases,
WindroseAxes,
barometric_height_ISA,


# Good variable names regexes, separated by a comma. If names match any regex,
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ straightforward as possible.
- MNT: Add repr method to Parachute class [#490](https://github.com/RocketPy-Team/RocketPy/pull/490)
- ENH: Function Reverse Arithmetic Priority [#488](https://github.com/RocketPy-Team/RocketPy/pull/488)
- DOC: Update Header Related Docs
- ENH Precalculate Barometric Height [#511](https://github.com/RocketPy-Team/RocketPy/pull/511)
- MNT: Encapsulate quaternion conversions [#537](https://github.com/RocketPy-Team/RocketPy/pull/537)
- MNT: improve the low pass filter and document an example [#538](https://github.com/RocketPy-Team/RocketPy/pull/538)


### Fixed

- ENH: Parachute trigger doesn't work if "Apogee" is used instead of "apogee" [#489](https://github.com/RocketPy-Team/RocketPy/pull/489)
Expand Down
67 changes: 66 additions & 1 deletion rocketpy/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class Environment:
Environment.pressure : Function
Air pressure in Pa as a function of altitude. Can be accessed as regular
array, or called as a Function. See Function for more information.
Environment.barometric_height : Function
MateusStano marked this conversation as resolved.
Show resolved Hide resolved
Geometric height above sea level in m as a function of pressure. Can be
accessed as regular array, or called as a Function. See Function for
more information.
Environment.temperature : Function
Air temperature in K as a function of altitude. Can be accessed as
regular array, or called as a Function. See Function for more
Expand Down Expand Up @@ -1315,6 +1319,8 @@ def process_standard_atmosphere(self):

# Save temperature, pressure and wind profiles
self.pressure = self.pressure_ISA
self.barometric_height = self.barometric_height_ISA

self.temperature = self.temperature_ISA
self.wind_direction = Function(
0,
Expand Down Expand Up @@ -1433,6 +1439,7 @@ def process_custom_atmosphere(
if pressure is None:
# Use standard atmosphere
self.pressure = self.pressure_ISA
self.barometric_height = self.barometric_height_ISA
else:
# Use custom input
self.pressure = Function(
Expand All @@ -1441,6 +1448,11 @@ def process_custom_atmosphere(
outputs="Pressure (Pa)",
interpolation="linear",
)
self.barometric_height = self.pressure.inverse_function().set_discrete(
0, max_expected_height, 100, extrapolation="constant"
)
self.barometric_height.set_inputs("Pressure (Pa)")
self.barometric_height.set_outputs("Height Above Sea Level (m)")
MateusStano marked this conversation as resolved.
Show resolved Hide resolved
# Check maximum height of custom pressure input
if not callable(self.pressure.source):
max_expected_height = max(self.pressure[-1, 0], max_expected_height)
Expand Down Expand Up @@ -1605,6 +1617,15 @@ def process_windy_atmosphere(self, model="ECMWF"):
outputs="Pressure (Pa)",
interpolation="linear",
)
# Linearly extrapolate pressure to ground level
bar_height = data_array[:, (0, 1)]
self.barometric_height = Function(
bar_height,
inputs="Pressure (Pa)",
outputs="Height Above Sea Level (m)",
interpolation="linear",
extrapolation="natural",
)
self.temperature = Function(
data_array[:, (1, 2)],
inputs="Height Above Sea Level (m)",
Expand Down Expand Up @@ -1732,6 +1753,15 @@ def process_wyoming_sounding(self, file):
outputs="Pressure (Pa)",
interpolation="linear",
)
# Linearly extrapolate pressure to ground level
bar_height = data_array[:, (0, 1)]
self.barometric_height = Function(
bar_height,
inputs="Pressure (Pa)",
outputs="Height Above Sea Level (m)",
interpolation="linear",
extrapolation="natural",
)

# Retrieve temperature from data array
data_array[:, 2] = data_array[:, 2] + 273.15 # Converts C to K
Expand Down Expand Up @@ -1845,6 +1875,7 @@ def process_noaaruc_sounding(self, file):

# Extract pressure as a function of height
pressure_array = []
barometric_height_array = []
for line in lines:
# Split line into columns
columns = re.split(" +", line)[1:]
Expand All @@ -1858,7 +1889,9 @@ def process_noaaruc_sounding(self, file):
if max(columns) != 99999:
# Save value
pressure_array.append(columns)
barometric_height_array.append([columns[1], columns[0]])
pressure_array = np.array(pressure_array)
barometric_height_array = np.array(barometric_height_array)

# Extract temperature as a function of height
temperature_array = []
Expand Down Expand Up @@ -1905,6 +1938,15 @@ def process_noaaruc_sounding(self, file):
outputs="Pressure (Pa)",
interpolation="linear",
)
# Converts 10*hPa to Pa and save values
barometric_height_array[:, 0] = 10 * barometric_height_array[:, 0]
MateusStano marked this conversation as resolved.
Show resolved Hide resolved
self.barometric_height = Function(
barometric_height_array,
inputs="Pressure (Pa)",
outputs="Height Above Sea Level (m)",
interpolation="linear",
extrapolation="natural",
)

# Convert 10*C to K and save values
temperature_array[:, 1] = (
Expand Down Expand Up @@ -2274,6 +2316,15 @@ def process_forecast_reanalysis(self, file, dictionary):
outputs="Pressure (Pa)",
interpolation="linear",
)
# Linearly extrapolate pressure to ground level
bar_height = data_array[:, (0, 1)]
self.barometric_height = Function(
bar_height,
inputs="Pressure (Pa)",
outputs="Height Above Sea Level (m)",
interpolation="linear",
extrapolation="natural",
)
self.temperature = Function(
data_array[:, (1, 2)],
inputs="Height Above Sea Level (m)",
Expand Down Expand Up @@ -2803,6 +2854,15 @@ def select_ensemble_member(self, member=0):
outputs="Pressure (Pa)",
interpolation="linear",
)
# Linearly extrapolate pressure to ground level
bar_height = data_array[:, (0, 1)]
self.barometric_height = Function(
bar_height,
inputs="Pressure (Pa)",
outputs="Height Above Sea Level (m)",
interpolation="linear",
extrapolation="natural",
)
self.temperature = Function(
data_array[:, (1, 2)],
inputs="Height Above Sea Level (m)",
Expand Down Expand Up @@ -2965,7 +3025,12 @@ def pressure_function(h):
outputs="Pressure (Pa)",
)

return None
# Discretize Function to speed up the trajectory simulation.
self.barometric_height_ISA = self.pressure_ISA.inverse_function().set_discrete(
pressure[-1], pressure[0], 100, extrapolation="constant"
)
self.barometric_height_ISA.set_inputs("Pressure (Pa)")
self.barometric_height_ISA.set_outputs("Height Above Sea Level (m)")

def calculate_density_profile(self):
"""Compute the density of the atmosphere as a function of
Expand Down
10 changes: 2 additions & 8 deletions rocketpy/simulation/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,7 @@ def __init__(
parachute.noisy_pressure_signal.append([node.t, pressure + noise])
# Gets height above ground level considering noise
hAGL = (
self.env.pressure.find_input(
pressure + noise,
self.y_sol[2],
)
self.env.barometric_height(pressure + noise)
- self.env.elevation
)
if parachute.triggerfunc(pressure + noise, hAGL, self.y_sol):
Expand Down Expand Up @@ -1011,10 +1008,7 @@ def __init__(
)
# Gets height above ground level considering noise
hAGL = (
self.env.pressure.find_input(
pressure + noise,
overshootable_node.y[2],
)
self.env.barometric_height(pressure + noise)
- self.env.elevation
)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_standard_atmosphere(mock_show, example_env):
assert example_env.info() == None
assert example_env.all_info() == None
assert abs(example_env.pressure(0) - 101325.0) < 1e-8
assert abs(example_env.barometric_height(101325.0)) < 1e-2
assert example_env.prints.print_earth_details() == None


Expand All @@ -43,6 +44,7 @@ def test_custom_atmosphere(mock_show, example_env):
)
assert example_env.all_info() == None
assert abs(example_env.pressure(0) - 101325.0) < 1e-8
assert abs(example_env.barometric_height(101325.0)) < 1e-2
assert abs(example_env.wind_velocity_x(0) - 5) < 1e-8
assert abs(example_env.temperature(100) - 300) < 1e-8

Expand Down Expand Up @@ -71,6 +73,7 @@ def test_wyoming_sounding_atmosphere(mock_show, example_env):
pass
assert example_env.all_info() == None
assert abs(example_env.pressure(0) - 93600.0) < 1e-8
assert abs(example_env.barometric_height(example_env.pressure(0)) - 722.0) < 1e-8
assert abs(example_env.wind_velocity_x(0) - -2.9005178894925043) < 1e-8
assert abs(example_env.temperature(100) - 291.75) < 1e-8

Expand Down
Loading