BoxSmoother.h 5.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef AMDIS_BOXSMOOTHER_H
#define AMDIS_BOXSMOOTHER_H

#include "SmootherBase.h"
#include "MemoryManager.h"
#include "DOFMatrix.h"
#include "SparseVector.h"
#include "MatrixVector.h"
#include "Global.h"
#include "GaussElimination.h"
#include "FiniteElemSpace.h"
#include "GaussElimination.h"
#include <set>

namespace AMDiS {

  class BoxSmoother 
    : public SmootherBase<Matrix<DOFMatrix*>, 
			  Vector<SparseVector<double>*>,
20
			  Vector< std::set<DegreeOfFreedom>*> >
21
22
23
24
25
26
27
  {
  public:
    MEMORY_MANAGED(BoxSmoother);

    class Creator 
      : public SmootherCreator<Matrix<DOFMatrix*>, 
			       Vector<SparseVector<double>*>,
28
			       Vector< std::set<DegreeOfFreedom>*> >
29
30
31
32
33
34
    {
    public:
      MEMORY_MANAGED(Creator);

      SmootherBase<Matrix<DOFMatrix*>, 
		   Vector<SparseVector<double>*>,
35
		   Vector< std::set<DegreeOfFreedom>*> >*
36
37
38
39
40
      create() {
	return NEW BoxSmoother(this->name);
      };
    };

41
    BoxSmoother(std::string name)
42
43
      : SmootherBase<Matrix<DOFMatrix*>, 
		     Vector<SparseVector<double>*>,
44
		     Vector< std::set<DegreeOfFreedom>*> >(name),
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	boxMatrix_(NULL),
	boxSolution_(NULL),
	boxRHS_(NULL),
	numComponents_(0),
	sumDOFs_(0)
    {
      directSolver_ = 
	NEW GaussElimination<Matrix<double>, Vector<double> >();
    };
 
    virtual ~BoxSmoother()
    {
      if(boxMatrix_) DELETE boxMatrix_;
      if(boxSolution_) DELETE boxSolution_;
      if(boxRHS_) DELETE boxRHS_;
      DELETE directSolver_;
    };

    void smooth(Matrix<DOFMatrix*>                       *matrix,
		Vector<SparseVector<double>*>            *solution,
		Vector<SparseVector<double>*>            *rhs,
		int                                       iterations,
67
		const Vector< std::set<DegreeOfFreedom>*> &dofSet)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    {
      FUNCNAME("BoxSmoother::smooth()");

      int i, j, k;

      // allocate memory
      if(!boxMatrix_) {
	numComponents_ = solution->getSize();
	numDOFs_.resize(numComponents_);
	sumDOFs_ = 0;
	for(i = 0; i < numComponents_; i++) {
	  numDOFs_[i] = (*solution)[i]->getFESpace()->getBasisFcts()->getNumber();
	  sumDOFs_ += numDOFs_[i];
	}

	boxMatrix_ = NEW Matrix<double>(sumDOFs_, sumDOFs_);
	boxSolution_ = NEW Vector<double>(sumDOFs_);
	boxRHS_ = NEW Vector<double>(sumDOFs_);
      }

      // create local equation system
      int boxSize = 0;
90
91
92
93
      std::vector<int> offset(numComponents_, 0);
      std::vector< std::map<DegreeOfFreedom, int> > indexOfDOF(numComponents_);
      std::set<DegreeOfFreedom>::const_iterator dofIt, dofBegin, dofEnd;
      std::vector<MatEntry>::iterator rowIt, rowBegin, rowEnd;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

      int pos;
      for(i = 0; i < numComponents_; i++) {
	if(dofSet[i]) {
	  offset[i] = boxSize;
	  boxSize += numDOFs_[i];
	  dofBegin = dofSet[i]->begin();
	  dofEnd = dofSet[i]->end();
	  pos = 0;
	  for(dofIt = dofBegin; dofIt != dofEnd; ++dofIt) {
	    indexOfDOF[i][*dofIt] = pos + 1;
// 	    MSG("%d : %d, %d \n", *dofIt, i, pos);
	    ++pos;
	  }
	}
      }

      for(i = 0; i < boxSize; i++) {
	for(j = 0; j < boxSize; j++) {
	  (*boxMatrix_)[i][j] = 0.0;
	}
	(*boxRHS_)[i] = 0.0;
      }

      pos = 0;
      for(i = 0; i < numComponents_; i++) {
	if(dofSet[i]) {
	  dofBegin = dofSet[i]->begin();
	  dofEnd = dofSet[i]->end();
	  for(dofIt = dofBegin; dofIt != dofEnd; ++dofIt) {

	    // fill box rhs
	    (*boxRHS_)[pos] = (*((*rhs)[i]))[*dofIt];

	    // fill box matrix
	    for(j = 0; j < numComponents_; j++) {
	      if((*matrix)[i][j] != NULL) {
		rowBegin = (*((*matrix)[i][j]))[*dofIt].begin();
		rowEnd = (*((*matrix)[i][j]))[*dofIt].end();
		for(rowIt = rowBegin; rowIt != rowEnd; ++rowIt) {
		  if(rowIt->col == DOFMatrix::UNUSED_ENTRY) continue;
		  if(rowIt->col == DOFMatrix::NO_MORE_ENTRIES) break;
		  int ind = indexOfDOF[j][rowIt->col] - 1;
		  if(ind != -1) {
		    (*boxMatrix_)[pos][offset[j] + ind] = rowIt->entry;
		  } else {
		    (*boxRHS_)[pos] -= 
		      rowIt->entry * (*((*solution)[j]))[rowIt->col];
		  }
		}
	      } else {
		for(k = 0; k < numDOFs_[j]; k++) {
		  (*boxMatrix_)[pos][offset[j] + k] = 0.0;
		}
	      }
	    }

	    ++pos;
	  }
	}
      }

//       int a,b;

//       for(i = 0; i < numComponents_; i++) {
// 	MSG("DOFs %d: ", i);
// 	dofBegin = dofSet[i]->begin();
// 	dofEnd = dofSet[i]->end();
// 	for(dofIt = dofBegin; dofIt != dofEnd; ++dofIt) {
// 	  MSG("%d ", *dofIt);
// 	}
// 	MSG("\n");
//       }

//       for(a = 0; a < numComponents_; a++) {
// 	for(b = 0; b < numComponents_; b++) {
// 	  MSG("matrix[%d][%d]:\n", a, b);
// 	  if((*matrix)[a][b]) {
// 	    (*matrix)[a][b]->print();
// 	    MSG("solution[%d]\n", b);
// 	    (*solution)[b]->print();
// 	    MSG("rhs[%d]\n", a);
// 	    (*rhs)[a]->print();
// 	  }
// 	}
//       }

//       MSG("matrix:\n");
//       for(a = 0; a < boxSize; a++) {
// 	for(b = 0; b < boxSize; b++) {
// 	  MSG("%e ", (*boxMatrix_)[a][b]);
// 	}
// 	MSG("\n");
//       }

//       MSG("rhs:\n");
//       for(a = 0; a < boxSize; a++) {
// 	MSG("%e\n", (*boxRHS_)[a]);
//       }

      // apply direct solver
      directSolver_->solve(*boxMatrix_, *boxRHS_, *boxSolution_, boxSize);

//       MSG("solution:\n");
//       for(a = 0; a < boxSize; a++) {
// 	MSG("%e\n", (*boxSolution_)[a]);
//       }

//       WAIT_REALLY;

      // copy solution
      pos = 0;
      for(i = 0; i < numComponents_; i++) {
	dofBegin = dofSet[i]->begin();
	dofEnd = dofSet[i]->end();
	for(dofIt = dofBegin; dofIt != dofEnd; ++dofIt) {
	  (*((*solution)[i]))[*dofIt] = (*boxSolution_)[pos];	    
	  ++pos;
	}
      }
    };

  protected:
    Matrix<double> *boxMatrix_;
    Vector<double> *boxSolution_;
    Vector<double> *boxRHS_;
    int numComponents_;
221
    std::vector<int> numDOFs_;
222
223
224
225
226
227
    int sumDOFs_;
    DirectSolverInterface<Matrix<double>, Vector<double> > *directSolver_;
  };
}

#endif