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.
kratos_components.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: Pooyan Dadvand
11 // Riccardo Rossi
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 #include <map>
20 #include <typeinfo>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/define.h"
27 #include "containers/flags.h"
28 #include "containers/variable.h"
29 #include "utilities/quaternion.h"
30 
31 namespace Kratos
32 {
33 
47 template<class TComponentType>
48 class KRATOS_API(KRATOS_CORE) KratosComponents
49 {
50 public:
51 
54 
57 
59  using ComponentsContainerType = std::map<std::string, const TComponentType*>;
60 
62  using ValueType = typename ComponentsContainerType::value_type;
63 
67 
69  KratosComponents() = default;
70 
72  virtual ~KratosComponents() = default;
73 
77 
81 
88  static void Add(const std::string& rName, const TComponentType& rComponent)
89  {
90  // Check if a different object was already registered with this name, since this is undefined behavior
91  auto it_comp = msComponents.find(rName);
92  KRATOS_ERROR_IF(it_comp != msComponents.end() && typeid(*(it_comp->second)) != typeid(rComponent)) << "An object of different type was already registered with name \"" << rName << "\"!" << std::endl;
93  msComponents.insert(ValueType(rName , &rComponent));
94  }
95 
101  static void Remove(const std::string& rName)
102  {
103  std::size_t num_erased = msComponents.erase(rName);
104  KRATOS_ERROR_IF(num_erased == 0) << "Trying to remove inexistent component \"" << rName << "\"." << std::endl;
105  }
106 
114  static const TComponentType& Get(const std::string& rName)
115  {
116  auto it_comp = msComponents.find(rName);
117  KRATOS_DEBUG_ERROR_IF(it_comp == msComponents.end()) << GetMessageUnregisteredComponent(rName) << std::endl;
118  return *(it_comp->second);
119  }
120 
124  static void Register()
125  {
126 
127  }
128 
132 
139  {
140  return msComponents;
141  }
142 
148  {
149  return &msComponents;
150  }
151 
155 
161  static bool Has(const std::string& rName)
162  {
163  return (msComponents.find(rName) != msComponents.end());
164  }
165 
169 
171  virtual std::string Info() const
172  {
173  return "Kratos components";
174  }
175 
177  virtual void PrintInfo(std::ostream& rOStream) const
178  {
179  rOStream << "Kratos components";
180  }
181 
183  virtual void PrintData(std::ostream& rOStream) const
184  {
185  for (const auto& r_comp : msComponents) {
186  rOStream << " " << r_comp.first << std::endl;
187  }
188  }
189 
193 
195 private:
198 
199  static ComponentsContainerType msComponents;
200 
204 
208 
212 
218  static std::string GetMessageUnregisteredComponent(const std::string& rName)
219  {
220  std::stringstream msg;
221  msg << "The component \"" << rName << "\" is not registered!\nMaybe you need to import the application where it is defined?\nThe following components of this type are registered:" << std::endl;
222  KratosComponents instance; // creating an instance for using "PrintData"
223  instance.PrintData(msg);
224  return msg.str();
225  }
226 
230 
234 
238 
240  KratosComponents& operator=(const KratosComponents& rOther);
241 
243  KratosComponents(const KratosComponents& rOther);
244 
246 
247 }; // Class KratosComponents
248 
249 
251 template<>
252 class KRATOS_API(KRATOS_CORE) KratosComponents<VariableData>
253 {
254 public:
255 
258 
261 
263  using ComponentsContainerType = std::map<std::string, VariableData*>;
264 
266  using ValueType = ComponentsContainerType::value_type;
267 
271 
273  KratosComponents() = default;
274 
276  virtual ~KratosComponents() = default;
277 
281 
285 
291  static void Add(const std::string& rName, VariableData& rComponent)
292  {
293  msComponents.insert(ValueType(rName, &rComponent));
294  }
295 
300  static void Remove(const std::string& rName)
301  {
302  std::size_t num_erased = msComponents.erase(rName);
303  KRATOS_ERROR_IF(num_erased == 0) << "Trying to remove inexistent component \"" << rName << "\"." << std::endl;
304  }
305 
310  static std::size_t Size()
311  {
312  return msComponents.size();
313  }
314 
322  static VariableData& Get(const std::string& rName)
323  {
324  auto it_comp = msComponents.find(rName);
325  KRATOS_DEBUG_ERROR_IF(it_comp == msComponents.end()) << GetMessageUnregisteredVariable(rName) << std::endl;
326  return *(it_comp->second);
327  }
328 
334  static VariableData* pGet(const std::string& rName)
335  {
336  auto it_comp = msComponents.find(rName);
337  KRATOS_DEBUG_ERROR_IF(it_comp == msComponents.end()) << GetMessageUnregisteredVariable(rName) << std::endl;
338  return it_comp->second;
339  }
340 
344  static void Register()
345  {
346 
347  }
348 
352 
359  {
360  return msComponents;
361  }
362 
368  {
369  return &msComponents;
370  }
371 
375 
381  static bool Has(const std::string& rName)
382  {
383  return (msComponents.find(rName) != msComponents.end());
384  }
385 
389 
391  virtual std::string Info() const
392  {
393  return "Kratos components <VariableData>";
394  }
395 
397  virtual void PrintInfo(std::ostream& rOStream) const
398  {
399  rOStream << "Kratos components <VariableData>";
400  }
401 
403  virtual void PrintData(std::ostream& rOStream) const
404  {
405  for (const auto& r_comp : msComponents) {
406  rOStream << " " << r_comp.first << std::endl;
407  }
408  }
409 
413 
415 private:
416 
419 
420  static ComponentsContainerType msComponents;
421 
425 
429 
433 
439  static std::string GetMessageUnregisteredVariable(const std::string& rName)
440  {
441  std::stringstream msg;
442  msg << "The variable \"" << rName << "\" is not registered!\nMaybe you need to import the application where it is defined?\nThe following variables are registered:" << std::endl;
443  KratosComponents instance; // creating an instance for using "PrintData"
444  instance.PrintData(msg);
445  return msg.str();
446  }
447 
451 
455 
459 
461  KratosComponents& operator=(const KratosComponents& rOther);
462 
464  KratosComponents(const KratosComponents& rOther);
465 
467 
468 }; // Class KratosComponents
469 
470 template<class TComponentType>
471 typename KratosComponents<TComponentType>::ComponentsContainerType KratosComponents<TComponentType>::msComponents;
472 
488 
491 
493 template<class TComponentType>
494 inline std::ostream& operator << (std::ostream& rOStream,
496 {
497  rThis.PrintInfo(rOStream);
498  rOStream << std::endl;
499  rThis.PrintData(rOStream);
500 
501  return rOStream;
502 }
504 
505 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<bool>& rComponent);
506 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<int>& rComponent);
507 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<unsigned int>& rComponent);
508 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<double>& rComponent);
509 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<array_1d<double, 3>>& rComponent);
510 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<array_1d<double, 4>>& rComponent);
511 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<array_1d<double, 6>>& rComponent);
512 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<array_1d<double, 9>>& rComponent);
513 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<Quaternion<double>>& rComponent);
514 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<Vector>& rComponent);
515 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<Matrix>& rComponent);
516 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<std::string>& rComponent);
517 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Flags& rComponent);
518 void KRATOS_API(KRATOS_CORE) AddKratosComponent(const std::string& rName, const Variable<Flags>& rComponent);
519 
520 template<class TComponentType> void AddKratosComponent(const std::string& rName, const TComponentType& rComponent)
521 {
522 }
523 
524 } // namespace Kratos.
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
Definition: flags.h:58
static void Add(const std::string &rName, VariableData &rComponent)
Adds a new element to the msComponents map.
Definition: kratos_components.h:291
virtual ~KratosComponents()=default
Destructor.
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: kratos_components.h:403
static bool Has(const std::string &rName)
Checks if the specified name exists in the set of components.
Definition: kratos_components.h:381
static void Register()
Registers the function.
Definition: kratos_components.h:344
std::map< std::string, VariableData * > ComponentsContainerType
The map type used to store the components // TODO: Replace std::map with faster alternative.
Definition: kratos_components.h:263
static std::size_t Size()
Get the size of the components.
Definition: kratos_components.h:310
static ComponentsContainerType * pGetComponents()
Returns a pointer to the ComponentsContainerType object.
Definition: kratos_components.h:367
static ComponentsContainerType & GetComponents()
Retrieves the ComponentsContainer.
Definition: kratos_components.h:358
ComponentsContainerType::value_type ValueType
Component type.
Definition: kratos_components.h:266
static VariableData & Get(const std::string &rName)
Retrieves the VariableData with the specified name.
Definition: kratos_components.h:322
static void Remove(const std::string &rName)
Remove a component from the list by name.
Definition: kratos_components.h:300
KRATOS_CLASS_POINTER_DEFINITION(KratosComponents)
Pointer definition of KratosComponents.
virtual std::string Info() const
Turn back information as a string.
Definition: kratos_components.h:391
KratosComponents()=default
Default constructor.
static VariableData * pGet(const std::string &rName)
Retrieves the variable data associated with the given name.
Definition: kratos_components.h:334
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: kratos_components.h:397
KratosComponents class encapsulates a lookup table for a family of classes in a generic way.
Definition: kratos_components.h:49
static void Add(const std::string &rName, const TComponentType &rComponent)
Adds a component to the collection.
Definition: kratos_components.h:88
KratosComponents()=default
Default constructor.
static ComponentsContainerType & GetComponents()
Retrieves the ComponentsContainer.
Definition: kratos_components.h:138
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: kratos_components.h:177
static void Register()
Registers the function.
Definition: kratos_components.h:124
static bool Has(const std::string &rName)
Check if the given name exists in the set of components.
Definition: kratos_components.h:161
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: kratos_components.h:183
typename ComponentsContainerType::value_type ValueType
Component type.
Definition: kratos_components.h:62
virtual std::string Info() const
Turn back information as a string.
Definition: kratos_components.h:171
virtual ~KratosComponents()=default
Destructor.
static void Remove(const std::string &rName)
Removes a component with the specified name.
Definition: kratos_components.h:101
static ComponentsContainerType * pGetComponents()
Retrieves the pointer to the ComponentsContainerType object.
Definition: kratos_components.h:147
static const TComponentType & Get(const std::string &rName)
Retrieves a component with the specified name.
Definition: kratos_components.h:114
KRATOS_CLASS_POINTER_DEFINITION(KratosComponents)
Pointer definition of KratosComponents.
std::map< std::string, const TComponentType * > ComponentsContainerType
The map type used to store the components // TODO: Replace std::map with faster alternative.
Definition: kratos_components.h:59
This class is the base of variables and variable's components which contains their common data.
Definition: variable_data.h:49
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
#define KRATOS_API_EXTERN
Definition: kratos_export_api.h:57
#define KRATOS_API(...)
Definition: kratos_export_api.h:40
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void AddKratosComponent(std::string const &Name, ExplicitBuilderType const &ThisComponent)
Definition: register_factories.cpp:23
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432