ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
vector
vector/test_01.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
13
14
#include "
ROL_StdVector.hpp
"
15
#include "
ROL_Types.hpp
"
16
#include "
ROL_Stream.hpp
"
17
#include "ROL_GlobalMPISession.hpp"
18
19
#include <iostream>
20
21
typedef
double
RealT
;
22
typedef
double
ElementT
;
23
24
int
main
(
int
argc,
char
*argv[]) {
25
26
ROL::GlobalMPISession mpiSession(&argc, &argv);
27
28
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29
int
iprint = argc - 1;
30
ROL::Ptr<std::ostream> outStream;
31
ROL::nullstream
bhs;
// outputs nothing
32
if
(iprint > 0)
33
outStream = ROL::makePtrFromRef(std::cout);
34
else
35
outStream = ROL::makePtrFromRef(bhs);
36
37
int
errorFlag = 0;
38
39
RealT
errtol =
ROL::ROL_THRESHOLD<RealT>
();
40
41
// *** Test body.
42
43
try
{
44
45
// Number of elements in StdVector objects
46
int
dim
= 100;
47
48
ROL::StdVector<RealT, ElementT>
x(
dim
);
49
ROL::StdVector<RealT, ElementT>
y(
dim
);
50
ROL::StdVector<RealT, ElementT>
z(
dim
);
51
52
// Lower and upper bounds on randomized vector element values
53
RealT
left = -1e0, right = 1e0;
54
55
// set x,y,z
56
x.
randomize
(left,right);
57
y.
randomize
(left,right);
58
z.
randomize
(left,right);
59
60
// Standard tests.
61
auto
consistency = x.
checkVector
(y, z,
true
, *outStream);
62
63
ROL::StdVector<RealT, ElementT>
checkvec( ROL::makePtrFromRef(consistency) );
64
65
if
(checkvec.
norm
() > std::sqrt(
ROL::ROL_EPSILON<RealT>
())) errorFlag++;
66
67
// Basis tests.
68
// set x to first basis vector
69
auto
zp = x.
basis
(0);
70
RealT
znorm = zp->norm();
71
*outStream <<
"Norm of ROL::Vector z (first basis vector): "
<< znorm <<
"\n"
;
72
if
( std::abs(znorm-1.0) > errtol ) {
73
*outStream <<
"---> POSSIBLE ERROR ABOVE!\n"
;
74
errorFlag++;
75
};
76
77
// set x to middle basis vector
78
zp = x.
basis
(
dim
/2);
79
znorm = zp->norm();
80
*outStream <<
"\nNorm of ROL::Vector z ('middle' basis vector): "
<< znorm <<
"\n"
;
81
82
if
( std::abs(znorm-1.0) > errtol ) {
83
*outStream <<
"---> POSSIBLE ERROR ABOVE!\n"
;
84
errorFlag++;
85
};
86
87
// set x to last basis vector
88
zp = x.
basis
(
dim
-1);
89
znorm = zp->norm();
90
*outStream <<
"\nNorm of ROL::Vector z (last basis vector): "
<< znorm <<
"\n"
;
91
if
( std::abs(znorm-1.0) > errtol ) {
92
*outStream <<
"---> POSSIBLE ERROR ABOVE!\n"
;
93
errorFlag++;
94
};
95
96
// Repeat the checkVector tests with a zero vector.
97
x.
scale
(0.0);
98
consistency = x.
checkVector
(x, x,
true
, *outStream);
99
if
(checkvec.
norm
() > 0.0) {
100
errorFlag++;
101
}
102
}
103
104
catch
(std::logic_error& err) {
105
*outStream << err.what() <<
"\n"
;
106
errorFlag = -1000;
107
};
// end try
108
109
if
(errorFlag != 0) std::cout <<
"End Result: TEST FAILED\n"
;
110
else
std::cout <<
"End Result: TEST PASSED\n"
;
111
112
return
0;
113
114
}
115
ROL_StdVector.hpp
ROL_Stream.hpp
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ROL_Types.hpp
Contains definitions of custom data types in ROL.
ROL::ROL::Vector::checkVector
virtual std::vector< Real > checkVector(const Vector< Real > &x, const Vector< Real > &y, const bool printToStream=true, std::ostream &outStream=std::cout) const
Verify vector-space methods.
Definition
ROL_Constraint_SerialSimOpt.hpp:292
ROL::StdVector
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Definition
ROL_StdVector.hpp:27
ROL::StdVector::scale
void scale(const Real alpha)
Compute where .
Definition
ROL_StdVector.hpp:84
ROL::StdVector::randomize
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
Definition
ROL_StdVector.hpp:178
ROL::StdVector::norm
Real norm() const
Returns where .
Definition
ROL_StdVector.hpp:109
ROL::StdVector::basis
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
Definition
ROL_StdVector.hpp:127
RealT
double RealT
Definition
function/constraint/test_01.cpp:29
ROL::details::nullstream
basic_nullstream< char, std::char_traits< char > > nullstream
Definition
ROL_Stream.hpp:36
ROL::ROL_EPSILON
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition
ROL_Types.hpp:57
ROL::ROL_THRESHOLD
Real ROL_THRESHOLD(void)
Tolerance for various equality tests.
Definition
ROL_Types.hpp:63
main
int main(int argc, char *argv[])
Definition
vector/test_01.cpp:24
ElementT
double ElementT
Definition
vector/test_01.cpp:22
dim
constexpr auto dim
Definition
vector/test_11.cpp:23
Generated by
1.17.0