Skip to content

Commit

Permalink
Changes proposed in the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
luca7084 committed Jul 3, 2023
1 parent 02a962f commit c6e84dc
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 219 deletions.
37 changes: 12 additions & 25 deletions rocket_twin/drivers/fueling_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,29 @@ class FuelingRocket(Driver):
------
"""

def __init__(self, name: str, w_out, dt, owner: Optional["System"] = None, **kwargs):
def __init__(
self,
name: str,
owner: Optional["System"] = None,
init: Optional[dict] = None,
stop: Optional[str] = None,
dt: Optional[float] = 0.1,
includes: Optional[list[str]] = None,
**kwargs
):
super().__init__(name, owner, **kwargs)

# Fueling:
self.rk = self.add_driver(RungeKutta("rk", owner=owner, order=4, dt=dt))
self.rk.time_interval = (0.0, 1.0)
self.rk.time_interval = (self.owner.time, 1000000.0)
self.solver = self.rk.add_child(NonLinearSolver("solver"))
self.time_end = 0.0

init = {
"rocket.dyn.switch": False,
"rocket.tank.w_out_temp": 0.0,
"pipe.is_open": True,
"g_tank.w_in": 0.0,
"g_tank.w_out_temp": w_out,
}

stop = "rocket.tank.weight_p >= rocket.tank.weight_max"

self.rk.set_scenario(init=init, stop=stop)
self.rk.add_recorder(
DataFrameRecorder(
includes=["rocket.dyn.a", "g_tank.weight", "rocket.tank.weight_p"], hold=True
),
DataFrameRecorder(includes=includes, hold=True),
period=dt,
)
self.data = None

def compute(self):
pass

def _precompute(self):
self.rk.time_interval = (self.owner.time, 1000000.0)
super()._precompute()

def _postcompute(self) -> None:
self.time_end = self.rk.time
self.data = self.rk.recorder.export_data()
return super()._postcompute()
42 changes: 35 additions & 7 deletions rocket_twin/drivers/mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,50 @@ class Mission(Driver):
------
"""

def __init__(self, name: str, w_in, w_out, dt, owner: Optional["System"] = None, **kwargs):
def __init__(
self,
name: str,
owner: Optional["System"] = None,
init: Optional[dict] = None,
stop: Optional[str] = None,
dt: Optional[float] = 0.1,
includes: Optional[list[str]] = None,
**kwargs
):
super().__init__(name, owner, **kwargs)

# Fuelling
self.add_child(FuelingRocket("fuelling", w_out=w_in, dt=dt, owner=owner))
# Init and stop conditions
init_fuel = init
init_flight = {
"rocket.engine.switch": True,
"rocket.tank.w_out_temp": 3.0,
"g_tank.w_in": 0.0,
"g_tank.is_open": False,
}

stop_fuel = "rocket.tank.weight_p >= rocket.tank.weight_max"
stop_flight = stop

# Fueling
self.add_child(
FuelingRocket(
"fr", owner=owner, init=init_fuel, stop=stop_fuel, includes=includes, dt=dt
)
)

# Flying
self.add_child(VerticalFlyingRocket("flying", w_out=w_out, dt=dt, owner=owner))
self.add_child(
VerticalFlyingRocket(
"vfr", owner=owner, init=init_flight, stop=stop_flight, includes=includes, dt=dt
)
)

# Recorder
self.data = None

