Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luca7084 committed Aug 23, 2023
1 parent 10eb163 commit f08d95a
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,6 @@ $RECYCLE.BIN/
**.html

# Command sequences
*.fmu
#*.fmu

# End of https://www.gitignore.io/api/git,linux,macos,python,windows,pycharm,jupyternotebook
3 changes: 0 additions & 3 deletions rocket_twin/drivers/mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ def __init__(
# Init and stop conditions
init_fuel = init
init_flight = {
"rocket.flying": True,
"controller.w_temp": 0.0,
"rocket.stage_1.controller.w_temp": 1.0,
"rocket.stage_1.tank.fuel.w_out_max": 3.0,
"g_tank.w_in": 0.0,
}
Expand Down
7 changes: 6 additions & 1 deletion rocket_twin/systems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from rocket_twin.systems.control import ControllerCoSApp, RocketControllerCoSApp, ControllerFMU, RocketControllerFMU
from rocket_twin.systems.control import (
ControllerCoSApp,
ControllerFMU,
RocketControllerCoSApp,
RocketControllerFMU,
)
from rocket_twin.systems.engine import Engine, EngineGeom, EnginePerfo
from rocket_twin.systems.ground import Ground
from rocket_twin.systems.physics import Dynamics
Expand Down
2 changes: 1 addition & 1 deletion rocket_twin/systems/control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from rocket_twin.systems.control.rocket_controller_cosapp import RocketControllerCoSApp

from rocket_twin.systems.control.controller_fmu import ControllerFMU # isort: skip
from rocket_twin.systems.control.rocket_controller_fmu import RocketControllerFMU # isort: skip
from rocket_twin.systems.control.rocket_controller_fmu import RocketControllerFMU # isort: skip

__all__ = ["ControllerCoSApp", "ControllerFMU", "RocketControllerCoSApp", "RocketControllerFMU"]
Binary file not shown.
4 changes: 2 additions & 2 deletions rocket_twin/systems/control/controller_cosapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def setup(self):

self.add_inward("is_on", 0, desc="Whether the fuel flow is allowed or not")

self.add_outward("w", 1., desc="Ratio of command fuel flow to maximum flow", unit="")
self.add_outward("w", 1.0, desc="Ratio of command fuel flow to maximum flow", unit="")

def compute(self):

self.w = self.is_on
self.w = self.is_on
Binary file not shown.
20 changes: 11 additions & 9 deletions rocket_twin/systems/control/rocket_controller_cosapp.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from cosapp.base import System

class RocketControllerCoSApp(System):

class RocketControllerCoSApp(System):
def setup(self, n_stages):

self.add_inward("n_stages", n_stages, desc="number of stages")
self.add_inward("stage", 1, desc="Current active stage")

for i in range(1, n_stages + 1):
self.add_inward(f"weight_prop_{i}", 0., desc=f"Stage {i} propellant weight", unit='kg')
self.add_inward(f"weight_max_{i}", 1., desc=f"Stage {i} maximum propellant weight", unit='kg')
self.add_inward(f"weight_prop_{i}", 0.0, desc=f"Stage {i} propellant weight", unit="kg")
self.add_inward(
f"weight_max_{i}", 1.0, desc=f"Stage {i} maximum propellant weight", unit="kg"
)
self.add_outward(f"is_on_{i}", 0, desc=f"Whether the stage {i} is on or not")

self.add_inward("time_int", 5., desc="Interval between fueling end and launch", unit='s')
self.add_inward("time_lnc", 100000., desc="Launch time", unit='s')
self.add_inward("time_int", 5.0, desc="Interval between fueling end and launch", unit="s")
self.add_inward("time_lnc", 100000.0, desc="Launch time", unit="s")

self.add_outward("fueling", 1, desc="Whether the rocket is fueling or not")
self.add_outward("flying", 0, desc="Whether the rocket is flying or not")

self.add_event("full", trigger = "weight_prop_1 == weight_max_1")
self.add_event("full", trigger="weight_prop_1 == weight_max_1")
self.add_event("launch", trigger="t == time_lnc")
self.add_event("drop", trigger="weight_prop_1 == 0.")

Expand All @@ -37,9 +39,9 @@ def transition(self):
self.is_on_1 = 1
if self.drop.present:
if self.stage < self.n_stages:
self[f'is_on_{self.stage}'] = 0
self[f"is_on_{self.stage}"] = 0
self.stage += 1
self[f'is_on_{self.stage}'] = 1
self[f"is_on_{self.stage}"] = 1
self.drop.trigger = f"weight_prop_{self.stage} == 0."
else:
self[f'is_on_{self.stage}'] = 0
self[f"is_on_{self.stage}"] = 0
10 changes: 4 additions & 6 deletions rocket_twin/systems/control/rocket_controller_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import rocket_twin.systems.control

class RocketControllerFMU(System):

class RocketControllerFMU(System):
def setup(self, n_stages, model_path, model_name):

self.add_inward("n_stages", n_stages, desc="number of stages")
Expand All @@ -18,7 +18,7 @@ def setup(self, n_stages, model_path, model_name):
self.add_inward("time_lnc", 100000.0, desc="Launch time", unit="")
self.add_transient("x", der="1")

pulling = {"flying" : "flying", "fueling" : "fueling", "tl" : "time_lnc", "ti" : "time_var"}
pulling = {"flying": "flying", "fueling": "fueling", "tl": "time_lnc", "ti": "time_var"}

for i in range(1, n_stages + 1):
self.add_outward(f"is_on_{i}", 0, desc=f"Whether the stage {i} is on or not")
Expand All @@ -32,7 +32,7 @@ def setup(self, n_stages, model_path, model_name):
pulling=pulling,
)

self.add_event("full", trigger = "weight_prop_1 > 0.9999*weight_max_1")
self.add_event("full", trigger="weight_prop_1 > 0.9999*weight_max_1")
self.add_event("drop", trigger="weight_prop_1 < 0.1")

def compute(self):
Expand All @@ -48,7 +48,7 @@ def transition(self):
else:
self.time_lnc = self.time + self.time_int
self.stage = 1

if self.drop.present:
if self.stage < self.n_stages:
self.stage += 1
Expand Down Expand Up @@ -86,5 +86,3 @@ def create_fmu(self, model_path, model_name):
os.remove(filename)

return fmu


19 changes: 14 additions & 5 deletions rocket_twin/systems/rocket/rocket.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from cosapp.base import System
from OCC.Core.GProp import GProp_GProps

from rocket_twin.systems import Dynamics, RocketControllerCoSApp
from rocket_twin.systems.rocket import OCCGeometry, Stage
from OCC.Core.GProp import GProp_GProps


class Rocket(System):
Expand Down Expand Up @@ -53,13 +53,23 @@ def setup(self, n_stages=1):
properties[i - 1] = f"stage_{i}"
forces[i - 1] = f"thrust_{i}"

self.add_child(RocketControllerCoSApp("controller", n_stages=n_stages), execution_index=0, pulling=['fueling', 'flying'])
self.add_child(
RocketControllerCoSApp("controller", n_stages=n_stages),
execution_index=0,
pulling=["fueling", "flying"],
)
self.add_child(OCCGeometry("geom", shapes=shapes, properties=properties))
self.add_child(Dynamics("dyn", forces=forces, weights=["weight_rocket"]), pulling=["a"])

for i in range(1, n_stages + 1):
self.connect(self.controller.outwards, self[f"stage_{i}"].inwards, {f"is_on_{i}" : "is_on"})
self.connect(self[f"stage_{i}"].outwards, self.controller.inwards, {"weight_prop" : f"weight_prop_{i}", "weight_max" : f"weight_max_{i}"})
self.connect(
self.controller.outwards, self[f"stage_{i}"].inwards, {f"is_on_{i}": "is_on"}
)
self.connect(
self[f"stage_{i}"].outwards,
self.controller.inwards,
{"weight_prop": f"weight_prop_{i}", "weight_max": f"weight_max_{i}"},
)
self.connect(self[f"stage_{i}"].outwards, self.geom.inwards, {"props": f"stage_{i}"})
self.connect(self[f"stage_{i}"].outwards, self.dyn.inwards, {"thrust": f"thrust_{i}"})

Expand All @@ -77,4 +87,3 @@ def transition(self):
self.geom[f"stage_{self.stage}"] = GProp_GProps()
self.dyn[f"thrust_{self.stage}"] = 0.0
self.stage += 1

2 changes: 1 addition & 1 deletion rocket_twin/systems/rocket/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setup(self, nose=False, wings=False):
shapes = ["tank_s", "engine_s", "tube_s"]
properties = ["tank", "engine", "tube"]

self.add_child(ControllerCoSApp("controller"), pulling=['is_on'])
self.add_child(ControllerCoSApp("controller"), pulling=["is_on"])
self.add_child(Tank("tank"), pulling=["w_in", "weight_max", "weight_prop"])
self.add_child(Engine("engine"), pulling={"force": "thrust"})
self.add_child(TubeGeom("tube"))
Expand Down
3 changes: 1 addition & 2 deletions rocket_twin/systems/station/station.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from cosapp.base import System
from OCC.Core.GProp import GProp_GProps

from rocket_twin.systems import ControllerCoSApp, Pipe, Rocket, Tank

Expand Down Expand Up @@ -27,7 +26,7 @@ def setup(self, n_stages=1):
self.connect(self.g_tank.outwards, self.pipe.inwards, {"w_out": "w_in"})
self.connect(self.pipe.outwards, self.rocket.inwards, {"w_out": "w_in_1"})

self.connect(self.rocket.outwards, self.controller.inwards, {'fueling' : 'is_on'})
self.connect(self.rocket.outwards, self.controller.inwards, {"fueling": "is_on"})
self.connect(self.controller.outwards, self.g_tank.inwards, {"w": "w_command"})

self.g_tank.geom.height = 2.0
Expand Down
29 changes: 17 additions & 12 deletions rocket_twin/tests/test_controller_cosapp.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
import numpy as np
from cosapp.drivers import NonLinearSolver, RungeKutta
from cosapp.recorders import DataFrameRecorder

from rocket_twin.systems import Station
from cosapp.drivers import RungeKutta, NonLinearSolver
from cosapp.recorders import DataFrameRecorder

class TestControllerCosapp:

class TestControllerCosapp:
def test_run_once(self):

sys = Station('sys', n_stages=3)
sys = Station("sys", n_stages=3)
sys.run_once()

def test_control(self):

sys = Station('sys', n_stages=3)
sys = Station("sys", n_stages=3)

init = {
"g_tank.fuel.weight_p": 20.0,
"g_tank.fuel.w_out_max": 1.0,
"rocket.stage_1.tank.fuel.w_out_max": 1.0,
"rocket.stage_2.tank.fuel.w_out_max": 1.0,
"rocket.stage_3.tank.fuel.w_out_max": 1.0,
"rocket.controller.time_int" : 5.
"rocket.controller.time_int": 5.0,
}

includes = [
"rocket.dyn.weight",
"rocket.weight_prop_1",
"rocket.weight_prop_2",
"rocket.weight_prop_3",
"rocket.a"
"rocket.a",
]

driver = sys.add_driver(RungeKutta("rk", order=4, dt=1))
solver = driver.add_child(NonLinearSolver('solver'))
solver = driver.add_child(NonLinearSolver("solver"))
driver.time_interval = (0, 35)
driver.set_scenario(init=init)
driver.add_recorder(DataFrameRecorder(includes=includes), period=1.0)

sys.run_drivers()

data = driver.recorder.export_data()
data1 = data.drop(["Section", "Status", "Error code", "rocket.weight_prop_2", "rocket.weight_prop_3"], axis=1)
data2 = data.drop(["Section", "Status", "Error code", "rocket.dyn.weight", "rocket.weight_prop_1"], axis=1)
data1 = data.drop(
["Section", "Status", "Error code", "rocket.weight_prop_2", "rocket.weight_prop_3"],
axis=1,
)
data2 = data.drop(
["Section", "Status", "Error code", "rocket.dyn.weight", "rocket.weight_prop_1"], axis=1
)
acel = np.asarray(data["rocket.a"])

