How to simulate a model multiple times with different parameter values

May 31st, 2011 by Hannah Hammond

Here are 2 examples of how you can simulate a model multiple times using different parameters values in Dymola, also referred to as batch running, and often used to run sensitivity sweeps.

For the purpose of these examples a simple test model, Test1.mo, will be used.  The Modelica text for Test1 is shown below; parameter a is multiplied by the sum of a large array of 1’s to derive x, and y is the integral of x.

model Test1 "Test model" 
  parameter Real a=1;
  Real x;
  Real y;
protected
  parameter Real table[:, :]=ones(300, 100);
equation
   x = a*sum(table);
  x = der(y);
end Test1;

The objective is to simulate Test1.mo 10 times with different values of parameter a, from 1 to 10 in steps of 1.

Method 1: Using the simulateExtendedModel function

Create the Dymola script below in the same directory location as the model to be run.

openModel("C:/Dymola/Testing/Test1.mo");
for i in 1:10 loop
         simulateExtendedModel(
         "Test1",
         initialNames={"a"},
         initialValues={i},
         finalNames={"x", "y"},
         resultFile="test"+String(i));
end for;

The arguments used in the simulateExtendedModel function are (see the Dymola User Manual Volume 1 section 5.5.16):

When executed this script opens the Test1 model, incrementally simulates the model 10 times, using values of parameter a from 1-10.  10 corresponding results files are produced.  This method of simulating the same model multiple times with different parameter values will re-translate the model for each iteration of the loop.  For a simple model, like this example, re-translation is insignificant to the total simulation time. However for a complex model re-translation could produce a significant time penalty.

Method 2: Using the translateModel and simulate functions

A method of simulating a model numerous times without re-translating the model, would be to use the translateModel and simulate functions.  Create the following Dymola script.

openModel("C:/Dymola/Testing/Test1.mo")
translateModel ("Test1");
for i in 1:10 loop
         a=i;
         simulate();
         system("copy dsres.mat results"+String(i) +".mat");
end for;

Note the system function is used to run the Windows command line function copy, to rename copies of the results file, dres.mat,  for each iteration loop.  By copying and renaming the file results1.mat, results2.mat, etc.,  overwriting of the results is prevented.

Archives

2017

2016

2015

2013

2012

2011

2010