Skip to content

Upload, but not run, simulation for write_sparameters routine#728

Open
jackgdsf wants to merge 5 commits into
gdsfactory:mainfrom
jackgdsf:sim_upload_only
Open

Upload, but not run, simulation for write_sparameters routine#728
jackgdsf wants to merge 5 commits into
gdsfactory:mainfrom
jackgdsf:sim_upload_only

Conversation

@jackgdsf

@jackgdsf jackgdsf commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Supersedes #664 — same changes with merge conflicts resolved against current main.

Original PR by @heydaridlm. This PR is opened from a fork because the original branch needed a merge conflict resolution and we didn't have push access to the upstream branch.


Original description:

In the write_sparameters routine, a simulation is created and automatically deployed/run on tidy servers. I think it is much safer to have an option to upload the simulation only, then let the user go into the Tidy Website GUI and inspect the simulation by hand to see if things make sense, then let the user run the simulation from the Tidy GUI. I also added a 'birdseye' monitor that spans the XY domain of the simulation and slices through the middle of the "core" of the component of interest in Z.

Todo: In doing this, I had to comment out the part where the s parameters are returned, this needs to be fixed, otherwise write_sparameters does not return anything except for the whether or not the upload was successful.

Summary by Sourcery

Add an option for write_sparameters to upload simulations without running them and introduce a diagnostic birdseye monitor for inspection in Tidy3D.

New Features:

  • Introduce an upload_only flag to write_sparameters to allow uploading simulations to the cloud without executing them.
  • Add a birdseye field monitor spanning the XY domain at the core layer center to aid visual inspection of simulations in the Tidy GUI.

Enhancements:

  • Use td.constants.C_0 for frequency and wavelength calculations in component modeling.

@sourcery-ai

sourcery-ai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds an upload_only mode to write_sparameters so that, instead of running the simulation and computing S‑parameters, it can upload a visualization‑oriented simulation (with additional sources and monitors) to Tidy3D for manual inspection, while preserving the existing behavior when upload_only is false.

Sequence diagram for write_sparameters upload_only behavior

sequenceDiagram
    actor User
    participant write_sparameters
    participant modeler as ComponentModeler
    participant web
    participant webapi

    User->>write_sparameters: write_sparameters(..., upload_only=True)
    write_sparameters->>modeler: simulation
    alt upload_only
        write_sparameters->>modeler: to_source(port, mode_index=0)
        write_sparameters->>modeler: to_monitor(port)
        write_sparameters->>modeler: simulation.copy(update={sources, monitors})
        write_sparameters->>webapi: upload(sim_with_sources, task_name)
        webapi-->>write_sparameters: task_handle
        write_sparameters-->>User: task_handle
    else run_simulation
        write_sparameters->>web: run(modeler, task_name, verbose, path)
        web-->>write_sparameters: modeler_data
        write_sparameters->>write_sparameters: smatrix()
        write_sparameters->>write_sparameters: compute_sparameters()
        write_sparameters->>write_sparameters: save npz(filepath)
        write_sparameters-->>User: sparameters
    end
Loading

File-Level Changes

Change Details Files
Introduce an upload_only workflow in write_sparameters that uploads a visualization-focused simulation to Tidy3D instead of running it locally and computing S-parameters.
  • Add an upload_only boolean parameter to write_sparameters and document it in the function docstring.
  • Guard the existing web.run execution path behind an else branch so it is only used when upload_only is False, preserving current behavior.
  • When upload_only is True, construct plotting sources for each port via modeler.to_source, and corresponding monitors via modeler.to_monitor.
  • Add a new birdseye FieldMonitor spanning the XY domain at the core layer’s Z center, configured at the design wavelength, and append it to the simulation monitors.
  • Create a modified copy of modeler.simulation that includes the new sources and monitors and upload it via webapi.upload, returning the resulting task handle instead of local S-parameter data.
gplugins/tidy3d/component.py
Fix usage of the speed-of-light constant in frequency calculations to match the current tidy3d API.
  • Replace td.C_0 with td.constants.C_0 when computing the frequency grid from wavelengths in the component modeler helper.
gplugins/tidy3d/component.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="gplugins/tidy3d/component.py" line_range="442-445" />
<code_context>
     plot_epsilon: bool = False,
     filepath: PathType | None = None,
     overwrite: bool = False,
+    upload_only: bool = False,
     **kwargs: Any,
 ) -> Sparameters:
</code_context>
<issue_to_address>
**issue (bug_risk):** When `upload_only=True`, the function returns a different type than in the normal path.

The `upload_only` branch returns `webapi.upload(...)`, which likely differs from the annotated `Sparameters` return type and from the dict-like S-parameter object returned in the normal path. This divergence can surprise callers and cause runtime errors. Please either keep the return type consistent (e.g., still return `sp` or a dedicated wrapper type) or split the upload behavior into a separate function with its own return annotation.
</issue_to_address>

### Comment 2
<location path="gplugins/tidy3d/component.py" line_range="587-596" />
<code_context>
+        if upload_only:
</code_context>
<issue_to_address>
**question (bug_risk):** `upload_only` is ignored when an S-parameter file already exists, which may be surprising.

Because this branch is only entered in the `else` of `if filepath.exists()`, setting `upload_only=True` has no effect when the file already exists and `overwrite=False`—the function just returns cached data. If `upload_only` is meant to always trigger a cloud upload, consider checking it before returning cached results, or clarify in the docs that it is ignored when a cache is present.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +442 to 445
upload_only: bool = False,
**kwargs: Any,
) -> Sparameters:
"""Writes the S-parameters for a component.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): When upload_only=True, the function returns a different type than in the normal path.

The upload_only branch returns webapi.upload(...), which likely differs from the annotated Sparameters return type and from the dict-like S-parameter object returned in the normal path. This divergence can surprise callers and cause runtime errors. Please either keep the return type consistent (e.g., still return sp or a dedicated wrapper type) or split the upload behavior into a separate function with its own return annotation.

Comment on lines +587 to +596
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),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): upload_only is ignored when an S-parameter file already exists, which may be surprising.

Because this branch is only entered in the else of if filepath.exists(), setting upload_only=True has no effect when the file already exists and overwrite=False—the function just returns cached data. If upload_only is meant to always trigger a cloud upload, consider checking it before returning cached results, or clarify in the docs that it is ignored when a cache is present.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an upload_only option to write_sparameters in gplugins/tidy3d/component.py, allowing users to configure and upload simulations to the cloud without running them locally. The feedback identifies three important improvements: dynamically retrieving the z-center from the simulation instead of hardcoding the "core" layer to prevent potential crashes, explicitly passing both task_name and folder_name to webapi.upload to preserve unique task names, and updating the return type annotation of write_sparameters to Sparameters | str to reflect the new upload-only return value.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +593 to +600
cz = c.get_layer_center("core")[2]
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),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Hardcoding the layer name "core" to find the z-center will cause a KeyError or crash for components or layer stacks that do not define a "core" layer. Instead, we can dynamically retrieve the z-center directly from the simulation's center (modeler.simulation.center[2]). Additionally, the freqs parameter in FieldMonitor should be wrapped in a list/tuple to prevent potential validation errors in tidy3d, and we should use the simulation's center and size to ensure the birdseye monitor perfectly spans the simulation domain.

Suggested change
cz = c.get_layer_center("core")[2]
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),
)
cz = modeler.simulation.center[2]
birdseye = FieldMonitor(
name="birdseye",
interval_space=(4, 4, 1),
freqs=[td.constants.C_0 / wavelength],
center=(modeler.simulation.center[0], modeler.simulation.center[1], cz),
size=(modeler.simulation.size[0], modeler.simulation.size[1], 0),
)

}
)

s = webapi.upload(sim_with_sources, task_name=folder_name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In webapi.upload, the second positional argument is task_name. By passing task_name=folder_name, the uploaded task will be named "default" (or whatever folder_name is set to) on the Tidy3D platform, and the unique task_name (hash) will be lost. We should pass task_name=task_name and folder_name=folder_name explicitly to ensure the task is named uniquely and placed in the correct folder.

Suggested change
s = webapi.upload(sim_with_sources, task_name=folder_name)
s = webapi.upload(sim_with_sources, task_name=task_name, folder_name=folder_name)

overwrite: bool = False,
upload_only: bool = False,
**kwargs: Any,
) -> Sparameters:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The return type annotation of write_sparameters is currently Sparameters (which is a dictionary). However, when upload_only is True, the function returns the task ID/handle (a string) from webapi.upload. To ensure type safety and correct static analysis, the return type annotation should be updated to Sparameters | str.

Suggested change
) -> Sparameters:
) -> Sparameters | str:

@tvt173 tvt173 requested a review from flaport June 15, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants