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
56typedef double RealT;
57typedef double ElementT;
58
59int 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 // Number of elements in StdVector objects
81 int dim = 100;
82
86
87 // Lower and upper bounds on randomized vector element values
88 RealT left = -1e0, right = 1e0;
89
90 // set x,y,z
91 x.randomize(left,right);
92 y.randomize(left,right);
93 z.randomize(left,right);
94
95 // Standard tests.
96 auto consistency = x.checkVector(y, z, true, *outStream);
97
98 ROL::StdVector<RealT, ElementT> checkvec( ROL::makePtrFromRef(consistency) );
99
100 if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON<RealT>())) errorFlag++;
101
102 // Basis tests.
103 // set x to first basis vector
104 auto zp = x.basis(0);
105 RealT znorm = zp->norm();
106 *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
107 if ( std::abs(znorm-1.0) > errtol ) {
108 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
109 errorFlag++;
110 };
111
112 // set x to middle basis vector
113 zp = x.basis(dim/2);
114 znorm = zp->norm();
115 *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
116
117 if ( std::abs(znorm-1.0) > errtol ) {
118 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
119 errorFlag++;
120 };
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) std::cout << "End Result: TEST FAILED\n";
145 else std::cout << "End Result: TEST PASSED\n";
146
147 return 0;
148
149}
150
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions of custom data types in ROL.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
void scale(const Real alpha)
Compute where .
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Real norm() const
Returns where .
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
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:325
int main(int argc, char *argv[])
double ElementT
double RealT
constexpr auto dim