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_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::RefCountPtr<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::RefCountPtr<const Thyra::VectorSpaceBase<Scalar> > >
00073 vecSpaces(numBlocks);
00074 const Teuchos::RefCountPtr<const Teuchos::Comm<Thyra::Index> >
00075 comm = Teuchos::DefaultComm<Thyra::Index>::getComm();
00076 const int numProcs = size(*comm);
00077 Teuchos::RefCountPtr<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::RefCountPtr<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::RefCountPtr<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::RefCountPtr<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
00189
00190 }
00191
00192 if(out.get()) *out
00193 << "\n*** Leaving run_product_space_tests<"<<ST::name()<<">(...) ...\n";
00194
00195 return success;
00196
00197 }
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::RefCountPtr<Teuchos::FancyOStream>
00209 out = Teuchos::VerboseObjectBase::getDefaultOStream();
00210
00211 try {
00212
00213
00214
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
00235
00236
00237 if( !run_product_space_tests<float>(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00238 if( !run_product_space_tests<double>(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00239 #if defined(HAVE_COMPLEX) && defined(HAVE_TEUCHOS_COMPLEX)
00240 if( !run_product_space_tests<std::complex<float> >(n,numBlocks,float(1e-5),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00241 if( !run_product_space_tests<std::complex<double> >(n,numBlocks,double(1e-13),showAllTests,dumpAll,verbose?&*out:NULL) ) success = false;
00242 #endif
00243 #ifdef HAVE_TEUCHOS_GNU_MP
00244
00245
00246 #endif
00247
00248 }
00249 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,*out,success)
00250
00251 if(verbose) {
00252 if(success)
00253 *out << "\nAll of the tests seem to have run successfully!\n";
00254 else
00255 *out << "\nOh no! at least one of the test failed!\n";
00256 }
00257
00258 return success ? 0 : 1;
00259
00260 }