Skip to content
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
feat(plots): add visual rendering and layout support for ClusterMotor
  • Loading branch information
ayoubdsp committed Mar 21, 2026
commit eb34298c9cd477ebd9a0c513e11839a7c154d171
91 changes: 59 additions & 32 deletions rocketpy/plots/rocket_plots.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import matplotlib.pyplot as plt
import numpy as np

from rocketpy.motors import EmptyMotor, HybridMotor, LiquidMotor, SolidMotor
from rocketpy.motors import HybridMotor, LiquidMotor, SolidMotor
from rocketpy.rocket.aero_surface import Fins, NoseCone, Tail
from rocketpy.rocket.aero_surface.generic_surface import GenericSurface

Expand Down Expand Up @@ -414,57 +414,84 @@ def _draw_tubes(self, ax, drawn_surfaces, vis_args):
def _draw_motor(self, last_radius, last_x, ax, vis_args):
"""Draws the motor from motor patches"""
total_csys = self.rocket._csys * self.rocket.motor._csys
is_cluster = hasattr(self.rocket.motor, "number")
base_motor = self.rocket.motor.motor if is_cluster else self.rocket.motor

if is_cluster:
angles = np.linspace(0, 2 * np.pi, self.rocket.motor.number, endpoint=False)
y_offsets = self.rocket.motor.radius * np.cos(angles)
else:
y_offsets = [0]
nozzle_position = (
self.rocket.motor_position + self.rocket.motor.nozzle_position * total_csys
self.rocket.motor_position + base_motor.nozzle_position * total_csys
)

# Get motor patches translated to the correct position
motor_patches = self._generate_motor_patches(total_csys, ax)

# Draw patches
if not isinstance(self.rocket.motor, EmptyMotor):
# Add nozzle last so it is in front of the other patches
nozzle = self.rocket.motor.plots._generate_nozzle(
translate=(nozzle_position, 0), csys=self.rocket._csys
)
motor_patches += [nozzle]
if type(self.rocket.motor).__name__ != "EmptyMotor":
for y_off in y_offsets:
nozzle = base_motor.plots._generate_nozzle(
translate=(nozzle_position, y_off), csys=self.rocket._csys
)
if y_off != y_offsets[0]:
nozzle.set_label("_nolegend_")
motor_patches.append(nozzle)

outline = self.rocket.motor.plots._generate_motor_region(
outline = base_motor.plots._generate_motor_region(
list_of_patches=motor_patches
)
# add outline first so it is behind the other patches
ax.add_patch(outline)
if not is_cluster:
ax.add_patch(outline)

for patch in motor_patches:
if is_cluster:
patch.set_alpha(0.6)
ax.add_patch(patch)

self._draw_nozzle_tube(last_radius, last_x, nozzle_position, ax, vis_args)

def _generate_motor_patches(self, total_csys, ax): # pylint: disable=unused-argument
def _generate_motor_patches(
self, total_csys, ax
): # pylint: disable=unused-argument
"""Generates motor patches for drawing"""
motor_patches = []

if isinstance(self.rocket.motor, SolidMotor):
is_cluster = hasattr(self.rocket.motor, "number")
base_motor = self.rocket.motor.motor if is_cluster else self.rocket.motor

if isinstance(base_motor, SolidMotor):
y_offsets = (
self.rocket.motor.radius
* np.cos(
np.linspace(0, 2 * np.pi, self.rocket.motor.number, endpoint=False)
)
if is_cluster
else [0]
)
grains_cm_position = (
self.rocket.motor_position
+ self.rocket.motor.grains_center_of_mass_position * total_csys
)
ax.scatter(
grains_cm_position,
0,
color="brown",
label="Grains Center of Mass",
s=8,
zorder=10,
+ base_motor.grains_center_of_mass_position * total_csys
)
for y_off in y_offsets:
ax.scatter(
grains_cm_position,
y_off,
color="brown",
label="Grains Center of Mass" if y_off == y_offsets[0] else "",
s=8,
zorder=10,
)

chamber = self.rocket.motor.plots._generate_combustion_chamber(
translate=(grains_cm_position, 0), label=None
)
grains = self.rocket.motor.plots._generate_grains(
translate=(grains_cm_position, 0)
)
chamber = base_motor.plots._generate_combustion_chamber(
translate=(grains_cm_position, y_off), label=None
)
grains = base_motor.plots._generate_grains(
translate=(grains_cm_position, y_off)
)
if y_off != y_offsets[0]:
for grain in grains:
grain.set_label("_nolegend_")

motor_patches += [chamber, *grains]
motor_patches += [chamber, *grains]

elif isinstance(self.rocket.motor, HybridMotor):
grains_cm_position = (
Expand Down