ROL
vector/test_09.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 "ROL_Stream.hpp"
45 #include "Teuchos_GlobalMPISession.hpp"
46 
47 #include "ROL_Objective.hpp"
48 
49 #include "ROL_StdVector.hpp"
51 #include "ROL_ProfiledVector.hpp"
52 #include "ROL_VectorClone.hpp"
53 
54 
55 
56 
57 using RealT = double;
59 
60 template<>
63 
64 template<typename Real>
65 class TestSingle {
66 private:
68 public:
69 
70  Real value( const ROL::Vector<Real>& x ) const {
71  auto xc = xclone_(x);
72  xc->set(x);
73  return xc->dot(x);
74  }
75 };
76 
77 template<typename Real>
78 class TestMulti {
79 private:
81 public:
82  TestMulti() : clones_("x","y") {}
83 
84  Real value_x( const ROL::Vector<Real>& x ) const {
85  auto xc = clones_(x,"x");
86  xc->set(x);
87  return xc->dot(x);
88  }
89 
90  Real value_y( const ROL::Vector<Real>& y ) const {
91  auto yc = clones_(y,"y");
92  yc->set(y);
93  return yc->dot(y);
94  }
95 
96  Real value_z( const ROL::Vector<Real>& z ) const {
97  auto zc = clones_(z,"z");
98  zc->set(z);
99  return zc->dot(z);
100  }
101 };
102 
103 
104 int main( int argc, char* argv[] ) {
105 
106  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
107 
108  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
109  int iprint = argc - 1;
110  ROL::Ptr<std::ostream> outStream;
111  ROL::nullstream bhs; // outputs nothing
112  if (iprint > 0)
113  outStream = ROL::makePtrFromRef(std::cout);
114  else
115  outStream = ROL::makePtrFromRef(bhs);
116 
117  int errorFlag = 0;
118  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
119 
120  try {
121 
122  size_type N = 20;
123  size_type M = 10;
124 
125  auto xp = ROL::makePtr<std::vector<RealT>>(N);
126  auto x = ROL::makePtr<ROL::StdVector<RealT>>(xp);
127  auto yp = ROL::makePtr<std::vector<RealT>>(M); // half the size
128  auto y = ROL::makePtr<ROL::StdVector<RealT>>(yp);
130 
132 
133  x->setScalar(1.0);
134  y->setScalar(2.0);
135 
136  TestSingle<RealT> test_single_1;
137  TestSingle<RealT> test_single_2;
138  TestMulti<RealT> test_multi;
139 
140  //------------------------------------------------------------------------
141  // Test vector of same size and type
142  auto value1 = test_single_1.value(*x);
143  RealT err1 = std::abs(N-value1);
144 
145  *outStream << "\nTesting single VectorClone of same size and type: ";
146  if (err1<errtol) { *outStream << "Works!" << std::endl; }
147  else {
148  errorFlag += err1>errtol;
149  *outStream << "Incorrect result!" << std::endl;
150  }
151 
152 
153  //------------------------------------------------------------------------
154  // Test that exception is thrown if vector has wrong size
155  bool passed_test_2 = false;
156  *outStream << "Throw exception on mismatched dimension : ";
157 
158  try { test_single_1.value(*y); }
159  catch( std::logic_error& size_mismatch ) { passed_test_2 = true; }
160 
161  if( passed_test_2 ) { *outStream << "Works!" << std::endl; }
162  else {
163  *outStream << "Failed to throw!" << std::endl;
164  errorFlag++;
165  }
166 
167  //------------------------------------------------------------------------
168  // Test that exception is thrown if vector has wrong type
169  bool passed_test_3 = false;
170  *outStream << "Throw exception on mismatched type : ";
171 
172  try { test_single_1.value(*y); }
173  catch( std::logic_error& dim_mismatch ) { passed_test_3 = true; }
174 
175  if( passed_test_3 ) { *outStream << "Works!" << std::endl; }
176  else {
177  *outStream << "Failed to throw!" << std::endl;
178  errorFlag++;
179  }
180 
181  //------------------------------------------------------------------------
182  // Test that clone is only called once
183  *outStream << "\n\nTesting with ProfiledVector. # calls to clone: ";
184  for( int i=0; i<10; ++i ) {
185  test_single_2.value(xprofile);
186  }
187 
188  auto calls = getVectorFunctionCalls(xprofile);
189  *outStream << calls.clone_ << std::endl;
190  if( calls.clone_ > 1 ) { errorFlag++; }
191 
192  // Display number of function calls
193  ROL::printVectorFunctionCalls(xprofile, *outStream);
194 
195  //------------------------------------------------------------------------
196  // Test VectorCloneMap
197 
198  bool vcm_pass = true;
199 
200  *outStream << "Testing VectorCloneMap: ";
201 
202  auto x_value = test_multi.value_x(*x);
203  auto y_value = test_multi.value_y(*y);
204  auto z_value = test_multi.value_z(*z);
205 
206  auto errx = std::abs(x_value-20.0);
207  auto erry = std::abs(y_value-40.0);
208  auto errz = std::abs(z_value-80.0);
209 
210  if( errx>errtol ) { vcm_pass = false; errorFlag++; }
211  if( erry>errtol ) { vcm_pass = false; errorFlag++; }
212  if( errz>errtol ) { vcm_pass = false; errorFlag++; }
213 
214  if( vcm_pass ) { *outStream << "Works!" << std::endl; }
215  else {
216  *outStream << "Error tolerance exceeded!" << std::endl;
217  *outStream << "x_value was " << x_value << ", should be 20." << std::endl;
218  *outStream << "y_value was " << y_value << ", should be 40." << std::endl;
219  *outStream << "z_value was " << z_value << ", should be 80." << std::endl;
220  }
221 
222  }
223  catch (std::logic_error& err) {
224  *outStream << err.what() << "\n";
225  errorFlag = -1000;
226  }; // end try
227 
228  if (errorFlag != 0)
229  std::cout << "End Result: TEST FAILED\n";
230  else
231  std::cout << "End Result: TEST PASSED\n";
232 
233  return 0;
234 }
235 
static Ptr< PartitionedVector > create(std::initializer_list< Vp > vs)
typename PV< Real >::size_type size_type
Real value_y(const ROL::Vector< Real > &y) const
Real value(const ROL::Vector< Real > &x) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ROL::VectorClone< Real > xclone_
void printVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x, std::ostream &outStream=std::cout)
By keeping a pointer to this in a derived Vector class, a tally of all methods is kept for profiling ...
Real value_x(const ROL::Vector< Real > &x) const
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
Real value_z(const ROL::Vector< Real > &z) const
VectorFunctionCalls< Ordinal > getVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x)
ROL::VectorCloneMap< Real > clones_