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

Model Selction based on Boolen Parameter or Enumeration

Model Selction based on Boolen Parameter or Enumeration

Morning, everyone,

I have two very similar model. Model_PT calculat the thermodynamic state based on pressure and Temperature. Model_PH calculat the thermodynamic state based on pressure and enthalpy. so I want to write a model to unify them. In this model there is two boolen parameter called Use_T and Use_h. if Use_T = true, then model_PT will be used, if use_h = true, then model_PH is used.
but I didn't find a way to do it in openModelica.
Could some one give a hint?

Best Regards

Here is my code:
----------------------------------------------------------------------------------------------------------
package Test_Selection
  model Model_PT
    package Medium = Modelica.Media.R134a.R134a_ph;
    parameter Modelica.SIunits.AbsolutePressure p;
    parameter Modelica.SIunits.Temperature T;
    Medium.ThermodynamicState state;
  equation
    state = Medium.setState_pTX(p, T);
  end Model_PT;

  model Model_PH
    package Medium = Modelica.Media.R134a.R134a_ph;
    parameter Modelica.SIunits.AbsolutePressure p;
    parameter Modelica.SIunits.Enthalpy h;
    Medium.ThermodynamicState state;
  equation
    state = Medium.setState_phX(p,h);
  end Model_PH;

  model selectModel
    //selct model based on boolen Parameter
    package Medium = Modelica.Media.R134a.R134a_ph;
    parameter Modelica.SIunits.AbsolutePressure p;
    parameter Boolean Use_T = false;
    parameter Boolean Use_h = false;
    parameter Real secondInput = 0;
    Medium.ThermodynamicState state;
   
  equation
  if Use_T then
      Model_PT model_PT(p = p, T = secondInput);
      state = model_PT.state;
    elseif Use_h then
      Model_PH model_PH(p = p, h = secondInput);
      state = model_PT.state;
    end if;
  end selectModel;
end Test_Selection;
----------------------------------------------------------------------------------

Re: Model Selction based on Boolen Parameter or Enumeration

I think that bei using ModelicaReference.Annotations.choices can do what i want, selecting model via the GUI. so I change my code.
according to the documentation, when annotation(choices(choice(redaclare bla bla))) be added to the code, there will be a selection list in the GUI. but it dosen't work.
could someone give me a example for how to use annotation.example? I tryed the example in https://build.openmodelica.org/Document … oices.html  but it dosen't work either
------------------------------------------------------
  model Model_P
    package Medium = Modelica.Media.R134a.R134a_ph;
    parameter Modelica.SIunits.AbsolutePressure p;
    Medium.ThermodynamicState state;
  equation

  end Model_P;
  model Model_PT
    extends Test_Selection.Model_P;
 
  parameter Modelica.SIunits.Temperature T;
  equation
    state = Medium.setState_pTX(p, T);
  end Model_PT;

  model Model_PH
    extends Test_Selection.Model_P;
    parameter Modelica.SIunits.Enthalpy h;
 
  equation
    state = Medium.setState_phX(p,h);
  end Model_PH;

  model selectModel
    replaceable Model_PT model_final(p = p, T=secondInput) annotation(choices(
    choice(redeclare Model_PH model_final(p=p, h= secondInput))));
    parameter Modelica.SIunits.AbsolutePressure p;
    parameter Real secondInput = 0;
  end selectModel;
------------------------------------------------------------------------------------------------

Edited by: Czzzzz - Jan-28-20 10:13:38

Re: Model Selction based on Boolen Parameter or Enumeration

That is because we do not have support for redeclares in the GUI yet.
Is work in progress to be part of 1.15 soon.

Re: Model Selction based on Boolen Parameter or Enumeration

thanks, so is the model selection through GUI also not possible in 1.14?
as my first post, I want to creat a Model, which select submodel to be used via Boolean parameter. Is it also not possible? 

Re: Model Selction based on Boolen Parameter or Enumeration

I supose that there is a good reason for not using a solution as simple as a direct selection. For the GUI input, it is not possible to have the parameters defined at the base level of the model, but yes those defined in objects inside the model. So the solution is to crate a separate model with the parameters you need as inputs, create an instance inside your model, and make equal the value of the parameters, or use directly those inside the GUI. I would sugest something like this:

package Test_Selection
  model GUI
    parameter Modelica.SIunits.AbsolutePressure p;
    parameter Modelica.SIunits.Temperature T;
    parameter Modelica.SIunits.Enthalpy h;
    parameter Boolean useT=true;
annotation(
        Diagram,
        Icon(graphics = {Text(origin = {-5, -67}, extent = {{105, -23}, {-93, 11}}, textString = "Use p,T: %useT"), Text(origin = {-5, 77}, extent = {{105, -23}, {-93, 11}}, textString = "p: %p"), Text(origin = {-7, 27}, extent = {{105, -23}, {-93, 11}}, textString = "T: %T"), Text(origin = {-7, -21}, extent = {{105, -23}, {-93, 11}}, textString = "h: %h")}));
  end GUI;
 
  model SelectModel2
    package Medium = Modelica.Media.R134a.R134a_ph;
    parameter Modelica.SIunits.AbsolutePressure p=gui.p;
    parameter Modelica.SIunits.Temperature T=gui.T;
    parameter Modelica.SIunits.Enthalpy h=gui.h;
    parameter Boolean useT=gui.useT "if made false enthalpy will be used";
    Medium.ThermodynamicState state;
    FreeFluids.Various.Test_Selection.GUI gui(T = 223.15, h = 1e5, p = 500000, useT = false)  annotation(
        Placement(visible = true, transformation(origin = {1, 1}, extent = {{-17, -17}, {17, 17}}, rotation = 0)));
  equation
    if useT then
      state=Medium.setState_pTX(p,T);
    else
      state=Medium.setState_phX(p,h);
    end if;
  end SelectModel2;
end Test_Selection;

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