#include "iwop.hh" #include "adjtest.hh" #include "MPS_to_RHS.hh" #include "MPS_iwop.hh" #include "MPS_Space_Examples.hh" #define CSQ "csq" #define BULKMOD "bulkmod" #define BUOYANCY "buoyancy" #define SOURCE_P "source_p" #define SOURCE_V0 "source_v0" #define SOURCE_V1 "source_v1" #define DATA_P "data_p" #define DATA_V0 "data_v0" #define DATA_V1 "data_v1" #ifndef MY_TOL #define MY_TOL 1e-3 #endif #define VERBOSE_MJB using RVL::valparse; using RVL::AssignFilename; using RVL::OperatorEvaluation; using TSOpt::IWaveLOVOp; using TSOpt::IWaveSpace; using TSOpt::CanScal_MPS_Space; using TSOpt::CanVec_MPS_Space; using TSOpt::ExVec_MPS_Space; using TSOpt::Scal_MPS_Space; using TSOpt::MPS_to_RHS; using TSOpt::MPS_IWaveLOVOp; template< class T_MPS_Space > void test_kernel(PARARRAY &pars, FILE &stream, string test_info){ std::stringstream str; string message; string jobname = valparse(pars,"jobname"); try { str << "\n============================================================\n" << "Test name: "<< jobname <<"\n" << "Runnning test for MPS_iwop.\n" << test_info <<"\n"; #ifdef IWAVE_USE_MPI str << "rank = "<< retrieveGlobalRank() <<"\n"; #endif str << "============================================================\n"; string driver_type = valparse(pars,"driver"); string src_type = valparse(pars,"src_type"); string data_type = valparse(pars,"data_type"); if( driver_type.compare("acd")!=0 && driver_type.compare("asg")!=0 ){ RVLException e; e << "Driver ("<< driver_type <<") is of unexpected type!\n"; throw e; } if( src_type.compare("pressure")!=0 && src_type.compare("velocity")!=0 ){ RVLException e; e << "Source ("<< src_type <<") is of unexpected type!\n"; throw e; } if( data_type.compare("pressure")!=0 && data_type.compare("velocity")!=0 ){ RVLException e; e << "Data ("<< data_type <<") is of unexpected type!\n"; throw e; } TSOpt::MPS_KEYS mks; mks.appx_ord = "appx_ord"; mks.MPS_file = "MPS_file"; mks.delta_file = "MPS_delta"; //setting mks.grid_file if( driver_type.compare("acd")==0 ){ mks.grid_file = CSQ; } else{ mks.grid_file = BULKMOD; } //setting mks.RHS_files if( src_type.compare("pressure")==0 ){ mks.RHS_files.push_back(SOURCE_P); } else{ mks.RHS_files.push_back(SOURCE_V0); mks.RHS_files.push_back(SOURCE_V1); } //setting mks.hdr_files and mks.G_files if( data_type.compare("pressure")==0 ){ mks.hdr_files.push_back(DATA_P); mks.G_files.push_back("data_p_g"); } else{ mks.hdr_files.push_back(DATA_V0); mks.hdr_files.push_back(DATA_V1); mks.G_files.push_back("data_v0_g"); mks.G_files.push_back("data_v1_g"); } ///////////////// // making data // ///////////////// #ifdef VERBOSE_MJB str << "\n[] Making data:\n"; #endif //initializing T_MPS_Space mps_sp(mks,pars); MPS_to_RHS m2r(mks,pars,mps_sp); IWaveLOVOp iwop(m2r.get_pars(),&stream); RVL::LinCompLOVOp IWOP(m2r,iwop); //setting inputs Vector x(IWOP.getDomain()); Components cx(x); Components cx0(cx[0]); Components cx1(cx[1]); if( driver_type.compare("acd")==0 ){ AssignFilename af_csq(valparse(pars,CSQ)); cx0[0].eval(af_csq); } else{ AssignFilename af_bmod(valparse(pars,BULKMOD)); AssignFilename af_buoy(valparse(pars,BUOYANCY)); cx0[0].eval(af_bmod); cx0[1].eval(af_buoy); } AssignFilename af_src(valparse(pars,mks.MPS_file)); cx1[0].eval(af_src); //setting outputs Vector y(IWOP.getRange()); Components cy(y); if(data_type.compare("pressure")==0){ AssignFilename af_data_p(valparse(pars,DATA_P)); cy[0].eval(af_data_p); } else{ AssignFilename af_data_v0(valparse(pars,DATA_V0)); AssignFilename af_data_v1(valparse(pars,DATA_V1)); cy[0].eval(af_data_v0); cy[1].eval(af_data_v1); } //evaluating operator OperatorEvaluation opeval(IWOP,x); y.copy(opeval.getValue()); #ifdef IWAVE_USE_MPI MPI_Barrier(retrieveGlobalComm()); #endif ///////////////////////////////// // making data via convolution // ///////////////////////////////// #ifdef VERBOSE_MJB str << "\n[] Making data via convolution:\n\n"; #endif //initialing MPS_IWaveLOVOp mps_lovop(mks,pars,&stream); //str << "writing out MPS_IWaveLOVOp:\n"; //mps_lovop.write(str); //output Vector y_re(mps_lovop.getRange()); Components cy_re(y_re); if( data_type.compare("pressure")==0 ){ AssignFilename af_data_p_re(valparse(pars,"data_p_re")); cy_re[0].eval(af_data_p_re); } else{ AssignFilename af_data_v0_re(valparse(pars,"data_v0_re")); AssignFilename af_data_v1_re(valparse(pars,"data_v1_re")); cy_re[0].eval(af_data_v0_re); cy_re[1].eval(af_data_v1_re); } //eval OperatorEvaluation opeval_re(mps_lovop,x); y_re.copy(opeval_re.getValue()); #ifdef IWAVE_USE_MPI MPI_Barrier(retrieveGlobalComm()); #endif //residual #ifdef VERBOSE_MJB str << "\n[] Residual\n\n"; #endif Vector fwd_res(y_re); fwd_res.copy(y_re); fwd_res.linComb(-1,y); float norm_fwd_res = fwd_res.norm() / y.norm(); bool fwd_test = (fabs(norm_fwd_res) lop(mps_lovop,cx[0]); std::stringstream res; RVL::RVLRandomize rnd(getpid(),-1.0,1.0); bool adj_test=RVL::AdjointTest(lop,rnd,res); if(adj_test) message="\n\033[1;32m[PASSED]\033[0m"; else message="\n\033[1;31m[FAILED]\033[0m"; message += " Adjoint test for MPS_conv op\n"; str << message; if(!adj_test) str << res.str(); #ifdef VERBOSE_MJB str << res.str(); #endif #ifdef IWAVE_USE_MPI MPI_Barrier(retrieveGlobalComm()); #endif // writing out string outfile = valparse(pars,"outfile",""); #ifdef IWAVE_USE_MPI for( int r=0; r0){ ofstream outf; outf.open(outfile.c_str(),ios::app); outf << str.str(); outf.close(); } else{ cout << str.str(); } #ifdef IWAVE_USE_MPI } MPI_Barrier(retrieveGlobalComm()); } #endif } catch(RVLException &e){ e << "ERROR from test_kernel()!\n" << "Dumping output stream:\n" << str.str(); throw e; } }