@@ -395,10 +395,10 @@ def make_structures(params: anp.ndarray) -> dict[str, td.Structure]:
395
395
custom_pole_res = td .Structure (geometry = box , medium = custom_med_pole_res )
396
396
397
397
radius = 0.4 * (1 + anp .abs (vector @ params ))
398
- cyl_center_y = 0 # vector @ params
399
- cyl_center_z = 0 # -vector @ params
398
+ cyl_center_y = vector @ params
399
+ cyl_center_z = - vector @ params
400
400
cylinder_geo = td .Cylinder (
401
- radius = radii ,
401
+ radius = anp . mean ( radii ) * 0.5 ,
402
402
center = (0 , cyl_center_y , cyl_center_z ),
403
403
axis = 0 ,
404
404
length = LX / 2 if IS_3D else td .inf ,
@@ -770,43 +770,67 @@ def objective(*args):
770
770
def test_autograd_polyslab_cylinder (use_emulated_run , monitor_key ):
771
771
"""Test an objective function through tidy3d autograd."""
772
772
773
- fn_dict_polyslab = get_functions ( "polyslab" , monitor_key )
774
- make_sim_polyslab = fn_dict_polyslab [ "sim" ]
773
+ t = 1.0
774
+ axis = 0
775
775
776
- fn_dict_cylinder = get_functions ("cylinder" , monitor_key )
777
- make_sim_cylinder = fn_dict_cylinder ["sim" ]
776
+ num_pts = 89
778
777
779
- postprocess = fn_dict_cylinder ["postprocess" ]
778
+ monitor , postprocess = make_monitors ()[monitor_key ]
779
+
780
+ def make_cylinder (radius , x0 , y0 ):
781
+ return td .Cylinder (
782
+ center = td .Cylinder .unpop_axis (0.0 , (x0 , y0 ), axis = axis ),
783
+ radius = radius ,
784
+ length = t ,
785
+ axis = axis ,
786
+ ) # .to_polyslab(num_pts)
787
+
788
+ def make_polyslab (radius , x0 , y0 ):
789
+ phis = anp .linspace (0 , 2 * np .pi , num_pts + 1 )[:- 1 ]
790
+
791
+ xs = radius * anp .cos (phis ) + x0
792
+ ys = radius * anp .sin (phis ) + y0
793
+
794
+ vertices = anp .stack ((xs , ys ), axis = - 1 )
795
+
796
+ return td .PolySlab (
797
+ vertices = vertices ,
798
+ axis = axis ,
799
+ slab_bounds = (- t / 2 , t / 2 ),
800
+ )
780
801
781
- def objective_polyslab (* args ):
802
+ def make_sim (params , geo_maker ):
803
+ geo = geo_maker (* params )
804
+ structure = td .Structure (geometry = geo , medium = td .Medium (permittivity = 2 ))
805
+
806
+ return SIM_BASE .updated_copy (structures = [structure ], monitors = [monitor ])
807
+
808
+ p0 = [1.0 , 0.0 , 0.0 ]
809
+
810
+ def objective_polyslab (params ):
782
811
"""Objective function."""
783
- sim = make_sim_polyslab ( * args )
812
+ sim = make_sim ( params , geo_maker = make_polyslab )
784
813
if PLOT_SIM :
785
814
plot_sim (sim , plot_eps = True )
786
815
data = run (sim , task_name = "autograd_test" , verbose = False )
787
- value = postprocess (data )
788
- return value
816
+ return anp .sum (anp .abs (data [monitor .name ].amps )).item ()
789
817
790
- val_polyslab , grad_polyslab = ag .value_and_grad (objective_polyslab )(params0 )
818
+ val_polyslab , grad_polyslab = ag .value_and_grad (objective_polyslab )(p0 )
791
819
print (val_polyslab , grad_polyslab )
792
820
assert anp .all (grad_polyslab != 0.0 ), "some gradients are 0"
793
821
794
- def objective_cylinder (* args ):
822
+ def objective_cylinder (params ):
795
823
"""Objective function."""
796
- sim = make_sim_cylinder ( * args )
824
+ sim = make_sim ( params , geo_maker = make_cylinder )
797
825
if PLOT_SIM :
798
826
plot_sim (sim , plot_eps = True )
799
827
data = run (sim , task_name = "autograd_test" , verbose = False )
800
- value = postprocess (data )
801
- return value
828
+ return anp .sum (anp .abs (data [monitor .name ].amps )).item ()
802
829
803
- val_cylinder , grad_cylinder = ag .value_and_grad (objective_cylinder )(params0 )
830
+ val_cylinder , grad_cylinder = ag .value_and_grad (objective_cylinder )(p0 )
804
831
print (val_cylinder , grad_cylinder )
805
832
assert anp .all (grad_cylinder != 0.0 ), "some gradients are 0"
806
833
807
- # just make sure they're somewhat close (use different discretizations)
808
- assert np .allclose (grad_cylinder , grad_polyslab , rtol = 0.3 )
809
-
810
834
811
835
@pytest .mark .parametrize ("structure_key, monitor_key" , args )
812
836
def test_autograd_server (use_emulated_run , structure_key , monitor_key ):
0 commit comments