Debian Codegraph @

packages/rol/adapters/tpetra/test/vector/test_03.cpp

raw
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 /*! \file test_01.cpp
11 \brief Test Tpetra_MultiVector interface.
12 */
13
14
15 #include "ROL_ScaledTpetraMultiVector.hpp"
16 #include "ROL_StdVector.hpp"
17
18 #include "ROL_Stream.hpp"
19 #include "Teuchos_GlobalMPISession.hpp"
20
21 #include "Tpetra_Core.hpp"
22
23 typedef double RealT;
24 typedef double ElementT;
25
26 typedef Tpetra::Map<>::local_ordinal_type LO;
27 typedef Tpetra::Map<>::global_ordinal_type GO;
28 typedef Tpetra::Map<>::node_type Node;
29 typedef Tpetra::Map<LO, GO, Node> Map;
30 typedef Tpetra::MultiVector<RealT, LO, GO, Node> MV;
31 typedef Tpetra::Vector<RealT, LO, GO, Node> V;
32 typedef ROL::Ptr<MV> MVP;
33 typedef ROL::Ptr<V> VP;
34
35 int main(int argc, char *argv[]) {
36
37 Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
38 ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
39
40 int iprint = argc - 1;
41 ROL::nullstream bhs; // outputs nothing
42 std::ostream& outStream = (iprint > 0) ? std::cout : bhs;
43
44 int errorFlag = 0;
45
46 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
47
48 try {
49 // Dimension of the optimization vector
50
51 int dim = 10;
52
53 ROL::Ptr<Map> map = ROL::makePtr<Map>(dim,0,comm);
54
55 // Create Tpetra::MultiVectors (single vectors)
56 MVP x_ptr = ROL::makePtr<MV>(map,1,true);
57 MVP y_ptr = ROL::makePtr<MV>(map,1,true);
58 VP W_ptr = ROL::makePtr<V>(map,true);
59
60 // Random elements
61 //x_ptr->randomize();
62 //y_ptr->randomize();
63 x_ptr->putScalar(1.0);
64 y_ptr->putScalar(1.0);
65
66 // Set all values to 2
67 W_ptr->putScalar(2.0);
68
69 // Create ROL vectors
70 ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> x(x_ptr,W_ptr);
71 ROL::DualScaledTpetraMultiVector<RealT,LO,GO,Node> y(y_ptr,W_ptr);
72
73 // const ROL::Vector<RealT> &g = x.dual();
74 // const ROL::Vector<RealT> &h = x.dual();
75 // RealT hnorm = h.norm();
76 // RealT gnorm = g.norm();
77
78 RealT xy = x.dot(y.dual());
79 RealT yx = y.dot(x.dual());
80
81 outStream << "\nAbsolute error between x.dot(y.dual()) and y.dot(x.dual()): "
82 << std::abs(xy-yx) << "\n";
83 outStream << "x.dot(y.dual()): " << xy << "\n";
84 outStream << "y.dot(x.dual()): " << yx << "\n";
85 if ( std::abs(xy-yx) > errtol ) {
86 outStream << "---> POSSIBLE ERROR ABOVE!\n";
87 errorFlag++;
88 }
89
90 RealT xx = std::sqrt(x.dot(x)), xnorm = x.norm();
91 RealT yy = std::sqrt(y.dot(y)), ynorm = y.norm();
92
93 outStream << "\nAbsolute error between sqrt(x.dot(x)) and x.norm(): "
94 << std::abs(xx-xnorm) << "\n";
95 outStream << "sqrt(x.dot(x)): " << xx << "\n";
96 outStream << "x.norm(): " << xnorm << "\n";
97 if ( std::abs(xx-xnorm) > errtol ) {
98 outStream << "---> POSSIBLE ERROR ABOVE!\n";
99 errorFlag++;
100 }
101
102 outStream << "\nAbsolute error between sqrt(y.dot(y)) and y.norm(): "
103 << std::abs(yy-ynorm) << "\n";
104 outStream << "sqrt(y.dot(y)): " << yy << "\n";
105 outStream << "y.norm(): " << ynorm << "\n";
106 if ( std::abs(yy-ynorm) > errtol ) {
107 outStream << "---> POSSIBLE ERROR ABOVE!\n";
108 errorFlag++;
109 }
110
111 // clone z from x, deep copy x into z, norm of z
112 ROL::Ptr<ROL::Vector<RealT> > z = x.clone();
113 z->set(x);
114 RealT znorm = z->norm();
115 outStream << "\nNorm of ROL::Vector z (clone of x): " << znorm << "\n";
116 if ( std::abs(xnorm - znorm) > errtol ) {
117 outStream << "---> POSSIBLE ERROR ABOVE!\n";
118 errorFlag++;
119 }
120 ROL::Ptr<ROL::Vector<RealT> > w = y.clone();
121 w = y.clone();
122 w->set(y);
123 RealT wnorm = w->norm();
124 outStream << "\nNorm of ROL::Vector w (clone of y): " << wnorm << "\n";
125 if ( std::abs(ynorm - wnorm) > errtol ) {
126 outStream << "---> POSSIBLE ERROR ABOVE!\n";
127 errorFlag++;
128 }
129
130 // Standard tests.
131 // Create Tpetra::MultiVectors (single vectors)
132 MVP x1_ptr = ROL::makePtr<MV>(map,1,true);
133 MVP y1_ptr = ROL::makePtr<MV>(map,1,true);
134 MVP z1_ptr = ROL::makePtr<MV>(map,1,true);
135 ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> x1(x1_ptr,W_ptr);
136 ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> y1(y1_ptr,W_ptr);
137 ROL::PrimalScaledTpetraMultiVector<RealT,LO,GO,Node> z1(z1_ptr,W_ptr);
138 x1_ptr->randomize();
139 y1_ptr->randomize();
140 z1_ptr->randomize();
141
142 std::vector<RealT> consistency = x1.checkVector(y1, z1, true, outStream);
143 ROL::StdVector<RealT> checkvec(ROL::makePtrFromRef(consistency));
144 if (checkvec.norm() > std::sqrt(errtol)) {
145 errorFlag++;
146 }
147
148 }
149
150 catch (std::logic_error& err) {
151 outStream << err.what() << "\n";
152 errorFlag = -1000;
153 }; // end try
154
155 if (errorFlag != 0)
156 std::cout << "End Result: TEST FAILED\n";
157 else
158 std::cout << "End Result: TEST PASSED\n";
159
160 return 0;
161 }