Search code examples
modelicaopenmodelica

Modelica coding standards / new OpenModelica compiler frontend


looks like a issue with new OpenModelica compiler frontend. I am using official release version of openmodelica 1.14 on windows-7 64bit OS.

package Test1_14
  model M1
    parameter Integer size = 2 "matrix size";
    parameter Real A[size] = {1.0, 1.0};
    Real B[size];
    Real T = 1;
    Real P = 2;
  equation
    B = A * (T/P);
  end M1;

  model M1_Extn
    Real C[size];
    Real D[size];
  equation
    for J in 1:size loop
      C[J] = Func1(T);
      D[J] = C[J] / P;
    end for;
  end M1_Extn;

  function Func1
    input Real a;
    output Real b;
  algorithm
  b := a*a;
  end Func1;

  model M1_Combined
    parameter Integer size = 2 "matrix size";
    Real B[size];
    Real T = 1;
    Real P = 2;
    extends M1_Extn;
  equation
    B = D;
  end M1_Combined;
end Test1_14;

When I compile the model ‘M1_Combined’, the code generation fails with new OpenModelica compiler frontend. Export FMU also fails with the same error.

Is my code as per Modelica programing standards?

How do I declare variables - size, T, P in the model M1_Extn and still use keyword ‘extends’ in ‘M1_Combined’ ?


Solution

  • This is because the old frontend did not handle the "extends" correctly, according to the Modelica Specification. The new frontend does it correctly.

    To be clear, you cannot define a variable in this class and then use it in the extends any other way than via a modification (and via inner/outer, via redeclare as element). An example is below, you cannot use a inside M1.

    package TestExtends
    
    model M1
      parameter Real b = a;
    end M1;
    
    model M2
      parameter Real a = 1;
      extends M1;
    end M2;
    
    end TestExtends;
    

    To fix your model so that is according to Modelica standards, you can do:

    package Test1_14
      model M1
        parameter Integer size = 2 "matrix size";
        parameter Real A[size] = {1.0, 1.0};
        Real B[size];
        Real T = 1;
        Real P = 2;
      equation
        B = A * (T/P);
      end M1;
    
      model M1_Extn
        parameter Integer size = 2;
        Real T = 1;
        Real P = 2;
        Real C[size];
        Real D[size];
      equation
        for J in 1:size loop
          C[J] = Func1(T);
          D[J] = C[J] / P;
        end for;
      end M1_Extn;
    
      function Func1
        input Real a;
        output Real b;
      algorithm
      b := a*a;
      end Func1;
    
      model M1_Combined
        extends M1_Extn(size=2, T = 1, P = 2);
        Real B[size]; // this one works as you can use things defined in extends.
      equation
        B = D;
      end M1_Combined;
    end Test1_14;