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.
data_value_container.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 //
12 
13 #pragma once
14 
15 // System includes
16 #include <string>
17 #include <iostream>
18 #include <cstddef>
19 #include <vector>
20 
21 // External includes
22 
23 // Project includes
24 #include "includes/define.h"
25 #include "containers/variable.h"
27 #include "includes/exception.h"
28 
29 #ifdef KRATOS_DEBUG
30 #include "utilities/openmp_utils.h"
31 #endif
32 
33 namespace Kratos
34 {
35 
38 
42 
46 
50 
54 
62 class KRATOS_API(KRATOS_CORE) DataValueContainer
63 {
64 public:
67 
69  KRATOS_DEFINE_LOCAL_FLAG(OVERWRITE_OLD_VALUES);
70 
73 
75  using ValueType = std::pair<const VariableData*, void*>;
76 
78  using ContainerType = std::vector<ValueType>;
79 
81  using iterator = ContainerType::iterator;
82 
84  using const_iterator = ContainerType::const_iterator;
85 
87  using SizeType = ContainerType::size_type;
88 
92 
95 
98  {
99  for(const_iterator i = rOther.mData.begin() ; i != rOther.mData.end() ; ++i)
100  mData.push_back(ValueType(i->first, i->first->Clone(i->second)));
101  }
102 
105  {
106  for(iterator i = mData.begin() ; i != mData.end() ; ++i)
107  i->first->Delete(i->second);
108  }
109 
113 
121  template<class TDataType>
122  const TDataType& operator()(const VariableData& rThisVariable) const
123  {
124  return GetValue<TDataType>(rThisVariable);
125  }
126 
134  template<class TDataType>
135  TDataType& operator()(const Variable<TDataType>& rThisVariable)
136  {
137  return GetValue<TDataType>(rThisVariable);
138  }
139 
147  template<class TDataType>
148  const TDataType& operator()(const Variable<TDataType>& rThisVariable) const
149  {
150  return GetValue<TDataType>(rThisVariable);
151  }
152 
160  template<class TDataType>
161  TDataType& operator[](const VariableData& rThisVariable)
162  {
163  return GetValue<TDataType>(rThisVariable);
164  }
165 
173  template<class TDataType>
174  const TDataType& operator[](const VariableData& rThisVariable) const
175  {
176  return GetValue<TDataType>(rThisVariable);
177  }
178 
186  template<class TDataType>
187  TDataType& operator[](const Variable<TDataType>& rThisVariable)
188  {
189  return GetValue<TDataType>(rThisVariable);
190  }
191 
199  template<class TDataType>
200  const TDataType& operator[](const Variable<TDataType>& rThisVariable) const
201  {
202  return GetValue<TDataType>(rThisVariable);
203  }
204 
210  {
211  return mData.begin();
212  }
213 
219  {
220  return mData.begin();
221  }
222 
228  {
229  return mData.end();
230  }
231 
237  {
238  return mData.end();
239  }
240 
248  {
249  Clear();
250 
251  for(const_iterator i = rOther.mData.begin() ; i != rOther.mData.end() ; ++i)
252  mData.push_back(ValueType(i->first, i->first->Clone(i->second)));
253 
254  return *this;
255  }
256 
260 
267  template<class TDataType>
268  TDataType& GetValue(const Variable<TDataType>& rThisVariable)
269  {
270  typename ContainerType::iterator i;
271 
272  if ((i = std::find_if(mData.begin(), mData.end(), IndexCheck(rThisVariable.SourceKey()))) != mData.end())
273  return *(static_cast<TDataType*>(i->second) + rThisVariable.GetComponentIndex());
274 
275 #ifdef KRATOS_DEBUG
276  if(OpenMPUtils::IsInParallel() != 0)
277  KRATOS_ERROR << "attempting to do a GetValue for: " << rThisVariable << " unfortunately the variable is not in the database and the operations is not threadsafe (this function is being called from within a parallel region)" << std::endl;
278 #endif
279 
280  auto p_source_variable = &rThisVariable.GetSourceVariable();
281  mData.push_back(ValueType(p_source_variable,p_source_variable->Clone(p_source_variable->pZero())));
282 
283  return *(static_cast<TDataType*>(mData.back().second) + rThisVariable.GetComponentIndex());
284  }
285 
293  template<class TDataType>
294  const TDataType& GetValue(const Variable<TDataType>& rThisVariable) const
295  {
296  typename ContainerType::const_iterator i;
297 
298  if ((i = std::find_if(mData.begin(), mData.end(), IndexCheck(rThisVariable.SourceKey()))) != mData.end())
299  return *(static_cast<const TDataType*>(i->second) + rThisVariable.GetComponentIndex());
300 
301  return rThisVariable.Zero();
302  }
303 
309  {
310  return mData.size();
311  }
312 
319  template<class TDataType>
320  void SetValue(const Variable<TDataType>& rThisVariable, TDataType const& rValue)
321  {
322  typename ContainerType::iterator i;
323 
324  if ((i = std::find_if(mData.begin(), mData.end(), IndexCheck(rThisVariable.SourceKey()))) != mData.end()) {
325  *(static_cast<TDataType*>(i->second) + rThisVariable.GetComponentIndex()) = rValue;
326  } else {
327  auto p_source_variable = &rThisVariable.GetSourceVariable();
328  mData.push_back(ValueType(p_source_variable,p_source_variable->Clone(p_source_variable->pZero())));
329  *(static_cast<TDataType*>(mData.back().second) + rThisVariable.GetComponentIndex()) = rValue;
330  }
331  }
332 
338  template<class TDataType>
339  void Erase(const Variable<TDataType>& rThisVariable)
340  {
341  typename ContainerType::iterator i;
342 
343  if ((i = std::find_if(mData.begin(), mData.end(), IndexCheck(rThisVariable.SourceKey()))) != mData.end()) {
344  i->first->Delete(i->second);
345  mData.erase(i);
346  }
347  }
348 
352  void Clear()
353  {
354  for(ContainerType::iterator i = mData.begin() ; i != mData.end() ; i++)
355  i->first->Delete(i->second);
356 
357  mData.clear();
358  }
359 
365  void Merge(const DataValueContainer& rOther, const Flags Options);
366 
370 
374 
381  template<class TDataType>
382  bool Has(const Variable<TDataType>& rThisVariable) const
383  {
384  return (std::find_if(mData.begin(), mData.end(), IndexCheck(rThisVariable.SourceKey())) != mData.end());
385  }
386 
391  bool IsEmpty() const
392  {
393  return mData.empty();
394  }
395 
399 
404  virtual std::string Info() const
405  {
406  return std::string("data value container");
407  }
408 
413  virtual void PrintInfo(std::ostream& rOStream) const
414  {
415  rOStream << "data value container";
416  }
417 
423  virtual void PrintData(std::ostream& rOStream) const
424  {
425  for(const_iterator i = mData.begin() ; i != mData.end() ; ++i) {
426  rOStream <<" ";
427  i->first->Print(i->second, rOStream);
428  rOStream << std::endl;
429  }
430  }
431 
435 
437 protected:
440 
444 
448 
452 
456 
460 
464 
466 private:
468 
473  class IndexCheck
474  {
475  std::size_t mI;
476 
477  public:
478 
483  explicit IndexCheck(std::size_t I) : mI(I) {}
484 
490  bool operator()(const ValueType& I)
491  {
492  return I.first->SourceKey() == mI;
493  }
494  };
495 
499 
503 
504  ContainerType mData;
505 
509 
513 
517 
522  friend class Serializer;
523 
528  virtual void save(Serializer& rSerializer) const;
529 
534  virtual void load(Serializer& rSerializer);
535 
539 
543 
547 
549 }; // Class DataValueContainer
550 
554 
558 
560 inline std::istream& operator >> (std::istream& rIStream,
561  DataValueContainer& rThis);
562 
564 inline std::ostream& operator << (std::ostream& rOStream,
565  const DataValueContainer& rThis)
566 {
567  rThis.PrintInfo(rOStream);
568  rOStream << std::endl;
569  rThis.PrintData(rOStream);
570 
571  return rOStream;
572 }
574 
575 } // namespace Kratos.
Container for storing data values associated with variables.
Definition: data_value_container.h:63
TDataType & operator[](const Variable< TDataType > &rThisVariable)
Index operator for retrieving a data value by a Variable.
Definition: data_value_container.h:187
std::vector< ValueType > ContainerType
Type of the container used for variables.
Definition: data_value_container.h:78
virtual ~DataValueContainer()
Destructor.
Definition: data_value_container.h:104
bool Has(const Variable< TDataType > &rThisVariable) const
Checks if the data container has a value associated with a given variable.
Definition: data_value_container.h:382
void Erase(const Variable< TDataType > &rThisVariable)
Erases the value associated with a given variable.
Definition: data_value_container.h:339
TDataType & operator[](const VariableData &rThisVariable)
Index operator for retrieving a data value by a VariableData.
Definition: data_value_container.h:161
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue)
Sets the value for a given variable.
Definition: data_value_container.h:320
virtual void PrintData(std::ostream &rOStream) const
Outputs the detailed data contents of the data value container to a given stream.
Definition: data_value_container.h:423
DataValueContainer & operator=(const DataValueContainer &rOther)
Assignment operator for copying data from another DataValueContainer.
Definition: data_value_container.h:247
SizeType Size()
Gets the size of the data container.
Definition: data_value_container.h:308
virtual std::string Info() const
Retrieves a string representation of the data value container.
Definition: data_value_container.h:404
DataValueContainer()
Default constructor.
Definition: data_value_container.h:94
ContainerType::iterator iterator
Type of the container used for variables.
Definition: data_value_container.h:81
virtual void PrintInfo(std::ostream &rOStream) const
Outputs a brief description of the data value container to a given stream.
Definition: data_value_container.h:413
const_iterator begin() const
Const iterator pointing to the beginning of the container.
Definition: data_value_container.h:218
ContainerType::const_iterator const_iterator
Type of the container used for variables.
Definition: data_value_container.h:84
const TDataType & operator()(const VariableData &rThisVariable) const
Accessor operator for retrieving a data value by a VariableData.
Definition: data_value_container.h:122
ContainerType::size_type SizeType
Type of the container used for variables.
Definition: data_value_container.h:87
const TDataType & operator()(const Variable< TDataType > &rThisVariable) const
Accessor operator for retrieving a data value by a Variable (const version).
Definition: data_value_container.h:148
const TDataType & GetValue(const Variable< TDataType > &rThisVariable) const
Gets the value associated with a given variable (const version).
Definition: data_value_container.h:294
void Clear()
Clears the entire data container.
Definition: data_value_container.h:352
iterator end()
Iterator pointing to the end of the container.
Definition: data_value_container.h:227
const TDataType & operator[](const VariableData &rThisVariable) const
Index operator for retrieving a data value by a VariableData (const version).
Definition: data_value_container.h:174
DataValueContainer(DataValueContainer const &rOther)
Copy constructor.
Definition: data_value_container.h:97
std::pair< const VariableData *, void * > ValueType
Type of the container used for variables.
Definition: data_value_container.h:75
KRATOS_DEFINE_LOCAL_FLAG(OVERWRITE_OLD_VALUES)
Define local flag.
TDataType & GetValue(const Variable< TDataType > &rThisVariable)
Gets the value associated with a given variable.
Definition: data_value_container.h:268
bool IsEmpty() const
Checks if the data container is empty.
Definition: data_value_container.h:391
TDataType & operator()(const Variable< TDataType > &rThisVariable)
Accessor operator for retrieving a data value by a Variable.
Definition: data_value_container.h:135
const_iterator end() const
Const iterator pointing to the end of the container.
Definition: data_value_container.h:236
const TDataType & operator[](const Variable< TDataType > &rThisVariable) const
Index operator for retrieving a data value by a Variable (const version).
Definition: data_value_container.h:200
iterator begin()
Iterator pointing to the beginning of the container.
Definition: data_value_container.h:209
KRATOS_CLASS_POINTER_DEFINITION(DataValueContainer)
Pointer definition of DataValueContainer.
Definition: flags.h:58
static int IsInParallel()
Wrapper for omp_in_parallel().
Definition: openmp_utils.h:122
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
This class is the base of variables and variable's components which contains their common data.
Definition: variable_data.h:49
KeyType GetComponentIndex() const
Returns the component index.
Definition: variable_data.h:227
KeyType SourceKey() const
Definition: variable_data.h:192
const VariableData & GetSourceVariable() const
Definition: variable_data.h:232
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
const TDataType & Zero() const
This method returns the zero value of the variable type.
Definition: variable.h:346
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
def load(f)
Definition: ode_solve.py:307
integer i
Definition: TensorModule.f:17
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247