Source code for vivyd.models.viv_model
from ..typing import arrf64
from ..solvers.compatibility import SolverCompatible
from abc import ABC, abstractmethod
from numpy import asarray
from typing import Callable
[docs]
class VIVModel(SolverCompatible, ABC):
"""
Base class for vortex-induced vibration (VIV) models. All VIV models must
inherit from this class to implement its attributes and methods, necessary
for having a common interface.
"""
[docs]
def __call__(self, t: float, state: arrf64, handle: Callable = (lambda t, state: None)) -> arrf64:
return self.rhs(t, state, handle=handle)
[docs]
@abstractmethod
def rhs(self, t: float, state: arrf64, handle: Callable = (lambda t, state: None)) -> arrf64: ...
[docs]
def validate_state(self, state: arrf64) -> arrf64:
"""
Validate the shape of the state vector.
Args:
state: State vector.
Returns:
arrf64: Validated state vector.
Raises:
ValueError: If the state vector has an invalid shape.
"""
array = asarray(state, dtype=float)
if array.ndim != 1:
raise ValueError("state must be a one-dimensional array")
if array.shape[0] != self.state_size:
raise ValueError(f"expected state size {self.state_size}, got {array.shape[0]}")
return array