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

Fix meep mpi #1358

Merged
merged 3 commits into from Feb 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions gdsfactory/simulation/gmeep/test_write_sparameters_meep.py
Expand Up @@ -8,6 +8,7 @@
import gdsfactory.simulation as sim
import gdsfactory.simulation.gmeep as gm
from gdsfactory.generic_tech import LAYER_STACK
import copy

PDK = gf.get_generic_pdk()
PDK.activate()
Expand Down Expand Up @@ -80,6 +81,16 @@ def test_sparameters_straight_mpi() -> None:
assert np.allclose(np.abs(sp["o1@0,o1@0"]), 0, atol=5e-02), np.abs(sp["o1@0,o1@0"])
assert np.allclose(np.abs(sp["o2@0,o2@0"]), 0, atol=5e-02), np.abs(sp["o2@0,o2@0"])

"""Now check different parameters are properly handled."""
modified_settings = copy.deepcopy(simulation_settings)
modified_settings["wavelength_points"] = 10
filepath2 = gm.write_sparameters_meep_mpi(
c, ymargin=0, overwrite=True, **modified_settings
)
sp2 = np.load(filepath2)
sp2 = dict(sp2)
assert len(sp["wavelengths"]) != len(sp2["wavelengths"])


def test_sparameters_straight_batch() -> None:
"""Checks Sparameters for a straight waveguide using an MPI pool."""
Expand Down
17 changes: 14 additions & 3 deletions gdsfactory/simulation/gmeep/write_sparameters_meep_mpi.py
Expand Up @@ -27,6 +27,7 @@
)
from gdsfactory.technology import LayerStack
from gdsfactory.typings import ComponentSpec, PathType
import pickle

ncores = multiprocessing.cpu_count()

Expand Down Expand Up @@ -160,23 +161,32 @@ def write_sparameters_meep_mpi(
layer_stack_json = layer_stack.json()
filepath_json.write_text(layer_stack_json)

parameters_file = tempfile.with_suffix(".pkl")
with open(parameters_file, "wb") as outp:
pickle.dump(settings, outp, pickle.HIGHEST_PROTOCOL)

# Save component to disk through gds for gdstk compatibility
component_file = tempfile.with_suffix(".gds")
component.write_gds_with_metadata(component_file)

# Write execution file
script_lines = [
"import pathlib\n",
"import pickle\n",
"from gdsfactory.simulation.gmeep import write_sparameters_meep\n\n",
"from gdsfactory.read import import_gds\n",
"from gdsfactory.technology import LayerStack\n\n",
"if __name__ == '__main__':\n",
f"\twith open(\"{parameters_file}\", 'rb') as inp:\n",
"\t\tparameters_dict = pickle.load(inp)\n\n",
f"\tcomponent = import_gds({str(component_file)!r}, read_metadata=True)\n",
f"\tfilepath_json = pathlib.Path({str(filepath_json)!r})\n",
"\tlayer_stack = LayerStack.parse_raw(filepath_json.read_text())\n",
f"\twrite_sparameters_meep(component=component, overwrite={overwrite}, "
f"layer_stack=layer_stack, filepath={str(filepath)!r})",
f"layer_stack=layer_stack, filepath={str(filepath)!r},",
]
script_lines.extend(f'\t\t{key} = parameters_dict["{key}"],\n' for key in settings)
script_lines.append("\t)")

script_file = tempfile.with_suffix(".py")
with open(script_file, "w") as script_file_obj:
Expand Down Expand Up @@ -242,8 +252,9 @@ def write_sparameters_meep_mpi(
live_output=True,
# lazy_parallelism=True,
lazy_parallelism=False,
# temp_dir = "./test/",
# filepath="instance_dict.csv",
temp_dir="./test/",
filepath="instance_dict.csv",
resolution=20,
)
sp = np.load(filepath)
print(list(sp.keys()))