Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Doubt about the instantiation process and calling of functions

Doubt about the instantiation process and calling of functions

Hi all,

I am working with a simple model of an electrical battery using phasors. The model works perfectly. I want to use the model for linking it with an optimisation software. To do so, I need the equations comming from the instantiation process which I can obtain without problem.

Here my doubt: When I instantiate the model, the model calls the atan3 function from the Modelica library, which contains the followinf if-else loop:

  if y0 == 0 then
    // For the default (y0 = 0), exactly the same result as with atan2(..) is returned
    y := w;
  else
    y := w + pi2*integer((pi+y0-w)/pi2);
  end if


When I try to run the optimisation process, an error comes up telling that I should use the if-expression instead of the if-else function.

My question is:

Can I do something simple to obtain the equations of the instantiated model with the if-else expression?
Can I call another version of the atan3 (own created) containing the if-else expression?

Thanks for your support!


Re: Doubt about the instantiation process and calling of functions

I guess the easiest would be do save the model using save total model in OMEdit.
Then edit the function in the new file to change the if equation to an if expression and then instantiate the model again.

Re: Doubt about the instantiation process and calling of functions

Thanks for your reply!

Well, now I have the total model and I am able to modify it, but some new if-statements have appear. For example:
   
        if initType == .Modelica.Blocks.Types.Init.SteadyState then
          der(y) = 0;
        elseif initType == .Modelica.Blocks.Types.Init.InitialState or initType == .Modelica.Blocks.Types.Init.InitialOutput then
          y = y_start;
        end if;

Which seems difficult to translate into an if-expression, since the variable is written as "y" and "der(y)".

Another example is that now I have obtained nested if-statements.

My questions:
1- It is possible to write an if-expression from an if-statement like the one I showed you?
1- It is possible to build nested if-expressions?

And last, I translated one if-statement to an if-expression, and opened it with OMShell, but then I am not  able to instantiate the model and save it. Could you please shed some light on hpw to do it?

Many thanks!


Re: Doubt about the instantiation process and calling of functions

Not sure if you are saying that you don't know how to instantiate the model or if the total model after edit and save is not loading.
If the file is not loading if OMShell probably you have made a mistake (syntax or semantic). Do you get true from loadFile?
The file will not load in OMEdit as OMEdit wants to just one class per file.

Anyhow, to instantiate the model in OMShell, just type:
instantiateModel(Path.To.Your.Model.Name);

Code:


        if initType == .Modelica.Blocks.Types.Init.SteadyState then
          der(y) = 0;
        elseif initType == .Modelica.Blocks.Types.Init.InitialState or initType == .Modelica.Blocks.Types.Init.InitialOutput then
          y = y_start;
        end if;

I guess it might work to translate it to this:

Code:


        0 = if initType == .Modelica.Blocks.Types.Init.SteadyState then
          der(y)
        elseif initType == .Modelica.Blocks.Types.Init.InitialState or initType == .Modelica.Blocks.Types.Init.InitialOutput then
          y_start - y
        else 0;

What kind of optimization software are you using?

There are 0 guests and 0 other users also viewing this topic
You are here: