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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GeoMechanicsApplication] Extract a static utility function for the calculation of the Damping Matrix (D) #12368

Merged
merged 7 commits into from May 13, 2024

Conversation

markelov208
Copy link
Contributor

馃摑 Description

  • The calculation of the damping matrix is available as a utility function, which takes the mass and stiffness matrices and the rayleigh coefficients ($\alpha$ and $\beta$)
  • This calculation is defined once and re-used at all locations where this calculation is done in the geomechanics application.
  • The utility function is documented and unit-tested.

@markelov208 markelov208 self-assigned this May 10, 2024
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.

Short and clear.
Only more philosophical questions. Still I would like Richard or Anne to comment on the things I'm blind for.

const Matrix& rMassMatrix,
const Matrix& rStiffnessMatrix)
{
return rMassMatrix * RayleighAlpha + rStiffnessMatrix * RayleighBeta;
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct, but the formula is usually written as: alphaM + betaK
Please do the same here.

Is there a need for checking the sizes of M and K before doing this operation, or do we accept that will be o.k. as those are generated for the same element and therefore have the same and correct size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. Indeed ublas provides both options. Changed it. Yes the same size. In all three cases the matrices are generated just before CalculateDampingMatrix function.

Comment on lines 282 to 283
noalias(rDampingMatrix) = GeoEquationOfMotionUtilities::CalculateDampingMatrix(
rCurrentProcessInfo[RAYLEIGH_ALPHA], rCurrentProcessInfo[RAYLEIGH_BETA], mass_matrix, stiffness_matrix);
Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently, the damping for the "structural" elements within geo have an implementation that differs from the continuum elements. I wonder whether we should make the possibility for a material based alpha and beta possible or to avoid that effort as we hope to let the "structural" elements within geo slowly disappear.

Copy link
Contributor

@rfaasse rfaasse left a comment

Choose a reason for hiding this comment

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

Neat and clear PR! I only have a few minor comments (on the level of some const-correctness and other minor suggestions).

Comment on lines 338 to 346
// Compute Damping Matrix
if (rDampingMatrix.size1() != N_DOF) rDampingMatrix.resize(N_DOF, N_DOF, false);
noalias(rDampingMatrix) = ZeroMatrix(N_DOF, N_DOF);

const PropertiesType& rProp = this->GetProperties();

if (rProp.Has(RAYLEIGH_ALPHA)) noalias(rDampingMatrix) += rProp[RAYLEIGH_ALPHA] * MassMatrix;
else noalias(rDampingMatrix) += rCurrentProcessInfo[RAYLEIGH_ALPHA] * MassMatrix;

if (rProp.Has(RAYLEIGH_BETA)) noalias(rDampingMatrix) += rProp[RAYLEIGH_BETA] * StiffnessMatrix;
else noalias(rDampingMatrix) += rCurrentProcessInfo[RAYLEIGH_BETA] * StiffnessMatrix;
const PropertiesType& r_prop = this->GetProperties();
noalias(rDampingMatrix) = GeoEquationOfMotionUtilities::CalculateDampingMatrix(
r_prop.Has(RAYLEIGH_ALPHA) ? r_prop[RAYLEIGH_ALPHA] : rCurrentProcessInfo[RAYLEIGH_ALPHA],
r_prop.Has(RAYLEIGH_BETA) ? r_prop[RAYLEIGH_BETA] : rCurrentProcessInfo[RAYLEIGH_BETA],
mass_matrix, stiffness_matrix);
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, I think it might be nice to simplify by just assigning rDampingMatrix to GeoEquationOfMotionUtilities::CalculateDampingMatrix without noalias, such that we can remove the resizing and assigning it a zeromatrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's start removing resize and noalias ;) Indeed it is not clear a benefit of noalias nowadays.

@@ -58,4 +58,12 @@ Vector GeoEquationOfMotionUtilities::CalculateDetJsInitialConfiguration(const Ge
return det_Js_initial_configuration;
}

Matrix GeoEquationOfMotionUtilities::CalculateDampingMatrix(double RayleighAlpha,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we ever want to check the inputs for the damping matrix (e.g. in this case I'd expect alpha and beta to be >0). We can also just assume the numbers to be correct, but since they are direct user inputs in this case, it might be a good idea to check them?

This might also be a broader discussion, since we don't do input validation for the jsons now, we might want to do that in a central place and avoid expensive checks for each time we call this function (which is done for each time step and each element of course)

Copy link
Contributor

Choose a reason for hiding this comment

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

Checks should be as early as possible and once, so not here but at element initialization and once per material property/constitutive law.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fully agreed with Wijtze-Pieter.

Comment on lines 28 to 31
auto mass_matrix = scalar_matrix(n, n, mass_matrix_value);

constexpr double stiffness_matrix_value = 20;
auto stiffness_matrix = scalar_matrix(n, n, stiffness_matrix_value);
Copy link
Contributor

Choose a reason for hiding this comment

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

These can be const

Suggested change
auto mass_matrix = scalar_matrix(n, n, mass_matrix_value);
constexpr double stiffness_matrix_value = 20;
auto stiffness_matrix = scalar_matrix(n, n, stiffness_matrix_value);
const auto mass_matrix = scalar_matrix(n, n, mass_matrix_value);
constexpr double stiffness_matrix_value = 20;
const auto stiffness_matrix = scalar_matrix(n, n, stiffness_matrix_value);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

rayleigh_alpha, rayleigh_beta, mass_matrix, stiffness_matrix);

const double expected_matrix_value = rayleigh_alpha * mass_matrix_value + rayleigh_beta * stiffness_matrix_value;
auto expected_damping_matrix = scalar_matrix(n, n, expected_matrix_value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be const

Suggested change
auto expected_damping_matrix = scalar_matrix(n, n, expected_matrix_value);
const auto expected_damping_matrix = scalar_matrix(n, n, expected_matrix_value);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

namespace Kratos::Testing
{

KRATOS_TEST_CASE_IN_SUITE(CalculateDampingMatrixGivesCorrectResults, KratosGeoMechanicsFastSuite)
Copy link
Contributor

Choose a reason for hiding this comment

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

Very clear test!

#include "custom_utilities/equation_of_motion_utilities.h"
#include "includes/checks.h"
#include "testing/testing.h"
#include <boost/numeric/ublas/assignment.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see assignment being used, could we remove this include?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. It is removed.

WPK4FEM
WPK4FEM previously approved these changes May 10, 2024
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.

Comments were taken up. I think this is good to go.

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.

Very clear, clean and concise PR. The suggestions I have are very minor, but hopefully useful. Thanks for the work done, Gennady!

@@ -327,26 +328,19 @@ void UPwBaseElement<TDim, TNumNodes>::CalculateDampingMatrix(MatrixType&
const unsigned int N_DOF = this->GetNumberOfDOF();

// Compute Mass Matrix
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to remove this comment, since it doesn't add anything that the code already tells.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. Indeed the comments just repeat the function names. Removed


this->CalculateMassMatrix(MassMatrix, rCurrentProcessInfo);
MatrixType mass_matrix(N_DOF, N_DOF);
this->CalculateMassMatrix(mass_matrix, rCurrentProcessInfo);

// Compute Stiffness matrix
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to also remove this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed


this->CalculateMaterialStiffnessMatrix(StiffnessMatrix, rCurrentProcessInfo);
MatrixType stiffness_matrix(N_DOF, N_DOF);
this->CalculateMaterialStiffnessMatrix(stiffness_matrix, rCurrentProcessInfo);

// Compute Damping Matrix
Copy link
Contributor

Choose a reason for hiding this comment

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

And the same thing applies to this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for eliminating the duplication; this looks much better. I have the same suggestions about code comments as for the previous file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the comments

Comment on lines +280 to +281
rDampingMatrix = GeoEquationOfMotionUtilities::CalculateDampingMatrix(
rCurrentProcessInfo[RAYLEIGH_ALPHA], rCurrentProcessInfo[RAYLEIGH_BETA], mass_matrix, stiffness_matrix);
Copy link
Contributor

Choose a reason for hiding this comment

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

The thing that struck me here was that apparently for "geo structural" elements the element properties need not be checked for the Rayleigh coefficients (contrary to the U-Pw elements). They are always taken from the "current process info".

Copy link
Contributor

Choose a reason for hiding this comment

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

Same suggestions about the code comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the comments

damping_matrix = GeoEquationOfMotionUtilities::CalculateDampingMatrix(
rayleigh_alpha, rayleigh_beta, mass_matrix, stiffness_matrix);

const double expected_matrix_value = rayleigh_alpha * mass_matrix_value + rayleigh_beta * stiffness_matrix_value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Excellent unit test! The only thing I'm a bit hesitant about is repeating the implementation of the damping matrix computation here. Instead, we could also express the expected matrix like you did in the unit test above this one (see expected_mass_matrix).

Copy link
Contributor

@rfaasse rfaasse May 13, 2024

Choose a reason for hiding this comment

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

Since the expected matrix only has a single value, I would not express the matrix in the same way as expected_mass_matrix. We might want to give this double just the value it should have (which I guess is 15.0) and use expected_damping_matrix = scalar_matrix(n, n, expected_matrix_value) to fill the matrix as we do now. I'm not sure this approach is needed for such a simple calculation though. Not sure what you both think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed with Richard. Now the calculated damping matrix is compared with expected damping matrix only. Each time the expected matrix is filled with a specific single value.

Copy link
Contributor

@rfaasse rfaasse left a comment

Choose a reason for hiding this comment

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

To me this is good to go, thank you for incorporating the comments! There is still the discussion point about repeating functionality in the test, which to me it's non-blocking, but not sure about others.

@markelov208 markelov208 requested a review from avdg81 May 13, 2024 11:11
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.

I also believe this PR is good to go.

@markelov208 markelov208 merged commit 8c20a67 into master May 13, 2024
11 checks passed
@markelov208 markelov208 deleted the geo/12366-extract-utility-damping-matrix branch May 13, 2024 11:43
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.

[GeoMechanicsApplication] Extract a static utility function for the calculation of the Damping Matrix (D)
4 participants