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

Geometry #20

Closed
wants to merge 10 commits into from
Closed
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 environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dependencies:
- pre-commit
- cosapp
- numpy
- cosapp_fmu
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ flake8
isort
black
pre-commit
cosapp_fmu @ git+https://github.com/twiinIT/cosapp-fmu.git@master
2 changes: 1 addition & 1 deletion rocket_twin/drivers/mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
init_fuel = init
init_flight = {
"rocket.flying": True,
"rocket.engine.force_command": 1.0,
"rocket.force_command": 1.0,
"rocket.tank.w_out_max": 3.0,
"g_tank.w_in": 0.0,
"g_tank.w_command": 0.0,
Expand Down
4 changes: 3 additions & 1 deletion rocket_twin/systems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from rocket_twin.systems.control import Clock
from rocket_twin.systems.engine import Engine
from rocket_twin.systems.ground import Ground
from rocket_twin.systems.physics import Dynamics
from rocket_twin.systems.tank import Pipe, Tank
from rocket_twin.systems.structure import Nose, Tube, Wings

from rocket_twin.systems.rocket import Rocket # isort: skip
from rocket_twin.systems.station import Station # isort: skip

__all__ = ["Engine", "Tank", "Rocket", "Pipe", "Dynamics", "Station", "Ground"]
__all__ = ["Clock", "Engine", "Tank", "Rocket", "Pipe", "Dynamics", "Station", "Ground", "Nose", "Tube", "Wings"]
3 changes: 3 additions & 0 deletions rocket_twin/systems/control/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from rocket_twin.systems.control.clock import Clock

__all__ = ["Clock"]
24 changes: 24 additions & 0 deletions rocket_twin/systems/control/clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +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.0, desc="Command time")

def compute(self):

self.time_var = self.time
Binary file added rocket_twin/systems/control/controller.fmu
Binary file not shown.
11 changes: 8 additions & 3 deletions rocket_twin/systems/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class Engine(System):
Inputs
------
force_command: float,
External control, which inputs the % of the maximum force the engine outputs
external control, which inputs the % of the maximum force the engine outputs
w_out [kg/s]: float,
rate of fuel consumption

Outputs
------
Expand All @@ -21,15 +23,18 @@ class Engine(System):

def setup(self):

self.add_inward("force_max", 100.0, desc="Maximum engine force", unit="N")
#self.add_inward("force_max", 100.0, desc="Maximum engine force", unit="N")
self.add_inward(
"force_command", 1.0, desc="Ratio of command force to maximum force", unit=""
)
self.add_inward("isp", 100., desc="Specific impulsion in vacuum", unit='s')
self.add_inward("w_out", 0., desc="Fuel consumption rate", unit='kg/s')
self.add_inward("g_0", 9.80665, desc="Gravity at Earth's surface", unit='m/s**2')

self.add_outward("weight", 1.0, desc="weight", unit="kg")
self.add_outward("cg", 1.0, desc="Center of Gravity", unit="m")
self.add_outward("force", 1.0, desc="Thrust force", unit="N")

def compute(self):

self.force = self.force_max * self.force_command
self.force = self.isp * self.g_0 * self.w_out * self.force_command
17 changes: 12 additions & 5 deletions rocket_twin/systems/rocket/rocket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cosapp.base import System

from rocket_twin.systems import Dynamics, Engine, Tank
from rocket_twin.systems import Dynamics, Engine, Tank, Nose, Tube, Wings


class Rocket(System):
Expand All @@ -16,24 +16,31 @@ class Rocket(System):
"""

def setup(self):
self.add_child(Engine("engine"))
self.add_child(Tank("tank"), pulling=["w_in"])
self.add_child(Nose('nose'))
self.add_child(Tube('tube'))
self.add_child(Wings('wings'))
self.add_child(Engine("engine"), pulling=["force_command"])
self.add_child(Tank("tank"), pulling=["w_in", "w_command"])
self.add_child(
Dynamics(
"dyn",
forces=["thrust"],
weights=["weight_eng", "weight_tank"],
centers=["engine", "tank"],
weights=["weight_eng", "weight_tank", "weight_nose", "weight_tube", "weight_wings"],
centers=["engine", "tank", "nose", "tube", "engine"],
),
pulling=["a"],
)

self.connect(self.tank.outwards, self.engine.inwards, ['w_out'])
self.connect(
self.engine.outwards,
self.dyn.inwards,
{"force": "thrust", "weight": "weight_eng", "cg": "engine"},
)
self.connect(self.tank.outwards, self.dyn.inwards, {"weight": "weight_tank", "cg": "tank"})
self.connect(self.nose.outwards, self.dyn.inwards, {'weight' : 'weight_nose', 'cg' : 'nose'})
self.connect(self.tube.outwards, self.dyn.inwards, {'weight' : 'weight_tube', 'cg' : 'tube'})
self.connect(self.wings.outwards, self.dyn.inwards, {'weight' : 'weight_wings', 'cg' : 'wings'})

self.add_inward("flying", False, desc="Whether the rocket is flying or not", unit="")

Expand Down
20 changes: 18 additions & 2 deletions rocket_twin/systems/station/station.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
from cosapp.base import System
from cosapp_fmu.FMUsystem import FMUSystem

from rocket_twin.systems import Pipe, Rocket, Tank
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
------
"""

