Skip to content

Remove sympy.simplify() from solver templates and internal paths - #100

Merged
lmoresi merged 1 commit into
developmentfrom
bugfix/remove-simplify
Mar 30, 2026
Merged

Remove sympy.simplify() from solver templates and internal paths#100
lmoresi merged 1 commit into
developmentfrom
bugfix/remove-simplify

Conversation

@lmoresi

@lmoresi lmoresi commented Mar 30, 2026

Copy link
Copy Markdown
Member

Summary

  • Remove sympy.simplify() from all solver template lambdas and internal code paths
  • Fixes TypeError crash when SymPy simplification walks into UWexpression.__new__ with unhashable arguments
  • User-facing simplify flags (visualisation, evaluate, expressions) preserved

Changes

5 files, 9 call sites:

  • solvers.py: Stokes F1/PF0, Poisson F1, Diffusion F0, NavierStokes PF0
  • solver_template.py: generic F1 template
  • constitutive_models.py: TransverseIsotropic stiffness tensor setup
  • petsc_generic_snes_solvers.pyx: Schur preconditioner computation
  • swarm.py: sympy.simplify(0) replaced with sympy.S.Zero

Rationale

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 call sympy.simplify() themselves.

Test plan

  • Tier A level 1: 33 passed
  • Tier A+B level 2: 174 passed, 0 failures

Underworld development team with AI support from Claude Code

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
Copilot AI review requested due to automatic review settings March 30, 2026 04:42
@lmoresi
lmoresi merged commit 418eb00 into development Mar 30, 2026
3 checks passed

Copilot AI 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.

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) with sympy.S.Zero in 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.

@lmoresi
lmoresi deleted the bugfix/remove-simplify branch June 13, 2026 00:51
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