| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/*! \file test_03.cpp |
| 11 |
\brief Test MultiVector interface. |
| 12 |
|
| 13 |
*/ |
| 14 |
|
| 15 |
#include "ROL_MultiVectorDefault.hpp" |
| 16 |
#include "ROL_StdVector.hpp" |
| 17 |
#include "ROL_Types.hpp" |
| 18 |
|
| 19 |
#include "ROL_Stream.hpp" |
| 20 |
#include "Teuchos_GlobalMPISession.hpp" |
| 21 |
|
| 22 |
#include <iomanip> |
| 23 |
#include <iostream> |
| 24 |
|
| 25 |
|
| 26 |
typedef double RealT; |
| 27 |
|
| 28 |
using namespace ROL; |
| 29 |
|
| 30 |
|
| 31 |
using Teuchos::ArrayRCP; |
| 32 |
|
| 33 |
|
| 34 |
template<class Real> |
| 35 |
Real norm_sum(const MultiVector<Real> &A) { |
| 36 |
int numVectors = A.getNumberOfVectors(); |
| 37 |
std::vector<RealT> norms(numVectors); |
| 38 |
A.norms(norms); |
| 39 |
Real sum = 0; |
| 40 |
for(int i=0;i<numVectors;++i) { |
| 41 |
sum += norms[i]; |
| 42 |
} |
| 43 |
return sum; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
int main(int argc, char *argv[]) { |
| 48 |
|
| 49 |
Teuchos::GlobalMPISession mpiSession(&argc,&argv); |
| 50 |
|
| 51 |
int iprint = argc - 1; |
| 52 |
ROL::Ptr<std::ostream> outStream; |
| 53 |
ROL::nullstream bhs; // outputs nothing |
| 54 |
if (iprint > 0) |
| 55 |
outStream = ROL::makePtrFromRef(std::cout); |
| 56 |
else |
| 57 |
outStream = ROL::makePtrFromRef(bhs); |
| 58 |
|
| 59 |
int errorFlag = 0; |
| 60 |
|
| 61 |
try { |
| 62 |
|
| 63 |
Teuchos::SerialDenseMatrix<int,RealT> M(2,2,true); |
| 64 |
M(0,0) = 2.0; |
| 65 |
M(0,1) = -1.0; |
| 66 |
M(1,0) = -2.0; |
| 67 |
M(1,1) = 3.0; |
| 68 |
|
| 69 |
|
| 70 |
ROL::Ptr<std::vector<RealT> > w_ptr = ROL::makePtr<std::vector<RealT>>(2); |
| 71 |
ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(2); |
| 72 |
ROL::Ptr<std::vector<RealT> > y_ptr = ROL::makePtr<std::vector<RealT>>(2); |
| 73 |
ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(2); |
| 74 |
|
| 75 |
(*w_ptr)[0] = 0.0; |
| 76 |
(*w_ptr)[1] = 1.0; |
| 77 |
|
| 78 |
(*x_ptr)[0] = 1.0; |
| 79 |
(*x_ptr)[1] = 0.0; |
| 80 |
|
| 81 |
(*y_ptr)[0] = 3.0; |
| 82 |
(*y_ptr)[1] = 4.0; |
| 83 |
|
| 84 |
(*z_ptr)[0] = -1.0; |
| 85 |
(*z_ptr)[1] = 1.0; |
| 86 |
|
| 87 |
ROL::Ptr<Vector<RealT> > w = ROL::makePtr<StdVector<RealT>>(w_ptr); |
| 88 |
ROL::Ptr<Vector<RealT> > x = ROL::makePtr<StdVector<RealT>>(x_ptr); |
| 89 |
ROL::Ptr<Vector<RealT> > y = ROL::makePtr<StdVector<RealT>>(y_ptr); |
| 90 |
ROL::Ptr<Vector<RealT> > z = ROL::makePtr<StdVector<RealT>>(z_ptr); |
| 91 |
|
| 92 |
ArrayRCP<ROL::Ptr<Vector<RealT> > > A_ptr(2); |
| 93 |
ArrayRCP<ROL::Ptr<Vector<RealT> > > B_ptr(2); |
| 94 |
|
| 95 |
A_ptr[0] = x; |
| 96 |
A_ptr[1] = y; |
| 97 |
|
| 98 |
B_ptr[0] = w; |
| 99 |
B_ptr[1] = z; |
| 100 |
|
| 101 |
ROL::Ptr<MultiVector<RealT> > A = ROL::makePtr<MultiVectorDefault<RealT>>(A_ptr); |
| 102 |
ROL::Ptr<MultiVector<RealT> > B = ROL::makePtr<MultiVectorDefault<RealT>>(B_ptr); |
| 103 |
|
| 104 |
// Test norm |
| 105 |
if(static_cast<int>(norm_sum(*A)) != 6) { |
| 106 |
*outStream << "Norm test failed!\n"; |
| 107 |
++errorFlag; |
| 108 |
} |
| 109 |
|
| 110 |
// Test clone |
| 111 |
ROL::Ptr<MultiVector<RealT> > C = A->clone(); |
| 112 |
if(norm_sum(*C) != 0) { |
| 113 |
*outStream << "Clone test failed!\n"; |
| 114 |
++errorFlag; |
| 115 |
} |
| 116 |
|
| 117 |
// Test deep copy |
| 118 |
ROL::Ptr<MultiVector<RealT> > D = A->deepCopy(); |
| 119 |
if(static_cast<int>(norm_sum(*D)) != 6) { |
| 120 |
*outStream << "Deep copy test failed!\n"; |
| 121 |
++errorFlag; |
| 122 |
} |
| 123 |
|
| 124 |
// Test shallow copy |
| 125 |
std::vector<int> index(1); |
| 126 |
index[0] = 0; |
| 127 |
|
| 128 |
ROL::Ptr<MultiVector<RealT> > S = A->shallowCopy(index); |
| 129 |
if(static_cast<int>(norm_sum(*S)) != 1) { |
| 130 |
*outStream << "Shallow copy test failed!\n"; |
| 131 |
++errorFlag; |
| 132 |
} |
| 133 |
|
| 134 |
// Test scaling |
| 135 |
std::vector<RealT> alpha(2); |
| 136 |
alpha[0] = 4.0; |
| 137 |
alpha[1] = 9.0; |
| 138 |
A->scale(alpha); |
| 139 |
if(static_cast<int>(norm_sum(*A)) != 49) { |
| 140 |
*outStream << "Scaling test failed!\n"; |
| 141 |
++errorFlag; |
| 142 |
} |
| 143 |
|
| 144 |
// Test matrix multiplication |
| 145 |
A->gemm(2.0,*B,M,1.0); |
| 146 |
if(static_cast<int>(norm_sum(*A)) != 53) { |
| 147 |
*outStream << "Matmat multiply test failed! The norm_sum is " << static_cast<int>(norm_sum(*A)) << ", not equal to 49.\n"; |
| 148 |
++errorFlag; |
| 149 |
} |
| 150 |
|
| 151 |
// Test set |
| 152 |
A->set(*B); |
| 153 |
if(static_cast<int>(norm_sum(*A)) != 2) { |
| 154 |
*outStream << "Set test failed!\n"; |
| 155 |
++errorFlag; |
| 156 |
} |
| 157 |
|
| 158 |
// Test innerProducts |
| 159 |
Teuchos::SerialDenseMatrix<int,RealT> P(2,2,true); |
| 160 |
B->innerProducts(1.0,*B,P); |
| 161 |
Teuchos::SerialDenseMatrix<int,RealT> Check(2,2,true); |
| 162 |
Check(0,0) = 1.0; |
| 163 |
Check(0,1) = 1.0; |
| 164 |
Check(1,0) = 1.0; |
| 165 |
Check(1,1) = 2.0; |
| 166 |
if( P != Check ) { |
| 167 |
*outStream << "Inner product test failed!\n"; |
| 168 |
++errorFlag; |
| 169 |
} |
| 170 |
|
| 171 |
// Test dot products |
| 172 |
std::vector<RealT> dots(2); |
| 173 |
D->dots(*D,dots); |
| 174 |
if(static_cast<int>(dots[0]) != 1 || |
| 175 |
static_cast<int>(dots[1]) != 25 ) { |
| 176 |
*outStream << "Dot product test failed!\n"; |
| 177 |
++errorFlag; |
| 178 |
} |
| 179 |
|
| 180 |
// StdVector<RealT> d0 = dynamic_cast<StdVector<RealT>>(*D-&>getVector(0)); |
| 181 |
// StdVector<RealT> d1 = dynamic_cast<StdVector<RealT>>(*D-&>getVector(1)); |
| 182 |
|
| 183 |
// std::cout << (*d0.getVector())[0] << std::endl; |
| 184 |
// std::cout << (*d0.getVector())[1] << std::endl; |
| 185 |
// std::cout << (*d1.getVector())[0] << std::endl; |
| 186 |
// std::cout << (*d1.getVector())[1] << std::endl; |
| 187 |
|
| 188 |
} |
| 189 |
catch(std::logic_error& err) { |
| 190 |
*outStream << err.what() << "\n"; |
| 191 |
errorFlag = -1000; |
| 192 |
}; // end try |
| 193 |
|
| 194 |
if (errorFlag != 0) { |
| 195 |
std::cout << "End Result: TEST FAILED\n"; |
| 196 |
} |
| 197 |
else { |
| 198 |
std::cout << "End Result: TEST PASSED\n"; |
| 199 |
} |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|