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 "ROL_Stream.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 = dynamic_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  auto vec = eb.get(k);
78  auto vp = ROL::dynamicPtrCast<const SV>(vec)->getVector();
79  for(size_type i=0;i<vp->size();++i) {
80  std::cout << (*vp)[i] << std::endl;
81  }
82  }
83 }
84 
85 typedef double RealT;
86 
87 int main(int argc, char *argv[]) {
88 
89  // Define vectors
90  typedef std::vector<RealT> vector;
91  typedef ROL::Vector<RealT> V;
92  typedef ROL::StdVector<RealT> SV;
93 
94  // Define operators
95  typedef ROL::LinearOperator<RealT> LinOp;
96  typedef ROL::DiagonalOperator<RealT> DiagOp;
97  typedef ROL::DyadicOperator<RealT> DyadOp;
98  typedef ROL::NullOperator<RealT> NullOp;
99 
100  typedef typename vector::size_type uint;
101 
102  using namespace Teuchos;
103 
104  GlobalMPISession mpiSession(&argc, &argv);
105 
106  int iprint = argc - 1;
107 
108  ROL::Ptr<std::ostream> outStream;
109  ROL::nullstream bhs; // no output
110 
111  if( iprint>0 )
112  outStream = ROL::makePtrFromRef(std::cout);
113  else
114  outStream = ROL::makePtrFromRef(bhs);
115 
116  int errorFlag = 0;
117 
118  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
119 
120  try {
121 
122  uint dim = 3; // Number of elements in each subvector (could be different)
123 
124  ROL::Ptr<vector> x1_ptr = ROL::makePtr<vector>(dim,1.0);
125  ROL::Ptr<vector> x2_ptr = ROL::makePtr<vector>(dim,2.0);
126 
127  ROL::Ptr<vector> y1_ptr = ROL::makePtr<vector>(dim,0.0);
128  ROL::Ptr<vector> y2_ptr = ROL::makePtr<vector>(dim,0.0);
129 
130  ROL::Ptr<vector> z1_ptr = ROL::makePtr<vector>(dim,0.0);
131  ROL::Ptr<vector> z2_ptr = ROL::makePtr<vector>(dim,0.0);
132 
133  ROL::Ptr<V> x1 = ROL::makePtr<SV>( x1_ptr);
134  ROL::Ptr<V> x2 = ROL::makePtr<SV>( x2_ptr);
135 
136  ROL::Ptr<V> y1 = ROL::makePtr<SV>( y1_ptr);
137  ROL::Ptr<V> y2 = ROL::makePtr<SV>( y2_ptr);
138 
139  ROL::Ptr<V> z1 = ROL::makePtr<SV>( z1_ptr);
140  ROL::Ptr<V> z2 = ROL::makePtr<SV>( z2_ptr);
141 
142  ROL::Ptr<V> x = ROL::CreatePartitionedVector( x1, x2 );
143  ROL::Ptr<V> y = ROL::CreatePartitionedVector( y1, y2 );
144  ROL::Ptr<V> z = ROL::CreatePartitionedVector( z1, z2 );
145 
146  // Operator diagonals
147  ROL::Ptr<vector> d1_ptr = ROL::makePtr<vector>(dim,0.0);
148  ROL::Ptr<vector> d2_ptr = ROL::makePtr<vector>(dim,0.0);
149 
150  // Dyadic components
151  ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(dim,0.0);
152  ROL::Ptr<vector> v_ptr = ROL::makePtr<vector>(dim,1.0);
153 
154  (*d1_ptr)[0] = 6.0; (*d2_ptr)[0] = 3.0;
155  (*d1_ptr)[1] = 5.0; (*d2_ptr)[1] = 2.0;
156  (*d1_ptr)[2] = 4.0; (*d2_ptr)[2] = 1.0;
157 
158  (*z1_ptr)[0] = 6.0; (*z2_ptr)[0] = 6.0;
159  (*z1_ptr)[1] = 11.0; (*z2_ptr)[1] = 4.0;
160  (*z1_ptr)[2] = 4.0; (*z2_ptr)[2] = 2.0;
161 
162  (*u_ptr)[1] = 1.0;
163 
164  ROL::Ptr<V> d1 = ROL::makePtr<SV>(d1_ptr);
165  ROL::Ptr<V> d2 = ROL::makePtr<SV>(d2_ptr);
166  ROL::Ptr<V> u = ROL::makePtr<SV>(u_ptr);
167  ROL::Ptr<V> v = ROL::makePtr<SV>(v_ptr);
168 
169  ROL::Ptr<LinOp> D1 = ROL::makePtr<DiagOp>(*d1);
170  ROL::Ptr<LinOp> NO = ROL::makePtr<NullOp>();
171  ROL::Ptr<LinOp> UV = ROL::makePtr<DyadOp>(u,v);
172  ROL::Ptr<LinOp> D2 = ROL::makePtr<DiagOp>(*d2);
173 
174 
175  RealT tol = 0.0;
176 
177  D1->apply(*x1,*x1,tol);
178  D1->applyInverse(*x1,*x1,tol);
179 
180  ROL::BlockOperator2<RealT> bkop(D1,NO,UV,D2);
181 
182 
183  bkop.apply(*y,*x,tol);
184 
185  z->axpy(-1.0,*y);
186 
187  errorFlag += static_cast<int>(z->norm()>errtol);
188 
189  }
190 
191  catch (std::logic_error err) {
192 
193 
194  }; // end try
195 
196  if (errorFlag != 0)
197  std::cout << "End Result: TEST FAILED\n";
198  else
199  std::cout << "End Result: TEST PASSED\n";
200 
201  return 0;
202 }
203 
PartitionedVector< Real > PV
typename PV< Real >::size_type size_type
Defines the linear algebra of vector space on a generic partitioned vector.
ROL::Ptr< Vector< Real > > CreatePartitionedVector(const ROL::Ptr< Vector< Real >> &a)
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)
Provides the interface to apply a diagonal operator which acts like elementwise multiplication when a...
Vector< Real > V
void apply(V &Hv, const V &v, Real &tol) const
Apply linear operator.
Interface to apply a dyadic operator to a vector.
Provides the interface to apply a linear operator.
Provides the interface to apply a 2x2 block operator to a partitioned vector.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
Multiplication by zero.