ROL
vector/test_07.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_StdVector.hpp"
50 #include "ROL_RieszVector.hpp"
51 #include "ROL_RandomVector.hpp"
52 
53 #include "ROL_DiagonalOperator.hpp"
54 
55 #include "ROL_Stream.hpp"
56 #include "Teuchos_GlobalMPISession.hpp"
57 
58 typedef double RealT;
59 
60 int main(int argc, char *argv[]) {
61 
62 
63 
64  typedef std::vector<RealT> vector;
65  typedef ROL::Vector<RealT> V;
66  typedef ROL::StdVector<RealT> SV;
67 
68  typedef ROL::LinearOperator<RealT> LinearOperator;
69  typedef ROL::DiagonalOperator<RealT> DiagonalOperator;
70 
71  typedef ROL::RieszPrimalVector<RealT> PrimalVector;
72  typedef ROL::RieszDualVector<RealT> DualVector;
73 
74 
75  Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
76 
77  int iprint = argc - 1;
78  ROL::nullstream bhs; // outputs nothing
79  ROL::Ptr<std::ostream> outStream;
80  if (iprint > 0)
81  outStream = ROL::makePtrFromRef(std::cout);
82  else
83  outStream = ROL::makePtrFromRef(bhs);
84 
85  int errorFlag = 0;
86 
87  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
88 
89  try {
90  // Dimension of the optimization vector
91 
92  int dim = 10;
93 
94  // Create Tpetra::MultiVectors (single vectors)
95  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim);
96  ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(dim);
97  ROL::Ptr<vector> w_ptr = ROL::makePtr<vector>(dim,2.0);
98 
99  ROL::Ptr<V> xs = ROL::makePtr<SV>( x_ptr );
100  ROL::Ptr<V> ys = ROL::makePtr<SV>( y_ptr );
101 
102  SV ws( w_ptr );
103 
106 
107  ROL::Ptr<LinearOperator> W = ROL::makePtr<DiagonalOperator>(ws);
108 
109  PrimalVector x(xs,W);
110  DualVector y(ys,W);
111 
112  RealT xy = x.dot(y.dual());
113  RealT yx = y.dot(x.dual());
114 
115  *outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
116  << std::abs(xy-yx) << "\n";
117  *outStream << "x.dot(y.dual()): " << xy << "\n";
118  *outStream << "y.dot(x.dual()): " << yx << "\n";
119  if ( std::abs(xy-yx) > 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<vector> x1_ptr = ROL::makePtr<vector>(dim);
166  ROL::Ptr<vector> y1_ptr = ROL::makePtr<vector>(dim);
167  ROL::Ptr<vector> z1_ptr = ROL::makePtr<vector>(dim);
168 
169  ROL::Ptr<V> x1s = ROL::makePtr<SV>( x1_ptr );
170  ROL::Ptr<V> y1s = ROL::makePtr<SV>( y1_ptr );
171  ROL::Ptr<V> z1s = ROL::makePtr<SV>( z1_ptr );
172 
173  ROL::RandomizeVector(*x1s);
174  ROL::RandomizeVector(*y1s);
175  ROL::RandomizeVector(*z1s);
176 
177  // Create ROL vectors
178  PrimalVector x1(x1s,W);
179  PrimalVector y1(y1s,W);
180  PrimalVector z1(z1s,W);
181 
182  std::vector<RealT> consistency = x1.checkVector(y1, z1, true, *outStream);
183  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
184  if (checkvec.norm() > std::sqrt(errtol)) {
185  errorFlag++;
186  }
187 
188  }
189 
190  catch (std::logic_error& err) {
191  *outStream << err.what() << "\n";
192  errorFlag = -1000;
193  }; // end try
194 
195  if (errorFlag != 0)
196  std::cout << "End Result: TEST FAILED\n";
197  else
198  std::cout << "End Result: TEST PASSED\n";
199 
200  return 0;
201 }
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,upper].
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides the interface to apply a diagonal operator which acts like elementwise multiplication when a...
Vector< Real > V
Real norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides the interface to apply a linear operator.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
constexpr auto dim