File size: 1,523 Bytes
fa34304 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import numpy as np
import matplotlib.pyplot as plt
from bpm.core import run_bpm, compute_dE_dz
from bpm.pml import generate_sigma_x
def test_core_propagation_plot():
# Simulation parameters
domain_size = 50 # um
wavelength = 0.532
n0 = 1.0
Nx = 256
Nz = 100
dx = domain_size / Nx
dz = 2 * dx
x = np.linspace(-domain_size/2, domain_size/2, Nx)
z = np.linspace(0, dz * Nz, Nz)
# Create an initial Gaussian field
E_init = np.exp(-x**2 / 2)
# Make the field 2D (each column is the same as initial condition)
E = np.tile(E_init, (Nz, 1)).T # shape (Nx, Nz)
# Use a homogeneous refractive index distribution (n0 everywhere)
n_r2 = np.full((Nx, Nz), n0**2, dtype=np.float64)
# Generate the PML damping profile in x
sigma_x = generate_sigma_x(x, dx, wavelength, domain_size, sigma_max=0.5, pml_factor=5)
# Run BPM propagation
E_out = run_bpm(E.copy(), n_r2, x, z, dx, dz, n0, sigma_x, wavelength)
# Plot the final intensity distribution
plt.figure(figsize=(8, 6))
plt.imshow(np.abs(E_out)**2, extent=[x[0], x[-1], z[0], z[-1]],
origin='lower', aspect='auto', cmap='inferno')
plt.xlabel("x (um)")
plt.ylabel("z (um)")
plt.title("Final Field Intensity from BPM Propagation")
plt.colorbar(label="Intensity")
plt.tight_layout()
# Save the figure as a PNG file
plt.savefig("core_test_field.png")
plt.close()
if __name__ == "__main__":
test_core_propagation_plot()
|