test_product_space.cpp

Click here for a more detailed discussion of this example/test program.

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_TestingTools.hpp"
00033 #include "Teuchos_CommandLineProcessor.hpp"
00034 #include "Teuchos_DefaultComm.hpp"
00035 #include "Teuchos_CommHelpers.hpp"
00036 #include "Teuchos_GlobalMPISession.hpp"
00037 #include "Teuchos_StandardCatchMacros.hpp"
00038 #include "Teuchos_VerboseObject.hpp"
00039 
00042 template <class Scalar>
00043 bool run_product_space_tests(
00044   const int                                                     n
00045   ,const int                                                    numBlocks
00046   ,const typename Teuchos::ScalarTraits<Scalar>::magnitudeType  &tol
00047   ,const bool                                                   showAllTests
00048   ,const bool                                                   dumpAll
00049   ,Teuchos::FancyOStream                                        *out_arg
00050   )
00051 {
00052 
00053   using Thyra::relErr;
00054   using Teuchos::OSTab;
00055   using Teuchos::rcp;
00056   using Teuchos::RCP;
00057 
00058   typedef Teuchos::ScalarTraits<Scalar> ST;
00059   typedef typename ST::magnitudeType    ScalarMag;
00060 
00061   RCP<Teuchos::FancyOStream>
00062     out = Teuchos::fancyOStream(rcp(out_arg,false));
00063 
00064   if(out.get()) *out << "\n*** Entering run_product_space_tests<"<<ST::name()<<">(...) ...\n";
00065 
00066   bool success = true, result;
00067   Scalar sresult1, sresult2;
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   Teuchos::Array<RCP<const Thyra::VectorSpaceBase<Scalar> > >
00076     vecSpaces(numBlocks);
00077   const RCP<const Teuchos::Comm<Thyra::Index> >
00078     comm = Teuchos::DefaultComm<Thyra::Index>::getComm();
00079   const int numProcs = size(*comm);
00080   RCP<const Thyra::VectorSpaceBase<Scalar> >
00081     spaceBlock = Thyra::defaultSpmdVectorSpace<Scalar>(comm,n,-1);
00082   for( int i = 0; i < numBlocks; ++i )
00083     vecSpaces[i] = spaceBlock;
00084   
00085   if(out.get()) *out << "\nA) Performing basic tests on product vectors with SPMD constituent vectors ...\n";
00086 
00087   if(out.get()) *out << "\nCreating a product space ps with numBlocks="<<numBlocks<<" and n="<<n<<"vector elements per block ...\n";
00088 
00089   Thyra::DefaultProductVectorSpace<Scalar> ps(numBlocks,&vecSpaces[0]);
00090 
00091   if(out.get()) *out << "\nps.numBlocks()=";
00092   result = ps.numBlocks() == numBlocks;
00093   if(!result) success = false;
00094   if(out.get()) *out
00095     << ps.numBlocks() << " == numBlocks=" << numBlocks
00096     << " : " << ( result ? "passed" : "failed" ) << std::endl;
00097 
00098   if(out.get()) *out << "\nTesting the product space ps ...\n";
00099 
00100   if(out.get()) *out << "\nps.dim()=";
00101   result = ps.dim() == numProcs*n*numBlocks;
00102   if(!result) success = false;
00103   if(out.get()) *out
00104     << ps.dim() << " == numProcs*n*numBlocks=" << numProcs*n*numBlocks
00105     << " : " << ( result ? "passed" : "failed" ) << std::endl;
00106   
00107   if(out.get()) *out << "\nTesting the VectorSpaceBase interface of ps ...\n";
00108   result = vectorSpaceTester.check(ps,out.get());
00109   if(!result) success = false;
00110   
00111   if(out.get()) *out << "\nB) Testing a nested product space of product vector spaces called pps ...\n";
00112 
00113   Teuchos::Array<RCP<const Thyra::VectorSpaceBase<Scalar> > >
00114     blockVecSpaces(numBlocks);
00115   for( int i = 0; i < numBlocks; ++i )
00116     blockVecSpaces[i] = Teuchos::rcp(&ps,false);
00117 
00118   Thyra::DefaultProductVectorSpace<Scalar> pps(numBlocks,&blockVecSpaces[0]);
00119   
00120   if(out.get()) *out << "\nTesting the VectorSpaceBase interface of pps ...\n";
00121   result = vectorSpaceTester.check(pps,out.get());
00122   if(!result) success = false;
00123 
00124   if(numProcs==1) {
00125 
00126     if(out.get()) *out
00127       << "\nC) Test the compatibility of serial vectors and product vectors with serial blocks."
00128       << "\n   These tests demonstrate the principle of how all in-core vectors are compatible ...\n";
00129     
00130     const Scalar
00131       one   = ST::one(),
00132       two   = Scalar(2)*one,
00133       three = Scalar(3)*one;
00134     
00135     if(out.get()) *out << "\nCreating a serial vector space ss with numBlocks*n=" << numBlocks*n << " vector elements ...\n";
00136     
00137     const RCP<const Thyra::VectorSpaceBase<Scalar> > ss
00138       = Thyra::defaultSpmdVectorSpace<Scalar>(numBlocks*n);
00139     
00140     if(out.get()) *out << "\nTesting the serial space ss ...\n";
00141     result = vectorSpaceTester.check(*ss, out.get());
00142     if(!result) success = false;
00143     
00144     if(out.get()) *out << "\nCreating product vectors; pv1, pv2 ...\n";
00145     RCP<Thyra::VectorBase<Scalar> >
00146       pv1 = createMember(ps),
00147       pv2 = createMember(ps);
00148     
00149     if(out.get()) *out << "\nassign(&pv1,2.0) ...\n";
00150     Thyra::assign( &*pv1, two );
00151     
00152     if(out.get()) *out << "\nassign(&pv1,3.0) ...\n";
00153     Thyra::assign( &*pv2, three );
00154     
00155     if(out.get()) *out << "\nCreating serial vectors; sv1, sv2 ...\n";
00156     RCP<Thyra::VectorBase<Scalar> >
00157       sv1 = createMember(ss),
00158       sv2 = createMember(ss);
00159     
00160     if(out.get()) *out << "\nassign(&sv1,*pv1) ...\n";
00161     Thyra::assign( &*sv1, *pv1 );
00162     
00163     if(out.get()) *out << "\nsum(sv1)=";
00164     sresult1 = Thyra::sum(*sv1);
00165     sresult2 = two*Scalar(ps.dim());
00166     result = ( ST::magnitude( Thyra::relErr( sresult1, sresult2 ) )
00167                < ST::magnitude( tol ) );
00168     if(!result) success = false;
00169     if(out.get()) *out
00170       << sresult1 << " == 2*ps.dim()=" << sresult2
00171       << " : " << ( result ? "passed" : "failed" ) << std::endl;
00172     
00173     if(out.get() && dumpAll) *out
00174       << "\nsv1 =\n" << *sv1;
00175     
00176     if(out.get()) *out << "\nassign(&pv2,*sv1) ...\n";
00177     Thyra::assign( &*pv2, *sv1 );
00178     
00179     if(out.get()) *out << "\nsum(pv2)=";
00180     sresult1 = Thyra::sum(*pv2);
00181     sresult2 = two*Scalar(ps.dim());
00182     result = ( ST::magnitude( Thyra::relErr( sresult1, sresult2 ) )
00183                < ST::magnitude( tol ) );
00184     if(!result) success = false;
00185     if(out.get()) *out
00186       << sresult1 << " == 2*ps.dim()=" << sresult2
00187       << " : " << ( result ? "passed" : "failed" ) << std::endl;
00188     
00189     if(out.get() && dumpAll) *out
00190       << "\npv2 =\n" << *pv2;
00191     
00192     // ToDo: Finish tests!
00193 
00194   }
00195     
00196   if(out.get()) *out
00197     << "\n*** Leaving run_product_space_tests<"<<ST::name()<<">(...) ...\n";
00198   
00199   return success;
00200 
00201 } // end run_product_space_tests() [Doxygen looks for this!]
00202 
00203 int main( int argc, char* argv[] ) {
00204 
00205   using Teuchos::CommandLineProcessor;
00206   using Teuchos::RCP;
00207 
00208   bool success = true;
00209   bool verbose = true;
00210 
00211   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00212 
00213   RCP<Teuchos::FancyOStream>
00214     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00215 
00216   try {
00217 
00218     //
00219     // Read options from command-line
00220     //
00221 
00222     int n              = 4;
00223     int numBlocks      = 4;
00224     bool showAllTests  = true;
00225     bool dumpAll       = false;
00226 
00227     CommandLineProcessor  clp;
00228     clp.throwExceptions(false);
00229     clp.addOutputSetupOptions(true);
00230     clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00231     clp.setOption( "n", &n, "Number of elements in each constituent vector." );
00232     clp.setOption( "num-blocks", &numBlocks, "blocks to create." );
00233     clp.setOption( "dump-all", "no-dump-all", &dumpAll, "Determines if vectors are printed or not." );
00234     clp.setOption( "show-all-tests", "no-show-all-tests", &showAllTests, "Determines if all tests are printed or not." );
00235     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00236     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00237 
00238     //
00239     // Run the tests
00240     //
00241 
00242 #ifdef HAVE_THYRA_TEUCHOS_BLASFLOAT
00243     if( !run_product_space_tests<float>(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00244 #endif // HAVE_THYRA_TEUCHOS_BLASFLOAT
00245     if( !run_product_space_tests<double>(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00246 #if defined(HAVE_THYRA_COMPLEX)
00247 #ifdef THYRA_TEUCHOS_BLASFLOAT
00248     if( !run_product_space_tests<std::complex<float> >(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00249 #endif // HAVE_THYRA_TEUCHOS_BLASFLOAT
00250     if( !run_product_space_tests<std::complex<double> >(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00251 #endif // defined(HAVE_THYRA_COMPLEX)
00252 #ifdef HAVE_TEUCHOS_GNU_MP
00253     //if( !run_product_space_tests<mpf_class>(n,numBlocks,mpf_class(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00254     // Above commented out code will not compile because its ScalarTraits specialization does not support eps()
00255 #endif // HAVE_TEUCHOS_GNU_MP
00256 
00257   } // end try
00258   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,success)
00259 
00260   if(verbose) {
00261     if(success)
00262       *out << "\nAll of the tests seem to have run successfully!\n";
00263     else
00264       *out << "\nOh no! at least one of the test failed!\n";  
00265   }
00266   
00267   return success ? 0 : 1;
00268 
00269 } // end main() [Doxygen looks for this!]

Generated on Wed May 12 21:26:53 2010 for Thyra Operator/Vector Support by  doxygen 1.4.7