Skip to content

[Core] Making Variable and VariableData constexpr#14260

Open
sunethwarna wants to merge 15 commits intomasterfrom
core/make_variable_constexpr
Open

[Core] Making Variable and VariableData constexpr#14260
sunethwarna wants to merge 15 commits intomasterfrom
core/make_variable_constexpr

Conversation

@sunethwarna
Copy link
Member

@sunethwarna sunethwarna commented Mar 6, 2026

📝 Description
As in the title, this PR tries to make the Variable and VariableData constexpr. One advantage is that we can now use switch jump table optimizations with variables.

This makes Variable::mZero static, which, anyway, was always default-initialized. No custom zero definitions were found anywhere in Kratos [ Check #14253 ].

Closes #14253

Eg:

const auto& func_variable = [](const Variable<double>& rVariable) -> int {
switch (rVariable.Key()) {
case PRESSURE.Key(): return 1;
case DENSITY.Key(): return 2;
default: return 3;
}
};

or
const auto& func_variable_data = [](const VariableData& rVariableData) -> int {
switch (rVariableData.Key()) {
case VELOCITY.Key(): return 1;
case ACCELERATION.Key(): return 2;
default: return 3;
}
};

Lets see whether what problems the CI gives.

🆕 Changelog

  • Made all the uses of Variable and VariableData to be const correct.
  • Made Variable::mZero static and default initialized to zero.
  • Made the Variable and VariableData constexpr
  • Added a unit test with the switch

@roigcarlo
Copy link
Member

Took a first look and unless I missed something this looks fine. I focused on checking that moving from external to static inline kept the functionality unchanged. This also improves the safety of the resulting code as it delegates the responsibility of merging the definitions of the variable to the compiler, and not to the runtime.

gj @sunethwarna !!

@sunethwarna sunethwarna marked this pull request as ready for review March 6, 2026 09:59
@sunethwarna sunethwarna requested review from a team as code owners March 6, 2026 09:59
Copy link
Member

@loumalouomega loumalouomega left a comment

Choose a reason for hiding this comment

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

LGTM

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.

On behalf of @KratosMultiphysics/geomechanics, we're okay with the changes proposed for the GeoMechanicsApplication 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 👀 Next meeting TODO

Development

Successfully merging this pull request may close these issues.

4 participants