Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceMoistAir, substances mass conservation issue #3666

Closed
GaelEnee opened this issue Nov 17, 2020 · 11 comments · Fixed by #3834
Closed

ReferenceMoistAir, substances mass conservation issue #3666

GaelEnee opened this issue Nov 17, 2020 · 11 comments · Fixed by #3834
Assignees
Labels
bug Critical/severe issue L: Media Issue addresses Modelica.Media
Milestone

Comments

@GaelEnee
Copy link

Hello,

I would like to report an issue with the Modelica.Media.Air.ReferenceMoistAir media.

From unexpected simulation results, I implemented the following simple model (cf. enclosed model): a constant mass flow source (Modelica.Fluid.Sources.MassFlowSource_T) with X = {0, 1} connected to a constant volume tank (Modelica.Fluid.Vessels.ClosedVolume) with X = {0.5, 0.5} @ t = 0. The tank water mass is expected to remain the same and the total mass was expected to increase linearly. Instead, the tank water mass increased and the total fluid mass increasing beyond expectation and beyond the water mass increase.

XBalance.zip

The issue has been observed with Dymola 2020 using Modelica Standard Library - Version 3.2.3 and confirmed using Modelica Standard Library - Version 4.0.0. As a comparison, using Modelica.Media.IdealGases.MixtureGases.AirSteam, the observed error is more likely due to computing small error propagation.

Quick investigations pointed to ReferenceMoistAir density and derivated functions.

Please let me know if problem if the problem has already been reported and if solution has been developed.

Thank you !

@beutlich beutlich added L: Fluid Issue addresses Modelica.Fluid (excl. Dissipation) L: Media Issue addresses Modelica.Media and removed L: Fluid Issue addresses Modelica.Fluid (excl. Dissipation) labels Nov 17, 2020
@GaelEnee
Copy link
Author

Hello,

After investigations, the problem seem to be solvable implementing the followings:

1/ Correction of the derivative within Utilities.pd_pTX_der():

    pd_der := (xw_der*(Modelica.Media.Air.ReferenceMoistAir.k_mair + xw) -
      xw*xw_der)*p + xw/(Modelica.Media.Air.ReferenceMoistAir.k_mair + xw)*
      p_der;

becomes

    pd_der := xw_der*Modelica.Media.Air.ReferenceMoistAir.k_mair/
      (Modelica.Media.Air.ReferenceMoistAir.k_mair + xw)^2*p
      + xw/(Modelica.Media.Air.ReferenceMoistAir.k_mair + xw)*p_der;

2/ Reformulating of Utilities.rho_pTX() and Utilities.rho_pTX_der() using X[1] and Xsat rather than xw and xws for numerical considerations (might hide errors in the current code). I can provide an example upon request.

Globally, the whole media code might be subject to improvements and simplifications.

@wischhusen
Copy link
Contributor

I will check as soon as possible and revert back. Many thanks for the work.

@beutlich beutlich added the bug Critical/severe issue label May 21, 2021
wischhusen added a commit to wischhusen/Modelica-1 that referenced this issue Jun 9, 2021
@wischhusen
Copy link
Contributor

wischhusen commented Jun 9, 2021

Sorry, I made two changes for issues in my master. Is someone else able to put

    pd_der := xw_der*Modelica.Media.Air.ReferenceMoistAir.k_mair/
      (Modelica.Media.Air.ReferenceMoistAir.k_mair + xw)^2*p
      + xw/(Modelica.Media.Air.ReferenceMoistAir.k_mair + xw)*p_der;

in function pd_pTX_der since the other PR is pending and I do not won't to roll back changes? Many thanks.

@beutlich
Copy link
Member

beutlich commented Jun 9, 2021

Is someone else able

Done. See #3834.

@wischhusen
Copy link
Contributor

Many thanks!

@beutlich beutlich added this to the MSL4.1.0 milestone Jun 9, 2021
@thorade
Copy link
Contributor

thorade commented Nov 24, 2021

Here is a test model for this function:

model pd_der_test
  replaceable package Medium = Modelica.Media.Air.ReferenceMoistAir;

  Real der_T = 300;
  Real T = 73.15 + der_T * time;
  Real der_p = -1e5;
  Real p = 1e7 + der_p * time;
  Real[Medium.nS] X = Medium.reference_X;

  Real var_from_function = Medium.Utilities.pd_pTX(p, T, X);
  Real timederiv = Medium.Utilities.pd_pTX_der(p, T, X, der_p, der_T, {0,0});
  Real var_from_timederiv;

  Real error_abs =  var_from_function - var_from_timederiv;
  Real error_rel =  abs(error_abs / var_from_function);

initial equation 
  var_from_timederiv = var_from_function;

equation 
  // var_from_timederiv = integral(timederiv);
  // but integral() operator does not exist
  // formulate it the other way round  
  der(var_from_timederiv) = timederiv;

  annotation (experiment(StopTime=100));
end pd_der_test;

@beutlich
Copy link
Member

Here is a test model for this function:

Thanks. If you like you can add it to the corresponding PR #3834.

@beutlich
Copy link
Member

Here is a test model for this function:

Edit: I do not see how #3834 fixes the two error variables of test model pd_der_test.

@hubertus65
Copy link
Member

To me, the proposed derivative looks wrong. If the function is:
//pd := xw/(Modelica.Media.Air.ReferenceMoistAir.k_mair + xw)*p;
And for better readability, I propose a shorthand and minor re-write:
Real k = Modelica.Media.Air.ReferenceMoistAir.k_mair; //pd := xw/(k + xw)*p;
I get:
pd_der = (p*xw_der + xw*p_der)/(k + xw) - p*xw*xw_der/(k + xw)^2
Could somebody please verify this, also with the test function from Matthis?

@TManikantan TManikantan assigned wischhusen and unassigned wischhusen Feb 15, 2023
beutlich pushed a commit to beutlich/ModelicaStandardLibrary that referenced this issue Apr 11, 2023
@beutlich
Copy link
Member

beutlich commented Apr 11, 2023

Could somebody please verify this, also with the test function from Matthis?

That is also correct, i.e. for pd = xw/(k + xw)*p the derivative is pd_der = (p*xw_der + xw*p_der)/(k + xw) - p*xw*xw_der/(k + xw)^2 or simplified pd_der = xw_der*k/(k + xw)^2*p + xw/(k + xw)*p_der as proposed by @GaelEnee or @wischhusen in #3666 (comment).

Still, my previous concern holds:

Edit: I do not see how #3834 fixes the two error variables of test model pd_der_test.

@HansOlsson
Copy link
Contributor

I believe there's a better solution will comment in #3834

beutlich added a commit to beutlich/ModelicaStandardLibrary that referenced this issue Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Critical/severe issue L: Media Issue addresses Modelica.Media
Projects
None yet
6 participants