示例#1
0
def add_monitors(
    component: Component,
    source_port_name: str = "o1",
    extension_length: float = 1.0,
    source_distance_to_monitors: float = 0.2,
    port_margin: float = 1.0,
    layer_monitor: Tuple[int, int] = (101, 0),
    layer_source: Tuple[int, int] = (110, 0),
    layer_simulation_region: Tuple[int, int] = (2, 0),
    top: float = 0.0,
    bottom: float = 0.0,
    right: float = 0.0,
    left: float = 0.0,
) -> Component:
    """Add monitors in layer_monitor, incrementing the layer number for each port
    Then extends all ports by source_distance_to_monitors
    Then add source in source_port_name
    Finally extends all ports by extension_length
    returns device centered at x=0, y=0

    Args:
        component: to add monitors
        source_port_name: name of the port to add the mode source
        extension_length: extension lenght when extending the ports
        port_margin: from waveguide edge to port edge
        layer_monitor: layer for the fist monitor
        layer_source: layer for the source
        layer_simulation_region: layer for the simulation region
        top: simulation region top padding
        bottom: simulation region south padding
        right: simulation region east padding
        left: simulation region west padding

    Returns: component with extended ports and monitors

    """
    c = gf.Component(f"{component.name}_monitors")

    # add monitors
    component_with_monitors = add_monitors_and_extend_ports(
        component=component,
        extension_length=source_distance_to_monitors,
        port_margin=port_margin,
        layer=layer_monitor,
    )

    # add source
    component_with_source = add_monitors_and_extend_ports(
        component_with_monitors,
        extension_length=extension_length,
        port_labels=[source_port_name],
        layer=layer_source,
    )

    # add simulation region
    component_with_padding = gf.add_padding(
        component=component_with_source,
        default=0,
        layers=[layer_simulation_region],
        top=top,
        bottom=bottom,
        right=right,
        left=left,
    )
    c.add(component_with_padding)
    c.ports = component_with_padding.ports
    return c
示例#2
0
        m.z = 0
        monitors[port_name] = m
    return dict(
        sim=sim,
        cell_size=cell_size,
        freqs=freqs,
        monitors=monitors,
        sources=sources,
        field_monitor_point=field_monitor_point,
        port_source_name=port_source_name,
    )


if __name__ == "__main__":
    c = gf.components.straight(length=2)
    c = gf.add_padding(c, default=0, bottom=2, top=2, layers=[(100, 0)])

    c = gf.components.mmi1x2()
    c = gf.add_padding(c, default=0, bottom=2, top=2, layers=[(100, 0)])

    c = gf.components.bend_circular(radius=2)
    c = gf.add_padding(c, default=0, bottom=2, right=2, layers=[(100, 0)])

    sim_dict = get_simulation(c, is_3d=False)
    sim = sim_dict["sim"]

    sim.plot2D()  # plot top view

    # center = (0, 0, 0)
    # size = sim.cell_size
    # sim.plot2D(
示例#3
0
def compare_mpb_lumerical(plot=False):
    """
    WARNING: Segmentation fault occurs if both ms object above and sim object exist in memory at the same time
    Instead load results from separate MPB run

    Same namespace run does not work
    # MPB mode
    # ms = get_mode_solver_rib(wg_width=0.5)
    # modes = find_modes(mode_solver=ms, res=50)
    # m1_MPB = modes[1]

    separate namespace run does not work either
    # m1_MPB = MPB_eigenmode()
    """
    # Test data
    filepath = gf.CONFIG["module_path"] / "simulation" / "gmeep" / "test_data"

    # MPB calculation
    # Load previously-computed waveguide results
    m1_MPB_neff = np.load(filepath / "stripWG_mpb" / "neff1.npy")
    m1_MPB_E = np.load(filepath / "stripWG_mpb" / "E1.npy")
    m1_MPB_H = np.load(filepath / "stripWG_mpb" / "H1.npy")
    m1_MPB_y = np.load(filepath / "stripWG_mpb" / "y1.npy")
    m1_MPB_z = np.load(filepath / "stripWG_mpb" / "z1.npy")
    m2_MPB_neff = np.load(filepath / "stripWG_mpb" / "neff2.npy")
    m2_MPB_E = np.load(filepath / "stripWG_mpb" / "E2.npy")
    m2_MPB_H = np.load(filepath / "stripWG_mpb" / "H2.npy")
    m2_MPB_y = np.load(filepath / "stripWG_mpb" / "y2.npy")
    m2_MPB_z = np.load(filepath / "stripWG_mpb" / "z2.npy")
    # Package into modes object
    m1_MPB = Mode(
        mode_number=1,
        neff=m1_MPB_neff,
        wavelength=None,
        ng=None,
        E=m1_MPB_E,
        H=m1_MPB_H,
        eps=None,
        y=m1_MPB_y,
        z=m1_MPB_z,
    )
    m2_MPB = Mode(
        mode_number=1,
        neff=m2_MPB_neff,
        wavelength=None,
        ng=None,
        E=m2_MPB_E,
        H=m2_MPB_H,
        eps=None,
        y=m2_MPB_y,
        z=m2_MPB_z,
    )

    # Load Lumerical result
    with h5py.File(filepath / "stripWG_lumerical" / "mode1.mat", "r") as f:
        E, H, y, z = lumerical_parser(f["E"]["E"],
                                      f["H"]["H"],
                                      f["E"]["y"],
                                      f["E"]["z"],
                                      res=50)
        # Package into modes object
        m1_lumerical = Mode(
            mode_number=1,
            neff=f["neff"][0][0][0],
            wavelength=None,
            ng=None,
            E=E,
            H=H,
            eps=None,
            y=y,
            z=z,
        )
    with h5py.File(filepath / "stripWG_lumerical" / "mode2.mat", "r") as f:
        E, H, y, z = lumerical_parser(f["E"]["E"],
                                      f["H"]["H"],
                                      f["E"]["y"],
                                      f["E"]["z"],
                                      res=50)
        # Package into modes object
        m2_lumerical = Mode(
            mode_number=1,
            neff=f["neff"][0][0][0],
            wavelength=None,
            ng=None,
            E=E,
            H=H,
            eps=None,
            y=y,
            z=z,
        )

    # MEEP calculation
    c = straight(length=2, width=0.45)
    c = add_padding(c.copy(), default=0, bottom=4, top=4, layers=[(100, 0)])

    sim_dict = get_simulation(
        c,
        is_3d=True,
        port_source_offset=-0.1,
        port_monitor_offset=-0.1,
        port_margin=3,
        resolution=50,
    )

    m1_MEEP = get_port_2Dx_eigenmode(
        sim_dict=sim_dict,
        source_index=0,
        port_name="o1",
    )

    m2_MEEP = get_port_2Dx_eigenmode(
        sim_dict=sim_dict,
        source_index=0,
        port_name="o1",
        band_num=2,
    )

    if plot:
        # M1, E-field
        plt.figure(figsize=(10, 8), dpi=100)
        plt.suptitle(
            "MEEP get_eigenmode / MPB find_modes / Lumerical (manual)",
            y=1.05,
            fontsize=18,
        )

        plt.subplot(3, 3, 1)
        m1_MEEP.plot_ex(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 2)
        m1_MPB.plot_ex(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 3)
        m1_lumerical.plot_ex(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 4)
        m1_MEEP.plot_ey(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 5)
        m1_MPB.plot_ey(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 6)
        m1_lumerical.plot_ey(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 7)
        m1_MEEP.plot_ez(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 8)
        m1_MPB.plot_ez(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 9)
        m1_lumerical.plot_ez(show=False, operation=np.abs, scale=False)

        plt.tight_layout()
        plt.show()

        # M1, H-field
        plt.figure(figsize=(10, 8), dpi=100)
        plt.suptitle(
            "MEEP get_eigenmode / MPB find_modes / Lumerical (manual)",
            y=1.05,
            fontsize=18,
        )

        plt.subplot(3, 3, 1)
        m1_MEEP.plot_hx(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 2)
        m1_MPB.plot_hx(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 3)
        m1_lumerical.plot_hx(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 4)
        m1_MEEP.plot_hy(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 5)
        m1_MPB.plot_hy(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 6)
        m1_lumerical.plot_hy(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 7)
        m1_MEEP.plot_hz(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 8)
        m1_MPB.plot_hz(show=False, operation=np.abs, scale=False)

        plt.subplot(3, 3, 9)
        m1_lumerical.plot_hz(show=False, operation=np.abs, scale=False)

        plt.tight_layout()
        plt.show()

        # # M2, E-field
        # plt.figure(figsize=(10, 8), dpi=100)

        # plt.subplot(3, 3, 1)
        # m2_MEEP.plot_ex(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 2)
        # m2_MPB.plot_ex(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 3)
        # m2_lumerical.plot_ex(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 4)
        # m2_MEEP.plot_ey(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 5)
        # m2_MPB.plot_ey(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 6)
        # m2_lumerical.plot_ey(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 7)
        # m2_MEEP.plot_ez(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 8)
        # m2_MPB.plot_ez(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 9)
        # m2_lumerical.plot_ez(show=False, operation=np.abs, scale=False)

        # plt.tight_layout()
        # plt.show()

        # # M2, H-field
        # plt.figure(figsize=(10, 8), dpi=100)

        # plt.subplot(3, 3, 1)
        # m2_MEEP.plot_hx(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 2)
        # m2_MPB.plot_hx(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 3)
        # m2_lumerical.plot_hx(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 4)
        # m2_MEEP.plot_hy(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 5)
        # m2_MPB.plot_hy(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 6)
        # m2_lumerical.plot_hy(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 7)
        # m2_MEEP.plot_hz(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 8)
        # m2_MPB.plot_hz(show=False, operation=np.abs, scale=False)

        # plt.subplot(3, 3, 9)
        # m2_lumerical.plot_hz(show=False, operation=np.abs, scale=False)

        # plt.tight_layout()
        # plt.show()

    # Check propagation constants
    # print(m1_MEEP.neff, m1_MPB.neff, m1_lumerical.neff)
    # print(m2_MEEP.neff, m2_MPB.neff, m2_lumerical.neff)

    # Check mode profiles
    assert np.isclose(m1_MPB.neff, m1_lumerical.neff, atol=0.02)
    assert np.isclose(m1_MEEP.neff, m1_MPB.neff, atol=0.02)
    assert np.isclose(m2_MPB.neff, m2_lumerical.neff, atol=0.07)
    assert np.isclose(m2_MEEP.neff, m2_MPB.neff, atol=0.07)
示例#4
0
    """

    component_filled = _fill_rectangle(
        component,
        fill_size=fill_size,
        avoid_layers=avoid_layers or "all",
        include_layers=include_layers,
        margin=margin,
        fill_layers=fill_layers,
        fill_densities=fill_densities,
        fill_inverted=fill_inverted,
        bbox=bbox,
    )
    return from_phidl(component_filled)


if __name__ == "__main__":
    import gdsfactory as gf

    c = gf.components.straight()
    c = gf.add_padding(c)
    c << fill_rectangle(
        c,
        fill_layers=((2, 0), ),
        # fill_densities=(1.0,),
        fill_densities=1.0,
        avoid_layers=((1, 0), ),
        # bbox=(100.0, 100.0),
    )
    c.show()
示例#5
0
"""
info:

- default
- changed
- full
- info (derived properties)
- child: if any

Calculated/derived properties are stored in info

"""

import gdsfactory as gf


def test_args():
    c1 = gf.c.pad((150, 150))
    assert c1.info.full.size[0] == 150


if __name__ == "__main__":
    test_args()
    # assert c1.settings.size.full[0] == 150
    c1 = gf.c.pad((150, 150))
    c2 = gf.add_padding(c1)
    c2.show()
    c3 = gf.add_padding(c2)
示例#6
0
        mode_number=band_num,
        neff=eigenmode.k.x / fsrc,
        wavelength=1 / fsrc,
        ng=None,  # Not currently supported
        E=E,
        H=H,
        eps=None,  # Eventually return the index distribution for co-plotting
        y=y,
        z=z,
    )
    return mode


if __name__ == "__main__":
    c = straight(length=2, width=0.5)
    c = add_padding(c.copy(), default=0, bottom=3, top=3, layers=[(100, 0)])

    sim_dict = get_simulation(
        c,
        is_3d=True,
        res=50,
        port_source_offset=-0.1,
        port_field_monitor_offset=-0.1,
        port_margin=2.5,
    )

    m1_MEEP = get_port_2Dx_eigenmode(
        sim_dict=sim_dict,
        source_index=0,
        port_name="o1",
    )