Skip to content

fix(constitutive_model): make flux_jacobian settable for custom Jacobian tangents#362

Draft
bknight1 wants to merge 1 commit into
developmentfrom
fix/constitutive_model_settable_flux_jacobian
Draft

fix(constitutive_model): make flux_jacobian settable for custom Jacobian tangents#362
bknight1 wants to merge 1 commit into
developmentfrom
fix/constitutive_model_settable_flux_jacobian

Conversation

@bknight1

Copy link
Copy Markdown
Member

Summary

Makes Constitutive_Model.flux_jacobian settable (was read-only, always returned None).

Motivation

The flux_jacobian property is consumed by _newton_flux in the Cython solver base class (used by both SNES_Scalar and SNES_Stokes_SaddlePt) when consistent_jacobian=True. Despite this, the base class had no way to store a custom expression — subclasses had to override the property, and GenericFluxModel (the most common flux model) couldn't provide one at all.

Use cases

  1. Multicomponent diffusion (composition-dependent D_ij coefficients): The exact flux wrapped in UWexpressions becomes opaque to SymPy diff, giving G3 = 0 (no stiffness matrix). A constant-coefficient flux_jacobian restores the SPD stiffness term, dropping Newton iterations from ~50 to ~3.

  2. 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

  • Added self._flux_jacobian = None to __init__
  • Getter returns self._flux_jacobian (default None — identical behaviour)
  • New setter stores the expression

Default remains None. Zero impact on existing models.

@bknight1 bknight1 requested a review from lmoresi as a code owner July 14, 2026 06:55
Copilot AI review requested due to automatic review settings July 14, 2026 06:55
@bknight1 bknight1 marked this pull request as draft July 14, 2026 06:55

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 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_jacobian backing attribute initialized to None in Constitutive_Model.__init__.
  • Updated flux_jacobian getter to return the backing value instead of always returning None.
  • Added a flux_jacobian setter 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.
@bknight1 bknight1 force-pushed the fix/constitutive_model_settable_flux_jacobian branch from 7890acd to 1046c9e Compare July 14, 2026 07:02
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