In [1]:
%load_ext autoreload
%autoreload 2

Issue with ammonia-aqua properties

On 2017-09-20, I'm noticing a problem with the reflux stream class. It comes up with non-monotonic (T,q) curve. I suspect that the property lookups are not well-behaved, that is, they are not monotonic.

In [2]:
from ammonia_props import convert_state_list_to_array, CStateTable
import ammonia1
import system_aqua1
import numpy as np
import matplotlib.pyplot as plt
In [3]:
xC = np.array([0.51284472, 277.97717012, 312.16427764, 313.6952877,
               310.24856734, 374.14020482])
ch = system_aqua1.makeChiller(xC)
In [43]:
ch.getRectifierStream()
====================Rectifier debug info====================
vapor_inlet (gen_vapor_outlet) =  State(T=351.9344380231556, P=15.139624377424653, x=0.9881288434190637, h=1424.61045386996, s=4.574749842640767, u=1268.729121159115, v=0.10296248362891122, Qu=1.0)
liquid_outlet (gen_reflux_inlet) =  State(T=351.9344380231556, P=15.139624377424653, x=0.5118797468777768, h=118.42029741010825, s=0.9705886173277942, u=116.44515361513855, v=0.0013046187578569727, Qu=0.0)
m_net, x_net =  0.0993943080368 0.999869
  x vapor    x liquid    T vapor    T liquid    h vapor    h liquid       q (kW)
---------  ----------  ---------  ----------  ---------  ----------  -----------
 0.999869    0.959099    313.652     313.652    1294.6     163.116   -16.1228
 0.999629    0.880904    316.455     316.455    1297.34    119.032   -15.614
 0.99939     0.819957    319.163     319.163    1299.67     89.6041  -15.2974
 0.99915     0.775868    321.679     321.679    1301.61     73.4354  -15.0334
 0.998911    0.742319    324.001     324.001    1303.25     64.8554  -14.8034
 0.998671    0.732601    324.747     324.747    1326.41     63.043   -12.3955
 0.998431    0.713404    326.324     326.324    1330.93     60.4086  -11.8746
 0.998192    0.697338    327.752     327.752    1335.14     59.2061  -11.3862
 0.997952    0.683572    329.057     329.057    1339.09     58.9284  -10.9251
 0.997713    0.671559    330.258     330.258    1342.81     59.2682  -10.4873
 0.997473    0.660922    331.371     331.371    1346.34     60.0305  -10.0694
 0.997233    0.651391    332.409     332.409    1349.71     61.0871   -9.66897
 0.996994    0.64277     333.38      333.38     1352.93     62.3504   -9.28379
 0.996754    0.634902    334.293     334.293    1356.02     63.7605   -8.91217
 0.996515    0.627673    335.155     335.155    1359        65.2739   -8.55261
 0.996275    0.62099     335.971     335.971    1361.87     66.8595   -8.2039
 0.996035    0.61478     336.747     336.747    1364.65     68.4943   -7.86498
 0.995796    0.608982    337.486     337.486    1367.34     70.1615   -7.53496
 0.995556    0.603547    338.191     338.191    1369.96     71.8484   -7.21304
 0.995317    0.598432    338.867     338.867    1372.5      73.5454   -6.89855
 0.995077    0.593603    339.514     339.514    1374.99     75.2452   -6.5909
 0.994838    0.589031    340.137     340.137    1377.41     76.9423   -6.28954
 0.994598    0.58469     340.735     340.735    1379.77     78.6326   -5.99404
 0.994358    0.580558    341.313     341.313    1382.09     80.3129   -5.70394
 0.994119    0.576617    341.87      341.87     1384.35     81.9809   -5.41889
 0.993879    0.572849    342.409     342.409    1386.57     83.6347   -5.13854
 0.99364     0.56924     342.931     342.931    1388.75     85.273    -4.86259
 0.9934      0.565777    343.436     343.436    1390.9      86.8949   -4.59077
 0.99316     0.56245     343.927     343.927    1393        88.4997   -4.32282
 0.992921    0.559247    344.403     344.403    1395.07     90.0869   -4.05857
 0.992681    0.55616     344.867     344.867    1397.11     91.6564   -3.7977
 0.992442    0.55318     345.317     345.317    1399.11     93.2081   -3.54008
 0.992202    0.550301    345.757     345.757    1401.09     94.7418   -3.28552
 0.991962    0.547515    346.185     346.185    1403.04     96.2578   -3.03386
 0.991723    0.544818    346.602     346.602    1404.96     97.7561   -2.78495
 0.991483    0.542202    347.01      347.01     1406.86     99.237    -2.53866
 0.991244    0.539663    347.408     347.408    1408.74    100.701    -2.29484
 0.991004    0.537864    347.692     347.692    1405.16    101.754    -2.5992
 0.990764    0.535469    348.072     348.072    1406.46    103.176    -2.41372
 0.990525    0.533136    348.444     348.444    1407.74    104.584    -2.23069
 0.990285    0.530861    348.809     348.809    1409       105.978    -2.04999
 0.990046    0.528642    349.167     349.167    1410.24    107.358    -1.87152
 0.989806    0.526475    349.518     349.518    1411.46    108.725    -1.69519
 0.989566    0.524359    349.864     349.864    1412.66    110.078    -1.5209
 0.989327    0.52229     350.203     350.203    1413.84    111.417    -1.34858
 0.989087    0.519687    350.632     350.632    1419.65    113.128    -0.710317
 0.988848    0.517705    350.96      350.96     1420.81    114.448    -0.540851
 0.988608    0.515764    351.283     351.283    1421.95    115.757    -0.373105
 0.988368    0.513863    351.601     351.601    1423.07    117.052    -0.207016
 0.988129    0.512001    351.914     351.914    1424.18    118.336    -0.0425232
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\Documents\GitHub\openACHP\src\ammonia1.py in getRectifierStream(self)
    843             return AmmoniaRefluxStream(self.gen_vapor_outlet, self.m_gen_vapor,
--> 844                                        self.gen_reflux_inlet, self.m_gen_reflux)
    845         except:

