ROL
vector/test_05.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 
15 #include "ROL_ScaledStdVector.hpp"
16 
17 #include "ROL_Stream.hpp"
18 #include "Teuchos_GlobalMPISession.hpp"
19 
20 typedef double RealT;
21 typedef double ElementT;
22 
23 int main(int argc, char *argv[]) {
24 
25  Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
26 
27  int iprint = argc - 1;
28  ROL::nullstream bhs; // outputs nothing
29  std::ostream& outStream = (iprint > 0) ? std::cout : bhs;
30 
31  int errorFlag = 0;
32 
33  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
34 
35  try {
36  // Dimension of the optimization vector
37 
38  int dim = 10;
39 
40  // Create Tpetra::MultiVectors (single vectors)
41  ROL::Ptr<std::vector<ElementT> > x_ptr
42  = ROL::makePtr<std::vector<ElementT>>(dim);
43  ROL::Ptr<std::vector<ElementT> > y_ptr
44  = ROL::makePtr<std::vector<ElementT>>(dim);
45  ROL::Ptr<std::vector<ElementT> > W_ptr
46  = ROL::makePtr<std::vector<ElementT>>(dim,static_cast<ElementT>(2));
47 
48  // Random elements
49  for (int i = 0; i < dim; i++) {
50  (*x_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
51  (*y_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
52  }
53 
54  // Create ROL vectors
57 
58  RealT xy = x.dot(y.dual());
59  RealT yx = y.dot(x.dual());
60  RealT axy = x.apply(y);
61  RealT ayx = y.apply(x);
62 
63  outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
64  << std::abs(xy-yx) << "\n";
65  outStream << "x.dot(y.dual()): " << xy << "\n";
66  outStream << "y.dot(x.dual()): " << yx << "\n";
67  if ( std::abs(xy-yx) > errtol ) {
68  outStream << "---> POSSIBLE ERROR ABOVE!\n";
69  errorFlag++;
70  }
71 
72  outStream << "\nAbsolute error between x.apply(y) and y.apply(x): "
73  << std::abs(axy-ayx) << "\n";
74  outStream << "x.apply(y): " << axy << "\n";
75  outStream << "y.apply(x): " << ayx << "\n";
76  if ( std::abs(axy-ayx) > errtol ) {
77  outStream << "---> POSSIBLE ERROR ABOVE!\n";
78  errorFlag++;
79  }
80 
81  outStream << "\nAbsolute error between x.apply(y) and x.dot(y.dual()): "
82  << std::abs(axy-ayx) << "\n";
83  outStream << "x.apply(y): " << axy << "\n";
84  outStream << "x.dot(y.dual()): " << xy << "\n";
85  if ( std::abs(axy-xy) > errtol ) {
86  outStream << "---> POSSIBLE ERROR ABOVE!\n";
87  errorFlag++;
88  }
89 
90  RealT xx = std::sqrt(x.dot(x)), xnorm = x.norm();
91  RealT yy = std::sqrt(y.dot(y)), ynorm = y.norm();
92 
93  outStream << "\nAbsolute error between sqrt(x.dot(x)) and x.norm(): "
94  << std::abs(xx-xnorm) << "\n";
95  outStream << "sqrt(x.dot(x)): " << xx << "\n";
96  outStream << "x.norm(): " << xnorm << "\n";
97  if ( std::abs(xx-xnorm) > errtol ) {
98  outStream << "---> POSSIBLE ERROR ABOVE!\n";
99  errorFlag++;
100  }
101 
102  outStream << "\nAbsolute error between sqrt(y.dot(y)) and y.norm(): "
103  << std::abs(yy-ynorm) << "\n";
104  outStream << "sqrt(y.dot(y)): " << yy << "\n";
105  outStream << "y.norm(): " << ynorm << "\n";
106  if ( std::abs(yy-ynorm) > errtol ) {
107  outStream << "---> POSSIBLE ERROR ABOVE!\n";
108  errorFlag++;
109  }
110 
111  // clone z from x, deep copy x into z, norm of z
112  ROL::Ptr<ROL::Vector<RealT> > z = x.clone();
113  z->set(x);
114  RealT znorm = z->norm();
115  outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n";
116  if ( std::abs(xnorm - znorm) > errtol ) {
117  outStream << "---> POSSIBLE ERROR ABOVE!\n";
118  errorFlag++;
119  }
120  ROL::Ptr<ROL::Vector<RealT> > w = y.clone();
121  w = y.clone();
122  w->set(y);
123  RealT wnorm = w->norm();
124  outStream << "\nNorm of ROL::Vector w (clone of y): " << wnorm << "\n";
125  if ( std::abs(ynorm - wnorm) > errtol ) {
126  outStream << "---> POSSIBLE ERROR ABOVE!\n";
127  errorFlag++;
128  }
129 
130  // Standard tests.
131  ROL::Ptr<std::vector<ElementT> > x1_ptr
132  = ROL::makePtr<std::vector<ElementT>>(dim);
133  ROL::Ptr<std::vector<ElementT> > y1_ptr
134  = ROL::makePtr<std::vector<ElementT>>(dim);
135  ROL::Ptr<std::vector<ElementT> > z1_ptr
136  = ROL::makePtr<std::vector<ElementT>>(dim);
137 
138  // Random elements
139  for (int i = 0; i < dim; i++) {
140  (*x1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
141  (*y1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
142  (*z1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
143  }
144 
145  // Create ROL vectors
149 
150  std::vector<RealT> consistency = x1.checkVector(y1, z1, true, outStream);
151  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
152  if (checkvec.norm() > std::sqrt(errtol)) {
153  errorFlag++;
154  }
155 
156  }
157 
158  catch (std::logic_error& err) {
159  outStream << err.what() << "\n";
160  errorFlag = -1000;
161  }; // end try
162 
163  if (errorFlag != 0)
164  std::cout << "End Result: TEST FAILED\n";
165  else
166  std::cout << "End Result: TEST PASSED\n";
167 
168  return 0;
169 }
double ElementT
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
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 dot(const Vector< Real > &x) const
Compute where .
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
Real dot(const Vector< Real > &x) const
Compute where .
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
int main(int argc, char *argv[])
const Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
const Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
constexpr auto dim
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .