Skip to main content

Declarative stock-flow DSL over GDS semantics — system dynamics with formal guarantees

Project description

gds-stockflow

PyPI Python License

Declarative stock-flow DSL over GDS semantics — system dynamics with formal guarantees.

Table of Contents

Quick Start

pip install gds-stockflow
from stockflow import (
    Stock, Flow, Auxiliary, Converter,
    StockFlowModel, compile_model, compile_to_system, verify,
)

# Declare a simple population model
model = StockFlowModel(
    name="Population",
    stocks=[Stock(name="Population", initial=1000.0)],
    flows=[
        Flow(name="Births", target="Population"),
        Flow(name="Deaths", source="Population"),
    ],
    auxiliaries=[
        Auxiliary(name="Birth Rate", inputs=["Population", "Fertility"]),
        Auxiliary(name="Death Rate", inputs=["Population"]),
    ],
    converters=[Converter(name="Fertility")],
)

# Compile to GDS — produces a real GDSSpec with role blocks, entities, wirings
spec = compile_model(model)
ir = compile_to_system(model)
print(f"{len(ir.blocks)} blocks, {len(ir.wirings)} wirings")

# Verify — domain checks + optional GDS structural checks
report = verify(model, include_gds_checks=True)
print(f"{report.checks_passed}/{report.checks_total} checks passed")

What is this?

gds-stockflow is a domain DSL that compiles stock-flow diagrams to GDS specifications. You declare stocks, flows, auxiliaries, and converters as plain data models — the compiler handles the mapping to GDS role blocks, entities, composition trees, and wirings.

Your declaration                    What the compiler produces
────────────────                    ─────────────────────────
Stock("Population")          →     Mechanism + Entity (state update f + state X)
Flow("Births", target=...)   →     Policy (rate computation g)
Auxiliary("Birth Rate")      →     Policy (decision logic g)
Converter("Fertility")       →     BoundaryAction (exogenous input U)
StockFlowModel(...)          →     GDSSpec + SystemIR (full GDS specification)

Once compiled, all downstream GDS tooling works immediately — canonical projection (h = f ∘ g), semantic checks, SpecQuery dependency analysis, JSON serialization, and gds-viz diagram generation.

Architecture

DSL over GDS

StockFlowModel (user-facing declarations)
       │
       ▼  compile_model()
GDSSpec (entities, blocks, wirings, parameters)
       │
       ▼  compile_to_system()
SystemIR (flat IR for verification + visualization)

No parallel IR stack. The compiler produces a real GDSSpec with real GDS role blocks. This means stock-flow models are first-class GDS citizens — they compose with other GDS models, share the same verification engine, and render with the same visualization tools.

Composition Tree

The compiler builds a tiered composition tree:

(converters |) >> (auxiliaries |) >> (flows |) >> (stock mechanisms |)
    .loop([stock forward_out → auxiliary forward_in])
  • Within each tier: parallel composition (|) — independent elements run side-by-side
  • Across tiers: sequential composition (>>) — converters feed auxiliaries, auxiliaries feed flows, flows feed stock mechanisms
  • Temporal recurrence: .loop() — stock levels at timestep t feed back to auxiliaries at timestep t+1

Elements

Four declaration types, each mapping to a specific GDS role:

Stock

Stock(name="Population", initial=1000.0, non_negative=True)

GDS mapping: Mechanism (state update f) + Entity (state X)

A stock accumulates value over time. Each stock becomes a GDS entity with a level state variable, and a mechanism block that applies incoming flow rates. Stocks emit a Level port for temporal feedback.

Field Type Default Description
name str required Stock name (becomes entity name)
initial float | None None Initial level
units str "" Unit label
non_negative bool True Constrain level ≥ 0

Flow

Flow(name="Births", target="Population")
Flow(name="Deaths", source="Population")
Flow(name="Migration", source="CityA", target="CityB")

GDS mapping: Policy (rate computation g)

A flow transfers value between stocks (or from/to "clouds" — external sources/sinks). Flows with only a target are inflows; flows with only a source are outflows; flows with both transfer between stocks.

