Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraLinearOp_UnitTests.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 1//
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include "Thyra_EpetraLinearOp.hpp"
45 #include "Thyra_ScaledLinearOpBase.hpp"
46 #include "Thyra_RowStatLinearOpBase.hpp"
47 #include "Thyra_LinearOpTester.hpp"
48 #include "Thyra_DefaultBlockedLinearOp.hpp"
49 #include "Thyra_VectorBase.hpp"
50 #include "Thyra_VectorStdOps.hpp"
51 #include "Thyra_MultiVectorBase.hpp"
52 #include "Thyra_MultiVectorStdOps.hpp"
54 
56 
57 
58 namespace {
59 
60 
61 bool g_dumpAll = false;
62 const int g_dim = 4;
63 
64 
66 {
68  "dump-all", "no-dump-all", &g_dumpAll,
69  "Dump lots of data" );
70 }
71 
72 
73 } // namespace
74 
75 
76 namespace Thyra {
77 
78 
79 //
80 // Unit Tests
81 //
82 
83 
84 TEUCHOS_UNIT_TEST( EpetraLinearOp, ScaledLinearOpBase )
85 {
86  using Teuchos::null;
87  using Teuchos::inOutArg;
89  using Teuchos::rcp_dynamic_cast;
90  typedef ScalarTraits<double> ST;
91 
92  // Set up an EpetraLinearOp
93 
94  const RCP<const Epetra_Comm> comm = getEpetraComm();
95  const int numLocalRows = g_localDim;
96  const int numRows = numLocalRows * comm->NumProc();
97  const int numCols = numLocalRows / 2;
98  const RCP<Epetra_CrsMatrix> epetraCrsM = getEpetraMatrix(numRows, numCols);
99  const RCP<LinearOpBase<double> > epetraOp = nonconstEpetraLinearOp(epetraCrsM);
100 
101  const RCP<ScaledLinearOpBase<double> > scaledOp =
102  rcp_dynamic_cast<ScaledLinearOpBase<double> >(epetraOp, true);
103 
104  // Get the original mat-vec
105 
106  const double two = 2.0;
107 
108  const RCP<VectorBase<double> > rhs_vec =
109  createMember<double>(epetraOp->domain());
110  assign<double>(rhs_vec.ptr(), two);
111 
112  const RCP<VectorBase<double> > lhs_orig_vec =
113  createMember<double>(epetraOp->range());
114 
115  apply<double>(*epetraOp, Thyra::NOTRANS, *rhs_vec, lhs_orig_vec.ptr());
116 
117  if (g_dumpAll) {
118  out << "epetraOp = " << *epetraOp;
119  out << "rhs_vec = " << *rhs_vec;
120  out << "lhs_orig_vec = " << *lhs_orig_vec;
121  }
122 
123  // Scale the op from the left (row scaling)
124 
125  const double three = 3.0;
126 
127  const RCP<VectorBase<double> > row_scaling =
128  createMember<double>(epetraOp->range());
129  assign<double>(row_scaling.ptr(), three);
130 
131  scaledOp->scaleLeft(*row_scaling);
132 
133  if (g_dumpAll) {
134  out << "row_scaling = " << *row_scaling;
135  out << "epetraOp left scaled = " << *epetraOp;
136  }
137 
138  // Test that resulting left scaling
139 
140  const RCP<VectorBase<double> > lhs_left_scaled_vec =
141  createMember<double>(epetraOp->range());
142 
143  apply<double>(*epetraOp, NOTRANS, *rhs_vec, lhs_left_scaled_vec.ptr());
144 
145  if (g_dumpAll) {
146  out << "lhs_left_scaled_vec = " << *lhs_left_scaled_vec;
147  }
148 
150  three * sum<double>(*lhs_orig_vec),
151  sum<double>(*lhs_left_scaled_vec),
152  as<double>(10.0 * ST::eps())
153  );
154 
155  // Left scale the matrix back
156 
157  const RCP<VectorBase<double> > inv_row_scaling =
158  createMember<double>(epetraOp->range());
159  reciprocal<double>(*row_scaling, inv_row_scaling.ptr());
160 
161  scaledOp->scaleLeft(*inv_row_scaling);
162 
163  if (g_dumpAll) {
164  out << "inv_row_scaling = " << *row_scaling;
165  out << "epetraOp left scaled back to orig = " << *epetraOp;
166  }
167 
168  const RCP<VectorBase<double> > lhs_orig2_vec =
169  createMember<double>(epetraOp->range());
170 
171  apply<double>(*epetraOp, NOTRANS, *rhs_vec, lhs_orig2_vec.ptr());
172 
173  if (g_dumpAll) {
174  out << "lhs_orig2_vec = " << *lhs_orig2_vec;
175  }
176 
178  sum<double>(*lhs_orig_vec),
179  sum<double>(*lhs_orig2_vec),
180  as<double>(10.0 * ST::eps())
181  );
182  // NOTE: Above, it would ask for exact binary match except if one uses
183  // threading it will not match exactly!
184 
185  // Scale the op from the right (col scaling)
186 
187  const double four = 4.0;
188 
189  const RCP<VectorBase<double> > col_scaling =
190  createMember<double>(epetraOp->domain());
191  assign<double>(col_scaling.ptr(), four);
192 
193  scaledOp->scaleRight(*col_scaling);
194 
195  if (g_dumpAll) {
196  out << "col_scaling = " << *col_scaling;
197  out << "epetraOp right scaled = " << *epetraOp;
198  }
199 
200  // Test that resulting right scaling
201 
202  const RCP<VectorBase<double> > lhs_right_scaled_vec =
203  createMember<double>(epetraOp->range());
204 
205  apply<double>(*epetraOp, NOTRANS, *rhs_vec, lhs_right_scaled_vec.ptr());
206 
207  if (g_dumpAll) {
208  out << "lhs_right_scaled_vec = " << *lhs_right_scaled_vec;
209  }
210 
212  four * sum<double>(*lhs_orig_vec),
213  sum<double>(*lhs_right_scaled_vec),
214  as<double>(10.0 * ST::eps())
215  );
216 
217  // Right scale the matrix back
218 
219  const RCP<VectorBase<double> > inv_col_scaling =
220  createMember<double>(epetraOp->domain());
221  reciprocal<double>(*col_scaling, inv_col_scaling.ptr());
222 
223  scaledOp->scaleRight(*inv_col_scaling);
224 
225  if (g_dumpAll) {
226  out << "inv_col_scaling = " << *col_scaling;
227  out << "epetraOp right scaled back to orig = " << *epetraOp;
228  }
229 
230  const RCP<VectorBase<double> > lhs_orig3_vec =
231  createMember<double>(epetraOp->range());
232 
233  apply<double>(*epetraOp, NOTRANS, *rhs_vec, lhs_orig3_vec.ptr());
234 
235  if (g_dumpAll) {
236  out << "lhs_orig3_vec = " << *lhs_orig3_vec;
237  }
238 
240  sum<double>(*lhs_orig_vec),
241  sum<double>(*lhs_orig3_vec),
242  as<double>(10.0 * ST::eps())
243  );
244  // NOTE: Above, it would ask for exact binary match except if one uses
245  // threading it will not match exactly!
246 
247 }
248 
249 
250 TEUCHOS_UNIT_TEST( EpetraLinearOp, RowStatLinearOpBase )
251 {
252  using Teuchos::null;
253  using Teuchos::inOutArg;
255  using Teuchos::rcp_dynamic_cast;
256  typedef ScalarTraits<double> ST;
257 
258  // Set up the EpetraLinearOp
259 
260  const RCP<const Epetra_Comm> comm = getEpetraComm();
261  const int numLocalRows = g_localDim;
262  const int numRows = numLocalRows * comm->NumProc();
263  const int numCols = numLocalRows / 2;
264  const RCP<Epetra_CrsMatrix> epetraCrsM = getEpetraMatrix(numRows, numCols);
265  const double two = 2.0;
266  epetraCrsM->PutScalar(two);
267  const RCP<LinearOpBase<double> > epetraOp = nonconstEpetraLinearOp(epetraCrsM);
268 
269  const RCP<RowStatLinearOpBase<double> > rowStatOp =
270  rcp_dynamic_cast<RowStatLinearOpBase<double> >(epetraOp, true);
271 
272  if (g_dumpAll) {
273  out << "epetraOp = " << *epetraOp;
274  }
275 
276  // Get the inverse row sums
277 
278  const RCP<VectorBase<double> > inv_row_sums =
279  createMember<double>(epetraOp->range());
280 
281  rowStatOp->getRowStat(RowStatLinearOpBaseUtils::ROW_STAT_INV_ROW_SUM,
282  inv_row_sums.ptr());
283 
284  if (g_dumpAll) {
285  out << "inv_row_sums = " << *inv_row_sums;
286  }
287 
289  sum<double>(*inv_row_sums),
290  as<double>((1.0 / (two * numCols)) * numRows),
291  as<double>(10.0 * ST::eps())
292  );
293 
294 }
295 
296 
298 {
299  using Teuchos::null;
300  using Teuchos::inOutArg;
302 
303  const RCP<const Epetra_Comm> comm = getEpetraComm();
304 
305  const int numLocalRows = g_localDim;
306  const int numRows = numLocalRows * comm->NumProc();
307  const int numCols = numLocalRows / 2;
308 
309  const RCP<Epetra_CrsMatrix> epetraCrsM = getEpetraMatrix(numRows, numCols);
310 
311  const RCP<const LinearOpBase<double> > epetraOp = epetraLinearOp(epetraCrsM);
312 
313  LinearOpTester<double> linearOpTester;
314  updateSuccess(linearOpTester.check(*epetraOp, inOutArg(out)), success);
315 
316 }
317 
318 
320 {
321 
322  using Teuchos::describe;
323 
324  // build sub operators
326  epetraLinearOp(getEpetraMatrix(4,4,0));
328  epetraLinearOp(getEpetraMatrix(4,3,1));
330  epetraLinearOp(getEpetraMatrix(4,2,2));
332  epetraLinearOp(getEpetraMatrix(3,4,3));
334  epetraLinearOp(getEpetraMatrix(3,3,4));
336  epetraLinearOp(getEpetraMatrix(3,2,5));
338  epetraLinearOp(getEpetraMatrix(2,4,6));
340  epetraLinearOp(getEpetraMatrix(2,3,8));
342  epetraLinearOp(getEpetraMatrix(2,2,8));
343 
344  out << "Sub operators built" << std::endl;
345 
346  {
347  // build composite operator
348  RCP<const LinearOpBase<double> > A = block2x2<double>(
349  block2x2<double>(A00, A01, A10, A11), block2x1<double>(A02,A12),
350  block1x2<double>(A20, A21), A22
351  );
352 
353  out << "First composite operator built" << std::endl;
354 
355  // build vectors for use in apply
356  RCP<MultiVectorBase<double> > x = createMembers<double>(A->domain(), 3);
357  RCP<MultiVectorBase<double> > y = createMembers<double>(A->range(), 3);
358 
359  randomize(-1.0, 1.0, x.ptr());
360 
361  out << "A = \n" << describe(*A, Teuchos::VERB_HIGH) << std::endl;
362  out << "x = \n" << describe(*x, Teuchos::VERB_HIGH) << std::endl;
363  out << "y = \n" << describe(*y, Teuchos::VERB_HIGH) << std::endl;
364 
365  // perform a matrix vector multiply
366  apply(*A, NOTRANS, *x, y.ptr());
367 
368  out << "First composite operator completed" << std::endl;
369  }
370 
371  {
372  RCP<const LinearOpBase<double> > A = block2x2<double>(
373  A11, block1x2<double>(A10, A12),
374  block2x1<double>(A01, A21), block2x2<double>(A00, A02, A20, A22)
375  );
376 
377  out << "Second composite operator built" << std::endl;
378 
379  // build vectors for use in apply
380  RCP<MultiVectorBase<double> > x = createMembers<double>(A->domain(), 3);
381  RCP<MultiVectorBase<double> > y = createMembers<double>(A->range(), 3);
382 
383  randomize(-1.0, 1.0, x.ptr());
384 
385  out << "A = \n" << describe(*A, Teuchos::VERB_HIGH) << std::endl;
386  out << "x = \n" << describe(*x, Teuchos::VERB_HIGH) << std::endl;
387  out << "y = \n" << describe(*y, Teuchos::VERB_HIGH) << std::endl;
388 
389  // perform a matrix vector multiply
390  apply(*A, NOTRANS, *x, y.ptr());
391 
392  out << "Second composite operator completed" << std::endl;
393  }
394 
395  out << "Test complete" << std::endl;
396 
397 }
398 
399 
400 } // namespace Thyra
Concrete LinearOpBase adapter subclass for Epetra_Operator object.
static CommandLineProcessor & getCLP()
TEUCHOS_UNIT_TEST(EpetraOperatorWrapper, basic)
void updateSuccess(const bool result, bool &success)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Ptr< T > ptr() const
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
TEUCHOS_STATIC_SETUP()