Commit ba42bbe3 authored by Pierre Kraemer's avatar Pierre Kraemer

some updates in openNL linear solver

parent 58dde55b
...@@ -17,6 +17,7 @@ find_package(GLEW REQUIRED) ...@@ -17,6 +17,7 @@ find_package(GLEW REQUIRED)
find_package(Qt4 REQUIRED) find_package(Qt4 REQUIRED)
find_package(QGLViewer REQUIRED) find_package(QGLViewer REQUIRED)
find_package(PythonLibs 2.7 REQUIRED) find_package(PythonLibs 2.7 REQUIRED)
find_package(SuiteSparse REQUIRED)
SET( QT_USE_QTOPENGL TRUE ) SET( QT_USE_QTOPENGL TRUE )
SET( QT_USE_QTXML TRUE ) SET( QT_USE_QTXML TRUE )
...@@ -66,6 +67,7 @@ SET (EXT_INCLUDES ...@@ -66,6 +67,7 @@ SET (EXT_INCLUDES
${QT_INCLUDE_DIR} ${QT_INCLUDE_DIR}
${QGLVIEWER_INCLUDE_DIR} ${QGLVIEWER_INCLUDE_DIR}
${PYTHON_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS}
${SUITESPARSE_INCLUDE_DIRS}
) )
# define libs for external libs # define libs for external libs
...@@ -81,6 +83,7 @@ SET (EXT_LIBS ...@@ -81,6 +83,7 @@ SET (EXT_LIBS
${QT_LIBRARIES} ${QT_LIBRARIES}
${QGLVIEWER_LIBRARIES} ${QGLVIEWER_LIBRARIES}
${PYTHON_LIBRARIES} ${PYTHON_LIBRARIES}
${SUITESPARSE_LIBRARIES}
) )
......
...@@ -5,7 +5,7 @@ differentialPropertiesPlugin = schnapps.loadPlugin("DifferentialProperties"); ...@@ -5,7 +5,7 @@ differentialPropertiesPlugin = schnapps.loadPlugin("DifferentialProperties");
subdivisionPlugin = schnapps.loadPlugin("SubdivideSurface"); subdivisionPlugin = schnapps.loadPlugin("SubdivideSurface");
surfaceDeformationPlugin = schnapps.loadPlugin("SurfaceDeformation"); surfaceDeformationPlugin = schnapps.loadPlugin("SurfaceDeformation");
obj = importPlugin.importFromFile("/home/kraemer/Media/Data/surface/midRes/camel_10k.off"); obj = importPlugin.importFromFile("/home/kraemer/Media/Data/surface/lowRes/iphi_good_9k.off");
v = schnapps.getView("view_0"); v = schnapps.getView("view_0");
......
#include <QSplashScreen> #include <QSplashScreen>
#include "window.h" #include "window.h"
#include <QFileInfo> #include <QFileInfo>
#include "PythonQt/PythonQt.h" #include "PythonQt/PythonQt.h"
#include "PythonQt/gui/PythonQtScriptingConsole.h" #include "PythonQt/gui/PythonQtScriptingConsole.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#define __CHOLESKY_SOLVER__ #define __CHOLESKY_SOLVER__
#include <cassert> #include <cassert>
#include "cholmod.h" #include <cholmod.h>
template <typename CoeffType> template <typename CoeffType>
class Solver_CHOLESKY class Solver_CHOLESKY
...@@ -58,43 +58,41 @@ public: ...@@ -58,43 +58,41 @@ public:
// assert(A_in.has_symmetric_storage()) ; // assert(A_in.has_symmetric_storage()) ;
N = A_in.rows() ; N = A_in.rows() ;
// int NNZ = A_in.nonZeros() ;
// Translate sparse matrix into cholmod format // Translate sparse matrix into cholmod format
// cholmod_sparse* A = cholmod_allocate_sparse(N, N, NNZ, false, true, -1, CHOLMOD_REAL, &c); int NNZ = A_in.nonZeros() ;
cholmod_sparse* A = cholmod_allocate_sparse(N, N, NNZ, false, true, -1, CHOLMOD_REAL, &c);
// int* colptr = static_cast<int*>(A->p) ; int* colptr = static_cast<int*>(A->p) ;
// int* rowind = static_cast<int*>(A->i) ; int* rowind = static_cast<int*>(A->i) ;
// double* a = static_cast<double*>(A->x) ; double* a = static_cast<double*>(A->x) ;
// Convert local Matrix into CHOLMOD Matrix // Convert local Matrix into CHOLMOD Matrix
// int count = 0 ; int count = 0 ;
// for(int j = 0; j < N; j++) { for(int j = 0; j < N; j++) {
// const typename SparseMatrix<CoeffType>::Column& Cj = A_in.column(j) ; colptr[j] = count;
// colptr[j] = count ; for(typename Eigen::SparseMatrix<CoeffType>::InnerIterator it(A_in, j); it; ++it)
// for(unsigned int ii = 0; ii < Cj.nb_coeffs(); ii++) { {
// a[count] = Cj.coeff(ii).a ; a[count] = it.value();
// rowind[count] = Cj.coeff(ii).index ; rowind[count] = it.row();
// count++ ; ++count;
// } }
// } }
// colptr[N] = NNZ ; colptr[N] = NNZ ;
cholmod_sparse A = Eigen::viewAsCholmod(A_in);
// Factorize // Factorize
L = cholmod_analyze(&A, &c) ; L = cholmod_analyze(A, &c) ;
cholmod_factorize(&A, L, &c) ; cholmod_factorize(A, L, &c) ;
factorized_ = true ; factorized_ = true ;
// Cleanup // Cleanup
// cholmod_free_sparse(&A, &c) ; cholmod_free_sparse(&A, &c) ;
} }
void solve(const VECTOR& b_in, VECTOR& x_out) { void solve(const Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>& b_in, Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>& x_out) {
assert(factorized_) ; assert(factorized_) ;
assert(L->n == b_in.dimension()) ; assert(L->n == b_in.rows()) ;
assert(L->n == x_out.dimension()) ; assert(L->n == x_out.rows()) ;
// Translate right-hand side into cholmod format // Translate right-hand side into cholmod format
cholmod_dense* b = cholmod_allocate_dense(N, 1, N, CHOLMOD_REAL, &c) ; cholmod_dense* b = cholmod_allocate_dense(N, 1, N, CHOLMOD_REAL, &c) ;
...@@ -111,8 +109,8 @@ public: ...@@ -111,8 +109,8 @@ public:
x_out[i] = cx[i] ; x_out[i] = cx[i] ;
// Cleanup // Cleanup
cholmod_free_dense(&x, &c) ;
cholmod_free_dense(&b, &c) ; cholmod_free_dense(&b, &c) ;
cholmod_free_dense(&x, &c) ;
} }
void reset() { void reset() {
......
...@@ -36,10 +36,8 @@ ...@@ -36,10 +36,8 @@
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
//#include "cholesky_solver.h"
#include <Eigen/Cholesky>
#include <Eigen/Sparse> #include <Eigen/Sparse>
#include "cholesky_solver.h"
template <class CoeffType> template <class CoeffType>
...@@ -83,7 +81,7 @@ public: ...@@ -83,7 +81,7 @@ public:
delete A_ ; delete A_ ;
delete x_ ; delete x_ ;
delete b_ ; delete b_ ;
// delete direct_solver_ ; delete direct_solver_ ;
} }
// __________________ Parameters ________________________ // __________________ Parameters ________________________
...@@ -191,13 +189,7 @@ private: ...@@ -191,13 +189,7 @@ private:
Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>* x_ ; Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>* x_ ;
Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>* b_ ; Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>* b_ ;
// Solver_CHOLESKY<CoeffType>* direct_solver_; Solver_CHOLESKY<CoeffType>* direct_solver_ ;
Eigen::LDLT<Eigen::SparseMatrix<CoeffType> >* direct_solver_;
// Eigen::ConjugateGradient<Eigen::SparseMatrix<CoeffType> >* symmetric_solver_;
// Eigen::BiCGSTAB<Eigen::SparseMatrix<CoeffType> >* nonsymmetric_solver_;
// Eigen::SimplicialLDLT<Eigen::SparseMatrix<CoeffType> >* direct_solver_;
} ; } ;
#include "OpenNL/linear_solver.hpp" #include "OpenNL/linear_solver.hpp"
......
...@@ -33,9 +33,7 @@ LinearSolver<CoeffType>::LinearSolver(unsigned int nb_variables) { ...@@ -33,9 +33,7 @@ LinearSolver<CoeffType>::LinearSolver(unsigned int nb_variables) {
A_ = NULL ; A_ = NULL ;
x_ = NULL ; x_ = NULL ;
b_ = NULL ; b_ = NULL ;
// symmetric_solver_ = NULL; direct_solver_ = new Solver_CHOLESKY<CoeffType>() ;
// nonsymmetric_solver_ = NULL;
direct_solver_ = NULL;
} }
template <typename CoeffType> template <typename CoeffType>
...@@ -138,7 +136,7 @@ void LinearSolver<CoeffType>::end_row() { ...@@ -138,7 +136,7 @@ void LinearSolver<CoeffType>::end_row() {
if(!matrix_already_set_) { if(!matrix_already_set_) {
for(unsigned int i = 0; i < nf; i++) { for(unsigned int i = 0; i < nf; i++) {
for(unsigned int j = 0; j < nf; j++) { for(unsigned int j = 0; j < nf; j++) {
A_->coeffRef(if_[i], if_[j]) += af_[i] * af_[j]; A_->coeffRef(if_[i], if_[j]) += af_[i] * af_[j] ;
} }
} }
} }
...@@ -156,11 +154,11 @@ void LinearSolver<CoeffType>::end_row() { ...@@ -156,11 +154,11 @@ void LinearSolver<CoeffType>::end_row() {
// Construct the matrix coefficients of the current row // Construct the matrix coefficients of the current row
if(!matrix_already_set_) { if(!matrix_already_set_) {
for(unsigned int i = 0; i < nf; i++) { for(unsigned int i = 0; i < nf; i++) {
A_->coeffRef(current_row_, if_[i]) += af_[i]; A_->coeffRef(current_row_, if_[i]) += af_[i] ;
} }
} }
// Construct the right-hand side of the current row // Construct the right-hand side of the current row
(*b_)[current_row_] = bk_ ; (*b_)[current_row_] = -bk_ ;
for(unsigned int i = 0; i < nl; i++) { for(unsigned int i = 0; i < nl; i++) {
(*b_)[current_row_] -= al_[i] * xl_[i] ; (*b_)[current_row_] -= al_[i] * xl_[i] ;
} }
...@@ -171,13 +169,10 @@ void LinearSolver<CoeffType>::end_row() { ...@@ -171,13 +169,10 @@ void LinearSolver<CoeffType>::end_row() {
template <typename CoeffType> template <typename CoeffType>
void LinearSolver<CoeffType>::end_system() { void LinearSolver<CoeffType>::end_system() {
if(least_squares_ && direct_ && !direct_solver_) if(least_squares_ && direct_ && !direct_solver_->factorized())
direct_solver_ = new Eigen::LDLT<Eigen::SparseMatrix<CoeffType> >(*A_); direct_solver_->factorize(*A_) ;
// if(least_squares_ && direct_ && !direct_solver_->factorized()) transition(IN_SYSTEM, CONSTRUCTED) ;
// direct_solver_->factorize(*A_);
transition(IN_SYSTEM, CONSTRUCTED) ;
} }
template <typename CoeffType> template <typename CoeffType>
...@@ -186,45 +181,48 @@ void LinearSolver<CoeffType>::solve() { ...@@ -186,45 +181,48 @@ void LinearSolver<CoeffType>::solve() {
if(least_squares_) if(least_squares_)
{ {
if(direct_) { if(direct_) {
*x_ = direct_solver_->solve(*b_) ; direct_solver_->solve(*b_, *x_) ;
} else { } else {
Eigen::ConjugateGradient<Eigen::SparseMatrix<CoeffType> > solver(*A_) ; Eigen::ConjugateGradient<Eigen::MatrixXf > solver(*A_) ;
// int n = x_->rows() ;
// Eigen::Matrix<CoeffType, Eigen::Dynamic, 1> guess(n) ;
// for(int i = 0; i < n; ++i)
// guess[i] = (*x_)[i] ;
// *x_ = solver.solveWithGuess(*b_, guess) ;
*x_ = solver.solve(*b_) ; *x_ = solver.solve(*b_) ;
} }
} else { } else {
Eigen::BiCGSTAB<Eigen::SparseMatrix<CoeffType> > solver(*A_) ; Eigen::BiCGSTAB<Eigen::MatrixXf > solver(*A_) ;
*x_ = solver.solve(*b_) ; *x_ = solver.solve(*b_) ;
} }
vector_to_variables() ; vector_to_variables() ;
transition(CONSTRUCTED, SOLVED) ; transition(CONSTRUCTED, SOLVED) ;
} }
template <typename CoeffType> template <typename CoeffType>
void LinearSolver<CoeffType>::reset(bool keep_matrix) { void LinearSolver<CoeffType>::reset(bool keep_matrix) {
if(keep_matrix) { if(keep_matrix) {
x_->setZero(); x_->setZero() ;
b_->setZero(); b_->setZero() ;
matrix_already_set_ = true ; matrix_already_set_ = true ;
} else { } else {
delete A_ ; A_ = NULL ; delete A_ ; A_ = NULL ;
delete x_ ; x_ = NULL ; delete x_ ; x_ = NULL ;
delete b_ ; b_ = NULL ; delete b_ ; b_ = NULL ;
// if(symmetric_solver_) delete symmetric_solver_ ; symmetric_solver_ = NULL ; direct_solver_->reset();
// if(nonsymmetric_solver_) delete nonsymmetric_solver_ ; nonsymmetric_solver_ = NULL ; matrix_already_set_ = false ;
if(direct_solver_) delete direct_solver_ ; direct_solver_ = NULL ; for(unsigned int i = 0; i < nb_variables_; ++i) {
// direct_solver_->reset(); variable_[i].unlock() ;
matrix_already_set_ = false ; }
for(unsigned int i = 0; i < nb_variables_; ++i) { }
variable_[i].unlock() ;
}
}
state_ = INITIAL ; state_ = INITIAL ;
} }
template <typename CoeffType> template <typename CoeffType>
void LinearSolver<CoeffType>::vector_to_variables() { void LinearSolver<CoeffType>::vector_to_variables() {
for(unsigned int i=0; i < nb_variables(); i++) { for(unsigned int i=0; i < nb_variables(); i++) {
Variable<CoeffType>& v = variable(i) ; Variable<CoeffType>& v = variable(i) ;
if(!v.is_locked()) { if(!v.is_locked()) {
v.set_value((*x_)[v.index()]) ; v.set_value((*x_)[v.index()]) ;
} }
...@@ -234,7 +232,7 @@ void LinearSolver<CoeffType>::vector_to_variables() { ...@@ -234,7 +232,7 @@ void LinearSolver<CoeffType>::vector_to_variables() {
template <typename CoeffType> template <typename CoeffType>
void LinearSolver<CoeffType>::variables_to_vector() { void LinearSolver<CoeffType>::variables_to_vector() {
for(unsigned int i=0; i < nb_variables(); i++) { for(unsigned int i=0; i < nb_variables(); i++) {
Variable<CoeffType>& v = variable(i) ; Variable<CoeffType>& v = variable(i) ;
if(!v.is_locked()) { if(!v.is_locked()) {
(*x_)[v.index()] = v.value() ; (*x_)[v.index()] = v.value() ;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment