diff --git a/rocketpy/Function.py b/rocketpy/Function.py index 32b620e8c..48b2e929a 100644 --- a/rocketpy/Function.py +++ b/rocketpy/Function.py @@ -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 ---------- @@ -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']) @@ -3025,6 +3025,7 @@ def __get__(self, instance, owner=None): ) val.__doc__ = self.__doc__ + val.__cached__ = True cache[self.attrname] = val return val @@ -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 diff --git a/rocketpy/motors/HybridMotor.py b/rocketpy/motors/HybridMotor.py index 8a9397398..e02afb2cf 100644 --- a/rocketpy/motors/HybridMotor.py +++ b/rocketpy/motors/HybridMotor.py @@ -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 @@ -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. diff --git a/rocketpy/motors/LiquidMotor.py b/rocketpy/motors/LiquidMotor.py index 8b4d357e3..4f5106452 100644 --- a/rocketpy/motors/LiquidMotor.py +++ b/rocketpy/motors/LiquidMotor.py @@ -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 @@ -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. diff --git a/rocketpy/motors/SolidMotor.py b/rocketpy/motors/SolidMotor.py index db7f04144..0aa040efc 100644 --- a/rocketpy/motors/SolidMotor.py +++ b/rocketpy/motors/SolidMotor.py @@ -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 @@ -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²)")