// MPS_to_RHS.cc // Author: Mario J. Bencomo // last modified: 11/10/16 #include "MPS_to_RHS.hh" //#define VERBOSE_MJB namespace TSOpt { //----------------------------------------------------------------------// void write_RHS_SEGY( vector stencils, vector s_idx, vector s_res, grid g, string ref_file, vector RHS_files ){ //----------------------------------------------------------------------// try{ #ifdef IWAVE_USE_MPI if( retrieveGlobalRank()!=0 ){ RVLException e; e << "write_RHS_SEGY should only be called by rank 0!\n"; } #endif //extrating prototype trace from ref_file FILE *fp_ref = iwave_const_fopen(ref_file.c_str(),"r",NULL,stderr); segy tr; fvgettr(fp_ref,&tr); iwave_fclose(fp_ref); //zeroing trace for( int it=0; it s_idx, vector s_res, vector stencils, vector MPS_basis, string MPS_file, vector RHS_files ){ //----------------------------------------------------------------------// try{ #ifdef IWAVE_USE_MPI if( retrieveGlobalRank()!=0 ){ RVLException e; e << "MPS_to_RHS_kern should only be called by rank 0!\n"; throw e; } #endif #ifdef VERBOSE_MJB cerr << "*************************************************\n" << " Inside MPS_to_RHS_kern\n" << "*************************************************\n\n"; cerr << "MPS file = " << MPS_file << "\n"; #endif FILE *fp_mps = iwave_const_fopen(MPS_file.c_str(),"r",NULL,stderr); segy tr_mps; fpos_t pos_rhs; ////////////////////////// // loop over RHS source // ////////////////////////// for( int i_rhs=0; i_rhs s_idx, vector s_res, vector stencils, vector MPS_basis, string MPS_file, vector RHS_files ){ //----------------------------------------------------------------------// try{ #ifdef IWAVE_USE_MPI if( retrieveGlobalRank()!=0 ){ RVLException e; e << "RHS_to_MPS_kern should only be called by rank 0!\n"; throw e; } #endif #ifdef VERBOSE_MJB cerr << "*************************************************\n" << " Inside RHS_to_MPS_kern\n" << "*************************************************\n\n"; #endif FILE *fp_mps = iwave_const_fopen(MPS_file.c_str(),"r+",NULL,stderr); segy tr_mps; fpos_t pos_mps; //reset file to beginning if (fseeko(fp_mps, 0L, SEEK_SET)){ RVLException e; e << "could not find trace 0 in file "<< MPS_file<<"\n"; throw e; } /////////////////////// // loop over sources // /////////////////////// for( int i_src=0; i_src(*pars,mks.appx_ord); //setting up spatial grid info string grid_file = valparse(*pars,mks.grid_file); init_default_grid(&g); read_grid(&g,grid_file.c_str(),stderr); if(g.axes[0].o<0) g.axes[0].o*=-1; if(g.axes[0].d<0) g.axes[0].d*=-1; //set s_idx and s_res set_s_idx_res(); //set stencils set_stencils(); //set range set_rng_ptr(); //sanity checks sanity_checks(); } catch(RVLException &e){ e << "ERROR from MPS_to_RHS constructor!\n"; throw e; } } //----------------------------------------------------------------------// MPS_to_RHS::~MPS_to_RHS(){ //----------------------------------------------------------------------// ps_delete(&pars); } //----------------------------------------------------------------------// void MPS_to_RHS::set_s_idx_res(){ //----------------------------------------------------------------------// try{ //getting source positions vector s_pos = dom.get_s_pos(); //cheking that s_pos is inside spatial grid for( int i=0; ig_end ){ RVLException e; e << "source position is out of bounds!\n" << "s_pos["< MPS_basis = dom.get_MPS_basis(); MPS_point pt; pt.ext[0]=0; pt.ext[1]=0; pt.ext[2]=0; pt.Lidx.resize(MPS_basis.size()); ///////////////////////// // Looping over z-axis // ///////////////////////// for( int iz=0; izg_e[0] ){ pt.loc[0] = pt.loc[0] - 2*g_e[0]; pt.ext[0] = 1; } ///////////////////////// // Looping over x-axis // ///////////////////////// for( int ix=0; ixg_e[1] ){ pt.loc[1] = pt.loc[1] - 2*g_e[1]; pt.ext[1] = 1; } ///////////////////////// // Looping over y-axis // ///////////////////////// for( int iy=0; iyg_e[2] ){ pt.loc[2] = pt.loc[2] - 2*g_e[2]; pt.ext[2] = 1; } //////////////////////////// // Looping over MPS basis // //////////////////////////// for( int i_b=0; i_b RHS_files; /////////////////////////////////////////////// // case where RHS files need to be generated // /////////////////////////////////////////////// if( make_RHS ){ try{ //Extracting RHS filenames, if empty will generate filenames for( int i=0; i(*pars,mks.RHS_files[i]); if(fn.compare("empty")==0){ RVLException e; throw e; } RHS_files.push_back(valparse(*pars,mks.RHS_files[i])); } } //Generating RHS filenames catch(RVLException &e){ cerr << "WARNING: from MPS_to_RHS::set_rng_ptr\n" << "No RHS filenames given, will generate names: "; RHS_files.clear(); for( int i=0; i(*pars,mks.RHS_files[i])); } /////////////////////////// // generating RHS spaces // /////////////////////////// //updating parameter table add_to_pars( *pars, mks.RHS_files, RHS_files ); //cerr << "Inside gen_RHS_Space\n"; //ps_printall(*pars,stderr); //generating space //will replace with: //shared_ptr sp_ptr // = make_shared(*pars,ic,true,2,cerr) IWaveSpace iw_sp(*pars,ic,true,2,cerr); shared_ptr< Space > tmp = Space::clonePtr(iw_sp); rng_ptr = dynamic_pointer_cast< IWaveSpace >(tmp); if( !(rng_ptr) ){ RVLException e; e << "failed to make rng_ptr\n"; throw e; } } catch(RVLException &e){ e << "ERROR from MPS_to_RHS::set_rng_ptr\n"; throw e; } } //----------------------------------------------------------------------// void MPS_to_RHS::sanity_checks() const{ //----------------------------------------------------------------------// try{ //checking that dom.t_order and dom.s_dim are compatible with //number of RHS files if( dom.get_t_ord()==0 ){ if( mks.RHS_files.size()!=1 ){ RVLException e; e << "Expecting number of RHS files to equal 1, " << "for t_ord="<getKeys().size(); i_rhs++ ){ string rhs_file = valparse(*pars,rng_ptr->getKeys()[i_rhs]); int ntr = get_ntr(rhs_file); //expected number of traces int NTR = s_idx.size(); //number of sources for(int i=0; i const &x, Vector const &y ) const{ //----------------------------------------------------------------------// try{ //setting x AssignParams apx(locpars, get_MPS_keys().MPS_file); x.eval(apx); //setting y Components cy(y); vector RHS_keys = rng_ptr->getKeys(); for( int i=0; i MPS_to_RHS::get_rng_filenames() const{ //----------------------------------------------------------------------// try{ vector keys = rng_ptr->getKeys(); vector fns(keys.size()); for( int i=0; i(*pars,keys[i]); return fns; } catch(RVLException &e){ e << "ERROR from MPS_to_RHS::get_dom_filename\n"; throw e; } } //----------------------------------------------------------------------// void MPS_to_RHS::print_info(ostream &str) const{ //----------------------------------------------------------------------// try{ str <<"======================================================\n" <<"Printing MPS_to_RHS info.\n" <<"printing domain: "; dom.print_info(str); str <<"\n" <<"make_RHS = "<< make_RHS <<"\n" <<"printing range: "; rng_ptr->write(str); str <<"\n" <<"printing grid:\n" <<" g.dim="< &x, Vector &y ) const { //----------------------------------------------------------------------// try{ SpaceTest(get_MPS_Domain(),x,"MPS_to_RHS::apply (dom)"); SpaceTest(get_IWave_Range(),y,"MPS_to_RHS::apply (rng)"); PARARRAY * locpars = ps_new(); ps_copy(&locpars,*pars); param_set(*locpars,x,y); // ps_printall(*pars,stderr); //fflush(stderr); //zero output y.zero(); //extracting filenames string MPS_file = valparse( *locpars, mks.MPS_file ); vector RHS_files( rng_ptr->getKeys().size() ); for( int i=0; i( *locpars, (rng_ptr->getKeys())[i] ); } #ifdef IWAVE_USE_MPI if( retrieveGlobalRank()==0 ){ #endif Ituple d_ord; //MOD d_ord = dom.get_d_ord(); //d_ord.coor[0]=dom.get_MPS_ord(); //function writes out RHS, given pars and iokeys MPS_to_RHS_kern( a_ord, d_ord, g, s_idx, s_res, stencils, dom.get_MPS_basis(), MPS_file, RHS_files ); #ifdef IWAVE_USE_MPI } MPI_Barrier(retrieveGlobalComm()); #endif } catch( RVLException &e ){ e << "ERROR from MPS_to_RHS::apply!\n"; throw e; } } //----------------------------------------------------------------------// void MPS_to_RHS::applyAdj( const Vector &y, Vector &x ) const { //----------------------------------------------------------------------// try{ SpaceTest(get_MPS_Domain(),x,"MPS_to_RHS::applyAdj (dom)"); SpaceTest(get_IWave_Range(),y,"MPS_to_RHS::applyAdj (rng)"); PARARRAY * locpars = ps_new(); ps_copy(&locpars,*pars); param_set(*locpars,x,y); //zero output x.zero(); //extracting filenames string MPS_file = valparse( *locpars, mks.MPS_file ); vector RHS_files( rng_ptr->getKeys().size() ); for( int i=0; i( *locpars, (rng_ptr->getKeys())[i] ); } #ifdef IWAVE_USE_MPI if( retrieveGlobalRank()==0 ){ #endif Ituple d_ord; //MOD d_ord = dom.get_d_ord(); //d_ord.coor[0]=dom.get_MPS_ord(); //function writes out RHS, given pars and iokeys RHS_to_MPS_kern( a_ord, d_ord, g, s_idx, s_res, stencils, dom.get_MPS_basis(), MPS_file, RHS_files ); #ifdef IWAVE_USE_MPI } MPI_Barrier(retrieveGlobalComm()); #endif } catch( RVLException &e ){ e << "ERROR from MPS_to_RHS::applyAdj!\n"; throw e; } } }//end TSOpt