42 using namespace Eigen;
    94   void CSparse::setupBlockStructure(
int n)
   108     for (
int i=0; i<(int)diag.size(); i++)
   110     for (
int i=0; i<(int)cols.size(); i++)
   112         map<int,Matrix<double,6,6>, less<int>, 
   113           aligned_allocator<Matrix<double,6,6> > > &col = cols[i];
   116             map<int,Matrix<double,6,6>, less<int>, 
   117               aligned_allocator<Matrix<double,6,6> > >::iterator it;
   118             for (it = col.begin(); it != col.end(); it++)
   119               it->second.setZero();
   124   void CSparse::incDiagBlocks(
double lam)
   126     for (
int i=0; i<(int)diag.size(); i++)
   127       diag[i].diagonal() *= lam;
   131   void CSparse::addOffdiagBlock(Matrix<double,6,6> &m, 
int ii, 
int jj)
   134     map<int,Matrix<double,6,6>, less<int>, 
   135       aligned_allocator<Matrix<double,6,6> > > &col = cols[jj];
   137     map<int,Matrix<double,6,6>, less<int>, 
   138       aligned_allocator<Matrix<double,6,6> > >::iterator it;
   141       col.insert(pair<
int,Matrix<double,6,6> >(ii,m));
   151   void CSparse::setupCSstructure(
double diaginc, 
bool init)
   155       cholmod_start(&Common); 
   161     if (init || useCholmod)
   165         for (
int i=0; i<(int)cols.size(); i++)
   167             map<int,Matrix<double,6,6>, less<int>, 
   168               aligned_allocator<Matrix<double,6,6> > > &col = cols[i];
   169             nnz += 36 * col.size(); 
   178             chA = cholmod_allocate_sparse(csize,csize,nnz,
true,
true,1,CHOLMOD_REAL,&Common);
   184             A = cs_spalloc(csize,csize,nnz,1,0); 
   202         for (
int i=0; i<(int)cols.size(); i++)
   205             map<int,Matrix<double,6,6>, less<int>, 
   206               aligned_allocator<Matrix<double,6,6> > > &col = cols[i];
   209             for (
int k=0; k<6; k++)
   218                     map<int,Matrix<double,6,6>, less<int>, 
   219                       aligned_allocator<Matrix<double,6,6> > >::iterator it;
   222                     for (it = col.begin(); it != col.end(); it++)
   225                         for (
int j=0; j<6; j++)
   232                 for (
int kk=0; kk<k+1; kk++)
   246        Ax = (
double *)chA->x;   
   250      for (
int i=0; i<(int)cols.size(); i++)
   253          map<int,Matrix<double,6,6>, less<int>, 
   254            aligned_allocator<Matrix<double,6,6> > > &col = cols[i];
   257          for (
int k=0; k<6; k++)
   263                  map<int,Matrix<double,6,6>, less<int>, 
   264                    aligned_allocator<Matrix<double,6,6> > >::iterator it;
   267                  for (it = col.begin(); it != col.end(); it++)
   269                      Matrix<double,6,6> &m = it->second;
   270                      for (
int j=0; j<6; j++)
   276              Matrix<double,6,6> &m = diag[i]; 
   277              for (
int kk=0; kk<k+1; kk++)
   278                Ax[colp++] = m(kk,k);
   279              Ax[colp-1] *= diaginc; 
   287   void CSparse::uncompress(MatrixXd &m)
   290     m.setZero(csize,csize);
   296     for (
int i=0; i<csize; i++)
   301           for (
int j=rbeg; j<rend; j++)
   307   bool CSparse::doChol()
   312         cholmod_dense *x, b, *R, *R2;
   314         double *Xx, *Rx, *bb;
   315         double one [2], minusone [2];
   322         cholmod_print_sparse (chA, (
char *)
"A", &Common) ; 
   327         b.xtype = CHOLMOD_REAL;
   328         b.dtype = CHOLMOD_DOUBLE;
   331         L = cholmod_analyze (chA, &Common) ; 
   333         cholmod_factorize (chA, L, &Common) ; 
   335         x = cholmod_solve (CHOLMOD_A, L, &b, &Common) ; 
   341         R = cholmod_copy_dense (&b, &Common) ;
   342         cholmod_sdmult(chA, 0, minusone, one, x, R, &Common) ;
   344         R2 = cholmod_solve (CHOLMOD_A, L, R, &Common) ;
   346         Xx = (
double *)x->x ;
   347         Rx = (
double *)R2->x ;
   348         for (
int i=0 ; i<csize ; i++)
   350           Xx[i] = Xx[i] + Rx[i] ;
   352         cholmod_free_dense (&R2, &Common) ;
   353         cholmod_free_dense (&R, &Common) ;
   356         for (
int i=0; i<csize; i++) 
   358         cholmod_free_factor (&L, &Common) ; 
   359         cholmod_free_dense (&x, &Common) ;
   360         cholmod_free_sparse(&chA, &Common);
   361         cholmod_finish (&Common) ;   
   371         if (csize > 400) order = 1;
   372         bool ok = (bool)cs_cholsol(order,A,B.data()); 
   384   CSparse::doBPCG(
int iters, 
double tol, 
int sba_iter)
   390     if (sba_iter > 0) abstol = 
true;
   392     ret = bpcg.doBPCG2(iters, tol, diag, cols, x, B, abstol);
   402   CSparse2d::CSparse2d()
   418   CSparse2d::~CSparse2d()
   421     if (AF) cs_spfree(AF);      
   426   void CSparse2d::setupBlockStructure(
int n, 
bool eraseit)
   433           for (
int i=0; i<(int)cols.size(); i++)
   435               map<int,Matrix<double,3,3>, less<int>, 
   436                 aligned_allocator< Matrix <double,3,3> > > &col = cols[i];
   447         for (
int i=0; i<(int)diag.size(); i++)
   449         for (
int i=0; i<(int)cols.size(); i++)
   451             map<int,Matrix<double,3,3>, less<int>, 
   452               aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   455                 map<int,Matrix<double,3,3>, less<int>, 
   456                   aligned_allocator<Matrix<double,3,3> > >::iterator it;
   457                 for (it = col.begin(); it != col.end(); it++)
   458                   it->second.setZero();
   466         if (Bprev.size() > 0)
   467           B.head(Bprev.size()) = Bprev;
   473   void CSparse2d::addOffdiagBlock(Matrix<double,3,3> &m, 
int ii, 
int jj)
   476     map<int,Matrix<double,3,3>, less<int>, 
   477       aligned_allocator<Matrix<double,3,3> > > &col = cols[jj];
   479     map<int,Matrix<double,3,3>, less<int>, 
   480       aligned_allocator<Matrix<double,3,3> > >::iterator it;
   483       col.insert(pair<
int,Matrix<double,3,3> >(ii,m));
   489   void CSparse2d::incDiagBlocks(
double lam)
   491     for (
int i=0; i<(int)diag.size(); i++)
   492       diag[i].diagonal() *= lam;
   502   void CSparse2d::setupCSstructure(
double diaginc, 
bool init)
   506       cholmod_start(&Common); 
   512     if (init || useCholmod)
   518         for (
int i=0; i<(int)cols.size(); i++)
   520             map<int,Matrix<double,3,3>, less<int>, 
   521               aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   522             nnz += 9 * col.size(); 
   530             chA = cholmod_allocate_sparse(csize,csize,nnz,
true,
true,1,CHOLMOD_REAL,&Common);
   535             A = cs_spalloc(csize,csize,nnz,1,0); 
   554         for (
int i=0; i<(int)cols.size(); i++)
   557             map<int,Matrix<double,3,3>, less<int>, 
   558               aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   561             for (
int k=0; k<3; k++)
   570                     map<int,Matrix<double,3,3>, less<int>, 
   571                       aligned_allocator<Matrix<double,3,3> > >::iterator it;
   574                     for (it = col.begin(); it != col.end(); it++)
   577                         for (
int j=0; j<3; j++)
   584                 for (
int kk=0; kk<k+1; kk++)
   596        Ax = (
double *)chA->x;   
   601      for (
int i=0; i<(int)cols.size(); i++)
   604          map<int,Matrix<double,3,3>, less<int>, 
   605            aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   608          for (
int k=0; k<3; k++)
   614                  map<int,Matrix<double,3,3>, less<int>, 
   615                    aligned_allocator<Matrix<double,3,3> > >::iterator it;
   618                  for (it = col.begin(); it != col.end(); it++)
   620                      Matrix<double,3,3> &m = it->second;
   621                      for (
int j=0; j<3; j++)
   627              Matrix<double,3,3> &m = diag[i]; 
   628              for (
int kk=0; kk<k+1; kk++)
   629                Ax[colp++] = m(kk,k);
   630              Ax[colp-1] *= diaginc; 
   641   void CSparse2d::uncompress(MatrixXd &m)
   644     m.setZero(csize,csize);
   650     for (
int i=0; i<csize; i++)
   655           for (
int j=rbeg; j<rend; j++)
   661   bool CSparse2d::doChol()
   666         cholmod_dense *x, b, *R, *R2;
   668         double *Xx, *Rx, *bb;
   669         double one [2], minusone [2];
   676         cholmod_print_sparse (chA, (
char *)
"A", &Common) ; 
   681         b.xtype = CHOLMOD_REAL;
   682         b.dtype = CHOLMOD_DOUBLE;
   685         L = cholmod_analyze (chA, &Common) ; 
   687         cholmod_factorize (chA, L, &Common) ; 
   689         x = cholmod_solve (CHOLMOD_A, L, &b, &Common) ; 
   695         R = cholmod_copy_dense (&b, &Common) ;
   696         cholmod_sdmult(chA, 0, minusone, one, x, R, &Common) ;
   698         R2 = cholmod_solve (CHOLMOD_A, L, R, &Common) ;
   700         Xx = (
double *)x->x ;
   701         Rx = (
double *)R2->x ;
   702         for (
int i=0 ; i<csize ; i++)
   704           Xx[i] = Xx[i] + Rx[i] ;
   706         cholmod_free_dense (&R2, &Common) ;
   707         cholmod_free_dense (&R, &Common) ;
   710         for (
int i=0; i<csize; i++) 
   712         cholmod_free_factor (&L, &Common) ; 
   713         cholmod_free_dense (&x, &Common) ;
   714         cholmod_free_sparse(&chA, &Common);
   715         cholmod_finish (&Common) ;   
   726         if (csize > 100) order = 1;
   727         bool ok = (bool)cs_cholsol(order,A,B.data()); 
   739   CSparse2d::doBPCG(
int iters, 
double tol, 
int sba_iter)
   745     if (sba_iter > 0) abstol = 
true;
   747     ret = bpcg.doBPCG2(iters, tol, diag, cols, x, B, abstol);
   756   int CSparse2d::doPCG(
int iters)
   763     AT = cs_transpose(A,1);
   764     cs_fkeep (AT, &dropdiag, NULL); 
   765     if (AF) cs_spfree(AF);      
   766     AF = cs_add (A, AT, 1, 1);  
   770     CompCol_Mat_double Ap(csize, csize, AF->nzmax, AF->x, AF->i, AF->p); 
   771     VECTOR_double b(B.data(),csize);   
   772     VECTOR_double x(csize, 0.0); 
   777     ICPreconditioner_double D(Ap); 
   778     res = CG(Ap,x,b,D,iters,tol); 
   780     for (
int i=0; i<csize; i++)