In [1]:
import os, sys, inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
if os.path.basename(currentdir) == 'notebooks':
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
elif os.path.basename(currentdir) == 'celldeposit-condenser':
currentdir = os.path.join(currentdir, 'notebooks')
os.chdir(currentdir)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from water_at_saturation_properties import density, enthalpy, heat_capacity, conductivity, viscosity
from water_at_saturation_properties import saturation_temperature, vapour_density, vaporization_enthalpy
In [2]:
def simplified_model_for_fit(x, a00, a01, a02, a03):
c = np.array([a00, a01, a02, a03,])
return simplified_model(x, c)
In [3]:
def simplified_model(x,c):
f = 0.
for i in range(c.shape[0]):
f += c[i] * (x ** i)
return f
In [4]:
def calculate_output(func,X,*args):
Z = np.zeros_like(X)
for i in range(X.shape[0]):
Z[i,] = func(X[i],*args)
return Z
In [5]:
def fit(model, X):
Z = calculate_output(model,X)
c, pcov = curve_fit(simplified_model_for_fit, X, Z)
Z_fit = simplified_model(X,c)
# residual sum of squares
ss_res = np.sum((Z - Z_fit) ** 2)
# total sum of squares
ss_tot = np.sum((Z - np.mean(Z)) ** 2)
# r-squared
r2 = 1 - (ss_res / ss_tot)
return (c, Z_fit, Z, r2)
In [6]:
from iapws.iapws97 import _Region4, _Region1
from iapws._iapws import _ThCond, _Viscosity
In [7]:
limits = [-1, 0]
npoints = 25
X = 1e5*np.logspace(limits[0], limits[1], num=npoints)
print(X)
out = fit(vapour_density, X)
print(out)
In [8]:
print("VAPOUR DENSITY")
c, Z_fit, Z, r2 = fit(vapour_density, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict = dict()
c_dict['vapour_density'] = c
In [9]:
print("VAPOUR TOTAL COMPRESSIBILITY")
c_v_density = c_dict['vapour_density']
c = np.zeros((c_v_density.shape[0]-1,))
i = 0
for ci in c_v_density:
if i > 0:
c[i-1] = i*ci
i += 1
Z_fit = simplified_model(X,c)
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c_v_density)
print("Parameters:",c)
print("R2:", r2)
c_dict['vapour_total_compressibility'] = c
In [10]:
print("SATURATION TEMPERATURE")
c, Z_fit, Z, r2 = fit(saturation_temperature, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['saturation_temperature'] = c
In [11]:
print("DENSITY")
c, Z_fit, Z, r2 = fit(density, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['density'] = c
In [12]:
print("ENTHALPY")
c, Z_fit, Z, r2 = fit(enthalpy, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['enthalpy'] = c
In [13]:
print("VISCOSITY")
c, Z_fit, Z, r2 = fit(viscosity, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['viscosity'] = c
In [14]:
print("HEAT_CAPACITY")
c, Z_fit, Z, r2 = fit(heat_capacity, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['heat_capacity'] = c
In [15]:
print("CONDUCTIVITY")
c, Z_fit, Z, r2 = fit(conductivity, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['conductivity'] = c
In [16]:
print("VAPORIZATION ENTHALPY")
c, Z_fit, Z, r2 = fit(vaporization_enthalpy, X)
plt.plot(X, Z, 'r') # plotting t, a separately
plt.plot(X, Z_fit, 'b') # plotting t, b separately
plt.show()
print("Parameters:",c)
print("R2:", r2)
c_dict['vaporization_enthalpy'] = c
In [17]:
import pprint
pprint.pprint(c_dict)
In [ ]:
In [ ]: