NanoComp / meep

free finite-difference time-domain (FDTD) software for electromagnetic simulations
GNU General Public License v2.0
1.17k stars 598 forks source link

Subpixel averaging and material discretization at edges of domain #2640

Open mawc2019 opened 10 months ago

mawc2019 commented 10 months ago

Sometimes the permittivity in simulation is different from what the user should expect. An example is as follows, where the permittivity should be 4 everywhere, but the value given by sim.get_epsilon() is different at the lateral boundaries. image The script is as follows.

import meep as mp
import numpy as np
from matplotlib import pyplot as plt
SiN = mp.Medium(epsilon=4)

Lx, Ly, dpml = 0.3149, 4, 1
resolution = 200
pml_layers = [mp.PML(dpml,direction=mp.Y)]
cell_size = mp.Vector3(Lx,Ly+2*dpml)
geometry = [mp.Block(center=mp.Vector3(), size=cell_size, material=SiN)]

sim = mp.Simulation(cell_size=cell_size,boundary_layers=pml_layers,eps_averaging=False,
                    geometry=geometry,k_point=mp.Vector3(),resolution=resolution)
sim.run(until_after_sources=1)
epsilon = sim.get_epsilon()
sim.reset_meep()

plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.imshow(np.transpose(epsilon))
plt.title('Permittivity')
plt.xlabel('X')
plt.ylabel('Y')
plt.colorbar()

plt.subplot(1,2,2)
plt.plot(epsilon);
plt.title('Permittivity projected to the x axis')
plt.xlabel('X')
plt.ylabel('Permittivity');

The flaw remains no matter eps_averaging=True or False. If k_point is changed to False, the flaw in permittivity will become different.

oskooi commented 10 months ago

Take a look at this FAQ, particularly the third paragraph and the accompanying image. Try removing the k_point from the Simulation object constructor.

mawc2019 commented 10 months ago

That FAQ does not seem to answer the issue.

the permittivity should be 4 everywhere

In the above example, there is only one material with epsilon=4.

If k_point is changed to False, the flaw in permittivity will become different.

Setting k_point to False is equivalent to removing it from Simulation. The flaw in permittivity becomes different but still exists. image

stevengj commented 10 months ago

Basically, it's an issue of what we should do right at the edge of the computational cell. Probably we should do subpixel averaging differently in that case. There is also some averaging with Yee grid points beyond the cell.

The simplest solution is to make the object a little bigger than the cell.