Skip to content

Conversation

@loumalouomega
Copy link
Member

📝 Description

Replaced explicit global functions with inline lambda expressions for pybind11 bindings in kratos/python/add_constitutive_law_to_python.cpp.

This change improves code readability and conciseness by defining the wrapper functions directly where they are used.

🆕 Changelog

@loumalouomega loumalouomega requested a review from a team as a code owner July 3, 2025 07:03
@loumalouomega loumalouomega added Cleanup Kratos Core Python FastPR This Pr is simple and / or has been already tested and the revision should be fast labels Jul 3, 2025
@loumalouomega loumalouomega enabled auto-merge July 3, 2025 07:03
@loumalouomega loumalouomega requested a review from roigcarlo July 3, 2025 07:03
@philbucher
Copy link
Member

I am quite sure that we used lambdas only as temp solution while we still had C++11

I think the proper solution is overload_cast: https://pybind11.readthedocs.io/en/stable/classes.html#overloaded-methods (its already used in some places)

@loumalouomega
Copy link
Member Author

I am quite sure that we used lambdas only as temp solution while we still had C++11

I think the proper solution is overload_cast: pybind11.readthedocs.io/en/stable/classes.html#overloaded-methods (its already used in some places)

I am unable to compile in Windows, I tried this already

@philbucher
Copy link
Member

can you post the error?

Then again, its already used in other places

@loumalouomega
Copy link
Member Author

can you post the error?

Then again, its already used in other places

I did some test, can you point me where are we using it?

@philbucher
Copy link
Member

just search overload_cast in the repo (?)

@loumalouomega
Copy link
Member Author

just search overload_cast in the repo (?)

Looks like fixed

@loumalouomega
Copy link
Member Author

just search overload_cast in the repo (?)

Looks like fixed

👋

Comment on lines +100 to +129
.def("Create", static_cast<ConstitutiveLaw::Pointer(ConstitutiveLaw::*)(Kratos::Parameters) const>(&ConstitutiveLaw::Create))
.def("Create", static_cast<ConstitutiveLaw::Pointer(ConstitutiveLaw::*)(Kratos::Parameters, const Properties&) const>(&ConstitutiveLaw::Create))
.def("WorkingSpaceDimension", &ConstitutiveLaw::WorkingSpaceDimension)
.def("GetStrainSize", &ConstitutiveLaw::GetStrainSize)
.def("GetStressMeasure", &ConstitutiveLaw::GetStressMeasure)
.def("IsIncremental", &ConstitutiveLaw::IsIncremental)
.def("Has", static_cast<bool (ConstitutiveLaw::*)(const Variable<bool>&)>(&ConstitutiveLaw::Has))
.def("Has", static_cast<bool (ConstitutiveLaw::*)(const Variable<int>&)>(&ConstitutiveLaw::Has))
.def("Has", static_cast<bool (ConstitutiveLaw::*)(const Variable<double>&)>(&ConstitutiveLaw::Has))
.def("Has", static_cast<bool (ConstitutiveLaw::*)(const Variable<array_1d<double, 3>>&)>(&ConstitutiveLaw::Has))
.def("Has", static_cast<bool (ConstitutiveLaw::*)(const Variable<Vector>&)>(&ConstitutiveLaw::Has))
.def("Has", static_cast<bool (ConstitutiveLaw::*)(const Variable<Matrix>&)>(&ConstitutiveLaw::Has))
.def("GetValue", [](ConstitutiveLaw& rCL, const Variable<bool>& rVar) { bool v; rCL.GetValue(rVar, v); return v; })
.def("GetValue", [](ConstitutiveLaw& rCL, const Variable<int>& rVar) { int v; rCL.GetValue(rVar, v); return v; })
.def("GetValue", [](ConstitutiveLaw& rCL, const Variable<double>& rVar) { double v; rCL.GetValue(rVar, v); return v; })
.def("GetValue", [](ConstitutiveLaw& rCL, const Variable<array_1d<double, 3>>& rVar) { array_1d<double, 3> v; rCL.GetValue(rVar, v); return v; })
.def("GetValue", [](ConstitutiveLaw& rCL, const Variable<Vector>& rVar) { Vector v; rCL.GetValue(rVar, v); return v; })
.def("GetValue", [](ConstitutiveLaw& rCL, const Variable<Matrix>& rVar) { Matrix v; rCL.GetValue(rVar, v); return v; })
.def("SetValue", py::overload_cast<const Variable<int>&, const int&, const ProcessInfo&>(&ConstitutiveLaw::SetValue))
.def("SetValue", py::overload_cast<const Variable<double>&, const double&, const ProcessInfo&>(&ConstitutiveLaw::SetValue))
.def("SetValue", py::overload_cast<const Variable<array_1d<double, 3>>&, const array_1d<double, 3>&, const ProcessInfo&>(&ConstitutiveLaw::SetValue))
.def("SetValue", py::overload_cast<const Variable<Vector>&, const Vector&, const ProcessInfo&>(&ConstitutiveLaw::SetValue))
.def("SetValue", py::overload_cast<const Variable<Matrix>&, const Matrix&, const ProcessInfo&>(&ConstitutiveLaw::SetValue))
.def("CalculateValue", [](ConstitutiveLaw& rCL, ConstitutiveLaw::Parameters& rP, const Variable<bool>& rVar) { bool v; rCL.CalculateValue(rP, rVar, v); return v; })
.def("CalculateValue", [](ConstitutiveLaw& rCL, ConstitutiveLaw::Parameters& rP, const Variable<int>& rVar) { int v; rCL.CalculateValue(rP, rVar, v); return v; })
.def("CalculateValue", [](ConstitutiveLaw& rCL, ConstitutiveLaw::Parameters& rP, const Variable<double>& rVar) { double v; rCL.CalculateValue(rP, rVar, v); return v; })
.def("CalculateValue", [](ConstitutiveLaw& rCL, ConstitutiveLaw::Parameters& rP, const Variable<array_1d<double, 3>>& rVar) { array_1d<double, 3> v; rCL.CalculateValue(rP, rVar, v); return v; })
.def("CalculateValue", [](ConstitutiveLaw& rCL, ConstitutiveLaw::Parameters& rP, const Variable<Vector>& rVar) { Vector v; rCL.CalculateValue(rP, rVar, v); return v; })
.def("CalculateValue", [](ConstitutiveLaw& rCL, ConstitutiveLaw::Parameters& rP, const Variable<Matrix>& rVar) { Matrix v; rCL.CalculateValue(rP, rVar, v); return v; })
.def("CalculateMaterialResponse", &ConstitutiveLaw::CalculateMaterialResponse)
Copy link
Member

Choose a reason for hiding this comment

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

Why not use overload_cast here?

.def("PushForwardConstitutiveMatrix", &ConstitutiveLaw::PushForwardConstitutiveMatrix)
.def("Check", &ConstitutiveLaw::Check)
.def("GetLawFeatures", &ConstitutiveLaw::GetLawFeatures)
.def("Info", [](const ConstitutiveLaw& rThisConstitutiveLaw){return rThisConstitutiveLaw.Info();})
Copy link
Member

Choose a reason for hiding this comment

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

why did you change this?

@loumalouomega
Copy link
Member Author

What is the status of this?

@philbucher
Copy link
Member

waiting for answers in the unresolved threads

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

Labels

Cleanup FastPR This Pr is simple and / or has been already tested and the revision should be fast Kratos Core Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants