From fb3628c3c578155bdd6a797e0611bad308193795 Mon Sep 17 00:00:00 2001 From: Leon Date: Tue, 17 Sep 2024 12:16:25 +1000 Subject: [PATCH] fix: Avoid zero-division under uniaxial compression (#217) This fixes a zero-division for some orientations under uniaxial compression, by catching the problematic values for slip invariants and returning null-values for orientation change and grain growth. There are some conditions where slip is simply not possible at all, and it is worth adding this check even though most Real World Problems are unlikely to hit this issue (strain rates would normally have pure shear, simple shear and compressional components combined). --- CHANGELOG.md | 1 + src/pydrex/core.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32032b32..b19402c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Handling of enstatite in Voigt averaging - Handling of optional keyword args in some visualisation functions +- Zero-division error for some grain orientations under uniaxial compression ### Removed - Access to `io` symbols in global `pydrex` namespace (use `pydrex.io` instead) diff --git a/src/pydrex/core.py b/src/pydrex/core.py index 9c8831b0..580e81f2 100644 --- a/src/pydrex/core.py +++ b/src/pydrex/core.py @@ -714,6 +714,12 @@ def _get_rotation_and_strain( """ crss = get_crss(phase, fabric) slip_invariants = _get_slip_invariants(strain_rate, orientation) + # Handle the case where all slip invariants are zero, + # thus no slip is possible (occurs for a few specific orientations and strain rates, + # for example in the case of uniaxial compression and an identity orientation + # i.e. grain orientation aligned to the coordinate system). + if np.all(slip_invariants == 0): + return np.zeros((3, 3)), 0.0 if phase == MineralPhase.olivine: slip_indices = np.argsort(np.abs(slip_invariants / crss)) slip_rates = _get_slip_rates_olivine(