42 #ifndef THYRA_TESTING_TOOLS_HPP
43 #define THYRA_TESTING_TOOLS_HPP
45 #include "Thyra_TestingToolsDecl.hpp"
46 #include "Thyra_VectorBase.hpp"
47 #include "Thyra_VectorStdOps.hpp"
48 #include "Thyra_LinearOpBase.hpp"
49 #include "Thyra_AssertOp.hpp"
50 #include "Teuchos_as.hpp"
53 template <
class Scalar>
58 typedef typename ST::magnitudeType ScalarMag;
64 V_VmV( diff.ptr(), v1, v2 );
68 nrm_diff =
norm(*diff);
75 + std::max( nrm_v1, nrm_v2 )
81 template<
class Scalar1,
class Scalar2,
class ScalarMag>
83 const std::string &v1_name,
85 const std::string &v2_name,
87 const std::string &maxRelErr_error_name,
88 const ScalarMag &maxRelErr_error,
89 const std::string &maxRelErr_warning_name,
90 const ScalarMag &maxRelErr_warning,
97 typedef typename Teuchos::PromotionTraits<Scalar1,Scalar2>::promote Scalar;
100 const int num_scalars = v1.
size();
103 return Teuchos::testRelErr<Scalar>(
104 v1_name, v1[0], v2_name, v2[0],
105 maxRelErr_error_name, maxRelErr_error,
106 maxRelErr_warning_name, maxRelErr_warning,
115 << li <<
"Check: rel_err(" << v1_name <<
"," << v2_name <<
") <= " << maxRelErr_error_name <<
" ?\n";
117 for(
int i = 0; i < num_scalars; ++i ) {
118 const ScalarMag rel_err = relErr<Scalar>( v1[i], v2[i] );
119 const bool result = ( !SMT::isnaninf(rel_err) && !SMT::isnaninf(maxRelErr_error) && rel_err <= maxRelErr_error );
120 if(!result) success =
false;
123 << li <<
" "<<setw(2)<<i<<
": rel_err("<<v1[i]<<
","<<v2[i]<<
") "<<
"= "<<rel_err
124 <<
" <= " << maxRelErr_error <<
" : " <<
passfail(result) << std::endl;
125 if( result && rel_err >= maxRelErr_warning ) {
127 << li <<
" Warning! rel_err(...) >= " << maxRelErr_warning_name <<
" = " << maxRelErr_warning <<
"!\n";
137 template<
class Scalar>
139 const std::string &v1_name,
141 const std::string &v2_name,
143 const std::string &maxRelErr_error_name,
145 const std::string &maxRelErr_warning_name,
147 std::ostream *out_inout,
149 const std::string &li
156 typedef typename ST::magnitudeType ScalarMag;
166 !SMT::isnaninf(rel_err)
167 && !SMT::isnaninf(maxRelErr_error)
168 && rel_err <= maxRelErr_error
173 << li <<
"Testing relative error between vectors "
174 << v1_name <<
" and " << v2_name <<
":\n";
177 << li <<
"||"<<v1_name<<
"|| = " << nrm_v1 << endl
178 << li <<
"||"<<v2_name<<
"|| = " << nrm_v2 << endl;
181 << li << v1_name <<
" = " << describe(v1,verbLevel)
182 << li << v2_name <<
" = " << describe(v2,verbLevel);
186 << li << v1_name <<
" - " << v2_name <<
" = " << describe(*diff,verbLevel);
189 << li <<
"Check: rel_err(" << v1_name <<
"," << v2_name <<
") = "
190 << rel_err <<
" <= " << maxRelErr_error_name <<
" = "
191 << maxRelErr_error <<
" : " <<
passfail(success) << endl;
192 if( success && rel_err >= maxRelErr_warning ) {
194 << li <<
"Warning! rel_err(" << v1_name <<
"," << v2_name <<
" >= "
195 << maxRelErr_warning_name <<
" = " << maxRelErr_warning <<
"!\n";
202 template<
class Scalar>
204 const std::string &error_name
206 ,
const std::string &max_error_name
208 ,
const std::string &max_warning_name
211 ,
const std::string &li
215 typedef typename ST::magnitudeType ScalarMag;
217 const ScalarMag error_mag = ST::magnitude(error);
218 const bool success = (
219 !SMT::isnaninf(error_mag)
220 && !SMT::isnaninf(max_error)
221 && error_mag <= max_error );
225 << li <<
"Check: |" << error_name <<
"| = " << error_mag
226 <<
" <= " << max_error_name <<
" = " << max_error <<
" : "
228 if( success && error_mag >= max_warning ) {
230 << li <<
"Warning! " << error_name <<
" = " << error_mag
231 <<
" >= " << max_warning_name <<
" = " << max_warning <<
"!\n";
238 template<
class Scalar>
240 const std::string &error_name,
242 const std::string &max_error_name,
244 const std::string &max_warning_name,
247 const std::string &li
252 typedef typename ST::magnitudeType ScalarMag;
255 const int num_scalars = errors.size();
258 return testMaxErr<Scalar>(
259 error_name, errors[0],
260 max_error_name, max_error,
261 max_warning_name, max_warning,
270 << li <<
"Check: |"<<error_name<<
"| <= "<<max_error_name<<
" ?\n";
271 for(
int i = 0; i < num_scalars; ++i ) {
272 const ScalarMag error_mag = ST::magnitude(errors[i]);
273 const bool result = (
274 !SMT::isnaninf(error_mag)
275 && !SMT::isnaninf(max_error)
276 && error_mag <= max_error );
277 if(!result) success =
false;
280 << li <<
" "<<setw(2)<<i<<
": |"<<errors[i]<<
"| = "<<error_mag<<
" <= "
281 <<max_error<<
" : "<<
passfail(success)<<
"\n";
282 if( result && error_mag >= max_warning ) {
284 << li <<
" Warning! |...| >= "<<max_warning_name<<
" = "<<max_warning<<
"!\n";
292 template<
class Scalar>
293 std::ostream& Thyra::operator<<( std::ostream& o, const VectorBase<Scalar>& v )
299 template<
class Scalar>
300 std::ostream& Thyra::operator<<( std::ostream& o, const LinearOpBase<Scalar>& M )
306 #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.