ROL
function/test_12.cpp
Go to the documentation of this file.
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 
17 #include "ROL_StdVector.hpp"
18 #include "ROL_RandomVector.hpp"
19 #include "ROL_Stream.hpp"
20 #include "Teuchos_GlobalMPISession.hpp"
21 
22 template<class Real>
23 void printVector( const ROL::Vector<Real> &x, std::ostream &outStream ) {
24 
25  ROL::Ptr<const std::vector<Real> > xp =
26  dynamic_cast<const ROL::StdVector<Real>&>(x).getVector();
27 
28  outStream << "Standard Vector" << std::endl;
29  for( size_t i=0; i<xp->size(); ++i ) {
30  outStream << (*xp)[i] << std::endl;
31  }
32 }
33 
34 
35 
36 typedef double RealT;
37 
38 int main(int argc, char *argv[]) {
39 
40  typedef ROL::Vector<RealT> V;
41  typedef ROL::StdVector<RealT> SV;
42 
43 
44 
45  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
46 
47  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
48  int iprint = argc - 1;
49  ROL::Ptr<std::ostream> outStream;
50  ROL::nullstream bhs; // outputs nothing
51  if (iprint > 0)
52  outStream = ROL::makePtrFromRef(std::cout);
53  else
54  outStream = ROL::makePtrFromRef(bhs);
55 
56  int errorFlag = 0;
57 
58  try {
59 
60  int dim = 10;
61 
62  RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
63 
64  ROL::Ptr<V> v = ROL::makePtr<SV>( ROL::makePtr<std::vector<RealT>>(dim) );
65  ROL::Ptr<V> Hv = v->clone();
66  ROL::Ptr<V> HHv = v->clone();
67 
68  ROL::Ptr<V> e0 = v->basis(0);
69 
70  RandomizeVector(*v);
71 
73 
74  // Reflect v about a vector to the x direction
75  // Verify that the result is parallel to x
76 
77  printVector(*v,*outStream);
78 
79  H.apply(*Hv, *v, tol);
80 
81  printVector(*Hv,*outStream);
82 
83  H.apply(*HHv, *Hv, tol);
84 
85  printVector(*HHv,*outStream);
86 
87  }
88 
89  catch (std::logic_error& err) {
90  *outStream << err.what() << "\n";
91  errorFlag = -1000;
92  }; // end try
93 
94  if (errorFlag != 0)
95  std::cout << "End Result: TEST FAILED\n";
96  else
97  std::cout << "End Result: TEST PASSED\n";
98 
99  return 0;
100 }
101 
102 
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides the interface to create a Householder reflector operator, that when applied to a vector x...
Vector< Real > V
void printVector(const ROL::Vector< Real > &x, std::ostream &outStream)
void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply linear operator.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
constexpr auto dim