Skip to main content
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:
You can write it step by step with 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.
The one mistake to watch for is calling a method without using its result. A bare sim.set({ a: 1 }) creates a new state and throws it away, and sim is unchanged. Write sim = sim.set({ a: 1 }).

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 a SimulationError, and nothing is applied.

Moving through time

Every state has: 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:
For internal signals, use inspect().

Keeping history

Because states never change, history is simply the states you keep. To watch a circuit settle tick by tick:
Starting over works the same way: keep the first state and go back to it. There’s no 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:
Each branch keeps its own trace, and the branches share their common history.

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:
The same goes for evaluator errors. If a primitive throws in the middle of a step, the step never produces a state, and the state you started from is untouched.

Independent simulations

Every simulate() call starts a separate simulation. Elaboration is cached per definition, so starting many is cheap: