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.
nearest_element_mapper.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 
20 // External includes
21 
22 // Project includes
25 
26 namespace Kratos
27 {
30 
33 {
34  bool UseApproximation = true; // Whether or not an approximation should be computed, if the full projection fails
35  double LocalCoordTol = 0.25; // tolerance for the local coordinates, until which the projection is considered
36 };
37 
38 class KRATOS_API(MAPPING_APPLICATION) NearestElementInterfaceInfo : public MapperInterfaceInfo
39 {
40 public:
41 
43  explicit NearestElementInterfaceInfo(const NearestElementOptions Options={}) : mOptions(Options) {}
44 
45  explicit NearestElementInterfaceInfo(const CoordinatesArrayType& rCoordinates,
46  const IndexType SourceLocalSystemIndex,
47  const IndexType SourceRank,
48  const NearestElementOptions Options={})
49  : MapperInterfaceInfo(rCoordinates, SourceLocalSystemIndex, SourceRank), mOptions(Options) {}
50 
51  MapperInterfaceInfo::Pointer Create() const override
52  {
53  return Kratos::make_shared<NearestElementInterfaceInfo>(mOptions);
54  }
55 
56  MapperInterfaceInfo::Pointer Create(const CoordinatesArrayType& rCoordinates,
57  const IndexType SourceLocalSystemIndex,
58  const IndexType SourceRank) const override
59  {
60  return Kratos::make_shared<NearestElementInterfaceInfo>(
61  rCoordinates,
62  SourceLocalSystemIndex,
63  SourceRank,
64  mOptions);
65  }
66 
68  {
70  }
71 
72  void ProcessSearchResult(const InterfaceObject& rInterfaceObject) override;
73 
74  void ProcessSearchResultForApproximation(const InterfaceObject& rInterfaceObject) override;
75 
76  void GetValue(std::vector<int>& rValue,
77  const InfoType ValueType) const override
78  {
79  rValue = mNodeIds;
80  }
81 
82  void GetValue(std::vector<double>& rValue,
83  const InfoType ValueType) const override
84  {
85  rValue = mShapeFunctionValues;
86  }
87 
88  void GetValue(double& rValue,
89  const InfoType ValueType) const override
90  {
91  rValue = mClosestProjectionDistance;
92  }
93 
94  void GetValue(int& rValue,
95  const InfoType ValueType) const override
96  {
97  rValue = (int)mPairingIndex;
98  }
99 
100  std::size_t GetNumSearchResults() const { return mNumSearchResults; }
101 
102 private:
103 
104  std::vector<int> mNodeIds;
105  std::vector<double> mShapeFunctionValues;
106  double mClosestProjectionDistance = std::numeric_limits<double>::max();
108  NearestElementOptions mOptions; // this is not needed after searching, hence no need to serialize it
109  std::size_t mNumSearchResults = 0;
110 
111  void SaveSearchResult(const InterfaceObject& rInterfaceObject,
112  const bool ComputeApproximation);
113 
114  friend class Serializer;
115 
116  void save(Serializer& rSerializer) const override
117  {
119  rSerializer.save("NodeIds", mNodeIds);
120  rSerializer.save("SFValues", mShapeFunctionValues);
121  rSerializer.save("ClosestProjectionDistance", mClosestProjectionDistance);
122  rSerializer.save("PairingIndex", (int)mPairingIndex);
123  rSerializer.save("NumSearchResults", mNumSearchResults);
124  }
125 
126  void load(Serializer& rSerializer) override
127  {
129  rSerializer.load("NodeIds", mNodeIds);
130  rSerializer.load("SFValues", mShapeFunctionValues);
131  rSerializer.load("ClosestProjectionDistance", mClosestProjectionDistance);
132  int temp;
133  rSerializer.load("PairingIndex", temp);
134  mPairingIndex = (ProjectionUtilities::PairingIndex)temp;
135  rSerializer.load("NumSearchResults", mNumSearchResults);
136  }
137 
138 };
139 
140 class KRATOS_API(MAPPING_APPLICATION) NearestElementLocalSystem : public MapperLocalSystem
141 {
142 public:
143 
144  explicit NearestElementLocalSystem(NodePointerType pNode) : mpNode(pNode) {}
145 
146  void CalculateAll(MatrixType& rLocalMappingMatrix,
147  EquationIdVectorType& rOriginIds,
148  EquationIdVectorType& rDestinationIds,
149  MapperLocalSystem::PairingStatus& rPairingStatus) const override;
150 
152  {
153  KRATOS_DEBUG_ERROR_IF_NOT(mpNode) << "Members are not intitialized!" << std::endl;
154  return mpNode->Coordinates();
155  }
156 
158  {
159  return Kratos::make_unique<NearestElementLocalSystem>(pNode);
160  }
161 
162  void PairingInfo(std::ostream& rOStream, const int EchoLevel) const override;
163 
164  void SetPairingStatusForPrinting() override;
165 
166  bool IsDoneSearching() const override;
167 
168 private:
169  NodePointerType mpNode;
171 
172 };
173 
175 
183 template<class TSparseSpace, class TDenseSpace, class TMapperBackend>
184 class KRATOS_API(MAPPING_APPLICATION) NearestElementMapper
185  : public InterpolativeMapperBase<TSparseSpace, TDenseSpace, TMapperBackend>
186 {
187 public:
190 
193 
197 
201 
202  // Default constructor, needed for registration
203  NearestElementMapper(ModelPart& rModelPartOrigin,
204  ModelPart& rModelPartDestination)
205  : BaseType(rModelPartOrigin,rModelPartDestination) {}
206 
207  NearestElementMapper(ModelPart& rModelPartOrigin,
208  ModelPart& rModelPartDestination,
209  Parameters JsonParameters)
210  : BaseType(rModelPartOrigin,
211  rModelPartDestination,
212  JsonParameters)
213  {
214  KRATOS_TRY;
215 
216  this->ValidateInput();
217 
218  const bool use_approximation = JsonParameters["use_approximation"].GetBool();
219  const double local_coord_tol = JsonParameters["local_coord_tolerance"].GetDouble();
220  KRATOS_ERROR_IF(local_coord_tol < 0.0) << "The local-coord-tolerance cannot be negative" << std::endl;
221 
222  mOptions = NearestElementOptions{use_approximation, local_coord_tol};
223 
224  this->Initialize();
225 
226  KRATOS_CATCH("");
227  }
228 
230  ~NearestElementMapper() override = default;
231 
235 
237  ModelPart& rModelPartDestination,
238  Parameters JsonParameters) const override
239  {
240  KRATOS_TRY;
241 
242  return Kratos::make_unique<NearestElementMapper<TSparseSpace, TDenseSpace, TMapperBackend>>(
243  rModelPartOrigin,
244  rModelPartDestination,
245  JsonParameters);
246 
247  KRATOS_CATCH("");
248  }
249 
253 
257 
259  std::string Info() const override
260  {
261  return "NearestElementMapper";
262  }
263 
265  void PrintInfo(std::ostream& rOStream) const override
266  {
267  rOStream << "NearestElementMapper";
268  }
269 
271  void PrintData(std::ostream& rOStream) const override
272  {
273  BaseType::PrintData(rOStream);
274  }
275 
276 private:
279 
280  NearestElementOptions mOptions;;
281 
283 
286 
287  void CreateMapperLocalSystems(
288  const Communicator& rModelPartCommunicator,
289  std::vector<Kratos::unique_ptr<MapperLocalSystem>>& rLocalSystems) override
290  {
292  NearestElementLocalSystem(nullptr),
293  rModelPartCommunicator,
294  rLocalSystems);
295  }
296 
297  MapperInterfaceInfoUniquePointerType GetMapperInterfaceInfo() const override
298  {
299  return Kratos::make_unique<NearestElementInterfaceInfo>(mOptions);
300  }
301 
302  Parameters GetMapperDefaultSettings() const override
303  {
304  return Parameters( R"({
305  "search_settings" : {},
306  "local_coord_tolerance" : 0.25,
307  "use_approximation" : true,
308  "use_initial_configuration" : false,
309  "echo_level" : 0,
310  "print_pairing_status_to_file" : false,
311  "pairing_status_file_path" : ""
312  })");
313  }
314 
316 
317 }; // Class NearestElementMapper
318 
319 } // namespace Kratos.
The Commmunicator class manages communication for distributed ModelPart instances.
Definition: communicator.h:67
Object used by the bin-search.
Definition: interface_object.h:40
ConstructionType
Definition: interface_object.h:63
Definition: interpolative_mapper_base.h:81
Base Class for all Mappers.
Definition: mapper.h:43
Kratos::unique_ptr< Mapper > MapperUniquePointerType
Definition: mapper.h:53
Object for storing data that is needed to construct the local-mapping-system.
Definition: mapper_interface_info.h:42
InfoType
Definition: mapper_interface_info.h:62
std::size_t IndexType
Definition: mapper_interface_info.h:50
InterfaceObject::CoordinatesArrayType CoordinatesArrayType
Definition: mapper_interface_info.h:52
This is the "Condition" of the mappers.
Definition: mapper_local_system.h:39
PairingStatus
Definition: mapper_local_system.h:63
MapperInterfaceInfo::CoordinatesArrayType CoordinatesArrayType
Definition: mapper_local_system.h:50
Kratos::unique_ptr< MapperLocalSystem > MapperLocalSystemUniquePointer
Definition: mapper_local_system.h:48
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Definition: nearest_element_mapper.h:39
NearestElementInterfaceInfo(const CoordinatesArrayType &rCoordinates, const IndexType SourceLocalSystemIndex, const IndexType SourceRank, const NearestElementOptions Options={})
Definition: nearest_element_mapper.h:45
void GetValue(int &rValue, const InfoType ValueType) const override
Definition: nearest_element_mapper.h:94
std::size_t GetNumSearchResults() const
Definition: nearest_element_mapper.h:100
InterfaceObject::ConstructionType GetInterfaceObjectType() const override
returning the type of construction for the InterfaceObject The returned type is used to create the ob...
Definition: nearest_element_mapper.h:67
void GetValue(double &rValue, const InfoType ValueType) const override
Definition: nearest_element_mapper.h:88
MapperInterfaceInfo::Pointer Create(const CoordinatesArrayType &rCoordinates, const IndexType SourceLocalSystemIndex, const IndexType SourceRank) const override
Definition: nearest_element_mapper.h:56
NearestElementInterfaceInfo(const NearestElementOptions Options={})
Default constructor.
Definition: nearest_element_mapper.h:43
void GetValue(std::vector< int > &rValue, const InfoType ValueType) const override
Definition: nearest_element_mapper.h:76
void GetValue(std::vector< double > &rValue, const InfoType ValueType) const override
Definition: nearest_element_mapper.h:82
MapperInterfaceInfo::Pointer Create() const override
Definition: nearest_element_mapper.h:51
Definition: nearest_element_mapper.h:141
NearestElementLocalSystem(NodePointerType pNode)
Definition: nearest_element_mapper.h:144
CoordinatesArrayType & Coordinates() const override
Definition: nearest_element_mapper.h:151
MapperLocalSystemUniquePointer Create(NodePointerType pNode) const override
Definition: nearest_element_mapper.h:157
Interpolative Mapper.
Definition: nearest_element_mapper.h:186
NearestElementMapper(ModelPart &rModelPartOrigin, ModelPart &rModelPartDestination)
Definition: nearest_element_mapper.h:203
BaseType::MapperInterfaceInfoUniquePointerType MapperInterfaceInfoUniquePointerType
Definition: nearest_element_mapper.h:196
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: nearest_element_mapper.h:271
~NearestElementMapper() override=default
Destructor.
MapperUniquePointerType Clone(ModelPart &rModelPartOrigin, ModelPart &rModelPartDestination, Parameters JsonParameters) const override
Cloning the Mapper returns a clone of the current Mapper pure virtual, has to be implemented in every...
Definition: nearest_element_mapper.h:236
KRATOS_CLASS_POINTER_DEFINITION(NearestElementMapper)
Pointer definition of NearestElementMapper.
BaseType::MapperUniquePointerType MapperUniquePointerType
Definition: nearest_element_mapper.h:195
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: nearest_element_mapper.h:265
InterpolativeMapperBase< TSparseSpace, TDenseSpace, TMapperBackend > BaseType
Definition: nearest_element_mapper.h:194
NearestElementMapper(ModelPart &rModelPartOrigin, ModelPart &rModelPartDestination, Parameters JsonParameters)
Definition: nearest_element_mapper.h:207
std::string Info() const override
Turn back information as a string.
Definition: nearest_element_mapper.h:259
This class defines the node.
Definition: node.h:65
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
bool GetBool() const
This method returns the boolean contained in the current Parameter.
Definition: kratos_parameters.cpp:675
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void load(std::string const &rTag, TDataType &rObject)
Definition: serializer.h:207
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
#define KRATOS_SERIALIZE_SAVE_BASE_CLASS(Serializer, BaseType)
Definition: define.h:812
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_SERIALIZE_LOAD_BASE_CLASS(Serializer, BaseType)
Definition: define.h:815
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_DEBUG_ERROR_IF_NOT(conditional)
Definition: exception.h:172
static int EchoLevel
Definition: co_sim_EMPIRE_API.h:42
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
static double max(double a, double b)
Definition: GeometryFunctions.h:79
void CreateMapperLocalSystemsFromNodes(const MapperLocalSystem &rMapperLocalSystemPrototype, const Communicator &rModelPartCommunicator, std::vector< Kratos::unique_ptr< MapperLocalSystem >> &rLocalSystems)
Definition: mapper_utilities.cpp:236
Kratos::unique_ptr< MapperInterfaceInfo > MapperInterfaceInfoUniquePointerType
Definition: mapper_utilities.h:40
NodeType::Pointer NodePointerType
Definition: mapping_intersection_utilities.h:36
PairingIndex
Definition: projection_utilities.h:32
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
REACTION_CHECK_STIFFNESS_FACTOR int
Definition: contact_structural_mechanics_application_variables.h:75
def load(f)
Definition: ode_solve.py:307
float temp
Definition: rotating_cone.py:85
Options for configuring the behavior of the Mapper.
Definition: nearest_element_mapper.h:33
bool UseApproximation
Definition: nearest_element_mapper.h:34
double LocalCoordTol
Definition: nearest_element_mapper.h:35