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

auto grab permittivity inside and outside PolySlab and Cylinder #2080

Merged
merged 1 commit into from
Dec 4, 2024

Conversation

tylerflex
Copy link
Collaborator

Fixes #1121

Approach is to call Simulation.epsilon without the structure present and then store this information in the DerivativeInfo. Then the gradient calculation can interpolate this data where it's needed, which gives a pretty good prox y to the permittivity outside the structture at that location.

Permittivity inside can still just use the Structure.medium.eps_model

Implemented for PolySlab but not yet for Box.

@tylerflex tylerflex marked this pull request as draft November 19, 2024 18:13
@tylerflex
Copy link
Collaborator Author

@yaugenst-flex this is causing the TestFieldProjection tests to give grads of nan and I dont understand why. Do you see anything in here that could be causing it?

@yaugenst-flex
Copy link
Collaborator

it's because the coordinates are outside of the interpolation range,:

eps_out = self.evaluate_flds_at(
fld_dataset={key: self.eps_no_structure},
spatial_coords=spatial_coords,
)[key]

where self.eps_no_structure only has z=0.05, but spatial_coords are sampled at z=0.

xarray.DataArray.interp() will use the default fill_value specified in scipy.interpolate.interpn() in that case, which is nan.

this can be avoided by setting kwargs={"bounds_error": False, "fill_value": None}, in which case the values will be extrapolated.

not sure if that is the correct solution though, probably the coordinates should be aligned before interpolation in this case. or we use nearest interpolation with fill_value=None in that situation.

@tylerflex
Copy link
Collaborator Author

hm, ok I'll try this. is there a good notebook to test?

@tylerflex tylerflex force-pushed the tyler/autograd_/auto_grab_eps branch 2 times, most recently from 946283d to 79c4bd0 Compare November 20, 2024 18:32
@tylerflex tylerflex marked this pull request as ready for review November 20, 2024 18:32
@tylerflex tylerflex changed the title auto grab permittivity outside structures auto grab permittivity inside and outside PolySlab Nov 20, 2024
@tylerflex tylerflex force-pushed the tyler/autograd_/auto_grab_eps branch from 9f4c0eb to e2e659d Compare November 20, 2024 18:53
@tylerflex tylerflex changed the title auto grab permittivity inside and outside PolySlab auto grab permittivity inside and outside PolySlab and Cylinder Nov 20, 2024
@tylerflex
Copy link
Collaborator Author

OK, so I ended up with this:

  • Automatically determines permittivity just inside and outside edges based on algorithm described earlier.
  • Decided to leave the Box implementation for another PR. We already handle that pretty generally and it screwed up the cases where two Boxes were touching at an interface. fully automatic permittivity grabbing for adjoint gradients #2084

I think this is best of both worlds, because it leaves Box alone and enables PolySlab to work properly. Tested with PhC and Metalens notebooks.

structures=structs_inf_struct,
medium=structure.medium,
monitors=[],
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Calling updated_copy on simulations always throws a red flag for me in terms of efficiency, because the simulation will get re-validated, which in some cases can be slow. In this particular case you're also doing this for every structure. I suspect this is going to get quite heavy e.g. in a metalens case where you have many structures so both re-validating each time may be costly, and it will be done many times (so at least O(N**2) scaling?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the answer here may be to refactor the Simulation.epsilon internals to have a function that can just get the epsilon of a list of structures without having to copy the simulation.

Copy link
Collaborator

@momchil-flex momchil-flex Nov 21, 2024

Choose a reason for hiding this comment

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

Also computing over the entire simulation grid sounds like a giant overkill right? I mean, maybe we can't do it close to edges only, but at least within the structure bounding box + some spacing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. It will be a performance hit for sure. We have plans to have an "autograd config" where we can toggle this on and off (off by default) depending on if the user needs this extra accuracy.
  2. For things like metalens, it's likely to be a single structure since it's a geometry group. For example I tried this for both metalens and phc notebooks and it was negligible.
  3. We can try refactoring simulation.epsilon, maybe adding a way to skip certain structures? for the inside one, I need to add the structure.medium to Simulation.medium, or make it infinitely large, which will require some modification.
  4. It would be worth also considering adding a validate: bool argument to updated_copy? I thought Weiliang implemented this somewhere recently.
  5. The epsilon is not computed over the entire simulation, just over the region defined by the .geometry of the graidient permittivity monitor.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting, thanks for the clarification!

For things like metalens, it's likely to be a single structure since it's a geometry group. For example I tried this for both metalens and phc notebooks and it was negligible.

I guess that's what we try to teach people to use, but they don't always - but fair point! So the inside and outside eps will be computed only once?

It would be worth also considering adding a validate: bool argument to updated_copy? I thought Weiliang implemented this somewhere recently.

You're right, this is currently on pre/2.8 only. Actually should we put this in 2.8 too just to avoid unexpected side-effects? It seems like there could be some since who knows what edge cases we haven't thought about.

The epsilon is not computed over the entire simulation, just over the region defined by the .geometry of the graidient permittivity monitor.

Ah ok I see.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[For geometry group] So the inside and outside eps will be computed only once?

yes

Actually should we put this in 2.8 too just to avoid unexpected side-effects?

I guess it's up to you, but that would be safer yea. Maybe I should build in some logic to preferentially skip this calculation if the user still manually supplies background_medium.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought that's what's gonna happen currently?

Yeah I think we should do 2.8 otherwise we're risking having to do another patch like few days later when we figure out some issue with this. :D

But wait for my merge before you change the base branch and cherry-pick this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, no rush

@tylerflex
Copy link
Collaborator Author

oops, accidentally force pushed and erased your merge commit @yaugenst-flex

@yaugenst-flex
Copy link
Collaborator

oops, accidentally force pushed and erased your merge commit @yaugenst-flex

yeah no problem that one was supposed to go anyways

@tylerflex tylerflex force-pushed the tyler/autograd_/auto_grab_eps branch from c66d0e0 to e3186d3 Compare November 25, 2024 21:21
@tylerflex tylerflex changed the base branch from develop to pre/2.8 November 25, 2024 21:21
@tylerflex tylerflex added 2.8 will go into version 2.8.* rc1 1st pre-release labels Nov 25, 2024
@tylerflex
Copy link
Collaborator Author

this has been rebased and tests fixed FYI

@momchil-flex momchil-flex self-requested a review December 3, 2024 12:56
Copy link
Collaborator

@momchil-flex momchil-flex left a comment

Choose a reason for hiding this comment

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

Fine by me to merge after squashing the commits. @yaugenst-flex you good on it too?

Copy link
Collaborator

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

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

Looks good!

@tylerflex tylerflex force-pushed the tyler/autograd_/auto_grab_eps branch 2 times, most recently from a8fc9be to f1c11d7 Compare December 3, 2024 18:54
@tylerflex
Copy link
Collaborator Author

this needs a bit more testing, dont merge it yet (just FYI)

@tylerflex tylerflex force-pushed the tyler/autograd_/auto_grab_eps branch from df4afa5 to e2fd9ba Compare December 4, 2024 14:43
@tylerflex
Copy link
Collaborator Author

ok I think this is ready to go. tested on a handful of notebooks with different settings and it works. Btw here's the logic for how the outside permittivity is set

if Box:

  • same logic as current, looks 2 grid cells from the interface for permittivity.

if PolySlab or Cylinder

  • automatically grabs permittivity at interface with no structure (outside) and with infinite structure (inside)
  • if the background_medium is supplied, uses this instead.

@momchil-flex
Copy link
Collaborator

Sounds good to me!

@momchil-flex momchil-flex merged commit 10958e3 into pre/2.8 Dec 4, 2024
15 checks passed
@momchil-flex momchil-flex deleted the tyler/autograd_/auto_grab_eps branch December 4, 2024 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.8 will go into version 2.8.* rc1 1st pre-release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grab boundary permittivity a few grid cells away automatically
3 participants