As an alternative to running a model interactively, models may be run using a script written in the Python or CMD language.  A script is file containing a set of commands used to set model parameters, run the model, collect desired outputs and trajectories, perform calculations on the inputs or output data, construct plots, etc.


To create a new script, select "File -> New -> CMD Script" or "File -> New -> Python Script" from the main menu or toolbar.  For the purposes of this example, we will create a new CMD script.  Once the empty script file opens, script code can be entered in the corresponding source code editor.  Generally, the first line of each script file will be a "load" command.  The load command instructs Magnolia to look for an open model with the designated name and connect to that model instance.  If a model with the designated name is not currently open, an error message will be issued when attempting to run the script file.  Here's an example of a simple CMD script file:



load 'styrene.csl'

set tinf=1.0
set ivdose=0.0

prepare ca, cx, cs, cr, cf, aucl, tmass
output t ca cx
start

plot @logy @ymin=1.0 @ymax=10000.0, ...
     @title='Styrene', @xlabel='Time (hours)', @ylabel='Concentration (mg/L)', ...
     ca cs cr cf


Notice the "load" command on the first line.  If the model with the name "styrene.csl" is not open in Magnolia and successfully built, this script file will fail when attempting to be run.  Detailed information on the CMD language is presented in the "CMD Language Section" section, but a brief summary of the contents of this file follows:


  1. The "load" command looks for the model with the designated name in memory, and connects the script to that model so it can coordinate setting parameter values, running the simulation, collecting trajectories, etc.
  2. The "set" commands assign values to specific model parameters (defined in the CSL model with the CONSTANT keyword).  In this case the model parameter "tinf" is initialized with the value 1.0.
  3. The "prepare" command tells Magnolia what model outputs should be collected (logged) during the course of the simulation run.  Preparing a variable prior to a simulation run is necessary if the time history of the variable is to be plotted at the conclusion of the run, for example.
  4. The "output" command is used to specify a list of model outputs whose values should be written to the output window at each communication interval.  This is a useful way of monitoring the progress of long simulation runs, and for model debugging.
  5. The "start" command causes the simulation to be executed.  Note that subsequent commands in the script will not be processed until the simulation concludes.
  6. The "plot" command is used to create a 2-D line chart of desired model outputs.  The tokens on the command preceded with the '@' symbol are various flags for the plot command (described in detail here).  The command is broken over three source code lines by using the "..." line continuation token.  The last four tokens (ca, cs, cr, cf) represent the four model outputs to be plotted.  These will be plotted against the model independent variable (typically "t").  Note that these outputs were included in the previous "prepare" list.  Had they not been, and error message would have been generated when running the script.