-
Notifications
You must be signed in to change notification settings - Fork 9
/
LaBandedDirectSolver.cpp
185 lines (167 loc) · 4.33 KB
/
LaBandedDirectSolver.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
181
182
183
184
185
#include "LaBandedDirectSolver.h"
namespace Daetk
{
using std::max;
using std::min;
using std::endl;
using std::cerr;
using std::cout;
void LaBandedDirectSolver::calculateCondition(DataCollector& d)
{
data = &d;
CALCULATE_CONDITION=true;
rwork = new real[3*neq];
iwork = new int[neq];
}
LaBandedDirectSolver::LaBandedDirectSolver():
PRINT_MATRICES(0),
CALCULATE_CONDITION(0),
x(this,0),
neq(0),
kl(0),
ku(0),
LEAD_DIM_STORAGE(0),
errorFlag(true),
arrayptr(0),
rwork(0),
pivotptr(0),
iwork(0),
BCM(0)
{
Tracer tr("LaBandedDirectSolver::LaBandedDirectSolver()");
}
void LaBandedDirectSolver::printMatrices(const char*filename)
{PRINT_MATRICES = true; matOut.open(filename);}
void LaBandedDirectSolver::attachMat(BandColMat& BCMin)
{
BCM = &BCMin;
neq = BCM->getNeq();
x.v_.newsize(neq);
kl = BCM->getLowerBandWidth();
ku = BCM->getUpperBandWidth();
LEAD_DIM_STORAGE= 2*kl+ku+1;
arrayptr = BCM->castToArray();
pivotptr = BCM->pivotPtr();
}
LaBandedDirectSolver::LaBandedDirectSolver(BandColMat& BCMin):LinearSolver(),
PRINT_MATRICES(0),
CALCULATE_CONDITION(0),
x(this,BCMin.getNeq()),
neq(BCMin.getNeq()),
kl(BCMin.getLowerBandWidth()),
ku(BCMin.getUpperBandWidth()),
LEAD_DIM_STORAGE(2*kl+ku+1),
arrayptr(0),
rwork(0),
iwork(0),
BCM(&BCMin)
{
Tracer tr("LaBandedDirectSolver::LaBandedDirectSolver(BandColMat& BCMin)");
arrayptr=BCMin.castToArray();
pivotptr=BCMin.pivotPtr();
}
LaBandedDirectSolver::~LaBandedDirectSolver()
{
Tracer tr("LaBandedDirectSolver::~LaBandedDirectSolver()");
delete [] rwork;
delete [] iwork;
}
bool LaBandedDirectSolver::prepare()
{
real anorm=0.0;
if (CALCULATE_CONDITION)
{
real sum=0;
for (int i=0;i<neq;i++)
{
for (int j=max(0,i-kl);j<min(neq-1,i+kl);j++)
sum+=fabs((*BCM)(i,j));
if (sum > anorm)
anorm = sum;
sum=0.0;
}
}
if (PRINT_MATRICES)
matOut<<*BCM<<endl;
/* dgbtrf(const int&,const int&,const int&,const int&,double*,const int&,
int*,int&);*/
#ifndef CRAYCC
#ifndef USE_SINGLE_PRECISION
F77NAME(dgbtrf)(neq,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,errorFlag);
#else
F77NAME(sgbtrf)(neq,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,errorFlag);
#endif
#else
#ifndef USE_SINGLE_PRECISION
F77NAME(DGBTRF)(neq,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,errorFlag);
#else
F77NAME(SGBTRF)(neq,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,errorFlag);
#endif
#endif
if (errorFlag != 0)
cerr<<"error in factor "<<errorFlag<<endl;
if (CALCULATE_CONDITION)
{
#ifndef CRAYCC
const char* nrm="1";
#else
char* one="1";
_fcd nrm = _cptofcd(one,1);
#endif
real rcond;
#ifndef CRAYCC
#ifndef USE_SINGLE_PRECISION
F77NAME(dgbcon)(nrm,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,&anorm,&rcond,rwork,iwork,errorFlag);
#else
F77NAME(sgbcon)(nrm,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,&anorm,&rcond,rwork,iwork,errorFlag);
#endif
#else
#ifndef USE_SINGLE_PRECISION
F77NAME(DGBCON)(nrm,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,&anorm,&rcond,rwork,iwork,errorFlag);
#else
F77NAME(SGBCON)(nrm,neq,kl,ku,arrayptr,LEAD_DIM_STORAGE,pivotptr,&anorm,&rcond,rwork,iwork,errorFlag);
#endif
#endif
rcond=1./rcond;
data->conditionNumber(rcond);
}
return errorFlag;
}
bool LaBandedDirectSolver::solve(const Vec& bIn,Vec& xIn)
{
int numberOfRhs(1);
#ifndef USE_BLAS
xIn=bIn;
#else
copy(bIn,xIn);
#endif
x.attachToTarget(xIn);
#ifndef CRAYCC
const char* n="N";
#else
char* N="N";
_fcd n = _cptofcd(N,1);
#endif
/* dgbtrs(const char*,const int&,const int&,const int&,const int&,
double*,const int&,int*,double*,const int&,int&);*/
#ifndef CRAYCC
#ifndef USE_SINGLE_PRECISION
F77NAME(dgbtrs)(n,neq,kl,ku,numberOfRhs,arrayptr,LEAD_DIM_STORAGE,pivotptr,
x.v_.castToArray(),neq,errorFlag);
#else
F77NAME(sgbtrs)(n,neq,kl,ku,numberOfRhs,arrayptr,LEAD_DIM_STORAGE,pivotptr,
x.v_.castToArray(),neq,errorFlag);
#endif
#else
#ifndef USE_SINGLE_PRECISION
F77NAME(DGBTRS)(n,neq,kl,ku,numberOfRhs,arrayptr,LEAD_DIM_STORAGE,pivotptr,
x.v_.castToArray(),neq,errorFlag);
#else
F77NAME(SGBTRS)(n,neq,kl,ku,numberOfRhs,arrayptr,LEAD_DIM_STORAGE,pivotptr,
x.v_.castToArray(),neq,errorFlag);
#endif
#endif
x.restoreToTarget();
return errorFlag;
}
}//Daetk