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

Incorrect adjoint gradients with overlapping design variables #1984

Open
mochen4 opened this issue Mar 8, 2022 · 3 comments
Open

Incorrect adjoint gradients with overlapping design variables #1984

mochen4 opened this issue Mar 8, 2022 · 3 comments

Comments

@mochen4
Copy link
Collaborator

mochen4 commented Mar 8, 2022

When the material grids of design variables are overlapped, e.g. the eigenmode tutorial example, the adjoint gradients don't agree with the finite difference gradients.

In the aforementioned example, the gradients are correct if only one block of the design region is kept in the geometry but not both:

geometry=[
...
mp.Block(center=design_region.center, size=design_region.size, material=design_variables), # design region
mp.Block(center=design_region.center, size=design_region.size, material=design_variables,
             e1=mp.Vector3(x=-1).rotate(mp.Vector3(z=1), np.pi/2), e2=mp.Vector3(y=1).rotate(mp.Vector3(z=1), np.pi/2))
]
@smartalecH
Copy link
Collaborator

IIRC, the error is a constant error, right? meaning we are just off by a scale factor (i.e. it's not as if the entire gradient is just wrong)?

If that's the case, it should be easy to debug. Perhaps we are double-counting the number of grids somewhere...

@mochen4
Copy link
Collaborator Author

mochen4 commented Mar 9, 2022

IIRC, the error is a constant error, right? meaning we are just off by a scale factor (i.e. it's not as if the entire gradient is just wrong)?

Yes, I think the gradients are off by a scale factor, but that scale varies a bit with different random structures.

@smartalecH
Copy link
Collaborator

I'm pretty sure the issue is due to the following snippet introduced in #1780.

meep/src/meepgeom.cpp

Lines 2758 to 2771 in 11545a1

if (ukind == material_data::U_MIN && u != uval) return; // TODO look into this
if (ukind == material_data::U_PROD) scaleby *= uval / u;
for (int xi=0;xi<lx;xi++){
for (int yi=0;yi<ly;yi++){
for (int zi=0;zi<lz;zi++){
int x=x_list[xi], y=y_list[yi], z=z_list[zi];
int u_idx = IDX(x,y,z);
std::complex<double> prod = adj*get_material_gradient(
r,adjoint_c,forward_c,fwd,freq,geps,gv,du,udata,u_idx);
D(x, y, z) += prod.real() * scaleby;
}
}
}

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

No branches or pull requests

2 participants