How to subtract or add time series data of a CombiTimeTable in Modelica?

Viewed 145

I have a text file that is used in a CombiTimeTable. The text file looks like as follows:

#1
double tab1(5,2)   # comment line
  0   0
  1   1
  2   4
  3   9
  4  16

The first column is time and the second one is my data. My goal is to add each datum to the previous one, starting from the second row.

model example
  Modelica.Blocks.Sources.CombiTimeTable Tsink(fileName = "C:Tin.txt", tableName = "tab1", tableOnFile = true, timeScale = 60) annotation(
    Placement(visible = true, transformation(origin = {-70, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation

end example;

Tsink.y[1] is the column 2 of the table but I do not know how to access it and how to implement an operation on it. Thanks for your help.

1 Answers

You can't use the blocks of the ModelicaStandardTables here, which are only meant for interpolation and hence do not expose the sample points to the Modelica model. However, you can use the Modelica library ExternData to easily read the array from a CSV file and do the required operations on the read data. For example,

model Example "Example model to read array and operate on it"
  parameter ExternData.CSVFile dataSource(
    fileName="C:/Tin.csv") "Data source"
    annotation(Placement(transformation(extent={{-60,60},{-40,80}})));
  parameter Integer n = 5 "Number of rows (must be known)";
  parameter Real a[n,2] = dataSource.getRealArray2D(n, 2) "Array from CSV file";
  parameter Real y[n - 1] = {a[i,2] + a[i + 1,2] for i in 1:n - 1} "Vector";
  annotation(uses(ExternData(version="2.6.1")));
end Example;

where Tin.csv is a CSV file with comma as delimiter

0,0
1,1
2,4
3,9
4,16
Related