Thyra Package Browser (Single Doxygen Collection) Version of the Day
DefaultBlockedLinearOp_UnitTests.cpp
Go to the documentation of this file.
00001 
00002 #include "Thyra_DefaultSpmdVectorSpace.hpp"
00003 #include "Thyra_DefaultProductMultiVector.hpp"
00004 #include "Thyra_DefaultBlockedLinearOp.hpp"
00005 #include "Thyra_DefaultScaledAdjointLinearOp.hpp"
00006 #include "Thyra_MultiVectorStdOps.hpp"
00007 #include "Thyra_TestingTools.hpp"
00008 #include "Thyra_LinearOpTester.hpp"
00009 #include "Teuchos_DefaultComm.hpp"
00010 #include "Teuchos_FancyOStream.hpp"
00011 
00012 #include "Teuchos_UnitTestHarness.hpp"
00013 #include "Thyra_UnitTestHelpers.hpp"
00014 
00015 
00016 namespace {
00017 
00018 
00019 //
00020 // Helper code and declarations
00021 //
00022 
00023 
00024 bool dumpAll = false;
00025 
00026 
00027 TEUCHOS_STATIC_SETUP()
00028 {
00029   Teuchos::UnitTestRepository::getCLP().setOption(
00030     "dump-all", "no-dump-all", &dumpAll,
00031     "Dump lots of data" );
00032 }
00033 
00034 
00035 using Teuchos::as;
00036 using Teuchos::null;
00037 using Teuchos::is_null;
00038 using Teuchos::updateSuccess;
00039 using Teuchos::RCP;
00040 using Teuchos::rcpFromRef;
00041 using Teuchos::ptrFromRef;
00042 using Teuchos::fancyOStream;
00043 using Teuchos::get_extra_data;
00044 using Thyra::VectorSpaceBase;
00045 using Thyra::VectorBase;
00046 using Thyra::MultiVectorBase;
00047 using Thyra::LinearOpBase;
00048 using Thyra::createMember;
00049 using Thyra::createMembers;
00050 using Thyra::DefaultSpmdVectorSpace;
00051 using Thyra::defaultSpmdVectorSpace;
00052 using Thyra::DefaultBlockedLinearOp;
00053 using Thyra::defaultBlockedLinearOp;
00054 using Thyra::ProductVectorSpaceBase;
00055 using Thyra::PhysicallyBlockedLinearOpBase;
00056 using Thyra::LinearOpTester;
00057 typedef Thyra::Ordinal Ordinal;
00058 
00059 
00060 //
00061 // Unit Tests
00062 //
00063 
00064 
00065 const int m = 5;
00066 const int n = 3;
00067 const int p = 2;
00068 
00069 
00070 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultBlockedLinearOp, defaultConstruct,
00071   Scalar )
00072 {
00073 
00074   typedef Scalar S;
00075 
00076   const RCP<PhysicallyBlockedLinearOpBase<S> > M = defaultBlockedLinearOp<S>();
00077 
00078   TEST_ASSERT(is_null(M->range()));
00079   TEST_ASSERT(is_null(M->domain()));
00080   TEST_ASSERT(is_null(M->productRange()));
00081   TEST_ASSERT(is_null(M->productDomain()));
00082  
00083   std::ostringstream describe_msg;
00084 
00085   describe_msg << "'";
00086   M->describe(*fancyOStream(rcpFromRef(describe_msg)), Teuchos::VERB_LOW);
00087   describe_msg << "'";
00088 
00089   std::ostringstream expected_msg;
00090   expected_msg
00091     << "' " << M->Describable::description() << "{"
00092     << "numRowBlocks="<<0
00093     << ",numColBlocks="<<0
00094     << "}\n'";
00095 
00096   TEST_EQUALITY_CONST( describe_msg.str(), expected_msg.str() );
00097   
00098 }
00099 
00100 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultBlockedLinearOp,
00101   defaultConstruct )
00102 
00103 
00104 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultBlockedLinearOp, block1x1,
00105   Scalar )
00106 {
00107 
00108   typedef Scalar S;  typedef Teuchos::ScalarTraits<Scalar> ST;
00109   typedef typename ST::magnitudeType ScalarMag;
00110   typedef Teuchos::ScalarTraits<ScalarMag> SMT;
00111 
00112   const RCP<const VectorSpaceBase<S> > vsm =  defaultSpmdVectorSpace<S>(m);
00113   const RCP<const VectorSpaceBase<S> > vsn =  defaultSpmdVectorSpace<S>(n);
00114 
00115   const RCP<MultiVectorBase<S> > A = createMembers(vsm, n);
00116   Thyra::assign(A.ptr(), as<S>(1.0));
00117 
00118   const RCP<PhysicallyBlockedLinearOpBase<S> > M = defaultBlockedLinearOp<S>();
00119 
00120   TEST_EQUALITY_CONST(M->blockFillIsActive(), false);
00121   M->beginBlockFill(1, 1);
00122   TEST_EQUALITY_CONST(M->blockFillIsActive(), true);
00123   TEST_EQUALITY_CONST(M->acceptsBlock(0, 0), true);
00124   M->setBlock(0, 0, A);
00125   M->endBlockFill();
00126   TEST_EQUALITY_CONST(M->blockFillIsActive(), false);
00127  
00128   Thyra::LinearOpTester<S> linearOpTester;
00129   linearOpTester.set_all_error_tol(1e3*SMT::eps());
00130   linearOpTester.dump_all(dumpAll);
00131 
00132   updateSuccess(linearOpTester.check(*M, ptrFromRef(out)), success);
00133 
00134   const RCP<VectorBase<S> >  x = createMember<S>(vsn), y = createMember<S>(vsm);
00135   Thyra::assign(x.ptr(), as<Scalar>(2.0));
00136   Thyra::apply<S>( *M, Thyra::NOTRANS, *x, y.ptr() );
00137   TEST_FLOATING_EQUALITY( Thyra::sum(*y), as<Scalar>(m*n*1.0*2.0),
00138     as<ScalarMag>(SMT::eps() / (n*m)) );
00139 
00140   const RCP<const LinearOpBase<S> > M2 = Thyra::block1x1<S>(A);
00141   updateSuccess(linearOpTester.compare(*M, *M2, ptrFromRef(out)), success);
00142 
00143   const RCP<const LinearOpBase<S> > M3 = Thyra::nonconstBlock1x1<S>(A);
00144   updateSuccess(linearOpTester.compare(*M, *M3, ptrFromRef(out)), success);
00145   
00146 }
00147 
00148 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultBlockedLinearOp,
00149   block1x1 )
00150 
00151 
00152 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( DefaultBlockedLinearOp, nestedBlock2x2,
00153   Scalar )
00154 {
00155 
00156   typedef Scalar S;
00157   typedef Teuchos::ScalarTraits<Scalar> ST;
00158   typedef typename ST::magnitudeType ScalarMag;
00159   typedef Teuchos::ScalarTraits<ScalarMag> SMT;
00160   
00161   using Teuchos::describe;
00162   using Teuchos::rcp_dynamic_cast;
00163   using Thyra::assign;
00164   using Thyra::block1x2;
00165   using Thyra::block2x1;
00166   using Thyra::block2x2;
00167   using Thyra::adjoint;
00168 
00169   const RCP<const VectorSpaceBase<S> > vsm = defaultSpmdVectorSpace<S>(m);
00170   const RCP<const VectorSpaceBase<S> > vsn = defaultSpmdVectorSpace<S>(n);
00171   const RCP<const VectorSpaceBase<S> > vsp = defaultSpmdVectorSpace<S>(p);
00172 
00173   const RCP<MultiVectorBase<S> >
00174     A = createMembers(vsm, m, "A"),
00175     B = createMembers(vsn, m, "B"),
00176     Pm = createMembers(vsm, p, "Pm"),
00177     Pn = createMembers(vsn, p, "Pn"),
00178     Q = createMembers(vsp, p, "Q");
00179   
00180   assign<S>(A.ptr(), as<S>(1.0));
00181   assign<S>(B.ptr(), as<S>(2.0));
00182   assign<S>(Pm.ptr(), as<S>(3.0));
00183   assign<S>(Pn.ptr(), as<S>(4.0));
00184   assign<S>(Q.ptr(), as<S>(5.0));
00185   
00186   const RCP<const LinearOpBase<S> >
00187     M00 = block2x2<S>(A, adjoint<S>(B), B, null),
00188     M01 = block2x1<S>(Pm, Pn),
00189     M10 = block1x2<S>(adjoint<S>(Pm), adjoint<S>(Pn));
00190 
00191   const RCP<const LinearOpBase<S> >
00192     M = block2x2<S>( M00, M01, M10, Q, "M" );
00193 
00194   out << "M = " << describe(*M, Teuchos::VERB_MEDIUM);
00195   out << "M->range() = " << describe(*M->range(), Teuchos::VERB_MEDIUM);
00196   out << "M->domain() = " << describe(*M->range(), Teuchos::VERB_MEDIUM);
00197 
00198   const RCP<const PhysicallyBlockedLinearOpBase<S> >
00199     pbM = rcp_dynamic_cast<const PhysicallyBlockedLinearOpBase<S> >(M, true);
00200 
00201   TEST_NOTHROW(
00202     rcp_dynamic_cast<const ProductVectorSpaceBase<S> >(
00203       pbM->productRange()->getBlock(0), true)
00204     );
00205   TEST_NOTHROW(
00206     rcp_dynamic_cast<const DefaultSpmdVectorSpace<S> >(
00207       pbM->productRange()->getBlock(1), true)
00208     );
00209   TEST_NOTHROW(
00210     rcp_dynamic_cast<const ProductVectorSpaceBase<S> >(
00211       pbM->productRange()->getBlock(0), true)
00212     );
00213   TEST_NOTHROW(
00214     rcp_dynamic_cast<const DefaultSpmdVectorSpace<S> >(
00215       pbM->productDomain()->getBlock(1), true)
00216     );
00217  
00218   Thyra::LinearOpTester<S> linearOpTester;
00219   linearOpTester.set_all_error_tol(1e3*SMT::eps());
00220   linearOpTester.dump_all(dumpAll);
00221 
00222   updateSuccess(linearOpTester.check(*M, ptrFromRef(out)), success);
00223 
00224   const RCP<const LinearOpBase<S> >
00225     M2 = block2x2<S>( Q, M10, M01, M00, "M2" );
00226 
00227   out << "M2 = " << describe(*M2, Teuchos::VERB_MEDIUM);
00228   out << "M2->range() = " << describe(*M2->range(), Teuchos::VERB_MEDIUM);
00229   out << "M2->domain() = " << describe(*M2->range(), Teuchos::VERB_MEDIUM);
00230 
00231   const RCP<const PhysicallyBlockedLinearOpBase<S> >
00232     pbM2 = rcp_dynamic_cast<const PhysicallyBlockedLinearOpBase<S> >(M2, true);
00233 
00234   TEST_NOTHROW(
00235     rcp_dynamic_cast<const DefaultSpmdVectorSpace<S> >(
00236       pbM2->productRange()->getBlock(0), true)
00237     );
00238   TEST_NOTHROW(
00239     rcp_dynamic_cast<const ProductVectorSpaceBase<S> >(
00240       pbM2->productRange()->getBlock(1), true)
00241     );
00242   TEST_NOTHROW(
00243     rcp_dynamic_cast<const DefaultSpmdVectorSpace<S> >(
00244       pbM2->productDomain()->getBlock(0), true)
00245     );
00246   TEST_NOTHROW(
00247     rcp_dynamic_cast<const ProductVectorSpaceBase<S> >(
00248       pbM2->productRange()->getBlock(1), true)
00249     );
00250  
00251   updateSuccess(linearOpTester.check(*M2, ptrFromRef(out)), success);
00252   
00253 }
00254 
00255 THYRA_UNIT_TEST_TEMPLATE_1_INSTANT_SCALAR_TYPES( DefaultBlockedLinearOp,
00256   nestedBlock2x2 )
00257 
00258 
00259 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines