simulate(definition, options?) starts a simulation of any definition, primitive or composite, and returns its first state.
States never change
Every method that moves a simulation forward returns a new state. The state you called it on stays exactly as it was:let and reassignment, as in the first example, or keep the states you care about in variables. Either way, nothing you’re holding ever changes behind your back.
Setting inputs
set() takes some or all of the external inputs and returns a state where they’re pending. They take effect on the next step() or run().
- Inputs you’ve never set stay
"X". - Setting inputs to the values they already have returns the same state.
- Unknown input names and invalid values (
true,2,"x") throw aSimulationError, and nothing is applied.
Moving through time
To find out how long something took, subtract ticks:
after.tick - before.tick.
Reading outputs
read(name) returns one external output and readAll() returns all of them. Reading never moves time, so a state straight out of set() still shows the old values:
inspect().
Keeping history
Because states never change, history is simply the states you keep. To watch a circuit settle tick by tick:reset(), because nothing needs resetting.
Branching
Any state can be the starting point for several experiments. Settle a latch once, then try different inputs from that same moment:The tick limit
run() takes at most maxTicks steps per call (default 10_000). If the circuit still hasn’t stabilized, it throws a StabilizationError instead of returning. The state you called run() on is still valid, so you can change its inputs and try again:
Independent simulations
Everysimulate() call starts a separate simulation. Elaboration is cached per definition, so starting many is cheap: