Skip to content

add topOpt-based contour finetuning notebook#515

Open
marcorudolphflex wants to merge 8 commits into
pre/2.12from
marco/wdm-contour-only-pre-2.12
Open

add topOpt-based contour finetuning notebook#515
marcorudolphflex wants to merge 8 commits into
pre/2.12from
marco/wdm-contour-only-pre-2.12

Conversation

@marcorudolphflex

@marcorudolphflex marcorudolphflex commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a new autograd notebook that starts from a saved topology optimized WDM pixel design, converts it to contour geometry, compares the conversion loss, and recovers performance with contour optimization.

Changes

  • add Autograd32WDMContourOptimization.ipynb
  • add placeholder thumbnail img/adjoint_32.png
  • add saved pixel-density input misc/wdm_contour_only_pixel_density.npy
  • register the notebook in docs/features/autograd.rst
  • register the misc asset in misc/import_file_mapping.json
  • clean notebook publish hygiene:
    • remove local-cache setup, hardcoded API key, version print, and dev kernel metadata
    • keep repo-relative asset and export paths
    • keep saved outputs while removing private-path leakage
    • add required notebook metadata fields

Validation

  • uvx ruff format Autograd32WDMContourOptimization.ipynb
  • uvx ruff check Autograd32WDMContourOptimization.ipynb
  • uv run --no-project python scripts/validate_notebook_metadata.py Autograd32WDMContourOptimization.ipynb
  • uv run --no-project python scripts/check_notebook_private_paths.py Autograd32WDMContourOptimization.ipynb
  • python3 misc/check_misc_references.py
  • uv run spellcheck.py Autograd32WDMContourOptimization.ipynb
  • pre-commit run --files Autograd32WDMContourOptimization.ipynb docs/features/autograd.rst misc/import_file_mapping.json

Notes

  • spellcheck.py passes for the new notebook, but the command still prints legacy schema warnings from unrelated reference notebooks in this branch while building its reference dictionary.
  • The thumbnail is a placeholder and can be replaced later without changing notebook metadata.

Note

Low Risk
Example notebook and doc/asset wiring only; no changes to library APIs or runtime behavior.

Overview
Adds Autograd32WDMTopologyToContourFinetuning.ipynb, an inverse-design example that loads a saved topology-optimized WDM pixel density, thresholds it, builds smoothed contour geometry via PolySlabSet, measures the conversion loss against the pixel baseline, and fine-tunes contour vertices with autograd adjoint optimization (including GDS round-trip checks).

Supporting assets: misc/wdm_contour_only_pixel_density.npy, placeholder thumbnail img/adjoint_32.png, plus registration in docs/features/autograd.rst and misc/import_file_mapping.json. The notebook is cleaned for publish (repo-relative paths, required metadata, no secrets or local-only setup).

Reviewed by Cursor Bugbot for commit f860f2b. Bugbot is set up for automated code reviews on this repo. Configure here.

@marcorudolphflex marcorudolphflex force-pushed the marco/wdm-contour-only-pre-2.12 branch from 7551b68 to fcc6fac Compare June 9, 2026 10:44
@marcorudolphflex marcorudolphflex changed the title [codex] add WDM contour postprocessing notebook add topOpt-based contour finetuning notebook Jun 9, 2026
@marcorudolphflex marcorudolphflex marked this pull request as ready for review June 9, 2026 11:54
Comment thread Autograd32WDMTopologyToContourFinetuning.ipynb
Comment thread Autograd32WDMTopologyToContourFinetuning.ipynb Outdated

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

Thanks @marcorudolphflex, this is a really cool notebook! I ended up organizing feedback in a markdown doc to try and make it easy to align with the different sections. I've attached it here!

top_opt_code_review.md

@marcorudolphflex marcorudolphflex force-pushed the marco/wdm-contour-only-pre-2.12 branch from edf2d94 to 6fec69d Compare June 24, 2026 15:37
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Spell check passed successfully for 1 notebook(s).
Generated by GitHub Action run: https://github.com/flexcompute/tidy3d-notebooks/actions/runs/28666831215

@marcorudolphflex

Copy link
Copy Markdown
Contributor Author

Thanks for the good and detailed review. I went through the notebook text and structure and addressed the main points as follows:

  • I expanded the introduction so it now frames the notebook explicitly as a topology-to-shape workflow: top-opt provides a strong seed in a large design space, but the binarized result remains staircased and not fabrication-friendly, while contour optimization is easier to export and regularize but benefits from a good initial design.
  • I defined the extracted polygon rings more explicitly in plain language, including outer boundaries and interior holes.
  • I cleaned up the setup and helper text in the places you flagged: CustomMedium is named consistently, the 3D extension wording is clearer, and the helper section now introduces those cells as reusable building blocks for the later comparisons and optimization.
  • I rewrote the transmission-metric explanation in words, so it is clearer that we use per-port in-band mode power minus leaked power from the other channels, followed by a smooth minimum over channels.
  • For the GDS baseline comparison, I added an earlier port-resolved dense spectral comparison so the effect of export/import is visible per channel rather than only through the scalar objective.
  • I reworked the pixel-to-contour and geometry-check sections to read more narratively and to explain more clearly why smoothing is needed from a fabrication perspective.
  • I expanded the contour-parameterization / safe-update explanation to spell out frame freezing, bounds clipping, and the self-intersection rejection step.
  • I clarified the contour-optimization text so it describes the accepted-state update more directly.
  • I split the optimization tracking so the transmission term and weighted curvature penalty are shown separately, as you suggested.
  • I expanded the conclusion so it is more broadly about the workflow and also points readers more explicitly to the plugin functionality used along the way.

On the visualization point: I tried the idea of showing contour evolution more directly, but in practice the accepted contour changes are small enough that a generic zoomed evolution view was not very visually informative. Instead, I now keep the global overview and then zoom into the local blocks with the largest differences, which makes the actual contour change easier to see.

As discussed, we stick to the import file approach and will offer a bundled download in the examples center to ensure it can be run by users with the shown input. Actually I just tested that feature locally and it worked!

One thing I am still a bit skeptical about is the rough corners in the zoom point. Should we revisit the curvature penalty here?
image

@groberts-flex

groberts-flex commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

thanks for making all of those changes and I like that visualization style. With regard to "One thing I am still a bit skeptical about is the rough corners in the zoom point. Should we revisit the curvature penalty here?", it looks like the rough corners are more present in the red rather than the gray. Is the red the final state?

@groberts-flex groberts-flex self-requested a review June 30, 2026 20:50

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

thanks for all of the changes! everything look great overall, just a few minor comments below!

  1. In the section Defining the Transmission Metric: "The final transmission metric is the smooth minimum of the four channel scores, which makes the weakest channel control the comparison and the later contour update." -> maybe change the wording of weakest channel "control the comparison" to something like "control the loss function" or "primarily control the loss function"
  2. Same section as 1, is there a reason to specify at this point things are not in dB? "The metric itself is computed from linear power, not in dB." -> could point that out later in the plots or just say the metric is computed in linear power.
  3. Maybe expand the "Export to GDS and Load It Back" section with a little more markdown text to explain more what's going on at that point. Or clarify that the function there is being set up for being used later to test the gds round trip.
  4. maybe for the cell that starts with num_freqs_measure = 151 # dense wavelength samples for the port resolved flux and power plots, we could add a little markdown before it to explain and/or docstrings to the functions there.
  5. for this line: "This notebook therefore assumes that misc/wdm_contour_only_pixel_density.npy is available locally as the starting point for the following conversion and contour optimization comparisons." do want to say that it should have been bundled with the download?
  6. one quick question, in "Defining Objective Function", in this line "The main optical term is the transmission metric measured from the four output waveguides." -> is it the same transmission based metric from before? if so would be good to clarify that or explain why it changed.
  7. more a question than a comment on the notebook. in this part of the contour optimization explanation "vertices on the outer frame are frozen so the design stays attached to the original device boundary, " -> is there a reason we need to keep the ones on the boundary attached to the boundary? it makes sense why they would be prevented from leaving the design region, but wouldn't it be ok if they were moved inside the region further?
  8. in the "Structure Evolution" section, this line "before the local zoom panels isolate the ring segments that move most." -> past tense for move most to read "isolate the ring segments that moved the most"
  9. In the section "Field Analysis", we can probably remove this sentence: "A 2x2 layout gives each wavelength map a bit more space."

@marcorudolphflex

Copy link
Copy Markdown
Contributor Author

thanks for making all of those changes and I like that visualization style. With regard to "One thing I am still a bit skeptical about is the rough corners in the zoom point. Should we revisit the curvature penalty here?", it looks like the rough corners are more present in the red rather than the gray. Is the red the final state?

Yes it is, should put a legend to it.
Still not sure how to handle. Increase the penalty weight, change the penalty function? Can try both.

@groberts-flex

Copy link
Copy Markdown
Contributor

thanks for making all of those changes and I like that visualization style. With regard to "One thing I am still a bit skeptical about is the rough corners in the zoom point. Should we revisit the curvature penalty here?", it looks like the rough corners are more present in the red rather than the gray. Is the red the final state?

Yes it is, should put a legend to it. Still not sure how to handle. Increase the penalty weight, change the penalty function? Can try both.

Sounds good! yeah, it could the penalty function form. It is optimizing and reducing the penalty function over the optimization so it's interesting it looks less smooth when zoomed in. So maybe the penalty is as it is currently written isn't sensitive to these smaller abrupt changes and is looking at curvature on a larger scale?

@groberts-flex

Copy link
Copy Markdown
Contributor

One thing you could try is having control points and then having the actual control be a smooth spline through the control points. Or have a base set of vertices and then pass them through a smoothing filter each time. The tricky part of this might be the self intersection free update part though since you wouldn't be directly dealing with the vertices that define the polyslab.

@marcorudolphflex

Copy link
Copy Markdown
Contributor Author

One thing you could try is having control points and then having the actual control be a smooth spline through the control points. Or have a base set of vertices and then pass them through a smoothing filter each time. The tricky part of this might be the self intersection free update part though since you wouldn't be directly dealing with the vertices that define the polyslab.

Actually I prepared some branch for this https://github.com/flexcompute/compute/pull/4896 but - as you already suspected - this was not super easy to handle and at one point I was not sure if this is needed (looks smooth when you zoom out). May revisit later

@marcorudolphflex

Copy link
Copy Markdown
Contributor Author

thanks for making all of those changes and I like that visualization style. With regard to "One thing I am still a bit skeptical about is the rough corners in the zoom point. Should we revisit the curvature penalty here?", it looks like the rough corners are more present in the red rather than the gray. Is the red the final state?

Yes it is, should put a legend to it. Still not sure how to handle. Increase the penalty weight, change the penalty function? Can try both.

Sounds good! yeah, it could the penalty function form. It is optimizing and reducing the penalty function over the optimization so it's interesting it looks less smooth when zoomed in. So maybe the penalty is as it is currently written isn't sensitive to these smaller abrupt changes and is looking at curvature on a larger scale?

Weirdly, this did improve by just removing the penalty, something is odd here (see slack). Would ignore this for now and just drop the penalty.

@marcorudolphflex

Copy link
Copy Markdown
Contributor Author

addressed your comments!
Worth to mention:

  • the metric later is measured over more frequencies, it is now frames as "dense transmission metric"
  • true, freeze boundary is here not strictly needed, we still clip bounds. Turned out you can turn that off here.
    Will migrate this PR later as we discussed.

@groberts-flex

Copy link
Copy Markdown
Contributor

looks great thanks for the updates!

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