ema workbench

Other Sub Sites

Vensim

Created on 25 mei 2011

Code author: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl>

convenience functions and classes to be used in combination with Vensim. This module contains frequently used functions with error checking. For more fine grained control, the vensimDLLwrapper can also be used directly.

vensim.be_quiet()

this allows you to turn off the work in progress dialog that Vensim displays during simulation and other activities, and also prevent the appearance of yes or no dialogs.

defaults to 2, suppressing all windows, for more fine grained control, use vensimDLLwrapper directly .

vensim.load_model(file)

load the model

Parameters:file – the location of the .vpm file to be loaded.
Exception :raises a VensimError if the model cannot be loaded.
vensim.read_cin_file(file)

read a .cin file

Parameters:file – location of the .cin file.
Exception :raises a VensimWarning if the cin file cannot be read.
vensim.set_value(variable, value)

set the value of a variable to value

current implementation only works for lookups and normal values. In case of a list, a lookup is assumed, else a normal value is assumed. See the DSS reference supplement, p. 58 for details.

Parameters:
  • variable – name of the variable to set.
  • value – the value for the variable. note: the value can be either a list, or an float/integer. If it is a list, it is assumed the variable is a lookup.
vensim.run_simulation(file)

Convenient function to run a model and store the results of the run in the specified .vdf file. The specified output file will be overwritten by default

Parameters:file – the location of the outputfile
Exception :raises a VensimError if running the model failed in some way.
vensim.get_data(filename, varname, step=1)

Retrieves data from simulation runs or imported data sets.

Parameters:
  • filename – the name of the .vdf file that contains the data
  • varname – the name of the variable to retrieve data on
  • step – steps used in slicing. Defaults to 1, meaning the full recored time series is returned.
Returns:

an array with the values for varname over the simulation

class vensim.VensimModelStructureInterface(workingDirectory, name)

This is a convenience extension of ModelStructureInterface that can be used as a base class for performing EMA on Vensim models. This class will handle starting Vensim, loading a model, setting parameters on the model, running the model, and retrieving the results. To this end it implements:

  • __init__
  • model_init
  • run_model

For the simplest case, it is sufficient to only specify _init__ in more detail. That is, specify the uncertainties and the outcomes. For more elaborate cases, for example when using different policies, it might be necessary to overwrite or extent model_init, while for dealing with lookups etc. it might be necessary to also extent run_model. The examples folder contains examples of each of these extensions.

Note

This class relies on the Vensim DLL, thus a complete installation of Vensim DSS is needed.

cinFile = None

attribute that can be set when one wants to load a cin file

modelFile = None

The path to the vensim model to be loaded.

note: The model file should be a .vpm file

model_init(policy, kwargs)

Init of the model, The provided implementation here assumes that self.modelFile is set correctly. In case of using different vensim models for different policies, it is recomended to extent this method, extract the model file from the policy dict, set self.modelFile to this file and then call this implementation through calling super.

Parameters:
  • policy – a dict specifying the policy. In this implementation, this argument is ignored.
  • kwargs – additional keyword arguments. In this implementation this argument is ignored.
reset_model()

Method for reseting the model to its initial state before runModel was called

resultFile = '\\Current.vdf'

default name of the results file (default: ‘Current.vdf’)

run_model(case)

Method for running an instantiated model structure. the provided implementation assumes that the keys in the case match the variable names in the Vensim model.

If lookups are to be set specify their transformation from uncertainties to lookup values in the extension of this method, then call this one using super with the updated case dict.

if you want to use cinFiles, set the cinFile, or cinFiles in the extension of this method to self.cinFile.

Parameters:case – the case to run

Note

setting parameters should always be done via run_model. The model is reset to its initial values automatically after each run.

step = 1

attribute used for getting a slice of the results array instead of the full array. This can cut down the amount of data saved. Alternatively, one can specify in Vensim the time steps for saving results