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