Skip to content

Conversation

yaugenst-flex
Copy link
Collaborator

@yaugenst-flex yaugenst-flex commented Sep 5, 2025

Greptile Summary

Updated On: 2025-09-05 10:24:39 UTC

This PR introduces a new topology optimization initialization feature to the Tidy3D inverse design module. The primary addition is the initialize_params_from_simulation function, which allows users to initialize design parameters for topology optimization by matching the permittivity distribution of an existing simulation within a specified design region.

The function addresses a common challenge in topology optimization where starting from random or uniform parameter values can lead to poor convergence or suboptimal results. Instead, this implementation uses L-BFGS-B optimization with automatic differentiation to find parameter values that best reproduce the permittivity distribution from a base simulation. The optimization minimizes the L2 error between the base simulation's permittivity and the parameterized structure's permittivity.

Key technical features include:

  • Permittivity extraction and interpolation using scipy's zoom function to handle resolution differences
  • Bounded optimization with L-BFGS-B that respects parameter constraints
  • Early stopping mechanism using a sliding window to detect convergence based on relative improvement tolerance
  • Comprehensive error handling that preserves the best solution even if optimization is interrupted
  • Proper normalization to handle edge cases where the denominator might be zero

The function is integrated into the public API through the __init__.py file, making it easily discoverable for users. The implementation follows established patterns in the codebase and includes thorough documentation with examples. A comprehensive test suite validates the function's behavior, ensuring shape preservation, bounds compliance, and actual loss reduction during optimization.

This addition enhances the inverse design workflow by enabling users to start topology optimization from realistic initial conditions derived from existing simulations, potentially leading to faster convergence and better optimization outcomes.

Important Files Changed

Changed Files
Filename Score Overview
tidy3d/plugins/autograd/invdes/__init__.py 5/5 Added import and export for the new initialize_params_from_simulation function to the public API
CHANGELOG.md 4/5 Added changelog entry documenting the new topology optimization initialization function
tidy3d/plugins/autograd/invdes/parametrizations.py 4/5 Implemented the core initialize_params_from_simulation function with L-BFGS-B optimization and early stopping
tests/test_plugins/autograd/invdes/test_initialize_params_from_sim.py 4/5 Added comprehensive test suite for the new initialization function with helper functions and validation checks

Confidence score: 4/5

  • This PR is safe to merge with minimal risk as it adds a well-contained new feature without modifying existing functionality
  • Score reflects solid implementation with proper optimization techniques, error handling, and comprehensive testing, but could benefit from additional edge case validation
  • Pay close attention to the optimization logic in parametrizations.py to ensure the permittivity matching and early stopping mechanisms work correctly across different simulation types

Sequence Diagram

sequenceDiagram
    participant User
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

Copy link
Contributor

github-actions bot commented Sep 5, 2025

Diff Coverage

Diff: origin/develop...HEAD, staged and unstaged changes

  • tidy3d/plugins/autograd/invdes/init.py (100%)
  • tidy3d/plugins/autograd/invdes/parametrizations.py (89.9%): Missing lines 287-291,305-306

Summary

  • Total: 70 lines
  • Missing: 7 lines
  • Coverage: 90%

tidy3d/plugins/autograd/invdes/parametrizations.py

  283             state["best_val"] = val
  284             state["best_x"] = xk.copy()
  285         state["vals"].append(val)
  286         if len(state["vals"]) == WINDOW:
! 287             v_then = state["vals"][0]
! 288             v_now = state["vals"][-1]
! 289             rel_improve = (v_then - v_now) / max(abs(v_then), 1e-12)
! 290             if rel_improve < rel_improve_tol:
! 291                 raise StopIteration
  292 
  293     bounds_list = [bounds] * params0.size
  294     try:
  295         res = minimize(

  301             callback=callback,
  302             options={"maxiter": maxiter, "disp": verbose},
  303         )
  304         x_final = res.x
! 305     except StopIteration:
! 306         x_final = state["best_x"]
  307     return x_final.reshape(params0.shape)

Copy link
Collaborator

@tylerflex tylerflex left a comment

Choose a reason for hiding this comment

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

Can test in my Smatrix notebook branch in a bit

@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/topopt-init branch 3 times, most recently from a148778 to 2f2657e Compare September 8, 2025 06:59
Copy link
Collaborator

@tylerflex tylerflex left a comment

Choose a reason for hiding this comment

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

Worked great in my test notebook, and overall implementation is good. Thank @yaugenst-flex . A couple comments:

  1. I see no reason to not import this also into the main tidy3d.plugins.autograd namespace. I forget why we decided to separate them into .invdes. do you remember?
  2. In some cases the param_to_structure might take not just params but also **kwargs. In my notebook, it took beta so I had to passs functools.partial(param_to_structure, beta=1), which is not a huge deal but I think it would be kind of nice to just allow passing kwargs into the param_to_structure what do you think?

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