File: SparseTAUCSSolver.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (175 lines) | stat: -rw-r--r-- 7,117 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
138
139
140
141
142
143
144
145
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
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* 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.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
// Author: Hadrien Courtecuisse
//
// Copyright: See COPYING file that comes with this distribution
#include <sofa/component/linearsolver/SparseTAUCSSolver.h>
#include <sofa/core/ObjectFactory.h>
#include <iostream>
#include "sofa/helper/system/thread/CTime.h"
#include <sofa/core/objectmodel/BaseContext.h>
#include <sofa/core/componentmodel/behavior/LinearSolver.h>
#include <math.h>
#include <sofa/helper/system/thread/CTime.h>
#include <sofa/component/linearsolver/CompressedRowSparseMatrix.h>

namespace sofa {

namespace component {

namespace linearsolver {

using namespace sofa::defaulttype;
using namespace sofa::core::componentmodel::behavior;
using namespace sofa::simulation;
using namespace sofa::core::objectmodel;
using sofa::helper::system::thread::CTime;
using sofa::helper::system::thread::ctime_t;
using std::cerr;
using std::endl;

template<class TMatrix, class TVector>
SparseTAUCSSolver<TMatrix,TVector>::SparseTAUCSSolver()
: f_options( initData(&f_options,"options","TAUCS unified solver list of space-separated options") )
, f_symmetric( initData(&f_symmetric,true,"symmetric","Consider the system matrix as symmetric") )
, f_verbose( initData(&f_verbose,false,"verbose","Dump system state at each iteration") )
, f_graph( initData(&f_graph,"graph","Graph of residuals at each iteration") )
, factorization(NULL)
{
	f_graph.setWidget("graph");
	f_graph.setReadOnly(true);
}

template<class TMatrix, class TVector>
SparseTAUCSSolver<TMatrix,TVector>::~SparseTAUCSSolver() {
    if (factorization) taucs_linsolve(NULL, &factorization, 0, NULL, NULL, NULL, NULL);
}

template<class T>
int get_taucs_flags();

template<>
int get_taucs_flags<double>() { return TAUCS_DOUBLE; }

template<>
int get_taucs_flags<float>() { return TAUCS_SINGLE; }

template<class TMatrix, class TVector>
void SparseTAUCSSolver<TMatrix,TVector>::invert(Matrix& M) {
    M.compress();
    if (f_symmetric.getValue())
    {
	Mfiltered.copyUpperNonZeros(M);
	sout << "Filtered upper part of M, nnz = " << Mfiltered.getRowBegin().back() << sendl;
    }
    else
    {
	Mfiltered.copyNonZeros(M);
	sout << "Filtered M, nnz = " << Mfiltered.getRowBegin().back() << sendl;
    }
    matrix_taucs.n = Mfiltered.rowSize();
    matrix_taucs.m = Mfiltered.colSize();
    matrix_taucs.flags = get_taucs_flags<Real>();
    if (f_symmetric.getValue())
    {
	matrix_taucs.flags |= TAUCS_SYMMETRIC;
	matrix_taucs.flags |= TAUCS_LOWER; // Upper on row-major is actually lower or column-major transposed matrix
    }
    matrix_taucs.colptr = (int *) &(Mfiltered.getRowBegin()[0]);
    matrix_taucs.rowind = (int *) &(Mfiltered.getColsIndex()[0]);
    matrix_taucs.values.d = (double*) &(Mfiltered.getColsValue()[0]);
    helper::vector<char*> opts;
    const helper::vector<std::string>& options = f_options.getValue();
    for (unsigned int i=0;i<options.size();++i)
	opts.push_back((char*)options[i].c_str());
    opts.push_back(NULL);
    if (this->f_printLog.getValue())
	taucs_logfile((char*)"stdout");
    if (factorization) taucs_linsolve(NULL, &factorization, 0, NULL, NULL, NULL, NULL);
    int rc = taucs_linsolve(&matrix_taucs, &factorization, 0, NULL, NULL, &(opts[0]), NULL);
    if (this->f_printLog.getValue())
	taucs_logfile((char*)"none");
    if (rc != TAUCS_SUCCESS)
    {
	const char* er = "";
	switch(rc)
	{
	case TAUCS_SUCCESS: er = "SUCCESS"; break;
	case TAUCS_ERROR  : er = "ERROR"; break;
	case TAUCS_ERROR_NOMEM: er = "NOMEM"; break;
	case TAUCS_ERROR_BADARGS: er = "BADARGS"; break;
	case TAUCS_ERROR_MAXDEPTH: er = "MAXDEPTH"; break;
	case TAUCS_ERROR_INDEFINITE: er = "INDEFINITE"; break;
	}
	serr << "TAUCS factorization failed: " << er << sendl;
    }

}

template<class TMatrix, class TVector>
void SparseTAUCSSolver<TMatrix,TVector>::solve (Matrix& /*M*/, Vector& z, Vector& r) {
    helper::vector<char*> opts;
    const helper::vector<std::string>& options = f_options.getValue();
    for (unsigned int i=0;i<options.size();++i)
	opts.push_back((char*)options[i].c_str());
    //opts.push_back((char*)"taucs.factor.symbolic=false");
    //opts.push_back((char*)"taucs.factor.numeric=false");
    opts.push_back((char*)"taucs.factor=false");
    opts.push_back(NULL);
    if (this->f_printLog.getValue())
	taucs_logfile((char*)"stdout");
    int rc = taucs_linsolve(&matrix_taucs, &factorization, 1, z.ptr(), r.ptr(), &(opts[0]), NULL);
    if (this->f_printLog.getValue())
	taucs_logfile((char*)"none");
    if (rc != TAUCS_SUCCESS)
    {
	const char* er = "";
	switch(rc)
	{
	case TAUCS_SUCCESS: er = "SUCCESS"; break;
	case TAUCS_ERROR  : er = "ERROR"; break;
	case TAUCS_ERROR_NOMEM: er = "NOMEM"; break;
	case TAUCS_ERROR_BADARGS: er = "BADARGS"; break;
	case TAUCS_ERROR_MAXDEPTH: er = "MAXDEPTH"; break;
	case TAUCS_ERROR_INDEFINITE: er = "INDEFINITE"; break;
	}
	serr << "TAUCS solve failed: " << er << sendl;
    }
}


SOFA_DECL_CLASS(SparseTAUCSSolver)

int SparseTAUCSSolverClass = core::RegisterObject("Linear system solver using the conjugate gradient iterative algorithm")
.add< SparseTAUCSSolver< CompressedRowSparseMatrix<double>,FullVector<double> > >(true)
.addAlias("TAUCSSolver")
;

} // namespace linearsolver

} // namespace component

} // namespace sofa