Skip to content

feat: allow custom GridSpec in tidy3d write_sparameters#729

Open
vvahidd wants to merge 1 commit into
mainfrom
feat/tidy3d-grid-spec
Open

feat: allow custom GridSpec in tidy3d write_sparameters#729
vvahidd wants to merge 1 commit into
mainfrom
feat/tidy3d-grid-spec

Conversation

@vvahidd

@vvahidd vvahidd commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Adds an optional grid_spec: td.GridSpec | None parameter to Tidy3DComponent.get_component_modeler, write_sparameters, and write_sparameters_batch, giving full control over the simulation mesh (e.g. td.GridSpec.uniform(dl=20*nm) or per-axis AutoGrid).

When grid_spec is None (default), behavior is unchanged: it falls back to td.GridSpec.auto(wavelength=wavelength, min_steps_per_wvl=min_steps_per_wvl). min_steps_per_wvl is ignored when grid_spec is provided.

Summary by Sourcery

Add support for user-provided Tidy3D GridSpec when generating component modelers and writing s-parameters, while preserving existing auto grid behavior by default.

New Features:

  • Allow passing a custom td.GridSpec to get_component_modeler for explicit control over the simulation mesh.
  • Expose the grid_spec parameter in write_sparameters and write_sparameters_batch so s-parameter simulations can use user-defined meshing.

Enhancements:

  • Clarify that min_steps_per_wvl is ignored when a custom grid_spec is provided and document the default auto GridSpec behavior in the public API.

@sourcery-ai

sourcery-ai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR adds an optional grid_spec parameter to Tidy3D-related APIs so callers can fully control the simulation mesh, while preserving the existing auto-grid behavior when grid_spec is not provided.

Sequence diagram for grid_spec handling in write_sparameters

sequenceDiagram
    participant Caller
    participant write_sparameters
    participant get_component_modeler
    participant td_GridSpec as td.GridSpec

    Caller->>write_sparameters: write_sparameters(..., grid_spec)
    write_sparameters->>get_component_modeler: get_component_modeler(..., grid_spec)
    alt grid_spec is not None
        get_component_modeler->>get_component_modeler: use provided grid_spec
    else grid_spec is None
        get_component_modeler->>td_GridSpec: auto(wavelength, min_steps_per_wvl)
        td_GridSpec-->>get_component_modeler: GridSpec
    end
Loading

File-Level Changes

Change Details Files
Add optional grid_spec parameter to component modeling and s-parameter writing APIs, using it when provided and falling back to the existing auto grid behavior otherwise.
  • Extend get_component_modeler signature with a grid_spec parameter defaulting to None and pass it through to the grid construction logic.
  • Change grid construction in get_component_modeler to use the provided grid_spec when not None, otherwise call td.GridSpec.auto with wavelength and min_steps_per_wvl.
  • Update docstrings for get_component_modeler, write_sparameters, and write_sparameters_batch to document the new grid_spec parameter and clarify that min_steps_per_wvl is ignored when grid_spec is provided.
  • Extend write_sparameters and write_sparameters_batch signatures with the grid_spec parameter and forward it into get_component_modeler.
  • Clarify default mesh behavior in the public API documentation to ensure backward compatibility when grid_spec is omitted.
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

@github-actions

Copy link
Copy Markdown

Label error. Requires at least 1 of: breaking, bug, github_actions, documentation, dependencies, enhancement, feature, maintenance, security. Found:

@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, and left some high level feedback:

  • In write_sparameters_batch, grid_spec is documented but not added to the function signature or passed through to get_component_modeler, which will confuse users expecting it to work; consider wiring the parameter through consistently or removing it from the docstring.
  • Silently ignoring min_steps_per_wvl when grid_spec is provided may be surprising; consider adding a small runtime check or warning when both are passed to make this behavior explicit to callers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `write_sparameters_batch`, `grid_spec` is documented but not added to the function signature or passed through to `get_component_modeler`, which will confuse users expecting it to work; consider wiring the parameter through consistently or removing it from the docstring.
- Silently ignoring `min_steps_per_wvl` when `grid_spec` is provided may be surprising; consider adding a small runtime check or warning when both are passed to make this behavior explicit to callers.

## Individual Comments

### Comment 1
<location path="gplugins/tidy3d/component.py" line_range="245" />
<code_context>
             wavelength: The wavelength for the ModalComponentModeler. Defaults to 1.55.
             bandwidth: The bandwidth for the ModalComponentModeler. Defaults to 0.2.
             num_freqs: The number of frequencies for the ModalComponentModeler. Defaults to 21.
-            min_steps_per_wvl: The minimum number of steps per wavelength for the ModalComponentModeler. Defaults to 30.
+            min_steps_per_wvl: The minimum number of steps per wavelength for the ModalComponentModeler. Defaults to 30. Ignored when grid_spec is provided.
</code_context>
<issue_to_address>
**issue:** Docstring default for `num_freqs` is inconsistent with the function signature.

In `get_component_modeler`, the parameter `num_freqs` defaults to `6`, but the docstring still says `Defaults to 21.` Please update the docstring so it matches the actual default value.
</issue_to_address>

### Comment 2
<location path="gplugins/tidy3d/component.py" line_range="281-283" />
<code_context>
         )

-        grid_spec = td.GridSpec.auto(
+        grid_spec = grid_spec or td.GridSpec.auto(
             wavelength=wavelength, min_steps_per_wvl=min_steps_per_wvl
         )

</code_context>
<issue_to_address>
**suggestion:** Use an explicit `is None` check for `grid_spec` instead of `grid_spec or ...`.

Using `grid_spec or td.GridSpec.auto(...)` depends on `grid_spec` always being truthy. If `GridSpec` (or a wrapper) ever implements a falsy `__bool__`/`__len__`, the provided spec would be incorrectly replaced. An explicit `if grid_spec is None` check avoids this potential misbehaviour and is safer.

```suggestion
        if grid_spec is None:
            grid_spec = td.GridSpec.auto(
                wavelength=wavelength, min_steps_per_wvl=min_steps_per_wvl
            )
```
</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 thread gplugins/tidy3d/component.py
Comment thread gplugins/tidy3d/component.py

@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 a grid_spec parameter to get_component_modeler and write_sparameters in gplugins/tidy3d/component.py, allowing users to provide a custom grid specification for the simulation mesh. Feedback on this change highlights that the custom grid_spec is not forwarded to the get_component_modeler call used for plotting the simulation, meaning the plotted simulation will not accurately reflect the custom grid configuration.

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 thread gplugins/tidy3d/component.py
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.

1 participant