-
Notifications
You must be signed in to change notification settings - Fork 629
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
Resolve m-number adjoint bugs introduced by 1855 #2194
Conversation
Hmm I'm not sure we need that. My guess is that 2 got placed somewhere else in the earlier version of the code.... we've got 2's everywhere and it's hard to keep track sometimes... |
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #2194 +/- ##
==========================================
+ Coverage 73.09% 73.21% +0.12%
==========================================
Files 17 17
Lines 4906 4914 +8
==========================================
+ Hits 3586 3598 +12
+ Misses 1320 1316 -4
|
if needs_complex_fields and self.fields.is_real: | ||
self.fields = None | ||
self._is_initialized = False | ||
self.init_sim() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to just add a boolean argument to fields::use_real_fields
so that we can switch back to real fields without initializing. And then this could be called by fields::change_m_number
for m ≠ 1
m = new_m; | ||
for (int i = 0; i < num_chunks; i++) { | ||
chunks[i]->change_m_number(new_m); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the very least, this needs to check that new_m
is consistent with whether we have real fields and abort if not. Better yet, have it call if (new_m != 0) use_real_fields(false);
as noted above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you envision use_real_fields(false)
does in the case that the current simulation does have real fields (e.g. suppose originally m=0
but we are changing it to m=1
). Wouldn't we need to reinitialize everything?
Or are you suggesting we refactor the initialization of the fields to use_real_fields()
, such that
use_real_fields(true)
deletes the extra array (the current behavior ofuse_real_fields()
)use_real_fields(false)
reallocates the fields if needed- does nothing if the simulation state is consistent
As a first pass, it might be easiest to leave use_real_fields
as is and simply ensure there's a proper check in place for change_m_number()
(like you suggest).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My latest commit adds a check to make sure that the fields are consistent. If the user requests complex fields when the current setup is using real fields, I just abort.
This isn't a problem for the adjoint code. We have a similar check in the python routine. But rather than aborting, we just reinitialize.
python/simulation.py
Outdated
@@ -4279,6 +4279,23 @@ def change_k_point(self, k): | |||
py_v3_to_vec(self.dimensions, self.k_point, self.is_cylindrical) | |||
) | |||
|
|||
def change_m_number(self, m): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just call it change_m
?
We can simply pick a different coordinate point here meep/python/tests/test_adjoint_cyl.py Line 120 in ea43f83
I think it was just a coincidence that the #1855 passed the test at that specific point with that specific fd_gradient step size. |
Note that the unit test in meep/python/tests/test_adjoint_cyl.py Lines 18 to 19 in ea43f83
Once #2182 is resolved, we will also want to add a separate test for |
I ran more tests, and the gradients still seem to be incorrect when |
@mochen4 can you elaborate? |
Running the test code #2193 (comment) with
But with
I tried different random seeds, and I also tried comparing the gradients at individual points, and concluded that the issue was with Moreover, I also found that, at |
Actually, this mismatch between On the other hand, running #2193 (comment) with On the other hand, when |
Yes the averaging is a major issue right now. Mostly because even the FD gradients are somewhat buggy (somehow...) and that's what we are using as our ground truth... |
Surprisingly, the issue seems to be related to the structure at On the other hand, the gradients between This is observed for both this PR and the initial PR#1780. |
Like we discussed, this has to do with numerically unstable way we currently compute the spatial gradient (similar to the discussion here).
Great!
Not great...
So this is not a result of #1855, right (which is what originally caused the bug the current PR is trying to fix)? |
I updated the I noticed that the tolerance is a bit high (10%) and it reflects in the tests. There's still some decent error (5-8%) that is slipping through. This is most likely due to the averaging (which is on by default). |
Correct, I believe this PR does fix the issue of #1855. On the other hand, the mismatch between The mismatch only occurs for nonzero m values in cylindrical coordinates (with a random structure). (2D Cartesian or m=0 works fine.) The issue is probably more related to the subpixel averaging of materialgrid: the simulation results are simply different by a lot. |
Closes #2193
#1855 changed the way the adjoint simulation was assembled. Before that PR, an entirely new simulation was initialized for both the forward and adjoint runs. After the PR, a new simulation is initialized only for the forward run. The adjoint run simply updates all relevant quantities.
Unfortunately, not all quantities (e.g.
m
number) are properly updated for the adjoint simulation. This PR attempts to fix that (along with a few other cleanup steps in the recombination step).(cc @mochen4).