int ConstructGalerkinMatrix( MT &Mcg, const FAMGGrid &fg ) // this matrix lives on the coarse grid // calculates Mcg := R * Mfg * P and with indices: // Mcg_(i,j) := \sum_{s,t} R_(i,s) * Mfg_(s,t) * P_(t,j) { typedef typename MT::Vector VT; const FAMGTransfer &transfer = *fg.GetTransfer(); const typename MT::GridVector& fg_gridvec = (typename MT::GridVector&)fg.GetGridVector(); const MT& Mfg = (MT&)*fg.GetConsMatrix(); // consistent matrix is essential here! const MT& Dfg = (MT&)*fg.GetDiagMatrix(); const VT &tvA = *fg.GetVector(FAMGTVA); const VT &tvB = *fg.GetVector(FAMGTVB); typename MT::MatrixEntry mij, mis; typename VT::VectorEntry i_fg, i_cg, j_fg, j_cg, s_fg, s_cg, t_cg; FAMGTransferEntry *pjs, *pij, *pst; typename VT::Iterator viter(fg_gridvec); #ifdef ModelP abort();// check the consistent mode of ALL occuring matrices!!! and remove this line then #endif // cast because GetSparseBlockPtr returns a const FAMGSparseBlock * pointer FAMGSparseBlock *cmatsb_d = (FAMGSparseBlock *)Mcg.GetDiagSparseBlockPtr(); FAMGSparseBlock *cmatsb_o = (FAMGSparseBlock *)Mcg.GetSparseBlockPtr(); const FAMGSparseBlock *dmatsb = Dfg.GetDiagSparseBlockPtr(); const FAMGSparseBlock *fmatsb_o = Mfg.GetSparseBlockPtr(); const FAMGSparseBlock *fmatsb_d = Mfg.GetDiagSparseBlockPtr(); const FAMGSparseVector *sp = transfer.Get_sp(); const FAMGSparseVector *sr = transfer.Get_sr(); const FAMGSparseVector *tvAsv = tvA.GetSparseVectorPtr(); const FAMGSparseVector *tvBsv = tvB.GetSparseVectorPtr(); double *tvAptr, *tvBptr; FAMGSparseBlock sb_o_p, sb_r_o, sb_r_o_p, sb_r_d_p, sb_r_dmat_p; // only offdiagonal blocks sb_o_p.Product(fmatsb_o,sp); sb_r_o.Product(sr,fmatsb_o); sb_r_o_p.Product(sr,fmatsb_o,sp); // sb_r_dmat_p.Product(sr,dmatsb,sp); sb_r_dmat_p = (*fmatsb_o); sb_r_d_p.Product(sr,fmatsb_d,sp); // chech sparse block structure if(cmatsb_o->CheckStructureforAdd(fmatsb_o)) return 1; if(cmatsb_o->CheckStructureforAdd(&sb_o_p)) return 1; if(cmatsb_o->CheckStructureforAdd(&sb_r_o)) return 1; if(cmatsb_o->CheckStructureforAdd(&sb_r_o_p)) return 1; if(cmatsb_o->CheckStructureforAdd(&sb_r_dmat_p)) return 1; if(cmatsb_d->CheckStructureforAdd(fmatsb_d)) return 1; if(cmatsb_d->CheckStructureforAdd(&sb_r_d_p)) return 1; if(cmatsb_d->CheckStructureforAdd(&sb_o_p)) return 1; if(cmatsb_d->CheckStructureforAdd(&sb_r_o)) return 1; if(cmatsb_d->CheckStructureforAdd(&sb_r_o_p)) return 1; if(cmatsb_d->CheckStructureforAdd(&sb_r_dmat_p)) return 1; short maxoffset = sb_o_p.Get_maxoffset(); maxoffset = Max(maxoffset,sb_r_o.Get_maxoffset()); maxoffset = Max(maxoffset,sb_r_o_p.Get_maxoffset()); maxoffset = Max(maxoffset,sb_r_dmat_p.Get_maxoffset()); maxoffset = Max(maxoffset,sb_r_d_p.Get_maxoffset()); double *val = new double[maxoffset+1]; double *diaginv = new double[dmatsb->Get_maxoffset()+1]; while (viter(i_fg) ) { #ifdef ModelP if ( IS_FAMG_GHOST(((FAMGugVectorEntryRef*)(i_fg.GetPointer()))->myvector()) ) { // repair coarse grid matrix of border vector, if it has no diagonal matrix entry if (fg_gridvec.IsCG(i_fg) ) { transfer.GetFirstEntry(i_fg)->GetColInVar(i_cg); typename MT::Iterator mijiter(Mcg,i_cg); if( mijiter(mij) ) // test first matrix entry of i_cg { if( mij.dest() != i_cg ) Mcg.AddEntry(0.0, i_cg, i_cg); // has no diag entry yet } else // i_cg has no matrix entry { Mcg.AddEntry(0.0, i_cg, i_cg); } } continue; } #endif // i is now in core partition if (fg_gridvec.IsCG(i_fg) ) { // i is coarse transfer.GetFirstEntry(i_fg)->GetColInVar(i_cg); typename MT::Iterator mijiter(Mfg,i_fg); while( mijiter(mij) ) { j_fg = mij.dest(); if( fg_gridvec.IsCG(j_fg) ) { transfer.GetFirstEntry(j_fg)->GetColInVar(j_cg); // Mcg.AddEntry(Mfg[mij], i_cg, j_cg); // Mcc if(i_cg == j_cg) Mcg.AddEntry(fmatsb_d,Mfg.GetValuePtr(mij), i_cg, j_cg); else Mcg.AddEntry(fmatsb_o,Mfg.GetValuePtr(mij), i_cg, j_cg); // Mcc } else { for( pjs=transfer.GetFirstEntry(j_fg); pjs != NULL; pjs = pjs->GetNext()) { pjs->GetColInVar(s_cg); SparseBlockMMProduct(&sb_o_p,fmatsb_o,sp,val,Mfg.GetValuePtr(mij),pjs->GetProlongationPtr()); Mcg.AddEntry(&sb_o_p,val,i_cg, s_cg); // Mcg.AddEntry(Mfg[mij]*pjs->GetProlongation(), i_cg, s_cg); // Mcf*P } } } } else { // i is fine typename MT::Iterator misiter(Mfg,i_fg); while( misiter(mis) ) { s_fg = mis.dest(); for( pij=transfer.GetFirstEntry(i_fg); pij != NULL; pij = pij->GetNext()) { pij->GetColInVar(j_cg); if( fg_gridvec.IsCG(s_fg) ) { transfer.GetFirstEntry(s_fg)->GetColInVar(s_cg); // pij is equivalent to rji // Mcg.AddEntry(pij->GetRestriction()*Mfg[mis], j_cg, s_cg); // R*Mfc SparseBlockMMProduct(&sb_r_o,sr,fmatsb_o,val,pij->GetRestrictionPtr(),Mfg.GetValuePtr(mis)); Mcg.AddEntry(&sb_r_o,val,j_cg, s_cg); } else { // s is fine if(s_fg == i_fg) { // special treatment for the A_{i,i} to keep block sparsity pattern for( pst=transfer.GetFirstEntry(s_fg); pst != NULL; pst = pst->GetNext()) { pst->GetColInVar(t_cg); // pij is equivalent to rji // Mcg.AddEntry(pij->GetRestriction()*Mfg[mis]*pst->GetProlongation(), j_cg, t_cg);// R*Mff*P SparseBlockMMProduct(&sb_r_d_p,sr,fmatsb_d,sp,val,pij->GetRestrictionPtr(),Mfg.GetValuePtr(mis),pst->GetProlongationPtr()); //Mcg.AddEntry(&sb_r_d_p,val,j_cg, j_cg); // lump to diagonal Mcg.AddEntry(&sb_r_d_p,val,t_cg, t_cg); // lump to diagonal // todo: make sure lumping preserves filter condition if(j_cg != t_cg) { // SparseBlockMInvertDiag(dmatsb, diaginv, Dfg.GetValuePtr(mis)); // SparseBlockMMProduct(&sb_r_dmat_p,sr,dmatsb,sp,val,pij->GetRestrictionPtr(),diaginv,pst->GetProlongationPtr()); tvAptr = tvA.GetValuePtr(t_cg); tvBptr = tvB.GetValuePtr(t_cg); SparseBlockGalDiagApprox(&sb_r_dmat_p,sr,fmatsb_d,sp,tvAsv,val,pij->GetRestrictionPtr(),Mfg.GetValuePtr(mis),pst->GetProlongationPtr(),tvAptr); // SparseBlockGalDiagApproxT(&sb_r_dmat_p,sr,fmatsb_d,sp,tvBsv,val,pij->GetRestrictionPtr(),Mfg.GetValuePtr(mis),pst->GetProlongationPtr(),tvBptr); Mcg.AddEntry(&sb_r_dmat_p,val,j_cg, t_cg); // Mcg.AddEntry(&sb_r_dmat_p,val,j_cg, j_cg,-1.0); Mcg.AddEntry(&sb_r_dmat_p,val,t_cg, t_cg,-1.0); } } } else { for( pst=transfer.GetFirstEntry(s_fg); pst != NULL; pst = pst->GetNext()) { pst->GetColInVar(t_cg); // pij is equivalent to rji // Mcg.AddEntry(pij->GetRestriction()*Mfg[mis]*pst->GetProlongation(), j_cg, t_cg);// R*Mff*P SparseBlockMMProduct(&sb_r_o_p,sr,fmatsb_o,sp,val,pij->GetRestrictionPtr(),Mfg.GetValuePtr(mis),pst->GetProlongationPtr()); Mcg.AddEntry(&sb_r_o_p,val,j_cg, t_cg); } } } } } } } delete val; delete diaginv; return 0; }
int ConstructGalerkinMatrix( MT &Mcg, const FAMGGrid &fg ) // this matrix lives on the coarse grid // calculates Mcg := R * Mfg * P and with indices: // Mcg_(i,j) := \sum_{s,t} R_(i,s) * Mfg_(s,t) * P_(t,j) { typedef typename MT::Vector VT; const FAMGTransfer &transfer = *fg.GetTransfer(); const typename MT::GridVector& fg_gridvec = (typename MT::GridVector&)fg.GetGridVector(); const MT& Mfg = (MT&)*fg.GetConsMatrix(); // consistent matrix is essential here! typename MT::MatrixEntry mij, mis; typename VT::VectorEntry i_fg, i_cg, j_fg, j_cg, s_fg, s_cg, t_cg; FAMGTransferEntry *pjs, *pij, *pst; typename VT::Iterator viter(fg_gridvec); // the next lines are for debugging only: //MATDATA_DESC *tmpA = ((FAMGugMatrix*)fg.GetConsMatrix())->GetMatDesc(); //GRID *tmpgrid = fg.GetugGrid(); //int tmpflevel = GLEVEL(tmpgrid); //printf("%d: GalerkinAss finelevel = %d\n",me,tmpflevel); prvGeom(tmpflevel,0); primGeom(tmpflevel); prmGeom(tmpflevel,MD_SCALCMP(tmpA)); prvGeom(tmpflevel-1,0); while (viter(i_fg) ) { #ifdef ModelP if ( IS_FAMG_GHOST(((FAMGugVectorEntryRef*)(i_fg.GetPointer()))->myvector()) ) { // repair coarse grid matrix of border vector, if it has no diagonal matrix entry if (fg_gridvec.IsCG(i_fg) ) { transfer.GetFirstEntry(i_fg)->GetColInVar(i_cg); typename MT::Iterator mijiter(Mcg,i_cg); if( mijiter(mij) ) // test first matrix entry of i_cg { if( mij.dest() != i_cg ) Mcg.AddEntry(0.0, i_cg, i_cg); // has no diag entry yet } else // i_cg has no matrix entry { Mcg.AddEntry(0.0, i_cg, i_cg); } } continue; } #endif // i is now in core partition if (fg_gridvec.IsCG(i_fg) ) { // i is coarse transfer.GetFirstEntry(i_fg)->GetColInVar(i_cg); typename MT::Iterator mijiter(Mfg,i_fg); while( mijiter(mij) ) { j_fg = mij.dest(); if( fg_gridvec.IsCG(j_fg) ) { transfer.GetFirstEntry(j_fg)->GetColInVar(j_cg); Mcg.AddEntry(Mfg[mij], i_cg, j_cg); // Mcc //printf("%d: G%d[%d] Mcc i f%d[%d] c%d[%d] j f%d[%d] c%d[%d] Mfg[mij]=%g\n",me, prvec(i_cg), // prvec(i_fg),prvec(i_cg),prvec(j_fg),prvec(j_cg),Mfg[mij]); } else { for( pjs=transfer.GetFirstEntry(j_fg); pjs != NULL; pjs = pjs->GetNext()) { pjs->GetColInVar(s_cg); Mcg.AddEntry(Mfg[mij]*pjs->GetProlongation(), i_cg, s_cg); // Mcf*P //printf("%d: G%d[%d] Mcf*P i f%d[%d] c%d[%d] j f%d[%d] s c%d[%d] Mfg[mij]=%g pjs=%g Mfg[mij]*pjs=%g\n",me, prvec(i_cg), // prvec(i_fg),prvec(i_cg),prvec(j_fg),prvec(s_cg),Mfg[mij],pjs->GetProlongation(),Mfg[mij]*pjs->GetProlongation()); } } } } else { // i is fine typename MT::Iterator misiter(Mfg,i_fg); while( misiter(mis) ) { s_fg = mis.dest(); for( pij=transfer.GetFirstEntry(i_fg); pij != NULL; pij = pij->GetNext()) { pij->GetColInVar(j_cg); if( fg_gridvec.IsCG(s_fg) ) { transfer.GetFirstEntry(s_fg)->GetColInVar(s_cg); // pij is equivalent to rji Mcg.AddEntry(pij->GetRestriction()*Mfg[mis], j_cg, s_cg); // R*Mfc //printf("%d: G%d[%d] R*Mfc j c%d[%d] i f%d[%d] s f%d[%d] c%d[%d] rji=%g Mfg[mis]=%g rji*Mfg[mis]=%g\n",me, prvec(j_cg), // prvec(j_cg),prvec(i_fg),prvec(s_fg),prvec(s_cg),pij->GetRestriction(), Mfg[mis], pij->GetRestriction()*Mfg[mis] ); } else { // s is fine for( pst=transfer.GetFirstEntry(s_fg); pst != NULL; pst = pst->GetNext()) { pst->GetColInVar(t_cg); // pij is equivalent to rji Mcg.AddEntry(pij->GetRestriction()*Mfg[mis]*pst->GetProlongation(), j_cg, t_cg);// R*Mff*P //printf("%d: G%d[%d] R*Mff*P j c%d[%d] i f%d[%d] s f%d[%d] t c%d[%d] rji=%g Mfg[mis]=%g pst=%g rji*Mfg[mis]*pst=%g\n",me, prvec(j_cg), // prvec(j_cg),prvec(i_fg),prvec(s_fg),prvec(t_cg),pij->GetRestriction(),Mfg[mis],pst->GetProlongation(),pij->GetRestriction()*Mfg[mis]*pst->GetProlongation() ); } } } } } } return 0; }