Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 26 additions & 4 deletions rocketpy/Function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2916,8 +2916,8 @@ def calcOutput(func, inputs):


def funcify_method(*args, **kwargs):
"""Decorator factory to wrap methods as Function objects and save them as cached
properties.
"""Decorator factory to wrap methods as Function objects and save them as
cached properties.

Parameters
----------
Expand Down Expand Up @@ -2952,8 +2952,8 @@ def funcify_method(*args, **kwargs):
>>> g(2)
11

2. Method which returns a rocketpy.Function instance. An interesting use is to reset
input and output names after algebraic operations.
2. Method which returns a rocketpy.Function instance. An interesting use is
to reset input and output names after algebraic operations.

>>> class Example():
... @funcify_method(inputs=['x'], outputs=['x**3'])
Expand Down Expand Up @@ -3025,6 +3025,7 @@ def __get__(self, instance, owner=None):
)

val.__doc__ = self.__doc__
val.__cached__ = True
cache[self.attrname] = val
return val

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


def reset_funcified_methods(instance):
"""Resets all the funcified methods of the instance. It does so by
deleting the current Functions, which will make the interperter redefine
them when they are called. This is useful when the instance has changed
and the methods need to be recalculated.

Parameters
----------
instance : object
The instance of the class whose funcified methods will be recalculated.
The class must have a mutable __dict__ attribute.

Return
------
None
"""
for key in list(instance.__dict__):
if hasattr(instance.__dict__[key], "__cached__"):
instance.__dict__.pop(key)


if __name__ == "__main__":
import doctest

Expand Down
4 changes: 3 additions & 1 deletion rocketpy/motors/HybridMotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
except ImportError:
from rocketpy.tools import cached_property

from rocketpy.Function import funcify_method

from rocketpy.Function import funcify_method, reset_funcified_methods

from .LiquidMotor import LiquidMotor
from .Motor import Motor
Expand Down Expand Up @@ -531,6 +532,7 @@ def addTank(self, tank, position):
"""
self.liquid.addTank(tank, position)
self.solid.massFlowRate = self.totalMassFlowRate - self.liquid.massFlowRate
reset_funcified_methods(self)

def allInfo(self):
"""Prints out all data and graphs available about the Motor.
Expand Down
3 changes: 2 additions & 1 deletion rocketpy/motors/LiquidMotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
except ImportError:
from rocketpy.tools import cached_property

from rocketpy.Function import Function, funcify_method
from rocketpy.Function import Function, funcify_method, reset_funcified_methods

from .Motor import Motor

Expand Down Expand Up @@ -429,6 +429,7 @@ def addTank(self, tank, position):
geometry reference zero point.
"""
self.positioned_tanks.append({"tank": tank, "position": position})
reset_funcified_methods(self)

def allInfo(self):
"""Prints out all data and graphs available about the Motor.
Expand Down
4 changes: 3 additions & 1 deletion rocketpy/motors/SolidMotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
except ImportError:
from rocketpy.tools import cached_property

from rocketpy.Function import Function, funcify_method
from rocketpy.Function import Function, funcify_method, reset_funcified_methods

from .Motor import Motor

Expand Down Expand Up @@ -511,6 +511,8 @@ def terminateBurn(t, y):
"constant",
)

reset_funcified_methods(self)

return [self.grainInnerRadius, self.grainHeight]

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