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_factory.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 #if !defined(KRATOS_MAPPER_FACTORY_H_INCLUDED )
17 #define KRATOS_MAPPER_FACTORY_H_INCLUDED
18 
19 // System includes
20 #include <unordered_map>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/define.h"
27 #include "containers/model.h"
28 
29 #include "mappers/mapper.h"
30 
31 namespace Kratos
32 {
35 
38 
40 
43 template<class TSparseSpace, class TDenseSpace>
44 class KRATOS_API(KRATOS_CORE) MapperFactory
45 {
46 public:
49 
52 
56 
58  virtual ~MapperFactory() = default;
59 
63 
65  ModelPart& rModelPartOrigin,
66  ModelPart& rModelPartDestination,
67  Parameters MapperSettings)
68  {
69  ModelPart& r_interface_model_part_origin = GetInterfaceModelPart(rModelPartOrigin, MapperSettings, "origin");
70  ModelPart& r_interface_model_part_destination = GetInterfaceModelPart(rModelPartDestination, MapperSettings, "destination");
71 
72  KRATOS_ERROR_IF(!TSparseSpace::IsDistributed() && (r_interface_model_part_origin.IsDistributed() || r_interface_model_part_destination.IsDistributed())) << "Trying to construct a non-MPI Mapper with a distributed ModelPart. Please use \"CreateMPIMapper\" instead!" << std::endl;
73 
74  KRATOS_ERROR_IF(TSparseSpace::IsDistributed() && !r_interface_model_part_origin.IsDistributed() && !r_interface_model_part_destination.IsDistributed()) << "Trying to construct a MPI Mapper without a distributed ModelPart. Please use \"CreateMapper\" instead!" << std::endl;
75 
76  const std::string mapper_name = MapperSettings["mapper_type"].GetString();
77 
78  const auto& mapper_list = GetRegisteredMappersList();
79 
80  if (mapper_list.find(mapper_name) != mapper_list.end()) {
81  // Removing Parameters that are not needed by the Mapper
82  MapperSettings.RemoveValue("mapper_type");
83  MapperSettings.RemoveValue("interface_submodel_part_origin");
84  MapperSettings.RemoveValue("interface_submodel_part_destination");
85 
86  // TODO check why this works, Clone currently returns a unique ptr!!!
87  return mapper_list.at(mapper_name)->Clone(r_interface_model_part_origin,
88  r_interface_model_part_destination,
89  MapperSettings);
90  }
91  else {
92  std::stringstream err_msg;
93  err_msg << "The requested Mapper \"" << mapper_name <<"\" is not not available!\n"
94  << "The following Mappers are available:" << std::endl;
95 
96  for (auto const& registered_mapper : mapper_list)
97  err_msg << "\t" << registered_mapper.first << "\n";
98 
99  KRATOS_ERROR << err_msg.str() << std::endl;
100  }
101  }
102 
103  static void Register(const std::string& rMapperName,
104  typename Mapper<TSparseSpace, TDenseSpace>::Pointer pMapperPrototype)
105  {
106  GetRegisteredMappersList().insert(
107  std::make_pair(rMapperName, pMapperPrototype));
108  }
109 
110  static bool HasMapper(const std::string& rMapperName)
111  {
112  const auto& mapper_list = GetRegisteredMappersList();
113  return mapper_list.find(rMapperName) != mapper_list.end();
114  }
115 
116  static std::vector<std::string> GetRegisteredMapperNames()
117  {
118  const auto& mapper_list = GetRegisteredMappersList();
119 
120  std::vector<std::string> mapper_names;
121 
122  mapper_names.reserve(mapper_list.size());
123  for (auto const& r_registered_mapper : mapper_list) {
124  mapper_names.push_back(r_registered_mapper.first);
125  }
126 
127  return mapper_names;
128  }
129 
133 
135  virtual std::string Info() const
136  {
137  std::stringstream buffer;
138  buffer << "MapperFactory" ;
139  return buffer.str();
140  }
141 
143  virtual void PrintInfo(std::ostream& rOStream) const
144  {
145  rOStream << "MapperFactory";
146  }
147 
149  virtual void PrintData(std::ostream& rOStream) const {}
150 
152 
153 private:
156 
158  MapperFactory() {}
159 
160  static ModelPart& GetInterfaceModelPart(ModelPart& rModelPart,
161  const Parameters InterfaceParameters,
162  const std::string& InterfaceSide)
163  {
164  const int echo_level = InterfaceParameters.Has("echo_level") ? InterfaceParameters["echo_level"].GetInt() : 0;
165 
166  const std::string key_sub_model_part = "interface_submodel_part_" + InterfaceSide;
167 
168  if (InterfaceParameters.Has(key_sub_model_part)) {
169  const std::string name_interface_submodel_part = rModelPart.FullName() + "." + InterfaceParameters[key_sub_model_part].GetString();
170  KRATOS_INFO_IF("MapperFactory", echo_level >= 3) << "Mapper: SubModelPart used for " << InterfaceSide << "-ModelPart" << std::endl;
171 
172  return rModelPart.GetModel().GetModelPart(name_interface_submodel_part);
173  } else {
174  KRATOS_INFO_IF("MapperFactory", echo_level >= 3) << "Mapper: Main ModelPart used for " << InterfaceSide << "-ModelPart" << std::endl;
175 
176  return rModelPart;
177  }
178  }
179 
180  static std::unordered_map<std::string, typename Mapper<TSparseSpace,
181  TDenseSpace>::Pointer>& GetRegisteredMappersList();
182 
184 
185 }; // Class MapperFactory
186 
188 
190 
191 } // namespace Kratos.
192 
193 #endif // KRATOS_MAPPER_FACTORY_H_INCLUDED defined
Python Interface of the MappingApplication.
Definition: mapper_factory.h:45
virtual ~MapperFactory()=default
Destructor.
KRATOS_CLASS_POINTER_DEFINITION(MapperFactory)
Pointer definition of MapperFactory.
static Mapper< TSparseSpace, TDenseSpace >::Pointer CreateMapper(ModelPart &rModelPartOrigin, ModelPart &rModelPartDestination, Parameters MapperSettings)
Definition: mapper_factory.h:64
virtual std::string Info() const
Turn back information as a string.
Definition: mapper_factory.h:135
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: mapper_factory.h:149
static bool HasMapper(const std::string &rMapperName)
Definition: mapper_factory.h:110
static std::vector< std::string > GetRegisteredMapperNames()
Definition: mapper_factory.h:116
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: mapper_factory.h:143
static void Register(const std::string &rMapperName, typename Mapper< TSparseSpace, TDenseSpace >::Pointer pMapperPrototype)
Definition: mapper_factory.h:103
Base Class for all Mappers.
Definition: mapper.h:43
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
bool IsDistributed() const
Definition: model_part.h:1898
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
bool RemoveValue(const std::string &rEntry)
This method removes an entry of the Parameters given a certain key.
Definition: kratos_parameters.cpp:482
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
Mapper
Definition: MappingApplication.py:39
MapperFactory
Definition: MappingApplication.py:40
string err_msg
Definition: fluid_chimera_analysis.py:21
echo_level
Definition: script.py:68