ROL
vector/test_03.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
50 #include "ROL_StdVector.hpp"
51 #include "ROL_Types.hpp"
52 
53 #include "Teuchos_oblackholestream.hpp"
54 #include "Teuchos_GlobalMPISession.hpp"
55 
56 #include <iomanip>
57 #include <iostream>
58 
59 
60 typedef double RealT;
61 
62 using namespace ROL;
63 using Teuchos::RCP;
64 using Teuchos::rcp;
65 using Teuchos::ArrayRCP;
66 
67 
68 template<class Real>
69 Real norm_sum(const MultiVector<Real> &A) {
70  int numVectors = A.getNumberOfVectors();
71  std::vector<RealT> norms(numVectors);
72  A.norms(norms);
73  Real sum = 0;
74  for(int i=0;i<numVectors;++i) {
75  sum += norms[i];
76  }
77  return sum;
78 }
79 
80 
81 int main(int argc, char *argv[]) {
82 
83  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
84 
85  int iprint = argc - 1;
86  Teuchos::RCP<std::ostream> outStream;
87  Teuchos::oblackholestream bhs; // outputs nothing
88  if (iprint > 0)
89  outStream = Teuchos::rcp(&std::cout, false);
90  else
91  outStream = Teuchos::rcp(&bhs, false);
92 
93  int errorFlag = 0;
94 
95  try {
96 
97  Teuchos::SerialDenseMatrix<int,RealT> M(2,2,true);
98  M(0,0) = 2.0;
99  M(0,1) = -1.0;
100  M(1,0) = -2.0;
101  M(1,1) = 3.0;
102 
103 
104  Teuchos::RCP<std::vector<RealT> > w_rcp = Teuchos::rcp(new std::vector<RealT>(2));
105  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp(new std::vector<RealT>(2));
106  Teuchos::RCP<std::vector<RealT> > y_rcp = Teuchos::rcp(new std::vector<RealT>(2));
107  Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp(new std::vector<RealT>(2));
108 
109  (*w_rcp)[0] = 0.0;
110  (*w_rcp)[1] = 1.0;
111 
112  (*x_rcp)[0] = 1.0;
113  (*x_rcp)[1] = 0.0;
114 
115  (*y_rcp)[0] = 3.0;
116  (*y_rcp)[1] = 4.0;
117 
118  (*z_rcp)[0] = -1.0;
119  (*z_rcp)[1] = 1.0;
120 
121  RCP<Vector<RealT> > w = rcp(new StdVector<RealT>(w_rcp));
122  RCP<Vector<RealT> > x = rcp(new StdVector<RealT>(x_rcp));
123  RCP<Vector<RealT> > y = rcp(new StdVector<RealT>(y_rcp));
124  RCP<Vector<RealT> > z = rcp(new StdVector<RealT>(z_rcp));
125 
126  ArrayRCP<RCP<Vector<RealT> > > A_rcp(2);
127  ArrayRCP<RCP<Vector<RealT> > > B_rcp(2);
128 
129  A_rcp[0] = x;
130  A_rcp[1] = y;
131 
132  B_rcp[0] = w;
133  B_rcp[1] = z;
134 
135  RCP<MultiVector<RealT> > A = rcp(new MultiVectorDefault<RealT>(A_rcp));
136  RCP<MultiVector<RealT> > B = rcp(new MultiVectorDefault<RealT>(B_rcp));
137 
138  // Test norm
139  if(static_cast<int>(norm_sum(*A)) != 6) {
140  *outStream << "Norm test failed!\n";
141  ++errorFlag;
142  }
143 
144  // Test clone
145  RCP<MultiVector<RealT> > C = A->clone();
146  if(norm_sum(*C) != 0) {
147  *outStream << "Clone test failed!\n";
148  ++errorFlag;
149  }
150 
151  // Test deep copy
152  RCP<MultiVector<RealT> > D = A->deepCopy();
153  if(static_cast<int>(norm_sum(*D)) != 6) {
154  *outStream << "Deep copy test failed!\n";
155  ++errorFlag;
156  }
157 
158  // Test shallow copy
159  std::vector<int> index(1);
160  index[0] = 0;
161 
162  RCP<MultiVector<RealT> > S = A->shallowCopy(index);
163  if(static_cast<int>(norm_sum(*S)) != 1) {
164  *outStream << "Shallow copy test failed!\n";
165  ++errorFlag;
166  }
167 
168  // Test scaling
169  std::vector<RealT> alpha(2);
170  alpha[0] = 4.0;
171  alpha[1] = 9.0;
172  A->scale(alpha);
173  if(static_cast<int>(norm_sum(*A)) != 49) {
174  *outStream << "Scaling test failed!\n";
175  ++errorFlag;
176  }
177 
178  // Test matrix multiplication
179  A->gemm(2.0,*B,M,1.0);
180  if(static_cast<int>(norm_sum(*A)) != 53) {
181  *outStream << "Matmat multiply test failed! The norm_sum is " << static_cast<int>(norm_sum(*A)) << ", not equal to 49.\n";
182  ++errorFlag;
183  }
184 
185  // Test set
186  A->set(*B);
187  if(static_cast<int>(norm_sum(*A)) != 2) {
188  *outStream << "Set test failed!\n";
189  ++errorFlag;
190  }
191 
192  // Test innerProducts
193  Teuchos::SerialDenseMatrix<int,RealT> P(2,2,true);
194  B->innerProducts(1.0,*B,P);
195  Teuchos::SerialDenseMatrix<int,RealT> Check(2,2,true);
196  Check(0,0) = 1.0;
197  Check(0,1) = 1.0;
198  Check(1,0) = 1.0;
199  Check(1,1) = 2.0;
200  if( P != Check ) {
201  *outStream << "Inner product test failed!\n";
202  ++errorFlag;
203  }
204 
205  // Test dot products
206  std::vector<RealT> dots(2);
207  D->dots(*D,dots);
208  if(static_cast<int>(dots[0]) != 1 ||
209  static_cast<int>(dots[1]) != 25 ) {
210  *outStream << "Dot product test failed!\n";
211  ++errorFlag;
212  }
213 
214 // StdVector<RealT> d0 = Teuchos::dyn_cast<StdVector<RealT>>(*D->getVector(0));
215 // StdVector<RealT> d1 = Teuchos::dyn_cast<StdVector<RealT>>(*D->getVector(1));
216 
217 // std::cout << (*d0.getVector())[0] << std::endl;
218 // std::cout << (*d0.getVector())[1] << std::endl;
219 // std::cout << (*d1.getVector())[0] << std::endl;
220 // std::cout << (*d1.getVector())[1] << std::endl;
221 
222  }
223  catch(std::logic_error err) {
224  *outStream << err.what() << "\n";
225  errorFlag = -1000;
226  }; // end try
227 
228  if (errorFlag != 0) {
229  std::cout << "End Result: TEST FAILED\n";
230  }
231  else {
232  std::cout << "End Result: TEST PASSED\n";
233  }
234 
235  return 0;
236 }
237 
virtual void norms(std::vector< Real > &normvec) const =0
Compute the norm of each vector in the MultiVector.
Contains definitions of custom data types in ROL.
Provides the std::vector implementation of the ROL::Vector interface.
Real norm_sum(const MultiVector< Real > &A)
Default implementation of the ROL::MultiVector container class.
virtual int getNumberOfVectors() const =0
Get the number of vectors in the MultiVector.
Provides a container and operations on multiple ROL vectors for use with other Trilinos packages whic...
int main(int argc, char *argv[])
double RealT