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

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