Simulate transient heat conduction in a 2D rectangular domain until a steady state is reached. Visualize the final steady-state temperature distribution. Follow these steps:
1. Grid Initialization
Create a 2D NumPy array representing the temperature grid. Initially, set all temperatures to zero. (See Parameter section below for specifics on the grid.) Also create a 3D array that will store the 2D array at each time step.
2. Boundary Conditions
Bottom row: Constant temperature of 100 degrees.
Left and right boundary columns: Constant temperature of 50 degrees.
Top boundary row: Insulated (no heat loss), implemented by setting these cells equal to their neighboring cells. At every time step, the top row equals the second top row.
3. Simulation Loop
Iterate over time steps
At each time step, the new temperature matrix computes from the previous matrix using the discretized heat equation (see Background section below). You will have to iterate through all matrix cells in order to implement it.
Once you computed the new grid, you will have to update the boundary conditions of the top boundary cells.
At each time step, check for convergence, and break the loop if the rule is fulfilled (see Convergence Check below).
Store the grid state at each time step in a 3D NumPy array.
4. Convergence Check
Stop the simulation when the steady state is reached. The steady state is reached when the maximum temperature change between two consecutive time steps is below a certain threshold (see Parameters section below).
5. Visualization
Use Matplotlib to visualize the final temperature distribution. At which time step is steady state reached?
import numpy as npimport matplotlib.pyplot as plt# Parametersheight, width =10, 10# Dimensions of the matrixmax_iter =1000# Maximum number of iterationstolerance =0.01# Convergence tolerancedt =0.1# Time step size# Initialize the temperature matrix and the matrix for temperature historytemperature = np.zeros((height, width))temp_history = np.zeros((height, width, max_iter))# Boundary conditionstemperature[0, :] =100# Bottom edgetemperature[:, 0] =50# Left edgetemperature[:, -1] =50# Right edge# Top edge is handled within the loop# Heat conduction loopfor t inrange(max_iter): temp_old = temperature.copy()# Update temperature (excluding boundaries)for i inrange(1, height -1):for j inrange(1, width -1): temperature[i, j] = temp_old[i, j] + dt * ( temp_old[i+1, j] + temp_old[i-1, j] + temp_old[i, j+1] + temp_old[i, j-1] -4* temp_old[i, j] )# Neumann boundary condition for the top edge (i.e., insulated: temperature gradient is zero) temperature[-1, :] = temperature[-2, :]# Check for convergenceif np.max(np.abs(temperature - temp_old)) < tolerance:print(f"Steady state reached at iteration {t}")break# Store the result temp_history[:, :, t] = temperature# Truncate the history array to the number of iterations actually performedtemp_history = temp_history[:, :, :t+1]# Visualization of the final steady statefig, ax = plt.subplots()im = ax.pcolormesh(temperature, cmap='hot')fig.colorbar(im, label='Temperature')ax.set_title('Steady State Temperature Distribution')fig.savefig("heat_final.png")