Skip to content

Notebook notebooks/workflow_3_cascaded_mzi.ipynb fails to write S-Parameters #674

@jonbenfri

Description

@jonbenfri

Describe the bug
When running the notebook, the code fails to get the S-parameters for a coupler, and raises a ValidationError with the following stack trace:

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[4], line 1
----> 1 _ = gt.write_sparameters(
      2     coupler,
      3     layer_stack=layer_stack,
      4     plot_simulation_layer_name="core",
      5 )

File [<env>/gplugins/gplugins/tidy3d/component.py:510](http://localhost:8888/lab/tree/repositories/gplugins/notebooks/repositories/gplugins/gplugins/tidy3d/component.py#line=509), in write_sparameters(component, layer_stack, material_mapping, extend_ports, port_offset, pad_xy_inner, pad_xy_outer, pad_z_inner, pad_z_outer, dilation, wavelength, bandwidth, num_freqs, min_steps_per_wvl, center_z, sim_size_z, port_size_mult, run_only, element_mappings, extra_monitors, mode_spec, boundary_spec, symmetry, run_time, shutoff, folder_name, dirpath, verbose, plot_simulation_layer_name, plot_simulation_port_index, plot_simulation_z, plot_simulation_x, plot_mode_index, plot_mode_port_name, plot_epsilon, filepath, overwrite, **kwargs)
    495 layer_stack = layer_stack or get_layer_stack()
    497 c = Tidy3DComponent(
    498     component=component,
    499     layer_stack=layer_stack,
   (...)    507     dilation=dilation,
    508 )
--> 510 modeler = c.get_component_modeler(
    511     wavelength=wavelength,
    512     bandwidth=bandwidth,
    513     num_freqs=num_freqs,
    514     min_steps_per_wvl=min_steps_per_wvl,
    515     center_z=center_z,
    516     sim_size_z=sim_size_z,
    517     port_size_mult=port_size_mult,
    518     run_only=run_only,
    519     element_mappings=element_mappings,
    520     extra_monitors=extra_monitors,
    521     mode_spec=mode_spec,
    522     boundary_spec=boundary_spec,
    523     run_time=run_time,
    524     shutoff=shutoff,
    525     folder_name=folder_name,
    526     verbose=verbose,
    527     symmetry=symmetry,
    528     **kwargs,
    529 )
    531 path_dir = pathlib.Path(dirpath) [/](http://localhost:8888/) modeler._hash_self()
    532 modeler = modeler.updated_copy(path_dir=str(path_dir))

File [<env>/gplugins/gplugins/tidy3d/component.py:305](http://localhost:8888/lab/tree/repositories/gplugins/notebooks/repositories/gplugins/gplugins/tidy3d/component.py#line=304), in Tidy3DComponent.get_component_modeler(self, wavelength, bandwidth, num_freqs, min_steps_per_wvl, center_z, sim_size_z, port_size_mult, run_only, element_mappings, extra_monitors, mode_spec, boundary_spec, run_time, shutoff, grid_eps, folder_name, path_dir, verbose, symmetry, **kwargs)
    291 sim = self.get_simulation(
    292     grid_spec=grid_spec,
    293     center_z=cz,
   (...)    300     **kwargs,
    301 )
    303 ports = self.get_ports(mode_spec, port_size_mult, grid_eps=grid_eps)
--> 305 return ComponentModeler(
    306     simulation=sim,
    307     ports=ports,
    308     freqs=tuple(freqs),
    309     element_mappings=element_mappings,
    310     run_only=run_only,
    311     folder_name=folder_name,
    312     path_dir=path_dir,
    313     verbose=verbose,
    314 )

File <env>/lib/python3.12/site-packages/tidy3d/components/base.py:203, in Tidy3dBaseModel.__init__(self, **kwargs)
    201 """Init method, includes post-init validators."""
    202 log.begin_capture()
--> 203 super().__init__(**kwargs)
    204 self._post_init_validators()
    205 log.end_capture(self)

File <env>/lib/python3.12/site-packages/pydantic/v1/main.py:347, in BaseModel.__init__(__pydantic_self__, **data)
    345 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    346 if validation_error:
--> 347     raise validation_error
    348 try:
    349     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 3 validation errors for ModalComponentModeler
folder_name
  extra fields not permitted (type=value_error.extra)
path_dir
  extra fields not permitted (type=value_error.extra)
verbose
  extra fields not permitted (type=value_error.extra)

To Reproduce
Run the notebook.

Expected behavior
Code runs to completion.

Suggested fix
Remove extra fields, as indicated at the end of the stack trace. Also update the web api call.

Suggested changes patch:

diff --git a/gplugins/tidy3d/component.py b/gplugins/tidy3d/component.py
index fe535a8..2abf354 100644
--- a/gplugins/tidy3d/component.py
+++ b/gplugins/tidy3d/component.py
@@ -23,6 +23,7 @@ from typing import Any, Literal
 import matplotlib.pyplot as plt
 import numpy as np
 import tidy3d as td
+import tidy3d.web as web
 from gdsfactory.component import Component
 from gdsfactory.pdk import get_layer_stack
 from gdsfactory.technology import LayerStack
@@ -227,7 +228,7 @@ class Tidy3DComponent(LayeredComponentBase):
         run_only: tuple[tuple[str, int], ...] | None = None,
         element_mappings: Tidy3DElementMapping = (),
         extra_monitors: tuple[Any, ...] | None = None,
-        mode_spec: td.ModeSpec = td.ModeSpec(num_modes=1, filter_pol="te"),
+        mode_spec: td.ModeSpec = td.ModeSpec(num_modes=1),
         boundary_spec: td.BoundarySpec = td.BoundarySpec.all_sides(boundary=td.PML()),
         run_time: float = 10e-12,
         shutoff: float = 1e-5,
@@ -251,7 +252,7 @@ class Tidy3DComponent(LayeredComponentBase):
             run_only: The run only specification for the ComponentModeler. Defaults to None.
             element_mappings: The element mappings for the ComponentModeler. Defaults to ().
             extra_monitors: The extra monitors for the ComponentModeler. Defaults to None.
-            mode_spec: The mode specification for the ComponentModeler. Defaults to td.ModeSpec(num_modes=1, filter_pol="te").
+            mode_spec: The mode specification for the ComponentModeler. Defaults to td.ModeSpec(num_modes=1).
             boundary_spec: The boundary specification for the ComponentModeler. Defaults to td.BoundarySpec.all_sides(boundary=td.PML()).
             run_time: The run time for the ComponentModeler.
             shutoff: The shutoff value for the ComponentModeler. Defaults to 1e-5.
@@ -308,9 +309,6 @@ class Tidy3DComponent(LayeredComponentBase):
             freqs=tuple(freqs),
             element_mappings=element_mappings,
             run_only=run_only,
-            folder_name=folder_name,
-            path_dir=path_dir,
-            verbose=verbose,
         )
 
     @td.components.viz.add_ax_if_none
@@ -428,7 +426,7 @@ def write_sparameters(
     run_only: tuple[tuple[str, int], ...] | None = None,
     element_mappings: Tidy3DElementMapping = (),
     extra_monitors: tuple[Any, ...] | None = None,
-    mode_spec: td.ModeSpec = td.ModeSpec(num_modes=1, filter_pol="te"),
+    mode_spec: td.ModeSpec = td.ModeSpec(num_modes=1),
     boundary_spec: td.BoundarySpec = td.BoundarySpec.all_sides(boundary=td.PML()),
     symmetry: tuple[Symmetry, Symmetry, Symmetry] = (0, 0, 0),
     run_time: float = 1e-12,
@@ -471,7 +469,7 @@ def write_sparameters(
         run_only: The run only specification for the ComponentModeler. Defaults to None.
         element_mappings: The element mappings for the ComponentModeler. Defaults to ().
         extra_monitors: The extra monitors for the ComponentModeler. Defaults to None.
-        mode_spec: The mode specification for the ComponentModeler. Defaults to td.ModeSpec(num_modes=1, filter_pol="te").
+        mode_spec: The mode specification for the ComponentModeler. Defaults to td.ModeSpec(num_modes=1).
         boundary_spec: The boundary specification for the ComponentModeler.
             Defaults to td.BoundarySpec.all_sides(boundary=td.PML()).
         symmetry (tuple[Symmetry, Symmetry, Symmetry], optional): The symmetry for the simulation. Defaults to (0,0,0).
@@ -522,14 +520,12 @@ def write_sparameters(
         boundary_spec=boundary_spec,
         run_time=run_time,
         shutoff=shutoff,
-        folder_name=folder_name,
-        verbose=verbose,
         symmetry=symmetry,
         **kwargs,
     )
 
     path_dir = pathlib.Path(dirpath) / modeler._hash_self()
-    modeler = modeler.updated_copy(path_dir=str(path_dir))
+    modeler = modeler.updated_copy()
 
     sp = {}
 
@@ -591,7 +587,8 @@ def write_sparameters(
         return dict(np.load(filepath))
     else:
         time.sleep(0.2)
-        s = modeler.run()
+        modeler_data = web.run(modeler, verbose=verbose, path=dirpath / "simulation.hdf5")
+        s = modeler_data.smatrix()
         for port_in in s.port_in.values:
             for port_out in s.port_out.values:
                 for mode_index_in in s.mode_index_in.values:
@@ -645,7 +642,7 @@ def write_sparameters_batch(
         run_only: The run only specification for the ComponentModeler. Defaults to None.
         element_mappings: The element mappings for the ComponentModeler. Defaults to ().
         extra_monitors: The extra monitors for the ComponentModeler. Defaults to None.
-        mode_spec: The mode specification for the ComponentModeler. Defaults to td.ModeSpec(num_modes=1, filter_pol="te").
+        mode_spec: The mode specification for the ComponentModeler. Defaults to td.ModeSpec(num_modes=1).
         boundary_spec: The boundary specification for the ComponentModeler.
             Defaults to td.BoundarySpec.all_sides(boundary=td.PML()).
         symmetry (tuple[Symmetry, Symmetry, Symmetry], optional): The symmetry for the simulation. Defaults to (0,0,0).

Versions
Note that I first made the suggested fix to the notebook described in #673 to add the line:

gf.gpdk.PDK.activate()
                                                      Modules                                                      
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃        Package ┃ version                                        ┃             Path ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│         python │ 3.12.8 (main, Jan 14 2025, 22:49:14) [Clang    │           <path> │
│                │ 19.1.6 ]                                       │                  │
│     gdsfactory │ 9.35.1                                         │           <path> │
│         devsim │ not installed                                  │                  │
│        femwell │ not installed                                  │                  │
│ gdsfactoryplus │ not installed                                  │                  │
│       gplugins │ 2.0.1                                          │           <path> │
│       kfactory │ 2.4.2                                          │           <path> │
│         lumapi │ not installed                                  │                  │
│           meep │                                                │                  │
│           meow │ not installed                                  │                  │
│            ray │ not installed                                  │                  │
│            sax │ 0.16.6                                         │           <path> │
│         tidy3d │ 2.10.2                                         │           <path> │
│          vlsir │ not installed                                  │                  │
└────────────────┴────────────────────────────────────────────────┴──────────────────┘

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions