modelicalookup-tablesdymola

Making a Lookup up tables in Modelica Dymola through CombiTable2Ds


I am at an absolute beginner level in usage of Modelica language. I am trying to make a look up table and would like to do a linear interpolation between the values. So, I have this data of temperature and pressure which would give me enthalpy. In the table below, the first column is temperature data, First row is pressure data, for these grid points, the rest is enthalpy which is my desired output.
Enthalpy as a function of temperature and pressure

I tried to find an example on a lookup table like this but was not able to find one. I would appreciate your help.
Screenshot of Dymola showing the below code

model CalculateEnthalpy1
  
  type Pressure =Real (unit="Pa");
  type Temperature =Real (unit="K");
  type Enthalpy =Real (unit= "J/kg");
  
  Modelica.Blocks.Tables.CombiTable2Ds combiTable2Ds(
    tableOnFile=true,
    fileName="C:/Users/shyousa/Downloads/TPH.mat",
    smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments)
    annotation (Placement(transformation(extent={{-52,22},{-32,42}})));
  annotation (uses(Modelica(version="4.0.0")));

  parameter Temperature T= 297; // input value for temperature 
  parameter Pressure P= 152500; // input value for pressure 
  Enthalpy h; // output value of enthalpy for the above mentioned inputs
equation 
  h = smoothness(table, T, P);
  annotation (uses(Modelica(version="4.0.0")));
end CalculateEnthalpy1;

for example, for P= 490000 and T= 344 , enthalpy= 494629.5

and if the inputs are within this range, but not exactly, in the table, it should do interpolation while extrapolation is not to be allowed.
Table with manual notes


Solution

  • So there are a couple of things to consider with the code you posted:

    1. No need to define those basic types yourself. Usually you find suitable ones in Modelica.Units.SI. Below you'll find how to import and use them.
    2. In the table it is possible to set the extrapolation to NoExtrapolation. This will abort the simulation if extrapolation would be necessary. You can e.g. change to HoldLastPoint to enable simulation outside the defined table (with the associated risks).
    3. In the above state, neither inputs nor the output of the table are connected. You can connect them by either adding graphical components (Constant-blocks) and the respective connects as done for the inputs (P, T) in the code below, or set an equation as done for the output (h).
    4. The values are added directly to the model (no .mat file used). This was simpler for me, but both ways are fine. Also: I used Excel to generate the values from the screenshot. It wasn't exactly doing great with this task, so potentially there are errors in the table...
    model CalculateEnthalpy
      import Modelica.Units.SI;
    
      // Parameters
      parameter SI.Temperature T= 297; // input value for temperature 
      parameter SI.Pressure P= 152500; // input value for pressure   
    
      // Variables
      SI.Enthalpy h; // output value of enthalpy for the above mentioned inputs
    
      // Components
      Modelica.Blocks.Sources.Constant temperature(k=T) annotation (Placement(transformation(extent={{-60,20},{-40,40}})));
      Modelica.Blocks.Sources.Constant pressure(k=P) annotation (Placement(transformation(extent={{-60,-40},{-40,-20}})));
      Modelica.Blocks.Tables.CombiTable2Ds combiTable2Ds(
        tableOnFile=false,
        table=[0,40000,152500,265000,377500,490000; 250,423315.2,419889.7,416118.2,165969.3,165995.8; 273.5,441261.1,438806.4,436236.1,433533.6,430676.7; 297,459931.2,458052.7,456121.8,454134.3,452084.8; 320.5,479386.9,477888.3,476356.9,474806.3,473219.7; 344,499557,498424.7,497176.4,495911.5,494629.5],
        smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
        extrapolation=Modelica.Blocks.Types.Extrapolation.NoExtrapolation)
        annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
    
    equation 
      h = combiTable2Ds.y;
    
      connect(temperature.y, combiTable2Ds.u1) annotation (Line(points={{-39,30},{-20,30},{-20,6},{-12,6}}, color={0,0,127}));
      connect(pressure.y, combiTable2Ds.u2) annotation (Line(points={{-39,-30},{-20,-30},{-20,-6},{-12,-6}}, color={0,0,127}));
    
      annotation(uses(Modelica(version="4.0.0")));
    end CalculateEnthalpy;
    

    As an extension to the above model, you can replace the constants with time-varying values e.g. using a sensor.