Spaces:
Sleeping
Sleeping
File size: 1,939 Bytes
5f38e95 | 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 | r"""Construct a CableModel by mode name.
The factory exists for two reasons:
1. **Strict mode names.** Anywhere the framework lets the user select a
constitutive law by string (configuration files, CLI flags, scenario
dataclasses, dissertation reports), the same canonical names live
here. Typos surface as :class:`ValueError`, not silent fall-throughs.
2. **One place to keep the registry.** Adding a fourth constitutive law
means adding it here; every consumer of :func:`cable_model_by_name`
picks it up automatically.
The directive explicitly forbids mixing the three laws at runtime.
:func:`cable_model_by_name` therefore never composes one model from
another; it just dispatches.
"""
from __future__ import annotations
from typing import Any, Literal
from cdpr.cables.base import CableModel
from cdpr.cables.irvine import IrvineModel
from cdpr.cables.kelvin_voigt import KelvinVoigtModel
from cdpr.cables.sqck_hybrid import SQCKHybridModel
CableModeName = Literal["kelvin_voigt", "irvine", "sqck_hybrid"]
_REGISTRY: dict[str, type[CableModel]] = {
"kelvin_voigt": KelvinVoigtModel,
"irvine": IrvineModel,
"sqck_hybrid": SQCKHybridModel,
}
def available_modes() -> tuple[str, ...]:
"""Names of the registered constitutive modes."""
return tuple(_REGISTRY)
def cable_model_by_name(name: CableModeName | str, **params: Any) -> CableModel:
"""Construct the named cable model.
Parameters
----------
name:
One of ``"kelvin_voigt"``, ``"irvine"``, ``"sqck_hybrid"``. Any
other value raises :class:`ValueError`.
**params:
Forwarded to the model's constructor. Unknown parameters raise
:class:`TypeError` --- there is no silent acceptance.
"""
if name not in _REGISTRY:
raise ValueError(
f"Unknown cable mode {name!r}; choose from {available_modes()}."
)
cls = _REGISTRY[name]
return cls(**params)
|