Search code examples
modelicaopenmodelicadymolatimetable

Modelica (Dymola) : get a particular value of a timetable?


I have a model using a timeTable which represents a variable evolution. I would like to initialize a subcomponent's parameter with the first value of the table (time = 0 second). The table's values are read from a .txt file. The idea would be to have a command as follow :

parameter Real InitialValue = timeTable.y[2](for timeTable.y[1] = 0)

Is there a command to do so ?


Solution

  • The solution depends on the block you are using and how the data is defined. Note that there is no easy solution for .txt files, so I recommend using .mat files instead.

    1. Data from model

    If you don't read from a file it is quite easy.

    The data is stored as matrix in the parameter table and we can use array indexing to access it:

    model Demo
      Modelica.Blocks.Sources.TimeTable timeTable(table=[0,1; 2,3]);
      parameter Real initialValue = timeTable.table[1, 2];
    end Demo;
    

    This works for both, the Modelica.Blocks.Sources.TimeTable and the CombiTimeTable found in the same package.

    2. Data from .mat file

    The MSL provides functions to access .mat files. You have to get the table size before you can read the data. See the code below how this can be done.

    model Demo2
    
      import Modelica.Utilities.Streams.{readMatrixSize, readRealMatrix};
    
      parameter String fileName = "C:/tmp/table.mat";
      parameter String tableName = "tab1";
      parameter Real initialValue = (readRealMatrix(fileName=fileName, matrixName=tableName, nrow=matrixSize[1], ncol=matrixSize[2]))[1, 2];
    
      Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(
        tableOnFile=true,
        tableName=tableName,
        fileName=fileName)
        annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
    
    protected 
      final parameter Integer matrixSize[2] = readMatrixSize(fileName, tableName);
    
    end Demo2;
    

    Note that we don't store the whole table in a variable. Instead, we read it and access the element of intereset with [1, 2]. This requires putting brackets around the function call.