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] Added a helper function that converts an angle in degrees to one in radians #11112

Merged
merged 1 commit into from
May 30, 2023

Conversation

avdg81
Copy link
Contributor

@avdg81 avdg81 commented May 11, 2023

📝 Description
Added a helper function that converts an angle in degrees to one in radians. With this function in place, the hard-coded conversions have been replaced by calls to this function.

🆕 Changelog

  • Introduced nonmember function DegreesToRadians.
  • Replaced the hard-coded angle conversions in the GeoMechanicsApplication by calling this new function.

@avdg81 avdg81 added the GeoMechanics Issues related to the GeoMechanicsApplication label May 11, 2023
@avdg81 avdg81 requested review from mcgicjn2 and huhaas May 11, 2023 12:47
Copy link
Member

@philbucher philbucher left a comment

Choose a reason for hiding this comment

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

would be cool to add this to the math_utils in the Core


double DegreesToRadians(double AngleInDegrees)
{
return (AngleInDegrees * M_PI) / 180.0;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return (AngleInDegrees * M_PI) / 180.0;
return (AngleInDegrees * Globals::Pi) / 180.0;

namespace Kratos
{

double DegreesToRadians(double AngleInDegrees)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
double DegreesToRadians(double AngleInDegrees)
double DegreesToRadians(const double AngleInDegrees)

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'm not sure whether this actually improves the code. I have checked the C++ Core Guidelines and there they explicitly mention this situation as an exception to the rule that objects should be immutable whenever possible. From section Con 1: By default, make objects immutable it says (under the heading Exception):

Function parameters passed by value are rarely mutated, but also rarely declared const. To avoid confusion and lots of false positives, don’t enforce this rule for function parameters.

Also the accompanying example illustrates this:

void g(const int i) { ... }  // pedantic

Since I don't see a good reason to deviate from that recommendation here, I'd like to keep it the way it is. (Unless you have a convincing reason why we should make it immutable here.) Thanks.

@@ -1236,5 +1236,8 @@ class StructuralMechanicsMathUtilities

private:
};// class StructuralMechanicsMathUtilities

KRATOS_API(GEO_MECHANICS_APPLICATION) double DegreesToRadians(double AngleInDegrees);
Copy link
Member

Choose a reason for hiding this comment

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

You're declaring the function at the global Kratos namespace. Though this is technically possible, in Kratos we always prefer the static but inside a class approach. As suggested by @philbucher, I'd place this within the Kratos core math utils.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I will move the function to the Kratos core math utils as suggested by @philbucher and you. However, what is the rationale behind using a static member function rather than a nonmember function? If you're trying to limit its scope (which is a fair point), I feel that using a nested namespace is a simpler and cleaner way to achieve just that.
I will follow your suggestion anyway, but I'm just asking you to think about this one and to consider whether we may want to change this policy. Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, my point is to basically limit its scope. About how to achieve it, i.e. the class vs namespace, this is a long term discussion in Kratos. At some point we tried to provide some guidelines & conclusions in #7434.

@avdg81
Copy link
Contributor Author

avdg81 commented May 12, 2023

would be cool to add this to the math_utils in the Core

I have looked into that, but it's not as straightforward as it may seem at first sight. Let me explain. MathUtils is actually a template class, which means it needs a template argument (TDataType) in order to be instantiated. Surely we wouldn't want the return type to be of this type, since undesired things may happen. For instance, consider TDataType to be set to int, it would simply truncate the returned value (which almost always will be a floating point number). That's probably not what you want. So a fixed return type of double seems more logical. You might argue that the input parameter could be of type TDataType, but in the end it needs to be converted to a double anyway. So, if we would express that explicitly (which is how my current function signature looks like), then the template parameter is not used in any way. But if you want to call the function, you'd still have to supply a template argument, which opens the door to silly code constructs like MathUtils<std::string>::DegreesToRadians(90);.

The whole problem here stems from the fact that the template parameter is actually redundant for this particular case. To me, whichever way we go here, it will never be really elegant in my opinion (please correct me if I have overlooked a good option). Therefore, I'm inclined to keep it the way it is now and leave it up to you to move it to a more general location (say somewhere in Kratos core) in a way that you feel best fits the way Kratos does things. Any feedback is greatly appreciated. Thanks.

@philbucher
Copy link
Member

sounds reasonable

@rubenzorrilla @loumalouomega maybe it is time to drop the template arg for the math utils? I don't think any type other than double is used

@loumalouomega
Copy link
Member

sounds reasonable

@rubenzorrilla @loumalouomega maybe it is time to drop the template arg for the math utils? I don't think any type other than double is used

I started to remove it long time ago in a branch, I can retake it

@rubenzorrilla
Copy link
Member

sounds reasonable
@rubenzorrilla @loumalouomega maybe it is time to drop the template arg for the math utils? I don't think any type other than double is used

I started to remove it long time ago in a branch, I can retake it

sounds reasonable

@rubenzorrilla @loumalouomega maybe it is time to drop the template arg for the math utils? I don't think any type other than double is used

I was about to suggest this.

@avdg81
Copy link
Contributor Author

avdg81 commented May 15, 2023

sounds reasonable
@rubenzorrilla @loumalouomega maybe it is time to drop the template arg for the math utils? I don't think any type other than double is used

I started to remove it long time ago in a branch, I can retake it

Also sounds good to me. If I can contribute in any way to get this change in, I'm more than happy to help. Thanks.

@loumalouomega
Copy link
Member

sounds reasonable
@rubenzorrilla @loumalouomega maybe it is time to drop the template arg for the math utils? I don't think any type other than double is used

I started to remove it long time ago in a branch, I can retake it

Also sounds good to me. If I can contribute in any way to get this change in, I'm more than happy to help. Thanks.

It is already done, as I said, I had the changes in a local branch and I only needed to do some clean up and push.

@avdg81 avdg81 force-pushed the geo/10946-deg-to-rad-conversion branch from 291a7a3 to 2a69cd4 Compare May 26, 2023 15:43
@avdg81 avdg81 requested a review from a team as a code owner May 26, 2023 15:43
Copy link
Member

@philbucher philbucher left a comment

Choose a reason for hiding this comment

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

thanks!

philbucher
philbucher previously approved these changes May 26, 2023
Other changes include:
- Replaced `M_PI` by `Globals::Pi`.
- Use the `std` variant of functions `sin` and `cos`.
@avdg81 avdg81 force-pushed the geo/10946-deg-to-rad-conversion branch from 2a69cd4 to d79d904 Compare May 30, 2023 07:49
@avdg81 avdg81 requested a review from philbucher May 30, 2023 07:50
Copy link
Member

@philbucher philbucher left a comment

Choose a reason for hiding this comment

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

Nice!

@avdg81 avdg81 removed the request for review from mcgicjn2 May 30, 2023 09:36
@huhaas huhaas merged commit 3bc6696 into master May 30, 2023
@huhaas huhaas deleted the geo/10946-deg-to-rad-conversion branch May 30, 2023 12:32
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] Add utility function for Radians → Degrees and Degrees → Radians conversions.
7 participants