0% found this document useful (0 votes)
8 views22 pages

dINESH AHMT Assignment 1

The document presents a computer assignment for an advanced heat and mass transfer course, detailing the implementation of Python code to solve temperature distribution problems using numerical methods. It includes solutions for steady-state and unsteady two-dimensional heat transfer, comparing numerical results with analytical solutions, and visualizing temperature distributions through contour plots. The results show that the numerical solutions closely match the analytical solutions, with minor discrepancies that can be reduced by refining the mesh size.
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)
8 views22 pages

dINESH AHMT Assignment 1

The document presents a computer assignment for an advanced heat and mass transfer course, detailing the implementation of Python code to solve temperature distribution problems using numerical methods. It includes solutions for steady-state and unsteady two-dimensional heat transfer, comparing numerical results with analytical solutions, and visualizing temperature distributions through contour plots. The results show that the numerical solutions closely match the analytical solutions, with minor discrepancies that can be reduced by refining the mesh size.
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/ 22

ME5101 – Advanced Heat and Mass Transfer

Computer Assignment – 1

Name – DINESH ARAVINDHAN R


Roll No- ME24M026

Question 1:

Solution:

a. Python Code for determining the temperature at nodes 1, 2, 3 and 4

import numpy as np
import matplotlib.pyplot as plt

# Domain parameters
L = 1.0
H = 0.5
nx = 4
ny = 4
dx = L / (nx - 1)
dy = H / (ny - 1)

# Initialize temperature array


temp = np.ones((ny, nx))

# Apply boundary conditions (edges only, exclude corners)


temp[0, :] = 100 # Top edge
temp[-1, :] = 300 # Bottom edge
temp[:, 0] = 50 # Left edge
temp[:, -1] = 200 # Right edge

# Convergence criteria settings


tolerance = 1e-5
change = 1 # Initial error

# Iterative computation loop


iterations = 0

while change > tolerance:


iterations += 1
temp_old = temp.copy()
beta_sq = (dy / dx) ** 2 # Aspect ratio factor

# Update interior nodes using finite difference method


for i in range(1, ny - 1):
for j in range(1, nx - 1):
temp[i, j] = ((beta_sq * (temp[i, j - 1] + temp[i, j + 1])) +
(temp[i - 1, j] + temp[i + 1, j])) / (2 * (1 + beta_sq))

# Calculate maximum change between iterations


change = np.max(np.abs(temp - temp_old))

# Display the final temperature values


print("Temperature at each node after convergence:")
for row in temp:
print(" ".join(f"{val:.1f}" for val in row))

# Given neighboring temperatures for the midpoint calculation


node1 = 143.52 # Temperature above the midpoint
node2 = 164.95 # Temperature to the left of the midpoint
node3 = 205.05 # Temperature to the right of the midpoint
node4 = 226.48 # Temperature below the midpoint

# Compute the midpoint temperature as the average of the four neighbors


avg_temp_mid = (node1 + node2 + node3 + node4) / 4

print(f"\nMidpoint temperature (average of neighbors): {avg_temp_mid:.1f}")

OUTPUT OF THE CODE:


Temperature at each node after convergence:
50.0 100.0 100.0 200.0
50.0 143.5 164.9 200.0
50.0 205.1 226.5 200.0
50.0 300.0 300.0 200.0

Midpoint temperature (average of neighbors): 185.0

b. Reducing the mesh size by a factor of 2,

To refine the grid, you doubled the number of grid points in both dimensions:

 nx =2×3+1=7

 ny =2×3+1=7

So we, changed the the nx and ny value in the previous code to get the coarser grid and
the run the code to get the temperature values at the nodes.

(Now, nx=7 and ny=7)


CODE:

import numpy as np
import matplotlib.pyplot as plt

# Domain parameters (same physical size, but finer mesh)


length = 1.0
height = 0.5
nx_points = 7
ny_points = 7
dx = length / (nx_points - 1)
dy = height / (ny_points - 1)

# Initialize temperature array


temp = np.ones((ny_points, nx_points))

# Apply boundary conditions (edges only, exclude corners)


temp[0, :] = 100 # Top edge
temp[-1, :] = 300 # Bottom edge
temp[:, 0] = 50 # Left edge
temp[:, -1] = 200 # Right edge

# Convergence criteria settings


tolerance = 1e-5
change = 1 # Initial error

# Iterative computation loop


iterations = 0

while change > tolerance:


iterations += 1
temp_old = temp.copy()
beta_sq = (dy / dx) ** 2 # Aspect ratio factor

# Update interior nodes using finite difference method


for i in range(1, ny_points - 1):
for j in range(1, nx_points - 1):
temp[i, j] = ((beta_sq * (temp[i, j - 1] + temp[i, j + 1])) +
(temp[i - 1, j] + temp[i + 1, j])) / (2 * (1 + beta_sq))

# Calculate maximum change between iterations


change = np.max(np.abs(temp - temp_old))

# Display the final temperature values


print("Temperature at each node after convergence:")
for row in temp:
print(" ".join(f"{val:.1f}" for val in row))

# Midpoint temperature taken as the temperature of the middle node


mid_x = nx_points // 2
mid_y = ny_points // 2
mid_temp = temp[mid_y, mid_x] # Temperature of the middle node

print(f"\nMidpoint temperature (temperature of the middle node): {mid_temp:.1f}")

# Plotting the isotherms


x = np.linspace(0, length, nx_points)
y = np.linspace(0, height, ny_points)
X, Y = np.meshgrid(x, y)

plt.figure(figsize=(10, 8))

# Fill contours with specified limits


contour_filled = plt.contourf(X, Y, temp, levels=np.linspace(50, 300, 21), cmap='Purples',
alpha=0.8)
# Plot the specific isotherms
contour_isotherms = plt.contour(X, Y, temp, levels=[75, 150, 250], colors='blue',
linestyles='solid', linewidths=2)
plt.clabel(contour_isotherms, inline=True, fontsize=10, fmt='%1.0f °C', colors='black')

plt.title('Temperature Distribution with Isotherms at 75°C, 150°C, and 250°C')


plt.xlabel('Length (m)')
plt.ylabel('Height (m)')
plt.xlim(0, length)
plt.ylim(0, height)

# Invert the y-axis


plt.gca().invert_yaxis()

plt.grid()
plt.colorbar(contour_filled, label='Temperature (°C)')
plt.show()

OUTPUT OF THE CODE:

Temperature at each node after convergence:

50.0 100.0 100.0 100.0 100.0 100.0 200.0


50.0 103.1 121.6 129.1 133.2 142.6 200.0
50.0 114.9 146.0 159.0 165.1 173.1 200.0
50.0 135.2 174.9 190.6 196.6 198.9 200.0
50.0 166.8 209.8 224.7 228.9 225.0 200.0
50.0 216.9 251.8 261.4 263.3 256.3 200.0
50.0 300.0 300.0 300.0 300.0 300.0 200.0

Midpoint temperature (temperature of the middle node): 190.6


c. Temperature distribution:
ANALYTICAL SOLUTION
So, now we will compare the analytical solution with the numerical solution with
the coarser grid.

Analytical Solution Numerical solution

T1 146.7275 143.5165

T2 164.623 164.9451

T3 210.9705 205.0549

T4 228.866 226.4835

Tmid-point 187.797 185.02

So, we can see that the analytical solution and the numerical solution are almost
the same. The slight difference between two solutions can be reduced by mesh
size.
Question 2:

Solution :

Code for the unsteady two dimensional problem

import numpy as np
import matplotlib.pyplot as plt

# Given data
L=1
H = 0.5
nx = 4
ny = 4
dx = L / (nx - 1)
dy = H / (ny - 1)
alpha = 10**-4
dt = 0.1 # time step size in seconds
F = (alpha * dt) / (dx * dy) # Fourier number
beta = dy / dx

# Boundary condition
T = 400 * np.ones((ny, nx)) # temperature array
# Set boundary temperatures (except the corner nodes)
T[0, :] = 100 # top boundary
T[-1, :] = 300 # bottom boundary
T[:, 0] = 50 # left boundary
T[:, -1] = 200 # right boundary

# Convergence criteria
tolerance = 10**-4 # timestep criteria
delta = 5 # initial delta

# Calculation
timestep = 0
time = 0
Tovertime = np.zeros((5, 160)) # stores temperature over time
temperature_snapshots = [] # list to store temperature distributions at specific times
snapshot_times = [25,225, 425, 625, 825] # times to plot

while delta > tolerance:


timestep += 1
time += dt
Told = T.copy() # Save the old temperature array

# Update internal grid points using the explicit scheme


for i in range(1, ny-1):
for j in range(1, nx-1):
T[i, j] = F * (beta * (T[i, j-1] + T[i, j+1]) + (1 / beta) * (T[i-1, j] + T[i+1, j])) + (1 - ((2 /
beta) + 2 * beta) * F) * T[i, j]

# Calculate max change (convergence)


delta = np.max(np.abs(Told - T))
# Store temperature at specific points over time
if timestep % int(10 / dt) == 0:
index = int(timestep * dt / 10)
Tovertime[0, index] = T[1, 1]
Tovertime[1, index] = T[1, 2]
Tovertime[2, index] = T[2, 1]
Tovertime[3, index] = T[2, 2]
Tovertime[4, index] = 0.25 * (T[1, 1] + T[1, 2] + T[2, 1] + T[2, 2]) # average of the
midpoint

# Store temperature distribution at specific times


if any(np.isclose(time, snapshot_times, atol=dt/2)): # Check if time is close to any
snapshot time
temperature_snapshots.append((time, T.copy()))

# Plot temperature distributions at specific times


for t, temp in temperature_snapshots:
Tnew = np.flipud(temp) # Flip for correct plotting orientation
x = np.linspace(0, L, nx)
y = np.linspace(0, H, ny)
plt.contourf(x, y, Tnew, cmap='Purples')
plt.colorbar(label='Temperature')
plt.title(f'Temperature distribution at t = {t:.1f} seconds')
plt.xlabel('x position')
plt.ylabel('y position')
plt.show()

# Temporal Evolution Plot


t = np.arange(0, 1590 + 10, 10)
plt.plot(t, Tovertime.T)
plt.title('Temporal Evolution')
plt.xlabel('Time (seconds)')
plt.ylabel('Temperature')
plt.legend(['T1', 'T2', 'T3', 'T4', 'Tmidpt'])
plt.show()
# Final steady state temperature distribution
Tnew = np.flipud(T) # Flip for correct plotting orientation
x = np.linspace(0, L, nx)
y = np.linspace(0, H, ny)
plt.contourf(x, y, Tnew, cmap='Purples', levels=100)
plt.colorbar(label='Temperature')
plt.title(f'Temperature distribution at steady state (after {time:.1f} sec)')
plt.xlabel('x position')
plt.ylabel('y position')
plt.show()

Plot of temporal evolution of temperature at nodes 1, 2, 3, 4 and mid-point

Comparison between steady state temperature at the nodes with those in problem 1
Steady State Temperature Temperature in Problem 1

T1 143.7382 143.5165

T2 165.1653 164.9451

T3 205.2766 205.0549

T4 226.7037 226.4835

Tmid-point 185.22 185.02

So, the steady state temperature at the nodes with those in problem 1 is the same.

PLOTTING OF ISOTHERMS AT VARIOUS TEMPERATURES


Submitted By- DINESH ARAVINDHAN R ME24M026

You might also like