Skip to content

Commit

Permalink
Pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luca7084 committed Jul 6, 2023
1 parent 8f1ff96 commit 431d7e0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion rocket_twin/systems/control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from rocket_twin.systems.control.clock import Clock

__all__ = ['Clock']
__all__ = ["Clock"]
19 changes: 15 additions & 4 deletions rocket_twin/systems/control/clock.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
from cosapp.base import System


class Clock(System):
"""A system that measures the elapsed time.
Inputs
------
Outputs
------
time_var: float,
the time since the beginning of the simulation
"""

def setup(self):

#Transient to ensure the system is visited in every time step
self.add_transient('x', der='1')
self.add_outward('time_var', 0., desc="Command time")
# Transient to ensure the system is visited in every time step
self.add_transient("x", der="1")
self.add_outward("time_var", 0.0, desc="Command time")

def compute(self):

self.time_var = self.time
self.time_var = self.time
26 changes: 16 additions & 10 deletions rocket_twin/systems/station/station.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from cosapp.base import System
from cosapp_fmu.FMUsystem import FMUSystem

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


class Station(System):
"""A space station composed by a rocket, a tank and a pipe connecting them.
Inputs
------
fmu_path: string,
the path to the .fmu file, if there is any
Outputs
------
Expand All @@ -23,12 +25,16 @@ def setup(self, fmu_path=None):
self.connect(self.pipe.outwards, self.rocket.inwards, {"w_out": "w_in"})

if fmu_path is not None:
self.add_child(Clock('clock'))
self.add_child(FMUSystem('controller', fmu_path=fmu_path))
self.connect(self.clock.outwards, self.controller.inwards, {'time_var' : 'ti'})
self.connect(self.controller.outwards, self.g_tank.inwards, {'wg' : 'w_command'})
self.connect(self.controller.outwards, self.rocket.inwards, {'wr' : 'w_command', 'f' : 'force_command'})

self.exec_order = ['clock', 'controller', 'g_tank', 'pipe', 'rocket']

self.g_tank.weight_max = 10.0
self.add_child(Clock("clock"))
self.add_child(FMUSystem("controller", fmu_path=fmu_path))
self.connect(self.clock.outwards, self.controller.inwards, {"time_var": "ti"})
self.connect(self.controller.outwards, self.g_tank.inwards, {"wg": "w_command"})
self.connect(
self.controller.outwards,
self.rocket.inwards,
{"wr": "w_command", "f": "force_command"},
)

self.exec_order = ["clock", "controller", "g_tank", "pipe", "rocket"]

self.g_tank.weight_max = 10.0
12 changes: 6 additions & 6 deletions rocket_twin/tests/test_command.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import numpy as np
from rocket_twin.systems import Clock
from cosapp.drivers import RungeKutta

class TestClock:
from rocket_twin.systems import Clock


class TestClock:
def test_time(self):

sys = Clock('sys')
sys.add_driver(RungeKutta(order=4,time_interval=[0, 15], dt=1.))
sys = Clock("sys")
sys.add_driver(RungeKutta(order=4, time_interval=[0, 15], dt=1.0))
sys.run_drivers()

np.testing.assert_allclose(sys.time_var, 15., atol=10**(-4))

np.testing.assert_allclose(sys.time_var, 15.0, atol=10 ** (-4))
4 changes: 2 additions & 2 deletions rocket_twin/tests/test_mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_run_once(self):
"g_tank.w_out_max": 3.0,
}


stop = "rocket.tank.weight_p <= 0."

includes = ["rocket.a", "g_tank.weight", "rocket.tank.weight_p"]
Expand All @@ -39,5 +38,6 @@ def test_run_once(self):
np.testing.assert_allclose(sys.rocket.tank.weight_p, 0.0, atol=10 ** (-10))
np.testing.assert_allclose(sys.g_tank.weight_p, 5.0, atol=10 ** (-10))


tm = TestMission()
tm.test_run_once()
tm.test_run_once()
30 changes: 16 additions & 14 deletions rocket_twin/tests/test_sequences_fmu.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import numpy as np
from rocket_twin.systems import Station
from cosapp.drivers import RungeKutta
from cosapp.recorders import DataFrameRecorder

class TestSequencesFMU:
from rocket_twin.systems import Station


class TestSequencesFMU:
def test_sequence_fmu(self):

sys = Station('sys', fmu_path="rocket_twin/systems/control/controller.fmu")
driver = sys.add_driver(RungeKutta(order=4,time_interval=[0, 15], dt=0.01))
init = {'g_tank.weight_p' : 10.,
'rocket.tank.weight_p' : 0.}
values = {'g_tank.w_out_max' : 1.,
'rocket.tank.w_out_max' : 0.5,
'rocket.engine.force_max' : 100.}
sys = Station("sys", fmu_path="rocket_twin/systems/control/controller.fmu")
driver = sys.add_driver(RungeKutta(order=4, time_interval=[0, 15], dt=0.01))
init = {"g_tank.weight_p": 10.0, "rocket.tank.weight_p": 0.0}
values = {
"g_tank.w_out_max": 1.0,
"rocket.tank.w_out_max": 0.5,
"rocket.engine.force_max": 100.0,
}
driver.set_scenario(init=init, values=values)
driver.add_recorder(DataFrameRecorder(includes=['rocket.dyn.a']), period=1.)
driver.add_recorder(DataFrameRecorder(includes=["rocket.dyn.a"]), period=1.0)
sys.run_drivers()

data=driver.recorder.export_data()
data = driver.recorder.export_data()
print(data)

np.testing.assert_allclose(sys.rocket.dyn.a, 40., atol=10**(-1))
np.testing.assert_allclose(sys.g_tank.weight_p, 5., atol=10**(-2))
np.testing.assert_allclose(sys.rocket.tank.weight_p, 0., atol=10**(-2))
np.testing.assert_allclose(sys.rocket.dyn.a, 40.0, atol=10 ** (-1))
np.testing.assert_allclose(sys.g_tank.weight_p, 5.0, atol=10 ** (-2))
np.testing.assert_allclose(sys.rocket.tank.weight_p, 0.0, atol=10 ** (-2))

0 comments on commit 431d7e0

Please sign in to comment.