Skip to content

Fill the view corners in the COMSOL cut-plane field plot - #808

Merged
nikosavola merged 2 commits into
gdsfactory:mainfrom
jackgdsf:807-comsol-field-corners
Nov 25, 2026
Merged

nikosavola merged 2 commits into
gdsfactory:mainfrom
jackgdsf:807-comsol-field-corners

Conversation

@jackgdsf

@jackgdsf jackgdsf commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #807.

draw_cut_plane_field Delaunay-triangulated only every 4th node inside view_um. The view corners fell outside that subset's convex hull and drew blank, not even in the lowest colour. Seen in the QPDK COMSOL CPW resonator notebook.

Changes

  • Triangulate every finite node within one window size of view_um on each side, then crop with the axis limits. The margin keeps the corners inside the hull. A 1M-node export with a 10 µm view takes about 1.6 s locally; triangulating the full export took about 7 s.
  • Drop rows with NaN in x, y or E before triangulating.
  • Nodes that share a position, e.g. on both sides of a metal edge, are merged to their largest value. Before, qhull silently kept whichever came first in the export.
  • Remove stride. gplugins.comsol is unreleased; QPDK's notebook will drop the argument when it bumps its gplugins pin.

Tests

  • New test_field_plot_fills_the_view_corners: nodes on a circle inside the view plus a frame outside it, a NaN node, a far node and a duplicate node. It asserts that the filled contours reach all four corners. It fails on main and passes here.
  • stride= removed from two existing tests; their assertions are unchanged.
  • Run locally against a stub package with only gplugins.comsol.plotting, because the uv venv's Python isn't available in my container. CI is the full check.
  • pre-commit run --files on the changed files is green.

Changelog fragment: .changelog.d/808.fixed.

Requested by @nikosavola

A human must review this before merge.

🤖 Generated with Claude Code

Summary by Sourcery

Fix COMSOL cut-plane field plotting so cropped views render completely and consistently across mesh edge cases.

Bug Fixes:

  • Ensure COMSOL cut-plane field plots fill the full view, including corners, instead of leaving blank regions outside the cropped nodes’ convex hull.

Enhancements:

  • Handle invalid field data safely and merge duplicate node positions using the largest field value.
  • Remove the field-plot drawing stride option and triangulate nearby nodes before applying the view crop.

Tests:

  • Add coverage for corner filling, NaN filtering, distant nodes, and duplicate node values.

draw_cut_plane_field Delaunay-triangulated only a strided subset of the
nodes inside the window, so the corners outside that subset's hull drew
blank. Triangulate every finite node within one window size of the view
and crop with the axis limits; coincident nodes keep their largest value.
Drop the stride parameter.

Fixes gdsfactory#807

Co-authored-by: Niko Savola <niko@gdsfactory.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jackgdsf
jackgdsf requested a review from nikosavola as a code owner November 25, 2026 13:38
Co-authored-by: Niko Savola <niko@gdsfactory.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

API

Update the function contract and docstring to describe margin-based triangulation and remove the stride argument.

  • gplugins/comsol/plotting.py L67 (removed lines), L71-78, L84 (removed lines)

Implementation

Filter invalid and distant export rows, merge coincident positions by maximum field value, and select the crop nodes.

  • gplugins/comsol/plotting.py L90-112

Use all prepared nodes for contouring, update the diagnostic output, and retain the requested axis limits.

  • gplugins/comsol/plotting.py L131-134, L139-143, L149-150

Documentation

Add the changelog fragment for the corner-filling field-plot fix.

  • .changelog.d/808.fixed L1

Tests

Update existing plotting tests for the removed stride parameter and add coverage for filled corners, NaN filtering, distant nodes, and duplicate positions.

  • gplugins/comsol/tests/test_comsol_plotting.py L53, L67-107, L128

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

@jackgdsf
jackgdsf requested a review from joamatab as a code owner November 25, 2026 13:38
@sourcery-ai

sourcery-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 1fac22a

#807 COMSOL draw_cut_plane_field leaves the view corners blank · 4 of 4 met

# Requirement Status Notes
1 Fill the view corners instead of leaving them blank. ✅ Met The plot triangulates finite nodes around the view and crops to the axis limits; the new corner test checks that contours reach all four corners.
2 Avoid speckle from striding scattered nodes near strong-field edges. ✅ Met The stride parameter is removed, and all retained local nodes are used for triangulation; coincident positions keep their largest field value.
3 Drop nodes with NaN coordinates or field values before triangulating. ✅ Met Rows with non-finite x, y, or E are filtered before selecting nodes for triangulation.
4 Remove the stride parameter. ✅ Met The function signature and its tests no longer pass or expose stride.

@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.

Sourcery assessment

Approved.

@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 1 issue

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

## Individual Comments

### Comment 1
<location path="gplugins/comsol/plotting.py" line_range="97-102" />
<code_context>
+    field = field[np.isfinite(field[:, [0, 1, 3]]).all(axis=1)]
+    xmin, xmax, ymin, ymax = view_um
+    margin = max(xmax - xmin, ymax - ymin)
+    near = (
+        (field[:, 0] >= xmin - margin)
+        & (field[:, 0] <= xmax + margin)
+        & (field[:, 1] >= ymin - margin)
+        & (field[:, 1] <= ymax + margin)
+    )
+    # The triangulation keeps one node per position and would drop the rest
+    # silently, in export order.
</code_context>
<issue_to_address>
**Some window corners remain blank**

When the finite cut-plane nodes within the expanded box do not surround `view_um`, such as near a truncated or non-convex simulation domain, `margin` only selects nodes inside an axis-aligned expanded box; it does not ensure their convex hull contains the view corners, so `tricontourf` still leaves those corners blank.

Ensure the triangulated coverage contains the view corners, or explicitly handle corners outside the selected nodes' convex hull.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: gplugins/comsol/plotting.py:102


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +97 to +102
near = (
(field[:, 0] >= xmin - margin)
& (field[:, 0] <= xmax + margin)
& (field[:, 1] >= ymin - margin)
& (field[:, 1] <= ymax + margin)
)

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.

🟡 Medium · Some window corners remain blank

When the finite cut-plane nodes within the expanded box do not surround view_um, such as near a truncated or non-convex simulation domain, margin only selects nodes inside an axis-aligned expanded box; it does not ensure their convex hull contains the view corners, so tricontourf still leaves those corners blank.

Ensure the triangulated coverage contains the view corners, or explicitly handle corners outside the selected nodes' convex hull.

Prompt for AI agents
In `gplugins/comsol/plotting.py` at lines 97-102:

**Some window corners remain blank**

When the finite cut-plane nodes within the expanded box do not surround `view_um`, such as near a truncated or non-convex simulation domain, `margin` only selects nodes inside an axis-aligned expanded box; it does not ensure their convex hull contains the view corners, so `tricontourf` still leaves those corners blank.

Ensure the triangulated coverage contains the view corners, or explicitly handle corners outside the selected nodes' convex hull.

@nikosavola nikosavola self-assigned this Nov 25, 2026
@nikosavola
nikosavola merged commit be1c037 into gdsfactory:main Nov 25, 2026
17 of 21 checks passed
jackgdsf added a commit to jackgdsf/quantum-rf-pdk that referenced this pull request Nov 25, 2026
The corner fix now lives in gplugins.comsol.plotting.draw_cut_plane_field
(gdsfactory/gplugins#808), so drop the notebook's local copy and its test,
call the helper without the removed stride argument, and pin gplugins to
the merge commit.

Co-authored-by: Niko Savola <niko@gdsfactory.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nikosavola added a commit to jackgdsf/quantum-rf-pdk that referenced this pull request Nov 26, 2026
The corner fix now lives in gplugins.comsol.plotting.draw_cut_plane_field
(gdsfactory/gplugins#808), which main already pins, so drop the notebook's
local copy and its test and call the helper without the removed stride
argument.

Co-authored-by: Niko Savola <niko@gdsfactory.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

COMSOL draw_cut_plane_field leaves the view corners blank

2 participants