Debian Codegraph @

src/Eigen/eigenvalue.cpp

raw symbols SCIP
1 /****************************************************************************/
2 /* This file is part of FreeFEM. */
3 /* */
4 /* FreeFEM is free software: you can redistribute it and/or modify */
5 /* it under the terms of the GNU Lesser General Public License as */
6 /* published by the Free Software Foundation, either version 3 of */
7 /* the License, or (at your option) any later version. */
8 /* */
9 /* FreeFEM is distributed in the hope that it will be useful, */
10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
12 /* GNU Lesser General Public License for more details. */
13 /* */
14 /* You should have received a copy of the GNU Lesser General Public License */
15 /* along with FreeFEM. If not, see <http://www.gnu.org/licenses/>. */
16 /****************************************************************************/
17 // SUMMARY : ...
18 // LICENSE : LGPLv3
19 // ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE
20 // AUTHORS : Frederic Hecht
21 // E-MAIL : [email protected]
22
23 #include <fstream>
24 #include <iostream>
25 #include <cassert>
26 #include <algorithm>
27 #include <complex>
28
29 using namespace std;
30 //#include "ff++.hpp"
31
32 #include "error.hpp"
33 #include "arpackff.hpp"
34 #include "AFunction.hpp"
35 #include "rgraph.hpp"
36 #include "RNM.hpp"
37
38 #include "HashMatrix.hpp"
39
40 #include "SparseLinearSolver.hpp"
41
42 #include "Mesh3dn.hpp"
43 #include "MeshPoint.hpp"
44 #include "lgfem.hpp"
45 #include "lgmesh3.hpp"
46 #include "lgsolver.hpp"
47 #include "problem.hpp"
48 //#include "ffstack.hpp"
49
50 extern Block *currentblock;
51
52 typedef double R;
53 static bool dddd = false;
54 template<class K>
55 void Show(int ido, KN_<K> w, const char *cmm) {
56 cout << cmm << ido << " max = " << w.max() << " - min = " << w.min() << " - sum = " << w.sum() << endl;
57 }
58
59 template<class R>
60 class FuncMat : public RNM_VirtualMatrix<R> {
61 public:
62 // typedef double K;
63 typedef KN<R> Kn;
64 typedef KN_<R> Kn_;
65
66 Stack stack;
67
68 mutable Kn x;
69 C_F0 c_x;
70 Kn *b;
71 Expression mat1, mat;
72 typedef typename RNM_VirtualMatrix<R>::plusAx plusAx;
73
74 FuncMat (int n, Stack stk, const OneOperator *op, const OneOperator *op1, Kn *bb=0)
75 : RNM_VirtualMatrix<R>(n),
76 stack(stk),
77 x(n), c_x(CPValue(x)), b(bb),
78 mat1(op1 ? CastTo<Kn_>(C_F0(op1->code(basicAC_F0_wa(c_x)), (aType)*op1)) : 0),
79 mat(op ? CastTo<Kn_>(C_F0(op->code(basicAC_F0_wa(c_x)), (aType)*op)) : 0)
80 {}
81
82 FuncMat () { if (mat1 != mat) delete mat; delete mat1; delete c_x.LeftValue(); }
83
84 void addMatMul (const Kn_ &xx, Kn_ &Ax) const {
85 // cout << " addMatMul " << xx.N() << " " << Ax.N() << " " << cas << endl;
86 ffassert(xx.N() == Ax.N() && mat);
87 x =xx;
88 Ax += GetAny<Kn_>((*mat)(stack));
89 if (b && &Ax != b) Ax += *b; // Ax - b => add b (not in cas of init. b c.a.d &Ax == b)
90 WhereStackOfPtr2Free(stack)->clean(); // add dec 2008
91 }
92
93 void Solve (KN_<R> &Ax, const KN_<R> & xx) const {
94 ffassert(xx.N() == Ax.N() && mat1);
95 x = xx;
96 Ax = GetAny<Kn_>((*mat1)(stack));
97 if (b && &Ax != b) Ax += *b; // Ax -b => add b (not in cas of init. b c.a.d &Ax == b)
98 WhereStackOfPtr2Free(stack)->clean(); // add dec 2008
99 }
100
101 plusAx operator * (const Kn &x) const { return plusAx(this,x); }
102 virtual bool ChecknbLine (int n) const { return true; }
103 virtual bool ChecknbColumn (int m) const { return true; }
104 bool WithSolver () const { return mat1; }
105 };
106
107 void ToWhich (Stack stack, char *which, Expression ew) {
108 if (!ew) strcpy(which, "LM");
109 else {
110 string *sw = GetAny<string*>((*ew)(stack));
111 if (sw && sw->length() == 2)
112 strcpy(which, sw->c_str());
113 int ok = 0;
114 if (strcmp(which, "LM") == 0) ok = 1;
115 else if (strcmp(which, "LA") == 0) ok = 1;
116 else if (strcmp(which, "SM") == 0) ok = 1;
117 else if (strcmp(which, "SA") == 0) ok = 1;
118 else if (strcmp(which, "SR") == 0) ok = 1;
119 else if (strcmp(which, "LR") == 0) ok = 1;
120 else if (strcmp(which, "BE") == 0) ok = 1;
121 ffassert(ok);
122 }
123 }
124
125 void CheckMode (int mode, bool A, bool A1, bool B, bool B1) {
126 if (mode == 1) { ffassert(A); }
127 if (mode == 2) { ffassert(A && B && B1 ); }
128 if (mode == 3) { ffassert(A1 && B); }
129 }
130
131 template<class K,class Mat>
132 void DoIdoAction (int ido, char bmat, int mode, KN_<K> &xx, KN_<K> &yy, KN_<K> &zz, KN_<K> &work, Mat &OP1, Mat &B) {
133 if ((mode == 3) && (bmat == 'G')) // shift-invert mode, generalized problem
134 switch (ido) {
135 case -1: //y <--- OP*x = inv[A-SIGMA*M]*M*x
136 if(dddd) Show(ido, xx, " <- (xx) ");
137 OP1.Solve(yy, work=B*xx);
138 if(dddd) Show(ido, yy, " -> (yy) ");
139 break;
140 case 1:// y <-- OP*x = inv[A-sigma*M]*z
141 if(dddd) Show(ido, zz, " <- (zz) ");
142 OP1.Solve(yy, zz);
143 if(dddd) Show(ido, yy, " -> (yy) ");
144 break;
145 case 2: // y <--- M*x
146 if(dddd) Show(ido, xx, " <- (xx) ");
147 yy = B*xx;
148 if(dddd) Show(ido, yy, " -> (yy) ");
149 break;
150 default:
151 ffassert(0);
152 }
153 else if ((mode == 2) && (bmat == 'G')) { // shift-invert mode, generalized problem
154 switch (ido) {
155 case -1: // y <--- OP*x = inv[M]*A*x
156 case 1:
157 if (mode == 1) // M = Id //TODO there is a non-sens here: mode == 2 && mode == 1
158 yy = OP1*xx;
159 else
160 B.Solve(yy, work=OP1*xx);
161 break;
162 case 2: // y <--- M*x. // not use mode = 1
163 yy = B*xx;
164 break;
165 default :
166 ffassert(0);
167 }
168 }
169 else if ((mode == 1) && (bmat == 'I')) // direct mode, simple problem
170 switch (ido) {
171 case 1:
172 B.Solve(yy, work=OP1*xx);
173 break;
174 default :
175 ffassert(0);
176 }
177 else if ((mode == 3) && (bmat == 'I')) // shift-invert mode, simple problem
178 switch (ido) {
179 case -1:
180 case 1:
181 OP1.Solve(yy, work=B*xx);
182 break;
183 default :
184 ffassert(0);
185 }
186 }
187
188 /*void DoIdoAction(int ido,int mode,KN_<K> &xx,KN_<K> &yy,KN_<K> &zz,KN_<K> &work,Mat &A,Mat &B)
189 {
190 if(mode>2) // inverse mode
191 switch (ido) {
192 case -1: //y <--- OP*x = inv[A-SIGMA*M]*M*x
193 if(dddd) Show(ido,xx," <- (xx) ");
194 A.Solve(yy,work=B*xx);
195 if(dddd) Show(ido,yy," -> (yy) ");
196 break;
197 case 1:// y <-- OP*x = inv[A-sigma*M]*z
198 if(dddd) Show(ido,zz," <- (zz) ");
199 A.Solve(yy,zz);
200 if(dddd) Show(ido,yy," -> (yy) ");
201 break;
202 case 2: // y <--- M*x
203 if(dddd) Show(ido,xx," <- (xx) ");
204 yy = B*xx;
205 if(dddd) Show(ido,yy," -> (yy) ");
206 break;
207 default :
208 ffassert(0);
209 }
210 else // direct mode
211 switch (ido)
212 {
213 case -1: // y <--- OP*x = inv[M]*A*x
214 case 1:
215 if(mode== 1) // M = Id
216 yy=A*xx;
217 else
218 B.Solve(yy,work=A*xx);
219 break;
220 case 2: // y <--- M*x. // not use mode = 1
221 yy = B*xx;
222 break;
223 default :
224 ffassert(0);
225 }
226 }*/
227
228 template<class K>
229 const OneOperator *ToCode(Expression e) {
230 const OneOperator *code = 0;
231 if (e != 0) {
232 const Polymorphic *op = dynamic_cast<const Polymorphic*>(e);
233 ffassert(op);
234 code = op->Find("(", ArrayOfaType(atype<KN<K>* >(), false));
235 if(code == 0)
236 {
237 cerr << " Erreur we are in Eigen "<< typeid(K).name() << " "<< endl;
238 cout << " bad type of function : Maybe we need to use complexEigenValue or EigenValue" << endl;
239 ffassert(code);
240 }
241 }
242 return code;
243 }
244
245 class EigenValue : public OneOperator {
246 public:
247 typedef R K;
248 typedef KN<K> Kn;
249 typedef KN_<K> Kn_;
250 const int cas;
251 class E_EV : public E_F0mps {
252 public:
253 const int cas;
254
255 static basicAC_F0::name_and_type name_param[];
256 static const int n_name_param = 18;
257 Expression nargs[n_name_param];
258 Expression expOP1, expB, expn;
259 const OneOperator *codeOP1, *codeB, *codeOP, *codeB1;
260 template<class T>
261 T arg (int i, Stack stack, const T &a) const { return nargs[i] ? GetAny<T>((*nargs[i])(stack)) : a; }
262 E_EV (const basicAC_F0 &args, int cc) :
263 cas(cc), expOP1(0), expB(0), expn(0), codeOP1(0), codeB(0), codeOP(0), codeB1(0) {
264 // OP1 = (A-sigma*B)
265 // int nbj= args.size()-1;
266 args.SetNameParam(n_name_param, name_param, nargs);
267 if (cas == 1 || cas == 2) {
268 expn = to< long>(args[0]);
269 const Polymorphic *op = dynamic_cast<const Polymorphic*>(args[1].LeftValue());
270 ffassert(op);
271 codeOP1 = op->Find("(", ArrayOfaType(atype<Kn* >(), false));
272 ffassert(codeOP1);
273 if (cas == 2) {
274 const Polymorphic *op = dynamic_cast<const Polymorphic*>(args[2].LeftValue());
275 ffassert(op);
276 codeB = op->Find("(", ArrayOfaType(atype<Kn* >(), false));
277 ffassert(codeB);
278 }
279 } else if (cas == 3) {
280 expn = to< long>(args[0]);
281 } else {
282 expOP1 = to<Matrice_Creuse<K> *>(args[0]);
283 expB = to<Matrice_Creuse<K> *>(args[1]);
284 }
285 ffassert((codeOP1 == 0) || (nargs[15] == 0)); // Double def
286 ffassert((codeB == 0) || (nargs[14] == 0)); // Double def
287 codeOP = ToCode<double>(nargs[13]);
288 if (!codeB) codeB = ToCode<double>(nargs[14]);
289 if (!codeOP1) codeOP1 = ToCode<double>(nargs[15]);
290 codeB1 = ToCode<double>(nargs[16]);
291 }
292
293 AnyType operator () (Stack stack) const;
294 operator aType () const { return atype<long>(); }
295 };
296
297 E_F0 * code (const basicAC_F0 & args) const { return new E_EV(args,cas); }
298
299 EigenValue () :
300 OneOperator(atype<long>(),
301 atype<Matrice_Creuse<K> *>(),
302 atype<Matrice_Creuse<K> *>()),
303 cas(0) {}
304
305 EigenValue (int, int) :
306 OneOperator(atype<long>(), atype<long>(),
307 atype<Polymorphic *>(),
308 atype<Polymorphic *>()),
309 cas(2) {}
310
311 EigenValue (int) :
312 OneOperator(atype<long>(), atype<long>(),
313 atype<Polymorphic *>()),
314 cas(1) {}
315
316 EigenValue(int, int, int) : // All data are given through A=, B= A1=, B1=
317 OneOperator(atype<long>(), atype<long>()),
318 cas(3) {}
319 };
320
321 class EigenValueC : public OneOperator {
322 public:
323 typedef Complex K;
324 typedef double R;
325 typedef KN<K> Kn;
326 typedef KN_<K> Kn_;
327 const int cas;
328 class E_EV : public E_F0mps {
329 public:
330 const int cas;
331
332 static basicAC_F0::name_and_type name_param[];
333 static const int n_name_param = 16;
334 Expression nargs[n_name_param];
335 Expression expOP1, expB, expn;
336 const OneOperator *codeOP1, *codeB, *codeOP, *codeB1;
337 template<class T>
338 T arg (int i, Stack stack, const T &a) const { return nargs[i] ? GetAny<T>((*nargs[i])(stack)) : a; }
339 E_EV (const basicAC_F0 &args, int cc) :
340 cas(cc), expOP1(0), expB(0), expn(0), codeOP1(0), codeB(0), codeOP(0), codeB1(0) {
341 // OP1 = (A-sigma*B)
342 // int nbj = args.size()-1;
343 args.SetNameParam(n_name_param, name_param, nargs);
344 if (cas == 1 || cas == 2) {
345 expn = to<long>(args[0]);
346 const Polymorphic *op = dynamic_cast<const Polymorphic*>(args[1].LeftValue());
347 ffassert(op);
348 codeOP1 = op->Find("(", ArrayOfaType(atype<Kn* >(), false));
349 ffassert(codeOP1);
350 if (cas == 2) {
351 const Polymorphic *op = dynamic_cast<const Polymorphic*>(args[2].LeftValue());
352 ffassert(op);
353 codeB = op->Find("(", ArrayOfaType(atype<Kn* >(), false));
354 ffassert(codeB);
355 }
356 } else if (cas == 3) {
357 expn = to<long>(args[0]);
358 } else {
359 expOP1 = to<Matrice_Creuse<K> *>(args[0]);
360 expB = to<Matrice_Creuse<K> *>(args[1]);
361 }
362 ffassert((codeOP1 == 0) || (nargs[13] == 0)); // Double def
363 ffassert((codeB == 0) || (nargs[12] == 0)); // Double def
364 codeOP = ToCode<K>(nargs[11]);
365 if (!codeB) codeB = ToCode<K>(nargs[12]);
366 if (!codeOP1) codeOP1 = ToCode<K>(nargs[13]);
367 codeB1 = ToCode<K>(nargs[14]);
368 }
369
370 AnyType operator () (Stack stack) const;
371 operator aType () const { return atype<long>(); }
372 };
373
374 E_F0 *code (const basicAC_F0 &args) const { return new E_EV(args,cas); }
375
376 EigenValueC () :
377 OneOperator(atype<long>(),
378 atype<Matrice_Creuse<K> *>(),
379 atype<Matrice_Creuse<K> *>()),
380 cas(0) {}
381
382 EigenValueC (int, int ) :
383 OneOperator(atype<long>(), atype<long>(),
384 atype<Polymorphic *>(),
385 atype<Polymorphic *>()),
386 cas(2) {}
387
388 EigenValueC (int) :
389 OneOperator(atype<long>(), atype<long>(),
390 atype<Polymorphic *>()),
391 cas(1) {}
392
393 EigenValueC (int, int, int) : // All data are given through A=, B= A1=, B1=
394 OneOperator(atype<long>(), atype<long>()),
395 cas(3) {}
396 };
397
398
399
400 basicAC_F0::name_and_type EigenValue::E_EV::name_param[] = {
401 {"tol", &typeid(double)},
402 {"nev", &typeid(long)},
403 {"sym", &typeid(bool)},
404 {"sigma", &typeid(double)},
405 {"value", &typeid(KN<double> *)},
406 {"vector", &typeid(FEbaseArrayKn<double> *)}, // pferarray
407 {"ncv", &typeid(long)}, // the number of Arnoldi vectors generated
408 {"maxit", &typeid(long)}, // the maximum number of Arnoldi iterations
409 {"ivalue", &typeid(KN<double> *)},
410 {"rawvector", &typeid(KNM<double> *)},
411 {"resid", &typeid(KN<double> *)},
412 {"driver", &typeid(long)}, // 11th
413 {"which", &typeid(string*)}, // 12th
414 {"A", &typeid(Polymorphic*)},
415 {"B", &typeid(Polymorphic*)},
416 {"A1", &typeid(Polymorphic*)},
417 {"B1", &typeid(Polymorphic*)},
418 {"mode", &typeid(long)} // 17th
419 };
420
421 basicAC_F0::name_and_type EigenValueC::E_EV::name_param[] = {
422 {"tol", &typeid(double)},
423 {"nev", &typeid(long)},
424 {"sigma", &typeid(Complex)},
425 {"value", &typeid(KN<Complex> *)},
426 {"vector", &typeid(FEbaseArrayKn<Complex> *)}, // pfecarray
427 {"ncv", &typeid(long)}, // the number of Arnoldi vectors generated
428 {"maxit", &typeid(long)}, // the maximum number of Arnoldi iterations
429 {"rawvector", &typeid(KNM<Complex> *)},
430 {"resid", &typeid(KN<Complex> *)},
431 {"driver", &typeid(long)},// 9th
432 {"which", &typeid(string*)}, // 10th
433 {"A", &typeid(Polymorphic*)},
434 {"B", &typeid(Polymorphic*)},
435 {"A1", &typeid(Polymorphic*)},
436 {"B1", &typeid(Polymorphic*)},
437 {"mode", &typeid(long)} // 15th
438 };
439
440 AnyType EigenValue::E_EV::operator () (Stack stack) const {
441 dddd = (verbosity >= 200);
442 double tol = 1e-6;
443 long nconv = 0;
444 long nbev = 1;
445 bool sym = false;
446 long ncv = 0; // the number of Arnoldi vectors generated
447 long maxit = 0; // the maximum number of Arnoldi iterations
448 double sigma = 0;
449 int driver = 4; //
450 int mymode = -1; // unset
451 KN<double> *evalue = 0;
452 KN<double> *evaluei = 0;
453 KN<double> *resid = 0;
454 KNM<double> *rawvector = 0;
455 //double ws, vs; // for debugging FH ++++++++++
456 // pferarray evector2;
457 FEbaseArrayKn<double> *evector = 0; // change may 2009
458 tol = arg<double>(0, stack, tol);// correct 26/06/24 thanks to PHT
459 nbev = arg<long>(1, stack, 10);
460 sym = arg<bool>(2, stack, false);
461 sigma = arg<double>(3, stack, 0.0);
462 evalue = arg<KN<double>*>(4, stack, 0);
463 evector = arg<FEbaseArrayKn<double>*>(5, stack, 0);
464 ncv = arg<long>(6, stack, 0);
465 maxit = arg<long>(7, stack, 0);
466 evaluei = arg<KN<double>*>(8, stack, 0);
467 rawvector = arg<KNM<double>*>(9, stack, 0);
468 resid = arg<KN<double>*>(10, stack, 0);
469 mymode = arg<long>(17, stack, -1);
470 driver = arg<long>(11, stack, mymode==2 ? 3 : 4);
471
472 char which[3];
473 ToWhich (stack, which, nargs[12]);
474
475 // evector=evector2.first;
476
477 Matrice_Creuse<K> *pOP1 = 0, *pB = 0;
478 if (expOP1)
479 pOP1 = GetAny<Matrice_Creuse<K> *>((*expOP1)(stack));
480 if (expB)
481 pB = GetAny<Matrice_Creuse<K> *>((*expB)(stack));
482 double *residptr = resid ? (double*) *resid : 0;
483 long n = 0;
484 if (pOP1)
485 n = pOP1->A->n;
486 else {
487 ffassert(expn);
488 n = GetAny<long>((*expn)(stack));
489 }
490
491 if (evalue) nbev = Max((long)evalue->N(), nbev);
492
493 const RNM_VirtualMatrix<K> *ptOP1 = 0, *ptB = 0, *ptOP = 0, *ptB1 = 0;
494
495 if (pOP1)
496 ptOP1 = &(const RNM_VirtualMatrix<K>&)(pOP1->A);
497 if (pB)
498 ptB = &(const RNM_VirtualMatrix<K>&)(pB->A);
499 FuncMat<K> *pcOP = 0, *pcB = 0;
500 if (codeOP1 || codeOP)
501 ptOP1 = pcOP = new FuncMat<K>(n, stack, codeOP, codeOP1);
502 if (codeB || codeB1)
503 ptB = pcB = new FuncMat<K>(n, stack, codeB, codeB1);
504
505 MatriceIdentite<K> Id(n);
506
507 if (!ptB) ptB = &Id;
508 const RNM_VirtualMatrix<K> &OP1 = *ptOP1, &B = *ptB;
509 if (sym) {
510 nbev = min(n-1, nbev);
511 if (!ncv) ncv = min(nbev*2+1, n);
512 } else {
513 nbev = min(nbev, n-2);
514 if (!ncv) ncv = nbev*2 + 1;
515 }
516 ncv = max(nbev+2, ncv);
517 ncv = min(ncv, n);
518
519 if (!maxit) maxit = 100*nbev;
520
521 const char *serr[10];
522 int err = 0;
523 if (!(nbev < n))
524 serr[err++] = " Number of eigenvalues of OP to be computed nev <= n ";
525
526 if ((driver < 1 || driver > 5) && sym)
527 serr[err++] = " the driver = 1, 2, 3, 4, 5 ";
528 if ((driver < 1 || driver > 4) && !sym)
529 serr[err++] = " the driver = 1, 2, 3, 4 ";
530 // 2 <= NCV-NEV and NCV <= N
531
532 if (!(ncv <= n) && sym)
533 serr[err++] = " ( ncv <= n) (symmetric case) ";
534 if (!((ncv <= n) && 2 <= (ncv-nbev )) && !sym )
535 serr[err++] = " ( ncv <= n) 2 <= (ncv-nev ) (non-symmetric case) ";
536
537 if (n != OP1.M)
538 serr[err++] = " the first matrix in EigneValue is not square.";
539 if (n != B.N)
540 serr[err++] = "Sorry the row's number of the secand matrix in EigneValue is wrong.";
541 if (n != B.M)
542 serr[err++] = "Sorry the column's number of the secand matrix in EigneValue is wrong.";
543
544 if (verbosity && mpirank == 0) {
545 if(sym)
546 cout << "Real symmetric eigenvalue problem: A*x - B*x*lambda" << endl;
547 else
548 cout << "Real non symmetric eigenvalue problem: A*x - B*x*lambda" << endl;
549 }
550
551 if (verbosity > 9 || err)
552 cout << " n " << n << ", nev "<< nbev << ", tol =" << tol << ", maxit =" << maxit
553 << ", ncv = " <<ncv << ", driver = " << driver << " which = " << which << endl;
554 if (err) {
555 cerr << " list of the error " << endl;
556 for (int i = 0; i < err; ++i)
557 cerr << "\n- " << serr[i] << endl;
558 ExecError("Fatal Error in EigenValue (real)");
559 }
560
561 KN<K> work(n);
562 int dmode[] = {-1, 1, 3, 2, 3};
563
564 if (sym) {
565 int ido = 0;
566 //char bmat= mode == 1 ? 'I' : 'G';
567 char bmat = (driver<3)? 'I' : 'G';
568 int mode = dmode[driver];
569 if (mymode > 0 && (mymode != mode)) {
570 cerr << " Error mode == " << mymode << " == " << mode << " driver =" << driver << endl;
571 ExecError("Wrong mod in Arpack sym");
572 }
573
574 // char which[3]= "LM"; // larger value
575 // if(mode >2) which[0] ='S'; // smaller value
576 int ishift = 1; // Auto Shift true by default
577 int iparam[12] = {0, ishift, 0, (int)maxit, 1, (int)nconv, 0, mode, 0, 0, 0, 0};
578 int ipntr[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
579 KN<double> workd(3*n+1);
580 int lworkl = ncv*(ncv+9);
581 KN<double> workl(lworkl+1);
582 KN<double> vp(ncv*n+1);
583
584 int info = (residptr !=0);
585 KN<double> vresid(residptr ? 1 : n);
586 if (!residptr) residptr = &vresid[0];
587
588 while (1) {
589 saupp(ido, bmat, n, which, nbev, tol, residptr, ncv, vp, n,
590 iparam, ipntr, workd, workl, lworkl, info);
591 if (verbosity > 99)
592 cout << " saupp ido: " << ido << " info : " << info << endl;
593 if (info < 0) { cerr << " -- err arpack info = " << info << endl; }
594 sauppError(info);
595
596 if (ido == 99) break;
597
598 KN_<double> xx(&workd[ipntr[1]], n);
599 KN_<double> yy(&workd[ipntr[2]], n);
600 KN_<double> zz(&workd[ipntr[3]], n);
601 DoIdoAction(ido, bmat, mode, xx, yy, zz, work, OP1, B);
602 }
603
604 nconv = iparam[5];
605 if (nconv == 0) cerr << " -- Strange: no eigens values ??? " << endl;
606 // Finding eigenvalues and eigenvectors.
607 if (nconv) {
608 int newdim = nbev;
609 if (nconv > nbev) {
610 cerr << "WARNING: nconv(saupp) > nbev: " << nconv << " > " << nbev << endl;
611 newdim = nconv;
612 }
613 KN<double> evr(newdim);
614 KNM<double> Z(n, newdim);
615 int ldz = n;
616 char HowMny ='A';
617 int rvec = 1;
618
619 seupp(rvec, HowMny, evr, Z, ldz, sigma, bmat, n,
620 which, nbev, tol, residptr, ncv, vp, n, iparam,ipntr, workd, workl, lworkl, info);
621
622 if (verbosity > 5) {
623 cout << "Dimension of the system : " << n << endl;
624 cout << "Number of 'requested' eigenvalues : " << nbev << endl;
625 cout << "Number of 'converged' eigenvalues : " << nconv << endl;
626 cout << "Number of Arnoldi vectors generated: " << ncv << endl;
627 cout << "Number of iterations taken : " << iparam[3] << endl;
628 cout << endl;
629 nconv = min(nconv, nbev);
630 //if (prob.EigenvaluesFound()) {
631 cout << "Eigenvalues:" << endl;// Add FH feb 2016
632 for (int i = 0; i < nconv; i++) {
633 cout << " lambda[" << (i+1) << "]: " << evr(i) << endl;
634 KN_<K> vi(Z(':', i)) ;
635 if (verbosity > 99)
636 cout <<" Eigenvector: :" << vi << endl;
637 }
638 cout << endl;
639 }
640
641 if (evalue) {
642 KN<double> & ev(*evalue);
643 int m = Min(nconv, ev.N());
644 for (int i = 0; i < m; i++)
645 ev[i] = evr(i);
646 }
647 if (rawvector) {
648 int m = Min(nconv, rawvector->M());
649 ffassert(rawvector->N() == n);
650 for (int i = 0; i < m; i++) {
651 KN_<K> vi(Z(':', i)) ;
652 // cout << " ------ EV--raw " << vi.min() << " " << vi.max() << endl;
653 (*rawvector)(':', i) = vi;
654 }
655 }
656
657 if (evector) {
658 FEbaseArrayKn<K> & ev(*evector);
659 int m = Min(nconv, (long)ev.N);
660 for (int i = 0; i < m; i++) {
661 //KN<K> & xx= *(ev(i));
662 //if(xx.pVh->NbDoF != n)
663 //ExecError("Wrong Type size of FEspace to store the eigen vector ");
664 // if (xx.pVh != pOP1->pUh)
665 // ExecError("Wrong Type of FEspace to store the eigen vector ");
666 //xx.Vh = pOP1->Uh;
667 KN_<K> vi(Z(':', i));
668 ev.set(i, vi);
669 }
670 }
671 }
672 } else {
673 // non symmetric case ,
674 // Finding an Arnoldi basis.
675 //int mode = 3; // Shift invert
676 int ido = 0;
677 //char bmat='G';
678 char bmat = (driver<3) ? 'I' : 'G';
679 int mode = dmode[driver];
680
681 if (mymode > 0 && (mymode != mode)) {
682 cerr << " Error nonsym mode == " << mymode << " == " << mode << " driver =" << driver << endl;
683 ExecError("Wrong mod in Arpack non-sym");
684 }
685
686 // char which[] = "LM";
687 int ishift = 1; // Auto Shift true by default
688 int iparam[12] = {0, ishift, 0, (int)maxit, 1, (int)nconv, 0, mode, 0, 0, 0, 0};
689 int ipntr[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
690 KN<double> workd(3*n+1);
691 int lworkl = 3*ncv*(ncv+2);
692 KN<double> workl(lworkl+1);
693 KN<double> vp(ncv*n+1);
694 int info = (residptr != 0);
695 KN<double> vresid(residptr ? 1 : n);
696 if (!residptr) residptr = &vresid[0];
697 if (verbosity > 9)
698 cout << " n " << n << " nbev "<< nbev << " tol =" << tol << " maxit =" << maxit << " ncv = " << ncv << endl;
699
700 while (1) {
701 naupp(ido, bmat, n, which, nbev, tol, residptr, ncv, vp, n,
702 iparam, ipntr, workd, workl, lworkl, info);
703 if (verbosity > 99) { cout << " naupp ido: " << ido << " info : " << info << endl; }
704 if (info < 0) { cerr << " -- err arpack info = " << info << endl; }
705 sauppError(info);
706 if (ido == 99) break;
707 KN_<double> xx(&workd[ipntr[1]], n);
708 KN_<double> yy(&workd[ipntr[2]], n);
709 KN_<double> zz(&workd[ipntr[3]], n);
710 DoIdoAction(ido, bmat, mode, xx, yy, zz, work, OP1, B);
711 }
712
713 nconv = iparam[5];
714 if (nconv) {
715 int newdim = nbev;
716 if (nconv > nbev) {
717 cerr << "WARNING: nconv(naupp) > nbev: " << nconv << " > " << nbev << endl;
718 newdim = nconv;
719 }
720 KN<double> evr(newdim+1), evi(newdim+1);
721 KNM<double> Z(n, newdim+1);
722 KN<double> workev(3*ncv);
723 int ldz = n;
724 char HowMny = 'A';
725 int rvec = 1;
726 double sigmai = 0;
727 neupp(rvec, HowMny, evr,evi, Z , ldz, sigma, sigmai, workev,
728 bmat, n, which, nbev, tol, residptr, ncv,
729 vp, n, iparam, ipntr, workd, workl, lworkl, info);
730
731 if (verbosity) {
732 cout << "Real non-symmetric eigenvalue problem: A*x - B*x*lambda" << endl;
733 cout << "mode " << mode << " sigma=" << sigma << endl << endl;
734
735 cout << "Dimension of the system : " << n << endl;
736 cout << "Number of 'requested' eigenvalues : " << nbev << endl;
737 cout << "Number of 'converged' eigenvalues : " << nconv << endl;
738 cout << "Number of Arnoldi vectors generated: " << ncv << endl;
739 cout << "Number of iterations taken : " << iparam[3] << endl;
740 cout << endl;
741 nconv = min(nconv, nbev);// Add FH feb 2016
742
743 cout << "Eigenvalues:" << endl;
744 for (int i = 0; i < nconv; i++) {
745 cout << " lambda[" << (i+1) << "]: ";
746 ios::fmtflags oldflag = cout.flags();
747 cout.setf(ios::showpos);
748 cout << evr(i) << evi(i) << "i" << endl;
749 cout.flags(oldflag); // restore flags
750 KN_<double> vi(Z(':', i));
751 if (verbosity > 99) {
752 cout << " Eigenvector: " << vi << endl;
753 cout << endl;
754 }
755 }
756 }
757
758 if (evalue) {
759 KN<double> &ev(*evalue);
760 int m = Min(nconv, ev.N());
761 for (int i = 0; i < m; i++)
762 ev[i] = evr(i);
763 }
764
765 if (evaluei) {
766 KN<double> &ev(*evaluei);
767 int m = Min(nconv, ev.N());
768 for (int i = 0; i < m; i++)
769 ev[i] = evi(i);
770 }
771
772 if (rawvector) {
773 //K* rawev(prob.RawEigenvectors());
774 int m = Min(nconv, rawvector->M());
775 ffassert(rawvector->N() == n);
776 for (int i = 0; i < m; i++) {
777 //int k = i;
778 KN_<K> vi(Z(':', i));
779 if (verbosity > 2 && mpirank ==0)
780 cout << " ------ EV--raw " << vi.min() << " " << vi.max() << endl;
781 (*rawvector)(':', i) = vi;
782 }
783 }
784
785 if (evector) {
786 // K* rawev(prob.RawEigenvectors());
787 // rawev + n*k is
788 // iev = prob.EigenvalueImag(k)
789 // iev==0 => the eigen vector
790 // iev> 0 => real
791 // start real : rawev + n*k
792 // start imag : ramev +n*(k+1)
793 // iev < 0 => complex
794 // start real : rawev + n*(k-1)
795 // -start imag : ramev +n*(k)
796 FEbaseArrayKn<K> &ev(*evector);
797 int m = Min(nconv, (long)ev.N);
798 for (int i = 0; i < m; i++) {
799 // K ev_i=
800 //prob.EigenvalueImag(i);
801 //KN<K> & xx= *(ev[i]);
802 // if (xx.pVh != pOP1->pUh)
803 // ExecError("Wrong Type of FEspace to store the eigen vector ");
804 // xx.Vh = pOP1->Uh;
805 // int k=(ev_i < 0) ? i-1 : i;
806 //int k=i;
807 KN_<K> vi(Z(':', i));//rawev+n*k,n) ;
808 //xx= new KN<K>(vi);
809 ev.set(i, vi);//new KN<K>(vi));
810 }
811 }
812 }
813 }
814 if(pcOP) delete pcOP;
815 if(pcB) delete pcB;
816
817 return (long)nconv;
818 }
819
820 AnyType EigenValueC::E_EV::operator () (Stack stack) const {
821 dddd = (verbosity >= 200);
822 double tol = 1e-6;
823 long nconv = 0;
824 long nbev = 1;
825 long ncv = 0; // the number of Arnoldi vectors generated
826 long maxit = 0; // the maximum number of Arnoldi iterations
827 long driver = 4;
828 K sigma = 0;
829 KN<K> *evalue = 0;
830 KN<K> *resid = 0;
831 KNM<K> *rawvector = 0;
832
833 // pfecarray evector2;
834 FEbaseArrayKn<Complex> *evector = 0;
835 tol = arg<double>(0, stack, 0);
836 nbev = arg<long>(1, stack, 10);
837 sigma = arg<K>(2, stack, 0.0);
838 evalue = arg<KN<K>*>(3, stack, 0);
839 // evector2 =arg<pfecarray>(4,stack,make_pair<pfecbasearray,int>(0,0));
840 evector = arg<FEbaseArrayKn<Complex>*>(4, stack, 0);
841 ncv = arg<long>(5, stack, 0);
842 maxit = arg<long>(6, stack, 0);
843 rawvector = arg<KNM<K>*>(7, stack, 0);
844 resid = arg<KN<K>*>(8, stack, 0);
845 int mymode = arg<long>(15, stack, -1);
846 driver = arg<long>(9, stack, mymode==2 ? 3 : 4);
847 K *residptr = resid ? (K*)*resid : 0;
848 char which[3];
849 ToWhich(stack, which, nargs[10]);
850
851 //evector=evector2.first;
852 ffassert(driver > 0 && driver < 5);
853 // Matrice_Creuse<K> *pOP1 = GetAny<Matrice_Creuse<K> *>((*expOP1)(stack));
854 // Matrice_Creuse<K> *pB = GetAny<Matrice_Creuse<K> *>((*expB)(stack));
855
856 if (evalue) nbev = Max((long)evalue->N(), nbev);
857 if (!maxit) maxit = 100*nbev;
858
859 //const MatriceCreuse<K> & OP1 = pOP1->A;
860 //const MatriceCreuse<K> & B = pB->A;
861
862 //long n=OP1.n;
863
864 Matrice_Creuse<K> *pOP1 = 0, *pB = 0;
865 if (expOP1)
866 pOP1 = GetAny<Matrice_Creuse<K>*>((*expOP1)(stack));
867 if (expB)
868 pB = GetAny<Matrice_Creuse<K>*>((*expB)(stack));
869 // double * residptr=resid? (double*) *resid : 0;
870 // cout << " residptr = " << residptr <<endl;
871 long n = 0;
872 if (pOP1)
873 n = pOP1->A->n;
874 else {
875 ffassert(expn);
876 n = GetAny<long>((*expn)(stack));
877 }
878
879
880 const RNM_VirtualMatrix<K> *ptOP1, *ptB;
881 if (pOP1)
882 ptOP1 = &(const RNM_VirtualMatrix<K>&)(pOP1->A);
883 if (pB)
884 ptB = &(const RNM_VirtualMatrix<K>&)(pB->A);
885 FuncMat<K> *pcOP1 = 0, *pcB = 0;
886 if (codeOP1 || codeOP)
887 ptOP1 = pcOP1 = new FuncMat<K>(n, stack, codeOP, codeOP1);
888 if (codeB || codeB1)
889 ptB = pcB = new FuncMat<K>(n, stack, codeB, codeB1);
890 MatriceIdentite<K> Id(n);
891
892 if (!ptB) ptB = &Id;
893 const RNM_VirtualMatrix<K> &OP1 = *ptOP1, &B = *ptB;
894
895 nbev = min(nbev, n-2);
896 if (!ncv) ncv = nbev*2+1;
897 ncv = max(nbev+2, ncv);
898 ncv = min(ncv, n);
899 const char *serr[10];
900 int err = 0;
901 if (nbev >= n-1)
902 serr[err++] = " Number of eigenvalues of OP to be computed <= n-2 ";
903 if (driver < 1 || driver > 4)
904 serr[err++] = " the driver = 1 ,2 ,4 ";
905 // 2 <= NCV-NEV and NCV <= N
906 if (!(2 <= nbev && ncv <= n))
907 serr[err++] = " ( 2 <= nbev && nvc <= n) ";
908 if (n != OP1.N)
909 serr[err++] = " the first matrix in EigneValue is not Hermitien.";
910 if (n != B.N)
911 serr[err++] = "Sorry the row's number of the secand matrix in EigneValue is wrong.";
912 if (n != B.M)
913 serr[err++] = "Sorry the column's number of the secand matrix in EigneValue is wrong.";
914 if (verbosity)
915 cout << "Complex eigenvalue problem: A*x - B*x*lambda" << endl;
916 if (verbosity > 9 || err)
917 cout << " n " << n << " nev "<< nbev << " tol = " << tol << " maxit = " << maxit << " ncv = " << ncv
918 << " driver = " << driver << " which = " << which << endl;
919 if (err) {
920 cerr << " list of the error " << endl;
921 for (int i = 0; i < err; ++i)
922 cerr << "\n- " << serr[i] << endl;
923 ExecError("Fatal Error in EigenValue (complex)");
924 }
925
926 KN<K> work(n);
927 // ARrcSymGenEig is a class that requires the user to provide a
928 // way to perform the matrix-vector products w = OP*Bv =
929 // inv(A-sigma*B)*B*v and w = B*v, where sigma is the adopted shift.
930 // OP1 = (A-sigma*B)
931 // OP = inv(OP)
932 /*
933 ffassert(0);
934
935
936 ARrcCompGenEig<R> prob( n, nbev, sigma,"LM",ncv,tol,maxit,residptr);
937
938
939 // OP = inv[A - sigma*I]
940
941
942 // Finding an Arnoldi basis.
943
944 while (!prob.ArnoldiBasisFound()) {
945
946 // Calling ARPACK FORTRAN code. Almost all work needed to
947
948 prob.TakeStep();
949 */
950
951 // cas non symetrique ,
952 // Finding an Arnoldi basis.\
953
954 // int mode=3; // Shift invert \
955
956 int ido = 0;
957 //char bmat='G';
958 int dmode[] = {-1, 1, 3, 2, 3};
959 char bmat = (driver<3) ? 'I' : 'G';
960 int mode = dmode[driver];
961 // char which[]="LM";
962 int ishift = 1; // Auto Shift true by default
963 if (mymode > 0 && (mymode != mode)) {
964 cerr << " Error complex case: mode == " << mymode << " == " << mode << " driver = " << driver << endl;
965 ExecError("Wrong mod in Arpack complex case:");
966 }
967
968 int iparam[12] = {0, ishift, 0, (int)maxit, 1, (int)nconv, 0, (int)mode, 0, 0, 0, 0};
969 int ipntr[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
970 KN<K> workd(3*n+1);
971 int lworkl = 3*ncv*ncv + 5*ncv;
972 KN<K> workl(lworkl+1);
973 KN<K> vp(ncv*n+1);
974 int info = (residptr != 0);
975 KN<double> rwork(ncv+1);
976 KN<K> vresid(residptr ? 1 : n);
977 if (!residptr) residptr = &vresid[0];
978
979 while(1) {
980 caupp(ido, bmat, n, which, nbev,
981 tol, residptr, ncv,
982 vp, n, iparam, ipntr, workd, workl,
983 lworkl, rwork, info);
984
985 if (verbosity > 99) { cout << " caupp ido: " << ido << " info : " << info << endl; }
986 if (info < 0) { cerr << " -- err arpack info = " << info << endl; }
987 sauppError(info);
988
989 if (ido == 99) break;
990
991 KN_<K> xx(&workd[ipntr[1]], n);
992 KN_<K> yy(&workd[ipntr[2]], n);
993 KN_<K> zz(&workd[ipntr[3]], n);
994
995 DoIdoAction(ido, bmat, mode, xx, yy, zz, work, OP1, B);
996 }
997
998 nconv = iparam[5];
999 if (nconv) {
1000 int newdim = nbev;
1001 if (nconv > nbev) {
1002 cerr << "WARNING: nconv(caupp) > nbev: " << nconv << " > " << nbev << endl;
1003 newdim = nconv;
1004 }
1005 KN<K> evc(newdim);
1006 KNM<K> Z(n, newdim);
1007 KN<K> workev(3*ncv);
1008 //int ldz=n;
1009 char HowMny ='A';
1010 int rvec = 1;
1011 ceupp(rvec, HowMny, evc, Z, n, sigma, workev, bmat, n, which,
1012 nbev, tol, residptr, ncv,
1013 vp, n, iparam, ipntr, workd, workl, lworkl, rwork, info);
1014
1015 if (verbosity) {
1016 cout << "Complex eigenvalue problem: A*x - B*x*lambda" << endl;
1017 cout << "driver = " << driver << " - sigma= " << sigma << endl << endl;
1018
1019 cout << "Dimension of the system : " << n << endl;
1020 cout << "Number of 'requested' eigenvalues : " << nbev << endl;
1021 cout << "Number of 'converged' eigenvalues : " << nconv << endl;
1022 cout << "Number of Arnoldi vectors generated: " << ncv << endl;
1023 cout << "Number of iterations taken : " << iparam[3] << endl;
1024 cout << endl;
1025 nconv = min(nconv, nbev);
1026 cout << "Eigenvalues:" << endl;// Add FH feb 2016
1027 for (int i = 0; i < nconv; i++) {
1028 cout << " lambda[" << (i+1) << "]: " << evc(i) << endl;
1029 KN_<K> vi(Z(':', i)) ;
1030 if (verbosity > 99)
1031 cout <<" Eigenvector: :" << vi << endl;
1032 }
1033 cout << endl;
1034 }
1035
1036 if (evalue) {
1037 KN<K> & ev(*evalue);
1038 int m = Min(nconv, ev.N());
1039 for (int i = 0; i < m; i++)
1040 ev[i] = evc(i);
1041 }
1042 if (evector) {
1043 //FEbaseArray<K,v_fes> & ev(*evector);
1044 FEbaseArrayKn<K> &ev(*evector);
1045 int m = Min(nconv, (long)ev.N);
1046 for (int i = 0; i < m; i++) {
1047 //FEbase<K,v_fes> & xx= **(ev[i]);
1048 //KN_<K> vi(Z(':',i)) ;
1049 //xx= new KN<K>(vi);
1050 ev.set(i, Z(':', i));
1051 }
1052 }
1053 if(rawvector) {
1054 int m = Min(nconv, rawvector->M());
1055 ffassert(rawvector->N() == n);
1056 for (int i = 0; i < m; i++) {
1057 KN_<K> vi(Z(':', i)) ;
1058 (*rawvector)(':', i) = vi;
1059 if (verbosity > 2 && mpirank == 0)
1060 cout << " raw " << vi.l2() << " == " << (*rawvector)(':', i).l2()<< endl;
1061 }
1062 }
1063 }
1064
1065 return (long)nconv;
1066 }
1067
1068 #ifndef DYNM_LOAD
1069 void init_eigenvalue () {
1070 if (verbosity && (mpirank == 0)) cout << "eigenvalue ";
1071 Global.Add("EigenValue", "(", new EigenValue()); // j + dJ
1072 Global.Add("EigenValue", "(", new EigenValueC()); // j + dJ
1073 Global.Add("EigenValue", "(", new EigenValue(1)); // j + dJ
1074 Global.Add("EigenValue", "(", new EigenValue(1, 1)); // j + dJ
1075 Global.Add("EigenValue", "(", new EigenValue(1, 1, 1)); // A=, B=
1076 // sorry no to solve ambiguity => change of name
1077 Global.Add("complexEigenValue", "(", new EigenValueC(1)); // j + dJ
1078 Global.Add("complexEigenValue", "(", new EigenValueC(1, 1)); // j + dJ
1079 Global.Add("complexEigenValue", "(", new EigenValueC(1, 1, 1)); // A=, B= ...
1080 }
1081 #else
1082 class Init {
1083 public:
1084 Init() {
1085 if (verbosity && (mpirank == 0)) cout << "eigenvalue ";
1086 Global.Add("EigenValue2", "(", new EigenValue()); // j + dJ
1087 Global.Add("EigenValue2", "(", new EigenValueC(1)); // j + dJ
1088 }
1089 };
1090 Init init;
1091 #endif