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 "Teuchos_oblackholestream.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 = Teuchos::dyn_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  Teuchos::RCP<const V> vec = eb.get(k);
71  Teuchos::RCP<const std::vector<Real> > vp =
72  Teuchos::dyn_cast<SV>(const_cast<V&>(*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 
81 int main(int argc, char *argv[]) {
82 
83  using namespace Teuchos;
84 
85  typedef ROL::Vector<RealT> V;
86  typedef ROL::StdVector<RealT> SV;
88 
89  GlobalMPISession mpiSession(&argc, &argv);
90 
91  int iprint = argc - 1;
92 
93  RCP<std::ostream> outStream;
94  oblackholestream bhs; // no output
95 
96  if( iprint>0 )
97  outStream = rcp(&std::cout,false);
98  else
99  outStream = rcp(&bhs,false);
100 
101  int errorFlag = 0;
102 
103  double errtol = ROL::ROL_THRESHOLD;
104 
105  try {
106 
107  PV::size_type nvec = 3;
108  std::vector<int> dim;
109 
110  dim.push_back(4);
111  dim.push_back(3);
112  dim.push_back(5);
113 
114  int total_dim = 0;
115 
116  RealT left = -1e0, right = 1e0;
117 
118  RCP<std::vector<RCP<V> > > x_rcp = rcp( new std::vector<RCP<V> > );
119  RCP<std::vector<RCP<V> > > y_rcp = rcp( new std::vector<RCP<V> > );
120  RCP<std::vector<RCP<V> > > z_rcp = rcp( new std::vector<RCP<V> > );
121 
122  for( PV::size_type k=0; k<nvec; ++k ) {
123  RCP<std::vector<RealT> > xk_rcp = rcp( new std::vector<RealT>(dim[k]) );
124  RCP<std::vector<RealT> > yk_rcp = rcp( new std::vector<RealT>(dim[k]) );
125  RCP<std::vector<RealT> > zk_rcp = rcp( new std::vector<RealT>(dim[k]) );
126 
127  for( int i=0; i<dim[k]; ++i ) {
128  (*xk_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
129  (*yk_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
130  (*zk_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
131  }
132 
133  RCP<V> xk = rcp( new SV( xk_rcp ) );
134  RCP<V> yk = rcp( new SV( yk_rcp ) );
135  RCP<V> zk = rcp( new SV( zk_rcp ) );
136 
137  x_rcp->push_back(xk);
138  y_rcp->push_back(yk);
139  z_rcp->push_back(zk);
140 
141  total_dim += dim[k];
142  }
143 
144  PV x(x_rcp);
145  PV y(y_rcp);
146  PV z(z_rcp);
147 
148 
149  // Standard tests.
150  std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream);
151  ROL::StdVector<RealT> checkvec(Teuchos::rcp(&consistency, false));
152  if (checkvec.norm() > std::sqrt(errtol)) {
153  errorFlag++;
154  }
155 
156  // Basis tests.
157  // set x to first basis vector
158  Teuchos::RCP<ROL::Vector<RealT> > zp = x.clone();
159  zp = x.basis(0);
160  RealT znorm = zp->norm();
161  *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
162  if ( std::abs(znorm-1.0) > errtol ) {
163  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
164  errorFlag++;
165  };
166  // set x to middle basis vector
167  zp = x.basis(total_dim/2);
168  znorm = zp->norm();
169  *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
170  if ( std::abs(znorm-1.0) > errtol ) {
171  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
172  errorFlag++;
173  };
174  // set x to last basis vector
175  zp = x.basis(total_dim-1);
176  znorm = zp->norm();
177  *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
178  if ( std::abs(znorm-1.0) > errtol ) {
179  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
180  errorFlag++;
181  };
182 
183  // Repeat the checkVector tests with a zero vector.
184  x.scale(0.0);
185  consistency = x.checkVector(x, x, true, *outStream);
186  if (checkvec.norm() > 0.0) {
187  errorFlag++;
188  }
189 
190  if(argc>1) {
191  int m = atoi(argv[1]);
192  print_vector(*(x.basis(m)));
193 
194  }
195 
196 
197 
198 
199  }
200  catch (std::logic_error err) {
201  *outStream << err.what() << "\n";
202  errorFlag = -1000;
203  }; // end try
204 
205  if (errorFlag != 0)
206  std::cout << "End Result: TEST FAILED\n";
207  else
208  std::cout << "End Result: TEST PASSED\n";
209 
210  return 0;
211 }
Defines the linear algebra of vector space on a generic partitioned vector.
double RealT
Contains definitions of custom data types in ROL.
static const double ROL_THRESHOLD
Tolerance for various equality tests.
Definition: ROL_Types.hpp:122
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
double RealT
void print_vector(const ROL::Vector< Real > &x)
int main(int argc, char *argv[])