-
Notifications
You must be signed in to change notification settings - Fork 284
[Core] Add GeometryMetricsTensorAdaptor #14208
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
Merged
sunethwarna
merged 5 commits into
master
from
core/tensor_adaptors/add_geometry_metrics_ta
Mar 3, 2026
+328
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
064a6b8
add geometry_metrics ta
sunethwarna b4e34d6
expose to python
sunethwarna a7b1726
add unit tests
sunethwarna 40f8092
Merge remote-tracking branch 'origin/master' into core/tensor_adaptor…
sunethwarna 45a4e91
addressing mate's comments
sunethwarna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
kratos/tensor_adaptors/geometry_metrics_tensor_adaptor.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // | / | | ||
| // ' / __| _` | __| _ \ __| | ||
| // . \ | ( | | ( |\__ ` | ||
| // _|\_\_| \__,_|\__|\___/ ____/ | ||
| // Multi-Physics | ||
| // | ||
| // License: BSD License | ||
| // Kratos default license: kratos/license.txt | ||
| // | ||
| // Main authors: Suneth Warnakulasuriya | ||
| // | ||
|
|
||
| // System includes | ||
| #include <unordered_map> | ||
|
|
||
| // External includes | ||
|
|
||
| // Project includes | ||
| #include "utilities/atomic_utilities.h" | ||
| #include "utilities/data_type_traits.h" | ||
|
|
||
| // Include base h | ||
| #include "geometry_metrics_tensor_adaptor.h" | ||
|
|
||
| namespace Kratos { | ||
|
|
||
| namespace GeometryMetricsTensorAdaptorHelperUtils { | ||
|
|
||
| DenseVector<unsigned int> GetShape( | ||
| const unsigned int NumberOfEntities, | ||
| const GeometryMetricsTensorAdaptor::Metric CurrentMetric) | ||
| { | ||
| switch (CurrentMetric) { | ||
| case GeometryMetricsTensorAdaptor::DomainSize: | ||
| return DenseVector<unsigned int>(1, NumberOfEntities); | ||
| } | ||
|
|
||
| return DenseVector<unsigned int>(0); | ||
| } | ||
|
|
||
| template<class TEntityType> | ||
| const ModelPart::GeometryType& GetGeometry(const TEntityType& rEntity) | ||
| { | ||
| if constexpr(std::is_same_v<TEntityType, ModelPart::GeometryType>) { | ||
| return rEntity; | ||
| } else { | ||
| return rEntity.GetGeometry(); | ||
| } | ||
| } | ||
|
|
||
| template<class TContainerType> | ||
| void FillDomainSize( | ||
| Kratos::span<double> DataSpan, | ||
| const TContainerType& rContainer) | ||
| { | ||
| IndexPartition<IndexType>(rContainer.size()).for_each([&rContainer, &DataSpan](const auto Index) { | ||
| DataSpan[Index] = GetGeometry(*(rContainer.begin() + Index)).DomainSize(); | ||
| }); | ||
| } | ||
|
|
||
| template<class TContainerType> | ||
| void FillData( | ||
| Kratos::span<double> DataSpan, | ||
| const GeometryMetricsTensorAdaptor::Metric CurrentMetric, | ||
| const TContainerType& rContainer) | ||
| { | ||
| switch (CurrentMetric) { | ||
| case GeometryMetricsTensorAdaptor::DomainSize: | ||
| FillDomainSize(DataSpan, rContainer); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } // namespace GeometryMetricsTensorAdaptorHelperUtils | ||
|
|
||
| template<class TContainerPointerType> | ||
| GeometryMetricsTensorAdaptor::GeometryMetricsTensorAdaptor( | ||
| TContainerPointerType pContainer, | ||
| const Metric CurrentMetric) | ||
| : mMetric(CurrentMetric) | ||
| { | ||
| this->mpContainer = pContainer; | ||
| this->mpStorage = Kratos::make_shared<Storage>(DenseVector<unsigned int>(1, pContainer->size())); | ||
| } | ||
|
|
||
| GeometryMetricsTensorAdaptor::GeometryMetricsTensorAdaptor( | ||
| const TensorAdaptor& rOther, | ||
| const Metric CurrentMetric, | ||
| const bool Copy) | ||
| : BaseType(rOther, Copy), | ||
| mMetric(CurrentMetric) | ||
| { | ||
| KRATOS_TRY | ||
|
|
||
| if (!HoldsAlternative<ModelPart::GeometryContainerType::Pointer, ModelPart::ConditionsContainerType::Pointer, | ||
| ModelPart::ElementsContainerType::Pointer>::Evaluate(this->GetContainer())) { | ||
| KRATOS_ERROR << "GeometryMetricsTensorAdaptor can only be used with tensor data " | ||
| "having geometry, condition or element containers."; | ||
| } | ||
|
|
||
| const auto& current_shape = this->Shape(); | ||
|
|
||
| KRATOS_ERROR_IF(current_shape.size() < 1) << "Tensor data's first dimension should represent number of entities [ tensor adaptor = " << *this << " ].\n"; | ||
|
|
||
| const auto& required_shape = GeometryMetricsTensorAdaptorHelperUtils::GetShape(current_shape[0], CurrentMetric); | ||
|
|
||
| for (IndexType i = 0; i < required_shape.size(); ++i) { | ||
| KRATOS_ERROR_IF_NOT(current_shape[i] == required_shape[i]) | ||
| << "Incompatible tensor data shape [ required tensor data shape = " << required_shape | ||
| << ", current tensor data shape = " << current_shape << ", tensor adaptor = " << *this << " ].\n"; | ||
| } | ||
|
|
||
| KRATOS_CATCH(""); | ||
| } | ||
|
|
||
| TensorAdaptor<double>::Pointer GeometryMetricsTensorAdaptor::Clone() const | ||
| { | ||
| return Kratos::make_shared<GeometryMetricsTensorAdaptor>(*this); | ||
| } | ||
|
|
||
| void GeometryMetricsTensorAdaptor::CollectData() | ||
| { | ||
| KRATOS_TRY | ||
|
|
||
| std::visit([this](auto pContainer) { | ||
| using container_type = BareType<decltype(*pContainer)>; | ||
| if constexpr(IsInList<container_type, ModelPart::GeometryContainerType, ModelPart::ConditionsContainerType, ModelPart::ElementsContainerType>) { | ||
| GeometryMetricsTensorAdaptorHelperUtils::FillData(this->ViewData(), this->mMetric, *pContainer); | ||
| } | ||
|
|
||
| }, this->GetContainer()); | ||
|
|
||
| KRATOS_CATCH(""); | ||
| } | ||
|
|
||
| void GeometryMetricsTensorAdaptor::StoreData() | ||
| { | ||
| KRATOS_ERROR << "StoreData method is not implemented."; | ||
| } | ||
|
|
||
| std::string GeometryMetricsTensorAdaptor::Info() const | ||
| { | ||
| std::stringstream info; | ||
|
|
||
| info << "GeometryMetricsTensorAdaptor:"; | ||
| info << " Metric = "; | ||
| switch (mMetric) { | ||
| case DomainSize: info << "DomainSize"; break; | ||
| } | ||
| info << ", " << BaseType::Info(); | ||
| return info.str(); | ||
| } | ||
|
|
||
| template KRATOS_API(KRATOS_CORE) GeometryMetricsTensorAdaptor::GeometryMetricsTensorAdaptor(ModelPart::GeometryContainerType::Pointer, GeometryMetricsTensorAdaptor::Metric); | ||
| template KRATOS_API(KRATOS_CORE) GeometryMetricsTensorAdaptor::GeometryMetricsTensorAdaptor(ModelPart::ConditionsContainerType::Pointer, GeometryMetricsTensorAdaptor::Metric); | ||
| template KRATOS_API(KRATOS_CORE) GeometryMetricsTensorAdaptor::GeometryMetricsTensorAdaptor(ModelPart::ElementsContainerType::Pointer, GeometryMetricsTensorAdaptor::Metric); | ||
|
|
||
| } // namespace Kratos |
122 changes: 122 additions & 0 deletions
122
kratos/tensor_adaptors/geometry_metrics_tensor_adaptor.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // | / | | ||
| // ' / __| _` | __| _ \ __| | ||
| // . \ | ( | | ( |\__ ` | ||
| // _|\_\_| \__,_|\__|\___/ ____/ | ||
| // Multi-Physics | ||
| // | ||
| // License: BSD License | ||
| // Kratos default license: kratos/license.txt | ||
| // | ||
| // Main authors: Suneth Warnakulasuriya | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| // System includes | ||
| #include <string> | ||
|
|
||
| // External includes | ||
|
|
||
| // Project includes | ||
| #include "tensor_adaptors/tensor_adaptor.h" | ||
|
|
||
| namespace Kratos { | ||
|
|
||
| ///@name Kratos Classes | ||
| ///@{ | ||
|
|
||
| /** | ||
| * @ingroup TensorAdaptors | ||
| * @brief Adaptor class for calculating geometry metrics. | ||
| * | ||
| * @details This class provides an interface to calculate geometry metrics for given entity container pointer @p pContainer . | ||
| * This @ref TensorAdaptor only implements the @ref CollectData method. Following geometry metrics are supported. | ||
| * - @ref GeometryMetricsTensorAdaptor::DomainSize | ||
| * | ||
| * @section GeometryMetricsTensorAdaptor_supported_container Supported entity container types | ||
| * - @ref ModelPart::GeometryContainerType | ||
| * - @ref ModelPart::ConditionsContainerType | ||
| * - @ref ModelPart::ElementsContainerType | ||
| * | ||
| * @section GeometryMetricsTensorAdaptor_usage Usage | ||
| * - Use @ref CollectData to fill internal data with the metric from each entity given by the container pointer @p pContainer . | ||
| * | ||
| * @author Suneth Warnakulasuriya | ||
| * @see @ref TensorAdaptor Base class. | ||
| */ | ||
| class KRATOS_API(KRATOS_CORE) GeometryMetricsTensorAdaptor: public TensorAdaptor<double> { | ||
| public: | ||
| ///@name Enums | ||
| ///@{ | ||
|
|
||
| enum class Metric | ||
| { | ||
| DomainSize | ||
| }; | ||
|
|
||
| ///@} | ||
| ///@name Type definitions | ||
| ///@{ | ||
|
|
||
| KRATOS_CLASS_POINTER_DEFINITION(GeometryMetricsTensorAdaptor); | ||
|
|
||
| using BaseType = TensorAdaptor<double>; | ||
|
|
||
| using enum Metric; | ||
|
|
||
| ///@} | ||
| ///@name Life cycle | ||
| ///@{ | ||
|
|
||
| template<class TContainerPointerType> | ||
| GeometryMetricsTensorAdaptor( | ||
| TContainerPointerType pContainer, | ||
| const Metric Datum); | ||
|
|
||
| GeometryMetricsTensorAdaptor( | ||
| const TensorAdaptor& rOther, | ||
| const Metric Datum, | ||
| const bool Copy = true); | ||
|
|
||
| // Destructor | ||
| ~GeometryMetricsTensorAdaptor() override = default; | ||
|
|
||
| ///@} | ||
| ///@name Public operations | ||
| ///@{ | ||
|
|
||
| /** | ||
| * @brief Clones the existing tensor adaptor. | ||
| */ | ||
| TensorAdaptor::Pointer Clone() const override; | ||
|
|
||
| /** | ||
| * @brief Fill the internal data with metric of the entities in the container. | ||
| */ | ||
| void CollectData() override; | ||
|
|
||
| /** | ||
| * @brief Does not do anything | ||
| * @throws std::runtime_error always. | ||
| */ | ||
| void StoreData() override; | ||
|
|
||
| ///@} | ||
| ///@name Input and output | ||
| ///@{ | ||
|
|
||
| std::string Info() const override; | ||
|
|
||
| ///@} | ||
|
|
||
| private: | ||
| ///@name Private member variables | ||
| ///@{ | ||
|
|
||
| Metric mMetric; | ||
|
|
||
| ///@} | ||
| }; | ||
|
|
||
| /// @} | ||
| } // namespace Kratos | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.