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<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  RCP<std::ostream> outStream;
93  oblackholestream bhs; // no output
94 
95  if( iprint>0 )
96  outStream = rcp(&std::cout,false);
97  else
98  outStream = rcp(&bhs,false);
99 
100  int errorFlag = 0;
101 
102  double errtol = ROL::ROL_THRESHOLD;
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  RCP<std::vector<RCP<V> > > x_rcp = rcp( new std::vector<RCP<V> > );
118  RCP<std::vector<RCP<V> > > y_rcp = rcp( new std::vector<RCP<V> > );
119  RCP<std::vector<RCP<V> > > z_rcp = rcp( new std::vector<RCP<V> > );
120 
121  for( PV::size_type k=0; k<nvec; ++k ) {
122  RCP<std::vector<RealT> > xk_rcp = rcp( new std::vector<RealT>(dim[k]) );
123  RCP<std::vector<RealT> > yk_rcp = rcp( new std::vector<RealT>(dim[k]) );
124  RCP<std::vector<RealT> > zk_rcp = rcp( new std::vector<RealT>(dim[k]) );
125 
126  for( int i=0; i<dim[k]; ++i ) {
127  (*xk_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
128  (*yk_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
129  (*zk_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
130  }
131 
132  RCP<V> xk = rcp( new SV( xk_rcp ) );
133  RCP<V> yk = rcp( new SV( yk_rcp ) );
134  RCP<V> zk = rcp( new SV( zk_rcp ) );
135 
136  x_rcp->push_back(xk);
137  y_rcp->push_back(yk);
138  z_rcp->push_back(zk);
139 
140  total_dim += dim[k];
141  }
142 
143  PV x(x_rcp);
144  PV y(y_rcp);
145  PV z(z_rcp);
146 
147 
148  // Standard tests.
149  std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream);
150  ROL::StdVector<RealT> checkvec(Teuchos::rcp(&consistency, false));
151  if (checkvec.norm() > std::sqrt(errtol)) {
152  errorFlag++;
153  }
154 
155  // Basis tests.
156  // set x to first basis vector
157  Teuchos::RCP<ROL::Vector<RealT> > zp = x.clone();
158  zp = x.basis(0);
159  RealT znorm = zp->norm();
160  *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
161  if ( std::abs(znorm-1.0) > errtol ) {
162  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
163  errorFlag++;
164  };
165  // set x to middle basis vector
166  zp = x.basis(total_dim/2);
167  znorm = zp->norm();
168  *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
169  if ( std::abs(znorm-1.0) > errtol ) {
170  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
171  errorFlag++;
172  };
173  // set x to last basis vector
174  zp = x.basis(total_dim-1);
175  znorm = zp->norm();
176  *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
177  if ( std::abs(znorm-1.0) > errtol ) {
178  *outStream << "---> POSSIBLE ERROR ABOVE!\n";
179  errorFlag++;
180  };
181 
182  // Repeat the checkVector tests with a zero vector.
183  x.scale(0.0);
184  consistency = x.checkVector(x, x, true, *outStream);
185  if (checkvec.norm() > 0.0) {
186  errorFlag++;
187  }
188 
189  if(argc>1) {
190  int m = atoi(argv[1]);
191  print_vector(*(x.basis(m)));
192 
193  }
194 
195 
196 
197 
198  }
199  catch (std::logic_error err) {
200  *outStream << err.what() << "\n";
201  errorFlag = -1000;
202  }; // end try
203 
204  if (errorFlag != 0)
205  std::cout << "End Result: TEST FAILED\n";
206  else
207  std::cout << "End Result: TEST PASSED\n";
208 
209  return 0;
210 }
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 .
double RealT
void print_vector(const ROL::Vector< Real > &x)
int main(int argc, char *argv[])