CholmodSolver.cc 6.39 KB
Newer Older
Henrik Zimmer's avatar
Henrik Zimmer committed
1 2
/*===========================================================================*\
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
3
 *                               CoMISo                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
4
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
Henrik Zimmer's avatar
Henrik Zimmer committed
5
 *                           www.rwth-graphics.de                            *
Henrik Zimmer's avatar
Henrik Zimmer committed
6
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
7 8
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
9
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
10 11 12 13
 *  CoMISo is free software: you can redistribute it and/or modify           *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation, either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
14 15 16 17
 *                                                                           *
 *  CoMISo is distributed in the hope that it will be useful,                *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
Henrik Zimmer's avatar
Henrik Zimmer committed
18
 *  GNU General Public License for more details.                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
19
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
20 21
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
Henrik Zimmer's avatar
Henrik Zimmer committed
22
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
23
\*===========================================================================*/ 
Henrik Zimmer's avatar
Henrik Zimmer committed
24

25

Henrik Zimmer's avatar
Henrik Zimmer committed
26

27 28 29
#include "CholmodSolver.hh"


30
namespace COMISO {
31 32 33 34 35 36 37

CholmodSolver::CholmodSolver()
{
    mp_cholmodCommon = new cholmod_common;
    cholmod_start( mp_cholmodCommon );

    mp_L = 0;
David Bommes's avatar
David Bommes committed
38 39 40 41 42 43 44 45 46 47 48 49

    show_timings_ = false;

    mp_cholmodCommon->nmethods = 1;
    // use AMD ordering
    mp_cholmodCommon->method[0].ordering = CHOLMOD_AMD ;

    // use METIS ordering
    //    mp_cholmodCommon->method[0].ordering = CHOLMOD_METIS ;

    // try all methods
    // mp_cholmodCommon->nmethods = 9;
50 51 52 53 54 55 56 57 58 59 60 61 62 63
}


  //-----------------------------------------------------------------------------
  

CholmodSolver::~CholmodSolver()
{
    if( mp_L )
    {
	cholmod_free_factor( &mp_L, mp_cholmodCommon );
    }

    cholmod_finish( mp_cholmodCommon );
64 65
    delete mp_cholmodCommon;
    mp_cholmodCommon = NULL;
66 67 68 69 70 71 72 73 74 75
}
  

//-----------------------------------------------------------------------------


bool CholmodSolver::calc_system( const std::vector<int>&    _colptr, 
				 const std::vector<int>&    _rowind, 
				 const std::vector<double>& _values)
{
David Bommes's avatar
David Bommes committed
76 77
    if(show_timings_) sw_.start();

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    colptr_ = _colptr;
    rowind_ = _rowind;
    values_ = _values;

    int n   = colptr_.size()-1;

    cholmod_sparse matA;

    matA.nrow = n;
    matA.ncol = n;
    matA.nzmax = _values.size();

    matA.p = &colptr_[0];
    matA.i = &rowind_[0];
    matA.x = &values_[0];
    matA.nz = 0;
    matA.z = 0;
    
David Bommes's avatar
David Bommes committed
96 97
    //    matA.stype = -1;
    matA.stype = 1;
98 99 100 101 102 103
    matA.itype = CHOLMOD_INT;
    matA.xtype = CHOLMOD_REAL;
    matA.dtype = CHOLMOD_DOUBLE;
    matA.sorted = 1;
    matA.packed = 1;

David Bommes's avatar
 
David Bommes committed
104

105 106 107 108 109 110 111
    // clean up
    if( mp_L )
    {
	cholmod_free_factor( &mp_L, mp_cholmodCommon );
	mp_L = 0;
    }

David Bommes's avatar
David Bommes committed
112 113 114 115 116
    if(show_timings_)
    {
      std::cerr << " Cholmod Timing cleanup: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }
117 118 119 120 121 122 123

    if( !(mp_L = cholmod_analyze( &matA, mp_cholmodCommon )) )
    {
	std::cout << "cholmod_analyze failed" << std::endl;
	return false;
    }

David Bommes's avatar
David Bommes committed
124 125 126 127 128 129 130 131 132 133 134
    // // show selected ordering method
    // std::cerr << "best    ordering was: " << mp_cholmodCommon->selected << std::endl;
    // std::cerr << "current ordering was: " << mp_cholmodCommon->current  << std::endl;


    if(show_timings_)
    {
      std::cerr << " Cholmod Timing analyze: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

135 136 137 138 139 140
    if( !cholmod_factorize( &matA, mp_L, mp_cholmodCommon ) )
    {
	std::cout << "cholmod_factorize failed" << std::endl;
	return false;
    }

David Bommes's avatar
David Bommes committed
141 142 143 144 145
    if(show_timings_)
    {
      std::cerr << " Cholmod Timing factorize: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    return true;
}


  //-----------------------------------------------------------------------------

    
bool CholmodSolver::update_system( const std::vector<int>& _colptr, 
				   const std::vector<int>& _rowind, 
				   const std::vector<double>& _values )
{
    if( !mp_L )
	return false;

    colptr_ = _colptr;
    rowind_ = _rowind;
    values_ = _values;
    int n   = colptr_.size()-1;

    cholmod_sparse matA;

    matA.nrow = n;
    matA.ncol = n;
    matA.nzmax = _values.size();

    matA.p = &colptr_[0];
    matA.i = &rowind_[0];
    matA.x = &values_[0];
    matA.nz = 0;
    matA.z = 0;
    
David Bommes's avatar
David Bommes committed
178 179
    //    matA.stype = -1;
    matA.stype = 1;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    matA.itype = CHOLMOD_INT;
    matA.xtype = CHOLMOD_REAL;
    matA.dtype = CHOLMOD_DOUBLE;
    matA.sorted = 1;
    matA.packed = 1;


    if( !cholmod_factorize( &matA, mp_L, mp_cholmodCommon ) )
    {
	std::cout << "cholmod_factorize failed" << std::endl;
	return false;
    }

    return true;
}


//-----------------------------------------------------------------------------
  

bool CholmodSolver::solve( double * _x, double * _b)
{
    const unsigned int n = colptr_.size() - 1;

    cholmod_dense *x, b;


    b.nrow = n;
    b.ncol = 1;
    b.nzmax = n;
    b.d = b.nrow;
    b.x = _b;
    b.z = 0;
    b.xtype = CHOLMOD_REAL;
    b.dtype = CHOLMOD_DOUBLE;

    if( !(x = cholmod_solve( CHOLMOD_A, mp_L, &b, mp_cholmodCommon )) )
    {
	std::cout << "cholmod_solve failed" << std::endl;
	return false;
    }
    
    for( unsigned int i = 0; i < n; ++i )
	_x[i] = ((double*)x->x)[i];

    cholmod_free_dense( &x, mp_cholmodCommon );

    return true;
}


//-----------------------------------------------------------------------------

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
int CholmodSolver::dimension()
{
  return std::max(int(0), (int)(colptr_.size()-1));
}

//-----------------------------------------------------------------------------

bool CholmodSolver::
solve ( std::vector<double>& _x0, std::vector<double>& _b)
{
  return solve( &(_x0[0]), &(_b[0]));
}

//-----------------------------------------------------------------------------

bool& CholmodSolver::
show_timings()
{
  return show_timings_;
}

254 255

}