Skip to content

Translators

Translate model files of other tools into the source of system modules.

A translator reads a model file, such as a stock and flow model of Vensim, and writes the Python source of a system module that builds the same model on elements and interactions.

RunSettingsSpec(start, until, dt, save_every) dataclass

Bases: BaseSpec

A spec of the run settings of a model file: its start, length, and steps.

It holds the time a run of the file starts at, how long it runs, its time step, and how often it saves values, such as a run from the year 1900 for 100 years in steps of a quarter. A translator reads it from the control section of the file and writes it as the constants of the system module. It sets the run of a population model, a supply chain, and a climate model alike.

Read more in the user guide, under Systems.

Parameters:

Name Type Description Default
start float

The time of the file at the start of a run.

required
until float

The length of a run, the end time of the file minus start.

required
dt float

The time step.

required
save_every float

The time between saved values.

required

Raises:

Type Description
ValidationError

The length of the run, the time step, or the saving interval is not positive, raised when its owner checks it.

See Also

SystemDynamicsTranslator: Reads the settings from a model file.

Examples:

>>> from skcomplex.core.engine.translators import RunSettingsSpec
>>> RunSettingsSpec(start=1900.0, until=100.0, dt=0.25, save_every=1.0).until
100.0

SystemDynamicsTranslator(path, *, name=None)

Bases: BaseConfig[None, None]

A translator of a Vensim or XMILE file into the source of a system module.

It reads a stock and flow model saved by Vensim or Stella and writes the Python source of a system module that builds the same model, with the constants START, UNTIL, DT, and SAVE_EVERY and one system class named after the model, such as GrowthSystem for growth.mdl. The system class takes the constants of the file as keyword arguments, with the values of the file as defaults. A model whose stocks change only through flows becomes a subclass of StockFlowSystem, and any other model becomes an element of the stocks and constants with one interaction per flow and per remaining rate. A model whose stocks all run over one subscript range becomes an element with one member per subscript. It brings population models, supply chains, and epidemic models of other tools into the library.

Read more in the user guide, under Systems.

Parameters:

Name Type Description Default
path str | Path

The path of a .mdl, .xmile, .xml, or .stmx file.

required
name str | None

The name of the element and the base of the name of its class, or None for the name of the file.

None

Attributes:

Name Type Description
system_

The model PySD reads from the file.

source_

The Python source of the system module.

settings_

The run settings of the control section of the file.

Raises:

Type Description
ValidationError

The path names no file of a known format, or the name is not an identifier, raised by prepare. The file uses features without a counterpart, or names that clash, all of them listed, raised by run.

See Also

RunSettingsSpec: The run settings of the file. StockFlowSystem: The base of the translated stock and flow models.

Examples:

>>> import pathlib, tempfile
>>> from skcomplex.core.engine.translators import SystemDynamicsTranslator
>>> path = pathlib.Path(tempfile.mkdtemp()) / 'growth.mdl'
>>> _ = path.write_text('Stock=INTEG(2*Stock,1)~~|INITIAL TIME=0~~|FINAL TIME=4~~|TIME STEP=1~~|SAVEPER=1~~|')
>>> 'class GrowthSystem' in SystemDynamicsTranslator(path).prepare().run().source_
True
Source code in src/skcomplex/core/engine/translators/_system_dynamics.py
90
91
92
def __init__(self, path: str | Path, *, name: str | None = None) -> None:
    self.path = path
    self.name = name