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

Robust hsu D flash method #2202

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

msaitta-mpr
Copy link
Contributor

Description of the Change

Providing inputs to CoolProp of D and U (or any of HSU) is usually stable, but sometimes can cause annoying issues. This is noted in issues #1965 and #2157 (as well as some of my personal projects). This PR attempts to solve these issues once and for all as an extension to the work done in PR #2173. This is done in 3 main changes:

  • Add additional checks and attempts at solving the ancillary equations. Adds more damping attempts and a way to force a guess.
  • Add limits to the extrapolated ancillary guesses. If the ancillary becomes very flat near the edges (e.g., near the triple point or critical point), it might not have a solution, but the extrapolated guess could be very far off. We limit the extrapolated guess so that the Akshasa solver can at least attempt something.
  • If all else fails, we give up on finding the saturation curve, and apply a Brent method directly to find T by iterating H/S/U. We do not know the phase, but at least we have everything else.

Benefits

We should now always get a solution for D_HSU inputs.

Possible Drawbacks

It is possible that one of our fallbacks does not provide the user with a phase, instead setting it to iphase_unknown.

The fallback solver can be slow because of needing a ton of Brent iterations, after already doing thousands of other iterations trying to find the saturation curve (which is now allowed to go to many of thousands of evaluations). However, a slow solution is almost always better than no solution.

Verification Process

I ran large sweeps of inputs to ensure that no errors were observed. I also integrated the results with another code and it seems to behave acceptably. I made custom test codes for issues #1965 and #2157. See the attached C++ code.

coolPropTester.zip

Applicable Issues

Closes #1965
Closes #2157

The SaturationAncillaryFunction::invert method is used to provide
guesses for several flash routines. However, if the inversion fails,
the entire flash routine will also fail. This commit makes the
routine more robust. Because it requires so many arguments, they
have been wrapped into their own struct.
The exrapolating secant solver is needed to prevent issues when the
Brent solver cannot bound the provided density, but in some cases
the ancillary function has no solutio and the extrapolation goes
very far out of range. This commit adds a limit to the result.
Perform more clipping to prevent the ancilliary curve
solution from giving us a bad starting point.
Finding the saturation curve can still sometimes fail, despite our
best efforts. However, we can simply brute force our way to a
temperature solution through Brent's method. This is slow, and we do
not know the phase, but it is better than nothing.
@msaitta-mpr msaitta-mpr changed the title Feat/robust hsu d Robust hsu D flash method Jan 9, 2023
@ibell
Copy link
Contributor

ibell commented Jan 19, 2023

This is an impressive PR, addressing a particular point that has caused me a lot of pain. Thank you very much. Are there any points to discuss before merging?

@jowr Do you want to take a look?

GCC does not seem to support brace initializers for structs with
default values. This commit removes the brace initializations, which
also makes the style more consistent throughout the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants