ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
test
vector
vector/test_10.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
10
#include "ROL_GlobalMPISession.hpp"
11
12
#include "
ROL_Objective.hpp
"
13
14
#include "
ROL_StdVector.hpp
"
15
#include "
ROL_PartitionedVector.hpp
"
16
#include "
ROL_RandomVector.hpp
"
17
#include "
ROL_VectorWorkspace.hpp
"
18
#include "
ROL_Stream.hpp
"
19
20
#include <iomanip>
21
22
using
RealT
= double;
23
using
size_type
=
typename
std::vector<RealT>::size_type;
24
25
namespace
details
{
26
27
using namespace
ROL
;
28
29
// An alternative, more expensive way to compute the dot product!
30
template
<
typename
Real>
31
class
PolarizationIdentity
{
32
private
:
33
34
mutable
VectorWorkspace<Real>
workspace_
;
35
36
public
:
37
38
Real
dot
(
const
Vector<Real>
& x,
const
Vector<Real>
& y ) {
39
40
auto
w =
workspace_
.copy(x);
41
auto
z =
workspace_
.copy(x);
42
w->plus(y);
43
z->axpy(-1.0,y);
44
auto
result = 0.25*( w->dot(*w) - z->dot(*z) );
45
return
result;
46
}
// w and z should go out of scope - decrement ref counts
47
48
void
status
( std::ostream& os )
const
{
workspace_
.status(os); }
49
};
50
51
52
53
54
}
// namespace details
55
56
using
details::PolarizationIdentity
;
57
58
59
int
main
(
int
argc,
char
* argv[] ) {
60
61
using namespace
ROL
;
62
63
ROL::GlobalMPISession mpiSession(&argc, &argv);
64
65
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
66
auto
outStream =
makeStreamPtr
( std::cout, argc > 1 );
67
68
int
errorFlag = 0;
69
RealT
errtol = std::sqrt(
ROL_EPSILON<RealT>
());
70
71
try
{
72
73
PolarizationIdentity<RealT>
polar;
74
75
size_type
N = 20;
76
77
auto
xp = makePtr<std::vector<RealT>>(N);
78
auto
x = makePtr<StdVector<RealT>>(xp);
79
auto
y = x->clone();
80
81
auto
xx =
PartitionedVector<RealT>::create
({x,x});
82
auto
yy =
PartitionedVector<RealT>::create
({y,y});
83
84
RandomizeVector
( *x );
85
RandomizeVector
( *y );
86
87
auto
result = 0.5*polar.
dot
(*x,*y);
88
result += 0.5*polar.
dot
(*y,*x);
89
90
auto
x_dot_y = x->dot(*y);
91
errorFlag += ( std::abs( x_dot_y - result ) > errtol );
92
93
*outStream << std::setprecision(16) << x_dot_y << std::endl;
94
*outStream << std::setprecision(16) << result << std::endl;
95
96
97
polar.
dot
(*xx,*yy);
98
99
polar.
status
(*outStream);
100
101
}
102
catch
(std::logic_error& err) {
103
*outStream << err.what() <<
"\n"
;
104
errorFlag = -1000;
105
};
// end try
106
107
if
(errorFlag != 0)
108
std::cout <<
"End Result: TEST FAILED\n"
;
109
else
110
std::cout <<
"End Result: TEST PASSED\n"
;
111
112
return
0;
113
}
114
ROL_Objective.hpp
size_type
typename PV< Real >::size_type size_type
Definition
ROL_Objective_SerialSimOpt.hpp:56
ROL_PartitionedVector.hpp
ROL_RandomVector.hpp
ROL_StdVector.hpp
ROL_Stream.hpp
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
ROL_VectorWorkspace.hpp
ROL::PartitionedVector::create
static Ptr< PartitionedVector > create(std::initializer_list< Vp > vs)
Definition
ROL_PartitionedVector.hpp:244
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
ROL::details::VectorWorkspace
Definition
ROL_VectorWorkspace.hpp:64
details::PolarizationIdentity
Definition
vector/test_10.cpp:31
details::PolarizationIdentity::workspace_
VectorWorkspace< Real > workspace_
Definition
vector/test_10.cpp:34
details::PolarizationIdentity::status
void status(std::ostream &os) const
Definition
vector/test_10.cpp:48
details::PolarizationIdentity::dot
Real dot(const Vector< Real > &x, const Vector< Real > &y)
Definition
vector/test_10.cpp:38
RealT
double RealT
Definition
function/constraint/test_01.cpp:29
ROL::RandomizeVector
void RandomizeVector(Vector< Real > &x, const Real &lower=0.0, const Real &upper=1.0)
Fill a ROL::Vector with uniformly-distributed random numbers in the interval [lower,...
Definition
ROL_RandomVector.hpp:30
ROL
Definition
ROL_ElementwiseVector.hpp:27
ROL::makeStreamPtr
Ptr< std::ostream > makeStreamPtr(std::ostream &os, bool noSuppressOutput=true)
Definition
ROL_Stream.hpp:39
ROL::ROL_EPSILON
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition
ROL_Types.hpp:57
details
Definition
vector/test_10.cpp:25
main
int main(int argc, char *argv[])
Definition
vector/test_10.cpp:59
Generated by
1.17.0