15 #if !defined(KRATOS_VARIABLES_LIST_H_INCLUDED )
16 #define KRATOS_VARIABLES_LIST_H_INCLUDED
27 #include <boost/iterator/indirect_iterator.hpp>
77 typedef boost::indirect_iterator<VariablesContainerType::const_iterator>
const_iterator;
80 typedef VariablesContainerType::size_type
size_type;
94 template <
class TInputIteratorType>
97 for (; First != Last; First++)
103 , mHashFunctionIndex(rOther.mHashFunctionIndex)
104 , mKeys(rOther.mKeys)
105 , mPositions(rOther.mPositions)
106 , mVariables(rOther.mVariables)
107 , mDofVariables(rOther.mDofVariables)
108 , mDofReactions(rOther.mDofReactions)
124 mDataSize = rOther.mDataSize;
125 mHashFunctionIndex = rOther.mHashFunctionIndex;
126 mKeys = rOther.mKeys;
127 mPositions = rOther.mPositions;
128 mVariables = rOther.mVariables;
129 mDofVariables=rOther.mDofVariables;
130 mDofReactions=rOther.mDofReactions;
137 return GetPosition(VariableKey);
140 template<
class TDataType>
143 return GetPosition(ThisVariable.
SourceKey());
148 return mVariables[
Index];
154 if (size() != r.size())
157 return std::equal(mPositions.begin(), mPositions.end(), r.mPositions.begin()) &&
158 std::equal(mVariables.begin(), mVariables.end(), r.mVariables.begin());
164 return mReferenceCounter;
188 return mVariables.begin();
192 return mVariables.end();
196 return mVariables.rbegin();
200 return mVariables.rend();
206 return *mVariables.front();
211 return *mVariables.back();
216 return mVariables.size();
221 return mVariables.max_size();
227 mDataSize = rOther.mDataSize;
228 rOther.mDataSize =
temp;
230 temp = mHashFunctionIndex;
231 mHashFunctionIndex = rOther.mHashFunctionIndex;
232 rOther.mHashFunctionIndex =
temp;
234 mVariables.swap(rOther.mVariables);
236 mDofVariables.swap(rOther.mDofVariables);
237 mDofReactions.swap(rOther.mDofReactions);
239 mKeys.swap(rOther.mKeys);
240 mPositions.swap(rOther.mPositions);
243 template<
class TOtherDataType>
252 mHashFunctionIndex = 0;
254 mDofVariables.clear();
255 mDofReactions.clear();
257 mPositions = {
static_cast<IndexType>(-1)};
265 "Adding uninitialize variable to this variable list. Check if all variables are registered before kernel initialization",
"");
268 if (
Has(ThisVariable))
276 mVariables.push_back(&ThisVariable);
277 SetPosition(ThisVariable.
SourceKey(), mDataSize);
284 for(std::size_t dof_index = 0 ; dof_index < mDofVariables.size() ; dof_index++){
285 if(*mDofVariables[dof_index] == *pThisDofVariable){
286 return static_cast<int>(dof_index);
292 KRATOS_ERROR <<
"attempting to call AddDof for: " << pThisDofVariable <<
". Unfortunately the Dof was not added before and the operations is not threadsafe (this function is being called within a parallel region)" << std::endl;
294 mDofVariables.push_back(pThisDofVariable);
295 mDofReactions.push_back(
nullptr);
297 KRATOS_DEBUG_ERROR_IF(mDofVariables.size()>64) <<
"Adding too many dofs to the node. Each node only can store 64 Dofs." << std::endl;
299 return mDofVariables.size() - 1;
304 for(std::size_t dof_index = 0 ; dof_index < mDofVariables.size() ; dof_index++){
305 if(*mDofVariables[dof_index] == *pThisDofVariable){
306 mDofReactions[dof_index] = pThisDofReaction;
307 return static_cast<int>(dof_index);
313 KRATOS_ERROR <<
"attempting to call AddDof for: " << pThisDofVariable <<
". Unfortunately the Dof was not added before and the operations is not threadsafe (this function is being called within a parallel region)" << std::endl;
315 mDofVariables.push_back(pThisDofVariable);
316 mDofReactions.push_back(pThisDofReaction);
318 KRATOS_DEBUG_ERROR_IF(mDofVariables.size()>64) <<
"Adding too many dofs to the node. Each node only can store 64 Dofs." << std::endl;
320 return mDofVariables.size() - 1;
324 return *mDofVariables[DofIndex];
328 return mDofReactions[DofIndex];
332 KRATOS_DEBUG_ERROR_IF(
static_cast<std::size_t
>(DofIndex) >= mDofReactions.size()) <<
"The given dof with index = " << DofIndex <<
" is not stored in this variables list" << std::endl;
333 mDofReactions[DofIndex] = pThisDofReaction;
338 return GetPosition(VariableKey);
341 template<
class TDataType>
344 return GetPosition(ThisVariable.
SourceKey());
349 return GetPosition(pThisVariable->
SourceKey());
378 if (mPositions.empty())
384 return mKeys[GetHashIndex(rThisVariable.
SourceKey(), mKeys.size(), mHashFunctionIndex)] == rThisVariable.
SourceKey();
389 return mVariables.empty();
399 return "variables list";
411 rOStream <<
" with " << size() <<
" variables";
412 rOStream <<
" (size : " << mDataSize <<
" blocks of " <<
sizeof(
BlockType) <<
" bytes) " << std::endl;
414 rOStream <<
" " << mVariables[
i]->Name() <<
" \t-> " << GetPosition(mVariables[
i]->Key()) << std::endl;
416 rOStream <<
" with " << mDofVariables.size() <<
" Dofs:";
418 rOStream <<
" [" << mDofVariables[
i]->Name() <<
" , " << ((mDofReactions[
i] ==
nullptr) ?
"NONE" : mDofReactions[
i]->Name()) <<
"]" << std::endl;
430 KeysContainerType mKeys = {
static_cast<IndexType>(-1)};
432 PositionsContainerType mPositions = {
static_cast<IndexType>(-1)};
434 VariablesContainerType mVariables;
436 VariablesContainerType mDofVariables;
438 VariablesContainerType mDofReactions;
444 mutable std::atomic<int> mReferenceCounter{0};
448 x->mReferenceCounter.fetch_add(1, std::memory_order_relaxed);
453 if (
x->mReferenceCounter.fetch_sub(1, std::memory_order_release) == 1) {
454 std::atomic_thread_fence(std::memory_order_acquire);
465 if (mPositions.empty())
468 if (mPositions[GetHashIndex(Key,mPositions.size(),mHashFunctionIndex)] < mDataSize)
471 mKeys[GetHashIndex(Key, mPositions.size(), mHashFunctionIndex)] = Key;
472 mPositions[GetHashIndex(Key, mPositions.size(), mHashFunctionIndex)] = ThePosition;
475 SizeType GetHashIndex(std::size_t Key, std::size_t TableSize, std::size_t HashFunctionIndex)
const{
476 return (Key >> HashFunctionIndex) & (TableSize - 1);
480 SizeType index = GetHashIndex(Key,mPositions.size(),mHashFunctionIndex);
481 return mPositions[index];
484 void ResizePositions() {
485 bool size_is_ok =
false;
486 std::size_t new_size = mPositions.size();
487 SizeType new_hash_function_index = 0;
488 while (size_is_ok !=
true) {
489 new_hash_function_index++;
490 if (new_hash_function_index > 31) {
491 new_hash_function_index = 0;
494 KeysContainerType new_keys(new_size,
static_cast<IndexType>(-1));
495 PositionsContainerType new_positions(new_size,
static_cast<IndexType>(-1));
498 for (
auto i_variable = mVariables.begin(); i_variable != mVariables.end(); i_variable++)
499 if (new_positions[GetHashIndex((*i_variable)->SourceKey(), new_size, new_hash_function_index)] > mDataSize) {
500 new_positions[GetHashIndex((*i_variable)->SourceKey(), new_size, new_hash_function_index)] = mPositions[GetHashIndex((*i_variable)->SourceKey(), mPositions.size(), mHashFunctionIndex)];
501 new_keys[GetHashIndex((*i_variable)->SourceKey(), new_size, new_hash_function_index)] = (*i_variable)->SourceKey();
509 mPositions.swap(new_positions);
510 mKeys.swap(new_keys);
511 mHashFunctionIndex = new_hash_function_index;
523 virtual void save(
Serializer& rSerializer)
const;
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
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 SourceKey() const
Definition: variable_data.h:192
std::size_t Size() const
Definition: variable_data.h:206
const VariableData & GetSourceVariable() const
Definition: variable_data.h:232
bool IsComponent() const
Definition: variable_data.h:211
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
Holds a list of variables and their position in VariablesListDataValueContainer.
Definition: variables_list.h:50
VariablesList(VariablesList const &rOther)
Copy constructor.
Definition: variables_list.h:102
ptr_const_iterator ptr_end() const
Definition: variables_list.h:190
VariablesContainerType::const_reverse_iterator ptr_const_reverse_iterator
Definition: variables_list.h:82
std::vector< IndexType > KeysContainerType
Definition: variables_list.h:66
VariableData data_type
Definition: variables_list.h:72
void SetDofReaction(VariableData const *pThisDofReaction, int DofIndex)
Definition: variables_list.h:331
KRATOS_CLASS_INTRUSIVE_POINTER_DEFINITION(VariablesList)
Pointer definition of VariablesList.
VariablesList & operator=(VariablesList const &rOther)
Assignment operator.
Definition: variables_list.h:122
const_iterator end() const
Definition: variables_list.h:174
VariablesList()
Default constructor. mPosition should have at least on entry.
Definition: variables_list.h:91
std::vector< const VariableData * > VariablesContainerType
Definition: variables_list.h:70
const VariableData * value_type
Definition: variables_list.h:73
~VariablesList()
Destructor.
Definition: variables_list.h:112
std::size_t SizeType
Definition: variables_list.h:60
const VariableData * pGetDofReaction(int DofIndex) const
Definition: variables_list.h:327
VariablesContainerType::const_iterator ptr_const_iterator
Definition: variables_list.h:81
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: variables_list.h:403
IndexType operator()(Variable< TDataType > const &ThisVariable) const
Definition: variables_list.h:141
size_type size() const
Definition: variables_list.h:214
VariablesContainerType const & Variables()
Definition: variables_list.h:363
int AddDof(VariableData const *pThisDofVariable)
Definition: variables_list.h:282
void push_back(TOtherDataType const &x)
Definition: variables_list.h:244
const_reference front() const
Definition: variables_list.h:203
friend void intrusive_ptr_add_ref(const VariablesList *x)
Definition: variables_list.h:446
boost::indirect_iterator< VariablesContainerType::const_reverse_iterator > const_reverse_iterator
Definition: variables_list.h:78
VariablesContainerType::difference_type difference_type
Definition: variables_list.h:83
ptr_const_reverse_iterator ptr_rend() const
Definition: variables_list.h:198
ptr_const_iterator ptr_begin() const
Definition: variables_list.h:186
IndexType Index(const VariableData *pThisVariable) const
Definition: variables_list.h:347
IndexType Index(Variable< TDataType > const &ThisVariable) const
Definition: variables_list.h:342
void clear()
Definition: variables_list.h:249
double BlockType
Definition: variables_list.h:64
const_reverse_iterator rend() const
Definition: variables_list.h:182
VariablesContainerType::size_type size_type
Definition: variables_list.h:80
VariablesList(TInputIteratorType First, TInputIteratorType Last)
Definition: variables_list.h:95
size_type max_size() const
Definition: variables_list.h:219
const_reference back() const
Definition: variables_list.h:208
IndexType Index(IndexType VariableKey) const
Definition: variables_list.h:336
std::string Info() const
Turn back information as a string.
Definition: variables_list.h:397
bool operator==(const VariablesList &r) const
Definition: variables_list.h:152
IndexType operator()(IndexType VariableKey) const
Definition: variables_list.h:135
friend void intrusive_ptr_release(const VariablesList *x)
Definition: variables_list.h:451
bool IsEmpty() const
Definition: variables_list.h:387
void swap(VariablesList &rOther)
Definition: variables_list.h:224
unsigned int use_count() const noexcept
Definition: variables_list.h:162
const VariableData * const_pointer
Definition: variables_list.h:74
std::size_t IndexType
Definition: variables_list.h:62
std::vector< IndexType > PositionsContainerType
Definition: variables_list.h:68
int AddDof(VariableData const *pThisDofVariable, VariableData const *pThisDofReaction)
Definition: variables_list.h:302
const VariableData & GetDofVariable(int DofIndex) const
Definition: variables_list.h:323
void Add(VariableData const &ThisVariable)
Definition: variables_list.h:261
const VariableData * operator[](IndexType Index) const
Definition: variables_list.h:146
VariableData const & const_reference
Definition: variables_list.h:75
bool Has(const VariableData &rThisVariable) const
Definition: variables_list.h:372
boost::indirect_iterator< VariablesContainerType::const_iterator > const_iterator
Definition: variables_list.h:77
ptr_const_reverse_iterator ptr_rbegin() const
Definition: variables_list.h:194
SizeType DataSize() const
Definition: variables_list.h:357
const_iterator begin() const
Definition: variables_list.h:170
const_reverse_iterator rbegin() const
Definition: variables_list.h:178
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: variables_list.h:409
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
bool Has(const std::string &ModelerName)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:24
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
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
int block_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:16
def load(f)
Definition: ode_solve.py:307
tuple const
Definition: ode_solve.py:403
float temp
Definition: rotating_cone.py:85
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17
def IsEmpty(_A)
Definition: custom_math.py:33