usnistgov / REFPROP-wrappers

Wrappers around NIST REFPROP for languages such as Python, MATLAB, etc.
190 stars 126 forks source link

Quality calculation in Python wrapper--> error -9999990 #565

Closed misseszti closed 10 months ago

misseszti commented 10 months ago

I am trying to calculate the quality based on density and internal energy of cryogenic hydrogen in Matlab/Python environment using the Python wrapper.

My problem comes when the fluid is superheated/subcooled as the results of my call for the database returns a value of "-9999990" which in the literature ((https://refprop-docs.readthedocs.io/en/latest/index.html) » REFPROP DLL documentation » High-Level API) indicates "nothing was calculated".

Meanwhile if I type my values directly to the REFPROP application I get the superheated/subcooled value as result.

In an example: rho = 1.332 [kg/m3] uv = 3.7065e+05 [J]

call in Matlab: RP = py.ctREFPROP.ctREFPROP.REFPROPFunctionLibrary('C:\Program Files (x86)\REFPROP') MASS_BASE_SI = RP.GETENUMdll(int8(0),'MASS BASE SI').iEnum; iMass = int8(1); iFlag = int8(0); r = double(RP.REFPROPdll('PARAHYD','DE','QMASS',MASS_BASE_SI,iMass,iFlag,rhov,uv,z).Output); quality = r(1)

Any suggestions how to make it work so I would get the information if my fluid is subcooled or superheated??

ianhbell commented 10 months ago

I think you are running into reference state problems. Does it work if you start with T,p, calculate rho,u, and then plug those rho,u back in? You must be in the gas phase, right?

ianhbell commented 10 months ago

This seems like an iteration failure in REFPROP. It seems to work in CoolProp though:

import CoolProp.CoolProp as CP 
print(CP.PropsSI('H', 'Dmass', rho, 'Umass', u, 'PARAHYD'))

although I am not 100% if the reference states are the same in both libraries (though the EOS are)

nist-aharvey commented 10 months ago

Part of the problem may be that it is just barely superheated. The saturated vapor at that density has an internal energy of 3.7031e+5 J. Iterative calculations very near a phase boundary can have a hard time converging. [Although I reproduce the result that the calculation works fine in the REFPROP desktop program.]

misseszti commented 10 months ago

Indeed, as I mentioned above the REFPROP desktop application allows me to retrieve the required values. I tried your approach @ianhbell : T = 18 K; p=1000000 Pa

r = double(RP.REFPROPdll('PARAHYD','TP','D',MASS_BASE_SI,iMass,iFlag,18,1000000,z).Output); rho = r(1) = 74.283066194738030

r = double(RP.REFPROPdll('PARAHYD','TP','E',MASS_BASE_SI,iMass,iFlag,1T,p,z).Output); uv = r(1) = -2.481432381242905e+04

r = double(RP.REFPROPdll('PARAHYD','DE','QMASS',MASS_BASE_SI,iMass,iFlag,rho,uv,z).Output); quality = r(1) = -9999990

And as you could see it returns me the same error.

@nist-aharvey : I am not sure if the error is characteristic for the values near the phase boundary. I tried a state further subcooled or superheated and the return of the call in python/matlab stays -9999990. As if the quality would only be a value representative if it is between 0 - 1.

ianhbell commented 10 months ago

I am very surprised that the GUI and the DLL are giving different results. Sometimes that is due to reference states between the GUI and the DLL, but the way you did the calls the reference state is a non-issue. So it comes down somehow to the iterative routines behaving differently, and I don't know why.

ianhbell commented 10 months ago

Have you tried to specify the phase with > or < ?

misseszti commented 10 months ago

I've just tried them, and it it doesn't work with that ones either.

r = double(RP.REFPROPdll('PARAHYD','DE<','QMASS',MASS_BASE_SI,iMass,iFlag,rho,uv,z).Output); quality = r(1) = -9999990

misseszti commented 10 months ago

A naiv question: could the problem come from the fact, that the expected Output of the DLL python wrapper is a number meanwhile the quality of the call at the GUI desktop application is in text (string format) in case of subcooled/ superheated results?

ianhbell commented 10 months ago

Ah I hadn't understood your comment, I see what you mean now. The results are the same, but not the messages, right? That is to be expected, as the GUI turns the quality into a human-readable string when not in [0,1]

misseszti commented 10 months ago

I understand now and I think I've found a way around. Actually, when the quality is subcooled or superheated, the quality is not defined, or as it is stated in the output of the call herr = 'State is single, quality not calculated'. So, it is normal for the call: _RP.REFPROPdll("PARAHYD","DE","QMASS",MASS_BASESI,0,0,rhov,uv,[1.0]).Output[0] the result is -9999990, because the quality is not calculated.

But, if I modify my call to _RP.REFPROPdll("PARAHYD","DE","QMASS",MASS_BASESI,0,0,rhov,uv,[1.0]).q I can withdraw the desired information and I can determine the state of my fluid following the results: q > 0 and q < 1: indicates a 2-phase state q < 0: Subcooled (compressed) liquid q = 0: Saturated liquid q = 1: Saturated vapor q > 1: Superheated vapor q = -998: Subcooled liquid, but quality not defined (usually P > Pc) q = 998: Superheated vapor, but quality not defined (usually T > Tc) q = 999: Supercritical state (T>Tc and P>Pc)

ianhbell commented 10 months ago

Yes I think you have a good resolution.