modelicaopenmodelicadymola

how to write a modelica/dymola function with a 2D table inside it?


Please pardon me for asking a very basic question. I have put effort to resolve the issue myself and then I came here to seek help from community. Thank you.

So I want to make a function that would take two real inputs. It would have a table as well inside it and then look up for an output from the table data inside the function. WIth some help form the stackoverflow, I can do this by using model keyword which would require the inputs to be given at the same time. But Since, I would be using this many time in my programing, I would go with function (I would prefer function due to my python programing experience) and call it as many times as I want. I am new user of Modelica, please forgive me for this basic question.

function InputTable
  input Real a;// input 1 to the function say 7 from the table data
  input Real b;// input 2 to the function say 6 from the table data 
  output Real c;// output of the function say 
protected 
   Modelica.Blocks.Tables.CombiTable2Ds table(table=[0,7,8; 5,35,40; 6,42,48])
    annotation (Placement(transformation(extent={{-44,4},{-24,24}})));   // this would be the table containing some data
algorithm 
   table.u1:=a;
   table.u2:=b;
   c:=table.y;
  annotation (uses(Modelica(version="4.0.0")));
end InputTable;

I tried to use the above code to write my functionInputTable and was calling it using model test as below.

model test
  Real a;
  Real b;
  Real c;
equation 
  a=5;
  b=7;
  c=InputTable(a,b);
end test;

But then it give these errors and I am not sure how to resolve these. I would be grateful if someone could help me on fixing this issue. Errors

Edit: Feb 5, 2024

As @tbeu mentioned in a comment below I can use external files like .mat or excel CSV, so I tried to edit the code like the following but it gave me errors. Overall, this function provided by @tbeu inputTable works fine but since I have a huge data it would be impossible for me to provide it manually in the code. so, I would like to provide it as a Matlab/CSV file. (Seems like there is no option to attach a Matlab file here, so I would only paste the data, but I would request get solution that work with an external file) 0 40000 152500 265000 250 423315 419889 416118 273.5 441261 438806 436236

the errors are enter image description here while the changes in the code are highlighted as here in enter image description here and the Matlab table that I would like to call externally in the function is


Solution

  • As already answered by Dag, you cannot use the block layer of the 2D table when using inside the function. When switching to the function layer instead, InputTable works as expected.

    pure function InputTable
      import Modelica.Blocks.Types.{ExternalCombiTable2D, Extrapolation, Smoothness};
      import Modelica.Blocks.Tables.Internal.{getTable2DValue, getTable2DValueNoDer, getTable2DValueNoDer2};
      input Real a;// input 1 to the function say 7 from the table data
      input Real b;// input 2 to the function say 6 from the table data 
      output Real c;// output of the function say 
    protected 
      final Smoothness smoothness = Smoothness.LinearSegments;
      final ExternalCombiTable2D tableID = ExternalCombiTable2D(
        tableName="NoName",
        fileName="NoName",
        table=[0,7,8; 5,35,40; 6,42,48],
        smoothness=smoothness,
        extrapolation=Extrapolation.LastTwoPoints,
        verboseRead=false) "External table object";
    algorithm 
       c := if smoothness == Smoothness.ConstantSegments then getTable2DValueNoDer(tableID, a, b)
        elseif smoothness == Smoothness.LinearSegments   then getTable2DValueNoDer2(tableID, a, b)
        else                                                  getTable2DValue(tableID, a, b);
    end InputTable;
    

    Note, that the external object is constructed (and destructed) with every function call which might not be desirable when it comes to performance. The better approach is to keep the external object alive during simulation, i.e. by using it in a block/model.