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.
proxies.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: Máté Kelemen
11 //
12 
13 #pragma once
14 
15 // Project includes
16 #include "includes/global_variables.h" // DataLocation
17 #include "includes/kratos_export_api.h" // KRATOS_API
18 #include "includes/node.h" // Node
19 #include "includes/element.h" // Element
20 #include "includes/condition.h" // Condition
21 #include "includes/model_part.h" // ModelPart::NodesContainerType, ModelPart::ElementsContainerType, ModelPart::ConditionsContainerType
22 #include "utilities/variable_utils.h" // VariableUtils::HasValue, VariableUtils::GetValue, VariableUtils::SetValue
23 #include "utilities/model_part_utils.h" // ModelPartUtils::GetContainer
24 
25 // System includes
26 #include <type_traits> // remove_reference_t, is_const_v, is_same_v, decay_t
27 #include <optional> // optional
28 
29 
30 namespace Kratos {
31 
32 
33 template <class TEntityProxy>
34 class ContainerProxy;
35 
36 
37 
51 template <Globals::DataLocation TLocation, bool TMutable>
53 {
54 private:
55  constexpr static Globals::DataLocation Location = TLocation;
56 
57  constexpr static bool IsMutable = TMutable;
58 
60  using UnqualifiedEntity = std::conditional_t<
62  Node,
63  std::conditional_t<
64  TLocation == Globals::DataLocation::Element,
65  Element,
66  std::conditional_t<
68  Condition,
69  std::conditional_t<
72  std::conditional_t<
74  ModelPart,
75  void // <== invalid fallback type; will throw a compile-time error
76  >
77  >
78  >
79  >
80  >;
81 
83  using QualifiedEntity = std::conditional_t<TMutable,
84  UnqualifiedEntity,
85  const UnqualifiedEntity>;
86 
88  friend class ContainerProxy<EntityProxy>;
89 
90 public:
92  EntityProxy() = delete;
93 
98  EntityProxy(QualifiedEntity& rEntity) noexcept : mpEntity(&rEntity) {}
99 
101  template <class TValue>
102  bool HasValue(const Variable<TValue>& rVariable) const noexcept
103  {
104  return VariableUtils::HasValue<TLocation>(*mpEntity, rVariable);
105  }
106 
108  template <class TValue>
109  std::conditional_t<std::is_integral_v<TValue> || std::is_floating_point_v<TValue>,
110  TValue, // <== return by value if scalar type
111  const TValue&> // <== return by reference in non-scalar type
112  GetValue(const Variable<TValue>& rVariable) const
113  {
114  return VariableUtils::GetValue<TLocation>(*mpEntity, rVariable);
115  }
116 
118  template <class TValue, std::enable_if_t</*this is required for SFINAE*/!std::is_same_v<TValue,void> && TMutable,bool> = true>
119  TValue& GetValue(const Variable<TValue>& rVariable)
120  {
121  return VariableUtils::GetValue<TLocation>(*mpEntity, rVariable);
122  }
123 
125  template <class TValue, std::enable_if_t</*this is required for SFINAE*/!std::is_same_v<TValue,void> && TMutable,bool> = true>
126  void SetValue(const Variable<TValue>& rVariable,
127  std::conditional_t<std::is_integral_v<TValue> || std::is_floating_point_v<TValue>,
128  TValue, /*pass scalar types by value*/
129  const TValue&> /*pass non-scalar types by reference*/ Value)
130  {
131  VariableUtils::SetValue<TLocation>(*mpEntity, rVariable, Value);
132  }
133 
135  const UnqualifiedEntity& GetEntity() const
136  {
137  return *mpEntity;
138  }
139 
141  QualifiedEntity& GetEntity()
142  {
143  return *mpEntity;
144  }
145 
146 private:
147  QualifiedEntity* mpEntity;
148 }; // class EntityProxy
149 
150 
151 
160 template <class TEntityProxy>
162 {
163 private:
164  using UnqualifiedContainer = std::conditional_t<
165  std::is_same_v<typename TEntityProxy::UnqualifiedEntity,Node>,
167  std::conditional_t<
168  std::is_same_v<typename TEntityProxy::UnqualifiedEntity,Element>,
170  std::conditional_t<
171  std::is_same_v<typename TEntityProxy::UnqualifiedEntity,Condition>,
173  void // <== invalid fallback type; will throw a compile-time error
174  >
175  >
176  >;
177 
178  constexpr static bool IsMutable = TEntityProxy::IsMutable;
179 
180  using WrappedIterator = std::conditional_t<IsMutable,
181  typename UnqualifiedContainer::iterator,
182  typename UnqualifiedContainer::const_iterator>;
183 
184  template <bool TMutable>
185  class Iterator
186  {
187  private:
188  using Wrapped = std::conditional_t<TMutable,
189  typename UnqualifiedContainer::iterator,
190  typename UnqualifiedContainer::const_iterator>;
191 
192  public:
194 
195  using pointer = std::conditional_t<TMutable,
196  value_type*,
197  const value_type*>;
198 
199  using reference = std::conditional_t<TMutable,
200  value_type&,
201  const value_type&>;
202 
203  using difference_type = std::ptrdiff_t;
204 
205  using iterator_category = std::random_access_iterator_tag;
206 
207  Iterator() noexcept = default;
208 
209  Iterator(Wrapped It) noexcept : mWrapped(It) {}
210 
211  value_type operator*() const noexcept {return value_type(*mWrapped);}
212 
213  Iterator& operator++() noexcept {++mWrapped; return *this;}
214 
215  Iterator operator++(int) noexcept {Iterator copy(mWrapped); ++mWrapped; return copy;}
216 
217  Iterator& operator--() noexcept {--mWrapped; return *this;}
218 
219  Iterator operator--(int) noexcept {Iterator copy(mWrapped); --mWrapped; return copy;}
220 
221  Iterator& operator+=(difference_type Rhs) noexcept {mWrapped += Rhs; return *this;}
222 
223  Iterator& operator-=(difference_type Rhs) noexcept {mWrapped -= Rhs; return *this;}
224 
225  Iterator operator+(difference_type Rhs) const noexcept {Iterator copy(mWrapped); copy += Rhs; return copy;}
226 
227  Iterator operator-(difference_type Rhs) const noexcept {Iterator copy(mWrapped); copy -= Rhs; return copy;}
228 
229  difference_type operator-(Iterator Rhs) const noexcept {return mWrapped - Rhs.mWrapped;}
230 
231  bool operator==(Iterator Rhs) const noexcept {return mWrapped == Rhs.mWrapped;}
232 
233  bool operator!=(Iterator Rhs) const noexcept {return mWrapped != Rhs.mWrapped;}
234 
235  bool operator<(Iterator Rhs) const noexcept {return mWrapped < Rhs.mWrapped;}
236 
237  bool operator>(Iterator Rhs) const noexcept {return mWrapped > Rhs.mWrapped;}
238 
239  bool operator<=(Iterator Rhs) const noexcept {return mWrapped <= Rhs.mWrapped;}
240 
241  bool operator>=(Iterator Rhs) const noexcept {return mWrapped >= Rhs.mWrapped;}
242 
243  private:
244  Wrapped mWrapped;
245  }; // class Iterator
246 public:
247  using iterator = Iterator<IsMutable>;
248 
249  using const_iterator = Iterator<false>;
250 
251  using size_type = std::size_t;
252 
254 
255  ContainerProxy() noexcept = default;
256 
257  ContainerProxy(WrappedIterator Begin, WrappedIterator End) noexcept
258  : mBegin(Begin),
259  mEnd(End)
260  {}
261 
262  typename const_iterator::value_type operator[](size_type Index) const noexcept {return typename const_iterator::value_type(*(mBegin + Index));}
263 
264  typename iterator::value_type operator[](size_type Index) noexcept {return typename iterator::value_type(*(mBegin + Index));}
265 
266  typename const_iterator::value_type at(size_type Index) const noexcept {return typename const_iterator::value_type(*(mBegin + Index));}
267 
268  typename iterator::value_type at(size_type Index) noexcept {return typename iterator::value_type(*(mBegin + Index));}
269 
270  size_type size() const noexcept {return std::distance(mBegin, mEnd);}
271 
272  bool empty() const noexcept {return this->size() == 0;}
273 
274  const_iterator cbegin() const noexcept {return const_iterator(mBegin);}
275 
276  const_iterator begin() const noexcept {return this->cbegin();}
277 
278  iterator begin() noexcept {return iterator(mBegin);}
279 
280  const_iterator cend() const noexcept {return const_iterator(mEnd);}
281 
282  const_iterator end() const noexcept {return this->cend();}
283 
284  iterator end() noexcept {return iterator(mEnd);}
285 
286 private:
287  WrappedIterator mBegin, mEnd;
288 }; // class ContainerProxy
289 
290 
291 
293 template <Globals::DataLocation TLocation, class TEntity>
294 inline auto MakeProxy(const TEntity& rEntity)
295 {
296  static_assert(std::is_same_v<TEntity,void>, "Invalid DataLocation-Entity combination");
297 }
298 
299 
301 template <Globals::DataLocation TLocation, class TEntity>
302 inline auto MakeProxy(TEntity& rEntity)
303 {
304  static_assert(std::is_same_v<TEntity,void>, "Invalid DataLocation-Entity combination");
305 }
306 
307 
308 #define KRATOS_DEFINE_ENTITY_PROXY_FACTORY(TLocation, TEntity) \
309  \
310  template <> \
311  inline auto MakeProxy<TLocation,TEntity>(const TEntity& rEntity) \
312  {return EntityProxy<TLocation,false>(rEntity);} \
313  \
314  template <> \
315  inline auto MakeProxy<TLocation,TEntity>(TEntity& rEntity) \
316  {return EntityProxy<TLocation,true>(rEntity);}
317 
319 
321 
323 
325 
327 
329 
330 #undef KRATOS_DEFINE_ENTITY_PROXY_FACTORY
331 
332 
334 template <>
335 inline auto MakeProxy<Globals::DataLocation::ProcessInfo,ModelPart>(const ModelPart& rModelPart)
336 {
338 }
339 
340 
342 template <>
343 inline auto MakeProxy<Globals::DataLocation::ProcessInfo,ModelPart>(ModelPart& rModelPart)
344 {
346 }
347 
348 
349 #define KRATOS_DEFINE_CONTAINER_PROXY_FACTORY(TLocation) \
350  \
351  template <> \
352  inline auto MakeProxy<TLocation,ModelPart>(const ModelPart& rModelPart) \
353  { \
354  const auto& r_container = ModelPartUtils::GetContainer<TLocation>(rModelPart); \
355  return ContainerProxy<EntityProxy<TLocation,false>>(r_container.begin(), \
356  r_container.end()); \
357  } \
358  \
359  template <> \
360  inline auto MakeProxy<TLocation,ModelPart>(ModelPart& rModelPart) \
361  { \
362  auto& r_container = ModelPartUtils::GetContainer<TLocation>(rModelPart); \
363  return ContainerProxy<EntityProxy<TLocation,true>>(r_container.begin(), \
364  r_container.end()); \
365  }
366 
368 
370 
372 
374 
375 #undef KRATOS_DEFINE_CONTAINER_PROXY_FACTORY
376 
377 
378 } // namespace Kratos
Base class for all Conditions.
Definition: condition.h:59
A view with a uniform interface for ModelPart::NodesContainerType, ModelPart::ElementsContainerType,...
Definition: proxies.h:162
const_iterator end() const noexcept
Definition: proxies.h:282
const_iterator cend() const noexcept
Definition: proxies.h:280
size_type size() const noexcept
Definition: proxies.h:270
iterator::value_type at(size_type Index) noexcept
Definition: proxies.h:268
const_iterator::value_type at(size_type Index) const noexcept
Definition: proxies.h:266
Iterator< false > const_iterator
Definition: proxies.h:249
bool empty() const noexcept
Definition: proxies.h:272
const_iterator::value_type operator[](size_type Index) const noexcept
Definition: proxies.h:262
Iterator< IsMutable > iterator
Definition: proxies.h:247
const_iterator begin() const noexcept
Definition: proxies.h:276
typename iterator::value_type value_type
Definition: proxies.h:253
ContainerProxy() noexcept=default
std::size_t size_type
Definition: proxies.h:251
iterator end() noexcept
Definition: proxies.h:284
iterator::value_type operator[](size_type Index) noexcept
Definition: proxies.h:264
const_iterator cbegin() const noexcept
Definition: proxies.h:274
iterator begin() noexcept
Definition: proxies.h:278
Base class for all Elements.
Definition: element.h:60
Wrapper class providing a uniform interface for historical/non-historical Node, Element,...
Definition: proxies.h:53
std::conditional_t< std::is_integral_v< TValue >||std::is_floating_point_v< TValue >, TValue, const TValue & > GetValue(const Variable< TValue > &rVariable) const
Fetch the value corresponding to the input variable in the wrapped entity.
Definition: proxies.h:112
QualifiedEntity & GetEntity()
Mutable or immutable access to the wrapped entity, depending on TMutable.
Definition: proxies.h:141
TValue & GetValue(const Variable< TValue > &rVariable)
Fetch the value corresponding to the input variable in the wrapped entity.
Definition: proxies.h:119
const UnqualifiedEntity & GetEntity() const
Immutable access to the wrapped entity.
Definition: proxies.h:135
EntityProxy(QualifiedEntity &rEntity) noexcept
Constructor creating a valid proxy, wrapping the input entity.
Definition: proxies.h:98
bool HasValue(const Variable< TValue > &rVariable) const noexcept
Check whether the entity has a value for the provided variable.
Definition: proxies.h:102
void SetValue(const Variable< TValue > &rVariable, std::conditional_t< std::is_integral_v< TValue >||std::is_floating_point_v< TValue >, TValue, const TValue & > Value)
Overwrite the value corresponding to the input variable in the wrapped entity.
Definition: proxies.h:126
EntityProxy()=delete
Delete the default constructor to prevent storing nullptr.
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
This class defines the node.
Definition: node.h:65
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
DataLocation
Enum for location of data.
Definition: global_variables.h:48
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
bool operator<=(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Less equal operator.
Definition: dof.h:606
Expression::Pointer operator*(const Expression::ConstPointer &rpLeft, const double Right)
Expression::Pointer operator-(const Expression::ConstPointer &rpLeft, const double Right)
bool operator<(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Less than operator.
Definition: dof.h:584
bool operator==(const Flags &Left, const Flags &Right)
Definition: flags.cpp:45
auto MakeProxy(const TEntity &rEntity)
Invalid template base to be specialized for valid template parameters.
Definition: proxies.h:294
bool operator>(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Greater than operator.
Definition: dof.h:573
bool operator!=(const Flags &Left, const Flags &Right)
Definition: flags.cpp:50
Expression::Pointer operator+(const Expression::ConstPointer &rpLeft, const double Right)
bool operator>=(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Greater equal operator.
Definition: dof.h:595
default
Definition: generate_gid_list_file.py:35
def Index()
Definition: hdf5_io_tools.py:38
tuple const
Definition: ode_solve.py:403
#define KRATOS_DEFINE_CONTAINER_PROXY_FACTORY(TLocation)
Definition: proxies.h:349
#define KRATOS_DEFINE_ENTITY_PROXY_FACTORY(TLocation, TEntity)
Definition: proxies.h:308