Field Type Default Description
name str required Flow name
source str "" Source stock (empty = cloud inflow)
target str "" Target stock (empty = cloud outflow)

Auxiliary

Auxiliary(name="Birth Rate", inputs=["Population", "Fertility"])

GDS mapping: Policy (decision logic g)

An auxiliary computes intermediate values from stocks, converters, or other auxiliaries. Auxiliaries form an acyclic dependency graph — the compiler validates this at construction time.

Field Type Default Description
name str required Auxiliary name
inputs list[str] [] Names of stocks, converters, or auxiliaries this depends on

Converter

Converter(name="Fertility", units="births/person/year")

GDS mapping: BoundaryAction (exogenous input U)

A converter represents an exogenous constant or parameter — a value that enters the system from outside. Converters have no internal inputs.

Field Type Default Description
name str required Converter name
units str "" Unit label

Semantic Type System

Three distinct semantic spaces, all float-backed but structurally separate — this prevents accidentally wiring a rate where a level is expected:

Type Space Used By Constraint
LevelType LevelSpace Stocks ≥ 0 (by default)
UnconstrainedLevelType UnconstrainedLevelSpace Stocks with non_negative=False None
RateType RateSpace Flows None (rates can be negative)
SignalType SignalSpace Auxiliaries, Converters None

Verification

Five domain-specific checks validate the stock-flow model structure before compilation:

ID Name Severity What It Checks
SF-001 Orphan stocks WARNING Every stock has ≥ 1 connected flow
SF-002 Flow-stock validity ERROR Flow source/target reference declared stocks
SF-003 Auxiliary acyclicity ERROR No cycles in auxiliary dependency graph
SF-004 Converter connectivity WARNING Every converter referenced by ≥ 1 auxiliary
SF-005 Flow completeness ERROR Every flow has at least one of source or target
from stockflow import verify

# Domain checks only
report = verify(model)

# Domain checks + GDS structural checks (G-001..G-006)
report = verify(model, include_gds_checks=True)

Examples

Two tutorial examples in gds-examples demonstrate stock-flow modeling using the GDS framework primitives:

Example Domain What It Teaches
SIR Epidemic Epidemiology 3-compartment accumulation, sequential + parallel composition
Lotka-Volterra Population dynamics Temporal loops (.loop()), predator-prey rate equations

Status

v0.1.0 — Alpha. Complete DSL with 5 verification checks and full GDS compilation. 215 tests.

License

Apache-2.0


Built with Claude Code. All code is test-driven and human-reviewed.

Credits & Attribution

Author: Rohan MehtaBlockScience

Theoretical foundation: Dr. Michael Zargham and Dr. Jamsheed ShorishGeneralized Dynamical Systems, Part I: Foundations (2021).

Architectural inspiration: Sean McOwenMSML and bdp-lib.

Contributors:

  • Michael Zargham — Project direction, GDS theory guidance, and technical review (BlockScience).
  • Peter Hacker — Code auditing and review (BlockScience).

Lineage: Part of the cadCAD ecosystem for Complex Adaptive Dynamics.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gds_stockflow-0.1.1.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gds_stockflow-0.1.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file gds_stockflow-0.1.1.tar.gz.

File metadata

  • Download URL: gds_stockflow-0.1.1.tar.gz
  • Upload date:
  • Size: 34.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gds_stockflow-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9af388180cda30314792fe7e11fa0fe64b8960bc0ad9bb39bbd6c1a1dd384e06
MD5 d16ca931b6ea6e009ab0f30c394c601c
BLAKE2b-256 890ea7348f55666acfb48c8c6b3b0fa5f83deb0ec4570a0a98aae9bd4ecb16bb

See more details on using hashes here.

File details

Details for the file gds_stockflow-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: gds_stockflow-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gds_stockflow-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa2af4c367187c6b894a7b256129fb89067d483c1a947ca6bcc0daa66900b6fd
MD5 1cd32d8eb7e6ce3b74bde31c573266ef
BLAKE2b-256 276fe6b7fd3dd273083d81b7a637863fb94ec5313c7de7a382817e35e8748eb8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page