def setup(self):
def setup(self, fmu_path=None):
self.add_child(Tank("g_tank"))
self.add_child(Pipe("pipe"))
self.add_child(Rocket("rocket"))

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"})

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
5 changes: 5 additions & 0 deletions rocket_twin/systems/structure/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from rocket_twin.systems.structure.nose import Nose
from rocket_twin.systems.structure.tube import Tube
from rocket_twin.systems.structure.wings import Wings

__all__ = ['Nose', 'Tube', 'Wings']
20 changes: 20 additions & 0 deletions rocket_twin/systems/structure/nose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from cosapp.base import System

class Nose(System):
"""Model of a rocket nose.
Inputs
------
Outputs
------
weight [kg]: float,
nose weight
cg [m]: float,
nose center of gravity
"""

def setup(self):

self.add_outward('weight', 0., desc="Weight", unit='kg')
self.add_outward('cg', 0., desc="Center of gravity", unit='m')
20 changes: 20 additions & 0 deletions rocket_twin/systems/structure/tube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from cosapp.base import System

class Tube(System):
"""Model of a rocket tube.

Inputs
------

Outputs
------
weight [kg]: float,
tube weight
cg [m]: float,
tube center of gravity
"""

def setup(self):

self.add_outward('weight', 0., desc="Weight", unit='kg')
self.add_outward('cg', 0., desc="Center of gravity", unit='m')
20 changes: 20 additions & 0 deletions rocket_twin/systems/structure/wings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from cosapp.base import System

class Wings(System):
"""Model of the wings of a rocket.
Inputs
------
Outputs
------
weight [kg]: float,
wings weight
cg [m]: float,
wings center of gravity
"""

def setup(self):

self.add_outward('weight', 0., desc="Weight", unit='kg')
self.add_outward('cg', 0., desc="Center of gravity", unit='m')
14 changes: 14 additions & 0 deletions rocket_twin/tests/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np
from cosapp.drivers import RungeKutta

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.0))
sys.run_drivers()

np.testing.assert_allclose(sys.time_var, 15.0, atol=10 ** (-4))
2 changes: 1 addition & 1 deletion rocket_twin/tests/test_flying_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_run_once(self):

init = {
"rocket.flying": True,
"rocket.engine.force_command": 1.0,
"rocket.force_command": 1.0,
"rocket.tank.weight_p": "rocket.tank.weight_max",
"rocket.tank.w_out_max": 3.0,
"g_tank.w_in": 0.0,
Expand Down
6 changes: 5 additions & 1 deletion rocket_twin/tests/test_mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_run_once(self):

init = {
"g_tank.weight_p": "g_tank.weight_max",
"rocket.engine.force_command": 0.0,
"rocket.force_command": 0.0,
"rocket.tank.weight_p": 0.0,
"rocket.tank.w_out_max": 0.0,
"g_tank.w_command": 1.0,
Expand All @@ -37,3 +37,7 @@ def test_run_once(self):
np.testing.assert_allclose(sys.rocket.a, 40.0, atol=10 ** (-10))
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()
2 changes: 1 addition & 1 deletion rocket_twin/tests/test_refuel_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def test_run_once(self):
dt = 0.1

init = {
"rocket.engine.force_command": 0.0,
"rocket.tank.weight_p": 0.0,
"rocket.tank.w_out_max": 0.0,
"rocket.force_command": 0.0,
"g_tank.w_command": 1.0,
"g_tank.weight_p": "g_tank.weight_max",
"g_tank.w_in": 0.0,
Expand Down
28 changes: 28 additions & 0 deletions rocket_twin/tests/test_sequences_fmu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
from cosapp.drivers import RungeKutta
from cosapp.recorders import DataFrameRecorder

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.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.0)
sys.run_drivers()

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

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))
Loading