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

[Core] Add variable type - vector of data value container #12812

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions kratos/python/add_containers_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "includes/constitutive_law.h"
#include "includes/convection_diffusion_settings.h"
#include "includes/radiation_settings.h"
#include "utilities/layered_thickness_data_container.h"
#include "utilities/quaternion.h"

namespace Kratos::Python
Expand Down Expand Up @@ -158,6 +159,10 @@ void AddContainersToPython(pybind11::module& m)
.def("__str__", PrintObject<Variable<Quaternion<double> >>)
;

py::class_<Variable<LayeredThicknessDataContainer >>(m, "LayeredThicknessDataContainer Variable")
.def("__str__", PrintObject<Variable<LayeredThicknessDataContainer >>)
;

typedef py::class_<DataValueContainer, DataValueContainer::Pointer> DataValueContainerBinderType;
DataValueContainerBinderType DataValueBinder(m, "DataValueContainer" );
DataValueBinder.def( "__len__", &DataValueContainer::Size );
Expand All @@ -175,6 +180,7 @@ void AddContainersToPython(pybind11::module& m)
DataValueContainerIndexingUtility< DataValueContainerBinderType, DataValueContainer, Variable<RadiationSettings::Pointer> >(DataValueBinder);
DataValueContainerIndexingUtility< DataValueContainerBinderType, DataValueContainer, Variable<Quaternion<double>> >(DataValueBinder);
DataValueContainerIndexingUtility< DataValueContainerBinderType, DataValueContainer, Variable<std::string> >(DataValueBinder);
DataValueContainerIndexingUtility< DataValueContainerBinderType, DataValueContainer, Variable<LayeredThicknessDataContainer > >(DataValueBinder);

typedef py::class_<VariablesListDataValueContainer, VariablesListDataValueContainer::Pointer> VariableDataValueContainerBinderType;
VariableDataValueContainerBinderType VariableDataValueBinder(m, "VariablesListDataValueContainer" );
Expand Down
14 changes: 14 additions & 0 deletions kratos/python/add_mesh_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "includes/properties.h"
#include "python/add_mesh_to_python.h"
#include "python/containers_interface.h"
#include "utilities/layered_thickness_data_container.h"


namespace Kratos::Python
{
Expand Down Expand Up @@ -457,6 +459,12 @@ void AddMeshToPython(pybind11::module& m)
.def("SetValue", SetValueHelperFunction< Element, Variable< std::string > >)
.def("GetValue", GetValueHelperFunction< Element, Variable< std::string > >)

.def("__setitem__", SetValueHelperFunction< Element, Variable< LayeredThicknessDataContainer > >)
.def("__getitem__", GetValueHelperFunction< Element, Variable< LayeredThicknessDataContainer > >)
.def("Has", HasHelperFunction< Element, Variable< LayeredThicknessDataContainer > >)
.def("SetValue", SetValueHelperFunction< Element, Variable< LayeredThicknessDataContainer > >)
.def("GetValue", GetValueHelperFunction< Element, Variable< LayeredThicknessDataContainer > >)

.def("GetNode", GetNodeFromElement )
.def("GetNodes", GetNodesFromElement )
.def("GetIntegrationPoints", GetIntegrationPointsFromElement )
Expand Down Expand Up @@ -603,6 +611,12 @@ void AddMeshToPython(pybind11::module& m)
.def("SetValue", SetValueHelperFunction< Condition, Variable< std::string > >)
.def("GetValue", GetValueHelperFunction< Condition, Variable< std::string > >)

.def("__setitem__", SetValueHelperFunction< Condition, Variable< LayeredThicknessDataContainer > >)
.def("__getitem__", GetValueHelperFunction< Condition, Variable< LayeredThicknessDataContainer > >)
.def("Has", HasHelperFunction< Condition, Variable< LayeredThicknessDataContainer > >)
.def("SetValue", SetValueHelperFunction< Condition, Variable< LayeredThicknessDataContainer > >)
.def("GetValue", GetValueHelperFunction< Condition, Variable< LayeredThicknessDataContainer > >)

.def("GetNode", GetNodeFromCondition )
.def("GetNodes", GetNodesFromCondition )

Expand Down
3 changes: 3 additions & 0 deletions kratos/python/add_node_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "includes/condition.h"
#include "python/containers_interface.h"
#include "python/add_mesh_to_python.h"
#include "utilities/layered_thickness_data_container.h"


namespace Kratos::Python
{
Expand Down Expand Up @@ -148,6 +150,7 @@ void AddNodeToPython(pybind11::module& m)
IndexingUtility<NodeBinderType,Node,Variable<Quaternion<double> > >(node_binder);
IndexingUtility<NodeBinderType,Node,Variable<Vector > >(node_binder);
IndexingUtility<NodeBinderType,Node,Variable<Matrix > >(node_binder);
IndexingUtility<NodeBinderType,Node,Variable<LayeredThicknessDataContainer > >(node_binder);

node_binder.def("GetBufferSize", &Node::GetBufferSize);
node_binder.def("AddDof", NodeAddDof<Variable<double> >);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "utilities/layered_thickness_data_container.h"
#include "testing/testing.h"

namespace Kratos::Testing
{

KRATOS_TEST_CASE_IN_SUITE(LayeredThicknessDataContainerConstructorWithVector, KratosCoreFastSuite)
{
std::vector<DataValueContainer> data_vector(2);
data_vector[0].SetValue(TEMPERATURE, 100.0);
data_vector[1].SetValue(TEMPERATURE, 200.0);

LayeredThicknessDataContainer container(data_vector);

KRATOS_EXPECT_EQ(container.Size(), 2);
KRATOS_EXPECT_NEAR(container.Get(0).GetValue(TEMPERATURE), 100.0, 1e-8);
KRATOS_EXPECT_NEAR(container.Get(1).GetValue(TEMPERATURE), 200.0, 1e-8);
}

KRATOS_TEST_CASE_IN_SUITE(LayeredThicknessDataContainerAddGetSet, KratosCoreFastSuite)
{
LayeredThicknessDataContainer container;
DataValueContainer data1, data2;
data1.SetValue(TEMPERATURE, 100.0);
data2.SetValue(TEMPERATURE, 200.0);

container.Add(data1);
container.Add(data2);

KRATOS_EXPECT_EQ(container.Size(), 2);
const DataValueContainer& data0_ref = container.Get(0);
DataValueContainer& data1_ref = container.Get(1);
KRATOS_EXPECT_NEAR(data0_ref.GetValue(TEMPERATURE), 100.0, 1e-8);
KRATOS_EXPECT_NEAR(data1_ref.GetValue(TEMPERATURE), 200.0, 1e-8);

DataValueContainer data3;
data3.SetValue(TEMPERATURE, 300.0);
container.Set(1, data3);

KRATOS_EXPECT_NEAR(container.Get(1).GetValue(TEMPERATURE), 300.0, 1e-8);
}

KRATOS_TEST_CASE_IN_SUITE(LayeredThicknessDataContainerSetOutOfRange, KratosCoreFastSuite)
{
LayeredThicknessDataContainer container;
DataValueContainer data;
data.SetValue(TEMPERATURE, 150.0);

container.Add(data);

KRATOS_EXPECT_EXCEPTION_IS_THROWN(container.Set(1, data), "Index out of range");
}

}
49 changes: 49 additions & 0 deletions kratos/utilities/layered_thickness_data_container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "utilities/layered_thickness_data_container.h"

namespace Kratos
{
LayeredThicknessDataContainer::LayeredThicknessDataContainer(
const std::vector<DataValueContainer>& rContainerVector)
: mContainerVector(rContainerVector)
{
}

DataValueContainer& LayeredThicknessDataContainer ::Get(
const std::size_t Index)
{
return mContainerVector.at(Index);
}

const DataValueContainer& LayeredThicknessDataContainer ::Get(
const std::size_t Index) const
{
return mContainerVector.at(Index);
}

// Set function to modify the DataValueContainer at a specific Index
void LayeredThicknessDataContainer ::Set(
const std::size_t Index,
const DataValueContainer& rValue)
{
if (Index < mContainerVector.size()) {
mContainerVector[Index] = rValue;
}
else {
KRATOS_ERROR << "Index out of range" << std::endl;
}
}

// Function to add a new DataValueContainer to the vector
void LayeredThicknessDataContainer ::Add(
const DataValueContainer& rValue)
{
mContainerVector.push_back(rValue);
}

// Function to get the size of the container vector
std::size_t LayeredThicknessDataContainer ::Size() const
{
return mContainerVector.size();
}

}
143 changes: 143 additions & 0 deletions kratos/utilities/layered_thickness_data_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#pragma once

#include "containers/data_value_container.h"

namespace Kratos
{

/**
* @brief This class is used to store data in a layered object,
* where each layer is represented by a DataValueContainer object.
*/
class KRATOS_API(KRATOS_CORE) LayeredThicknessDataContainer
{
public:

/**
* @brief Pointer definition of LayeredThicknessDataContainer .
*/
KRATOS_CLASS_POINTER_DEFINITION(DataValueContainer);

/**
* @brief Constructor with std::vector<DataValueContainer>.
* @param rContainerVector Reference to a vector of DataValueContainer objects.
*/
LayeredThicknessDataContainer(
const std::vector<DataValueContainer>& rContainerVector);

/**
* @brief Default constructor.
*/
LayeredThicknessDataContainer() = default;

/**
* @brief Copy constructor.
* @param rOther Reference to another LayeredThicknessDataContainer object.
*/
LayeredThicknessDataContainer(
const LayeredThicknessDataContainer & rOther) = default;

/**
* @brief Move constructor.
* @param rOther Rvalue reference to another LayeredThicknessDataContainer object.
*/
LayeredThicknessDataContainer(
LayeredThicknessDataContainer && rOther) noexcept = default;

/**
* @brief Copy assignment operator.
* @param rOther Reference to another LayeredThicknessDataContainer object.
* @return Reference to the current object.
*/
LayeredThicknessDataContainer & operator=(
const LayeredThicknessDataContainer & rOther) = default;

/**
* @brief Move assignment operator.
* @param rOther Rvalue reference to another LayeredThicknessDataContainer object.
* @return Reference to the current object.
*/
LayeredThicknessDataContainer & operator=(
LayeredThicknessDataContainer && rOther) noexcept = default;

/**
* @brief Destructor.
*/
virtual ~LayeredThicknessDataContainer () = default;

/**
* @brief Get function to access the DataValueContainer at a specific index.
* @param Index The index of the DataValueContainer to access.
* @return Reference to the DataValueContainer at the specified index.
*/
DataValueContainer& Get(
const std::size_t Index);

/**
* @brief Const version of the Get function.
* @param Index The index of the DataValueContainer to access.
* @return Const reference to the DataValueContainer at the specified index.
*/
const DataValueContainer& Get(
const std::size_t Index) const;

/**
* @brief Set function to modify the DataValueContainer at a specific index.
* @param Index The index of the DataValueContainer to modify.
* @param rValue Reference to the new DataValueContainer value.
*/
void Set(
const std::size_t Index,
const DataValueContainer& rValue);

/**
* @brief Function to add a new DataValueContainer to the vector.
* @param rValue Reference to the DataValueContainer to add.
*/
void Add(
const DataValueContainer& rValue);

/**
* @brief Function to get the size of the container vector.
* @return The size of the container vector.
*/
std::size_t Size() const;

// The following methods must exist to compile but are not used
friend class Serializer;

virtual void save(Serializer& rSerializer) const
{
KRATOS_ERROR << "Not implemented" << std::endl;
}

virtual void load(Serializer& rSerializer)
{
KRATOS_ERROR << "Not implemented" << std::endl;
}

protected:
/**
* @brief The vector of DataValueContainer objects.
*/
std::vector<DataValueContainer> mContainerVector;

};

inline std::istream& operator >> (
std::istream& rIStream,
LayeredThicknessDataContainer & rThis)
{
return rIStream;
}

inline std::ostream& operator << (
std::ostream& rOStream,
const LayeredThicknessDataContainer & rThis)
{
rOStream << "LayeredThicknessDataContainer " << std::endl;

return rOStream;
}

} // namespace Kratos
Loading