ROL
function/test_03.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
18 #include "ROL_NullOperator.hpp"
19 #include "ROL_DyadicOperator.hpp"
20 #include "ROL_BlockOperator2.hpp"
21 #include "ROL_DiagonalOperator.hpp"
22 #include "ROL_Elementwise_Function.hpp"
24 #include "ROL_StdVector.hpp"
25 #include "ROL_Types.hpp"
26 
27 #include "ROL_Stream.hpp"
28 #include "Teuchos_GlobalMPISession.hpp"
29 
30 template<class Real>
31 void print_vector( const ROL::Vector<Real> &x ) {
32 
33 // typedef ROL::Vector<Real> V;
34  typedef ROL::StdVector<Real> SV;
36  typedef typename PV::size_type size_type;
37 
38  const PV eb = dynamic_cast<const PV&>(x);
39  size_type n = eb.numVectors();
40 
41  for(size_type k=0; k<n; ++k) {
42  std::cout << "[subvector " << k << "]" << std::endl;
43  auto vec = eb.get(k);
44  auto vp = ROL::dynamicPtrCast<const SV>(vec)->getVector();
45  for(size_type i=0;i<vp->size();++i) {
46  std::cout << (*vp)[i] << std::endl;
47  }
48  }
49 }
50 
51 typedef double RealT;
52 
53 int main(int argc, char *argv[]) {
54 
55  // Define vectors
56  typedef std::vector<RealT> vector;
57  typedef ROL::Vector<RealT> V;
58  typedef ROL::StdVector<RealT> SV;
59 
60  // Define operators
61  typedef ROL::LinearOperator<RealT> LinOp;
62  typedef ROL::DiagonalOperator<RealT> DiagOp;
63  typedef ROL::DyadicOperator<RealT> DyadOp;
64  typedef ROL::NullOperator<RealT> NullOp;
65 
66  typedef typename vector::size_type uint;
67 
68  using namespace Teuchos;
69 
70  GlobalMPISession mpiSession(&argc, &argv);
71 
72  int iprint = argc - 1;
73 
74  ROL::Ptr<std::ostream> outStream;
75  ROL::nullstream bhs; // no output
76 
77  if( iprint>0 )
78  outStream = ROL::makePtrFromRef(std::cout);
79  else
80  outStream = ROL::makePtrFromRef(bhs);
81 
82  int errorFlag = 0;
83 
84  RealT errtol = ROL::ROL_THRESHOLD<RealT>();
85 
86  try {
87 
88  uint dim = 3; // Number of elements in each subvector (could be different)
89 
90  ROL::Ptr<vector> x1_ptr = ROL::makePtr<vector>(dim,1.0);
91  ROL::Ptr<vector> x2_ptr = ROL::makePtr<vector>(dim,2.0);
92 
93  ROL::Ptr<vector> y1_ptr = ROL::makePtr<vector>(dim,0.0);
94  ROL::Ptr<vector> y2_ptr = ROL::makePtr<vector>(dim,0.0);
95 
96  ROL::Ptr<vector> z1_ptr = ROL::makePtr<vector>(dim,0.0);
97  ROL::Ptr<vector> z2_ptr = ROL::makePtr<vector>(dim,0.0);
98 
99  ROL::Ptr<V> x1 = ROL::makePtr<SV>( x1_ptr);
100  ROL::Ptr<V> x2 = ROL::makePtr<SV>( x2_ptr);
101 
102  ROL::Ptr<V> y1 = ROL::makePtr<SV>( y1_ptr);
103  ROL::Ptr<V> y2 = ROL::makePtr<SV>( y2_ptr);
104 
105  ROL::Ptr<V> z1 = ROL::makePtr<SV>( z1_ptr);
106  ROL::Ptr<V> z2 = ROL::makePtr<SV>( z2_ptr);
107 
108  ROL::Ptr<V> x = ROL::CreatePartitionedVector( x1, x2 );
109  ROL::Ptr<V> y = ROL::CreatePartitionedVector( y1, y2 );
110  ROL::Ptr<V> z = ROL::CreatePartitionedVector( z1, z2 );
111 
112  // Operator diagonals
113  ROL::Ptr<vector> d1_ptr = ROL::makePtr<vector>(dim,0.0);
114  ROL::Ptr<vector> d2_ptr = ROL::makePtr<vector>(dim,0.0);
115 
116  // Dyadic components
117  ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(dim,0.0);
118  ROL::Ptr<vector> v_ptr = ROL::makePtr<vector>(dim,1.0);
119 
120  (*d1_ptr)[0] = 6.0; (*d2_ptr)[0] = 3.0;
121  (*d1_ptr)[1] = 5.0; (*d2_ptr)[1] = 2.0;
122  (*d1_ptr)[2] = 4.0; (*d2_ptr)[2] = 1.0;
123 
124  (*z1_ptr)[0] = 6.0; (*z2_ptr)[0] = 6.0;
125  (*z1_ptr)[1] = 11.0; (*z2_ptr)[1] = 4.0;
126  (*z1_ptr)[2] = 4.0; (*z2_ptr)[2] = 2.0;
127 
128  (*u_ptr)[1] = 1.0;
129 
130  ROL::Ptr<V> d1 = ROL::makePtr<SV>(d1_ptr);
131  ROL::Ptr<V> d2 = ROL::makePtr<SV>(d2_ptr);
132  ROL::Ptr<V> u = ROL::makePtr<SV>(u_ptr);
133  ROL::Ptr<V> v = ROL::makePtr<SV>(v_ptr);
134 
135  ROL::Ptr<LinOp> D1 = ROL::makePtr<DiagOp>(*d1);
136  ROL::Ptr<LinOp> NO = ROL::makePtr<NullOp>();
137  ROL::Ptr<LinOp> UV = ROL::makePtr<DyadOp>(u,v);
138  ROL::Ptr<LinOp> D2 = ROL::makePtr<DiagOp>(*d2);
139 
140 
141  RealT tol = 0.0;
142 
143  D1->apply(*x1,*x1,tol);
144  D1->applyInverse(*x1,*x1,tol);
145 
146  ROL::BlockOperator2<RealT> bkop(D1,NO,UV,D2);
147 
148 
149  bkop.apply(*y,*x,tol);
150 
151  z->axpy(-1.0,*y);
152 
153  errorFlag += static_cast<int>(z->norm()>errtol);
154 
155  }
156 
157  catch (std::logic_error& err) {
158 
159 
160  }; // end try
161 
162  if (errorFlag != 0)
163  std::cout << "End Result: TEST FAILED\n";
164  else
165  std::cout << "End Result: TEST PASSED\n";
166 
167  return 0;
168 }
169 
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:46
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
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
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.
int main(int argc, char *argv[])
Multiplication by zero.
constexpr auto dim