Search code examples
scriptingsimulationmodelicadymolatimetable

How to define a Combitimetable through a script in Dymola?


I am trying to perform several simulations in a sequence using a for loop in a script. From simulation to simulation, the only variable to change is the file path of a Combitimetable.

I propagated the variable fileName in order to assign a new path in each iteration. However, when the model reads the extension, changes the timeScale and the resolution is lower than needed. I tried to propagate timeScale too, but without changes. Is there a function to define the Combitimetable variables? Is my only alternative to merge all tables and split the results manually?

Example of the script on a single run (without the for loop):

filePath="RL_30_200g";
dymolaPath = "modelica://customTILComponents/Combitables/Combitimetable_"+filePath+".txt";
fileName= ModelicaServices.ExternalReferences.loadResource(dymolaPath);
result ="Full_Year_Simulation_"+filePath;
timeScale = 1/3600;
translateModel ("customTILComponents.MA_Santoro.FullModels.OptiHorst_FullModel_New_Year_Simulation_Batch");
simulateModel(startTime=0,stopTime=8860,numberOfIntervals=300,method="Dassl",tolerance=0.000001,resultFile=result);

Solution

  • I am not sure where your problem is and how you change fileName. In your question timeScale is also not used anywhere. Anyway, here is how I would do it: add a parameter to your model for fileName. Since it is a string, the only way to change it is via a modifier which can be included in the model name of the simulateModel command.

    Here is an example: In your model with the time table, propagate the parameter fileName:

    model MyModel
      parameter String fileName="NoName" "File where matrix is stored";
      Modelica.Blocks.Tables.CombiTable1Ds combiTable1Ds(
        tableOnFile=true,
        tableName="x",
        fileName=fileName) annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
      Modelica.Blocks.Sources.Ramp ramp(duration=1) annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));
    equation 
      connect(ramp.y, combiTable1Ds.u) annotation (Line(points={{-39,0},{-12,0}}, color={0,0,127}));
      annotation (uses(Modelica(version="4.0.0")));
    end MyModel;
    

    Then change the value of fileName in every loop. Here we assume that there are three .mat files available in the workspace, named First.mat, Second.mat and Third.mat.

    function batchSim
      input String fileNames[:] = {"First", "Second", "Third"};
    algorithm 
      for f in fileNames loop
        simulateModel("MyModel(fileName=\""+f+".mat\")", stopTime=1, resultFile="Full_Year_Simulation_"+f);
      end for;
      annotation(__Dymola_interactive=true);
    end batchSim;
    

    This works quite well, but the downside is that the model will be recompiled in every iteration of the for loop, as the modifier has changed. If this is a big problem, define all file paths in a string vector in the model and add an integer parameter for the index. Then use the command simulateExtendedModel and change only the index via the parameters initialNames and initialValues.