-
Notifications
You must be signed in to change notification settings - Fork 54
Upload, but not run, simulation for write_sparameters routine #664
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 3 commits
f44bdf3
c2134d2
f206f13
8e7fff3
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,9 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from functools import cached_property | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any, Literal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import tidy3d.web.api.webapi as web | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from tidy3d.components.monitor import FieldMonitor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import matplotlib.pyplot as plt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import tidy3d as td | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -277,7 +280,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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -445,6 +448,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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -591,25 +595,40 @@ def write_sparameters( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dict(np.load(filepath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| time.sleep(0.2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s = modeler.run() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "core" not in c.layers: | |
| raise ValueError('Layer "core" not found in component layers. Please ensure the component has a "core" layer.') |
Outdated
Copilot
AI
Dec 12, 2025
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 freqs parameter for FieldMonitor expects a list or array of frequencies, but td.constants.C_0 / wavelength produces a scalar value. This should be wrapped in a list or array, for example: freqs=[td.constants.C_0 / wavelength].
| birdseye = FieldMonitor(name="birdseye", interval_space=(4, 4, 1),freqs=td.constants.C_0 / wavelength, center=(c.center[0], c.center[1], cz), size=(c.size[0], c.size[1], 0)) | |
| birdseye = FieldMonitor(name="birdseye", interval_space=(4, 4, 1),freqs=[td.constants.C_0 / wavelength], center=(c.center[0], c.center[1], cz), size=(c.size[0], c.size[1], 0)) |
Outdated
Copilot
AI
Dec 12, 2025
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.
Missing space after comma in the interval_space parameter. The code has interval_space=(4, 4, 1),freqs= but should have a space between the comma and freqs.
| birdseye = FieldMonitor(name="birdseye", interval_space=(4, 4, 1),freqs=td.constants.C_0 / wavelength, center=(c.center[0], c.center[1], cz), size=(c.size[0], c.size[1], 0)) | |
| birdseye = FieldMonitor(name="birdseye", interval_space=(4, 4, 1), freqs=td.constants.C_0 / wavelength, center=(c.center[0], c.center[1], cz), size=(c.size[0], c.size[1], 0)) |
Copilot
AI
Dec 12, 2025
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 logic condition if s.status != "completed" appears inverted. When the status is NOT completed (e.g., failed, pending, etc.), the code proceeds to extract S-parameters and save results. This should only happen when the status IS "completed". The condition should be if s.status == "completed" instead.
| if s.status != "completed": | |
| if s.status == "completed": |
Copilot
AI
Dec 12, 2025
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 redundant condition check if not upload_only is unnecessary since the code is already within an else block where upload_only is False (from line 598's if upload_only). This condition will always be True and can be removed.
| if not upload_only: | |
| s = modeler.run() | |
| if s.status != "completed": | |
| 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 | |
| s = modeler.run() | |
| if s.status != "completed": | |
| 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 |
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 function can return None when the simulation status is not "completed" (due to the inverted condition on line 613). The return type annotation specifies
Sparameters, which does not include None as a valid return type. This creates a type safety issue. The return type should be updated toSparameters | Noneor the logic should ensure a valid return in all paths.