def compute(self):
super().compute
for child_name in ["fuelling", "flying"]:
for child in self.children.values():
self.data = pd.concat(
[self.data, self.children[child_name].rk.recorder.export_data()],
[self.data, child.rk.recorder.export_data()],
ignore_index=True,
)
36 changes: 12 additions & 24 deletions rocket_twin/drivers/vertical_flying_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,29 @@ class VerticalFlyingRocket(Driver):
------
"""

def __init__(self, name: str, w_out, dt, owner: Optional["System"] = None, **kwargs):
def __init__(
self,
name: str,
owner: Optional["System"] = None,
init: Optional[dict] = None,
stop: Optional[str] = None,
dt: Optional[float] = 0.1,
includes: Optional[list[str]] = None,
**kwargs
):
super().__init__(name, owner, **kwargs)

# Fueling:
self.rk = self.add_driver(RungeKutta("rk", owner=owner, order=4, dt=dt))
self.rk.time_interval = (0.0, 1.0)
self.rk.time_interval = (self.owner.time, 1000000.0)
self.solver = self.rk.add_child(NonLinearSolver("solver"))
self.time_end = 0.0

init = {
"rocket.dyn.switch": True,
"g_tank.w_in": 0.0,
"pipe.is_open": False,
"rocket.tank.w_out_temp": w_out,
}

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

self.rk.set_scenario(init=init, stop=stop)
self.rk.add_recorder(
DataFrameRecorder(
includes=["rocket.dyn.a", "g_tank.weight", "rocket.tank.weight_p"], hold=True
),
DataFrameRecorder(includes=includes, hold=True),
period=dt,
)
self.data = None

def compute(self):
pass

def _precompute(self):
self.rk.time_interval = (self.owner.time, 1000000.0)
super()._precompute()

def _postcompute(self) -> None:
self.time_end = self.rk.time
self.data = self.rk.recorder.export_data()
return super()._postcompute()
15 changes: 9 additions & 6 deletions rocket_twin/notebooks/test_sequences.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"source": [
"#Run initial sequence (loads initial values for ground tank)\n",
"\n",
"s1 = [{'name' : 'start', 'design_method' : ['start'], 'type' : 'static'}]\n",
"s1 = [{'name' : 'start', 'init' : {'g_tank.weight_p' : sys.g_tank.weight_max}, 'type' : 'static'}]\n",
"run_sequences(sys, s1)"
]
},
Expand Down Expand Up @@ -93,7 +93,7 @@
"source": [
"#Run fuelling sequence (transfers fuel from ground tank to rocket tank until it is full)\n",
"\n",
"s2 = [{'name' : 'fuel', 'type' : 'transient', 'init' : {'g_tank.w_out_temp' : 1., 'pipe.is_open' : True},\n",
"s2 = [{'name' : 'fuel', 'type' : 'transient', 'init' : {'g_tank.w_out_temp' : 1., 'g_tank.is_open' : True},\n",
" 'dt' : 0.1, 'Tf' : 5., 'stop' : 'rocket.tank.weight_p == rocket.tank.weight_max'}]\n",
"run_sequences(sys, s2)"
]
Expand Down Expand Up @@ -143,7 +143,8 @@
"source": [
"#Run flight sequence (turns rocket on and discards rocket tank fuel until it is empty)\n",
"\n",
"s3 = [{'name' : 'flight', 'type' : 'transient', 'init' : {'rocket.tank.w_out_temp' : 0.5, 'pipe.is_open' : False, 'rocket.dyn.switch' : True},\n",
"s3 = [{'name' : 'flight', 'type' : 'transient', 'init' : {'rocket.tank.w_out_temp' : 0.5,\n",
" 'g_tank.is_open' : False, 'rocket.engine.switch' : True, 'rocket.tank.is_open' : True},\n",
" 'dt' : 0.1, 'Tf' : 10., 'stop' : 'rocket.tank.weight_p == 0.'}]\n",
"run_sequences(sys, s3)"
]
Expand Down Expand Up @@ -204,9 +205,11 @@
"#Run all sequences\n",
"\n",
"sequences = [\n",
" {'name' : 'start', 'design_method' : ['start'], 'type' : 'static'},\n",
" {'name' : 'fuel', 'type' : 'transient', 'init' : {'g_tank.w_out_temp' : 1.}, 'dt' : 0.1, 'Tf' : 5., 'stop' : 'rocket.tank.weight_p == rocket.tank.weight_max'},\n",
" {'name' : 'flight', 'type' : 'transient', 'init' : {'rocket.tank.w_out_temp' : 0.5, 'g_tank.w_out_temp' : 0., 'rocket.dyn.switch' : True},\n",
" {'name' : 'start', 'init' : {'g_tank.weight_p' : sys2.g_tank.weight_max}, 'type' : 'static'},\n",
" {'name' : 'fuel', 'type' : 'transient', 'init' : {'g_tank.w_out_temp' : 1., 'g_tank.is_open' : True},\n",
" 'dt' : 0.1, 'Tf' : 5., 'stop' : 'rocket.tank.weight_p == rocket.tank.weight_max'},\n",
" {'name' : 'flight', 'type' : 'transient', 'init' : {'rocket.tank.w_out_temp' : 0.5,\n",
" 'g_tank.is_open' : False, 'rocket.engine.switch' : True, 'rocket.tank.is_open' : True},\n",
" 'dt' : 0.1, 'Tf' : 10., 'stop' : 'rocket.tank.weight_p == 0.'}\n",
"]\n",
"run_sequences(sys2, sequences)"
Expand Down
6 changes: 3 additions & 3 deletions rocket_twin/systems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from rocket_twin.systems.engine import Engine
from rocket_twin.systems.physics import Dynamics
from rocket_twin.systems.tank import Pipe, Tank

from rocket_twin.systems.tank import Pipe, Tank # isort: skip
from rocket_twin.systems.rocket import Rocket, RocketGeom # isort: skip
from rocket_twin.systems.rocket import Rocket # isort: skip
from rocket_twin.systems.station import Station # isort: skip
from rocket_twin.systems.ground import Ground # isort: skip

__all__ = ["Engine", "Tank", "RocketGeom", "Rocket", "Pipe", "Dynamics", "Station", "Ground"]
__all__ = ["Engine", "Tank", "Rocket", "Pipe", "Dynamics", "Station", "Ground"]
16 changes: 14 additions & 2 deletions rocket_twin/systems/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Engine(System):
Inputs
------
force_command: float,
External control, which inputs the % of the maximum force the engine outputs
Outputs
------
Expand All @@ -18,10 +20,20 @@ class Engine(System):
"""

def setup(self):

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("switch", False, desc="Whether the engine is on or off", unit="")

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 = 100.0
self.cg = 1.0

if self.switch:
self.force = self.force_max * self.force_command
else:
self.force = 0.0
2 changes: 0 additions & 2 deletions rocket_twin/systems/ground.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def setup(self, stations=None):
if stations is None:
stations = []

self.add_property("stations", stations)

for station in stations:

self.add_child(station)
35 changes: 25 additions & 10 deletions rocket_twin/systems/physics/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Dynamics(System):
total force in each component of the system
weights [kg]: float,
total weight of each component of the system
centers[m]: float,
center of gravity of each component of the system
Outputs
------
Expand All @@ -21,16 +23,23 @@ class Dynamics(System):
acceleration
"""

def setup(self, forces=None, weights=None):
def setup(self, forces=None, weights=None, centers=None):
if forces is None:
forces = []
if weights is None:
weights = []
if centers is None:
centers = []

self.add_property("forces", forces)
self.add_property("weights", weights)
self.add_property("centers", centers)

# Checks if rocket is mid-flight or still in station
self.add_inward_modevar(
"flight", False, desc="Whether the rocket is flying or not", unit=""
)

self.add_inward("switch", False, desc="Whether rocket is on or off")
self.add_inward("g", -10.0, desc="Gravity", unit="m/s**2")

self.add_outward("a", 1.0, desc="Acceleration", unit="m/s**2")
Expand All @@ -39,21 +48,27 @@ def setup(self, forces=None, weights=None):
self.add_inward(weight, 0.0, desc=f"Weight called {weight}", unit="kg")
for force in self.forces:
self.add_inward(force, 0.0, desc=f"Force called {force}", unit="N")
for center in self.centers:
self.add_inward(center, 0.0, desc=f"Center of gravity of the {center}", unit="m")

self.add_outward("force", 1.0, desc="Force", unit="N")
self.add_outward("weight", 1.0, desc="Weight", unit="kg")
self.add_outward("center", 1.0, desc="Center of gravity", unit="m")

def compute(self):
self.weight = 0
for weight in self.weights:
self.weight += self[weight]
self.center = 0
for i in range(len(self.centers)):
self.center += self[self.centers[i]] * self[self.weights[i]]
self.weight += self[self.weights[i]]

if self.switch:
self.force = self.weight * self.g
for force in self.forces:
self.force += self[force]
self.center /= self.weight

self.force = self.weight * self.g
for force in self.forces:
self.force += self[force]

else:
self.force = 0.0
if self.force < 0 and self.flight is False:
self.force = 0

self.a = self.force / self.weight
3 changes: 1 addition & 2 deletions rocket_twin/systems/rocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rocket_twin.systems.rocket.rocket_geom import RocketGeom # isort: skip
from rocket_twin.systems.rocket.rocket import Rocket

__all__ = ["RocketGeom", "Rocket"]
__all__ = ["Rocket"]
27 changes: 17 additions & 10 deletions rocket_twin/systems/rocket/rocket.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from cosapp.base import System

from rocket_twin.systems import Dynamics, Engine, Tank
from rocket_twin.systems.rocket import RocketGeom


class Rocket(System):
Expand All @@ -18,17 +17,25 @@ def setup(self):
self.add_child(Engine("engine"))
self.add_child(Tank("tank"), pulling=["w_in"])
self.add_child(
RocketGeom("geom", centers=["engine", "tank"], weights=["weight_eng", "weight_tank"])
Dynamics(
"dyn",
forces=["thrust"],
weights=["weight_eng", "weight_tank"],
centers=["engine", "tank"],
),
pulling=["a"],
)
self.add_child(Dynamics("dyn", forces=["thrust"], weights=["weight_eng", "weight_tank"]))

self.connect(
self.engine.outwards, self.geom.inwards, {"cg": "engine", "weight": "weight_eng"}
self.engine.outwards,
self.dyn.inwards,
{"force": "thrust", "weight": "weight_eng", "cg": "engine"},
)
self.connect(self.tank.outwards, self.geom.inwards, {"cg": "tank", "weight": "weight_tank"})
self.connect(
self.engine.outwards, self.dyn.inwards, {"force": "thrust", "weight": "weight_eng"}
)
self.connect(self.tank.outwards, self.dyn.inwards, {"weight": "weight_tank"})
self.connect(self.tank.outwards, self.dyn.inwards, {"weight": "weight_tank", "cg": "tank"})

# Event
self.add_event("is_flying", trigger="a > 0")

self.exec_order = ["engine", "tank", "geom", "dyn"]
def transition(self):
if self.is_flying.present:
self.dyn.flight = True
Loading

0 comments on commit c6e84dc

Please sign in to comment.