Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_VectorStdOpsTester_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) 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 Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_VECTOR_STD_OPS_TESTER_HPP
43 #define THYRA_VECTOR_STD_OPS_TESTER_HPP
44 
45 #include "Thyra_VectorStdOpsTester_decl.hpp"
46 #include "Thyra_TestingTools.hpp"
47 #include "RTOpPack_TOpSetAssendingValues.hpp"
48 #include "Teuchos_TestingHelpers.hpp"
49 #include "Teuchos_Assert.hpp"
50 
51 //#define THYRA_VECTOR_STD_OPS_TESTER_DUMP
52 
53 #ifdef THYRA_VECTOR_STD_OPS_TESTER_DUMP
54 # include "RTOpPack_SPMD_apply_op.hpp"
55 #endif // THYRA_VECTOR_STD_OPS_TESTER_DUMP
56 
57 
58 namespace Thyra {
59 
60 
61 // VectorStdOpsTesterComparable (using partial specialization to only do tests in some cases)
62 
63 
64 template <bool isComparable, class Scalar>
65 class VectorStdOpsTesterComparable {
66 public:
67  static bool checkComparableStdOps(
68  const VectorSpaceBase<Scalar> &vecSpc,
69  const Ptr<VectorBase<Scalar> > &z,
70  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &error_tol,
71  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &warning_tol,
72  const Ptr<std::ostream> &out,
73  const bool &dumpAll
74  )
75  {
77  }
78 };
79 
80 
81 template <class Scalar>
82 class VectorStdOpsTesterComparable<false,Scalar> {
83 public:
84  static bool checkComparableStdOps(
85  const VectorSpaceBase<Scalar> &vecSpc,
86  const Ptr<VectorBase<Scalar> > &z,
87  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &error_tol,
88  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &warning_tol,
89  const Ptr<std::ostream> &out,
90  const bool &dumpAll
91  )
92  {
93  if (nonnull(out)) *out
94  << "\nThis scalar type does not support comparable operations so"
95  << " we can not test min(), max() and other such functions.\n";
96  return true;
97  }
98 };
99 
100 
101 template <class Scalar>
102 class VectorStdOpsTesterComparable<true,Scalar> {
103 public:
104  static bool checkComparableStdOps(
105  const VectorSpaceBase<Scalar> &vecSpc,
106  const Ptr<VectorBase<Scalar> > &z,
107  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &error_tol,
108  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType &warning_tol,
109  const Ptr<std::ostream> &out,
110  const bool &dumpAll
111  )
112  {
114  using Teuchos::outArg;
115 
116  bool success = true, result;
117 
118  if (nonnull(out)) *out << "\nTesting comparable operations ...\n";
119 
120  const Scalar scalarSmall(1e-5), scalarMedium(2.0), scalarLarge(100.0);
121  if (nonnull(out)) *out << "\nassign(z.ptr(),"<<scalarMedium<<");\n";
122  assign(z.ptr(),Scalar(scalarMedium));
123  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
124  if (nonnull(out)) *out << "\nset_ele(0,"<<scalarSmall<<",z.ptr());\n";
125  set_ele(0,scalarSmall,z.ptr());
126  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
127  if (nonnull(out)) *out << "\nset_ele(1,"<<scalarLarge<<",z.ptr());\n";
128  set_ele(1,scalarLarge,z.ptr());
129  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
130  if (nonnull(out)) *out << "\nset_ele(vecSpc.dim()-2,"<<scalarSmall<<",z.ptr());\n";
131  set_ele(vecSpc.dim()-2,scalarSmall,z.ptr());
132  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
133  if (nonnull(out)) *out << "\nset_ele(vecSpc.dim()-1,"<<scalarLarge<<",z.ptr());\n";
134  set_ele(vecSpc.dim()-1,scalarLarge,z.ptr());
135  if(nonnull(out) && dumpAll) *out << "\nz =\n" << *z;
136 
137  Scalar minEle; Ordinal minIndex;
138  Scalar maxEle; Ordinal maxIndex;
139 
140  if(!Teuchos::testRelErr<Scalar>(
141  "min(*z)",min(*z),"scalarSmall",scalarSmall
142  ,"error_tol",error_tol,"warning_tol",warning_tol,out
143  )
144  ) success=false;
145 
146  if (nonnull(out)) *out << "\nmin(*z,&minEle,&minIndex);\n";
147  minEle = ST::zero(); minIndex = 0;
148  min(*z, outArg(minEle), outArg(minIndex));
149  if(!Teuchos::testRelErr<Scalar>(
150  "minEle",minEle,"scalarSmall",scalarSmall
151  ,"error_tol",error_tol,"warning_tol",warning_tol, out
152  )
153  ) success=false;
154  result = minIndex == 0;
155  if (nonnull(out)) *out << "\nminIndex = " << minIndex << " == 0 ? " << passfail(result) << std::endl;
156  if(!result) success = false;
157 
158  if (nonnull(out)) *out << "\nminGreaterThanBound(*z,"<<scalarMedium<<",&minEle,&minIndex);\n";
159  minEle = ST::zero(); minIndex = 0;
160  minGreaterThanBound(*z, scalarMedium, outArg(minEle), outArg(minIndex));
161  if(!Teuchos::testRelErr<Scalar>(
162  "minEle",minEle,"scalarLarge",scalarLarge
163  ,"error_tol",error_tol,"warning_tol",warning_tol,out
164  )
165  ) success=false;
166  result = minIndex == 1;
167  if (nonnull(out)) *out << "\nminIndex = " << minIndex << " == 1 ? " << passfail(result) << std::endl;
168  if(!result) success = false;
169 
170  if (nonnull(out)) *out << "\nminGreaterThanBound(*z,"<<scalarLarge<<",&minEle,&minIndex);\n";
171  minEle = ST::zero(); minIndex = 0;
172  minGreaterThanBound(*z,scalarLarge, outArg(minEle), outArg(minIndex));
173  result = minIndex < 0;
174  if (nonnull(out)) *out << "\nminIndex = " << minIndex << " < 0 ? " << passfail(result) << std::endl;
175  if(!result) success = false;
176 
177  if(!Teuchos::testRelErr<Scalar>(
178  "max(*z)",max(*z),"scalarLarge",scalarLarge
179  ,"error_tol",error_tol,"warning_tol",warning_tol,out)
180  ) success=false;
181 
182  if (nonnull(out)) *out << "\nmax(*z,&maxEle,&maxIndex);\n";
183  maxEle = ST::zero(); maxIndex = 0;
184  max(*z, outArg(maxEle), outArg(maxIndex));
185  if(!Teuchos::testRelErr<Scalar>(
186  "maxEle",maxEle,"scalarLarge",scalarLarge
187  ,"error_tol",error_tol,"warning_tol",warning_tol,out)
188  ) success=false;
189  result = maxIndex == 1;
190  if (nonnull(out)) *out << "\nmaxIndex = " << maxIndex << " == 1 ? " << passfail(result) << std::endl;
191  if(!result) success = false;
192 
193  if (nonnull(out)) *out << "\nmaxLessThanBound(*z,"<<scalarMedium<<",&maxEle,&maxIndex);\n";
194  maxEle = ST::zero(); maxIndex = 0;
195  maxLessThanBound(*z, scalarMedium, outArg(maxEle), outArg(maxIndex));
196  if(!Teuchos::testRelErr<Scalar>(
197  "maxEle",maxEle,"scalarSmall",scalarSmall
198  ,"error_tol",error_tol,"warning_tol",warning_tol,out)
199  ) success=false;
200  result = maxIndex == 0;
201  if (nonnull(out)) *out << "\nmaxIndex = " << maxIndex << " == 0 ? " << passfail(result) << std::endl;
202  if(!result) success = false;
203 
204  if (nonnull(out)) *out << "\nmaxLessThanBound(*z,"<<scalarSmall<<",&maxEle,&maxIndex);\n";
205  maxEle = ST::zero(); maxIndex = 0;
206  maxLessThanBound(*z, scalarSmall, outArg(maxEle), outArg(maxIndex));
207  result = ( maxIndex < 0 );
208  if (nonnull(out)) *out << "\nmaxIndex = " << maxIndex << " < 0 ? " << passfail(result) << std::endl;
209  if(!result) success = false;
210 
211 
212  // Test pair_wise_max
213  if (nonnull(out)) *out << "\nTesting pair_wise_max(*v1, *v2, z0.ptr()) ...\n";
214  {
215  using Teuchos::as;
216  using Teuchos::tuple;
217  using Teuchos::null;
218  using Teuchos::outArg;
219  using Teuchos::inOutArg;
220  Teuchos::RCP<VectorBase<Scalar> > v1 = createMember(vecSpc);
221  Teuchos::RCP<VectorBase<Scalar> > v2 = createMember(vecSpc);
222  Teuchos::RCP<VectorBase<Scalar> > v3 = createMember(vecSpc);
223  Teuchos::RCP<VectorBase<Scalar> > z0 = createMember(vecSpc);
224  const Scalar alpha = as<Scalar>(2.0);
225  seed_randomize<Scalar>(12345);
226  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
227  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
228 
229  // z0 = alpha*max(v1_i, v2_i)
230  pair_wise_max(alpha, *v1, *v2, z0.ptr());
231 
232  V_VpV(v3.ptr(), *v1, *v2); // v3 = v1 + v2
233  Vp_V(v1.ptr(), *v2, as<Scalar>(-ST::one())); // v1 -= v2
234  abs(*v1, v2.ptr()); // v2 = abs(v1)
235  Vp_V(v3.ptr(), *v2); // v3 += v2
236 
237  // v1 + v2 + | v1 - v2 | = 2 max (v1, v2)
238  Vp_V(z0.ptr(), *v3, as<Scalar>(-ST::one())); // z0 -= v3
239  if(!testMaxErr<Scalar>(
240  "norm_2(*z0)",norm_2(*z0)
241  ,"error_tol",error_tol,"warning_tol",warning_tol,out.get()
242  )
243  ) success=false;
244  }
245 
246  return success;
247  }
248 };
249 
250 
251 // Other helpers
252 
253 
254 template<class Scalar>
255 void setEleTestCase( const Ptr<VectorBase<Scalar> > &z, const Ordinal i, int &tc,
256  std::ostream &out, bool &success)
257 {
258  using Teuchos::as;
259  out << "\n"<<tc<<") set_ele(z, "<<i<<");\n";
260  ++tc;
261  {
262  typedef ScalarTraits<Scalar> ST;
263  const Scalar val_i = as<Scalar>(i+1);
264  assign<Scalar>(z, ST::zero());
265  set_ele(i, val_i, z);
266  TEUCHOS_TEST_EQUALITY_CONST(get_ele(*z, i), val_i, out, success);
267  TEUCHOS_TEST_EQUALITY_CONST(sum(*z), val_i, out, success);
268  }
269 }
270 
271 
272 // VectorStdOpsTester
273 
274 
275 template <class Scalar>
277  const ScalarMag &warning_tol_in,
278  const ScalarMag &error_tol_in
279  )
280  :warning_tol_(warning_tol_in),
281  error_tol_(error_tol_in)
282 {}
283 
284 
285 template <class Scalar>
287  const VectorSpaceBase<Scalar> &vecSpc,
288  std::ostream *out_out,
289  const bool &dumpAll
290  )
291 {
292  using Teuchos::as;
293  using Teuchos::tuple;
294  using Teuchos::null;
295  using Teuchos::outArg;
296  using Teuchos::inOutArg;
298 
299  TEUCHOS_ASSERT(out_out);
300  std::ostream &out = *out_out;
301 
302  out << "\n*** Entering VectorStdOpsTester<"<<ST::name()<<">::checkStdOps(...) ...\n"
303  << "using a \'" << vecSpc.description() << "\' object ...\n";
304 
305  bool success = true;
306  out << "\nvecSpc.dim() = " << vecSpc.dim() << std::endl;
307 
308  const Ordinal n = vecSpc.dim();
309 
310  TEUCHOS_TEST_FOR_EXCEPTION( n < 4, std::logic_error,
311  "Error: n = "<<n<<" must be least 4 or greater to"
312  " run Thyra::VectorStdOpsTester::checkStdOps(...)!" );
313 
314  const Scalar
315  two = as<Scalar>(2.0),
316  three = as<Scalar>(3.0),
317  four = as<Scalar>(4.0);
318 
319  int tc = 0;
320 
321  out << "\nCreating vectors v1, v2, v3, v4, x and z ...\n";
323  v1 = createMember(vecSpc),
324  v2 = createMember(vecSpc),
325  v3 = createMember(vecSpc),
326  v4 = createMember(vecSpc),
327  y = createMember(vecSpc),
328  x = createMember(vecSpc),
329  z = createMember(vecSpc);
330 
331  out << "\nassign(v1.ptr(), -2.0);\n";
332  assign<Scalar>(v1.ptr(), -two);
333  out << "\nassign(v2.ptr(), -3.0);\n";
334  assign<Scalar>(v2.ptr(), -three);
335  out << "\nassign(v3.ptr(), -4.0);\n";
336  assign<Scalar>(v3.ptr(), -four);
337  out << "\ny[i] = i+1\n";
338  {
339  RTOpPack::TOpSetAssendingValues<Scalar> setAssendOp(ST::zero());
340  applyOp<Scalar>( setAssendOp,
342  tuple<Ptr<VectorBase<Scalar> > >(y.ptr())(),
343  null );
344  }
345 
346  // sum
347  out << "\n"<<tc<<") sum(*y);\n";
348  ++tc;
350  Teuchos::testRelErr<Scalar>(
351  "sum(*y)", sum(*y),
352  "0.5*(n+1)*n", as<Scalar>(0.5*(n+1)*n),
353  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
354  out, success);
355 
356  // norm_inf
357  out << "\n"<<tc<<") nom_inf(*v1);\n";
358  ++tc;
360  Teuchos::testRelErr<Scalar>(
361  "norm_inf(*v1)", norm_inf(*v1),
362  "2.0", two,
363  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
364  out, success);
365 
366  // norm_2
367  out << "\n"<<tc<<") norm_2(*v1);\n";
368  ++tc;
370  Teuchos::testRelErr<Scalar>(
371  "norm_2(*v1)", norm_2(*v1),
372  "2.0*sqrt(vecSpc.dim())", as<Scalar>(2.0)*ST::squareroot(vecSpc.dim()),
373  "error_tol", error_tol(), "warning_tol",warning_tol(), inOutArg(out)),
374  out, success);
375 
376  // norm_1
377  out << "\n"<<tc<<") norm_1(*v1);\n";
378  ++tc;
380  Teuchos::testRelErr<Scalar>(
381  "norm_1(*v1)" ,norm_1(*v1),
382  "2.0*vecSpc.dim()", as<Scalar>(2.0)*as<Scalar>(vecSpc.dim()),
383  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
384  out, success);
385 
386  // abs
387  out << "\n"<<tc<<") abs(z.ptr(),*v1);\n";
388  ++tc;
389  {
390  abs<Scalar>(*v1, z.ptr());
391  if(!Teuchos::testRelErr<Scalar>(
392  "sum(*z)", sum(*z), "2.0*vecSpc.dim()", as<Scalar>(2.0)*as<Scalar>(vecSpc.dim()),
393  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out))
394  ) success=false;
395  }
396 
397  // get_ele
398 
399  out << "\n"<<tc<<") val = get_ele(y, 0);\n";
400  ++tc;
401  {
402  const Scalar val = get_ele<Scalar>(*y, 0);
403  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(1), out, success );
404  }
405 
406  out << "\n"<<tc<<") val = get_ele<Scalar>(*y, 1);\n";
407  ++tc;
408  {
409  const Scalar val = get_ele<Scalar>(*y, 1);
410  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(2), out, success );
411  }
412 
413  out << "\n"<<tc<<") val = get_ele<Scalar>(*y, n-2);\n";
414  ++tc;
415  {
416  const Scalar val = get_ele<Scalar>(*y, n-2);
417  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(n-1), out, success );
418  }
419 
420  out << "\n"<<tc<<") val = get_ele<Scalar>(*y, n-1);\n";
421  ++tc;
422  {
423  const Scalar val = get_ele<Scalar>(*y, n-1);
424  TEUCHOS_TEST_EQUALITY_CONST( val, as<Scalar>(n), out, success );
425  }
426 
427 #ifdef THYRA_DEBUG
428 
429  out << "\n"<<tc<<") get_ele<Scalar>(*y, -1);\n";
430  ++tc;
431  {
432  TEUCHOS_TEST_THROW(get_ele<Scalar>(*y, -1), std::out_of_range, out, success );
433  }
434 
435  out << "\n"<<tc<<") get_ele<Scalar>(*y, n);\n";
436  ++tc;
437  {
438  TEUCHOS_TEST_THROW(get_ele<Scalar>(*y, n), std::out_of_range, out, success );
439  }
440 
441 #endif // THYRA_DEBUG
442 
443  // set_ele
444 
445  setEleTestCase<Scalar>(z.ptr(), 0, tc, out, success);
446 
447  setEleTestCase<Scalar>(z.ptr(), 1, tc, out, success);
448 
449  setEleTestCase<Scalar>(z.ptr(), n-2, tc, out, success);
450 
451  setEleTestCase<Scalar>(z.ptr(), n-1, tc, out, success);
452 
453 #ifdef THYRA_DEBUG
454 
455  TEUCHOS_TEST_THROW(set_ele(-1, two, z.ptr()),
456  std::out_of_range, out, success);
457 
458  TEUCHOS_TEST_THROW(set_ele(n, two, z.ptr()),
459  std::out_of_range, out, success);
460 
461 #endif // THYRA_DEBUG
462 
463  // reciprocal
464  out << "\n"<<tc<<") reciprocal(z.ptr(),*v1);\n";
465  ++tc;
466  {
467  reciprocal(*v1, z.ptr());
468  if(!Teuchos::testRelErr<Scalar>(
469  "sum(*z)",sum(*z),"-0.5*vecSpc.dim()",as<Scalar>(-0.5)*as<Scalar>(vecSpc.dim())
470  ,"error_tol",error_tol(),"warning_tol",warning_tol(),inOutArg(out))
471  ) success=false;
472  }
473 
474  // linear_combination
475 
476  out << "\n"<<tc<<") linear_combination(2,{0.5,0.25},{v1.ptr(),v2.ptr()},0.0,z.ptr());\n";
477  ++tc;
478  {
479  linear_combination<Scalar>(
480  tuple<Scalar>(0.5, 0.25)(),
481  tuple<Ptr<const VectorBase<Scalar> > >(v1.ptr(), v2.ptr())(),
482  ST::zero(),
483  z.ptr());
485  Teuchos::testRelErr<Scalar>(
486  "sum(*z)", sum(*z),
487  "(-0.5*2.0-0.25*3.0)*vecSpc.dim()", as<Scalar>((-0.5 * 2.0 - 0.25 * 3.0) *vecSpc.dim()),
488  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)),
489  out, success);
490  }
491 
492  out << "\nassign(z.ptr(), 2.0);\n";
493  ++tc;
494  assign(z.ptr(), as<Scalar>(2.0));
495 
496  out << "\n"<<tc<<") linear_combination(3,{0.5,0.25,0.125},{v1.ptr(),v2.ptr(),v2.ptr()},0.5,z.ptr());\n";
497  ++tc;
498  {
499  linear_combination<Scalar>(
500  tuple<Scalar>(0.5, 0.25, 0.125)(),
501  tuple<Ptr<const VectorBase<Scalar> > >(v1.ptr(), v2.ptr(), v3.ptr())(),
502  as<Scalar>(0.5),
503  z.ptr());
504  if(!Teuchos::testRelErr<Scalar>(
505  "sum(*z)", sum(*z),
506  "(0.5*2.0-0.5*2.0-0.25*3.0-0.125*4.0)*vecSpc.dim()",
507  as<Scalar>(0.5*2.0-0.5*2.0-0.25*3.0-0.125*4.0)*as<Scalar>(vecSpc.dim()),
508  "error_tol", error_tol(), "warning_tol", warning_tol(), inOutArg(out)
509  )
510  ) success=false;
511  }
512 
513  // assgin
514  out << "\n"<<tc<<") assign(z.ptr(),2.0);\n";
515  ++tc;
516  {
517  assign(z.ptr(),as<Scalar>(2.0));
518  if(!Teuchos::testRelErr<Scalar>(
519  "norm_2(*z,*v2)",norm_2(*z,*v2)
520  ,"sqrt(2.0*3.0*3.0*vecSpc.dim())",ST::magnitude(ST::squareroot(as<Scalar>(2.0*3.0*3.0)*as<Scalar>(vecSpc.dim())))
521  ,"error_tol",error_tol(),"warning_tol",warning_tol(),inOutArg(out)
522  )
523  ) success=false;
524 
525  if(!VectorStdOpsTesterComparable<ST::isComparable,Scalar>::checkComparableStdOps(
526  vecSpc,z.ptr(),error_tol(),warning_tol(),inOutArg(out),dumpAll)
527  ) success=false;
528  }
529 
530  // Test Vt_S
531  out << "\n"<<tc<<") Testing Vt_S(z.ptr(),alpha) ...\n";
532  ++tc;
533  {
534 #ifdef THYRA_VECTOR_STD_OPS_TESTER_DUMP
535  RTOpPack::show_spmd_apply_op_dump = true;
536 #endif // THYRA_VECTOR_STD_OPS_TESTER_DUMP
537  v1 = createMember(vecSpc);
538  v2 = createMember(vecSpc);
539  const Scalar alpha = as<Scalar>(1.2345);
540  seed_randomize<Scalar>(12345);
541  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr());
542  V_V(v2.ptr(),*v1);
543  Vt_S(v1.ptr(), alpha);
544  const Scalar norm_alpha_v1 = norm_2(*v1);
545  //out << "norm_alpha_v1 = " << norm_alpha_v1 << "\n";
546  const Scalar mag_alpha = ST::magnitude(alpha);
547  //out << "mag_alpha = " << mag_alpha << "\n";
548  const Scalar norm_2_v2 = norm_2(*v2);
549  //out << "norm_2_v2 = " << norm_2_v2 << "\n";
550  const Scalar alpha_norm_v1 = mag_alpha * norm_2_v2;
551  //out << "alpha_norm_v1 = " << alpha_norm_v1 << "\n";
552  if(!testMaxErr<Scalar>(
553  "norm_alpha_v1 - alpha_norm_v1",ST::magnitude(norm_alpha_v1-alpha_norm_v1)
554  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
555  )
556  ) success=false;
557 #ifdef THYRA_VECTOR_STD_OPS_TESTER_DUMP
558  RTOpPack::show_spmd_apply_op_dump = false;
559 #endif // THYRA_VECTOR_STD_OPS_TESTER_DUMP
560  }
561 
562  // Test V_StV
563  out << "\n"<<tc<<") Testing V_StV(z.ptr(),alpha,*v) ...\n";
564  ++tc;
565  {
566  v1 = createMember(vecSpc);
567  v2 = createMember(vecSpc);
568  z = createMember(vecSpc);
569  const Scalar alpha = as<Scalar>(-1.2345);
570  seed_randomize<Scalar>(12345);
571  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr());
572  V_StV(v2.ptr(),alpha,*v1);
573  Vt_S(v1.ptr(),alpha);
574  V_V(z.ptr(),*v1);
575  Vp_V(z.ptr(),*v2,as<Scalar>(-ST::one()));
576  if(!testMaxErr<Scalar>(
577  "norm_2(*z)",norm_2(*z)
578  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
579  )
580  ) success=false;
581  }
582 
583  // Test Vp_StV
584  out << "\n"<<tc<<") Testing Vp_StV(z.ptr(),alpha,*v) ...\n";
585  ++tc;
586  {
587  v1 = createMember(vecSpc);
588  v2 = createMember(vecSpc);
589  v3 = createMember(vecSpc);
590  z = createMember(vecSpc);
591  const Scalar alpha = as<Scalar>(-1.2345);
592  seed_randomize<Scalar>(12345);
593  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
594  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
595  V_V(v3.ptr(),*v1); // v3 = v1
596  Vp_StV(v1.ptr(),alpha,*v2); // v1 += alpha*v2
597  V_StV(z.ptr(),alpha,*v2); // z = alpha*v2
598  Vp_V(z.ptr(),*v3); // z += v3
599  V_V(v3.ptr(),*v1); // v3 = v1
600  Vp_V(v3.ptr(),*z,as<Scalar>(-ST::one())); // v3 -= z
601  if(!testMaxErr<Scalar>(
602  "norm_2(*v3)",norm_2(*v3)
603  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
604  )
605  ) success=false;
606  }
607 
608  // Test ele_wise_prod
609  out << "\n"<<tc<<") Testing ele_wise_prod(alpha,*v1, *v2, z.ptr()) ...\n";
610  ++tc;
611  {
612  v1 = createMember(vecSpc);
613  v2 = createMember(vecSpc);
614  v3 = createMember(vecSpc);
615  z = createMember(vecSpc);
616  const Scalar alpha = as<Scalar>(-1.2345);
617  seed_randomize<Scalar>(12345);
618  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
619  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
620  randomize(as<Scalar>(-ST::one()),ST::one(),v3.ptr()); // v3 = rand
621  V_V(v4.ptr(), *v1); // v4 = v1
622  V_V(z.ptr(), *v2); // z = v2
623  ele_wise_prod(alpha, *v2, *v3, v1.ptr()); // v1 += alpha * v2 * v3
624  ele_wise_prod_update(alpha, *v3, z.ptr()); // z *= alpha * v3
625  Vp_V(z.ptr(), *v4); // z += v4
626  V_V(v2.ptr(), *v1); // v2 = v1
627  Vp_V(v2.ptr(), *z, as<Scalar>(-ST::one())); // v2 -= z
628  if(!testMaxErr<Scalar>(
629  "norm_2(*v2)",norm_2(*v2)
630  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
631  )
632  ) success=false;
633  }
634 
635 
636  // Test ele_wise_scale
637  out << "\n"<<tc<<") Testing ele_wise_scale(*v1, z.ptr()) ...\n";
638  ++tc;
639  {
640  v1 = createMember(vecSpc);
641  z = createMember(vecSpc);
642  V_S(v1.ptr(), as<Scalar>(2.0));
643  V_S(z.ptr(), as<Scalar>(3.0));
644  ele_wise_scale( *v1, z.ptr() );
645  if (!Teuchos::testRelErr<Scalar>(
646  "norm_2(*z)", norm_2(*z),
647  "ST::squareroot(n*sqr(3.0*2.0))", ST::squareroot(n*36.0),
648  "error_tol", error_tol(),
649  "warning_tol", warning_tol(),
650  inOutArg(out)
651  )
652  ) success=false;
653  }
654 
655  // Test Vt_StV
656  out << "\n"<<tc<<") Testing Vt_StV(z.ptr(), alpha, *v) ...\n";
657  ++tc;
658  {
659  v1 = createMember(vecSpc);
660  v2 = createMember(vecSpc);
661  v3 = createMember(vecSpc);
662  z = createMember(vecSpc);
663  const Scalar alpha = as<Scalar>(-1.2345);
664  seed_randomize<Scalar>(12345);
665  randomize(as<Scalar>(-ST::one()),ST::one(),v1.ptr()); // v1 = rand
666  randomize(as<Scalar>(-ST::one()),ST::one(),v2.ptr()); // v2 = rand
667  V_V(v3.ptr(),*v1); // v3 = v1
668  Vt_StV(v1.ptr(),alpha,*v2); // v1 *= alpha*v2
669  V_S(z.ptr(),ST::zero()); // z = 0
670  Vp_StVtV(z.ptr(),alpha,*v3,*v2); // z += alpha*v3*v2
671  V_V(v2.ptr(),*v1); // v2 = v1
672  Vp_V(v2.ptr(),*z,as<Scalar>(-ST::one())); // v2 -= z
673  if(!testMaxErr<Scalar>(
674  "norm_2(*v2)",norm_2(*v2)
675  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
676  )
677  ) success=false;
678  }
679 
680  // Test V_StVpV
681  out << "\n"<<tc<<") Testing V_StVpV(z.ptr(),alpha,*v1,*v2) ...\n";
682  ++tc;
683  {
684  v1 = createMember(vecSpc);
685  v2 = createMember(vecSpc);
686  v3 = createMember(vecSpc);
687  x = createMember(vecSpc);
688  z = createMember(vecSpc);
689  const Scalar alpha = as<Scalar>(1.2345);
690  seed_randomize<Scalar>(12345);
691  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v1.ptr());
692  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
693  V_StVpV(v3.ptr(),alpha,*v1,*v2);
694  V_V(z.ptr(),*v1);
695  Vp_V(z.ptr(),*v2,alpha);
696  V_V(x.ptr(),*v3);
697  Vp_V(x.ptr(),*z,as<Scalar>(-ST::one()));
698  if(!testMaxErr<Scalar>(
699  "norm_2(*x)",norm_2(*x)
700  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
701  )
702  ) success=false;
703  }
704 
705  // Test V_VpStV
706  out << "\n"<<tc<<") Testing V_VpStV(z.ptr(),*v1,alpha,*v2) ...\n";
707  ++tc;
708  {
709  v1 = createMember(vecSpc);
710  v2 = createMember(vecSpc);
711  v3 = createMember(vecSpc);
712  x = createMember(vecSpc);
713  z = createMember(vecSpc);
714  const Scalar alpha = as<Scalar>(1.2345);
715  seed_randomize<Scalar>(12345);
716  randomize<Scalar>(as<Scalar>(as<Scalar>(-10)*ST::one()),
717  as<Scalar>(as<Scalar>(10)*ST::one()), v1.ptr());
718  randomize<Scalar>(as<Scalar>(as<Scalar>(-10)*ST::one()),
719  as<Scalar>(as<Scalar>(10)*ST::one()), v2.ptr());
720  V_VpStV(outArg(*v3), *v1, alpha, *v2);
721  V_V(z.ptr(), *v1);
722  Vp_StV(z.ptr(), alpha, *v2);
723  V_VmV(outArg(*x), *z, *v3);
724  if(!testMaxErr<Scalar>(
725  "norm_2(*x)",norm_2(*x)
726  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
727  )
728  ) success=false;
729  }
730 
731  // Test V_StVpStV
732  out << "\n"<<tc<<") Testing V_StVpStV(z.ptr(),alpha,*v1,beta,*v2) ...\n";
733  ++tc;
734  {
735  v1 = createMember(vecSpc);
736  v2 = createMember(vecSpc);
737  v3 = createMember(vecSpc);
738  x = createMember(vecSpc);
739  z = createMember(vecSpc);
740  const Scalar alpha = as<Scalar>(1.2345);
741  const Scalar beta = as<Scalar>(5.4321);
742  seed_randomize<Scalar>(12345);
743  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v1.ptr());
744  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
745  V_StVpStV(v3.ptr(),alpha,*v1,beta,*v2);
746  V_StV(z.ptr(),alpha,*v1);
747  Vp_StV(z.ptr(),beta,*v2);
748  V_V(x.ptr(),*v3);
749  Vp_V(x.ptr(),*z,as<Scalar>(-ST::one()));
750  if(!testMaxErr<Scalar>(
751  "norm_2(*x)",norm_2(*x)
752  ,"10*error_tol",ScalarMag(ScalarMag(10)*error_tol()),"warning_tol",warning_tol(),&out
753  )
754  ) success=false;
755  }
756 
757  // Test Vp_V
758  out << "\n"<<tc<<") Testing Vp_V(v1.ptr(),*v2,beta) ...\n";
759  ++tc;
760  {
761  v1 = createMember(vecSpc);
762  v2 = createMember(vecSpc);
763  v3 = createMember(vecSpc);
764  x = createMember(vecSpc);
765  z = createMember(vecSpc);
766  const Scalar alpha = as<Scalar>(-2.0);
767  const Scalar beta = as<Scalar>(10.0);
768  V_S(v1.ptr(),alpha);
769  seed_randomize<Scalar>(12345);
770  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
771  Vp_V(v1.ptr(),*v2,beta);
772  V_S(v3.ptr(),alpha);
773  V_StVpV(z.ptr(),beta,*v3,*v2);
774  V_StVpV(x.ptr(),as<Scalar>(-ST::one()),*z,*v1);
775  if(!testMaxErr<Scalar>(
776  "norm_2(*x)",norm_2(*x)
777  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
778  )
779  ) success=false;
780  }
781 
782  // Test Vp_V
783  out << "\n"<<tc<<") Testing Vp_V(v1.ptr(),*v2) ...\n";
784  ++tc;
785  {
786  v1 = createMember(vecSpc);
787  v2 = createMember(vecSpc);
788  v3 = createMember(vecSpc);
789  x = createMember(vecSpc);
790  z = createMember(vecSpc);
791  const Scalar alpha = as<Scalar>(-2.0);
792  V_S(v1.ptr(),alpha);
793  seed_randomize<Scalar>(12345);
794  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v2.ptr());
795  Vp_V(v1.ptr(),*v2);
796  V_S(v3.ptr(),alpha);
797  V_StVpV(z.ptr(),ST::one(),*v3,*v2);
798  V_StVpV(x.ptr(),as<Scalar>(-ST::one()),*z,*v1);
799  if(!testMaxErr<Scalar>(
800  "norm_2(*x)",norm_2(*x)
801  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
802  )
803  ) success=false;
804  }
805 
806  // Test V_S
807  out << "\n"<<tc<<") Testing V_S(v1.ptr(),alpha) ...\n";
808  ++tc;
809  {
810  v1 = createMember(vecSpc);
811  v2 = createMember(vecSpc);
812  z = createMember(vecSpc);
813  const Scalar alpha = as<Scalar>(1.2345);
814  assign(v1.ptr(),alpha);
815  V_S(v2.ptr(),alpha);
816  V_StVpV(z.ptr(),as<Scalar>(-ST::one()),*v1,*v2);
817  if(!testMaxErr<Scalar>(
818  "norm_2(*z)",norm_2(*z)
819  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
820  )
821  ) success=false;
822  }
823 
824  // Test V_V
825  out << "\n"<<tc<<") Testing V_V(v1.ptr(),*v2) ...\n";
826  ++tc;
827  {
828  v1 = createMember(vecSpc);
829  v2 = createMember(vecSpc);
830  z = createMember(vecSpc);
831  seed_randomize<Scalar>(12345);
832  randomize(as<Scalar>(as<Scalar>(-10)*ST::one()),as<Scalar>(as<Scalar>(10)*ST::one()),v1.ptr());
833  V_V(v2.ptr(),*v1);
834  V_StVpV(z.ptr(),as<Scalar>(-ST::one()),*v1,*v2);
835  if(!testMaxErr<Scalar>(
836  "norm_2(*z)",norm_2(*z)
837  ,"error_tol",error_tol(),"warning_tol",warning_tol(),&out
838  )
839  ) success=false;
840  }
841 
842  // ToDo: Add tests for *all* standard operators!
843 
844  out << "\n*** Leaving VectorStdOpsTester<"<<ST::name()<<">::checkStdOps(...) ...\n";
845 
846  return success;
847 
848 
849 }
850 
851 
852 } // namespace Thyra
853 
854 
855 #endif // THYRA_VECTOR_STD_OPS_TESTER_HPP
VectorStdOpsTester(const ScalarMag &warning_tol=0, const ScalarMag &error_tol=0)
const std::string passfail(const bool result)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Abstract interface for objects that represent a space for vectors.
KOKKOS_INLINE_FUNCTION RealType abs(const complex< RealType > &x)
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
#define TEUCHOS_TEST_ASSERT(v1, out, success)
Ptr< T > ptr() const
virtual std::string description() const
Abstract interface for finite-dimensional dense vectors.
Teuchos::ScalarTraits< Scalar >::magnitudeType ScalarMag
bool nonnull(const boost::shared_ptr< T > &p)
TypeTo as(const TypeFrom &t)
bool checkStdOps(const VectorSpaceBase< Scalar > &vecSpc, std::ostream *out=0, const bool &dumpAll=false)
Run the tests using a vector space.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_EQUALITY_CONST(v1, v2, out, success)
#define TEUCHOS_TEST_THROW(code, ExceptType, out, success)
virtual Ordinal dim() const =0
Return the dimension of the vector space.