Model Core¶
model_core
¶
Core class for managing the numerical state of a model.
This module provides a lightweight state container and time-grid manager for time-evolving models. It is designed to support:
- Non-uniform time grids via per-step dt accessors.
- Multi-axis state tensors (e.g., state x age x space x traits).
- Optional full history storage for post-analysis.
- Array-API namespace preservation for numerical state and history.
The core intentionally does not build RHS functions or construct operators; it only manages state, time, and shape/axis metadata in a solver-friendly manner.
ModelCore(n_states, n_subgroups, time_grid, *, options=None)
¶
Core state and time manager for time-evolving models.
Examples:
>>> import numpy as np
>>> core = ModelCore(1, 1, np.array([0.0, 0.5, 1.0]))
>>> core.set_initial_state(np.array([[2.0]]))
>>> core.get_current_state().shape
(1, 1)
>>> core.get_time_at(1)
0.5
Initialize ModelCore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_states
|
int
|
Number of state variables. |
required |
n_subgroups
|
int
|
Number of subgroups (e.g., age groups). |
required |
time_grid
|
ndarray
|
1D array of times, shape (n_timesteps,). |
required |
options
|
ModelCoreOptions | None
|
Optional ModelCoreOptions for additional configuration. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if time_grid is invalid or any shapes mismatch. |
Source code in src/op_engine/model_core.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
current_time
property
¶
Current simulation time t = time_grid[current_step].
Returns:
| Type | Description |
|---|---|
float
|
Current time as a float. |
state_ndim
property
¶
Number of state tensor dimensions (rank).
Returns:
| Type | Description |
|---|---|
int
|
State tensor rank as an integer. |
advance_timestep(next_state)
¶
Alias for apply_next_state, for solver-friendly naming.
Source code in src/op_engine/model_core.py
622 623 624 | |
apply_deltas(deltas)
¶
Apply state deltas, advancing the timestep.
Supports alternate solver implementations (e.g., splitting updates, additive increments) without forcing allocation of y_next.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deltas
|
Array
|
State deltas to apply, shape state_shape. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if deltas has incorrect shape. |
Source code in src/op_engine/model_core.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
apply_next_state(next_state)
¶
Set the next state directly, advancing the timestep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
next_state
|
Array
|
State at the next timestep, shape state_shape. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if next_state has incorrect shape. |
Source code in src/op_engine/model_core.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
apply_trajectory(trajectory)
¶
Adopt a complete trajectory produced by a whole-grid solver.
This provides one functional update for strategies such as Diffrax that return every requested output at once. It avoids replaying immutable history through one concatenation per time point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trajectory
|
Array
|
Array shaped |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the trajectory changes the active array namespace. |
ValueError
|
If the trajectory shape is invalid. |
Source code in src/op_engine/model_core.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
axis_index(axis)
¶
Resolve an axis name or integer into an axis index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
str | int
|
Axis name or index. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Axis index as an integer. |
Raises:
| Type | Description |
|---|---|
IndexError
|
if axis index is out of bounds. |
ValueError
|
if axis name is unknown. |
Source code in src/op_engine/model_core.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
get_axis_coords(axis)
¶
Return coordinate array for a given axis, or None if not set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
str | int
|
Axis name or index. |
required |
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Coordinate array for the axis, or None if not set. |
Source code in src/op_engine/model_core.py
278 279 280 281 282 283 284 285 286 287 288 289 290 | |
get_current_state()
¶
Return the current state.
Returns:
| Type | Description |
|---|---|
Array
|
Current state, shape state_shape. |
Source code in src/op_engine/model_core.py
515 516 517 518 519 520 521 522 | |
get_dt(step_idx)
¶
Return dt for the step [t_step_idx, t_step_idx+1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_idx
|
int
|
Timestep index in [0, n_timesteps - 1]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
dt as a float. |
Raises:
| Type | Description |
|---|---|
IndexError
|
if step_idx is out of bounds. |
Source code in src/op_engine/model_core.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
get_state_at(step)
¶
Return the state at a given timestep from history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
Timestep index in [0, n_timesteps). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
State at the given timestep, shape state_shape. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if history is not stored. |
IndexError
|
if step is out of bounds. |
Source code in src/op_engine/model_core.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
get_time_at(step_idx)
¶
Return time at a given step index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step_idx
|
int
|
Timestep index in [0, n_timesteps). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Time as a float. |
Raises:
| Type | Description |
|---|---|
IndexError
|
if step_idx is out of bounds. |
Source code in src/op_engine/model_core.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
reshape_for_axis_solve(x, axis)
¶
Reshape a state-like tensor into 2D for an axis-local operator solve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
State-like tensor of shape state_shape. |
required |
axis
|
str | int
|
Axis name or index along which to reshape. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, tuple[int, ...], int]
|
(x2d, original_shape, axis_index) |
Raises:
| Type | Description |
|---|---|
ValueError
|
if x does not have shape state_shape. |
Source code in src/op_engine/model_core.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
set_initial_state(initial_state)
¶
Set the initial state at time_grid[0].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_state
|
Array
|
Initial state, shape state_shape. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if initial_state has incorrect shape. |
Source code in src/op_engine/model_core.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
unreshape_from_axis_solve(x2d, original_shape, axis)
¶
Inverse of reshape_for_axis_solve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x2d
|
ndarray
|
2D array of shape (axis_len, batch). |
required |
original_shape
|
tuple[int, ...]
|
Original full shape before reshape. |
required |
axis
|
str | int
|
Axis name or index along which the reshape was done. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Reconstructed array of shape original_shape. |
Source code in src/op_engine/model_core.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
validate_state_shape(arr, *, msg=None)
¶
Validate that arr has state_shape.
This is intentionally small and solver-friendly; higher layers can reuse it to avoid duplicating shape checks for intermediate/stage states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
Array
|
Array to validate. |
required |
msg
|
str | None
|
Optional custom error message prefix. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if arr does not have shape state_shape. |
Source code in src/op_engine/model_core.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
ModelCoreOptions(other_axes=(), axis_names=None, axis_coords=None, store_history=True, dtype=np.float64)
dataclass
¶
Optional configuration for ModelCore.
This object groups non-essential constructor parameters to keep the ModelCore initializer compact and stable while allowing future extensions without breaking the public API.
Attributes:
| Name | Type | Description |
|---|---|---|
other_axes |
tuple[int, ...]
|
Additional axis sizes appended after the default (state, subgroup) axes. Example: (n_space,) or (n_space, n_trait). |
axis_names |
tuple[str, ...] | None
|
Optional names for each axis in the state tensor. Length must equal the total state rank. |
axis_coords |
Mapping[str, ndarray] | None
|
Optional coordinate arrays keyed by axis name. This is metadata only and enables non-uniform grids and FV/FDM operator builders. |
store_history |
bool
|
Whether to store the full time history. |
dtype |
DTypeLike
|
NumPy-compatible dtype for internal arrays, resolved to the initial state's namespace when that state is supplied. |
__post_init__()
¶
Validate options that do not depend on a concrete model shape.
Raises:
| Type | Description |
|---|---|
ValueError
|
If an axis size or axis-name count is invalid. |
Source code in src/op_engine/model_core.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |