-
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
Incorrect adjoint fields in cylindrical coordinates #2193
Comments
In your |
Here is the results with the above printf statement:
After:
|
Hmm is it possible the I remember for #1855 I had to modify the way I flipped the meep/python/adjoint/optimization_problem.py Line 233 in ea43f83
I'm not sure the current code to change the meep/python/adjoint/optimization_problem.py Lines 263 to 264 in ea43f83
(Note that we still have the following line... which I don't think does anything either... if it does we need to remove it!) meep/python/adjoint/optimization_problem.py Lines 267 to 268 in ea43f83
EDIT: I'm guessing this is the issue, as my gradients for a simulation with |
I don't think this is the main issue, at least not the only issue. I ran the test with
The purposes were to reset the parameter to the value in the forward simulation, so that we could subsequently run |
I slightly modified your example: import meep as mp
import meep.adjoint as mpa
import numpy as np
from autograd import numpy as npa
from matplotlib import pyplot as plt
np.random.rand(240)
mp.verbosity(0)
resolution = 20
Si = mp.Medium(epsilon=13)
design_region_width = 1.0
design_region_height = 1.0
pml_size = 1.0
padl, padr, padz = 0.5 , 0.5, 0.5
Sr = design_region_width + pml_size + padl + padr
Sz = 2*pml_size + design_region_height + 2*padz + 1
cell_size = mp.Vector3(Sr,0,Sz)
pml_layers = [mp.PML(pml_size)]
nf = 2
fcen = 1/1.55
width = 0.1
fwidth = width * fcen
source_center = mp.Vector3(padl+design_region_width/2,0,-padz-design_region_height/2)
source_size = mp.Vector3(design_region_width,0,0)
src = mp.GaussianSource(frequency=fcen,fwidth=fwidth)
source = [mp.Source(src,component=mp.Ep,center=source_center, size=source_size)]
design_region_resolution = 20
Nx = int(design_region_resolution*design_region_width)
Ny = int(design_region_resolution*design_region_height)
design_variables = mp.MaterialGrid(mp.Vector3(Nx,1, Ny),mp.air, Si, do_averaging=False)
design_region = mpa.DesignRegion(design_variables,volume=mp.Volume(center=mp.Vector3(padl+design_region_width/2, 0, 0), size=mp.Vector3(design_region_width, 0, design_region_height)))
geometry = [mp.Block(center=design_region.center, size=design_region.size, material=design_variables)] # design region
dimensions = mp.CYLINDRICAL
sim = mp.Simulation(cell_size=cell_size, boundary_layers=pml_layers, geometry=geometry, sources=source, default_material=mp.air,
resolution=resolution, dimensions=dimensions, m=0, force_complex_fields=True)
far_x = [mp.Vector3(0.1,0,20)]
NearRegions = [mp.Near2FarRegion(center=mp.Vector3(padl+(design_region_width)/2,0,padz+design_region_height/2),
size=mp.Vector3(0.5,0,0), weight=+1)]
FarFields = mpa.Near2FarFields(sim, NearRegions ,far_x)
ob_list = [FarFields]
def J(ff):
return npa.abs(ff[0,0,1])**2
obj_fs = [J]
opt = mpa.OptimizationProblem(
simulation = sim,
objective_functions = obj_fs,
objective_arguments = ob_list,
design_regions = [design_region],
frequencies = [fcen]
)
x = 0.5*np.ones(Nx*Ny)
## ensure reproducible results
np.random.seed(314159)
deps = 1e-5
dp = deps*np.random.rand(Nx*Ny)
# adjoint simulation
f0, adj = opt([x])
adjoint_dd = (dp@adj).flatten() # adjoint directional derivative
# finite difference approximation
opt.update_design([x+dp/2])
f_pp, _ = opt(need_gradient=False)
opt.update_design([x-dp/2])
f_pm, _ = opt(need_gradient=False)
fd_grad = f_pp-f_pm
# errors
abs_error = np.abs(adjoint_dd-fd_grad)
rel_error = abs_error / np.abs(fd_grad)
print("Directional derivative -- adjoint solver: {}, FD: {}|| rel_error = {} || abs_error = {}".format(adjoint_dd,fd_grad,rel_error,abs_error)) Using #2194 and
Which is pretty accurate... I modified some of the above parameters (e.g. the size of the design region) just to stress test it a bit. It seems good so far, but there are definitely more tests we need to do. However, this doesn't work if
As I mention in #2194, I get the same result even if I don't change the
I spent more time looking into this. The Cartesian cases actually seem rather robust. I still need to look at the cases introduced by @mawc2019... but I have a feeling those are more "edge" cases. Note that #2194 modified a few lines of the recombination step which seems to help with accuracy. |
There is an issue with the gradients after PR #1855. Specifically, if I plot the forward fields, adjoint fields, and coordinates of the points from this line:
meep/src/meepgeom.cpp
Lines 2925 to 2927 in 2aa9164
The forward fields are the same as before the PR, yet the adjoint fields are different compared to before.
cc @smartalecH
Here is a simple test case (in cylindrical coordinates) I used:
The fields before PR:
The fields after PR:
The text was updated successfully, but these errors were encountered: