ModelicaStandardLibrary icon indicating copy to clipboard operation
ModelicaStandardLibrary copied to clipboard

Generate events in functions; to avoid non-discrete booleans.

Open HansOlsson opened this issue 1 year ago • 7 comments

Closes #4209

I don't know if significant enough to push for release.

HansOlsson avatar Jan 15 '24 11:01 HansOlsson

model Model1b
  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 Model1b;

still fails in Dymola 2024x - even with the proposed changes. Would be good to have this model as test model.

Are other functions affected as well?

No, but it seems that I messed up the annotations a bit, it should now be resolved.

HansOlsson avatar Jan 17 '24 11:01 HansOlsson

still fails in Dymola 2024x - even with the proposed changes. Would be good to have this model as test model.

I added a test-model, but made it similar to an existing one instead.

HansOlsson avatar Jan 17 '24 12:01 HansOlsson

I don't know if significant enough to push for release.

For me it seems like a MSL bug. What's the opinion of the Media officers?

beutlich avatar Jan 18 '24 19:01 beutlich

Unfortunately, the new test model ModelicaTest.Media.TestOnly.R134a_setState_phX raises a AssignmentFromNonDiscreteToDiscrete warning in SimulationX 4.4 and fails to simulate with all tested solvers due to a singular system of equations. It does not matter if this PR is applied or not.

beutlich avatar Jan 18 '24 19:01 beutlich

Unfortunately, the new test model ModelicaTest.Media.TestOnly.R134a_setState_phX raises a AssignmentFromNonDiscreteToDiscrete warning in SimulationX 4.4 and fails to simulate with all tested solvers due to a singular system of equations. It does not matter if this PR is applied or not.

Ok, that is problematic. I assume the failure to simulate and assignment-error are related; indicating that the current version of the function doesn't work, and not some unrelated error in the example model.

I don't know the exact reason - is it related to calling GenerateEvents indirectly (i.e., one function with GenerateEvents calling another one)? If so it might be possible to rewrite it as:

  state := ThermodynamicState(phase=
         if ((h < bubbleEnthalpy(SaturationProperties(psat=p,Tsat=0)) 
          or (h > dewEnthalpy(SaturationProperties(psat=p,Tsat=0))) or (p > R134aData.data.FPCRIT)) then 1
         else 2 , p=p, h=h, d=density_ph(p, h), T=temperature_ph(p, h));

(I cannot test that in SimulationX.)

So what to do:

  • Just add
  • Delay until the next release.
  • Add the changes in MSL - but don't add the test.
  • Use the weird rewriting above - even if horrible copy-pasta. One could argue that this indirect calling isn't fully specified, whereas this rewriting should be ok.

HansOlsson avatar Jan 19 '24 10:01 HansOlsson

I would be in favor to add the rewrite proposed by Hans. It makes the event generation explicit. Could you add it to the PR?

hubertus65 avatar Jan 28 '24 18:01 hubertus65

I would be in favor to add the rewrite proposed by Hans. It makes the event generation explicit. Could you add it to the PR?

Should be done now. Please re-test

HansOlsson avatar Feb 15 '24 12:02 HansOlsson

@gkurzbach can you give some feedback? As I see it, the changes give clear benefits

HansOlsson avatar Jun 28 '24 09:06 HansOlsson

This PR should be back-ported to maint/4.1.0

casella avatar Jul 02 '24 12:07 casella

Backporting by #4541

Esther-Devakirubai avatar Feb 24 '25 16:02 Esther-Devakirubai