Skip to content
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

Add tidy3d plot epsilon #256

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions docs/notebooks/workflow_3_cascaded_mzi.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@


# %%
simulation = gt.write_sparameters(
_ = gt.write_sparameters(
coupler,
plot_simulation_layer_name="core",
layer_stack=layer_stack,
plot_simulation_layer_name="core",
)

# %% [markdown]
Expand Down Expand Up @@ -436,11 +436,17 @@ def cascaded_mzi(


# %%
@jax.jit
def complex_interp(xs, x, y):
ys_mag = jnp.interp(xs, x, jnp.abs(y))
ys_phase = jnp.interp(xs, x, jnp.unwrap(jnp.angle(y)))
return ys_mag * jnp.exp(1j * ys_phase)


@jax.jit
def straight_model(wl=1.55, length: float = 1.0):
s21 = jnp.exp(
2j * jnp.pi * jnp.interp(wl, straight_wavelengths, straight_neffs) * length / wl
)
n_eff = complex_interp(wl, straight_wavelengths, straight_neffs.real)
s21 = jnp.exp(2j * jnp.pi * n_eff * length / wl)
zero = jnp.zeros_like(wl)
return {
("o1", "o1"): zero,
Expand Down Expand Up @@ -471,8 +477,8 @@ def bend_model(cross_section: gf.typings.CrossSectionSpec = "xs_sc"):

@jax.jit
def _model(wl=1.55):
s11 = jnp.interp(wl, wavelengths, s["o1@0,o1@0"])
s21 = jnp.interp(wl, wavelengths, s["o2@0,o1@0"])
s11 = complex_interp(wl, wavelengths, s["o1@0,o1@0"])
s21 = complex_interp(wl, wavelengths, s["o2@0,o1@0"])
return {
("o1", "o1"): s11,
("o1", "o2"): s21,
Expand Down Expand Up @@ -524,10 +530,10 @@ def coupler_model(

@jax.jit
def _model(wl=1.55):
s11 = jnp.interp(wl, wavelengths, s["o1@0,o1@0"])
s21 = jnp.interp(wl, wavelengths, s["o2@0,o1@0"])
s31 = jnp.interp(wl, wavelengths, s["o3@0,o1@0"])
s41 = jnp.interp(wl, wavelengths, s["o4@0,o1@0"])
s11 = complex_interp(wl, wavelengths, s["o1@0,o1@0"])
s21 = complex_interp(wl, wavelengths, s["o2@0,o1@0"])
s31 = complex_interp(wl, wavelengths, s["o3@0,o1@0"])
s41 = complex_interp(wl, wavelengths, s["o4@0,o1@0"])
return {
("o1", "o1"): s11,
("o1", "o2"): s21,
Expand Down Expand Up @@ -613,12 +619,6 @@ def patch_netlist(netlist, models, models_to_patch):
# %%
fig, ax = plt.subplots(1, 1, figsize=(12, 4))

layout = cascaded_mzi(
coupler_gaps=[gap] * len(lengths),
coupler_lengths=lengths,
mzi_deltas=mzi_deltas,
cross_section=cross_section,
)
netlist, models = patch_netlist(
layout.get_netlist(),
{"straight": straight_model, "bend_euler": bend_model(cross_section=cross_section)},
Expand Down
111 changes: 53 additions & 58 deletions gplugins/tidy3d/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import hashlib
import io
import pathlib
import time
from collections.abc import Awaitable
from functools import cached_property
from typing import Any
Expand Down Expand Up @@ -439,6 +440,7 @@ def write_sparameters(
plot_simulation_x: float | None = None,
plot_mode_index: int | None = 0,
plot_mode_port_name: str | None = None,
plot_epsilon: bool = False,
filepath: PathType | None = None,
overwrite: bool = False,
) -> Sparameters:
Expand Down Expand Up @@ -498,6 +500,7 @@ def write_sparameters(
pad_z_outer=pad_z_outer,
dilation=dilation,
)
path_dir = str(pathlib.Path(dirpath) / str(hash(c)))

modeler = c.get_component_modeler(
wavelength=wavelength,
Expand All @@ -515,7 +518,7 @@ def write_sparameters(
run_time=run_time,
shutoff=shutoff,
folder_name=folder_name,
path_dir=str(dirpath),
path_dir=path_dir,
verbose=verbose,
)
sp = {}
Expand All @@ -531,9 +534,14 @@ def write_sparameters(
modeler = c.get_component_modeler(
center_z=plot_simulation_layer_name, port_size_mult=(6, 4), sim_size_z=3.0
)
fig, ax = plt.subplots(2, 1)
modeler.plot_sim(z=z, ax=ax[0])
modeler.plot_sim(x=x, ax=ax[1])
_, ax = plt.subplots(2, 1)
if plot_epsilon:
modeler.plot_sim_eps(z=z, ax=ax[0])
modeler.plot_sim_eps(x=x, ax=ax[1])

else:
modeler.plot_sim(z=z, ax=ax[0])
modeler.plot_sim(x=x, ax=ax[1])
plt.show()
return sp

Expand All @@ -542,7 +550,7 @@ def write_sparameters(
mode_solver = modes[f"smatrix_{plot_mode_port_name}_{plot_mode_index}"]
mode_data = mode_solver.solve()

fig, ax = plt.subplots(1, 3, tight_layout=True, figsize=(10, 3))
_, ax = plt.subplots(1, 3, tight_layout=True, figsize=(10, 3))
abs(mode_data.Ex.isel(mode_index=plot_mode_index, f=0)).plot(
x="y", y="z", ax=ax[0], cmap="magma"
)
Expand All @@ -568,6 +576,7 @@ def write_sparameters(
print(f"Simulation loaded from {filepath!r}")
return dict(np.load(filepath))
else:
time.sleep(0.2)
s = modeler.run()
for port_in in s.port_in.values:
for port_out in s.port_out.values:
Expand Down Expand Up @@ -644,60 +653,46 @@ def write_sparameters_batch(


if __name__ == "__main__":
from functools import partial

import gdsfactory as gf

from gplugins.common.config import PATH

pdk = gf.get_active_pdk()
layer_stack = pdk.get_layer_stack()
layer_stack.layers.pop("substrate", None)

width = 0.45
cross_section = pdk.get_cross_section("xs_sc", width=width)
coupler_sc = partial(
gf.components.coupler,
dx=4,
dy=2,
cross_section=cross_section,
) # Coupler Strip C-Band

sps = write_sparameters_batch(
[
dict(
component=coupler_sc(length=i),
# sim_size_z=0,
filepath=PATH.sparameters_repo / f"dc_{i}.npz",
layer_stack=layer_stack,
# overwrite=True,
)
for i in range(25)
]
)

sp = []
for spi in sps:
sp.append(spi.result())

# c = gf.components.taper_sc_nc()

# run = False
# c = write_sparameters(c, sim_size_z=0, run=False, center_z="core")
# modeler = c.get_component_modeler()
# modeler.plot_sim(z=0)
# plt.show()

# mode_spec = td.ModeSpec(num_modes=2, filter_pol="te")
# sp = write_sparameters(c, sim_size_z=0, center_z="core", plot_simulation_layer_name='core', plot_simulation_port_index=1, mode_spec=mode_spec)
# sp = write_sparameters(
# c,
# sim_size_z=4,
# center_z="core",
# plot_simulation_x=10,
# plot_simulation_layer_name="core",
# # plot_mode_port_name="o1",
# # plot_mode_index=1,
# # mode_spec=mode_spec,
# pdk = gf.get_active_pdk()
# layer_stack = pdk.get_layer_stack()
# layer_stack.layers.pop("substrate", None)

# width = 0.45
# cross_section = pdk.get_cross_section("xs_sc", width=width)
# coupler_sc = partial(
# gf.components.coupler,
# dx=4,
# dy=2,
# cross_section=cross_section,
# ) # Coupler Strip C-Band

# sims = write_sparameters_batch(
# [
# dict(
# component=coupler_sc(length=i),
# # sim_size_z=0,
# # filepath=PATH.sparameters_repo / f"dc_{i}.npz",
# layer_stack=layer_stack,
# sim_size_z=0,
# overwrite=True,
# )
# for i in range(2)
# ]
# )
# s_params_list = [sim.result() for sim in sims]
c = gf.components.taper_sc_nc()

sp = write_sparameters(
c,
sim_size_z=4,
center_z="core",
plot_simulation_x=10,
plot_simulation_layer_name="core",
plot_epsilon=True,
# plot_mode_port_name="o1",
# plot_mode_index=1,
# mode_spec=mode_spec,
)
# gp.plot.plot_sparameters(sp)
2 changes: 1 addition & 1 deletion gplugins/tidy3d/get_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _get_results(
def get_results(
sim: td.Simulation,
dirpath=PATH.results_tidy3d,
overwrite: bool = True,
overwrite: bool = False,
verbose: bool = False,
) -> Awaitable[td.SimulationData]:
"""Return a List of SimulationData from a Simulation.
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dev = [
devsim = [
"devsim",
"pyvista<=0.40",
"tidy3d>=2.4.0,<2.5.0"
"tidy3d==2.5.0rc3"
]
docs = [
"jupytext",
Expand Down Expand Up @@ -75,7 +75,7 @@ meow = [
"jaxlib",
"jax",
"meow-sim>0.8,<0.9",
"tidy3d>=2.4.0,<2.5.0"
"tidy3d==2.5.0rc3"
]
sax = [
"jaxlib",
Expand All @@ -88,7 +88,7 @@ schematic = [
"natsort"
]
tidy3d = [
"tidy3d>=2.4.0,<2.5.0",
"tidy3d==2.5.0rc3",
"meshio",
"meshwell>=1.0.0,<1.1"
]
Expand Down