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 
95  outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
96  << std::abs(xy-yx) << "\n";
97  outStream << "x.dot(y.dual()): " << xy << "\n";
98  outStream << "y.dot(x.dual()): " << yx << "\n";
99  if ( std::abs(xy-yx) > errtol ) {
100  outStream << "---> POSSIBLE ERROR ABOVE!\n";
101  errorFlag++;
102  }
103 
104  RealT xx = std::sqrt(x.dot(x)), xnorm = x.norm();
105  RealT yy = std::sqrt(y.dot(y)), ynorm = y.norm();
106 
107  outStream << "\nAbsolute error between sqrt(x.dot(x)) and x.norm(): "
108  << std::abs(xx-xnorm) << "\n";
109  outStream << "sqrt(x.dot(x)): " << xx << "\n";
110  outStream << "x.norm(): " << xnorm << "\n";
111  if ( std::abs(xx-xnorm) > errtol ) {
112  outStream << "---> POSSIBLE ERROR ABOVE!\n";
113  errorFlag++;
114  }
115 
116  outStream << "\nAbsolute error between sqrt(y.dot(y)) and y.norm(): "
117  << std::abs(yy-ynorm) << "\n";
118  outStream << "sqrt(y.dot(y)): " << yy << "\n";
119  outStream << "y.norm(): " << ynorm << "\n";
120  if ( std::abs(yy-ynorm) > errtol ) {
121  outStream << "---> POSSIBLE ERROR ABOVE!\n";
122  errorFlag++;
123  }
124 
125  // clone z from x, deep copy x into z, norm of z
126  ROL::Ptr<ROL::Vector<RealT> > z = x.clone();
127  z->set(x);
128  RealT znorm = z->norm();
129  outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n";
130  if ( std::abs(xnorm - znorm) > errtol ) {
131  outStream << "---> POSSIBLE ERROR ABOVE!\n";
132  errorFlag++;
133  }
134  ROL::Ptr<ROL::Vector<RealT> > w = y.clone();
135  w = y.clone();
136  w->set(y);
137  RealT wnorm = w->norm();
138  outStream << "\nNorm of ROL::Vector w (clone of y): " << wnorm << "\n";
139  if ( std::abs(ynorm - wnorm) > errtol ) {
140  outStream << "---> POSSIBLE ERROR ABOVE!\n";
141  errorFlag++;
142  }
143 
144  // Standard tests.
145  ROL::Ptr<std::vector<ElementT> > x1_ptr
146  = ROL::makePtr<std::vector<ElementT>>(dim);
147  ROL::Ptr<std::vector<ElementT> > y1_ptr
148  = ROL::makePtr<std::vector<ElementT>>(dim);
149  ROL::Ptr<std::vector<ElementT> > z1_ptr
150  = ROL::makePtr<std::vector<ElementT>>(dim);
151 
152  // Random elements
153  for (int i = 0; i < dim; i++) {
154  (*x1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
155  (*y1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
156  (*z1_ptr)[i] = static_cast<ElementT>(rand())/static_cast<ElementT>(RAND_MAX);
157  }
158 
159  // Create ROL vectors
163 
164  std::vector<RealT> consistency = x1.checkVector(y1, z1, true, outStream);
165  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
166  if (checkvec.norm() > std::sqrt(errtol)) {
167  errorFlag++;
168  }
169 
170  }
171 
172  catch (std::logic_error& err) {
173  outStream << err.what() << "\n";
174  errorFlag = -1000;
175  }; // end try
176 
177  if (errorFlag != 0)
178  std::cout << "End Result: TEST FAILED\n";
179  else
180  std::cout << "End Result: TEST PASSED\n";
181 
182  return 0;
183 }
double ElementT
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:312
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