KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
mapper_utilities.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Philipp Bucher, Jordi Cotela
11 //
12 // See Master-Thesis P.Bucher
13 // "Development and Implementation of a Parallel
14 // Framework for Non-Matching Grid Mapping"
15 
16 #pragma once
17 
18 // System includes
19 #include <array>
20 #include <vector>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/model_part.h"
27 #include "utilities/math_utils.h"
29 #include "mappers/mapper_flags.h"
31 
32 namespace Kratos {
33 namespace MapperUtilities {
34 
35 typedef std::size_t SizeType;
36 typedef std::size_t IndexType;
37 
38 typedef Node NodeType;
39 
41 
43 typedef std::vector<std::vector<MapperInterfaceInfoPointerType>> MapperInterfaceInfoPointerVectorType;
44 
46 typedef std::vector<MapperLocalSystemPointer> MapperLocalSystemPointerVector;
48 
49 using BoundingBoxType = std::array<double, 6>;
50 
51 static void FillFunction(const NodeType& rNode,
52  const Variable<double>& rVariable,
53  double& rValue)
54 {
55  rValue = rNode.FastGetSolutionStepValue(rVariable);
56 }
57 
58 static void FillFunctionNonHist(const NodeType& rNode,
59  const Variable<double>& rVariable,
60  double& rValue)
61 {
62  rValue = rNode.GetValue(rVariable);
63 }
64 
65 static inline std::function<void(const NodeType&, const Variable<double>&, double&)>
66 GetFillFunction(const Kratos::Flags& rMappingOptions)
67 {
68  if (rMappingOptions.Is(MapperFlags::FROM_NON_HISTORICAL))
69  return &FillFunctionNonHist;
70  return &FillFunction;
71 }
72 
73 static void UpdateFunction(NodeType& rNode,
74  const Variable<double>& rVariable,
75  const double Value,
76  const double Factor)
77 {
78  rNode.FastGetSolutionStepValue(rVariable) = Value * Factor;
79 }
80 
81 static void UpdateFunctionWithAdd(NodeType& rNode,
82  const Variable<double>& rVariable,
83  const double Value,
84  const double Factor)
85 {
86  rNode.FastGetSolutionStepValue(rVariable) += Value * Factor;
87 }
88 
89 static void UpdateFunctionNonHist(NodeType& rNode,
90  const Variable<double>& rVariable,
91  const double Value,
92  const double Factor)
93 {
94  rNode.GetValue(rVariable) = Value * Factor;
95 }
96 
98  const Variable<double>& rVariable,
99  const double Value,
100  const double Factor)
101 {
102  rNode.GetValue(rVariable) += Value * Factor;
103 }
104 
105 static inline std::function<void(NodeType&, const Variable<double>&, const double, const double)>
106 GetUpdateFunction(const Kratos::Flags& rMappingOptions)
107 {
108  if (rMappingOptions.Is(MapperFlags::ADD_VALUES) && rMappingOptions.Is(MapperFlags::TO_NON_HISTORICAL))
110  if (rMappingOptions.Is(MapperFlags::ADD_VALUES))
111  return &UpdateFunctionWithAdd;
112  if (rMappingOptions.Is(MapperFlags::TO_NON_HISTORICAL))
113  return &UpdateFunctionNonHist;
114  return &UpdateFunction;
115 }
116 
117 template<class TVectorType, bool TParallel=true>
119  TVectorType& rVector,
120  const ModelPart& rModelPart,
121  const Variable<double>& rVariable,
122  const Kratos::Flags& rMappingOptions,
123  const bool InParallel=true)
124 {
125  KRATOS_TRY;
126 
127  if (!rModelPart.GetCommunicator().GetDataCommunicator().IsDefinedOnThisRank()) return;
128 
129  // Here we construct a function pointer to not have the if all the time inside the loop
130  const auto fill_fct = MapperUtilities::GetFillFunction(rMappingOptions);
131 
132  const int num_local_nodes = rModelPart.GetCommunicator().LocalMesh().NumberOfNodes();
133  const auto nodes_begin = rModelPart.GetCommunicator().LocalMesh().NodesBegin();
134 
135  // necessary bcs the Trilinos Vector is not threadsafe in the default configuration
136  const int num_threads = InParallel ? ParallelUtilities::GetNumThreads() : 1;
137 
138  KRATOS_ERROR_IF(!rMappingOptions.Is(MapperFlags::FROM_NON_HISTORICAL) && !rModelPart.HasNodalSolutionStepVariable(rVariable)) << "Solution step variable \"" << rVariable.Name() << "\" missing in ModelPart \"" << rModelPart.FullName() << "\"!" << std::endl;
139 
140  IndexPartition<std::size_t>(num_local_nodes, num_threads).for_each([&](const std::size_t i){
141  fill_fct(*(nodes_begin + i), rVariable, rVector[i]);
142  });
143 
144  KRATOS_CATCH("");
145 }
146 
147 template<class TVectorType>
149  const TVectorType& rVector,
150  ModelPart& rModelPart,
151  const Variable<double>& rVariable,
152  const Kratos::Flags& rMappingOptions,
153  const bool InParallel=true)
154 {
155  KRATOS_TRY;
156 
157  if (!rModelPart.GetCommunicator().GetDataCommunicator().IsDefinedOnThisRank()) return;
158 
159  const double factor = rMappingOptions.Is(MapperFlags::SWAP_SIGN) ? -1.0 : 1.0;
160 
161  // Here we construct a function pointer to not have the if all the time inside the loop
162  const auto update_fct = std::bind(MapperUtilities::GetUpdateFunction(rMappingOptions),
163  std::placeholders::_1,
164  std::placeholders::_2,
165  std::placeholders::_3,
166  factor);
167  const int num_local_nodes = rModelPart.GetCommunicator().LocalMesh().NumberOfNodes();
168  const auto nodes_begin = rModelPart.GetCommunicator().LocalMesh().NodesBegin();
169 
170  // necessary bcs the Trilinos Vector is not threadsafe in the default configuration
171  const int num_threads = InParallel ? ParallelUtilities::GetNumThreads() : 1;
172 
173  KRATOS_ERROR_IF(!rMappingOptions.Is(MapperFlags::TO_NON_HISTORICAL) && !rModelPart.HasNodalSolutionStepVariable(rVariable)) << "Solution step variable \"" << rVariable.Name() << "\" missing in ModelPart \"" << rModelPart.FullName() << "\"!" << std::endl;
174 
175  IndexPartition<std::size_t>(num_local_nodes, num_threads).for_each([&](const std::size_t i){
176  update_fct(*(nodes_begin + i), rVariable, rVector[i]);
177  });
178 
179  if (rMappingOptions.Is(MapperFlags::TO_NON_HISTORICAL)) {
180  rModelPart.GetCommunicator().SynchronizeNonHistoricalVariable(rVariable);
181  } else {
182  rModelPart.GetCommunicator().SynchronizeVariable(rVariable);
183  }
184 
185  KRATOS_CATCH("");
186 }
187 
196 void AssignInterfaceEquationIds(Communicator& rModelPartCommunicator);
197 
198 void CreateMapperLocalSystemsFromNodes(const MapperLocalSystem& rMapperLocalSystemPrototype,
199  const Communicator& rModelPartCommunicator,
200  std::vector<Kratos::unique_ptr<MapperLocalSystem>>& rLocalSystems);
201 
202 void CreateMapperLocalSystemsFromGeometries(const MapperLocalSystem& rMapperLocalSystemPrototype,
203  const Communicator& rModelPartCommunicator,
204  std::vector<Kratos::unique_ptr<MapperLocalSystem>>& rLocalSystems);
205 
206 template <class T1, class T2>
207 inline double ComputeDistance(const T1& rCoords1,
208  const T2& rCoords2)
209 {
210  return std::sqrt( std::pow(rCoords1[0] - rCoords2[0] , 2) +
211  std::pow(rCoords1[1] - rCoords2[1] , 2) +
212  std::pow(rCoords1[2] - rCoords2[2] , 2) );
213 }
214 
215 template <class T1, class T2, class T3>
217  const T1& rP1,
218  const T2& rP2,
219  const T3& rP3)
220 {
221  // TODO this can probably be optimized
222  const double a = MathUtils<double>::Norm3(rP1-rP2);
223  const double b = MathUtils<double>::Norm3(rP2-rP3);
224  const double c = MathUtils<double>::Norm3(rP3-rP1);
225 
226  const double s = (a+b+c) / 2.0;
227 
228  return (std::sqrt(s*(s-a)*(s-b)*(s-c))) < 1e-12;
229 }
230 
231 template <typename TContainer>
232 double ComputeMaxEdgeLengthLocal(const TContainer& rEntityContainer);
233 
234 double ComputeSearchRadius(const ModelPart& rModelPart, int EchoLevel);
235 
236 double ComputeSearchRadius(const ModelPart& rModelPart1, const ModelPart& rModelPart2, const int EchoLevel);
237 
238 void CheckInterfaceModelParts(const int CommRank);
239 
241 
243 
244 std::string BoundingBoxStringStream(const BoundingBoxType& rBoundingBox);
245 
246 void KRATOS_API(MAPPING_APPLICATION) SaveCurrentConfiguration(ModelPart& rModelPart);
247 void KRATOS_API(MAPPING_APPLICATION) RestoreCurrentConfiguration(ModelPart& rModelPart);
248 
249 template<class TDataType>
250 void EraseNodalVariable(ModelPart& rModelPart, const Variable<TDataType>& rVariable)
251 {
252  KRATOS_TRY;
253 
254  block_for_each(rModelPart.Nodes(), [&](Node& rNode){
255  rNode.GetData().Erase(rVariable);
256  });
257 
258  KRATOS_CATCH("");
259 }
260 
261 void FillBufferBeforeLocalSearch(const MapperLocalSystemPointerVector& rMapperLocalSystems,
262  const std::vector<double>& rBoundingBoxes,
263  const SizeType BufferSizeEstimate,
264  std::vector<std::vector<double>>& rSendBuffer,
265  std::vector<int>& rSendSizes);
266 
267 void CreateMapperInterfaceInfosFromBuffer(const std::vector<std::vector<double>>& rRecvBuffer,
268  const MapperInterfaceInfoUniquePointerType& rpRefInterfaceInfo,
269  const int CommRank,
270  MapperInterfaceInfoPointerVectorType& rMapperInterfaceInfosContainer);
271 
272 void FillBufferAfterLocalSearch(MapperInterfaceInfoPointerVectorType& rMapperInterfaceInfosContainer,
273  const MapperInterfaceInfoUniquePointerType& rpRefInterfaceInfo,
274  const int CommRank,
275  std::vector<std::vector<char>>& rSendBuffer,
276  std::vector<int>& rSendSizes);
277 
279  MapperLocalSystemPointerVectorPointer& rpMapperLocalSystems);
280 
282  const std::vector<std::vector<char>>& rSendBuffer,
283  const MapperInterfaceInfoUniquePointerType& rpRefInterfaceInfo,
284  const int CommRank,
285  MapperInterfaceInfoPointerVectorType& rMapperInterfaceInfosContainer);
286 
298 class KRATOS_API(MAPPING_APPLICATION) MapperInterfaceInfoSerializer
299 {
300 public:
301 
302  MapperInterfaceInfoSerializer(std::vector<MapperInterfaceInfoPointerType>& rMapperInterfaceInfosContainer,
303  const MapperInterfaceInfoUniquePointerType& rpRefInterfaceInfo)
304  : mrInterfaceInfos(rMapperInterfaceInfosContainer)
305  , mrpRefInterfaceInfo(rpRefInterfaceInfo->Create())
306  { }
307 
308 private:
309 
310  std::vector<MapperInterfaceInfoPointerType>& mrInterfaceInfos;
311  MapperInterfaceInfoPointerType mrpRefInterfaceInfo;
312 
313  friend class Kratos::Serializer; // Adding "Kratos::" is nedded bcs of the "MapperUtilities"-namespace
314 
315  virtual void save(Kratos::Serializer& rSerializer) const;
316  virtual void load(Kratos::Serializer& rSerializer);
317 };
318 
319 } // namespace MapperUtilities.
320 
321 } // namespace Kratos.
The Commmunicator class manages communication for distributed ModelPart instances.
Definition: communicator.h:67
virtual bool SynchronizeNonHistoricalVariable(Variable< int > const &rThisVariable)
Definition: communicator.cpp:407
virtual bool SynchronizeVariable(Variable< int > const &rThisVariable)
Definition: communicator.cpp:357
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
MeshType & LocalMesh()
Returns the reference to the mesh storing all local entities.
Definition: communicator.cpp:245
virtual bool IsDefinedOnThisRank() const
Check whether this DataCommunicator involves the current rank.
Definition: data_communicator.h:616
Definition: flags.h:58
bool Is(Flags const &rOther) const
Definition: flags.h:274
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
This is the "Condition" of the mappers.
Definition: mapper_local_system.h:39
Helper class to serialize/deserialize a vector containing MapperInterfaceInfos.
Definition: mapper_utilities.h:299
MapperInterfaceInfoSerializer(std::vector< MapperInterfaceInfoPointerType > &rMapperInterfaceInfosContainer, const MapperInterfaceInfoUniquePointerType &rpRefInterfaceInfo)
Definition: mapper_utilities.h:302
static double Norm3(const TVectorType &a)
Calculates the norm of vector "a" which is assumed to be of size 3.
Definition: math_utils.h:691
SizeType NumberOfNodes() const
Definition: mesh.h:259
NodeIterator NodesBegin()
Definition: mesh.h:326
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
std::string FullName() const
This method returns the full name of the model part (including the parents model parts)
Definition: model_part.h:1850
Communicator & GetCommunicator()
Definition: model_part.h:1821
bool HasNodalSolutionStepVariable(VariableData const &ThisVariable) const
Definition: model_part.h:544
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
This class defines the node.
Definition: node.h:65
TVariableType::Type & FastGetSolutionStepValue(const TVariableType &rThisVariable)
Definition: node.h:435
TVariableType::Type & GetValue(const TVariableType &rThisVariable)
Definition: node.h:466
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
const std::string & Name() const
Definition: variable_data.h:201
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_API(...)
Definition: kratos_export_api.h:40
static int EchoLevel
Definition: co_sim_EMPIRE_API.h:42
void DeserializeMapperInterfaceInfosFromBuffer(const std::vector< std::vector< char >> &rRecvBuffer, const MapperInterfaceInfoUniquePointerType &rpRefInterfaceInfo, const int CommRank, MapperInterfaceInfoPointerVectorType &rMapperInterfaceInfosContainer)
Definition: mapper_utilities.cpp:437
void SaveCurrentConfiguration(ModelPart &rModelPart)
Definition: mapper_utilities.cpp:280
static std::function< void(NodeType &, const Variable< double > &, const double, const double)> GetUpdateFunction(const Kratos::Flags &rMappingOptions)
Definition: mapper_utilities.h:106
std::vector< std::vector< MapperInterfaceInfoPointerType > > MapperInterfaceInfoPointerVectorType
Definition: mapper_utilities.h:43
bool PointsAreCollinear(const T1 &rP1, const T2 &rP2, const T3 &rP3)
Definition: mapper_utilities.h:216
void AssignInterfaceInfosAfterRemoteSearch(const MapperInterfaceInfoPointerVectorType &rMapperInterfaceInfosContainer, MapperLocalSystemPointerVectorPointer &rpMapperLocalSystems)
void UpdateModelPartFromSystemVector(const TVectorType &rVector, ModelPart &rModelPart, const Variable< double > &rVariable, const Kratos::Flags &rMappingOptions, const bool InParallel=true)
Definition: mapper_utilities.h:148
static void FillFunction(const NodeType &rNode, const Variable< double > &rVariable, double &rValue)
Definition: mapper_utilities.h:51
static void UpdateFunctionNonHist(NodeType &rNode, const Variable< double > &rVariable, const double Value, const double Factor)
Definition: mapper_utilities.h:89
std::vector< MapperLocalSystemPointer > MapperLocalSystemPointerVector
Definition: mapper_utilities.h:46
Kratos::shared_ptr< MapperInterfaceInfo > MapperInterfaceInfoPointerType
Definition: mapper_utilities.h:42
Kratos::shared_ptr< MapperLocalSystemPointerVector > MapperLocalSystemPointerVectorPointer
Definition: mapper_utilities.h:47
double ComputeMaxEdgeLengthLocal(const TContainer &rEntityContainer)
Definition: mapper_utilities.cpp:55
void FillBufferBeforeLocalSearch(const MapperLocalSystemPointerVector &rMapperLocalSystems, const std::vector< double > &rBoundingBoxes, const SizeType BufferSizeEstimate, std::vector< std::vector< double >> &rSendBuffer, std::vector< int > &rSendSizes)
Definition: mapper_utilities.cpp:308
static void UpdateFunctionWithAdd(NodeType &rNode, const Variable< double > &rVariable, const double Value, const double Factor)
Definition: mapper_utilities.h:81
std::array< double, 6 > BoundingBoxType
Definition: mapper_utilities.h:49
static void UpdateFunctionNonHistWithAdd(NodeType &rNode, const Variable< double > &rVariable, const double Value, const double Factor)
Definition: mapper_utilities.h:97
void CreateMapperLocalSystemsFromGeometries(const MapperLocalSystem &rMapperLocalSystemPrototype, const Communicator &rModelPartCommunicator, std::vector< Kratos::unique_ptr< MapperLocalSystem >> &rLocalSystems)
Definition: mapper_utilities.cpp:259
BoundingBoxType ComputeGlobalBoundingBox(const ModelPart &rModelPart)
Definition: mapper_utilities.cpp:193
static std::function< void(const NodeType &, const Variable< double > &, double &)> GetFillFunction(const Kratos::Flags &rMappingOptions)
Definition: mapper_utilities.h:66
Kratos::unique_ptr< MapperLocalSystem > MapperLocalSystemPointer
Definition: mapper_utilities.h:45
void FillBufferAfterLocalSearch(MapperInterfaceInfoPointerVectorType &rMapperInterfaceInfosContainer, const MapperInterfaceInfoUniquePointerType &rpRefInterfaceInfo, const int CommRank, std::vector< std::vector< char >> &rSendBuffer, std::vector< int > &rSendSizes)
Definition: mapper_utilities.cpp:399
BoundingBoxType ComputeLocalBoundingBox(const ModelPart &rModelPart)
Definition: mapper_utilities.cpp:176
void CreateMapperLocalSystemsFromNodes(const MapperLocalSystem &rMapperLocalSystemPrototype, const Communicator &rModelPartCommunicator, std::vector< Kratos::unique_ptr< MapperLocalSystem >> &rLocalSystems)
Definition: mapper_utilities.cpp:236
double ComputeDistance(const T1 &rCoords1, const T2 &rCoords2)
Definition: mapper_utilities.h:207
static void UpdateFunction(NodeType &rNode, const Variable< double > &rVariable, const double Value, const double Factor)
Definition: mapper_utilities.h:73
void UpdateSystemVectorFromModelPart(TVectorType &rVector, const ModelPart &rModelPart, const Variable< double > &rVariable, const Kratos::Flags &rMappingOptions, const bool InParallel=true)
Definition: mapper_utilities.h:118
std::size_t SizeType
Definition: mapper_utilities.cpp:31
static void FillFunctionNonHist(const NodeType &rNode, const Variable< double > &rVariable, double &rValue)
Definition: mapper_utilities.h:58
Node NodeType
Definition: mapper_utilities.h:38
void AssignInterfaceEquationIds(Communicator &rModelPartCommunicator)
Assigning INTERFACE_EQUATION_IDs to the nodes, with and without MPI This function assigns the INTERFA...
Definition: mapper_utilities.cpp:34
void RestoreCurrentConfiguration(ModelPart &rModelPart)
Definition: mapper_utilities.cpp:291
Kratos::unique_ptr< MapperInterfaceInfo > MapperInterfaceInfoUniquePointerType
Definition: mapper_utilities.h:40
std::string BoundingBoxStringStream(const BoundingBoxType &rBoundingBox)
Definition: mapper_utilities.cpp:223
void CreateMapperInterfaceInfosFromBuffer(const std::vector< std::vector< double >> &rRecvBuffer, const MapperInterfaceInfoUniquePointerType &rpRefInterfaceInfo, const int CommRank, MapperInterfaceInfoPointerVectorType &rMapperInterfaceInfosContainer)
Definition: mapper_utilities.cpp:349
void CheckInterfaceModelParts(const int CommRank)
Definition: mapper_utilities.cpp:120
void EraseNodalVariable(ModelPart &rModelPart, const Variable< TDataType > &rVariable)
Definition: mapper_utilities.h:250
std::size_t IndexType
Definition: mapper_utilities.cpp:32
double ComputeSearchRadius(const ModelPart &rModelPart, const int EchoLevel)
Definition: mapper_utilities.cpp:69
Modeler::Pointer Create(const std::string &ModelerName, Model &rModel, const Parameters ModelParameters)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:30
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
std::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
a
Definition: generate_stokes_twofluid_element.py:77
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
c
Definition: generate_weakly_compressible_navier_stokes_element.py:108
def load(f)
Definition: ode_solve.py:307
def num_threads
Definition: script.py:75
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254