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"
22#include "ROL_Elementwise_Function.hpp"
24#include "ROL_StdVector.hpp"
25#include "ROL_Types.hpp"
26
27#include "ROL_Stream.hpp"
28#include "ROL_GlobalMPISession.hpp"
29
30template<class Real>
31void 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
51typedef double RealT;
52
53int 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 luint;
67
68 ROL::GlobalMPISession mpiSession(&argc, &argv);
69
70 int iprint = argc - 1;
71
72 ROL::Ptr<std::ostream> outStream;
73 ROL::nullstream bhs; // no output
74
75 if( iprint>0 )
76 outStream = ROL::makePtrFromRef(std::cout);
77 else
78 outStream = ROL::makePtrFromRef(bhs);
79
80 int errorFlag = 0;
81
82 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
83
84 try {
85
86 luint dim = 3; // Number of elements in each subvector (could be different)
87
88 ROL::Ptr<vector> x1_ptr = ROL::makePtr<vector>(dim,1.0);
89 ROL::Ptr<vector> x2_ptr = ROL::makePtr<vector>(dim,2.0);
90
91 ROL::Ptr<vector> y1_ptr = ROL::makePtr<vector>(dim,0.0);
92 ROL::Ptr<vector> y2_ptr = ROL::makePtr<vector>(dim,0.0);
93
94 ROL::Ptr<vector> z1_ptr = ROL::makePtr<vector>(dim,0.0);
95 ROL::Ptr<vector> z2_ptr = ROL::makePtr<vector>(dim,0.0);
96
97 ROL::Ptr<V> x1 = ROL::makePtr<SV>( x1_ptr);
98 ROL::Ptr<V> x2 = ROL::makePtr<SV>( x2_ptr);
99
100 ROL::Ptr<V> y1 = ROL::makePtr<SV>( y1_ptr);
101 ROL::Ptr<V> y2 = ROL::makePtr<SV>( y2_ptr);
102
103 ROL::Ptr<V> z1 = ROL::makePtr<SV>( z1_ptr);
104 ROL::Ptr<V> z2 = ROL::makePtr<SV>( z2_ptr);
105
106 ROL::Ptr<V> x = ROL::CreatePartitionedVector( x1, x2 );
107 ROL::Ptr<V> y = ROL::CreatePartitionedVector( y1, y2 );
108 ROL::Ptr<V> z = ROL::CreatePartitionedVector( z1, z2 );
109
110 // Operator diagonals
111 ROL::Ptr<vector> d1_ptr = ROL::makePtr<vector>(dim,0.0);
112 ROL::Ptr<vector> d2_ptr = ROL::makePtr<vector>(dim,0.0);
113
114 // Dyadic components
115 ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(dim,0.0);
116 ROL::Ptr<vector> v_ptr = ROL::makePtr<vector>(dim,1.0);
117
118 (*d1_ptr)[0] = 6.0; (*d2_ptr)[0] = 3.0;
119 (*d1_ptr)[1] = 5.0; (*d2_ptr)[1] = 2.0;
120 (*d1_ptr)[2] = 4.0; (*d2_ptr)[2] = 1.0;
121
122 (*z1_ptr)[0] = 6.0; (*z2_ptr)[0] = 6.0;
123 (*z1_ptr)[1] = 11.0; (*z2_ptr)[1] = 4.0;
124 (*z1_ptr)[2] = 4.0; (*z2_ptr)[2] = 2.0;
125
126 (*u_ptr)[1] = 1.0;
127
128 ROL::Ptr<V> d1 = ROL::makePtr<SV>(d1_ptr);
129 ROL::Ptr<V> d2 = ROL::makePtr<SV>(d2_ptr);
130 ROL::Ptr<V> u = ROL::makePtr<SV>(u_ptr);
131 ROL::Ptr<V> v = ROL::makePtr<SV>(v_ptr);
132
133 ROL::Ptr<LinOp> D1 = ROL::makePtr<DiagOp>(*d1);
134 ROL::Ptr<LinOp> NO = ROL::makePtr<NullOp>();
135 ROL::Ptr<LinOp> UV = ROL::makePtr<DyadOp>(u,v);
136 ROL::Ptr<LinOp> D2 = ROL::makePtr<DiagOp>(*d2);
137
138
139 RealT tol = 0.0;
140
141 D1->apply(*x1,*x1,tol);
142 D1->applyInverse(*x1,*x1,tol);
143
144 ROL::BlockOperator2<RealT> bkop(D1,NO,UV,D2);
145
146
147 bkop.apply(*y,*x,tol);
148
149 z->axpy(-1.0,*y);
150
151 errorFlag += static_cast<int>(z->norm()>errtol);
152
153 }
154
155 catch (std::logic_error& err) {
156
157
158 }; // end try
159
160 if (errorFlag != 0)
161 std::cout << "End Result: TEST FAILED\n";
162 else
163 std::cout << "End Result: TEST PASSED\n";
164
165 return 0;
166}
167
Vector< Real > V
PartitionedVector< Real > PV
typename PV< Real >::size_type size_type
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Contains definitions of custom data types in ROL.
Provides the interface to apply a 2x2 block operator to a partitioned vector.
Provides the interface to apply a diagonal operator which acts like elementwise multiplication when a...
Interface to apply a dyadic operator to a vector.
Provides the interface to apply a linear operator.
Multiplication by zero.
Defines the linear algebra of vector space on a generic partitioned vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
int main(int argc, char *argv[])
ROL::Ptr< Vector< Real > > CreatePartitionedVector(const ROL::Ptr< Vector< Real > > &a)
void print_vector(const ROL::Vector< Real > &x)
constexpr auto dim