modelica / ModelicaStandardLibrary

Free (standard conforming) library to model mechanical (1D/3D), electrical (analog, digital, machines), magnetic, thermal, fluid, control systems and hierarchical state machines. Also numerical functions and functions for strings, files and streams are included.
https://doc.modelica.org
BSD 3-Clause "New" or "Revised" License
466 stars 166 forks source link

Problem with the function setState_phX #4209

Open congtoantran opened 11 months ago

congtoantran commented 11 months ago

I have a library which works well on Modelica 3.3.2, but when converted to Modelica 4.0.0, it doesn't work anymore, and the message error is "Non-real equation in continuous time are not legal:".

I'm trying to understand the problem by using a very simple example (see below):

model model1a
  replaceable package Medium = Modelica.Media.R134a.R134a_ph "Medium model" 
    annotation (choicesAllMatching=true);
  Medium.ThermodynamicState state_a ;
  Modelica.Units.SI.AbsolutePressure p=Medium.p_default;
  Modelica.Units.SI.SpecificEnthalpy h=Medium.h_default;

equation 
  state_a = Medium.setState_phX(Medium.p_default, Medium.h_default);
  //     state_a = Medium.setState_phX(p, h);
end model1a;

The above model runs sucessfully. But when I redefine the variable state_a by using the second line in the equation part, the model does not work and the error is :

  Non-real equation in continuous time are not legal:
  state_a.phase = Modelica.Media.R134a.R134a_ph.getPhase_ph(p, h);

Any help to understand the error? Many thanks. I use Dymola 2021, Modelica 4.0.0

beutlich commented 8 months ago

@HansOlsson I can reproduce in Dymola 2024x, but not in SimulationX 4.4. Is it a tool-issue (of one of the tools)?

It works though (in both tools) if the variability is changed to parameter.

model Model1b
  replaceable package Medium = Modelica.Media.R134a.R134a_ph "Medium model"
    annotation (choicesAllMatching=true);
  Medium.ThermodynamicState state_a;
  parameter Modelica.Units.SI.AbsolutePressure p=Medium.p_default;
  parameter Modelica.Units.SI.SpecificEnthalpy h=Medium.h_default;

equation 
  //state_a = Medium.setState_phX(Medium.p_default, Medium.h_default);
  state_a = Medium.setState_phX(p, h);
end Model1b;
HansOlsson commented 8 months ago

I would say it is a library issue that can be corrected.

The issue is that this record has a Boolean member (phase), and a Boolean equation requires that both sides are discrete-time valued, section 3.8.4.

The correction would be that the phase-functions get inlined with events.

casella commented 2 months ago

I checked @HansOlsson's pull request PR #4263 and it looks good to me.

@arunkumar-narasimhan please back-port this to maint/4.1.0

casella commented 2 months ago

This issue is actually a regression from v.4.0.0 but I also marked it with the 4.1.0-dev label so we don't forget about it

casella commented 1 week ago

@Esther-Devakirubai please back-port this to maint/4.1.x, then you can close this ticket. Thanks!