Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GeoMechanicsApplication] Extract B matrix calculations #12337

Merged
merged 16 commits into from
May 6, 2024

Conversation

rfaasse
Copy link
Contributor

@rfaasse rfaasse commented May 2, 2024

📝 Description
In this PR we extract the calculation of the b-matrices for all integration points. This is done by:

  1. Creating a CalculateBMatrices function, which calculates B for all integration points at once.
  2. The calculation of the B matrix is removed from the CalculateKinematics function to avoid duplicate work.
  3. Everywhere CalculateKinematics was called, a call to CalculateBMatrices is done before the loop, and in the loop Variables.B is set with the corresponding matrix in the list.
  4. In a few places (see the following commit) the calculation of B was redundant, i.e. for the calculation of the outputs DEGREE_OF_SATURATION, EFFECTIVE_SATURATION, BISHOP_COEFFICIENT, DERIVATIVE_OF_SATURATION, RELATIVE_PERMEABILITY (since they are only related to pressure), GREEN_LAGRANGE_STRAIN_TENSOR/GREEN_LAGRANGE_STRAIN_VECTOR (because these use the deformation gradient instead of B) and the calculation of the mass matrix.

@rfaasse rfaasse self-assigned this May 2, 2024
@rfaasse rfaasse added GeoMechanics Issues related to the GeoMechanicsApplication Refactor When code is moved or rewrote keeping the same behavior labels May 2, 2024
@rfaasse rfaasse requested review from WPK4FEM, markelov208 and avdg81 May 2, 2024 09:57
@rfaasse rfaasse marked this pull request as ready for review May 2, 2024 10:01
Copy link
Contributor

@markelov208 markelov208 left a comment

Choose a reason for hiding this comment

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

Hi Richard, a very dedicated PR that is very easy to review. Just a small comment on possible improvement.

Copy link
Contributor

@avdg81 avdg81 left a comment

Choose a reason for hiding this comment

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

This is a very clear and clean PR. In essence, you apply the same transformation in several locations. I have checked the usage of ElementVariables::B and as far as I could see you have modified the correct usages (i.e. where this member wasn't used in the first place, no additional changes are required).

{
rB = this->GetStressStatePolicy().CalculateBMatrix(GradNpT, Np, this->GetGeometry());
}

template <unsigned int TDim, unsigned int TNumNodes>
std::vector<Matrix> UPwSmallStrainElement<TDim, TNumNodes>::CalculateBMatrices(
const Matrix& NContainer, const GeometryType::ShapeFunctionsGradientsType& DN_DXContainer) const
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to follow the Kratos Style Guide:

Suggested change
const Matrix& NContainer, const GeometryType::ShapeFunctionsGradientsType& DN_DXContainer) const
const Matrix& rNContainer, const GeometryType::ShapeFunctionsGradientsType& rDN_DXContainer) const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 1248 to 1250
Matrix b_matrix;
this->CalculateBMatrix(b_matrix, DN_DXContainer[GPoint], row(NContainer, GPoint));
result.push_back(b_matrix);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to let CalculateBMatrix return a Matrix object (rather than void). In that case, you can simplify the code as follows:

Suggested change
Matrix b_matrix;
this->CalculateBMatrix(b_matrix, DN_DXContainer[GPoint], row(NContainer, GPoint));
result.push_back(b_matrix);
result.push_back(this->CalculateBMatrix(DN_DXContainer[GPoint], row(NContainer, GPoint)));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (every reviewer suggested the same, such harmony 😄 )

Comment on lines 233 to 235
void CalculateBMatrix(Matrix& rB, const Matrix& GradNpT, const Vector& Np) const;
std::vector<Matrix> CalculateBMatrices(const Matrix& NContainer,
const GeometryType::ShapeFunctionsGradientsType& DN_DXContainer) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

The ordering of the function parameters for the shape functions (Np and NContainer) and the gradients of the shape functions (GradNpT and DN_DXContainer) is not consistent between CalculateBMatrix and CalculateBMatrices. Perhaps we should stick to the ordering adopted by CalculateBMatrix, since that order is consistent with the one adopted by StressStatePolicy::CalculateBMatrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, done!

Comment on lines 1723 to 1725
Matrix b_matrix;
this->CalculateBMatrix(b_matrix, DN_DXContainer[GPoint], row(NContainer, GPoint));
result.push_back(b_matrix);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to point out that here we may consider similar changes as proposed before:

Suggested change
Matrix b_matrix;
this->CalculateBMatrix(b_matrix, DN_DXContainer[GPoint], row(NContainer, GPoint));
result.push_back(b_matrix);
result.push_back(this->CalculateBMatrix(DN_DXContainer[GPoint], row(NContainer, GPoint)));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -287,7 +287,9 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) SmallStrainUPwDiffOrderElement : pub
double CalculateBulkModulus(const Matrix& ConstitutiveMatrix) const;
double CalculateBiotCoefficient(const ElementVariables& rVariables, const bool& hasBiotCoefficient) const;

virtual void CalculateBMatrix(Matrix& rB, const Matrix& DNu_DX, const Vector& Np);
virtual void CalculateBMatrix(Matrix& rB, const Matrix& DNu_DX, const Vector& Np) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, the virtual keyword is redundant, since this member is never overridden.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I only removed it in upw small strain, forgot in diff order, good catch!

Copy link
Contributor

@WPK4FEM WPK4FEM left a comment

Choose a reason for hiding this comment

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

Hi Richard,
Tried to go trough the CalculateKinematics places where B is not used afterwards. Could not find mistakes there. See my other remarks. Keeping similar function setup with return values and arguments in the same order would help me read faster.
For the b_matrices you correctly adopt snake_case, but the argument names for & lack the r's and ofter p is used where I see no reason for the p. Obviouly these are existing anomalies that only happen to coincide with the lines you had to adapt.
Regards, Wijtze Pieter

Comment on lines 233 to 235
void CalculateBMatrix(Matrix& rB, const Matrix& GradNpT, const Vector& Np) const;
std::vector<Matrix> CalculateBMatrices(const Matrix& NContainer,
const GeometryType::ShapeFunctionsGradientsType& DN_DXContainer) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably only confusion for me:
the stress state policies have a CalculateBMatrix which return a matrix and take the 3 arguments rGradNpT, rNp and rGeometry
the elements have a CalculateBMatrix with return type void and 3 arguments rB, GradNpT and Np
and a CalculateBMatrices which returns a std::vector<Matrix> and 2 arguments NContainer and DN_DXContainer

The naming and order of the arguments is confusing for me. Please have the same order for GradNpT and Np
GradNpT and the elements of DN_DXContainer are the same thing.
The p I don't understand, we look at the N for displacement here, so Nu or just N

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 1248 to 1250
Matrix b_matrix;
this->CalculateBMatrix(b_matrix, DN_DXContainer[GPoint], row(NContainer, GPoint));
result.push_back(b_matrix);
Copy link
Contributor

Choose a reason for hiding this comment

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

With b_matrix as return value this becomes more readable for me:
result.push_back(this->CalculateBMatrix(DN_DXContainer[GPoint], row(NContainer, GPoint));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@rfaasse rfaasse changed the title Geo/12319 extract b matrix calculations [GeoMechanicsApplication] Extract B matrix calculations May 3, 2024
@rfaasse rfaasse requested review from avdg81, markelov208 and WPK4FEM May 6, 2024 08:59
@rfaasse rfaasse enabled auto-merge (squash) May 6, 2024 09:05
Copy link
Contributor

@WPK4FEM WPK4FEM left a comment

Choose a reason for hiding this comment

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

The remarks about redundant or now incorrect comments can be made far more often than I did here. I think that changing that can be taken up in next PR's, because we are continuing to unravel these functionalities.

@@ -91,6 +92,7 @@ void UPwUpdatedLagrangianElement<TDim, TNumNodes>::CalculateAll(MatrixType& rLef
for (IndexType GPoint = 0; GPoint < IntegrationPoints.size(); ++GPoint) {
// Compute element kinematics B, F, GradNpT ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Now this really has become a "rotting" comment.

@@ -149,10 +149,13 @@ void UPwSmallStrainElement<TDim, TNumNodes>::InitializeSolutionStep(const Proces
// Create general parameters of retention law
RetentionLaw::Parameters RetentionParameters(this->GetProperties(), rCurrentProcessInfo);

const auto b_matrices = CalculateBMatrices(Variables.DN_DXContainer, Variables.NContainer);

// Loop over integration points
for (unsigned int GPoint = 0; GPoint < NumGPoints; ++GPoint) {
// Compute Np, GradNpT, B and StrainVector
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe remove this comment, B is not computed here anymore.

@@ -253,9 +256,12 @@ void UPwSmallStrainElement<TDim, TNumNodes>::InitializeNonLinearIteration(const
ElementVariables Variables;
this->InitializeElementVariables(Variables, rCurrentProcessInfo);

const auto b_matrices = CalculateBMatrices(Variables.DN_DXContainer, Variables.NContainer);

// Loop over integration points
Copy link
Contributor

Choose a reason for hiding this comment

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

what else? Needless repeated comment on integration point loops.

@@ -17,11 +17,11 @@
namespace Kratos
{

Matrix AxisymmetricStressState::CalculateBMatrix(const Matrix& rGradNpT,
const Vector& rNp,
Matrix AxisymmetricStressState::CalculateBMatrix(const Matrix& rDN_DX,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is better, I did not understand the p in the old name GradNpT.
An alternative would have been GradNT ( I guess the T is for transposed ) DX also refers to a spatial gradient.

@@ -1666,9 +1683,6 @@ void SmallStrainUPwDiffOrderElement::CalculateKinematics(ElementVariables& rVari
noalias(rVariables.DNu_DX) = rVariables.DNu_DXContainer[GPoint];
Copy link
Contributor

Choose a reason for hiding this comment

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

This now seems redundant. It was input for the B computation, which now already has taken place.

@@ -157,7 +157,7 @@ void SteadyStatePwElement<TDim, TNumNodes>::CalculateAll(MatrixType& rLef

const auto integration_coefficients =
this->CalculateIntegrationCoefficients(IntegrationPoints, Variables.detJContainer);

Copy link
Contributor

Choose a reason for hiding this comment

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

Unintended addition of spaces?

@rfaasse rfaasse merged commit e0b29dc into master May 6, 2024
11 checks passed
@rfaasse rfaasse deleted the geo/12319-extract-b-matrix-calculations branch May 6, 2024 09:25
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 Refactor When code is moved or rewrote keeping the same behavior
Projects
None yet
4 participants