Skip to content

Commit c389162

Browse files
Merge branch 'enh/liquid-motors' into enh/move-liquid-plots-and-prints
2 parents be524dc + 031db97 commit c389162

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

rocketpy/Function.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,8 +2916,8 @@ def calcOutput(func, inputs):
29162916

29172917

29182918
def funcify_method(*args, **kwargs):
2919-
"""Decorator factory to wrap methods as Function objects and save them as cached
2920-
properties.
2919+
"""Decorator factory to wrap methods as Function objects and save them as
2920+
cached properties.
29212921
29222922
Parameters
29232923
----------
@@ -2952,8 +2952,8 @@ def funcify_method(*args, **kwargs):
29522952
>>> g(2)
29532953
11
29542954
2955-
2. Method which returns a rocketpy.Function instance. An interesting use is to reset
2956-
input and output names after algebraic operations.
2955+
2. Method which returns a rocketpy.Function instance. An interesting use is
2956+
to reset input and output names after algebraic operations.
29572957
29582958
>>> class Example():
29592959
... @funcify_method(inputs=['x'], outputs=['x**3'])
@@ -3025,6 +3025,7 @@ def __get__(self, instance, owner=None):
30253025
)
30263026

30273027
val.__doc__ = self.__doc__
3028+
val.__cached__ = True
30283029
cache[self.attrname] = val
30293030
return val
30303031

@@ -3034,6 +3035,27 @@ def __get__(self, instance, owner=None):
30343035
return funcify_method_decorator
30353036

30363037

3038+
def reset_funcified_methods(instance):
3039+
"""Resets all the funcified methods of the instance. It does so by
3040+
deleting the current Functions, which will make the interperter redefine
3041+
them when they are called. This is useful when the instance has changed
3042+
and the methods need to be recalculated.
3043+
3044+
Parameters
3045+
----------
3046+
instance : object
3047+
The instance of the class whose funcified methods will be recalculated.
3048+
The class must have a mutable __dict__ attribute.
3049+
3050+
Return
3051+
------
3052+
None
3053+
"""
3054+
for key in list(instance.__dict__):
3055+
if hasattr(instance.__dict__[key], "__cached__"):
3056+
instance.__dict__.pop(key)
3057+
3058+
30373059
if __name__ == "__main__":
30383060
import doctest
30393061

rocketpy/motors/HybridMotor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
except ImportError:
1010
from rocketpy.tools import cached_property
1111

12-
from rocketpy.Function import funcify_method
12+
from rocketpy.Function import funcify_method, reset_funcified_methods
1313
from rocketpy.plots.hybrid_motor_plots import _HybridMotorPlots
1414
from rocketpy.prints.hybrid_motor_prints import _HybridMotorPrints
1515

@@ -499,6 +499,7 @@ def addTank(self, tank, position):
499499
"""
500500
self.liquid.addTank(tank, position)
501501
self.solid.massFlowRate = self.totalMassFlowRate - self.liquid.massFlowRate
502+
reset_funcified_methods(self)
502503

503504
def info(self):
504505
"""Prints out basic data about the Motor."""

rocketpy/motors/LiquidMotor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
except ImportError:
1010
from rocketpy.tools import cached_property
1111

12-
from rocketpy.Function import Function, funcify_method
12+
from rocketpy.Function import Function, funcify_method, reset_funcified_methods
1313
from rocketpy.plots.liquid_motor_plots import _LiquidMotorPlots
1414
from rocketpy.prints.liquid_motor_prints import _LiquidMotorPrints
1515

@@ -437,6 +437,7 @@ def addTank(self, tank, position):
437437
geometry reference zero point.
438438
"""
439439
self.positioned_tanks.append({"tank": tank, "position": position})
440+
reset_funcified_methods(self)
440441

441442
def info(self):
442443
"""Prints out basic data about the Motor."""

rocketpy/motors/SolidMotor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
except ImportError:
1616
from rocketpy.tools import cached_property
1717

18-
from rocketpy.Function import Function, funcify_method
18+
from rocketpy.Function import Function, funcify_method, reset_funcified_methods
1919

2020
from .Motor import Motor
2121

@@ -518,6 +518,8 @@ def terminateBurn(t, y):
518518
"constant",
519519
)
520520

521+
reset_funcified_methods(self)
522+
521523
return [self.grainInnerRadius, self.grainHeight]
522524

523525
@funcify_method("Time (s)", "burn area (m²)")

0 commit comments

Comments
 (0)