Skip to content

Commit 9aedf27

Browse files
adapts motor model
1 parent 81ae563 commit 9aedf27

File tree

9 files changed

+285
-505
lines changed

9 files changed

+285
-505
lines changed

src/models/motor.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from enum import Enum
22
from typing import Optional, Tuple, List, Union, Self, ClassVar, Literal
3-
from pydantic import PrivateAttr, model_validator, computed_field
3+
from pydantic import model_validator
44

55
from src.models.interface import ApiBaseModel
66
from src.models.sub.tanks import MotorTank
@@ -24,6 +24,7 @@ class MotorModel(ApiBaseModel):
2424
dry_mass: float
2525
dry_inertia: Tuple[float, float, float] = (0, 0, 0)
2626
center_of_dry_mass_position: float
27+
motor_kind: MotorKinds
2728

2829
# Generic motor parameters
2930
chamber_radius: Optional[float] = None
@@ -56,30 +57,18 @@ class MotorModel(ApiBaseModel):
5657
] = 'nozzle_to_combustion_chamber'
5758
reshape_thrust_curve: Union[bool, tuple] = False
5859

59-
# Computed parameters
60-
_motor_kind: MotorKinds = PrivateAttr(default=MotorKinds.SOLID)
61-
6260
@model_validator(mode='after')
6361
# TODO: extend guard to check motor kinds and tank kinds specifics
6462
def validate_motor_kind(self):
6563
if (
66-
self._motor_kind not in (MotorKinds.SOLID, MotorKinds.GENERIC)
64+
self.motor_kind not in (MotorKinds.SOLID, MotorKinds.GENERIC)
6765
and self.tanks is None
6866
):
6967
raise ValueError(
7068
"Tanks must be provided for liquid and hybrid motors."
7169
)
7270
return self
7371

74-
@computed_field
75-
@property
76-
def selected_motor_kind(self) -> str:
77-
return self._motor_kind.value
78-
79-
def set_motor_kind(self, motor_kind: MotorKinds):
80-
self._motor_kind = motor_kind
81-
return self
82-
8372
@staticmethod
8473
def UPDATED():
8574
return

src/routes/flight.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from src.models.environment import EnvironmentModel
1414
from src.models.flight import FlightModel
1515
from src.models.rocket import RocketModel
16-
from src.models.motor import MotorKinds
1716
from src.controllers.flight import FlightController
1817

1918
router = APIRouter(
@@ -30,9 +29,7 @@
3029

3130

3231
@router.post("/", status_code=201)
33-
async def create_flight(
34-
flight: FlightModel, motor_kind: MotorKinds
35-
) -> FlightCreated:
32+
async def create_flight(flight: FlightModel) -> FlightCreated:
3633
"""
3734
Creates a new flight
3835
@@ -41,7 +38,6 @@ async def create_flight(
4138
"""
4239
with tracer.start_as_current_span("create_flight"):
4340
controller = FlightController()
44-
flight.rocket.motor.set_motor_kind(motor_kind)
4541
return await controller.post_flight(flight)
4642

4743

@@ -59,9 +55,7 @@ async def read_flight(flight_id: str) -> FlightRetrieved:
5955

6056

6157
@router.put("/{flight_id}", status_code=204)
62-
async def update_flight(
63-
flight_id: str, flight: FlightModel, motor_kind: MotorKinds
64-
) -> None:
58+
async def update_flight(flight_id: str, flight: FlightModel) -> None:
6559
"""
6660
Updates an existing flight
6761
@@ -73,7 +67,6 @@ async def update_flight(
7367
"""
7468
with tracer.start_as_current_span("update_flight"):
7569
controller = FlightController()
76-
flight.rocket.motor.set_motor_kind(motor_kind)
7770
return await controller.put_flight_by_id(flight_id, flight)
7871

7972

@@ -144,11 +137,7 @@ async def update_flight_environment(
144137

145138

146139
@router.put("/{flight_id}/rocket", status_code=204)
147-
async def update_flight_rocket(
148-
flight_id: str,
149-
rocket: RocketModel,
150-
motor_kind: MotorKinds,
151-
) -> None:
140+
async def update_flight_rocket(flight_id: str, rocket: RocketModel) -> None:
152141
"""
153142
Updates flight rocket.
154143
@@ -160,7 +149,6 @@ async def update_flight_rocket(
160149
"""
161150
with tracer.start_as_current_span("update_flight_rocket"):
162151
controller = FlightController()
163-
rocket.motor.set_motor_kind(motor_kind)
164152
return await controller.update_rocket_by_flight_id(
165153
flight_id,
166154
rocket=rocket,

src/routes/motor.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
MotorCreated,
1111
MotorRetrieved,
1212
)
13-
from src.models.motor import MotorModel, MotorKinds
13+
from src.models.motor import MotorModel
1414
from src.controllers.motor import MotorController
1515

1616
router = APIRouter(
@@ -27,9 +27,7 @@
2727

2828

2929
@router.post("/", status_code=201)
30-
async def create_motor(
31-
motor: MotorModel, motor_kind: MotorKinds
32-
) -> MotorCreated:
30+
async def create_motor(motor: MotorModel) -> MotorCreated:
3331
"""
3432
Creates a new motor
3533
@@ -38,7 +36,6 @@ async def create_motor(
3836
"""
3937
with tracer.start_as_current_span("create_motor"):
4038
controller = MotorController()
41-
motor.set_motor_kind(motor_kind)
4239
return await controller.post_motor(motor)
4340

4441

@@ -56,9 +53,7 @@ async def read_motor(motor_id: str) -> MotorRetrieved:
5653

5754

5855
@router.put("/{motor_id}", status_code=204)
59-
async def update_motor(
60-
motor_id: str, motor: MotorModel, motor_kind: MotorKinds
61-
) -> None:
56+
async def update_motor(motor_id: str, motor: MotorModel) -> None:
6257
"""
6358
Updates an existing motor
6459
@@ -70,7 +65,6 @@ async def update_motor(
7065
"""
7166
with tracer.start_as_current_span("update_motor"):
7267
controller = MotorController()
73-
motor.set_motor_kind(motor_kind)
7468
return await controller.put_motor_by_id(motor_id, motor)
7569

7670

src/routes/rocket.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
RocketRetrieved,
1212
)
1313
from src.models.rocket import RocketModel
14-
from src.models.motor import MotorKinds
1514
from src.controllers.rocket import RocketController
1615

1716
router = APIRouter(
@@ -28,9 +27,7 @@
2827

2928

3029
@router.post("/", status_code=201)
31-
async def create_rocket(
32-
rocket: RocketModel, motor_kind: MotorKinds
33-
) -> RocketCreated:
30+
async def create_rocket(rocket: RocketModel) -> RocketCreated:
3431
"""
3532
Creates a new rocket
3633
@@ -39,7 +36,6 @@ async def create_rocket(
3936
"""
4037
with tracer.start_as_current_span("create_rocket"):
4138
controller = RocketController()
42-
rocket.motor.set_motor_kind(motor_kind)
4339
return await controller.post_rocket(rocket)
4440

4541

@@ -57,9 +53,7 @@ async def read_rocket(rocket_id: str) -> RocketRetrieved:
5753

5854

5955
@router.put("/{rocket_id}", status_code=204)
60-
async def update_rocket(
61-
rocket_id: str, rocket: RocketModel, motor_kind: MotorKinds
62-
) -> None:
56+
async def update_rocket(rocket_id: str, rocket: RocketModel) -> None:
6357
"""
6458
Updates an existing rocket
6559
@@ -71,7 +65,6 @@ async def update_rocket(
7165
"""
7266
with tracer.start_as_current_span("update_rocket"):
7367
controller = RocketController()
74-
rocket.motor.set_motor_kind(motor_kind)
7568
return await controller.put_rocket_by_id(rocket_id, rocket)
7669

7770

src/services/motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def from_motor_model(cls, motor: MotorModel) -> Self:
4747
"reshape_thrust_curve": False or motor.reshape_thrust_curve,
4848
}
4949

50-
match MotorKinds(motor.selected_motor_kind):
50+
match MotorKinds(motor.motor_kind):
5151
case MotorKinds.LIQUID:
5252
rocketpy_motor = LiquidMotor(**motor_core)
5353
case MotorKinds.HYBRID:

tests/unit/test_routes/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def stub_motor_dump():
2424
dry_mass=0,
2525
dry_inertia=[0, 0, 0],
2626
center_of_dry_mass_position=0,
27+
motor_kind='GENERIC',
2728
)
2829
motor_json = motor.model_dump_json()
2930
return json.loads(motor_json)

0 commit comments

Comments
 (0)