// MPS_conv.cc // Author: Mario J. Bencomo // last modified: 11/10/16 #include "MPS_conv.hh" //#define VERBOSE_MJB namespace TSOpt{ //----------------------------------------------------------------------// void MPS_conv_kern( vector s_pos, int MPS_dim, int MPS_idx, string x_file, vector y_files, vector k_files ){ //----------------------------------------------------------------------// try{ #ifdef VERBOSE_MJB cerr << "*************************************************\n" << " Inside MPS_conv_kern\n" << "*************************************************\n\n"; cerr << "x file = " << x_file << "\n" << "y files = "; print_vec(y_files); cerr << "k files = "; print_vec(k_files); cerr << "MPS_dim = " << MPS_dim << "\n" << "MPS_idx = " << MPS_idx << "\n"; #endif FILE *fp_x, *fp_y, *fp_k; segy tr_x, tr_y, tr_k; float * x_arr = NULL; float * y_arr = NULL; float * k_arr = NULL; int NTR_x, NTR_y, NTR_k; int NS_x, NS_y, NS_k; int NTR_src; //# traces per source int i_shift, i_shift_x, i_shift_y, i_shift_k; fpos_t pos_y; if( MPS_idx<0 || MPS_idx>=MPS_dim ){ RVLException e; e << "MPS_idx="<< MPS_idx << " out of bounds!\n"; throw e; } //getting size of input and setting up buffer fp_x = iwave_const_fopen(x_file.c_str(),"r",NULL,stderr); if( fp_x==NULL ){ RVLException e; e << "Could not open input file "<< x_file <<"\n"; throw e; } fgettr(fp_x,&tr_x); NTR_x = get_ntr(x_file); NS_x = tr_x.ns; iwave_fclose(fp_x); fp_x=NULL; #ifdef VERBOSE_MJB cerr << "NTR_x = " << NTR_x << "\n" << "NS_x = " << NS_x << "\n"; #endif x_arr = new float[NTR_x*NS_x]; if(x_arr==NULL){ RVLException e; e << "Trouble allocating memory for input buffer array.\n"; throw e; } //reading in input buffer fp_x = iwave_const_fopen(x_file.c_str(),"r",NULL,stderr); for( int i_tr=0; i_tr s_pos, int MPS_dim, int MPS_idx, string x_file, vector y_files, vector k_files ){ //----------------------------------------------------------------------// try{ #ifdef VERBOSE_MJB cerr << "*************************************************\n" << " Inside MPS_corr_kern\n" << "*************************************************\n\n"; cerr << "x file = " << x_file << "\n" << "y files = "; print_vec(y_files); cerr << "k files = "; print_vec(k_files); cerr << "MPS_dim = " << MPS_dim << "\n" << "MPS_idx = " << MPS_idx << "\n"; #endif FILE *fp_x, *fp_y, *fp_k; segy tr_x, tr_y, tr_k; float * x_arr = NULL; float * y_arr = NULL; float * k_arr = NULL; float * buff_arr = NULL; int NTR_x, NTR_y, NTR_k; int NS_x, NS_y, NS_k; int NTR_src; //# traces per source int i_shift, i_shift_x, i_shift_y, i_shift_k; fpos_t pos_x; if( MPS_idx<0 || MPS_idx>=MPS_dim ){ RVLException e; e << "MPS_idx="<< MPS_idx << " out of bounds!\n"; throw e; } //getting size of output and setting up buffer fp_x = iwave_const_fopen(x_file.c_str(),"r",NULL,stderr); if( fp_x==NULL ){ RVLException e; e << "Could not open output file "<< x_file <<"\n"; throw e; } fgettr(fp_x,&tr_x); NTR_x = get_ntr(x_file); NS_x = tr_x.ns; iwave_fclose(fp_x); fp_x=NULL; #ifdef VERBOSE_MJB cerr << "NTR_x = " << NTR_x << "\n" << "NS_x = " << NS_x << "\n"; #endif x_arr = new float[NTR_x*NS_x]; if(x_arr==NULL){ RVLException e; e << "Trouble allocating memory for output buffer array.\n"; throw e; } for(int i_tr=0; i_tr &k, MPS_Space const &d, IWaveSpace const &r ) : pars(NULL), ker_ptr(), ker_keys(0), dom(d), rng(r), MPS_idx(-1) { //----------------------------------------------------------------------// try{ //copying pars pars = ps_new(); if(ps_copy(&pars,_pars)){ RVLException e; e << "failed to copy parameter table\n"; throw e; } //initializing and setting ker set_kernel(k); //running sanity check sanity_checks(); //extracting kernel keys IWaveSpace const* sp_ker = dynamic_cast(&(ker_ptr->getSpace())); for( int i=0; igetSize(); i++){ string key = sp_ker->getKeys()[i]; key += "_ker"; ker_keys.push_back(key); } } catch(RVLException & e){ e << "ERROR from MPS_conv constructor\n"; throw e; } } //----------------------------------------------------------------------// MPS_conv::~MPS_conv(){ //----------------------------------------------------------------------// ps_delete(&pars); } //----------------------------------------------------------------------// void MPS_conv::set_kernel(Vector const &k){ //----------------------------------------------------------------------// try{ ker_ptr = Vector::newPtr(k.getSpace()); change_kernel(k); } catch(RVLException &e){ e << "ERROR from MPS_conv::set_kernel\n"; throw e; } } //----------------------------------------------------------------------// void MPS_conv::change_kernel(Vector const &k){ //----------------------------------------------------------------------// try{ SpaceTest(ker_ptr->getSpace(),k,"MPS_conv::change_kernel (kern sp)"); ker_ptr->copy(k); //sanity_checks(); } catch(RVLException &e){ e << "ERROR from MPS_conv::change_kernel\n"; throw e; } } //----------------------------------------------------------------------// void MPS_conv::sanity_checks(){ //----------------------------------------------------------------------// try{ //Checking that size of greens and rng spaces matches. IWaveSpace const* sp_ker = dynamic_cast(&(ker_ptr->getSpace())); if(sp_ker->getSize()!=rng.getSize()){ RVLException e; e << "size of kernel space and rng space does not match!\n"; throw e; } //Checking that MPS_Space has same sources as //green's kernel, and rng IWaveSpace. //number of sources from MPS_Space domain vector s_pos_MPS = dom.get_s_pos(); ///////////////////////////////// //looping over subspaces of rng// ///////////////////////////////// for( int i=0; i(&(rng[i])); #else SEGYSpace const *segy_rng = dynamic_cast(&(rng[i])); #endif if(segy_rng==NULL){ RVLException e; e << "trouble getting segy space from rng["<getPrototypeFilename(); vector s_pos_rng = ex_pos(file_rng,false); //extracting src pos for green sp i #ifdef IWAVE_USE_MPI MPISEGYSpace const *segy_ker = dynamic_cast(&((*sp_ker)[i])); #else SEGYSpace const *segy_ker = dynamic_cast(&((*sp_ker)[i])); #endif if(segy_ker==NULL){ RVLException e; e << "trouble getting segy space from sp_ker["<getPrototypeFilename(); vector s_pos_ker = ex_pos(file_ker,false); //checking number of sources if( s_pos_MPS.size()!=s_pos_rng.size() || s_pos_MPS.size()!=s_pos_ker.size() ){ RVLException e; e << "Number of sources does not match!\n" << "Nsrc in MPS = "<< s_pos_MPS.size() << "\n" << "Nsrc in rng = "<< s_pos_rng.size() << "\n" << "Nsrc in ker = "<< s_pos_ker.size() << "\n"; throw e; } //checking src pos for( int j=0; j const &x, Vector const &y ) const{ //----------------------------------------------------------------------// try{ AssignParams ap_x(*pars,dom.get_MPS_keys().MPS_file,stderr); x.eval(ap_x); Components cy(y); for (size_t i=0; i ck(*ker_ptr); for (size_t i=0; iwrite(str); return str; } //----------------------------------------------------------------------// void MPS_conv::apply( const Vector &x, Vector &y ) const{ //----------------------------------------------------------------------// try{ if( !(ker_ptr) ){ RVLException e; e << "Empty kernel vector!\n"; throw e; } SpaceTest(this->get_MPS_Domain(),x,"MPS_conv::apply (dom)"); SpaceTest(this->get_IWave_Range(),y,"MPS_conv::apply (rng)"); param_set(x,y); // zero output y.zero(); //extracting file names string x_file = valparse(*pars,dom.get_MPS_keys().MPS_file); vector y_files(0); vector k_files(0); for(int i=0; i(*pars,rng.getKeys()[i])); k_files.push_back(valparse(*pars,ker_keys[i])); } //convolution kernel #ifdef IWAVE_USE_MPI MPS_conv_kern_MPI( dom.get_s_pos(), dom.get_dim(), MPS_idx, x_file, y_files, k_files ); #else MPS_conv_kern( dom.get_s_pos(), dom.get_dim(), MPS_idx, x_file, y_files, k_files ); #endif } catch (RVLException & e) { e<<"ERROR from MPS_conv::apply\n"; throw e; } } //----------------------------------------------------------------------// void MPS_conv::applyAdj( const Vector &y, Vector &x ) const { //----------------------------------------------------------------------// try{ if( !(ker_ptr) ){ RVLException e; e << "Empty kernel vector!\n"; throw e; } SpaceTest(this->get_MPS_Domain(),x,"MPS_conv::applyAdj (dom)"); SpaceTest(this->get_IWave_Range(),y,"MPS_conv::applyAdj (rng)"); param_set(x,y); // zero output x.zero(); //extracting file names string x_file = valparse(*pars,dom.get_MPS_keys().MPS_file); vector y_files(0); vector k_files(0); for(int i=0; i(*pars,rng.getKeys()[i])); k_files.push_back(valparse(*pars,ker_keys[i])); } //cross-correlation kernel #ifdef IWAVE_USE_MPI MPS_corr_kern_MPI( dom.get_s_pos(), dom.get_dim(), MPS_idx, x_file, y_files, k_files ); #else MPS_corr_kern( dom.get_s_pos(), dom.get_dim(), MPS_idx, x_file, y_files, k_files ); #endif } catch (RVLException & e) { e<<"ERROR from MPS_conv::applyAdj\n"; throw e; } } }