CMSF Assigment Solved
CMSF Assigment Solved
import numpy as np
import math as m
import matplotlib.pyplot as plt
#defining function
def g(fe , k , re): #k = e/D
return (1/mt.sqrt(fe) + 2*mt.log10(k/3.7 + 2.51/(re*mt.sqrt(fe))))
for k in K:
#defining array to store values of fe and re to plot them
Re = np.zeros(90)
Fe = np.zeros(90)
counter = 0
for re in range(10000 , 100000 , 1000):
a = 0.02
b = 0.05
while abs(a-b) > tol :
c = (a+b)/2
if g(a,k,re)*g(c,k,re) < 0 :
b=c
else :
a=c
Re[counter] = re
Fe[counter] = (a+b)/2
counter = counter + 1
import numpy as np
import math as mt
import matplotlib.pyplot as plt
#defining functions
def g(fe,re,k):
return (1/mt.sqrt(fe) + 2*mt.log10(k/3.7 + 2.51/(re*mt.sqrt(fe))))
def g_1(fe,re,k):
return -1/(2*fe*mt.sqrt(fe)) - 2/(mt.log10(1.26/(k/3.7 + 2.51/(re*mt.sqrt(fe)))))
tol = 0.0000001
for k in K :
counter = 0
for re in range(10000 , 100000 , 1000):
x0 = 0.02 #initial guess
x = 0.01 #varriable initialization
m=0
while abs(x-x0) > tol :
x = x0 - g(x0,re,k)/g_1(x0,re,k)
m=m +1
if m == 200 : # to ensure the code dosnt run infinetly
break
Re[counter] = re
Fe[counter] = x
counter = counter + 1
plt.plot(Re,Fe,label = 'k = {:.03f}'.format(k))
import math as m
import numpy as np
import matplotlib.pyplot as plt
#parameters
dx = length /(nodes - 1)
#creating arrays
T = np.zeros(nodes) #for tempreture
B = np.zeros(nodes) #for constants
B[1:-1] = -g*dx**2/k
B[0] = T_left
B[-1] = T_right
#A matrix
A = np.zeros((nodes,nodes))
A[0,0] = A[-1,-1] = 1
for i in range(1,nodes-1):
A[i][i] = -2
A[i][i-1] = A[i][i+1] = 1
#finding x at T_max
for i in range(0,nodes):
if T[i] == np.max(T):
break
X = np.linspace(0,length, nodes)
plt.figure()
plt.xlabel("length")
plt.ylabel("tempreture")
plt.title("1D_FDM_heat generation")
plt.plot(X,T)
plt.show()
ASSIGNMENT 2 :
Q2) 2D Steady state with heat generation using FDM
import math as m
import numpy as np
import matplotlib.pyplot as plt
#parameters
dx = length /(nodes - 1)
dy = length /(nodes -1 )
#constants
k1 = -2*(dx**2 + dy**2)
k2 = dy**2
k3 = dx**2
heatgen = -g*(dx**2 )*( dy**2) /k
#creating arrays
T = np.zeros(nodes*nodes) #for tempreture
B = np.zeros(nodes*nodes) #for constants
B[:] = heatgen
#A matrix
A = np.zeros((nodes*nodes , nodes*nodes))
for g in range(0,nodes):
for k in range(0,nodes):
if g == 0 or g == nodes-1 :
A[g*nodes + k ][g*nodes + k] = 1
continue
A[g*nodes + k][g*nodes + k ] = k1
A[g*nodes + k][g*nodes + k - 1] = A[g*nodes + k][g*nodes + k + 1] = k3
A[g*nodes + k][g*nodes + k + nodes] = A[g*nodes + k][g*nodes + k - nodes] = k2
#creating B matrix
for g in range(0,nodes):
for k in range(0,nodes):
if g==0 : #asigned values of top tempreture
B[g*nodes + k] = T_up
continue
if k == 0 :
B[g*nodes + k] = T_left #assigned values for left boundary
continue
if k == (nodes - 1):
B[g*nodes + k ] = T_right #assigned values for rigth boundary
continue
plt.figure()
CS = plt.contour(X, Y, T_final , cmap = plt.cm.inferno)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('2D_steady_heat_genration')
plt.legend()
plt.show()
ASSIGNMENT 3 :
Q1) 1) 1D usteady heat diffusion using explicit scheme
import numpy as np
import matplotlib.pyplot as plt
#establishing data
dx = length/(nodes - 1)
dt = 0.5*dx**2 / a
#tempreture arrays
T_im = np.zeros(nodes)
T_exp = np.zeros(nodes)
#Solving functions
counter = 1
counter_2 = 1
m=0
ASSIGNMENT 3:
Q1) 2) 1D Unsteady heat diffusion using implicit scheme
import numpy as np
import matplotlib.pyplot as plt
#establishing data
dx = length/(nodes - 1)
dt = 0.5*dx**2 / a
#tempreture arrays
T_im = np.zeros(nodes)
T_exp = np.zeros(nodes)
#definig A matrix
A = np.zeros((nodes, nodes))
for i in range(1,nodes -1):
A[i , i-1] = -s
A[i,i] = 1+2*s
A[i,i+1] = -s
A[0,0] = 1
A[nodes - 1 , nodes - 1] = 1
#definig B matrix
B = np.zeros(nodes)
#Solving functions
counter = 1
counter_2 = 1
m=0
T_im = np.linalg.solve(A,B)
#plot design
plt.plot(x,T_im ,label = 'time = {:.02f}s'.format(counter*dt) )
plt.title("1D_Unsteady_FDM_implicit scheme")
plt.xlabel("length")
plt.ylabel("tempreture")
plt.legend()
plt.show()
ASSIGNMENT 3 :
Q2) 2D Unsteady heat diffusion using explicit scheme
import numpy as np
import matplotlib.pyplot as plt
length = 50 #mm
nodes = 15
a = 100
time = 100 #sec
tol = 0.0001
#establishing data
dx = length/(nodes-1)
dy = length/(nodes-1)
dt = min(dx**2/(4*a), dy**2/(4*a) ) #this condition prevent explicit code from breaking
T = np.zeros((nodes,nodes))#array to store tempreture
counter = 0
if np.max(np.abs(T - w)) < tol : #to detect steady state we compare tol with max difference between
tempreture
#of current and precious time state
print("steady state reached")
print("avg temp = {:.03f} , time = {:.02f}".format(np.average(T) , counter))
break #exits the while loop
#ploting
#making arrays for x and y
X = np.linspace(0,length,nodes)
Y = np.linspace(0,length,nodes)
plt.figure()
CS = plt.contour(X, Y, T , cmap = plt.cm.inferno)
plt.colorbar()
plt.clabel(CS, inline=1, fontsize=10)
plt.xlabel("length in x (mm) ")
plt.ylabel("length in y (mm)")
plt.title('2D_unsteady_FDM_explicit')
plt.show()
ASSIGNMENT 4 :
Q1) 1D Steady state heat diffusion using FVM
import numpy as np
import matplotlib.pyplot as plt
#parameters
dx = len/nodes
aw = k/dx
ae = k/dx
#internal nodes
Su_i = S*dx
ap_i = aw + ae
ap = aw + ae
#generating matrix
#A matrix
A = np.zeros((nodes,nodes))
for i in range(1,nodes-1):
A[i][i] = ap
A[i][i-1] = A[i][i+1] = -ae
A[0][0] = ap_left
A[-1][-1] = ap_right
A[0][1] = A[-1][-2] = -ae
#constant matrix
B = np.zeros(nodes)
B[:] = Su_i
B[0] = Su_left
B[-1] = Su_right
X = np.zeros(nodes + 2 )
X[1] = dx/2
for i in range(2,nodes+1):
X[i] = dx + X[i-1]
print(X)
print(T_final)
plt.plot(X,T_final)
plt.show()