1

I would like to use a class function/method in my Modelica model as follows:

optimization Moo(objective=-x(finalTime), startTime = 0, finalTime = 12)
  parameter Real e = 0.05;

  Real x(start=2, fixed=true, min=0, max=100);

  input Real v (min=0, max=1);

  function omega
    input  Real t;
    output Real y;
  algorithm
    y := e;
  end omega;

equation
  der(x) = v*omega(time);
constraint
  v<=1;
end Moo;

I would like the variable e in the function omega to be a variable so that I can easily change its value at a later point in time when I am doing a parameter sweep. Unfortunately, the function omega does not seem to know about the variable e and the JModelica compiler returns the error:

Cannot find class or component declaration for e

I would naïvely expect that since omega and e belong to the same class omega would be able to see e.

Is there a way to achieve this?

Richard
  • 56,349
  • 34
  • 180
  • 251

2 Answers2

1

Member functions are not supported in Modelica, so a function declared inside a model acts like a standalone function without having access to the surrounding model variables. Member functions are not allowed due to functions need to be pure, i.e. they are not allowed to have any side effect. This is a fundamental assumption in Modelica that makes it possible for a tool to apply symbolic transformation and rearrange calculations.

jWindahl
  • 76
  • 6
  • 2
    A possible workaround is to change the function to a block, add a parameter to it, instantiate the block and propagate e to the new parameter and then reference the blockInstance.y in the equation. – jWindahl Oct 06 '15 at 22:39
1

You can have something like a member function, if you explicitly pass the needed variables as additional input to the function. See this example:

package MemberFunction
  
  model A
    Real x=1;
    function get_x = MemberFunction.get(u=x);
  end A;

  function get
    input Real u;
    output Real y;
  algorithm 
    y := u;
  end get;

  model Test
      A a;
      Real x;
  equation 
    x = a.get_x();
  end Test;

end MemberFunction;

Manuel
  • 313
  • 1
  • 6