0% found this document useful (0 votes)
35 views15 pages

CMSF Assigment Solved

The document contains solutions to multiple numerical problems using different methods like bisection, Newton-Raphson, explicit and implicit finite difference, and finite volume. The problems include 1D and 2D steady and unsteady heat diffusion with and without internal heat generation.

Uploaded by

Pranav Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views15 pages

CMSF Assigment Solved

The document contains solutions to multiple numerical problems using different methods like bisection, Newton-Raphson, explicit and implicit finite difference, and finite volume. The problems include 1D and 2D steady and unsteady heat diffusion with and without internal heat generation.

Uploaded by

Pranav Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ASSIGNMENT 1 :

Q1) fe – Re plot using Bisection method

import numpy as np
import math as m
import matplotlib.pyplot as plt

#taking initial range


a = 0.02
b = 0.05
tol = 0.00000000001

#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))))

#solving using bisection algortithm


plt.figure()
K = np.arange(0.001,0.01,0.001)
print(K)

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

plt.plot(Re,Fe, label = 'k = {:.03f}'.format(k))


plt.xlabel("Reynolds Number")
plt.ylabel("friction factor")
plt.legend()
plt.show()
ASSIGNMENT 1:
Q2) fe-Re Plot using Newton-Rapson method

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

#solving using NR method


K = np.arange(0.001,0.01,0.001)
Re = np.zeros(90)
Fe =np.zeros(90)
plt.figure()

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))

plt.xlabel("Reynolds number ")


plt.ylabel("friction factor ")
plt.legend()
plt.show()
ASSIGNMENT 2 :
Q1) 1D Steady state with heat generation using FDM

import math as m
import numpy as np
import matplotlib.pyplot as plt

#initializing the problem


length = 0.03 #m
nodes = 10
g = 80000000 #W/m^3 internal heat generation at 10mm from left end
k = 200
T_left = 180 # in deg C
T_right = 120

#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

#solving the equation


T = np.linalg.solve(A,B)
print("T_max = ",np.max(T))
print(T)

#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

#initializing the problem


length = 1 #m
nodes = 5
g = 1830000 #W/m^3 internal heat generation at 10mm from left end
k = 366
T_left = 400 # in deg C
T_right = 200
T_up = 100
T_down = 300

#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))

#counters for row identification


k=0
g=0

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

if (k == 0 or k == (nodes -1)) and (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 g== (nodes - 1) :#asigned values of bottom tempreture


B[g*nodes + k ] = T_down
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

#solving the equation


T = np.linalg.solve(A,B)
print("T_max = ",np.max(T))
T_final = T.reshape((nodes,nodes))
print(T_final)

#ploting the graph


X = np.linspace(0 , length , nodes )
Y = np.linspace(0 , length , nodes)

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

#defining the problem


length = 100 #mm
nodes = 50
a = 150 #alpha = thermal diffusivity
time = 10000 #sec
tol= 0.000001

#establishing data
dx = length/(nodes - 1)
dt = 0.5*dx**2 / a

#making array for x


x = np.linspace(0 , length , nodes )
s = a*dt/dx**2
error = np.zeros(time) #array to store error
iteration = np.arange(0,time*dt,dt) #array of iteration

#tempreture arrays
T_im = np.zeros(nodes)
T_exp = np.zeros(nodes)

#initializing the problem


T_im[0] = T_exp[0] = 0
T_im[-1] = T_exp[-1] = 100

#Solving functions
counter = 1
counter_2 = 1
m=0

while counter < time :


#making explicit tempreture array
w = T_exp.copy()
for i in range(1 , nodes - 1 ):
T_exp[i] = a*dt*(w[i+1] + w[i-1] - 2*w[i])/dx**2 + w[i]

#ploting at some time intervals


if counter == counter_2 :
plt.plot(x,T_exp , label = 'time = {:.03f}s'.format(counter*dt))
plt.pause(0.5)
counter_2 = counter_2 + 2*counter

#end computation once steady state achieved


if np.max(np.abs(T_exp-w)) < tol :
print("steady state reached , time = {:.02f}s".format(counter*dt))
m=1
counter = counter + 1
if m == 1 :
break

#displaying the plot


plt.xlabel("length")
plt.ylabel("tempreture")
plt.title("1D_unsteady_FDM_explicit scheme")
plt.legend()
plt.show()

ASSIGNMENT 3:
Q1) 2) 1D Unsteady heat diffusion using implicit scheme

import numpy as np
import matplotlib.pyplot as plt

#defining the problem


length = 100 #mm
nodes = 50
a = 150 #alpha = thermal diffusivity
time = 10000 #sec
tol= 0.000001

#establishing data
dx = length/(nodes - 1)
dt = 0.5*dx**2 / a

#making array for x


x = np.linspace(0 , length , nodes )
s = a*dt/dx**2
error = np.zeros(time) #array to store error
iteration = np.arange(0,time*dt,dt) #array of iteration

#tempreture arrays
T_im = np.zeros(nodes)
T_exp = np.zeros(nodes)

#initializing the problem


T_im[0] = T_exp[0] = 0
T_im[-1] = T_exp[-1] = 100

#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

while counter < time :


#making implicit tempreture array
B = T_im.copy()

T_im = np.linalg.solve(A,B)

#ploting the graphs


if counter == counter_2 :
plt.plot(x,T_im ,label = 'time = {:.02f}s'.format(counter*dt) )
plt.pause(0.05)
counter_2 = counter_2 + 2*counter

#end computation once steady state achieved


if np.max(np.abs(T_im - B)) < tol :
print("steady state reached , time = {:.02f}".format(counter*dt))
m=1
counter = counter + 1
if m == 1 :
break

#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

#defining the problem

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

#initializing the problem


T[0,:] = 700

#solving the equation

counter = 0

while counter < time :


w = T.copy() #an array w is created to store the values of tempreture of previous time step

for i in range(1 , nodes - 1 ):


for j in range(1 , nodes -1 ):
dd_y = (w[i,j+1] +w[i,j-1] - 2*w[i,j])/ dy**2
dd_x = (w[i+1,j] +w[i-1,j] - 2*w[i,j])/ dx**2
T[i,j] = w[i,j] + a*dt*(dd_x + dd_y) #discritized governing equation
counter += dt#increasing counter by dt

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

#initialize the problem


len = 0.03
nodes = 5
S = 80*10**6
k = 200
T_left = 180
T_right = 120

#parameters
dx = len/nodes
aw = k/dx
ae = k/dx

#internal nodes
Su_i = S*dx
ap_i = aw + ae
ap = aw + ae

#left boundry node (node = 1)


Sp = -2*k/dx
ap_left = ae - Sp
Su_left = S*dx + 2*k*T_left/dx

#right boundry node (node = 5)


Su_right = S*dx + 2*k*T_right/dx
ap_right = aw - Sp

#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

#solving for steady state


T = np.linalg.solve(A,B)
print("max tempreture = {:.02f}".format(np.max(T)))
T_final = np.zeros(nodes + 2)
T_final[0] = T_left
T_final[-1] = T_right
T_final[1:nodes+1] = T.copy()

X = np.zeros(nodes + 2 )
X[1] = dx/2
for i in range(2,nodes+1):
X[i] = dx + X[i-1]

X[-1] = X[-2] + dx/2

print(X)
print(T_final)
plt.plot(X,T_final)
plt.show()

You might also like