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

Multistages #22

Merged
merged 8 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
Binary file removed 1
Binary file not shown.
1 change: 1 addition & 0 deletions requirements-ci.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cosapp==0.15.0
fmpy==0.3.15
ipython
matplotlib==3.6.3
numpy
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ flake8
isort
black
pre-commit
fmpy==0.3.15
cosapp_fmu @ git+ssh://[email protected]/twiinIT/cosapp-fmu.git@master
6 changes: 3 additions & 3 deletions rocket_twin/drivers/mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def __init__(
init_flight = {
"rocket.flying": True,
"controller.w_temp": 0.0,
"rocket.controller.w_temp": 1.0,
"rocket.tank.fuel.w_out_max": 3.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,
}

stop_fuel = "rocket.tank.weight_prop >= rocket.tank.weight_max"
stop_fuel = "rocket.stage_1.tank.weight_prop >= rocket.stage_1.tank.weight_max"
stop_flight = stop

# Fueling
Expand Down
3 changes: 2 additions & 1 deletion rocket_twin/systems/rocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rocket_twin.systems.rocket.occ_geometry import OCCGeometry
from rocket_twin.systems.rocket.rocket import Rocket
from rocket_twin.systems.rocket.stage import Stage

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

__all__ = ["Stage", "Rocket", "OCCGeometry"]
33 changes: 28 additions & 5 deletions rocket_twin/systems/rocket/occ_geometry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy as np
from cosapp.base import System
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse
from OCC.Core.GProp import GProp_GProps
from OCC.Core.TopoDS import TopoDS_Compound, TopoDS_Solid
from pyoccad.create import CreateSphere

# from OCC.Display.SimpleGui import init_display

Expand Down Expand Up @@ -46,23 +48,44 @@ def setup(self, shapes=None, properties=None):
for props in properties:
self.add_inward(props, GProp_GProps(), desc=f"Properties of the {props}")

self.add_outward(
"shape",
CreateSphere.from_radius_and_center(1.0),
dtype=(TopoDS_Solid, TopoDS_Compound),
desc="global shape",
)
self.add_outward("props", GProp_GProps(), desc="global properties")
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("I", np.empty((3, 3)), desc="Inertia matrix", unit="kg*m**2")

def compute(self):

vprop = GProp_GProps()
self.props = GProp_GProps()
for props in self.properties:
vprop.Add(self[props])
self.props.Add(self[props])

self.weight = vprop.Mass()
self.cg = vprop.CentreOfMass().Z()
try:
self.shape = self.fusion(self.shapes)
except TypeError:
pass

inertia = vprop.MatrixOfInertia()
self.weight = self.props.Mass()
self.cg = self.props.CentreOfMass().Z()

inertia = self.props.MatrixOfInertia()
for i, j in zip(range(3), range(3)):
self.I[i, j] = inertia.Value(i + 1, j + 1)

def fusion(self, shapes):

shape_list = shapes.copy()
fusion = self[shape_list.pop(0)]
for shape in shape_list:
fusion = BRepAlgoAPI_Fuse(fusion, self[shape]).Shape()

return fusion

# def view(self):

# display, start_display, add_menu, add_function_to_menu = init_display()
Expand Down
80 changes: 30 additions & 50 deletions rocket_twin/systems/rocket/rocket.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
from cosapp.base import System

from rocket_twin.systems import (
ControllerCoSApp,
Dynamics,
Engine,
NoseGeom,
Tank,
TubeGeom,
WingsGeom,
)
from rocket_twin.systems.rocket import OCCGeometry
from rocket_twin.systems import Dynamics
from rocket_twin.systems.rocket import OCCGeometry, Stage


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

def setup(self):
self.add_child(ControllerCoSApp("controller"))
self.add_child(Tank("tank"), pulling=["w_in", "weight_max", "weight_prop"])
self.add_child(Engine("engine"))
self.add_child(NoseGeom("nose"))
self.add_child(TubeGeom("tube"))
self.add_child(WingsGeom("wings"))
self.add_child(
OCCGeometry(
"geom",
shapes=["tank_s", "engine_s", "nose_s", "tube_s", "wings_s"],
properties=["tank", "engine", "nose", "tube", "wings"],
def setup(self, n_stages=1):

shapes, properties, forces = ([None] * n_stages for i in range(3))

for i in range(1, n_stages + 1):
nose = False
wings = False
if i == 1:
wings = True
if i == n_stages:
nose = True

self.add_child(
Stage(f"stage_{i}", nose=nose, wings=wings),
pulling={
"w_in": f"w_in_{i}",
"weight_max": f"weight_max_{i}",
"weight_prop": f"weight_prop_{i}",
},
)
)
self.add_child(
Dynamics(
"dyn",
forces=["thrust"],
weights=["weight_rocket"],
),
pulling=["a"],
)
shapes[i - 1] = f"stage_{i}_s"
properties[i - 1] = f"stage_{i}"
forces[i - 1] = f"thrust_{i}"
luca7084 marked this conversation as resolved.
Show resolved Hide resolved

self.connect(self.controller.outwards, self.tank.inwards, {"w": "w_command"})
self.connect(self.tank.outwards, self.engine.inwards, {"w_out": "w_out"})
self.add_child(OCCGeometry("geom", shapes=shapes, properties=properties))
self.add_child(Dynamics("dyn", forces=forces, weights=["weight_rocket"]), pulling=["a"])

self.connect(self.tank.outwards, self.geom.inwards, {"shape": "tank_s", "props": "tank"})
self.connect(
self.engine.outwards, self.geom.inwards, {"shape": "engine_s", "props": "engine"}
)
self.connect(self.nose.outwards, self.geom.inwards, {"shape": "nose_s", "props": "nose"})
self.connect(self.tube.outwards, self.geom.inwards, {"shape": "tube_s", "props": "tube"})
self.connect(self.wings.outwards, self.geom.inwards, {"shape": "wings_s", "props": "wings"})

self.connect(
self.engine.outwards,
self.dyn.inwards,
{"force": "thrust"},
)
for i in range(1, n_stages + 1):
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}"})

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

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

self.add_event("Takeoff", trigger="engine.force > 0")
# self.add_event("view", trigger="t == 0.2")
self.add_event("Takeoff", trigger="stage_1.engine.force > 0")

def compute(self):
self.a *= self.flying
Expand All @@ -80,5 +62,3 @@ def transition(self):

if self.Takeoff.present:
self.flying = True
# if self.view.present:
# self.geom.view()
40 changes: 27 additions & 13 deletions rocket_twin/systems/rocket/stage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from cosapp.base import System

from rocket_twin.systems import ControllerCoSApp, Engine, Tank
from rocket_twin.systems import ControllerCoSApp, Engine, NoseGeom, Tank, TubeGeom, WingsGeom
from rocket_twin.systems.rocket import OCCGeometry


class Stage(System):
Expand All @@ -19,21 +20,34 @@ class Stage(System):
center of gravity
"""

def setup(self):
def setup(self, nose=False, wings=False):

shapes = ["tank_s", "engine_s", "tube_s"]
properties = ["tank", "engine", "tube"]

self.add_child(ControllerCoSApp("controller"))
self.add_child(Tank("tank"), pulling=["w_in", "weight_max", "weight_p"])
self.add_child(Engine("engine"))
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"))

if nose:
self.add_child(NoseGeom("nose"))
shapes.append("nose_s")
properties.append("nose")

if wings:
self.add_child(WingsGeom("wings"))
shapes.append("wings_s")
properties.append("wings")

self.add_child(
OCCGeometry("geom", shapes=shapes, properties=properties), pulling=["shape", "props"]
)

self.connect(self.controller.outwards, self.tank.inwards, {"w": "w_command"})
self.connect(self.tank.outwards, self.engine.inwards, {"w_out": "w_out"})

self.add_outward("weight", 1.0, desc="Weight", unit="kg")
self.add_outward("cg", 1.0, desc="Center of gravity", unit="m")

def compute(self):

self.weight = self.tank.weight + self.engine.weight
self.cg = (self.tank.cg * self.tank.weight + self.engine.cg * self.engine.weight) / (
self.weight
)
for prop in properties:
self.connect(
self[prop].outwards, self.geom.inwards, {"shape": prop + "_s", "props": prop}
)
52 changes: 49 additions & 3 deletions rocket_twin/systems/station/station.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cosapp.base import System
from OCC.Core.GProp import GProp_GProps

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

Expand All @@ -13,15 +14,60 @@ class Station(System):
------
"""

def setup(self):
def setup(self, n_stages=1):

self.add_inward("n_stages", n_stages, desc="Number of stages")
self.add_outward_modevar("stage", 1, desc="Current stage")

self.add_child(ControllerCoSApp("controller"))
self.add_child(Tank("g_tank"))
self.add_child(Pipe("pipe"))
self.add_child(Rocket("rocket"))
self.add_child(Rocket("rocket", n_stages=n_stages))

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"})
self.connect(self.pipe.outwards, self.rocket.inwards, {"w_out": "w_in_1"})

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

self.g_tank.geom.height = 2.0

self.add_event("stage_full", trigger="rocket.weight_prop_1 == rocket.weight_max_1")
self.add_event("stage_empty", trigger="rocket.weight_prop_1 == 0.")

def transition(self):

if self.stage_full.present:
if self.stage < self.n_stages:
self.stage += 1

self.pop_child("pipe")
self.add_child(Pipe("pipe"), execution_index=2)

self.connect(self.g_tank.outwards, self.pipe.inwards, {"w_out": "w_in"})
self.connect(
self.pipe.outwards, self.rocket.inwards, {"w_out": f"w_in_{self.stage}"}
)

self.rocket[f"w_in_{self.stage - 1}"] = 0.0
self.stage_full.trigger = (
f"rocket.weight_prop_{self.stage} == rocket.weight_max_{self.stage}"
)
else:
self.controller.w_temp = 0.0
self.rocket[f"w_in_{self.stage}"] = 0.0
self.stage = 1

if self.stage_empty.present:
if self.stage < self.n_stages:
stage = self.rocket.pop_child(f"stage_{self.stage}")
self.rocket.add_child(stage, execution_index=self.stage - 1)
self.rocket[f"stage_{self.stage}"].controller.w_temp = 0.0
self.rocket.geom[f"stage_{self.stage}"] = GProp_GProps()
self.rocket.dyn[f"thrust_{self.stage}"] = 0.0

self.stage += 1
self.stage_empty.trigger = f"rocket.weight_prop_{self.stage} == 0."
self.rocket.Takeoff.trigger = "dyn.a == -10000000"
self.rocket[f"stage_{self.stage}"].controller.w_temp = 1.0
else:
self.rocket[f"stage_{self.stage}"].controller.w_temp = 0.0
53 changes: 0 additions & 53 deletions rocket_twin/tests/test_controller_fmu.py

This file was deleted.

Loading