-
Notifications
You must be signed in to change notification settings - Fork 3
/
NewtonKrylov.h
241 lines (188 loc) · 6.4 KB
/
NewtonKrylov.h
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#pragma once
template<typename VectorType>
class NewtonKrylov
{
public:
NewtonKrylov()
: q(K)
, H(K, K-1)
{
H.setZero();
}
// using the GMRES routine, perform Newton-Raphson iteration
// x : an initial guess, also the result when finished
void Run(VectorType& x)
{
MakeCleanDir("ICs");
VectorType dx; // update, solve into here to save reallocating memory
stratifloat bestResidual;
VectorType xPrevious;
VectorType linStartPrevious;
VectorType linEndPrevious;
VectorType rhsPrevious;
stratifloat Delta = 100;
int step = 0;
stratifloat targetResidual = 1e-7;
int worsefor = 0;
while(true)
{
step++;
x.SaveToFile("ICs/"+std::to_string(step));
x.PlotAll("ICs/"+std::to_string(step));
// first nonlinearly evolve current state
VectorType rhs = EvalFunction(x);
linearAboutStart = x;
linearAboutEnd = rhs;
stratifloat residual = rhs.Norm();
std::cout << "NEWTON STEP " << step << ", RESIDUAL: " << residual << std::endl;
if (true)//residual < bestResidual || step == 1)
{
if (residual < targetResidual)
{
return;
}
bestResidual = residual;
xPrevious = x;
linStartPrevious = linearAboutStart;
linEndPrevious = linearAboutEnd;
rhsPrevious = rhs;
worsefor = 0;
}
else
{
worsefor++;
}
if (false)//worsefor > 0)
{
// not good enough, reduce trust region size and retry
Delta /= 2;
std::cout << "Delta: " << Delta << std::endl;
// reset snapshots to previous
// the time spent on this is small compared to GMRES time
x = xPrevious;
linearAboutStart = linStartPrevious;
linearAboutEnd = linEndPrevious;
rhs = rhsPrevious;
// if (worsefor == 1)
// {
// vectorsToReuse = 0;
// H.setZero();
// }
}
else
{
// will have to construct Krylov space from scratch
vectorsToReuse = 0;
H.setZero();
}
// solve matrix system
GMRES(rhs, dx, 0.01, Delta);
// update
x += dx;
// do this a few times, as relative precision can mean it gets better
EnforceConstraints(x);
EnforceConstraints(x);
EnforceConstraints(x);
}
}
virtual void EnforceConstraints(VectorType& at) {}
protected:
virtual VectorType EvalFunction(const VectorType& at) = 0;
stratifloat T = 11; // time interval for integration
private:
VectorType linearAboutStart;
VectorType linearAboutEnd;
VectorType EvalDerivative(const VectorType& at)
{
const stratifloat eps = 1e-7*linearAboutStart.Norm()/at.Norm();
VectorType temp = linearAboutStart;
temp.MulAdd(eps, at);
temp = EvalFunction(temp);
temp -= linearAboutEnd;
temp *= 1/eps;
return temp;
}
// solves A x = G-x0 for x
// where A = I-G_x
// GMRES is a Krylov-subspace method, hence Newton-Krylov
// Delta is a maximum size for x in the least squares solution
void GMRES(const VectorType& rhs, VectorType& x, stratifloat epsilon, stratifloat Delta=0)
{
VectorX y; // result in new basis
q[0] = rhs;
q[0].EnforceBCs();
stratifloat beta = q[0].Norm();
std::cout << beta << std::endl;
q[0] *= 1/beta;
K = q.size();
for (int k=1; k<K; k++)
{
if (k > vectorsToReuse) // if we need to construct this basis vector
{
vectorsToReuse = k;
// Arnoldi Algorithm
// find orthogonal basis q1,...,qn
// from x, A x, A^2 x, ...
// q_k = A q_k-1
q[k] = EvalDerivative(q[k-1]);
q[k] *= -1.0; // factor of -1 for Newton iteration
// remove component in direction of preceding vectors
for (int j=0; j<k; j++)
{
H(j,k-1) = q[j].Dot(q[k]);
q[k].MulAdd(-H(j,k-1), q[j]);
}
// normalise
H(k,k-1) = q[k].Norm();
q[k] *= 1/H(k,k-1);
// enforce BCs
q[k].EnforceBCs();
}
// Construct least squares problem in this basis
VectorX Beta(k+1);
Beta.setZero();
Beta[0] = beta;
MatrixX subH = H.block(0,0,k+1,k);
// Now we solve Hy = Beta
// follows notation of Chandler & Kerswell 2013
// first H = UDV*
JacobiSVD<MatrixX> svd(subH, ComputeFullU | ComputeFullV);
MatrixX U = svd.matrixU();
MatrixX V = svd.matrixV();
ArrayX d = svd.singularValues();
// solve problem in space of singular vectors
VectorX p = U.transpose() * Beta; // p = U* Beta
VectorX z = p.array()/d; // D z = p
// enforce trust region
stratifloat mu = 0;
while (z.norm() > Delta)
{
mu += 0.00001;
for (int j=0; j<z.size(); j++)
{
z(j) = p(j)*d(j)/(d(j)*d(j)+mu);
}
}
std::cout << "For |z|<Delta, mu=" << mu << std::endl;
// z = V* y
y = V*z;
stratifloat residual = (subH*y - Beta).norm()/beta;
std::cout << "GMRES STEP " << k << ", RESIDUAL: " << residual << std::endl;
if (residual < epsilon)
{
K = k+1;
break;
}
}
// Now compute the solution using the basis vectors
x.Zero();
for (int k=0; k<K-1; k++)
{
x.MulAdd(y[k], q[k]);
}
}
int K = 2048; // max iterations
int vectorsToReuse = 0;
std::vector<VectorType> q;
MatrixX H; // upper Hessenberg matrix
};