After 🎉 successfully teaching 👨🏫 the neural network about the 〰️ wave 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 <2025> <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 necessary libraries
import numpy as np
import matplotlib.pyplot as plt
#%% define parameters
L = 1 # domain length
D = 1 # domain width
h = D / 100 # grid spacing
Nx = round(L / h) + 1 # grid points in x-axis
Ny = round(D / h) + 1 # grid points in y-axis
nu = 0.175 # Poisson's ratio
E = 3e10 # Young's modulus [Pa]
rho = 2350 # density [Kg/m3]
cp = np.sqrt(E * (1 - nu) / (rho * (1 + nu) * (1 - 2 * nu))) # wave speed
travel = 100 # times the disturbance travels entire length of computational domain
TT = travel * L / cp # total time
CFL = 0.1 # CFL number
dt = CFL * h / cp # time step
ns = int(TT / dt) # number of time steps
#%% initialize fields
u = np.zeros((Nx, Ny)) # u displacement (next time step) u_n+1
u_vel = np.zeros((Nx, Ny)) # initial velocity
un = np.copy(u) # u displacement (current time step) u
unn = un - dt * u_vel # u displacement (previous time step) u_n-1
#%% pre calculate for speed
P1 = dt**2 * cp**2 / h**2
f = 2 / TT # frequency
omega = 2 * np.pi * f * dt
#%% plotting
x = np.linspace(0, L, Nx) # initialize x
y = np.linspace(0, D, Ny) # initialize y
X, Y = np.meshgrid(x, y) # create combinations of sample points along the domain
#%% solve 2D-transient wave equation
for nt in range(ns):
u[1:-1, 1:-1] = 2 * un[1:-1, 1:-1] - unn[1:-1, 1:-1] + (dt**2 * cp**2 / h**2) * (un[2:, 1:-1] + un[:-2, 1:-1] + un[1:-1, 2:] + un[1:-1, :-2] - 4 * un[1:-1, 1:-1])
u[0, :] = 0 # u = 0 at x = 0
u[-1, :] = 0 # u = 0 at x = L
u[:, 0] = 0 # u = 0 at y = 0
u[:, -1] = 0 # u = 0 at y = D
u[int(0.5 * Nx / L) - 1:int(0.5 * Nx / L) + 2, int(0.5 * Ny / D) - 1:int(0.5 * Ny / D) + 2] = np.sin(omega * nt) # u = sin(2*pi*f*t) at x = 0.5 and y = 0.5
un[:, :] = u[:, :] # shift u_n+1 to u_n
unn[:, :] = un[:, :] # shift u_n to u_n-1
if nt % 1000 == 0:
plt.figure(dpi = 200) # make a nice crisp image :)
plt.contourf(X, Y, u.T, cmap = 'prism', levels = 256, vmin = -0.25, vmax = 0.25) # contour plot
plt.gca().set_aspect('equal', adjustable = 'box')
plt.xticks([])
plt.yticks([])
plt.show()
The code creates a pulse, as shown in Fig. 1. Within Fig. 1, 1 of several snapshots is shown. The material properties used are that of concrete.
![]() |
Fig. 1, A nice wave |
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