ROL
vector/test_09.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "ROL_Stream.hpp"
11#include "Teuchos_GlobalMPISession.hpp"
12
13#include "ROL_Objective.hpp"
14
15#include "ROL_StdVector.hpp"
18#include "ROL_VectorClone.hpp"
19
20
21
22
23using RealT = double;
24using size_type = typename std::vector<RealT>::size_type;
25
26template<>
29
30template<typename Real>
32private:
34public:
35
36 Real value( const ROL::Vector<Real>& x ) const {
37 auto xc = xclone_(x);
38 xc->set(x);
39 return xc->dot(x);
40 }
41};
42
43template<typename Real>
44class TestMulti {
45private:
47public:
48 TestMulti() : clones_("x","y") {}
49
50 Real value_x( const ROL::Vector<Real>& x ) const {
51 auto xc = clones_(x,"x");
52 xc->set(x);
53 return xc->dot(x);
54 }
55
56 Real value_y( const ROL::Vector<Real>& y ) const {
57 auto yc = clones_(y,"y");
58 yc->set(y);
59 return yc->dot(y);
60 }
61
62 Real value_z( const ROL::Vector<Real>& z ) const {
63 auto zc = clones_(z,"z");
64 zc->set(z);
65 return zc->dot(z);
66 }
67};
68
69
70int main( int argc, char* argv[] ) {
71
72 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
73
74 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
75 int iprint = argc - 1;
76 ROL::Ptr<std::ostream> outStream;
77 ROL::nullstream bhs; // outputs nothing
78 if (iprint > 0)
79 outStream = ROL::makePtrFromRef(std::cout);
80 else
81 outStream = ROL::makePtrFromRef(bhs);
82
83 int errorFlag = 0;
84 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
85
86 try {
87
88 size_type N = 20;
89 size_type M = 10;
90
91 auto xp = ROL::makePtr<std::vector<RealT>>(N);
92 auto x = ROL::makePtr<ROL::StdVector<RealT>>(xp);
93 auto yp = ROL::makePtr<std::vector<RealT>>(M); // half the size
94 auto y = ROL::makePtr<ROL::StdVector<RealT>>(yp);
96
98
99 x->setScalar(1.0);
100 y->setScalar(2.0);
101
102 TestSingle<RealT> test_single_1;
103 TestSingle<RealT> test_single_2;
104 TestMulti<RealT> test_multi;
105
106 //------------------------------------------------------------------------
107 // Test vector of same size and type
108 auto value1 = test_single_1.value(*x);
109 RealT err1 = std::abs(N-value1);
110
111 *outStream << "\nTesting single VectorClone of same size and type: ";
112 if (err1<errtol) { *outStream << "Works!" << std::endl; }
113 else {
114 errorFlag += err1>errtol;
115 *outStream << "Incorrect result!" << std::endl;
116 }
117
118
119 //------------------------------------------------------------------------
120 // Test that exception is thrown if vector has wrong size
121 bool passed_test_2 = false;
122 *outStream << "Throw exception on mismatched dimension : ";
123
124 try { test_single_1.value(*y); }
125 catch( std::logic_error& size_mismatch ) { passed_test_2 = true; }
126
127 if( passed_test_2 ) { *outStream << "Works!" << std::endl; }
128 else {
129 *outStream << "Failed to throw!" << std::endl;
130 errorFlag++;
131 }
132
133 //------------------------------------------------------------------------
134 // Test that exception is thrown if vector has wrong type
135 bool passed_test_3 = false;
136 *outStream << "Throw exception on mismatched type : ";
137
138 try { test_single_1.value(*y); }
139 catch( std::logic_error& dim_mismatch ) { passed_test_3 = true; }
140
141 if( passed_test_3 ) { *outStream << "Works!" << std::endl; }
142 else {
143 *outStream << "Failed to throw!" << std::endl;
144 errorFlag++;
145 }
146
147 //------------------------------------------------------------------------
148 // Test that clone is only called once
149 *outStream << "\n\nTesting with ProfiledVector. # calls to clone: ";
150 for( int i=0; i<10; ++i ) {
151 test_single_2.value(xprofile);
152 }
153
154 auto calls = getVectorFunctionCalls(xprofile);
155 *outStream << calls.clone_ << std::endl;
156 if( calls.clone_ > 1 ) { errorFlag++; }
157
158 // Display number of function calls
159 ROL::printVectorFunctionCalls(xprofile, *outStream);
160
161 //------------------------------------------------------------------------
162 // Test VectorCloneMap
163
164 bool vcm_pass = true;
165
166 *outStream << "Testing VectorCloneMap: ";
167
168 auto x_value = test_multi.value_x(*x);
169 auto y_value = test_multi.value_y(*y);
170 auto z_value = test_multi.value_z(*z);
171
172 auto errx = std::abs(x_value-20.0);
173 auto erry = std::abs(y_value-40.0);
174 auto errz = std::abs(z_value-80.0);
175
176 if( errx>errtol ) { vcm_pass = false; errorFlag++; }
177 if( erry>errtol ) { vcm_pass = false; errorFlag++; }
178 if( errz>errtol ) { vcm_pass = false; errorFlag++; }
179
180 if( vcm_pass ) { *outStream << "Works!" << std::endl; }
181 else {
182 *outStream << "Error tolerance exceeded!" << std::endl;
183 *outStream << "x_value was " << x_value << ", should be 20." << std::endl;
184 *outStream << "y_value was " << y_value << ", should be 40." << std::endl;
185 *outStream << "z_value was " << z_value << ", should be 80." << std::endl;
186 }
187
188 }
189 catch (std::logic_error& err) {
190 *outStream << err.what() << "\n";
191 errorFlag = -1000;
192 }; // end try
193
194 if (errorFlag != 0)
195 std::cout << "End Result: TEST FAILED\n";
196 else
197 std::cout << "End Result: TEST PASSED\n";
198
199 return 0;
200}
201
typename PV< Real >::size_type size_type
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
static Ptr< PartitionedVector > create(std::initializer_list< Vp > vs)
By keeping a pointer to this in a derived Vector class, a tally of all methods is kept for profiling ...
Defines the linear algebra or vector space interface.
Real value_x(const ROL::Vector< Real > &x) const
Real value_y(const ROL::Vector< Real > &y) const
ROL::VectorCloneMap< Real > clones_
Real value_z(const ROL::Vector< Real > &z) const
Real value(const ROL::Vector< Real > &x) const
ROL::VectorClone< Real > xclone_
void printVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x, std::ostream &outStream=std::cout)
int main(int argc, char *argv[])