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] Format custom constitutive folder using clang format #12130

Merged

Conversation

rfaasse
Copy link
Contributor

@rfaasse rfaasse commented Mar 1, 2024

No description provided.

@rfaasse rfaasse requested a review from mnabideltares March 1, 2024 10:54
Copy link
Contributor

@mnabideltares mnabideltares left a comment

Choose a reason for hiding this comment

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

@rfaasse
Thank you for the nice work on clang formatting.
The format of the code is improved a lot by clang.
I have few preferences in formatting the code. I commented some. However, it is matter of convention, agreement and discussion, which style to be prefered.

Comment on lines +77 to +91
rConstitutiveMatrix(0, 0) =
YieldStress / ((1.0 - DamageThreshold) * CriticalDisplacement) *
((1.0 - mStateVariable) / mStateVariable -
StrainVector[0] * StrainVector[0] /
(CriticalDisplacement * CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable));
rConstitutiveMatrix(1, 1) =
YieldStress / ((1.0 - DamageThreshold) * CriticalDisplacement) *
((1.0 - mStateVariable) / mStateVariable -
StrainVector[1] * StrainVector[1] /
(CriticalDisplacement * CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable));

rConstitutiveMatrix(0, 1) = -YieldStress * StrainVector[0] * StrainVector[1] /
((1.0 - DamageThreshold) * CriticalDisplacement * CriticalDisplacement *
CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable);
rConstitutiveMatrix(1, 0) = rConstitutiveMatrix(0, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

In line 77 and 78: "YieldStress ...." is in the second line
Line 88: "-YieldStress ..." at the same line of "rConstitutiveMatrix(0, 1) ="
There is then inconsistency in formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

clang-format works with penalties, so it could always be a bit inconsistent, because it depends on the situation and there are always trade-offs (penalties can be conflicting, e.g. breaking a line has a certain penalty and going over the character limit has another penalty). However, now we don't have a penalty for breaking after assignment. Let's discuss in the team if we'd like to add it. When adding a (high) penalty for this, most cases will continue on the same line after an assignment.

Comment on lines +106 to +111
rConstitutiveMatrix(0, 0) =
YieldStress / ((1.0 - DamageThreshold) * CriticalDisplacement) *
((1.0 - mStateVariable) / mStateVariable -
StrainVector[0] * StrainVector[0] /
(CriticalDisplacement * CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable));
rConstitutiveMatrix(1, 1) = YoungModulus / (DamageThreshold * CriticalDisplacement);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as previous comment. Inconsistency in format

Comment on lines +116 to +121
rConstitutiveMatrix(0, 1) =
-YieldStress * StrainVector[0] * StrainVector[1] /
((1.0 - DamageThreshold) * CriticalDisplacement * CriticalDisplacement *
CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable) -
std::copysign(1.0, StrainVector[0]) * YoungModulus * FrictionCoefficient /
(DamageThreshold * CriticalDisplacement);
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer to start them from the same column.
for example: line 118 starts from 4 spaces after line 117, but line 117 hasn't priority/hirarchy above line 118.
line 120, std:: hasn't extra priority on line 119

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason of the indent here, is because line 118 and 119 belong together, if that's not there it's hard to see where the parenthesis ends.

Comment on lines +156 to +157
rConstitutiveMatrix(0, 0) = YieldStress / (CriticalDisplacement * mStateVariable) *
(1.0 - mStateVariable) / (1.0 - DamageThreshold);
Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion, it is a good format, and the rest need to follow this formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes and when the lines are not too long/complicated, this is what it will do. When it gets more complicated we'd need to introduce a penalty (see previous comment) for breaking a line after assignment

Comment on lines +199 to +201
rStressVector[indexStress2DInterface::INDEX_2D_INTERFACE_ZZ] =
YoungModulus / (DamageThreshold * CriticalDisplacement) *
StrainVector[indexStress2DInterface::INDEX_2D_INTERFACE_ZZ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer to have line 200 at the same line of 119, to keep consistency with the format of other parts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we make PenaltyBreakAssignment high enough, this will be enforced (at cost of longer lines)

Comment on lines +338 to +340
KRATOS_INFO("Error in loadUMATWindows")
<< "cannot load function umat in the specified UMAT: " << rMaterialProperties[UDSM_NAME]
<< std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as previous

Comment on lines +445 to +448
}
} else {
for (unsigned int i = 0; i < VOIGT_SIZE_3D; i++) {
for (unsigned int j = 0; j < VOIGT_SIZE_3D; j++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

}
else {
...
}

{
if (rThisVariable == NUMBER_OF_UMAT_STATE_VARIABLES) rValue = static_cast<int>(mStateVariablesFinalized.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

if (rThisVariable == NUMBER_OF_UMAT_STATE_VARIABLES) {
rValue = static_cast(mStateVariablesFinalized.size());
}

Comment on lines +706 to +707
if (rThisVariable == NUMBER_OF_UMAT_STATE_VARIABLES)
rValue = static_cast<int>(mStateVariablesFinalized.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

use { }

Comment on lines +730 to 731
} else if ((rThisVariable == CAUCHY_STRESS_VECTOR) && (rValue.size() == mStressVectorFinalized.size())) {
std::copy(rValue.begin(), rValue.end(), mStressVectorFinalized.begin());
Copy link
Contributor

Choose a reason for hiding this comment

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

}
else if ...

Copy link
Contributor Author

@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.

Thank you for the thorough review and extensive comments @mnabideltares! I agree there are still improvement points and inconsistencies, which as discussed in the stand-up, I would like to take up point for point when all code is formatted:

  1. We should discuss in the team if the majority prefers else on the same or next line with respect to the closing '}' of the if-statement
  2. We can experiment and discuss about higher penalties on breaking after assignment operators (i.e. force a line to continue after '='.
  3. We can experiment and discuss the breaking after '<<', which in combination with the KRATOS_ERROR is not always consistent now.
  4. We can discuss how we want to handle indentation when breaking a line (remove it or keep it).

If you agree, I'd propose to tackle these 4 issues separately and merge this PR if there are no other blockers from your perspective.

Comment on lines +109 to -108
if (Options.Is(ConstitutiveLaw::COMPUTE_CONSTITUTIVE_TENSOR)) {
// COMPUTE_CONSTITUTIVE_TENSOR
Matrix& rConstitutiveMatrix = rValues.GetConstitutiveMatrix();

if (EquivalentStrain >= mStateVariable) //Loading
if (EquivalentStrain >= mStateVariable) // Loading
{
this->ComputeConstitutiveMatrixLoading(rConstitutiveMatrix,
rStrainVector,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, the issue here though is the // Loading comment. Because of the comment, clang-format cannot move the "{" to the same line. I would propose to keep it like this for the re-formatting (to not have a too large scope for this PR), but whenever we work on code that has these kinds of comments next to if-statements, we (re)move them

Comment on lines +141 to -147
if (EquivalentStrain >= mStateVariable) // Loading
{
this->ComputeConstitutiveMatrixContactLoading(rConstitutiveMatrix,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same answer as previous, it's because of the comment

Comment on lines +146 to -157
} else // Unloading
{
this->ComputeConstitutiveMatrixContactUnloading(rConstitutiveMatrix,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can discuss this in the team if we want to do the 'else' always on a new line. The "{ " on the same line is again an issue with the // Unloading comment

Comment on lines +77 to +91
rConstitutiveMatrix(0, 0) =
YieldStress / ((1.0 - DamageThreshold) * CriticalDisplacement) *
((1.0 - mStateVariable) / mStateVariable -
StrainVector[0] * StrainVector[0] /
(CriticalDisplacement * CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable));
rConstitutiveMatrix(1, 1) =
YieldStress / ((1.0 - DamageThreshold) * CriticalDisplacement) *
((1.0 - mStateVariable) / mStateVariable -
StrainVector[1] * StrainVector[1] /
(CriticalDisplacement * CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable));

rConstitutiveMatrix(0, 1) = -YieldStress * StrainVector[0] * StrainVector[1] /
((1.0 - DamageThreshold) * CriticalDisplacement * CriticalDisplacement *
CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable);
rConstitutiveMatrix(1, 0) = rConstitutiveMatrix(0, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clang-format works with penalties, so it could always be a bit inconsistent, because it depends on the situation and there are always trade-offs (penalties can be conflicting, e.g. breaking a line has a certain penalty and going over the character limit has another penalty). However, now we don't have a penalty for breaking after assignment. Let's discuss in the team if we'd like to add it. When adding a (high) penalty for this, most cases will continue on the same line after an assignment.

Comment on lines +116 to +121
rConstitutiveMatrix(0, 1) =
-YieldStress * StrainVector[0] * StrainVector[1] /
((1.0 - DamageThreshold) * CriticalDisplacement * CriticalDisplacement *
CriticalDisplacement * mStateVariable * mStateVariable * mStateVariable) -
std::copysign(1.0, StrainVector[0]) * YoungModulus * FrictionCoefficient /
(DamageThreshold * CriticalDisplacement);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason of the indent here, is because line 118 and 119 belong together, if that's not there it's hard to see where the parenthesis ends.

Comment on lines +105 to +108
if (rValue.size() != VoigtSize) rValue.resize(VoigtSize);

rValue[INDEX_2D_INTERFACE_ZZ] = mStressVectorFinalized[INDEX_3D_ZZ];
rValue[INDEX_2D_INTERFACE_XZ] = mStressVectorFinalized[INDEX_3D_XZ];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clang-format does not add {}, so if we would like this we should do it ourselves. In the same way clang-format won't delete one blank line automatically (only if there are > 1, it will bring it back to 1 in our settings), there is still freedom to add these when needed.

I would propose to do these changes when we work in the code and encounter it (to limit the scope of this PR)

Comment on lines +119 to +121
} else if ((rThisVariable == CAUCHY_STRESS_VECTOR) && (rValue.size() == VoigtSize)) {
this->SetInternalStressVector(rValue);
}
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 discuss else/else if on a new line. I personally like to have it on the same to compact the code a bit, but let's see what the rest feels.

Comment on lines -93 to +139
for (unsigned int i = 0; i < VOIGT_SIZE_3D; ++i)
for (unsigned int j = 0; j < VOIGT_SIZE_3D; ++j)
mMatrixD[i][j] = rOther.mMatrixD[i][j];
for (unsigned int i = 0; i < VOIGT_SIZE_3D; ++i)
for (unsigned int j = 0; j < VOIGT_SIZE_3D; ++j)
mMatrixD[i][j] = rOther.mMatrixD[i][j];
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, but as said before, clang-format won't add these automatically

Comment on lines +333 to +334
int iAbort = 0;
auto nSizeProjectDirectory = static_cast<int>(mProjectDirectory.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.

They are for aligning the assignments and variables

Comment on lines +46 to +54
if (!rMaterialProperties.Has(CRITICAL_DISPLACEMENT) || rMaterialProperties[CRITICAL_DISPLACEMENT] <= 0.0)
KRATOS_ERROR << "CRITICAL_DISPLACEMENT has Key zero, is not defined or has an invalid "
"value for property: "
<< rMaterialProperties.Id() << std::endl;

if (!rMaterialProperties.Has(YOUNG_MODULUS) || rMaterialProperties[YOUNG_MODULUS] <= 0.00)
KRATOS_ERROR
<< "YOUNG_MODULUS has Key zero, is not defined or has an invalid value for property"
<< rMaterialProperties.Id() << std::endl;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the KRATOS_ERROR <<, Wijtze Pieter and I tried several settings to make it as readable as possible when formatting the elements. We landed on the current settings due to other inconsistencies that arise when putting a higher or lower penalty on breaking after the first <<. I'd propose to keep it like it is for now and after we did all the formatting, experiment a bit more, keeping in mind the entire geomechanics code base.

@rfaasse
Copy link
Contributor Author

rfaasse commented Mar 6, 2024

@mnabideltares I tried to answer all your (unique) questions in the comments, please check if you agree!

@rfaasse rfaasse enabled auto-merge (squash) March 6, 2024 15:42
@rfaasse rfaasse merged commit 3ff1502 into master Mar 7, 2024
16 of 17 checks passed
@rfaasse rfaasse deleted the geo/11919-format-custom-constitutive-using-clang-format branch March 7, 2024 10:53
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] Format GeoMechanics code base using clang-format
2 participants