jjgomera / iapws

python libray for IAPWS standard calculation of water and steam properties
GNU General Public License v3.0
170 stars 64 forks source link

Ammonia Class not converging to the right roots #47

Closed vancromy closed 4 years ago

vancromy commented 4 years ago

Hello,

Thanks for this great repo allowing us to make use of the IAPWS standards. I was playing around with the ammonia.NH3 class and noticed some very unusual behaviour:

import iapws

iapws.ammonia.NH3(T=110+273.15,P=0.621325).rho #383 K and 6.2 bar (clearly gas phase)

/Users/yvc/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py:162: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)

Out[14]: 366.92738568157847

However when passing the correct density for that P and T, 3.4 kg/m3, the module is instantly returning the correct P without any issue:

iapws.ammonia.NH3(T=110+273.15,rho=3.4).P

Out[15]: 0.6199719347952626 #Pretty much 6.2 bar

It seems the MEoS is not able to converge to the right density but is having no problem working backwards to the pressure. Passing an initial rho0 did nothing:

iapws.ammonia.NH3(T=110+273.15,P=0.62, rho0=3.4).rho

/Users/yvc/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py:162: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last five Jacobian evaluations.
  warnings.warn(msg, RuntimeWarning)

Out[16]: 367.0501795698331

Trying this with the thermo package:

from thermo import Chemical

Chemical('Ammonia',T=110+273.15,P=620000).rho #Thermo uses Pascal
Out[17]: 3.400078841476925

I noticed that in the ammonia.py file, the critical pressure is defined in kPa and not MPa so I was wondering whether that might be one of the issues but could it be something with the root finder too?

OS version: macOS Catalina version 10.15.4 Python version: 3.7.6 Conda version: 4.8.3 IAPWS version: 1.4

jjgomera commented 4 years ago

Thanks for report it, it was a problem in logic in initial value for PT iteration, try with last commit, i think is fixed. The critical pressure value from ammonia and air was bad too, thanks for that too.

vancromy commented 4 years ago

Thanks for the quick reply and fix.

Cloned the repo and confirmed its now working as intended. Is the new commit also automatically added to the pypi build or should I keep working off my cloned copy?

jjgomera commented 4 years ago

I've uploaded a new version to pypi, https://pypi.org/project/iapws/ with a new minor version release, try it from there. I remember you it can install using pip from any github repository without waiting a new release in pypi:

pip install git+https://github.com/jjgomera/iapws

If you are interesting in other compound properties, not only water, give a try to another library: https://github.com/jjgomera/pychemqt/tree/master/lib/mEoS

Best regards