-
Notifications
You must be signed in to change notification settings - Fork 269
[GeoMechanicsApplication] Fix std::format code smells #13750
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
base: master
Are you sure you want to change the base?
Conversation
…, which I can't figure out how to combine with std::format)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for preparing this PR to fix some code smells!
My knowledge in this regard is very limited but I tried to look things up and understand a bit. I have two questions.
Overall, looks good to me. Nice cleanup!
<< "value of " << rVariable.Name() << " for property " << rMaterialProperties.Id() | ||
<< " is out of range: " << rMaterialProperties[rVariable] << " is not in [0.0, " | ||
<< (MaxValue ? std::to_string(*MaxValue) + "]" : "->") << std::endl; | ||
<< std::format("value of {} for property {} is out of range: {} is not in [0.0, {}]", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me it seems that now your new message adds a closing bracket like [0.0, ->]
.
That reads better, but I'm not sure if this was intentional or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can not be intentional, the open interval boundary is -> a closed one is "value"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't, my mistake, fixed now.
The weird part is this function is quite heavily tested, but even with the extra bracket, this still passed:
Lines 123 to 126 in 065b656
KRATOS_EXPECT_EXCEPTION_IS_THROWN( | |
[[maybe_unused]] const auto unused = law.Check(properties, element_geometry, process_info), | |
"Error: value of YOUNG_MODULUS for property 3 is out of range: -1 is not in [0.0, ->") | |
properties.SetValue(YOUNG_MODULUS, 1.0); |
I double-checked and it turns out KRATOS_EXPECT_EXCEPTION_IS_THROWN
checks if the message is there as a substring (meaning if there are extra things around a message, the test still passes).
std::string progress = "Calculating head level " + current_head_string + "m (" + | ||
std::to_string(step) + "/" + std::to_string(max_steps) + ")"; | ||
const auto progress = | ||
std::format("Calculating head level {:.8}m ({}/{})", mCurrentHead, step, max_steps); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the std::setprecision(8)
now the same as: std::format(...{:.8}...)
?
I was looking it up a bit, and I saw that if you don't specify it, it does general formatting (so g
).
I guess the intention is not to use fixed-point with exactly 8 digits after the decimal? (so {:.8f}
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, like this it works the same as setting the precision (I did some experimentation and both give the same results). When adding the f
we indeed always get that precision, so 3.2 would become 3.20000000, which is different from the current functionality (also the related unit tests started failing when I tried this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear! Thanks!
…der somehow leads to build issues on ubuntu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Processed the review comments
std::string progress = "Calculating head level " + current_head_string + "m (" + | ||
std::to_string(step) + "/" + std::to_string(max_steps) + ")"; | ||
const auto progress = | ||
std::format("Calculating head level {:.8}m ({}/{})", mCurrentHead, step, max_steps); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, like this it works the same as setting the precision (I did some experimentation and both give the same results). When adding the f
we indeed always get that precision, so 3.2 would become 3.20000000, which is different from the current functionality (also the related unit tests started failing when I tried this)
<< "value of " << rVariable.Name() << " for property " << rMaterialProperties.Id() | ||
<< " is out of range: " << rMaterialProperties[rVariable] << " is not in [0.0, " | ||
<< (MaxValue ? std::to_string(*MaxValue) + "]" : "->") << std::endl; | ||
<< std::format("value of {} for property {} is out of range: {} is not in [0.0, {}]", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't, my mistake, fixed now.
The weird part is this function is quite heavily tested, but even with the extra bracket, this still passed:
Lines 123 to 126 in 065b656
KRATOS_EXPECT_EXCEPTION_IS_THROWN( | |
[[maybe_unused]] const auto unused = law.Check(properties, element_geometry, process_info), | |
"Error: value of YOUNG_MODULUS for property 3 is out of range: -1 is not in [0.0, ->") | |
properties.SetValue(YOUNG_MODULUS, 1.0); |
I double-checked and it turns out KRATOS_EXPECT_EXCEPTION_IS_THROWN
checks if the message is there as a substring (meaning if there are extra things around a message, the test still passes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix! No further comments from my side. Seems good to go 👍🏼
@KratosMultiphysics/geomechanics Unfortunately, I was a bit too early with this PR: before we can start using For reference, see https://en.cppreference.com/w/cpp/20.html |
📝 Description
After the upgrade, static analysis suggests to change a couple of places where string concatenation is done, which can now be improved by using std::format.