Skip to content

fix(examples): make Drucker-Prager triaxial 2D strain embedding vmap-safe#21

Open
kilickursat wants to merge 1 commit into
camlab-ethz:mainfrom
kilickursat:fix/drucker-prager-triaxial-2d-vmap-safe
Open

fix(examples): make Drucker-Prager triaxial 2D strain embedding vmap-safe#21
kilickursat wants to merge 1 commit into
camlab-ethz:mainfrom
kilickursat:fix/drucker-prager-triaxial-2d-vmap-safe

Conversation

@kilickursat

Copy link
Copy Markdown
Contributor

Summary

Makes the 2D (plane-strain) branch of the example-local
DruckerPragerPlasticity._small_strain_3d helper safe for batched inputs and
under vmap.

The helper previously embedded the in-plane strain into a 3x3 tensor with an
in-place slice assignment into a freshly allocated tensor:

eps = torch.zeros((3, 3), ...)
eps[:2, :2] = eps_2d

energy() applies element_energy per quadrature point under vmap, and that
in-place embed raises RuntimeError: vmap: inplace arithmetic ... is not possible when the 2D branch is exercised. The merged triaxial example only runs
in 3D, so this path was never hit, but it blocks reusing the same constitutive
helper for a 2D problem.

Change

  • Replace the in-place embed with an out-of-place torch.nn.functional.pad,
    which is correct for both batched inputs and under vmap.
  • Add a test exercising _small_strain_3d with a batched 2D gradient and under
    torch.vmap.

Validation

Ran locally:

  • pytest tests/assemble/test_drucker_prager_triaxial_example.py -v
  • python examples/solid/geomechanics/drucker_prager_triaxial/drucker_prager_triaxial.py --no-plot --steps 8

Example + test only. No public API changes and no core TensorMesh code changes.

…safe

Signed-off-by: Kursat Kilic <kilic_kursat@hotmail.com>
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.

1 participant