Skip to content

Instantly share code, notes, and snippets.

@j-c-cook
Last active October 24, 2022 22:43
Show Gist options
  • Save j-c-cook/89c69c16ee2683c80cd5e1d08cb94859 to your computer and use it in GitHub Desktop.
Save j-c-cook/89c69c16ee2683c80cd5e1d08cb94859 to your computer and use it in GitHub Desktop.
Validation of SecondaryCoolProps to Coolprop for use cases in pygfunction
import pygfunction as gt
import scp
import matplotlib.pyplot as plt
import numpy as np
import os
def make_dir_if_not(path_to_dir: str):
if not os.path.exists(path_to_dir):
os.makedirs(path_to_dir)
def setup_meshgrid(x_min, x_max, T_min, T_max, num=10):
concentrations: np.array = np.linspace(x_min, x_max, num=num)
temperatures: np.array = np.linspace(T_min, T_max, num=num)
xv, yv = np.meshgrid(concentrations, temperatures, indexing='ij')
return xv, yv
def compute_coolprop(mixer, xv, yv):
rho_cp = np.zeros_like(xv)
mu_cp = np.zeros_like(xv)
cp_cp = np.zeros_like(xv)
k_cp = np.zeros_like(xv)
Pr_cp = np.zeros_like(xv)
for i in range(len(xv)):
for j in range(len(xv[i])):
fluid = gt.media.Fluid(mixer, xv[i, j] * 100., T=yv[i, j])
rho_cp[i, j] = fluid.rho
mu_cp[i, j] = fluid.mu
cp_cp[i, j] = fluid.cp
k_cp[i, j] = fluid.k
Pr_cp[i, j] = fluid.Pr
return rho_cp, mu_cp, cp_cp, k_cp, Pr_cp
def compute_scp(mixer, xv, yv):
rho_scp = np.zeros_like(xv)
mu_scp = np.zeros_like(xv)
cp_scp = np.zeros_like(xv)
k_scp = np.zeros_like(xv)
Pr_scp = np.zeros_like(xv)
for i in range(len(xv)):
for j in range(len(xv[i])):
fluid = scp.fluid.Fluid(mixer, xv[i, j])
T = yv[i, j]
rho_scp[i, j] = fluid.density(T)
mu_scp[i, j] = fluid.viscosity(T)
cp_scp[i, j] = fluid.specific_heat(T)
k_scp[i, j] = fluid.conductivity(T)
Pr_scp[i, j] = fluid.prandtl(T)
return rho_scp, mu_scp, cp_scp, k_scp, Pr_scp
def _compute_error(predictions, targets):
_err = np.zeros_like(predictions)
for i in range(len(predictions)):
for j in range(len(predictions[i])):
# Calculates the percent error
p = predictions[i, j]
t = targets[i, j]
_err[i, j] = (p - t) / t * 100.
return _err
def calculate_errors(cp, sec_cp):
errors = []
for i in range(len(cp)):
errors.append(_compute_error(sec_cp[i], cp[i]))
return errors
def main():
# Ethyl alcohol - 'MEA' - Ethanol mixed with water
mixes = {'MEA': {'x_min': 0.0001, 'x_max': 0.6, 'T_min': 0.1, 'T_max': 40.},
'MEG': {'x_min': 0.0001, 'x_max': 0.6, 'T_min': 0.1, 'T_max': 40.},
'MPG': {'x_min': 0.0001, 'x_max': 0.6, 'T_min': 0.1, 'T_max': 40.},
'MMA': {'x_min': 0.0001, 'x_max': 0.6, 'T_min': 0.1, 'T_max': 40.}}
for mixer in mixes:
mixer_ranges = mixes[mixer]
xv, yv = setup_meshgrid(**mixer_ranges, num=100)
# Calculate the coolprop values
cp = compute_coolprop(mixer, xv, yv)
# Calculate the secondary coolant property values
sec_cp = compute_scp(mixer, xv, yv)
# Calculate the RMSE for each of the thermal properties
errors = calculate_errors(cp, sec_cp)
properties = ['rho', 'mu', 'cp', 'k', 'Pr']
make_dir_if_not(mixer)
summary = f'{mixer} max abs errors: '
# Create contour plots of all the errors
for i in range(len(properties)):
fig = gt.utilities._initialize_figure()
cs = plt.contourf(xv, yv, errors[i])
plt.title(f'{mixer} - {properties[i]}')
plt.xlabel('Concentration')
plt.ylabel('Temperature ($\degree$C)')
cbar = plt.colorbar(cs)
cbar.ax.set_ylabel('Error (%)')
fig.savefig(os.path.join(mixer, properties[i] + '.png'))
plt.close(fig)
summary += f'{properties[i]} : {float(np.max(np.abs(errors[i]))):.2f}% '
print(summary)
if __name__ == '__main__':
main()
pygfunction==2.2.1
git+https://github.com/j-c-cook/SecondaryCoolantProps@9499c31af994fadd4760ef5bb3e810f09faa4a50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment