ROL
vector/test_11.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 
14 #include "ROL_StdArray.hpp"
15 #include "ROL_StdVector.hpp"
16 #include "ROL_Stream.hpp"
17 #include "Teuchos_GlobalMPISession.hpp"
18 
19 #include <iostream>
20 
21 using RealT = double;
22 
23 constexpr auto dim = 100u;
24 
25 int main(int argc, char *argv[]) {
26 
27  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
28 
30 
31  int iprint = argc - 1;
32  ROL::Ptr<std::ostream> outStream;
33  ROL::nullstream bhs; // outputs nothing
34  if (iprint > 0) outStream = ROL::makePtrFromRef(std::cout);
35  else outStream = ROL::makePtrFromRef(bhs);
36 
37  auto print_pool_count = [&outStream]() {
38  *outStream << "Currently using " << ROL::StdArray<RealT, dim>::pool_count()
39  << " Vectors from the pool" << std::endl;
40  };
41 
42  int errorFlag = 0;
43 
44  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
45 
46  // *** Test body.
47 
48  print_pool_count();
49 
50  try {
51 
52 
54 
55  print_pool_count();
56 
57  x.randomize();
58  y.randomize();
59  z.randomize();
60 
61  // Standard tests.
62  auto consistency = x.checkVector(y, z, true, *outStream);
63  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
64 
65  if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON<RealT>())) errorFlag++;
66 
67  // Basis tests.
68  // set x to first basis vector
69  auto zp = x.clone();
70  zp = x.basis(0);
71  RealT znorm = zp->norm();
72  *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
73  if ( std::abs(znorm-1.0) > errtol ) {
74  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
75  errorFlag++;
76  };
77 
78  // set x to middle basis vector
79  zp = x.basis(dim/2);
80  znorm = zp->norm();
81  *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
82  if ( std::abs(znorm-1.0) > errtol ) {
83  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
84  errorFlag++;
85  };
86 
87  // set x to last basis vector
88  zp = x.basis(dim-1);
89  znorm = zp->norm();
90  *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
91  if ( std::abs(znorm-1.0) > errtol ) {
92  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
93  errorFlag++;
94  };
95 
96  // Repeat the checkVector tests with a zero vector.
97  x.scale(0.0);
98  consistency = x.checkVector(x, x, true, *outStream);
99  if (checkvec.norm() > 0.0) {
100  errorFlag++;
101  }
102 
103  print_pool_count();
104  }
105  catch (std::logic_error& err) {
106  *outStream << err.what() << "\n";
107  errorFlag = -1000;
108  }; // end try
109 
110  print_pool_count();
111 
112  if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n";
113  else std::cout << "End Result: TEST PASSED\n";
114 
115 
116  return 0;
117 
118 }
119 
static std::size_t pool_count()
virtual std::vector< Real > checkVector(const Vector< Real > &x, const Vector< Real > &y, const bool printToStream=true, std::ostream &outStream=std::cout) const
Verify vector-space methods.
Definition: ROL_Vector.hpp:291
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the std::array implementation of the ROL::Vector interface.
void scale(const Real alpha)
Compute where .
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
static void initialize_pool()
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
void randomize(const Real l=-1.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
constexpr auto dim