37 #ifndef __H__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__COMPONENT_GAUSS_SEIDEL__
38 #define __H__UG__LIB_DISC__OPERATOR__LINEAR_OPERATOR__COMPONENT_GAUSS_SEIDEL__
54 template <
typename TDomain,
typename TAlgebra>
88 const std::vector<int>& vSmooth,
const std::vector<number>& vDamp)
109 virtual const char*
name()
const {
return "ComponentGaussSeidel";}
164 template <
typename TDomain,
typename TAlgebra>
174 std::vector<IndexLayout::Element> vIndex;
185 template <
typename TDomain,
typename TAlgebra>
197 const std::vector<std::vector<DoFIndex> >& vvDoFIndex = dimCache.
vvDoFIndex;
198 const std::vector<DenseMatrix< VariableArray2<number> > >& vBlockInverse = dimCache.
vBlockInverse;
201 for(
size_t b = 0; b < vvDoFIndex.size(); ++b)
203 size_t block = (!bReverse) ? b : (vvDoFIndex.size()-1 - b);
206 const std::vector<DoFIndex>& vDoFIndex = vvDoFIndex[block];
208 const size_t numIndex = vDoFIndex.size();
212 typedef typename matrix_type::const_row_iterator const_row_iterator;
213 const static int blockSize = TAlgebra::blockSize;
215 for (
size_t j = 0; j<numIndex; j++){
217 for(const_row_iterator it = A.begin_row(vDoFIndex[j][0]);
218 it != A.end_row(vDoFIndex[j][0]) ; ++it){
219 MatMultAdd(sj, 1.0, sj, -1.0, it.value(), c[it.index()]);
221 s.subassign(j*blockSize,sj);
229 for (
size_t j=0;j<numIndex;j++)
230 DoFRef(c, vDoFIndex[j]) += x[j];
235 template <
typename TDomain,
typename TAlgebra>
247 const std::vector<std::vector<DoFIndex> >& vvDoFIndex = dimCache.
vvDoFIndex;
248 const std::vector<DenseMatrix< VariableArray2<number> > >& vBlockInverse = dimCache.
vBlockInverse;
251 for(
size_t b = 0; b < vvDoFIndex.size(); ++b)
253 size_t block = (!bReverse) ? b : (vvDoFIndex.size()-1 - b);
256 const std::vector<DoFIndex>& vDoFIndex = vvDoFIndex[block];
258 const size_t numIndex = vDoFIndex.size();
262 typedef typename matrix_type::const_row_iterator const_row_iterator;
263 const static int blockSize = TAlgebra::blockSize;
265 for (
size_t i = 0; i<numIndex; i++)
268 for(const_row_iterator it = A.begin_row(vDoFIndex[i][0]);
269 it != A.end_row(vDoFIndex[i][0]) ; ++it)
271 MatMultAdd(si, 1.0, si, -1.0, it.value(), c[it.index()]);
273 s.subassign(i*blockSize, si);
277 for (
size_t i = 0; i<numIndex; ++i)
280 UG_ASSERT(wi!=0.0,
"Huhh: Zero weights found!" << i <<
"dofIndex" << vDoFIndex[i])
289 for (
size_t i=0; i<numIndex;i++)
292 DoFRef(c, vDoFIndex[i]) += x[i]/wi;
298 template<
typename TGroupObj,
typename GF>
301 const std::vector<size_t>& vFullRowCmp,
302 const std::vector<size_t>& vRemainCmp)
305 typedef typename GF::element_type Element;
308 std::vector<DoFIndex> vFullRowDoFIndex;
309 std::vector<Element*> vElem;
316 typedef typename GF::template traits<TGroupObj>::const_iterator GroupObjIter;
317 for(GroupObjIter iter = c.template begin<TGroupObj>();
318 iter != c.template end<TGroupObj>(); ++iter)
321 TGroupObj* groupObj = *iter;
324 vFullRowDoFIndex.clear();
325 for(
size_t f = 0; f < vFullRowCmp.size(); ++f)
326 c.inner_dof_indices(groupObj, vFullRowCmp[f], vFullRowDoFIndex,
false);
329 if(vFullRowDoFIndex.empty())
330 UG_THROW(
"extract_by_grouping: Should not happen.");
334 std::vector<Edge*> vSub;
335 c.collect_associated(vSub, groupObj);
336 for(
size_t i = 0; i < vSub.size(); ++i)
337 for(
size_t f = 0; f < vFullRowCmp.size(); ++f)
338 c.inner_dof_indices(vSub[i], vFullRowCmp[f], vFullRowDoFIndex,
false);
341 std::vector<Face*> vSub;
342 c.collect_associated(vSub, groupObj);
343 for(
size_t i = 0; i < vSub.size(); ++i)
344 for(
size_t f = 0; f < vFullRowCmp.size(); ++f)
345 c.inner_dof_indices(vSub[i], vFullRowCmp[f], vFullRowDoFIndex,
false);
349 c.collect_associated(vElem, groupObj);
352 std::vector<DoFIndex> vDoFIndex;
353 for(
size_t i = 0; i < vElem.size(); ++i)
354 for(
size_t f = 0; f < vRemainCmp.size(); ++f)
355 c.dof_indices(vElem[i], vRemainCmp[f], vDoFIndex,
false,
false);
358 if(vElem.size() > 1){
359 std::sort(vDoFIndex.begin(), vDoFIndex.end());
360 vDoFIndex.erase(std::unique(vDoFIndex.begin(), vDoFIndex.end()), vDoFIndex.end());
364 vDoFIndex.insert(vDoFIndex.end(), vFullRowDoFIndex.begin(), vFullRowDoFIndex.end());
367 vvDoFIndex.push_back(vDoFIndex);
371 template <
typename TDomain,
typename TAlgebra>
380 if(m_vFullRowCmp.empty())
381 UG_THROW(
"ComponentGaussSeidel: No components set.")
384 std::vector<size_t> vFullRowCmp;
385 for(
size_t i = 0; i < m_vFullRowCmp.size(); ++i)
386 vFullRowCmp.push_back(ddinfo->fct_id_by_name(m_vFullRowCmp[i].c_str()));
389 std::vector<size_t> vRemainCmp;
390 for(
size_t f = 0; f < ddinfo->num_fct(); ++f)
391 if(
std::find(vFullRowCmp.begin(), vFullRowCmp.end(), f) == vFullRowCmp.end())
392 vRemainCmp.push_back(f);
395 if(m_vGroupObj.empty()){
397 bool bCarryDoFs =
false;
398 for(
size_t f = 0; f < vFullRowCmp.size(); ++f)
399 if(ddinfo->max_fct_dofs(vFullRowCmp[f], d) > 0)
402 m_vGroupObj.push_back(d);
410 if(
std::find(m_vGroupObj.begin(), m_vGroupObj.end(), d) == m_vGroupObj.end())
414 bool bCarryDoFs =
false;
415 for(
size_t f = 0; f < vFullRowCmp.size(); ++f)
416 if(ddinfo->max_fct_dofs(vFullRowCmp[f], d) > 0)
422 std::vector<std::vector<DoFIndex> >& vvDoFIndex = m_vDimCache[d].vvDoFIndex;
424 case VERTEX: extract_by_grouping<Vertex, GF>(vvDoFIndex, c, vFullRowCmp, vRemainCmp);
break;
425 case EDGE: extract_by_grouping<Edge, GF>(vvDoFIndex, c, vFullRowCmp, vRemainCmp);
break;
426 case FACE: extract_by_grouping<Face, GF>(vvDoFIndex, c, vFullRowCmp, vRemainCmp);
break;
427 case VOLUME: extract_by_grouping<Volume, GF>(vvDoFIndex, c, vFullRowCmp, vRemainCmp);
break;
439 std::vector<std::vector<DoFIndex> >& vvDoFIndex = m_vDimCache[d].vvDoFIndex;
441 for(
size_t j = 0; j < vvDoFIndex.size(); ++j)
444 std::vector<DoFIndex>& vDoFIndex = vvDoFIndex[j];
445 const size_t numIndex = vDoFIndex.size();
448 DoFRef(*m_weight, vDoFIndex[numIndex-1]) = 1.0;
449 for (
size_t i =0; i<numIndex-1; ++i)
450 DoFRef(*m_weight, vDoFIndex[i]) += 1.0;
459 std::vector<std::vector<DoFIndex> >& vvDoFIndex = m_vDimCache[d].vvDoFIndex;
460 std::vector<DenseMatrix<VariableArray2<number> > >& vBlockInverse = m_vDimCache[d].vBlockInverse;
462 vBlockInverse.resize(vvDoFIndex.size());
463 for(
size_t b = 0; b < vvDoFIndex.size(); ++b)
466 std::vector<DoFIndex>& vDoFIndex = vvDoFIndex[b];
470 const size_t numIndex = vDoFIndex.size();
474 BlockInv.resize(numIndex, numIndex);
478 for (
size_t i = 0; i < numIndex; i++)
481 BlockInv(i,i) = m_alpha*
DoFRef(A, vDoFIndex[i], vDoFIndex[i]);
482 BlockInv(numIndex-1,i) =
DoFRef(A, vDoFIndex[numIndex-1], vDoFIndex[i]);
483 BlockInv(i,numIndex-1) =
DoFRef(A, vDoFIndex[i], vDoFIndex[numIndex-1]);
494 if (m_weight.invalid())
496 for (
size_t i = 0; i < numIndex-1; i++)
498 schur += BlockInv(numIndex-1,i)/BlockInv(i,i)*BlockInv(i,numIndex-1);
502 BlockInv(numIndex-1, numIndex-1) = schur + (BlockInv(numIndex-1, numIndex-1)-schur)/m_beta;
507 for (
size_t i = 0; i < numIndex-1; i++)
510 schur += wi2*BlockInv(numIndex-1,i)/BlockInv(i,i)*BlockInv(i,numIndex-1);
515 BlockInv(numIndex-1, numIndex-1) = schur + (BlockInv(numIndex-1, numIndex-1) - schur)/m_beta;
528 std::stringstream ss;
529 ss << d <<
"dim - Elem-Mat "<<b<<
" singular. size: "<<numIndex<<
"\n";
530 for (
size_t j = 0; j < numIndex; j++)
531 ss << vDoFIndex[j][0] <<
", ";
535 for (
size_t j = 0; j < numIndex; j++)
536 for (
size_t k = 0; k < numIndex; k++)
537 BlockInv(j,k) =
DoFRef(A, vDoFIndex[j], vDoFIndex[k]);
549 template <
typename TDomain,
typename TAlgebra>
557 UG_THROW(
"ComponentGaussSeidel: expects correction to be a GridFunction.");
574 extract_blocks(*pMat, *pC);
580 for(
size_t i = 0; i < m_vGroupObj.size(); ++i)
583 const int d = m_vGroupObj[i];
587 if(d < (
int)m_vDamp.size()) damp *= m_vDamp[d];
590 if (m_weight.invalid()) {
591 apply_blocks(*pMat, *pC, *pD, damp, m_vDimCache[d],
false);
592 apply_blocks(*pMat, *pC, *pD, damp, m_vDimCache[d],
true);
594 apply_blocks_weighted(*pMat, *pC, *pD, damp, m_vDimCache[d],
false);
595 apply_blocks_weighted(*pMat, *pC, *pD, damp, m_vDimCache[d],
true);
608 template <
typename TDomain,
typename TAlgebra>
614 newInst->set_debug(debug_writer());
615 newInst->set_damp(this->damping());
616 newInst->set_alpha(this->m_alpha);
617 newInst->set_beta(this->m_beta);
618 newInst->set_weights(this->m_bWeighted);
Definition: smart_pointer.h:296
Definition: smart_pointer.h:108
T * get()
returns encapsulated pointer
Definition: smart_pointer.h:197
ComponentGaussSeidel Preconditioner.
Definition: component_gauss_seidel.h:56
ComponentGaussSeidel(const std::vector< std::string > &vFullRowCmp)
default constructor
Definition: component_gauss_seidel.h:79
virtual bool postprocess()
Postprocess routine.
Definition: component_gauss_seidel.h:135
ComponentGaussSeidel(number relax, const std::vector< std::string > &vFullRowCmp, const std::vector< int > &vSmooth, const std::vector< number > &vDamp)
constructor setting relaxation and type
Definition: component_gauss_seidel.h:87
static const int dim
Definition: component_gauss_seidel.h:62
void set_beta(number beta)
Definition: component_gauss_seidel.h:101
IPreconditioner< TAlgebra > base_type
Definition: component_gauss_seidel.h:72
number m_relax
relaxing parameter
Definition: component_gauss_seidel.h:146
std::vector< std::string > m_vFullRowCmp
components, whose matrix row must be fulfilled completely on gs-block
Definition: component_gauss_seidel.h:154
TAlgebra::matrix_type matrix_type
Definition: component_gauss_seidel.h:68
virtual bool preprocess(SmartPtr< MatrixOperator< matrix_type, vector_type > > pOp)
Preprocess routine.
Definition: component_gauss_seidel.h:166
SmartPtr< GF > m_weight
Definition: component_gauss_seidel.h:151
TAlgebra::vector_type vector_type
Definition: component_gauss_seidel.h:67
GridFunction< TDomain, TAlgebra > GF
World dimension.
Definition: component_gauss_seidel.h:59
number m_beta
Definition: component_gauss_seidel.h:148
bool m_bWeighted
Definition: component_gauss_seidel.h:150
void extract_blocks(const matrix_type &A, const GF &c)
Definition: component_gauss_seidel.h:373
std::vector< int > m_vGroupObj
smooth order
Definition: component_gauss_seidel.h:157
std::vector< number > m_vDamp
Definition: component_gauss_seidel.h:158
bool m_bInit
init flag
Definition: component_gauss_seidel.h:143
GF::element_type Element
Definition: component_gauss_seidel.h:60
matrix_type m_A
Definition: component_gauss_seidel.h:139
GF::side_type Side
Definition: component_gauss_seidel.h:61
virtual const char * name() const
Name of preconditioner.
Definition: component_gauss_seidel.h:109
void set_weights(bool b)
Definition: component_gauss_seidel.h:104
number m_alpha
Definition: component_gauss_seidel.h:147
void apply_blocks_weighted(const matrix_type &A, GF &c, const vector_type &d, number relax, const DimCache &dimCache, bool bReverse)
Definition: component_gauss_seidel.h:237
virtual bool step(SmartPtr< MatrixOperator< matrix_type, vector_type > > pOp, vector_type &c, const vector_type &d)
step method
Definition: component_gauss_seidel.h:551
virtual SmartPtr< ILinearIterator< vector_type > > clone()
Clone.
Definition: component_gauss_seidel.h:610
DimCache m_vDimCache[VOLUME+1]
caching storage
Definition: component_gauss_seidel.h:161
TAlgebra algebra_type
Definition: component_gauss_seidel.h:66
ComponentGaussSeidel(number relax, const std::vector< std::string > &vFullRowCmp)
constructor setting relaxation and type
Definition: component_gauss_seidel.h:83
void apply_blocks(const matrix_type &A, GF &c, const vector_type &d, number relax, const DimCache &dimCache, bool bReverse)
Definition: component_gauss_seidel.h:187
void set_alpha(number alpha)
Definition: component_gauss_seidel.h:98
virtual bool supports_parallel() const
returns if parallel solving is supported
Definition: component_gauss_seidel.h:96
virtual void set_debug(SmartPtr< IDebugWriter< algebra_type > > spDebugWriter)
set debug writer
Definition: debug_writer.h:384
void write_debug(const matrix_type &mat, const char *filename)
write debug output for a matrix (if debug writer set)
Definition: debug_writer.h:399
TAlgebra::vector_type vector_type
type of vector
Definition: debug_writer.h:360
SmartPtr< IDebugWriter< algebra_type > > debug_writer()
returns the debug writer
Definition: debug_writer.h:391
TAlgebra::matrix_type matrix_type
type of matrix
Definition: debug_writer.h:363
Definition: densematrix.h:57
Definition: densevector.h:101
represents numerical solutions on a grid using an algebraic vector
Definition: grid_function.h:121
SmartPtr< ApproximationSpace< TDomain > > approx_space()
returns approx space
Definition: grid_function.h:348
SmartPtr< this_type > clone_without_values() const
clone excluding values
Definition: grid_function.h:219
dim_traits< dim >::grid_base_object element_type
Definition: grid_function.h:159
dim_traits< dim-1 >::grid_base_object side_type
Definition: grid_function.h:163
describes a linear iterator that is based on a matrix operator
Definition: preconditioner.h:103
Definition: matrix_operator.h:49
@ PST_UNIQUE
Definition: parallel_storage_type.h:70
void SetDirichletRow(TSparseMatrix &A, size_t i, size_t alpha)
Definition: sparsematrix_util.h:796
int NumProcs()
returns the number of processes
Definition: pcl_base.cpp:91
void CollectUniqueElements(std::vector< typename TLayout::Element > &elemsOut, const TLayout &layout)
writes all elements in the interfaces into the resulting vector. avoids doubles.
Definition: pcl_layout_util.h:142
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_THROW(msg)
Definition: error.h:57
double number
Definition: types.h:124
IndexLayout::Interface::iterator find(IndexLayout::Interface &interface, size_t i)
Definition: parallel_index_layout.h:77
number & DoFRef(TMatrix &mat, const DoFIndex &iInd, const DoFIndex &jInd)
Definition: multi_index.h:276
bool MakeConsistent(const ParallelMatrix< matrix_type > &_mat, ParallelMatrix< matrix_type > &newMat)
Definition: parallel_matrix_overlap_impl.h:438
bool MatMultAdd(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = alpha1*v1 + beta1 * A1 *w1;
Definition: operations_mat.h:68
void extract_by_grouping(std::vector< std::vector< DoFIndex > > &vvDoFIndex, const GF &c, const std::vector< size_t > &vFullRowCmp, const std::vector< size_t > &vRemainCmp)
Definition: component_gauss_seidel.h:299
@ VOLUME
Definition: grid_base_objects.h:63
@ VERTEX
Definition: grid_base_objects.h:60
@ EDGE
Definition: grid_base_objects.h:61
@ FACE
Definition: grid_base_objects.h:62
bool MatMult(vector_t &dest, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = beta1 * A1;
Definition: operations_mat.h:59
T value_type
Definition: sparsematrix_interface.h:2
Definition: component_gauss_seidel.h:114
std::vector< DenseMatrix< VariableArray2< number > > > vBlockInverse
Definition: component_gauss_seidel.h:116
std::vector< std::vector< DoFIndex > > vvDoFIndex
Definition: component_gauss_seidel.h:115