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

Warn if Source is outside Harminv frequency range #355

Merged
merged 2 commits into from
Jun 1, 2018
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
19 changes: 19 additions & 0 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,28 @@ def _collect2(sim):
return _collect2
return _collect1

def _check_freqs(self, sim):
source_freqs = [(s.src.frequency, 0 if s.src.width == 0 else 1 / s.src.width)
for s in sim.sources
if hasattr(s.src, 'frequency')]

harminv_max = self.fcen + 0.5 * self.df
harminv_min = self.fcen - 0.5 * self.df

for sf in source_freqs:
sf_max = sf[0] + 0.5 * sf[1]
sf_min = sf[0] - 0.5 * sf[1]
if harminv_max > sf_max:
warn_fmt = "Harminv frequency {} is outside maximum Source frequency {}"
warnings.warn(warn_fmt.format(harminv_max, sf_max), RuntimeWarning)
if harminv_min < sf_min:
warn_fmt = "Harminv frequency {} is outside minimum Source frequency {}"
warnings.warn(warn_fmt.format(harminv_min, sf_min), RuntimeWarning)

def _analyze_harminv(self, sim, maxbands):
harminv_cols = ['frequency', 'imag. freq.', 'Q', '|amp|', 'amplitude', 'error']
display_run_data(sim, 'harminv', harminv_cols)
self._check_freqs(sim)

dt = self.data_dt if self.data_dt is not None else sim.fields.dt

Expand Down
27 changes: 27 additions & 0 deletions python/tests/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import sys
import unittest
import warnings
import h5py
import numpy as np
import meep as mp
Expand Down Expand Up @@ -397,6 +398,32 @@ def test_synchronized_magnetic(self):

sim.run(mp.synchronized_magnetic(mp.output_bfield_y), until=10)

def test_harminv_warnings(self):

def check_warnings(sim, h, should_warn=True):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
sim.run(mp.after_sources(h), until_after_sources=5)

if should_warn:
self.assertEqual(len(w), 1)
self.assertIn("Harminv", str(w[-1].message))
else:
self.assertEqual(len(w), 0)

sources = [mp.Source(src=mp.GaussianSource(1, fwidth=1), center=mp.Vector3(), component=mp.Ez)]
sim = mp.Simulation(cell_size=mp.Vector3(10, 10), resolution=10, sources=sources)
h = mp.Harminv(mp.Ez, mp.Vector3(), 1.4, 0.5)
check_warnings(sim, h)

sim = mp.Simulation(cell_size=mp.Vector3(10, 10), resolution=10, sources=sources)
h = mp.Harminv(mp.Ez, mp.Vector3(), 0.5, 0.5)
check_warnings(sim, h)

sim = mp.Simulation(cell_size=mp.Vector3(10, 10), resolution=10, sources=sources)
h = mp.Harminv(mp.Ez, mp.Vector3(), 1, 1)
check_warnings(sim, h, should_warn=False)


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