Stockflow: Forrester and causal loop diagrams
Stockflow draws system dynamics diagrams; it does not simulate. Prefer the MCP tools (diagram_*); if they are not
available use the CLI (stockflow …, or node <repo>/bin/stockflow.js …). Both read and write the same store, and the live
viewer (diagram_open / stockflow open) refreshes by itself.
Workflow
- Model first, then draw. List the stocks (what accumulates), the flows that fill/drain each stock, and the auxiliaries, constants and exogenous inputs that drive each flow. Decide every link's polarity.
- Create everything in one call with
diagram_create(orstockflow create file.json). Omitx/y: the automatic layout places nodes. Setauto_loops: trueto add R/B markers for the detected feedback loops. - Check with
diagram_analyze: fix every notation warning and confirm the loop polarities make sense. - Look at it:
diagram_renderwithpreview: truereturns a PNG. If labels cross or links are confusing, adjust withdiagram_update(nodex/y, linkcurvefrom -1.5 to 1.5, floworienth/v, loopx/y) and preview again. Give fixed positions only to the nodes you move; the rest keeps auto-layout. - Publish: set
caption,figure,source,lang(es/en) andlegend: true, thendiagram_renderwithformat: "pdf"(oreps,svg,pngwithdpi: 300). Report the file path(s).
JSON shape
{
"title": "SIR epidemic", "type": "forrester", "auto_loops": true,
"caption": "Stock-and-flow structure of the SIR model.", "figure": "1", "source": "Adapted from Sterman (2000).", "lang": "en", "legend": true,
"nodes": [
{"id": "s", "type": "stock", "label": "Susceptible"},
{"id": "i", "type": "stock", "label": "Infected"},
{"id": "infection", "type": "flow", "label": "Infection rate", "from": "s", "to": "i"},
{"id": "births", "type": "flow", "label": "Births", "from": null, "to": "s"},
{"id": "contact", "type": "constant", "label": "Contact rate"},
{"id": "time", "type": "aux", "label": "Time", "shadow": true}
],
"links": [
{"from": "s", "to": "infection", "polarity": "+"},
{"from": "contact", "to": "infection", "polarity": "+"}
],
"loops": [{"type": "B", "nodes": ["s", "infection"], "label": "Depletion"}]
}
Node types: stock, flow (from/to = stock id or null for a cloud), aux, constant, exogenous,
delay, table, cloud, variable (text node for causal diagrams). Spanish aliases work (nivel, flujo, auxiliar,
constante, exogena, retardo, tabla, nube). Links reference node ids or labels. shadow: true draws <Name>.
Links accept delay: true (‖ mark) and style: "solid"|"dashed".
Notation rules (the analyzer enforces most of them)
- A stock changes only through flows. Never draw an information link into a stock; link into its flow instead.
- Every flow needs at least one driver (an information link into it). Constants and exogenous variables receive no links.
- In causal diagrams (
type: "causal") every link needs a polarity. A loop with an even number of−links is reinforcing (R); an odd number makes it balancing (B). Flows count as+into their target stock and−into their source stock. - Keep labels as short noun phrases (variables, not verbs or sentences); they wrap automatically.
- Mark only the loops that matter to the argument; trivial drain loops (stock ↔ own outflow) are skipped by
auto_loops.
Styles
style: "academic" (default) is monochrome serif, ready for journals and grayscale print. Use
style: "modern" (colour and animation) only for slides or web, when the user asks for it. Animated SVG is only
produced in the modern style.
CLI quick reference
stockflow create model.json --loops # full diagram from JSON
stockflow add <id> stock "Inventory" # incremental edits
stockflow add <id> flow "Sales" --from inventory
stockflow link <id> demand sales +
stockflow analyze <id>
stockflow meta <id> --figure 2 --caption "…" --source "…" --lang en --legend
stockflow render <id> -o figure.pdf # .eps .svg .png (--dpi 300)
stockflow open <id> # live viewer at http://localhost:4610