Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
yaugenst-flex committed Aug 28, 2024
1 parent e61440b commit 9e6d6bd
Show file tree
Hide file tree
Showing 7 changed files with 850 additions and 18 deletions.
62 changes: 47 additions & 15 deletions farfield_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from autograd import grad, value_and_grad
from autograd.test_util import check_grads
from autograd.tracer import getval
from scipy.ndimage import gaussian_filter

import tidy3d as td
from tidy3d.web import run
Expand All @@ -31,20 +32,20 @@ def get_spatial_coords_dict(simulation: td.Simulation, monitor: td.Monitor, fiel
return coords


def make_field_data(x, sim, monitor, rng) -> td.FieldData:
def make_field_data_random(x, sim, monitor, rng) -> td.FieldData:
field_cmps = {}
grid = sim.discretize_monitor(monitor)
for field_name in monitor.fields:
coords = get_spatial_coords_dict(sim, monitor, field_name)
coords["f"] = list(monitor.freqs)

data_shape = [len(coords[k]) for k in td.ScalarFieldDataArray._dims]
data_shape = np.array([len(coords[k]) for k in td.ScalarFieldDataArray._dims])
dims = np.where(data_shape > 1)[0]

x = x[:, None]
s = (1, np.prod(data_shape))
re = np.mean(x @ rng.uniform(-1, 1, s), axis=0)
im = np.mean(x @ rng.uniform(-1, 1, s), axis=0)
data = np.reshape(re + 1j * im, data_shape)
re = gaussian_filter(rng.uniform(-10, 10, min(data_shape[dims])), 1)
im = gaussian_filter(rng.uniform(-10, 10, min(data_shape[dims])), 1)
data = np.outer(x[: x.size // 2] ** 2, re) + 1j * np.outer(x[x.size // 2 :] ** 2, im)
data = np.reshape(data, data_shape)

field_cmps[field_name] = td.ScalarFieldDataArray(data, coords=coords)

Expand All @@ -57,11 +58,42 @@ def make_field_data(x, sim, monitor, rng) -> td.FieldData:
)


def make_sim_data(x, sim, seed):
rng = np.random.default_rng(seed)
data = [
make_field_data(x, sim, mnt, rng) for mnt in sim.monitors if type(mnt) is td.FieldMonitor
]
def make_field_data_from_file(x, monitor, sim_data) -> td.FieldData:
field_cmps = {}
field_data = sim_data[monitor.name]
for c in field_data.field_components:
field = getattr(field_data, c)
f = field.values
data = x[0] * np.real(f) + 1j * x[1] * np.imag(f)
field_cmps[c] = td.ScalarFieldDataArray(data, coords=field.coords)

return td.FieldData(
monitor=field_data.monitor,
symmetry=field_data.symmetry,
symmetry_center=field_data.symmetry_center,
grid_expanded=field_data.grid_expanded,
**field_cmps,
)


def make_sim_data(x, sim=None, seed=None, fp=None):
if fp is None and sim is not None:
rng = np.random.default_rng(seed)
data = [
make_field_data_random(x, sim, mnt, rng, fp)
for mnt in sim.monitors
if type(mnt) is td.FieldMonitor
]
elif fp is not None:
sim_data = td.SimulationData.from_file(fp)
sim = sim_data.simulation
data = [
make_field_data_from_file(x, mnt, sim_data)
for mnt in sim.monitors
if type(mnt) is td.FieldMonitor
]
else:
raise ValueError("yeah no")
return td.SimulationData(simulation=sim, data=data)


Expand Down Expand Up @@ -143,7 +175,7 @@ def objective(x, *, is_2d=False, local=False, emulated=False, seed=None):
fp = f'autograd_projection_dbg_{"2d" if is_2d else "3d"}.hdf5'
if emulated: # static sim, traced sim data
sim = make_simulation(1.0, is_2d=is_2d)
sim_data = make_sim_data(x, sim, seed or 3467643734)
sim_data = make_sim_data(x, sim, fp="simulation_data.hdf5")
elif Path(fp).is_file() and local:
sim = make_simulation(x, is_2d=is_2d)
sim_data = td.SimulationData.from_file(fp)
Expand All @@ -169,9 +201,9 @@ def objective(x, *, is_2d=False, local=False, emulated=False, seed=None):
def main():
seed = 46435543
np.random.seed(seed) # global seed for autograd
x = np.random.uniform(-1, 1, 10)
x = np.random.uniform(-10, 10, 2 * 71)

# objective(x, emulated=True, seed=seed)
# print(grad(objective)(x, emulated=True, seed=seed))
check_grads(objective, modes=["rev"], order=1)(x, emulated=True, seed=seed)


Expand Down
Loading

0 comments on commit 9e6d6bd

Please sign in to comment.