ROL
vector/test_04.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
15#include "ROL_StdVector.hpp"
16#include "ROL_Types.hpp"
17#include "ROL_Stream.hpp"
18#include "Teuchos_GlobalMPISession.hpp"
19
20typedef double RealT;
21
22
23template<class Real>
25
26 typedef ROL::Vector<Real> V;
27 typedef ROL::StdVector<Real> SV;
29 typedef typename PV::size_type size_type;
30
31 const PV eb = dynamic_cast<const PV&>(x);
32 size_type n = eb.numVectors();
33
34 for(size_type k=0; k<n; ++k) {
35 std::cout << "[subvector " << k << "]" << std::endl;
36 ROL::Ptr<const V> vec = eb.get(k);
37 ROL::Ptr<const std::vector<Real> > vp =
38 dynamic_cast<const SV&>(*vec).getVector();
39 for(size_type i=0;i<vp->size();++i) {
40 std::cout << (*vp)[i] << std::endl;
41 }
42 }
43}
44
45
46int main(int argc, char *argv[]) {
47
48 using namespace Teuchos;
49
50 typedef ROL::Vector<RealT> V;
51 typedef ROL::StdVector<RealT> SV;
53
54 GlobalMPISession mpiSession(&argc, &argv);
55
56 int iprint = argc - 1;
57
58 ROL::Ptr<std::ostream> outStream;
59 ROL::nullstream bhs; // no output
60
61 if( iprint>0 )
62 outStream = ROL::makePtrFromRef(std::cout);
63 else
64 outStream = ROL::makePtrFromRef(bhs);
65
66 int errorFlag = 0;
67
68 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
69
70 try {
71
72 PV::size_type nvec = 3;
73 std::vector<int> dim;
74
75 dim.push_back(4);
76 dim.push_back(3);
77 dim.push_back(5);
78
79 int total_dim = 0;
80
81 RealT left = -1e0, right = 1e0;
82
83 std::vector<ROL::Ptr<V> > x_ptr;
84 std::vector<ROL::Ptr<V> > y_ptr;
85 std::vector<ROL::Ptr<V> > z_ptr;
86
87 for( PV::size_type k=0; k<nvec; ++k ) {
88 ROL::Ptr<std::vector<RealT> > xk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
89 ROL::Ptr<std::vector<RealT> > yk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
90 ROL::Ptr<std::vector<RealT> > zk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
91
92 for( int i=0; i<dim[k]; ++i ) {
93 (*xk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
94 (*yk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
95 (*zk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
96 }
97
98 ROL::Ptr<V> xk = ROL::makePtr<SV>( xk_ptr );
99 ROL::Ptr<V> yk = ROL::makePtr<SV>( yk_ptr );
100 ROL::Ptr<V> zk = ROL::makePtr<SV>( zk_ptr );
101
102 x_ptr.push_back(xk);
103 y_ptr.push_back(yk);
104 z_ptr.push_back(zk);
105
106 total_dim += dim[k];
107 }
108
109 PV x(x_ptr);
110 ROL::Ptr<V> y = ROL::CreatePartitionedVector<RealT>(y_ptr[0],y_ptr[1],y_ptr[2]);
111 PV z(z_ptr);
112
113 // Standard tests.
114 std::vector<RealT> consistency = x.checkVector(*y, z, true, *outStream);
115 ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
116 if (checkvec.norm() > std::sqrt(errtol)) {
117 errorFlag++;
118 }
119
120 // Basis tests.
121 // set x to first basis vector
122 ROL::Ptr<ROL::Vector<RealT> > zp = x.clone();
123 zp = x.basis(0);
124 RealT znorm = zp->norm();
125 *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
126 if ( std::abs(znorm-1.0) > errtol ) {
127 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
128 errorFlag++;
129 };
130 // set x to middle basis vector
131 zp = x.basis(total_dim/2);
132 znorm = zp->norm();
133 *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
134 if ( std::abs(znorm-1.0) > errtol ) {
135 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
136 errorFlag++;
137 };
138 // set x to last basis vector
139 zp = x.basis(total_dim-1);
140 znorm = zp->norm();
141 *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
142 if ( std::abs(znorm-1.0) > errtol ) {
143 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
144 errorFlag++;
145 };
146
147 // Repeat the checkVector tests with a zero vector.
148 x.scale(0.0);
149 consistency = x.checkVector(x, x, true, *outStream);
150 if (checkvec.norm() > 0.0) {
151 errorFlag++;
152 }
153
154 if(argc>1) {
155 int m = atoi(argv[1]);
156 print_vector(*(x.basis(m)));
157
158 }
159
160
161
162
163 }
164 catch (std::logic_error& err) {
165 *outStream << err.what() << "\n";
166 errorFlag = -1000;
167 }; // end try
168
169 if (errorFlag != 0)
170 std::cout << "End Result: TEST FAILED\n";
171 else
172 std::cout << "End Result: TEST PASSED\n";
173
174 return 0;
175}
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.
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...
Real norm() const
Returns where .
Defines the linear algebra or vector space interface.
void print_vector(const ROL::Vector< Real > &x)
int main(int argc, char *argv[])
double RealT
constexpr auto dim