Skip to content

Conversation

rfaasse
Copy link
Contributor

@rfaasse rfaasse commented Aug 22, 2025

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

@rfaasse rfaasse requested a review from a team as a code owner August 22, 2025 08:43
@rfaasse rfaasse requested a review from indigocoral August 22, 2025 08:47
@rfaasse rfaasse assigned avdg81 and rfaasse and unassigned avdg81 Aug 22, 2025
@rfaasse rfaasse requested a review from avdg81 August 22, 2025 08:48
indigocoral
indigocoral previously approved these changes Aug 22, 2025
Copy link
Contributor

@indigocoral indigocoral 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 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, {}]",
Copy link
Contributor

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?

Copy link
Contributor

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"]

Copy link
Contributor Author

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:

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);
Copy link
Contributor

@indigocoral indigocoral Aug 22, 2025

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})

Copy link
Contributor Author

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)

Copy link
Contributor

Choose a reason for hiding this comment

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

Clear! Thanks!

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.

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);
Copy link
Contributor Author

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, {}]",
Copy link
Contributor Author

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:

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

indigocoral
indigocoral previously approved these changes Aug 22, 2025
Copy link
Contributor

@indigocoral indigocoral left a 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 👍🏼

@rfaasse
Copy link
Contributor Author

rfaasse commented Aug 25, 2025

@KratosMultiphysics/geomechanics Unfortunately, I was a bit too early with this PR: before we can start using std::format, the compilers first need to be updated, as they are not implemented in the currently used GCC 12 and Clang 14. Support starts at GCC 13 and Clang 17, so this PR can be merged only after these upgrades. I'll create an issue to update the compilers.

For reference, see https://en.cppreference.com/w/cpp/20.html

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.

4 participants