Skip to content
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

[GeoMechanicsApplication] Added (de)serialization of stress state policies #13163

Merged
merged 22 commits into from
Mar 4, 2025

Conversation

avdg81
Copy link
Contributor

@avdg81 avdg81 commented Feb 24, 2025

📝 Description

Stress state policies are typically used and manipulated through a pointer to their abstract base class (i.e. StressStatePolicy). This PR adds (de)serialization of the concrete stress state policy classes through a pointer to this interface. Note that it requires some changes in Kratos Core, which have been moved to a separate PR, that needs to be reviewed and approved by CIMNE. The current PR builds on top of that. Once the other PR has been merged with master, we can take out the duplicated changes from this PR.

To make this work, we had to make a minor change to the (de)serialization code of Kratos Core. We've added a compile-time check to see whether a given class is an abstract class. If it is, we shouldn't try to instantiate it. In practice, this is not expected to ever happen.
It was no longer needed, since we make better use of the (de)serialization code offered by the Kratos Core.
…ate policy

This required us to register the axisymmetric stress state policy.
… policy

This required us to register the interface stress state policy.
This required us to register the 3D stress state policy.
When the data type of the shared pointer is an abstract base class, no longer attempt to instantiate it (which yields a compile-time error). This would only be attempted when the pointer type is `SP_BASE_CLASS_POINTER`. In practice, the underlying type will always be different from that abstract base class, and therefore the pointer type must always be `SP_DERIVED_CLASS_POINTER`, which looks up the underlying concrete type to instantiate.
When the data type of the intrusive pointer is an abstract base class, no longer attempt to instantiate it (which yields a compile-time error). This would only be attempted when the pointer type is `SP_BASE_CLASS_POINTER`. In practice, the underlying type will always be different from that abstract base class, and therefore the pointer type must always be `SP_DERIVED_CLASS_POINTER`, which looks up the underlying concrete type to instantiate.

To make this work with the test classes, a few more members had to be defined.
When the data type of the raw owning pointer is an abstract base class, no longer attempt to instantiate it (which yields a compile-time error). This would only be attempted when the pointer type is `SP_BASE_CLASS_POINTER`. In practice, the underlying type will always be different from that abstract base class, and therefore the pointer type must always be `SP_DERIVED_CLASS_POINTER`, which looks up the underlying concrete type to instantiate.

Also changed the test suite of the new unit tests to the one without a kernel, since it's not needed.
Moved the new test classes and the scoped registration class to an anonymous namespace. Need to see whether GCC and Clang then build without errors.
Use a `Serializer` instance rather than a `StreamSerializer` instance.
Made the `Serializer` class a `friend` of the derived test class.
The branch that contains the required changes for Kratos Core has been merged into this branch by copying two files from there to here. This should fix the build problems with GCC and Clang.
@avdg81 avdg81 added Serialization GeoMechanics Issues related to the GeoMechanicsApplication labels Feb 24, 2025
@avdg81 avdg81 self-assigned this Feb 24, 2025
markelov208
markelov208 previously approved these changes Feb 24, 2025
Copy link
Contributor

@markelov208 markelov208 left a comment

Choose a reason for hiding this comment

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

Hi Anne, that is another nice step to have serialization for all classes. I do not have a blocking comment.

Comment on lines +75 to +84
void AxisymmetricStressState::save(Serializer&) const
{
// No data members to be saved (yet)
}

void AxisymmetricStressState::load(Serializer&)
{
// No data members to be loaded (yet)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, there are no any data members. Would it be better to have these functions as protected in the base class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure whether I get your point. Both member functions are currently private. Can you help me to see why you think they should be protected?

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, these empty functions shall be implemented in each child. As far as I understand, if the base class defines them as protected then all children will use them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I start to see your point. Since the StressStatePolicy class is intended to provide an interface only and not an implementation, I'd rather keep the save and load members pure virtual. In that case, any derived class must implement them, even if they are empty. Since I value this separation of interface and implementation very much, I wouldn't want to change that, unless you have good reasons why we should. Thanks.

rfaasse
rfaasse previously approved these changes Feb 25, 2025
Copy link
Contributor

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

Nicely done, I don't have any blocking comments, just a minor question and suggestion.

@@ -61,6 +63,12 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) StressStatePolicy
{
return Dimension == N_DIM_3D ? GetStressTensorSize3D() : GetStressTensorSize2D();
}

friend class Serializer;

Copy link
Contributor

Choose a reason for hiding this comment

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

Here there are some redundant blank lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Removed the redundant blank lines.

KRATOS_TEST_CASE_IN_SUITE(AxisymmetricStressState_CanBeSavedAndLoadedThroughInterface, KratosGeoMechanicsFastSuiteWithoutKernel)
{
// Arrange
RegistrationUtilities::RegisterStressStatePolicies();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we deregister at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree that it's better to keep the scope of the registration as short as possible (i.e. as long as the test runs, but no longer). I have added an RAII class (ScopedSerializerRegistrationOfAllStressStatePolicies) in test_utilties.h that takes care of that.

- Extended class `RegistrationUtilities` with a member function to deregister all stress state policies.
- Added a class for scoped registration of all stress state policies. The unit tests use this class to keep the registration scope as small as possible.
- Removed a few redundant blank lines.
- Construct `std::string` instances directly, rather than through temporary `const char*` objects.
@avdg81 avdg81 marked this pull request as ready for review March 4, 2025 10:20
@avdg81 avdg81 requested review from rfaasse and markelov208 March 4, 2025 10:20
Copy link
Contributor

@markelov208 markelov208 left a comment

Choose a reason for hiding this comment

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

Hi Anne, from my viewpoint it is good to be merged.

Copy link
Contributor

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

Nice, looks good to go to me!

@avdg81 avdg81 merged commit ab70eb1 into master Mar 4, 2025
11 checks passed
@avdg81 avdg81 deleted the geo/13136-serialization-of-stress-state-policies branch March 4, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GeoMechanics Issues related to the GeoMechanicsApplication Serialization
Development

Successfully merging this pull request may close these issues.

[GeoMechanicsApplication] Fix serialization of the stress state policies
3 participants