ROL
vector/test_04.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 
49 #include "ROL_StdVector.hpp"
50 #include "ROL_Types.hpp"
51 #include "ROL_Stream.hpp"
52 #include "Teuchos_GlobalMPISession.hpp"
53 
54 typedef double RealT;
55 
56 
57 template<class Real>
58 void print_vector( const ROL::Vector<Real> &x ) {
59 
60  typedef ROL::Vector<Real> V;
61  typedef ROL::StdVector<Real> SV;
63  typedef typename PV::size_type size_type;
64 
65  const PV eb = dynamic_cast<const PV&>(x);
66  size_type n = eb.numVectors();
67 
68  for(size_type k=0; k<n; ++k) {
69  std::cout << "[subvector " << k << "]" << std::endl;
70  ROL::Ptr<const V> vec = eb.get(k);
71  ROL::Ptr<const std::vector<Real> > vp =
72  dynamic_cast<const SV&>(*vec).getVector();
73  for(size_type i=0;i<vp->size();++i) {
74  std::cout << (*vp)[i] << std::endl;
75  }
76  }
77 }
78 
79 
80 int main(int argc, char *argv[]) {
81 
82  using namespace Teuchos;
83 
84  typedef ROL::Vector<RealT> V;
85  typedef ROL::StdVector<RealT> SV;
87 
88  GlobalMPISession mpiSession(&argc, &argv);
89 
90  int iprint = argc - 1;
91 
92  ROL::Ptr<std::ostream> outStream;
93  ROL::nullstream bhs; // no output
94 
95  if( iprint>0 )
96  outStream = ROL::makePtrFromRef(std::cout);
97  else
98  outStream = ROL::makePtrFromRef(bhs);
99 
100  int errorFlag = 0;
101 
102  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
103 
104  try {
105 
106  PV::size_type nvec = 3;
107  std::vector<int> dim;
108 
109  dim.push_back(4);
110  dim.push_back(3);
111  dim.push_back(5);
112 
113  int total_dim = 0;
114 
115  RealT left = -1e0, right = 1e0;
116 
117  std::vector<ROL::Ptr<V> > x_ptr;
118  std::vector<ROL::Ptr<V> > y_ptr;
119  std::vector<ROL::Ptr<V> > z_ptr;
120 
121  for( PV::size_type k=0; k<nvec; ++k ) {
122  ROL::Ptr<std::vector<RealT> > xk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
123  ROL::Ptr<std::vector<RealT> > yk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
124  ROL::Ptr<std::vector<RealT> > zk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
125 
126  for( int i=0; i<dim[k]; ++i ) {
127  (*xk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
128  (*yk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
129  (*zk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
130  }
131 
132  ROL::Ptr<V> xk = ROL::makePtr<SV>( xk_ptr );
133  ROL::Ptr<V> yk = ROL::makePtr<SV>( yk_ptr );
134  ROL::Ptr<V> zk = ROL::makePtr<SV>( zk_ptr );
135 
136  x_ptr.push_back(xk);
137  y_ptr.push_back(yk);
138  z_ptr.push_back(zk);
139 
140  total_dim += dim[k];
141  }
142 
143  PV x(x_ptr);
144  ROL::Ptr<V> y = ROL::CreatePartitionedVector<RealT>(y_ptr[0],y_ptr[1],y_ptr[2]);
145  PV z(z_ptr);
146 
147  // Standard tests.
148  std::vector<RealT> consistency = x.checkVector(*y, z, true, *outStream);
149  ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
150  if (checkvec.norm() > std::sqrt(errtol)) {
151  errorFlag++;
152  }
153 
154  // Basis tests.
155  // set x to first basis vector
156  ROL::Ptr<ROL::Vector<RealT> > zp = x.clone();
157  zp = x.basis(0);
158  RealT znorm = zp->norm();
159  *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
160  if ( std::abs(znorm-1.0) > errtol ) {
161  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
162  errorFlag++;
163  };
164  // set x to middle basis vector
165  zp = x.basis(total_dim/2);
166  znorm = zp->norm();
167  *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
168  if ( std::abs(znorm-1.0) > errtol ) {
169  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
170  errorFlag++;
171  };
172  // set x to last basis vector
173  zp = x.basis(total_dim-1);
174  znorm = zp->norm();
175  *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
176  if ( std::abs(znorm-1.0) > errtol ) {
177  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
178  errorFlag++;
179  };
180 
181  // Repeat the checkVector tests with a zero vector.
182  x.scale(0.0);
183  consistency = x.checkVector(x, x, true, *outStream);
184  if (checkvec.norm() > 0.0) {
185  errorFlag++;
186  }
187 
188  if(argc>1) {
189  int m = atoi(argv[1]);
190  print_vector(*(x.basis(m)));
191 
192  }
193 
194 
195 
196 
197  }
198  catch (std::logic_error& err) {
199  *outStream << err.what() << "\n";
200  errorFlag = -1000;
201  }; // end try
202 
203  if (errorFlag != 0)
204  std::cout << "End Result: TEST FAILED\n";
205  else
206  std::cout << "End Result: TEST PASSED\n";
207 
208  return 0;
209 }
PartitionedVector< Real > PV
typename PV< Real >::size_type size_type
Defines the linear algebra of vector space on a generic partitioned vector.
Contains definitions of custom data types in ROL.
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...
void print_vector(const ROL::Vector< Real > &x)
Vector< Real > V
Real norm() const
Returns where .
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
constexpr auto dim