gdsfactory / gplugins

gdsfactory plugins
https://gdsfactory.github.io/gplugins/
MIT License
35 stars 30 forks source link

gm.get_meep_adjoint_optimizer function #477

Open kdotom opened 1 month ago

kdotom commented 1 month ago

Describe the bug /path/to/miniconda3/envs/mp/lib/python3.11/site-packages/gplugins/gmeep/meep_adjoint_optimization.py:148: UserWarning: Settingrectangle_S5_5_LWG_CFal_d89a89d8_2500_2500.centerin um is deprecated and will change to DataBaseUnits in gdsfactory9. Please userectangle_S5_5_LWG_CFal_d89a89d8_2500_2500.dcenterinstead.

To Reproduce

import gdsfactory as gf
import gplugins as gp
import gplugins.gmeep as gm
import numpy as np
import meep as mp
from meep import MaterialGrid, Medium, Vector3, Volume
from meep.adjoint import (
    conic_filter,
    DesignRegion,
    get_conic_radius_from_eta_e,
    tanh_projection,
)

#### Demo to reproduce bug
Nx = 50
Ny = 50

c = gf.Component()
wg = c << gf.components.straight(layer=(1,0),width=0.5,length=10)

design_variables = mp.MaterialGrid(mp.Vector3(Nx, Ny), mp.Medium(index=1.444), mp.Medium(index=3.45), grid_type="U_MEAN")
design_region = \
    DesignRegion(
        design_variables,
        volume=Volume(
            center=Vector3(),
            size=Vector3(5, 5, 0),
        ),
    )

seed = 240
np.random.seed(seed)
x0 = np.random.rand(
        Nx * Ny,
    )

c.add_port("o1", port=wg.ports["o1"]) # assign ports to component
c.add_port("o2", port=wg.ports["o2"]) # assign ports to component

def J1(o1, o2):
    criterion = npa.abs(o2/o1)
    return criterion

gm.get_meep_adjoint_optimizer(
    c,
    J1,
    [design_region],
    [design_variables],
    x0,
    resolution=10,
    cell_size=(
        10,
        10,
    ),
    tpml=1.0,
    extend_ports_length=0,
    port_margin=1,
    port_source_name=f"o1",
    port_source_offset=-1.5,
    port_monitor_offset=-1.5,
    distance_source_to_monitors=1,
)

Expected behavior Warning should not occur and design with a waveguide and design region (properly sized) should be produced.

Suggested fix Potentially, Change block.center to block.dcenter on line 148 of path/to/site-packages/gplugins/gmeep/meep_adjoint_optimization.py I haven't updated this as I'm not sure how changes will propagate through the gmeep library, given the new units update.

joamatab commented 2 days ago

@simbilod

if you have some time maybe you can take a look at the meep plugin

kdotom commented 2 days ago

@simbilod If you need backup on the updates, let me know. I could probably put something together; I just want to make sure it lines up with any scaling changes you guys have in mind for future versions.

simbilod commented 2 days ago

Hi @kdotom , I actually don't use MEEP anymore so if you have ideas on how to improve the plugin feel free to make any changes you see fit!