print(data1)
print(data2)

np.testing.assert_allclose(sys.rocket.geom.weight, 4., atol=10 ** (0))
np.testing.assert_allclose(acel[-3], 40., atol=0.1)
np.testing.assert_allclose(sys.rocket.geom.weight, 4.0, atol=10 ** (0))
np.testing.assert_allclose(acel[-3], 40.0, atol=0.1)
37 changes: 22 additions & 15 deletions test_controller_fmu.py → rocket_twin/tests/test_controller_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,30 @@ def test_controller_fmu(self):
)
swap_system(
sys.rocket.controller,
RocketControllerFMU("controller", n_stages=n_stages, model_path=model_path_r, model_name=model_name_r),
RocketControllerFMU(
"controller", n_stages=n_stages, model_path=model_path_r, model_name=model_name_r
),
)
for i in range(1, n_stages + 1):
swap_system(
sys.rocket[f"stage_{i}"].controller,
ControllerFMU("controller", model_path=model_path, model_name=model_name)
ControllerFMU("controller", model_path=model_path, model_name=model_name),
)

driver = sys.add_driver(RungeKutta(order=4, time_interval=[0, 40], dt=0.1))
solver = driver.add_child(NonLinearSolver("solver"))
init = {"g_tank.fuel.weight_p": 20.0,
"rocket.stage_1.tank.fuel.weight_p": 0.0,
"rocket.stage_2.tank.fuel.weight_p": 0.0,
"rocket.stage_3.tank.fuel.weight_p": 0.0}
driver.add_child(NonLinearSolver("solver"))
init = {
"g_tank.fuel.weight_p": 20.0,
"rocket.stage_1.tank.fuel.weight_p": 0.0,
"rocket.stage_2.tank.fuel.weight_p": 0.0,
"rocket.stage_3.tank.fuel.weight_p": 0.0,
}
values = {
"g_tank.fuel.w_out_max": 1.0,
"rocket.controller.time_int": 5.0,
"rocket.stage_1.tank.fuel.w_out_max": 1.,
"rocket.stage_2.tank.fuel.w_out_max": 1.,
"rocket.stage_3.tank.fuel.w_out_max": 1.,
"rocket.stage_1.tank.fuel.w_out_max": 1.0,
"rocket.stage_2.tank.fuel.w_out_max": 1.0,
"rocket.stage_3.tank.fuel.w_out_max": 1.0,
}
driver.set_scenario(init=init, values=values)
driver.add_recorder(
Expand All @@ -53,12 +57,15 @@ def test_controller_fmu(self):
sys.run_drivers()
data = driver.recorder.export_data()
data1 = data.drop(["Section", "Status", "Error code", "rocket.a"], axis=1)
data2 = data.drop(["Section", "Status", "Error code", "rocket.geom.weight", "rocket.weight_prop_3"], axis=1)
data2 = data.drop(
["Section", "Status", "Error code", "rocket.geom.weight", "rocket.weight_prop_3"],
axis=1,
)

print(data1)
print(data2)

np.testing.assert_allclose(sys.rocket.geom.weight, 4., atol=10 ** (0))
np.testing.assert_allclose(sys.rocket.a, 40., rtol=0.1)
#np.testing.assert_allclose(sys.g_tank.weight_prop, 5.0, atol=10 ** (0))
#np.testing.assert_allclose(sys.rocket.stage_1.tank.weight_prop, 0.0, atol=10 ** (0))
np.testing.assert_allclose(sys.rocket.geom.weight, 4.0, atol=10 ** (0))
np.testing.assert_allclose(sys.rocket.a, 40.0, rtol=0.1)
# np.testing.assert_allclose(sys.g_tank.weight_prop, 5.0, atol=10 ** (0))
# np.testing.assert_allclose(sys.rocket.stage_1.tank.weight_prop, 0.0, atol=10 ** (0))
Loading

0 comments on commit f08d95a

Please sign in to comment.