Compiler API
The compiler flattens a GenericWorld into a CompiledSpec for the solver backends.
compile_spec
numen.compiler.flatten.compile_spec(world)
Walk a GenericWorld and produce a flat CompiledSpec for the solver.
State/param keys use the full path entity_id.component_kind.field_name.
ExcitationPort-annotated fields are skipped (pure metadata for the
characterization framework; not compiled into p).
Source code in src/numen/compiler/flatten.py
458 459 460 461 462 463 464 465 466 467 468 469 470 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
CompiledSpec
numen.compiler.flatten.CompiledSpec
dataclass
Flat representation of a compiled world, consumed by all solver backends.
Produced by compile_spec(world). Never construct manually.
Attributes:
| Name | Type | Description |
|---|---|---|
state_size |
int
|
Total number of state slots in |
param_size |
int
|
Total number of parameter slots in |
state_index_map |
dict[str, tuple[int, int]]
|
Maps |
param_index_map |
dict[str, tuple[int, int]]
|
Maps |
discrete_dts |
list[float]
|
Sorted list of unique |
x0 |
list[float]
|
Initial state vector (length |
p |
list[float]
|
Parameter vector (length |
differential_mask |
list[float]
|
1.0 for |
systems |
list[CompiledSystem]
|
Compiled systems in world-definition order. |
compiled_callbacks |
list[CompiledCallback]
|
Compiled controller callbacks. |
required_features |
frozenset[str]
|
Feature strings set by |
jac_sparsity_rows |
list[int]
|
Row indices of the Jacobian sparsity pattern (0-based,
COO format). Julia converts to 1-based SparseMatrixCSC
for |
jac_sparsity_cols |
list[int]
|
Column indices of the Jacobian sparsity pattern. |
Source code in src/numen/compiler/flatten.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
dx_view(entity_id, component_type, dx)
Write accessor for an entity's derivative slots. Use inside dynamics functions.
Source code in src/numen/compiler/flatten.py
294 295 296 297 298 299 300 301 302 | |
param_idx(key)
Return the start index for a parameter field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Full path |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer index into the flat parameter vector |
Source code in src/numen/compiler/flatten.py
317 318 319 320 321 322 323 324 325 326 | |
param_slice(key)
Return a slice for a (possibly vector) parameter field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Full path |
required |
Returns:
| Type | Description |
|---|---|
slice
|
|
Source code in src/numen/compiler/flatten.py
340 341 342 343 344 345 346 347 348 349 350 | |
state_idx(key)
Return the start index for a state field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Full path |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer index into the flat state vector |
Source code in src/numen/compiler/flatten.py
306 307 308 309 310 311 312 313 314 315 | |
state_slice(key)
Return a slice for a (possibly vector) state field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Full path |
required |
Returns:
| Type | Description |
|---|---|
slice
|
|
Source code in src/numen/compiler/flatten.py
328 329 330 331 332 333 334 335 336 337 338 | |
view(entity_id, component_type, x, p)
Read-only accessor for an entity's component fields. Use inside dynamics functions.
The component_type's kind literal is used to construct the key prefix
entity_id.kind.* for all field lookups.
Source code in src/numen/compiler/flatten.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
CompiledSystem
numen.compiler.flatten.CompiledSystem
dataclass
Bases: Generic[GroupT]
Compiled representation of one ECS system, ready for solver dispatch.
Attributes:
| Name | Type | Description |
|---|---|---|
dynamics_fn |
str
|
Julia function reference string, e.g. |
entity_ids |
list[str]
|
Flat list of entity IDs operated on by this system.
For multi-slot systems: [a0, s0, b0, a1, s1, b1, …]
where |
group_size |
int
|
Number of entity IDs per dynamics invocation. 1 for
single-entity systems; > 1 for coupled systems declared
with |
entity_groups |
tuple[GroupT, ...]
|
Pre-grouped tuple of entity ID tuples, derived from
|
python_fn |
Any
|
Python callable for scipy/JAX backends. Not serialized. |
Source code in src/numen/compiler/flatten.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
DxBuffer
numen.compiler.flatten.DxBuffer
Derivative accumulator compatible with both NumPy (in-place) and JAX (functional).
NumPy: dx[s] = value mutates in place.
JAX: dx[s] = value calls arr.at[s].set(value) and stores the new array —
preserving JAX's functional semantics while keeping the same user-facing API.
Dynamics functions never touch dx directly; they always go through
spec.dx_view() which wraps dx in a DerivativeView. The buffer is
created by the backend before each RHS call, so it is always fresh.
Source code in src/numen/compiler/flatten.py
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 | |
ComponentView
numen.compiler.flatten.ComponentView
Read-only accessor for a single entity's component fields during a dynamics call.
Attribute reads resolve to values in the flat state/param arrays.
Keys in the spec index maps use the full path entity_id.component_kind.field_name.
Raises AttributeError with a clear message if an unknown field is accessed.
Source code in src/numen/compiler/flatten.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
DerivativeView
numen.compiler.flatten.DerivativeView
Write accessor for a single entity's derivative slots during a dynamics call.
Attribute assignment writes into the flat dx array at the correct index. Only state fields (IntegratedField, DiscreteField, ContinuousField) are writable — attempting to write a ParameterField raises AttributeError.
Source code in src/numen/compiler/flatten.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 | |
Spec & System base classes
numen.spec.component.Component
Bases: BaseModel
Base class for all ECS components.
Subclasses declare fields using Annotated with IntegratedField, DiscreteField, etc.
Example
class TankComponent(Component): kind: Literal["tank"] = "tank" pressure: Annotated[float, IntegratedField()] = 0.0 volume: Annotated[float, ParameterField()] = 1.0
Source code in src/numen/spec/component.py
13 14 15 16 17 18 19 20 21 22 23 24 25 | |
numen.spec.system.System
Bases: BaseModel
Base class for all ECS systems.
Class variables (statically declared, not serialized): component_types: Component subclasses this system auto-queries. compile_spec finds all matching entities and populates entity_ids. Leave empty when using entity_groups instead. entity_slots: Declares slot types and group size for multi-slot systems. compile_spec validates entity_groups against slot_types and stores size in CompiledSystem.group_size. python_fn: Python dynamics callable for the scipy backend.
Pydantic fields (serialized): dynamics_fn: Julia function reference, e.g. "MyModule.my_fn!". entity_ids: Flat entity list for single-type systems (auto-populated from component_types, or set manually to restrict the set). entity_groups: Explicit connection topology for multi-slot systems. Each inner list is one group; types validated against entity_slots.slot_types. compile_spec flattens and caches groups on CompiledSystem.
Single-type system (auto-populated): class MassKinematicsSystem(System): component_types: ClassVar[tuple[type, ...]] = (MassComponent,) python_fn: ClassVar[DynamicsFn] = staticmethod(mass_kinematics_dynamics) kind: Literal["mass_kinematics"] = "mass_kinematics" dynamics_fn: str = "Dynamics.mass_kinematics!"
Multi-slot coupled system (explicit topology): class SpringForceSystem(System): entity_slots: ClassVar[EntityGroup] = EntityGroup(MassComponent, SpringComponent, MassComponent) python_fn: ClassVar[DynamicsFn] = staticmethod(spring_force_dynamics) kind: Literal["spring_force"] = "spring_force" dynamics_fn: str = "Dynamics.spring_force!"
SpringForceSystem(entity_groups=[["m1","s1","m2"], ["m2","s2","m3"]])
Source code in src/numen/spec/system.py
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 | |
numen.spec.system.DynamicsFn
Bases: Protocol[GroupT]
Calling convention for all Python dynamics functions.
Generic over GroupT — the tuple type of one entity group — so the type checker knows the group arity at each call site: CompiledSystem[tuple[str]] → each group is one entity CompiledSystem[tuple[str, str, str]] → each group is three entities
Source code in src/numen/spec/system.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
numen.spec.world.GenericWorld
Bases: BaseModel, Generic[CC, SC, BC]
Typed ECS world. CC, SC, BC are discriminated union types for each catalogue.
Each entity maps to a dict of components keyed by component kind, supporting multiple components per entity:
components = {
"robot": {
"body": BodyComponent(...),
"sensor": SensorComponent(...),
},
"target": {
"point_mass": PointMassComponent(...),
},
}
State/param keys use the full path entity_id.component_kind.field_name.
Example
ComponentCatalogue = Annotated[TankComponent | PipeComponent, Field(discriminator="kind")] SystemCatalogue = Annotated[TankSystem | PipeSystem, Field(discriminator="kind")] CallbackCatalogue = Annotated[ControlCallback, Field(discriminator="kind")]
World = GenericWorld[ComponentCatalogue, SystemCatalogue, CallbackCatalogue]
world = World( components={"tank_a": {"tank": TankComponent(...)}}, systems={"tank_sys": TankSystem(...)}, callbacks={}, )
Source code in src/numen/spec/world.py
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 | |