Thyra Package Browser (Single Doxygen Collection) Version of the Day
test_product_space.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Thyra_DefaultProductVectorSpace.hpp"
00030 #include "Thyra_DefaultSpmdVectorSpace.hpp"
00031 #include "Thyra_VectorSpaceTester.hpp"
00032 #include "Thyra_VectorStdOpsTester.hpp"
00033 #include "Thyra_TestingTools.hpp"
00034 #include "Teuchos_CommandLineProcessor.hpp"
00035 #include "Teuchos_DefaultComm.hpp"
00036 #include "Teuchos_CommHelpers.hpp"
00037 #include "Teuchos_GlobalMPISession.hpp"
00038 #include "Teuchos_StandardCatchMacros.hpp"
00039 #include "Teuchos_VerboseObject.hpp"
00040 
00043 template <class Scalar>
00044 bool run_product_space_tests(
00045   const int                                                     n
00046   ,const int                                                    numBlocks
00047   ,const typename Teuchos::ScalarTraits<Scalar>::magnitudeType  &tol
00048   ,const bool                                                   showAllTests
00049   ,const bool                                                   dumpAll
00050   ,Teuchos::FancyOStream                                        *out_arg
00051   )
00052 {
00053 
00054   using Thyra::relErr;
00055   using Teuchos::OSTab;
00056   using Teuchos::rcp;
00057   using Teuchos::RCP;
00058 
00059   typedef Teuchos::ScalarTraits<Scalar> ST;
00060   typedef typename ST::magnitudeType    ScalarMag;
00061 
00062   RCP<Teuchos::FancyOStream>
00063     out = Teuchos::fancyOStream(rcp(out_arg,false));
00064 
00065   if(out.get()) *out << "\n*** Entering run_product_space_tests<"<<ST::name()<<">(...) ...\n";
00066 
00067   bool success = true, result;
00068 
00069   Thyra::VectorSpaceTester<Scalar> vectorSpaceTester;
00070   vectorSpaceTester.warning_tol(ScalarMag(0.1)*tol);
00071   vectorSpaceTester.error_tol(tol);
00072   vectorSpaceTester.show_all_tests(showAllTests);
00073   vectorSpaceTester.dump_all(dumpAll);
00074 
00075   Thyra::VectorStdOpsTester<Scalar> vectorStdOpsTester;
00076   vectorStdOpsTester.warning_tol(ScalarMag(0.1)*tol);
00077   vectorStdOpsTester.error_tol(tol);
00078 
00079   Teuchos::Array<RCP<const Thyra::VectorSpaceBase<Scalar> > >
00080     vecSpaces(numBlocks);
00081   const RCP<const Teuchos::Comm<Thyra::Ordinal> >
00082     comm = Teuchos::DefaultComm<Thyra::Ordinal>::getComm();
00083   const int numProcs = size(*comm);
00084   RCP<const Thyra::VectorSpaceBase<Scalar> >
00085     spaceBlock = Thyra::defaultSpmdVectorSpace<Scalar>(comm,n,-1);
00086   for( int i = 0; i < numBlocks; ++i )
00087     vecSpaces[i] = spaceBlock;
00088   
00089   if(out.get()) *out << "\nA) Performing basic tests on product vectors with SPMD constituent vectors ...\n";
00090 
00091   if(out.get()) *out << "\nCreating a product space ps with numBlocks="<<numBlocks<<" and n="<<n<<"vector elements per block ...\n";
00092 
00093   RCP<Thyra::DefaultProductVectorSpace<Scalar> > ps =
00094     rcp(new Thyra::DefaultProductVectorSpace<Scalar>(numBlocks,&vecSpaces[0]));
00095 
00096   if(out.get()) *out << "\nps->numBlocks()=";
00097   result = ps->numBlocks() == numBlocks;
00098   if(!result) success = false;
00099   if(out.get()) *out
00100     << ps->numBlocks() << " == numBlocks=" << numBlocks
00101     << " : " << ( result ? "passed" : "failed" ) << std::endl;
00102 
00103   if(out.get()) *out << "\nTesting the product space ps ...\n";
00104 
00105   if(out.get()) *out << "\nps->dim()=";
00106   result = ps->dim() == numProcs*n*numBlocks;
00107   if(!result) success = false;
00108   if(out.get()) *out
00109     << ps->dim() << " == numProcs*n*numBlocks=" << numProcs*n*numBlocks
00110     << " : " << ( result ? "passed" : "failed" ) << std::endl;
00111   
00112   if(out.get()) *out << "\nTesting the VectorSpaceBase interface of ps ...\n";
00113   TEUCHOS_TEST_ASSERT(vectorSpaceTester.check(*ps, out.get()), *out, success);
00114   
00115   if(out.get()) *out << "\nTesting standard vector ops for ps ...\n";
00116   TEUCHOS_TEST_ASSERT(vectorStdOpsTester.checkStdOps(*ps, out.get()), *out, success);
00117   
00118   if(out.get()) *out << "\nB) Testing a nested product space of product vector spaces called pps ...\n";
00119 
00120   Teuchos::Array<RCP<const Thyra::VectorSpaceBase<Scalar> > >
00121     blockVecSpaces(numBlocks);
00122   for( int i = 0; i < numBlocks; ++i )
00123     blockVecSpaces[i] = ps;
00124 
00125   RCP<Thyra::DefaultProductVectorSpace<Scalar> > pps =
00126     rcp(new Thyra::DefaultProductVectorSpace<Scalar> (numBlocks,&blockVecSpaces[0]));
00127   
00128   if(out.get()) *out << "\nTesting the VectorSpaceBase interface of pps ...\n";
00129   TEUCHOS_TEST_ASSERT(vectorSpaceTester.check(*pps, out.get()), *out, success);
00130   
00131   if(out.get()) *out << "\nTesting standard vector ops for pps ...\n";
00132   TEUCHOS_TEST_ASSERT(vectorStdOpsTester.checkStdOps(*pps, out.get()), *out, success);
00133     
00134   if(out.get()) *out
00135     << "\n*** Leaving run_product_space_tests<"<<ST::name()<<">(...) ...\n";
00136   
00137   return success;
00138 
00139 } // end run_product_space_tests() [Doxygen looks for this!]
00140 
00141 
00142 int main( int argc, char* argv[] ) {
00143 
00144   using Teuchos::CommandLineProcessor;
00145   using Teuchos::RCP;
00146 
00147   bool success = true;
00148   bool verbose = true;
00149 
00150   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00151 
00152   RCP<Teuchos::FancyOStream>
00153     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00154 
00155   try {
00156 
00157     //
00158     // Read options from command-line
00159     //
00160 
00161     int n              = 4;
00162     int numBlocks      = 4;
00163     bool showAllTests  = true;
00164     bool dumpAll       = false;
00165 
00166     CommandLineProcessor  clp;
00167     clp.throwExceptions(false);
00168     clp.addOutputSetupOptions(true);
00169     clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00170     clp.setOption( "n", &n, "Number of elements in each constituent vector." );
00171     clp.setOption( "num-blocks", &numBlocks, "blocks to create." );
00172     clp.setOption( "dump-all", "no-dump-all", &dumpAll, "Determines if vectors are printed or not." );
00173     clp.setOption( "show-all-tests", "no-show-all-tests", &showAllTests, "Determines if all tests are printed or not." );
00174     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00175     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00176 
00177     //
00178     // Run the tests
00179     //
00180 
00181 #ifdef HAVE_THYRA_TEUCHOS_BLASFLOAT
00182     if( !run_product_space_tests<float>(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00183 #endif // HAVE_THYRA_TEUCHOS_BLASFLOAT
00184     if( !run_product_space_tests<double>(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00185 #if defined(HAVE_THYRA_COMPLEX)
00186 #ifdef THYRA_TEUCHOS_BLASFLOAT
00187     if( !run_product_space_tests<std::complex<float> >(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00188 #endif // HAVE_THYRA_TEUCHOS_BLASFLOAT
00189     if( !run_product_space_tests<std::complex<double> >(n,numBlocks,double(1e-12),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00190 #endif // defined(HAVE_THYRA_COMPLEX)
00191 #ifdef HAVE_TEUCHOS_GNU_MP
00192     //if( !run_product_space_tests<mpf_class>(n,numBlocks,mpf_class(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00193     // Above commented out code will not compile because its ScalarTraits specialization does not support eps()
00194 #endif // HAVE_TEUCHOS_GNU_MP
00195 
00196   } // end try
00197   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,success)
00198 
00199   if(verbose) {
00200     if(success)
00201       *out << "\nAll of the tests seem to have run successfully!\n";
00202     else
00203       *out << "\nOh no! at least one of the test failed!\n";  
00204   }
00205   
00206   return success ? 0 : 1;
00207 
00208 } // end main() [Doxygen looks for this!]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines