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

Filters for periodic design patterns #2465

Merged
merged 5 commits into from
Apr 28, 2023

Conversation

mawc2019
Copy link
Contributor

@mawc2019 mawc2019 commented Apr 10, 2023

This PR makes the 2d filters in filters.py applicable to periodic 2d designs, where the design pattern is periodic in x, y, or both x and y directions. In cylindrical_filter, conic_filter, and gaussian_filter, and the periodicity is specified by a new argument periodic_axes, whose value can be 0, 1, (0,1), or None.

In most cases where the filter diameter is not larger than the size of the input design pattern, extra padding along the periodic axis is simply not added before convolution. If the filter diameter is larger than the size of the input design pattern along a periodic axis, before convolution, the design pattern is duplicated along that axis so that the resultant design pattern along that axis is not smaller than the filter diameter.

The change in those filter functions also makes constraint_solid and constraint_void applicable to periodic 2d designs.

By the way, the missing +1 in calculating the array size for the material grid is restored in this PR.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mawc2019
Copy link
Contributor Author

In this example, the function conic_filter with radius = 55 and resolution = 1 is used.
image

@codecov-commenter
Copy link

codecov-commenter commented Apr 10, 2023

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

Codecov Report

Attention: Patch coverage is 92.94118% with 6 lines in your changes missing coverage. Please review.

Project coverage is 73.82%. Comparing base (845cdf4) to head (e203255).
Report is 124 commits behind head on master.

Files with missing lines Patch % Lines
python/adjoint/filters.py 92.94% 6 Missing ⚠️

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2465      +/-   ##
==========================================
+ Coverage   73.25%   73.82%   +0.57%     
==========================================
  Files          17       17              
  Lines        5212     5269      +57     
==========================================
+ Hits         3818     3890      +72     
+ Misses       1394     1379      -15     
Files with missing lines Coverage Δ
python/adjoint/filters.py 79.89% <92.94%> (+19.31%) ⬆️

@mawc2019
Copy link
Contributor Author

After restoring +1 in calculating the array size for the material grid, some tests in test_adjoint_solver.py fail when the single-precision floating point is used. The relevant tolerances need to be increased in order to make those tests pass.

Python 3.7:

======================================================================
FAIL: test_eigenmode (__main__.TestAdjointSolver)
Verifies that the adjoint gradient for an objective function based
----------------------------------------------------------------------
Traceback (most recent call last):
  File "../../../python/tests/test_adjoint_solver.py", line 592, in test_eigenmode
    self.assertClose(adj_dd, fnd_dd, epsilon=tol)
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/python/tests/utils.py", line 30, in assertClose
    self.assertLessEqual(diff_norm, epsilon * np.maximum(x_norm, y_norm), msg)
AssertionError: 2.4692787936065122e-05 not less than or equal to 2.147141650190434e-05 : 

======================================================================
FAIL: test_ldos (__main__.TestAdjointSolver)
Verifies that the adjoint gradient for an objective function based
----------------------------------------------------------------------
Traceback (most recent call last):
  File "../../../python/tests/test_adjoint_solver.py", line 621, in test_ldos
    self.assertClose(adj_dd, fnd_dd, epsilon=tol)
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/python/tests/utils.py", line 30, in assertClose
    self.assertLessEqual(diff_norm, epsilon * np.maximum(x_norm, y_norm), msg)
AssertionError: 1.866014771444676e-10 not less than or equal to 7.504694841953264e-11 : 

Python 3.10:

======================================================================

FAIL: test_DFT_fields (__main__.TestAdjointSolver)
Verifies that the adjoint gradient for an objective function based
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/_build/sub/python/../../../python/tests/test_adjoint_solver.py", line 551, in test_DFT_fields
    self.assertClose(adj_dd, fnd_dd, epsilon=tol)
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/python/tests/utils.py", line 30, in assertClose
    self.assertLessEqual(diff_norm, epsilon * np.maximum(x_norm, y_norm), msg)
AssertionError: 0.00032576555598241895 not less than or equal to 0.00015930181276331502 : 

======================================================================
FAIL: test_eigenmode (__main__.TestAdjointSolver)
Verifies that the adjoint gradient for an objective function basedFAILED (failures=3)

----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/_build/sub/python/../../../python/tests/test_adjoint_solver.py", line 592, in test_eigenmode
    self.assertClose(adj_dd, fnd_dd, epsilon=tol)
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/python/tests/utils.py", line 30, in assertClose
    self.assertLessEqual(diff_norm, epsilon * np.maximum(x_norm, y_norm), msg)
AssertionError: 6.287598863467803e-05 not less than or equal to 3.214987858228824e-05 : 

======================================================================
FAIL: test_ldos (__main__.TestAdjointSolver)
Verifies that the adjoint gradient for an objective function based
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/_build/sub/python/../../../python/tests/test_adjoint_solver.py", line 621, in test_ldos
    self.assertClose(adj_dd, fnd_dd, epsilon=tol)
  File "/home/runner/work/meep/meep/build/meep-1.27.0-beta/python/tests/utils.py", line 30, in assertClose
    self.assertLessEqual(diff_norm, epsilon * np.maximum(x_norm, y_norm), msg)
AssertionError: 2.0344705001442236e-10 not less than or equal to 1.5012644966061473e-10 : 

@stevengj
Copy link
Collaborator

Just split the off-by-one changes to a separate PR.

python/adjoint/filters.py Outdated Show resolved Hide resolved
@mawc2019
Copy link
Contributor Author

This PR also makes lengthscale constraint functions applicable to 2d periodic design patterns. The new test in test_adjoint_solver.py checks that outputs of constraint_solid() and constraint_void() are invariant when a design pattern is shifted along periodic directions. Because each of the lengthscale constraint functions contains a filter function, tests exclusively for periodic filter functions are not included.

@stevengj stevengj merged commit 9bc3f2c into NanoComp:master Apr 28, 2023
eta_d = 1 - eta_e
beta, eta = 10, 0.5
radius, c = 0.3, 400
places = 18
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems way too low (smaller than machine precision). Are you sure this test is actually doing anything with the tolerance set so low?

Copy link
Collaborator

Choose a reason for hiding this comment

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

By making the tolerance so low it's basically checking that the value is invariant to machine precision.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But I agree that the places should probably be smaller.

Copy link
Collaborator

@stevengj stevengj Apr 28, 2023

Choose a reason for hiding this comment

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

@mawc2019 says that the absolute magnitude of the constraint functions is about 1e-4, so this is asking it to match to 14 digits. So this is okay, but I would reduce it to places=16 just to be safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants