Skip to content

Commit

Permalink
Changed fmu control loop to speed execution
Browse files Browse the repository at this point in the history
  • Loading branch information
luca7084 committed Aug 24, 2023
1 parent d1dc782 commit f65ff89
Show file tree
Hide file tree
Showing 24 changed files with 314 additions and 115 deletions.
6 changes: 4 additions & 2 deletions rocket_twin/systems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from rocket_twin.systems.control import (
ControllerFMU,
RocketControllerCoSApp,
RocketControllerFMU,
StageControllerCoSApp,
StageControllerFMU,
StationControllerCoSApp,
StationControllerFMU
)
from rocket_twin.systems.engine import Engine, EngineGeom, EnginePerfo
from rocket_twin.systems.ground import Ground
Expand All @@ -30,7 +31,8 @@
"StageControllerCoSApp",
"StationControllerCoSApp",
"RocketControllerCoSApp",
"ControllerFMU",
"StageControllerFMU",
"StationControllerFMU",
"RocketControllerFMU",
"NoseGeom",
"TubeGeom",
Expand Down
6 changes: 4 additions & 2 deletions rocket_twin/systems/control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from rocket_twin.systems.control.stage_controller_cosapp import StageControllerCoSApp
from rocket_twin.systems.control.station_controller_cosapp import StationControllerCoSApp

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.stage_controller_fmu import StageControllerFMU # isort: skip
from rocket_twin.systems.control.station_controller_fmu import StationControllerFMU # isort: skip

__all__ = [
"StageControllerCoSApp",
"StationControllerCoSApp",
"ControllerFMU",
"RocketControllerCoSApp",
"StageControllerFMU",
"StationControllerFMU",
"RocketControllerFMU",
]
11 changes: 0 additions & 11 deletions rocket_twin/systems/control/controller.mo

This file was deleted.

Binary file not shown.
27 changes: 0 additions & 27 deletions rocket_twin/systems/control/controller_cosapp.py

This file was deleted.

22 changes: 2 additions & 20 deletions rocket_twin/systems/control/rocket_controller.mo
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
model rocket_controller
input Real ti;
input Boolean flying;
input Real weight_1;
input Real weight_2;
input Real weight_3;
parameter Real tl;
parameter Real weight_max_1;
parameter Real weight_max_2;
parameter Real weight_max_3;
output Real is_on_1;
output Real is_on_2;
output Real is_on_3;
output Real fueling;
output Real flying;
equation
if (flying < 0.5 and weight_3 < weight_max_3) then
fueling = 1.;
else
fueling = 0.;
end if;

if (ti < tl) then
flying = 0.;
else
flying = 1.;
end if;

if (flying < 0.5) then
if (flying == false) then
is_on_1 = 0.;
is_on_2 = 0.;
is_on_3 = 0.;
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions rocket_twin/systems/control/rocket_controller_cosapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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")
self.add_inward("flying", False, desc="Whether the rocket is flying or not")

