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.
variables_list_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 
14 #pragma once
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 #include <cstddef>
20 #include <cstring>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/define.h"
26 #include "containers/variable.h"
28 
29 namespace Kratos
30 {
31 
34 
38 
42 
46 
50 
60 class KRATOS_API(KRATOS_CORE) VariablesListDataValueContainer final
61 {
62 public:
65 
68 
71 
74 
76  using IndexType = std::size_t;
77 
79  using SizeType = std::size_t;
80 
84 
86  explicit VariablesListDataValueContainer(SizeType NewQueueSize = 1)
87  : mQueueSize(NewQueueSize), mpCurrentPosition(0),
88  mpData(0), mpVariablesList(nullptr)
89  {
90  if(!mpVariablesList)
91  return;
92 
93  // Allcating memory
94  Allocate();
95 
96  // Setting the current position at the begining of data
97  mpCurrentPosition = mpData;
98 
99  const SizeType size = mpVariablesList->DataSize();
100  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
101  BlockType* position = Position(*it_variable);
102  for(SizeType i = 0 ; i < mQueueSize ; i++) {
103  it_variable->AssignZero(position + i * size);
104  }
105  }
106  }
107 
110  : mQueueSize(rOther.mQueueSize), mpCurrentPosition(0),
111  mpData(0), mpVariablesList(rOther.mpVariablesList)
112  {
113  if(!mpVariablesList)
114  return;
115 
116  // Allcating memory
117  Allocate();
118 
119  // Setting the current position with relative source container offset
120  mpCurrentPosition = mpData + (rOther.mpCurrentPosition - rOther.mpData);
121 
122  const SizeType size = mpVariablesList->DataSize();
123  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
124  const SizeType offset = LocalOffset(*it_variable);
125  for(SizeType i = 0 ; i < mQueueSize ; i++) {
126  const SizeType total_offset = offset + i * size;
127  it_variable->Copy(rOther.mpData + total_offset, mpData + total_offset);
128  }
129  }
130  }
131 
133  VariablesListDataValueContainer(VariablesList::Pointer pVariablesList, SizeType NewQueueSize = 1)
134  : mQueueSize(NewQueueSize), mpCurrentPosition(0),
135  mpData(0), mpVariablesList(pVariablesList)
136  {
137  if(!mpVariablesList)
138  return;
139 
140  // Allcating memory
141  Allocate();
142 
143  // Setting the current position at the begining of data
144  mpCurrentPosition = mpData;
145 
146  const SizeType size = mpVariablesList->DataSize();
147  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
148  BlockType* position = Position(*it_variable);
149  for(SizeType i = 0 ; i < mQueueSize ; i++) {
150  it_variable->AssignZero(position + i * size);
151  }
152  }
153  }
154 
156  VariablesListDataValueContainer(VariablesList::Pointer pVariablesList, BlockType const * ThisData, SizeType NewQueueSize = 1)
157  : mQueueSize(NewQueueSize), mpCurrentPosition(0),
158  mpData(0), mpVariablesList(pVariablesList)
159  {
160  if(!mpVariablesList)
161  return;
162 
163  // Allcating memory
164  Allocate();
165 
166  // Setting the current position at the begining of data
167  mpCurrentPosition = mpData;
168 
169  const SizeType size = mpVariablesList->DataSize();
170  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
171  const SizeType offset = LocalOffset(*it_variable);
172  for(SizeType i = 0 ; i < mQueueSize ; i++) {
173  const SizeType total_offset = offset + i * size;
174  it_variable->Copy(ThisData + total_offset, mpData + total_offset);
175  }
176  }
177  }
178 
181  {
182  Clear();
183  }
184 
188 
189 // template<class TDataType> const TDataType& operator()(const VariableData& rThisVariable, SizeType QueueIndex = 0) const
190 // {
191 // return GetValue(rThisVariable, QueueIndex);
192 // }
193 
194  template<class TDataType>
195  TDataType& operator()(const Variable<TDataType>& rThisVariable)
196  {
197  return GetValue(rThisVariable);
198  }
199 
200  template<class TDataType>
201  TDataType& operator()(const Variable<TDataType>& rThisVariable, SizeType QueueIndex)
202  {
203  return GetValue(rThisVariable, QueueIndex);
204  }
205 
206  template<class TDataType>
207  const TDataType& operator()(const Variable<TDataType>& rThisVariable) const
208  {
209  return GetValue(rThisVariable);
210  }
211 
212  template<class TDataType>
213  const TDataType& operator()(const Variable<TDataType>& rThisVariable, SizeType QueueIndex) const
214  {
215  return GetValue(rThisVariable, QueueIndex);
216  }
217 
218 // template<class TDataType> TDataType& operator[](const VariableData& rThisVariable)
219 // {
220 // return GetValue(rThisVariable, 0);
221 // }
222 
223 // template<class TDataType> const TDataType& operator[](const VariableData& rThisVariable) const
224 // {
225 // return GetValue(rThisVariable, 0);
226 // }
227 
228  template<class TDataType> TDataType& operator[](const Variable<TDataType>& rThisVariable)
229  {
230  return GetValue(rThisVariable);
231  }
232 
233  template<class TDataType> const TDataType& operator[](const Variable<TDataType>& rThisVariable) const
234  {
235  return GetValue(rThisVariable);
236  }
237 
240  {
241  if(rOther.mpVariablesList == 0) {
242  Clear();
243  } else if((mpVariablesList == rOther.mpVariablesList) && (mQueueSize == rOther.mQueueSize)) {
244  mpCurrentPosition = mpData + (rOther.mpCurrentPosition - rOther.mpData);
245 
246  const SizeType size = mpVariablesList->DataSize();
247  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
248  const SizeType offset = LocalOffset(*it_variable);
249  for(SizeType i = 0 ; i < mQueueSize ; i++) {
250  const SizeType total_offset = offset + i * size;
251  it_variable->Assign(rOther.mpData + total_offset, mpData + total_offset);
252  }
253  }
254  } else {
255  DestructAllElements();
256 
257  mQueueSize = rOther.mQueueSize;
258  mpVariablesList = rOther.mpVariablesList;
259 
260  Reallocate();
261 
262  mpCurrentPosition = mpData + (rOther.mpCurrentPosition - rOther.mpData);
263 
264  const SizeType size = mpVariablesList->DataSize();
265  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
266  const SizeType offset = LocalOffset(*it_variable);
267  for(SizeType i = 0 ; i < mQueueSize ; i++) {
268  const SizeType total_offset = offset + i * size;
269  it_variable->Copy(rOther.mpData + total_offset, mpData + total_offset);
270  }
271  }
272  }
273 
274  return *this;
275  }
276 
280 
281  template<class TDataType>
282  TDataType& GetValue(const Variable<TDataType>& rThisVariable)
283  {
284  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
285  KRATOS_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
286  return *(reinterpret_cast<TDataType*>(Position(rThisVariable)) + rThisVariable.GetComponentIndex());
287  }
288 
289  template<class TDataType>
290  TDataType& GetValue(const Variable<TDataType>& rThisVariable, SizeType QueueIndex)
291  {
292  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
293  KRATOS_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
294  return *(reinterpret_cast<TDataType*>(Position(rThisVariable, QueueIndex)) + rThisVariable.GetComponentIndex());
295  }
296 
297  template<class TDataType>
298  const TDataType& GetValue(const Variable<TDataType>& rThisVariable) const
299  {
300  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
301  KRATOS_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
302  return *(reinterpret_cast<const TDataType*>(Position(rThisVariable)) + rThisVariable.GetComponentIndex());
303  }
304 
305  template<class TDataType>
306  const TDataType& GetValue(const Variable<TDataType>& rThisVariable, SizeType QueueIndex) const
307  {
308  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
309  KRATOS_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
310  return *(reinterpret_cast<const TDataType*>(Position(rThisVariable, QueueIndex)) + rThisVariable.GetComponentIndex());
311  }
312 
313  template<class TDataType>
314  TDataType& FastGetValue(const Variable<TDataType>& rThisVariable)
315  {
316  return *(reinterpret_cast<TDataType*>(Position(rThisVariable)) + rThisVariable.GetComponentIndex());
317  }
318 
319  template<class TDataType>
320  TDataType* pFastGetValue(const Variable<TDataType>& rThisVariable)
321  {
322  return (reinterpret_cast<TDataType*>(Position(rThisVariable)) + rThisVariable.GetComponentIndex());
323  }
324 
325  template<class TDataType>
326  TDataType& FastGetValue(const Variable<TDataType>& rThisVariable, SizeType QueueIndex)
327  {
328  return *(reinterpret_cast<TDataType*>(Position(rThisVariable, QueueIndex)) + rThisVariable.GetComponentIndex());
329  }
330 
331  template<class TDataType>
332  TDataType& FastGetValue(const Variable<TDataType>& rThisVariable, SizeType QueueIndex, SizeType ThisPosition)
333  {
334  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
335  KRATOS_DEBUG_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
336  KRATOS_DEBUG_ERROR_IF((QueueIndex + 1) > mQueueSize) << "Trying to access data from step " << QueueIndex << " but only " << mQueueSize << " steps are stored." << std::endl;
337  return *(reinterpret_cast<TDataType*>(Position(QueueIndex) + ThisPosition) + rThisVariable.GetComponentIndex());
338  }
339 
340  template<class TDataType>
341  TDataType& FastGetCurrentValue(const Variable<TDataType>& rThisVariable, SizeType ThisPosition)
342  {
343  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
344  KRATOS_DEBUG_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
345  return *(reinterpret_cast<TDataType*>(mpCurrentPosition + ThisPosition) + rThisVariable.GetComponentIndex());
346  }
347 
348  template<class TDataType>
349  const TDataType& FastGetValue(const Variable<TDataType>& rThisVariable) const
350  {
351  return *(reinterpret_cast<const TDataType*>(Position(rThisVariable)) + rThisVariable.GetComponentIndex());
352  }
353 
354  template<class TDataType>
355  const TDataType* pFastGetValue(const Variable<TDataType>& rThisVariable) const
356  {
357  return (reinterpret_cast<const TDataType*>(Position(rThisVariable)) + rThisVariable.GetComponentIndex());
358  }
359 
360  template<class TDataType>
361  const TDataType& FastGetValue(const Variable<TDataType>& rThisVariable, SizeType QueueIndex) const
362  {
363  return *(reinterpret_cast<const TDataType*>(Position(rThisVariable, QueueIndex)) + rThisVariable.GetComponentIndex());
364  }
365 
366  template<class TDataType>
367  const TDataType& FastGetValue(const Variable<TDataType>& rThisVariable, SizeType QueueIndex, SizeType ThisPosition) const
368  {
369  return *(reinterpret_cast<const TDataType*>(Position(QueueIndex) + ThisPosition) + rThisVariable.GetComponentIndex());
370  }
371 
372 
373  template<class TDataType>
374  const TDataType& FastGetCurrentValue(const Variable<TDataType>& rThisVariable, SizeType ThisPosition) const
375  {
376  return *(reinterpret_cast<TDataType*>(mpCurrentPosition + ThisPosition) + rThisVariable.GetComponentIndex());
377  }
378 
379  SizeType Size() const
380  {
381  if(!mpVariablesList)
382  return 0;
383 
384  return mpVariablesList->DataSize();
385  }
386 
388  {
389  return mQueueSize;
390  }
391 
393  {
394  if(!mpVariablesList)
395  return 0;
396 
397  return mQueueSize * mpVariablesList->DataSize();
398  }
399 
400  template<class TDataType> void SetValue(const Variable<TDataType>& rThisVariable, TDataType const& rValue)
401  {
402  GetValue(rThisVariable) = rValue;
403  }
404 
405  template<class TDataType> void SetValue(const Variable<TDataType>& rThisVariable, TDataType const& rValue, SizeType QueueIndex)
406  {
407  GetValue(rThisVariable, QueueIndex) = rValue;
408  }
409 
410  void Clear()
411  {
412  DestructAllElements();
413  if(mpData)
414  free(mpData);
415 
416  mpData = 0;
417  }
418 
419 
423 
424  VariablesList::Pointer pGetVariablesList()
425  {
426  return mpVariablesList;
427  }
428 
429  const VariablesList::Pointer pGetVariablesList() const
430  {
431  return mpVariablesList;
432  }
433 
435  {
436  return *mpVariablesList;
437  }
438 
440  {
441  return *mpVariablesList;
442  }
443 
444  void SetVariablesList(VariablesList::Pointer pVariablesList)
445  {
446  DestructAllElements();
447 
448  mpVariablesList = pVariablesList;
449 
450  if(!mpVariablesList)
451  return;
452 
453  Reallocate();
454 
455  mpCurrentPosition = mpData;
456 
457  const SizeType size = mpVariablesList->DataSize();
458  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
459  BlockType* position = Position(*it_variable);
460  for(SizeType i = 0 ; i < mQueueSize ; i++) {
461  it_variable->AssignZero(position + i * size);
462  }
463  }
464  }
465 
466  void SetVariablesList(VariablesList::Pointer pVariablesList, SizeType ThisQueueSize)
467  {
468  DestructAllElements();
469 
470  mpVariablesList = pVariablesList;
471 
472  mQueueSize = ThisQueueSize;
473 
474  if(!mpVariablesList)
475  return;
476 
477  Reallocate();
478 
479  mpCurrentPosition = mpData;
480 
481  const SizeType size = mpVariablesList->DataSize();
482  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
483  BlockType* position = Position(*it_variable);
484  for(SizeType i = 0 ; i < mQueueSize ; i++) {
485  it_variable->AssignZero(position + i * size);
486  }
487  }
488  }
489 
490  void Resize(SizeType NewSize)
491  {
492  if(mQueueSize == NewSize) // if new size is equal to the provious one
493  return; // Do nothing!!
494 
495  if(!mpVariablesList)
496  return;
497 
498  if(mQueueSize > NewSize) { // if new size is smaller
499  // Destructing elements out of the new size
500  for(SizeType i = NewSize ; i < mQueueSize ; i++)
501  DestructElements(i);
502 
503  const SizeType size = mpVariablesList->DataSize();
504 
505  //allocating memory
506  BlockType* temp = (BlockType*)malloc(size * sizeof(BlockType) * NewSize);
507 
508  //Copying data to allocated memory
509  for(SizeType i = 0 ; i < NewSize ; i++)
510  memcpy(temp + i * size, Position(i), size * sizeof(BlockType));
511 
512  // Updating the queue size
513  mQueueSize = NewSize;
514 
515  // freeing the old memory
516  free(mpData);
517 
518  // Setting data pointer to the allocated memory
519  mpData = temp;
520 
521  // updating the current position
522  mpCurrentPosition = mpData;
523 
524  } else {
525  // Calculating the difference of new and old sizes
526  const SizeType difference = NewSize - mQueueSize;
527 
528  //keeping the old queue size
529  const SizeType old_size = mQueueSize;
530 
531  // Getting the relative offset of current position
532  const SizeType current_offset = mpCurrentPosition - mpData;
533 
534  //Updating the queue size
535  mQueueSize = NewSize;
536 
537  // Reallocating for new size
538  Reallocate();
539 
540  SizeType size = mpVariablesList->DataSize();
541 
542  // Updating the mpCurrentPosition
543  mpCurrentPosition = mpData + current_offset;
544 
545  // moving the region after current position to the end
546  const SizeType region_size = old_size * size - current_offset;
547  memmove(mpCurrentPosition + difference * size, mpCurrentPosition, region_size * sizeof(BlockType));
548 
549  // Assigning zero to the added elements
550  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
551  BlockType* position = mpCurrentPosition + LocalOffset(*it_variable);
552  for(SizeType i = 0 ; i < difference ; i++) {
553  it_variable->AssignZero(position + i * size);
554  }
555  }
556 
557  //moving the current position to the moved place
558  mpCurrentPosition += difference * size;
559  }
560  }
561 
562 
563 // void Resize(SizeType NewSize, BlockType* SourceData)
564 // {
565 // if(mQueueSize == NewSize)
566 // return;
567 
568 // if(mQueueSize > NewSize)
569 // {
570 // for(SizeType i = NewSize ; i < mQueueSize ; i++)
571 // DestructElements(i);
572 
573 // mQueueSize = NewSize;
574 
575 // Reallocate();
576 // }
577 // else
578 // {
579 // SizeType old_size = mQueueSize;
580 // mQueueSize = NewSize;
581 // Reallocate();
582 // SizeType size = mpVariablesList->DataSize();
583 // for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
584 // SizeType offset = LocalOffset(*it_variable);
585 // for(SizeType i = old_size ; i < mQueueSize ; i++)
586 // it_variable->Copy(SourceData + offset, mpData + offset + i * size);
587 // }
588 // }
589 // }
590 
591 
593  {
594  return mpData;
595  }
596 
597  const BlockType* Data() const
598  {
599  return mpData;
600  }
601 
602  BlockType* Data(SizeType QueueIndex)
603  {
604  return Position(QueueIndex);
605  }
606 
607  BlockType* Data(VariableData const & rThisVariable)
608  {
609  KRATOS_DEBUG_ERROR_IF(!mpVariablesList->Has(rThisVariable)) << "Variable " << rThisVariable.Name() << " is not added to this variables list. Stopping" << std::endl;
610  return Position(rThisVariable);
611  }
612 
614  {
615  if(!mpVariablesList)
616  return 0;
617 
618  return mpVariablesList->DataSize() * sizeof(BlockType);
619  }
620 
622  {
623  if(!mpVariablesList)
624  return 0;
625 
626  return mpVariablesList->DataSize() * sizeof(BlockType) * mQueueSize;
627  }
628 
629  void AssignData(BlockType* Source, SizeType QueueIndex)
630  {
631  AssignData(Source, Position(QueueIndex));
632  }
633 
634 // void SetData(BlockType* pThisData)
635 // {
636 // if(mpData)
637 // free(mpData);
638 // mpData = pThisData;
639 // }
640 
641  void CloneFront()
642  {
643  if(mQueueSize == 0) {
644  Resize(1);
645  return;
646  }
647 
648  if(mQueueSize == 1)
649  return;
650 
651  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
652 
653  const SizeType size = mpVariablesList->DataSize();
654  BlockType* position = (mpCurrentPosition == mpData) ? mpData + TotalSize() - size : mpCurrentPosition - size;
655  AssignData(mpCurrentPosition, position);
656  mpCurrentPosition = position;
657 
658  }
659 
660  void PushFront()
661  {
662  if(mQueueSize == 0) {
663  Resize(1);
664  return;
665  }
666 
667  if(mQueueSize == 1)
668  return;
669 
670  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
671 
672  const SizeType size = mpVariablesList->DataSize();
673  mpCurrentPosition = (mpCurrentPosition == mpData) ? mpData + TotalSize() - size : mpCurrentPosition - size;
674  AssignZero();
675 
676  }
677 
678  void AssignZero()
679  {
680  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
681  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
682  it_variable->AssignZero(mpCurrentPosition + LocalOffset(*it_variable));
683  }
684  }
685 
686  void AssignZero(const SizeType QueueIndex)
687  {
688  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
689  BlockType* position = Position(QueueIndex);
690  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
691  it_variable->AssignZero(position + LocalOffset(*it_variable));
692  }
693  }
694 
698 
704  bool Has(const VariableData& rThisVariable) const
705  {
706  if(!mpVariablesList)
707  return false;
708 
709  return mpVariablesList->Has(rThisVariable);
710  }
711 
712  bool IsEmpty()
713  {
714  if(!mpVariablesList)
715  return true;
716 
717  return mpVariablesList->IsEmpty();
718  }
719 
723 
725  std::string Info() const
726  {
727  return std::string("variables list data value container");
728  }
729 
731  void PrintInfo(std::ostream& rOStream) const
732  {
733  rOStream << "variables list data value container";
734  }
735 
737  void PrintData(std::ostream& rOStream) const
738  {
739  if(!mpVariablesList)
740  rOStream << "No varaibles list is assigned yet." << std::endl;
741 
742  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
743  rOStream <<" ";
744  for(SizeType i = 0 ; i < mQueueSize ; i++) {
745  rOStream << i << ": ";
746  it_variable->Print(Position(*it_variable, i), rOStream);
747  rOStream << " ";
748  }
749  rOStream << std::endl;
750  }
751 
752  }
753 
757 
759 protected:
762 
766 
770 
774 
778 
782 
786 
788 private:
791 
792 
796 
797  SizeType mQueueSize;
798 
799  BlockType* mpCurrentPosition;
800 
801  ContainerType mpData;
802 
803  VariablesList::Pointer mpVariablesList;
804 
808 
812 
813 
814  inline void Allocate()
815  {
816  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
817  mpData = (BlockType*)malloc(mpVariablesList->DataSize() * sizeof(BlockType) * mQueueSize);
818  }
819 
820  inline void Reallocate()
821  {
822  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
823  mpData = (BlockType*)realloc(mpData, mpVariablesList->DataSize() * sizeof(BlockType) * mQueueSize);
824  }
825 
826  void DestructElements(SizeType ThisIndex)
827  {
828  if(!mpVariablesList)
829  return;
830 
831  if(mpData == 0)
832  return;
833  BlockType* position = Position(ThisIndex);
834  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
835  it_variable->Destruct(position + LocalOffset(*it_variable));
836  }
837  }
838 
839  void DestructAllElements()
840  {
841  if(!mpVariablesList)
842  return;
843 
844  if(mpData == 0)
845  return;
846 
847  const SizeType size = mpVariablesList->DataSize();
848  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
849  BlockType* position = mpData + LocalOffset(*it_variable);
850  for(SizeType i = 0 ; i < mQueueSize ; i++) {
851  it_variable->Destruct(position + i * size);
852  }
853  }
854  }
855 
856 
857  void AssignData(BlockType* Source, BlockType* Destination)
858  {
859  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
860  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
861  const SizeType offset = LocalOffset(*it_variable);
862  it_variable->Assign(Source + offset, Destination + offset);
863  }
864  }
865 
866  inline SizeType LocalOffset(VariableData const & rThisVariable) const
867  {
868  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
869  return mpVariablesList->Index(rThisVariable.SourceKey());
870  }
871 
872  inline BlockType* Position(VariableData const & rThisVariable) const
873  {
874  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
875  KRATOS_DEBUG_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
876  return mpCurrentPosition + mpVariablesList->Index(rThisVariable.SourceKey());
877  }
878 
879  inline BlockType* Position(VariableData const & rThisVariable, SizeType ThisIndex) const
880  {
881  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
882  KRATOS_DEBUG_ERROR_IF_NOT(mpVariablesList->Has(rThisVariable)) << "This container only can store the variables specified in its variables list. The variables list doesn't have this variable:" << rThisVariable << std::endl;
883  KRATOS_DEBUG_ERROR_IF((ThisIndex + 1) > mQueueSize) << "Trying to access data from step " << ThisIndex << " but only " << mQueueSize << " steps are stored." << std::endl;
884  return Position(ThisIndex) + mpVariablesList->Index(rThisVariable.SourceKey());
885  }
886 
887  inline BlockType* Position() const
888  {
889  return mpCurrentPosition;
890  }
891 
892  inline BlockType* Position(SizeType ThisIndex) const
893  {
894  KRATOS_DEBUG_ERROR_IF(!mpVariablesList) << "This container don't have a variables list assigned. A possible reason is creating a node without a model part." << std::endl;
895  const SizeType total_size = TotalSize();
896  BlockType* position = mpCurrentPosition + ThisIndex * mpVariablesList->DataSize();
897  return (position < mpData + total_size) ? position : position - total_size;
898  }
899 
903 
904  friend class Serializer;
905 
906  void save(Serializer& rSerializer) const
907  {
908  KRATOS_ERROR_IF(!mpVariablesList) << "Cannot save a container with no variables list assigned" << std::endl;
909  KRATOS_ERROR_IF(mpData == 0) << "Cannot save an empty variables list container" << std::endl;
910 
911  rSerializer.save("Variables List", mpVariablesList);
912  rSerializer.save("QueueSize", mQueueSize);
913  if(mpVariablesList->DataSize() != 0 )
914  rSerializer.save("QueueIndex", SizeType(mpCurrentPosition-mpData)/mpVariablesList->DataSize());
915  else
916  rSerializer.save("QueueIndex", SizeType(0));
917 
918  const SizeType size = mpVariablesList->DataSize();
919  for(VariablesList::const_iterator it_variable = mpVariablesList->begin(); it_variable != mpVariablesList->end() ; it_variable++) {
920  BlockType* position = mpData + LocalOffset(*it_variable);
921  for(SizeType i = 0 ; i < mQueueSize ; i++) {
922  //rSerializer.save("VariableName", it_variable->Name());
923  it_variable->Save(rSerializer, position + i * size);
924  }
925  }
926  }
927 
928  void load(Serializer& rSerializer)
929  {
930  rSerializer.load("Variables List", mpVariablesList);
931  rSerializer.load("QueueSize", mQueueSize);
932  SizeType queue_index;
933  rSerializer.load("QueueIndex", queue_index);
934  Allocate();
935 
936  // Setting the current position at the begining of data
937  if(queue_index > mQueueSize)
938  KRATOS_THROW_ERROR(std::invalid_argument, "Invalid Queue index loaded : ", queue_index)
939  mpCurrentPosition = mpData + queue_index * mpVariablesList->DataSize();
940 
941  std::string name;
942  for(SizeType i = 0 ; i < mQueueSize ; i++)
943  AssignZero(i);
944 
945  const SizeType size = mpVariablesList->DataSize();
946  for(VariablesList::const_iterator it_variable = mpVariablesList->begin() ;it_variable != mpVariablesList->end() ; it_variable++) {
947  BlockType* position = mpData + LocalOffset(*it_variable);
948  for(SizeType i = 0 ; i < mQueueSize ; i++) {
949  //rSerializer.load("VariableName", name);
950  it_variable->Load(rSerializer, position + i * size);
951  }
952  }
953  }
954 
958 
959 
963 
964 
968 
969 
971 
972 }; // Class VariablesListDataValueContainer
973 
977 
978 
982 
983 
985 inline std::istream& operator >> (std::istream& rIStream,
987 
989 inline std::ostream& operator << (std::ostream& rOStream,
990  const VariablesListDataValueContainer& rThis)
991 {
992  rThis.PrintInfo(rOStream);
993  rOStream << std::endl;
994  rThis.PrintData(rOStream);
995 
996  return rOStream;
997 }
999 
1000 
1001 } // namespace Kratos.
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
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
const std::string & Name() const
Definition: variable_data.h:201
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
A shared variable list gives the position of each variable in the containers sharing it.
Definition: variables_list_data_value_container.h:61
KRATOS_CLASS_POINTER_DEFINITION(VariablesListDataValueContainer)
Pointer definition of VariablesListDataValueContainer.
bool IsEmpty()
Definition: variables_list_data_value_container.h:712
SizeType TotalDataSize()
Definition: variables_list_data_value_container.h:621
void AssignZero(const SizeType QueueIndex)
Definition: variables_list_data_value_container.h:686
const TDataType * pFastGetValue(const Variable< TDataType > &rThisVariable) const
Definition: variables_list_data_value_container.h:355
void Clear()
Definition: variables_list_data_value_container.h:410
VariablesListDataValueContainer & operator=(const VariablesListDataValueContainer &rOther)
Assignment operator.
Definition: variables_list_data_value_container.h:239
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue)
Definition: variables_list_data_value_container.h:400
void AssignData(BlockType *Source, SizeType QueueIndex)
Definition: variables_list_data_value_container.h:629
TDataType & FastGetValue(const Variable< TDataType > &rThisVariable, SizeType QueueIndex, SizeType ThisPosition)
Definition: variables_list_data_value_container.h:332
std::string Info() const
Turn back information as a string.
Definition: variables_list_data_value_container.h:725
SizeType Size() const
Definition: variables_list_data_value_container.h:379
TDataType & FastGetCurrentValue(const Variable< TDataType > &rThisVariable, SizeType ThisPosition)
Definition: variables_list_data_value_container.h:341
~VariablesListDataValueContainer()
Destructor.
Definition: variables_list_data_value_container.h:180
void PushFront()
Definition: variables_list_data_value_container.h:660
TDataType & FastGetValue(const Variable< TDataType > &rThisVariable, SizeType QueueIndex)
Definition: variables_list_data_value_container.h:326
SizeType TotalSize() const
Definition: variables_list_data_value_container.h:392
SizeType QueueSize() const
Definition: variables_list_data_value_container.h:387
const TDataType & FastGetCurrentValue(const Variable< TDataType > &rThisVariable, SizeType ThisPosition) const
Definition: variables_list_data_value_container.h:374
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: variables_list_data_value_container.h:737
TDataType & GetValue(const Variable< TDataType > &rThisVariable, SizeType QueueIndex)
Definition: variables_list_data_value_container.h:290
VariablesListDataValueContainer(VariablesList::Pointer pVariablesList, SizeType NewQueueSize=1)
Variables list constructor.
Definition: variables_list_data_value_container.h:133
BlockType * Data(SizeType QueueIndex)
Definition: variables_list_data_value_container.h:602
void SetVariablesList(VariablesList::Pointer pVariablesList)
Definition: variables_list_data_value_container.h:444
TDataType & GetValue(const Variable< TDataType > &rThisVariable)
Definition: variables_list_data_value_container.h:282
std::size_t SizeType
The size type definition.
Definition: variables_list_data_value_container.h:79
VariablesListDataValueContainer(SizeType NewQueueSize=1)
Default constructor.
Definition: variables_list_data_value_container.h:86
VariablesListDataValueContainer(VariablesListDataValueContainer const &rOther)
Copy constructor.
Definition: variables_list_data_value_container.h:109
std::size_t IndexType
The index type definition.
Definition: variables_list_data_value_container.h:76
const TDataType & GetValue(const Variable< TDataType > &rThisVariable) const
Definition: variables_list_data_value_container.h:298
bool Has(const VariableData &rThisVariable) const
This method returns if a certain variable is stored in the data value container.
Definition: variables_list_data_value_container.h:704
const TDataType & FastGetValue(const Variable< TDataType > &rThisVariable, SizeType QueueIndex) const
Definition: variables_list_data_value_container.h:361
TDataType & operator()(const Variable< TDataType > &rThisVariable, SizeType QueueIndex)
Definition: variables_list_data_value_container.h:201
const TDataType & operator[](const Variable< TDataType > &rThisVariable) const
Definition: variables_list_data_value_container.h:233
const TDataType & GetValue(const Variable< TDataType > &rThisVariable, SizeType QueueIndex) const
Definition: variables_list_data_value_container.h:306
VariablesListDataValueContainer(VariablesList::Pointer pVariablesList, BlockType const *ThisData, SizeType NewQueueSize=1)
Variables list and data constructor.
Definition: variables_list_data_value_container.h:156
const TDataType & FastGetValue(const Variable< TDataType > &rThisVariable, SizeType QueueIndex, SizeType ThisPosition) const
Definition: variables_list_data_value_container.h:367
void SetVariablesList(VariablesList::Pointer pVariablesList, SizeType ThisQueueSize)
Definition: variables_list_data_value_container.h:466
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: variables_list_data_value_container.h:731
const VariablesList::Pointer pGetVariablesList() const
Definition: variables_list_data_value_container.h:429
void Resize(SizeType NewSize)
Definition: variables_list_data_value_container.h:490
VariablesList::Pointer pGetVariablesList()
Definition: variables_list_data_value_container.h:424
TDataType & operator()(const Variable< TDataType > &rThisVariable)
Definition: variables_list_data_value_container.h:195
BlockType * Data(VariableData const &rThisVariable)
Definition: variables_list_data_value_container.h:607
TDataType & operator[](const Variable< TDataType > &rThisVariable)
Definition: variables_list_data_value_container.h:228
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue, SizeType QueueIndex)
Definition: variables_list_data_value_container.h:405
const TDataType & operator()(const Variable< TDataType > &rThisVariable, SizeType QueueIndex) const
Definition: variables_list_data_value_container.h:213
SizeType DataSize()
Definition: variables_list_data_value_container.h:613
const TDataType & operator()(const Variable< TDataType > &rThisVariable) const
Definition: variables_list_data_value_container.h:207
VariablesList::BlockType BlockType
The block type definition.
Definition: variables_list_data_value_container.h:70
BlockType * ContainerType
Type of the container used for variables.
Definition: variables_list_data_value_container.h:73
void AssignZero()
Definition: variables_list_data_value_container.h:678
const TDataType & FastGetValue(const Variable< TDataType > &rThisVariable) const
Definition: variables_list_data_value_container.h:349
const VariablesList & GetVariablesList() const
Definition: variables_list_data_value_container.h:439
const BlockType * Data() const
Definition: variables_list_data_value_container.h:597
BlockType * Data()
Definition: variables_list_data_value_container.h:592
TDataType * pFastGetValue(const Variable< TDataType > &rThisVariable)
Definition: variables_list_data_value_container.h:320
TDataType & FastGetValue(const Variable< TDataType > &rThisVariable)
Definition: variables_list_data_value_container.h:314
VariablesList & GetVariablesList()
Definition: variables_list_data_value_container.h:434
void CloneFront()
Definition: variables_list_data_value_container.h:641
Holds a list of variables and their position in VariablesListDataValueContainer.
Definition: variables_list.h:50
double BlockType
Definition: variables_list.h:64
boost::indirect_iterator< VariablesContainerType::const_iterator > const_iterator
Definition: variables_list.h:77
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
#define KRATOS_DEBUG_ERROR_IF_NOT(conditional)
Definition: exception.h:172
Parameters GetValue(Parameters &rParameters, const std::string &rEntry)
Definition: add_kratos_parameters_to_python.cpp:53
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
def load(f)
Definition: ode_solve.py:307
float temp
Definition: rotating_cone.py:85
integer i
Definition: TensorModule.f:17
Configure::ContainerType ContainerType
Definition: transfer_utility.h:247