ROL
vector/test_07.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_StdVector.hpp"
16 #include "ROL_RieszVector.hpp"
17 #include "ROL_RandomVector.hpp"
18 
19 #include "ROL_DiagonalOperator.hpp"
20 
21 #include "ROL_Stream.hpp"
22 #include "Teuchos_GlobalMPISession.hpp"
23 
24 typedef double RealT;
25 
26 int main(int argc, char *argv[]) {
27 
28 
29 
30  typedef std::vector<RealT> vector;
31  typedef ROL::Vector<RealT> V;
32  typedef ROL::StdVector<RealT> SV;
33 
34  typedef ROL::LinearOperator<RealT> LinearOperator;
35  typedef ROL::DiagonalOperator<RealT> DiagonalOperator;
36 
37  typedef ROL::RieszPrimalVector<RealT> PrimalVector;
38  typedef ROL::RieszDualVector<RealT> DualVector;
39 
40 
41  Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
42 
43  int iprint = argc - 1;
44  ROL::nullstream bhs; // outputs nothing
45  ROL::Ptr<std::ostream> outStream;
46  if (iprint > 0)
47  outStream = ROL::makePtrFromRef(std::cout);
48  else
49  outStream = ROL::makePtrFromRef(bhs);
50 
51  int errorFlag = 0;
52 
53  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
54 
55  try {
56  // Dimension of the optimization vector
57 
58  int dim = 10;
59 
60  // Create Tpetra::MultiVectors (single vectors)
61  ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(dim);
62  ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(dim);
63  ROL::Ptr<vector> w_ptr = ROL::makePtr<vector>(dim,2.0);
64 
65  ROL::Ptr<V> xs = ROL::makePtr<SV>( x_ptr );
66  ROL::Ptr<V> ys = ROL::makePtr<SV>( y_ptr );
67 
68  SV ws( w_ptr );
69 
72 
73  ROL::Ptr<LinearOperator> W = ROL::makePtr<DiagonalOperator>(ws);
74 
75  PrimalVector x(xs,W);
76  DualVector y(ys,W);
77 
78  RealT xy = x.dot(y.dual());
79  RealT yx = y.dot(x.dual());
80 
81  *outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
82  << std::abs(xy-yx) << "\n";
83  *outStream << "x.dot(y.dual()): " << xy << "\n";
84  *outStream << "y.dot(x.dual()): " << yx << "\n";
85  if ( std::abs(xy-yx) > 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<vector> x1_ptr = ROL::makePtr<vector>(dim);
132  ROL::Ptr<vector> y1_ptr = ROL::makePtr<vector>(dim);
133  ROL::Ptr<vector> z1_ptr = ROL::makePtr<vector>(dim);
134 
135  ROL::Ptr<V> x1s = ROL::makePtr<SV>( x1_ptr );
136  ROL::Ptr<V> y1s = ROL::makePtr<SV>( y1_ptr );
137  ROL::Ptr<V> z1s = ROL::makePtr<SV>( z1_ptr );
138 
139  ROL::RandomizeVector(*x1s);
140  ROL::RandomizeVector(*y1s);
141  ROL::RandomizeVector(*z1s);
142 
143  // Create ROL vectors
144  PrimalVector x1(x1s,W);
145  PrimalVector y1(y1s,W);
146  PrimalVector z1(z1s,W);
147 
148  std::vector<RealT> consistency = x1.checkVector(y1, z1, true, *outStream);
149  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
150  if (checkvec.norm() > std::sqrt(errtol)) {
151  errorFlag++;
152  }
153 
154  }
155 
156  catch (std::logic_error& err) {
157  *outStream << err.what() << "\n";
158  errorFlag = -1000;
159  }; // end try
160 
161  if (errorFlag != 0)
162  std::cout << "End Result: TEST FAILED\n";
163  else
164  std::cout << "End Result: TEST PASSED\n";
165 
166  return 0;
167 }
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:46
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
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
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.
int main(int argc, char *argv[])
constexpr auto dim