ROL
function/operator/test_01.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
62 #include "Teuchos_oblackholestream.hpp"
63 #include "Teuchos_GlobalMPISession.hpp"
64 
65 typedef double RealT;
66 
67 int main(int argc, char *argv[]) {
68 
69  using Teuchos::RCP; using Teuchos::rcp;
70 
71  typedef std::vector<RealT> vector;
72 
73  typedef ROL::StdVector<RealT> SV;
74 
75  typedef ROL::StdLinearOperator<RealT> StdLinearOperator;
76 
77 
78  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
79 
80  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
81  int iprint = argc - 1;
82  Teuchos::RCP<std::ostream> outStream;
83  Teuchos::oblackholestream bhs; // outputs nothing
84  if (iprint > 0)
85  outStream = Teuchos::rcp(&std::cout, false);
86  else
87  outStream = Teuchos::rcp(&bhs, false);
88 
89  // Save the format state of the original std::cout.
90  Teuchos::oblackholestream oldFormatState;
91  oldFormatState.copyfmt(std::cout);
92 
93  int errorFlag = 0;
94 
95  // *** Test body.
96 
97  try {
98 
99  RCP<vector> a_rcp = rcp( new vector {4.0,2.0,1.0,3.0} );
100  RCP<vector> ai_rcp = rcp( new vector {3.0/10.0, -2.0/10.0, -1.0/10.0, 4.0/10.0} );
101 
102  RCP<vector> x1_rcp = rcp( new vector {1.0,-1.0} );
103  RCP<vector> b1_rcp = rcp( new vector(2) );
104 
105  RCP<vector> x2_rcp = rcp( new vector(2) );
106  RCP<vector> b2_rcp = rcp( new vector {3.0,-1.0} );
107 
108  RCP<vector> y3_rcp = rcp( new vector {-2.0,1.0} );
109  RCP<vector> c3_rcp = rcp( new vector(2) );
110 
111  RCP<vector> y4_rcp = rcp( new vector(2) );
112  RCP<vector> c4_rcp = rcp( new vector {-6.0,1.0} );
113 
114  StdLinearOperator A(a_rcp);
115  StdLinearOperator Ai(ai_rcp);
116 
117  SV x1(x1_rcp); SV x2(x2_rcp); SV y3(y3_rcp); SV y4(y4_rcp);
118  SV b1(b1_rcp); SV b2(b2_rcp); SV c3(c3_rcp); SV c4(c4_rcp);
119 
120  RealT tol = ROL::ROL_EPSILON<RealT>();
121 
122  // Test 1
123  *outStream << "\nTest 1: Matrix multiplication" << std::endl;
124  A.apply(b1,x1,tol);
125  *outStream << "x = [" << (*x1_rcp)[0] << "," << (*x1_rcp)[1] << "]" << std::endl;
126  *outStream << "b = [" << (*b1_rcp)[0] << "," << (*b1_rcp)[1] << "]" << std::endl;
127  b1.axpy(-1.0,b2);
128 
129  RealT error1 = b1.norm();
130  errorFlag += error1 > tol;
131  *outStream << "Error = " << error1 << std::endl;
132 
133  // Test 2
134  *outStream << "\nTest 2: Linear solve" << std::endl;
135  A.applyInverse(*x2_rcp,*b2_rcp,tol);
136  *outStream << "x = [" << (*x2_rcp)[0] << "," << (*x2_rcp)[1] << "]" << std::endl;
137  *outStream << "b = [" << (*b2_rcp)[0] << "," << (*b2_rcp)[1] << "]" << std::endl;
138  x2.axpy(-1.0,x1);
139 
140  RealT error2 = x2.norm();
141  errorFlag += error2 > tol;
142  *outStream << "Error = " << error2 << std::endl;
143 
144  // Test 3
145  *outStream << "\nTest 3: Transposed matrix multiplication" << std::endl;
146  A.applyAdjoint(*c3_rcp,*y3_rcp,tol);
147  *outStream << "y = [" << (*y3_rcp)[0] << "," << (*y3_rcp)[1] << "]" << std::endl;
148  *outStream << "c = [" << (*c3_rcp)[0] << "," << (*c3_rcp)[1] << "]" << std::endl;
149  c3.axpy(-1.0,c4);
150 
151  RealT error3 = c3.norm();
152  errorFlag += error3 > tol;
153  *outStream << "Error = " << error3 << std::endl;
154 
155  // Test 4
156  *outStream << "\nTest 4: Linear solve with transpose" << std::endl;
157  A.applyAdjointInverse(y4,c4,tol);
158  *outStream << "y = [" << (*y4_rcp)[0] << "," << (*y4_rcp)[1] << "]" << std::endl;
159  *outStream << "c = [" << (*c4_rcp)[0] << "," << (*c4_rcp)[1] << "]" << std::endl;
160  y4.axpy(-1.0,y3);
161 
162  RealT error4 = y4.norm();
163  errorFlag += error4 > tol;
164  *outStream << "Error = " << error4 << std::endl;
165 
166  *outStream << "x1 = "; x1.print(*outStream);
167  Ai.applyInverse(b1,x1,tol);
168  *outStream << "b1 = "; b1.print(*outStream);
169  A.apply(b1,x1,tol);
170  *outStream << "b1 = "; b1.print(*outStream);
171  A.applyInverse(x1,b1,tol);
172  *outStream << "x1 = "; x1.print(*outStream);
173  Ai.apply(x1,b1,tol);
174  *outStream << "x1 = "; x1.print(*outStream);
175 
176 
177  }
178  catch (std::logic_error err) {
179  *outStream << err.what() << "\n";
180  errorFlag = -1000;
181  }; // end try
182 
183  if (errorFlag != 0)
184  std::cout << "End Result: TEST FAILED\n";
185  else
186  std::cout << "End Result: TEST PASSED\n";
187 
188  // reset format state of std::cout
189  std::cout.copyfmt(oldFormatState);
190 
191  return 0;
192 
193 }
194 
Provides the std::vector implementation to apply a linear operator, which is a std::vector representa...
Provides the std::vector implementation of the ROL::Vector interface.
int main(int argc, char *argv[])