-
Notifications
You must be signed in to change notification settings - Fork 299
/
DEPRECATED_fit_incompressible.py
670 lines (587 loc) · 26.1 KB
/
DEPRECATED_fit_incompressible.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import numpy, matplotlib.pyplot
import CoolProp.CoolProp as CP
from scipy.optimize._minimize import minimize
from scipy.optimize.minpack import curve_fit
from matplotlib.ticker import MaxNLocator
import os
import numpy as np
class IncompLiquidFit(object):
"""
A class for fitting data sheet data to predefined functions.
Some functions are only used during the fitting procedure.
Note that the order in which you fit the different properties
might impact the coefficients. Usually, the fitting order should be:
1) Density
2) Heat capacity
3) Thermal conductivity
4) Viscosity
5) Vapour pressure
"""
def __init__(self):
self.DEBUG = False
# parameters for the different fits
self._cDensity = numpy.ones(4) # Typically 4 parameters
self._cHeatCapacity = numpy.ones(4) # Typically 4 parameters
self._cTConductivity = numpy.ones(3) # Typically 3 parameters
self._cViscosity = numpy.ones(3) # Typically 3 parameters
self._cPsat = numpy.ones(3) # Typically 3 parameters
# bounds for fit
self._Tmin = None
self._TminPsat = None
self._Tmax = None
self._Tref = 273.15 + 25.
self._Tbase = 0.0
# some flags to set
self._TinC = False # Temperature in Celsius
self._DynVisc = True # Data for dynamic viscosity
self._minPoints = 3
self._expPoly = False # Fit exponential as polynomial
def setParams(self, fluid):
if fluid == 'init':
# initial parameters for the different fits
# self._cDensity = [+9.2e+2, -0.5e+0, +2.8e-4, -1.1e-6]
# self._cHeatCapacity = [+1.0e+0, +3.6e-3, -2.9e-7, +1.7e-9]
# self._cTConductivity = [+1.1e-1, +7.8e-5, +3.5e-7]
# self._cViscosity = [+7.1e+2, +2.3e+2, +3.4e+1]
# self._cPsat = [-5.3e+3, +3.2e+1, -1.6e+1]
self._cDensity = [1, 1, 1, 1]
self._cHeatCapacity = [1, 1, 1, 1]
self._cTConductivity = [1, 1, 1]
#self._cViscosity = [+8e+2, -2e+2, +3e+1]
self._cViscosity = [+7e+2, -6e+1, +1e+1]
self._cPsat = [-5e+3, +3e+1, -1e+1]
return True
# elif fluid=='TherminolD12inCelsius':
# self._cDensity = [776.257 , -0.696982, -0.000131384, -0.00000209079]
# self._cHeatCapacity = [2.01422 , 0.00386884, 2.05029e-6, -1.12621e-8, 3.86282e-11]
# self._cTConductivity = [0.112994, 0.00014781, -1.61429e-7]
# self._cViscosity = [530.944, 146.4, -2.68168]
# self._cPsat = [-3562.69, 194, 13.8526]
# self._Tmin = -85.0 + 273.15
# self._TminPsat = 40.0 + 273.15
# self._Tmax = 260.0 + 273.15
# elif fluid=='TherminolD12':
# self._cDensity = [1.08315084e+04,-8.21176568e+01,2.23399244e-01, -2.03753274e-04]
# self._cHeatCapacity = [2.01422 , 0.00386884, 2.05029e-6, -1.12621e-8, 3.86282e-11]
# self._cTConductivity = [0.112994, 0.00014781, -1.61429e-7]
# self._cViscosity = [530.944, 146.4, -2.68168]
# self._cPsat = [-3562.69, 194, 13.8526]
# self._Tmin = -85.0 + 273.15
# self._TminPsat = 40.0 + 273.15
# self._Tmax = 260.0 + 273.15
else:
raise (ValueError("No coefficients available for " + str(fluid)))
def _checkT(self, T=0):
Tmin = self.Props('Tmin')
Tmax = self.Props('Tmax')
if Tmin is None:
raise (ValueError("Please specify the minimum temperature."))
if Tmax is None:
raise (ValueError("Please specify the maximum temperature."))
if not (Tmin <= T <= Tmax):
raise (ValueError("Temperature out of range: " + str(T) + " not in " + str(Tmin) + "-" + str(Tmax) + ". "))
def _checkP(self, T=0, P=0):
Psat = self.Props('Psat', T=T)
if P < Psat:
raise (ValueError("Equations are valid for liquid phase only: " + str(P) + " < " + str(Psat) + ". "))
def _checkTP(self, T=0, P=0):
self._checkT(T=T)
#self._checkP(T=T, P=P)
def _basePolynomial(self, coefficients, x):
""" Base function to produce polynomials of
order len(coefficients) with the coefficients
"""
result = 0.
for i in range(len(coefficients)):
result += coefficients[i] * x**i
return result
def _basePolynomialInt(self, coefficients, x1, x0=-1):
""" Base function to produce the integral of
order len(coefficients) with coefficients from
x0 to x1.
"""
if x0 == -1: x0 = self._Tref - self._Tbase
result = 0.
for i in range(len(coefficients)):
result += 1. / (i + 1.) * coefficients[i] * (x1**(i + 1.) - x0**(i + 1.))
return result
def _baseExponential(self, coefficients, x, num):
""" Base function to produce exponential
with defined coefficients
"""
# Determine limits:
maxVal = numpy.log(numpy.finfo(numpy.float64).max - 1)
minVal = -maxVal # numpy.log(numpy.finfo(numpy.float64).min+1)
# if len(coefficients)==num:
if num == 1: return numpy.exp(numpy.clip((coefficients[0] / (x + coefficients[1]) - coefficients[2]), minVal, maxVal))
if num == 2: return numpy.exp(numpy.clip(self._basePolynomial(coefficients, x), minVal, maxVal))
# else:
# print "Error!"
def Props(self, out, T=0, P=0):
if out == 'D':
self._checkTP(T=T, P=P)
return self._basePolynomial(self._cDensity, T - self._Tbase)
elif out == 'C':
self._checkTP(T=T, P=P)
return self._basePolynomial(self._cHeatCapacity, T - self._Tbase)
elif out == 'L':
self._checkTP(T=T, P=P)
return self._basePolynomial(self._cTConductivity, T - self._Tbase)
elif out == 'V':
self._checkTP(T=T, P=P)
if self._expPoly:
return numpy.exp(self._basePolynomial(self._cViscosity, T - self._Tbase))
else:
return self._baseExponential(self._cViscosity, T - self._Tbase, 1)
elif out == 'Psat':
self._checkT(T=T)
if T < self._TminPsat:
return 1e-14
if self._expPoly:
return numpy.exp(self._basePolynomial(self._cPsat, T - self._Tbase))
else:
return self._baseExponential(self._cPsat, T - self._Tbase, 1)
elif out == 'Tmin':
return self._Tmin
elif out == 'Tmax':
return self._Tmax
else:
raise (ValueError("Error: You used an unknown output qualifier."))
def _PropsFit(self, coefficients, inVal, T=0):
"""
Calculates a property from a given set of
coefficients for a certain temperature. Is used
to obtain data to feed to the optimisation
procedures.
"""
if inVal == 'D':
self._checkT(T=T)
return self._basePolynomial(coefficients, T - self._Tbase)
elif inVal == 'C':
self._checkT(T=T)
return self._basePolynomial(coefficients, T - self._Tbase)
elif inVal == 'L':
self._checkT(T=T)
return self._basePolynomial(coefficients, T - self._Tbase)
elif inVal == 'V':
self._checkT(T=T)
if self._expPoly:
return numpy.exp(self._basePolynomial(coefficients, T - self._Tbase))
else:
return self._baseExponential(coefficients, T - self._Tbase, 1)
elif inVal == 'Psat':
self._checkT(T=T)
if T < self._TminPsat:
return 1e-14
if self._expPoly:
return numpy.exp(self._basePolynomial(coefficients, T - self._Tbase))
else:
return self._baseExponential(coefficients, T - self._Tbase, 1)
else:
raise (ValueError("Error: You used an unknown property qualifier."))
def inCoolProp(self, name):
from CoolProp.CoolProp import FluidsList
# print FluidsList()
result = name in FluidsList()
if not result:
try:
CP.PropsU('Tmin', 'T', 0, 'P', 0, name, "SI")
return True
except ValueError as e:
print(e)
return False
def getCoefficients(self, inVal):
"""
Get the array with coefficients.
"""
if inVal == 'D':
return self._cDensity
elif inVal == 'C':
return self._cHeatCapacity
elif inVal == 'L':
return self._cTConductivity
elif inVal == 'V':
return self._cViscosity
elif inVal == 'Psat':
return self._cPsat
else:
raise (ValueError("Error: You used an unknown property qualifier."))
def setCoefficients(self, inVal, coeffs):
"""
Set the array of coefficients.
"""
if inVal == 'D':
self._cDensity = coeffs
elif inVal == 'C':
self._cHeatCapacity = coeffs
elif inVal == 'L':
self._cTConductivity = coeffs
elif inVal == 'V':
self._cViscosity = coeffs
elif inVal == 'Psat':
self._cPsat = coeffs
else:
raise (ValueError("Error: You used an unknown property qualifier."))
def setTmin(self, T):
self._Tmin = T
def setTmax(self, T):
self._Tmax = T
def setTminPsat(self, T):
self._TminPsat = T
def setTref(self, T):
self._Tref = T
def setTbase(self, T):
self._Tbase = T
def setExpPoly(self, bo):
self._expPoly = bo
def fitCoefficients(self, xName, T=[], xData=[]):
if (len(T) != len(xData)):
raise (ValueError("Error: There has to be the same number of temperature and data points."))
if len(T) < self._minPoints:
raise (ValueError("Error: You should use at least " + str(self._minPoints) + " points."))
def fun(coefficients, xName, T, xData):
# Values for conductivity are very small,
# algorithms prefer larger values
if xName == 'L':
calculated = numpy.array([self._PropsFit(coefficients, xName, T=Ti) for Ti in T])
data = numpy.array(xData)
# Fit logarithms for viscosity and saturation pressure
elif xName == 'V' or xName == 'Psat':
calculated = numpy.log(numpy.array([self._PropsFit(coefficients, xName, T=Ti) for Ti in T]))
data = numpy.log(numpy.array(xData))
else:
calculated = numpy.array([self._PropsFit(coefficients, xName, T=Ti) for Ti in T])
data = numpy.array(xData)
res = numpy.sum((calculated - data)**2.)
return res
initValues = self.getCoefficients(xName)[:]
# Fit logarithms for viscosity and saturation pressure
if xName == 'V' or xName == 'Psat':
# fit = "MIN" # use a home-made minimisation with Powell and Broyden-Fletcher-Goldfarb-Shanno
# fit = "LMA" # use the Levenberg-Marquardt algorithm from curve_fit
# fit = "POL" # use a polynomial in an exponential function
fit = ["LMA", "MIN"] # First try LMA, use MIN as a fall-back solver
if self._expPoly:
fit = ["POL"] # Overwrite preferences for polynomial
success = False
counter = -1
while (not success):
counter += 1
if fit[counter] == "LMA":
xData = numpy.array(xData)
fit_log = True
def func(T, *coefficients):
result = numpy.array([self._PropsFit(coefficients, xName, T=Ti) for Ti in T])
if fit_log:
return numpy.log(result)
else:
return result
if fit_log:
xData = numpy.log(xData)
try:
# Do the actual fitting
popt, pcov = curve_fit(func, T, xData, p0=initValues, maxfev=1000)
# print popt
# print pcov
success = True
return popt
except RuntimeError as e:
print("Exception: " + str(e))
print("Using: " + str(fit[counter + 1]) + " as a fall-back.")
success = False
elif fit[counter] == "MIN":
print("Fitting exponential with " + str(len(initValues)) + " coefficients.")
arguments = (xName, T, numpy.exp(xData))
#options = {'maxiter': 1e2, 'maxfev': 1e5}
if xName == 'V':
method = "Powell"
elif xName == 'Psat':
method = "BFGS"
tolStart = 1e-13
tol = tolStart
res = minimize(fun, initValues, method=method, args=arguments, tol=tol)
while ((not res.success) and tol < 1e-2):
tol *= 1e2
print("Fit did not succeed, reducing tolerance to " + str(tol))
res = minimize(fun, initValues, method=method, args=arguments, tol=tol)
# Include these lines for an additional fit with new guess values.
# if res.success and tol>tolStart:
# print "Refitting with new guesses and original tolerance of "+str(tolStart)
# res = minimize(fun, res.x, method=method, args=arguments, tol=tolStart)
if res.success:
success = True
return res.x
else:
print("Fit failed: ")
print(res)
success = False
elif fit[counter] == "POL":
print("Fitting exponential polynomial with " + str(len(initValues)) + " coefficients.")
z = numpy.polyfit(T - self._Tbase, numpy.log(xData)[:], len(initValues) - 1)
return z[::-1]
else:
raise (ValueError("Error: You used an unknown fit method."))
else: # just a polynomial
print("Fitting polynomial with " + str(len(initValues)) + " coefficients.")
z = numpy.polyfit(T - self._Tbase, xData, len(initValues) - 1)
return z[::-1]
# def fitCoefficientsCentered(self,xName,T=[],xData=[]):
# tBase = (self._Tmax-self._Tmin) / 2.0 + self._Tmin
# self.setTbase(tBase)
# return self.fitCoefficients(xName,T=T,xData=xData)
# Load the data
from data_incompressible import *
containerList = []
containerList += [TherminolD12()]
containerList += [TherminolVP1(), Therminol66(), Therminol72()]
containerList += [DowthermJ(), DowthermQ()]
containerList += [Texatherm22(), NitrateSalt(), SylthermXLT()]
containerList += [HC50(), HC40(), HC30(), HC20(), HC10()]
containerList += [AS10(), AS20(), AS30(), AS40(), AS55()]
containerList += [ZS10(), ZS25(), ZS40(), ZS45(), ZS55()]
def relError(A=[], B=[], PCT=False):
result = (numpy.array(A) - numpy.array(B)) / numpy.array(B);
if PCT:
return result * 100.
else:
return result
j = {}
for data in containerList:
# Some test case
liqObj = IncompLiquidFit()
liqObj.setParams("init")
liqObj.setTmin(data.Tmin)
liqObj.setTminPsat(data.TminPsat)
liqObj.setTmax(data.Tmax)
j['Tmin'] = data.Tmin
j['Tmax'] = data.Tmax
j['TminPsat'] = data.TminPsat
j['name'] = data.Name
j['description'] = data.Desc
j['reference'] = ''
#liqObj._cViscosity[0] = numpy.max(data.mu_dyn)
#liqObj._cPsat[0] = numpy.min(data.psat)
#numpy.set_printoptions(formatter={'float': lambda x: format(x, '+1.10E')})
print("")
print("------------------------------------------------------")
print("Fitting " + str(data.Name))
print("------------------------------------------------------")
print("")
print("minimum T: " + str(data.Tmin))
print("maximum T: " + str(data.Tmax))
print("min T pSat:" + str(data.TminPsat))
#liqObj.setTbase((data.Tmax-data.Tmin) / 2.0 + data.Tmin)
# liqObj.setExpPoly(True)
print("T base:" + str(liqObj._Tbase))
print("")
# row and column sharing for test plots
# matplotlib.pyplot.subplots_adjust(top=0.85)
f, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = matplotlib.pyplot.subplots(3, 2, sharex='col')
f.set_size_inches(matplotlib.pyplot.figaspect(1.2) * 1.5)
#f.suptitle("Fit for "+str(data.Desc), fontsize=14)
# This is the actual fitting
tData = data.T
tDat1 = numpy.linspace(numpy.min(tData) + 1, numpy.max(tData) - 1, 10)
Pin = 1e20 # Dummy pressure
inCP = liqObj.inCoolProp(data.Name)
print("Fluid in CoolProp: " + str(inCP))
print("")
inVal = 'D'
xData = data.rho
oldCoeffs = liqObj.getCoefficients(inVal)
newCoeffs = liqObj.fitCoefficients(inVal, T=tData, xData=xData)
# print "Density, old: "+str(oldCoeffs)
print("Density, new: " + str(newCoeffs))
# print
liqObj.setCoefficients(inVal, newCoeffs)
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tDat1])
# ax1.plot(tData-273.15, xData, 'o', label="Data Sheet")
# ax1.plot(tDat1-273.15, fData, 'o', label="Python")
# if inCP:
# Tmin = CP.PropsU('Tmin','T',0,'P',0,data.Name,"SI")
# Tmax = CP.PropsU('Tmax','T',0,'P',0,data.Name,"SI")
# tDat2 = numpy.linspace(Tmin+1, Tmax-1, 100)
# ax1.plot(tDat2-273.15, CP.PropsU(inVal, 'T', tDat2, 'P', Pin*1e3, data.Name, "SI"), label="CoolProp")
# ax12 = ax1.twinx()
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tData])
# ax12.plot(tData-273.15, relError(fData, xData, True), 'o', label="Error", alpha=0.25)
# ax12.set_ylabel(r'$\mathregular{rel.\/Error\/(\%)}$')
# ax1.set_ylabel(r'$\mathregular{Density\/(kg\/m^{-3})}$')
j['density'] = {}
j['density']['coeffs'] = liqObj.getCoefficients('D').tolist()
j['density']['type'] = 'polynomial'
inVal = 'C'
xData = data.c_p
oldCoeffs = liqObj.getCoefficients(inVal)
newCoeffs = liqObj.fitCoefficients(inVal, T=tData, xData=xData)
# print "Heat c., old: "+str(oldCoeffs)
# print "Heat c., new: "+str(newCoeffs)
# print
liqObj.setCoefficients(inVal, newCoeffs)
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tDat1])
# ax2.plot(tData-273.15, xData/1e3, 'o', label="Data Sheet")
# ax2.plot(tDat1-273.15, fData/1e3, 'o', label="Python")
# if inCP:
# ax2.plot(tDat2-273.15, CP.PropsU(inVal, 'T', tDat2, 'P', Pin*1e3, data.Name, "SI")/1e3, label="CoolProp")
# ax22 = ax2.twinx()
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tData])
# ax22.plot(tData-273.15, relError(fData, xData, True), 'o', label="Error", alpha=0.25)
# ax22.set_ylabel(r'$\mathregular{rel.\/Error\/(\%)}$')
# ax2.set_ylabel(r'$\mathregular{Heat\/Cap.\/(kJ\/kg^{-1}\/K^{-1})}$')
j['specific_heat'] = {}
j['specific_heat']['coeffs'] = liqObj.getCoefficients('C').tolist()
j['specific_heat']['type'] = 'polynomial'
inVal = 'L'
xData = data.lam
oldCoeffs = liqObj.getCoefficients(inVal)
newCoeffs = liqObj.fitCoefficients(inVal, T=tData, xData=xData)
# print "Th. Co., old: "+str(oldCoeffs)
# print "Th. Co., new: "+str(newCoeffs)
# print
liqObj.setCoefficients(inVal, newCoeffs)
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tDat1])
# ax3.plot(tData-273.15, xData*1e3, 'o', label="Data Sheet")
# ax3.plot(tDat1-273.15, fData*1e3, 'o', label="Python")
# if inCP:
# ax3.plot(tDat2-273.15, CP.PropsU(inVal, 'T', tDat2, 'P', Pin*1e3, data.Name, "SI")*1e3, label="CoolProp")
# ax32 = ax3.twinx()
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tData])
# ax32.plot(tData-273.15, relError(fData, xData, True), 'o', label="Error", alpha=0.25)
# ax32.set_ylabel(r'$\mathregular{rel.\/Error\/(\%)}$')
# ax3.set_ylabel(r'$\mathregular{Th.\/Cond.\/(mW\/m^{-1}\/K^{-1})}$')
j['conductivity'] = {}
j['conductivity']['coeffs'] = liqObj.getCoefficients('L').tolist()
j['conductivity']['type'] = 'polynomial'
inVal = 'V'
tData = data.T[data.mu_dyn > 0]
if len(tData) > liqObj._minPoints:
tDat1 = numpy.linspace(numpy.min(tData) + 1, numpy.max(tData) - 1, 10)
xData = data.mu_dyn[data.mu_dyn > 0]
oldCoeffs = liqObj.getCoefficients(inVal)
newCoeffs = liqObj.fitCoefficients(inVal, T=tData, xData=xData)
# print "Viscos., old: "+str(oldCoeffs)
# print "Viscos., new: "+str(newCoeffs)
# print
liqObj.setCoefficients(inVal, newCoeffs)
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tDat1])
# ax4.plot(tData-273.15, xData*1e3, 'o', label="Data Sheet")
# ax4.plot(tDat1-273.15, fData*1e3, 'o', label="Python")
# if inCP:
# ax4.plot(tDat2-273.15, CP.PropsU(inVal, 'T', tDat2, 'P', Pin*1e3, data.Name, "SI")*1e3, label="CoolProp")
# ax42 = ax4.twinx()
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tData])
# ax42.plot(tData-273.15, relError(fData, xData, True), 'o', label="Error", alpha=0.25)
# ax42.set_ylabel(r'$\mathregular{rel.\/Error\/(\%)}$')
# ax4.set_ylabel(r'$\mathregular{Dyn.\/Viscosity\/(mPa\/s)}$')
# ax4.set_yscale('log')
j['viscosity'] = {}
j['viscosity']['coeffs'] = liqObj.getCoefficients('V').tolist()
j['viscosity']['type'] = 'polynomial'
inVal = 'Psat'
mask = numpy.logical_and(numpy.greater_equal(data.T, data.TminPsat), numpy.greater(data.psat, 0))
tData = data.T[mask]
if len(tData) > liqObj._minPoints:
tDat1 = numpy.linspace(numpy.min(tData) + 1, numpy.max(tData) - 1, 10)
xData = data.psat[mask]
oldCoeffs = liqObj.getCoefficients(inVal)
newCoeffs = liqObj.fitCoefficients(inVal, T=tData, xData=xData)
# print "P sat. , old: "+str(oldCoeffs)
# print "P sat. , new: "+str(newCoeffs)
# print
liqObj.setCoefficients(inVal, newCoeffs)
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tDat1])
# ax5.plot(tData-273.15, xData/1e3, 'o', label="Data Sheet")
# ax5.plot(tDat1-273.15, fData/1e3, 'o', label="Python")
# if inCP:
# ax5.plot(tDat2-273.15, CP.PropsU(inVal, 'T', tDat2, 'P', Pin*1e3, data.Name, "SI")/1e3, label="CoolProp")
# ax52 = ax5.twinx()
# fData = numpy.array([liqObj.Props(inVal, T=Tin, P=Pin) for Tin in tData])
# ax52.plot(tData-273.15, relError(fData, xData, True), 'o', label="Error", alpha=0.25)
# ax52.set_ylabel(r'$\mathregular{rel.\/Error\/(\%)}$')
#
# ax5.set_ylabel(r'$\mathregular{Vap.\/Pressure\/(kPa)}$')
# ax5.set_yscale('log')
#
# ax5.set_xlabel(ur'$\mathregular{Temperature\/(\u00B0C)}$')
# ax6.set_xlabel(ur'$\mathregular{Temperature\/(\u00B0C)}$')
j['saturation_pressure'] = {}
j['saturation_pressure']['coeffs'] = np.array(liqObj.getCoefficients('Psat')).tolist()
j['saturation_pressure']['type'] = 'polynomial'
#x5min,x5max = ax5.get_xlim()
#x6min,x6max = ax6.get_xlim()
#xmin, xmax = (numpy.min([x5min,x6min]),numpy.max([x5max,x6max]))
#x3min,x3max = ax3.get_xlim()
#x4min,x4max = ax4.get_xlim()
#xmin, xmax = (numpy.min([x3min,x4min]),numpy.max([x3max,x4max]))
#x1min,x1max = ax1.get_xlim()
#x2min,x2max = ax2.get_xlim()
#xmin, xmax = (numpy.min([x1min,x2min]),numpy.max([x1max,x2max]))
#xmin, xmax = (-10,30)
#
# xmin = numpy.round(numpy.min(data.T)-273.15-5, -1)
# xmax = numpy.round(numpy.max(data.T)-273.15+5, -1)
#
# ax5.set_xlim([xmin,xmax])
# ax6.set_xlim(ax5.get_xlim())
#
# ax5.xaxis.set_major_locator(MaxNLocator(5))
# ax6.xaxis.set_major_locator(ax5.xaxis.get_major_locator())
#
# tData = numpy.array(data.Tmin + (data.Tmax-data.Tmin)/2.)
# xData = numpy.array(1)
# ax6.plot(tData-273.15, xData, 'o', label="Data Sheet")
# ax6.plot(tData-273.15, xData, 'o', label="Python")
# if inCP:
# ax6.plot(tData-273.15, xData, label="CoolProp")
# ax6.legend(loc=1)
# ax6.text(tData-273.15, xData*1.005, 'Fits for '+str(data.Name),
# verticalalignment='top', horizontalalignment='center',
# backgroundcolor='white', fontsize=18)
# matplotlib.pyplot.tight_layout()
# matplotlib.pyplot.savefig("fit_current_std.pdf")
# #TODO Remove for normal fitting
# matplotlib.pyplot.savefig("fit_"+data.Name+"_std.pdf")
# ### Print the output for the C++ file
# print "name = std::string(\""+data.Name+"\");"
# print "description = std::string(\""+data.Desc+"\");"
# print "reference = std::string(\"\");"
# print ""
# print "Tmin = "+str(data.Tmin)+";"
# print "Tmax = "+str(data.Tmax)+";"
# print "TminPsat = "+str(data.TminPsat)+";"
# print ""
# print "cRho.clear();"
# C = liqObj.getCoefficients('D')
# for Ci in C:
# print "cRho.push_back(%+1.10E);" %(Ci)
#
# print ""
# print "cHeat.clear();"
# C = liqObj.getCoefficients('C')
# for Ci in C:
# print "cHeat.push_back(%+1.10E);" %(Ci)
#
# print ""
# print "cCond.clear();"
# C = liqObj.getCoefficients('L')
# for Ci in C:
# print "cCond.push_back(%+1.10E);" %(Ci)
#
# print ""
# print "cVisc.clear();"
# C = liqObj.getCoefficients('V')
# for Ci in C:
# print "cVisc.push_back(%+1.10E);" %(Ci)
#
# print ""
# print "cPsat.clear();"
# C = liqObj.getCoefficients('Psat')
# for Ci in C:
# print "cPsat.push_back(%+1.10E);" %(Ci)
#
# raw_input("Finished with "+data.Name+", press Enter to continue...")
import json
print(json.dumps(j, indent=2))
fp = open(j['name'] + '.json', 'w')
fp.write(json.dumps(j, indent=2, sort_keys=True))
fp.close()