ROL
function/test_03.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 
52 #include "ROL_NullOperator.hpp"
53 #include "ROL_DyadicOperator.hpp"
54 #include "ROL_BlockOperator2.hpp"
55 #include "ROL_DiagonalOperator.hpp"
56 #include "ROL_Elementwise_Function.hpp"
58 #include "ROL_StdVector.hpp"
59 #include "ROL_Types.hpp"
60 
61 #include "Teuchos_oblackholestream.hpp"
62 #include "Teuchos_GlobalMPISession.hpp"
63 
64 template<class Real>
65 void print_vector( const ROL::Vector<Real> &x ) {
66 
67  typedef ROL::Vector<Real> V;
68  typedef ROL::StdVector<Real> SV;
70  typedef typename PV::size_type size_type;
71 
72  const PV eb = Teuchos::dyn_cast<const PV>(x);
73  size_type n = eb.numVectors();
74 
75  for(size_type k=0; k<n; ++k) {
76  std::cout << "[subvector " << k << "]" << std::endl;
77  Teuchos::RCP<const V> vec = eb.get(k);
78  Teuchos::RCP<const std::vector<Real> > vp =
79  Teuchos::dyn_cast<SV>(const_cast<V&>(*vec)).getVector();
80  for(size_type i=0;i<vp->size();++i) {
81  std::cout << (*vp)[i] << std::endl;
82  }
83  }
84 }
85 
86 typedef double RealT;
87 
88 int main(int argc, char *argv[]) {
89 
90  // Define vectors
91  typedef std::vector<RealT> vector;
92  typedef ROL::Vector<RealT> V;
93  typedef ROL::StdVector<RealT> SV;
94 
95  // Define operators
96  typedef ROL::LinearOperator<RealT> LinOp;
97  typedef ROL::DiagonalOperator<RealT> DiagOp;
98  typedef ROL::DyadicOperator<RealT> DyadOp;
99  typedef ROL::NullOperator<RealT> NullOp;
100 
101  typedef typename vector::size_type uint;
102 
103  using namespace Teuchos;
104 
105  GlobalMPISession mpiSession(&argc, &argv);
106 
107  int iprint = argc - 1;
108 
109  RCP<std::ostream> outStream;
110  oblackholestream bhs; // no output
111 
112  if( iprint>0 )
113  outStream = rcp(&std::cout,false);
114  else
115  outStream = rcp(&bhs,false);
116 
117  int errorFlag = 0;
118 
119  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
120 
121  try {
122 
123  uint dim = 3; // Number of elements in each subvector (could be different)
124 
125  RCP<vector> x1_rcp = rcp( new vector(dim,1.0) );
126  RCP<vector> x2_rcp = rcp( new vector(dim,2.0) );
127 
128  RCP<vector> y1_rcp = rcp( new vector(dim,0.0) );
129  RCP<vector> y2_rcp = rcp( new vector(dim,0.0) );
130 
131  RCP<vector> z1_rcp = rcp( new vector(dim,0.0) );
132  RCP<vector> z2_rcp = rcp( new vector(dim,0.0) );
133 
134  RCP<V> x1 = rcp( new SV( x1_rcp) );
135  RCP<V> x2 = rcp( new SV( x2_rcp) );
136 
137  RCP<V> y1 = rcp( new SV( y1_rcp) );
138  RCP<V> y2 = rcp( new SV( y2_rcp) );
139 
140  RCP<V> z1 = rcp( new SV( z1_rcp) );
141  RCP<V> z2 = rcp( new SV( z2_rcp) );
142 
143  RCP<V> x = ROL::CreatePartitionedVector( x1, x2 );
144  RCP<V> y = ROL::CreatePartitionedVector( y1, y2 );
145  RCP<V> z = ROL::CreatePartitionedVector( z1, z2 );
146 
147  // Operator diagonals
148  RCP<vector> d1_rcp = rcp( new vector(dim,0.0) );
149  RCP<vector> d2_rcp = rcp( new vector(dim,0.0) );
150 
151  // Dyadic components
152  RCP<vector> u_rcp = rcp( new vector(dim,0.0) );
153  RCP<vector> v_rcp = rcp( new vector(dim,1.0) );
154 
155  (*d1_rcp)[0] = 6.0; (*d2_rcp)[0] = 3.0;
156  (*d1_rcp)[1] = 5.0; (*d2_rcp)[1] = 2.0;
157  (*d1_rcp)[2] = 4.0; (*d2_rcp)[2] = 1.0;
158 
159  (*z1_rcp)[0] = 6.0; (*z2_rcp)[0] = 6.0;
160  (*z1_rcp)[1] = 11.0; (*z2_rcp)[1] = 4.0;
161  (*z1_rcp)[2] = 4.0; (*z2_rcp)[2] = 2.0;
162 
163  (*u_rcp)[1] = 1.0;
164 
165  RCP<V> d1 = rcp( new SV(d1_rcp) );
166  RCP<V> d2 = rcp( new SV(d2_rcp) );
167  RCP<V> u = rcp( new SV(u_rcp) );
168  RCP<V> v = rcp( new SV(v_rcp) );
169 
170  RCP<LinOp> D1 = rcp( new DiagOp(*d1) );
171  RCP<LinOp> NO = rcp( new NullOp() );
172  RCP<LinOp> UV = rcp( new DyadOp(u,v) );
173  RCP<LinOp> D2 = rcp( new DiagOp(*d2) );
174 
175 
176  RealT tol = 0.0;
177 
178  D1->apply(*x1,*x1,tol);
179  D1->applyInverse(*x1,*x1,tol);
180 
181  ROL::BlockOperator2<RealT> bkop(D1,NO,UV,D2);
182 
183 
184  bkop.apply(*y,*x,tol);
185 
186  z->axpy(-1.0,*y);
187 
188  errorFlag += static_cast<int>(z->norm()>errtol);
189 
190  }
191 
192  catch (std::logic_error err) {
193 
194 
195  }; // end try
196 
197  if (errorFlag != 0)
198  std::cout << "End Result: TEST FAILED\n";
199  else
200  std::cout << "End Result: TEST PASSED\n";
201 
202  return 0;
203 }
204 
int main(int argc, char *argv[])
PartitionedVector< Real > PV
Defines the linear algebra of vector space on a generic partitioned vector.
double RealT
Contains definitions of custom data types in ROL.
Teuchos::RCP< Vector< Real > > CreatePartitionedVector(const Teuchos::RCP< Vector< Real > > &a)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void print_vector(const ROL::Vector< Real > &x)
Provides the interface to apply a diagonal operator which acts like elementwise multiplication when a...
Vector< Real > V
Interface to apply a dyadic operator to a vector.
Provides the interface to apply a linear operator.
void apply(V &Hv, const V &v, Real &tol) const
Apply linear operator.
Provides the interface to apply a 2x2 block operator to a partitioned vector.
Multiplication by zero.