Search code examples
modelicadymolaopenmodelica

How to store and interpolate multidimensional, varibale size arrays in Modelica?


I have large size tables (A1, A2, ...) with at least 3 dimensions (say b, c, d) which can be represented as shown in the image, where array size n_b, n_c and n_d are variable.

package Arrays

  record A
    extends Modelica.Icons.Record;
    parameter Real b[:] "base array";
    parameter Arrays.Baseclasses.B b_target[:] "target array";

  end A;

  package Baseclasses
    record D
        extends Modelica.Icons.Record;
        parameter Real E[:];
        parameter Real F[:];

    end D;

    record C
      extends Modelica.Icons.Record;
      parameter Real d[:] "base array";
      parameter Arrays.Baseclasses.D d_target[:] "target array";

    end C;

    record B
      extends Modelica.Icons.Record;
      parameter Real c[:] "base array";
      parameter Arrays.Baseclasses.C c_target[:] "target array";
    end B;
  end Baseclasses;

  package Example
    model A
      Arrays.A a(b={1}, b_target={Arrays.Baseclasses.B(c={2}, c_target={
            Arrays.Baseclasses.C(d={3,4}, d_target={Arrays.Baseclasses.D(E={4,5},
            F={5,8}),Arrays.Baseclasses.D(E={2,3}, F={3,4})})})});
    end A;
  end Example;
  annotation (uses(Modelica(version="3.2.2")));
end Arrays;

enter image description here

For this type of structure I get following error:

Current version of the Modelica translator can only handle
array of components with fixed size.
But component a.b_target[1].c_target[1].d_target had non-fixed array dimensions [:].
In file: C:/....../Arrays.mo

All variables used in size expressions must be declared as constants or parameters.

Could not find member c_target[1] in record expression Arrays.Baseclasses.B(
{2}, 
{Arrays.Baseclasses.C(
{3, 4}, 
{Arrays.Baseclasses.D({4, 5}, {5, 8}), Arrays.Baseclasses.D({2, 3}, {3, 4})})}).

Could not find member c_target[1] in record expression Arrays.Baseclasses.B(
{2}, 
{Arrays.Baseclasses.C(
{3, 4}, 
{Arrays.Baseclasses.D({4, 5}, {5, 8}), Arrays.Baseclasses.D({2, 3}, {3, 4})})}).

The model contained invalid expressions.

Check of class aborted.

WARNINGS have been issued.

ERRORS have been issued.

One observation I had was that, when I make the n_d value constant, as an example:

    record C
      extends Modelica.Icons.Record;
      parameter Real d[2] "base array";
      parameter Arrays.Baseclasses.D d_target[2] "target array";

the error goes away. Does that mean Modelica translator allows variable size arrays till two dimensions and beyond that the array size need to be fixed? If so, is there a way around it to have a record with multi-dimensional variable size array?


Solution

  • I would say you encountered a bug.

    Arrays can have more than two dimension with unknown size, as this little example demonstrates (which runs fine in Dymola, but not in OpenModelica for some reason...)

    model MultDim
      Real A[:,:,:,:] = {B, B};
      Real B[:,:,:] = {C};
      Real C[:,:] = {D, D, D, D};
      Real D[:] = {1, 2, 3};
    end MultDim;
    

    In your case you a have a vector of Records, where each record contains another vector of records, and so on - so the problem is more complex than the one above. It's not only matter of multiple dimension with unknown size.

    I simplified your code to a minimal example with the same problem. This might be useful for others to understand the problem faster. Note, that the problem already occurs at B-level when you use intermediate variables.

    package Arrays2
      record A
        parameter B b[:];
      end A;
    
      record B
        parameter C c[:];
      end B;
    
      record C
        parameter D d[:];
      end C;
    
      record D
      end D;
    
      model Example_ok
          B b( c={ C( d={ D() }) });
      end Example_ok;
    
      model Example_fail
        A a(b={ B( c={ C(d={ D() }) }) });
      end Example_fail;
    
      model Example_fail2
        // if we use intermediate variables,
        // the example already fails at B-level.
        B b = B(c={ c });
        C c = C(d={ D() });
      end Example_fail2;
    end Arrays2;