Debian Codegraph @

packages/anasazi/test/OrthoManager/cxx_mattest_complex.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 // This test is for the MatOrthoManager interface to ICGSOrthoManager,
11 // SVQBOrthoManager and BasicOrthoManager
12 //
13 // The matrix used is from MatrixMarket:
14 // Name: MHD1280B: Alfven Spectra in Magnetohydrodynamics
15 // Source: Source: A. Booten, M.N. Kooper, H.A. van der Vorst, S. Poedts and J.P. Goedbloed University of Utrecht, the Netherlands
16 // Discipline: Plasma physics
17 // URL: http://math.nist.gov/MatrixMarket/data/NEP/mhd/mhd1280b.html
18 // Size: 1280 x 1280
19 // NNZ: 22778 entries
20 //
21 #include "AnasaziConfigDefs.hpp"
22 #include "AnasaziSolverUtils.hpp"
23 #include "AnasaziBasicOutputManager.hpp"
24 #include "AnasaziSVQBOrthoManager.hpp"
25 #include "AnasaziICGSOrthoManager.hpp"
26 #include "AnasaziBasicOrthoManager.hpp"
27 #include "Teuchos_CommandLineProcessor.hpp"
28 #include "Teuchos_StandardCatchMacros.hpp"
29
30 // I/O for Harwell-Boeing files
31 #ifdef HAVE_ANASAZI_TRIUTILS
32 #include "Trilinos_Util_iohb.h"
33 #endif
34
35 // templated multivector and sparse matrix classes
36 #include "MyMultiVec.hpp"
37 #include "MyBetterOperator.hpp"
38 #include "MySDMHelpers.hpp"
39
40 using namespace Teuchos;
41 using namespace Anasazi;
42 using namespace std;
43
44 typedef std::complex<double> ST;
45 typedef MultiVec<ST> MV;
46 typedef Operator<ST> OP;
47 typedef MultiVecTraits<ST,MV> MVT;
48 typedef OperatorTraits<ST,MV,OP> OPT;
49 typedef ScalarTraits<ST> SCT;
50 typedef SCT::magnitudeType MT;
51
52 // this is the tolerance that all tests are performed against
53 const MT TOL = 1.0e-12;
54 const MT ATOL = 10;
55
56 // declare an output manager for handling local output
57 RCP< Anasazi::BasicOutputManager<ST> > MyOM;
58
59 // some forward declarations
60 int testProjectMat(RCP<MatOrthoManager<ST,MV,OP> > OM, RCP<const MV> S, RCP<const MV> X1, RCP<const MV> X2);
61 int testNormalizeMat(RCP<MatOrthoManager<ST,MV,OP> > OM, RCP<const MV> S);
62 int testProjectAndNormalizeMat(RCP<MatOrthoManager<ST,MV,OP> > OM, RCP<const MV> S, RCP<const MV> X1, RCP<const MV> X2);
63
64 MT MVDiff(const MV &X, const MV &Y);
65
66 int main(int argc, char *argv[])
67 {
68 const ST ONE = SCT::one();
69 const MT ZERO = SCT::magnitude(SCT::zero());
70
71 bool verbose = false;
72 int numFailed = 0;
73 bool debug = false;
74 string ortho = "SVQB";
75 std::string filename;
76 int dim = 100;
77 int sizeS = 5;
78 int sizeX1 = 11; // MUST: sizeS + sizeX1 + sizeX2 <= elements[0]-1
79 int sizeX2 = 13; // MUST: sizeS + sizeX1 + sizeX2 <= elements[0]-1
80
81 bool success = true;
82 try {
83
84 CommandLineProcessor cmdp(false,true);
85 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
86 cmdp.setOption("debug","nodebug",&debug,"Print debugging information.");
87 cmdp.setOption("ortho",&ortho,"Which ortho manager: SVQB or Basic or ICGS");
88 cmdp.setOption("dim",&dim,"Controls the size of multivectors.");
89 cmdp.setOption("sizeS",&sizeS,"Controls the width of the input multivector.");
90 cmdp.setOption("sizeX1",&sizeX1,"Controls the width of the first basis.");
91 cmdp.setOption("sizeX2",&sizeX2,"Controls the width of the second basis.");
92 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
93 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
94 return -1;
95 }
96
97 // below we will assume that sizeX1 > sizeX2
98 // this does not effect our testing, since we will test P_{X1,Y1} P_{X2,Y2} as well as P_{X2,Y2} P_{X1,Y1}
99 // however, is does allow us to simplify some logic
100 if (sizeX1 < sizeX2) {
101 swap(sizeX1,sizeX2);
102 }
103
104 // instantiate the output manager
105 MyOM = rcp( new BasicOutputManager<ST>() );
106 if (verbose) {
107 // output in this driver will be sent to Anasazi::Warnings
108 MyOM->setVerbosity(Anasazi::Warnings);
109 }
110
111 // Output Anasazi version
112 MyOM->stream(Anasazi::Warnings) << Anasazi_Version() << endl << endl;
113
114 // Problem information
115 RCP< const MyBetterOperator<ST> > M;
116 if (filename != "") {
117 int dim2,nnz;
118 double *dvals;
119 int *colptr,*rowind;
120 nnz = -1;
121 int info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
122 &colptr,&rowind,&dvals);
123 TEUCHOS_TEST_FOR_EXCEPTION(info == 0 || nnz < 0,
124 std::runtime_error,
125 "Error reading file " << filename << ". info == " << info << " and nnz == " << nnz);
126 // Convert interleaved doubles to complex values
127 std::vector<ST> cvals(nnz);
128 for (int ii=0; ii<nnz; ii++) {
129 cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
130 }
131 // Build the problem matrix
132 M = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,&cvals[0]) );
133 }
134
135 // Create ortho managers
136 RCP<MatOrthoManager<ST,MV,OP> > OM;
137 if (ortho == "SVQB") {
138 OM = rcp( new SVQBOrthoManager<ST,MV,OP>(M) );
139 }
140 else if (ortho == "Basic") {
141 OM = rcp( new BasicOrthoManager<ST,MV,OP>(M) );
142 }
143 else if (ortho == "ICGS") {
144 OM = rcp( new ICGSOrthoManager<ST,MV,OP>(M) );
145 }
146 else {
147 TEUCHOS_TEST_FOR_EXCEPTION(true,invalid_argument,"Command line parameter \"ortho\" must be \"SVQB\" or \"Basic\".");
148 }
149
150
151 // multivector to spawn off of
152 RCP<MV> S = rcp( new MyMultiVec<ST>(dim, sizeS) );
153
154 // create X1, X2
155 // they must be M-orthonormal and mutually M-orthogonal
156 MyOM->stream(Errors) << " Generating X1,X2 for testing... " << endl;
157 RCP<MV> X1 = MVT::Clone(*S,sizeX1),
158 X2 = MVT::Clone(*S,sizeX2);
159 {
160 int dummy;
161 MT err;
162 // X1
163 MVT::MvRandom(*X1);
164 dummy = OM->normalize(*X1);
165 TEUCHOS_TEST_FOR_EXCEPTION(dummy != sizeX1, std::runtime_error,
166 "normalize(X1) returned rank " << dummy << " from "
167 << sizeX1 << " vectors. Cannot continue.");
168 err = OM->orthonormError(*X1);
169 TEUCHOS_TEST_FOR_EXCEPTION(err > TOL,std::runtime_error,
170 "normalize(X1) did meet tolerance: orthonormError(X1) == " << err);
171 MyOM->stream(Warnings) << " || <X1,X1> - I || : " << err << endl;
172 // X2
173 MVT::MvRandom(*X2);
174 dummy = OM->projectAndNormalize(*X2,Teuchos::tuple<RCP<const MV> >(X1));
175 TEUCHOS_TEST_FOR_EXCEPTION(dummy != sizeX2, std::runtime_error,
176 "projectAndNormalize(X2,X1) returned rank " << dummy << " from "
177 << sizeX2 << " vectors. Cannot continue.");
178 err = OM->orthonormError(*X2);
179 TEUCHOS_TEST_FOR_EXCEPTION(err > TOL,std::runtime_error,
180 "projectAndNormalize(X2,X1) did not meet tolerance: orthonormError(X2) == " << err);
181 err = OM->orthogError(*X2,*X1);
182 MyOM->stream(Warnings) << " || <X2,X2> - I || : " << err << endl;
183 TEUCHOS_TEST_FOR_EXCEPTION(err > TOL,std::runtime_error,
184 "projectAndNormalize(X2,X1) did not meet tolerance: orthogError(X2,X1) == " << err);
185 MyOM->stream(Warnings) << " || <X2,X1> || : " << err << endl;
186 }
187 MyOM->stream(Warnings) << endl;
188
189
190 {
191 // just a random multivector
192 MVT::MvRandom(*S);
193
194 MyOM->stream(Errors) << " projectMat(): testing on random multivector " << endl;
195 numFailed += testProjectMat(OM,S,X1,X2);
196 }
197
198 {
199 // run a X1,X2 range multivector against P_X1 P_X2
200 // note, this is allowed under the restrictions on projectMat,
201 // because <X1,X2> = 0
202 SerialDenseMatrix<int,ST> C1(sizeX1,sizeS), C2(sizeX2,sizeS);
203 Anasazi::randomSDM(C1);
204 Anasazi::randomSDM(C2);
205 MVT::MvTimesMatAddMv(ONE,*X1,C1,ZERO,*S);
206 MVT::MvTimesMatAddMv(ONE,*X2,C2,ONE,*S);
207
208 MyOM->stream(Errors) << " projectMat(): testing [X1 X2]-range multivector against P_X1 P_X2 " << endl;
209 numFailed += testProjectMat(OM,S,X1,X2);
210 }
211
212 if (sizeS > 2) {
213 MVT::MvRandom(*S);
214 RCP<MV> mid = MVT::Clone(*S,1);
215 SerialDenseMatrix<int,ST> c(sizeS,1);
216 MVT::MvTimesMatAddMv(ONE,*S,c,ZERO,*mid);
217 std::vector<int> ind(1);
218 ind[0] = sizeS-1;
219 MVT::SetBlock(*mid,ind,*S);
220
221 MyOM->stream(Errors) << " normalizeMat(): testing on rank-deficient multivector " << endl;
222 numFailed += testNormalizeMat(OM,S);
223 }
224
225
226 if (sizeS > 1) {
227 // rank-1
228 RCP<MV> one = MVT::Clone(*S,1);
229 MVT::MvRandom(*one);
230 SerialDenseMatrix<int,ST> scaleS(sizeS,1);
231 Anasazi::randomSDM(scaleS);
232 // put multiple of column 0 in columns 0:sizeS-1
233 for (int i=0; i<sizeS; i++) {
234 std::vector<int> ind(1);
235 ind[0] = i;
236 RCP<MV> Si = MVT::CloneViewNonConst(*S,ind);
237 MVT::MvAddMv(scaleS(i,0),*one,ZERO,*one,*Si);
238 }
239
240 MyOM->stream(Errors) << " normalizeMat(): testing on rank-1 multivector " << endl;
241 numFailed += testNormalizeMat(OM,S);
242 }
243
244
245 {
246 MVT::MvRandom(*S);
247
248 MyOM->stream(Errors) << " projectAndNormalizeMat(): testing on random multivector " << endl;
249 numFailed += testProjectAndNormalizeMat(OM,S,X1,X2);
250 }
251
252 /* Commenting out because it's a numerically sensitive test
253 {
254 // run a X1,X2 range multivector against P_X1 P_X2
255 // this is allowed as <X1,X2> == 0
256 // it should require randomization, as
257 // P_X1 P_X2 (X1*C1 + X2*C2) = P_X1 X1*C1 = 0
258 // and
259 // P_X2 P_X1 (X2*C2 + X1*C1) = P_X2 X2*C2 = 0
260 SerialDenseMatrix<int,ST> C1(sizeX1,sizeS), C2(sizeX2,sizeS);
261 Anasazi::randomSDM(C1);
262 Anasazi::randomSDM(C2);
263 MVT::MvTimesMatAddMv(ONE,*X1,C1,ZERO,*S);
264 MVT::MvTimesMatAddMv(ONE,*X2,C2,ONE,*S);
265
266 MyOM->stream(Errors) << " projectAndNormalizeMat(): testing [X1 X2]-range multivector against P_X1 P_X2 " << endl;
267 numFailed += testProjectAndNormalizeMat(OM,S,X1,X2);
268 }
269 */
270
271
272 if (sizeS > 2) {
273 MVT::MvRandom(*S);
274 RCP<MV> mid = MVT::Clone(*S,1);
275 SerialDenseMatrix<int,ST> c(sizeS,1);
276 MVT::MvTimesMatAddMv(ONE,*S,c,ZERO,*mid);
277 std::vector<int> ind(1);
278 ind[0] = sizeS-1;
279 MVT::SetBlock(*mid,ind,*S);
280
281 MyOM->stream(Errors) << " projectAndNormalizeMat(): testing on rank-deficient multivector " << endl;
282 numFailed += testProjectAndNormalizeMat(OM,S,X1,X2);
283 }
284
285
286 if (sizeS > 1) {
287 // rank-1
288 RCP<MV> one = MVT::Clone(*S,1);
289 MVT::MvRandom(*one);
290 SerialDenseMatrix<int,ST> scaleS(sizeS,1);
291 Anasazi::randomSDM(scaleS);
292 // put multiple of column 0 in columns 0:sizeS-1
293 for (int i=0; i<sizeS; i++) {
294 std::vector<int> ind(1);
295 ind[0] = i;
296 RCP<MV> Si = MVT::CloneViewNonConst(*S,ind);
297 MVT::MvAddMv(scaleS(i,0),*one,ZERO,*one,*Si);
298 }
299
300 MyOM->stream(Errors) << " projectAndNormalizeMat(): testing on rank-1 multivector " << endl;
301 numFailed += testProjectAndNormalizeMat(OM,S,X1,X2);
302 }
303
304 }
305 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success);
306
307 if (numFailed || success==false) {
308 if (numFailed) {
309 MyOM->stream(Errors) << numFailed << " errors." << endl;
310 }
311 MyOM->stream(Errors) << "End Result: TEST FAILED" << endl;
312 return -1;
313 }
314 //
315 // Default return value
316 //
317 MyOM->stream(Errors) << "End Result: TEST PASSED" << endl;
318 return 0;
319 }
320
321
322
323
324
325 ////////////////////////////////////////////////////////////////////////////
326 int testProjectAndNormalizeMat(RCP<MatOrthoManager<ST,MV,OP> > OM,
327 RCP<const MV> S,
328 RCP<const MV> X1, RCP<const MV> X2) {
329
330 const ST ONE = SCT::one();
331 const MT ZERO = SCT::magnitude(SCT::zero());
332
333 const int sizeS = MVT::GetNumberVecs(*S);
334 const int sizeX1 = MVT::GetNumberVecs(*X1);
335 const int sizeX2 = MVT::GetNumberVecs(*X2);
336 int numerr = 0;
337 bool hasM = (OM->getOp() != null);
338 std::ostringstream sout;
339
340 //
341 // output tests:
342 // <S_out,S_out> = I
343 // <S_out,X1> = 0
344 // <S_out,X2> = 0
345 // S_in = S_out B + X1 C1 + X2 C2
346 //
347 // we will loop over an integer specifying the test combinations
348 // the bit pattern for the different tests is listed in parenthesis
349 //
350 // for the projectors, test the following combinations:
351 // none (00)
352 // P_X1 (01)
353 // P_X2 (10)
354 // P_X1 P_X2 (11)
355 // P_X2 P_X1 (11)
356 // the latter two should be tested to give the same answer
357 //
358 // for each of these, we should test with C1, C2 and B
359 //
360 // if hasM:
361 // with and without MX1 (1--)
362 // with and without MX2 (1---)
363 // with and without MS (1----)
364 //
365 // as hasM controls the upper level bits, we need only run test cases 0-3 if hasM==false
366 // otherwise, we run test cases 0-31
367 //
368
369 int numtests;
370 RCP<const MV> MX1, MX2;
371 RCP<MV> MS;
372 if (hasM) {
373 MX1 = MVT::Clone(*S,sizeX1);
374 MX2 = MVT::Clone(*S,sizeX2);
375 MS = MVT::Clone(*S,sizeS);
376 OPT::Apply(*(OM->getOp()),*X1,const_cast<MV&>(*MX1));
377 OPT::Apply(*(OM->getOp()),*X2,const_cast<MV&>(*MX2));
378 OPT::Apply(*(OM->getOp()),*S ,*MS);
379 numtests = 32;
380 }
381 else {
382 numtests = 4;
383 }
384
385 // test ortho error before orthonormalizing
386 if (X1 != null) {
387 MT err = OM->orthogError(*S,*X1);
388 sout << " || <S,X1> || before : " << err << endl;
389 }
390 if (X2 != null) {
391 MT err = OM->orthogError(*S,*X2);
392 sout << " || <S,X2> || before : " << err << endl;
393 }
394
395 for (int t=0; t<numtests; t++) {
396
397 // pointers to simplify calls below
398 RCP<const MV> lclMX1;
399 RCP<const MV> lclMX2;
400 RCP<MV> lclMS;
401 if ( t & 4 ) {
402 lclMX1 = MX1;
403 }
404 if ( t & 8 ) {
405 lclMX2 = MX2;
406 }
407 if ( t & 16 ) {
408 lclMS = MS;
409 }
410
411 Array<RCP<const MV> > theX, theMX;
412 RCP<SerialDenseMatrix<int,ST> > B = rcp( new SerialDenseMatrix<int,ST>(sizeS,sizeS) );
413 Array<RCP<SerialDenseMatrix<int,ST> > > C;
414 if ( (t & 3) == 0 ) {
415 // neither <X1,Y1> nor <X2,Y2>
416 // C, theX and theY are already empty
417 }
418 else if ( (t & 3) == 1 ) {
419 // X1
420 theX = Teuchos::tuple(X1);
421 theMX = Teuchos::tuple(lclMX1);
422 C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)) );
423 }
424 else if ( (t & 3) == 2 ) {
425 // X2
426 theX = Teuchos::tuple(X2);
427 theMX = Teuchos::tuple(lclMX2);
428 C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) );
429 }
430 else {
431 // X1 and X2, and the reverse.
432 theX = Teuchos::tuple(X1,X2);
433 theMX = Teuchos::tuple(lclMX1,lclMX2);
434 C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)),
435 rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) );
436 }
437
438 try {
439 // call routine
440 // if (t & 3) == 3, {
441 // call with reversed input: X2 X1
442 // }
443 // test all outputs for correctness
444 // test all outputs for equivalence
445
446 // here is where the outputs go
447 Array<RCP<const MV> > S_outs;
448 Array<RCP<const MV> > MS_outs;
449 Array<Array<RCP<SerialDenseMatrix<int,ST> > > > C_outs;
450 Array<RCP<SerialDenseMatrix<int,ST> > > B_outs;
451 RCP<MV> Scopy, MScopy;
452 Array<int> ret_out;
453
454 // copies of S,MS
455 Scopy = MVT::CloneCopy(*S);
456 if (lclMS != Teuchos::null) {
457 MScopy = MVT::CloneCopy(*lclMS);
458 }
459 // randomize this data, it should be overwritten
460 Anasazi::randomSDM(*B);
461 for (unsigned int i=0; i<C.size(); i++) {
462 Anasazi::randomSDM(*C[i]);
463 }
464 // run test
465 int ret = OM->projectAndNormalizeMat(*Scopy,theX,C,B,MScopy,theMX);
466 sout << "projectAndNormalizeMat() returned rank " << ret << endl;
467 if (ret == 0) {
468 sout << " Cannot continue." << endl;
469 numerr++;
470 break;
471 }
472 ret_out.push_back(ret);
473 // projectAndNormalizeMat() is only required to return a
474 // basis of rank "ret"
475 // this is what we will test:
476 // the first "ret" columns in Scopy, MScopy
477 // the first "ret" rows in B
478 // save just the parts that we want
479 // we allocate S and MS for each test, so we can save these as views
480 // however, save copies of the C and B
481 if (ret < sizeS) {
482 vector<int> ind(ret);
483 for (int i=0; i<ret; i++) {
484 ind[i] = i;
485 }
486 S_outs.push_back( MVT::CloneView(*Scopy,ind) );
487 if (MScopy != null) {
488 MS_outs.push_back( MVT::CloneView(*MScopy,ind) );
489 }
490 else {
491 MS_outs.push_back( Teuchos::null );
492 }
493 B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(Teuchos::Copy,*B,ret,sizeS) ) );
494 }
495 else {
496 S_outs.push_back( Scopy );
497 MS_outs.push_back( MScopy );
498 B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(*B) ) );
499 }
500 C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >(0) );
501 if (C.size() > 0) {
502 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) );
503 }
504 if (C.size() > 1) {
505 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) );
506 }
507
508 // do we run the reversed input?
509 if ( (t & 3) == 3 ) {
510 // copies of S,MS
511 Scopy = MVT::CloneCopy(*S);
512 if (lclMS != Teuchos::null) {
513 MScopy = MVT::CloneCopy(*lclMS);
514 }
515 // randomize this data, it should be overwritten
516 Anasazi::randomSDM(*B);
517 for (unsigned int i=0; i<C.size(); i++) {
518 Anasazi::randomSDM(*C[i]);
519 }
520 // flip the inputs
521 C = Teuchos::tuple( C[1], C[0] );
522 theX = Teuchos::tuple( theX[1], theX[0] );
523 theMX = Teuchos::tuple( theMX[1], theMX[0] );
524 // run test
525 ret = OM->projectAndNormalizeMat(*Scopy,theX,C,B,MScopy,theMX);
526 sout << "projectAndNormalizeMat() returned rank " << ret << endl;
527 if (ret == 0) {
528 sout << " Cannot continue." << endl;
529 numerr++;
530 break;
531 }
532 ret_out.push_back(ret);
533 // projectAndNormalizeMat() is only required to return a
534 // basis of rank "ret"
535 // this is what we will test:
536 // the first "ret" columns in Scopy, MScopy
537 // the first "ret" rows in B
538 // save just the parts that we want
539 // we allocate S and MS for each test, so we can save these as views
540 // however, save copies of the C and B
541 if (ret < sizeS) {
542 vector<int> ind(ret);
543 for (int i=0; i<ret; i++) {
544 ind[i] = i;
545 }
546 S_outs.push_back( MVT::CloneView(*Scopy,ind) );
547 if (MScopy != null) {
548 MS_outs.push_back( MVT::CloneView(*MScopy,ind) );
549 }
550 else {
551 MS_outs.push_back( Teuchos::null );
552 }
553 B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(Teuchos::Copy,*B,ret,sizeS) ) );
554 }
555 else {
556 S_outs.push_back( Scopy );
557 MS_outs.push_back( MScopy );
558 B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(*B) ) );
559 }
560 C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >() );
561 // reverse the Cs to compensate for the reverse projectors
562 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) );
563 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) );
564 // flip the inputs back
565 C = Teuchos::tuple( C[1], C[0] );
566 theX = Teuchos::tuple( theX[1], theX[0] );
567 theMX = Teuchos::tuple( theMX[1], theMX[0] );
568 }
569
570
571 // test all outputs for correctness
572 for (unsigned int o=0; o<S_outs.size(); o++) {
573 // MS == M*S
574 if (MS_outs[o] != null) {
575 RCP<MV> tmp = MVT::Clone(*S_outs[o],ret_out[o]);
576 OPT::Apply(*(OM->getOp()),*S_outs[o],*tmp);
577 MT err = MVDiff(*tmp,*MS_outs[o]);
578 if (err > TOL) {
579 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
580 numerr++;
581 }
582 sout << " " << t << "|| MS - M*S || : " << err << endl;
583 }
584 // S^T M S == I
585 {
586 MT err = OM->orthonormError(*S_outs[o]);
587 if (err > TOL) {
588 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
589 numerr++;
590 }
591 sout << " || <S,S> - I || after : " << err << endl;
592 }
593 // S_in = X1*C1 + X2*C2 + S_out*B
594 {
595 RCP<MV> tmp = MVT::Clone(*S,sizeS);
596 MVT::MvTimesMatAddMv(ONE,*S_outs[o],*B_outs[o],ZERO,*tmp);
597 if (C_outs[o].size() > 0) {
598 MVT::MvTimesMatAddMv(ONE,*theX[0],*C_outs[o][0],ONE,*tmp);
599 if (C_outs[o].size() > 1) {
600 MVT::MvTimesMatAddMv(ONE,*theX[1],*C_outs[o][1],ONE,*tmp);
601 }
602 }
603 MT err = MVDiff(*tmp,*S);
604 if (err > ATOL*TOL) {
605 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
606 numerr++;
607 }
608 sout << " " << t << "|| S_in - X1*C1 - X2*C2 - S_out*B || : " << err << endl;
609 }
610 // <X1,S> == 0
611 if (theX.size() > 0 && theX[0] != null) {
612 MT err = OM->orthogError(*theX[0],*S_outs[o]);
613 if (err > TOL) {
614 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
615 numerr++;
616 }
617 sout << " " << t << "|| <X[0],S> || after : " << err << endl;
618 }
619 // <X2,S> == 0
620 if (theX.size() > 1 && theX[1] != null) {
621 MT err = OM->orthogError(*theX[1],*S_outs[o]);
622 if (err > TOL) {
623 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
624 numerr++;
625 }
626 sout << " " << t << "|| <X[1],S> || after : " << err << endl;
627 }
628 }
629 }
630 catch (const OrthoError &e) {
631 sout << " ------------------------------------------- projectAndNormalizeMat() threw exception" << endl;
632 sout << " Error: " << e.what() << endl;
633 numerr++;
634 }
635
636 } // test for
637
638 MsgType type = Warnings;
639 if (numerr>0) type = Errors;
640 MyOM->stream(type) << sout.str();
641 MyOM->stream(type) << endl;
642
643 return numerr;
644 }
645
646
647
648 ////////////////////////////////////////////////////////////////////////////
649 int testNormalizeMat(RCP<MatOrthoManager<ST,MV,OP> > OM, RCP<const MV> S)
650 {
651
652 const ST ONE = SCT::one();
653 const MT ZERO = SCT::magnitude(SCT::zero());
654
655 const int sizeS = MVT::GetNumberVecs(*S);
656 int numerr = 0;
657 bool hasM = (OM->getOp() != null);
658 std::ostringstream sout;
659
660 //
661 // output tests:
662 // <S_out,S_out> = I
663 // S_in = S_out B
664 //
665 // we will loop over an integer specifying the test combinations
666 // the bit pattern for the different tests is listed in parenthesis
667 //
668 // for each of the following, we should test B
669 //
670 // if hasM:
671 // with and without MS (1)
672 //
673 // as hasM controls the upper level bits, we need only run test case 0 if hasM==false
674 // otherwise, we run test cases 0-1
675 //
676
677 int numtests;
678 RCP<MV> MS;
679 if (hasM) {
680 MS = MVT::Clone(*S,sizeS);
681 OPT::Apply(*(OM->getOp()),*S ,*MS);
682 numtests = 2;
683 }
684 else {
685 numtests = 1;
686 }
687
688 for (int t=0; t<numtests; t++) {
689
690 // pointers to simplify calls below
691 RCP<MV> lclMS;
692 if ( t & 1 ) {
693 lclMS = MS;
694 }
695
696 // allocate the data separately from B, to allow it to persist as B is delete/reallocated
697 Teuchos::ArrayRCP<ST> Bdata = Teuchos::arcp<ST>(sizeS*sizeS);
698 RCP<SerialDenseMatrix<int,ST> > B = rcp( new SerialDenseMatrix<int,ST>(Teuchos::View,Bdata.getRawPtr(),sizeS,sizeS,sizeS) );
699
700 try {
701 // call routine
702 // test all outputs for correctness
703
704 // here is where the outputs go
705 RCP<MV> Scopy, MScopy;
706 int ret;
707
708 // copies of S,MS
709 Scopy = MVT::CloneCopy(*S);
710 if (lclMS != Teuchos::null) {
711 MScopy = MVT::CloneCopy(*lclMS);
712 }
713 // randomize this data, it should be overwritten
714 Anasazi::randomSDM(*B);
715 // run test
716 ret = OM->normalizeMat(*Scopy,B,MScopy);
717 sout << "normalizeMat() returned rank " << ret << endl;
718 if (ret == 0) {
719 sout << " Cannot continue." << endl;
720 numerr++;
721 break;
722 }
723 // normalizeMat() is only required to return a
724 // basis of rank "ret"
725 // this is what we will test:
726 // the first "ret" columns in Scopy, MScopy
727 // the first "ret" rows in B
728 // get pointers to the parts that we want
729 if (ret < sizeS) {
730 vector<int> ind(ret);
731 for (int i=0; i<ret; i++) {
732 ind[i] = i;
733 }
734 Scopy = MVT::CloneViewNonConst(*Scopy,ind);
735 if (MScopy != null) {
736 MScopy = MVT::CloneViewNonConst(*MScopy,ind);
737 }
738 B = rcp( new SerialDenseMatrix<int,ST>(Teuchos::View,Bdata.getRawPtr(),ret,ret,sizeS) );
739 }
740
741 // test all outputs for correctness
742 // MS == M*S
743 if (MScopy != null) {
744 RCP<MV> tmp = MVT::Clone(*Scopy,ret);
745 OPT::Apply(*(OM->getOp()),*Scopy,*tmp);
746 MT err = MVDiff(*tmp,*MScopy);
747 if (err > TOL) {
748 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
749 numerr++;
750 }
751 sout << " " << t << "|| MS - M*S || : " << err << endl;
752 }
753 // S^T M S == I
754 {
755 MT err = OM->orthonormError(*Scopy);
756 if (err > TOL) {
757 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
758 numerr++;
759 }
760 sout << " || <S,S> - I || after : " << err << endl;
761 }
762 // S_in = S_out*B
763 {
764 RCP<MV> tmp = MVT::Clone(*S,sizeS);
765 MVT::MvTimesMatAddMv(ONE,*Scopy,*B,ZERO,*tmp);
766 MT err = MVDiff(*tmp,*S);
767 if (err > ATOL*TOL) {
768 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
769 numerr++;
770 }
771 sout << " " << t << "|| S_in - S_out*B || : " << err << endl;
772 }
773 }
774 catch (const OrthoError &e) {
775 sout << " ------------------------------------------- normalizeMat() threw exception" << endl;
776 sout << " Error: " << e.what() << endl;
777 numerr++;
778 }
779
780 } // test for
781
782 MsgType type = Warnings;
783 if (numerr>0) type = Errors;
784 MyOM->stream(type) << sout.str();
785 MyOM->stream(type) << endl;
786
787 return numerr;
788 }
789
790
791
792 ////////////////////////////////////////////////////////////////////////////
793 int testProjectMat(RCP<MatOrthoManager<ST,MV,OP> > OM,
794 RCP<const MV> S,
795 RCP<const MV> X1, RCP<const MV> X2) {
796
797 const ST ONE = SCT::one();
798
799 const int sizeS = MVT::GetNumberVecs(*S);
800 const int sizeX1 = MVT::GetNumberVecs(*X1);
801 const int sizeX2 = MVT::GetNumberVecs(*X2);
802 int numerr = 0;
803 bool hasM = (OM->getOp() != null);
804 std::ostringstream sout;
805
806 //
807 // output tests:
808 // <S_out,X1> = 0
809 // <S_out,X2> = 0
810 // S_in = S_out + X1 C1 + X2 C2
811 //
812 // we will loop over an integer specifying the test combinations
813 // the bit pattern for the different tests is listed in parenthesis
814 //
815 // for the projectors, test the following combinations:
816 // none (00)
817 // P_X1 (01)
818 // P_X2 (10)
819 // P_X1 P_X2 (11)
820 // P_X2 P_X1 (11)
821 // the latter two should be tested to give the same answer
822 //
823 // for each of these, we should test
824 // with C1 and C2
825 //
826 // if hasM:
827 // with and without MX1 (1--)
828 // with and without MX2 (1---)
829 // with and without MS (1----)
830 //
831 // as hasM controls the upper level bits, we need only run test cases 0-3 if hasM==false
832 // otherwise, we run test cases 0-31
833 //
834
835 int numtests;
836 RCP<const MV> MX1, MX2;
837 RCP<MV> MS;
838 if (hasM) {
839 MX1 = MVT::Clone(*S,sizeX1);
840 MX2 = MVT::Clone(*S,sizeX2);
841 MS = MVT::Clone(*S,sizeS);
842 OPT::Apply(*(OM->getOp()),*X1,const_cast<MV&>(*MX1));
843 OPT::Apply(*(OM->getOp()),*X2,const_cast<MV&>(*MX2));
844 OPT::Apply(*(OM->getOp()),*S ,*MS);
845 numtests = 64;
846 }
847 else {
848 numtests = 8;
849 }
850
851 // test ortho error before orthonormalizing
852 if (X1 != null) {
853 MT err = OM->orthogError(*S,*X1);
854 sout << " || <S,X1> || before : " << err << endl;
855 }
856 if (X2 != null) {
857 MT err = OM->orthogError(*S,*X2);
858 sout << " || <S,X2> || before : " << err << endl;
859 }
860
861 for (int t=0; t<numtests; t++) {
862
863 // pointers to simplify calls below
864 RCP<const MV> lclMX1;
865 RCP<const MV> lclMX2;
866 RCP<MV> lclMS;
867 if ( t & 8 ) {
868 lclMX1 = MX1;
869 }
870 if ( t & 16 ) {
871 lclMX2 = MX2;
872 }
873 if ( t & 32 ) {
874 lclMS = MS;
875 }
876
877 Array<RCP<const MV> > theX, theMX;
878 Array<RCP<SerialDenseMatrix<int,ST> > > C;
879 if ( (t & 3) == 0 ) {
880 // neither X1 nor X2
881 // C and theX are already empty
882 }
883 else if ( (t & 3) == 1 ) {
884 // X1
885 theX = Teuchos::tuple(X1);
886 theMX = Teuchos::tuple(lclMX1);
887 C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)) );
888 }
889 else if ( (t & 3) == 2 ) {
890 // X2
891 theX = Teuchos::tuple(X2);
892 theMX = Teuchos::tuple(lclMX2);
893 C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) );
894 }
895 else if ( (t & 3) == 3 ) {
896 // X1 and X2, and the reverse.
897 theX = Teuchos::tuple(X1,X2);
898 theMX = Teuchos::tuple(lclMX1,lclMX2);
899 C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)),
900 rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) );
901 }
902 else {}
903
904 try {
905 // call routine
906 // if (t & 3) == 3, {
907 // call with reversed input: X2 X1
908 // }
909 // test all outputs for correctness
910 // test all outputs for equivalence
911
912 // here is where the outputs go
913 Array<RCP<MV> > S_outs;
914 Array<RCP<MV> > MS_outs;
915 Array<Array<RCP<SerialDenseMatrix<int,ST> > > > C_outs;
916 RCP<MV> Scopy, MScopy;
917
918 // copies of S,MS
919 Scopy = MVT::CloneCopy(*S);
920 if (lclMS != Teuchos::null) {
921 MScopy = MVT::CloneCopy(*lclMS);
922 }
923 // randomize this data, it should be overwritten
924 for (unsigned int i=0; i<C.size(); i++) {
925 Anasazi::randomSDM(*C[i]);
926 }
927 // run test
928 OM->projectMat(*Scopy,theX,C,MScopy,theMX);
929 // we allocate S and MS for each test, so we can save these as views
930 // however, save copies of the C
931 S_outs.push_back( Scopy );
932 MS_outs.push_back( MScopy );
933 C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >(0) );
934 if (C.size() > 0) {
935 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) );
936 }
937 if (C.size() > 1) {
938 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) );
939 }
940
941 // do we run the reversed input?
942 if ( (t & 3) == 3 ) {
943 // copies of S,MS
944 Scopy = MVT::CloneCopy(*S);
945 if (lclMS != Teuchos::null) {
946 MScopy = MVT::CloneCopy(*lclMS);
947 }
948 // randomize this data, it should be overwritten
949 for (unsigned int i=0; i<C.size(); i++) {
950 Anasazi::randomSDM(*C[i]);
951 }
952 // flip the inputs
953 C = Teuchos::tuple( C[1], C[0] );
954 theX = Teuchos::tuple( theX[1], theX[0] );
955 theMX = Teuchos::tuple( theMX[1], theMX[0] );
956 // run test
957 OM->projectMat(*Scopy,theX,C,MScopy,theMX);
958 // we allocate S and MS for each test, so we can save these as views
959 // however, save copies of the C
960 S_outs.push_back( Scopy );
961 MS_outs.push_back( MScopy );
962 // we are in a special case: P_X1 and P_X2, so we know we applied
963 // two projectors, and therefore have two C[i]
964 C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >() );
965 // reverse the Cs to compensate for the reverse projectors
966 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) );
967 C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) );
968 // flip the inputs back
969 C = Teuchos::tuple( C[1], C[0] );
970 theX = Teuchos::tuple( theX[1], theX[0] );
971 theMX = Teuchos::tuple( theMX[1], theMX[0] );
972 }
973
974 // test all outputs for correctness
975 for (unsigned int o=0; o<S_outs.size(); o++) {
976 // MS == M*S
977 if (MS_outs[o] != null) {
978 RCP<MV> tmp = MVT::Clone(*S_outs[o],sizeS);
979 OPT::Apply(*(OM->getOp()),*S_outs[o],*tmp);
980 MT err = MVDiff(*tmp,*MS_outs[o]);
981 if (err > TOL) {
982 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
983 numerr++;
984 }
985 sout << " " << t << "|| MS - M*S || : " << err << endl;
986 }
987 // S_in = X1*C1 + X2*C2 + S_out
988 {
989 RCP<MV> tmp = MVT::CloneCopy(*S_outs[o]);
990 if (C_outs[o].size() > 0) {
991 MVT::MvTimesMatAddMv(ONE,*theX[0],*C_outs[o][0],ONE,*tmp);
992 if (C_outs[o].size() > 1) {
993 MVT::MvTimesMatAddMv(ONE,*theX[1],*C_outs[o][1],ONE,*tmp);
994 }
995 }
996 MT err = MVDiff(*tmp,*S);
997 if (err > ATOL*TOL) {
998 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
999 numerr++;
1000 }
1001 sout << " " << t << "|| S_in - X1*C1 - X2*C2 - S_out || : " << err << endl;
1002 }
1003 // <X1,S> == 0
1004 if (theX.size() > 0 && theX[0] != null) {
1005 MT err = OM->orthogError(*theX[0],*S_outs[o]);
1006 if (err > TOL) {
1007 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
1008 numerr++;
1009 }
1010 sout << " " << t << "|| <X[0],S> || after : " << err << endl;
1011 }
1012 // <X2,S> == 0
1013 if (theX.size() > 1 && theX[1] != null) {
1014 MT err = OM->orthogError(*theX[1],*S_outs[o]);
1015 if (err > TOL) {
1016 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
1017 numerr++;
1018 }
1019 sout << " " << t << "|| <X[1],S> || after : " << err << endl;
1020 }
1021 }
1022
1023 // test all outputs for equivalence
1024 // check all combinations:
1025 // output 0 == output 1
1026 // output 0 == output 2
1027 // output 1 == output 2
1028 for (unsigned int o1=0; o1<S_outs.size(); o1++) {
1029 for (unsigned int o2=o1+1; o2<S_outs.size(); o2++) {
1030 // don't need to check MS_outs because we check
1031 // S_outs and MS_outs = M*S_outs
1032 // don't need to check C_outs either
1033 //
1034 // check that S_outs[o1] == S_outs[o2]
1035 MT err = MVDiff(*S_outs[o1],*S_outs[o2]);
1036 if (err > ATOL*TOL) {
1037 sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl;
1038 numerr++;
1039 }
1040 }
1041 }
1042
1043 }
1044 catch (const OrthoError &e) {
1045 sout << " ------------------------------------------- projectMat() threw exception" << endl;
1046 sout << " Error: " << e.what() << endl;
1047 numerr++;
1048 }
1049
1050 } // test for
1051
1052 MsgType type = Warnings;
1053 if (numerr>0) type = Errors;
1054 MyOM->stream(type) << sout.str();
1055 MyOM->stream(type) << endl;
1056
1057 return numerr;
1058 }
1059
1060
1061
1062 MT MVDiff(const MV &X, const MV &Y) {
1063
1064 const ST ONE = SCT::one();
1065
1066 const int sizeX = MVT::GetNumberVecs(X);
1067 SerialDenseMatrix<int,ST> xTmx(sizeX,sizeX);
1068
1069 // tmp <- X
1070 RCP<MV> tmp = MVT::CloneCopy(X);
1071
1072 // tmp <- tmp - Y
1073 MVT::MvAddMv(-ONE,Y,ONE,*tmp,*tmp);
1074
1075 MVT::MvTransMv(ONE,*tmp,*tmp,xTmx);
1076 MT err = 0;
1077 for (int i=0; i<sizeX; i++) {
1078 MT lclErr = SCT::magnitude(xTmx(i,i));
1079 err += lclErr;
1080 }
1081 return SCT::magnitude(SCT::squareroot(err));
1082 }