void SGSSmoother( VT &sol, const MT &M, VT &def ) // backward Gauss-Seidel // result in sol_vec; def_vec: correct defect before call, after call destroyed { typename VT::Iterator viter(def); typename VT::RevIterator vReviter(def); typename VT::VectorEntry row, col; typename MT::MatrixEntry me; register double sum, diag; int row_index; /* symmetric Gauss-Seidel */ while(viter(row)) { typename MT::Iterator miter(M,row); row_index = row.GetIndex(); sum = def[row]; miter(me); diag = M[me]; while(miter(me)) { col = me.dest(); if( col.GetIndex() < row_index ) sum -= M[me] * def[col]; } def[row] = sum / diag; } viter.reset(); while(viter(row)) def[row] *= M.DiagValue(row); while(vReviter(row)) { typename MT::Iterator miter(M,row); row_index = row.GetIndex(); sum = def[row]; miter(me); diag = M[me]; while(miter(me)) { col = me.dest(); if( col.GetIndex() > row_index ) sum -= M[me] * def[col]; } def[row] = sum / diag; } sol += def; // update solution return; }
void MarkStrongLinks(const MT &A, const FAMGGrid &grid) { typedef typename MT::Vector VT; const typename MT::GridVector& gridvec = (typename MT::GridVector&)grid.GetGridVector(); typename MT::MatrixEntry matij; typename VT::VectorEntry vi; typename VT::Iterator viter(gridvec); while (viter(vi)) { typename MT::Iterator mij_iter(A,vi); mij_iter(matij); matij.set_strong(1); while( mij_iter(matij) ) { // if(VSKIPME(matij.dest().myvector(),0)) matij.set_strong(0); // else matij.set_strong(1); matij.set_strong(1); } } return; }
void Scale( VT& v, double scale ) { // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; while(viter(ve)) v[ve] *= scale; }
void SubtractValue( VT &dest, const VT &source ) { // typename is a new C++ keyword! typename VT::Iterator viter(dest); typename VT::VectorEntry ve; while(viter(ve)) dest[ve] -= source[ve]; }
void SetValue( VT &v, double val ) { // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; while(viter(ve)) v[ve] = val; }
void AddScaledValue( VT &dest, double scale, const VT &source ) { // typename is a new C++ keyword! typename VT::Iterator viter(dest); typename VT::VectorEntry ve; while(viter(ve)) dest[ve] += scale * source[ve]; }
void JacobiSmoother( VT &sol, const MT &M, const VT &def ) // result in sol_vec; def_vec: correct defect before call, after call destroyed { typename VT::Iterator viter(sol); typename VT::VectorEntry ve; while(viter(ve)) sol[ve] += def[ve] / M.DiagValue(ve); return; }
void JacobiSmoothFG( VT &sol, const MT &A, const VT &def ) // changes only the fine unknowns // result in sol_vec; def_vec: correct defect before call, after call destroyed { typename VT::Iterator viter(sol); typename VT::VectorEntry ve; const FAMGSparseVector *svsol = sol.GetSparseVectorPtr(); const FAMGSparseVector *svdef = def.GetSparseVectorPtr(); const FAMGSparseBlock *sb = A.GetDiagSparseBlockPtr(); double *solptr, *defptr, *matptr; short nr = sb->Get_nr(); if(nr != sb->Get_nc()) assert(0); if(nr != svsol->Get_n()) assert(0); if(nr != svdef->Get_n()) assert(0); // todo: implement for more general vectors for(short i = 1; i < nr; i++) { if(svsol->Get_comp(i) - svsol->Get_comp(i-1) != 1) assert(0); if(svdef->Get_comp(i) - svdef->Get_comp(i-1) != 1) assert(0); } short sol_off = svsol->Get_comp(0); short def_off = svdef->Get_comp(0); double *decomp = new double[nr*nr]; short *pivotmap = new short[nr]; while(viter(ve)) { if( sol.IsFG(ve) ) { solptr = sol.GetValuePtr(ve)+sol_off; defptr = def.GetValuePtr(ve)+def_off; matptr = A.GetDiagValuePtr(ve); SparseBlockMCopyDense(decomp,sb,matptr); if(LR_Decomp(nr,decomp,pivotmap)) assert(0); if(LR_Solve(nr,decomp,pivotmap,solptr,defptr)) assert(0); } #ifdef USE_UG_DS else { // set coarse components to 0 SparseBlockVSet(svsol,sol.GetValuePtr(ve),0.0); } #endif } delete decomp; delete pivotmap; return; }
void CopyValue( VT &dest, const VT &source ) { // typename is a new C++ keyword! typename VT::Iterator viter(dest); typename VT::VectorEntry ve; if( &dest == &source ) return; // nothing to do while(viter(ve)) dest[ve] = source[ve]; }
void JacobiSmoothFG( VT &sol, const MT &M, const VT &def ) // changes only the fine unknowns // result in sol_vec; def_vec: correct defect before call, after call destroyed { typename VT::Iterator viter(sol); typename VT::VectorEntry ve; #ifdef USE_UG_DS while(viter(ve)) if( sol.IsFG(ve) ) sol[ve] = def[ve] / M.DiagValue(ve); else sol[ve] = 0; // init other components #else while(viter(ve)) if( sol.IsFG(ve) ) sol[ve] += def[ve] / M.DiagValue(ve); #endif return; }
double sum( const VT& v ) { // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; register double res=0.0; while(viter(ve)) res += v[ve]; return res; }
void dampedJacobiSmoother( VT &sol, const MT &M, const VT &def ) // result in sol_vec; def_vec: correct defect before call, after call destroyed { static const double omega = 2.0/3.0; typename VT::Iterator viter(sol); typename VT::VectorEntry ve; while(viter(ve)) sol[ve] += omega * def[ve] / M.DiagValue(ve); return; }
double sum( const VT& v ) { assert(0);// todo: adapt to sparse matrix // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; register double res=0.0; while(viter(ve)) res += v[ve]; return res; }
void Scale( VT &v, double scale ) { // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; short ncmp = v.GetSparseVectorPtr()->Get_n(); short *comp = v.GetSparseVectorPtr()->Get_comp(); double *vptr; while(viter(ve)) { vptr = v.GetValuePtr(ve); for(short i = 0; i < ncmp; i++) vptr[comp[i]] *= scale; } }
double ScalProd( const VT& v, const VT& w ) { // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; register double res=0.0; while(viter(ve)) res += v[ve]*w[ve]; #ifdef ModelP res = UG_GlobalSumDOUBLE( res ); #endif return res; }
void MatVec( VT &dest, const MT &M, const VT &source ) { typename VT::Iterator viter(dest); typename VT::VectorEntry row; typename MT::MatrixEntry col; register double sum; while(viter(row)) { typename MT::Iterator miter(M,row); sum = 0.0; while(miter(col)) sum += M[col] * source[col.dest()]; dest[row] = sum; } }
void VecMinusMatVec( VT &d, const VT &f, const MT &M, const VT &u ) { typename VT::Iterator viter(d); typename VT::VectorEntry row; typename MT::MatrixEntry col; register double sum; while(viter(row)) { typename MT::Iterator miter(M,row); sum = 0.0; while(miter(col)) sum += M[col] * u[col.dest()]; d[row] = f[row] - sum; } }
void VecMinusMatVec( VT &d, const VT &f, const MT &M, const VT &u ) { typename VT::Iterator viter(d); typename VT::VectorEntry row; typename MT::MatrixEntry col; double *dptr, *fptr, *uptr, *mptr; const FAMGSparseVector *svu = u.GetSparseVectorPtr(); const FAMGSparseVector *svf = f.GetSparseVectorPtr(); const FAMGSparseVector *svd = d.GetSparseVectorPtr(); const FAMGSparseBlock *sb = M.GetSparseBlockPtr(); const FAMGSparseBlock *sbd = M.GetDiagSparseBlockPtr(); FAMGSparseVector svsum_d, svsum_o; svsum_d.Product(sbd,svu); svsum_o.Product(sb,svu); double *sum_d = new double[svsum_d.Get_maxcomp()+1]; double *sum_o = new double[svsum_o.Get_maxcomp()+1]; while(viter(row)) { typename MT::Iterator miter(M,row); dptr = d.GetValuePtr(row); fptr = f.GetValuePtr(row); // diagonal miter(col); uptr = u.GetValuePtr(col.dest()); mptr = M.GetValuePtr(col); SparseBlockVSet(&svsum_d,sum_d,0.0); SparseBlockVSet(&svsum_o,sum_o,0.0); SparseBlockMVAddProduct(&svsum_d,sbd,svu,sum_d,mptr,uptr,1.0); while(miter(col)) { uptr = u.GetValuePtr(col.dest()); mptr = M.GetValuePtr(col); SparseBlockMVAddProduct(&svsum_o,sb,svu,sum_o,mptr,uptr,1.0); } SparseBlockVSub(svd,svf,&svsum_o,dptr,fptr,sum_o); SparseBlockVSub(svd,svd,&svsum_d,dptr,dptr,sum_d); } delete sum_d; delete sum_o; }
double norm( const VT& v ) { // typename is a new C++ keyword! typename VT::Iterator viter(v); typename VT::VectorEntry ve; register double val, res=0.0; while(viter(ve)) { val = v[ve]; res += val*val; } #ifdef ModelP res = UG_GlobalSumDOUBLE( res ); #endif return sqrt(res); }
void PolygonView::slotApplyColor() { EM_CERR("Polygonview::slotApplyColor"); bool bVertex = false; // change selected vertex Q3ListViewItemIterator viter(p_VertexListView); for (; viter.current(); ++viter) { if (viter.current()->isSelected()) { bVertex = true; if (((ListItem*)viter.current())->getObjectType() == LISTITEM_VERTEX) { // Color * color = poly->getColor((*coloritem).second); // assert(color != NULL); // color->r = p_EditR->text().toFloat(); // color->g = p_EditG->text().toFloat(); // color->b = p_EditB->text().toFloat(); // color->a = p_EditA->text().toFloat(); EM_CERR("PolygonView::slotApplyColor vertex"); } } } // change selected polygon only if no vertex was selected Q3ListViewItemIterator piter(p_PolygonListView); for (; piter.current() && !bVertex; ++piter) { if (piter.current()->isSelected()) { if (((ListItem*)piter.current())->getObjectType() == LISTITEM_POLYGON) { Polygon3D * poly = (Polygon3D*)((ListItem*)piter.current())->getObject(); assert(poly != NULL); poly->setColor(p_EditR->text().toFloat(), p_EditG->text().toFloat(), p_EditB->text().toFloat(), p_EditA->text().toFloat()); EM_CERR("PolygonView::slotApplyColor polygon"); } } } p_Doc->updateAll("polygon"); }
void AddScaledValue( VT &dest, double scale, const VT &source ) { // not really tesyed // typename is a new C++ keyword! typename VT::Iterator viter(dest); typename VT::VectorEntry ve; short ncmp_d = dest.GetSparseVectorPtr()->Get_n(); short ncmp_s = source.GetSparseVectorPtr()->Get_n(); short *comp_d = dest.GetSparseVectorPtr()->Get_comp(); short *comp_s = source.GetSparseVectorPtr()->Get_comp(); short ncmp; double *vptr_d, *vptr_s; ncmp = Min(ncmp_d,ncmp_s); while(viter(ve)) { vptr_d = dest.GetValuePtr(ve); vptr_s = source.GetValuePtr(ve); for(short i = 0; i < ncmp; i++) vptr_d[comp_d[i]] += scale*vptr_s[comp_s[i]]; } }
void JacobiSmoothFGSimple( VT &sol, const MT &D, const VT &def ) // changes only the fine unknowns // result in sol_vec; def_vec: correct defect before call, after call destroyed { typename VT::Iterator viter(sol); typename VT::VectorEntry ve; const FAMGSparseVector *svsol = sol.GetSparseVectorPtr(); const FAMGSparseVector *svdef = def.GetSparseVectorPtr(); const FAMGSparseBlock *sb = D.GetDiagSparseBlockPtr(); double *solptr, *defptr, *matptr; while(viter(ve)) { if( sol.IsFG(ve) ) { solptr = sol.GetValuePtr(ve); defptr = def.GetValuePtr(ve); matptr = D.GetDiagValuePtr(ve); SparseBlockMVAddProduct(svsol,sb,svdef,solptr,matptr,defptr,1.0); } } return; }
VRange<Unit *> UncheckedModelImpl::units() const { VIter<Unit *> begin = viter(castingIter<Unit *>(mUnits.begin())); VIter<Unit *> end = viter(castingIter<Unit *>(mUnits.end())); return vrange(begin, end); }
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; }
void MarkStrongLinks(const MT &A, const FAMGGrid &grid) { typedef typename MT::Vector VT; const typename MT::GridVector& gridvec = (typename MT::GridVector&)grid.GetGridVector(); typename MT::MatrixEntry matij; typename VT::VectorEntry vi; typename VT::Iterator viter(gridvec); double rlist[20], llist[20], mij, mji, rmax, lmax; int z, y; const double sigma = FAMGGetParameter()->Getsigma(); const int minsl = 2 - 1; while (viter(vi)) { for(z = 0; z <= minsl; z++) { rlist[z] = llist[z] = 0.0; } typename MT::Iterator mij_iter(A,vi); mij_iter(matij); // skip diagonal while( mij_iter(matij) ) { mij = Abs(A[matij]); mji = Abs(A.GetAdjData(matij)); for(z = minsl; z >= 0; z--) { if (mij < rlist[z]) break; } for(y = minsl; y > z+1; y--) { rlist[y] = rlist[y-1]; } if(z+1 <= minsl) rlist[z+1] = mij; for(z = minsl; z >= 0; z--) { if (mji < llist[z]) break; } for(y = minsl; y > z+1; y--) { llist[y] = llist[y-1]; } if(z+1 <= minsl) llist[z+1] = mji; } rmax = rlist[minsl]*sigma; lmax = llist[minsl]*sigma; mij_iter.reset(); mij_iter(matij); matij.set_strong(1); while( mij_iter(matij) ) { mij = Abs(A[matij]); mji = Abs(A.GetAdjData(matij)); if((mij > rmax) || (mji > lmax)) { matij.set_strong(1); } else matij.set_strong(0); } } return; }
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; }
bool CXRayObjectExport::initializeSetsAndLookupTables( bool exportAll ) // // Description : // Creates a list of all sets in Maya, a list of mesh objects, // and polygon/vertex lookup tables that will be used to // determine which sets are referenced by the poly components. // { int i=0,j=0, length; MStatus stat; // Initialize class data. // Note: we cannot do this in the constructor as it // only gets called upon registry of the plug-in. // numSets = 0; sets = NULL; lastSets = NULL; lastMaterials = NULL; objectId = 0; objectCount = 0; polygonTable = NULL; vertexTable = NULL; polygonTablePtr = NULL; vertexTablePtr = NULL; objectGroupsTablePtr = NULL; objectNodeNamesArray.clear(); transformNodeNameArray.clear(); ////////////////////////////////////////////////////////////////// // // Find all sets in Maya and store the ones we care about in // the 'sets' array. Also make note of the number of sets. // ////////////////////////////////////////////////////////////////// // Get all of the sets in maya and put them into // a selection list // MStringArray result; MGlobal::executeCommand( "ls -sets", result ); MSelectionList * setList = new MSelectionList(); length = result.length(); for ( i=0; i<length; i++ ) { setList->add( result[i] ); } // Extract each set as an MObject and add them to the // sets array. // We may be excluding groups, matierials, or ptGroups // in which case we can ignore those sets. // MObject mset; sets = new MObjectArray(); length = setList->length(); for ( i=0; i<length; i++ ) { setList->getDependNode( i, mset ); MFnSet fnSet( mset, &stat ); if ( stat ) { if ( MFnSet::kRenderableOnly == fnSet.restriction(&stat) ) { sets->append( mset ); } } } xr_delete(setList); numSets = sets->length(); ////////////////////////////////////////////////////////////////// // // Do a dag-iteration and for every mesh found, create facet and // vertex look-up tables. These tables will keep track of which // sets each component belongs to. // // If exportAll is false then iterate over the activeSelection // list instead of the entire DAG. // // These arrays have a corrisponding entry in the name // stringArray. // ////////////////////////////////////////////////////////////////// MIntArray vertexCounts; MIntArray polygonCounts; if ( exportAll ) { MItDag dagIterator( MItDag::kBreadthFirst, MFn::kInvalid, &stat); if ( MS::kSuccess != stat) { fprintf(stderr,"Failure in DAG iterator setup.\n"); return false; } objectNames = new MStringArray(); for ( ; !dagIterator.isDone(); dagIterator.next() ) { MDagPath dagPath; stat = dagIterator.getPath( dagPath ); if ( stat ) { // skip over intermediate objects // MFnDagNode dagNode( dagPath, &stat ); if (dagNode.isIntermediateObject()) { continue; } if (( dagPath.hasFn(MFn::kMesh)) && ( dagPath.hasFn(MFn::kTransform))) { // We want only the shape, // not the transform-extended-to-shape. continue; } else if ( dagPath.hasFn(MFn::kMesh)) { // We have a mesh so create a vertex and polygon table // for this object. // MFnMesh fnMesh( dagPath ); int vtxCount = fnMesh.numVertices(); int polygonCount = fnMesh.numPolygons(); // we do not need this call anymore, we have the shape. // dagPath.extendToShape(); MString name = dagPath.fullPathName(); objectNames->append( name ); objectNodeNamesArray.append( fnMesh.name() ); vertexCounts.append( vtxCount ); polygonCounts.append( polygonCount ); objectCount++; } } } }else{ MSelectionList slist; MGlobal::getActiveSelectionList( slist ); MItSelectionList iter( slist ); MStatus status; objectNames = new MStringArray(); // We will need to interate over a selected node's heirarchy // in the case where shapes are grouped, and the group is selected. MItDag dagIterator( MItDag::kDepthFirst, MFn::kInvalid, &status); for ( ; !iter.isDone(); iter.next() ){ MDagPath objectPath; stat = iter.getDagPath( objectPath ); // reset iterator's root node to be the selected node. status = dagIterator.reset (objectPath.node(), MItDag::kDepthFirst, MFn::kInvalid ); // DAG iteration beginning at at selected node for ( ; !dagIterator.isDone(); dagIterator.next() ){ MDagPath dagPath; MObject component = MObject::kNullObj; status = dagIterator.getPath(dagPath); if (!status){ fprintf(stderr,"Failure getting DAG path.\n"); freeLookupTables(); return false; } // skip over intermediate objects // MFnDagNode dagNode( dagPath, &stat ); if (dagNode.isIntermediateObject()) continue; if (( dagPath.hasFn(MFn::kMesh)) && ( dagPath.hasFn(MFn::kTransform))){ // We want only the shape, // not the transform-extended-to-shape. continue; }else if ( dagPath.hasFn(MFn::kMesh)){ // We have a mesh so create a vertex and polygon table // for this object. // MFnMesh fnMesh( dagPath ); int vtxCount = fnMesh.numVertices(); int polygonCount = fnMesh.numPolygons(); // we do not need this call anymore, we have the shape. // dagPath.extendToShape(); MString name = dagPath.fullPathName(); objectNames->append( name ); objectNodeNamesArray.append( fnMesh.name() ); vertexCounts.append( vtxCount ); polygonCounts.append( polygonCount ); objectCount++; } } } } // Now we know how many objects we are dealing with // and we have counts of the vertices/polygons for each // object so create the maya group look-up table. // if( objectCount > 0 ) { // To export Maya groups we traverse the hierarchy starting at // each objectNodeNamesArray[i] going towards the root collecting transform // nodes as we go. length = objectNodeNamesArray.length(); for( i=0; i<length; i++ ) { MIntArray transformNodeNameIndicesArray; recFindTransformDAGNodes( objectNodeNamesArray[i], transformNodeNameIndicesArray ); } if( transformNodeNameArray.length() > 0 ) { objectGroupsTablePtr = xr_alloc<bool*>(objectCount);// (bool**) malloc( sizeof(bool*)*objectCount ); length = transformNodeNameArray.length(); for ( i=0; i<objectCount; i++ ) { // objectGroupsTablePtr[i] = (bool*)calloc( length, sizeof(bool) ); objectGroupsTablePtr[i] = xr_alloc<bool>(length); ZeroMemory(objectGroupsTablePtr[i],length*sizeof(bool)); // XXX nitrocaster: remove this 'cause malloc failure shouldn't be handled there if ( objectGroupsTablePtr[i] == NULL ) { Log("! calloc returned NULL (objectGroupsTablePtr)"); return false; } } } // else{ // Log("! Can't find transform for node."); // return false; // } } // Create the vertex/polygon look-up tables. // if ( objectCount > 0 ) { vertexTablePtr = xr_alloc<bool*>(objectCount); //(bool**) malloc( sizeof(bool*)*objectCount ); polygonTablePtr = xr_alloc<bool*>(objectCount); //(bool**) malloc( sizeof(bool*)*objectCount ); for ( i=0; i<objectCount; i++ ) { // vertexTablePtr[i] = (bool*)calloc( vertexCounts[i]*numSets, sizeof(bool) ); vertexTablePtr[i] = xr_alloc<bool>(vertexCounts[i]*numSets); ZeroMemory(vertexTablePtr[i],vertexCounts[i]*numSets*sizeof(bool)); // XXX nitrocaster: remove this 'cause malloc failure shouldn't be handled there if ( vertexTablePtr[i] == NULL ) { Log("! calloc returned NULL (vertexTable)"); return false; } // polygonTablePtr[i] = (bool*)calloc( polygonCounts[i]*numSets, sizeof(bool) ); polygonTablePtr[i] = xr_alloc<bool>(polygonCounts[i]*numSets); ZeroMemory(polygonTablePtr[i],polygonCounts[i]*numSets*sizeof(bool)); // XXX nitrocaster: remove this 'cause malloc failure shouldn't be handled there if ( polygonTablePtr[i] == NULL ) { Log("! calloc returned NULL (polygonTable)"); return false; } } } // If we found no meshes then return // if ( objectCount == 0 ) { return false; } ////////////////////////////////////////////////////////////////// // // Go through all of the set members (flattened lists) and mark // in the lookup-tables, the sets that each mesh component belongs // to. // // ////////////////////////////////////////////////////////////////// bool flattenedList = true; MDagPath object; MObject component; MSelectionList memberList; for ( i=0; i<numSets; i++ ) { MFnSet fnSet( (*sets)[i] ); memberList.clear(); stat = fnSet.getMembers( memberList, flattenedList ); if (MS::kSuccess != stat) { fprintf(stderr,"Error in fnSet.getMembers()!\n"); } int m, numMembers; numMembers = memberList.length(); for ( m=0; m<numMembers; m++ ) { if ( memberList.getDagPath(m,object,component) ) { if ( (!component.isNull()) && (object.apiType() == MFn::kMesh) ) { if (component.apiType() == MFn::kMeshVertComponent) { MItMeshVertex viter( object, component ); for ( ; !viter.isDone(); viter.next() ) { int compIdx = viter.index(); MString name = object.fullPathName(); // Figure out which object vertexTable // to get. // int o, numObjectNames; numObjectNames = objectNames->length(); for ( o=0; o<numObjectNames; o++ ) { if ( (*objectNames)[o] == name ) { // Mark set i as true in the table // vertexTable = vertexTablePtr[o]; *(vertexTable + numSets*compIdx + i) = true; break; } } } } else if (component.apiType() == MFn::kMeshPolygonComponent) { MItMeshPolygon piter( object, component ); for ( ; !piter.isDone(); piter.next() ) { int compIdx = piter.index(); MString name = object.fullPathName(); // Figure out which object polygonTable // to get. // int o, numObjectNames; numObjectNames = objectNames->length(); for ( o=0; o<numObjectNames; o++ ) { if ( (*objectNames)[o] == name ) { // Mark set i as true in the table // // Check for bad components in the set // if ( compIdx >= polygonCounts[o] ) { Msg("! Bad polygon index '%d' found. Polygon skipped",compIdx); break; } polygonTable = polygonTablePtr[o]; *(polygonTable + numSets*compIdx + i) = true; break; } } } } } else { // There are no components, therefore we can mark // all polygons as members of the given set. // if (object.hasFn(MFn::kMesh)) { MFnMesh fnMesh( object, &stat ); if ( MS::kSuccess != stat) { fprintf(stderr,"Failure in MFnMesh initialization.\n"); return false; } // We are going to iterate over all the polygons. // MItMeshPolygon piter( object, MObject::kNullObj, &stat ); if ( MS::kSuccess != stat) { fprintf(stderr, "Failure in MItMeshPolygon initialization.\n"); return false; } for ( ; !piter.isDone(); piter.next() ) { int compIdx = piter.index(); MString name = object.fullPathName(); // Figure out which object polygonTable to get. // int o, numObjectNames; numObjectNames = objectNames->length(); for ( o=0; o<numObjectNames; o++ ) { if ( (*objectNames)[o] == name ) { // Check for bad components in the set // if ( compIdx >= polygonCounts[o] ) { Msg("! Bad polygon index '%d' found. Polygon skipped",compIdx); break; } // Mark set i as true in the table // polygonTable = polygonTablePtr[o]; *(polygonTable + numSets*compIdx + i) = true; break; } } } // end of piter.next() loop } // end of condition if (object.hasFn(MFn::kMesh)) } // end of else condifion if (!component.isNull()) } // end of memberList.getDagPath(m,object,component) } // end of memberList loop } // end of for-loop for sets // Go through all of the group members and mark in the // lookup-table, the group that each shape belongs to. length = objectNodeNamesArray.length(); if (objectGroupsTablePtr){ for( i=0; i<length; i++ ) { MIntArray groupTableIndicesArray; bool *objectGroupTable = objectGroupsTablePtr[i]; int length2; recFindTransformDAGNodes( objectNodeNamesArray[i], groupTableIndicesArray ); length2 = groupTableIndicesArray.length(); for( j=0; j<length2; j++ ) { int groupIdx = groupTableIndicesArray[j]; objectGroupTable[groupIdx] = true; } } } return true; }