Source code for mercurial.tests.enhancements.test_fdtd

"""Test FDTD electromagnetic wave propagation."""

import matplotlib.pyplot as plt
import numpy as np

from mercurial.simulation.engine import SimulationEngine


# Define a Gaussian pulse source at the centre
[docs] def gaussian_source(i, j, t, centre_x=50, centre_y=50, width=5, amplitude=1.0): dist2 = (i - centre_x) ** 2 + (j - centre_y) ** 2 if dist2 < width**2: return amplitude * np.exp(-((t - 0.1e-9) ** 2) / (2 * (0.02e-9) ** 2)) return 0.0
engine = SimulationEngine(dim=10) results = engine.run_fdtd( (0.0, 0.5e-9), nx=100, ny=100, dx=0.01, dy=0.01, source=lambda i, j, t: gaussian_source(i, j, t), save_every=5, ) # Plot final E field intensity intensity = results["final_Ex"] ** 2 + results["final_Ey"] ** 2 plt.imshow(intensity, cmap="hot", origin="lower", extent=[0, 1, 0, 1]) plt.colorbar(label="|E|²") plt.title("FDTD: Final Electric Field Intensity") plt.show()