Skip to content

Commit

Permalink
tyler fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yaugenst-flex committed Aug 28, 2024
1 parent b182602 commit 2a7916c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
17 changes: 11 additions & 6 deletions gradcheck_nb.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def make_structures(params, apply_symmetry: bool = True):
if apply_symmetry and symmetry[1] != 0 and y0 < -S / 2:
continue

# geometry = td.Box(center=(x0, y0, center_z), size=(size, size, H))
geometry = td.Cylinder(center=(x0, y0, center_z), length=H, radius=size / 2)
geometry = td.Box(center=(x0, y0, center_z), size=(size, size, H))
# geometry = td.Cylinder(center=(x0, y0, center_z), length=H, radius=size / 2)

geometries.append(geometry)
geo_group = td.GeometryGroup(geometries=geometries)
Expand Down Expand Up @@ -235,14 +235,17 @@ def measure_focal_intensity(sim_data: td.SimulationData) -> float:

far_fields = n2f.project_fields(monitor_far)
# return anp.sum(anp.abs(far_fields.Etheta.values))
return abs(far_fields.power.values.item())
return far_fields.power.values.item()


def J(params) -> float:
"""Objective function, returns intensity at focal point as a function of params."""
sim = make_sim(params)
# sim.plot(z=0)
# plt.show()
# exit()
# sim = make_sim(np.zeros(x_centers.shape))
sim_data = run_adj(sim, task_name="metalens_invdes", verbose=True)
sim_data = run_adj(sim, task_name="metalens_invdes_dbg", verbose=True)
# sim_data = make_sim_data(params, fp="simulation_data.hdf5")
# sim_data = td.SimulationData.from_file("simulation_data.hdf5")
return measure_focal_intensity(sim_data)
Expand All @@ -255,7 +258,9 @@ def J(params) -> float:
# # print(J(params0))
dJ = ag.value_and_grad(J)
val, grad = dJ(params0)
print(val, grad)
print("val: ", val)
print("grad: ", grad)
print("|grad|: ", np.linalg.norm(grad))
# exit()

# check_grads(J, modes=["rev"], order=1)(params0)
Expand Down Expand Up @@ -296,7 +301,7 @@ def J_normalized(params):
print(f"\tgrad_norm = {np.linalg.norm(gradient):.4e}")

# compute and apply updates to the optimizer based on gradient (-1 sign to maximize obj_fn)
updates, opt_state = optimizer.update(gradient, opt_state, params)
updates, opt_state = optimizer.update(-gradient, opt_state, params)
params = optax.apply_updates(params, updates)
params = np.array(params)

Expand Down
2 changes: 1 addition & 1 deletion tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ def shift_value(coords) -> float:
for name, field_component in self.field_components.items():
field_component = field_component.sel(f=freq0)
forward_amps = field_component.values
values = -1j * forward_amps
values = np.conj(forward_amps)
if "H" in name:
values *= -1
coords = dict(field_component.coords.copy())
Expand Down
3 changes: 0 additions & 3 deletions tidy3d/web/api/autograd/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,6 @@ def setup_adj(
td.log.info("Running custom vjp (adjoint) pipeline.")

# immediately filter out any data_vjps with all 0's in the data
# data_fields_vjp = {
# key: get_static(value) for key, value in data_fields_vjp.items() if not np.all(value == 0.0)
# }
data_fields_vjp = {key: get_static(value) for key, value in data_fields_vjp.items()}

# insert the raw VJP data into the .data of the original SimulationData
Expand Down

0 comments on commit 2a7916c

Please sign in to comment.