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

enable automatic DFT decimation by default #1732

Merged
merged 11 commits into from
Aug 26, 2021

Conversation

oskooi
Copy link
Collaborator

@oskooi oskooi commented Aug 14, 2021

Closes #1731.

@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2021

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 77.27273% with 5 lines in your changes missing coverage. Please review.

Project coverage is 73.46%. Comparing base (a523a87) to head (898d438).
Report is 400 commits behind head on master.

Files with missing lines Patch % Lines
python/simulation.py 75.00% 5 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1732      +/-   ##
==========================================
+ Coverage   73.39%   73.46%   +0.07%     
==========================================
  Files          13       13              
  Lines        4540     4557      +17     
==========================================
+ Hits         3332     3348      +16     
- Misses       1208     1209       +1     
Files with missing lines Coverage Δ
python/adjoint/objective.py 75.15% <100.00%> (ø)
python/adjoint/optimization_problem.py 69.69% <ø> (ø)
python/adjoint/utils.py 93.44% <ø> (ø)
python/geom.py 93.98% <ø> (ø)
python/simulation.py 76.45% <75.00%> (+0.13%) ⬆️

@smartalecH
Copy link
Collaborator

Seems a bit odd that so many of the existing tests don't support decimation. Aren't all the tests involving DFTs using band-limited sources?

@oskooi
Copy link
Collaborator Author

oskooi commented Aug 14, 2021

Almost all the existing unit tests in which automatic decimation had to be disabled in this PR involved comparing results based on DFT fields to hard-coded values which were generated without decimation. I suppose we could update all unit tests which involve DFT fields (a large fraction of the overall total) to use decimation to possibly speed things up but that would be a bit of work.

src/dft.cpp Outdated Show resolved Hide resolved
src/dft.cpp Show resolved Hide resolved
src/dft.cpp Outdated Show resolved Hide resolved
src/dft.cpp Outdated Show resolved Hide resolved
src/dft.cpp Outdated Show resolved Hide resolved
src/dft.cpp Outdated Show resolved Hide resolved
@oskooi
Copy link
Collaborator Author

oskooi commented Aug 20, 2021

This is working now. I verified that the fwidth returned by gaussian_src_time::get_fwidth(double tol) for three different values of tol (1e-1, 1e-2, 1e-3) matches nearly exactly the bandwidth computed directly from the DTFT spectrum using this 1d test:

import meep as mp
import numpy as np
import matplotlib.pyplot as plt

sz = 10.0
dpml = 2.0

fcen = 1.0
df = 0.2
src = [mp.Source(mp.GaussianSource(fcen,fwidth=df),
                 mp.Ex,
                 mp.Vector3(z=-0.5*sz+dpml))]

sim = mp.Simulation(cell_size=mp.Vector3(z=sz),
                    dimensions=1,
                    sources=src,
                    resolution=50,
                    boundary_layers=[mp.PML(dpml)])

nfreq = 200
dft_mon = sim.add_dft_fields([mp.Ex], fcen, 2*df, nfreq, center=mp.Vector3(z=0.5*sz-dpml), size=mp.Vector3())
sim.run(until_after_sources=200)

ex_dft = [ np.absolute(sim.get_dft_array(dft_mon, mp.Ex, i)) for i in range(nfreq) ]
freqs = mp.get_flux_freqs(dft_mon)

tols = [ 1e-1, 1e-2, 1e-3 ]
for tol in tols:
    max_val = np.amax(ex_dft)
    idx = np.where(ex_dft > tol*max_val)[0]
    fwidth = freqs[idx[-1]] - freqs[idx[0]]
    print('fwidth({:.2E}) = {:.5f}'.format(tol,fwidth))

plt.figure()
plt.plot(freqs,ex_dft,'bo-')
plt.xlabel('frequency')
plt.ylabel('DTFT of Ex')
plt.savefig('ex_dft.png',dpi=150,bbox_inches='tight')

ex_dft

fwidth computed using gaussian_src_time::get_fwidth(double tol)

get_fwidth(1e-1) = 0.13662
get_fwidth(1e-2) = 0.19320
get_fwidth(1e-3) = 0.23663

fwidth computed from DTFT spectrum

fwidth(1.00E-01) = 0.13266
fwidth(1.00E-02) = 0.19296
fwidth(1.00E-03) = 0.23317

src/dft.cpp Outdated Show resolved Hide resolved
@stevengj stevengj merged commit 74cb6dd into NanoComp:master Aug 26, 2021
@oskooi oskooi deleted the decimation_default branch August 26, 2021 14:56
@oskooi oskooi mentioned this pull request Sep 1, 2021
bencbartlett pushed a commit to bencbartlett/meep that referenced this pull request Sep 9, 2021
* automatic DFT decimation

* disable automatic decimation for certain tests

* fixes

* disable decimation for more tests

* compute the maximum frequency of the DFT monitor rather than use the last array element

* replace get_width with get_fwidth and use analytic formula for gaussian_src_time

* add missing 2pi factor to gaussian_src_time::get_fwidth

* add factor of 2 to gaussian_src_time::get_fwidth(double tol)

* various fixes

* disable automatic decimation for test_n2f_periodic.py

* switch ceil to floor when setting decimation_factor
mawc2019 pushed a commit to mawc2019/meep that referenced this pull request Nov 3, 2021
* automatic DFT decimation

* disable automatic decimation for certain tests

* fixes

* disable decimation for more tests

* compute the maximum frequency of the DFT monitor rather than use the last array element

* replace get_width with get_fwidth and use analytic formula for gaussian_src_time

* add missing 2pi factor to gaussian_src_time::get_fwidth

* add factor of 2 to gaussian_src_time::get_fwidth(double tol)

* various fixes

* disable automatic decimation for test_n2f_periodic.py

* switch ceil to floor when setting decimation_factor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

automatic DFT decimation for band-limited sources
4 participants