17 #include <type_traits>
41 template <
class TContainerType,
class TContainerDataIO,
class TVariableType>
43 const TContainerType& rContainer,
44 TVariableType pVariable,
49 const IndexType number_of_entities = rContainer.size();
51 return std::visit([&rContainer, &rDataCommunicator, number_of_entities](
auto pVariable) {
52 using data_type =
typename std::remove_const_t<std::remove_pointer_t<decltype(pVariable)>>::Type;
53 using raw_data_type = std::conditional_t<std::is_same_v<data_type, int>,
int,
double>;
56 std::vector<int> shape_info;
57 if (number_of_entities != 0) {
61 const auto& shape = variable_flatten_data_io.
GetItemShape();
62 shape_info.resize(shape.size() + 1, number_of_entities);
63 std::transform(shape.begin(), shape.end(), shape_info.begin() + 1, [](
const auto v) ->
int { return v; });
65 shape_info.resize(1, number_of_entities);
69 const auto& r_shapes_info_in_ranks = rDataCommunicator.AllGatherv(shape_info);
72 std::vector<IndexType> shape;
73 for (
const auto& r_shape_info_in_rank : r_shapes_info_in_ranks) {
74 if (r_shape_info_in_rank[0] != 0) {
75 shape.resize(r_shape_info_in_rank.size() - 1);
76 std::transform(r_shape_info_in_rank.begin() + 1, r_shape_info_in_rank.end(), shape.begin(), [](
const auto v) ->
IndexType { return v;});
82 shape_info.erase(shape_info.begin());
86 KRATOS_ERROR_IF(number_of_entities > 0 && !std::all_of(shape.begin(), shape.end(), [&local_index, &shape_info](
const auto v) { return static_cast<int>(v) == shape_info[local_index++]; }))
87 <<
"All the ranks should have values with the same shape.\n";
90 auto& r_expression = *p_expression;
92 if (number_of_entities != 0) {
102 return Kratos::intrusive_ptr<Expression>(&r_expression);
109 template <
class TContainerType,
class TContainerDataIO,
class TVariableType>
111 TContainerType& rContainer,
114 TVariableType pVariable)
118 const IndexType number_of_entities = rContainer.size();
120 std::visit([&, number_of_entities](
auto pVariable) {
123 if (number_of_entities > 0) {
125 using data_type =
typename std::remove_const_t<std::remove_pointer_t<decltype(pVariable)>>::Type;
130 if constexpr(std::is_same_v<TContainerType, ModelPart::NodesContainerType>) {
138 if constexpr(std::is_same_v<data_type, Vector> || std::is_same_v<data_type, Matrix>) {
139 data_type dummy_value{};
140 variable_flatten_data_io.
Assign(dummy_value, rExpression, 0);
156 variable_flatten_data_io.
Assign(rValue, rExpression,
Index);
161 if constexpr(std::is_same_v<TContainerType, ModelPart::NodesContainerType>) {
239 template<
class TApplyFunctor>
243 TApplyFunctor&& rApplyFunctor)
252 <<
"Local expression number of entities and local nodes size mismatch [ local nodes size = "
253 << r_local_nodes.size() <<
", local expression number of entities = "
255 << rLocalNodesExpression <<
" ].\n";
257 const IndexType number_of_ghost_nodes = r_ghost_nodes.
size();
259 std::vector<int> ghost_indices(number_of_ghost_nodes);
260 std::transform(r_ghost_nodes.begin(), r_ghost_nodes.end(), ghost_indices.begin(), [](
const auto& rNode) { return rNode.Id(); });
271 std::vector<double>
values(number_of_components);
273 auto values_proxy = pointer_comm.
Apply(
275 const auto p_itr = r_local_nodes.find(rGP->Id());
276 if (p_itr != r_local_nodes.end()) {
277 const IndexType local_index = std::distance(r_local_nodes.begin(), p_itr);
278 const IndexType enitity_data_begin_index = local_index * number_of_components;
279 for (IndexType i = 0; i < number_of_components; ++i) {
280 values[i] = rLocalNodesExpression.Evaluate(local_index, enitity_data_begin_index, i);
283 KRATOS_ERROR <<
"The node with id " << rGP->Id() <<
" not found in the owning rank.";
294 auto& r_ghost_node = *(r_ghost_nodes.begin() +
Index);
295 const auto& r_ghost_node_expression_evaluated_values = values_proxy.Get(gp_list(
Index));
297 rApplyFunctor(r_ghost_node, r_ghost_node_expression_evaluated_values);
The Commmunicator class manages communication for distributed ModelPart instances.
Definition: communicator.h:67
virtual bool SynchronizeNonHistoricalVariable(Variable< int > const &rThisVariable)
Definition: communicator.cpp:407
MeshType & GhostMesh()
Returns the reference to the mesh storing all ghost entities.
Definition: communicator.cpp:251
virtual bool SynchronizeVariable(Variable< int > const &rThisVariable)
Definition: communicator.cpp:357
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
MeshType & InterfaceMesh()
Returns the reference to the mesh storing all interface entities.
Definition: communicator.cpp:257
MeshType & LocalMesh()
Returns the reference to the mesh storing all local entities.
Definition: communicator.cpp:245
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
Base class or all the expression types.
Definition: expression.h:38
IndexType size() const
Definition: expression.cpp:113
virtual const std::vector< IndexType > GetItemShape() const =0
Get the Shape of the expression.
IndexType GetItemComponentCount() const
Get the Local Size of the expression.
Definition: expression.cpp:104
IndexType NumberOfEntities() const
Get the maximum number of entities allowed for this expression.
Definition: expression.h:181
Kratos::intrusive_ptr< Expression > Pointer
Definition: expression.h:44
Definition: expression_io_utils.h:36
static ModelPart::MeshType & GetMesh(Communicator &rCommunicator, MeshType rMeshType)
Definition: expression_io_utils.h:177
static Expression::Pointer ReadToExpression(const TContainerType &rContainer, TVariableType pVariable, const DataCommunicator &rDataCommunicator)
Definition: expression_io_utils.h:42
static void WriteFromExpression(TContainerType &rContainer, Communicator &rCommunicator, const Expression &rExpression, TVariableType pVariable)
Definition: expression_io_utils.h:110
static void EvaluateExpressionOnGhostNodes(Communicator &rCommunicator, const Expression &rLocalNodesExpression, TApplyFunctor &&rApplyFunctor)
Evaluates a local expression on ghost nodes.
Definition: expression_io_utils.h:240
static const ModelPart::MeshType & GetMesh(const Communicator &rCommunicator, MeshType rMeshType)
Definition: expression_io_utils.h:201
A template class for handling communication related to global pointers.
Definition: pointer_communicator.h:178
ResultsProxy< TPointerDataType, TFunctorType > Apply(TFunctorType &&UserFunctor)
Applies a user-provided function to the global pointers and return a proxy to the results.
Definition: pointer_communicator.h:266
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
static GlobalPointersVector< typename TContainerType::value_type > RetrieveGlobalIndexedPointers(const TContainerType &rContainer, const std::vector< int > &rIdList, const DataCommunicator &rDataCommunicator)
Retrieve global indexed pointers from container and data communicator.
Definition: global_pointer_utilities.h:329
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
static LiteralFlatExpression< TRawDataType >::Pointer Create(const IndexType NumberOfEntities, const std::vector< IndexType > &rShape)
A specific create method is used in here to return a pointer to the LiteralFlatExpression.
Definition: literal_flat_expression.cpp:45
NodesContainerType & Nodes()
Definition: mesh.h:346
Construct class to read into expressions from templated data values and write in to templated data va...
Definition: variable_expression_data_io.h:35
void Assign(TDataType &rOutput, const Expression &rExpression, const IndexType EntityIndex) const
Evaluate the expression and assign the values to rOutput at the given index.
Definition: variable_expression_data_io.cpp:234
void Read(RawLiteralFlatExpression &rExpression, const IndexType EntityIndex, const TDataType &Value) const
Read into expression the values from given value.
Definition: variable_expression_data_io.cpp:257
const std::vector< IndexType > GetItemShape() const
Get the shape of the data type.
Definition: variable_expression_data_io.h:132
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void SetNonHistoricalVariable(const TVarType &rVariable, const TType &Value, TContainerType &rContainer)
Sets the container value of any type of non historical variable.
Definition: variable_utils.h:790
void SetNonHistoricalVariableToZero(const Variable< TType > &rVariable, TContainerType &rContainer)
Sets the nodal value of any variable to zero.
Definition: variable_utils.h:724
void SetVariable(const TVarType &rVariable, const TDataType &rValue, NodesContainerType &rNodes, const unsigned int Step=0)
Sets the nodal value of a scalar variable.
Definition: variable_utils.h:675
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
Parameters GetValue(Parameters &rParameters, const std::string &rEntry)
Definition: add_kratos_parameters_to_python.cpp:53
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
MeshType
Definition: traits.h:19
@ Local
Definition: traits.h:20
@ Ghost
Definition: traits.h:21
@ Interface
Definition: traits.h:22
REACTION_CHECK_STIFFNESS_FACTOR int
Definition: contact_structural_mechanics_application_variables.h:75
list values
Definition: bombardelli_test.py:42
def SetValue(entity, variable, value)
Definition: coupling_interface_data.py:256
v
Definition: generate_convection_diffusion_explicit_element.py:114