ROL
vector/test_11.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_StdArray.hpp"
50#include "ROL_StdVector.hpp"
51#include "ROL_Stream.hpp"
52#include "Teuchos_GlobalMPISession.hpp"
53
54#include <iostream>
55
56using RealT = double;
57
58constexpr auto dim = 100u;
59
60int main(int argc, char *argv[]) {
61
62 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
63
65
66 int iprint = argc - 1;
67 ROL::Ptr<std::ostream> outStream;
68 ROL::nullstream bhs; // outputs nothing
69 if (iprint > 0) outStream = ROL::makePtrFromRef(std::cout);
70 else outStream = ROL::makePtrFromRef(bhs);
71
72 auto print_pool_count = [&outStream]() {
73 *outStream << "Currently using " << ROL::StdArray<RealT, dim>::pool_count()
74 << " Vectors from the pool" << std::endl;
75 };
76
77 int errorFlag = 0;
78
79 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
80
81 // *** Test body.
82
83 print_pool_count();
84
85 try {
86
87
89
90 print_pool_count();
91
92 x.randomize();
93 y.randomize();
94 z.randomize();
95
96 // Standard tests.
97 auto consistency = x.checkVector(y, z, true, *outStream);
98 ROL::StdVector<RealT> 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.clone();
105 zp = x.basis(0);
106 RealT znorm = zp->norm();
107 *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
108 if ( std::abs(znorm-1.0) > errtol ) {
109 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
110 errorFlag++;
111 };
112
113 // set x to middle basis vector
114 zp = x.basis(dim/2);
115 znorm = zp->norm();
116 *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
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 print_pool_count();
139 }
140 catch (std::logic_error& err) {
141 *outStream << err.what() << "\n";
142 errorFlag = -1000;
143 }; // end try
144
145 print_pool_count();
146
147 if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n";
148 else std::cout << "End Result: TEST PASSED\n";
149
150
151 return 0;
152
153}
154
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides the std::array implementation of the ROL::Vector interface.
void randomize(const Real l=-1.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
static void initialize_pool()
static std::size_t pool_count()
void scale(const Real alpha)
Compute where .
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Real norm() const
Returns where .
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[])
constexpr auto dim