Shaga
Shaga

Reputation: 170

Maximum number of event iterations in Modelica

Is there a way to change the value of "Maximum number of event iterations" ,eviter, that is set to 20 in dymola?

Upvotes: 2

Views: 375

Answers (2)

Markus A.
Markus A.

Reputation: 7525

Since Dymola 2020x there is an advanced variable that should do exactly that.

From the 2020x changelog:

Advanced.Simulation.MaxEventIterations The maximum number of evnet iterations, eviter. Use with care, since too small value will lead to simulation failure. The default value is Advanced.Simulation.MaxEventIterations=20

Additionally the precision for the iteration can be changed:

Advanced.Simulation.EventEpsilon The time resolution for events, eveps. The default value is Advanced.Simulation.EventEpsilon=1e-10

Upvotes: 1

marco
marco

Reputation: 6655

It seems like dymosim.ini is not used for regular simulations (Note the version in line 4: dymosim.ini states 1.1, whereas the dsin.txt has 1.4 written in Dymola 2019). I also could not find a function or variable in Dymola to change eviter, so the only solution I can think of is to edit dsin.txt before simulation.

The code below shows how a function could look like which updates dsin.txt and then performs a simulation. Note that the function should be extended for error handling.

function MySim
  import Modelica.Utilities.Strings;
  import Modelica.Utilities.Streams;
  import Modelica.Utilities.Files.removeFile;
  import SimAPI = DymolaCommands.SimulatorAPI;

  input String m = "Modelica.Blocks.Examples.PID_Controller"
    annotation (Dialog(__Dymola_translatedModel(translate=false)));
  input Integer iterations = 40;
protected 
  String[:] dsin;
algorithm 
  // initialize
  removeFile("dsin_custom.txt");
  removeFile("dsin_export.txt");
  SimAPI.translateModel(m);
  SimAPI.exportInitialDsin("dsin_export.txt");  // exports simulation setup of active model

  // write custom dsin file
  dsin :=Streams.readFile("dsin_export.txt");
  for i in 1:size(dsin, 1) loop
    if Strings.find(dsin[i], " # eviter ") > 0 then
      dsin[i] :=Strings.replace(dsin[i], "20", String(iterations));
      break;
    end if;
  end for;
  for i in 1:size(dsin, 1) loop
    Streams.print(dsin[i], "dsin_custom.txt");
  end for;

  // simulate and open file
  // using simulateModel would generate a new dsin file, so dymosim.exe must be called
  SimAPI.system("dymosim.exe -s dsin_custom.txt "+m+".mat");
  // workaround: create empty plot to open result, as there is no load-result command
  DymolaCommands.Plot.createPlot(y={""}, grid=true, filename=m+".mat");
end MySim;

The function translates the selected model, exports the simulation setup, edits it as needed, performs the simulation and opens the result file - so it is basically a customized simulateModel-command.

With the features described in 7.2 Extendable user interface – menus, toolbars and favorites in the Dymola User Manual Volume 2 a shortcut to the function can be created in the Dymola GUI, which allows to perform the simulation for the selected model. This would make the function as convenient to use as the regular simulate button.

Upvotes: 1

Related Questions