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 "ROL_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 typedef ROL::Vector<RealT> V;
49 typedef ROL::StdVector<RealT> SV;
51
52 ROL::GlobalMPISession mpiSession(&argc, &argv);
53
54 int iprint = argc - 1;
55
56 ROL::Ptr<std::ostream> outStream;
57 ROL::nullstream bhs; // no output
58
59 if( iprint>0 )
60 outStream = ROL::makePtrFromRef(std::cout);
61 else
62 outStream = ROL::makePtrFromRef(bhs);
63
64 int errorFlag = 0;
65
66 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
67
68 try {
69
70 PV::size_type nvec = 3;
71 std::vector<int> dim;
72
73 dim.push_back(4);
74 dim.push_back(3);
75 dim.push_back(5);
76
77 int total_dim = 0;
78
79 RealT left = -1e0, right = 1e0;
80
81 std::vector<ROL::Ptr<V> > x_ptr;
82 std::vector<ROL::Ptr<V> > y_ptr;
83 std::vector<ROL::Ptr<V> > z_ptr;
84
85 for( PV::size_type k=0; k<nvec; ++k ) {
86 ROL::Ptr<std::vector<RealT> > xk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
87 ROL::Ptr<std::vector<RealT> > yk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
88 ROL::Ptr<std::vector<RealT> > zk_ptr = ROL::makePtr<std::vector<RealT>>(dim[k]);
89
90 for( int i=0; i<dim[k]; ++i ) {
91 (*xk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
92 (*yk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
93 (*zk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
94 }
95
96 ROL::Ptr<V> xk = ROL::makePtr<SV>( xk_ptr );
97 ROL::Ptr<V> yk = ROL::makePtr<SV>( yk_ptr );
98 ROL::Ptr<V> zk = ROL::makePtr<SV>( zk_ptr );
99
100 x_ptr.push_back(xk);
101 y_ptr.push_back(yk);
102 z_ptr.push_back(zk);
103
104 total_dim += dim[k];
105 }
106
107 PV x(x_ptr);
108 ROL::Ptr<V> y = ROL::CreatePartitionedVector<RealT>(y_ptr[0],y_ptr[1],y_ptr[2]);
109 PV z(z_ptr);
110
111 // Standard tests.
112 std::vector<RealT> consistency = x.checkVector(*y, z, true, *outStream);
113 ROL::StdVector<RealT> checkvec( ROL::makePtrFromRef(consistency) );
114 if (checkvec.norm() > std::sqrt(errtol)) {
115 errorFlag++;
116 }
117
118 // Basis tests.
119 // set x to first basis vector
120 ROL::Ptr<ROL::Vector<RealT> > zp = x.clone();
121 zp = x.basis(0);
122 RealT znorm = zp->norm();
123 *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
124 if ( std::abs(znorm-1.0) > errtol ) {
125 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
126 errorFlag++;
127 };
128 // set x to middle basis vector
129 zp = x.basis(total_dim/2);
130 znorm = zp->norm();
131 *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
132 if ( std::abs(znorm-1.0) > errtol ) {
133 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
134 errorFlag++;
135 };
136 // set x to last basis vector
137 zp = x.basis(total_dim-1);
138 znorm = zp->norm();
139 *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
140 if ( std::abs(znorm-1.0) > errtol ) {
141 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
142 errorFlag++;
143 };
144
145 // Repeat the checkVector tests with a zero vector.
146 x.scale(0.0);
147 consistency = x.checkVector(x, x, true, *outStream);
148 if (checkvec.norm() > 0.0) {
149 errorFlag++;
150 }
151
152 if(argc>1) {
153 int m = atoi(argv[1]);
154 print_vector(*(x.basis(m)));
155
156 }
157
158
159
160
161 }
162 catch (std::logic_error& err) {
163 *outStream << err.what() << "\n";
164 errorFlag = -1000;
165 }; // end try
166
167 if (errorFlag != 0)
168 std::cout << "End Result: TEST FAILED\n";
169 else
170 std::cout << "End Result: TEST PASSED\n";
171
172 return 0;
173}
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