Vensim performs compiled simulations by creating a Dynamic Link Library (sim.dll) that contains the computer code necessary to execute the model equations. Vensim maintains control of the simulation, calling this DLL to compute model values. The DLL, in turn, calls Vensim to evaluate the different functions used.
When you ask to have a compiled simulation performed, Vensim orders the model's equations and writes them out as C. The output is several files rather than one, because a large model generates more C than a compiler will accept in a single file: mdl.h holds the declarations every file needs, mdl_000.c holds the eight entry points listed below, and mdl_001.c onwards hold the equations themselves. mdl_files.txt names them in order, for the compiler to read. Even the smallest model gets three files. The eight functions in mdl_000.c are:
| • | mdl_desc returns a description of the model. |
| • | comp_rate computes the rates of changes for the levels. |
| • | comp_delay computes the inputs to delays. |
| • | init_time initializes to begin a simulation. |
| • | init_tstep initializes TIME_STEP before beginning a simulation. |
| • | comp_init computes the initial values of all Levels. Auxiliaries needed to initialize the Levels are also computed. |
| • | comp_reinit computes the initial values of all equations using the REINITIAL function. |
| • | comp_aux computes the values of all Auxiliaries, given the values of all the Levels. |
Vensim calls mdl_desc to verify the source of the model, init_time to find the starting Time, init_tstep to find the initial value of TIME_STEP, comp_init to initialize levels and then performs the simulation by calling comp_aux and comp_rate and performing integration steps. comp_reinit is only called during resume.
The separate init_tstep is used because TIME_STEP can be input as an exogenous driver, and this requires a value for time. Since TIME_STEP is usually a constant, init_tstep is most commonly an empty function. Depending on what you have in the model some of the other functions may also be empty.
Because models can be quite large, Vensim breaks the equations up into smaller functions, which makes a large model much faster to compile. In mdl_000.c you will see that comp_aux simply calls one or more mdl_funcN() functions, whose bodies are in mdl_001.c and the files after it. To control the size of those functions use the "Variable count for function breaks in mdl.c" setting on the Advanced tab of the Global Option Settings dialog (Tools>Options). On Windows the generated files also compile in parallel, one per processor core.
Having written those files, Vensim invokes the batch file in the compiled simulation directory — MDLDP64.BAT on 64-bit Windows, mdldp.macx on the Mac (the batch file is discussed further below). On Windows it runs in a console window that should appear and disappear by itself. The compiler and linker messages are written to compile_out.txt beside the model; when the build fails the batch file also prints that file to the window and waits for a keystroke. See Compile Errors below for what to do next.