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?

4

2 回答 2

1

Modelica 不支持成员函数,因此在模型中声明的函数就像独立函数一样,无法访问周围的模型变量。不允许使用成员函数,因为函数必须是纯函数,即不允许它们有任何副作用。这是 Modelica 中的一个基本假设,它使工具能够应用符号转换和重新排列计算。

于 2015-10-06T21:58:25.983 回答
1

如果您将所需的变量作为附加输入显式传递给函数,则可以拥有类似成员函数的东西。看这个例子:

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;

于 2020-08-31T09:59:40.750 回答