1
votes

I'm trying to read in multiple files (csv, 2 columns) with belonging names (String) and interpolation methods smoothness (String). By using a record I get a nice GUI in Dymola: GUI

To explain my problem, here is a simplified model:

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;

The GUI for this looks as follows: enter image description here

The code to run it:

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

This will give the following result in the console: enter image description here

Now, if I try to use a record:

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

And adapt the model (only the first parameter line MyRecord2 x[:] and within the for loop x[j].name changed):

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;

Then I get a translation error: Internal error: failed to expand string. enter image description here

If I fix k or j within the for loop to a given number (let's say 3) then it works, but depending if it's within an algorithm or equation section (see comments within code).

I had similar problems with flexible array sizes and still don't understand how to solve it. Do I have to use functions? How can I use flexible array sizes which are defined depending on external data, selected as parameters prior to simulation (e.g. the length of a table)? Or is the problem in this case somewhere else?

Thanks.

1

1 Answers

1
votes

You can change the model to have an array of records as visible parameters, but internally use an array of strings (tested with Dymola 2017 and later):

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;