Saturday, 21 December 2024

2D Heat Equation Code (Finite Difference Method)

     After 🎉 successfully teaching 👨‍🏫 the neural network about the 🔥 heat / diffusion 💉 equation, yours truly thought it is the time ⏱️ to write a simple code 🖥️ for discretized domain as well. A code yours truly created in abundant spare time is mentioned in this blog 📖.

     A complex (sinusoidal) 🌊 boundary condition is implemented. Dirichlet and Neumann boundary conditions are also implemented. The code is vectorized ↗ so there is only one loop 😁.  CFL condition is implemented in the time-step calculation so time-step ⏳ is adjusted based on the mesh size automatically 😇.

     For simple geometries, traditional numerical methods are is still better than PINNs. 🧠

Code

#Copyright <2024> <FAHAD BUTT>
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import numpy as np
import matplotlib.pyplot as plt
# mesh parameters
L = 1 # length of plate
D = np.pi # width of plate
h = 1 / 50 # grid size
Nx = int((L / h) + 1) # grid points in x-axis
Ny = int((D / h) + 1) # grid points in y-axis
alpha = 1 # thermal diffusivity
dt = h**2 / (4 * alpha) # time step (based on CFL condition)
total_time = 1 # total time
nt = int(total_time / dt) # total time steps
# initialization
T = np.zeros((Nx, Ny)) # initial condition
T_new = np.zeros_like(T)
x = np.linspace(0, L, Nx)
y = np.linspace(0, D, Ny)
# solve 2D-transient heat equation
for n in range(nt):
    T_new[1:-1, 1:-1] = T[1:-1, 1:-1] + ((alpha * dt) / h**2) * (T[2:, 1:-1] + T[:-2, 1:-1] + T[1:-1, 2:] + T[1:-1, :-2] - 4 * T[1:-1, 1:-1])
    T[:, :] = T_new
    # apply boundary conditions
    T[0, :] = np.sin(2 * np.pi * y / D) # T = sin(y) at x = 0
    T[-1, :] = T[-2, :] # dT/dx = 0 at x = L
    T[:, 0] = 0 # T = 0 at y = 0
    T[:, -1] = 0 # T = 0 at y = D
# plotting
X, Y = np.meshgrid(x, y, indexing="ij")
plt.figure(dpi = 500)
plt.contourf(X, Y, T, levels = 64, cmap = "jet")
plt.gca().set_aspect('equal', adjustable = 'box')
plt.colorbar(label = "Temperature")
plt.title("Temperature Distribution")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

     The code creates the output as shown in Fig. 1.



Fig. 1, Temperature distribution

     Thank you for reading! If you want to hire me as a post-doc researcher in the fields of thermo-fluids and / or fracture mechanics, do reach out!

No comments:

Post a Comment