~\Documents\GitHub\openACHP\src\ammonia1.py in __init__(self, vapor_inlet, m_inlet, liquid_outlet, m_reflux, debug)
    330         self.q = scipy.interpolate.PchipInterpolator(T_points, q_points)
--> 331         self.T = scipy.interpolate.PchipInterpolator(q_points, T_points)
    332 

~\Miniconda3\envs\openachp\lib\site-packages\scipy\interpolate\_cubic.py in __init__(self, x, y, axis, extrapolate)
    102 
--> 103         _b = BPoly.from_derivatives(x, data, orders=None)
    104         super(PchipInterpolator, self).__init__(_b.c, _b.x,

~\Miniconda3\envs\openachp\lib\site-packages\scipy\interpolate\interpolate.py in from_derivatives(cls, xi, yi, orders, extrapolate)
   1688         c = np.asarray(c)
-> 1689         return cls(c.swapaxes(0, 1), xi, extrapolate)
   1690 

~\Miniconda3\envs\openachp\lib\site-packages\scipy\interpolate\interpolate.py in __init__(self, c, x, extrapolate, axis)
    689         if not (np.all(dx >= 0) or np.all(dx <= 0)):
--> 690             raise ValueError("`x` must be strictly increasing or decreasing.")
    691 

ValueError: `x` must be strictly increasing or decreasing.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-43-fa32c1c31937> in <module>()
----> 1 ch.getRectifierStream()

~\Documents\GitHub\openACHP\src\ammonia1.py in getRectifierStream(self)
    846             return AmmoniaRefluxStream(self.gen_vapor_outlet, self.m_gen_vapor,
    847                                        self.gen_reflux_inlet, self.m_gen_reflux,
--> 848                                        debug=True)
    849 
    850     def getSHX(self, **kwargs):

~\Documents\GitHub\openACHP\src\ammonia1.py in __init__(self, vapor_inlet, m_inlet, liquid_outlet, m_reflux, debug)
    329 
    330         self.q = scipy.interpolate.PchipInterpolator(T_points, q_points)
--> 331         self.T = scipy.interpolate.PchipInterpolator(q_points, T_points)
    332 
    333 

~\Miniconda3\envs\openachp\lib\site-packages\scipy\interpolate\_cubic.py in __init__(self, x, y, axis, extrapolate)
    101         data = np.hstack((yp[:, None, ...], dk[:, None, ...]))
    102 
--> 103         _b = BPoly.from_derivatives(x, data, orders=None)
    104         super(PchipInterpolator, self).__init__(_b.c, _b.x,
    105                                                 extrapolate=extrapolate)

~\Miniconda3\envs\openachp\lib\site-packages\scipy\interpolate\interpolate.py in from_derivatives(cls, xi, yi, orders, extrapolate)
   1687 
   1688         c = np.asarray(c)
-> 1689         return cls(c.swapaxes(0, 1), xi, extrapolate)
   1690 
   1691     @staticmethod

~\Miniconda3\envs\openachp\lib\site-packages\scipy\interpolate\interpolate.py in __init__(self, c, x, extrapolate, axis)
    688         dx = np.diff(self.x)
    689         if not (np.all(dx >= 0) or np.all(dx <= 0)):
--> 690             raise ValueError("`x` must be strictly increasing or decreasing.")
    691 
    692         dtype = self._get_dtype(self.c.dtype)

ValueError: `x` must be strictly increasing or decreasing.

The error occurs in the vapor phase enthalpy around a temperature of 347 K. Maybe we can zoom in on that. Using functions of (P, T, Qu):

In [71]:
P=ch.gen_vapor_outlet.P
x=ch.gen_vapor_outlet.x
vap, liq = [], []
a_T = np.linspace(346.,348.)
#a_T = np.linspace(300.,400.)
for T in a_T:
    vap.append(ammonia1.amm.props2(P=P, T=T, Qu=1))
    liq.append(ammonia1.amm.props2(P=P, T=T, Qu=0))
a_vap = convert_state_list_to_array(vap)
a_liq = convert_state_list_to_array(liq)
#display(CStateTable(a_vap))
plt.figure(1)
plt.plot(a_T, a_vap['h'],'.')
plt.figure(2)
plt.plot(a_T, a_liq['h'],'.')
plt.show()

That shows no problems - ie, the curves are monotonic. Perhaps the problem is with the inputs to the property functions. The code uses AmmoniaProps.equilibriumStates2, which in turn calls props2 first for the vapor properties with inputs (P, x, Qu), and then for the liquid properties with inputs (P, T, Qu).

In [74]:
P=ch.gen_vapor_outlet.P
a_x_wide = np.linspace(0.9, 0.9999)
a_x_narrow = np.linspace(0.990, 0.992)
for a_x in (a_x_wide, a_x_narrow):
    vap, liq = [], []
    for x in a_x:
        vap.append(ammonia1.amm.props2(P=P, x=x, Qu=1))
        liq.append(ammonia1.amm.props2(P=P, x=x, Qu=0))
    a_vap = convert_state_list_to_array(vap)
    a_liq = convert_state_list_to_array(liq)
    plt.figure()
    plt.plot(a_x, a_vap['h'],'r.')
    plt.xlabel("Mass fraction ammonia in vapor (kg/kg)")
    plt.ylabel("Enthalpy in vapor (kJ/kg)")
    plt.figure()
    plt.plot(a_x, a_liq['h'],'b.')
    plt.xlabel("Mass fraction ammonia in vapor (kg/kg)")
    plt.ylabel("Enthalpy in vapor (kJ/kg)")
plt.show()

display(CStateTable(a_vap))
T P x h s u v Qu
349.23415.13960.99 1410.484.534061257.020.101363 1
349.17415.13960.9900411410.274.533461256.840.101339 1
349.11415.13960.9900821410.064.532851256.670.101314 1
349.05315.13960.9901221409.854.532241256.5 0.10129 1
348.99215.13960.9901631409.644.531631256.320.101265 1
348.93115.13960.9902041409.424.531021256.150.101241 1
348.87 15.13960.9902451409.214.5304 1255.980.101216 1
348.80815.13960.9902861409 4.529791255.8 0.101191 1
348.74615.13960.9903271408.794.529171255.630.101166 1
348.68515.13960.9903671408.574.528551255.450.101141 1
348.62215.13960.9904081408.364.527931255.270.101116 1
348.56 15.13960.9904491408.144.5273 1255.1 0.101091 1
348.49815.13960.99049 1407.934.526681254.920.101065 1
348.43515.13960.9905311407.714.526051254.740.10104 1
348.37215.13960.9905711407.5 4.525421254.560.101015 1
348.30915.13960.9906121407.284.524791254.390.100989 1
348.24515.13960.9906531407.064.524161254.210.100963 1
348.18215.13960.9906941406.844.523521254.030.100937 1
348.11815.13960.9907351406.624.522881253.850.100912 1
348.05415.13960.9907761406.4 4.522241253.670.100886 1
347.99 15.13960.9908161406.184.5216 1253.490.10086 1
347.92515.13960.9908571405.964.520961253.3 0.100833 1
347.86115.13960.9908981405.744.520311253.120.100807 1
347.79615.13960.9909391405.524.519671252.940.100781 1
347.73115.13960.99098 1405.294.519021252.760.100754 1
347.66515.13960.99102 1405.074.518371252.570.100728 1
347.6 15.13960.9910611404.854.517711252.390.100701 1
347.53415.13960.9911021404.624.517061252.2 0.100674 1
347.46815.13960.9911431404.394.5164 1252.020.100647 1
347.40215.13960.9911841404.174.515741251.830.10062 1
347.43915.13960.9912241408.894.529461255.710.101178 1
347.37215.13960.9912651408.574.528531255.450.10114 1
347.30515.13960.9913061408.254.527611255.180.101103 1
347.23715.13960.9913471407.934.526681254.920.101066 1
347.16915.13960.9913881407.614.525751254.660.101028 1
347.10115.13960.9914291407.294.524821254.4 0.10099 1
347.03315.13960.9914691406.974.523891254.130.100953 1
346.96415.13960.99151 1406.654.522961253.870.100915 1
346.89515.13960.9915511406.334.522021253.6 0.100877 1
346.82615.13960.9915921406 4.521081253.340.100838 1
346.75715.13960.9916331405.684.520141253.070.1008 1
346.68715.13960.9916731405.364.5192 1252.810.100762 1
346.61715.13960.9917141405.034.518251252.540.100723 1
346.54615.13960.9917551404.7 4.5173 1252.270.100684 1
346.47615.13960.9917961404.384.516351252 0.100645 1
346.40515.13960.9918371404.054.5154 1251.740.100606 1
346.33315.13960.9918781403.724.514441251.470.100567 1
346.26215.13960.9919181403.394.513481251.2 0.100528 1
346.19 15.13960.9919591403.064.512521250.930.100489 1
346.11815.13960.992 1402.734.511561250.660.100449 1

From what I observe, I would guess that there is a transition between fitting functions that was not smoothed. Another possibility for a discrepancy is that the (P, x, Qu) inputs require an iterative solve with different inputs, but that would not explain this jump. We probably ought to apply some smoothing or use a different set of inputs. Argh.

After making some changes, here is the output for the same test case:

In [20]:
rs = ch.getRectifierStream()
rs
C:\Users\user1\Miniconda3\envs\openachp\lib\site-packages\scipy\optimize\minpack.py:161: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)
Out[20]:
<ammonia1.AmmoniaRefluxStream at 0x7affb30>

No error means the inerpolation was successfully performed.

In [ ]: