Skip to content

Commit

Permalink
Merge pull request #28 from gdsfactory/fix_serializer
Browse files Browse the repository at this point in the history
fixes serializer and add test for waveguide coupler
  • Loading branch information
joamatab authored Aug 10, 2023
2 parents 9173360 + dd8e2fa commit 9a6ba62
Show file tree
Hide file tree
Showing 26 changed files with 147 additions and 61 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
__pycache__/
*.py[cod]
*$py.class
temp.msh2


# C extensions
*.so
Expand Down
2 changes: 0 additions & 2 deletions docs/notebooks/devsim_01_pin_waveguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
from gplugins.devsim.get_simulation_xsection import k_to_alpha

gf.config.rich_output()
PDK = gf.get_generic_pdk()
PDK.activate()

# +
# %%capture
Expand Down
8 changes: 5 additions & 3 deletions docs/notebooks/femwell_02_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
PDK = get_generic_pdk()
PDK.activate()

gf.generic_tech.LAYER_STACK.layers["heater"].thickness = 0.13
gf.generic_tech.LAYER_STACK.layers["heater"].zmin = 2.2
LAYER_STACK = PDK.layer_stack

LAYER_STACK.layers["heater"].thickness = 0.13
LAYER_STACK.layers["heater"].zmin = 2.2

heater = gf.components.straight_heater_metal(length=50, heater_width=2)
heater.plot()
# -

print(gf.generic_tech.LAYER_STACK.layers.keys())
print(LAYER_STACK.layers.keys())

filtered_layerstack = LayerStack(
layers={
Expand Down
5 changes: 3 additions & 2 deletions docs/notebooks/meow_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
PDK = get_generic_pdk()
PDK.activate()

LAYER_STACK = PDK.layer_stack
c = gf.components.taper_cross_section_sine()
c.plot()
# -

# You also need to explicitly provide a LayerStack to define cross-sections, for instance the generic one:

# +
layerstack = gf.generic_tech.LAYER_STACK
layerstack = LAYER_STACK

filtered_layerstack = gf.technology.LayerStack(
layers={
Expand Down Expand Up @@ -78,7 +79,7 @@
# Lets sweep the length of the taper.

# +
layerstack = gf.generic_tech.LAYER_STACK
layerstack = LAYER_STACK

filtered_layerstack = gf.technology.LayerStack(
layers={
Expand Down
2 changes: 0 additions & 2 deletions docs/notebooks/workflow_0_layout_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
from omegaconf import OmegaConf

gf.config.rich_output()
PDK = gf.get_generic_pdk()
PDK.activate()

c = gf.Component()
mzi = c << gf.components.mzi() # equivalent to mzi = c.add_ref(gf.components.mzi())
Expand Down
2 changes: 0 additions & 2 deletions docs/notebooks/workflow_2_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import numpy as np

gf.config.rich_output()
PDK = gf.get_generic_pdk()
PDK.activate()


def ring(
Expand Down
6 changes: 3 additions & 3 deletions gplugins/devsim/doping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Callable

import gdsfactory as gf
import numpy as np
from gdsfactory.generic_tech import LAYER
from gdsfactory.typings import Layer
from pydantic import BaseModel

Expand Down Expand Up @@ -37,8 +37,8 @@ def get_doping_info_generic(
p_conc: float = 1e17, # * cm3_to_um3,
npp_conc: float = 1e18, # * cm3_to_um3,
ppp_conc: float = 1e18, # * cm3_to_um3,
):
layermap = gf.generic_tech.LayerMap()
) -> dict[str, DopingLayerLevel]:
layermap = LAYER

return {
"N": DopingLayerLevel(
Expand Down
8 changes: 4 additions & 4 deletions gplugins/devsim/get_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def create_2Duz_simulation(

if __name__ == "__main__":
import gdsfactory as gf
from gdsfactory.generic_tech import get_layer_stack_generic
from gdsfactory.generic_tech import LAYER, LAYER_STACK

# We choose a representative subdomain of the component
waveguide = gf.Component()
Expand All @@ -249,10 +249,10 @@ def create_2Duz_simulation(
waveguide.show()

# We will restrict the physical mesh to a subset of layers:
layermap = gf.generic_tech.LayerMap()
layermap = LAYER
physical_layerstack = LayerStack(
layers={
k: get_layer_stack_generic().layers[k]
k: LAYER_STACK.layers[k]
for k in (
"slab90",
"core",
Expand Down Expand Up @@ -314,7 +314,7 @@ def create_2Duz_simulation(
device_name, regions, interfaces = create_2Duz_simulation(
component=waveguide,
xsection_bounds=[(4, -4), (4, 4)],
full_layerstack=get_layer_stack_generic(),
full_layerstack=LAYER_STACK,
physical_layerstack=physical_layerstack,
doping_info=get_doping_info_generic(),
contact_info=contact_info,
Expand Down
5 changes: 0 additions & 5 deletions gplugins/devsim/test_devsim.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import gdsfactory as gf

from gplugins.devsim import get_simulation_xsection

PDK = gf.get_generic_pdk()
PDK.activate()


def test_pin_waveguide() -> None:
"""Test reverse bias waveguide."""
Expand Down
3 changes: 0 additions & 3 deletions gplugins/femwell/mode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ def compute_component_slice_modes(
if __name__ == "__main__":
import matplotlib.pyplot as plt

PDK = gf.get_generic_pdk()
PDK.activate()

start = time.time()

filtered_layerstack = LayerStack(
Expand Down
9 changes: 5 additions & 4 deletions gplugins/femwell/solve_thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,14 @@ def update(event) -> None:
if __name__ == "__main__":
import gdsfactory as gf
import gmsh
from gdsfactory.generic_tech import LAYER_STACK

from gplugins.gmsh.mesh2D import mesh2D

gf.generic_tech.LAYER_STACK.layers["heater"].thickness = 0.13
gf.generic_tech.LAYER_STACK.layers["heater"].zmin = 2.2
print(gf.generic_tech.LAYER_STACK.layers.keys())
# gf.generic_tech.LAYER_STACK.layers["core"].thickness = 2
LAYER_STACK.layers["heater"].thickness = 0.13
LAYER_STACK.layers["heater"].zmin = 2.2
print(LAYER_STACK.layers.keys())
# LAYER_STACK.layers["core"].thickness = 2

heater1 = gf.components.straight_heater_metal(length=50, heater_width=2)
heater2 = gf.components.straight_heater_metal(length=50, heater_width=2).move(
Expand Down
4 changes: 0 additions & 4 deletions gplugins/femwell/test_mode_solver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gdsfactory as gf
import numpy as np
from gdsfactory.generic_tech import LAYER_STACK
from gdsfactory.technology import LayerStack
Expand All @@ -7,9 +6,6 @@

NUM_MODES = 1

PDK = gf.get_generic_pdk()
PDK.activate()


def compute_modes(
overwrite: bool = True, with_cache: bool = False, num_modes: int = NUM_MODES
Expand Down
3 changes: 0 additions & 3 deletions gplugins/gmeep/test_write_sparameters_meep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import gplugins as sim
import gplugins.gmeep as gm

PDK = gf.get_generic_pdk()
PDK.activate()

simulation_settings = dict(resolution=20, is_3d=False)


Expand Down
3 changes: 0 additions & 3 deletions gplugins/gmsh/tests/test_meshing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from gplugins.gmsh.uz_xsection_mesh import uz_xsection_mesh
from gplugins.gmsh.xy_xsection_mesh import xy_xsection_mesh

PDK = gf.get_generic_pdk()
PDK.activate()


def test_gmsh_uz_xsection_mesh() -> None:
waveguide = gf.components.straight_pin(length=10, taper=None)
Expand Down
3 changes: 2 additions & 1 deletion gplugins/klayout/drc/write_connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from __future__ import annotations

import gdsfactory as gf
from gdsfactory.geometry.write_drc import write_drc_deck_macro
from gdsfactory.typings import CrossSectionSpec, Layer, LayerSpec
from pydantic import BaseModel

from gplugins.klayout.drc.write_drc import write_drc_deck_macro

layer_name_to_min_width: dict[str, float]

nm = 1e-3
Expand Down
73 changes: 73 additions & 0 deletions gplugins/klayout/tests/test_geometry_write_connectivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest

from gplugins.klayout.drc.write_connectivity import write_connectivity_checks

nm = 1e-3


def test_default_values() -> None:
script = write_connectivity_checks(pin_widths=[0.5, 0.9, 0.45], pin_layer=(1, 10))
assert isinstance(script, str)
assert "pin = input(1, 10)" in script
width = 0.5
print(script)
assert f"pin2 = pin.rectangles.without_area({width} * 0.002)" in script
assert 'pin2.output("port alignment error")' in script
assert 'pin2.non_rectangles.output("port width check")' in script


# Tests that the function works with different pin widths
def test_different_pin_widths() -> None:
width = 0.3
script = write_connectivity_checks(pin_widths=[width], pin_layer=(1, 10))
assert isinstance(script, str)
assert "pin = input(1, 10)" in script
assert f"pin.rectangles.without_area({width} * 0.002)" in script


# Tests that the function works with different pin layers
def test_different_pin_layers() -> None:
script = write_connectivity_checks(pin_widths=[0.5, 0.9, 0.45], pin_layer=(1, 20))
assert isinstance(script, str)
assert "pin = input(1, 20)" in script


# Tests that the function works with different pin lengths
def test_different_pin_lengths() -> None:
script = write_connectivity_checks(
pin_widths=[0.5, 0.9, 0.45], pin_layer=(1, 10), pin_length=2 * nm
)
assert isinstance(script, str)
assert "pin = input(1, 10)" in script
assert "pin2 = pin.rectangles.without_area(0.5 * 0.004)" in script


# Tests that the function raises an error with an invalid pin layer
def test_invalid_pin_layer() -> None:
with pytest.raises(ValueError):
write_connectivity_checks(
pin_widths=[0.5, 0.9, 0.45], pin_layer="wrong_layer_name"
)


# Tests that the function raises an error with an invalid device layer
def test_invalid_device_layer() -> None:
with pytest.raises(ValueError):
write_connectivity_checks(
pin_widths=[0.5, 0.9, 0.45],
pin_layer=(1, 10),
device_layer="wrong_layer_name",
)


if __name__ == "__main__":
# s = write_connectivity_checks(pin_widths=[0.5, 0.9, 0.45], pin_layer='wrong')
# print(s)
# script = write_connectivity_checks(
# pin_widths=[0.5, 0.9, 0.45], pin_layer=(1, 10), pin_length=2 * nm
# )
# print(script)
# test_default_values()
# test_different_pin_widths()
# test_different_pin_layers()
test_different_pin_lengths()
3 changes: 0 additions & 3 deletions gplugins/meow/test_meow_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

from gplugins.meow import MEOW

PDK = gf.get_generic_pdk()
PDK.activate()


def test_meow_defaults() -> None:
c = gf.components.taper_cross_section_linear()
Expand Down
5 changes: 2 additions & 3 deletions gplugins/modes/overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def innerprod_trapz(
zmax: float = 2.0,
trapz_num_y: int = 2000,
trapz_num_z: int = 2000,
):
) -> float:
"""Compute the inner product of two modes as 1/4*int(E1* x H2 + E2 x H1*)_x dydz.
with int double integral over y,z, x cross product, and _x x-projection
Expand Down Expand Up @@ -67,8 +67,7 @@ def test_innerprod_trapz() -> None:
"""Checks that overlaps do not change."""
m = gm.find_modes_waveguide()
overlap = innerprod_trapz(m[1], m[1])
assert overlap > 0
# assert np.isclose(np.real(overlap), 0.143, atol=1e-2), np.real(overlap)
assert abs(overlap) < 0.2


if __name__ == "__main__":
Expand Down
3 changes: 0 additions & 3 deletions gplugins/sax/integrations/meow_eme_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ def get_results(self, input_dict):

from gplugins.sax.parameter import LayerStackThickness, NamedParameter

PDK = gf.get_generic_pdk()
PDK.activate()

c = gf.components.taper_cross_section_linear(
cross_section1=rib(width=2), cross_section2=rib(width=0.5)
)
Expand Down
16 changes: 10 additions & 6 deletions gplugins/tidy3d/get_simulation_grating_coupler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from __future__ import annotations

import warnings
from functools import partial

import gdsfactory as gf
import matplotlib.pyplot as plt
import numpy as np
import tidy3d as td
from gdsfactory.add_padding import add_padding
from gdsfactory.component import Component
from gdsfactory.components.extension import move_polar_rad_copy
from gdsfactory.config import logger
Expand Down Expand Up @@ -256,17 +258,19 @@ def get_simulation_grating_coupler(
raise ValueError(
f"No port named {fiber_port_prefix!r} in {component.ports.keys()}"
)

component_with_booleans = layer_stack.get_component_with_derived_layers(component)

component_padding = gf.add_padding_container(
component_with_booleans,
add_padding_custom = partial(
add_padding,
default=0,
top=ymargin or ymargin_top,
bottom=ymargin or ymargin_bot,
left=xmargin or xmargin_left,
right=xmargin or xmargin_right,
)

component_padding = layer_stack.get_component_with_derived_layers(
component, decorator=add_padding_custom
)

component_extended = (
gf.components.extension.extend_ports(
component=component_padding,
Expand Down Expand Up @@ -344,7 +348,7 @@ def get_simulation_grating_coupler(

structures = [substrate, box, clad]

component_layers = component_with_booleans.get_layers()
component_layers = component_padding.get_layers()

for layer, thickness in layer_to_thickness.items():
if layer in layer_to_material and layer in component_layers:
Expand Down
8 changes: 8 additions & 0 deletions gplugins/tidy3d/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def custom_serializer(data: str | float | BaseModel) -> str:
if isinstance(data, BaseModel):
return data.json()

# If data is a list or tuple, recursively serialize each element.
if isinstance(data, list | tuple):
return [custom_serializer(item) for item in data]

# If data is a dictionary, recursively serialize each key-value pair.
if isinstance(data, dict):
return {key: custom_serializer(value) for key, value in data.items()}

# For all other data types, raise an exception.
raise ValueError(f"Unsupported data type: {type(data)}")

Expand Down
Loading

0 comments on commit 9a6ba62

Please sign in to comment.