首页 > 解决方案 > 如何在 Modelica 中减去或添加 CombiTimeTable 的时间序列数据?

问题描述

我有一个用于CombiTimeTable. 文本文件如下所示:

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

第一列是时间,第二列是我的数据。我的目标是将每个数据添加到前一个数据中,从第二行开始。

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]是表的第 2 列,但我不知道如何访问它以及如何对其执行操作。谢谢你的帮助。

标签: text-filesmodelicalookup-tables

解决方案


您不能在此处使用 ModelicaStandardTables 的块,这些块仅用于插值,因此不会将样本点暴露给 Modelica 模型。但是,您可以使用 Modelica 库ExternData轻松地从 CSV 文件中读取数组并对读取的数据执行所需的操作。例如,

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;

其中 Tin.csv 是以逗号作为分隔符的 CSV 文件

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

推荐阅读