Using the XMILE Compiler Standalone

How to compile an XMILE model into SD DSL code and run it without a scenario file.
Keywords

system dynamics, systemdynamics, xmile, bptk, bptk-py, python, business simulation, stella

Using the XMILE Compiler Standalone

A scenario file with a source compiles its XMILE model for you, as XMILE Step by Step shows. The compiler can also be called on its own - to look at the code it writes, to keep the compiled model in your project, or to run a model many times without scenarios around it.

The compiler writes a file, so the code on this page is shown rather than run. The model is the customer acquisition model of the XMILE quickstart.

Compiling a model

compile_xmile takes the XMILE file, the file to write and a target:

from BPTK_Py.sdcompiler.compile import compile_xmile

compile_xmile("simulation_models/customer_acquisition_xmile.stmx",
              "simulation_models/customer_acquisition_xmile.py",
              "py-sddsl")

There are two targets:

Target Writes
py-sddsl A Python file with the model as SD DSL code: a subclass of Model
json-rust-engine The JSON the Rust engine loads - the same model, serialized

Whatever the SD DSL cannot express is refused with a message naming the element; the XMILE in the SD DSL chapter lists what that is.

What the compiler writes

The py-sddsl file is SD DSL code as you would write it by hand: one element per stock, flow, converter and constant, and their equations. Its beginning:

from BPTK_Py import Model
from BPTK_Py import sd_functions as sd


class CustomerAcquisitionXmile(Model):

    def __init__(self):
        super().__init__(starttime=0.0, stoptime=60.0, dt=1.0, name='customer_acquisition_xmile')

        customers = self.stock('customers')
        potentialCustomers = self.stock('potentialCustomers')
        customerAcquisition = self.flow('customerAcquisition')
        ...
        customerAcquisition.equation = sd.max(0.0, (acquisitionThroughAdvertising + acquisitionThroughWordOfMouth))
        ...

The names are those of the XMILE model in camel case.

Running it

The class is an ordinary Model, so simulate runs it and returns a DataFrame - on the Python engine by default, on the Rust engine with backend="rust":

from simulation_models.customer_acquisition_xmile import CustomerAcquisitionXmile

model = CustomerAcquisitionXmile()
model.simulate(["customers", "customerAcquisition"], backend="rust")
t customers customerAcquisition
0 0.0 1000.0
10 13843.5 1834.2
20 33620.5 1917.8
30 49222.2 1063.8
60 59686.5 36.4

Both engines give the same numbers. Everything else the SD DSL offers works on the compiled model as well - changing a constant, plotting an element, registering it with bptk - as the SD DSL chapter describes.