modelicajmodelica

Modelica Class Method


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?


Solution

  • 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.