00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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::Ordinal> >
00078 comm = Teuchos::DefaultComm<Thyra::Ordinal>::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 RCP<Thyra::DefaultProductVectorSpace<Scalar> > ps =
00090 rcp(new Thyra::DefaultProductVectorSpace<Scalar>(numBlocks,&vecSpaces[0]));
00091
00092 if(out.get()) *out << "\nps->numBlocks()=";
00093 result = ps->numBlocks() == numBlocks;
00094 if(!result) success = false;
00095 if(out.get()) *out
00096 << ps->numBlocks() << " == numBlocks=" << numBlocks
00097 << " : " << ( result ? "passed" : "failed" ) << std::endl;
00098
00099 if(out.get()) *out << "\nTesting the product space ps ...\n";
00100
00101 if(out.get()) *out << "\nps->dim()=";
00102 result = ps->dim() == numProcs*n*numBlocks;
00103 if(!result) success = false;
00104 if(out.get()) *out
00105 << ps->dim() << " == numProcs*n*numBlocks=" << numProcs*n*numBlocks
00106 << " : " << ( result ? "passed" : "failed" ) << std::endl;
00107
00108 if(out.get()) *out << "\nTesting the VectorSpaceBase interface of ps ...\n";
00109 result = vectorSpaceTester.check(*ps, out.get());
00110 if(!result) success = false;
00111
00112 if(out.get()) *out << "\nB) Testing a nested product space of product vector spaces called pps ...\n";
00113
00114 Teuchos::Array<RCP<const Thyra::VectorSpaceBase<Scalar> > >
00115 blockVecSpaces(numBlocks);
00116 for( int i = 0; i < numBlocks; ++i )
00117 blockVecSpaces[i] = ps;
00118
00119 RCP<Thyra::DefaultProductVectorSpace<Scalar> > pps =
00120 rcp(new Thyra::DefaultProductVectorSpace<Scalar> (numBlocks,&blockVecSpaces[0]));
00121
00122 if(out.get()) *out << "\nTesting the VectorSpaceBase interface of pps ...\n";
00123 result = vectorSpaceTester.check(*pps, out.get());
00124 if(!result) success = false;
00125
00126 if(numProcs==1) {
00127
00128 if(out.get()) *out
00129 << "\nC) Test the compatibility of serial vectors and product vectors with serial blocks."
00130 << "\n These tests demonstrate the principle of how all in-core vectors are compatible ...\n";
00131
00132 const Scalar
00133 one = ST::one(),
00134 two = Scalar(2)*one,
00135 three = Scalar(3)*one;
00136
00137 if(out.get()) *out << "\nCreating a serial vector space ss with numBlocks*n=" << numBlocks*n << " vector elements ...\n";
00138
00139 const RCP<const Thyra::VectorSpaceBase<Scalar> > ss
00140 = Thyra::defaultSpmdVectorSpace<Scalar>(numBlocks*n);
00141
00142 if(out.get()) *out << "\nTesting the serial space ss ...\n";
00143 result = vectorSpaceTester.check(*ss, out.get());
00144 if(!result) success = false;
00145
00146 if(out.get()) *out << "\nCreating product vectors; pv1, pv2 ...\n";
00147 RCP<Thyra::VectorBase<Scalar> >
00148 pv1 = createMember(*ps),
00149 pv2 = createMember(*ps);
00150
00151 if(out.get()) *out << "\nassign(&pv1,2.0) ...\n";
00152 Thyra::assign( &*pv1, two );
00153
00154 if(out.get()) *out << "\nassign(&pv1,3.0) ...\n";
00155 Thyra::assign( &*pv2, three );
00156
00157 if(out.get()) *out << "\nCreating serial vectors; sv1, sv2 ...\n";
00158 RCP<Thyra::VectorBase<Scalar> >
00159 sv1 = createMember(ss),
00160 sv2 = createMember(ss);
00161
00162 if(out.get()) *out << "\nassign(&sv1,*pv1) ...\n";
00163 Thyra::assign( &*sv1, *pv1 );
00164
00165 if(out.get()) *out << "\nsum(sv1)=";
00166 sresult1 = Thyra::sum(*sv1);
00167 sresult2 = two*Scalar(ps->dim());
00168 result = ( ST::magnitude( Thyra::relErr( sresult1, sresult2 ) )
00169 < ST::magnitude( tol ) );
00170 if(!result) success = false;
00171 if(out.get()) *out
00172 << sresult1 << " == 2*ps->dim()=" << sresult2
00173 << " : " << ( result ? "passed" : "failed" ) << std::endl;
00174
00175 if(out.get() && dumpAll) *out
00176 << "\nsv1 =\n" << *sv1;
00177
00178 if(out.get()) *out << "\nassign(&pv2,*sv1) ...\n";
00179 Thyra::assign( &*pv2, *sv1 );
00180
00181 if(out.get()) *out << "\nsum(pv2)=";
00182 sresult1 = Thyra::sum(*pv2);
00183 sresult2 = two*Scalar(ps->dim());
00184 result = ( ST::magnitude( Thyra::relErr( sresult1, sresult2 ) )
00185 < ST::magnitude( tol ) );
00186 if(!result) success = false;
00187 if(out.get()) *out
00188 << sresult1 << " == 2*ps->dim()=" << sresult2
00189 << " : " << ( result ? "passed" : "failed" ) << std::endl;
00190
00191 if(out.get() && dumpAll) *out
00192 << "\npv2 =\n" << *pv2;
00193
00194
00195
00196 }
00197
00198 if(out.get()) *out
00199 << "\n*** Leaving run_product_space_tests<"<<ST::name()<<">(...) ...\n";
00200
00201 return success;
00202
00203 }
00204
00205 int main( int argc, char* argv[] ) {
00206
00207 using Teuchos::CommandLineProcessor;
00208 using Teuchos::RCP;
00209
00210 bool success = true;
00211 bool verbose = true;
00212
00213 Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00214
00215 RCP<Teuchos::FancyOStream>
00216 out = Teuchos::VerboseObjectBase::getDefaultOStream();
00217
00218 try {
00219
00220
00221
00222
00223
00224 int n = 4;
00225 int numBlocks = 4;
00226 bool showAllTests = true;
00227 bool dumpAll = false;
00228
00229 CommandLineProcessor clp;
00230 clp.throwExceptions(false);
00231 clp.addOutputSetupOptions(true);
00232 clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00233 clp.setOption( "n", &n, "Number of elements in each constituent vector." );
00234 clp.setOption( "num-blocks", &numBlocks, "blocks to create." );
00235 clp.setOption( "dump-all", "no-dump-all", &dumpAll, "Determines if vectors are printed or not." );
00236 clp.setOption( "show-all-tests", "no-show-all-tests", &showAllTests, "Determines if all tests are printed or not." );
00237 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00238 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00239
00240
00241
00242
00243
00244 #ifdef HAVE_THYRA_TEUCHOS_BLASFLOAT
00245 if( !run_product_space_tests<float>(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00246 #endif // HAVE_THYRA_TEUCHOS_BLASFLOAT
00247 if( !run_product_space_tests<double>(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00248 #if defined(HAVE_THYRA_COMPLEX)
00249 #ifdef THYRA_TEUCHOS_BLASFLOAT
00250 if( !run_product_space_tests<std::complex<float> >(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00251 #endif // HAVE_THYRA_TEUCHOS_BLASFLOAT
00252 if( !run_product_space_tests<std::complex<double> >(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00253 #endif // defined(HAVE_THYRA_COMPLEX)
00254 #ifdef HAVE_TEUCHOS_GNU_MP
00255
00256
00257 #endif // HAVE_TEUCHOS_GNU_MP
00258
00259 }
00260 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,success)
00261
00262 if(verbose) {
00263 if(success)
00264 *out << "\nAll of the tests seem to have run successfully!\n";
00265 else
00266 *out << "\nOh no! at least one of the test failed!\n";
00267 }
00268
00269 return success ? 0 : 1;
00270
00271 }