ROL
vector/test_01.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_StdVector.hpp"
15 #include "ROL_Types.hpp"
16 #include "ROL_Stream.hpp"
17 #include "Teuchos_GlobalMPISession.hpp"
18 
19 #include <iostream>
20 
21 typedef double RealT;
22 typedef double ElementT;
23 
24 int main(int argc, char *argv[]) {
25 
26  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
27 
28  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29  int iprint = argc - 1;
30  ROL::Ptr<std::ostream> outStream;
31  ROL::nullstream bhs; // outputs nothing
32  if (iprint > 0)
33  outStream = ROL::makePtrFromRef(std::cout);
34  else
35  outStream = ROL::makePtrFromRef(bhs);
36 
37  int errorFlag = 0;
38 
39  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
40 
41  // *** Test body.
42 
43  try {
44 
45  // Number of elements in StdVector objects
46  int dim = 100;
47 
51 
52  // Lower and upper bounds on randomized vector element values
53  RealT left = -1e0, right = 1e0;
54 
55  // set x,y,z
56  x.randomize(left,right);
57  y.randomize(left,right);
58  z.randomize(left,right);
59 
60  // Standard tests.
61  auto consistency = x.checkVector(y, z, true, *outStream);
62 
63  ROL::StdVector<RealT, ElementT> 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.basis(0);
70  RealT znorm = zp->norm();
71  *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
72  if ( std::abs(znorm-1.0) > errtol ) {
73  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
74  errorFlag++;
75  };
76 
77  // set x to middle basis vector
78  zp = x.basis(dim/2);
79  znorm = zp->norm();
80  *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
81 
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 
104  catch (std::logic_error& err) {
105  *outStream << err.what() << "\n";
106  errorFlag = -1000;
107  }; // end try
108 
109  if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n";
110  else std::cout << "End Result: TEST PASSED\n";
111 
112  return 0;
113 
114 }
115 
double ElementT
void scale(const Real alpha)
Compute where .
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Contains definitions of custom data types in ROL.
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...
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
constexpr auto dim