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 "Teuchos_oblackholestream.hpp"
56 #include "Teuchos_GlobalMPISession.hpp"
57 
58 typedef double RealT;
59 
60 int main(int argc, char *argv[]) {
61 
62  using Teuchos::RCP; using Teuchos::rcp;
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  Teuchos::oblackholestream bhs; // outputs nothing
79  std::ostream& outStream = (iprint > 0) ? std::cout : bhs;
80 
81  int errorFlag = 0;
82 
83  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
84 
85  try {
86  // Dimension of the optimization vector
87 
88  int dim = 10;
89 
90  // Create Tpetra::MultiVectors (single vectors)
91  RCP<vector> x_rcp = rcp( new vector(dim) );
92  RCP<vector> y_rcp = rcp( new vector(dim) );
93  RCP<vector> w_rcp = rcp( new vector(dim,2.0) );
94 
95  RCP<V> xs = rcp( new SV( x_rcp ) );
96  RCP<V> ys = rcp( new SV( y_rcp ) );
97 
98  SV ws( w_rcp );
99 
102 
103  RCP<LinearOperator> W = rcp( new DiagonalOperator(ws) );
104 
105  PrimalVector x(xs,W);
106  DualVector y(ys,W);
107 
108  RealT xy = x.dot(y.dual());
109  RealT yx = y.dot(x.dual());
110 
111  outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
112  << std::abs(xy-yx) << "\n";
113  outStream << "x.dot(y.dual()): " << xy << "\n";
114  outStream << "y.dot(x.dual()): " << yx << "\n";
115  if ( std::abs(xy-yx) > errtol ) {
116  outStream << "---> POSSIBLE ERROR ABOVE!\n";
117  errorFlag++;
118  }
119 
120  RealT xx = std::sqrt(x.dot(x)), xnorm = x.norm();
121  RealT yy = std::sqrt(y.dot(y)), ynorm = y.norm();
122 
123  outStream << "\nAbsolute error between sqrt(x.dot(x)) and x.norm(): "
124  << std::abs(xx-xnorm) << "\n";
125  outStream << "sqrt(x.dot(x)): " << xx << "\n";
126  outStream << "x.norm(): " << xnorm << "\n";
127  if ( std::abs(xx-xnorm) > errtol ) {
128  outStream << "---> POSSIBLE ERROR ABOVE!\n";
129  errorFlag++;
130  }
131 
132  outStream << "\nAbsolute error between sqrt(y.dot(y)) and y.norm(): "
133  << std::abs(yy-ynorm) << "\n";
134  outStream << "sqrt(y.dot(y)): " << yy << "\n";
135  outStream << "y.norm(): " << ynorm << "\n";
136  if ( std::abs(yy-ynorm) > errtol ) {
137  outStream << "---> POSSIBLE ERROR ABOVE!\n";
138  errorFlag++;
139  }
140 
141  // clone z from x, deep copy x into z, norm of z
142  Teuchos::RCP<ROL::Vector<RealT> > z = x.clone();
143  z->set(x);
144  RealT znorm = z->norm();
145  outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n";
146  if ( std::abs(xnorm - znorm) > errtol ) {
147  outStream << "---> POSSIBLE ERROR ABOVE!\n";
148  errorFlag++;
149  }
150  Teuchos::RCP<ROL::Vector<RealT> > w = y.clone();
151  w = y.clone();
152  w->set(y);
153  RealT wnorm = w->norm();
154  outStream << "\nNorm of ROL::Vector w (clone of y): " << wnorm << "\n";
155  if ( std::abs(ynorm - wnorm) > errtol ) {
156  outStream << "---> POSSIBLE ERROR ABOVE!\n";
157  errorFlag++;
158  }
159 
160  // Standard tests.
161  RCP<vector> x1_rcp = rcp( new vector(dim) );
162  RCP<vector> y1_rcp = rcp( new vector(dim) );
163  RCP<vector> z1_rcp = rcp( new vector(dim) );
164 
165  RCP<V> x1s = rcp( new SV( x1_rcp ) );
166  RCP<V> y1s = rcp( new SV( y1_rcp ) );
167  RCP<V> z1s = rcp( new SV( z1_rcp ) );
168 
169  ROL::RandomizeVector(*x1s);
170  ROL::RandomizeVector(*y1s);
171  ROL::RandomizeVector(*z1s);
172 
173  // Create ROL vectors
174  PrimalVector x1(x1s,W);
175  PrimalVector y1(y1s,W);
176  PrimalVector z1(z1s,W);
177 
178  std::vector<RealT> consistency = x1.checkVector(y1, z1, true, outStream);
179  ROL::StdVector<RealT> checkvec(Teuchos::rcp(&consistency, false));
180  if (checkvec.norm() > std::sqrt(errtol)) {
181  errorFlag++;
182  }
183 
184  }
185 
186  catch (std::logic_error err) {
187  outStream << err.what() << "\n";
188  errorFlag = -1000;
189  }; // end try
190 
191  if (errorFlag != 0)
192  std::cout << "End Result: TEST FAILED\n";
193  else
194  std::cout << "End Result: TEST PASSED\n";
195 
196  return 0;
197 }
double RealT
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:76
Provides the interface to apply a diagonal operator which acts like elementwise multiplication when a...
Vector< Real > V
Provides the std::vector implementation of the ROL::Vector interface.
Provides the interface to apply a linear operator.
int main(int argc, char *argv[])
Real norm() const
Returns where .