Debian Codegraph @

packages/rol/adapters/teuchos/test/vector/test_01.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 Teuchos::SerialDenseVector interface.
12 */
13
14 #include "ROL_RandomVector.hpp"
15 #include "ROL_StdVector.hpp"
16 #include "ROL_TeuchosVector.hpp"
17 #include "ROL_Stream.hpp"
18 #include "Teuchos_GlobalMPISession.hpp"
19
20 #include <iostream>
21
22 int main(int argc, char *argv[]) {
23
24
25 using OrdinalT = int;
26 using RealT = double;
27 using Vector = ROL::Vector<RealT>;
28 using StdVector = ROL::StdVector<RealT>;
29 using TeuchosVector = ROL::TeuchosVector<OrdinalT,RealT>;
30
31 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
32
33 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
34 int iprint = argc - 1;
35 ROL::Ptr<std::ostream> outStream;
36 ROL::nullstream bhs; // outputs nothing
37 if (iprint > 0)
38 outStream = ROL::makePtrFromRef(std::cout);
39 else
40 outStream = ROL::makePtrFromRef(bhs);
41
42 int errorFlag = 0;
43
44 RealT errtol = ROL::ROL_THRESHOLD<RealT>();
45
46 // *** Test body.
47
48 try {
49 OrdinalT dim = 10;
50
51 TeuchosVector x(dim);
52 TeuchosVector y(dim);
53 TeuchosVector z(dim);
54
55 *outStream << x.dimension() << std::endl;
56
57 RealT left = -1e0, right = 1e0;
58
59 ROL::RandomizeVector(x, left, right );
60 ROL::RandomizeVector(y, left, right );
61 ROL::RandomizeVector(z, left, right );
62
63 // Standard tests.
64 std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream);
65 StdVector checkvec( ROL::makePtrFromRef(consistency) );
66 if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON<RealT>())) {
67
68 errorFlag++;
69 }
70
71 // Basis tests.
72 // set x to first basis vector
73 ROL::Ptr<Vector> zp = x.basis(0);
74 RealT znorm = zp->norm();
75 *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n";
76 if ( std::abs(znorm-1.0) > errtol ) {
77 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
78 errorFlag++;
79 };
80 // set x to middle basis vector
81 zp = x.basis(dim/2);
82 znorm = zp->norm();
83 *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n";
84 if ( std::abs(znorm-1.0) > errtol ) {
85 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
86 errorFlag++;
87 };
88 // set x to last basis vector
89 zp = x.basis(dim-1);
90 znorm = zp->norm();
91 *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n";
92 if ( std::abs(znorm-1.0) > errtol ) {
93 *outStream << "---> POSSIBLE ERROR ABOVE!\n";
94 errorFlag++;
95 };
96
97 // Repeat the checkVector tests with a zero vector.
98 x.scale(0.0);
99 consistency = x.checkVector(x, x, true, *outStream);
100 if (checkvec.norm() > 0.0) {
101 errorFlag++;
102 }
103 }
104 catch (std::logic_error& err) {
105 *outStream << err.what() << "\n";
106 errorFlag = -1000;
107 }; // end try
108
109 if (errorFlag != 0)
110 std::cout << "End Result: TEST FAILED\n";
111 else
112 std::cout << "End Result: TEST PASSED\n";
113 return 0;
114
115 }
116