| 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 OrthoManager 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 testProject(RCP<OrthoManager<ST,MV> > OM, RCP<const MV> S, RCP<const MV> X1, RCP<const MV> X2); |
| 61 |
int testNormalize(RCP<OrthoManager<ST,MV> > OM, RCP<const MV> S); |
| 62 |
int testProjectAndNormalize(RCP<OrthoManager<ST,MV> > 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 |
|
| 69 |
const ST ONE = SCT::one(); |
| 70 |
const MT ZERO = SCT::magnitude(SCT::zero()); |
| 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 |
// however, it does allow us to simplify some logic |
| 99 |
if (sizeX1 < sizeX2) { |
| 100 |
swap(sizeX1,sizeX2); |
| 101 |
} |
| 102 |
|
| 103 |
// instantiate the output manager |
| 104 |
MyOM = rcp( new BasicOutputManager<ST>() ); |
| 105 |
if (verbose) { |
| 106 |
// output in this driver will be sent to Anasazi::Warnings |
| 107 |
MyOM->setVerbosity(Anasazi::Warnings); |
| 108 |
} |
| 109 |
|
| 110 |
// Output Anasazi version |
| 111 |
MyOM->stream(Anasazi::Warnings) << Anasazi_Version() << endl << endl; |
| 112 |
|
| 113 |
// Problem information |
| 114 |
RCP< const MyBetterOperator<ST> > M; |
| 115 |
if (filename != "") { |
| 116 |
int dim2,nnz; |
| 117 |
double *dvals; |
| 118 |
int *colptr,*rowind; |
| 119 |
nnz = -1; |
| 120 |
int info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz, |
| 121 |
&colptr,&rowind,&dvals); |
| 122 |
TEUCHOS_TEST_FOR_EXCEPTION(info == 0 || nnz < 0, |
| 123 |
std::runtime_error, |
| 124 |
"Error reading file " << filename << ". info == " << info << " and nnz == " << nnz); |
| 125 |
// Convert interleaved doubles to complex values |
| 126 |
std::vector<ST> cvals(nnz); |
| 127 |
for (int ii=0; ii<nnz; ii++) { |
| 128 |
cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]); |
| 129 |
} |
| 130 |
// Build the problem matrix |
| 131 |
M = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,&cvals[0]) ); |
| 132 |
} |
| 133 |
|
| 134 |
// Create ortho managers |
| 135 |
RCP<OrthoManager<ST,MV> > OM; |
| 136 |
if (ortho == "SVQB") { |
| 137 |
OM = rcp( new SVQBOrthoManager<ST,MV,OP>(M) ); |
| 138 |
} |
| 139 |
else if (ortho == "Basic") { |
| 140 |
OM = rcp( new BasicOrthoManager<ST,MV,OP>(M) ); |
| 141 |
} |
| 142 |
else if (ortho == "ICGS") { |
| 143 |
OM = rcp( new ICGSOrthoManager<ST,MV,OP>(M) ); |
| 144 |
} |
| 145 |
else { |
| 146 |
TEUCHOS_TEST_FOR_EXCEPTION(true,invalid_argument,"Command line parameter \"ortho\" must be \"SVQB\" or \"Basic\"."); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// multivector to spawn off of |
| 151 |
RCP<MV> S = rcp( new MyMultiVec<ST>(dim, sizeS) ); |
| 152 |
|
| 153 |
// create X1, X2 |
| 154 |
// they must be M-orthonormal and mutually M-orthogonal |
| 155 |
MyOM->stream(Errors) << " Generating X1,X2 for testing... " << endl; |
| 156 |
RCP<MV> X1 = MVT::Clone(*S,sizeX1), |
| 157 |
X2 = MVT::Clone(*S,sizeX2); |
| 158 |
{ |
| 159 |
int dummy; |
| 160 |
MT err; |
| 161 |
// X1 |
| 162 |
MVT::MvRandom(*X1); |
| 163 |
dummy = OM->normalize(*X1); |
| 164 |
TEUCHOS_TEST_FOR_EXCEPTION(dummy != sizeX1, std::runtime_error, |
| 165 |
"normalize(X1) returned rank " << dummy << " from " |
| 166 |
<< sizeX1 << " vectors. Cannot continue."); |
| 167 |
err = OM->orthonormError(*X1); |
| 168 |
TEUCHOS_TEST_FOR_EXCEPTION(err > TOL,std::runtime_error, |
| 169 |
"normalize(X1) did meet tolerance: orthonormError(X1) == " << err); |
| 170 |
MyOM->stream(Warnings) << " || <X1,X1> - I || : " << err << endl; |
| 171 |
// X2 |
| 172 |
MVT::MvRandom(*X2); |
| 173 |
dummy = OM->projectAndNormalize(*X2,Teuchos::tuple<RCP<const MV> >(X1)); |
| 174 |
TEUCHOS_TEST_FOR_EXCEPTION(dummy != sizeX2, std::runtime_error, |
| 175 |
"projectAndNormalize(X2,X1) returned rank " << dummy << " from " |
| 176 |
<< sizeX2 << " vectors. Cannot continue."); |
| 177 |
err = OM->orthonormError(*X2); |
| 178 |
TEUCHOS_TEST_FOR_EXCEPTION(err > TOL,std::runtime_error, |
| 179 |
"projectAndNormalize(X2,X1) did not meet tolerance: orthonormError(X2) == " << err); |
| 180 |
err = OM->orthogError(*X2,*X1); |
| 181 |
MyOM->stream(Warnings) << " || <X2,X2> - I || : " << err << endl; |
| 182 |
TEUCHOS_TEST_FOR_EXCEPTION(err > TOL,std::runtime_error, |
| 183 |
"projectAndNormalize(X2,X1) did not meet tolerance: orthogError(X2,X1) == " << err); |
| 184 |
MyOM->stream(Warnings) << " || <X2,X1> || : " << err << endl; |
| 185 |
} |
| 186 |
MyOM->stream(Warnings) << endl; |
| 187 |
|
| 188 |
|
| 189 |
{ |
| 190 |
// just a random multivector |
| 191 |
MVT::MvRandom(*S); |
| 192 |
|
| 193 |
MyOM->stream(Errors) << " project(): testing on random multivector " << endl; |
| 194 |
numFailed += testProject(OM,S,X1,X2); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
{ |
| 199 |
SerialDenseMatrix<int,ST> C1(sizeX1,sizeS), C2(sizeX2,sizeS); |
| 200 |
Anasazi::randomSDM(C1); |
| 201 |
Anasazi::randomSDM(C2); |
| 202 |
MVT::MvTimesMatAddMv(ONE,*X1,C1,ZERO,*S); |
| 203 |
MVT::MvTimesMatAddMv(ONE,*X2,C2,ONE,*S); |
| 204 |
|
| 205 |
MyOM->stream(Errors) << " project(): testing [X1 X2]-range multivector against P_X1 P_X2 " << endl; |
| 206 |
numFailed += testProject(OM,S,X1,X2); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if (sizeS > 2) { |
| 211 |
MVT::MvRandom(*S); |
| 212 |
RCP<MV> mid = MVT::Clone(*S,1); |
| 213 |
SerialDenseMatrix<int,ST> c(sizeS,1); |
| 214 |
MVT::MvTimesMatAddMv(ONE,*S,c,ZERO,*mid); |
| 215 |
std::vector<int> ind(1); |
| 216 |
ind[0] = sizeS-1; |
| 217 |
MVT::SetBlock(*mid,ind,*S); |
| 218 |
|
| 219 |
MyOM->stream(Errors) << " normalize(): testing on rank-deficient multivector " << endl; |
| 220 |
numFailed += testNormalize(OM,S); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if (sizeS > 1) { |
| 225 |
// rank-1 |
| 226 |
RCP<MV> one = MVT::Clone(*S,1); |
| 227 |
MVT::MvRandom(*one); |
| 228 |
SerialDenseMatrix<int,ST> scaleS(sizeS,1); |
| 229 |
Anasazi::randomSDM(scaleS); |
| 230 |
// put multiple of column 0 in columns 0:sizeS-1 |
| 231 |
for (int i=0; i<sizeS; i++) { |
| 232 |
std::vector<int> ind(1); |
| 233 |
ind[0] = i; |
| 234 |
RCP<MV> Si = MVT::CloneViewNonConst(*S,ind); |
| 235 |
MVT::MvAddMv(scaleS(i,0),*one,ZERO,*one,*Si); |
| 236 |
} |
| 237 |
|
| 238 |
MyOM->stream(Errors) << " normalize(): testing on rank-1 multivector " << endl; |
| 239 |
numFailed += testNormalize(OM,S); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
{ |
| 244 |
std::vector<int> ind(1); |
| 245 |
MVT::MvRandom(*S); |
| 246 |
|
| 247 |
MyOM->stream(Errors) << " projectAndNormalize(): testing on random multivector " << endl; |
| 248 |
numFailed += testProjectAndNormalize(OM,S,X1,X2); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
{ |
| 253 |
// run a X1,X2 range multivector against P_X1 P_X2 |
| 254 |
// this is allowed as <X1,X2> == 0 |
| 255 |
// it should require randomization, as |
| 256 |
// P_X1 P_X2 (X1*C1 + X2*C2) = P_X1 X1*C1 = 0 |
| 257 |
// and |
| 258 |
// P_X2 P_X1 (X2*C2 + X1*C1) = P_X2 X2*C2 = 0 |
| 259 |
SerialDenseMatrix<int,ST> C1(sizeX1,sizeS), C2(sizeX2,sizeS); |
| 260 |
Anasazi::randomSDM(C1); |
| 261 |
Anasazi::randomSDM(C2); |
| 262 |
MVT::MvTimesMatAddMv(ONE,*X1,C1,ZERO,*S); |
| 263 |
MVT::MvTimesMatAddMv(ONE,*X2,C2,ONE,*S); |
| 264 |
|
| 265 |
MyOM->stream(Errors) << " projectAndNormalize(): testing [X1 X2]-range multivector against P_X1 P_X2 " << endl; |
| 266 |
numFailed += testProjectAndNormalize(OM,S,X1,X2); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
if (sizeS > 2) { |
| 271 |
MVT::MvRandom(*S); |
| 272 |
RCP<MV> mid = MVT::Clone(*S,1); |
| 273 |
SerialDenseMatrix<int,ST> c(sizeS,1); |
| 274 |
MVT::MvTimesMatAddMv(ONE,*S,c,ZERO,*mid); |
| 275 |
std::vector<int> ind(1); |
| 276 |
ind[0] = sizeS-1; |
| 277 |
MVT::SetBlock(*mid,ind,*S); |
| 278 |
|
| 279 |
MyOM->stream(Errors) << " projectAndNormalize(): testing on rank-deficient multivector " << endl; |
| 280 |
numFailed += testProjectAndNormalize(OM,S,X1,X2); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
if (sizeS > 1) { |
| 285 |
// rank-1 |
| 286 |
RCP<MV> one = MVT::Clone(*S,1); |
| 287 |
MVT::MvRandom(*one); |
| 288 |
SerialDenseMatrix<int,ST> scaleS(sizeS, 1); |
| 289 |
Anasazi::randomSDM(scaleS); |
| 290 |
// put multiple of column 0 in columns 0:sizeS-1 |
| 291 |
for (int i=0; i<sizeS; i++) { |
| 292 |
std::vector<int> ind(1); |
| 293 |
ind[0] = i; |
| 294 |
RCP<MV> Si = MVT::CloneViewNonConst(*S,ind); |
| 295 |
MVT::MvAddMv(scaleS(i,0),*one,ZERO,*one,*Si); |
| 296 |
} |
| 297 |
|
| 298 |
MyOM->stream(Errors) << " projectAndNormalize(): testing on rank-1 multivector " << endl; |
| 299 |
numFailed += testProjectAndNormalize(OM,S,X1,X2); |
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success); |
| 304 |
|
| 305 |
if (numFailed || success==false) { |
| 306 |
if (numFailed) { |
| 307 |
MyOM->stream(Errors) << numFailed << " errors." << endl; |
| 308 |
} |
| 309 |
MyOM->stream(Errors) << "End Result: TEST FAILED" << endl; |
| 310 |
return -1; |
| 311 |
} |
| 312 |
// |
| 313 |
// Default return value |
| 314 |
// |
| 315 |
MyOM->stream(Errors) << "End Result: TEST PASSED" << endl; |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
//////////////////////////////////////////////////////////////////////////// |
| 324 |
int testProjectAndNormalize(RCP<OrthoManager<ST,MV> > OM, |
| 325 |
RCP<const MV> S, |
| 326 |
RCP<const MV> X1, RCP<const MV> X2) { |
| 327 |
|
| 328 |
const ST ONE = SCT::one(); |
| 329 |
const MT ZERO = SCT::magnitude(SCT::zero()); |
| 330 |
const int sizeS = MVT::GetNumberVecs(*S); |
| 331 |
const int sizeX1 = MVT::GetNumberVecs(*X1); |
| 332 |
const int sizeX2 = MVT::GetNumberVecs(*X2); |
| 333 |
int numerr = 0; |
| 334 |
std::ostringstream sout; |
| 335 |
|
| 336 |
// |
| 337 |
// output tests: |
| 338 |
// <S_out,S_out> = I |
| 339 |
// <S_out,X1> = 0 |
| 340 |
// <S_out,X2> = 0 |
| 341 |
// S_in = S_out B + X1 C1 + X2 C2 |
| 342 |
// |
| 343 |
// we will loop over an integer specifying the test combinations |
| 344 |
// the bit pattern for the different tests is listed in parenthesis |
| 345 |
// |
| 346 |
// for the projectors, test the following combinations: |
| 347 |
// none (00) |
| 348 |
// P_X1 (01) |
| 349 |
// P_X2 (10) |
| 350 |
// P_X1 P_X2 (11) |
| 351 |
// P_X2 P_X1 (11) |
| 352 |
// the latter two should be tested to give the same answer |
| 353 |
// |
| 354 |
// for each of these, we should test with C1, C2 and B |
| 355 |
// |
| 356 |
|
| 357 |
int numtests; |
| 358 |
numtests = 4; |
| 359 |
|
| 360 |
// test ortho error before orthonormalizing |
| 361 |
if (X1 != null) { |
| 362 |
MT err = OM->orthogError(*S,*X1); |
| 363 |
sout << " || <S,X1> || before : " << err << endl; |
| 364 |
} |
| 365 |
if (X2 != null) { |
| 366 |
MT err = OM->orthogError(*S,*X2); |
| 367 |
sout << " || <S,X2> || before : " << err << endl; |
| 368 |
} |
| 369 |
|
| 370 |
for (int t=0; t<numtests; t++) { |
| 371 |
|
| 372 |
Array<RCP<const MV> > theX; |
| 373 |
RCP<SerialDenseMatrix<int,ST> > B = rcp( new SerialDenseMatrix<int,ST>(sizeS,sizeS) ); |
| 374 |
Array<RCP<SerialDenseMatrix<int,ST> > > C; |
| 375 |
if ( (t & 3) == 0 ) { |
| 376 |
// C, theX and theY are already empty |
| 377 |
} |
| 378 |
else if ( (t & 3) == 1 ) { |
| 379 |
// X1 |
| 380 |
theX = Teuchos::tuple(X1); |
| 381 |
C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)) ); |
| 382 |
} |
| 383 |
else if ( (t & 3) == 2 ) { |
| 384 |
// X2 |
| 385 |
theX = Teuchos::tuple(X2); |
| 386 |
C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) ); |
| 387 |
} |
| 388 |
else if ( (t & 3) == 3 ) { |
| 389 |
// X1 and X2, and the reverse. |
| 390 |
theX = Teuchos::tuple(X1,X2); |
| 391 |
C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)), |
| 392 |
rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) ); |
| 393 |
} |
| 394 |
else {} |
| 395 |
|
| 396 |
try { |
| 397 |
// call routine |
| 398 |
// if (t & 3) == 3, { |
| 399 |
// call with reversed input: X2 X1 |
| 400 |
// } |
| 401 |
// test all outputs for correctness |
| 402 |
// test all outputs for equivalence |
| 403 |
|
| 404 |
// here is where the outputs go |
| 405 |
Array<RCP<const MV> > S_outs; |
| 406 |
Array<Array<RCP<SerialDenseMatrix<int,ST> > > > C_outs; |
| 407 |
Array<RCP<SerialDenseMatrix<int,ST> > > B_outs; |
| 408 |
RCP<MV> Scopy; |
| 409 |
Array<int> ret_out; |
| 410 |
|
| 411 |
// copies of S |
| 412 |
Scopy = MVT::CloneCopy(*S); |
| 413 |
// randomize this data, it should be overwritten |
| 414 |
Anasazi::randomSDM(*B); |
| 415 |
for (unsigned int i=0; i<C.size(); i++) { |
| 416 |
Anasazi::randomSDM(*C[i]); |
| 417 |
} |
| 418 |
// run test |
| 419 |
int ret = OM->projectAndNormalize(*Scopy,theX,C,B); |
| 420 |
sout << "projectAndNormalize() returned rank " << ret << endl; |
| 421 |
if (ret == 0) { |
| 422 |
sout << " Cannot continue." << endl; |
| 423 |
numerr++; |
| 424 |
break; |
| 425 |
} |
| 426 |
ret_out.push_back(ret); |
| 427 |
// projectAndNormalize() is only required to return a |
| 428 |
// basis of rank "ret" |
| 429 |
// this is what we will test: |
| 430 |
// the first "ret" columns in Scopy |
| 431 |
// the first "ret" rows in B |
| 432 |
// save just the parts that we want |
| 433 |
// we allocate S for each test, so we can save these as views |
| 434 |
// however, save copies of the C and B |
| 435 |
if (ret < sizeS) { |
| 436 |
vector<int> ind(ret); |
| 437 |
for (int i=0; i<ret; i++) { |
| 438 |
ind[i] = i; |
| 439 |
} |
| 440 |
S_outs.push_back( MVT::CloneView(*Scopy,ind) ); |
| 441 |
B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(Teuchos::Copy,*B,ret,sizeS) ) ); |
| 442 |
} |
| 443 |
else { |
| 444 |
S_outs.push_back( Scopy ); |
| 445 |
B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(*B) ) ); |
| 446 |
} |
| 447 |
C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >(0) ); |
| 448 |
if (C.size() > 0) { |
| 449 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) ); |
| 450 |
} |
| 451 |
if (C.size() > 1) { |
| 452 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) ); |
| 453 |
} |
| 454 |
|
| 455 |
// do we run the reversed input? |
| 456 |
if ( (t & 3) == 3 ) { |
| 457 |
// copies of S |
| 458 |
Scopy = MVT::CloneCopy(*S); |
| 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 |
// flip the inputs |
| 465 |
C = Teuchos::tuple( C[1], C[0] ); |
| 466 |
theX = Teuchos::tuple( theX[1], theX[0] ); |
| 467 |
// run test |
| 468 |
ret = OM->projectAndNormalize(*Scopy,theX,C,B); |
| 469 |
sout << "projectAndNormalize() returned rank " << ret << endl; |
| 470 |
if (ret == 0) { |
| 471 |
sout << " Cannot continue." << endl; |
| 472 |
numerr++; |
| 473 |
break; |
| 474 |
} |
| 475 |
ret_out.push_back(ret); |
| 476 |
// projectAndNormalize() is only required to return a |
| 477 |
// basis of rank "ret" |
| 478 |
// this is what we will test: |
| 479 |
// the first "ret" columns in Scopy |
| 480 |
// the first "ret" rows in B |
| 481 |
// save just the parts that we want |
| 482 |
// we allocate S for each test, so we can save these as views |
| 483 |
// however, save copies of the C and B |
| 484 |
if (ret < sizeS) { |
| 485 |
vector<int> ind(ret); |
| 486 |
for (int i=0; i<ret; i++) { |
| 487 |
ind[i] = i; |
| 488 |
} |
| 489 |
S_outs.push_back( MVT::CloneView(*Scopy,ind) ); |
| 490 |
B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(Teuchos::Copy,*B,ret,sizeS) ) ); |
| 491 |
} |
| 492 |
else { |
| 493 |
S_outs.push_back( Scopy ); |
| 494 |
B_outs.push_back( rcp( new SerialDenseMatrix<int,ST>(*B) ) ); |
| 495 |
} |
| 496 |
C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >() ); |
| 497 |
// reverse the Cs to compensate for the reverse projectors |
| 498 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) ); |
| 499 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) ); |
| 500 |
// flip the inputs back |
| 501 |
C = Teuchos::tuple( C[1], C[0] ); |
| 502 |
theX = Teuchos::tuple( theX[1], theX[0] ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
// test all outputs for correctness |
| 507 |
for (unsigned int o=0; o<S_outs.size(); o++) { |
| 508 |
// S^T M S == I |
| 509 |
{ |
| 510 |
MT err = OM->orthonormError(*S_outs[o]); |
| 511 |
if (err > TOL) { |
| 512 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 513 |
numerr++; |
| 514 |
} |
| 515 |
sout << " || <S,S> - I || after : " << err << endl; |
| 516 |
} |
| 517 |
// S_in = X1*C1 + C2*C2 + S_out*B |
| 518 |
{ |
| 519 |
RCP<MV> tmp = MVT::Clone(*S,sizeS); |
| 520 |
MVT::MvTimesMatAddMv(ONE,*S_outs[o],*B_outs[o],ZERO,*tmp); |
| 521 |
if (C_outs[o].size() > 0) { |
| 522 |
MVT::MvTimesMatAddMv(ONE,*theX[0],*C_outs[o][0],ONE,*tmp); |
| 523 |
if (C_outs[o].size() > 1) { |
| 524 |
MVT::MvTimesMatAddMv(ONE,*theX[1],*C_outs[o][1],ONE,*tmp); |
| 525 |
} |
| 526 |
} |
| 527 |
MT err = MVDiff(*tmp,*S); |
| 528 |
if (err > ATOL*TOL) { |
| 529 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 530 |
numerr++; |
| 531 |
} |
| 532 |
sout << " " << t << "|| S_in - X1*C1 - X2*C2 - S_out*B || : " << err << endl; |
| 533 |
} |
| 534 |
// <X1,S> == 0 |
| 535 |
if (theX.size() > 0 && theX[0] != null) { |
| 536 |
MT err = OM->orthogError(*theX[0],*S_outs[o]); |
| 537 |
if (err > TOL) { |
| 538 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 539 |
numerr++; |
| 540 |
} |
| 541 |
sout << " " << t << "|| <X[0],S> || after : " << err << endl; |
| 542 |
} |
| 543 |
// <X2,S> == 0 |
| 544 |
if (theX.size() > 1 && theX[1] != null) { |
| 545 |
MT err = OM->orthogError(*theX[1],*S_outs[o]); |
| 546 |
if (err > TOL) { |
| 547 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 548 |
numerr++; |
| 549 |
} |
| 550 |
sout << " " << t << "|| <X[1],S> || after : " << err << endl; |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
catch (const OrthoError &e) { |
| 555 |
sout << " ------------------------------------------- projectAndNormalize() threw exception" << endl; |
| 556 |
sout << " Error: " << e.what() << endl; |
| 557 |
numerr++; |
| 558 |
} |
| 559 |
|
| 560 |
} // test for |
| 561 |
|
| 562 |
MsgType type = Warnings; |
| 563 |
if (numerr>0) type = Errors; |
| 564 |
MyOM->stream(type) << sout.str(); |
| 565 |
MyOM->stream(type) << endl; |
| 566 |
|
| 567 |
return numerr; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
//////////////////////////////////////////////////////////////////////////// |
| 573 |
int testNormalize(RCP<OrthoManager<ST,MV> > OM, RCP<const MV> S) |
| 574 |
{ |
| 575 |
|
| 576 |
const ST ONE = SCT::one(); |
| 577 |
const MT ZERO = SCT::magnitude(SCT::zero()); |
| 578 |
const int sizeS = MVT::GetNumberVecs(*S); |
| 579 |
int numerr = 0; |
| 580 |
std::ostringstream sout; |
| 581 |
|
| 582 |
// |
| 583 |
// output tests: |
| 584 |
// <S_out,S_out> = I |
| 585 |
// S_in = S_out B |
| 586 |
// |
| 587 |
// we will loop over an integer specifying the test combinations |
| 588 |
// the bit pattern for the different tests is listed in parenthesis |
| 589 |
// |
| 590 |
// for each of the following, we should test B |
| 591 |
// |
| 592 |
|
| 593 |
int numtests; |
| 594 |
numtests = 1; |
| 595 |
|
| 596 |
for (int t=0; t<numtests; t++) { |
| 597 |
|
| 598 |
// allocate the data separately from B, to allow it to persist as B is delete/reallocated |
| 599 |
Teuchos::ArrayRCP<ST> Bdata = Teuchos::arcp<ST>(sizeS*sizeS); |
| 600 |
RCP<SerialDenseMatrix<int,ST> > B = rcp( new SerialDenseMatrix<int,ST>(Teuchos::View,Bdata.getRawPtr(),sizeS,sizeS,sizeS) ); |
| 601 |
|
| 602 |
try { |
| 603 |
// call routine |
| 604 |
// test all outputs for correctness |
| 605 |
|
| 606 |
// here is where the outputs go |
| 607 |
RCP<MV> Scopy; |
| 608 |
int ret; |
| 609 |
|
| 610 |
// copies of S |
| 611 |
Scopy = MVT::CloneCopy(*S); |
| 612 |
// randomize this data, it should be overwritten |
| 613 |
Anasazi::randomSDM(*B); |
| 614 |
// run test |
| 615 |
ret = OM->normalize(*Scopy,B); |
| 616 |
sout << "normalize() returned rank " << ret << endl; |
| 617 |
if (ret == 0) { |
| 618 |
sout << " Cannot continue." << endl; |
| 619 |
numerr++; |
| 620 |
break; |
| 621 |
} |
| 622 |
// normalize() is only required to return a |
| 623 |
// basis of rank "ret" |
| 624 |
// this is what we will test: |
| 625 |
// the first "ret" columns in Scopy |
| 626 |
// the first "ret" rows in B |
| 627 |
// get pointers to the parts that we want |
| 628 |
if (ret < sizeS) { |
| 629 |
vector<int> ind(ret); |
| 630 |
for (int i=0; i<ret; i++) { |
| 631 |
ind[i] = i; |
| 632 |
} |
| 633 |
Scopy = MVT::CloneViewNonConst(*Scopy,ind); |
| 634 |
B = rcp( new SerialDenseMatrix<int,ST>(Teuchos::View,Bdata.getRawPtr(),ret,ret,sizeS) ); |
| 635 |
} |
| 636 |
|
| 637 |
// test all outputs for correctness |
| 638 |
// S^T M S == I |
| 639 |
{ |
| 640 |
MT err = OM->orthonormError(*Scopy); |
| 641 |
if (err > TOL) { |
| 642 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 643 |
numerr++; |
| 644 |
} |
| 645 |
sout << " || <S,S> - I || after : " << err << endl; |
| 646 |
} |
| 647 |
// S_in = S_out*B |
| 648 |
{ |
| 649 |
RCP<MV> tmp = MVT::Clone(*S,sizeS); |
| 650 |
MVT::MvTimesMatAddMv(ONE,*Scopy,*B,ZERO,*tmp); |
| 651 |
MT err = MVDiff(*tmp,*S); |
| 652 |
if (err > ATOL*TOL) { |
| 653 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 654 |
numerr++; |
| 655 |
} |
| 656 |
sout << " " << t << "|| S_in - S_out*B || : " << err << endl; |
| 657 |
} |
| 658 |
} |
| 659 |
catch (const OrthoError &e) { |
| 660 |
sout << " ------------------------------------------- normalize() threw exception" << endl; |
| 661 |
sout << " Error: " << e.what() << endl; |
| 662 |
numerr++; |
| 663 |
} |
| 664 |
|
| 665 |
} // test for |
| 666 |
|
| 667 |
MsgType type = Warnings; |
| 668 |
if (numerr>0) type = Errors; |
| 669 |
MyOM->stream(type) << sout.str(); |
| 670 |
MyOM->stream(type) << endl; |
| 671 |
|
| 672 |
return numerr; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
//////////////////////////////////////////////////////////////////////////// |
| 678 |
int testProject(RCP<OrthoManager<ST,MV> > OM, |
| 679 |
RCP<const MV> S, |
| 680 |
RCP<const MV> X1, RCP<const MV> X2) { |
| 681 |
|
| 682 |
const ST ONE = SCT::one(); |
| 683 |
const int sizeS = MVT::GetNumberVecs(*S); |
| 684 |
const int sizeX1 = MVT::GetNumberVecs(*X1); |
| 685 |
const int sizeX2 = MVT::GetNumberVecs(*X2); |
| 686 |
int numerr = 0; |
| 687 |
std::ostringstream sout; |
| 688 |
|
| 689 |
// |
| 690 |
// output tests: |
| 691 |
// <S_out,X1> = 0 |
| 692 |
// <S_out,X2> = 0 |
| 693 |
// S_in = S_out + X1 C1 + X2 C2 |
| 694 |
// |
| 695 |
// we will loop over an integer specifying the test combinations |
| 696 |
// the bit pattern for the different tests is listed in parenthesis |
| 697 |
// |
| 698 |
// for the projectors, test the following combinations: |
| 699 |
// none (00) |
| 700 |
// P_X1 (01) |
| 701 |
// P_X2 (10) |
| 702 |
// P_X1 P_X2 (11) |
| 703 |
// P_X2 P_X1 (11) |
| 704 |
// the latter two should be tested to give the same answer |
| 705 |
// |
| 706 |
|
| 707 |
int numtests; |
| 708 |
numtests = 8; |
| 709 |
|
| 710 |
// test ortho error before orthonormalizing |
| 711 |
if (X1 != null) { |
| 712 |
MT err = OM->orthogError(*S,*X1); |
| 713 |
sout << " || <S,X1> || before : " << err << endl; |
| 714 |
} |
| 715 |
if (X2 != null) { |
| 716 |
MT err = OM->orthogError(*S,*X2); |
| 717 |
sout << " || <S,X2> || before : " << err << endl; |
| 718 |
} |
| 719 |
|
| 720 |
for (int t=0; t<numtests; t++) { |
| 721 |
|
| 722 |
Array<RCP<const MV> > theX; |
| 723 |
Array<RCP<SerialDenseMatrix<int,ST> > > C; |
| 724 |
if ( (t & 3) == 0 ) { |
| 725 |
// neither X1 nor X2 |
| 726 |
// C and theX are already empty |
| 727 |
} |
| 728 |
else if ( (t & 3) == 1 ) { |
| 729 |
// X1 |
| 730 |
theX = Teuchos::tuple(X1); |
| 731 |
C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)) ); |
| 732 |
} |
| 733 |
else if ( (t & 3) == 2 ) { |
| 734 |
// X2 |
| 735 |
theX = Teuchos::tuple(X2); |
| 736 |
C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) ); |
| 737 |
} |
| 738 |
else if ( (t & 3) == 3 ) { |
| 739 |
// X1 and X2, and the reverse. |
| 740 |
theX = Teuchos::tuple(X1,X2); |
| 741 |
C = Teuchos::tuple( rcp(new SerialDenseMatrix<int,ST>(sizeX1,sizeS)), |
| 742 |
rcp(new SerialDenseMatrix<int,ST>(sizeX2,sizeS)) ); |
| 743 |
} |
| 744 |
else {} |
| 745 |
|
| 746 |
try { |
| 747 |
// call routine |
| 748 |
// if (t & 3) == 3, { |
| 749 |
// call with reversed input: X2 X1 |
| 750 |
// } |
| 751 |
// test all outputs for correctness |
| 752 |
// test all outputs for equivalence |
| 753 |
|
| 754 |
// here is where the outputs go |
| 755 |
Array<RCP<MV> > S_outs; |
| 756 |
Array<Array<RCP<SerialDenseMatrix<int,ST> > > > C_outs; |
| 757 |
RCP<MV> Scopy; |
| 758 |
|
| 759 |
// copies of S |
| 760 |
Scopy = MVT::CloneCopy(*S); |
| 761 |
// randomize this data, it should be overwritten |
| 762 |
for (unsigned int i=0; i<C.size(); i++) { |
| 763 |
Anasazi::randomSDM(*C[i]); |
| 764 |
} |
| 765 |
// run test |
| 766 |
OM->project(*Scopy,theX,C); |
| 767 |
// we allocate S for each test, so we can save these as views |
| 768 |
// however, save copies of the C |
| 769 |
S_outs.push_back( Scopy ); |
| 770 |
C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >(0) ); |
| 771 |
if (C.size() > 0) { |
| 772 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) ); |
| 773 |
} |
| 774 |
if (C.size() > 1) { |
| 775 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) ); |
| 776 |
} |
| 777 |
|
| 778 |
// do we run the reversed input? |
| 779 |
if ( (t & 3) == 3 ) { |
| 780 |
// copies of S |
| 781 |
Scopy = MVT::CloneCopy(*S); |
| 782 |
// randomize this data, it should be overwritten |
| 783 |
for (unsigned int i=0; i<C.size(); i++) { |
| 784 |
Anasazi::randomSDM(*C[i]); |
| 785 |
} |
| 786 |
// flip the inputs |
| 787 |
C = Teuchos::tuple( C[1], C[0] ); |
| 788 |
theX = Teuchos::tuple( theX[1], theX[0] ); |
| 789 |
// run test |
| 790 |
OM->project(*Scopy,theX,C); |
| 791 |
// we allocate S for each test, so we can save these as views |
| 792 |
// however, save copies of the C |
| 793 |
S_outs.push_back( Scopy ); |
| 794 |
// we are in a special case: P_X1 and P_X2, so we know we applied |
| 795 |
// two projectors, and therefore have two C[i] |
| 796 |
C_outs.push_back( Array<RCP<SerialDenseMatrix<int,ST> > >() ); |
| 797 |
// reverse the Cs to compensate for the reverse projectors |
| 798 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[1]) ) ); |
| 799 |
C_outs.back().push_back( rcp( new SerialDenseMatrix<int,ST>(*C[0]) ) ); |
| 800 |
// flip the inputs back |
| 801 |
C = Teuchos::tuple( C[1], C[0] ); |
| 802 |
theX = Teuchos::tuple( theX[1], theX[0] ); |
| 803 |
} |
| 804 |
|
| 805 |
// test all outputs for correctness |
| 806 |
for (unsigned int o=0; o<S_outs.size(); o++) { |
| 807 |
// S_in = X1*C1 + C2*C2 + S_out |
| 808 |
{ |
| 809 |
RCP<MV> tmp = MVT::CloneCopy(*S_outs[o]); |
| 810 |
if (C_outs[o].size() > 0) { |
| 811 |
MVT::MvTimesMatAddMv(ONE,*theX[0],*C_outs[o][0],ONE,*tmp); |
| 812 |
if (C_outs[o].size() > 1) { |
| 813 |
MVT::MvTimesMatAddMv(ONE,*theX[1],*C_outs[o][1],ONE,*tmp); |
| 814 |
} |
| 815 |
} |
| 816 |
MT err = MVDiff(*tmp,*S); |
| 817 |
if (err > ATOL*TOL) { |
| 818 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 819 |
numerr++; |
| 820 |
} |
| 821 |
sout << " " << t << "|| S_in - X1*C1 - X2*C2 - S_out || : " << err << endl; |
| 822 |
} |
| 823 |
// <X1,S> == 0 |
| 824 |
if (theX.size() > 0 && theX[0] != null) { |
| 825 |
MT err = OM->orthogError(*theX[0],*S_outs[o]); |
| 826 |
if (err > TOL) { |
| 827 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 828 |
numerr++; |
| 829 |
} |
| 830 |
sout << " " << t << "|| <X[0],S> || after : " << err << endl; |
| 831 |
} |
| 832 |
// <X2,S> == 0 |
| 833 |
if (theX.size() > 1 && theX[1] != null) { |
| 834 |
MT err = OM->orthogError(*theX[1],*S_outs[o]); |
| 835 |
if (err > TOL) { |
| 836 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 837 |
numerr++; |
| 838 |
} |
| 839 |
sout << " " << t << "|| <X[1],S> || after : " << err << endl; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
// test all outputs for equivalence |
| 844 |
// check all combinations: |
| 845 |
// output 0 == output 1 |
| 846 |
// output 0 == output 2 |
| 847 |
// output 1 == output 2 |
| 848 |
for (unsigned int o1=0; o1<S_outs.size(); o1++) { |
| 849 |
for (unsigned int o2=o1+1; o2<S_outs.size(); o2++) { |
| 850 |
// check that S_outs[o1] == S_outs[o2] |
| 851 |
MT err = MVDiff(*S_outs[o1],*S_outs[o2]); |
| 852 |
if (err > TOL) { |
| 853 |
sout << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv tolerance exceeded! test failed!" << endl; |
| 854 |
numerr++; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
} |
| 860 |
catch (const OrthoError &e) { |
| 861 |
sout << " ------------------------------------------- project() threw exception" << endl; |
| 862 |
sout << " Error: " << e.what() << endl; |
| 863 |
numerr++; |
| 864 |
} |
| 865 |
|
| 866 |
} // test for |
| 867 |
|
| 868 |
MsgType type = Warnings; |
| 869 |
if (numerr>0) type = Errors; |
| 870 |
MyOM->stream(type) << sout.str(); |
| 871 |
MyOM->stream(type) << endl; |
| 872 |
|
| 873 |
return numerr; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
MT MVDiff(const MV &X, const MV &Y) { |
| 879 |
const ST ONE = SCT::one(); |
| 880 |
const int sizeX = MVT::GetNumberVecs(X); |
| 881 |
SerialDenseMatrix<int,ST> xTmx(sizeX,sizeX); |
| 882 |
|
| 883 |
// tmp <- X |
| 884 |
RCP<MV> tmp = MVT::CloneCopy(X); |
| 885 |
|
| 886 |
// tmp <- tmp - Y |
| 887 |
MVT::MvAddMv(-ONE,Y,ONE,*tmp,*tmp); |
| 888 |
|
| 889 |
MVT::MvTransMv(ONE,*tmp,*tmp,xTmx); |
| 890 |
MT err = 0; |
| 891 |
for (int i=0; i<sizeX; i++) { |
| 892 |
err += SCT::magnitude(xTmx(i,i)); |
| 893 |
} |
| 894 |
return SCT::magnitude(SCT::squareroot(err)); |
| 895 |
} |