Skip to content

Cache S-matrix linear-algebra artifacts for repeated solves; add docs and tests#10

Open
darkviruzz wants to merge 3 commits intomasterfrom
codex/optimize-rcwa-code-for-performance
Open

Cache S-matrix linear-algebra artifacts for repeated solves; add docs and tests#10
darkviruzz wants to merge 3 commits intomasterfrom
codex/optimize-rcwa-code-for-performance

Conversation

@darkviruzz
Copy link
Owner

Motivation

  • Reduce redundant linear-algebra cost in repeated S-matrix assembly by caching per-layer inverses/factorizations.
  • Provide internal invalidation rules so cached artifacts stay consistent when layer eigensystems or material grids change.
  • Document planned optimization tasks (1D mode inference, S-matrix caching, phased implementation) for future performance work.

Description

  • Added an internal cache on the solver object: _smatrix_cache, with helper methods _invalidate_smatrix_cache() and _get_smatrix_cache() that build and return cached phi and kp@phi inverses.
  • Wire the cache into the solve path by changing GetSMatrix, SolveExterior, and SolveInterior to accept an optional smatrix_cache parameter and use cached phi/kpphi inverses when provided.
  • Invalidate the cache at appropriate points by calling _invalidate_smatrix_cache() from setup and when patterned-layer epsilon is updated (added call in GridLayer_geteps and after initial layer setup).
  • Make public solve entry points use the cache by passing self._get_smatrix_cache() from methods like RT_Solve, GetAmplitudes*, and Volume_integral.
  • Add documentation docs/optimization_tasks.rst describing 1D inference and S-matrix caching tasks and include it in docs/index.rst.
  • Add unit tests in tests/test_rcwa.py covering cache availability, cached vs uncached numerical equivalence for GetSMatrix, SolveExterior, and SolveInterior, cache reuse across repeated solves, and cache invalidation after updating patterned epsilon.

Testing

  • Ran the test suite (pytest) including the new tests test_smatrix_cache_unavailable_before_patterned_setup, test_getsmatrix_cached_vs_uncached_identical, test_exterior_interior_cached_vs_uncached_identical, and test_smatrix_cache_reuse_and_invalidate, and all tests completed successfully.
  • Existing RCWA regression checks in test_rcwa were also executed and remained within tolerance.
  • No automated tests failed after these changes.

Codex Task

@darkviruzz darkviruzz marked this pull request as ready for review February 27, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant