Skip to content

Motor: update type hints and tests#253

Open
akarev0 wants to merge 8 commits into
reilleya:stagingfrom
akarev0:refactoring/motor-module-type-hints
Open

Motor: update type hints and tests#253
akarev0 wants to merge 8 commits into
reilleya:stagingfrom
akarev0:refactoring/motor-module-type-hints

Conversation

@akarev0
Copy link
Copy Markdown
Contributor

@akarev0 akarev0 commented Oct 26, 2025

Motor module updates.

  • added type hints and return values (might be inaccurate but it will help to identify issues)
  • implemented early quit for methods that supposed to work with self.propellant because it could be None and could cause errors (I have added ValueError but I might need to be converted into SimAllert, @reilleya wdyt?)
  • moved Mach related functions into separate module to prevent colisioin because of same values could be reused
  • added a proposal for Mach function doc-string and arguments naming (sorry it was confusing for me :D) also let me know if you OK with google style for doc-strings
  • moved slag rate calculations into separate method for better visability
  • updated related tests (removed duplication)

@akarev0 akarev0 marked this pull request as ready for review October 26, 2025 15:46
Copy link
Copy Markdown
Owner

@reilleya reilleya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of thoughts! Thanks for this!

Comment thread motorlib/motor.py Outdated
using the pressure to determine how the motor will regress in the given timestep at the current pressure.
def runSimulation(self, callback=None) -> SimulationResult:
"""
Runs a simulation of the motor and returns a simRes instance with the results.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess breaking these up ("google style"?) could make them easier to read, but I don't think they were all that long to begin with. Maybe revert this for now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, reverted

Comment thread motorlib/motor.py Outdated
"""
if not self.propellant:
raise ValueError(
"Ideal pressure cannot be calculated. Propelant is missing."
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: propelant -> propellant

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed, thanks )

Comment thread motorlib/motor.py
optionally be passed in to save time on motors where calculating surface area is expensive.
"""
if not self.propellant:
raise ValueError(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exception is correct here rather than a simAlert. simAlerts are intended for cases where the user has done something that they need to be alerted about. If this function is ever called without a propellant set, it is likely a software bug that should bubble up as an exception.

Comment thread motorlib/motor.py Outdated
"""Calculates the mach number in the core of a grain for a given chamber pressure and mass flux."""
if not self.propellant:
raise ValueError(
"Ideal pressure cannot be calculated. Propelant is missing."
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Ideal pressure -> Mach number, Propelant -> Propellant

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed, thanks )

Comment thread motorlib/motor.py Outdated
simRes.channels["volumeLoading"].addData(
100 * (1 - (self.calcFreeVolume(perGrainReg) / motorVolume))
)
simRes.channels["massFlow"].addData([0] * len(self.grains))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a really minor potential performance improvement given that this isn't in a loop or anything. Maybe not worth the diff?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, reverted

Comment thread motorlib/motor.py Outdated
for g, r in zip(self.grains, perGrainReg)
]
)
if callback(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like a lot of what ruff has done, but this just reads worse to me. Absolutely no reason to split a line this short with one function call with one arg over so many lines... I'll look into if there's a way to configure ruff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, reverted

Comment thread motorlib/constants.py
maximumRefDiameter = 6.6

# Max core Mach number subsonic limit
machSubsonicLimit: float = 1.0
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little bit silly because this is always going to be exactly 1.0. Maybe only worth doing if we think we are going to change the type someday? Certainly not going to have to update this value in multiple places. Up to you if you want to keep it or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main point here is to update raw float value into meaningful constant, which from my prospective makes code easier to read and understand, even if it looks silly :)

Comment thread motorlib/utils.py Outdated
@@ -0,0 +1,52 @@
"""Module for utils functions to prevent reassign values from an outer scope."""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow, can you give an example of what this is trying to avoid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for the lack of clarity. My intent with this module description is not to implement a language feature, but to enforce a code convention that avoids a specific type of subtle bug and confusion.

Comment thread motorlib/utils.py
) -> float:
"""Calculates the objective function for finding the Mach number.

This function represents a rearranged form of the isentropic mass flow equation
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this!

@akarev0 akarev0 force-pushed the refactoring/motor-module-type-hints branch from 651bfe9 to 0a5c3bf Compare November 4, 2025 08:55
@akarev0 akarev0 requested a review from reilleya January 7, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants