Skip to content

[GeoMechanicsApplication] p q theta - #15006

Open
avdg81 wants to merge 19 commits into
masterfrom
geo/p_q_theta
Open

avdg81 wants to merge 19 commits into
masterfrom
geo/p_q_theta

Conversation

@avdg81

@avdg81 avdg81 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

📝 Description

Extended PQ class to complete P, Q, Theta ( Lode angle ) class

Key changes

  • extension with Theta
  • extended conversions in stress_strain_utilities
  • added unit tests

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.

🟡 Changes recommended

The Lode-angle cutoff corrupts small nonhydrostatic stress round trips, reconstructed stresses can be misordered, and the compression-cap flow gradient lacks its theta component.

3 open issues
What changed in this PR

Extends GeoMechanics stress-state handling from (p, q) to (p, q, theta), carrying the Lode angle through principal-stress conversions and yield-surface evaluation.

Changes:

  • Replaces PQ with the three-component PQTheta type and updates its tests.
  • Adds Lode-angle calculation and inverse conversion to principal stresses.
  • Adds a Coulomb yield-value overload and migrates compression-cap interfaces to PQTheta.
File Description
applications/​GeoMechanicsApplication/​tests/​cpp_tests/​custom_utilities/​test_stress_strain_utilities.cpp Updates the forward-conversion test for theta.
applications/​GeoMechanicsApplication/​tests/​cpp_tests/​custom_constitutive/​test_yield_surface.cpp Migrates compression-cap tests to PQTheta.
applications/​GeoMechanicsApplication/​tests/​cpp_tests/​custom_constitutive/​test_p_q.cpp Removes the old two-component tests.
applications/​GeoMechanicsApplication/​tests/​cpp_tests/​custom_constitutive/​test_p_q_theta.cpp Tests three-component construction, access, and arithmetic.
applications/​GeoMechanicsApplication/​custom_utilities/​stress_strain_utilities.h Declares the PQTheta conversion APIs.
applications/​GeoMechanicsApplication/​custom_utilities/​stress_strain_utilities.cpp Implements conversions including the Lode angle.
applications/​GeoMechanicsApplication/​custom_constitutive/​p_q.cpp Removes the old PQ implementation.
applications/​GeoMechanicsApplication/​custom_constitutive/​p_q_theta.hpp Defines the three-component stress-state interface.
applications/​GeoMechanicsApplication/​custom_constitutive/​p_q_theta.cpp Implements component access and arithmetic.
applications/​GeoMechanicsApplication/​custom_constitutive/​coulomb_yield_surface.h Declares the PQTheta yield-value overload.
applications/​GeoMechanicsApplication/​custom_constitutive/​coulomb_yield_surface.cpp Evaluates PQTheta through principal stresses.
applications/​GeoMechanicsApplication/​custom_constitutive/​compression_cap_yield_surface.h Changes cap interfaces to accept PQTheta.
applications/​GeoMechanicsApplication/​custom_constitutive/​compression_cap_yield_surface.cpp Uses PQTheta in cap calculations.

Review effort: Balanced

@avdg81 avdg81 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Wijtze Pieter,
I've reviewed the extension of class PQ to PQTheta. In addition, I've used Copilot to attempt to find edge cases and other things that we didn't think of. Can you please see to what extent you agree/disagree with the comments/suggestions? After that, we can see how to make this draft PR ready to be merged into master, and how I can contribute to that. Thank you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, feel free to add your name to the list of authors, since you've extended this class. 👍

Copilot rightly indicates that we should also #include <iterator>, since we're using std::begin, std::end, and std::distance.

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.

added author and include.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, feel free to add your name to the list of authors.

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.

added author

Comment on lines +52 to +53
[[nodiscard]] double Theta() const noexcept;
double& Theta() noexcept;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether we should add a comment here that says Theta equals Lode angle. Also, would it be wise to make explicit that the returned angle is in radians?

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.

gave the constructor that accepts 3 values a comment explaining p, q, theta.
Is that sufficient?

// with UBlas vector expressions. Therefore, we're using the iterator-style algorithms.
auto first = std::begin(rValues);
auto last = std::end(rValues);
KRATOS_DEBUG_ERROR_IF(std::distance(first, last) != msVectorSize)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a reminder to myself (found by Copilot): std::distance returns a signed value and msVectorSize is unsigned, so the comparison produces a sign-compare warning.

Comment on lines 55 to 61
@@ -58,9 +60,9 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) PQ
return result;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet another useful remark by Copilot: CopyTo<VectorType> requires VectorType(size_t) and begin(). That works for Vector but not for array_1d or BoundedVector (the latter would break on the size argument). Constrain it with a concept, or drop it in favour of Values() plus a conversion.

}

template <typename T>
class TestPQFixture : public ::testing::Test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking:

Suggested change
class TestPQFixture : public ::testing::Test
class TestPQThetaFixture : public ::testing::Test

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.

done

{
};

using VectorTypesForTypedPQTest = ::testing::Types<Vector, BoundedVector<double, 3>, std::vector<double>>;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More nitpicking:

Suggested change
using VectorTypesForTypedPQTest = ::testing::Types<Vector, BoundedVector<double, 3>, std::vector<double>>;
using VectorTypesForTypedPQThetaTest = ::testing::Types<Vector, BoundedVector<double, 3>, std::vector<double>>;

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.

done.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also requested Copilot to review these unit tests. I've instructed it to focus on functionality in the PQTheta class that is not properly covered. This was its response:

The suite covers nearly all of the class. A few behaviours are untested, and some tests are weaker than they look.

Coverage
Tested: default construction, construction from three values, construction from a vector (Vector, BoundedVector, std::vector), construction from a ublas expression, the debug-only size check, mutable accessors, CopyTo, += and +.

Gaps

  1. Aliasing and independence in +=.
    • stress_state += stress_state is untested. It works with std::ranges::transform, but it is the classic edge case.
    • Add a check that the right-hand operand is unchanged after +=.
    • Add a check that operator+ doesn't mutate its operands. Both are passed so that Lhs is a copy, so a regression to a reference parameter would go unnoticed.
  2. Const accessors are barely distinguished.
    • Most P(), Q() and Theta() reads go through temporaries or const auto objects, which does exercise the const overloads.
    • PQTheta_ComponentsCanBeModifiedDirectly checks the mutators via Values() only.
    • Add a check that the other components are untouched. The current asserts do cover that implicitly, so this one is minor.
  3. No test that the Values() order matches P, Q, Theta. The 3-value constructor test checks the accessors but not Values(). The vector constructor test checks both, so ordering is covered. This one is minor.
  4. Copy and move semantics. Copying and assigning a PQTheta is untested. They are defaulted, so low value.
  5. The release-build gap is real.
    • The size-mismatch test is skipped in non-debug builds.
    • The release behaviour for a vector that is too long is a buffer overflow (the unchecked std::copy), and for one that is too short it leaves the remaining values at zero.
    • Neither is tested. If you switch to KRATOS_ERROR_IF as I suggested earlier, the test can run in both modes.
  6. CopyTo with a type it can't support. Only Vector, BoundedVector and std::vector are tested. BoundedVector<double,3>(3) works here, but array_1d would not compile. That is fine, since the test documents the supported set.
  7. No negative or extreme values. Every value is positive. Add a negative-value case (stresses are often negative) to make sure nothing is clamped or abs'd.

Test quality

• The test names are good and the Arrange/Act/Assert structure is consistent.
• PQTheta_HasZeroesAsValuesWhenDefaultConstructed constructs three temporaries. A single named object would be cleaner.
• The expression test's comment is excellent, since it documents why the test exists.
• CanBeConstructedFromFromThreeValues has a typo ("FromFrom").
• The Exception type in the debug test is the generic one. It would be stronger to check the message, e.g. with KRATOS_EXPECT_EXCEPTION_IS_THROWN, which would also verify the "expected 3 values" text.

If I had to add only two tests: self-addition (x += x) and operand-preservation for operator+, then a negative-value case.

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.

So far I only fixed the FromFrom repetition, so there quite something left todo.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add at least one test that covers the calculation of the yield function value given a $(p, q, \theta)$ stress state, since an overload for it has been added. If you agree that we should extend the Coulomb yield surface with an overload for the calculation of the derivative of flow function given a $(p, q, \theta)$ stress state, then we'll also need a unit test for that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've asked Copilot to check for any gaps in the test coverage of functions TransformPrincipalStressesToPQandTheta and TransformPQThetaToPrincipalStresses.
This was its response:

Coverage in test_stress_strain_utilities.cpp is thin. It has one test, for the forward direction only.

What exists
CheckTransformPrincipalStressesToPQandTheta uses σ = (30, 20, 10) and expects p = 20, q = √300, θ = 0. That is a single, symmetric, positive-stress case.

TransformPQThetaToPrincipalStresses has no direct test

• It is only exercised indirectly through CoulombYieldSurface (coulomb_yield_surface.cpp:145).
• If that indirect path is covered, only the yield function value is checked, and the individual principal stresses are not.
• A mistake in the phase shifts or amplitude could slip through if it happened not to affect the tested yield values.

Gaps in the forward test (σ → p, q, θ)

  1. θ ≠ 0 is never checked here. Add these cases:
    • Triaxial compression (θ = +30° for σ1 > σ2 = σ3).
    • Triaxial extension (θ = −30° for σ1 = σ2 > σ3).
    • An intermediate angle, e.g. θ = 15°.
    • The sign of θ is the most error-prone part.
  2. Hydrostatic state, e.g. (10, 10, 10). The expected result is q = 0 and θ = 0 via the q < 1e-12 branch, and CalculateLodeAngle has this special case.
  3. Negative principal stresses, which are the realistic case in this application. The test has only positive values.
  4. Unsorted input. Check what happens when the order is not descending. The current behaviour is undocumented.
  5. Clamping near ±30°. The asin argument can exceed ±1 through round-off. A test at exactly θ = ±30° would exercise the clamp, because the argument is ≈ ±1.

