Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kz_2d option #1035

Merged
merged 3 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def __init__(self,
default_material=mp.Medium(),
m=0,
k_point=False,
special_kz=False,
kz_2d="complex",
extra_materials=[],
material_function=None,
epsilon_func=None,
Expand Down Expand Up @@ -609,7 +609,6 @@ def __init__(self,
self.Courant = Courant
self.global_d_conductivity = 0
self.global_b_conductivity = 0
self.special_kz = special_kz
self.k_point = k_point
self.fields = None
self.structure = None
Expand Down Expand Up @@ -639,6 +638,19 @@ def __init__(self,
self.fragment_stats = None
self._output_stats = os.environ.get('MEEP_STATS', None)

self.special_kz = False
if self.cell_size.z == 0 and self.k_point and self.k_point.z != 0:
if kz_2d == "complex":
self.special_kz = True
self.force_complex_fields = True
elif kz_2d == "real/imag":
self.special_kz = True
self.force_complex_fields = False
elif kz_2d == "3d":
self.special_kz = False
else:
raise ValueError("Invalid kz_2d option: {} not in [complex, real/imag, 3d]".format(kz_2d))

Copy link
Collaborator

@oskooi oskooi Oct 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 641-653 should be replaced with:

        self.special_kz = False
        if self.cell_size.z == 0 and self.k_point and self.k_point.z != 0:
            if kz_2d == "complex":
                self.special_kz = True
                self.force_complex_fields = True
            elif kz_2d == "real/imag":
                self.special_kz = True
                self.force_complex_fields = False
            elif kz_2d == "3d":
                self.special_kz = False
            else:
                raise ValueError("Invalid kz_2d option: {} not in [complex, real/imag, 3d]".format(kz_2d))

Setting kpoint=0 when special_kz=True is incorrect since its non-zero z component is what is used in the calculations.

# To prevent the user from having to specify `dims` and `is_cylindrical`
# to Volumes they create, the library will adjust them appropriately based
# on the settings in the Simulation instance. This method must be called on
Expand Down
30 changes: 18 additions & 12 deletions python/tests/special_kz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestSpecialKz(unittest.TestCase):

def refl_planar(self, theta, special_kz):
def refl_planar(self, theta, kz_2d):
resolution = 100 # pixels/um

dpml = 1.0
Expand All @@ -30,7 +30,7 @@ def refl_planar(self, theta, special_kz):
boundary_layers=pml_layers,
sources=sources,
k_point=k_point,
special_kz=special_kz,
kz_2d=kz_2d,
resolution=resolution)

refl_fr = mp.FluxRegion(center=mp.Vector3(-0.25*sx),
Expand All @@ -52,7 +52,7 @@ def refl_planar(self, theta, special_kz):
geometry=geometry,
sources=sources,
k_point=k_point,
special_kz=special_kz,
kz_2d=kz_2d,
resolution=resolution)

refl = sim.add_flux(fcen,0,1,refl_fr)
Expand Down Expand Up @@ -80,20 +80,22 @@ def test_special_kz(self):
theta = math.radians(23)

start = time()
Rmeep_no_kz = self.refl_planar(theta, False)
t_no_kz = time() - start
Rmeep_complex = self.refl_planar(theta, 'complex')
t_complex = time() - start

start = time()
Rmeep_kz = self.refl_planar(theta, True)
t_kz = time() - start
Rmeep_real_imag = self.refl_planar(theta, 'real/imag')
t_real_imag = time() - start

Rfres = Rfresnel(theta)

self.assertAlmostEqual(Rmeep_no_kz,Rfres,places=2)
self.assertAlmostEqual(Rmeep_kz,Rfres,places=2)
self.assertLess(t_kz,t_no_kz)
self.assertAlmostEqual(Rmeep_complex,Rfres,places=2)
self.assertAlmostEqual(Rmeep_real_imag,Rfres,places=2)
self.assertLess(t_real_imag,t_complex)

def test_eigsrc_kz(self):

def eigsrc_kz(self, kz_2d):
print(kz_2d)
resolution = 30 # pixels/um
cell_size = mp.Vector3(14,14)
pml_layers = [mp.PML(thickness=2)]
Expand All @@ -120,7 +122,7 @@ def test_eigsrc_kz(self):
geometry=geometry,
symmetries=[mp.Mirror(mp.Y)],
k_point=mp.Vector3(z=kz),
special_kz=True)
kz_2d=kz_2d)

tran = sim.add_flux(fsrc, 0, 1, mp.FluxRegion(center=mp.Vector3(x=5), size=mp.Vector3(y=14)))
sim.run(until_after_sources=50)
Expand All @@ -144,6 +146,10 @@ def test_eigsrc_kz(self):
self.assertAlmostEqual(ratio_ez.real,phase_diff.real,places=10)
self.assertAlmostEqual(ratio_ez.imag,phase_diff.imag,places=10)

def test_eigsrc_kz(self):
self.eigsrc_kz("complex")
self.eigsrc_kz("real/imag")


if __name__ == '__main__':
unittest.main()