ROL
vector/test_10.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#include "Teuchos_GlobalMPISession.hpp"
45
46#include "ROL_Objective.hpp"
47
48#include "ROL_StdVector.hpp"
50#include "ROL_RandomVector.hpp"
52#include "ROL_Stream.hpp"
53
54#include <iomanip>
55
56using RealT = double;
57using size_type = typename std::vector<RealT>::size_type;
58
59namespace details {
60
61using namespace ROL;
62
63// An alternative, more expensive way to compute the dot product!
64template<typename Real>
66private:
67
69
70public:
71
72 Real dot( const Vector<Real>& x, const Vector<Real>& y ) {
73
74 auto w = workspace_.copy(x);
75 auto z = workspace_.copy(x);
76 w->plus(y);
77 z->axpy(-1.0,y);
78 auto result = 0.25*( w->dot(*w) - z->dot(*z) );
79 return result;
80 } // w and z should go out of scope - decrement ref counts
81
82 void status( std::ostream& os ) const { workspace_.status(os); }
83};
84
85
86
87
88} // namespace details
89
91
92
93int main( int argc, char* argv[] ) {
94
95 using namespace ROL;
96
97 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
98
99 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
100 auto outStream = makeStreamPtr( std::cout, argc > 1 );
101
102 int errorFlag = 0;
103 RealT errtol = std::sqrt(ROL_EPSILON<RealT>());
104
105 try {
106
108
109 size_type N = 20;
110
111 auto xp = makePtr<std::vector<RealT>>(N);
112 auto x = makePtr<StdVector<RealT>>(xp);
113 auto y = x->clone();
114
115 auto xx = PartitionedVector<RealT>::create({x,x});
116 auto yy = PartitionedVector<RealT>::create({y,y});
117
118 RandomizeVector( *x );
119 RandomizeVector( *y );
120
121 auto result = 0.5*polar.dot(*x,*y);
122 result += 0.5*polar.dot(*y,*x);
123
124 auto x_dot_y = x->dot(*y);
125 errorFlag += ( std::abs( x_dot_y - result ) > errtol );
126
127 *outStream << std::setprecision(16) << x_dot_y << std::endl;
128 *outStream << std::setprecision(16) << result << std::endl;
129
130
131 polar.dot(*xx,*yy);
132
133 polar.status(*outStream);
134
135 }
136 catch (std::logic_error& err) {
137 *outStream << err.what() << "\n";
138 errorFlag = -1000;
139 }; // end try
140
141 if (errorFlag != 0)
142 std::cout << "End Result: TEST FAILED\n";
143 else
144 std::cout << "End Result: TEST PASSED\n";
145
146 return 0;
147}
148
typename PV< Real >::size_type size_type
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Defines the linear algebra of vector space on a generic partitioned vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
VectorWorkspace< Real > workspace_
void status(std::ostream &os) const
Real dot(const Vector< Real > &x, const Vector< Real > &y)
int main(int argc, char *argv[])