File size: 2,236 Bytes
f421c2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import numpy as np
from scipy.interpolate import interp1d
class Solution(np.ndarray):
"""Brief description of 'Solution'"""
pass
class SolutionContainer(dict):
"""Brief description of 'SolutionContainer'"""
pass
class SteadyStateSolution(np.ndarray):
r"""Brief description of 'SteadyStateSolution'"""
def __new__(cls, x):
obj = np.asarray(x).view(cls)
return obj
class UnsteadySolution(Solution):
"""Brief description of 'UnsteadySolution'"""
def __new__(cls, x, y):
obj = np.asarray(y).view(cls)
obj._x = np.asarray(x).view(cls)
return obj
def _create_interpolant(self):
self._interpolant = interp1d(self._x, self, bounds_error=True)
def interpolate(self, x):
"""
Interpolates solution at point 'x'.
Parameters
----------
x : float
Point at which the solution is to be interpolated
Returns
-------
ndarray
Solution interpolated at the given point 'x'
"""
# Cache interpolant to avoid overhead
if not hasattr(self, "_interpolant"):
self._create_interpolant()
return self._interpolant(x)
__call__ = interpolate
class TransientSolution(UnsteadySolution):
def interpolate(self, t):
"""
Interpolates solution at time 't'.
Parameters
----------
t : float
Time at which the solution is to be interpolated
Returns
-------
ndarray
Transient solution interpolated at the given time 't'
Notes
-----
't' must reside inside the 'tspan' used during integration.
"""
return super().interpolate(x=t)
@property
def t(self):
r"""
Wrapper to access the generic _x attribute on the super class
"""
return self._x
class PressureScan(UnsteadySolution):
def interpolate(self, p):
s = super().interpolate(x=p).astype(self.dtype)
return s
@property
def p(self):
r"""
Wrapper to access the generic _x attribute on the super class
"""
return self._x
__call__ = interpolate
|