NeoPZ
TPZMatWithMem.h
Go to the documentation of this file.
1 
6 #ifndef PZMATWITHMEM_H
7 #define PZMATWITHMEM_H
8 
9 #include "TPZMaterial.h"
10 #include "pzadmchunk.h"
11 #include <memory>
12 
22 template <class TMEM, class TFather = TPZMaterial>
23 class TPZMatWithMem : public TFather {
24 public:
25 
27  TPZMatWithMem();
28 
31  TPZMatWithMem(int id);
32 
39 
40  virtual ~TPZMatWithMem();
41 
43  virtual std::string Name() override {
44  return "TPZMatWithMem< >";
45  }
46 
48  virtual void PrintMem(std::ostream &out = std::cout, const int memory = 0);
49 
51  virtual void Print(std::ostream &out) override;
52 
53  virtual TMEM &MemItem(const int i) const;
54 
55 public:
56 
58  int ClassId() const override;
59 
60  void Write(TPZStream &buf, int withclassid) const override;
61 
62  void Read(TPZStream &buf, void *context) override;
63 
64  std::shared_ptr<TPZAdmChunkVector<TMEM>> & GetMemory();
65 
66  void SetMemory(std::shared_ptr<TPZAdmChunkVector<TMEM>> & memory);
67 
73  virtual int PushMemItem(int sourceIndex = -1) override;
74 
76  virtual void FreeMemItem(int index) override;
77 
79  void ResetMemItem(int index) {
80  this->MemItem(index) = fDefaultMem;
81  }
82 
84  void ResetMemory() {
85  int nmem = fMemory->NElements();
86  for (unsigned int im = 0; im < nmem; im++) {
87  ResetMemItem(im);
88  }
89  }
90 
92  virtual void SetDefaultMem(TMEM & defaultMem);
93 
95  virtual TMEM &GetDefaultMemory()
96  {
97  return fDefaultMem;
98  }
99 
101  virtual void SetUpdateMem(bool update = 1);
102 
104  virtual bool GetUpdateMem();
105 
106 protected:
107 
109  std::shared_ptr<TPZAdmChunkVector<TMEM>> fMemory;
110 
113 
116 };
117 
118 template <class TMEM, class TFather>
121 TFather(),
122 fMemory(new TPZAdmChunkVector<TMEM>()), fDefaultMem(), fUpdateMem(0) {
123 }
124 
125 template <class TMEM, class TFather>
128 TFather(id),
129 fMemory(new TPZAdmChunkVector<TMEM>()), fDefaultMem(), fUpdateMem(false) {
130 }
131 
132 template <class TMEM, class TFather>
135 TFather(mat),
137  *fMemory = *mat.fMemory;
138 }
139 
140 template <class TMEM, class TFather>
142 }
143 
144 template <class TMEM, class TFather>
145 void TPZMatWithMem<TMEM, TFather>::Print(std::ostream &out) {
146  out << __PRETTY_FUNCTION__ << std::endl;
147  TFather::Print(out);
148 
149  out << "\nfDefaultMem = \n" << fDefaultMem;
150  out << "\nfUpdateMem = " << fUpdateMem;
151  int size = fMemory->NElements();
152  out << "\nfMemory with " << size << " elements";
153  for (int i = 0; i < size; i++) {
154  out << "\nfMemory element : " << i << std::endl;
155  this->MemItem(i).Print(out);
156  }
157 
158 }
159 
160 template <class TMEM, class TFather>
161 void TPZMatWithMem<TMEM, TFather>::PrintMem(std::ostream &out, const int memory) {
162  int size = fMemory->NElements();
163  if (memory >= 0 && memory < size) {
164  out << "fMemory element : " << memory << std::endl;
165  this->MemItem(memory).Print(out);
166  } else {
167  out << "Memory index out of range : memory " << memory << " no elements " << size << std::endl;
168  }
169 }
170 
171 template <class TMEM, class TFather>
172 TMEM &TPZMatWithMem<TMEM, TFather>::MemItem(const int i) const {
173  return fMemory.get()->operator [](i);
174 }
175 
176 template <class TMEM, class TFather>
178  return Hash("TPZMatWithMem") ^ TFather::ClassId() << 1 ^ TMEM().ClassId() << 2;
179 }
180 
181 template <class TMEM, class TFather>
182 void TPZMatWithMem<TMEM, TFather>::Write(TPZStream &buf, int withclassid) const {
183  TFather::Write(buf, withclassid);
184  int updatemem = fUpdateMem;
185  buf.Write(&updatemem);
186  fDefaultMem.Write(buf, 0);
188 }
189 
190 template <class TMEM, class TFather>
192  TFather::Read(buf, context);
193  int updatemem;
194  buf.Read(&updatemem);
195  if (updatemem) {
196  fUpdateMem = true;
197  } else {
198  fUpdateMem = false;
199  }
200  fDefaultMem.Read(buf, 0);
201  fMemory = std::dynamic_pointer_cast<TPZAdmChunkVector<TMEM> >(TPZPersistenceManager::GetSharedPointer(&buf));
202 
203 }
204 
205 template <class TMEM, class TFather>
206 std::shared_ptr<TPZAdmChunkVector<TMEM>> & TPZMatWithMem<TMEM, TFather>::GetMemory() {
207  return fMemory;
208 }
209 
210 template <class TMEM, class TFather>
212  fMemory = memory;
213 }
214 
215 template <class TMEM, class TFather>
217  int index = fMemory->AllocateNewElement();
218  if (sourceIndex < 0) {
219  this->ResetMemItem(index);
220  } else {
221  this->MemItem(index) = this->MemItem(sourceIndex);
222  }
223  return index;
224 }
225 
226 template <class TMEM, class TFather>
228  fMemory->SetFree(index);
229 }
230 
231 template <class TMEM, class TFather>
233  fDefaultMem = defaultMem;
234 }
235 
236 template <class TMEM, class TFather>
238  fUpdateMem = update;
239 }
240 
241 template <class TMEM, class TFather>
243  return fUpdateMem;
244 }
245 
246 #endif
virtual void Print(std::ostream &out) override
Prints out the data associated with the material.
virtual TMEM & GetDefaultMemory()
Access the default memory settings for initislization.
Definition: TPZMatWithMem.h:95
void SetMemory(std::shared_ptr< TPZAdmChunkVector< TMEM >> &memory)
virtual std::string Name() override
Returns the name of the material.
Definition: TPZMatWithMem.h:43
Declarates the TPZBlock<REAL>class which implements block matrices.
void ResetMemItem(int index)
Definition: TPZMatWithMem.h:79
int ClassId() const override
Unique identifier for serialization purposes.
void Write(TPZStream &buf, int withclassid) const override
virtual void SetDefaultMem(TMEM &defaultMem)
Sets the default memory settings for initialization.
Implements a chunk vector with free store administration. Utility.
Definition: TPZStream.h:39
Implements an abstract class implementing the memory features.
Definition: TPZMatWithMem.h:23
virtual void FreeMemItem(int index) override
Frees an entry in the material with memory internal history storage.
std::shared_ptr< TPZAdmChunkVector< TMEM > > & GetMemory()
virtual ~TPZMatWithMem()
bool fUpdateMem
Flag to indicate whether the memory data are to be updated in an assemble loop.
void ResetMemory()
Reset all memory items.
Definition: TPZMatWithMem.h:84
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
virtual void SetUpdateMem(bool update=1)
Sets/Unsets the internal memory data to be updated in the next assemble/contribute call...
virtual int PushMemItem(int sourceIndex=-1) override
Pushes a new entry in the context of materials with memory.
TPZMatWithMem()
Default constructor.
void Read(TPZStream &buf, void *context) override
static std::shared_ptr< TPZSavable > GetSharedPointer(const int64_t &objId)
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
TMEM fDefaultMem
Default memory settings.
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
virtual void PrintMem(std::ostream &out=std::cout, const int memory=0)
Prints out the data associated with the material.
virtual TMEM & MemItem(const int i) const
virtual bool GetUpdateMem()
Gets the internal memory data to be updated in the next assemble/contribute call. ...
static void WritePointer(const TPZSavable *obj, TPZStream *stream)
std::shared_ptr< TPZAdmChunkVector< TMEM > > fMemory
Shared pointer to material memory.
virtual void Read(bool &val)
Definition: TPZStream.cpp:91