Skip to content

Commit

Permalink
bugfix for is_integrated=True sources (#2044)
Browse files Browse the repository at this point in the history
* bugfix for is_integrated=True sources

* lower tolerance in single precision

* fix is_integrated for CustomSource
  • Loading branch information
stevengj authored Apr 14, 2022
1 parent 66cdd37 commit 03317b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions python/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ TESTS = \
$(TEST_DIR)/test_get_epsilon_grid.py \
$(TEST_DIR)/test_holey_wvg_bands.py \
$(TEST_DIR)/test_holey_wvg_cavity.py \
$(TEST_DIR)/test_integrated_source.py \
$(KDOM_TEST) \
$(TEST_DIR)/test_ldos.py \
$(MDPYTEST) \
Expand Down
6 changes: 3 additions & 3 deletions python/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def __init__(self, frequency=None, start_time=0, end_time=1.0e20, width=0,
if frequency is None and wavelength is None:
raise ValueError("Must set either frequency or wavelength in {}.".format(self.__class__.__name__))

super(ContinuousSource, self).__init__(**kwargs)
super(ContinuousSource, self).__init__(is_integrated=is_integrated, **kwargs)
self.frequency = 1 / wavelength if wavelength else float(frequency)
self.start_time = start_time
self.end_time = end_time
Expand Down Expand Up @@ -240,7 +240,7 @@ def __init__(self, frequency=None, width=0, fwidth=float('inf'), start_time=0, c
if frequency is None and wavelength is None:
raise ValueError("Must set either frequency or wavelength in {}.".format(self.__class__.__name__))

super(GaussianSource, self).__init__(**kwargs)
super(GaussianSource, self).__init__(is_integrated=is_integrated, **kwargs)
self.frequency = 1 / wavelength if wavelength else float(frequency)
self.width = max(width, 1 / fwidth)
self.start_time = start_time
Expand Down Expand Up @@ -301,7 +301,7 @@ def __init__(self, src_func, start_time=-1.0e20, end_time=1.0e20, is_integrated=
automatically determine the decimation factor of the time-series updates
of the DFT fields monitors (if any).
"""
super(CustomSource, self).__init__(**kwargs)
super(CustomSource, self).__init__(is_integrated=is_integrated, **kwargs)
self.src_func = src_func
self.start_time = start_time
self.end_time = end_time
Expand Down
32 changes: 32 additions & 0 deletions python/tests/test_integrated_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
import meep as mp
import numpy as np

# Test that is_integrated=True source correctly generates planewaves
# for sources extending into the PML, as in this tutorial:
# https://meep.readthedocs.io/en/latest/Perfectly_Matched_Layer/#planewave-sources-extending-into-pml
# Regression test for issue #2043.

class TestIntegratedSource(unittest.TestCase):

def test_integrated_source(self):
sources = [mp.Source(mp.ContinuousSource(1,is_integrated=True),
center=mp.Vector3(-2),
size=mp.Vector3(y=6),
component=mp.Ez)]
sim = mp.Simulation(resolution=20,
cell_size=(6,6),
boundary_layers=[mp.PML(thickness=1)],
sources=sources,
k_point=mp.Vector3())
sim.run(until=30)

# field in mid-plane should be nearly constant,
# so compute its normalized std. dev. and check << 1
ez = sim.get_array(mp.Ez, center=mp.Vector3(2), size=mp.Vector3(y=6))
std = np.std(ez) / np.sqrt(np.mean(ez**2))
print("std = ", std)
self.assertAlmostEqual(std, 0.0, places = 4 if mp.is_single_precision() else 8)

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

0 comments on commit 03317b7

Please sign in to comment.