-
Notifications
You must be signed in to change notification settings - Fork 626
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
plot geometry for dispersive materials without initializing structure object #1827
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1827 +/- ##
==========================================
- Coverage 73.53% 73.44% -0.10%
==========================================
Files 17 17
Lines 4879 4891 +12
==========================================
+ Hits 3588 3592 +4
- Misses 1291 1299 +8
|
For the record, there was a single test failure unrelated to this PR that went away after rerunning. The symmetry unit test of
It might therefore be useful to increase the tolerance slightly for this test to avoid this failure in the future. |
To demonstrate that this feature is working correctly, here is a 2d test case which contains a variety of different simulation objects: two types of geometric objects ( import meep as mp
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
mp.verbosity(3)
cell_size = mp.Vector3(1,1,0)
design_region_resolution = 500
design_shape = mp.Vector3(1.0,1.0,0)
Nx = int(design_region_resolution*design_shape.x) + 1
Ny = int(design_region_resolution*design_shape.y) + 1
x = np.linspace(-0.5*cell_size.x,0.5*cell_size.x,Nx)
y = np.linspace(-0.5*cell_size.y,0.5*cell_size.y,Ny)
xv, yv = np.meshgrid(x,y)
rad = 0.201943
design_params = np.sqrt(np.square(xv) + np.square(yv)) < rad
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
mp.Medium(index=3.5),
weights=design_params,
do_averaging=True,
beta=1000,
eta=0.5)
geometry = [mp.Cylinder(center=mp.Vector3(0.35,0.1),
radius=0.1,
height=mp.inf,
material=mp.Medium(index=1.5)),
mp.Block(center=mp.Vector3(-0.15,-0.2),
size=mp.Vector3(0.2,0.24,mp.inf),
material=mp.Medium(index=2.5)),
mp.Block(center=mp.Vector3(-0.2,0.2),
size=mp.Vector3(0.4,0.4,mp.inf),
material=matgrid)]
sources = [mp.Source(mp.ContinuousSource(1.0),
center=mp.Vector3(),
size=mp.Vector3(0.2,0.2,0),
component=mp.Ez)]
sim = mp.Simulation(resolution=20,
cell_size=cell_size,
geometry=geometry,
sources=sources,
boundary_layers=[mp.PML(thickness=0.1,side=mp.High,direction=mp.Y)])
mon = sim.add_dft_fields([mp.Ez],
1.0,
0,
1,
center=mp.Vector3(0.3,-0.2),
size=mp.Vector3(0.3,0.1))
grid_resolution = 320
sim.plot2D(resolution=grid_resolution)
plt.title('resolution = {}'.format(grid_resolution))
plt.savefig('ring_matgrid_res{}'.format(grid_resolution),
dpi=150,
bbox_inches='tight') output
|
Is it just as fast when plotting a 2D cross section of a 3D simulation? |
Taking the example above and extending it to 3d, the difference in runtimes at a resolution of 320 for this PR compared to master is 0.5773 s vs. 87.5444 s. This translates to a speedup of a factor of more than 150. Using the revamped PR 1827
master
|
Why can't we update |
This seems like it is possible without much additional effort since |
See how array slice gets a scalar quantity from the trace of inv(ε): Lines 379 to 387 in 03ea7ad
|
Pull out and refactor: Lines 2568 to 2588 in 03ea7ad
|
Refactor that code into new function |
48b4c0d
to
a7fb4c3
Compare
Added support for dispersive materials following the approach we discussed above. This involved reinstating the import meep as mp
from meep.materials import SiN, Co
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
mp.verbosity(3)
cell_size = mp.Vector3(1,1,1)
design_region_resolution = 500
design_shape = mp.Vector3(1.0,1.0,0)
Nx = int(design_region_resolution*design_shape.x) + 1
Ny = int(design_region_resolution*design_shape.y) + 1
x = np.linspace(-0.5*cell_size.x,0.5*cell_size.x,Nx)
y = np.linspace(-0.5*cell_size.y,0.5*cell_size.y,Ny)
xv, yv = np.meshgrid(x,y)
rad = 0.201943
w = 0.104283
design_params = np.logical_and(np.sqrt(np.square(xv) + np.square(yv)) > rad,
np.sqrt(np.square(xv) + np.square(yv)) < rad+w,
dtype=np.double)
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
mp.Medium(index=3.5),
weights=design_params,
do_averaging=True,
beta=1000,
eta=0.5)
geometry = [mp.Cylinder(center=mp.Vector3(0.35,0.1),
radius=0.1,
height=0.5,
material=mp.Medium(index=1.5)),
mp.Block(center=mp.Vector3(-0.15,-0.2),
size=mp.Vector3(0.2,0.24,0.8),
material=SiN),
mp.Block(center=mp.Vector3(-0.2,0.2),
size=mp.Vector3(0.4,0.4,0.3),
material=matgrid),
mp.Prism(vertices=[mp.Vector3(0.05,0.45),
mp.Vector3(0.32,0.22),
mp.Vector3(0.15,0.10)],
height=0.5,
material=Co)]
sources = [mp.Source(mp.ContinuousSource(1.0),
center=mp.Vector3(),
size=mp.Vector3(0.2,0.2,0.2),
component=mp.Ez)]
sim = mp.Simulation(resolution=400,
cell_size=cell_size,
geometry=geometry,
sources=sources,
boundary_layers=[mp.PML(thickness=0.1,side=mp.High,direction=mp.Y)])
mon = sim.add_dft_fields([mp.Ez],
1.0,
0,
1,
center=mp.Vector3(0.3,-0.2),
size=mp.Vector3(0.3,0.1))
grid_resolution = 400
frequency = 0
sim.plot2D(resolution=grid_resolution,
frequency=frequency,
output_plane=mp.Volume(center=mp.Vector3(),
size=mp.Vector3(1,1,0)))
plt.title('frequency = {}, resolution = {}'.format(frequency,grid_resolution))
plt.savefig('ring_matgrid_3d_dispersive_res{}_freq{}_master'.format(grid_resolution,frequency),
dpi=150,
bbox_inches='tight') |
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
… object (NanoComp#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check
* first draft of loop_in_chunks using grid_vol calculations * try to only use owned points in loop_in_chunks * don't unpad if !use_symmetry * stop vscode from complaining about comments * rm hack to loop over centered grid * fix and example * almost fix * before fix * one more loop over num_chunk * include set * different approach * minor * include climits * include climits * fix retval * clean up * using flipped * add missing changes * fix bug * fix pml * Fix memory leaks in SWIG wrappers. (#1826) * Fix memory leaks in SWIG wrappers. * move delete into material_free Co-authored-by: Steven G. Johnson <[email protected]> * Fix adjoint gradient with conductivities (#1830) * damp_fix * increase run time Co-authored-by: Mo Chen <[email protected]> * plot geometry for dispersive materials without initializing structure object (#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check * unit test for `get_epsilon_grid` (#1835) * unit test for get_epsilon_grid * fix * limit test points to homogeneous regions (i.e. no material interfaces) and away from potential chunk boundaries * support for single-precision floating point for fields array functions (#1833) * switch dft-related functions to using realnum from double * more fixes * more type conversions from double to realnum * adjust check tolerance of tests/integrate.cpp based on floating-point precision * more fixes * rebase from master from fix merge conflicts * slight adjustment to tolerances in unit tests and update docs * remove type check in test_adjoint_solver.py * revert return types of integration functions to double * revert return type of process_dft_component to double * cleanup * Fix memory leaks (#1839) * Fix memory leaks * Add kkg to authors list * Fix for Issue #1834 (#1840) * Fix memory leaks * Add kkg to authors list * Expose set_default_material and use it in libpympb/pympb.cpp * use unique_ptr (C++11) instead of make_unique (C++14) (#1844) * Use None instead of empty list in constructors (#1846) * use None * minor fix Co-authored-by: Mo Chen <[email protected]> * Define what happens when `β=∞` and `u=η` (#1842) * define what happens when beta=inf and u=0.5 * use eta not 0.5 * Update src/meepgeom.cpp Co-authored-by: Steven G. Johnson <[email protected]> * fix for arrays (#1845) * minor improvements to docs (#1848) * update homebrew instructions for hdf5 and openblas (fixes #1850) * recommend python3 on macos * silence compiler warnings * whoops, missing commit * tests need scipy and autograd * missing sudo * parameterized package is also used for tests * h5py and jax on mac * note on autogen.sh for git clone * xcode installation shortcut * bug fix for get_epsilon_point and cell boundary in parallel simulation (#1849) * bug fix for get_epsilon_point and cell boundary in parallel simulation * check for six digits in test_material_grid.py because of single precision * unit test for conductivity (#1857) * unit test for conductivity * describe in the docs how to model the attenutation coefficient using conductivity * Update python/tests/test_conductivity.py Co-authored-by: Steven G. Johnson <[email protected]> * Fix the failure message for absorber 1D test (#1859) * add missing fixed field phase to MPB unit test (#1860) * fix memory leak in array-slice-ll.cpp (#1865) * fix memory leak in array-slice-ll.cpp * reinstate line break * fix memory leak in cyl-ellipsoid-ll.cpp (#1866) * level parameter for contour plot calls epsilon data (#1869) * fix heap buffer overflow error for update E from D in cylindrical coordinates (#1871) * Add cylindrical coordinates support for `plot2d` (#1873) * add visualization support for plot2d * bug fix with cartesian plotting * fix near-field monitor position in cylindrical coordinate tutorial (#1874) * fix memory leaks in structure and fields load during checkpointing (#1872) * fix memory leaks in structure and fields load during checkpointing * delete the chi1inv and fields array if it exists and reallocate * in unit test, set gaussian source cutoff to 0 due to off-by-1 timestep counter bug * remove cutoff=0 from unit tests * lazily allocate H only if B is not NULL * allocate fields array for H in PML region * fix two memory leaks in geom_epsilon class (#1877) * fix two memory leaks in geom_epsilon class * delete global variable default_material at the end of unit tests * add unset_default_material function to class meep_geom * include ring-ll.cpp in C++ unit tests (#1878) * include ring-ll.cpp in C++ unit tests * only validate Harminv modes with error below some threshold * fix and example * first draft of loop_in_chunks using grid_vol calculations * try to only use owned points in loop_in_chunks * don't unpad if !use_symmetry * stop vscode from complaining about comments * rm hack to loop over centered grid * almost fix * before fix * one more loop over num_chunk * include set * different approach * minor * include climits * include climits * fix retval * clean up * using flipped * add missing changes * fix bug * fix pml * Fix memory leaks in SWIG wrappers. (#1826) * Fix memory leaks in SWIG wrappers. * move delete into material_free Co-authored-by: Steven G. Johnson <[email protected]> * Fix adjoint gradient with conductivities (#1830) * damp_fix * increase run time Co-authored-by: Mo Chen <[email protected]> * plot geometry for dispersive materials without initializing structure object (#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check * unit test for `get_epsilon_grid` (#1835) * unit test for get_epsilon_grid * fix * limit test points to homogeneous regions (i.e. no material interfaces) and away from potential chunk boundaries * support for single-precision floating point for fields array functions (#1833) * switch dft-related functions to using realnum from double * more fixes * more type conversions from double to realnum * adjust check tolerance of tests/integrate.cpp based on floating-point precision * more fixes * rebase from master from fix merge conflicts * slight adjustment to tolerances in unit tests and update docs * remove type check in test_adjoint_solver.py * revert return types of integration functions to double * revert return type of process_dft_component to double * cleanup * Fix memory leaks (#1839) * Fix memory leaks * Add kkg to authors list * Fix for Issue #1834 (#1840) * Fix memory leaks * Add kkg to authors list * Expose set_default_material and use it in libpympb/pympb.cpp * use unique_ptr (C++11) instead of make_unique (C++14) (#1844) * Use None instead of empty list in constructors (#1846) * use None * minor fix Co-authored-by: Mo Chen <[email protected]> * Define what happens when `β=∞` and `u=η` (#1842) * define what happens when beta=inf and u=0.5 * use eta not 0.5 * Update src/meepgeom.cpp Co-authored-by: Steven G. Johnson <[email protected]> * fix for arrays (#1845) * minor improvements to docs (#1848) * update homebrew instructions for hdf5 and openblas (fixes #1850) * recommend python3 on macos * silence compiler warnings * whoops, missing commit * tests need scipy and autograd * missing sudo * parameterized package is also used for tests * h5py and jax on mac * note on autogen.sh for git clone * xcode installation shortcut * bug fix for get_epsilon_point and cell boundary in parallel simulation (#1849) * bug fix for get_epsilon_point and cell boundary in parallel simulation * check for six digits in test_material_grid.py because of single precision * unit test for conductivity (#1857) * unit test for conductivity * describe in the docs how to model the attenutation coefficient using conductivity * Update python/tests/test_conductivity.py Co-authored-by: Steven G. Johnson <[email protected]> * Fix the failure message for absorber 1D test (#1859) * add missing fixed field phase to MPB unit test (#1860) * fix memory leak in array-slice-ll.cpp (#1865) * fix memory leak in array-slice-ll.cpp * reinstate line break * fix memory leak in cyl-ellipsoid-ll.cpp (#1866) * level parameter for contour plot calls epsilon data (#1869) * fix heap buffer overflow error for update E from D in cylindrical coordinates (#1871) * Add cylindrical coordinates support for `plot2d` (#1873) * add visualization support for plot2d * bug fix with cartesian plotting * fix near-field monitor position in cylindrical coordinate tutorial (#1874) * fix memory leaks in structure and fields load during checkpointing (#1872) * fix memory leaks in structure and fields load during checkpointing * delete the chi1inv and fields array if it exists and reallocate * in unit test, set gaussian source cutoff to 0 due to off-by-1 timestep counter bug * remove cutoff=0 from unit tests * lazily allocate H only if B is not NULL * allocate fields array for H in PML region * fix two memory leaks in geom_epsilon class (#1877) * fix two memory leaks in geom_epsilon class * delete global variable default_material at the end of unit tests * add unset_default_material function to class meep_geom * include ring-ll.cpp in C++ unit tests (#1878) * include ring-ll.cpp in C++ unit tests * only validate Harminv modes with error below some threshold * fix and example * first draft of loop_in_chunks using grid_vol calculations * try to only use owned points in loop_in_chunks * don't unpad if !use_symmetry * stop vscode from complaining about comments * rm hack to loop over centered grid * almost fix * before fix * one more loop over num_chunk * include set * different approach * minor * include climits * include climits * fix retval * clean up * using flipped * add missing changes * fix bug * fix pml * Fix adjoint gradient with conductivities (#1830) * damp_fix * increase run time Co-authored-by: Mo Chen <[email protected]> * plot geometry for dispersive materials without initializing structure object (#1827) * plot geometry without initializing structure class * update docstrings * rotate epsilon grid by 90 degrees to properly orient axes * add support for dispersive ε * update markdown pages from docstrings * make frequency and resolution parameters of plot2D dictionary keys of eps_parameters * reinstate frequency parameter of plot2D and add warning that it is deprecated * fix order of frequency check * support for single-precision floating point for fields array functions (#1833) * switch dft-related functions to using realnum from double * more fixes * more type conversions from double to realnum * adjust check tolerance of tests/integrate.cpp based on floating-point precision * more fixes * rebase from master from fix merge conflicts * slight adjustment to tolerances in unit tests and update docs * remove type check in test_adjoint_solver.py * revert return types of integration functions to double * revert return type of process_dft_component to double * cleanup * Fix memory leaks (#1839) * Fix memory leaks * Add kkg to authors list * Fix for Issue #1834 (#1840) * Fix memory leaks * Add kkg to authors list * Expose set_default_material and use it in libpympb/pympb.cpp * use unique_ptr (C++11) instead of make_unique (C++14) (#1844) * update homebrew instructions for hdf5 and openblas (fixes #1850) * rm hack to loop over centered grid * almost fix * one more loop over num_chunk * different approach * include climits * include climits * clean up * using flipped * Revert "fix pml" This reverts commit 9ed741e. * fix rebase * fix rebase * fix rebase * fix rebase * fix rebase * fix error * increase tol * cleanup * cleanup Co-authored-by: Steven G. Johnson <[email protected]> Co-authored-by: Mo Chen <[email protected]> Co-authored-by: Mo Chen <[email protected]> Co-authored-by: Krishna Gadepalli <[email protected]> Co-authored-by: Ardavan Oskooi <[email protected]> Co-authored-by: Steven G. Johnson <[email protected]> Co-authored-by: Alec Hammond <[email protected]> Co-authored-by: Andreas Hoenselaar <[email protected]> Co-authored-by: simbilod <[email protected]>
Closes #1824.
Removed the
frequency
parameter fromplot2D
since this cannot be passed toget_epsilon_grid
. As a result, this API change is not backwards compatible. As a workaround, we can leave thefrequency
parameter in but just mention in the documentation that it is simply ignored.cc @joamatab