fix(constitutive_model): make flux_jacobian settable for custom Jacobian tangents#362
Draft
bknight1 wants to merge 1 commit into
Draft
fix(constitutive_model): make flux_jacobian settable for custom Jacobian tangents#362bknight1 wants to merge 1 commit into
bknight1 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes Constitutive_Model.flux_jacobian writable so callers can provide a custom SymPy expression used specifically for Jacobian/tangent assembly (while leaving the residual flux unchanged), enabling more robust/faster Newton solves in cases where differentiating the exact flux is problematic.
Changes:
- Added a
_flux_jacobianbacking attribute initialized toNoneinConstitutive_Model.__init__. - Updated
flux_jacobiangetter to return the backing value instead of always returningNone. - Added a
flux_jacobiansetter to store a user-provided expression.
Comments suppressed due to low confidence (1)
src/underworld3/constitutive_models.py:619
- Typo in the _reset() docstring: "consitutive" should be "constitutive". Since this hunk is being touched, it’s a good opportunity to correct the spelling to avoid propagating it into user-facing docs/help.
"""Flags that the expressions in the consitutive tensor need to be refreshed and also that the
solver will need to rebuild the stiffness matrix and jacobians"""
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+612
to
+615
| @flux_jacobian.setter | ||
| def flux_jacobian(self, value): | ||
| """Set a custom Jacobian flux expression (or None to revert).""" | ||
| self._flux_jacobian = value |
…ian tangents The flux_jacobian property on Constitutive_Model was read-only and always returned None. This prevented models from providing a simplified flux expression for the Jacobian independently of the exact residual flux. Now it has a getter/setter backed by an instance attribute. Default remains None (identical behaviour) — setting it to a SymPy expression and enabling consistent_jacobian = True on the solver gives the solver a custom expression to differentiate for the Jacobian tangent while the residual keeps the exact flux. Use cases: - Viscoplastic models with sharp yield kinks: supply a smooth constitutive law for the tangent only (Newton avoids Min-composite derivative discontinuities). - Multicomponent diffusion with composition-dependent coefficients: supply a constant-coefficient flux so the Jacobian gets a clean SPD stiffness matrix (D_ii · ∇·∇) instead of the zero-G3 from UWexpression-wrapped coefficients, reducing Newton iterations from ~50 to ~3.
7890acd to
1046c9e
Compare
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
Makes
Constitutive_Model.flux_jacobiansettable (was read-only, always returnedNone).Motivation
The
flux_jacobianproperty is consumed by_newton_fluxin the Cython solver base class (used by bothSNES_ScalarandSNES_Stokes_SaddlePt) whenconsistent_jacobian=True. Despite this, the base class had no way to store a custom expression — subclasses had to override the property, andGenericFluxModel(the most common flux model) couldn't provide one at all.Use cases
Multicomponent diffusion (composition-dependent
D_ijcoefficients): The exact flux wrapped in UWexpressions becomes opaque to SymPy diff, givingG3 = 0(no stiffness matrix). A constant-coefficientflux_jacobianrestores the SPD stiffness term, dropping Newton iterations from ~50 to ~3.Viscoplasticity with sharp yield kinks: Supply a smooth constitutive law for the Jacobian tangent while the residual keeps the exact sharp-Min yield surface, avoiding derivative discontinuities in Newton.
Change
self._flux_jacobian = Noneto__init__self._flux_jacobian(defaultNone— identical behaviour)Default remains
None. Zero impact on existing models.