Here is my code simulating liquid in a pipe cooling under different conditions. How did I do approaching this in an object orientated way? Is there any way I can improve this code?
import numpy as np
import scipy
from scipy.optimize import minimize
from matplotlib import pyplot as plt
class pipeSimulator:
R_w = 0.654# W/mk water heat loss ocefficient
# R_w = 0.028# W/mk air heat loss ocefficient
R_f = 0.042 # W/mk pipe heat loss coefficient
inner_radius = 0.2 # m pipe radius
T_0 = 50 # C initial temperature
T_a = 20 # C ambiant temperature
dx = 0.01 # m differential distance
Length = 25 # m length of pipe
def heat_balance(self,T_1,T_2):
heat_in = (T_1-T_2) * scipy.pi * self.inner_radius**2 * self.R_w
heat_out = ( (T_1 + T_2) / 2 - self.T_a ) * 2 * self.inner_radius * scipy.pi * self.dx * self.R_f
return abs(heat_in-heat_out)
def simulation(self):
T = [self.T_0]
X = [0]
for x in np.arange(self.dx,self.Length,self.dx):
X.append(x)
fun = lambda s: self.heat_balance(T[-1],s)
T.append(float(minimize(fun,T[-1]-self.dx,method="SLSQP")['x']))
return([T,X])
trial = pipeSimulator()
trial.Length = 30
diameters = [0.02,.2,1,3]
R_values = [100,400]
T_a_values = [20]
cols = ['red', 'blue', 'black', 'purple', 'brown']
mylines = ['-','--','-.',':']
i=0
labs = []
for rval in R_values:
trial.R_f = 1.0/rval
for dval in diameters:
trial.inner_radius = dval/2.0
for tval in T_a_values:
trial.T_a = tval
ans = trial.simulation()
plt.plot(ans[1],ans[0],c=cols[i%len(cols)],ls=mylines[i%len(mylines)],lw=2*(i%len(mylines)+1))
labs.append(str(rval)+' R-value (EU), '+str(dval)+' meter diameter, '+str(tval)+" degree C ambient")
i+=1
plt.legend(fontsize=20,title='',labels=labs,prop={'size':12},loc=3)
plt.xlabel('Distance (m)',fontsize=20)
plt.ylabel('Average Temperature (C)',fontsize=20)
plt.show()