Please enable JavaScript to view this site.

Vensim Help

To define a macro, use the format:

:MACRO: macroname(inarg1,inarg2,inarg3:outarg1,outarg2)

macroname=inarg1+inarg2~~|

...

:END OF MACRO:

Where macroname is any valid unquoted Vensim name (starting with a letter and containing letters, numbers, spaces, underbars _ and dollar signs  $).  The arguments can have any valid unquoted name and there can be any number of them.  The arguments cannot be subscripted, but you can use subscripted values when calling the macro.

The output arguments are variables that are defined inside of the macro, in addition to the output of the macro itself.  These are optional and must be separated from input arguments by a colon :.  Their use is discouraged.  When no outputs are specified, omit the colon :.  

The macroname and output arguments each must have a single equation inside of the macro description.  Variables can also be defined internal to the macro description.  In this way macros define a self-contained world that can be accessed only through the macro call itself. This means that if your model has a variable name population, and you use population inside of a macro, no connection will be made between the two names.  The variable names inside of a macro relate to nothing outside the macro or in other macros.  You can use, for example, L1 in as many macros as you like.  

The one exception to the local nature of macro variables is that you may specify a model variable by following its name with a dollar sign $.  For example you can use TIME STEP$ inside of a macro to refer to the model variable TIME STEP.  Only unsubscripted variables may be referred to in this manner.

The Units inside of a macro definition can include either the standard sorts of Units in use in your model ($, People, etc.) or the names of input variables.  If the units are the name of an input variable, the units from the input will be substituted wherever the macro is invoked.  You can also use a name such as Time$ to specify the units for Time.

Examples

 

 

:MACRO: VSMOOTH(input,SMOOTH TIME)

 

Vsmooth = INTEG((input - Vsmooth)/SMOOTH TIME, input)
~        input
~        The first order smoothed value of a variable.
|

 

:END OF MACRO:

 

 

:MACRO: MYNPVE(str,dr,in,f)

 

MYNPVE = (NCUM+str*TIME STEP$*df)*f
~        str/dr
~
|

 

NCUM = INTEG(str*df,in)
~        str/dr
~
|

 

df = INTEG(-df*dr/(1+dr*TIME STEP$),1/(1+dr*TIME STEP$))
~        Dmnl
~
|

 

:END OF MACRO:

 

 

 

 

 

 

 

 

NOTE You must use the Text Editor to define macros.  Macros cannot be defined using the Equation Editor, although they can be used within the Equation Editor.

When you start a new model you can specify an input file using the Advanced tab in the Global Options dialog.  You can fill this file with macros that you routinely use so that you do not have to paste them into a new model every time you start one.