Remove sympy.simplify() from solver templates and internal paths - #100
Merged
Conversation
sympy.simplify() is expensive and fragile with custom symbol types (UWexpression, UWCoordinate). It triggered TypeError crashes when SymPy's simplification walked into UWexpression.__new__ with unhashable arguments. The simplify calls in solver templates (F0, F1, PF0) were purely cosmetic — the JIT compiler and PETSc do not require simplified expressions. Removed from: solver Template lambdas (Stokes F1, PF0, Poisson F1, Diffusion F0, NavierStokes PF0), solver_template.py, constitutive model setup, preconditioner computation, and swarm createMask(). User-facing simplify flags (in visualisation, evaluate, expressions) are preserved — users can still call simplify when they want cleaner display. Underworld development team with AI support from Claude Code
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes sympy.simplify() from solver template lambdas and a few internal code paths to avoid expensive/fragile simplification (including a reported TypeError involving UWexpression.__new__), while keeping user-facing “simplify” behavior elsewhere.
Changes:
- Remove
sympy.simplify()from multiple solver residual/Jacobian expression templates (Poisson/Stokes/Diffusion/Navier-Stokes). - Remove
sympy.simplify()from Schur preconditioner expression construction and a constitutive-model tensor setup path. - Replace
sympy.simplify(0)withsympy.S.Zeroin swarm mask construction.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/underworld3/systems/solvers.py |
Drops simplification in several solver Template/expression constructors to avoid SymPy walking UWexpression internals. |
src/underworld3/systems/solver_template.py |
Removes simplification from the generic F1 template expression. |
src/underworld3/swarm.py |
Uses sympy.S.Zero instead of sympy.simplify(0) for a clean symbolic zero initializer. |
src/underworld3/cython/petsc_generic_snes_solvers.pyx |
Removes simplify from the default Schur preconditioner scalar expression (1/K). |
src/underworld3/constitutive_models.py |
Removes simplify from rank4→Mandel tensor conversion step. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sympy.simplify()from all solver template lambdas and internal code pathsUWexpression.__new__with unhashable argumentsChanges
5 files, 9 call sites:
solvers.py: Stokes F1/PF0, Poisson F1, Diffusion F0, NavierStokes PF0solver_template.py: generic F1 templateconstitutive_models.py: TransverseIsotropic stiffness tensor setuppetsc_generic_snes_solvers.pyx: Schur preconditioner computationswarm.py:sympy.simplify(0)replaced withsympy.S.ZeroRationale
sympy.simplify()is expensive and fragile with custom symbol types. The calls in solver templates were purely cosmetic — the JIT compiler and PETSc do not require simplified expressions. Users who want cleaner display can callsympy.simplify()themselves.Test plan
Underworld development team with AI support from Claude Code