Debian Codegraph @

src/femlib/gmres.hpp

raw symbols SCIP
1 //*****************************************************************cxx . . $ `<file>/src/femlib/gmres.hpp`/
2 // Iterative template routine -- GMRES
3 //
4 // GMRES solves the unsymmetric linear system Ax = b using the
5 // Generalized Minimum Residual method
6 //
7 // GMRES follows the algorithm described on p. 20 of the
8 // SIAM Templates book.
9 //
10 // The return value indicates convergence within max_iter (input)
11 // iterations (0), or no convergence within max_iter iterations (1).
12 //
13 // Upon successful return, output arguments have the following values:
14 //
15 // x -- approximate solution to Ax = b
16 // max_iter -- the number of iterations performed before the
17 // tolerance was reached
18 // tol -- the residual after the final iteration
19 //
20 //*****************************************************************
21 // modif F. hecht to by compatible with RNM.hpp
22 // no dummy vector result
23 // M.solve(xx) => M*(xx)
24 // dot(u,v) => (u,v)
25 // norm(u) => sqrt( (u,u) )
26
27 #include <cmath>
28
29
30
31 template<class Real>
32 void GeneratePlaneRotationcxx . . $ GeneratePlaneRotation(a7200cdef8ed62e).(Real &dx, Real &dy, Real &cs, Real &sn)
33 {
34 if (dy ==cxx . . $ std/`operator==`(bf4c77cd91c4b201). 0.0) {
35 cs = 1.0;
36 sn = 0.0;
37 } else if (abs(dy) >cxx . . $ std/`operator>`(c2ad8cf295ccec45). abs(dx)) {
38 Real temp = dx /cxx . . $ `operator/`(42306a5d7abcb141). dy;
39 sn = 1.0 /cxx . . $ `operator/`(42306a5d7abcb141). sqrt( 1.0 +cxx . . $ operator+(1f62e43363bf5779). temp*cxx . . $ `operator*`(ca0d58f853b0940).temp );
40 cs = temp *cxx . . $ `operator*`(ca0d58f853b0940). sn;
41 } else {
42 Real temp = dy /cxx . . $ `operator/`(42306a5d7abcb141). dx;
43 cs = 1.0 /cxx . . $ `operator/`(42306a5d7abcb141). sqrt( 1.0 +cxx . . $ operator+(1f62e43363bf5779). temp*cxx . . $ `operator*`(ca0d58f853b0940).temp );
44 sn = temp *cxx . . $ `operator*`(ca0d58f853b0940). cs;
45 }
46 }
47
48
49 template<class Real>
50 void ApplyPlaneRotationcxx . . $ ApplyPlaneRotation(a7200cdef8ed62e).(Real &dx, Real &dy, Real &cs, Real &sn)
51 {
52 Real temp = cs *cxx . . $ `operator*`(ca0d58f853b0940). dx +cxx . . $ operator+(1f62e43363bf5779). sn *cxx . . $ `operator*`(ca0d58f853b0940). dy;
53 dy = -cxx . . $ operator-(4b6fd085471a9fb1).sn *cxx . . $ `operator*`(ca0d58f853b0940). dx +cxx . . $ operator+(1f62e43363bf5779). cs *cxx . . $ `operator*`(ca0d58f853b0940). dy;
54 dx = temp;
55 }
56
57 template < class Matrix, class Vector >
58 void
59 Updatecxx . . $ Update(ad8699d64272260f).(Vector &x, int k, Matrix &h, Vector &s, Vector v[])
60 {
61 Vector y(s);
62
63 // Backsolve:
64 for (int i = k; i >= 0; i--) {
65 y(i) /= h(i,i);
66 for (int j = i - 1; j >= 0; j--)
67 y(j) -= h(j,i) *cxx . . $ `operator*`(ca0d58f853b0940). y(i);
68 }
69
70 for (int j = 0; j <= k; j++)
71 x +=cxx . . $ `operator+=`(d02e6a613fcf7dc7). v[j] *cxx . . $ `operator*`(ca0d58f853b0940). y(j);
72 }
73
74
75 template < class Real >
76 Real
77 abscxx . . $ abs(ada6a8422704cf8a).(Real x)
78 {
79 return (x >cxx . . $ std/`operator>`(c2ad8cf295ccec45). 0 ? x : -cxx . . $ operator-(4b6fd085471a9fb1).x);
80 }
81
82
83 template < class Operator, class Vector, class Preconditioner,
84 class Matrix, class Real, class CStop >
85 int
86 GMREScxx . . $ GMRES(9507530564999e62).(const Operator &A, Vector &x, const Vector &b,
87 const Preconditioner &M, Matrix &H, int &m, int &max_iter,
88 Real &tol,long verbosity, CStop * Stop )
89 {
90 Real resid;
91 int i, j = 1, k;
92 Vector s(m+1), cs(m+1), sn(m+1), w,r,Ax;
93 r=M*cxx . . $ `operator*`(ca0d58f853b0940).b;
94 Real normb = sqrt((r,cxx . . $ `operator,`(5d54d1eb02c625aa).r));
95
96 Ax=A *cxx . . $ `operator*`(ca0d58f853b0940). x;
97 Ax=b-cxx . . $ operator-(4b6fd085471a9fb1).Ax;
98 r = M*cxx . . $ `operator*`(ca0d58f853b0940).(Ax);
99 Real beta = sqrt((r,cxx . . $ `operator,`(5d54d1eb02c625aa).r));
100
101 if ( abscxx . . $ abs(ada6a8422704cf8a).(normb) <cxx . . $ std/`operator<`(c2ad8cf295ccec45). 1.e-30)
102 normb = 1;
103 if( tol<cxx . . $ std/`operator<`(c2ad8cf295ccec45).0) tol = -cxx . . $ operator-(4b6fd085471a9fb1).tol/cxx . . $ `operator/`(42306a5d7abcb141). normb; // tol < 0 => tol abs..
104 if ((resid = beta /cxx . . $ `operator/`(42306a5d7abcb141). normb) <=cxx . . $ std/`operator<=`(c2ad8cf295ccec45). tol) {
105 tol = resid*cxx . . $ `operator*`(ca0d58f853b0940).normb;
106 max_iter = 0;
107 return 0;
108 }
109
110 Vector *v = new Vector[m+1];
111
112 while (j <= max_iter) {
113 v[0] = r /cxx . . $ `operator/`(42306a5d7abcb141). beta;
114 s = 0.0;
115 s(0) = beta;
116
117 for (i = 0; i < m && j <= max_iter; i++, j++) {
118 w = M*cxx . . $ `operator*`(ca0d58f853b0940).(Ax=A *cxx . . $ `operator*`(ca0d58f853b0940). v[i]);
119 for (k = 0; k <= i; k++) {
120 H(k, i) = (w,cxx . . $ `operator,`(5d54d1eb02c625aa). v[k]);
121 w -= H(k, i) *cxx . . $ `operator*`(ca0d58f853b0940). v[k];
122 }
123 H(i+1, i) = sqrt((w,cxx . . $ `operator,`(5d54d1eb02c625aa).w));
124 v[i+1] = w /cxx . . $ `operator/`(42306a5d7abcb141). H(i+1, i) ;
125
126 for (k = 0; k < i; k++)
127 ApplyPlaneRotationcxx . . $ ApplyPlaneRotation(a7200cdef8ed62e).(H(k,i), H(k+1,i), cs(k), sn(k));
128
129 GeneratePlaneRotationcxx . . $ GeneratePlaneRotation(a7200cdef8ed62e).(H(i,i), H(i+1,i), cs(i), sn(i));
130 ApplyPlaneRotationcxx . . $ ApplyPlaneRotation(a7200cdef8ed62e).(H(i,i), H(i+1,i), cs(i), sn(i));
131 ApplyPlaneRotationcxx . . $ ApplyPlaneRotation(a7200cdef8ed62e).(s(i), s(i+1), cs(i), sn(i));
132 if(verbosity>5 || (verbosity>2 && j%100==0) )
133 coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "GMRES: " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). j <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). abscxx . . $ abs(ada6a8422704cf8a).(s(i+1)) <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " "
134 <<cxx . . $ `operator<<`(fc711bdf08d71f4f). abscxx . . $ abs(ada6a8422704cf8a).(s(i+1)) /cxx . . $ `operator/`(42306a5d7abcb141). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " < " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). tol <<cxx . . $ `operator<<`(fc711bdf08d71f4f). endlcxx . . $ endl(345bf97a19cc83a9).;
135 bool stop = Stop && Stop->Stop(i,x,r);// bof ????
136 if (((resid = abscxx . . $ abs(ada6a8422704cf8a).(s(i+1)) /cxx . . $ `operator/`(42306a5d7abcb141). normb) <cxx . . $ std/`operator<`(c2ad8cf295ccec45). tol )|| stop ) {
137 if(verbosity)
138 coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "GMRES converges: " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). j <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). abscxx . . $ abs(ada6a8422704cf8a).(s(i+1)) <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " "
139 <<cxx . . $ `operator<<`(fc711bdf08d71f4f). abscxx . . $ abs(ada6a8422704cf8a).(s(i+1)) /cxx . . $ `operator/`(42306a5d7abcb141). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " < " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). tol <<cxx . . $ `operator<<`(fc711bdf08d71f4f). endlcxx . . $ endl(345bf97a19cc83a9).;
140
141 Updatecxx . . $ Update(ad8699d64272260f).(x, i, H, s, v);
142 tol = resid;
143 max_iter = j;
144 delete [] v;
145 return 0;
146 }
147 }
148 Updatecxx . . $ Update(ad8699d64272260f).(x, i - 1, H, s, v);// chanhe FH oct 2010 m -1 -> i -1 (bug max_iter < m)
149 Ax = A*cxx . . $ `operator*`(ca0d58f853b0940).x;
150 Ax = b-cxx . . $ operator-(4b6fd085471a9fb1).Ax;
151
152 r = M*cxx . . $ `operator*`(ca0d58f853b0940).(Ax);
153 beta = sqrt((r,cxx . . $ `operator,`(5d54d1eb02c625aa).r));
154 if(verbosity>4)
155 coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "GMRES: restart" <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). j <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). beta <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " "
156 <<cxx . . $ `operator<<`(fc711bdf08d71f4f). beta /cxx . . $ `operator/`(42306a5d7abcb141). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). " < " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). tol <<cxx . . $ `operator<<`(fc711bdf08d71f4f). endlcxx . . $ endl(345bf97a19cc83a9).;
157 if ((resid = beta /cxx . . $ `operator/`(42306a5d7abcb141). normb) <cxx . . $ std/`operator<`(c2ad8cf295ccec45). tol) {
158 tol = resid*cxx . . $ `operator*`(ca0d58f853b0940).normb;
159 max_iter = j;
160 delete [] v;
161 return 0;
162 }
163 }
164
165 if(verbosity)
166 coutcxx . . $ cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "WARNING: GMRES do not converges: " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). j <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7)."/" <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). max_iter <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ", resid = " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). resid
167 <<cxx . . $ `operator<<`(fc711bdf08d71f4f). ", tol= " <<cxx . . $ `operator<<`(fc711bdf08d71f4f). tol <<cxx . . $ `operator<<`(fc711bdf08d71f4f). ", normb "<<cxx . . $ `operator<<`(fc711bdf08d71f4f). normb <<cxx . . $ `operator<<`(fc711bdf08d71f4f). endlcxx . . $ endl(345bf97a19cc83a9).;
168 tol = resid*cxx . . $ `operator*`(ca0d58f853b0940).normb;// erreor absolue
169 delete [] v;
170
171 return 1;
172 }
173
174 template < class Operator, class Vector, class Preconditioner,
175 class Matrix, class Real >
176 int
177 GMREScxx . . $ GMRES(c577987398dfed32).(const Operator &A, Vector &x, const Vector &b,
178 const Preconditioner &M, Matrix &H, int &m, int &max_iter,
179 Real &tol,long verbosity)
180 {
181 return
182 GMREScxx . . $ GMRES(c577987398dfed32).(A,x,b,M,H,m,max_iter,tol,verbosity,(StopGCcxx . . $ StopGC#<Real> *)0); }
183
184