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

Investigating regfun3 #4128

Open
TManikantan opened this issue May 9, 2023 · 5 comments
Open

Investigating regfun3 #4128

TManikantan opened this issue May 9, 2023 · 5 comments
Labels
bug Critical/severe issue
Milestone

Comments

@TManikantan
Copy link
Contributor

TManikantan commented May 9, 2023

          OK, I made some more enquiries using the very nice OMC [algorithmic debugger](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/debugger.html#the-algorithmic-debugger).

This MWE replicates the problem with regFun3() at the point were the mass flow rate function jumps:

model TestRegFun3
  Real x = 1.5023328;
  Real x0 = 0;
  Real x1 = 1.5023328811860801;
  Real y0 = 0;
  Real y1 = 0.0015270174250288026;
  Real yd0 = 0.0010172719108540143;
  Real yd1 = 0.0010164308084791662;
  Real y = Modelica.Fluid.Utilities.regFun3(x, x0, x1, y0, y1, yd0, yd1);
end TestRegFun3;

Since x is very close to x1, one would expect y to be very close to y1 = 0.0015270174250288026. Instead one gets y = 0.00137275.

What I understand is that the derivative yd1 is very, very close to (y1-y0)/(x1-x0), while the derivative yd0 is slightly larger. I can replicate the issue with round figures:

model TestRegFun3_round
  Real x = time;
  Real x0 = 0;
  Real x1 = 1;
  Real y0 = 0;
  Real y1 = 0.001;
  Real yd0 = 0.0010008;
  Real yd1 = 0.001;
  Real y = Modelica.Fluid.Utilities.regFun3(x, x0, x1, y0, y1, yd0, yd1);
  annotation(experiment(StopTime = 1, StartTime = 0, Tolerance = 1e-06, Interval = 0.002));
end TestRegFun3_round;

immagine

Variable y should go from y0 to y1, and quite obviously it doesn't do that.

Bottom line: regFun3() is numerically ill-conditioned when the two supplied derivatives are very close to the slope of the curve connecting the two points. I'm not sure whether this is inherent to the algorithm by Gasparo and Morandi or if it is just @sielemann's implementation that has some glitch in those conditions.

At this point this is no longer a Fluid issue, but a plain old computer science problem, so I gladly hand it over to someone else who's more competent than I am. @sielemann, @gkurzbach, @beutlich any suggestion? @hubertus65 do you think Michael would be interested at plunging back into this problem?

Originally posted by @casella in #3758 (comment)

@TManikantan
Copy link
Contributor Author

TManikantan commented May 9, 2023

Also read #3758 (comment)
by @beutlich

@TManikantan TManikantan added the bug Critical/severe issue label Jun 8, 2023
@TManikantan TManikantan modified the milestones: maintenance, MSL4.1.0 Jun 8, 2023
@HansOlsson
Copy link
Contributor

Also read #3758 (comment) by @beutlich

I agree that there is something there, but I also realized a simple fix, that both seems to work and make sense. After computing xi2 add:

      if  xi1 < x0 or xi2>x1 then
        // The limits don't make sense, just use linear interpolation
        y := (y1-y0)*(x-x0)/(x1-x0) + y0;
      else
        ...
      end if;

@TManikantan
Copy link
Contributor Author

Do we looker deeper into this issue for this milestone or different milestone ?as we have already found a working solution.

@casella
Copy link
Contributor

casella commented Jun 30, 2023

I guess this has lower priority now. I would fix other pending issues first.

@TManikantan TManikantan added the P: low Low priority issue label Jul 7, 2023
@casella casella removed the P: low Low priority issue label Feb 9, 2025
@casella
Copy link
Contributor

casella commented Feb 9, 2025

The first fix by @HansOlsson in #4157 fixed the originally reported issue in #3758, namely a jump in the mass flow rate when dp became larger than dp_small. Unfortunately, as reported by @MatthiasBSchaefer in #4312, it broke other models, in particular causing chattering in Modelica.Fluid.Examples.TraceSubstances.RoomCO2WithControls around zero mass flow rate.

PR #4522, also by @HansOlsson, added further fixes to regfun3, which now seems to be working fine in all cases of interest. So, once #4522 is fixed and back-ported to maint/4.1.x, we should be able to close this ticket for good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Critical/severe issue
Projects
None yet
Development

No branches or pull requests

3 participants