-
Notifications
You must be signed in to change notification settings - Fork 9
/
DaeDefinition.cpp
188 lines (167 loc) · 3.91 KB
/
DaeDefinition.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
186
187
188
#include "DaeDefinition.h"
namespace Daetk
{
using std::cerr;
using std::endl;
bool DaeDefinition::yPrimeValue(const real& t,const Vec& y,Vec& yp)
{/*not required*/ return true;}
bool DaeDefinition::evaluateDaeJacobian(const real& t,const Vec& y,const Vec& yp,
const real& alpha)
{
std::cerr<<"You must override evaluateDaeJacobian to use an analytical jacobian"<<std::endl;
return true;
}
bool DaeDefinition::jacVec(const Vec& x, Vec& Jx)
{
std::cerr<<"You must override jacVec to use an analytical jacobian with an iterative method"<<std::endl;
return true;
}
void DaeDefinition::initializeFunction(const Vec& y, const Vec& yp, const Vec& f)
{
#ifndef USE_BLAS
yDaeDef = y;
ypDaeDef= yp;
Fcurrent= f;
#else
copy(y,yDaeDef);
copy(yp,ypDaeDef);
copy(f,Fcurrent);
#endif
updateF=false;
}
DaeDefinition::~DaeDefinition()
{
Tracer tr("DaeDefinition:~DaeDefinition()");
}
DaeDefinition::DaeDefinition(int dim,DataCollector* dataIn):
VectorFunction(dim,dim),
updateF(true),
updateJac(true),
alphaDaeDef(0.0),
tDaeDef(0.0),
yDaeDef(dim,12345),
ypDaeDef(dim,12345),
Fcurrent(dim,12345),
yLast(dim,12345),
ypLast(dim,12345),
Flast(dim,12345),
del(dim,12345),
betaDaeDef(dim,0.0),
data(dataIn),
//mwf added
computeOwnJacobianDelta(false)
{
Tracer tr("DaeDefinition::DaeDefinition(Data& dataIn)");
}
const Vec& DaeDefinition::argument()
{
return yDaeDef;
}
const Vec& DaeDefinition::value(bool& evalError)
{
evalError = false;
if (updateF)
{
data->functionEvaluation();
evalError = residual(tDaeDef,yDaeDef,ypDaeDef,Fcurrent);
if (!evalError)
updateF=false;
}
return Fcurrent;
}
void DaeDefinition::correctArgument(Vec& correction)
{
yLast = yDaeDef;
ypLast = ypDaeDef;
Flast = Fcurrent;
updateJac=true;
updateF=true;
#ifndef USE_BLAS
yDaeDef-=correction;
#else
axpy(-1.0,correction,yDaeDef);
#endif
#ifndef USE_BLAS
ypDaeDef-=alphaDaeDef*correction;
#else
axpy(-alphaDaeDef,correction,ypDaeDef);
#endif
}
void DaeDefinition::resetFunction()
{
updateF=true;
updateJac=true;
}
void DaeDefinition::unCorrect()
{
updateF = false;
yDaeDef = yLast;
ypDaeDef = ypLast;
Fcurrent = Flast;
}
bool DaeDefinition::evaluateAnalyticalJacobian()
{
return evaluateDaeJacobian(tDaeDef,yDaeDef,ypDaeDef,alphaDaeDef);
}
bool DaeDefinition::numericalJacVec(const Vec& v, Vec& Jv)
{
bool evalError=false;
real delFac=1.0e-5;
value(evalError);//sets Fcurrent
if (evalError)
return evalError;
else
{
// del = -delFac*v;
del = v;
scal(-delFac,del);
correctArgument(del);//sets Flast to Fcurrent -- remebmer correction is -del = delFac*v
value(evalError);
while (evalError)
{
cerr<<"cutting back on del in numerical jac vec"<<endl;
unCorrect();//sets Flast to Fcurrent
delFac*=0.1;
// del = -delFac*v;
del = v;
scal(-delFac,del);
correctArgument(del);
value(evalError);//sets Fcurrent
}
// Jv = (Fcurrent - Flast)/delFac;
Jv = Fcurrent;
axpy(-1.0,Flast,Jv);
scal(1.0/delFac,Jv);
unCorrect();
}
return evalError;
}
bool DaeDefinition::analyticalJacVec(const Vec& v, Vec& Jv)
{
bool evalError = false;
if (updateJac)
{
evalError = evaluateAnalyticalJacobian();
if (evalError)
return evalError;
else
updateJac=false;
}
jacVec(v,Jv);
return evalError;
}
//mwf added to allow other integrators that don't compute delta
void DaeDefinition::computeDeltaForJacobian()
{
if (computeOwnJacobianDelta)
{
deltaVF = 0.0;
for (int i=0;i<yDaeDef.ldim();i++)
{
//mwf this is what it should be
deltaVF[i] = SQRT_MACHINE_EPSILON*fabs(yDaeDef[i])+SQRT_MACHINE_EPSILON;
deltaVF[i]= yDaeDef[i] - (yDaeDef[i] - deltaVF[i]);
}
}
}
}//Daetk