ROL
vector/test_01.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 
44 
49 #include "ROL_StdVector.hpp"
50 #include "ROL_Types.hpp"
51 #include "ROL_Stream.hpp"
52 #include "Teuchos_GlobalMPISession.hpp"
53 
54 #include <iostream>
55 
56 typedef double RealT;
57 typedef double ElementT;
58 
59 int main(int argc, char *argv[]) {
60 
61  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
62 
63  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
64  int iprint = argc - 1;
65  ROL::Ptr<std::ostream> outStream;
66  ROL::nullstream bhs; // outputs nothing
67  if (iprint > 0)
68  outStream = ROL::makePtrFromRef(std::cout);
69  else
70  outStream = ROL::makePtrFromRef(bhs);
71 
72  int errorFlag = 0;
73 
74  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
75 
76  // *** Test body.
77 
78  try {
79 
80  int dim = 100;
81  ROL::Ptr<std::vector<ElementT> > x_ptr = ROL::makePtr<std::vector<ElementT>>(dim, 0.0);
82  ROL::Ptr<std::vector<ElementT> > y_ptr = ROL::makePtr<std::vector<ElementT>>(dim, 0.0);
83  ROL::Ptr<std::vector<ElementT> > z_ptr = ROL::makePtr<std::vector<ElementT>>(dim, 0.0);
87 
88  RealT left = -1e0, right = 1e0;
89 
90  // set x,y,z
91  for (int i=0; i<dim; i++) {
92  (*x_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
93  (*y_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
94  (*z_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
95  }
96 
97  // Standard tests.
98  std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream);
99  ROL::StdVector<RealT, ElementT> checkvec( ROL::makePtrFromRef(consistency) );
100  if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON<RealT>())) {
101  errorFlag++;
102  }
103 
104  // Basis tests.
105  // set x to first basis vector
106  ROL::Ptr<ROL::Vector<RealT> > zp = x.clone();
107  zp = x.basis(0);
108  RealT znorm = zp->norm();
109  *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
110  if ( std::abs(znorm-1.0) > errtol ) {
111  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
112  errorFlag++;
113  };
114  // set x to middle basis vector
115  zp = x.basis(dim/2);
116  znorm = zp->norm();
117  *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
118  if ( std::abs(znorm-1.0) > errtol ) {
119  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
120  errorFlag++;
121  };
122  // set x to last basis vector
123  zp = x.basis(dim-1);
124  znorm = zp->norm();
125  *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
126  if ( std::abs(znorm-1.0) > errtol ) {
127  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
128  errorFlag++;
129  };
130 
131  // Repeat the checkVector tests with a zero vector.
132  x.scale(0.0);
133  consistency = x.checkVector(x, x, true, *outStream);
134  if (checkvec.norm() > 0.0) {
135  errorFlag++;
136  }
137 
138  }
139  catch (std::logic_error err) {
140  *outStream << err.what() << "\n";
141  errorFlag = -1000;
142  }; // end try
143 
144  if (errorFlag != 0)
145  std::cout << "End Result: TEST FAILED\n";
146  else
147  std::cout << "End Result: TEST PASSED\n";
148 
149  return 0;
150 
151 }
152 
double ElementT
void scale(const Real alpha)
Compute where .
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Contains definitions of custom data types in ROL.
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 norm() const
Returns where .
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])