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