2017-06-21 48 views
1

我试图阅读多个文件(csv,2列)与所有者名称(字符串)和插值方法平滑(字符串)。通过使用记录我在Dymola的得到一个不错的GUI: GUIModelica灵活的阵列大小记录 - 无法扩展

解释我的问题,这里是一个简化的模型:

model Test_Strings 
    parameter String x[:]; 
    parameter Integer k = size(x,1); 
    Integer i; 

initial algorithm 
    i := 0; 
    Modelica.Utilities.Streams.print(String(i)); 
    Modelica.Utilities.Streams.print(String(k)); 

equation 

algorithm 
    when sample(1,1) then 
     i :=pre(i) + 1; 
     Modelica.Utilities.Streams.print(String(i)); 

     for j in 1:k loop 
     Modelica.Utilities.Streams.print(x[j]); 
     end for; 
    end when; 
end Test_Strings; 

这样做的GUI如下所示: enter image description here

运行它的代码:

model Test_Strings_System 
    Test_Strings test_Strings(x={"a","b","c","d"}); 
end Test_Strings_System; 

这将在conso中给出以下结果乐:现在 enter image description here

,如果我尝试使用记录:

record MyRecord2 
    parameter String name = "NoName"; 
end MyRecord2; 

,并调整模型(仅第一个参数线MyRecord2 X [:]和for循环X [内J]。名称改变):

model Test_Strings2 
    parameter MyRecord2 x[:]; 
    parameter Integer k = size(x,1); 
    Integer i; 

initial algorithm 
    i := 0; 
    Modelica.Utilities.Streams.print(String(i)); 
    Modelica.Utilities.Streams.print(String(k)); 

equation 

algorithm 
    when sample(1,1) then 
    i :=pre(i) + 1; 
    Modelica.Utilities.Streams.print(String(i)); 

    for j in 1:k loop // k must be fixed number if equation 
     Modelica.Utilities.Streams.print(x[j].name); // j must be fixed number if algorithm 
    end for; 
    end when; 
end Test_Strings2; 

然后我得到一个编译错误:内部错误:未能扩大字符串。 enter image description here

如果我解决中k个或j for循环给定的数字(假设3),那么它的工作原理,但根据如果它是一个算法或方程段内(见中的代码注释)。

我有类似的问题灵活的数组大小,仍然不明白如何解决它。 我必须使用功能吗? 如何使用根据外部数据定义的灵活阵列大小,在模拟之前选择为参数(例如表格长度)? 或者在这种情况下,在其他地方的问题?

谢谢。

回答

1

可以改变模型有记录为可见的参数数组,但在内部使用的字符串数组(用Dymola的测试,2017年及以后):

model Test_Strings2 
    parameter MyRecord2 x[:]; 
    parameter Integer k = size(x,1); 
    Integer i; 
protected 
    parameter String s[:]=x.name; // Hack 
initial algorithm 
    i := 0; 
    Modelica.Utilities.Streams.print(String(i)); 
    Modelica.Utilities.Streams.print(String(k)); 

equation 

algorithm 
    when sample(1,1) then 
    i :=pre(i) + 1; 
    Modelica.Utilities.Streams.print(String(i)); 

    for j in 1:k loop 
     Modelica.Utilities.Streams.print(s[j]); 
    end for; 
    end when; 
end Test_Strings2;