ROL
vector/test_05.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 
49 #include "ROL_ScaledStdVector.hpp"
50 
51 #include "ROL_Stream.hpp"
52 #include "Teuchos_GlobalMPISession.hpp"
53 
54 typedef double RealT;
55 typedef double ElementT;
56 
57 int main(int argc, char *argv[]) {
58 
59  Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
60 
61  int iprint = argc - 1;
62  ROL::nullstream bhs; // outputs nothing
63  std::ostream& outStream = (iprint > 0) ? std::cout : bhs;
64 
65  int errorFlag = 0;
66 
67  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
68 
69  try {
70  // Dimension of the optimization vector
71 
72  int dim = 10;
73 
74  // Create Tpetra::MultiVectors (single vectors)
75  ROL::Ptr<std::vector<ElementT> > x_ptr
76  = ROL::makePtr<std::vector<ElementT>>(dim);
77  ROL::Ptr<std::vector<ElementT> > y_ptr
78  = ROL::makePtr<std::vector<ElementT>>(dim);
79  ROL::Ptr<std::vector<ElementT> > W_ptr
80  = ROL::makePtr<std::vector<ElementT>>(dim,static_cast<ElementT>(2));
81 
82  // Random elements
83  for (int i = 0; i < dim; i++) {
84  (*x_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
85  (*y_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
86  }
87 
88  // Create ROL vectors
91 
92  RealT xy = x.dot(y.dual());
93  RealT yx = y.dot(x.dual());
94  RealT axy = x.apply(y);
95  RealT ayx = y.apply(x);
96 
97  outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
98  << std::abs(xy-yx) << "\n";
99  outStream << "x.dot(y.dual()): " << xy << "\n";
100  outStream << "y.dot(x.dual()): " << yx << "\n";
101  if ( std::abs(xy-yx) > errtol ) {
102  outStream << "---> POSSIBLE ERROR ABOVE!\n";
103  errorFlag++;
104  }
105 
106  outStream << "\nAbsolute error between x.apply(y) and y.apply(x): "
107  << std::abs(axy-ayx) << "\n";
108  outStream << "x.apply(y): " << axy << "\n";
109  outStream << "y.apply(x): " << ayx << "\n";
110  if ( std::abs(axy-ayx) > errtol ) {
111  outStream << "---> POSSIBLE ERROR ABOVE!\n";
112  errorFlag++;
113  }
114 
115  outStream << "\nAbsolute error between x.apply(y) and x.dot(y.dual()): "
116  << std::abs(axy-ayx) << "\n";
117  outStream << "x.apply(y): " << axy << "\n";
118  outStream << "x.dot(y.dual()): " << xy << "\n";
119  if ( std::abs(axy-xy) > errtol ) {
120  outStream << "---> POSSIBLE ERROR ABOVE!\n";
121  errorFlag++;
122  }
123 
124  RealT xx = std::sqrt(x.dot(x)), xnorm = x.norm();
125  RealT yy = std::sqrt(y.dot(y)), ynorm = y.norm();
126 
127  outStream << "\nAbsolute error between sqrt(x.dot(x)) and x.norm(): "
128  << std::abs(xx-xnorm) << "\n";
129  outStream << "sqrt(x.dot(x)): " << xx << "\n";
130  outStream << "x.norm(): " << xnorm << "\n";
131  if ( std::abs(xx-xnorm) > errtol ) {
132  outStream << "---> POSSIBLE ERROR ABOVE!\n";
133  errorFlag++;
134  }
135 
136  outStream << "\nAbsolute error between sqrt(y.dot(y)) and y.norm(): "
137  << std::abs(yy-ynorm) << "\n";
138  outStream << "sqrt(y.dot(y)): " << yy << "\n";
139  outStream << "y.norm(): " << ynorm << "\n";
140  if ( std::abs(yy-ynorm) > errtol ) {
141  outStream << "---> POSSIBLE ERROR ABOVE!\n";
142  errorFlag++;
143  }
144 
145  // clone z from x, deep copy x into z, norm of z
146  ROL::Ptr<ROL::Vector<RealT> > z = x.clone();
147  z->set(x);
148  RealT znorm = z->norm();
149  outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n";
150  if ( std::abs(xnorm - znorm) > errtol ) {
151  outStream << "---> POSSIBLE ERROR ABOVE!\n";
152  errorFlag++;
153  }
154  ROL::Ptr<ROL::Vector<RealT> > w = y.clone();
155  w = y.clone();
156  w->set(y);
157  RealT wnorm = w->norm();
158  outStream << "\nNorm of ROL::Vector w (clone of y): " << wnorm << "\n";
159  if ( std::abs(ynorm - wnorm) > errtol ) {
160  outStream << "---> POSSIBLE ERROR ABOVE!\n";
161  errorFlag++;
162  }
163 
164  // Standard tests.
165  ROL::Ptr<std::vector<ElementT> > x1_ptr
166  = ROL::makePtr<std::vector<ElementT>>(dim);
167  ROL::Ptr<std::vector<ElementT> > y1_ptr
168  = ROL::makePtr<std::vector<ElementT>>(dim);
169  ROL::Ptr<std::vector<ElementT> > z1_ptr
170  = ROL::makePtr<std::vector<ElementT>>(dim);
171 
172  // Random elements
173  for (int i = 0; i < dim; i++) {
174  (*x1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
175  (*y1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
176  (*z1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
177  }
178 
179  // Create ROL vectors
183 
184  std::vector<RealT> consistency = x1.checkVector(y1, z1, true, outStream);
185  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
186  if (checkvec.norm() > std::sqrt(errtol)) {
187  errorFlag++;
188  }
189 
190  }
191 
192  catch (std::logic_error& err) {
193  outStream << err.what() << "\n";
194  errorFlag = -1000;
195  }; // end try
196 
197  if (errorFlag != 0)
198  std::cout << "End Result: TEST FAILED\n";
199  else
200  std::cout << "End Result: TEST PASSED\n";
201 
202  return 0;
203 }
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:325
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 .
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...
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
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 .