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

Catch singular explosions in saturation_PHSU_pure #2250

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions dev/fluids/Ammonia.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,31 @@
"Tmax": 405.56,
"Tmin": 195.495,
"description": "rho'' = rhoc*exp(Tc/T*sum(n_i*theta^t_i))",
"max_abserror_percentage": 0.802513430755436,
"max_abserror_percentage": 0.312705,
"n": [
-0.053296,
-3.4589,
-6.7572,
-17.26,
-43.12,
-115.18
0.0,
-3.6322306612603197,
43.53295636848281,
-396.7025942753399,
1946.6788208368914,
-5739.851852849688,
10341.987533476391,
-11168.484405912883,
6642.700525911098,
-1675.050456540271
],
"reducing_value": 13696,
"t": [
0.14,
0.44,
1.314,
3.225,
6.4,
14.0
0.0,
0.3333333333333333,
0.5,
0.6666666666666666,
0.8333333333333334,
1.0,
1.1666666666666667,
1.3333333333333333,
1.5,
1.6666666666666667
],
"type": "rhoV",
"using_tau_r": true
Expand Down
46 changes: 46 additions & 0 deletions src/Backends/Helmholtz/VLERoutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,52 @@ void SaturationSolvers::saturation_PHSU_pure(HelmholtzEOSMixtureBackend& HEOS, C
info.c_str(), specified_value, error));
}
} while (error > 1e-9);
// Recalculate error
// The result has changed since the last error calculation.
// In rare scenarios, the final step can become unstable due to solving a singular
// J matrix. This final error check verifies that the solution is still good.
// Furthermore, the forced phase of SatL and SatV may have caused errors. We will recalculate them without this assumption.
Comment on lines +557 to +561
Copy link

@danielkwalsh danielkwalsh Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the singular J matrix still crop up with the fixed ancillary? I'm wondering if this part of the code is still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never actually checked whether it is still needed. I figure that it is a cheap enough check that it is probably worth leaving in.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, great. I'm satisfied with that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msaitta-mpr @ibell, so what are the next steps to get this merged?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If everyone agrees we are ready I can merge it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that in its current state that it fixes the known issue and does not introduce any others. It raises the question of whether there are other ancillary issues hidden elsewhere, but I think that we should merge for now and reevaluate the ancillaries some other time.

SatL->unspecify_phase();
SatV->unspecify_phase();
SatL->update(DmolarT_INPUTS, rhoL, T);
SatV->update(DmolarT_INPUTS, rhoV, T);
negativer[0] = -(deltaV * (1 + deltaV * SatV->dalphar_dDelta()) - deltaL * (1 + deltaL * SatL->dalphar_dDelta()));
negativer[1] = -(deltaV * SatV->dalphar_dDelta() + SatV->alphar() + log(deltaV) - deltaL * SatL->dalphar_dDelta() - SatL->alphar() - log(deltaL));
switch (options.specified_variable) {
case saturation_PHSU_pure_options::IMPOSED_PL:
// -r_3 (equate calculated pressure and specified liquid pressure)
negativer[2] = -(SatL->p() / specified_value - 1);
break;
case saturation_PHSU_pure_options::IMPOSED_PV:
// -r_3 (equate calculated pressure and specified vapor pressure)
negativer[2] = -(SatV->p() / specified_value - 1);
break;
case saturation_PHSU_pure_options::IMPOSED_HL:
// -r_3 (equate calculated liquid enthalpy and specified liquid enthalpy)
negativer[2] = -(SatL->hmolar() - specified_value);
break;
case saturation_PHSU_pure_options::IMPOSED_HV:
// -r_3 (equate calculated vapor enthalpy and specified vapor enthalpy)
negativer[2] = -(SatV->hmolar() - specified_value);
break;
case saturation_PHSU_pure_options::IMPOSED_SL:
// -r_3 (equate calculated liquid entropy and specified liquid entropy)
negativer[2] = -(SatL->smolar() - specified_value);
break;
case saturation_PHSU_pure_options::IMPOSED_SV:
// -r_3 (equate calculated vapor entropy and specified vapor entropy)
negativer[2] = -(SatV->smolar() - specified_value);
break;
default:
throw ValueError(format("options.specified_variable to saturation_PHSU_pure [%d] is invalid", options.specified_variable));
}
error = sqrt(pow(negativer[0], 2) + pow(negativer[1], 2) + pow(negativer[2], 2));
// reset the phase for the next update.
SatL->specify_phase(iphase_liquid);
SatV->specify_phase(iphase_gas);
if (error > 1e-8){
throw SolutionError(format("saturation_PHSU_pure solver was good, but went bad. Current error is %Lg", error));
}
}
void SaturationSolvers::saturation_D_pure(HelmholtzEOSMixtureBackend& HEOS, CoolPropDbl rhomolar, saturation_D_pure_options& options)
{
Expand Down