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

Simple question - simple answer ?

Simple question - simple answer ?

I just want to choose between Model 1 and 2 and I don't  understand how to do it in modelica :

package TestChoices

model M1
  parameter Real T ;
  Real f;
equation
  f = 1. + time;
end M1;

model M2
  parameter Real T ;
  Real f;
equation
  f = 2. + time;
end M2;



model fullmodel
  parameter Integer I = 1;
  parameter Real x = 5. ;
  Real y ;
//
equation

if 1 == I then
  "CALL MODEL1"          ?
elseif  1 == 2  then
  "CALL MODEL 2"         ?
end if;

end fullmodel;

  model Essai
  fullmodel full2(I = 2, x = 2.5)  annotation(
      Placement(visible = true, transformation(origin = {-20, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  equation

  end Essai;


end TestChoices;

Re: Simple question - simple answer ?

Code:


package TestChoices

model M1
  parameter Real T ;
  Real f;
equation
  f = 1. + time;
end M1;

model M2
  parameter Real T ;
  Real f;
equation
  f = 2. + time;
end M2;

model fullmodel
  parameter Integer I = 1;
  parameter Real x = 5. ;
  Real y ;
  M1 m1;
  M2 m2;
//
equation

if 1 == I then
  y = m1.f;
elseif  1 == 2  then
y = m2.f;
end if;

end fullmodel;

  model Essai
   fullmodel full2(I = 2, x = 2.5)  annotation(
      Placement(visible = true, transformation(origin = {-20, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  equation

  end Essai;


end TestChoices;

Not sure what you need, if it is an if equation or a when equation.
You can also use conditional components but that is a bit more
involved as you need connectors and connects to handle the
various choices:
https://specification.modelica.org/main … eclaration
https://mbe.modelica.university/compone … nt_models/

Re: Simple question - simple answer ?

Thank you,

I have just combined your different propositions to change the instantiation of M1 and M2 like this :

  M1 m1 if I==1 "Conditional component";
  M2 m2 if I==2 "Conditional component";

works and avoids to run both models as can be seen in the flat modelica :

class TestChoices2.fullmodel
  final parameter Integer I = 1;
  parameter Real x = 5.0;
  Real y;
  parameter Real m1.T;
  Real m1.f;
equation
  m1.f = 1.0 + time;
  y = m1.f;
end TestChoices2.fullmodel;

without conditional definition :

class TestChoices2.Essai
...
equation
  full2.m1.f = 1.0 + time;
  full2.m2.f = 2.0 + time;
  full2.y = full2.m1.f;
end TestChoices2.Essai;

I migth choose this solution with the parameter "I" in a inner/outer declaration (like system in the fluid library) so that all the models and connectors can be change with only an parameter.

Maybe it's an old fortran 66's way of programmation !

Edited by: JPG84 - Mar-25-21 14:19:13
There are 0 guests and 0 other users also viewing this topic
You are here: