Skip to content

Commit 3d95e0c

Browse files
authored
Merge pull request #592 from gdsfactory/improve_meep_plugin
improve meep plugin and add tests
2 parents 0e50bf5 + 3d4115b commit 3d95e0c

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

.github/workflows/test_code.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ jobs:
4747
run: |
4848
uv run pytest gplugins/${{ matrix.plugin }}
4949
50+
test_meep:
51+
runs-on: ubuntu-latest
52+
name: Test meep ubuntu-latest 3.11
53+
steps:
54+
- uses: actions/checkout@v4
55+
- name: Setup Miniconda
56+
uses: conda-incubator/setup-miniconda@v3
57+
with:
58+
auto-update-conda: true
59+
python-version: 3.11
60+
- name: Install libglu1-mesa
61+
run: |
62+
sudo apt-get install -y libglu1-mesa
63+
- name: Install dependencies
64+
shell: bash -l {0}
65+
run: |
66+
conda create -n mp python=3.11 -y
67+
conda activate mp
68+
conda install -c conda-forge pymeep pymeep-extras gdsfactory -y
69+
pip install .[meep,dev] setuptools
70+
make test-data
71+
- name: Test with pytest
72+
shell: bash -l {0}
73+
run: |
74+
conda activate mp
75+
pytest gplugins/gmeep
76+
5077
test_code_coverage:
5178
runs-on: ubuntu-latest
5279
steps:

gplugins/common/utils/get_sparameters_path.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def _get_sparameters_path(
3131
dirpath: PathType | None = PATH.sparameters,
3232
**kwargs,
3333
) -> Path:
34-
"""Return Sparameters npz filepath hashing simulation settings for \
35-
a consistent unique name.
34+
"""Return Sparameters npz filepath hashing simulation settings for a consistent unique name.
3635
3736
Args:
3837
component: component or component factory.
@@ -47,7 +46,7 @@ def _get_sparameters_path(
4746

4847
dirpath = pathlib.Path(dirpath)
4948
dirpath = (
50-
dirpath / component.function_name
49+
dirpath / component.name
5150
if hasattr(component, "function_name")
5251
else dirpath
5352
)

gplugins/gmeep/get_meep_geometry.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ def get_meep_geometry_from_component(
5858
layer = level.layer
5959

6060
if isinstance(layer, LogicalLayer):
61-
assert isinstance(layer.layer, tuple | LayerEnum)
62-
layer_tuple = cast(tuple[int, int], tuple(layer.layer))
61+
layer_tuple = gf.get_layer_tuple(layer.layer)
6362
elif isinstance(layer, DerivedLayer):
64-
assert level.derived_layer is not None
65-
assert isinstance(level.derived_layer.layer, tuple | LayerEnum)
66-
layer_tuple = cast(tuple[int, int], tuple(level.derived_layer.layer))
63+
layer_tuple = gf.get_layer_tuple(level.derived_layer.layer)
6764
else:
6865
raise ValueError(f"Layer {layer!r} is not a DerivedLayer or LogicalLayer")
6966

gplugins/gmeep/write_sparameters_meep.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def remove_simulation_kwargs(d: dict[str, Any]) -> dict[str, Any]:
5454
def parse_port_eigenmode_coeff(
5555
port_name: str, ports: dict[str, Port], sim_dict: dict, port_mode: int = 0
5656
):
57-
"""Returns the coefficients relative to whether the wavevector is entering or \
58-
exiting simulation.
57+
"""Returns the coefficients relative to whether the wavevector is entering or exiting simulation.
5958
6059
Args:
6160
port_index: index of port.
@@ -320,7 +319,7 @@ def write_sparameters_meep(
320319
filepath = filepath or get_sparameters_path(
321320
component=component,
322321
dirpath=dirpath,
323-
layer_stack=layer_stack,
322+
layer_stack=layer_stack.to_dict(),
324323
**sim_settings,
325324
)
326325

@@ -605,8 +604,8 @@ def sparameter_calculation(
605604
)
606605

607606
if __name__ == "__main__":
608-
wavelength_start = 1.26
609-
wavelength_stop = 1.36
607+
wavelength_start = 1.5
608+
wavelength_stop = 1.6
610609
sim_settings = dict(
611610
wavelength_start=wavelength_start, wavelength_stop=wavelength_stop
612611
)
@@ -621,8 +620,9 @@ def func(x):
621620
sp = write_sparameters_meep(
622621
c,
623622
run=True,
624-
animate=True,
623+
#animate=True,
625624
is_3d=False,
625+
ymargin=3,
626626
plot_args={
627627
"eps_parameters": {"contour": True},
628628
"field_parameters": {

gplugins/gmeep/write_sparameters_meep_mpi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def write_sparameters_meep_mpi(
4545
cores: int = core_materials,
4646
filepath: PathType | None = None,
4747
dirpath: PathType | None = None,
48-
temp_dir: Path = temp_dir_default,
48+
temp_dir: Path|str = temp_dir_default,
4949
temp_file_str: str = "write_sparameters_meep_mpi",
5050
live_output: bool = False,
5151
overwrite: bool = False,
@@ -152,6 +152,7 @@ def write_sparameters_meep_mpi(
152152
filepath.unlink()
153153

154154
# Save all the simulation arguments for later retrieval
155+
temp_dir = Path(temp_dir)
155156
temp_dir.mkdir(exist_ok=True, parents=True)
156157
tempfile = temp_dir / temp_file_str
157158
filepath_json = tempfile.with_suffix(".json")
@@ -178,7 +179,7 @@ def write_sparameters_meep_mpi(
178179
"if __name__ == '__main__':\n",
179180
f"\twith open(\"{parameters_file}\", 'rb') as inp:\n",
180181
"\t\tparameters_dict = pickle.load(inp)\n\n",
181-
f"\tcomponent = import_gds({str(component_file)!r}, read_metadata=True)\n",
182+
f"\tcomponent = import_gds({str(component_file)!r})\n",
182183
f"\tfilepath_json = pathlib.Path({str(filepath_json)!r})\n",
183184
"\tlayer_stack = LayerStack.parse_raw(filepath_json.read_text())\n",
184185
f"\twrite_sparameters_meep(component=component, overwrite={overwrite}, "
@@ -254,6 +255,7 @@ def write_sparameters_meep_mpi(
254255
temp_dir="./test/",
255256
filepath="instance_dict.csv",
256257
resolution=20,
258+
is_3d=False,
257259
)
258260
sp = np.load(filepath)
259261
print(list(sp.keys()))

notebooks/meep_02_gratings.ipynb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,6 @@
154154
"\n",
155155
"You can also run a batch of multicore simulations"
156156
]
157-
},
158-
{
159-
"cell_type": "markdown",
160-
"id": "11",
161-
"metadata": {},
162-
"source": [
163-
"## Far field\n",
164-
"\n",
165-
"TODO"
166-
]
167157
}
168158
],
169159
"metadata": {
@@ -172,9 +162,21 @@
172162
"custom_cell_magics": "kql"
173163
},
174164
"kernelspec": {
175-
"display_name": "base",
165+
"display_name": "mp311",
176166
"language": "python",
177167
"name": "python3"
168+
},
169+
"language_info": {
170+
"codemirror_mode": {
171+
"name": "ipython",
172+
"version": 3
173+
},
174+
"file_extension": ".py",
175+
"mimetype": "text/x-python",
176+
"name": "python",
177+
"nbconvert_exporter": "python",
178+
"pygments_lexer": "ipython3",
179+
"version": "3.11.9"
178180
}
179181
},
180182
"nbformat": 4,

0 commit comments

Comments
 (0)