-
Notifications
You must be signed in to change notification settings - Fork 297
/
SparseLDLSolver.inl
195 lines (159 loc) · 7.78 KB
/
SparseLDLSolver.inl
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#include <sofa/component/linearsolver/direct/SparseLDLSolver.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/helper/system/thread/CTime.h>
#include <sofa/core/objectmodel/BaseContext.h>
#include <sofa/core/behavior/LinearSolver.h>
#include <cmath>
#include <fstream>
#include <iomanip> // std::setprecision
#include <string>
namespace sofa::component::linearsolver::direct
{
template<class TMatrix, class TVector, class TThreadManager>
SparseLDLSolver<TMatrix,TVector,TThreadManager>::SparseLDLSolver()
: numStep(0){}
template <class TMatrix, class TVector, class TThreadManager>
void SparseLDLSolver<TMatrix, TVector, TThreadManager>::parse(sofa::core::objectmodel::BaseObjectDescription* arg)
{
Inherit1::parse(arg);
if (!arg->getAttribute("template"))
{
std::string header = this->getClassName();
if (const std::string& name = this->getName(); !name.empty())
{
header.append("(" + name + ")");
}
static const char* blocksType =
sofa::linearalgebra::CompressedRowSparseMatrix<sofa::type::Mat<3, 3, SReal> >::Name();
msg_advice(header) << "Template is empty\n"
<< "By default " << this->getClassName() << " uses blocks with a single scalar (to handle all cases of simulations).\n"
<< "If you are using only 3D DOFs, you may consider using blocks of Matrix3 to speedup the calculations.\n"
<< "If it is the case, add template=\"" << blocksType << "\" to this object in your scene\n"
<< "Otherwise, if you want to disable this message, add " << "template=\"" << this->getTemplateName() << "\" " << ".";
}
if (arg->getAttribute("savingMatrixToFile"))
{
msg_warning() << "It is no longer possible to export the linear system matrix from within " << this->getClassName() << ". Instead, use the component GlobalSystemMatrixExporter (from the SofaMatrix plugin).";
}
}
template<class TMatrix, class TVector, class TThreadManager>
void SparseLDLSolver<TMatrix,TVector,TThreadManager>::solve (Matrix& M, Vector& z, Vector& r)
{
sofa::helper::ScopedAdvancedTimer solveTimer("solve");
Inherit::solve_cpu(&z[0],&r[0],(InvertData *) this->getMatrixInvertData(&M));
}
template <class TMatrix, class TVector, class TThreadManager>
bool SparseLDLSolver<TMatrix, TVector, TThreadManager>::factorize(
Matrix& M, InvertData * invertData)
{
Mfiltered.copyNonZeros(M);
Mfiltered.compress();
int n = M.colSize();
int * M_colptr = (int *) &Mfiltered.getRowBegin()[0];
int * M_rowind = (int *) &Mfiltered.getColsIndex()[0];
Real * M_values = (Real *) &Mfiltered.getColsValue()[0];
if(M_colptr==nullptr || M_rowind==nullptr || M_values==nullptr || Mfiltered.getRowBegin().size() < (size_t)n )
{
msg_warning() << "Invalid Linear System to solve. Please insure that there is enough constraints (not rank deficient)." ;
return true;
}
Inherit::factorize(n,M_colptr,M_rowind,M_values, invertData);
numStep++;
return false;
}
template<class TMatrix, class TVector, class TThreadManager>
void SparseLDLSolver<TMatrix,TVector,TThreadManager>::invert(Matrix& M)
{
factorize(M, (InvertData *) this->getMatrixInvertData(&M));
}
template <class TMatrix, class TVector, class TThreadManager>
bool SparseLDLSolver<TMatrix, TVector, TThreadManager>::doAddJMInvJtLocal(ResMatrixType* result, const JMatrixType* J, SReal fact, InvertData* data)
{
if (J->rowSize()==0) return true;
Jlocal2global.clear();
Jlocal2global.reserve(J->rowSize());
for (auto jit = J->begin(), jitend = J->end(); jit != jitend; ++jit) {
int l = jit->first;
Jlocal2global.push_back(l);
}
if (Jlocal2global.empty()) return true;
const unsigned int JlocalRowSize = (unsigned int)Jlocal2global.size();
JLinv.clear();
JLinv.resize(J->rowSize(), data->n);
JLinvDinv.resize(J->rowSize(), data->n);
unsigned int localRow = 0;
for (auto jit = J->begin(), jitend = J->end(); jit != jitend; ++jit, ++localRow) {
Real* line = JLinv[localRow];
for (auto it = jit->second.begin(), i2end = jit->second.end(); it != i2end; ++it) {
int col = data->invperm[it->first];
double val = it->second;
line[col] = val;
}
}
//Solve the lower triangular system
for (unsigned c = 0; c < JlocalRowSize; c++) {
Real* line = JLinv[c];
for (int j=0; j<data->n; j++) {
for (int p = data->LT_colptr[j] ; p<data->LT_colptr[j+1] ; p++) {
int col = data->LT_rowind[p];
double val = data->LT_values[p];
line[j] -= val * line[col];
}
}
}
//apply diagonal
for (unsigned j = 0; j < JlocalRowSize; j++) {
Real* lineD = JLinv[j];
Real* lineM = JLinvDinv[j];
for (unsigned i = 0; i < (unsigned)data->n; i++) {
lineM[i] = lineD[i] * data->invD[i];
}
}
for (unsigned j = 0; j < JlocalRowSize; j++) {
Real* lineJ = JLinvDinv[j];
int globalRowJ = Jlocal2global[j];
for (unsigned i = j; i < JlocalRowSize; i++) {
Real* lineI = JLinv[i];
int globalRowI = Jlocal2global[i];
double acc = 0.0;
for (unsigned k = 0; k < (unsigned)data->n; k++) {
acc += lineJ[k] * lineI[k];
}
acc *= fact;
result->add(globalRowJ, globalRowI, acc);
if (globalRowI != globalRowJ) result->add(globalRowI, globalRowJ, acc);
}
}
return true;
}
// Default implementation of Multiply the inverse of the system matrix by the transpose of the given matrix, and multiply the result with the given matrix J
template<class TMatrix, class TVector, class TThreadManager>
bool SparseLDLSolver<TMatrix,TVector,TThreadManager>::addJMInvJtLocal(TMatrix * M, ResMatrixType * result,const JMatrixType * J, SReal fact)
{
InvertData* data = (InvertData*)this->getMatrixInvertData(M);
return doAddJMInvJtLocal(result, J, fact, data);
}
} // namespace sofa::component::linearsolver::direct