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()
   417   CSparse2d::~CSparse2d()
   420     if (AF) cs_spfree(AF);      
   425   void CSparse2d::setupBlockStructure(
int n, 
bool eraseit)
   432           for (
int i=0; i<(int)cols.size(); i++)
   434               map<int,Matrix<double,3,3>, less<int>, 
   435                 aligned_allocator< Matrix <double,3,3> > > &col = cols[i];
   446         for (
int i=0; i<(int)diag.size(); i++)
   448         for (
int i=0; i<(int)cols.size(); i++)
   450             map<int,Matrix<double,3,3>, less<int>, 
   451               aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   454                 map<int,Matrix<double,3,3>, less<int>, 
   455                   aligned_allocator<Matrix<double,3,3> > >::iterator it;
   456                 for (it = col.begin(); it != col.end(); it++)
   457                   it->second.setZero();
   465         if (Bprev.size() > 0)
   466           B.head(Bprev.size()) = Bprev;
   472   void CSparse2d::addOffdiagBlock(Matrix<double,3,3> &m, 
int ii, 
int jj)
   475     map<int,Matrix<double,3,3>, less<int>, 
   476       aligned_allocator<Matrix<double,3,3> > > &col = cols[jj];
   478     map<int,Matrix<double,3,3>, less<int>, 
   479       aligned_allocator<Matrix<double,3,3> > >::iterator it;
   482       col.insert(pair<
int,Matrix<double,3,3> >(ii,m));
   488   void CSparse2d::incDiagBlocks(
double lam)
   490     for (
int i=0; i<(int)diag.size(); i++)
   491       diag[i].diagonal() *= lam;
   501   void CSparse2d::setupCSstructure(
double diaginc, 
bool init)
   505       cholmod_start(&Common); 
   511     if (init || useCholmod)
   517         for (
int i=0; i<(int)cols.size(); i++)
   519             map<int,Matrix<double,3,3>, less<int>, 
   520               aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   521             nnz += 9 * col.size(); 
   529             chA = cholmod_allocate_sparse(csize,csize,nnz,
true,
true,1,CHOLMOD_REAL,&Common);
   534             A = cs_spalloc(csize,csize,nnz,1,0); 
   553         for (
int i=0; i<(int)cols.size(); i++)
   556             map<int,Matrix<double,3,3>, less<int>, 
   557               aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   560             for (
int k=0; k<3; k++)
   569                     map<int,Matrix<double,3,3>, less<int>, 
   570                       aligned_allocator<Matrix<double,3,3> > >::iterator it;
   573                     for (it = col.begin(); it != col.end(); it++)
   576                         for (
int j=0; j<3; j++)
   583                 for (
int kk=0; kk<k+1; kk++)
   595        Ax = (
double *)chA->x;   
   600      for (
int i=0; i<(int)cols.size(); i++)
   603          map<int,Matrix<double,3,3>, less<int>, 
   604            aligned_allocator<Matrix<double,3,3> > > &col = cols[i];
   607          for (
int k=0; k<3; k++)
   613                  map<int,Matrix<double,3,3>, less<int>, 
   614                    aligned_allocator<Matrix<double,3,3> > >::iterator it;
   617                  for (it = col.begin(); it != col.end(); it++)
   619                      Matrix<double,3,3> &m = it->second;
   620                      for (
int j=0; j<3; j++)
   626              Matrix<double,3,3> &m = diag[i]; 
   627              for (
int kk=0; kk<k+1; kk++)
   628                Ax[colp++] = m(kk,k);
   629              Ax[colp-1] *= diaginc; 
   640   void CSparse2d::uncompress(MatrixXd &m)
   643     m.setZero(csize,csize);
   649     for (
int i=0; i<csize; i++)
   654           for (
int j=rbeg; j<rend; j++)
   660   bool CSparse2d::doChol()
   665         cholmod_dense *x, b, *R, *R2;
   667         double *Xx, *Rx, *bb;
   668         double one [2], minusone [2];
   675         cholmod_print_sparse (chA, (
char *)
"A", &Common) ; 
   680         b.xtype = CHOLMOD_REAL;
   681         b.dtype = CHOLMOD_DOUBLE;
   684         L = cholmod_analyze (chA, &Common) ; 
   686         cholmod_factorize (chA, L, &Common) ; 
   688         x = cholmod_solve (CHOLMOD_A, L, &b, &Common) ; 
   694         R = cholmod_copy_dense (&b, &Common) ;
   695         cholmod_sdmult(chA, 0, minusone, one, x, R, &Common) ;
   697         R2 = cholmod_solve (CHOLMOD_A, L, R, &Common) ;
   699         Xx = (
double *)x->x ;
   700         Rx = (
double *)R2->x ;
   701         for (
int i=0 ; i<csize ; i++)
   703           Xx[i] = Xx[i] + Rx[i] ;
   705         cholmod_free_dense (&R2, &Common) ;
   706         cholmod_free_dense (&R, &Common) ;
   709         for (
int i=0; i<csize; i++) 
   711         cholmod_free_factor (&L, &Common) ; 
   712         cholmod_free_dense (&x, &Common) ;
   713         cholmod_free_sparse(&chA, &Common);
   714         cholmod_finish (&Common) ;   
   725         if (csize > 100) order = 1;
   726         bool ok = (bool)cs_cholsol(order,A,B.data()); 
   738   CSparse2d::doBPCG(
int iters, 
double tol, 
int sba_iter)
   744     if (sba_iter > 0) abstol = 
true;
   746     ret = bpcg.doBPCG2(iters, tol, diag, cols, x, B, abstol);
   755   int CSparse2d::doPCG(
int iters)
   762     AT = cs_transpose(A,1);
   763     cs_fkeep (AT, &dropdiag, NULL); 
   764     if (AF) cs_spfree(AF);      
   765     AF = cs_add (A, AT, 1, 1);  
   769     CompCol_Mat_double Ap(csize, csize, AF->nzmax, AF->x, AF->i, AF->p); 
   770     VECTOR_double b(B.data(),csize);   
   771     VECTOR_double x(csize, 0.0); 
   776     ICPreconditioner_double D(Ap); 
   777     res = CG(Ap,x,b,D,iters,tol); 
   779     for (
int i=0; i<csize; i++)