OP System¶
op_system
¶
op_system.
Domain-agnostic RHS specification + compilation utilities.
Public API (v1)¶
Primary user entrypoints:
- compile_spec: Validate, normalize, and compile a RHS specification in one step.
- normalize_rhs: Validate and normalize a YAML-friendly RHS specification.
- compile_rhs: Compile a NormalizedRhs into an efficient callable RHS.
Core data structures:
- NormalizedRhs
- CompiledRhs
Design guarantees: - No dependency on provider/adapters (eg flepimop2). - Stable interface for downstream engines. - Forward-compatible with multiphysics extensions.
IdentifierString = Annotated[str, AfterValidator(_validate_identifier_string)]
module-attribute
¶
Custom pydantic type for validated identifier strings used in op_system.
Identifier strings are used for state names, dimension names, and other keys in the system. They must be non-empty, contain only alphanumeric characters, and start with a letter. Leading and trailing whitespace is stripped before validation.
Examples:
>>> from pydantic import BaseModel
>>> from op_system import IdentifierString
>>> class ExampleModel(BaseModel):
... identifier: IdentifierString
...
>>> ExampleModel(identifier="S")
ExampleModel(identifier='S')
>>> ExampleModel(identifier=" Foobar ")
ExampleModel(identifier='Foobar')
>>> ExampleModel(identifier="123abc")
Traceback (most recent call last):
...
pydantic_core._pydantic_core.ValidationError: 1 validation error for ExampleModel
identifier
Value error, IdentifierString must contain only alphanumerical characters and start with a letter. [...]
For further information visit ...
>>> ExampleModel(identifier="")
Traceback (most recent call last):
...
pydantic_core._pydantic_core.ValidationError: 1 validation error for ExampleModel
identifier
Value error, IdentifierString must not be empty. [...]
For further information visit ...
CompiledRhs(state_names, param_names, eval_fn)
dataclass
¶
Container for a compiled RHS evaluation function.
bind(params)
¶
Bind parameter values and return a 2-arg RHS: rhs(t, y) -> dydt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Mapping[str, object]
|
Mapping of parameter names to values. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[float64, Float64Array], Float64Array]
|
A callable |
Source code in src/op_system/compile.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
EvalFn
¶
Bases: Protocol
Callable RHS evaluator supporting runtime parameter kwargs.
NormalizedRhs(kind, state_names, equations, aliases, param_names, all_symbols, meta)
dataclass
¶
Normalized RHS representation suitable for compilation/execution.
StateString
¶
Bases: BaseModel
Structured representation of a state string.
A state string is either a bare state name like "S" or a state name
followed immediately by bracketed dimensions like "R[age,vax]".
Examples:
>>> StateString.model_validate("S")
StateString(name='S', dims=())
>>> recovery = StateString.model_validate("R[age,vax]")
>>> recovery
StateString(name='R', dims=('age', 'vax'))
>>> print(recovery)
R[age,vax]
>>> recovery.model_dump()
'R[age,vax]'
>>> StateString.model_validate("Foobar[ age , vax ]")
StateString(name='Foobar', dims=('age', 'vax'))
__str__()
¶
Return the compact string form.
Returns:
| Type | Description |
|---|---|
str
|
Compact state string. |
Examples:
>>> str(StateString(name="S", dims=()))
'S'
>>> str(StateString(name="R", dims=("age",)))
'R[age]'
>>> str(StateString(name="R", dims=("age", "vax")))
'R[age,vax]'
>>> str(StateString(name="lambda", dims=("age", "vax", "state")))
'lambda[age,vax,state]'
Source code in src/op_system/_state_string.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
compile_rhs(rhs)
¶
Compile a normalized RHS into a runnable evaluation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
NormalizedRhs
|
Normalized RHS produced by |
required |
Returns:
| Type | Description |
|---|---|
CompiledRhs
|
A |
Source code in src/op_system/compile.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
compile_spec(spec)
¶
Validate, normalize, and compile a RHS specification in one call.
This is the recommended public entrypoint for most users and adapters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[str, object]
|
Raw RHS specification mapping (YAML/JSON friendly). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CompiledRhs |
CompiledRhs
|
Runnable RHS callable container. |
Source code in src/op_system/__init__.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
normalize_expr_rhs(spec)
¶
Normalize an expression-based RHS specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Mapping[str, Any]
|
Raw RHS specification mapping. |
required |
Returns:
| Type | Description |
|---|---|
NormalizedRhs
|
Backend-facing normalized RHS representation. |
Source code in src/op_system/specs.py
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 | |
normalize_rhs(spec)
¶
Normalize a RHS specification dict into a backend-facing representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Mapping[str, Any] | None
|
Raw RHS specification mapping. |
required |
Returns:
| Type | Description |
|---|---|
NormalizedRhs
|
Backend-facing normalized RHS representation. |
Source code in src/op_system/specs.py
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 | |
normalize_transitions_rhs(spec)
¶
Normalize a transition-based RHS specification (diagram/hazard semantics).
Returns:
| Type | Description |
|---|---|
NormalizedRhs
|
Backend-facing normalized RHS representation for transitions kind. |
Source code in src/op_system/specs.py
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 | |