:>> [[ABIO]] → ABIO Docs → ABIO bio
Bio Module¶
Core biology simulation classes.
alienbio.bio
¶
Bio module: the chemistry substrate alienbio's instrument runs on.
Protocols (for type hints) — from alienbio.protocols.bio; the
implementations conform under pyright src/ (protocols/_conformance):
- Atom, Molecule, Reaction, Chemistry: the entities
- Flow: transport between compartments (TransportFlux, GeneralFlow)
- CompartmentTree, WorldState: topology and the dense multi-compartment store
- Simulator: the reference and JAX steppers, which must agree to 1e-9
Implementations:
- AtomImpl, MoleculeImpl, ReactionImpl (+ Modulation), ChemistryImpl
- CompartmentImpl, CompartmentTreeImpl, WorldStateImpl
- WorldImpl: the declarative world and its single resolution point
- WorldSimulatorImpl / ReactionSpec, and jax_simulator.JaxWorldSimulator
- TransportFlux, GeneralFlow; PerCapitaGrowth / PerCapitaDeath / CountFlow
- conservation / energy canaries, the compiled rate grammar (rate_expr)
The M1 single-compartment runtime that used to live beside these
(ReferenceSimulatorImpl, BioSystem, StateImpl, the agent/task
layer, equilibrium/perturbation/quiescence analysis, MembraneFlow) was
deleted in T056 (2026-09-10): it ran a different physics from the world
simulator and nothing on the instrument's path imported it.
Atom
¶
Bases: Protocol
Protocol for atomic elements.
Atoms are the building blocks of molecules. Each atom has: - symbol: 1-2 letter chemical notation (e.g., "C", "H", "Na") - name: Human-readable name (e.g., "Carbon", "Hydrogen") - atomic_weight: Mass in atomic mass units
Source code in src/alienbio/protocols/bio.py
Molecule
¶
Bases: Protocol
Protocol for molecule entities.
Molecules are composed of atoms and have: - atoms: Composition as {Atom: count} - bdepth: Biosynthetic depth (0 = primitive, higher = more complex) - name: Human-readable name (e.g., "glucose", "water") - symbol: Chemical formula derived from atoms (e.g., "C6H12O6") - molecular_weight: Computed from atom weights
Source code in src/alienbio/protocols/bio.py
local_name
property
¶
The molecule's local name within its parent entity.
atoms
property
¶
Atom composition: {atom: count}.
bdepth
property
¶
Biosynthetic depth (0 = primitive, 4+ = complex).
name
property
¶
Human-readable name: 'glucose', 'water'.
symbol
property
¶
Chemical formula derived from atoms: 'C6H12O6', 'H2O'.
molecular_weight
property
¶
Molecular mass computed from atom weights.
Reaction
¶
Bases: Protocol
Protocol for reaction entities.
Reactions define transformations within a single compartment. Each reaction has reactants, products, and a rate.
Source code in src/alienbio/protocols/bio.py
local_name
property
¶
The reaction's local name.
name
property
¶
Human-readable name.
symbol
property
¶
Formula string: 'A + B -> C + D'.
reactants
property
¶
Reactant molecules and their stoichiometric coefficients.
products
property
¶
Product molecules and their stoichiometric coefficients.
modifiers
property
¶
Catalyst/regulator molecules acting on the reaction without being
stoichiometrically consumed, mapped to a modulation value: a bidirectional
rate-modulation descriptor (bio.reaction.Modulation — kind + params, e.g.
an activator/inhibitor) or a bare opaque role tag str (e.g. an enzyme with
role "catalyst"), which is rate-inert. Empty for an unmodified reaction.
rate
property
¶
Reaction rate (constant or function of state).
Chemistry
¶
Bases: Protocol
Protocol for chemistry containers.
Chemistry acts as the "world" for a chemical system, holding atoms, molecules, and reactions as public dict attributes.
Source code in src/alienbio/protocols/bio.py
local_name
property
¶
The chemistry's local name.
atoms
property
¶
All atoms in this chemistry (by symbol).
molecules
property
¶
All molecules in this chemistry (by name).
reactions
property
¶
All reactions in this chemistry (by name).
validate()
¶
neighbors(node)
¶
paths(a, b, max_len=8)
¶
subgraph(nodes)
¶
CompartmentTree
¶
Bases: Protocol
Protocol for compartment topology.
Represents the hierarchical structure of compartments (organism > organ > cell). Stored separately from concentrations to allow efficient updates.
Source code in src/alienbio/protocols/bio.py
WorldState
¶
Bases: Protocol
Protocol for world concentration state.
Stores concentrations for all compartments and molecules. Dense storage: [num_compartments x num_molecules] array. Can be extended with sparse overflow for large molecule counts.
Each WorldState holds a reference to its CompartmentTree. Multiple states can share the same tree (immutable sharing). When topology changes (e.g., cell division), a new tree is created and new states point to it while historical states keep their original tree reference.
Source code in src/alienbio/protocols/bio.py
tree
property
¶
The compartment tree this state belongs to.
num_compartments
property
¶
Number of compartments.
num_molecules
property
¶
Number of molecules in vocabulary.
get(compartment, molecule)
¶
set(compartment, molecule, value)
¶
get_compartment(compartment)
¶
get_multiplicity(compartment)
¶
set_multiplicity(compartment, value)
¶
total_molecules(compartment, molecule)
¶
copy()
¶
Simulator
¶
Bases: Protocol
Protocol for simulators.
A Simulator advances the state of a chemical system over time. Applies reactions within compartments and flows across membranes.
Source code in src/alienbio/protocols/bio.py
MockDat
¶
Lightweight mock DAT for hydrating entities without a real DAT.
Used when creating entities from YAML specs that don't have backing DAT files. Provides the minimal interface needed by Entity.
Source code in src/alienbio/infra/entity.py
AtomImpl
¶
Implementation: A chemical element.
Atoms are the building blocks of molecules. They are essentially constants representing chemical elements with their properties.
Attributes:
| Name | Type | Description |
|---|---|---|
symbol |
str
|
Chemical symbol (1-2 letters): 'C', 'H', 'O', 'Na' |
name |
str
|
Human-readable name: 'Carbon', 'Hydrogen' |
atomic_weight |
float
|
Atomic mass in atomic mass units |
Source code in src/alienbio/bio/atom.py
symbol
property
¶
Chemical symbol (1-2 letters): 'C', 'H', 'O', 'Na'.
name
property
¶
Human-readable name: 'Carbon', 'Hydrogen'.
atomic_weight
property
¶
Atomic mass in atomic mass units.
__init__(symbol, name, atomic_weight)
¶
Initialize an atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Chemical symbol (1-2 letters) |
required |
name
|
str
|
Human-readable English name |
required |
atomic_weight
|
float
|
Atomic mass in atomic mass units |
required |
Source code in src/alienbio/bio/atom.py
__eq__(other)
¶
__hash__()
¶
__repr__()
¶
MoleculeImpl
¶
Bases: Entity
Implementation: A molecule in the biological system.
Molecules are composed of atoms and participate in reactions.
Attributes:
| Name | Type | Description |
|---|---|---|
atoms |
Dict[AtomImpl, int]
|
Atom composition as {AtomImpl: count} |
bdepth |
int
|
Biosynthetic depth (0 = primitive, higher = more complex) |
name |
str
|
Human-readable name (e.g., 'glucose', 'water') |
symbol |
str
|
Chemical formula derived from atoms (e.g., 'C6H12O6', 'H2O') |
molecular_weight |
float
|
Computed from atom weights |
formation_energy |
Optional[float]
|
Free energy of formation, an opaque assigned scalar (F018);
|
Source code in src/alienbio/bio/molecule.py
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 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 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 136 137 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 | |
atoms
property
¶
Atom composition: {atom: count}.
bdepth
property
¶
Biosynthetic depth (0 = primitive, 4+ = complex).
formation_energy
property
¶
Free energy of formation (F018), or None if energy-neutral.
name
property
¶
Human-readable name: 'glucose', 'water'.
symbol
property
¶
Chemical formula derived from atoms: 'C6H12O6', 'H2O'.
Atoms are ordered by Hill system: C first, then H, then alphabetically.
molecular_weight
property
¶
Molecular mass computed from atom weights.
__init__(local_name, *, parent=None, dat=None, description='', atoms=None, bdepth=0, name=None, formation_energy=None)
¶
Initialize a molecule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_name
|
str
|
Local name within parent (used as entity identifier) |
required |
parent
|
Optional[Entity]
|
Link to containing entity |
None
|
dat
|
Optional[DatLike]
|
DAT anchor for root molecules |
None
|
description
|
str
|
Human-readable description |
''
|
atoms
|
Optional[Dict[AtomImpl, int]]
|
Atom composition as {AtomImpl: count} |
None
|
bdepth
|
int
|
Biosynthetic depth (0 = primitive) |
0
|
name
|
Optional[str]
|
Human-readable name (defaults to local_name) |
None
|
formation_energy
|
Optional[float]
|
Free energy of formation (F018), an opaque assigned
scalar. |
None
|
Source code in src/alienbio/bio/molecule.py
hydrate(data, *, dat=None, parent=None, local_name=None)
classmethod
¶
Create a Molecule from a dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dict with optional keys: name, bdepth, atoms, description |
required |
dat
|
Optional[DatLike]
|
DAT anchor (if root entity) |
None
|
parent
|
Optional[Entity]
|
Parent entity (if child) |
None
|
local_name
|
Optional[str]
|
Override name (defaults to data["name"]) |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
New MoleculeImpl instance |
Source code in src/alienbio/bio/molecule.py
attributes()
¶
Semantic content of this molecule.
Source code in src/alienbio/bio/molecule.py
__repr__()
¶
Full representation.
Source code in src/alienbio/bio/molecule.py
ReactionImpl
¶
Bases: Entity
Implementation: A reaction transforming reactants into products.
Reactions define transformations in the biological system.
Each reaction has:
- reactants: molecules consumed (with stoichiometric coefficients)
- products: molecules produced (with stoichiometric coefficients)
- modifiers: catalysts/regulators acting on the reaction WITHOUT being
consumed (enzymes, inhibitors), each mapped to a Modulation (or a
bare opaque role-tag str, for backward compat — see Modulation)
- rate: constant or function determining reaction speed
Example
A + 2B -> C, catalyzed by enzyme E, with rate 0.1¶
reaction = ReactionImpl( "r1", reactants={mol_a: 1, mol_b: 2}, products={mol_c: 1}, modifiers={enzyme_e: "catalyst"}, rate=0.1, parent=chemistry, )
Source code in src/alienbio/bio/reaction.py
83 84 85 86 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 136 137 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 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 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 | |
reactants
property
¶
Reactant molecules and their stoichiometric coefficients.
products
property
¶
Product molecules and their stoichiometric coefficients.
modifiers
property
¶
Catalyst/regulator molecules (not consumed) mapped to a Modulation
(or a bare opaque role-tag str, for backward compat — see Modulation).
rate
property
¶
Reaction rate (constant or function).
name
property
¶
Human-readable name (same as local_name).
symbol
property
¶
Formula string: 'glucose + ATP -> G6P + ADP'.
rate_law
property
¶
The compiled rate expression (bio.rate_expr tree, species by
name), or None for plain mass action (M47.10).
__init__(name, *, reactants=None, products=None, modifiers=None, rate=1.0, rate_law=None, parent=None, dat=None, description='')
¶
Initialize a reaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Local name within parent |
required |
reactants
|
Optional[Dict[MoleculeImpl, float]]
|
Dict mapping molecules to stoichiometric coefficients |
None
|
products
|
Optional[Dict[MoleculeImpl, float]]
|
Dict mapping molecules to stoichiometric coefficients |
None
|
modifiers
|
Optional[Mapping[MoleculeImpl, ModifierValue]]
|
Mapping catalyst/regulator molecules (not consumed) to a
|
None
|
rate
|
RateValue
|
Reaction rate (constant float or function of StateImpl) |
1.0
|
rate_law
|
Optional[Any]
|
Optional compiled rate expression ( |
None
|
parent
|
Optional[Entity]
|
Link to containing entity |
None
|
dat
|
Optional[DatLike]
|
DAT anchor for root reactions |
None
|
description
|
str
|
Human-readable description |
''
|
Source code in src/alienbio/bio/reaction.py
hydrate(data, *, molecules, dat=None, parent=None, local_name=None)
classmethod
¶
Create a Reaction from a dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dict with keys: reactants, products, rate, name, description |
required |
molecules
|
dict[str, 'MoleculeImpl']
|
Dict mapping molecule names to MoleculeImpl instances |
required |
dat
|
Optional[DatLike]
|
DAT anchor (if root entity) |
None
|
parent
|
Optional[Entity]
|
Parent entity (if child) |
None
|
local_name
|
Optional[str]
|
Override name (defaults to data key) |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
New ReactionImpl instance |
Source code in src/alienbio/bio/reaction.py
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 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
set_rate(rate)
¶
add_reactant(molecule, coefficient=1.0)
¶
add_product(molecule, coefficient=1.0)
¶
add_modifier(molecule, role='')
¶
attributes()
¶
Semantic content of this reaction.
Source code in src/alienbio/bio/reaction.py
__repr__()
¶
Full representation.
Source code in src/alienbio/bio/reaction.py
Modulation
dataclass
¶
A modifier's effect on its reaction's rate (F015 S2 bidirectional modulation).
Ship-now form is LINEAR (F015 Q1): an "activator" (param a) scales the rate
up via (1 + a * [modifier]); an "inhibitor" (param Ki) scales it down via
dividing by (1 + [modifier] / Ki). Two saturable kinds (M38.3): "michaelis"
(params Vmax/K) and "hill" (additionally n) — see the field docs
below and WorldSimulatorImpl._modulation_factor. Any other kind (including
the label-only default "") is rate-inert — a pure documentation tag,
contributing a factor of exactly 1.0.
Frozen + a pure function of the frozen start-of-step state elsewhere (F015 Q4): this dataclass only carries the parameters, it has no simulation behavior of its own.
Source code in src/alienbio/bio/reaction.py
from_value(value)
classmethod
¶
Coerce a bare str role tag into a label-only, rate-inert Modulation.
Backward-compat (F015 Q2): every existing call site passes a bare str role
(e.g. "catalyst"); that role becomes Modulation(kind=<the string>), whose
factor is exactly 1.0 — matching today, where the role never reached the
simulator.
Source code in src/alienbio/bio/reaction.py
to_dict()
¶
Serialize non-default fields (for attributes()/hydrate() round-trips).
Source code in src/alienbio/bio/reaction.py
Flow
¶
Bases: ABC
Abstract base class for all flows.
Flows move molecules (or instances) between compartments. Each flow is anchored to an origin compartment.
Subclasses: - TransportFlux: amount-conserving transport between any two compartments - GeneralFlow: arbitrary state modifications (placeholder)
Common interface: - origin: the compartment where this flow is anchored - name: human-readable identifier - compute_flux(): calculate transfer rate - apply(): modify state based on flux
Source code in src/alienbio/bio/flow.py
origin
property
¶
The origin compartment (where this flow is anchored).
name
property
¶
Human-readable name.
is_membrane_flow
abstractmethod
property
¶
True if this is a membrane flow (origin ↔ parent).
is_general_flow
abstractmethod
property
¶
True if this is a general flow (arbitrary edits).
__init__(origin, name='')
¶
Initialize base flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
CompartmentId
|
The origin compartment (where this flow is anchored) |
required |
name
|
str
|
Human-readable name for this flow |
''
|
Source code in src/alienbio/bio/flow.py
compute_flux(state, tree)
abstractmethod
¶
Compute flux for this flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
WorldStateImpl
|
Current world state with concentrations |
required |
tree
|
CompartmentTreeImpl
|
Compartment topology |
required |
Returns:
| Type | Description |
|---|---|
float
|
Flux value (positive = into origin for membrane flows) |
Source code in src/alienbio/bio/flow.py
apply(state, tree, dt=1.0)
abstractmethod
¶
Apply this flow to the state (mutates in place).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
WorldStateImpl
|
World state to modify |
required |
tree
|
CompartmentTreeImpl
|
Compartment topology |
required |
dt
|
float
|
Time step |
1.0
|
Source code in src/alienbio/bio/flow.py
GeneralFlow
¶
Bases: Flow
Arbitrary state modifications (placeholder).
GeneralFlow is a catch-all for flows that don't fit the TransportFlux pattern. This includes: - Lateral flows between siblings - Instance transfers (RBCs moving between compartments) - Any other arbitrary edits to the system
NOTE: This is currently a placeholder. Full implementation will require a more general interpreter to handle arbitrary state modifications specified via Expr or similar.
For now, GeneralFlow stores an apply_fn that takes state and tree and performs arbitrary modifications.
Source code in src/alienbio/bio/flow.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
description
property
¶
Description of what this flow does.
is_membrane_flow
property
¶
False - this is not a membrane flow.
is_general_flow
property
¶
True - this is a general flow.
__init__(origin, apply_fn=None, name='', description='')
¶
Initialize a general flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
CompartmentId
|
The compartment where this flow is conceptually anchored |
required |
apply_fn
|
Optional[Callable[[WorldStateImpl, CompartmentTreeImpl, float], None]]
|
Function (state, tree, dt) -> None that modifies state |
None
|
name
|
str
|
Human-readable name for this flow |
''
|
description
|
str
|
Description of what this flow does |
''
|
NOTE: This is a placeholder. Full implementation will need a more general interpreter to support Expr-based specifications.
Source code in src/alienbio/bio/flow.py
compute_flux(state, tree)
¶
General flows don't have a simple flux concept.
Returns 0.0 as placeholder. The actual work happens in apply().
apply(state, tree, dt=1.0)
¶
Apply this flow to the state (mutates in place).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
WorldStateImpl
|
World state to modify |
required |
tree
|
CompartmentTreeImpl
|
Compartment topology |
required |
dt
|
float
|
Time step |
1.0
|
Source code in src/alienbio/bio/flow.py
attributes()
¶
Semantic content for serialization.
NOTE: apply_fn cannot be serialized. Full implementation will need Expr-based specification that can be serialized.
Source code in src/alienbio/bio/flow.py
__repr__()
¶
TransportFlux
¶
Bases: Flow
Cross-compartment flux: moves conserved AMOUNT (not concentration) between two independently-addressed compartments (F016/S3, skeleton decision S3 / coverage gap G3).
Unlike the M1 MembraneFlow it replaced (anchored to a parent-child pair
via the tree; deleted in T056), origin/dest here are two arbitrary compartments — no tree
relationship required, which is what lets a :class:~alienbio.suite.blocks.
SpatialLatticeBlock wire an arbitrary neighbor graph.
Rate law (Q1=C, gradient default) — the event rate is driven by ONE
driver_molecule's concentration:
rate_law="gradient"(default): Fickian,rate_constant * ([X]_origin - [X]_dest)— drives the driver species toward equal concentration across the two pools (the mechanism a diffusive lattice relaxes through).rate_law="first_order":rate_constant * [X]_origin— a pump/boundary-flavored unidirectional law (no dependence ondest).
Either law's raw rate is floored at 0: this ONE flux is strictly
origin -> dest. A reversed local gradient (or a negative first-order
rate) contributes nothing from THIS flux — wire a second, reversed
TransportFlux for true bidirectional equilibration (e.g. a lattice's
neighbor pair in each direction). This also protects against oscillation:
once the driver species reaches equality, tiny numerical overshoot floors
to 0 instead of flip-flopping sign every step.
stoichiometry ({molecule_id: count}) lets several species move together per event — active
co-transport needs no new law, just an extra (possibly negative-count,
counter-direction) entry co-transporting an energy carrier. Every species
is rationed against the SAME shared event count (so a co-transported group
moves in lockstep): for each species, the event count is clamped so its
LOSING compartment's :meth:WorldStateImpl.amount never goes negative —
the amount-conservation invariant (F012 count basis) this class exists to
guarantee. The identical transferred amount Δn leaves the losing pool
and enters the other, so Σ amount is invariant regardless of the two
compartments' volumes/multiplicities.
Source code in src/alienbio/bio/flow.py
226 227 228 229 230 231 232 233 234 235 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
dest
property
¶
The compartment this flux moves species INTO.
stoichiometry
property
¶
Molecule id -> count moved per event (shared event count).
driver_molecule
property
¶
Which molecule id's concentration drives the rate law.
rate_constant
property
¶
D (gradient law) or k (first-order law).
rate_law
property
¶
"gradient" (Fickian) or "first_order".
is_membrane_flow
property
¶
False - this is not a parent-child membrane flow.
is_general_flow
property
¶
False - this is not an arbitrary-edit general flow.
__init__(origin, dest, stoichiometry, driver_molecule, rate_constant=1.0, rate_law='gradient', name='')
¶
Initialize a cross-compartment transport flux.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
CompartmentId
|
The compartment this flux moves species OUT OF (the "src" pool) |
required |
dest
|
CompartmentId
|
The compartment this flux moves species INTO (the "dst" pool) |
required |
stoichiometry
|
Dict[int, float]
|
Molecule id -> count moved per event (all species move together, in lockstep, scaled by the shared event count) |
required |
driver_molecule
|
int
|
Which molecule id's concentration drives the rate law |
required |
rate_constant
|
float
|
|
1.0
|
rate_law
|
str
|
|
'gradient'
|
name
|
str
|
Human-readable name for this flow |
''
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in src/alienbio/bio/flow.py
compute_flux(state, tree)
¶
Raw (unfloored, unrationed) event rate from the configured rate law.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
WorldStateImpl
|
Current world state with concentrations |
required |
tree
|
CompartmentTreeImpl
|
Compartment topology (unused — origin/dest need no tree relationship) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float
|
Event rate (events per unit time); may be negative (floored to 0 |
|
in |
float
|
meth: |
Source code in src/alienbio/bio/flow.py
demand(frozen, tree, dt=1.0)
¶
The event count this flux wants this step, read off the FROZEN
start-of-step state, and the AMOUNT it would draw from each losing
(compartment, molecule) at that count (T053).
The losing pool for a species is origin when its count is positive (origin -> dest), else dest (a negative count antiports that species).
Source code in src/alienbio/bio/flow.py
apply_events(state, scales, event_count)
¶
Move event_count events' worth of every species (amounts read
against scales' multiplicity x volume), mutating state.
Source code in src/alienbio/bio/flow.py
apply(state, tree, dt=1.0)
¶
Apply this flux ALONE to state (mutates in place): the event
count read off state, rationed against every transported species'
available AMOUNT in its losing compartment, then the same clamped
count moved for each species. The stepper does not call this — it
runs every flow together through :func:apply_flows, which rations
the SUMMED demand of all flows on each pool; this is the one-flow
path for callers that step a flux by hand.
Source code in src/alienbio/bio/flow.py
attributes()
¶
Semantic content for serialization.
Source code in src/alienbio/bio/flow.py
__repr__()
¶
Full representation.
Source code in src/alienbio/bio/flow.py
ChemistryImpl
¶
Bases: Entity
Implementation: Container for a chemical system.
Chemistry holds atoms, molecules, and reactions as public dict attributes. These are indexed by: - atoms: by symbol ("C", "H", "O") - molecules: by name ("glucose", "atp") - reactions: by name ("glycolysis_step1", "atp_synthesis")
Chemistry is conceptually immutable - built complete via constructor, though the dicts are technically mutable for flexibility.
Example
chem = ChemistryImpl( "glycolysis", atoms={"C": carbon, "H": hydrogen, "O": oxygen}, molecules={"glucose": glucose_mol, "atp": atp_mol}, reactions={"step1": reaction1, "step2": reaction2}, dat=dat, )
Direct access to contents¶
chem.atoms["C"] # -> carbon atom chem.molecules["glucose"] # -> glucose molecule chem.reactions["step1"] # -> reaction1
Source code in src/alienbio/bio/chemistry.py
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 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 136 137 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 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 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 | |
__init__(name, *, atoms=None, molecules=None, reactions=None, parent=None, dat=None, description='')
¶
Initialize a chemistry container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Local name within parent |
required |
atoms
|
Optional[Dict[str, AtomImpl]]
|
Dict of atoms by symbol |
None
|
molecules
|
Optional[Dict[str, MoleculeImpl]]
|
Dict of molecules by name |
None
|
reactions
|
Optional[Dict[str, ReactionImpl]]
|
Dict of reactions by name |
None
|
parent
|
Optional[Entity]
|
Link to containing entity |
None
|
dat
|
Optional[DatLike]
|
DAT anchor for root chemistry entities |
None
|
description
|
str
|
Human-readable description |
''
|
Source code in src/alienbio/bio/chemistry.py
hydrate(data, *, dat=None, parent=None, local_name=None)
classmethod
¶
Create a Chemistry from a dict.
Recursively hydrates molecules and reactions from nested dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dict with keys: molecules, reactions, atoms, description Each molecule/reaction can be a dict that gets hydrated. |
required |
dat
|
Optional[DatLike]
|
DAT anchor (if root entity) |
None
|
parent
|
Optional[Entity]
|
Parent entity (if child) |
None
|
local_name
|
Optional[str]
|
Override name |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
New ChemistryImpl with hydrated molecules and reactions |
Source code in src/alienbio/bio/chemistry.py
133 134 135 136 137 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 201 202 203 204 205 206 207 208 | |
validate()
¶
Validate the chemistry for consistency.
Checks: - All molecule atoms are atoms in this chemistry - All reaction reactants/products are molecules in this chemistry
Returns:
| Type | Description |
|---|---|
list[str]
|
List of error messages (empty if valid) |
Source code in src/alienbio/bio/chemistry.py
neighbors(node)
¶
paths(a, b, max_len=8)
¶
All simple paths (by name) from a to b within max_len edges.
subgraph(nodes)
¶
The induced sub-chemistry over nodes (edges to dropped nodes removed).
Reuses the surviving molecule objects; rebuilds each surviving reaction with its reactant/product entries filtered to the kept molecules (rate is carried through by identity). All atoms are retained (they are not graph nodes).
Source code in src/alienbio/bio/chemistry.py
match(pattern)
¶
All subgraph embeddings of pattern into this chemistry.
Molecules match on (name, symbol, bdepth, molecular_weight) equality,
reactions structurally; injectivity and every pattern edge are enforced.
Returns each embedding as {pattern_name: host_name}; [] if none.
Source code in src/alienbio/bio/chemistry.py
attributes()
¶
Semantic content of this chemistry.
Source code in src/alienbio/bio/chemistry.py
__repr__()
¶
Full representation.
CompartmentImpl
¶
Bases: Entity
Implementation: A compartment in the biological hierarchy.
Compartments represent biological regions: organisms, organs, cells, organelles. Each compartment can contain child compartments, forming a tree structure.
The compartment entity specifies: - Structure: kind and child compartments - Initial state: multiplicity and concentrations - Behavior: membrane flows and active reactions
This entity tree serves as both the initial WorldState specification and the complete simulation configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
Type of compartment ("organism", "organ", "cell", "organelle", etc.) |
multiplicity |
float
|
Number of instances (default 1.0) |
volume |
float
|
Volume of each instance in arbitrary units (default 1.0) |
concentrations |
Dict[str, float]
|
Initial molecule concentrations {molecule_name: value} |
membrane_flows |
List[GeneralFlow]
|
Flows across this compartment's membrane |
active_reactions |
Optional[List[str]]
|
Reactions active here (None = all from chemistry) |
children |
List[CompartmentImpl]
|
Child compartments |
Example
Define an organism with cells¶
organism = CompartmentImpl( "body", volume=70000, # 70 liters in mL kind="organism", concentrations={"glucose": 5.0, "oxygen": 2.0}, )
liver = CompartmentImpl( "liver", volume=1500, # 1.5 liters in mL parent=organism, kind="organ", )
hepatocyte = CompartmentImpl( "hepatocyte", volume=3e-9, # ~3000 cubic microns in mL parent=liver, kind="cell", multiplicity=1e9, # 1 billion liver cells concentrations={"glucose": 1.0}, membrane_flows=[glucose_uptake_flow], active_reactions=["glycolysis", "gluconeogenesis"], )
Source code in src/alienbio/bio/compartment.py
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 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 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 136 137 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 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 239 | |
kind
property
¶
Type of compartment: 'organism', 'organ', 'cell', 'organelle'.
multiplicity
property
¶
Number of instances of this compartment.
volume
property
¶
Volume of each instance in arbitrary units.
concentrations
property
¶
Initial molecule concentrations {name: value}.
membrane_flows
property
¶
Flows across this compartment's membrane.
active_reactions
property
¶
Reaction names active in this compartment (None = all).
children
property
¶
Child compartments.
__init__(local_name, *, volume, parent=None, dat=None, description='', kind='compartment', multiplicity=1.0, concentrations=None, membrane_flows=None, active_reactions=None)
¶
Initialize a compartment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_name
|
str
|
Local name within parent (used as entity identifier) |
required |
volume
|
float
|
Volume of each instance (required - no default, scale depends on use case) |
required |
parent
|
Optional[Entity]
|
Parent compartment (or None for root) |
None
|
dat
|
Optional[DatLike]
|
DAT anchor for root compartments |
None
|
description
|
str
|
Human-readable description |
''
|
kind
|
str
|
Type of compartment ("organism", "organ", "cell", "organelle") |
'compartment'
|
multiplicity
|
float
|
Number of instances of this compartment (default 1.0) |
1.0
|
concentrations
|
Optional[Dict[str, float]]
|
Initial molecule concentrations {name: value} |
None
|
membrane_flows
|
Optional[List[GeneralFlow]]
|
Flows across this compartment's membrane |
None
|
active_reactions
|
Optional[List[str]]
|
Reaction names active here (None = all from chemistry) |
None
|
Source code in src/alienbio/bio/compartment.py
add_child(child)
¶
add_flow(flow)
¶
set_concentration(molecule, value)
¶
set_multiplicity(value)
¶
set_volume(value)
¶
set_active_reactions(reactions)
¶
all_descendants()
¶
Get all descendant compartments (depth-first).
Source code in src/alienbio/bio/compartment.py
all_compartments()
¶
depth()
¶
Get depth in tree (root = 0).
attributes()
¶
Semantic content for serialization.
Source code in src/alienbio/bio/compartment.py
__repr__()
¶
CompartmentTreeImpl
¶
Implementation: Hierarchical structure of compartments.
Represents the tree topology of compartments (organism > organ > cell > organelle). Stored separately from concentrations to allow efficient structure updates.
The tree is represented with: - parents: List[Optional[CompartmentId]] - parent[child] = parent_id or None for root - children: Dict[CompartmentId, List[CompartmentId]] - children by parent
Compartments are identified by integer IDs (0, 1, 2, ...).
Example
Create tree: organism with two organs¶
tree = CompartmentTreeImpl() organism = tree.add_root("organism") # 0 organ_a = tree.add_child(organism, "organ_a") # 1 organ_b = tree.add_child(organism, "organ_b") # 2 cell_1 = tree.add_child(organ_a, "cell_1") # 3
print(tree.parent(cell_1)) # 1 (organ_a) print(tree.children(organism)) # [1, 2]
Source code in src/alienbio/bio/compartment_tree.py
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 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 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 136 137 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 | |
num_compartments
property
¶
Total number of compartments.
__init__()
¶
Initialize empty compartment tree.
Source code in src/alienbio/bio/compartment_tree.py
parent(child)
¶
children(parent)
¶
root()
¶
is_root(compartment)
¶
name(compartment)
¶
add_root(name='root')
¶
Add the root compartment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Human-readable name for the root |
'root'
|
Returns:
| Type | Description |
|---|---|
CompartmentId
|
The root compartment ID (always 0) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If root already exists |
Source code in src/alienbio/bio/compartment_tree.py
add_child(parent, name='')
¶
Add a child compartment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
CompartmentId
|
Parent compartment ID |
required |
name
|
str
|
Human-readable name for the child |
''
|
Returns:
| Type | Description |
|---|---|
CompartmentId
|
The new compartment ID |
Source code in src/alienbio/bio/compartment_tree.py
ancestors(compartment)
¶
Get all ancestors from compartment to root (inclusive).
Source code in src/alienbio/bio/compartment_tree.py
descendants(compartment)
¶
Get all descendants of a compartment (not including self).
Source code in src/alienbio/bio/compartment_tree.py
depth(compartment)
¶
Get depth of compartment (root = 0).
to_dict()
¶
from_dict(data)
classmethod
¶
Deserialize tree structure.
Source code in src/alienbio/bio/compartment_tree.py
__repr__()
¶
__str__()
¶
Tree visualization.
Source code in src/alienbio/bio/compartment_tree.py
WorldStateImpl
¶
Implementation: Dense concentration storage for all compartments.
Stores concentrations as a flat array indexed by [compartment, molecule]. Also stores multiplicity (instance count) per compartment. Dense storage is efficient for small to medium molecule counts.
Each WorldState holds a reference to its CompartmentTree. Multiple states share the same tree reference (immutable sharing) until topology changes. When topology changes (e.g., cell division), a new tree is created.
Attributes:
| Name | Type | Description |
|---|---|---|
tree |
CompartmentTreeImpl
|
The CompartmentTree this state belongs to (shared reference) |
num_compartments |
int
|
Number of compartments (derived from tree) |
num_molecules |
int
|
Number of molecules in vocabulary |
concentrations |
int
|
Flat array [num_compartments * num_molecules] |
multiplicities |
int
|
Array [num_compartments] - instance count per compartment |
The concentration array is row-major: concentrations[comp * num_molecules + mol]
Multiplicity represents how many instances of this compartment exist. For example, "arterial red blood cells" might have multiplicity 1e6. Concentrations are per-instance; total molecules = multiplicity * concentration.
Example
tree = CompartmentTreeImpl() root = tree.add_root("organism") cell = tree.add_child(root, "cell") state = WorldStateImpl(tree=tree, num_molecules=50)
Set concentrations¶
state.set(compartment=cell, molecule=5, value=1.0) print(state.get(cell, 5)) # 1.0
Set multiplicity (number of cells)¶
state.set_multiplicity(cell, 1000.0) print(state.get_multiplicity(cell)) # 1000.0
Source code in src/alienbio/bio/world_state.py
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 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 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 136 137 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 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 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 | |
tree
property
¶
The compartment tree this state belongs to (shared reference).
num_compartments
property
¶
Number of compartments (from tree).
num_molecules
property
¶
Number of molecules in vocabulary.
compartment_ids
property
¶
Ordered real compartment ids ([i] labels index i), or None.
None on a pure-int state (the hot-loop simulator path). Present on
self-describing snapshots so real ids surface without fabrication.
molecule_ids
property
¶
Ordered real molecule ids ([j] labels index j), or None.
__init__(tree, num_molecules, initial_concentrations=None, initial_multiplicities=None, compartment_ids=None, molecule_ids=None, initial_volumes=None)
¶
Initialize world state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
CompartmentTreeImpl
|
CompartmentTree defining the topology (shared reference) |
required |
num_molecules
|
int
|
Number of molecules in vocabulary |
required |
initial_concentrations
|
Optional[List[float]]
|
Optional flat array of initial concentrations |
None
|
initial_multiplicities
|
Optional[List[float]]
|
Optional array of initial multiplicities per compartment |
None
|
compartment_ids
|
Optional[List[str]]
|
Optional ordered real ids for the compartment axis
( |
None
|
molecule_ids
|
Optional[List[str]]
|
Optional ordered real ids for the molecule axis
( |
None
|
Source code in src/alienbio/bio/world_state.py
concentration(compartment_id, molecule_id)
¶
Get concentration by REAL ids (the unified WorldState.get surface).
Translates ids → int indices via the ordered axes, then reads the flat
store (int indexing is the *Impl optimization). Requires a
self-describing state (both id axes present).
Raises:
| Type | Description |
|---|---|
ValueError
|
if this state has no id axes (pure-int state). |
KeyError
|
if either id is absent from its axis. |
Source code in src/alienbio/bio/world_state.py
get(compartment, molecule)
¶
set(compartment, molecule, value)
¶
Set concentration of molecule in compartment.
get_compartment(compartment)
¶
Get all concentrations for a compartment.
set_compartment(compartment, values)
¶
Set all concentrations for a compartment.
Source code in src/alienbio/bio/world_state.py
get_multiplicity(compartment)
¶
set_multiplicity(compartment, value)
¶
get_all_multiplicities()
¶
total_molecules(compartment, molecule)
¶
Get total molecules = multiplicity * concentration.
get_volume(compartment)
¶
set_volume(compartment, value)
¶
get_all_volumes()
¶
amount(compartment, molecule)
¶
Get the amount (count) = multiplicity * volume * concentration.
The extensive source of truth for conservation: reactions and flows conserve
amount, not concentration. With the default volume 1.0 this equals
total_molecules and existing worlds are unchanged.
Source code in src/alienbio/bio/world_state.py
copy()
¶
Create a copy of this state (shares tree reference; propagates id axes).
Source code in src/alienbio/bio/world_state.py
as_array()
¶
Get concentrations as 2D numpy array [compartments x molecules].
Returns a view if numpy is available, otherwise a list of lists.
Source code in src/alienbio/bio/world_state.py
from_array(arr)
¶
Set concentrations from 2D array [compartments x molecules].
Source code in src/alienbio/bio/world_state.py
__repr__()
¶
__str__()
¶
Short representation with summary stats.
Source code in src/alienbio/bio/world_state.py
WorldSimulatorImpl
¶
Implementation: Multi-compartment simulator with reactions and flows.
Simulates a world with: - Multiple compartments organized in a tree (organism > organ > cell) - Reactions that occur within compartments - Flows that transport molecules across compartment membranes
Each step: 1. Compute all reaction rates (per compartment) 2. Compute all flow fluxes (between parent-child pairs) 3. Apply reactions (modify concentrations within compartments) 4. Apply flows (transfer molecules across membranes)
Example
Build world¶
tree = CompartmentTreeImpl() organism = tree.add_root("organism") cell = tree.add_child(organism, "cell")
Define reactions and flows¶
reactions = [ReactionSpec("r1", {0: 1}, {1: 1}, rate_constant=0.1)] flows = [GeneralFlow(child=cell, molecule=0, rate_constant=0.05)]
Create simulator¶
sim = WorldSimulatorImpl( tree=tree, reactions=reactions, flows=flows, num_molecules=10, dt=0.1, )
Run simulation¶
state = WorldStateImpl(tree=tree, num_molecules=10) state.set(organism, 0, 100.0) # initial concentration history = sim.run(state, steps=1000, sample_every=100)
All states in history share the same tree reference¶
assert history[0].tree is history[-1].tree
Source code in src/alienbio/bio/world_simulator.py
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 136 137 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 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 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 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 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 | |
tree
property
¶
Compartment topology.
reactions
property
¶
Reaction specifications.
flows
property
¶
Flow specifications.
population_laws
property
¶
Count-based rate-law records driving the multiplicity axis (F017).
num_molecules
property
¶
Number of molecules in vocabulary.
dt
property
¶
Time step size.
__init__(tree, reactions, flows, num_molecules, dt=1.0, population_laws=None)
¶
Initialize world simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
CompartmentTreeImpl
|
Compartment topology |
required |
reactions
|
List[ReactionSpec]
|
List of reaction specifications |
required |
flows
|
Sequence[Flow]
|
List of flow specifications |
required |
num_molecules
|
int
|
Number of molecules in vocabulary |
required |
dt
|
float
|
Time step size |
1.0
|
population_laws
|
Optional[Sequence[PopulationLaw]]
|
Optional list of count-based rate-law records driving
the multiplicity axis (F017); empty (the default) is the fast path —
|
None
|
Source code in src/alienbio/bio/world_simulator.py
step(state)
¶
Advance simulation by one time step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
WorldStateImpl
|
Current world state |
required |
Returns:
| Type | Description |
|---|---|
WorldStateImpl
|
New state after applying reactions and flows |
Source code in src/alienbio/bio/world_simulator.py
run(state, steps, sample_every=None)
¶
Run simulation for multiple steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
WorldStateImpl
|
Initial state (not modified) |
required |
steps
|
int
|
Number of steps to run |
required |
sample_every
|
Optional[int]
|
If set, only keep every Nth state (plus final) |
None
|
Returns:
| Type | Description |
|---|---|
List[WorldStateImpl]
|
List of states (timeline) |
Source code in src/alienbio/bio/world_simulator.py
from_chemistry(chemistry, tree, flows=None, dt=1.0, population_laws=None)
classmethod
¶
Create simulator from a Chemistry and compartment tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chemistry
|
ChemistryImpl
|
Chemistry containing molecules and reactions |
required |
tree
|
CompartmentTreeImpl
|
Compartment topology |
required |
flows
|
Optional[Sequence[Flow]]
|
Optional list of flows (empty if not provided) |
None
|
dt
|
float
|
Time step |
1.0
|
population_laws
|
Optional[Sequence[PopulationLaw]]
|
Optional list of count-based rate-law records (F017; empty if not provided — the no-op fast path) |
None
|
Returns:
| Type | Description |
|---|---|
WorldSimulatorImpl
|
Configured WorldSimulatorImpl |
Source code in src/alienbio/bio/world_simulator.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 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 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 | |
__repr__()
¶
Full representation.
Source code in src/alienbio/bio/world_simulator.py
ReactionSpec
¶
Specification for a reaction in the world simulator.
Reactions occur within a single compartment and transform molecules. This is a lightweight spec using molecule IDs for efficient simulation.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Human-readable name |
|
reactants |
Dict[MoleculeId, stoichiometry] |
|
products |
Dict[MoleculeId, stoichiometry] |
|
rate_constant |
Base reaction rate |
|
compartments |
Which compartments this reaction occurs in (None = all) |
|
modulators |
Dict[MoleculeId, Modulation] — non-consumed modifier species that scale the rate (F015 S2); empty for an unmodified reaction (the fast path). |
Source code in src/alienbio/bio/world_simulator.py
CountFlow
¶
Bases: PopulationLaw
Size-class transition (F017 Q2=A): moves Δmultiplicity origin -> dest at
rate_constant · N_origin, floored at 0 and clamped so origin never goes
negative. A maturation edge — the same extent leaves origin and enters
dest, so headcount is conserved exactly (parallel in shape to
:class:~alienbio.bio.flow.TransportFlux, but on the multiplicity axis rather
than a molecule pool).
Source code in src/alienbio/bio/population.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
origin
property
¶
The compartment this flow moves multiplicity OUT OF.
dest
property
¶
The compartment this flow moves multiplicity INTO.
rate_constant
property
¶
k.
__init__(origin, dest, rate_constant=1.0, name='')
¶
Initialize a count flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin
|
CompartmentId
|
the compartment this flow moves multiplicity OUT OF |
required |
dest
|
CompartmentId
|
the compartment this flow moves multiplicity INTO |
required |
rate_constant
|
float
|
|
1.0
|
name
|
str
|
human-readable name |
''
|
Source code in src/alienbio/bio/population.py
compute_extent(frozen)
¶
Raw (unrationed) Δmultiplicity from the frozen state, floored at 0.
attributes()
¶
Semantic content for serialization.
__repr__()
¶
PerCapitaDeath
¶
Bases: PopulationLaw
Per-capita death: extent = rate_constant · N · dt, floored at 0 and clamped
so a compartment's multiplicity never goes negative.
Optionally releases biomass back to a named pool (the reverse of
:class:PerCapitaGrowth's draw) — release_stoich · extent resource AMOUNT is
added to (release_compartment, release_resource) when all three are supplied;
absent (the default), death is a pure multiplicity shrink with no release.
Source code in src/alienbio/bio/population.py
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 | |
compartment
property
¶
The population compartment whose multiplicity shrinks.
rate_constant
property
¶
k.
release_compartment
property
¶
Optional compartment receiving released biomass.
release_resource
property
¶
Optional molecule id receiving released biomass.
release_stoich
property
¶
Resource AMOUNT released per death.
__init__(compartment, rate_constant=1.0, release_compartment=None, release_resource=None, release_stoich=0.0, name='')
¶
Initialize a per-capita death law.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartment
|
CompartmentId
|
the population compartment whose multiplicity shrinks |
required |
rate_constant
|
float
|
|
1.0
|
release_compartment
|
Optional[CompartmentId]
|
optional compartment receiving released biomass |
None
|
release_resource
|
Optional[MoleculeId]
|
optional molecule id receiving released biomass |
None
|
release_stoich
|
float
|
resource AMOUNT released per death, when both
|
0.0
|
name
|
str
|
human-readable name |
''
|
Source code in src/alienbio/bio/population.py
compute_extent(frozen)
¶
Raw (unrationed) Δmultiplicity magnitude from the frozen state, floored at 0.
attributes()
¶
Semantic content for serialization.
Source code in src/alienbio/bio/population.py
__repr__()
¶
PerCapitaGrowth
¶
Bases: PopulationLaw
Resource-coupled per-capita growth (F017 Q3=A/Q4=A).
extent = rate_constant · N_compartment · [resource]_resource_compartment · dt,
floored at 0 (a growth law never itself causes shrinkage — use
:class:PerCapitaDeath for that). Bilinear in population size and resource
concentration: as the resource pool draws down, growth self-limits toward 0 —
logistic boundedness from nutrient limitation, with no separate governor.
The resource draw is rationed exactly like a reaction's reactant (see
:class:~alienbio.bio.flow.TransportFlux's amount-rationing precedent): if
stoich · extent exceeds the resource pool's available AMOUNT, the extent is
scaled down so the pool never goes negative. stoich == 0.0 (the uncoupled
config) skips the draw entirely — growth still happens, but nothing funds the new
instances' biomass, so the F012 amount-canary (:func:alienbio.bio.conservation.
total_quantity) fires: matter created from nothing. This is deliberate — it is
the negative half of F017's conservation red-then-green test.
Source code in src/alienbio/bio/population.py
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 136 137 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 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 239 240 241 242 | |
compartment
property
¶
The population compartment whose multiplicity grows.
resource_compartment
property
¶
The compartment holding the resource pool.
resource
property
¶
Molecule id of the resource.
stoich
property
¶
Resource AMOUNT consumed per new instance.
rate_constant
property
¶
k.
__init__(compartment, resource_compartment, resource, stoich, rate_constant=1.0, name='')
¶
Initialize a resource-coupled per-capita growth law.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compartment
|
CompartmentId
|
the population compartment whose multiplicity grows |
required |
resource_compartment
|
CompartmentId
|
the compartment holding the resource pool (may equal
|
required |
resource
|
MoleculeId
|
molecule id of the resource |
required |
stoich
|
float
|
resource AMOUNT consumed per new instance ( |
required |
rate_constant
|
float
|
|
1.0
|
name
|
str
|
human-readable name |
''
|
Source code in src/alienbio/bio/population.py
compute_extent(frozen)
¶
Raw (unrationed) Δmultiplicity from the frozen state, floored at 0.
Source code in src/alienbio/bio/population.py
attributes()
¶
Semantic content for serialization.
Source code in src/alienbio/bio/population.py
__repr__()
¶
Full representation.
Source code in src/alienbio/bio/population.py
PopulationLaw
¶
Bases: ABC
Abstract base for a typed count-based rate-law record (multiplicity axis).
Subclasses: - PerCapitaGrowth: resource-coupled per-capita growth - PerCapitaDeath: per-capita death, with an optional biomass release - CountFlow: size-class maturation (Δmultiplicity origin -> dest)
Source code in src/alienbio/bio/population.py
name
property
¶
Human-readable name.
contribute(frozen, dt, mult_delta, mol_delta)
abstractmethod
¶
Read frozen (start-of-step state) and ACCUMULATE this law's
Δmultiplicity / Δconcentration into the shared caller-supplied dicts.
Never mutates frozen, and never reads the dicts back — every law is a
pure function of the frozen state, so order among laws does not matter.
Source code in src/alienbio/bio/population.py
get_atom(symbol)
¶
Get an atom by its symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Chemical symbol (e.g., 'C', 'H', 'Na') |
required |
Returns:
| Type | Description |
|---|---|
AtomImpl
|
The AtomImpl for that element |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the symbol is not in COMMON_ATOMS |