-
Notifications
You must be signed in to change notification settings - Fork 62
Upload, but not run, simulation for write_sparameters routine #728
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
base: main
Are you sure you want to change the base?
Changes from all commits
f44bdf3
c2134d2
f206f13
8e7fff3
4ff59e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,11 +24,13 @@ | |||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||
| import tidy3d as td | ||||||||||||||||||||||||||||||||||
| import tidy3d.web as web | ||||||||||||||||||||||||||||||||||
| import tidy3d.web.api.webapi as webapi | ||||||||||||||||||||||||||||||||||
| from gdsfactory.component import Component | ||||||||||||||||||||||||||||||||||
| from gdsfactory.pdk import get_layer_stack | ||||||||||||||||||||||||||||||||||
| from gdsfactory.technology import LayerStack | ||||||||||||||||||||||||||||||||||
| from pydantic import NonNegativeFloat | ||||||||||||||||||||||||||||||||||
| from tidy3d.components.geometry.base import from_shapely | ||||||||||||||||||||||||||||||||||
| from tidy3d.components.monitor import FieldMonitor | ||||||||||||||||||||||||||||||||||
| from tidy3d.components.types import Symmetry | ||||||||||||||||||||||||||||||||||
| from tidy3d.plugins.smatrix import ModalComponentModeler, Port | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -272,7 +274,7 @@ def get_component_modeler( | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| cz = np.round(cz, abs(int(np.log10(grid_eps)))).item() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| freqs = td.C_0 / np.linspace( | ||||||||||||||||||||||||||||||||||
| freqs = td.constants.C_0 / np.linspace( | ||||||||||||||||||||||||||||||||||
| wavelength - bandwidth / 2, wavelength + bandwidth / 2, num_freqs | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -437,6 +439,7 @@ def write_sparameters( | |||||||||||||||||||||||||||||||||
| plot_epsilon: bool = False, | ||||||||||||||||||||||||||||||||||
| filepath: PathType | None = None, | ||||||||||||||||||||||||||||||||||
| overwrite: bool = False, | ||||||||||||||||||||||||||||||||||
| upload_only: bool = False, | ||||||||||||||||||||||||||||||||||
| **kwargs: Any, | ||||||||||||||||||||||||||||||||||
| ) -> Sparameters: | ||||||||||||||||||||||||||||||||||
| """Writes the S-parameters for a component. | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+442
to
445
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): When The |
||||||||||||||||||||||||||||||||||
|
|
@@ -482,7 +485,7 @@ def write_sparameters( | |||||||||||||||||||||||||||||||||
| filepath: Optional file path for the S-parameters. If None, uses hash of simulation. | ||||||||||||||||||||||||||||||||||
| overwrite: Whether to overwrite existing S-parameters. Defaults to False. | ||||||||||||||||||||||||||||||||||
| kwargs: Additional keyword arguments for the tidy3d Simulation constructor. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| upload_only: Whether to upload the simulation to the cloud only. Defaults to False. | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| layer_stack = layer_stack or get_layer_stack() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -581,31 +584,58 @@ def write_sparameters( | |||||||||||||||||||||||||||||||||
| return dict(np.load(filepath)) | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| time.sleep(0.2) | ||||||||||||||||||||||||||||||||||
| modeler_data = web.run( | ||||||||||||||||||||||||||||||||||
| modeler, # TODO: web.run does not currently support ModalComponentModeler, need to convert to tidy3d_stub.SimulationType | ||||||||||||||||||||||||||||||||||
| task_name=task_name, | ||||||||||||||||||||||||||||||||||
| verbose=verbose, | ||||||||||||||||||||||||||||||||||
| path=path_dir / "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: | ||||||||||||||||||||||||||||||||||
| for mode_index_out in s.mode_index_out.values: | ||||||||||||||||||||||||||||||||||
| sp[f"{port_in}@{mode_index_in},{port_out}@{mode_index_out}"] = ( | ||||||||||||||||||||||||||||||||||
| s.sel( | ||||||||||||||||||||||||||||||||||
| port_in=port_in, | ||||||||||||||||||||||||||||||||||
| port_out=port_out, | ||||||||||||||||||||||||||||||||||
| mode_index_in=mode_index_in, | ||||||||||||||||||||||||||||||||||
| mode_index_out=mode_index_out, | ||||||||||||||||||||||||||||||||||
| ).values | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| frequency = s.f.values | ||||||||||||||||||||||||||||||||||
| sp["wavelengths"] = td.constants.C_0 / frequency | ||||||||||||||||||||||||||||||||||
| np.savez_compressed(filepath, **sp) | ||||||||||||||||||||||||||||||||||
| print(f"Simulation saved to {filepath!r}") | ||||||||||||||||||||||||||||||||||
| return sp | ||||||||||||||||||||||||||||||||||
| if upload_only: | ||||||||||||||||||||||||||||||||||
| plot_sources = [ | ||||||||||||||||||||||||||||||||||
| modeler.to_source(port=p, mode_index=0) for p in modeler.ports | ||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||
| plot_monitors = [modeler.to_monitor(port=p) for p in modeler.ports] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| cz = c.get_layer_center("core")[2] | ||||||||||||||||||||||||||||||||||
| birdseye = FieldMonitor( | ||||||||||||||||||||||||||||||||||
| name="birdseye", | ||||||||||||||||||||||||||||||||||
| interval_space=(4, 4, 1), | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+587
to
+596
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question (bug_risk): Because this branch is only entered in the |
||||||||||||||||||||||||||||||||||
| freqs=td.constants.C_0 / wavelength, | ||||||||||||||||||||||||||||||||||
| center=(c.center[0], c.center[1], cz), | ||||||||||||||||||||||||||||||||||
| size=(c.size[0], c.size[1], 0), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+593
to
+600
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding the layer name
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| sim_with_sources = modeler.simulation.copy( | ||||||||||||||||||||||||||||||||||
| update={ | ||||||||||||||||||||||||||||||||||
| "sources": plot_sources, | ||||||||||||||||||||||||||||||||||
| "monitors": list(modeler.simulation.monitors) | ||||||||||||||||||||||||||||||||||
| + plot_monitors | ||||||||||||||||||||||||||||||||||
| + [birdseye], | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| s = webapi.upload(sim_with_sources, task_name=folder_name) | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In
Suggested change
|
||||||||||||||||||||||||||||||||||
| return s | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| modeler_data = web.run( | ||||||||||||||||||||||||||||||||||
| modeler, # TODO: web.run does not currently support ModalComponentModeler, need to convert to tidy3d_stub.SimulationType | ||||||||||||||||||||||||||||||||||
| task_name=task_name, | ||||||||||||||||||||||||||||||||||
| verbose=verbose, | ||||||||||||||||||||||||||||||||||
| path=path_dir / "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: | ||||||||||||||||||||||||||||||||||
| for mode_index_out in s.mode_index_out.values: | ||||||||||||||||||||||||||||||||||
| sp[f"{port_in}@{mode_index_in},{port_out}@{mode_index_out}"] = ( | ||||||||||||||||||||||||||||||||||
| s.sel( | ||||||||||||||||||||||||||||||||||
| port_in=port_in, | ||||||||||||||||||||||||||||||||||
| port_out=port_out, | ||||||||||||||||||||||||||||||||||
| mode_index_in=mode_index_in, | ||||||||||||||||||||||||||||||||||
| mode_index_out=mode_index_out, | ||||||||||||||||||||||||||||||||||
| ).values | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| frequency = s.f.values | ||||||||||||||||||||||||||||||||||
| sp["wavelengths"] = td.constants.C_0 / frequency | ||||||||||||||||||||||||||||||||||
| np.savez_compressed(filepath, **sp) | ||||||||||||||||||||||||||||||||||
| print(f"Simulation saved to {filepath!r}") | ||||||||||||||||||||||||||||||||||
| return sp | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def write_sparameters_batch( | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type annotation of
write_sparametersis currentlySparameters(which is a dictionary). However, whenupload_onlyisTrue, the function returns the task ID/handle (a string) fromwebapi.upload. To ensure type safety and correct static analysis, the return type annotation should be updated toSparameters | str.