[Core] Making Variable and VariableData constexpr#14260
[Core] Making Variable and VariableData constexpr#14260sunethwarna wants to merge 30 commits intomasterfrom
Conversation
|
Took a first look and unless I missed something this looks fine. I focused on checking that moving from gj @sunethwarna !! |
avdg81
left a comment
There was a problem hiding this comment.
On behalf of @KratosMultiphysics/geomechanics, we're okay with the changes proposed for the GeoMechanicsApplication 👍
RiccardoRossi
left a comment
There was a problem hiding this comment.
@pooyan-dadvand is taking this over for a detailed review and approval
| const int neigh_color = *(neighbor_colors_iterator++); | ||
| array_1d<double,3> dist = cell_center - rConditionCenter; | ||
| double dist_modulus = norm_2(dist); | ||
| array_1d<double,3> unit_dist; |
There was a problem hiding this comment.
why this change is needed?
There was a problem hiding this comment.
It is because, this variable may be uninitialized because of the following if condition.
There was a problem hiding this comment.
related, since this PR is triggering the warning.... [For some reason it was not triggered earlier, but the warning makes sense]
There was a problem hiding this comment.
related, since this PR is triggering the warning.... [For some reason it was not triggered earlier, but the warning makes sense]
| const VariableType* pTimeDerivativeVariable = nullptr | ||
| ) | ||
| : VariableData(NewName, sizeof(TDataType)), | ||
| mZero(Zero), |
There was a problem hiding this comment.
the problem is that ublas does not allow constexpr assignement?
There was a problem hiding this comment.
Not for the types which are dynamic.... Like Vector and Matrix.
| // Application includes | ||
| #include "includes/variables.h" | ||
| #include "includes/kratos_flags.h" | ||
| #include "includes/cfd_variables.h" |
There was a problem hiding this comment.
Yes, the FACTIONAL_STEP variable was defined both in variables.h and cfd_variables.h... since we now doing inline constexpr, there can only be one definition in one TU. So I removed the definition of the variable from variables.h. so now, this element uses it, so I had to include it.
There was a problem hiding this comment.
I think this is an advantage! Nevertheless, this leads me to a very important question:
What about having the same variable in two different application?
There was a problem hiding this comment.
This is the nice thing. If the variable is in the same TU, then, it will have the same memory location, key (given the name is the same) and the name. But if they are in different TU, they will have different memory location but with the same key (as long as the name is the same) and the key same... So, now no problems in redefining the variable in multiple applications or TUs.... :)
This is the reason why i put in the pybind the py::eq and __hash__ function definitions. earlier it was using the memory locations to check for equality and hashing in python, now it uses the var key. Which i think is better and proper.
pooyan-dadvand
left a comment
There was a problem hiding this comment.
While I support the idea behind this PR, I am not sure how it affects the variables defined in different apps. Is it possible to add test for that?
| KRATOS_DEFINE_APPLICATION_VARIABLE( MPM_APPLICATION, double, MP_KINETIC_ENERGY ) | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE( MPM_APPLICATION, double, MP_STRAIN_ENERGY ) | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE( MPM_APPLICATION, double, MP_TOTAL_ENERGY ) | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE( MPM_APPLICATION, double, MP_TEMPERATURE) |
There was a problem hiding this comment.
Is this related to this PR?
There was a problem hiding this comment.
yes, this was a redeclaration of a variable defined in line 45
| KRATOS_DEFINE_APPLICATION_VARIABLE(OPTIMIZATION_APPLICATION, double, D_MAX_STRESS_D_PT); | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE(OPTIMIZATION_APPLICATION, double, D_MAX_STRESS_D_CT); | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE(OPTIMIZATION_APPLICATION, double, D_MAX_STRESS_D_PD); | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE(OPTIMIZATION_APPLICATION, double, D_MAX_STRESS_D_CD); |
There was a problem hiding this comment.
Is this related to this PR?
There was a problem hiding this comment.
ok, a short summary
KRATOS_DEFINE_APPLICATION_VARIABLE used to declare a variable. Now it actually defines one.
What you're seeing here are all variables that have duplicate declarations, which have now become duplicate definitions, which is of course an error.
|
|
||
| KRATOS_CREATE_VARIABLE_WITH_TIME_DERIVATIVE(double, TURBULENT_ENERGY_DISSIPATION_RATE_2, RANS_AUXILIARY_VARIABLE_2) | ||
| KRATOS_CREATE_VARIABLE_WITH_TIME_DERIVATIVE(double, TURBULENT_ENERGY_DISSIPATION_RATE, TURBULENT_ENERGY_DISSIPATION_RATE_2) | ||
|
|
There was a problem hiding this comment.
explained in the above comment.
| // k-omega turbulence modelling specific additional variables | ||
| KRATOS_CREATE_VARIABLE_WITH_TIME_DERIVATIVE(double, TURBULENT_SPECIFIC_ENERGY_DISSIPATION_RATE_2, RANS_AUXILIARY_VARIABLE_2) | ||
| KRATOS_CREATE_VARIABLE_WITH_TIME_DERIVATIVE(double, TURBULENT_SPECIFIC_ENERGY_DISSIPATION_RATE, TURBULENT_SPECIFIC_ENERGY_DISSIPATION_RATE_2) | ||
|
|
There was a problem hiding this comment.
explained in the above comment
| KRATOS_DEFINE_APPLICATION_VARIABLE( RANS_APPLICATION, double, TURBULENT_KINETIC_ENERGY ) | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE( RANS_APPLICATION, double, TURBULENT_ENERGY_DISSIPATION_RATE ) | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE( RANS_APPLICATION, double, TURBULENT_KINETIC_ENERGY_RATE ) | ||
| KRATOS_DEFINE_APPLICATION_VARIABLE( RANS_APPLICATION, double, TURBULENT_ENERGY_DISSIPATION_RATE_2 ) |
There was a problem hiding this comment.
xplained in the above comment
| KRATOS_DEFINE_3D_VARIABLE_WITH_COMPONENTS( REACTION_MOMENT ) | ||
| KRATOS_DEFINE_3D_VARIABLE_WITH_COMPONENTS( ANGULAR_VELOCITY ) | ||
| KRATOS_DEFINE_3D_VARIABLE_WITH_COMPONENTS( ANGULAR_ACCELERATION ) | ||
| KRATOS_DEFINE_3D_VARIABLE_WITH_COMPONENTS_WITH_TIME_DERIVATIVE(ANGULAR_VELOCITY, ANGULAR_ACCELERATION) |
There was a problem hiding this comment.
not a cosmetic change. Ealier it was possible to define a variable with extern and pass the time derivatives with the proper definition with another macro. Now since it is inline static constexpr, the whole definition and declaration should be in once place [In my opinion it is cleaner). Therefore, when you want to define a var with time derivatives, you should do it at once, hence the above change.
| const int neigh_color = *(neighbor_colors_iterator++); | ||
| array_1d<double,3> dist = cell_center - rConditionCenter; | ||
| double dist_modulus = norm_2(dist); | ||
| array_1d<double,3> unit_dist; |
| .def("GetComponentIndex", &VariableData::GetComponentIndex) | ||
| .def("IsComponent", &VariableData::IsComponent) | ||
| .def("__str__", PrintObject<VariableData>) | ||
| .def("__hash__", &VariableData::Key) |
There was a problem hiding this comment.
The both py::eq and __hash__ was implemented to allow comparing variables correctly even if they come from different TUs (different applications). Earlier, the memory location was used for hashing [ luckily, the unit there was one unit test, and it was comparing the memory locations for hashing ]. Now this one make sure that we always comapre and hash based on the var key.
So the nice thing about the change is, now all the variables which have the same name will have the same key and same memory location if they are in the same TU. But if they are redefined in a different TU again such as in an application, they will have a different memory location, but the key and the name will be the same. In .def("__hash__", &VariableData::Key)
.def(py::self == py::self)
.def(py::self != py::self)A unit test is added to check the intended behavior in a scenario where the same variable is deifned from multiple TUs (or multiple apps) We have addressed all of your comments @pooyan-dadvand . Could you please have a look again? Thanks... :D |
📝 Description
As in the title, this PR tries to make the
VariableandVariableDataconstexpr. One advantage is that we can now useswitchjump table optimizations with variables.This makes
Variable::mZerostatic, which, anyway, was always default-initialized. No customzerodefinitions were found anywhere in Kratos [ Check #14253 ].Closes #14253
Eg:
Kratos/kratos/tests/cpp_tests/containers/test_variables.cpp
Lines 95 to 101 in 4eff549
or
Kratos/kratos/tests/cpp_tests/containers/test_variables.cpp
Lines 107 to 113 in 4eff549
Lets see whether what problems the CI gives.
🆕 Changelog
VariableandVariableDatato be const correct.Variable::mZerostatic and default initialized to zero.VariableandVariableDataconstexprswitch