-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathtest_adjoint_solver.py
540 lines (411 loc) · 20.7 KB
/
test_adjoint_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import meep as mp
try:
import meep.adjoint as mpa
except:
import adjoint as mpa
import numpy as np
from autograd import numpy as npa
from autograd import tensor_jacobian_product
import unittest
from enum import Enum
from utils import ApproxComparisonTestCase
MonitorObject = Enum('MonitorObject', 'EIGENMODE DFT')
resolution = 30
silicon = mp.Medium(epsilon=12)
sapphire = mp.Medium(epsilon_diag=(10.225,10.225,9.95),
epsilon_offdiag=(-0.825,-0.55*np.sqrt(3/2),0.55*np.sqrt(3/2)))
sxy = 5.0
cell_size = mp.Vector3(sxy,sxy,0)
dpml = 1.0
pml_xy = [mp.PML(thickness=dpml)]
pml_x = [mp.PML(thickness=dpml,direction=mp.X)]
eig_parity = mp.EVEN_Y + mp.ODD_Z
design_region_size = mp.Vector3(1.5,1.5)
design_region_resolution = int(2*resolution)
Nx = int(design_region_resolution*design_region_size.x) + 1
Ny = int(design_region_resolution*design_region_size.y) + 1
## ensure reproducible results
rng = np.random.RandomState(9861548)
## random design region
p = rng.rand(Nx*Ny)
## random epsilon perturbation for design region
deps = 1e-5
dp = deps*rng.rand(Nx*Ny)
w = 1.0
waveguide_geometry = [mp.Block(material=silicon,
center=mp.Vector3(),
size=mp.Vector3(mp.inf,w,mp.inf))]
fcen = 1/1.55
df = 0.23*fcen
wvg_source = [mp.EigenModeSource(src=mp.GaussianSource(fcen,fwidth=df),
center=mp.Vector3(-0.5*sxy+dpml+0.1,0),
size=mp.Vector3(0,sxy-2*dpml),
eig_parity=eig_parity)]
pt_source = [mp.Source(src=mp.GaussianSource(fcen,fwidth=df),
center=mp.Vector3(-0.5*sxy+dpml,0),
size=mp.Vector3(),
component=mp.Ez)]
k_point = mp.Vector3(0.23,-0.38)
def forward_simulation(design_params, mon_type, frequencies=None, mat2=silicon):
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
mat2,
weights=design_params.reshape(Nx,Ny))
matgrid_geometry = [mp.Block(center=mp.Vector3(),
size=mp.Vector3(design_region_size.x,design_region_size.y,0),
material=matgrid)]
geometry = waveguide_geometry + matgrid_geometry
sim = mp.Simulation(resolution=resolution,
cell_size=cell_size,
boundary_layers=pml_xy,
sources=wvg_source,
geometry=geometry)
if not frequencies:
frequencies = [fcen]
if mon_type.name == 'EIGENMODE':
mode = sim.add_mode_monitor(frequencies,
mp.ModeRegion(center=mp.Vector3(0.5*sxy-dpml-0.1),
size=mp.Vector3(0,sxy-2*dpml,0)),
yee_grid=True,
eig_parity=eig_parity)
elif mon_type.name == 'DFT':
mode = sim.add_dft_fields([mp.Ez],
frequencies,
center=mp.Vector3(1.25),
size=mp.Vector3(0.25,1,0),
yee_grid=False)
sim.run(until_after_sources=mp.stop_when_dft_decayed())
if mon_type.name == 'EIGENMODE':
coeff = sim.get_eigenmode_coefficients(mode,[1],eig_parity).alpha[0,:,0]
S12 = np.power(np.abs(coeff),2)
elif mon_type.name == 'DFT':
Ez2 = []
for f in range(len(frequencies)):
Ez_dft = sim.get_dft_array(mode, mp.Ez, f)
Ez2.append(np.power(np.abs(Ez_dft[4,10]),2))
Ez2 = np.array(Ez2)
sim.reset_meep()
if mon_type.name == 'EIGENMODE':
return S12
elif mon_type.name == 'DFT':
return Ez2
def adjoint_solver(design_params, mon_type, frequencies=None, mat2=silicon):
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
mat2,
weights=np.ones((Nx,Ny)))
matgrid_region = mpa.DesignRegion(matgrid,
volume=mp.Volume(center=mp.Vector3(),
size=mp.Vector3(design_region_size.x,
design_region_size.y,
0)))
matgrid_geometry = [mp.Block(center=matgrid_region.center,
size=matgrid_region.size,
material=matgrid)]
geometry = waveguide_geometry + matgrid_geometry
sim = mp.Simulation(resolution=resolution,
cell_size=cell_size,
boundary_layers=pml_xy,
sources=wvg_source,
geometry=geometry)
if not frequencies:
frequencies = [fcen]
if mon_type.name == 'EIGENMODE':
obj_list = [mpa.EigenmodeCoefficient(sim,
mp.Volume(center=mp.Vector3(0.5*sxy-dpml-0.1),
size=mp.Vector3(0,sxy-2*dpml,0)),
1,
eig_parity=eig_parity)]
def J(mode_mon):
return npa.power(npa.abs(mode_mon),2)
elif mon_type.name == 'DFT':
obj_list = [mpa.FourierFields(sim,
mp.Volume(center=mp.Vector3(1.25),
size=mp.Vector3(0.25,1,0)),
mp.Ez)]
def J(mode_mon):
return npa.power(npa.abs(mode_mon[:,4,10]),2)
opt = mpa.OptimizationProblem(
simulation=sim,
objective_functions=J,
objective_arguments=obj_list,
design_regions=[matgrid_region],
frequencies=frequencies)
f, dJ_du = opt([design_params])
sim.reset_meep()
return f, dJ_du
def forward_simulation_complex_fields(design_params, frequencies=None):
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
silicon,
weights=design_params.reshape(Nx,Ny))
geometry = [mp.Block(center=mp.Vector3(),
size=mp.Vector3(design_region_size.x,
design_region_size.y,
0),
material=matgrid)]
sim = mp.Simulation(resolution=resolution,
cell_size=cell_size,
k_point=k_point,
boundary_layers=pml_x,
sources=pt_source,
geometry=geometry)
if not frequencies:
frequencies = [fcen]
mode = sim.add_dft_fields([mp.Ez],
frequencies,
center=mp.Vector3(0.9),
size=mp.Vector3(0.2,0.5),
yee_grid=False)
sim.run(until_after_sources=mp.stop_when_dft_decayed())
Ez2 = []
for f in range(len(frequencies)):
Ez_dft = sim.get_dft_array(mode, mp.Ez, f)
Ez2.append(np.power(np.abs(Ez_dft[3,9]),2))
Ez2 = np.array(Ez2)
sim.reset_meep()
return Ez2
def adjoint_solver_complex_fields(design_params, frequencies=None):
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
silicon,
weights=np.ones((Nx,Ny)))
matgrid_region = mpa.DesignRegion(matgrid,
volume=mp.Volume(center=mp.Vector3(),
size=mp.Vector3(design_region_size.x,
design_region_size.y,
0)))
geometry = [mp.Block(center=matgrid_region.center,
size=matgrid_region.size,
material=matgrid)]
sim = mp.Simulation(resolution=resolution,
cell_size=cell_size,
k_point=k_point,
boundary_layers=pml_x,
sources=pt_source,
geometry=geometry)
if not frequencies:
frequencies = [fcen]
obj_list = [mpa.FourierFields(sim,
mp.Volume(center=mp.Vector3(0.9),
size=mp.Vector3(0.2,0.5)),
mp.Ez)]
def J(dft_mon):
return npa.power(npa.abs(dft_mon[:,3,9]),2)
opt = mpa.OptimizationProblem(
simulation=sim,
objective_functions=J,
objective_arguments=obj_list,
design_regions=[matgrid_region],
frequencies=frequencies)
f, dJ_du = opt([design_params])
sim.reset_meep()
return f, dJ_du
def forward_simulation_damping(design_params, frequencies=None, mat2=silicon):
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
mat2,
weights=design_params.reshape(Nx,Ny),
damping = 3.14*fcen)
matgrid_geometry = [mp.Block(center=mp.Vector3(),
size=mp.Vector3(design_region_size.x,design_region_size.y,0),
material=matgrid)]
geometry = waveguide_geometry + matgrid_geometry
sim = mp.Simulation(resolution=resolution,
cell_size=cell_size,
boundary_layers=pml_xy,
sources=wvg_source,
geometry=geometry)
if not frequencies:
frequencies = [fcen]
mode = sim.add_mode_monitor(frequencies,
mp.ModeRegion(center=mp.Vector3(0.5*sxy-dpml-0.1),
size=mp.Vector3(0,sxy-2*dpml,0)),
yee_grid=True,
eig_parity=eig_parity)
sim.run(until_after_sources=mp.stop_when_dft_decayed())
coeff = sim.get_eigenmode_coefficients(mode,[1],eig_parity).alpha[0,:,0]
S12 = np.power(np.abs(coeff),2)
sim.reset_meep()
return S12
def adjoint_solver_damping(design_params, frequencies=None, mat2=silicon):
matgrid = mp.MaterialGrid(mp.Vector3(Nx,Ny),
mp.air,
mat2,
weights=np.ones((Nx,Ny)),
damping = 3.14*fcen)
matgrid_region = mpa.DesignRegion(matgrid,
volume=mp.Volume(center=mp.Vector3(), size=mp.Vector3(design_region_size.x, design_region_size.y, 0)))
matgrid_geometry = [mp.Block(center=matgrid_region.center,
size=matgrid_region.size,
material=matgrid)]
geometry = waveguide_geometry + matgrid_geometry
sim = mp.Simulation(resolution=resolution,
cell_size=cell_size,
boundary_layers=pml_xy,
sources=wvg_source,
geometry=geometry)
if not frequencies:
frequencies = [fcen]
obj_list = [mpa.EigenmodeCoefficient(sim, mp.Volume(center=mp.Vector3(0.5*sxy-dpml-0.1),
size=mp.Vector3(0,sxy-2*dpml,0)), 1, eig_parity=eig_parity)]
def J(mode_mon):
return npa.power(npa.abs(mode_mon),2)
opt = mpa.OptimizationProblem(
simulation=sim,
objective_functions=J,
objective_arguments=obj_list,
design_regions=[matgrid_region],
frequencies=frequencies,
minimum_run_time=150)
f, dJ_du = opt([design_params])
sim.reset_meep()
return f, dJ_du
def mapping(x,filter_radius,eta,beta):
filtered_field = mpa.conic_filter(x,
filter_radius,
design_region_size.x,
design_region_size.y,
design_region_resolution)
projected_field = mpa.tanh_projection(filtered_field,beta,eta)
return projected_field.flatten()
class TestAdjointSolver(ApproxComparisonTestCase):
def test_adjoint_solver_DFT_fields(self):
print("*** TESTING DFT ADJOINT ***")
## test the single frequency and multi frequency cases
for frequencies in [[fcen], [1/1.58, fcen, 1/1.53]]:
## compute gradient using adjoint solver
adjsol_obj, adjsol_grad = adjoint_solver(p, MonitorObject.DFT, frequencies)
## compute unperturbed |Ez|^2
Ez2_unperturbed = forward_simulation(p, MonitorObject.DFT, frequencies)
## compare objective results
print("|Ez|^2 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,Ez2_unperturbed))
self.assertClose(adjsol_obj,Ez2_unperturbed,epsilon=1e-6)
## the DFT fields finite differences are more sensitive in single precision
deps_dft = 1e-4 if mp.is_single_precision() else 1e-5
dp_dft = deps_dft*rng.rand(Nx*Ny)
## compute perturbed Ez2
Ez2_perturbed = forward_simulation(p+dp_dft, MonitorObject.DFT, frequencies)
## compare gradients
if adjsol_grad.ndim < 2:
adjsol_grad = np.expand_dims(adjsol_grad,axis=1)
adj_scale = (dp_dft[None,:]@adjsol_grad).flatten()
fd_grad = Ez2_perturbed-Ez2_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
tol = 0.0461 if mp.is_single_precision() else 0.005
self.assertClose(adj_scale,fd_grad,epsilon=tol)
def test_adjoint_solver_eigenmode(self):
print("*** TESTING EIGENMODE ADJOINT ***")
## test the single frequency and multi frequency case
for frequencies in [[fcen], [1/1.58, fcen, 1/1.53]]:
## compute gradient using adjoint solver
adjsol_obj, adjsol_grad = adjoint_solver(p, MonitorObject.EIGENMODE, frequencies)
## compute unperturbed S12
S12_unperturbed = forward_simulation(p, MonitorObject.EIGENMODE, frequencies)
## compare objective results
print("S12 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
self.assertClose(adjsol_obj,S12_unperturbed,epsilon=1e-6)
## compute perturbed S12
S12_perturbed = forward_simulation(p+dp, MonitorObject.EIGENMODE, frequencies)
## compare gradients
if adjsol_grad.ndim < 2:
adjsol_grad = np.expand_dims(adjsol_grad,axis=1)
adj_scale = (dp[None,:]@adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
tol = 0.04 if mp.is_single_precision() else 0.01
self.assertClose(adj_scale,fd_grad,epsilon=tol)
def test_gradient_backpropagation(self):
print("*** TESTING BACKPROP ***")
for frequencies in [[fcen], [1/1.58, fcen, 1/1.53]]:
## filter/thresholding parameters
filter_radius = 0.21985
eta = 0.49093
beta = 4.0698
mapped_p = mapping(p,filter_radius,eta,beta)
## compute gradient using adjoint solver
adjsol_obj, adjsol_grad = adjoint_solver(mapped_p, MonitorObject.EIGENMODE, frequencies)
## backpropagate the gradient
if len(frequencies) > 1:
bp_adjsol_grad = np.zeros(adjsol_grad.shape)
for i in range(len(frequencies)):
bp_adjsol_grad[:,i] = tensor_jacobian_product(mapping,0)(p,filter_radius,eta,beta,adjsol_grad[:,i])
else:
bp_adjsol_grad = tensor_jacobian_product(mapping,0)(p,filter_radius,eta,beta,adjsol_grad)
## compute unperturbed S12
S12_unperturbed = forward_simulation(mapped_p,MonitorObject.EIGENMODE,frequencies)
## compare objective results
print("S12 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
self.assertClose(adjsol_obj,S12_unperturbed,epsilon=1e-6)
## compute perturbed S12
S12_perturbed = forward_simulation(mapping(p+dp,filter_radius,eta,beta),MonitorObject.EIGENMODE,frequencies)
if bp_adjsol_grad.ndim < 2:
bp_adjsol_grad = np.expand_dims(bp_adjsol_grad,axis=1)
adj_scale = (dp[None,:]@bp_adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
tol = 0.02 if mp.is_single_precision() else 0.01
self.assertClose(adj_scale,fd_grad,epsilon=tol)
def test_complex_fields(self):
print("*** TESTING COMPLEX FIELDS ***")
for frequencies in [[fcen], [1/1.58, fcen, 1/1.53]]:
## compute gradient using adjoint solver
adjsol_obj, adjsol_grad = adjoint_solver_complex_fields(p, frequencies)
## compute unperturbed |Ez|^2
Ez2_unperturbed = forward_simulation_complex_fields(p, frequencies)
## compare objective results
print("Ez2 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,Ez2_unperturbed))
self.assertClose(adjsol_obj,Ez2_unperturbed,epsilon=1e-6)
## compute perturbed |Ez|^2
Ez2_perturbed = forward_simulation_complex_fields(p+dp, frequencies)
## compare gradients
if adjsol_grad.ndim < 2:
adjsol_grad = np.expand_dims(adjsol_grad,axis=1)
adj_scale = (dp[None,:]@adjsol_grad).flatten()
fd_grad = Ez2_perturbed-Ez2_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
tol = 0.005 if mp.is_single_precision() else 0.0008
self.assertClose(adj_scale,fd_grad,epsilon=tol)
def test_damping(self):
print("*** TESTING CONDUCTIVITIES ***")
for frequencies in [[1/1.58, fcen, 1/1.53]]:
## compute gradient using adjoint solver
adjsol_obj, adjsol_grad = adjoint_solver_damping(p, frequencies)
## compute unperturbed S12
S12_unperturbed = forward_simulation_damping(p, frequencies)
## compare objective results
print("S12 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
self.assertClose(adjsol_obj,S12_unperturbed,epsilon=1e-6)
## compute perturbed S12
S12_perturbed = forward_simulation_damping(p+dp, frequencies)
## compare gradients
if adjsol_grad.ndim < 2:
adjsol_grad = np.expand_dims(adjsol_grad,axis=1)
adj_scale = (dp[None,:]@adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
tol = 0.06 if mp.is_single_precision() else 0.03
self.assertClose(adj_scale,fd_grad,epsilon=tol)
def test_offdiagonal(self):
print("*** TESTING OFFDIAGONAL COMPONENTS ***")
## test the single frequency and multi frequency case
for frequencies in [[fcen], [1/1.58, fcen, 1/1.53]]:
## compute gradient using adjoint solver
adjsol_obj, adjsol_grad = adjoint_solver(p, MonitorObject.EIGENMODE, frequencies, sapphire)
## compute unperturbed S12
S12_unperturbed = forward_simulation(p, MonitorObject.EIGENMODE, frequencies, sapphire)
## compare objective results
print("S12 -- adjoint solver: {}, traditional simulation: {}".format(adjsol_obj,S12_unperturbed))
self.assertClose(adjsol_obj,S12_unperturbed,epsilon=1e-6)
## compute perturbed S12
S12_perturbed = forward_simulation(p+dp, MonitorObject.EIGENMODE, frequencies, sapphire)
## compare gradients
if adjsol_grad.ndim < 2:
adjsol_grad = np.expand_dims(adjsol_grad,axis=1)
adj_scale = (dp[None,:]@adjsol_grad).flatten()
fd_grad = S12_perturbed-S12_unperturbed
print("Directional derivative -- adjoint solver: {}, FD: {}".format(adj_scale,fd_grad))
tol = 0.05 if mp.is_single_precision() else 0.04
self.assertClose(adj_scale,fd_grad,epsilon=tol)
if __name__ == '__main__':
unittest.main()