cholesky_solver.h 4.34 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library 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 GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

/*
 * This interface to CHOLMOD is largely inspired from the one found in
 * Graphite developped by Bruno Levy, ALICE Project, LORIA, INRIA Lorraine
 */


#ifndef __CHOLESKY_SOLVER__
#define __CHOLESKY_SOLVER__

#include <cassert>
#include "OpenNL/sparse_matrix.h"
#include "CHOLMOD/cholmod.h"

template< class MATRIX, class VECTOR >
class Solver_CHOLESKY
{
public:
    typedef MATRIX Matrix ;
    typedef VECTOR Vector ;
    typedef typename Vector::CoeffType CoeffType ;

    Solver_CHOLESKY() {
    	N = 0 ;
    	factorized_ = false ;
    	L = NULL ;
		// Initialize CHOLMOD library
		cholmod_start(&c) ;
    }

    ~Solver_CHOLESKY() {
		cholmod_free_factor(&L, &c) ;
		cholmod_finish(&c) ;
    }

    bool factorized() { return factorized_ ; }

    void factorize(const MATRIX &A_in) {
    	assert(A_in.n() == A_in.m()) ;
		assert(A_in.has_symmetric_storage()) ;

		N = A_in.n() ;
		int NNZ = A_in.nnz() ;

		// Translate sparse matrix into cholmod format
		cholmod_sparse* A = cholmod_allocate_sparse(N, N, NNZ, false, true, -1, CHOLMOD_REAL, &c);

		int* colptr = static_cast<int*>(A->p) ;
		int* rowind = static_cast<int*>(A->i) ;
		double* a = static_cast<double*>(A->x) ;

		// Convert local Matrix into CHOLMOD Matrix
		int count = 0 ;
		for(int j = 0; j < N; j++) {
			const typename SparseMatrix<CoeffType>::Column& Cj = A_in.column(j) ;
			colptr[j] = count ;
			for(unsigned int ii = 0; ii < Cj.nb_coeffs(); ii++) {
				a[count] = Cj.coeff(ii).a ;
				rowind[count] = Cj.coeff(ii).index ;
				count++ ;
			}
		}
		colptr[N] = NNZ ;

		// Factorize
		L = cholmod_analyze(A, &c) ;
		cholmod_factorize(A, L, &c) ;
		factorized_ = true ;

		// Cleanup
		cholmod_free_sparse(&A, &c) ;
    }

    void solve(const VECTOR& b_in, VECTOR& x_out) {
    	assert(factorized_) ;
        assert(L->n == b_in.dimension()) ;
        assert(L->n == x_out.dimension()) ;

		// Translate right-hand side into cholmod format
		cholmod_dense* b = cholmod_allocate_dense(N, 1, N, CHOLMOD_REAL, &c) ;
		double* cb = static_cast<double*>(b->x) ;
		for(int i = 0; i < N; ++i)
			cb[i] = b_in[i] ;

		// Solve
		cholmod_dense* x = cholmod_solve(CHOLMOD_A, L, b, &c) ;

		// Get result
		double* cx = static_cast<double*>(x->x) ;
		for(int i = 0; i < N; ++i)
			x_out[i] = cx[i] ;

		// Cleanup
		cholmod_free_dense(&x, &c) ;
		cholmod_free_dense(&b, &c) ;
    }

    void reset() {
    	if(factorized_)
    	{
    		cholmod_free_factor(&L, &c) ;
			factorized_ = false ;
			L = NULL ;
    	}
    }

private:
    int N ;
    bool factorized_ ;
	cholmod_common c ;
	cholmod_factor* L ;
} ;

#endif