10 #ifndef THYRA_TESTING_TOOLS_HPP
11 #define THYRA_TESTING_TOOLS_HPP
13 #include "Thyra_TestingToolsDecl.hpp"
14 #include "Thyra_VectorBase.hpp"
15 #include "Thyra_VectorStdOps.hpp"
16 #include "Thyra_LinearOpBase.hpp"
17 #include "Thyra_AssertOp.hpp"
18 #include "Teuchos_as.hpp"
21 template <
class Scalar>
26 typedef typename ST::magnitudeType ScalarMag;
32 V_VmV( diff.ptr(), v1, v2 );
36 nrm_diff =
norm(*diff);
43 + std::max( nrm_v1, nrm_v2 )
49 template<
class Scalar1,
class Scalar2,
class ScalarMag>
51 const std::string &v1_name,
53 const std::string &v2_name,
55 const std::string &maxRelErr_error_name,
56 const ScalarMag &maxRelErr_error,
57 const std::string &maxRelErr_warning_name,
58 const ScalarMag &maxRelErr_warning,
65 typedef typename Teuchos::PromotionTraits<Scalar1,Scalar2>::promote Scalar;
68 const int num_scalars = v1.
size();
71 return Teuchos::testRelErr<Scalar>(
72 v1_name, v1[0], v2_name, v2[0],
73 maxRelErr_error_name, maxRelErr_error,
74 maxRelErr_warning_name, maxRelErr_warning,
83 << li <<
"Check: rel_err(" << v1_name <<
"," << v2_name <<
") <= " << maxRelErr_error_name <<
" ?\n";
85 for(
int i = 0; i < num_scalars; ++i ) {
86 const ScalarMag rel_err = relErr<Scalar>( v1[i], v2[i] );
87 const bool result = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error) && rel_err <= maxRelErr_error );
88 if(!result) success =
false;
91 << li <<
" "<<setw(2)<<i<<
": rel_err("<<v1[i]<<
","<<v2[i]<<
") "<<
"= "<<rel_err
92 <<
" <= " << maxRelErr_error <<
" : " <<
passfail(result) << std::endl;
93 if( result && rel_err >= maxRelErr_warning ) {
95 << li <<
" Warning! rel_err(...) >= " << maxRelErr_warning_name <<
" = " << maxRelErr_warning <<
"!\n";
105 template<
class Scalar>
107 const std::string &v1_name,
109 const std::string &v2_name,
111 const std::string &maxRelErr_error_name,
113 const std::string &maxRelErr_warning_name,
115 std::ostream *out_inout,
117 const std::string &li
124 typedef typename ST::magnitudeType ScalarMag;
134 !SMT::isnaninf(rel_err)
135 && !SMT::isnaninf(maxRelErr_error)
136 && rel_err <= maxRelErr_error
141 << li <<
"Testing relative error between vectors "
142 << v1_name <<
" and " << v2_name <<
":\n";
145 << li <<
"||"<<v1_name<<
"|| = " << nrm_v1 << endl
146 << li <<
"||"<<v2_name<<
"|| = " << nrm_v2 << endl;
149 << li << v1_name <<
" = " << describe(v1,verbLevel)
150 << li << v2_name <<
" = " << describe(v2,verbLevel);
154 << li << v1_name <<
" - " << v2_name <<
" = " << describe(*diff,verbLevel);
157 << li <<
"Check: rel_err(" << v1_name <<
"," << v2_name <<
") = "
158 << rel_err <<
" <= " << maxRelErr_error_name <<
" = "
159 << maxRelErr_error <<
" : " <<
passfail(success) << endl;
160 if( success && rel_err >= maxRelErr_warning ) {
162 << li <<
"Warning! rel_err(" << v1_name <<
"," << v2_name <<
" >= "
163 << maxRelErr_warning_name <<
" = " << maxRelErr_warning <<
"!\n";
170 template<
class Scalar>
172 const std::string &error_name
174 ,
const std::string &max_error_name
176 ,
const std::string &max_warning_name
179 ,
const std::string &li
183 typedef typename ST::magnitudeType ScalarMag;
185 const ScalarMag error_mag = ST::magnitude(error);
186 const bool success = (
187 !SMT::isnaninf(error_mag)
188 && !SMT::isnaninf(max_error)
189 && error_mag <= max_error );
193 << li <<
"Check: |" << error_name <<
"| = " << error_mag
194 <<
" <= " << max_error_name <<
" = " << max_error <<
" : "
196 if( success && error_mag >= max_warning ) {
198 << li <<
"Warning! " << error_name <<
" = " << error_mag
199 <<
" >= " << max_warning_name <<
" = " << max_warning <<
"!\n";
206 template<
class Scalar>
208 const std::string &error_name,
210 const std::string &max_error_name,
212 const std::string &max_warning_name,
215 const std::string &li
220 typedef typename ST::magnitudeType ScalarMag;
223 const int num_scalars = errors.size();
226 return testMaxErr<Scalar>(
227 error_name, errors[0],
228 max_error_name, max_error,
229 max_warning_name, max_warning,
238 << li <<
"Check: |"<<error_name<<
"| <= "<<max_error_name<<
" ?\n";
239 for(
int i = 0; i < num_scalars; ++i ) {
240 const ScalarMag error_mag = ST::magnitude(errors[i]);
241 const bool result = (
242 !SMT::isnaninf(error_mag)
243 && !SMT::isnaninf(max_error)
244 && error_mag <= max_error );
245 if(!result) success =
false;
248 << li <<
" "<<setw(2)<<i<<
": |"<<errors[i]<<
"| = "<<error_mag<<
" <= "
249 <<max_error<<
" : "<<
passfail(success)<<
"\n";
250 if( result && error_mag >= max_warning ) {
252 << li <<
" Warning! |...| >= "<<max_warning_name<<
" = "<<max_warning<<
"!\n";
260 template<
class Scalar>
261 std::ostream& Thyra::operator<<( std::ostream& o, const VectorBase<Scalar>& v )
267 template<
class Scalar>
268 std::ostream& Thyra::operator<<( std::ostream& o, const LinearOpBase<Scalar>& M )
274 #endif // THYRA_TESTING_TOOLS_HPP
virtual RCP< const VectorSpaceBase< Scalar > > space() const =0
Return a smart pointer to the vector space that this vector belongs to.
bool testMaxErrors(const std::string &error_name, const ArrayView< const typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &errors, const std::string &max_error_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &max_error, const std::string &max_warning_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &max_warning, const Ptr< std::ostream > &out, const std::string &leadingIndent=std::string(""))
Check that an array of errors is less than some error tolerence.
#define THYRA_ASSERT_VEC_SPACES(FUNC_NAME, VS1, VS2)
This is a very useful macro that should be used to validate that two vector spaces are compatible...
basic_OSTab< char > OSTab
const std::string passfail(const bool result)
bool testRelNormDiffErr(const std::string &v1_name, const VectorBase< Scalar > &v1, const std::string &v2_name, const VectorBase< Scalar > &v2, const std::string &maxRelErr_error_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &maxRelErr_error, const std::string &maxRelErr_warning_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &maxRelErr_warning, std::ostream *out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_LOW, const std::string &leadingIndent=std::string(""))
Compute, check and optionally print the relative errors in two vectors.
bool testMaxErr(const std::string &error_name, const Scalar &error, const std::string &max_error_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &max_error, const std::string &max_warning_name, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType &max_warning, std::ostream *out, const std::string &leadingIndent=std::string(""))
Check that an error is less than some error tolerence.
void V_VmV(const Ptr< MultiVectorBase< Scalar > > &Z, const MultiVectorBase< Scalar > &X, const MultiVectorBase< Scalar > &Y)
Z(i,j) = X(i,j) - Y(i,j), i = 0...Z->range()->dim()-1, j = 0...Z->domain()->dim()-1.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< VectorBase< Scalar > > createMember(const RCP< const VectorSpaceBase< Scalar > > &vs, const std::string &label="")
Create a vector member from the vector space.
Abstract interface for finite-dimensional dense vectors.
Teuchos::ScalarTraits< Scalar >::magnitudeType relVectorErr(const VectorBase< Scalar > &v1, const VectorBase< Scalar > &v2)
Return relative error of two vectors.
bool nonnull(const boost::shared_ptr< T > &p)
TypeTo as(const TypeFrom &t)
Teuchos::ScalarTraits< Scalar >::magnitudeType norm(const VectorBase< Scalar > &v)
Natural norm: result = sqrt(<v,v>).
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
bool testRelErrors(const std::string &v1_name, const ArrayView< const Scalar1 > &v1, const std::string &v2_name, const ArrayView< const Scalar2 > &v2, const std::string &maxRelErr_error_name, const ScalarMag &maxRelErr_error, const std::string &maxRelErr_warning_name, const ScalarMag &maxRelErr_warning, const Ptr< std::ostream > &out, const std::string &leadingIndent=std::string(""))
Compute, check and optionally print the relative errors in two scalar arays.