-
Notifications
You must be signed in to change notification settings - Fork 298
/
UmfPack.cpp
180 lines (142 loc) · 4.41 KB
/
UmfPack.cpp
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
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#include <Solver/UmfPack/UmfPack.h>
#include <Core/Math/ILapack.h>
#ifdef USE_UMFPACK
#include "umfpack.h"
#include <Core/Utils/numeric/bindings/umfpack/umfpack.hpp>
#include <Core/Utils/numeric/bindings/ublas/vector.hpp>
#include <Core/Utils/numeric/bindings/ublas.hpp>
#include <boost/numeric/ublas/io.hpp>
namespace umf = boost::numeric::bindings::umfpack;
#endif
UmfPack::UmfPack(ILinSolverSettings* settings,shared_ptr<ILinearAlgLoop> algLoop)
:AlgLoopSolverDefaultImplementation()
,_iterationStatus(CONTINUE),
_umfpackSettings(settings),
_algLoop(algLoop),
_rhs(NULL),
_x(NULL),
_firstuse(true),
_jacd(NULL)
{
if (_algLoop)
{
AlgLoopSolverDefaultImplementation::initialize(_algLoop->getDimZeroFunc(),_algLoop->getDimReal());
}
else
{
throw ModelicaSimulationError(ALGLOOP_SOLVER, "solve for single instance is not supported");
}
}
UmfPack::~UmfPack()
{
if(_jacd) delete [] _jacd;
if(_rhs) delete [] _rhs;
if(_x) delete [] _x;
}
void UmfPack::initialize()
{
#ifdef USE_UMFPACK
_firstuse=false;
if(_algLoop)
_algLoop->initialize();
else
throw ModelicaSimulationError(ALGLOOP_SOLVER, "algloop system is not initialized");
_dimSys = _algLoop->getDimReal();
if(_algLoop->queryDensity()<1. &&_umfpackSettings->getUseSparseFormat() )
{
_algLoop->setUseSparseFormat(true);
}
else
{
_jacd= new double[_algLoop->getDimReal()*_algLoop->getDimReal()];
_algLoop->setUseSparseFormat(false);
}
_rhs = new double[_algLoop->getDimReal()];
_x = new double[_algLoop->getDimReal()];
#endif
}
bool* UmfPack::getConditionsWorkArray()
{
return AlgLoopSolverDefaultImplementation::getConditionsWorkArray();
}
bool* UmfPack::getConditions2WorkArray()
{
return AlgLoopSolverDefaultImplementation::getConditions2WorkArray();
}
double* UmfPack::getVariableWorkArray()
{
return AlgLoopSolverDefaultImplementation::getVariableWorkArray();
}
void UmfPack::solve(shared_ptr<ILinearAlgLoop> algLoop,bool first_solve)
{
throw ModelicaSimulationError(ALGLOOP_SOLVER, "solve for single instance is not supported");
}
void UmfPack::solve()
{
#ifdef USE_UMFPACK
if(!_algLoop)
throw ModelicaSimulationError(ALGLOOP_SOLVER, "algloop system is not initialized");
if(_firstuse)
initialize();
if(!_algLoop->getUseSparseFormat())
{
long int dimRHS = 1; // Dimension of right hand side of linear system (=b)
long int dimSys = _algLoop->getDimReal();
long int irtrn = 0; // Retrun-flag of Fortran code _algLoop->getReal(_y);
long int * _helpArray = new long int[_algLoop->getDimReal()];
_algLoop->evaluate();
_algLoop->getb(_rhs);
const matrix_t& A = _algLoop->getAMatrix();
const double* jacd = A.data().begin();
memcpy(_jacd, jacd, dimSys*dimSys*sizeof(double));
dgesv_(&dimSys,&dimRHS,_jacd,&dimSys,_helpArray,_rhs,&dimSys,&irtrn);
std::memcpy(_x,_rhs,dimSys*sizeof(double));
_algLoop->setReal(_x);
delete [] _helpArray;
}
else
{
int status;
// get the default control parameters
umf::control_type<> Control;
// change the default print level
Control [UMFPACK_PRL] = 6;
_algLoop->evaluate();
_algLoop->getb(_rhs);
long int dimSys = _algLoop->getDimReal();
const sparsematrix_t& A = _algLoop->getSparseAMatrix();
adaptor_t rhs_adaptor(dimSys,_rhs);
shared_vector_t b(dimSys,rhs_adaptor);
adaptor_t x_adaptor(dimSys,_x);
shared_vector_t x(dimSys,x_adaptor);
umf::symbolic_type<double> Symbolic;
umf::numeric_type<double> Numeric;
status = umf::symbolic(A, Symbolic);
if(status<0)
//umf::report_status (Control, status)
throw ModelicaSimulationError(ALGLOOP_SOLVER,"Error in umfpack symbolic function");
status = umf::numeric (A, Symbolic, Numeric);
if(status<0)
throw ModelicaSimulationError(ALGLOOP_SOLVER,"Error in umfpack numeric function");
status = umf::solve (A, x, b, Numeric);
if(status<0)
throw ModelicaSimulationError(ALGLOOP_SOLVER,"Error in umfpack solve function");
_algLoop->setReal(_x);
}
#endif
}
ILinearAlgLoopSolver::ITERATIONSTATUS UmfPack::getIterationStatus()
{
return _iterationStatus;
}
void UmfPack::stepCompleted(double time)
{
}
void UmfPack::restoreOldValues()
{
}
void UmfPack::restoreNewValues()
{
}