Gaps in the reverse test (p, q, θ → σ)
This direction needs a test written from scratch:

• Pure hydrostatic: q = 0 gives σ = (p, p, p).
• θ = 0: σ = (p + (√3/3)·q, p, p − (√3/3)·q). For p = 20 and q = √300 that gives (30, 20, 10), the mirror image of the existing forward test.
• θ = ±30° (compression and extension).
• A negative p.
• Ordering of the result: σ1 ≥ σ2 ≥ σ3 for θ ∈ [−30°, 30°].

Round trip
Add a parameterised test checking Reverse(Forward(σ)) == σ for several sorted stress states, including negative values and the ±30° boundaries. It guards the consistency of the two conventions, which I verified analytically but which isn't pinned down by any test.

- formatting
- Adapt unit tests
- remove empty statement
- sort principals before constructing a principal stress state from PQTheta
@WPK4FEM
WPK4FEM requested a balanced review from Copilot November 25, 2026 15:15

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.

Comment on lines +72 to +85
if (q > 0.0) {
Matrix sigma_princi;
Matrix eigen_vectors;
MathUtils<>::GaussSeidelEigenSystem(MathUtils<>::StressVectorToTensor(rStressVector),
eigen_vectors, sigma_princi, 1.0e-16, 20);
const auto p = CalculateMeanStress(rStressVector);
const auto numerator =
(sigma_princi(0, 0) - p) * (sigma_princi(1, 1) - p) * (sigma_princi(2, 2) - p);
// Avoid a domain error when computing the arc sine (which results in a NaN value)
const auto arg_to_asin = std::clamp((-27. / 2.) * numerator / std::pow(q, 3), -1.0, 1.0);

return std::asin(arg_to_asin) / 3.0;
}
return 0.0;

PQTheta& PQTheta::operator+=(const PQTheta& rRhs)
{
std::ranges::transform(mValues, rRhs.mValues, mValues.begin(), std::plus{});

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.

Setup was changed to avoid meaningless states.

Comment on lines +250 to 260
Geo::PrincipalStresses StressStrainUtilities::TransformPQThetaToPrincipalStresses(const Geo::PQTheta& rPQTheta)
{
auto principal_stresses = Geo::PrincipalStresses{
rPQTheta.P() + (2.0 / 3.0) * rPQTheta.Q() * std::sin(rPQTheta.Theta() + (2.0 / 3.0) * Globals::Pi),
rPQTheta.P() + (2.0 / 3.0) * rPQTheta.Q() * std::sin(rPQTheta.Theta()),
rPQTheta.P() + (2.0 / 3.0) * rPQTheta.Q() * std::sin(rPQTheta.Theta() - (2.0 / 3.0) * Globals::Pi)};
auto& principal_stress_values = principal_stresses.Values();
std::sort(principal_stress_values.begin(), principal_stress_values.end(),
[](auto lhs, auto rhs) { return rhs < lhs; });
return principal_stresses;
}

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.

Added series of round trip tests.

@WPK4FEM WPK4FEM added the GeoMechanics Issues related to the GeoMechanicsApplication label Nov 27, 2026
@WPK4FEM
WPK4FEM marked this pull request as ready for review November 27, 2026 15:36
@WPK4FEM
WPK4FEM requested a review from a team as a code owner November 27, 2026 15:36

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.

🟡 Changes recommended

The revised angle calculation loses accuracy near repeated principal stresses and produces NaN for tiny stress values when cubic terms underflow.

4 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Avoid underflow in the deviatoric cubic term

applications/​GeoMechanicsApplication/​custom_utilities/​stress_strain_utilities.cpp:97

Replacing the small-q guard with q > 0 allows the diagonal stress vector {4e-109, 1e-109, 1e-109, 0} into this branch, where both the numerator and q³ underflow to zero. The resulting 0/0 produces NaN, which std::clamp cannot repair; the previous guard returned a finite angle. Normalize each deviatoric stress by q before multiplying to avoid dividing underflowed cubic quantities.

6 resolved since last review

Review effort: Balanced

Comment on lines +106 to +115
const auto q = CalculateVonMisesStress(rPrincipalStresses);
if (q > 0.0) {
const auto p = CalculateMeanStress(rPrincipalStresses);
const auto numerator = (rPrincipalStresses.Values()[0] - p) *
(rPrincipalStresses.Values()[1] - p) * (rPrincipalStresses.Values()[2] - p);
// Avoid a domain error when computing the arc sine (which results in a NaN value)
const auto arg_to_asin = std::clamp((-27. / 2.) * numerator / std::pow(q, 3), -1.0, 1.0);

return std::asin(arg_to_asin) / 3.0;
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GeoMechanics Issues related to the GeoMechanicsApplication

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GeoMechanicsApplication] Extend class PQ to include Lode angle (i.e. make it PQTheta)

3 participants