NeoPZ
TPZEquationFilter.h
Go to the documentation of this file.
1 #ifndef PZEQUATIONFILTERHPP
2 #define PZEQUATIONFILTERHPP
3 
4 #include "pzvec.h"
5 #include "pzfmatrix.h"
6 
7 #include <set>
8 
9 class TPZEquationFilter : public TPZSavable {
10 public:
11 
13 
14  TPZEquationFilter(int64_t numeq) : fNumEq(numeq), fIsActive(false), fActiveEqs(), fDestIndices()
15  {
16 
17  }
18 
21  {
23  }
24 
26  {
28  }
29 
31  {
32  this->fNumEq = cp.fNumEq;
33  this->fIsActive = cp.fIsActive;
34  this->fActiveEqs = cp.fActiveEqs;
35  this->fDestIndices = cp.fDestIndices;
36  return *this;
37  }
38 
39  int ClassId() const override{
40  return Hash("TPZEquationFilter");
41  }
42 
43  void Read(TPZStream& buf, void* context) override {
44  buf.Read(&fNumEq);
45  buf.Read(fIsActive);
46  buf.Read(fActiveEqs);
47  buf.Read(fDestIndices);
48  }
49 
50  void Write(TPZStream &buf, int withclassid) const override{
51  buf.Write(&fNumEq);
52  buf.Write(fIsActive);
53  buf.Write(fActiveEqs);
54  buf.Write(fDestIndices);
55  }
56 
58  void SetMinMaxEq(int64_t mineq, int64_t maxeq)
59  {
60  if (mineq < 0 || mineq > fNumEq ||
61  maxeq < 0 || maxeq > fNumEq ||
62  mineq > maxeq){
63  DebugStop();
64  }
65 
66  const int64_t n = maxeq-mineq;
67  TPZVec<int64_t> activeEquations(n);
68  for(int64_t i = 0; i < n; i++){
69  activeEquations[i] = i + mineq;
70  }
71  this->SetActiveEquations( activeEquations );
72  }
73 
76  {
78 
79  fIsActive = true;
81  std::set<int64_t> activeset;
82  int64_t neq = active.size();
83  if (neq) {
84  activeset.insert(&active[0], &active[neq-1]+1);
85  }
86 
88  fDestIndices.Fill(-1);
89  int64_t count = 0;
90  fActiveEqs.Resize(activeset.size());
91  for (std::set<int64_t>::iterator it=activeset.begin(); it != activeset.end(); it++) {
92  fActiveEqs[count] = *it;
93  fDestIndices[*it] = count++;
94  }
95  }
96 
98  void Reset()
99  {
100  fIsActive = false;
101  fActiveEqs.Resize(0);
102  fDestIndices.Resize(0);
103  }
104 
109  void Filter(TPZVec<int64_t> &orig, TPZVec<int64_t> &dest) const
110  {
111  if (fDestIndices.size() == 0) {
112  return;
113  }
114  else {
115  int64_t count = 0;
116  int64_t numeq = dest.size();
117  for (int64_t i=0; i<numeq; i++) {
118  if (fDestIndices[dest[i]] != -1) {
119  orig[count] = orig[i];
120  dest[count] = fDestIndices[dest[i]];
121  count++;
122  }
123  }
124  orig.Resize(count);
125  dest.Resize(count);
126  }
127  }
128 
132  void Filter(TPZVec<int64_t> &dest) const
133  {
134  if (fDestIndices.size() == 0) {
135  return;
136  }
137  else{
138  int64_t count = 0;
139  int64_t numeq = dest.size();
140  for (int64_t i=0; i<numeq; i++) {
141  if (fDestIndices[dest[i]] != -1) {
142  dest[count] = fDestIndices[dest[i]];
143  count++;
144  }
145  }
146  dest.Resize(count);
147  }
148  }
149 
151  int64_t NActiveEquations() const
152  {
153  if (IsActive()) {
154  return fActiveEqs.size();
155  }
156  else
157  {
158  return fNumEq;
159  }
160  }
161 
163  int64_t NEqExpand() const
164  {
165  return fNumEq;
166  }
167 
171  bool IsActive() const
172  {
173  return fIsActive;
174 
175  }
176 
180  template<class TVar>
181  void Scatter(const TPZFMatrix<TVar> &vsmall, TPZFMatrix<TVar> &vexpand) const
182  {
183  int64_t neqcondense = this->NActiveEquations();
184  if(vsmall.Rows() != neqcondense || vexpand.Rows() != fNumEq)
185  {
186  DebugStop();
187  }
188  if(! IsActive())
189  {
190  vexpand = vsmall;
191  return;
192  }
193  vexpand.Zero();
194 
195 #ifdef PZDEBUG
196  {
197  for(int64_t i=0; i<neqcondense; i++)
198  {
199  if(fActiveEqs[i] >= fNumEq)
200  {
201  DebugStop();
202  }
203  }
204  }
205 #endif
206  for(int64_t i=0; i<neqcondense; i++) vexpand(fActiveEqs[i],0) = vsmall.GetVal(i,0);
207  }
208 
212  template<class T>
213  void Gather(const TPZFMatrix<T> &large, TPZFMatrix<T> &gathered) const
214  {
215  int64_t neqcondense = this->NActiveEquations();
216  if(gathered.Rows() != neqcondense || large.Rows() != fNumEq)
217  {
218  DebugStop();
219  }
220  if(! IsActive())
221  {
222  gathered = large;
223  return;
224  }
225  gathered.Zero();
226  for(int64_t i=0; i<neqcondense; i++) gathered(i,0) = large.GetVal(fActiveEqs[i],0);
227  }
228 
232  int64_t NumActive(int64_t minindex, int64_t maxindex) const
233  {
234  if (minindex < 0 || maxindex < 0 || minindex > fNumEq || maxindex > fNumEq ||
235  maxindex < minindex) {
236  DebugStop();
237  }
238  if (!IsActive()) {
239  return maxindex-minindex;
240  }
241  int numactive = 0;
242  for (int64_t i=minindex; i<maxindex; i++) {
243  if (fDestIndices[i] != -1) {
244  numactive++;
245  }
246  }
247  return numactive;
248  }
249 
250  void FilterSkyline(TPZVec<int64_t> &skyline) const
251  {
252  if (!IsActive()) {
253  return;
254  }
255 
256  for (int64_t ieq = 0; ieq<fActiveEqs.size(); ieq++)
257  {
258  int64_t skyl = skyline[fActiveEqs[ieq]];
259  while (fDestIndices[skyl] == -1 && skyl < fNumEq) {
260  skyl++;
261  }
262 #ifdef PZDEBUG
263  // all active equations should have a destination
264  if (skyl > fActiveEqs[ieq] || fDestIndices[skyl] < 0) {
265  DebugStop();
266  }
267 #endif
268  skyline[ieq] = fDestIndices[skyl];
269  }
270  skyline.Resize(fActiveEqs.size());
271 
272  }
273 
274  void SetNumEq(const int64_t numEq){
275  fNumEq = numEq;
276  }
277 
278 private:
279 
281  int64_t fNumEq;
282 
284  bool fIsActive;
285 
288 
291 
292 };
293 
294 #endif
void Scatter(const TPZFMatrix< TVar > &vsmall, TPZFMatrix< TVar > &vexpand) const
int64_t fNumEq
Numero de equacoes do sistema original.
void Filter(TPZVec< int64_t > &orig, TPZVec< int64_t > &dest) const
Templated vector implementation.
TPZEquationFilter(int64_t numeq)
void FilterSkyline(TPZVec< int64_t > &skyline) const
TPZVec< int64_t > fDestIndices
Posicao das equacoes originais no sistema reduzido.
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
void SetNumEq(const int64_t numEq)
TPZEquationFilter(const TPZEquationFilter &cp)
bool fIsActive
Flag indicating whether the filter is active.
TPZEquationFilter & operator=(const TPZEquationFilter &cp)
int Zero() override
Makes Zero all the elements.
Definition: pzfmatrix.h:651
int64_t size() const
Returns the number of elements of the vector.
Definition: pzvec.h:196
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object reallocating the necessary storage, copying the existing objects to the new...
Definition: pzvec.h:373
void Gather(const TPZFMatrix< T > &large, TPZFMatrix< T > &gathered) const
Reduce the vector to the number of active equations.
int64_t NActiveEquations() const
Retorna o numero de equacoes ativas do sistema.
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
int64_t NumActive(int64_t minindex, int64_t maxindex) const
Returns the number of active equations between [minindex,maxindex].
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
int64_t NEqExpand() const
Retorna o numero de equacoes do sistema original.
void SetActiveEquations(TPZVec< int64_t > &active)
Define as equacoes ativas.
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
int ClassId() const override
Define the class id associated with the class.
void SetMinMaxEq(int64_t mineq, int64_t maxeq)
Define as equacoes ativas de [mineq, maxeq)
void Fill(const T &copy, const int64_t from=0, const int64_t numelem=-1)
Will fill the elements of the vector with a copy object.
Definition: pzvec.h:460
void Filter(TPZVec< int64_t > &dest) const
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
void Read(TPZStream &buf, void *context) override
read objects from the stream
TPZVec< int64_t > fActiveEqs
Equacoes ativas.
This class defines the interface to save and restore objects from TPZStream objects. Persistency.
Definition: TPZSavable.h:67
const TVar & GetVal(const int64_t row, const int64_t col) const override
Get values without bounds checking This method is faster than "Get" if DEBUG is defined.
Definition: pzfmatrix.h:566
void Reset()
Reset method.
virtual void Read(bool &val)
Definition: TPZStream.cpp:91