for i in range(1, n_stages + 1):
self.add_inward(f"weight_prop_{i}", 0.0, desc=f"Stage {i} propellant weight", unit="kg")
Expand Down
30 changes: 6 additions & 24 deletions rocket_twin/systems/control/rocket_controller_fmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,29 @@ class RocketControllerFMU(System):
the path to the .mo file, if any
model_name: string,
the .fmu file name
flying: boolean,
whether the rocket is mid-flight or not
n_stages: int,
rocket's number of stages
weight_prop_i: float,
i-th stage fuel weight
weight_max_i: float,
i-th stage maximum fuel weight
Outputs
------
is_on_i: float,
is_on_i: boolean,
whether the i-th stage controller is active or not
fueling: float,
whether the rocket is in the fueling phase or not
flying: float,
whether the rocket is mid-flight or not
"""

def setup(self, n_stages, model_path, model_name):

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

self.add_inward("time_var", 0.0, desc="System time", unit="")
self.add_inward("time_int", 0.0, desc="Interval between fueling end and launch", unit="")
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"}

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")
pulling[f"weight_{i}"] = f"weight_prop_{i}"
pulling[f"weight_max_{i}"] = f"weight_max_{i}"
pulling[f"is_on_{i}"] = f"is_on_{i}"

fmu_path = self.create_fmu(model_path, model_name)
Expand All @@ -57,23 +47,15 @@ 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("drop", trigger="weight_prop_1 < 0.1")

def compute(self):

self.time_var = self.time
for i in range(1, self.n_stages):
self[f"is_on_{i}"] = bool(self[f"is_on_{i}"])

def transition(self):

if self.full.present:
if self.stage < self.n_stages:
self.stage += 1
self.full.trigger = f"weight_prop_{self.stage} > 0.9999 * weight_max_{self.stage}"
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
10 changes: 10 additions & 0 deletions rocket_twin/systems/control/stage_controller.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
model stage_controller
input Boolean is_on;
output Real w;
equation
if (is_on == true) then
w = 1.;
else
w = 0.;
end if;
end stage_controller;
77 changes: 77 additions & 0 deletions rocket_twin/systems/control/stage_controller_fmu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os

from cosapp.base import System
from cosapp_fmu.FMUsystem import FMUSystem
from OMPython import ModelicaSystem

import rocket_twin.systems.control


class StageControllerFMU(System):
"""Controller of the command variables.
Inputs
------
model_path: string,
the path to the .mo file, if any
model_name: string,
the .fmu file name
is_on: boolean,
whether the system is in fueling phase or not
weight_prop: float,
stage fuel weight
weight_max: float,
stage maximum fuel weight
Outputs
------
w: float,
command flux
"""

def setup(self, model_path, model_name):

self.add_inward("weight_prop", 0.0, desc="Stage propellant weight", unit="kg")
self.add_inward("weight_max", 1.0, desc="Stage maximum propellant weight", unit="kg")
self.add_inward("is_on", False, desc="Whether the stage is on or not")

fmu_path = self.create_fmu(model_path, model_name)
self.add_child(
FMUSystem("fmu_controller", fmu_path=fmu_path),
pulling=['is_on', 'w'],
)

self.add_event("full", trigger="weight_prop == weight_max")

def create_fmu(self, model_path, model_name):
"""Create an fmu file in the control folder from an mo file.
Inputs
------
model_path: string
the path of the .mo file
model_name: string
the name of the .fmu file to be created
Outputs
------
fmu: string
the path to the .fmu file
"""

fmu_path = os.path.join(rocket_twin.systems.control.__path__[0], model_name)
model_path = os.path.join(rocket_twin.__path__[0], model_path)
model_path = model_path.replace("\\", "/")
try:
os.mkdir(fmu_path)
except OSError:
pass
os.chdir(fmu_path)
mod = ModelicaSystem(model_path, model_name)
fmu = mod.convertMo2Fmu()
for filename in os.listdir(fmu_path):
if filename != (model_name + ".fmu"):
os.remove(filename)

return fmu
10 changes: 10 additions & 0 deletions rocket_twin/systems/control/station_controller.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
model station_controller
input Boolean fueling;
output Real w;
equation
if (fueling == true) then
w = 1.;
else
w = 0.;
end if;
end station_controller;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import rocket_twin.systems.control


class ControllerFMU(System):
class StationControllerFMU(System):
"""Controller of the command variables.
Inputs
Expand All @@ -16,21 +16,21 @@ class ControllerFMU(System):
the path to the .mo file, if any
model_name: string,
the .fmu file name
is_on: float,
whether the system is active or not
fueling: boolean,
whether the system is in fueling phase or not
Outputs
------
'w': float,
command flow
w: float,
command flux
"""

def setup(self, model_path, model_name):

fmu_path = self.create_fmu(model_path, model_name)
self.add_child(
FMUSystem("fmu_controller", fmu_path=fmu_path),
pulling=["w", "is_on"],
pulling=['fueling', 'w'],
)

def create_fmu(self, model_path, model_name):
Expand Down Expand Up @@ -64,4 +64,4 @@ def create_fmu(self, model_path, model_name):
if filename != (model_name + ".fmu"):
os.remove(filename)

return fmu
return fmu
6 changes: 5 additions & 1 deletion rocket_twin/systems/rocket/occ_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ class OCCGeometry(System):
Inputs
------
shapes: TopoDS_Solid, TopoDS_Compound
shapes: TopoDS_Solid, TopoDS_Compound,
pyoccad models of each component of the system
props: GProp_GProps,
properties of each model
Outputs
------
shapes: TopoDS_Solid, TopoDS_Compound,
fusion of all input models
props: GProp_GProps,
properties of the global model
cg [m]: float,
center of gravity
weight [kg]: float,
Expand Down
6 changes: 3 additions & 3 deletions rocket_twin/systems/rocket/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def setup(self, n_stages=1):
shapes, properties, forces = ([None] * n_stages for i in range(3))

self.add_inward("n_stages", n_stages, desc="Number of stages")
self.add_outward_modevar("stage", 1, desc="Current stage")
self.add_outward("stage", 1, desc="Current stage")
self.add_inward("flying", False, desc="Whether the rocket is flying or not")

for i in range(1, n_stages + 1):
nose = False
Expand All @@ -59,6 +60,7 @@ def setup(self, n_stages=1):
self.add_child(
RocketControllerCoSApp("controller", n_stages=n_stages),
execution_index=0,
pulling=["flying"]
)
self.add_child(OCCGeometry("geom", shapes=shapes, properties=properties))
self.add_child(Dynamics("dyn", forces=forces, weights=["weight_rocket"]), pulling=["a"])
Expand All @@ -77,8 +79,6 @@ def setup(self, n_stages=1):

self.connect(self.geom.outwards, self.dyn.inwards, {"weight": "weight_rocket"})

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

def compute(self):
self.a *= self.flying

Expand Down
Loading

0 comments on commit f65ff89

Please sign in to comment.