FDTD绘制场强随波长变化的FDTDscript
时间: 2024-06-08 21:07:36 浏览: 158
以下是一个简单的FDTDscript,用于模拟电磁波在二维空间中的传播,并绘制随波长变化的场强分布。
```python
import numpy as np
import matplotlib.pyplot as plt
# Parameters
c = 3e8 # speed of light in vacuum
dx = 1e-6 # spatial step size
dt = dx/c # time step size
lambda_0 = 500e-9 # central wavelength
n = 1.5 # refractive index of the medium
Lx = 20e-6 # domain length in x-direction
Ly = 20e-6 # domain length in y-direction
M = int(Lx/dx) # number of spatial grids in x-direction
N = int(Ly/dx) # number of spatial grids in y-direction
T = 2000 # number of time steps
lambda_list = np.linspace(400e-9, 700e-9, 61) # wavelength list
# Initialization
Ez = np.zeros((M, N))
Hy = np.zeros((M, N))
# Main loop
for t in range(T):
# Update Hy
for i in range(M-1):
for j in range(N-1):
Hy[i, j] -= (Ez[i+1, j] - Ez[i, j])/dx
# Update Ez
for i in range(1, M-1):
for j in range(1, N-1):
Ez[i, j] += (Hy[i, j] - Hy[i-1, j])/dx
Ez[i, j] /= n
# Source
Ez[int(M/2), int(N/2)] += np.sin(2*np.pi*c*t*dt/lambda_0)
# Boundary conditions
Ez[0, :] = 0
Ez[M-1, :] = 0
Ez[:, 0] = 0
Ez[:, N-1] = 0
# Plot the field profile at the central point
if t % 100 == 0:
plt.plot(lambda_list, np.abs(Ez[int(M/2), int(N/2)]), 'o-')
plt.xlabel('wavelength (m)')
plt.ylabel('electric field amplitude')
plt.show()
```
该脚本使用了FDTD方法模拟电磁波在二维空间中的传播,并在每个时间步长绘制了场强随波长变化的曲线。可以通过修改参数来调整模拟的精度和范围,以及绘制的曲线的数量和分辨率。
阅读全文