Thyra Package Browser (Single Doxygen Collection) Version of the Day
SolveStatus_UnitTests.cpp
Go to the documentation of this file.
00001 #include "Thyra_SolveSupportTypes.hpp"
00002 #include "Thyra_DefaultSpmdVectorSpace.hpp"
00003 #include "Thyra_VectorBase.hpp"
00004 #include "Thyra_VectorStdOps.hpp"
00005 
00006 #include "Teuchos_UnitTestHarness.hpp"
00007 #include "Thyra_UnitTestHelpers.hpp"
00008 
00009 
00010 namespace Thyra {
00011 
00012 
00013 //
00014 // Helper code
00015 //
00016 
00017 
00018 using Teuchos::as;
00019 
00020 
00021 const Ordinal n = 4;
00022 
00023 
00025 template<class Scalar>
00026 class MockNormReductionFunctional : public ReductionFunctional<Scalar> {
00027 public:
00028 
00030   MockNormReductionFunctional(
00031     const RCP<const VectorSpaceBase<Scalar> > &space
00032     )
00033     : space_(space)
00034     {}
00035 
00038   virtual std::string description() const
00039     {
00040       std::ostringstream oss;
00041       oss << typeName(*this)<<"{"<<space_->description()<<"}";
00042       return oss.str();
00043     }
00044 
00046  
00047 protected:
00048 
00051 
00053   virtual typename ScalarTraits<Scalar>::magnitudeType
00054   reduceImpl(const VectorBase<Scalar> &v) const
00055     {
00056       typedef ScalarTraits<Scalar> ST;
00057       return norm<Scalar>(v);
00058     }
00059 
00061   virtual bool isCompatibleImpl( const VectorBase<Scalar> &v ) const
00062     {
00063       return space_->isCompatible(*v.space());
00064     }
00065 
00067 
00068 private:
00069 
00070   RCP<const VectorSpaceBase<Scalar> > space_;
00071 
00072 };
00073 
00074 
00075 template<class Scalar>
00076 RCP<MockNormReductionFunctional<Scalar> >
00077 createMockNormReductionFunctional(
00078   const RCP<const VectorSpaceBase<Scalar> > &space
00079   )
00080 {
00081   return Teuchos::rcp(new MockNormReductionFunctional<Scalar>(space));
00082 }
00083 
00084 
00085 //
00086 // Unit Tests
00087 //
00088 
00089 
00090 //
00091 // SolveMeasureType
00092 //
00093 
00094 
00095 TEUCHOS_UNIT_TEST( SolveMeasureType, matches )
00096 {
00097   TEST_ASSERT(SolveMeasureType()(SOLVE_MEASURE_ONE, SOLVE_MEASURE_ONE));
00098   ECHO(SolveMeasureType solveMeasureType(SOLVE_MEASURE_NORM_RESIDUAL, SOLVE_MEASURE_NORM_INIT_RESIDUAL));
00099   TEST_ASSERT(solveMeasureType(SOLVE_MEASURE_NORM_RESIDUAL, SOLVE_MEASURE_NORM_INIT_RESIDUAL));
00100 }
00101 
00102 
00103 TEUCHOS_UNIT_TEST( SolveMeasureType, contains )
00104 {
00105   ECHO(SolveMeasureType solveMeasureType(SOLVE_MEASURE_NORM_RESIDUAL, SOLVE_MEASURE_NORM_INIT_RESIDUAL));
00106   TEST_EQUALITY_CONST(solveMeasureType.contains(SOLVE_MEASURE_ONE), false);
00107   TEST_EQUALITY_CONST(solveMeasureType.contains(SOLVE_MEASURE_NORM_RESIDUAL), true);
00108   TEST_EQUALITY_CONST(solveMeasureType.contains(SOLVE_MEASURE_NORM_SOLUTION), false);
00109   TEST_EQUALITY_CONST(solveMeasureType.contains(SOLVE_MEASURE_NORM_INIT_RESIDUAL), true);
00110   TEST_EQUALITY_CONST(solveMeasureType.contains(SOLVE_MEASURE_NORM_RHS), false);
00111 }
00112 
00113 
00114 //
00115 // ReductionFunctional
00116 //
00117 
00118 
00119 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ReductionFunctional, reduce, Scalar )
00120 {
00121   typedef ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
00122   typedef ScalarTraits<ScalarMag> SMT;
00123   const RCP<VectorBase<Scalar> > v = createMember<Scalar>(defaultSpmdVectorSpace<Scalar>(n));
00124   V_S(v.ptr(), ST::one());
00125   MockNormReductionFunctional<Scalar> mockNormReductionFunctional(v->space());
00126   TEST_FLOATING_EQUALITY(mockNormReductionFunctional.reduce(*v),
00127     SMT::squareroot(n),
00128     as<ScalarMag>(SMT::eps() * 10.0) );
00129 }
00130 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( ReductionFunctional, reduce )
00131 
00132 
00133 #ifdef THYRA_DEBUG
00134 
00135   
00136 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ReductionFunctional, notCompatible, Scalar )
00137 {
00138   typedef ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
00139   typedef ScalarTraits<ScalarMag> SMT;
00140   const RCP<VectorBase<Scalar> > v = createMember<Scalar>(defaultSpmdVectorSpace<Scalar>(n));
00141   MockNormReductionFunctional<Scalar> mockNormReductionFunctional(
00142     defaultSpmdVectorSpace<Scalar>(n/2));
00143   TEST_THROW(mockNormReductionFunctional.reduce(*v), Exceptions::IncompatibleVectorSpaces);
00144 }
00145 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( ReductionFunctional, notCompatible )
00146 
00147 
00148 #endif // THYRA_DEBUG
00149 
00150 
00151 //
00152 // SolveCriteria
00153 //
00154 
00155   
00156 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SolveCriteria, defaultConstruct, Scalar )
00157 {
00158   typedef ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
00159   typedef ScalarTraits<ScalarMag> SMT;
00160   SolveCriteria<Scalar> solveCriteria;
00161   TEST_EQUALITY(solveCriteria.solveMeasureType.numerator, SOLVE_MEASURE_ONE);
00162   TEST_EQUALITY(solveCriteria.solveMeasureType.denominator, SOLVE_MEASURE_ONE);
00163   TEST_EQUALITY(solveCriteria.requestedTol, SolveCriteria<Scalar>::unspecifiedTolerance());
00164   TEST_EQUALITY_CONST(solveCriteria.extraParameters, Teuchos::null);
00165   TEST_EQUALITY_CONST(solveCriteria.numeratorReductionFunc, Teuchos::null);
00166   TEST_EQUALITY_CONST(solveCriteria.denominatorReductionFunc, Teuchos::null);
00167 }
00168 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( SolveCriteria, defaultConstruct )
00169 
00170   
00171 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SolveCriteria, constructAll, Scalar )
00172 {
00173   typedef ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
00174   typedef ScalarTraits<ScalarMag> SMT;
00175   const SolveMeasureType solveMeasureType(SOLVE_MEASURE_NORM_RESIDUAL, SOLVE_MEASURE_NORM_RHS);
00176   const ScalarMag requestedTol = 0.5;
00177   RCP<ParameterList> extraParameters = Teuchos::parameterList();
00178   RCP<ReductionFunctional<Scalar> > numeratorReductionFunc =
00179     createMockNormReductionFunctional<Scalar>(Teuchos::null);
00180   RCP<ReductionFunctional<Scalar> > denominatorReductionFunc =
00181     createMockNormReductionFunctional<Scalar>(Teuchos::null);
00182   SolveCriteria<Scalar> solveCriteria(solveMeasureType, requestedTol,
00183     extraParameters, numeratorReductionFunc, denominatorReductionFunc);
00184   TEST_EQUALITY(solveCriteria.solveMeasureType.numerator, SOLVE_MEASURE_NORM_RESIDUAL);
00185   TEST_EQUALITY(solveCriteria.solveMeasureType.denominator, SOLVE_MEASURE_NORM_RHS);
00186   TEST_EQUALITY(solveCriteria.requestedTol, requestedTol);
00187   TEST_EQUALITY(solveCriteria.extraParameters, extraParameters);
00188   TEST_EQUALITY(solveCriteria.numeratorReductionFunc, numeratorReductionFunc);
00189   TEST_EQUALITY(solveCriteria.denominatorReductionFunc, denominatorReductionFunc);
00190 }
00191 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( SolveCriteria, constructAll )
00192 
00193 
00194 
00195 } // namespace Thyra
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines