Ejemplo n.º 1
0
int main(int argc, char* argv[])
{
    int nz,nx,ny,sou_z,sou_ox,sou_oy,sou_jx,sou_jy,sou_nx,sou_ny,rec_z,rec_nx,rec_ny,npad,noff,roll;
    int dim1, dim2;
    sf_axis ad1=NULL, ad2=NULL;
    sf_file Fgeo=NULL;
    int **geo=NULL;

    sf_init(argc,argv);

    if (!sf_getint("nz",&nz)) sf_error("Need nz="); /* dimension in z */
    if (!sf_getint("nx",&nx)) sf_error("Need nx="); /* dimension in x */
    if (!sf_getint("ny",&ny)) sf_error("Need ny="); /* dimension in y */
    if (!sf_getint("sou_z", &sou_z )) sf_error("Need sou_z=" ); /* source position in depth      */
    if (!sf_getint("sou_ox",&sou_ox)) sf_error("Need sou_ox="); /* source starting location in x */
    if (!sf_getint("sou_oy",&sou_oy)) sf_error("Need sou_oy="); /* source starting location in y */
    if (!sf_getint("sou_nx",&sou_nx)) sf_error("Need sou_nx="); /* number of sources in x        */
    if (!sf_getint("sou_ny",&sou_ny)) sf_error("Need sou_ny="); /* number of sources in y        */
    if (!sf_getint("sou_jx",&sou_jx)) sou_jx = (sou_nx>1)? (nx-sou_ox)/(sou_nx-1):0; /* source interval in x */
    if (!sf_getint("sou_jy",&sou_jy)) sou_jy = (sou_ny>1)? (ny-sou_oy)/(sou_ny-1):0; /* source interval in y */
    if (!sf_getint("rec_z", &rec_z )) sf_error("Need rec_z=" ); /* receiver position in depth */
    if (!sf_getint("rec_nx",&rec_nx)) sf_error("Need rec_nx="); /* number of receivers in x   */
    if (!sf_getint("rec_ny",&rec_ny)) sf_error("Need rec_ny="); /* number of receivers in y   */
    if (!sf_getint("npad",&npad)) sf_error("Need npad="); /* computational domain padding */
    if (!sf_getint("noff",&noff)) sf_error("Need noff="); /* near offset */
    if (!sf_getint("roll",&roll)) sf_error("Need roll="); /* acquisition pattern: 0-> fixed-spread, 1-> towed-streamer to the negative */

    /* double check dimension */
    if (sou_nx > (nx-sou_ox)/sou_jx+1) {
        sou_nx = (nx-sou_ox)/sou_jx+1;
        sf_warning("Setting sou_nx to %d",sou_nx);
    }
    if (sou_ny > 1 && sou_ny > (ny-sou_oy)/sou_jy+1) {
        sou_ny = (ny-sou_oy)/sou_jy+1;
        sf_warning("Setting sou_ny to %d",sou_ny);
    }

    /* do the work */
    dim1 = 14;
    dim2 = sou_nx*sou_ny;

    ad1 = sf_maxa(dim1,0,1); sf_setlabel(ad1,"acqpar"); sf_raxa(ad1);
    ad2 = sf_maxa(dim2,0,1); sf_setlabel(ad2,"shot");   sf_raxa(ad2);

    Fgeo = sf_output("out");
    sf_settype(Fgeo,SF_INT);
    sf_oaxa(Fgeo,ad1,1);
    sf_oaxa(Fgeo,ad2,2);

    geo = sf_intalloc2(dim1,dim2);
    geogen(geo,nz,nx,ny,sou_z,sou_ox,sou_oy,sou_jx,sou_jy,sou_nx,sou_ny,rec_z,rec_nx,rec_ny,npad,noff,roll);

    sf_intwrite(geo[0],dim1*dim2,Fgeo);

    exit(0);
}
Ejemplo n.º 2
0
	int main(int argc, char* argv[])
	{

		bool verb,conj,twin,Pf1,PG;
		
	    /* OMP parameters */
		#ifdef _OPENMP
	    int ompnth;
		#endif 	
		
		float	*F_arrival, *Refl, *G, *Gm, *Gp, *f1pS, *f1p, *F1pS, *F1p;
		float	*MS, *MS1, *F1m_0, *f1m_0, *F1m, *F1m1, *f1m, *ms, *gm, *gp;
    float *MS_0, *ms_0, *ms_2, *MS2;
    float *gp1, *gm1;
		float	*window, *taper, pi;
		int		*tw,allocated=0;

	    /* I/O files */
	    sf_file FF_arrival;
	    sf_file FRefl;
	    sf_file FGp;
	    sf_file FGm;
	    sf_file FG;
	    sf_file Ff1m;
	    sf_file Ff1p;
	    sf_file Ftwin;

		char *filename1, filename2[256], filename3[256];
		
		/* Cube axes */
	    sf_axis at,af,ax,at1;

	    int     nt,nf,ntr,mode,nshots,niter,len;
	    int     i,it,ix,ishot,iter,i0;
	    int		twc, twa, shift, n[2], rect[2], s[2], tap;
	    float   scale,eps,dt,df,dx,ot,of,a,b,c,d,e,f,r;

		sf_triangle tr;

	    /*------------------------------------------------------------*/
	    /* Initialize RSF parameters 								  */
	    /*------------------------------------------------------------*/
	    sf_init(argc,argv);	
		
	    /*------------------------------------------------------------*/
	    /* Initialize OMP parameters */
	    /*------------------------------------------------------------*/
		#ifdef _OPENMP
	    ompnth=omp_init();
		#endif	

		/*------------------------------------------------------------*/
		/* Flags 													  */
		/*------------------------------------------------------------*/
	    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
	    if(! sf_getbool("conj",&conj)) conj=false; /* complex conjugation (time-reversal) flag */
	    if(! sf_getbool("twin",&twin)) twin=false; /* returns the timewindow as one of the outputs */
	    if(! sf_getbool("Pf1",&Pf1)) Pf1=false; /* Htot=true: returns H=Gp-Gm */
	    if(! sf_getbool("PG",&PG)) PG=false; /* Htot=true: returns H=Gp-Gm */
	    if(! sf_getint("niter",&niter)) niter=1; /* number of iterations */
	    if(! sf_getint("nshots",&nshots)) nshots=1; /* number of shots */
	    if(! sf_getfloat("r",&r)) r=-1; /* reflection coefficient if flux 
						normalised r=-1 */
	    if(! sf_getfloat("scale",&scale)) scale=1.0; /* scale factor */
		if(! sf_getfloat("eps",&eps)) eps=1e-4; /* threshold for the timewindow */
		if(! sf_getint("shift",&shift)) shift=5; /* shift in samples for the timewindow */
		if(! sf_getint("tap",&tap)) tap=20; /* taper of R */
	    
		if (verb) {
			fprintf(stderr,"This program was called with \"%s\".\n",argv[0]);
			/*fprintf(stderr,"Nr: %d Nx: %d Nt:%d\n",nr,nx,nt);*/
			
			if (argc > 1) {
				for (i = 1; i<argc; i++) {
					fprintf(stderr,"argv[%d] = %s\n", i, argv[i]);
				}
			}
			else {
				fprintf(stderr,"The command had no other arguments.\n");
		}	
		}

	    /*------------------------------------------------------------*/
	    /* I/O files 												  */
	    /*------------------------------------------------------------*/	
		/* "in" is the transposed version of p00plus_xxxx_xxxx.rsf
		   Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=ntr,n2=nt
		   Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=nt,n2=ntr */
		FF_arrival = sf_input("in");
		
		/* refl is REFL_000.rsf
		   It is used to read nf, df, of
		  Dimensions are: n1=nf,n2=ntr */
		/*FRefl = (sf_file)sf_alloc(1,sizeof(sf_file));*/	
		FRefl = sf_input("refl");

		FGp = sf_output("out");
		FGm = sf_output("Gm");
		
		if (PG) {
			FG  = sf_output("G");
		}
		if (Pf1) {
			Ff1p  = sf_output("f1p");
			Ff1m  = sf_output("f1m");
		}
		
		if (twin) {
			Ftwin = sf_output("window"); /* time window */
		}
		
		/*------------------------------------------------------------*/
		/* Axes */
		/*------------------------------------------------------------*/    
		at = sf_iaxa(FF_arrival,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */
		at1 = sf_iaxa(FF_arrival,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */
		af = sf_iaxa(FRefl,1); sf_setlabel(af,"Frequency"); if(verb) sf_raxa(af); /* frequency */
		ax = sf_iaxa(FF_arrival,2); sf_setlabel(ax,"r"); if(verb) sf_raxa(ax); /* space */
	    
		nt = sf_n(at); dt = sf_d(at); ot = sf_o(at);
		nf = sf_n(af); df = sf_d(af); of = sf_o(af);
		ntr = sf_n(ax); dx = sf_d(ax);

    int nt2=(nt/2);
    sf_setn(at1,nt2);


		if (verb) fprintf(stderr,"nt: %d nf: %d ntr:%d\n",nt,nf,ntr);

		sf_fileclose(FRefl);

	    /*------------------------------------------------------------*/
	    /* Setup output data and wavefield header					  */
	    /*------------------------------------------------------------*/
		sf_oaxa(FGp,at1,1);
		sf_oaxa(FGp,ax,2);
		sf_oaxa(FGm,at1,1);
		sf_oaxa(FGm,ax,2);
		
		if (PG) {
			sf_oaxa(FG,at1,1);
			sf_oaxa(FG,ax,2);
		}
		if (Pf1) {
			sf_oaxa(Ff1p,at,1);
			sf_oaxa(Ff1p,ax,2);
			sf_oaxa(Ff1m,at,1);
			sf_oaxa(Ff1m,ax,2);
		}
		if (twin) {
			sf_oaxa(Ftwin,at,1);
			sf_oaxa(Ftwin,ax,2);
		}

	    /*------------------------------------------------------------*/
	    /* Allocate arrays											  */
	    /*------------------------------------------------------------*/
		/* First arrival - Time */
		F_arrival = (float *)calloc(nt*ntr,sizeof(float)); allocated+=nt*ntr;
		sf_floatread(F_arrival,nt*ntr,FF_arrival);
		ms   = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		ms_0 = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		ms_2 = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		f1m_0= (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		f1m  = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		f1pS = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		f1p  = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		memcpy(ms,F_arrival,nt*ntr*sizeof(float));allocated+=nt*ntr;

		/* Allocate for coda M of f2 - Frequency */
		MS   = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		MS_0 = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		MS1  = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		MS2  = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		F1m_0= (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		F1m  = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		F1m1 = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		F1pS = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		F1p  = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr;
		/* The three flags of fft1 are: inv, sym, and opt */

    fft1_init (nt, dt, ot, true, false);
    fft1_2D_fwd(F_arrival,MS,ntr);
    //sf_warning("passed fft? yes");
		//fft1(F_arrival,MS,FF_arrival,0,0,1);
	/*	memcpy(FA,2*nf*ntr*sizeof(float));*/

    fprintf(stderr,"nt2 is %d\n",nt2);
		/* Output wavefields */
		G = (float *)calloc(nt2*ntr,sizeof(float));allocated+=nt2*ntr;
		gp1= (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		gp= (float *)calloc(nt2*ntr,sizeof(float));allocated+=nt2*ntr;
		gm1= (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		gm= (float *)calloc(nt2*ntr,sizeof(float));allocated+=nt2*ntr;
		Gp= (float *)calloc(2*nf*ntr,sizeof(float));allocated+=nf*ntr;
		Gm= (float *)calloc(2*nf*ntr,sizeof(float));allocated+=nf*ntr;


		
		/* Time-reversal flag */
		if (conj) {
			mode = -1;
		}
		else {
			mode = +1;
		}
	    
		/* Load the reflection response into the memory */
		if (verb) fprintf(stderr,"Before loading R %d\n",2*nf*ntr);
		Refl = (float *)calloc(2*nf*ntr*nshots,sizeof(float));allocated+=2*nf*ntr*nshots;
		
		/* Read REFL_000.rsf */
    FRefl = sf_input("refl");
    sf_warning("reading refl");
    sf_floatread(Refl,2*nf*nshots*ntr,FRefl); 
    sf_warning("read refl");

		/* Build time-window */
		tw = (int *)calloc(ntr,sizeof(int)); allocated+=ntr;
		window = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr;
		/*memset(window,0,nt*ntr*sizeof(float));*/
	    /* I am not sure why I set it to this value */
		/*for (ix=0; ix<ntr; ix++) {
			tw[ix] = nt*dt+ot+0.15; 
		}*/
		
		if (verb) fprintf(stderr,"---> Build time-window?\n");
    // checking time sample corresponding to muting time
		for (ix=0; ix<ntr; ix++) {
      for (it=0; it<nt; it++) {
        if ((F_arrival[it+ix*nt]*F_arrival[it+ix*nt])>eps*eps) {
          /*tw[ix] = it*dt+ot;*/
          tw[ix] = it;
          break;
        }
			}
		}

		if (verb) fprintf(stderr,"---> Build time-window1\n");
		for (ix=0; ix<ntr; ix++) {
			twc = (int)(tw[ix]-shift-10);
			twa = (int)(-twc+nt);
			/*if (verb) fprintf(stderr,"%d %d\n",twc,twa);*/
			for (it=0; it<nt; it++) {
			/*	if ((it>twa) || (it<twc)) {*/
				if ((it>twa) && (it<twc)) { 
					window[it+ix*nt] = 1.0; // building windowing function W from Filippo's paper
				}
			}
		}

		if (verb) fprintf(stderr,"---> Build time-window2\n");
		/* Smoothing of the window */
		/* Should I implement flags for rect and iter? */
		/* Look at Msmooth.c to understand below */
		n[0] = nt;
		n[1] = ntr;
		s[0] = 1;
		s[1] = nt;
		rect[0] = 5;
		rect[1] = 5;

		for (ix=0; ix <= 1; ix++) {
			if (rect[ix] <= 1) continue;
			tr = sf_triangle_init (rect[ix],n[ix],false);
			for (it=0; it < (nt*ntr/n[ix]); it++) {
				i0 = sf_first_index (ix,it,1+1,n,s);
				for (iter=0; iter < 2; iter++) {
					sf_smooth2 (tr,i0,s[ix],false,window );
				}
			}
			sf_triangle_close(tr);
		}
		if (verb) fprintf(stderr,"---> Here\n");
		
		/* Tapering */
		pi = 4.0*atan(1.0);
		
		taper = (float *)calloc(ntr,sizeof(float));allocated+=ntr;
    sf_warning("estimated memory: %f bytes",allocated*4.0f);

		memset(taper,0,ntr*sizeof(float));

		for (ix=0; ix<tap; ix++) {
			taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/(2*tap))));
			taper[ntr-ix-1] = taper[ix];
		}
		for (ix=tap; ix<(ntr-tap); ix++) {
			taper[ix] = 1.0;
		}
		if (verb) fprintf(stderr,"---> taper finish\n");
		
		FRefl = sf_input("refl");

		/*------------------------------------------------------------*/
		/* Loop over iterations */
		/*------------------------------------------------------------*/
		if (verb) fprintf(stderr,"---> Begin to iterative solve for f1p and f1m\n");
		/*starting iteration for f1m */
		memset(F1m_0,0,2*nf*ntr*sizeof(float));

			for (ishot=0; ishot<nshots; ishot++) {

				/* Loop over receivers (traces) */
				#ifdef _OPENMP
				#pragma omp parallel for private(ix,it,a,b,c,d) \
					shared(MS,taper,Refl,F1m_0,MS2)
				#endif
				for (ix=0; ix<ntr; ix++) {
					/* Loop over frequencies */
					#pragma ivdep
					for (it=0; it<2*nf; it=it+2) {

						/*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/
						a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot];
						b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot];
						c = MS[ishot*2*nf+it];
						d = MS[ishot*2*nf+it+1];

						F1m_0[ix*2*nf+it]   += (a*c - mode*b*d);
						F1m_0[ix*2*nf+it+1] += (mode*a*d + b*c);
						MS2  [ix*2*nf+it]   += r*(a*c - b*d); // rTd* R
						MS2  [ix*2*nf+it+1] += r*(a*d + b*c);

					} /* End of loop over frequencies */
				} /* End of loop over receivers (traces) */
			}

      fft1_2D_inv (F1m_0, f1m_0,ntr);
      fft1_2D_inv (MS2, ms_2,ntr);

			#ifdef _OPENMP
			#pragma omp parallel for private(ix,it) \
				shared(f1m, f1m_0, window,ms_2)
			#endif
			/* window to get f1m_0 */
			for (ix=0; ix<ntr; ix++) {
				#pragma ivdep
				for (it=0; it<nt; it++) {
					f1m_0[it+ix*nt] = scale*window[it+ix*nt]*f1m_0[it+ix*nt];  
					ms_2[it+ix*nt] = scale*window[it+ix*nt]*ms_2[it+ix*nt];  
					//f1m_0[it+ix*nt] = scale*f1m_0[it+ix*nt];  
					f1m[it+ix*nt] = f1m_0[it+ix*nt];
				}	
			}
		  fft1_2D_fwd(f1m,F1m,ntr);
	
			//fft1(f1m,F1m,FF_arrival,0,0,1);


	/* initialise MS the coda for f1+ */
		memset(MS_0,0,2*nf*ntr*sizeof(float));
		memset(MS,0,2*nf*ntr*sizeof(float));

			for (ishot=0; ishot<nshots; ishot++) {

				/* Loop over receivers (traces) */
				#ifdef _OPENMP
				#pragma omp parallel for private(ix,it,a,b,c,d) \
					shared(MS_0,taper,Refl,F1m)
				#endif
				for (ix=0; ix<ntr; ix++) {
					/* Loop over frequencies */
					#pragma ivdep
					for (it=0; it<2*nf; it=it+2) {

						/*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/
						a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot];
						b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot];
						c = F1m[ishot*2*nf+it];
						d = F1m[ishot*2*nf+it+1];

						MS_0[ix*2*nf+it]   += (a*c - mode*b*d);
						MS_0[ix*2*nf+it+1] += (mode*a*d + b*c);

					} /* End of loop over frequencies */
				} /* End of loop over receivers (traces) */
			}

		  fft1_2D_inv(MS_0,ms_0,ntr);
			//fft1(MS_0,ms_0,FRefl,1,0,1);

			#ifdef _OPENMP
			#pragma omp parallel for private(ix,it) \
				shared(ms, ms_0, window,ms_2)
			#endif
			/* window to get f1m_0 */
			for (ix=0; ix<ntr; ix++) {
				#pragma ivdep
				for (it=0; it<nt; it++) {
					ms[it+ix*nt] =-ms_2[it+ix*nt] +scale*window[it+ix*nt]*ms_0[it+ix*nt];  
					//f1m_0[it+ix*nt] = scale*f1m_0[it+ix*nt];  
				  //ms[it+ix*nt] = ms_0[it+ix*nt];
				}	
			}
			
		  fft1_2D_fwd(ms,MS,ntr);



	if (verb) fprintf(stderr,"---> Beginning Iteration\n");
		for (iter=0; iter<niter; iter++) {

	/* initialise MS1 and f1m1 the coda for f1+ */	
		memset(MS1,0,2*nf*ntr*sizeof(float));
		memset(F1m1,0,2*nf*ntr*sizeof(float));

			/*------------------------------------------------------------*/
			/* Loop over shot positions */
			/*------------------------------------------------------------*/
			for (ishot=0; ishot<nshots; ishot++) {
		
				/* Loop over receivers (traces) */
				#ifdef _OPENMP
				#pragma omp parallel for private(ix,it,a,b,c,d,e,f) \
					shared(MS,taper,Refl,F1m,F1m1)
				#endif 	
				for (ix=0; ix<ntr; ix++) {
					/* Loop over frequencies */
					#pragma ivdep
					for (it=0; it<2*nf; it=it+2) {
						
						/*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/
						/*(a + bi)(e + fi) = (ae - bf) + (af + be)i*/
						a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot];
						b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot];
						c = MS[ishot*2*nf+it];
						d = MS[ishot*2*nf+it+1];
						e = F1m[ishot*2*nf+it];
						f = F1m[ishot*2*nf+it+1];
						
						F1m1[ix*2*nf+it]   += (a*c - mode*b*d) - r*(a*e - b*f);
						F1m1[ix*2*nf+it+1] += (mode*a*d + b*c) - r*(a*f + b*e);
					
					} /* End of loop over frequencies */	
				} /* End of loop over receivers (traces) */
				
			} /* End of loop over shot positions */

			/* Get time domain output of f1m and ms */
		  fft1_2D_inv(F1m1,f1m,ntr);

			
			#ifdef _OPENMP
			#pragma omp parallel for private(ix,it) \
				shared(f1m, f1m_0, window)
			#endif
			for (ix=0; ix<ntr; ix++) {
				#pragma ivdep
				for (it=0; it<nt; it++) {
					f1m[it+ix*nt] = f1m_0[it+ix*nt] + scale*window[it+ix*nt]*(f1m[it+ix*nt]);  
				}	
			}
			
		  fft1_2D_fwd(f1m,F1m,ntr);

			for (ishot=0; ishot<nshots; ishot++) {
		
				/* Loop over receivers (traces) */
				#ifdef _OPENMP
				#pragma omp parallel for private(ix,it,a,b,c,d,e,f) \
					shared(MS,MS1,taper,Refl,F1m)
				#endif 	
				for (ix=0; ix<ntr; ix++) {
					/* Loop over frequencies */
					#pragma ivdep
					for (it=0; it<2*nf; it=it+2) {
						
						/*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/
						/*(a + bi)(e + fi) = (ae - bf) + (af + be)i*/
						a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot];
						b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot];
						c = MS[ishot*2*nf+it];
						d = MS[ishot*2*nf+it+1];
						e = F1m[ishot*2*nf+it];
						f = F1m[ishot*2*nf+it+1];

						MS1[ix*2*nf+it]    += (a*e - mode*b*f) - r*(a*c - b*d);
						MS1[ix*2*nf+it+1]  += (mode*a*f + b*e) - r*(a*d + b*c);
					
					} /* End of loop over frequencies */	
				} /* End of loop over receivers (traces) */
				
			} /* End of loop over shot positions */

			/* Get time domain output of f1m and ms */
		  fft1_2D_inv(MS1,ms,ntr);

			
			#ifdef _OPENMP
			#pragma omp parallel for private(ix,it) \
				shared(ms, window)
			#endif
			for (ix=0; ix<ntr; ix++) {
				#pragma ivdep
				for (it=0; it<nt; it++) {
					ms[it+ix*nt] =-ms_2[it+ix*nt]+ scale*window[it+ix*nt]*(ms[it+ix*nt]);  
				}	
			}
		  fft1_2D_fwd(ms,MS,ntr);

			if(iter%4==0) fprintf(stderr,"Iteration %d\n",iter);

		} /* End of loop over iterations */ 


		/* Build f1p* by adding Tinv to coda M */
		#ifdef _OPENMP
		#pragma omp parallel for private(ix,it) \
			shared(f1pS,F_arrival,ms)
		#endif
		for (ix=0; ix<ntr; ix++) {
			#pragma ivdep
			for (it=0; it<nt; it++) {
				f1pS[it+ix*nt] =  F_arrival[it+ix*nt] + ms[it+ix*nt];
				/* note  this is the time reverse version of f1p */
			}	
		}
		fft1_2D_fwd(f1pS,F1pS,ntr);


	/* to get G by looping over shots */
		memset(Gp,0,2*nf*ntr*sizeof(float));
		memset(Gm,0,2*nf*ntr*sizeof(float));
			for (ishot=0; ishot<nshots; ishot++) {

				/* Loop over receivers (traces) */
				#ifdef _OPENMP
				#pragma omp parallel for private(ix,it,a,b,c,d,e,f) \
					shared(F1pS, F1m, taper, Refl, Gp, Gm)
				#endif
				for (ix=0; ix<ntr; ix++) {
					/* Loop over frequencies */
					#pragma ivdep
					for (it=0; it<2*nf; it=it+2) {

						/*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/
						a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot];
						b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot];
						c = F1pS[ishot*2*nf+it];
						d = F1pS[ishot*2*nf+it+1];
						e = F1m[ishot*2*nf+it];
						f = F1m[ishot*2*nf+it+1];

						Gm[ix*2*nf+it]   += (a*c -mode* b*d) -r*(a*e - b*f);
						Gm[ix*2*nf+it+1] += (mode*a*d + b*c) -r*(a*f + b*e);

						Gp[ix*2*nf+it]   += -(a*e - mode*b*f) + r*(a*c - b*d);
						Gp[ix*2*nf+it+1] += -(mode*a*f + b*e) + r*(a*d + b*c);   
						

					} /* End of loop over frequencies */
				} /* End of loop over receivers (traces) */
			}
		  fft1_2D_inv(Gp,gp1,ntr);
		  fft1_2D_inv(Gp,gm1,ntr);
		if (Pf1) { 
			if (verb) fprintf(stderr,"---> Build f1p\n");
			for (ishot=0; ishot<nshots; ishot++) {

				/* Loop over receivers (traces) */
				#ifdef _OPENMP
				#pragma omp parallel for private(ix,it,c,d) \
					shared(F1pS, F1p)
				#endif
					#pragma ivdep
					for (it=0; it<2*nf; it=it+2) {

						/*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/
						c = F1pS[ishot*2*nf+it];
						d = F1pS[ishot*2*nf+it+1];

						F1p[ishot*2*nf+it]   =  c;
						F1p[ishot*2*nf+it+1] = -d;   
						

					} /* End of loop over frequencies */
			}
		}
		  fft1_2D_inv(F1p,f1p,ntr);
	 


			if (verb) fprintf(stderr,"Build Gp, Gm and G\n");

		#ifdef _OPENMP
		#pragma omp parallel for private(ix,it) \
			shared(f1m, f1pS, gp, gm, gp1, gm1, G)
		#endif
		for (ix=0; ix<ntr; ix++) {
			#pragma ivdep
			for (it=0; it<nt2; it++) {
				gm[it+ix*nt2] =  ((scale*gm1[it+nt2+ix*nt]) - f1m[ it+nt2+ix*nt])*(1.0 - window[it+nt2+ix*nt]); 	
				gp[it+ix*nt2] =  ((scale*gp1[it+nt2+ix*nt]) + f1pS[it+nt2+ix*nt])*(1.0 - window[it+nt2+ix*nt]); 	
				G[ it+ix*nt2] = 0.5*(gp[it+ix*nt2] +    gm[it+ix*nt2]);
			}
		}

		fprintf(stderr,"Build Gp, Gm and G\n");



		/* Write the final result */
	    /*FRefl = sf_input(argv[1]);*/
		/*fft1(Gp,,FRefl,1,0,0);
		fft1(Gm,,FRefl,1,0,0);*/
		
		sf_floatwrite(gp,nt2*ntr,FGp);
		sf_fileclose(FGp);
		sf_floatwrite(gm,nt2*ntr,FGm);
		sf_fileclose(FGm);
		if (PG) {
			sf_floatwrite(G,nt2*ntr,FG);
			sf_fileclose(FG);
		}
		if (Pf1) {
			sf_floatwrite(f1p,nt*ntr,Ff1p);
			sf_fileclose(Ff1p);
			sf_floatwrite(f1m,nt*ntr,Ff1m);
			sf_fileclose(Ff1m);
		}
		
		if (twin) {
			sf_floatwrite(window,nt*ntr,Ftwin);
		sf_fileclose(Ftwin);
	}
	sf_fileclose(FRefl);
	sf_fileclose(FF_arrival);
	
	free(G);
	free(Gm);
	free(Gp);
	free(Refl);
	free(f1pS);
	free(F1pS);
	free(f1p);
	free(F1p);
	free(tw);
	free(filename1);
	free(MS);
	free(MS_0);
	free(MS1);
	free(MS2);
	free(F1m);
	free(F1m1);
	free(f1m_0);
	free(F1m_0);
	free(f1m);
	free(gp);
	free(gp1);
	free(gm);
	free(gm1);
	free(ms);
	free(ms_0);
	free(ms_2);
	free(F_arrival);
	free(window);
	
    exit (0);
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
    bool verb,isreversed;

    sf_file Fs,Fr,Fi;    /* I/O files */
    sf_axis az,ax,at,aa; /* cube axes */
    int     iz,ix,it;
    int     nz,nx,nt;
    off_t iseek;

    float **us=NULL,**ur=NULL,**ii=NULL;

    float scale;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);
    
    /* OMP parameters */
#ifdef _OPENMP
    omp_init();
#endif

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("isreversed",&isreversed)) isreversed=false; /* received wavefield */

    Fs = sf_input ("in" );
    Fr = sf_input ("ur" );
    Fi = sf_output("out");

    /*------------------------------------------------------------*/
    /* read axes */
    az=sf_iaxa(Fs,1); nz = sf_n(az);
    ax=sf_iaxa(Fs,2); nx = sf_n(ax);
    at=sf_iaxa(Fs,3); nt = sf_n(at);

    aa=sf_maxa(1,0,1); 
    sf_setlabel(aa,""); 
    sf_setunit (aa,""); 

    if(verb) {
	sf_raxa(az);
	sf_raxa(ax);
	sf_raxa(at);
    }

    /* write axes */
    sf_oaxa(Fi,az,1);
    sf_oaxa(Fi,ax,2);
    sf_oaxa(Fi,aa,3);
    
    /*------------------------------------------------------------*/
    /* allocate work arrays */
    ii = sf_floatalloc2(nz,nx); 
    us = sf_floatalloc2(nz,nx);
    ur = sf_floatalloc2(nz,nx);
    for    (ix=0; ix<nx; ix++) {
	for(iz=0; iz<nz; iz++) {
	    ii[ix][iz]=0.;
	}
    }

    /*------------------------------------------------------------*/
    if(isreversed) { /* receiver wavefield is reversed */

	if(verb) fprintf(stderr,"nt\n");
	for (it=0; it<nt; it++) {
	    if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",it);
	    
	    sf_floatread(us[0],nz*nx,Fs);
	    sf_floatread(ur[0],nz*nx,Fr);
	    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic)				\
    private(ix,iz)							\
    shared (ii,us,ur,nx,nz)
#endif
	    for    (ix=0; ix<nx; ix++) {
		for(iz=0; iz<nz; iz++) {
		    ii[ix][iz] += us[ix][iz]*ur[ix][iz];
		}
	    }
	    
	} /* it */
	if(verb) fprintf(stderr,"\n");
	
    } else { /* receiver wavefield is NOT reversed */

	if(verb) fprintf(stderr,"nt\n");
	for (it=0; it<nt; it++) {
	    if(verb) fprintf(stderr,"\b\b\b\b\b%d",(nt-it-1));
	    
	    sf_floatread(us[0],nz*nx,Fs);
	    iseek=(off_t)(nt-1-it)*nz*nx*sizeof(float);
	    sf_seek(Fr,iseek,SEEK_SET);
	    sf_floatread(ur[0],nz*nx,Fr);
	    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic)				\
    private(ix,iz)							\
    shared (ii,us,ur,nx,nz)
#endif
	    for    (ix=0; ix<nx; ix++) {
		for(iz=0; iz<nz; iz++) {
		    ii[ix][iz] += us[ix][iz]*ur[ix][iz];
		}
	    }

	} /* it */
	if(verb) fprintf(stderr,"\n");

    } /* end "is reversed" */
    /*------------------------------------------------------------*/
       
    /*------------------------------------------------------------*/
    /* scale image */
    scale = 1./nt;
    for    (ix=0; ix<nx; ix++) {
	for(iz=0; iz<nz; iz++) {
	    ii[ix][iz] *=scale;
	}
    }

    /*------------------------------------------------------------*/
    /* write image */
    sf_floatwrite(ii[0],nx*nz,Fi);
    
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(*ii); free(ii);
    free(*us); free(us);
    free(*ur); free(ur);
    /*------------------------------------------------------------*/
    
    exit (0);
}
Ejemplo n.º 4
0
int main(int argc, char* argv[])
{
    bool verb; /* verbosity flag */
    bool abc;  /* absorbing boundary conditions flag */
    bool free; /* free surface flag*/
    bool snap; /* wavefield snapshots flag */
    int  jsnap;/* save wavefield every *jsnap* time steps */

    /* cube axes */
    sf_axis at,az,ax,as,ar,bt;
    int it,iz,ix,is,ir, iop;
    int nt,nz,nx,ns,nr,nz2,nx2;
    float z0,x0,dt,dx,dz, idx,idz,dt2;

    /* Laplacian */
    int   nop=2;       /* Laplacian operator size */
    float c0, c1, c2;  /* Laplacian operator coefficients */
    float co,c1x,c2x,c1z,c2z;

    int  nbz,nbx; /* boundary size */
    float tz, tx; /* sponge boundary decay coefficients */
    float dp;
    float ws;     /* injected data */

    /* linear interpolation */
    float *fzs,*fxs,    *fzr,*fxr;
    int   *jzs,*jxs,    *jzr,*jxr;

    float *ws00,*ws01,*ws10,*ws11;
    float *wr00,*wr01,*wr10,*wr11;

    /* boundary */
    float  *bzl,*bzh,*bxl,*bxh;

    /* I/O files */
    sf_file Fw,Fs,Fr;
    float  *ww;      /* wavelet */
    pt2d   *ss, *rr; /* source/receiver locations */

    float **tt; /* taper */

    /* background */
    sf_file Bv,Bd,Bu; /* velocity, data, wavefield */
    float **bvv,**bvo;               /* velocity   */
    float  *bdd;                     /* data       */
    float **bum,**buo,**bup,**bud;   /* wavefields */

    /* perturbation */
    sf_file Pv,Pd,Pu;
    float **pvv,**pvo;
    float  *pdd;
    float **pum,**puo,**pup,**pud;

    int ompchunk;  /* OpenMP data chunk size */

/*------------------------------------------------------------*/

    /* init RSF */
    sf_init(argc,argv);

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;

    if(! sf_getbool("verb",&verb)) verb=false;
    if(! sf_getbool( "abc",&abc ))  abc=false;
    if(! sf_getbool("snap",&snap)) snap=false;
    if(! sf_getbool("free",&free)) free=false;

    Fw = sf_input ("in" ); /* wavelet */
    Fs = sf_input ("sou"); /* sources */
    Fr = sf_input ("rec"); /* receivers */

    Bv = sf_input ("vel"); /* velocity */
    Bu = sf_output("wfl"); /* wavefield */
    Bd = sf_output("out"); /* data */

    Pv = sf_input ("ref"); /* velocity */
    Pu = sf_output("liw"); /* linearized wavefield */
    Pd = sf_output("lid"); /* linearized data */

    /* read axes*/
    at=sf_iaxa(Fw,1); sf_setlabel(at,"t"); 
    nt=sf_n(at); dt=sf_d(at); if(verb) sf_raxa(at); /* time */
    as=sf_iaxa(Fs,2); sf_setlabel(as,"s"); 
    ns=sf_n(as); if(verb) sf_raxa(as); /* sources */
    ar=sf_iaxa(Fr,2); sf_setlabel(ar,"r"); 
    nr=sf_n(ar); if(verb) sf_raxa(ar); /* receivers */

    az=sf_iaxa(Bv,1); sf_setlabel(az,"z"); 
    nz=sf_n(az); dz=sf_d(az); if(verb) sf_raxa(az); /* depth */
    ax=sf_iaxa(Bv,2); sf_setlabel(ax,"x"); 
    nx=sf_n(ax); dx=sf_d(ax); if(verb) sf_raxa(ax); /* space */

    /* configure wavefield snapshots */
    if(snap) {
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }

/*------------------------------------------------------------*/

    /* expand domain for absorbing boundary conditions */
    if(abc) {
	if(! sf_getint("nbz",&nbz)) nbz=nop; if(nbz<nop) nbz=nop;
	if(! sf_getint("nbx",&nbx)) nbx=nop; if(nbx<nop) nbx=nop;
	
	if(! sf_getfloat("tz",&tz)) tz=0.025;
	if(! sf_getfloat("tx",&tx)) tx=0.025;
    } else {
	nbz=nop;
	nbx=nop;
    }
    /* expanded domain ( az+2 nz, ax+2 nx ) */
    nz2=nz+2*nbz; z0=sf_o(az)-nbz*dz; 
    nx2=nx+2*nbx; x0=sf_o(ax)-nbx*dx; 

    sf_setn(az,nz2); sf_seto(az,z0); if(verb) sf_raxa(az);
    sf_setn(ax,nx2); sf_seto(ax,x0); if(verb) sf_raxa(ax);
/*------------------------------------------------------------*/

    /* setup output wavefield header */
    if(snap) {
	bt = sf_maxa(nt/jsnap,sf_o(at),dt*jsnap);
	sf_setlabel(bt,"t");

	sf_oaxa(Bu,az,1);
	sf_oaxa(Bu,ax,2);
	sf_oaxa(Bu,bt,3);

	sf_oaxa(Pu,az,1);
	sf_oaxa(Pu,ax,2);
	sf_oaxa(Pu,bt,3);
    }

    /* setup output data header */
    sf_oaxa(Bd,ar,1);
    sf_oaxa(Bd,at,2);

    sf_oaxa(Pd,ar,1);
    sf_oaxa(Pd,at,2);

    dt2 =    dt*dt;
    idz = 1/(dz*dz);
    idx = 1/(dx*dx);

    /* Laplacian coefficients */
    c0=-30./12.; 
    c1=+16./12.;
    c2=- 1./12.;

    co = c0 * (idx+idz);
    c1x= c1 *  idx;
    c2x= c2 *  idx;
    c1z= c1 *      idz;
    c2z= c2 *      idz;

/*------------------------------------------------------------*/
     
    /* allocate arrays */
    ww=sf_floatalloc (nt);      sf_floatread(ww   ,nt     ,Fw);

    bvv=sf_floatalloc2(nz,nx); sf_floatread(bvv[0],nz*nx,Bv);
    pvv=sf_floatalloc2(nz,nx); sf_floatread(pvv[0],nz*nx,Pv);

    /* allocate source/receiver point arrays */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fs,ss,ns,3); /* read 3 elements (x,z,v) */
    pt2dread1(Fr,rr,nr,2); /* read 2 elements (x,z)   */

    bdd=sf_floatalloc(nr);
    pdd=sf_floatalloc(nr);

    jzs=sf_intalloc(ns); fzs=sf_floatalloc(ns); 
    jzr=sf_intalloc(nr); fzr=sf_floatalloc(nr);
    jxs=sf_intalloc(ns); fxs=sf_floatalloc(ns);
    jxr=sf_intalloc(nr); fxr=sf_floatalloc(nr);

    ws00 = sf_floatalloc(ns); wr00 = sf_floatalloc(nr); 
    ws01 = sf_floatalloc(ns); wr01 = sf_floatalloc(nr);
    ws10 = sf_floatalloc(ns); wr10 = sf_floatalloc(nr);
    ws11 = sf_floatalloc(ns); wr11 = sf_floatalloc(nr);
/*------------------------------------------------------------*/

    for (is=0;is<ns;is++) {

	if(ss[is].z >= z0 && 
	   ss[is].z <  z0 + (nz2-1)*dz &&
	   ss[is].x >= x0 && 
	   ss[is].x <  x0 + (nx2-1)*dx) {
	    
	    jzs[is] = (int)( (ss[is].z-z0)/dz);
	    fzs[is] =        (ss[is].z-z0)/dz - jzs[is];	    
	    jxs[is] = (int)( (ss[is].x-x0)/dx);
	    fxs[is] =        (ss[is].x-x0)/dx - jxs[is];
	} else {
	    jzs[is] = 0; jxs[is] = 0;
	    fzs[is] = 1; fxs[is] = 0;
	    ss[is].v= 0;
	}

	ws00[is] = (1-fzs[is])*(1-fxs[is]);
	ws01[is] = (  fzs[is])*(1-fxs[is]);
	ws10[is] = (1-fzs[is])*(  fxs[is]);
	ws11[is] = (  fzs[is])*(  fxs[is]);

    }

    for (ir=0;ir<nr;ir++) {

	if(rr[ir].z >= z0 && 
	   rr[ir].z < z0 + (nz2-1)*dz &&
	   rr[ir].x >= x0 && 
	   rr[ir].x < x0 + (nx2-1)*dx) {
	    
	    jzr[ir] = (int)( (rr[ir].z-z0)/dz);
	    fzr[ir] =        (rr[ir].z-z0)/dz - jzr[ir];
	    jxr[ir] = (int)( (rr[ir].x-x0)/dx);
	    fxr[ir] =        (rr[ir].x-x0)/dx - jxr[ir];

	    rr[ir].v=1;
	} else {
	    jzr[ir] = 0;
	    fzr[ir] = 1;
	    rr[ir].v= 0;
	}

	wr00[ir] = (1-fzr[ir])*(1-fxr[ir]);
	wr01[ir] = (  fzr[ir])*(1-fxr[ir]);
	wr10[ir] = (1-fzr[ir])*(  fxr[ir]);
	wr11[ir] = (  fzr[ir])*(  fxr[ir]);
    }
    
/*------------------------------------------------------------*/
    
    /* allocate temporary arrays */
    bum=sf_floatalloc2(nz2,nx2);
    buo=sf_floatalloc2(nz2,nx2);
    bup=sf_floatalloc2(nz2,nx2);
    bud=sf_floatalloc2(nz2,nx2);

    pum=sf_floatalloc2(nz2,nx2);
    puo=sf_floatalloc2(nz2,nx2);
    pup=sf_floatalloc2(nz2,nx2);
    pud=sf_floatalloc2(nz2,nx2);

    tt=sf_floatalloc2(nz2,nx2);

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nx2,nz2,bum,buo,bup,bud,pum,puo,pup,pud,tt)
#endif
    for (iz=0; iz<nz2; iz++) {
	for (ix=0; ix<nx2; ix++) {
	    bum[ix][iz]=pum[ix][iz]=0;
	    buo[ix][iz]=puo[ix][iz]=0;
	    bup[ix][iz]=pup[ix][iz]=0;
	    bud[ix][iz]=pud[ix][iz]=0;
	    tt[ix][iz]=1;
	}
    }

/*------------------------------------------------------------*/

    /* velocity in the expanded domain (vo=vv^2)*/
    bvo=sf_floatalloc2(nz2,nx2);
    pvo=sf_floatalloc2(nz2,nx2);

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nz,nx,bvv,pvv,bvo,pvo)
#endif
    for (iz=0; iz<nz; iz++) {
	for (ix=0; ix<nx; ix++) {
	    bvo[nbx+ix][nbz+iz] = bvv[ix][iz] * bvv[ix][iz];
	    pvo[nbx+ix][nbz+iz] = pvv[ix][iz];
	}
    }
    /* fill boundaries */
    for (iz=0; iz<nbz; iz++) {
	for (ix=0; ix<nx2; ix++) {
	    bvo[ix][    iz  ] = bvo[ix][    nbz  ];
	    bvo[ix][nz2-iz-1] = bvo[ix][nz2-nbz-1];

	    pvo[ix][    iz  ] = pvo[ix][    nbz  ];
	    pvo[ix][nz2-iz-1] = pvo[ix][nz2-nbz-1];
	}
    }
    for (iz=0; iz<nz2; iz++) {
	for (ix=0; ix<nbx; ix++) {
	    bvo[    ix  ][iz] = bvo[    nbx  ][iz];
	    bvo[nx2-ix-1][iz] = bvo[nx2-nbx-1][iz];

	    pvo[    ix  ][iz] = pvo[    nbx  ][iz];
	    pvo[nx2-ix-1][iz] = pvo[nx2-nbx-1][iz];
	}
    }
 
/*------------------------------------------------------------*/

    /* free surface */
    if(abc && free) {
	for (iz=0; iz<nbz; iz++) {
	    for (ix=0; ix<nx2; ix++) {
		bvo[ix][iz]=0;
		pvo[ix][iz]=0;
	    }
	}
    }

/*------------------------------------------------------------*/

    /* sponge ABC setup */
    if(abc) {
	for (iz=0; iz<nbz; iz++) {
	    for (ix=0; ix<nx2; ix++) {
		tt[ix][    iz  ] = exp( - (tz*(nbz-iz))*(tz*(nbz-iz)) );
		tt[ix][nz2-iz-1] = tt[ix][iz];
	    }
	}
	for (iz=0; iz<nz2; iz++) {
	    for (ix=0; ix<nbx; ix++) {
		tt[    ix  ][iz] = exp( - (tx*(nbx-ix))*(tx*(nbx-ix)) );
		tt[nx2-ix-1][iz] = tt[ix][iz];
	    }
	}
    }

    /* one-way ABC setup */
    bzl=sf_floatalloc(nx2);
    bzh=sf_floatalloc(nx2);
    bxl=sf_floatalloc(nz2);
    bxh=sf_floatalloc(nz2);
    
    for (ix=0;ix<nx2;ix++) {
	dp = bvo[ix][     nop  ] *dt/dz; bzl[ix] = (1-dp)/(1+dp);
	dp = bvo[ix][nz2-nop-1] *dt/dz; bzh[ix] = (1-dp)/(1+dp);
    }
    for (iz=0;iz<nz2;iz++) {
	dp = bvo[    nop  ][iz] *dt/dx; bxl[iz] = (1-dp)/(1+dp);
	dp = bvo[nx2-nop-1][iz] *dt/dx; bxh[iz] = (1-dp)/(1+dp);
    }
/*------------------------------------------------------------*/

    /* 
     *  MAIN LOOP
     */
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);
	
	/* 4th order Laplacian operator */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nop,nx2,nz2,bud,buo,pud,puo,co,c1x,c1z,c2x,c2z,idx,idz)
#endif
	for (ix=nop; ix<nx2-nop; ix++) {
	    for (iz=nop; iz<nz2-nop; iz++) {
		bud[ix][iz] = 
		    co * buo[ix  ][iz  ] + 
		    c1x*(buo[ix-1][iz  ] + buo[ix+1][iz  ]) +
		    c2x*(buo[ix-2][iz  ] + buo[ix+2][iz  ]) +
		    c1z*(buo[ix  ][iz-1] + buo[ix  ][iz+1]) +
		    c2z*(buo[ix  ][iz-2] + buo[ix  ][iz+2]);	  

		pud[ix][iz] = 
		    co * puo[ix  ][iz  ] + 
		    c1x*(puo[ix-1][iz  ] + puo[ix+1][iz  ]) +
		    c2x*(puo[ix-2][iz  ] + puo[ix+2][iz  ]) +
		    c1z*(puo[ix  ][iz-1] + puo[ix  ][iz+1]) +
		    c2z*(puo[ix  ][iz-2] + puo[ix  ][iz+2]);	 
	    }
	}

	/* inject source */
	for (is=0;is<ns;is++) {
	    ws = ww[it] * ss[is].v;
	    bud[ jxs[is]  ][ jzs[is]  ] -= ws * ws00[is];
	    bud[ jxs[is]  ][ jzs[is]+1] -= ws * ws01[is];
	    bud[ jxs[is]+1][ jzs[is]  ] -= ws * ws10[is];
	    bud[ jxs[is]+1][ jzs[is]+1] -= ws * ws11[is];
	}

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,pud,bud,pvo)
#endif
	for (ix=0; ix<nx2; ix++) {
	    for (iz=0; iz<nz2; iz++) {
		pud[ix][iz] -= bud[ix][iz] * 2*pvo[ix][iz];
	    }
	}

	/* velocity scale */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,bud,pud,bvo)
#endif
	for (ix=0; ix<nx2; ix++) {
	    for (iz=0; iz<nz2; iz++) {
		bud[ix][iz] *= bvo[ix][iz];
		pud[ix][iz] *= bvo[ix][iz];
	    }
	}
	
	/* time step */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,bud,buo,bum,bup,pud,puo,pum,pup,dt2)
#endif
	for (ix=0; ix<nx2; ix++) {
	    for (iz=0; iz<nz2; iz++) {
		bup[ix][iz] = 2*buo[ix][iz] - bum[ix][iz] + bud[ix][iz] * dt2; 
		bum[ix][iz] =   buo[ix][iz];
		buo[ix][iz] =   bup[ix][iz];

		pup[ix][iz] = 2*puo[ix][iz] - pum[ix][iz] + pud[ix][iz] * dt2; 
		pum[ix][iz] =   puo[ix][iz];
		puo[ix][iz] =   pup[ix][iz];
	    }
	}
	
	/* one-way ABC apply */
	if(abc) {
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz,iop) shared(nx2,nz2,nop,buo,bum,puo,pum,bzl,bzh)
#endif
	    for(ix=0;ix<nx2;ix++) {
		for(iop=0;iop<nop;iop++) {
		    iz = nop-iop;
		    buo      [ix][iz  ] 
			= bum[ix][iz+1] 
			+(bum[ix][iz  ]
			- buo[ix][iz+1]) * bzl[ix];
		    puo      [ix][iz  ] 
			= pum[ix][iz+1] 
			+(pum[ix][iz  ]
			- puo[ix][iz+1]) * bzl[ix];
		    
		    iz = nz2-nop+iop-1;
		    buo      [ix][iz  ] 
			= bum[ix][iz-1]
			+(bum[ix][iz  ]
			- buo[ix][iz-1]) * bzh[ix];
		    puo      [ix][iz  ] 
			= pum[ix][iz-1]
			+(pum[ix][iz  ]
			- puo[ix][iz-1]) * bzh[ix];
		}
	    }

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,1) private(ix,iz,iop) shared(nx2,nz2,nop,buo,bum,puo,pum,bzl,bzh)
#endif
	    for(iop=0;iop<nop;iop++) {
		for(iz=0;iz<nz2;iz++) {
		    ix = nop-iop;
		    buo      [ix  ][iz] 
			= bum[ix+1][iz] 
			+(bum[ix  ][iz]
			- buo[ix+1][iz]) * bxl[iz];
		    puo      [ix  ][iz] 
			= pum[ix+1][iz] 
			+(pum[ix  ][iz]
			- puo[ix+1][iz]) * bxl[iz];
		    
		    ix = nx2-nop+iop-1;
		    buo      [ix  ][iz] 
			= bum[ix-1][iz]
			+(bum[ix  ][iz]
			- buo[ix-1][iz]) * bxh[iz];
		    puo      [ix  ][iz] 
			= pum[ix-1][iz]
			+(pum[ix  ][iz]
			- puo[ix-1][iz]) * bxh[iz];
		}
	    }
	}
	
	/* sponge ABC apply */
	if(abc) {
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,buo,bum,bud,puo,pum,pud,tt)
#endif
	    for (ix=0; ix<nx2; ix++) {
		for (iz=0; iz<nz2; iz++) {
		    buo[ix][iz] *= tt[ix][iz];
		    bum[ix][iz] *= tt[ix][iz];
		    bud[ix][iz] *= tt[ix][iz];

		    puo[ix][iz] *= tt[ix][iz];
		    pum[ix][iz] *= tt[ix][iz];
		    bud[ix][iz] *= tt[ix][iz];
		}
	    }
	}
	
	/* write wavefield */
	if(snap && it%jsnap==0) {
	    sf_floatwrite(buo[0],nz2*nx2,Bu);
	    sf_floatwrite(puo[0],nz2*nx2,Pu);
	}

	/* write data */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,1) private(ir) shared(bdd,pdd,rr,buo,puo,jzr,wr00,wr01,wr10,wr11)
#endif
	for (ir=0;ir<nr;ir++) {
	    bdd[ir] =
		buo[ jxr[ir]  ][ jzr[ir]  ] * wr00[ir] +
		buo[ jxr[ir]  ][ jzr[ir]+1] * wr01[ir] +
		buo[ jxr[ir]+1][ jzr[ir]  ] * wr10[ir] +
		buo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir];
	    bdd[ir] *= rr[ir].v;

	    pdd[ir] =
		puo[ jxr[ir]  ][ jzr[ir]  ] * wr00[ir] +
		puo[ jxr[ir]  ][ jzr[ir]+1] * wr01[ir] +
		puo[ jxr[ir]+1][ jzr[ir]  ] * wr10[ir] +
		puo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir];
	    pdd[ir] *= rr[ir].v;
	}
	/* write data */
	sf_floatwrite(bdd,nr,Bd);
	sf_floatwrite(pdd,nr,Pd);
    }
    if(verb) fprintf(stderr,"\n");    

    exit (0);
}
Ejemplo n.º 5
0
int main(int argc, char* argv[])
{
    
    
    bool verb;
    int  nx,nz,nc,ic,iz,ix;
    int  ind=0;
    float tmp;
    float dx,dz,ox,oz;
    
    pt2d  *cc=NULL;
    float **eps=NULL,**del,***out=NULL;
    
    float x,y,lambda0,lambda1,lambda2,twoA;
    float lm00,lm01,lm02,lm10,lm11,lm12,lm20,lm21,lm22;
    
    float *a=NULL,*b=NULL,**llm=NULL;
    sf_axis ax,az,ac;
    sf_file Fin=NULL,Fdel=NULL, Fc=NULL,Fout=NULL; 
    
     /* init RSF */
    sf_init(argc,argv);
    

    if(! sf_getbool("verb",&verb)) verb=false;

    Fin = sf_input ("in" ); 
    Fdel= sf_input ("del" ); 
    Fout = sf_output ("out" );  
    Fc = sf_input ("cc" ); /* sample locations  */

    /* input axes */
    az = sf_iaxa(Fin,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az);
    ax = sf_iaxa(Fin,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax);

    nz = sf_n(az); dz = sf_d(az); oz = sf_o(az);
    nx = sf_n(ax); dx = sf_d(ax); ox = sf_o(ax);

    eps = sf_floatalloc2(nz,nx);
    del = sf_floatalloc2(nz,nx);
    sf_floatread(eps[0],nz*nx,Fin);
    sf_floatread(del[0],nz*nx,Fdel);
     /* CIP coordinates */
    ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,"");
    if(verb) sf_raxa(ac);
    nc = sf_n(ac);
    cc= (pt2d*) sf_alloc(nc,sizeof(*cc));
    pt2dread1(Fc,cc,nc,2); /* read coordinates */    
    /*  nc=3*/


     /* output files*/
    out = sf_floatalloc3(nz,nx,nc); 
    sf_oaxa(Fout,az,1);
    sf_oaxa(Fout,ax,2);
    sf_oaxa(Fout,ac,3);
    

    nc=3;
    a = sf_floatalloc(nc);
    b = sf_floatalloc(nc);
    llm = sf_floatalloc2(2,nc);

    /*find sample indexes*/
    for(ic=0; ic<nc; ic++) { 
	b[ic]=cc[ic].z; /*delta*/
	a[ic]=cc[ic].x; /*epsilon*/
	sf_warning("x%d=%f,z%d=%f: epsilon=%f delta=%f",ic,cc[ic].x,ic,cc[ic].z,a[ic],b[ic]);
    }
    
/*    a[0]=0.0; b[0]=0.0;*/
/*    a[2]=0.65; b[2]=0.0; */
/*    a[1]=0.0; b[1]=0.35;*/

    /*matrix*/
 
    twoA=(a[0]-a[2])*(b[1]-b[2])-(a[1]-a[2])*(b[0]-b[2]);
   /*  if(twoA<0) { */
/* 	tmp=a[1];a[1]=a[2];a[2]=tmp; */
/* 	tmp=b[1];b[1]=b[2];b[2]=tmp; */
/*     } */
   
    sf_warning("a=%f %f %f",a[0],a[1],a[2]);
    sf_warning("b=%f %f %f",b[0],b[1],b[2]);

    twoA=(a[0]-a[2])*(b[1]-b[2])-(a[1]-a[2])*(b[0]-b[2]);
    llm[0][0]=a[1]*b[2]-a[2]*b[1]; llm[0][1]=b[1]-b[2]; llm[0][2]=a[2]-a[1];
    llm[1][0]=a[2]*b[0]-a[0]*b[2]; llm[1][1]=b[2]-b[0]; llm[1][2]=a[0]-a[2];
    llm[2][0]=a[0]*b[1]-a[1]*b[0]; llm[2][1]=b[0]-b[1]; llm[2][2]=a[1]-a[0];
    sf_warning("");
    sf_warning("%f %f %f",llm[0][0], llm[0][1], llm[0][2]);
    sf_warning("%f %f %f",llm[1][0], llm[1][1], llm[1][2]);
    sf_warning("%f %f %f",llm[2][0], llm[2][1], llm[2][2]);
    sf_warning("area=%f",twoA);

    lm00=a[1]*b[2]-a[2]*b[1]; lm01=b[1]-b[2]; lm02=a[2]-a[1];
    lm10=a[2]*b[0]-a[0]*b[2]; lm11=b[2]-b[0]; lm12=a[0]-a[2];
    lm20=a[0]*b[1]-a[1]*b[0]; lm21=b[0]-b[1]; lm22=a[1]-a[0];
    sf_warning("");
    sf_warning("%f %f %f",lm00, lm01, lm02);
    sf_warning("%f %f %f",lm10, lm11, lm12);
    sf_warning("%f %f %f",lm20, lm21, lm22);








    /* find shape functions  */
    for    (ic=0; ic<nc; ic++) {
     for    (ix=0; ix<nx; ix++) {
	for(iz=0; iz<nz; iz++) {
	    out[ic][ix][iz]=0.;
	}
     }
    }

    if(twoA!=0){    
	for    (ix=0; ix<nx; ix++) {
	    for(iz=0; iz<nz; iz++) {
		x=eps[ix][iz];
		y=del[ix][iz];
		lambda0=lm00 + lm01*x + lm02*y;
		lambda1=lm10 + lm11*x + lm12*y;
		lambda2=lm20 + lm21*x + lm22*y;
		out[0][ix][iz]=lambda0/twoA;
		out[1][ix][iz]=lambda1/twoA;
		out[2][ix][iz]=lambda2/twoA;
	    }
	}
    }
    sf_floatwrite(out[0][0],nz*nx*nc,Fout);

}
Ejemplo n.º 6
0
int main(int argc, char* argv[])
{
    bool verb;
    bool  adj;
    bool anis;
    sf_file Fcip=NULL;	/*   lag-domain CIPs */
    sf_file Fang=NULL;	/* angle-domain CIPs */
    sf_file Fvel=NULL;  /*   velocity @ CIPs */
    sf_file Fnor=NULL;	/*    normals @ CIPs */
    sf_file Ftlt=NULL;	/*       tilt @ CIPs */
    sf_file Fani=NULL;  /* anisotropy @ CIPs */

    sf_axis ahx,ahy,ahz,aht,ac,ath,aph,aps,aj;
    int     ihx,ihy,ihz,iht,ic,ith,iph;

    
    /* angle parameters */
    int   nth,nph,nps,nhx,nhy,nhz,nht;
    float oth,oph,ops,ohx,ohy,ohz,oht;
    float dth,dph,dps,dhx,dhy,dhz,dht;
    float phi;
    float tht;
    float psi;
    float v_s,v_r;
    float cosum,codif,sitovel;

    /*  arrays                  1   2   3   4  */
    float     ****cip;      /* nhx-nhy-nhz-nht */
    float       **ang;      /* nph-nth         */
    float        *vep;      /* nc              */
    float        *ves;      /* nc              */
    float        *eps=NULL; /* nc              */
    float        *dlt=NULL; /* nc              */

    vc3d           vv;      /* azimuth reference vector */
    vc3d          *nn;      /* normal vectors  */
    vc3d          *tt=NULL; /*   tilt vectors  */
    vc3d          *aa;      /* in-plane reference vector */
    vc3d           qq;
    vc3d           jk;      /* temp vector */
    float    hx,hy,hz;

    float tau; /* time lag */
    int   jht; /* tau axis index */
    float fht; /* tau axis weight */

    float ssn; /* slant-stack normalization */

    float *ttipar;
    /*-----------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);    
    
#ifdef _OPENMP
    omp_init(); /* OMP parameters */
#endif

    if(! sf_getbool("verb",&verb)) verb=false;	/* verbosity flag */
    if(! sf_getbool("anis",&anis)) anis=false;	/* anisotropy flag */
    if(! sf_getbool("adj", &adj))   adj=true;	/* adj flag */    
    /* 
     * ADJ: cip to ang
     * FOR: ang to cip
     */

    sf_warning("verb=%d",verb);
    sf_warning("anis=%d",anis);

    /* select anisotropy model */
    if(anis) sf_warning("ANI model");
    else     sf_warning("ISO model");

    if(adj) {
	Fcip=sf_input ( "in"); /* CIP file */
	Fang=sf_output("out"); /* ANG file */
    } else {
	Fcip=sf_output("out"); /* CIP file */
	Fang=sf_input ("in");  /* ANG file */
    }
    Fvel=sf_input ("vel");     /* velocity file  */
    Fnor=sf_input ("nor");     /* normal vectors */
    if(anis) {
	Ftlt=sf_input ("tlt"); /*   tilt vectors */
	Fani=sf_input ("ani"); /*     anisotropy */
    }

    aj  = sf_maxa(1,0,1);

    if(adj) {
	/* input axes */
	ahx = sf_iaxa(Fcip,1); sf_setlabel(ahx,"hx");
	ahy = sf_iaxa(Fcip,2); sf_setlabel(ahy,"hy");
	ahz = sf_iaxa(Fcip,3); sf_setlabel(ahz,"hz");
	aht = sf_iaxa(Fcip,4); sf_setlabel(aht,"ht");

	/* CIP axis */
	ac  = sf_iaxa(Fcip,5); sf_setlabel(ac ,"c ");
		
	/* reflection angle */
	if(! sf_getint  ("nth",&nth)) nth=90;
	if(! sf_getfloat("oth",&oth)) oth=0;
	if(! sf_getfloat("dth",&dth)) dth=1.;
	ath = sf_maxa(nth,oth,dth);
	sf_setlabel(ath,"th");
	sf_setunit (ath,"deg");
	
	/* azimuth angle */
	if(! sf_getint  ("nph",&nph)) nph=360;
	if(! sf_getfloat("oph",&oph)) oph=-180;
	if(! sf_getfloat("dph",&dph)) dph=1.;
	aph = sf_maxa(nph,oph,dph);
	sf_setlabel(aph,"ph");
	sf_setunit (aph,"deg");

	/* output axes */
	sf_oaxa(Fang,ath,1);
	sf_oaxa(Fang,aph,2);
	sf_oaxa(Fang,ac ,3);
	sf_oaxa(Fang,aj ,4);
	sf_oaxa(Fang,aj ,5);

    } else {

	/* lag in x */
	if(! sf_getint  ("nhx",&nhx)) nhx=1;
	if(! sf_getfloat("ohx",&ohx)) ohx=0;
	if(! sf_getfloat("dhx",&dhx)) dhx=1.;
	ahx = sf_maxa(nhx,ohx,dhx);
	sf_setlabel(ahx,"hx");
	sf_setunit (ahx,"");

	/* lag in y */
	if(! sf_getint  ("nhy",&nhy)) nhy=1;
	if(! sf_getfloat("ohy",&ohy)) ohy=0;
	if(! sf_getfloat("dhy",&dhy)) dhy=1.;
	ahy = sf_maxa(nhy,ohy,dhy);
	sf_setlabel(ahy,"hy");
	sf_setunit (ahy,"");

	/* lag in z */
	nhz=1;
	ohz=0.;
	dhz=1.;
	ahz = sf_maxa(nhz,ohz,dhz);
	sf_setlabel(ahz,"hz");
	sf_setunit (ahz,"");

	/* lag in t */
	if(! sf_getint  ("nht",&nht)) nht=1;
	if(! sf_getfloat("oht",&oht)) oht=0.;
	if(! sf_getfloat("dht",&dht)) dht=1.;
	aht = sf_maxa(nht,oht,dht);
	sf_setlabel(aht,"ht");
	sf_setunit (aht,"");

	/* reflection angle */
	ath = sf_iaxa(Fang,1); sf_setlabel(ath,"th");
	/* azimuth angle */
	aph = sf_iaxa(Fang,2); sf_setlabel(aph,"ph");
	/* CIP axis */
	ac  = sf_iaxa(Fang,3); sf_setlabel(ac ,"c ");

	/* output axes */
	sf_oaxa(Fcip,ahx,1);
	sf_oaxa(Fcip,ahy,2);
	sf_oaxa(Fcip,ahz,3);
	sf_oaxa(Fcip,aht,4);
	sf_oaxa(Fcip,ac ,5);
    }

    if (verb){
	sf_raxa(ahx);
	sf_raxa(ahy);
	sf_raxa(ahz);
	sf_raxa(aht);
	sf_raxa(ac);
	sf_raxa(ath);
	sf_raxa(aph);
    }

    if(anis) {
	/* deviation angle */
	if(! sf_getint  ("nps",&nps)) nps=251;
	if(! sf_getfloat("ops",&ops)) ops=-25;
	if(! sf_getfloat("dps",&dps)) dps=0.2;
	aps = sf_maxa(nps,ops,dps);
	sf_setlabel(aps,"ps");
	sf_setunit (aps,"deg");

	if(verb) sf_raxa(aps);
    } else {
	aps = NULL;
    }

    /*------------------------------------------------------------*/
    /* allocate arrays */
    cip = sf_floatalloc4  (sf_n(ahx),sf_n(ahy),sf_n(ahz),sf_n(aht));
    ang = sf_floatalloc2  (sf_n(ath),sf_n(aph));

    /* read velocity */
    vep = sf_floatalloc  (sf_n(ac));    
    sf_floatread(vep,sf_n(ac),Fvel);

    ves = sf_floatalloc  (sf_n(ac));    
    sf_floatread(ves,sf_n(ac),Fvel);
	
    /*------------------------------------------------------------*/
    /* read normals */
    nn  = (vc3d*) sf_alloc(sf_n(ac),sizeof(*nn)); /* normals  */
    vc3dread1(Fnor,nn,sf_n(ac));

    if(anis) {
	/* read anisotropy */
	eps = sf_floatalloc   (sf_n(ac));
	sf_floatread(eps,sf_n(ac),Fani);

	dlt = sf_floatalloc   (sf_n(ac));
	sf_floatread(dlt,sf_n(ac),Fani);

	/* read tilts */	
	tt  = (vc3d*) sf_alloc(sf_n(ac),sizeof(*tt));
	vc3dread1(Ftlt,tt,sf_n(ac));
    }

    /*------------------------------------------------------------*/
    /* in-plane azimuth reference */
    vv.dx=1;
    vv.dy=0;
    vv.dz=0;

    aa  = (vc3d*) sf_alloc(sf_n(ac),sizeof(*aa));
    for(ic=0;ic<sf_n(ac);ic++) {
	jk    =vcp3d(&nn[ic],&vv);
	aa[ic]=vcp3d(&jk,&nn[ic]);
    }

    /*------------------------------------------------------------*/
    ssn = 1./sqrt(sf_n(ahx)*sf_n(ahy)*sf_n(ahz));

    /*------------------------------------------------------------*/
    /* loop over CIPs */
/*    if(verb) fprintf(stderr,"ic\n");*/
    for(ic=0;ic<sf_n(ac);ic++) {
/*	if(verb) fprintf(stderr,"\b\b\b\b\b%d",ic);*/

	if(adj) {

	    /* read CIP */
	    sf_floatread(cip[0][0][0],sf_n(ahx)*sf_n(ahy)*sf_n(ahz)*sf_n(aht),Fcip);
	    
	    /* init ANG */
	    for    (iph=0;iph<sf_n(aph);iph++) {
		for(ith=0;ith<sf_n(ath);ith++) {
		    ang[iph][ith]=0;
		}
	    }
	} else {
	    
	    /* init CIP */
	    for            (iht=0;iht<sf_n(aht);iht++) {
		for        (ihz=0;ihz<sf_n(ahz);ihz++) {
		    for    (ihy=0;ihy<sf_n(ahy);ihy++) {
			for(ihx=0;ihx<sf_n(ahx);ihx++) {
			    cip[iht][ihz][ihy][ihx]=0;
			}
		    }
		}
	    }

	    /* read ANG */
	    sf_floatread(ang[0],sf_n(ath)*sf_n(aph),Fang);
	}

	/* phi loop */

	nph = sf_n(aph);

#ifdef _OPENMP
#pragma omp parallel for schedule(static)				\
    private(iph,phi,jk,qq,						\
	    ith,tht,							\
	    ihy,ihx,hy,hx,hz,						\
	    tau,jht,fht,cosum,codif,v_s,v_r,psi,sitovel)	\
    shared( nph,aph,ath,aps,ahy,ahx,aht,cip,ang,vep,ves,eps,dlt)
#endif
	for(iph=0;iph<nph;iph++) {
	    phi=(180+sf_o(aph)+iph*sf_d(aph))/180.*SF_PI;
	    /* use '180' to reverse illumination direction: */
	    /* at a CIP, look toward the source */

	    /* reflection azimuth vector */
	    jk = rot3d(nn,aa,phi);
	    qq = nor3d(&jk);
	    
	    /* theta loop */
	    for(ith=0;ith<sf_n(ath);ith++) {
		tht=(sf_o(ath)+ith*sf_d(ath))/180.*SF_PI;
		
		if(anis) {

		    ttipar = psitti(nn,&qq,tt,aa,
				    tht,phi,aps,
				    vep[ic],ves[ic],eps[ic],dlt[ic]);
		    psi = ttipar[0];
		    v_s = ttipar[1];
		    v_r = ttipar[2];

		    psi *= SF_PI/180.;
                    cosum = cosf(tht+psi);
                    codif = cosf(tht-psi);

                    sitovel = sinf(2*tht)/(v_s*cosum + v_r*codif);
		} else {
		    sitovel = sinf(tht)/vep[ic];
		}

		/* lag loops */
		if(adj) {
		    for    (ihy=0;ihy<sf_n(ahy);ihy++) { hy=sf_o(ahy)+ihy*sf_d(ahy);
			for(ihx=0;ihx<sf_n(ahx);ihx++) { hx=sf_o(ahx)+ihx*sf_d(ahx);
 
			    hz = -(hx*(nn[ic].dx)+hy*(nn[ic].dy))/(nn[ic].dz);
			    tau = -((qq.dx)*hx+(qq.dy)*hy+(qq.dz)*hz)*sitovel;			    
			    jht=0.5+(tau-sf_o(aht))/sf_d(aht);

			    if(jht>=0 && jht<sf_n(aht)-1) {
				fht= (tau-sf_o(aht))/sf_d(aht)-jht;
				ang[iph][ith] += (1-fht)*ssn*cip[jht  ][0][ihy][ihx]
				    +               fht *ssn*cip[jht+1][0][ihy][ihx]; 
			    }
			    
			} /* hx */
		    } /* hy */
		} else {
		    for    (ihy=0;ihy<sf_n(ahy);ihy++) { hy=sf_o(ahy)+ihy*sf_d(ahy);
			for(ihx=0;ihx<sf_n(ahx);ihx++) { hx=sf_o(ahx)+ihx*sf_d(ahx);
		    
			    hz = -(hx*(nn[ic].dx)+hy*(nn[ic].dx))/(nn[ic].dz);
			    tau = -((qq.dx)*hx+(qq.dy)*hy+(qq.dz)*hz)*sitovel; 		    
			    jht=0.5+(tau-sf_o(aht))/sf_d(aht);

			    if(jht>=0 && jht<sf_n(aht)-1) {
				fht= (tau-sf_o(aht))/sf_d(aht)-jht;
				cip[jht  ][0][ihy][ihx] += (1-fht)*ssn*ang[iph][ith];
				cip[jht+1][0][ihy][ihx] +=    fht *ssn*ang[iph][ith];
			    }
			    
			} /* hx */
		    } /* hy */
		}

	    } /* th */
	} /* ph */
	
	if(adj) {
	    /* write ANG */
	    sf_floatwrite(ang[0],sf_n(ath)*sf_n(aph),Fang);
	} else {
	    /* write CIP */
	    sf_floatwrite(cip[0][0][0],sf_n(ahx)*sf_n(ahy)*sf_n(ahz)*sf_n(aht),Fcip);
	}

    }
    if(verb) fprintf(stderr,"\n");
    /*------------------------------------------------------------*/
 
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"free memory...");
    free(***cip);free(**cip);free(*cip);free(cip);
    ;                        free(*ang);free(ang);
    ;                                   free(vep);
    ;                                   free (nn);
    ;                                   free (aa);
    if(anis) {
	free(ves);
	free(eps);
	free(dlt);
	free(tt);
    }
    if(verb) fprintf(stderr,"OK\n");
    /*------------------------------------------------------------*/

    exit(0);
}		
Ejemplo n.º 7
0
int main (int argc, char *argv[])
{
    int  adj;             /* forward or adjoint */
    int  eic;             /* EIC or CIC */
    bool verb;            /* verbosity */
    float eps;            /* dip filter constant */
    int   nrmax;          /* number of reference velocities */
    float dtmax;          /* time error */
    int   pmx,pmy;        /* padding in the k domain */
    int   tmx,tmy;        /* boundary taper size */

    int nhx, nhy, nhz, nht, nc;
    int nhx2,nhy2,nhz2,nht2;
    float dht, oht;

    sf_axis amx,amy,az;
    sf_axis alx,aly;
    sf_axis aw,ae,ac,aa;
    sf_axis ahx,ahy,ahz,aht;         

    /* I/O files */
    sf_file Bws=NULL;   /*  background wavefield file Bws */
    sf_file Bwr=NULL;   /*  background wavefield file Bwr */
    sf_file Bs=NULL;    /*  background slowness file Bs   */
    sf_file Ps=NULL;    /*  slowness perturbation file Ps */
    sf_file Pi=NULL;    /*  image perturbation file Pi    */
    sf_file Fc=NULL;    /*  CIP coordinates               */
    sf_file Pws=NULL;   /*  perturbed wavefield file Pws */
    sf_file Pwr=NULL;   /*  perturbed wavefield file Pwr */
    sf_file Pti=NULL;  

    int ompnth=1;

    wexcub3d cub; /* wavefield hypercube */
    wexcip3d cip; /* CIP gathers */
    wextap3d tap; /* tapering */
    wexssr3d ssr; /* SSR operator */
    wexlsr3d lsr; /* LSR operator */
    wexslo3d slo; /* slowness */

    wexmvaop3d mva;
    float dsmax;

    /*------------------------------------------------------------*/
    sf_init(argc,argv);

    /* OMP parameters */
#ifdef _OPENMP
    ompnth=omp_init();
#endif

    if (!sf_getbool(  "verb",&verb ))  verb =  true; /* verbosity flag */
    if (!sf_getint(   "adj",&adj   ))  sf_error("Specify adjoint!"); /* y=ADJ Back-projection; n=FWD Forward Scattering */
    if (!sf_getint(  "feic",&eic   ))  sf_error("Specify EIC!");     /* extended I.C. flag */
    if (!sf_getfloat(  "eps",&eps  ))   eps =  0.01; /* stability parameter */
    if (!sf_getint(  "nrmax",&nrmax)) nrmax =     1; /* max number of refs */
    if (!sf_getfloat("dtmax",&dtmax)) dtmax = 0.004; /* max time error */
    if (!sf_getint(    "pmx",&pmx  ))   pmx =     0; /* padding on x */
    if (!sf_getint(    "pmy",&pmy  ))   pmy =     0; /* padding on y */
    if (!sf_getint(    "tmx",&tmx  ))   tmx =     0; /* taper on x   */
    if (!sf_getint(    "tmy",&tmy  ))   tmy =     0; /* taper on y   */

    ae  = sf_maxa(1,0,1);
    nhx=nhy=nhz=nht=nc=nhx2=nhy2=nhz2=nht2=0;
    oht = dht = 0.0;
    /*------------------------------------------------------------*/
    /* slowness */

    Bs = sf_input("slo");
    alx = sf_iaxa(Bs,1); sf_setlabel(alx,"lx");
    aly = sf_iaxa(Bs,2); sf_setlabel(aly,"ly");
    az =  sf_iaxa(Bs,3); sf_setlabel(az, "z");

    /*------------------------------------------------------------*/
    /* input file */
    if(adj)
        Pi = sf_input("in");
    else
        Ps = sf_input("in");

    /*------------------------------------------------------------*/
    /* wavefield */
    Bws = sf_input("swfl");
    Bwr = sf_input("rwfl");

    amx = sf_iaxa(Bws,1); sf_setlabel(amx,"mx");
    amy = sf_iaxa(Bws,2); sf_setlabel(amy,"my");
    aw  = sf_iaxa(Bws,4); sf_setlabel(aw ,"w" );

    Pws = sf_tmpfile(NULL); sf_settype(Pws,SF_COMPLEX);
    Pwr = sf_tmpfile(NULL); sf_settype(Pwr,SF_COMPLEX);

    /*------------------------------------------------------------*/
    cub = wex_cube(verb,
                   amx,amy,az,
                   alx,aly,
                   aw,
                   ae,
                   eps,
                   ompnth);
    dsmax = dtmax/cub->az.d;

    /*------------------------------------------------------------*/
    /* init structures */
    tap = wextap_init(cub->amx.n,
                      cub->amy.n,
                      1,
                      SF_MIN(tmx,cub->amx.n-1), /* tmx */
                      SF_MIN(tmy,cub->amy.n-1), /* tmy */
                      0,
                      true,true,false);
    slo = wexslo_init(cub,Bs,nrmax,dsmax);
    ssr = wexssr_init(cub,slo,pmx,pmy,tmx,tmy,dsmax);
    lsr = wexlsr_init(cub,pmx,pmy,dsmax);

    /*------------------------------------------------------------*/
    Pti = sf_tmpfile(NULL); sf_settype(Pti,SF_COMPLEX);

    /*------------------------------------------------------------*/
    /* WEMVA */
    if(adj) {
        sf_warning("adjoint operator...");

        if(eic){
            ahx = sf_iaxa(Pi,1); sf_setlabel(ahx,"hx");
            ahy = sf_iaxa(Pi,2); sf_setlabel(ahy,"hy");
            ahz = sf_iaxa(Pi,3); sf_setlabel(ahz,"hz");
            aht = sf_iaxa(Pi,4); sf_setlabel(aht,"ht");

            dht = sf_d(aht);  oht = sf_o(aht);

            nhx2 = sf_n(ahx); nhx = (nhx2-1)/2;
            nhy2 = sf_n(ahy); nhy = (nhy2-1)/2;
            nhz2 = sf_n(ahz); nhz = (nhz2-1)/2;
            nht2 = sf_n(aht); nht = (nht2-1)/2;

            /* CIP coordinates */
            Fc = sf_input ("cc" );
            ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,"");
            nc = sf_n(ac);
        }

        cip = wexcip_init(cub,nhx,nhy,nhz,nht,nhx2,nhy2,nhz2,nht2,nc,dht,oht,Fc,eic);
        mva = wexmva_init(cub,cip);

        Ps = sf_output("out"); sf_settype(Ps,SF_COMPLEX);
        sf_oaxa(Ps,amx,1);
        sf_oaxa(Ps,amy,2);
        sf_oaxa(Ps,az, 3);
        if(eic){
        sf_oaxa(Ps,ae, 4);
        sf_oaxa(Ps,ae, 5);}

        /* Adjoint I.C. operator, dI -> dW */
        wexcip_adj(cub,cip,Bwr,Pws,Pi,eic,1,1); /* Ws dR */
        wexcip_adj(cub,cip,Bws,Pwr,Pi,eic,0,0); /* Wr dR */

        sf_filefresh(Pws);
        sf_filefresh(Pwr);

        /* Adjoint WEMVA operator, dW -> dS */
        wexmva(mva,adj,cub,ssr,lsr,tap,slo,Bws,Bwr,Pws,Pwr,Ps);

    } else {
        /* set up the I/O of output CIP gathers */
        Pi = sf_output("out"); sf_settype(Pi,SF_COMPLEX);

        if(eic){
            /* CIP coordinates */
            Fc = sf_input ("cc" );
            ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,"");
            nc = sf_n(ac);

            if(! sf_getint("nhx",&nhx)) nhx=0; /* number of lags on the x axis */
            if(! sf_getint("nhy",&nhy)) nhy=0; /* number of lags on the y axis */
            if(! sf_getint("nhz",&nhz)) nhz=0; /* number of lags on the z axis */
            if(! sf_getint("nht",&nht)) nht=0; /* number of lags on the t axis */
            if(! sf_getfloat("dht",&dht)) sf_error("need dht");
            oht = -nht*dht;

            nhx2=2*nhx+1; nhy2=2*nhy+1;
            nhz2=2*nhz+1; nht2=2*nht+1;

            aa=sf_maxa(nhx2,-nhx*cub->amx.d,cub->amx.d);
            sf_setlabel(aa,"hx"); sf_setunit(aa,"");
            if(verb) sf_raxa(aa);
            sf_oaxa(Pi,aa,1);

            aa=sf_maxa(nhy2,-nhy*cub->amy.d,cub->amy.d);
            sf_setlabel(aa,"hy"); sf_setunit(aa,"");
            if(verb) sf_raxa(aa);
            sf_oaxa(Pi,aa,2);

            aa=sf_maxa(nhz2,-nhz*cub->az.d,cub->az.d);
            sf_setlabel(aa,"hz"); sf_setunit(aa,"");
            if(verb) sf_raxa(aa);
            sf_oaxa(Pi,aa,3);

            aa=sf_maxa(nht2,-nht*dht,dht);
            sf_setlabel(aa,"ht"); sf_setunit(aa,"s");
            if(verb) sf_raxa(aa);
            sf_oaxa(Pi,aa,4);
 
            sf_oaxa(Pi,ac,5);

        }
        else{
            sf_oaxa(Pi,amx,1);
            sf_oaxa(Pi,amy,2);
            sf_oaxa(Pi,az, 3);
        }

        cip = wexcip_init(cub,nhx,nhy,nhz,nht,nhx2,nhy2,nhz2,nht2,nc,dht,oht,Fc,eic);
        mva = wexmva_init(cub,cip);
 
        /* WEMVA operator, dS -> dW */
        wexmva(mva,adj,cub,ssr,lsr,tap,slo,Bws,Bwr,Pws,Pwr,Ps);
         
        sf_filefresh(Pws);
        sf_filefresh(Pwr);

        /* I.C. operator, dW -> dI */
        wexcip_for(cub,cip,Bws,Pwr,Pti,eic,0,0); /* CONJ( Ws) dWr */
        sf_seek(Pti,(off_t)0,SEEK_SET);
        wexcip_for(cub,cip,Pws,Bwr,Pti,eic,0,1); /* CONJ(dWs)  Wr */

        sf_filefresh(Pti);
        sf_filecopy(Pi,Pti,SF_COMPLEX);
    }

    /*------------------------------------------------------------*/
    /* close structures */
    wexslo_close(slo);
    wexssr_close(cub,ssr);
    wextap2D_close(tap);
    wexmva_close(mva);
    wexcip_close(cip,eic);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* close files */
    if (Ps!=NULL) sf_fileclose(Ps);
    if (Fc!=NULL) sf_fileclose(Fc);
    if (Pi!=NULL) sf_fileclose(Pi);
    if (Bws!=NULL) sf_fileclose(Bws);
    if (Bwr!=NULL) sf_fileclose(Bwr);
    if (Pws!=NULL) sf_tmpfileclose(Pws);
    if (Pwr!=NULL) sf_tmpfileclose(Pwr);
    if (Pti!=NULL) sf_tmpfileclose(Pti);
    /*------------------------------------------------------------*/

    exit (0);
}
Ejemplo n.º 8
0
int main(int argc, char* argv[])
{
    bool  verb;
    sf_axis  az,ah,ahx,ahy,ahz;
    int  iz,   ihx,ihy,ihz;
    int  nz,   nhx,nhy,nhz;
    float       hx, hy, hz;
    float oh,dh,ohx,dhx,ohy,dhy,ohz,dhz;
    sf_bands spl;

    sf_file Fd; /*  data =   vector offset (hx,hy,hz)-z */
    sf_file Fm; /* model = absolute offset     h     -z */

    int nw;    /* spline order */
    int nd,id; /*  data size (nd=nhx*nhy*nhz) */
    int nh;    /* model size (nm=nh) */

    float *dat=NULL;
    float *mod=NULL;
    float *map=NULL;

    float *mwt=NULL;
    float *dwt=NULL;

    int i;
/*    int im;*/
/*------------------------------------------------------------*/

    sf_init(argc,argv);

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getint(   "nw",&nw))     nw=4;     /* spline order */

    Fd = sf_input ("in");
    ahx = sf_iaxa(Fd,1); sf_setlabel(ahx,"hx"); if(verb) sf_raxa(ahx);
    ahy = sf_iaxa(Fd,2); sf_setlabel(ahy,"hy"); if(verb) sf_raxa(ahy);
    ahz = sf_iaxa(Fd,3); sf_setlabel(ahz,"hz"); if(verb) sf_raxa(ahz);
    az  = sf_iaxa(Fd,4); sf_setlabel(az,"z");   if(verb) sf_raxa(az);

    nhx = sf_n(ahx); ohx = sf_o(ahx); dhx = sf_d(ahx);
    nhy = sf_n(ahy); ohy = sf_o(ahy); dhy = sf_d(ahy);
    nhz = sf_n(ahz); ohz = sf_o(ahz); dhz = sf_d(ahz);
    nz = sf_n(az);

    if(!sf_getint  ("nh",&nh)) nh=nhx + ohx/dhx;
    if(!sf_getfloat("oh",&oh)) oh=0;
    if(!sf_getfloat("dh",&dh)) dh=dhx;
    ah = sf_maxa(nh,oh,dh); sf_setlabel(ah,"h"); if(verb) sf_raxa(ah);

    Fm = sf_output("out");
    sf_oaxa(Fm,ah,1);
    sf_oaxa(Fm,az,2);
    sf_putint(Fm,"n3",1);
    sf_putint(Fm,"n4",1);

/*------------------------------------------------------------*/
    nd = nhx*nhy*nhz;  /*  data size */

    map = sf_floatalloc(nd); /* mapping */

    mod = sf_floatalloc(nh); /* model vector */
    dat = sf_floatalloc(nd); /*  data vector */

    mwt = sf_floatalloc(nh); /* model weight */
    dwt = sf_floatalloc(nd); /*  data weight */

    spl = sf_spline_init(nw,nd);

    for(ihz=0;ihz<nhz;ihz++) {
	hz = ohz + ihz * dhz;         hz*=hz;
	for(ihy=0;ihy<nhy;ihy++) {
	    hy = ohy + ihy * dhy;     hy*=hy;
	    for(ihx=0;ihx<nhx;ihx++) {
		hx = ohx + ihx * dhx; hx*=hx;
		
		i = ihz * nhx*nhy + 
		    ihy * nhx     +
		    ihx;
		
		map[i] = sqrtf(hx+hy+hz);
	    }
	}
    }

    sf_int1_init( map, 
		  oh, dh, nh, 
		  sf_spline_int, 
		  nw, 
		  nd, 
		  0.0);

    for(id=0;id<nd;id++) {
	dwt[id]=1;
    }
    sf_banded_solve(spl,dwt);  

    for(iz=0;iz<nz;iz++) {
	sf_warning("iz=%d of %d",iz+1,nz);

	sf_floatread(dat,nd,Fd);

	sf_banded_solve(spl,dat);  
	sf_int1_lop( true,   /* adj */
		     false,  /* add */
		     nh,     /* n model */
		     nd,     /* n data */
		     mod,   
		     dat);

	sf_floatwrite(mod,nh,Fm);
    }

/*------------------------------------------------------------*/

    sf_int1_close();

    free(map);
    free(mod);
    free(dat);
    free(mwt);
    free(dwt);

    exit(0);
}
Ejemplo n.º 9
0
int main(int argc, char* argv[])
{
    bool verb,fsrf,snap,expl,dabc,sout,uses;
    int  jsnap,ntsnap,jdata;
    char *atype;
#ifdef _OPENMP
    int ompnth=1;
#endif

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */
    sf_file Fvel=NULL; /* velocity  */
    sf_file Fang=NULL; /* angles    */
    sf_file Fdat=NULL; /* data      */
    sf_file Fwfl=NULL; /* wavefield */

    /* cube axes */
    sf_axis at,az,ax;
    sf_axis as,ar;

    int     nt,nz,nx,ns,nr,nb;
    int     it,iz,ix;
    float   dt,dz,dx,dt2;

    /* FDM structure */
    fdm2d    fdm=NULL;
    abcone2d abc=NULL;
    sponge   spo=NULL;

    /* I/O arrays */
    float  *ww=NULL;           /* wavelet   */
    pt2d   *ss=NULL;           /* sources   */
    pt2d   *rr=NULL;           /* receivers */
    float  *dd=NULL;           /* data      */

    float **tt=NULL;
    float **vp=NULL;           /* velocity */

    float **vpn=NULL;
    float **vpz=NULL;
    float **vpx=NULL;
    float **vsz=NULL;

    float **tht=NULL,**sit=NULL,**cot=NULL;
    float st,ct;

    float **pm=NULL,**po=NULL,**pp=NULL,**pa=NULL,**pt=NULL; /*      main wavefield */
    float **qm=NULL,**qo=NULL,**qp=NULL,**qa=NULL,**qt=NULL; /* auxiliary wavefield */
    float **sf=NULL; /* "stress" field */

    /* linear inteppolation weights/indices */
    lint2d cs,cr;

    /* FD operator size */
    float cox,cax,cbx,c1x,c2x;
    float coz,caz,cbz,c1z,c2z;

    /* wavefield cut params */
    sf_axis   acz=NULL,acx=NULL;
    int       nqz,nqx;
    float     oqz,oqx;
    float     dqz,dqx;
    float     **pc=NULL;

    float H1p,H2p,H1q,H2q;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    /* select anisotropy model */
    if (NULL == (atype = sf_getstring("atype"))) atype = "i";
    switch(atype[0]) {
	case 't':
	    sf_warning("TTI model");
	    break;

	case 'v':
	    sf_warning("VTI model");
	    break;

	case 'i':
	default:
	    sf_warning("ISO model");
	    break;
    }

    /*------------------------------------------------------------*/
    /* OMP parameters */
#ifdef _OPENMP
    ompnth=omp_init();
#endif
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface */
    if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */
    if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */
    if(! sf_getbool("sout",&sout)) sout=false; /* stress output */
    if(! sf_getbool("uses",&uses)) uses=false; /* use vsz */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* I/O files */
    Fwav = sf_input ("in" ); /* wavelet   */
    Fvel = sf_input ("vel"); /* velocity  */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */
    Fang = sf_input ("ang"); /* angles    */
    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */

    /*------------------------------------------------------------*/
    /* axes */
    at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(Fvel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(Fvel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */

    as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */

    nt = sf_n(at); dt = sf_d(at);
    nz = sf_n(az); dz = sf_d(az);
    nx = sf_n(ax); dx = sf_d(ax);

    ns = sf_n(as);
    nr = sf_n(ar);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* other execution parameters */
    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;        
    }
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* setup output data header */
    sf_oaxa(Fdat,ar,1);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,2);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(az);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax);

	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax);

	dqz=sf_d(az);
	dqx=sf_d(ax);

	acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx);
	/* check if the imaging window fits in the wavefield domain */

	pc=sf_floatalloc2(sf_n(acz),sf_n(acx));

	ntsnap=0;
	for(it=0; it<nt; it++) {
	    if(it%jsnap==0) ntsnap++;
	}
	sf_setn(at,  ntsnap);
	sf_setd(at,dt*jsnap);
	if(verb) sf_raxa(at);

	sf_oaxa(Fwfl,acz,1);
	sf_oaxa(Fwfl,acx,2);
	sf_oaxa(Fwfl,at, 3);
    }

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC */
    if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;

    fdm=fdutil_init(verb,fsrf,az,ax,nb,1);

    sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);
    /*------------------------------------------------------------*/

    if(expl) {
	ww = sf_floatalloc( 1);
    } else {
	ww = sf_floatalloc(ns);
    }
    dd = sf_floatalloc(nr);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */
    pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */

    cs = lint2d_make(ns,ss,fdm);
    cr = lint2d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* setup FD coefficients */
    cox = C0 / (dx*dx);
    cax = CA / (dx*dx);
    cbx = CB / (dx*dx);
    c1x = C1 / dx;
    c2x = C2 / dx;

    coz = C0 / (dz*dz);
    caz = CA / (dz*dz);
    cbz = CB / (dz*dz);
    c1z = C1 / dz;
    c2z = C2 / dz;

    /* precompute dt^2*/
    dt2 = dt*dt;

    /*------------------------------------------------------------*/ 
    tt = sf_floatalloc2(nz,nx); 
    /*------------------------------------------------------------*/

    /* input velocity */
    vp  =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 

    vpz =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    sf_floatread(tt[0],nz*nx,Fvel ); 
    expand(tt,vpz,fdm); /* VPz */

    for    (ix=0; ix<fdm->nxpad; ix++) {
	for(iz=0; iz<fdm->nzpad; iz++) {	    
	    vp [ix][iz] = vpz[ix][iz];
	    vpz[ix][iz] = vpz[ix][iz] * vpz[ix][iz];
	}
    }
    if(fsrf) { /* free surface */
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nb; iz++) {
		vpz[ix][iz]=0;
	    }
	}
    }

    if(atype[0] != 'i') {
	vpn =sf_floatalloc2(fdm->nzpad,fdm->nxpad);     
	sf_floatread(tt[0],nz*nx,Fvel );    
	expand(tt,vpn,fdm); /* VPn */

	vpx =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
	sf_floatread(tt[0],nz*nx,Fvel );    
	expand(tt,vpx,fdm); /* VPx */

	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {	    
		vpn[ix][iz] = vpn[ix][iz] * vpn[ix][iz];
		vpx[ix][iz] = vpx[ix][iz] * vpx[ix][iz];
	    }
	}

	if(fsrf) { /* free surface */
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nb; iz++) {
		    vpn[ix][iz]=0;
		    vpx[ix][iz]=0;
		}
	    }
	}

	if(uses) {
	    vsz =sf_floatalloc2(fdm->nzpad,fdm->nxpad);
	    sf_floatread(tt[0],nz*nx,Fvel );    
	    expand(tt,vsz,fdm); /* VSz */
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nzpad; iz++) {
		    vsz[ix][iz] = vsz[ix][iz] * vsz[ix][iz];
		}
	    }
	}
    }

    /*------------------------------------------------------------*/
    if( atype[0]=='t') {
	/* input tilt angle */
	tht =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 

	sit =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
	cot =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 

	sf_floatread(tt[0],nz*nx,Fang); 
	expand(tt,tht,fdm);
	
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {	    
		tht[ix][iz] *= SF_PI/180.;
		sit[ix][iz] =   sinf(tht[ix][iz]);
		cot[ix][iz] =   cosf(tht[ix][iz]);
	    }
	}

	free(*tht); free(tht);
    }

    /*------------------------------------------------------------*/
    free(*tt); free(tt);    
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* allocate wavefield arrays */
    pm=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    po=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    pp=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    pa=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    for    (ix=0; ix<fdm->nxpad; ix++) {
	for(iz=0; iz<fdm->nzpad; iz++) {
	    pm[ix][iz]=0;
	    po[ix][iz]=0;
	    pp[ix][iz]=0;
	    pa[ix][iz]=0;
	}
    }
    
    if(atype[0] != 'i') {
	qm=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
	qo=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
	qp=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
	qa=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		qm[ix][iz]=0;
		qo[ix][iz]=0;
		qp[ix][iz]=0;
		qa[ix][iz]=0;
	    }
	}

	if(sout) sf=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    }

    /*------------------------------------------------------------*/
    if(dabc) {
	abc = abcone2d_make(NOP,dt,vp,fsrf,fdm); /* one-way */
	spo = sponge_make(fdm->nb);              /* sponge  */
    }

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);

	/* compute acceleration */
	switch(atype[0]) {
	    case 't':

		if(uses) {
#ifdef _OPENMP
#pragma omp parallel for						\
    schedule(dynamic,fdm->ompchunk)					\
    private(ix,iz,H1p,H2p,H1q,H2q,st,ct)				\
    shared(fdm,pa,po,qa,qo,						\
	   cox,cax,cbx,c1x,c2x,						\
	   coz,caz,cbz,c1z,c2z,						\
	   vpn,vpz,vpx,vsz,						\
	   sit,cot)
#endif
		    for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
			for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
			    
			    st=sit[ix][iz];
			    ct=cot[ix][iz];
			    
			    H1p = H1(po,ix,iz,				\
				     st,ct,				\
				     cox,cax,cbx,c1x,c2x,		\
				     coz,caz,cbz,c1z,c2z);
			    
			    H2p = H2(po,ix,iz,				\
				     st,ct,				\
				     cox,cax,cbx,c1x,c2x,		\
				     coz,caz,cbz,c1z,c2z);
			    
			    H1q = H1(qo,ix,iz,				\
				     st,ct,				\
				     cox,cax,cbx,c1x,c2x,		\
				     coz,caz,cbz,c1z,c2z);
			    
			    H2q = H2(qo,ix,iz,				\
				     st,ct,				\
				     cox,cax,cbx,c1x,c2x,		\
				     coz,caz,cbz,c1z,c2z);
			    
			    /* p - main field */
			    pa[ix][iz] = 
				H1p * vsz[ix][iz] +
				H2p * vpx[ix][iz] + 
				H1q * vpz[ix][iz] -
				H1q * vsz[ix][iz];
			    
			    /* q - auxiliary field */
			    qa[ix][iz] = 
				H2p * vpn[ix][iz] -
				H2p * vsz[ix][iz] +
				H1q * vpz[ix][iz] +
				H2q * vsz[ix][iz];
			    
			}
		    }
		} else {
#ifdef _OPENMP
#pragma omp parallel for						\
    schedule(dynamic,fdm->ompchunk)					\
    private(ix,iz,H2p,H1q,st,ct)					\
    shared(fdm,pa,po,qa,qo,						\
	   cox,cax,cbx,c1x,c2x,						\
	   coz,caz,cbz,c1z,c2z,						\
	   vpn,vpz,vpx,							\
	   sit,cot)
#endif
		    for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
			for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
			    
			    st=sit[ix][iz];
			    ct=cot[ix][iz];
			    
			    H2p = H2(po,ix,iz,				\
				     st,ct,				\
				     cox,cax,cbx,c1x,c2x,		\
				     coz,caz,cbz,c1z,c2z);
			    
			    H1q = H1(qo,ix,iz,				\
				     st,ct,				\
				     cox,cax,cbx,c1x,c2x,		\
				     coz,caz,cbz,c1z,c2z);
			    
			    /* p - main field */
			    pa[ix][iz] = 
				H2p * vpx[ix][iz] + 
				H1q * vpz[ix][iz];
			    
			    /* q - auxiliary field */
			    qa[ix][iz] = 
				H2p * vpn[ix][iz] +
				H1q * vpz[ix][iz];
			}
		    }

		}
		break;
		    
	    case 'v':

		if(uses) {
#ifdef _OPENMP
#pragma omp parallel for						\
    schedule(dynamic,fdm->ompchunk)					\
    private(ix,iz,H1p,H2p,H1q,H2q)					\
    shared(fdm,pa,po,qa,qo,						\
	   cox,cax,cbx,							\
	   coz,caz,cbz,							\
	   vpn,vpz,vpx,vsz)
#endif
		    for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
			for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
			    
			    H1p = Dzz(po,ix,iz,coz,caz,cbz);
			    H1q = Dzz(qo,ix,iz,coz,caz,cbz);
			    
			    H2p = Dxx(po,ix,iz,cox,cax,cbx);
			    H2q = Dxx(qo,ix,iz,cox,cax,cbx);
			    
			    /* p - main field */
			    pa[ix][iz] = 
				H1p * vsz[ix][iz] +
				H2p * vpx[ix][iz] + 
				H1q * vpz[ix][iz] -
				H1q * vsz[ix][iz];
			    
			    /* q - auxiliary field */
			    qa[ix][iz] = 
				H2p * vpn[ix][iz] -
				H2p * vsz[ix][iz] +
				H1q * vpz[ix][iz] +
				H2q * vsz[ix][iz];
			}
		    } 
		} else {
#ifdef _OPENMP
#pragma omp parallel for					\
    schedule(dynamic,fdm->ompchunk)				\
    private(ix,iz,H2p,H1q)					\
    shared(fdm,pa,po,qa,qo,					\
	   cox,cax,cbx,						\
	   coz,caz,cbz,						\
	   vpn,vpx,vpz)
#endif
		    for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
			for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
			    
			    H1q = Dzz(qo,ix,iz,coz,caz,cbz);			    
			    H2p = Dxx(po,ix,iz,cox,cax,cbx);
			    
			    /* p - main field */
			    pa[ix][iz] = 
				H2p * vpx[ix][iz] + 
				H1q * vpz[ix][iz];
			    
			    /* q - auxiliary field */
			    qa[ix][iz] = 
				H2p * vpn[ix][iz] +
				H1q * vpz[ix][iz];
			}
		    } 
		}
		break;
		
	    case 'i':
	    default:
#ifdef _OPENMP
#pragma omp parallel for					\
    schedule(dynamic,fdm->ompchunk)				\
    private(ix,iz)						\
    shared(fdm,pa,po,						\
	   cox,cax,cbx,						\
	   coz,caz,cbz,						\
	   vpz)
#endif
		for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
		    for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
			
			pa[ix][iz] = ( Dxx(po,ix,iz,cox,cax,cbx) + 
				       Dzz(po,ix,iz,coz,caz,cbz) ) * vpz[ix][iz];
			
		    }
		}   
		break;
	}

	/* inject acceleration source */
	if(expl) {
	    sf_floatread(ww, 1,Fwav);
	    ;                   lint2d_inject1(pa,ww[0],cs);
	    if(atype[0] != 'i') lint2d_inject1(qa,ww[0],cs);
	} else {
	    sf_floatread(ww,ns,Fwav);	
	    ;                   lint2d_inject(pa,ww,cs);
	    if(atype[0] != 'i') lint2d_inject(qa,ww,cs);
	}

	/* step forward in time */
#ifdef _OPENMP
#pragma omp parallel for	    \
    schedule(dynamic,fdm->ompchunk) \
    private(ix,iz)		    \
    shared(fdm,pa,po,pm,pp,dt2)
#endif
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		pp[ix][iz] = 2*po[ix][iz] 
		    -          pm[ix][iz] 
		    +          pa[ix][iz] * dt2;
	    }
	}
	/* circulate wavefield arrays */
	pt=pm;
	pm=po;
	po=pp;
	pp=pt;
	
	if(atype[0] != 'i') {
	    
#ifdef _OPENMP
#pragma omp parallel for			\
    schedule(dynamic,fdm->ompchunk)		\
    private(ix,iz)				\
    shared(fdm,qa,qo,qm,qp,dt2)
#endif
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nzpad; iz++) {
		    qp[ix][iz] = 2*qo[ix][iz] 
			-          qm[ix][iz] 
			+          qa[ix][iz] * dt2;
		}
	    }
	    /* circulate wavefield arrays */
	    qt=qm;
	    qm=qo;
	    qo=qp;
	    qp=qt;
	}

	/* one-way abc apply */
	if(dabc) {
	    abcone2d_apply(po,pm,NOP,abc,fdm);
	    sponge2d_apply(pm,spo,fdm);
	    sponge2d_apply(po,spo,fdm);
	    
	    if(atype[0] != 'i') {
		abcone2d_apply(qo,qm,NOP,abc,fdm);
		sponge2d_apply(qm,spo,fdm);
		sponge2d_apply(qo,spo,fdm);
	    }
	}
	
	/* compute stress */
	if(sout && (atype[0] != 'i')) {
#ifdef _OPENMP
#pragma omp parallel for			\
    schedule(dynamic,fdm->ompchunk)		\
    private(ix,iz)				\
    shared(fdm,po,qo,sf)
#endif
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nzpad; iz++) {
		    sf[ix][iz] = po[ix][iz] + qo[ix][iz];
		}
	    }
	}

	/* extract data at receivers */
	if(sout && (atype[0] != 'i')) {lint2d_extract(sf,dd,cr);
	} else {                       lint2d_extract(po,dd,cr);}
	if(it%jdata==0) sf_floatwrite(dd,nr,Fdat);

	/* extract wavefield in the "box" */
	if(snap && it%jsnap==0) {
	    if(sout && (atype[0] != 'i')) {cut2d(sf,pc,fdm,acz,acx);
	    } else {                       cut2d(po,pc,fdm,acz,acx);}
	    sf_floatwrite(pc[0],sf_n(acz)*sf_n(acx),Fwfl);
	}

    }
    if(verb) fprintf(stderr,"\n");    

    /*------------------------------------------------------------*/
    /* deallocate arrays */

    free(*pm); free(pm);
    free(*pp); free(pp);
    free(*po); free(po);
    free(*pa); free(pa);
    free(*pc); free(pc);

    free(*vp);  free(vp);
    free(*vpz); free(vpz);

    if(atype[0] != 'i') {
	free(*qm); free(qm);
	free(*qp); free(qp);
	free(*qo); free(qo);
	free(*qa); free(qa);

	free(*vpn); free(vpn);
	free(*vpx); free(vpx);

	if(uses){ free(*vsz); free(vsz); }
	if(sout){ free(*sf);  free(sf);  }
    }

    if(atype[0] == 't') {
	free(*sit); free(sit);
	free(*cot); free(cot);
    }

    free(ww);
    free(ss);
    free(rr);
    free(dd);
    /*------------------------------------------------------------*/

    exit (0);
}
Ejemplo n.º 10
0
int main(int argc, char* argv[])
{
    bool verb,fsrf,snap,expl,dabc; 
    int  jsnap,ntsnap,jdata;

    /* OMP parameters */
#ifdef _OPENMP
    int ompnth;
#endif 

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */
    sf_file Fvel=NULL; /* velocity  */
    sf_file Fdat=NULL; /* data      */
    sf_file Fwfl=NULL; /* wavefield */

    /* cube axes */
    sf_axis at,az,ax;
    sf_axis as,ar;

    int     nt,nz,nx,ns,nr,nb;
    int     it,iz,ix;
    float   dt,dz,dx,idz,idx,dt2;

    /* FDM structure */
    fdm2d    fdm=NULL;
    abcone2d abc=NULL;
    sponge   spo=NULL;

    /* I/O arrays */
    float  *ww=NULL;           /* wavelet   */
    pt2d   *ss=NULL;           /* sources   */
    pt2d   *rr=NULL;           /* receivers */
    float  *dd=NULL;           /* data      */

    float **tt=NULL;
    float **vp=NULL;           /* velocity */

    float **vp2=NULL;
    float **vv2=NULL;
    float **vh2=NULL;

    float **rm,**ro,**rp,**ra,**rt; /*      main wavefield */
    float **qm,**qo,**qp,**qa,**qt; /* auxiliary wavefield */

    /* linear interpolation weights/indices */
    lint2d cs,cr;

    /* FD operator size */
    float cox,coz,cax,cbx,caz,cbz;

    /* wavefield cut params */
    sf_axis   acz=NULL,acx=NULL;
    int       nqz,nqx;
    float     oqz,oqx;
    float     dqz,dqx;
    float     **qc=NULL;

    float H2q,H1r;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    /*------------------------------------------------------------*/
    /* OMP parameters */
#ifdef _OPENMP
    ompnth=omp_init();
#endif
    /*------------------------------------------------------------*/

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
    if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */
    if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* I/O files */
    Fwav = sf_input ("in" ); /* wavelet   */
    Fvel = sf_input ("vel"); /* velocity  */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */
    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */

    /*------------------------------------------------------------*/
    /* axes */
    at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(Fvel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(Fvel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */

    as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */

    nt = sf_n(at); dt = sf_d(at);
    nz = sf_n(az); dz = sf_d(az);
    nx = sf_n(ax); dx = sf_d(ax);

    ns = sf_n(as);
    nr = sf_n(ar);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* other execution parameters */
    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;        
    }
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC */
    if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;

    fdm=fdutil_init(verb,fsrf,az,ax,nb,1);

    sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* setup output data header */
    sf_oaxa(Fdat,ar,1);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,2);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(az);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax);

	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax);

	dqz=sf_d(az);
	dqx=sf_d(ax);

	acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx);
	/* check if the imaging window fits in the wavefield domain */

	qc=sf_floatalloc2(sf_n(acz),sf_n(acx));

	ntsnap=0;
	for(it=0; it<nt; it++) {
	    if(it%jsnap==0) ntsnap++;
	}
	sf_setn(at,  ntsnap);
	sf_setd(at,dt*jsnap);
	if(verb) sf_raxa(at);

	sf_oaxa(Fwfl,acz,1);
	sf_oaxa(Fwfl,acx,2);
	sf_oaxa(Fwfl,at, 3);
    }

    if(expl) {
	ww = sf_floatalloc( 1);
    } else {
	ww = sf_floatalloc(ns);
    }
    dd = sf_floatalloc(nr);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */
    pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */

    cs = lint2d_make(ns,ss,fdm);
    cr = lint2d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* setup FD coefficients */
    idz = 1/dz;
    idx = 1/dx;

    cox= C0 * (idx*idx);
    cax= CA *  idx*idx;
    cbx= CB *  idx*idx;

    coz= C0 * (idz*idz);
    caz= CA *  idz*idz;
    cbz= CB *  idz*idz;

    /* precompute dt^2*/
    dt2 = dt*dt;

    /*------------------------------------------------------------*/ 
    tt = sf_floatalloc2(nz,nx); 
    /*------------------------------------------------------------*/

    /* input velocity */

    vp  =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    vp2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    vv2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad);     
    vh2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 

    sf_floatread(tt[0],nz*nx,Fvel );    expand(tt,vv2,fdm); /* vertical v */
    sf_floatread(tt[0],nz*nx,Fvel );    expand(tt,vp2,fdm); /* NMO v */
    sf_floatread(tt[0],nz*nx,Fvel );    expand(tt,vh2,fdm); /* horizontal v */

    for    (ix=0; ix<fdm->nxpad; ix++) {
	for(iz=0; iz<fdm->nzpad; iz++) {	    
	    vp2[ix][iz] = vp2[ix][iz] * vp2[ix][iz];
	    vv2[ix][iz] = vv2[ix][iz] * vv2[ix][iz];
	    vh2[ix][iz] = vh2[ix][iz] * vh2[ix][iz];
	}
    }    
    if(fsrf) { /* free surface */
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nb; iz++) {
		vp2[ix][iz]=0;
		vv2[ix][iz]=0;
		vh2[ix][iz]=0;
	    }
	}
    }

    /*------------------------------------------------------------*/
    free(*tt); free(tt);    
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* allocate wavefield arrays */
    qm=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    qo=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    qp=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    qa=sf_floatalloc2(fdm->nzpad,fdm->nxpad);

    rm=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    ro=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    rp=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    ra=sf_floatalloc2(fdm->nzpad,fdm->nxpad);

    for    (ix=0; ix<fdm->nxpad; ix++) {
	for(iz=0; iz<fdm->nzpad; iz++) {
	    qm[ix][iz]=0;
	    qo[ix][iz]=0;
	    qp[ix][iz]=0;
	    qa[ix][iz]=0;

	    rm[ix][iz]=0;
	    ro[ix][iz]=0;
	    rp[ix][iz]=0;
	    ra[ix][iz]=0;
	}
    }

    /*------------------------------------------------------------*/
    if(dabc) {
	/* one-way abc setup */
	abc = abcone2d_make(NOP,dt,vp,fsrf,fdm);
	/* sponge abc setup */
	spo = sponge_make(fdm->nb);
    }

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);

#ifdef _OPENMP
#pragma omp parallel for				\
    schedule(dynamic,fdm->ompchunk)			\
    private(ix,iz,H2q,H1r)					\
    shared(fdm,ra,ro,qa,qo,cox,coz,cax,caz,cbx,cbz,idx,idz,vp2)
#endif
	for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
	    for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {

		H2q = Dxx(qo,ix,iz,cox,cax,cbx);

		H1r = Dzz(ro,ix,iz,coz,caz,cbz);
		
		/* main field - q */
		qa[ix][iz] = H2q * vh2[ix][iz] + H1r * vv2[ix][iz] ;

		/* auxiliary field - r */
		ra[ix][iz] = H2q * vp2[ix][iz] + H1r * vv2[ix][iz] ;
		
	    }
	}   

	/* inject acceleration source */
	if(expl) {
	    sf_floatread(ww, 1,Fwav);
	    lint2d_inject1(ra,ww[0],cs);
	    lint2d_inject1(qa,ww[0],cs);
	} else {
	    sf_floatread(ww,ns,Fwav);	
	    lint2d_inject(ra,ww,cs);
	    lint2d_inject(qa,ww,cs);
	}

	/* step forward in time */
#ifdef _OPENMP
#pragma omp parallel for	    \
    schedule(dynamic,fdm->ompchunk) \
    private(ix,iz)		    \
    shared(fdm,ra,ro,rm,rp,qa,qo,qm,qp,dt2)
#endif
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		qp[ix][iz] = 2*qo[ix][iz] 
		    -          qm[ix][iz] 
		    +          qa[ix][iz] * dt2;

		rp[ix][iz] = 2*ro[ix][iz] 
		    -          rm[ix][iz] 
		    +          ra[ix][iz] * dt2;
	    }
	}
	/* circulate wavefield arrays */
	qt=qm;
	qm=qo;
	qo=qp;
	qp=qt;

	rt=rm;
	rm=ro;
	ro=rp;
	rp=rt;
	
	if(dabc) {
	    /* one-way abc apply */
	    abcone2d_apply(qo,qm,NOP,abc,fdm);
	    sponge2d_apply(qm,spo,fdm);
	    sponge2d_apply(qo,spo,fdm);
	    sponge2d_apply(qp,spo,fdm);

	    /* one-way abc apply */
	    abcone2d_apply(ro,rm,NOP,abc,fdm);
	    sponge2d_apply(rm,spo,fdm);
	    sponge2d_apply(ro,spo,fdm);
	    sponge2d_apply(rp,spo,fdm);
	}

	/* extract data */
	lint2d_extract(qo,dd,cr);

	if(snap && it%jsnap==0) {
	    cut2d(qo,qc,fdm,acz,acx);
	    sf_floatwrite(qc[0],sf_n(acz)*sf_n(acx),Fwfl);
	}
	if(        it%jdata==0) 
	    sf_floatwrite(dd,nr,Fdat);
    }
    if(verb) fprintf(stderr,"\n");    

    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(*rm); free(rm);
    free(*rp); free(rp);
    free(*ro); free(ro);
    free(*ra); free(ra);

    free(*qm); free(qm);
    free(*qp); free(qp);
    free(*qo); free(qo);
    free(*qa); free(qa);
    free(*qc); free(qc);

    free(*vp);  free(vp);
    free(*vp2); free(vp2);
    free(*vh2); free(vh2);
    free(*vv2); free(vv2);

    free(ww);
    free(ss);
    free(rr);
    free(dd);

    exit (0);
}
Ejemplo n.º 11
0
int main(int argc, char* argv[])
{
    bool verb; /* verbosity flag */
    bool abc;  /* absorbing boundary conditions flag */
    bool free; /* free surface flag*/
    bool snap; /* wavefield snapshots flag */
    bool dens;
    int  jsnap;/* save wavefield every *jsnap* time steps */

    /* I/O files */
    sf_file Fw,Fs,Fr;
    sf_file Fd,Fu;
    sf_file Fv=NULL; /* velocity */
    sf_file Fe=NULL; /* density */

    /* cube axes */
    sf_axis at,az,ax,as,ar;
    int it,iz,ix,is,ir, iop;
    int nt,nz,nx,ns,nr,nz2,nx2;
    float z0,dz,x0,dx,idx,idz,dt,dt2;

    /* arrays */
    pt2d   *ss, *rr; /* source/receiver locations */
    float  *ww=NULL; /* wavelet */
    float  *dd=NULL; /* data */
    float **vv=NULL; /* velocity */
    float **ee=NULL; /* density  */

    float *fzs,*fxs,    *fzr,*fxr;
    int   *jzs,*jxs,    *jzr,*jxr;

    float *ws00,*ws01,*ws10,*ws11;
    float *wr00,*wr01,*wr10,*wr11;

    float **um,**uo,**up,**ud,**vp,**ro,**tt,**ut;
    float  *bzl,*bzh,*bxl,*bxh;  /* boundary */

    int   nop=2;       /* Laplacian operator size */
    float c0, c1, c2;  /* Laplacian operator coefficients */
    float co,c1x,c2x,c1z,c2z;

    int  nbz,nbx; /* boundary size */
    float tz, tx; /* sponge boundary decay coefficients */
    float dp;
    float ws;     /* injected data */

    int ompchunk;  /* OpenMP data chunk size */

/*------------------------------------------------------------*/

    /* init RSF */
    sf_init(argc,argv);

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;

    if(! sf_getbool("verb",&verb)) verb=false;
    if(! sf_getbool( "abc",&abc ))  abc=false;
    if(! sf_getbool("snap",&snap)) snap=false;
    if(! sf_getbool("free",&free)) free=false;
    if(! sf_getbool("dens",&dens)) dens=false;

    Fw = sf_input ("in" ); /* wavelet */
    Fv = sf_input ("vel"); /* velocity */
    Fs = sf_input ("sou"); /* sources */
    Fr = sf_input ("rec"); /* receivers */
    Fu = sf_output("wfl"); /* wavefield */
    Fd = sf_output("out"); /* data */

    if(dens) Fe = sf_input("den"); /* density */

    /* read axes*/
    at=sf_iaxa(Fw,1); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az=sf_iaxa(Fv,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax=sf_iaxa(Fv,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */
    as=sf_iaxa(Fs,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* source */
    ar=sf_iaxa(Fr,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receiver */

    nt=sf_n(at); dt=sf_d(at);
    nz=sf_n(az);
    nx=sf_n(ax);
    ns=sf_n(as);
    nr=sf_n(ar);

    /* configure wavefield snapshots */
    if(snap) {
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }

/*------------------------------------------------------------*/

    /* expand domain for absorbing boundary conditions */
    if(abc) {
	if(! sf_getint("nbz",&nbz)) nbz=nop; if(nbz<nop) nbz=nop;
	if(! sf_getint("nbx",&nbx)) nbx=nop; if(nbx<nop) nbx=nop;
	
	if(! sf_getfloat("tz",&tz)) tz=0.025;
	if(! sf_getfloat("tx",&tx)) tx=0.025;
    } else {
	nbz=nop;
	nbx=nop;
    }
    /* expanded domain ( az+2 nz, ax+2 nx ) */
    nz2=nz+2*nbz; dz=sf_d(az); z0=sf_o(az)-nbz*dz; 
    nx2=nx+2*nbx; dx=sf_d(ax); x0=sf_o(ax)-nbx*dx; 

    sf_setn(az,nz2); sf_seto(az,z0); if(verb) sf_raxa(az);
    sf_setn(ax,nx2); sf_seto(ax,x0); if(verb) sf_raxa(ax);
    
/*------------------------------------------------------------*/

    /* setup output data header */
    sf_oaxa(Fd,ar,1);
    sf_oaxa(Fd,at,2);

    /* setup output wavefield header */
    if(snap) {
	sf_setn(at,nt/jsnap);
	sf_setd(at,dt*jsnap);

	sf_oaxa(Fu,az,1);
	sf_oaxa(Fu,ax,2);
	sf_oaxa(Fu,at,3);
    }

    /* Laplacian coefficients */
    c0=-30./12.; 
    c1=+16./12.;
    c2=- 1./12.;

    dt2 = dt*dt;
    idz = 1/dz;
    idx = 1/dx;

    co = c0 * (idx*idx+idz*idz);
    c1x= c1 *  idx*idx;
    c2x= c2 *  idx*idx;
    c1z= c1 *          idz*idz;
    c2z= c2 *          idz*idz;

/*------------------------------------------------------------*/
     
    /* allocate arrays */
    ww=sf_floatalloc (nt);    sf_floatread(ww   ,nt   ,Fw);
    vv=sf_floatalloc2(nz,nx); sf_floatread(vv[0],nz*nx,Fv);

    ee=sf_floatalloc2(nz,nx); 
    if(dens) {
	sf_floatread(ee[0],nz*nx,Fe); 
    } else {
	for (iz=0; iz<nz; iz++) {
	    for (ix=0; ix<nx; ix++) {
		ee[ix][iz]=1;
	    }
	}
    }
    
    /* allocate source/receiver point arrays */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fs,ss,ns,3); /* read 3 elements (x,z,v) */
    pt2dread1(Fr,rr,nr,2); /* read 2 elements (x,z)   */

    dd=sf_floatalloc(nr);
    for(ir=0;ir<nr;ir++) {
	dd[ir]=0;
    }
    
    jzs=sf_intalloc(ns); fzs=sf_floatalloc(ns); 
    jzr=sf_intalloc(nr); fzr=sf_floatalloc(nr);
    jxs=sf_intalloc(ns); fxs=sf_floatalloc(ns);
    jxr=sf_intalloc(nr); fxr=sf_floatalloc(nr);

    ws00 = sf_floatalloc(ns); wr00 = sf_floatalloc(nr); 
    ws01 = sf_floatalloc(ns); wr01 = sf_floatalloc(nr);
    ws10 = sf_floatalloc(ns); wr10 = sf_floatalloc(nr);
    ws11 = sf_floatalloc(ns); wr11 = sf_floatalloc(nr);
/*------------------------------------------------------------*/

    for (is=0;is<ns;is++) {

	if(ss[is].z >= z0 && 
	   ss[is].z <  z0 + (nz2-1)*dz &&
	   ss[is].x >= x0 && 
	   ss[is].x <  x0 + (nx2-1)*dx) {
	    
	    jzs[is] = (int)( (ss[is].z-z0)/dz);
	    fzs[is] =        (ss[is].z-z0)/dz - jzs[is];	    
	    jxs[is] = (int)( (ss[is].x-x0)/dx);
	    fxs[is] =        (ss[is].x-x0)/dx - jxs[is];
	} else {
	    jzs[is] = 0; jxs[is] = 0;
	    fzs[is] = 1; fxs[is] = 0;
	    ss[is].v= 0;
	}

	ws00[is] = (1-fzs[is])*(1-fxs[is]);
	ws01[is] = (  fzs[is])*(1-fxs[is]);
	ws10[is] = (1-fzs[is])*(  fxs[is]);
	ws11[is] = (  fzs[is])*(  fxs[is]);

    }

    for (ir=0;ir<nr;ir++) {

	if(rr[ir].z >= z0 && 
	   rr[ir].z < z0 + (nz2-1)*dz &&
	   rr[ir].x >= x0 && 
	   rr[ir].x < x0 + (nx2-1)*dx) {
	    
	    jzr[ir] = (int)( (rr[ir].z-z0)/dz);
	    fzr[ir] =        (rr[ir].z-z0)/dz - jzr[ir];
	    jxr[ir] = (int)( (rr[ir].x-x0)/dx);
	    fxr[ir] =        (rr[ir].x-x0)/dx - jxr[ir];

	    rr[ir].v=1;
	} else {
	    jzr[ir] = 0;
	    fzr[ir] = 1;
	    rr[ir].v= 0;
	}

	wr00[ir] = (1-fzr[ir])*(1-fxr[ir]);
	wr01[ir] = (  fzr[ir])*(1-fxr[ir]);
	wr10[ir] = (1-fzr[ir])*(  fxr[ir]);
	wr11[ir] = (  fzr[ir])*(  fxr[ir]);
    }
    
/*------------------------------------------------------------*/
    
    /* allocate temporary arrays */
    um=sf_floatalloc2(nz2,nx2);
    uo=sf_floatalloc2(nz2,nx2);
    up=sf_floatalloc2(nz2,nx2);
    ud=sf_floatalloc2(nz2,nx2);
    tt=sf_floatalloc2(nz2,nx2);

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nx2,nz2,um,uo,up,ud,tt)
#endif
    for (iz=0; iz<nz2; iz++) {
	for (ix=0; ix<nx2; ix++) {
	    um[ix][iz]=0;
	    uo[ix][iz]=0;
	    up[ix][iz]=0;
	    ud[ix][iz]=0;
	    tt[ix][iz]=1;
	}
    }

/*------------------------------------------------------------*/

    /* velocity in the expanded domain (vp=vv^2)*/
    vp=sf_floatalloc2(nz2,nx2);
    ro=sf_floatalloc2(nz2,nx2);
    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nz,nx,vp,ro,vv,ee)
#endif
    for (iz=0; iz<nz; iz++) {
	for (ix=0; ix<nx; ix++) {
	    vp[nbx+ix][nbz+iz] = vv[ix][iz] * vv[ix][iz];
	    ro[nbx+ix][nbz+iz] = ee[ix][iz];
	}
    }
    /* fill boundaries */
    for (iz=0; iz<nbz; iz++) {
	for (ix=0; ix<nx2; ix++) {
	    vp[ix][    iz  ] = vp[ix][    nbz  ];
	    vp[ix][nz2-iz-1] = vp[ix][nz2-nbz-1];
	    
	    ro[ix][    iz  ] = ro[ix][    nbz  ];
	    ro[ix][nz2-iz-1] = ro[ix][nz2-nbz-1];
	}
    }
    for (iz=0; iz<nz2; iz++) {
	for (ix=0; ix<nbx; ix++) {
	    vp[    ix  ][iz] = vp[    nbx  ][iz];
	    vp[nx2-ix-1][iz] = vp[nx2-nbx-1][iz];

	    ro[    ix  ][iz] = ro[    nbx  ][iz];
	    ro[nx2-ix-1][iz] = ro[nx2-nbx-1][iz];
	}
    }

/*------------------------------------------------------------*/

    /* free surface */
    if(abc && free) {
	for (iz=0; iz<nbz; iz++) {
	    for (ix=0; ix<nx2; ix++) {
		vp[ix][iz]=0;
	    }
	}
    }

/*------------------------------------------------------------*/

    /* sponge ABC setup */
    if(abc) {
	for (iz=0; iz<nbz; iz++) {
	    for (ix=0; ix<nx2; ix++) {
		tt[ix][    iz  ] = exp( - (tz*(nbz-iz))*(tz*(nbz-iz)) );
		tt[ix][nz2-iz-1] = tt[ix][iz];
	    }
	}
	for (iz=0; iz<nz2; iz++) {
	    for (ix=0; ix<nbx; ix++) {
		tt[    ix  ][iz] = exp( - (tx*(nbx-ix))*(tx*(nbx-ix)) );
		tt[nx2-ix-1][iz] = tt[ix][iz];
	    }
	}
    }

    /* one-way ABC setup */
    bzl=sf_floatalloc(nx2);
    bzh=sf_floatalloc(nx2);
    bxl=sf_floatalloc(nz2);
    bxh=sf_floatalloc(nz2);
    
    for (ix=0;ix<nx2;ix++) {
	dp = vp[ix][    nop  ] *dt/dz; bzl[ix] = (1-dp)/(1+dp);
	dp = vp[ix][nz2-nop-1] *dt/dz; bzh[ix] = (1-dp)/(1+dp);
    }
    for (iz=0;iz<nz2;iz++) {
	dp = vp[    nop  ][iz] *dt/dx; bxl[iz] = (1-dp)/(1+dp);
	dp = vp[nx2-nop-1][iz] *dt/dx; bxh[iz] = (1-dp)/(1+dp);
    }
/*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);
	
	if(dens) { 	/* variable density */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nop,nx2,nz2,ud,uo,ro,co,c1x,c1z,c2x,c2z,idx,idz)
#endif
	    for(    ix=nop; ix<nx2-nop; ix++) {
		for(iz=nop; iz<nz2-nop; iz++) {

		    /* 4th order Laplacian operator */
		    ud[ix][iz] = 
			co * uo[ix  ][iz  ] + 
			c1x*(uo[ix-1][iz  ] + uo[ix+1][iz  ]) +
			c2x*(uo[ix-2][iz  ] + uo[ix+2][iz  ]) +
			c1z*(uo[ix  ][iz-1] + uo[ix  ][iz+1]) +
			c2z*(uo[ix  ][iz-2] + uo[ix  ][iz+2]);	  

		    /* density terms */
		    ud[ix][iz] -= (
			D1(uo,ix,iz,idz) * D1(ro,ix,iz,idz) +
			D2(uo,ix,iz,idx) * D2(ro,ix,iz,idx) ) / ro[ix][iz];
		}
	    }   

	} else {	/* constant density */

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nop,nx2,nz2,ud,uo,co,c1x,c1z,c2x,c2z)
#endif
	    for(    ix=nop; ix<nx2-nop; ix++) {
		for(iz=nop; iz<nz2-nop; iz++) {

		    /* 4th order Laplacian operator */
		    ud[ix][iz] = 
			co * uo[ix  ][iz  ] + 
			c1x*(uo[ix-1][iz  ] + uo[ix+1][iz  ]) +
			c2x*(uo[ix-2][iz  ] + uo[ix+2][iz  ]) +
			c1z*(uo[ix  ][iz-1] + uo[ix  ][iz+1]) +
			c2z*(uo[ix  ][iz-2] + uo[ix  ][iz+2]);	  
		}
	    }
	}
	
	/* inject wavelet */
	for (is=0;is<ns;is++) {
	    ws = ww[it] * ss[is].v;
	    ud[ jxs[is]  ][ jzs[is]  ] -= ws * ws00[is];
	    ud[ jxs[is]  ][ jzs[is]+1] -= ws * ws01[is];
	    ud[ jxs[is]+1][ jzs[is]  ] -= ws * ws10[is];
	    ud[ jxs[is]+1][ jzs[is]+1] -= ws * ws11[is];
	}

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,ud,uo,um,up,vp,dt2)
#endif
	for(    ix=0; ix<nx2; ix++) {
	    for(iz=0; iz<nz2; iz++) {

		/* time step and velocity scale*/
		up[ix][iz] = 2*uo[ix][iz] - 
		               um[ix][iz] + 
		               ud[ix][iz] * vp[ix][iz] * dt2; 
	    }
	}
	/* circulate arrays */
	ut=um;
	um=uo;
	uo=up;
	up=ut;


	/* one-way ABC apply */	
	if(abc) {
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz,iop) shared(nx2,nz2,nop,uo,um,bzl,bzh)
#endif
	    for(ix=0;ix<nx2;ix++) {
		for(iop=0;iop<nop;iop++) {
		    iz = nop-iop;
		    uo      [ix][iz  ] 
			= um[ix][iz+1] 
			+(um[ix][iz  ]
			- uo[ix][iz+1]) * bzl[ix];
		    
		    iz = nz2-nop+iop-1;
		    uo      [ix][iz  ] 
			= um[ix][iz-1]
			+(um[ix][iz  ]
			- uo[ix][iz-1]) * bzh[ix];
		}
	    }

#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,1) private(ix,iz,iop) shared(nx2,nz2,nop,uo,um,bzl,bzh)
#endif
	    for(iop=0;iop<nop;iop++) {
		for(iz=0;iz<nz2;iz++) {
		    ix = nop-iop;
		    uo      [ix  ][iz] 
			= um[ix+1][iz] 
			+(um[ix  ][iz]
			- uo[ix+1][iz]) * bxl[iz];
		    
		    ix = nx2-nop+iop-1;
		    uo      [ix  ][iz] 
			= um[ix-1][iz]
			+(um[ix  ][iz]
			- uo[ix-1][iz]) * bxh[iz];
		}
	    }
	}
	
	/* sponge ABC apply */
	if(abc) {
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,uo,um,ud,tt)
#endif
	    for(    ix=0; ix<nx2; ix++) {
		for(iz=0; iz<nz2; iz++) {
		    uo[ix][iz] *= tt[ix][iz];
		    um[ix][iz] *= tt[ix][iz];
		    ud[ix][iz] *= tt[ix][iz];
		}
	    }
	}
	
	/* write wavefield */
	if(snap && it%jsnap==0) {
	    sf_floatwrite(uo[0],nz2*nx2,Fu);
	}

	/* collect data */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,1) private(ir) shared(dd,rr,uo,jzr,wr00,wr01,wr10,wr11)
#endif
	for (ir=0;ir<nr;ir++) {
	    dd[ir] =
		uo[ jxr[ir]  ][ jzr[ir]  ] * wr00[ir] +
		uo[ jxr[ir]  ][ jzr[ir]+1] * wr01[ir] +
		uo[ jxr[ir]+1][ jzr[ir]  ] * wr10[ir] +
		uo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir];
	    dd[ir] *= rr[ir].v;
	}
	/* write data */
	sf_floatwrite(dd,nr,Fd);
    }
    if(verb) fprintf(stderr,"\n");

    exit (0);
}
Ejemplo n.º 12
0
int main(int argc, char* argv[])
{
    bool verb,adj; 

    /* I/O files */
    sf_file Ftrc=NULL; /* traces       */
    sf_file Fcoo=NULL; /* coordinates  */
    sf_file Fwfl=NULL; /* wavefield    */

    /* cube axes */
    sf_axis at,az,ax,aa,ac;

    /* I/O arrays */
    float  *wco=NULL;  /* traces   */
    pt2d   *coo=NULL;  /* coordinates   */
    lint2d  cow;       /* weights/indices */
    float **wfl=NULL;  /* wavefield   */

    fdm2d fdm=NULL;
    int   iz,ix,it;
    int   nz,nx;
    float dz,dx;
    float oz,ox;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    /*------------------------------------------------------------*/
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("adj", &adj))   adj=false; /* adjoint flag */

    /*------------------------------------------------------------*/
    /* setup I/O */
    Fcoo = sf_input("coo"); /* coordinates */
    ac = sf_iaxa(Fcoo,2); sf_setlabel(ac,"c"); sf_setunit(ac,"");
    coo = (pt2d*) sf_alloc(sf_n(ac),sizeof(*coo)); 
    pt2dread1(Fcoo,coo,sf_n(ac),2); /* read (x,z) coordinates */

    if(adj) {
	Fwfl = sf_input ("in");  /* wavefield */
	Ftrc = sf_output("out"); /* traces   */

	az = sf_iaxa(Fwfl,1); sf_setlabel(az,"z");
	ax = sf_iaxa(Fwfl,2); sf_setlabel(ax,"x");
	at = sf_iaxa(Fwfl,3); sf_setlabel(at,"t");

	aa = sf_maxa(1,0,1);
	sf_oaxa(Ftrc,ac,1);
	sf_oaxa(Ftrc,at,2);
	sf_oaxa(Ftrc,aa,3);
    } else {
	Ftrc = sf_input ("in" ); /* traces   */
	Fwfl = sf_output("out"); /* wavefield */

	at = sf_iaxa(Ftrc,2); sf_setlabel(at,"t");

	if(!sf_getint  ("nz",&nz)) nz=1;
	if(!sf_getfloat("oz",&oz)) oz=0.0;
	if(!sf_getfloat("dz",&dz)) dz=1.0;
	az = sf_maxa(nz,oz,dz);
	sf_setlabel(az,"z");

	if(!sf_getint  ("nx",&nx)) nx=1; 
	if(!sf_getfloat("ox",&ox)) ox=0.0;
	if(!sf_getfloat("dx",&dx)) dx=1.0;
	ax = sf_maxa(nx,ox,dx);
	sf_setlabel(ax,"x");

	sf_oaxa(Fwfl,az,1);
	sf_oaxa(Fwfl,ax,2);
	sf_oaxa(Fwfl,at,3);
    }
    
    if(verb) {
	sf_raxa(az);
	sf_raxa(ax);
	sf_raxa(at);
	sf_raxa(ac);	
    }

    /* allocate wavefield arrays */
    wco = sf_floatalloc (sf_n(ac));
    wfl = sf_floatalloc2(sf_n(az),sf_n(ax));

    /* interpolation coefficients */
    fdm = fdutil_init(verb,'n',az,ax,0,1);
    cow = lint2d_make(sf_n(ac),coo,fdm);

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<sf_n(at); it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);
	
	if(adj) {
	    sf_floatread(wfl[0],sf_n(az)*sf_n(ax),Fwfl);

	    lint2d_extract(wfl,wco,cow);

	    sf_floatwrite(wco,sf_n(ac),Ftrc);
	} else {
	    sf_floatread(wco,sf_n(ac),Ftrc);

	    for    (ix=0; ix<sf_n(ax); ix++)
		for(iz=0; iz<sf_n(az); iz++)
		    wfl[ix][iz]=0;
	    lint2d_inject(wfl,wco,cow);

	    sf_floatwrite(wfl[0],sf_n(az)*sf_n(ax),Fwfl);
	}

    }	/* end time loop */
    if(verb) fprintf(stderr,"\n");
	
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(*wfl); free(wfl);
    free(wco);
    free(coo);
    
    /*------------------------------------------------------------*/ 
    /* close files */
    if (Ftrc!=NULL) sf_fileclose(Ftrc); 
    if (Fwfl!=NULL) sf_fileclose(Fwfl);
    if (Fcoo!=NULL) sf_fileclose(Fcoo);

    exit (0);
}
Ejemplo n.º 13
0
int main(int argc, char* argv[])
{
    sf_file Fi=NULL, Fo=NULL, Fr=NULL; /* I/O files */
    bool adj, verb, linear;

    sf_axis ax,az,at,ag;
    int ix,iz,it,ig;
    int nx,nz,nt,ng;
    float dx,dz,x0,z0;

    float  **mapCC=NULL,  **mapRC=NULL;
    float ***comCC=NULL, ***comRC=NULL;
    sf_complex **rays=NULL;

    int nn,ii;
    bool comp; /* complex input */

    /* init RSF */
    sf_init(argc,argv);

    Fi = sf_input (  "in");
    Fr = sf_input ("rays");
    Fo = sf_output( "out");

    if(! sf_getbool(  "verb",&verb   ))   verb=false;
    if(! sf_getbool(   "adj",&adj    ))    adj=false;
    if(! sf_getbool("linear",&linear )) linear=true;  

    ag=sf_iaxa(Fr,1); ng=sf_n(ag); if(verb) sf_raxa(ag);
    at=sf_iaxa(Fr,2); nt=sf_n(at); if(verb) sf_raxa(at);

    if(adj) {
	if(! sf_getint  ("a2n",&nz)) nz=1;
	if(! sf_getfloat("a2o",&z0)) z0=0.;
	if(! sf_getfloat("a2d",&dz)) dz=1.;
	az = sf_maxa(nz,z0,dz); sf_setlabel(az,"a2"); if(verb) sf_raxa(az);

	if(! sf_getint  ("a1n",&nx)) nx=1;
	if(! sf_getfloat("a1o",&x0)) x0=0.;
	if(! sf_getfloat("a1d",&dx)) dx=1.;
	ax = sf_maxa(nx,x0,dx); sf_setlabel(ax,"a1"); if(verb) sf_raxa(ax);

	sf_oaxa(Fo,ax,1);
	sf_oaxa(Fo,az,2);
    } else {
	ax = sf_iaxa(Fi,1); nx=sf_n(ax); if(verb) sf_raxa(ax);
	az = sf_iaxa(Fi,2); nz=sf_n(az); if(verb) sf_raxa(az);

	sf_oaxa(Fo,ag,1);
	sf_oaxa(Fo,at,2);
    }

    nn = sf_leftsize(Fi,2);

    rays =sf_complexalloc2(ng,nt);
    sf_complexread(rays[0],ng*nt,Fr);

    c2r_init(ax,az,ag,at,verb);

/*------------------------------------------------------------*/

    comp=false;
    if(SF_COMPLEX == sf_gettype(Fi)) comp=true;

/*------------------------------------------------------------*/

    mapCC=sf_floatalloc2(nx,nz);
    mapRC=sf_floatalloc2(ng,nt);

    if(comp) {
	comCC=sf_floatalloc3(2,nx,nz);
	comRC=sf_floatalloc3(2,ng,nt);

	for(ii=0;ii<nn;ii++) {
	    sf_warning("%d of %d;",ii,nn);
	    if(adj) {
		sf_floatread (comRC[0][0],2*ng*nt,Fi);

		/* REAL */
		LOOPRC( mapRC[it][ig] = comRC[it][ig][0]; );
		c2r(linear,adj,mapCC,mapRC,rays);
		LOOPCC( comCC[iz][ix][0] = mapCC[iz][ix]; );

		/* IMAGINARY */
		LOOPRC( mapRC[it][ig] = comRC[it][ig][1]; );
		c2r(linear,adj,mapCC,mapRC,rays);
		LOOPCC( comCC[iz][ix][1] = mapCC[iz][ix]; );
Ejemplo n.º 14
0
int main(int argc, char* argv[])
{
    bool verb,fsrf,snap,dabc;
    int  jsnap,ntsnap;
    int  jdata;

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */

    sf_file Fvel=NULL; /* velocity  */
    sf_file Fref=NULL; /* reflectivity */
    sf_file Fden=NULL; /* density   */

    sf_file Fdat=NULL; /* data (background)      */
    sf_file Fwfl=NULL; /* wavefield (background) */

    sf_file Flid=NULL; /* data (scattered)      */
    sf_file Fliw=NULL; /* wavefield (scattered) */

    /* I/O arrays */
    float  *ww=NULL;           /* wavelet   */
    pt3d   *ss=NULL;           /* sources   */
    pt3d   *rr=NULL;           /* receivers */

    float ***vpin=NULL;         /* velocity  */
    float ***roin=NULL;         /* density   */
    float ***rfin=NULL;         /* reflectivity */

    float ***vp=NULL;           /* velocity     in expanded domain */
    float ***ro=NULL;           /* density      in expanded domain */
    float ***iro=NULL;          /* buoyancy     in the expanded domain */

    float ***rf=NULL;           /* reflectivity in expanded domain */

    float  *bdd=NULL;          /* data (background) */
    float  *sdd=NULL;          /* data (scattered)  */

    float ***vt=NULL;           /* temporary vp*vp * dt*dt */

    float ***fsrfbck=NULL;		/* ghost cells for free surface BC */
    float ***fsrfsct=NULL;		/* ghost cells for free surface BC */

    float ***bum,***buo,***bup,***bua,***buat,***but; /* wavefield: um = U @ t-1; uo = U @ t; up = U @ t+1 */
    float ***sum,***suo,***sup,***sua,***suat,***sut; /* wavefield: um = U @ t-1; uo = U @ t; up = U @ t+1 */

    /* cube axes */
    sf_axis at,a1,a2,a3,as,ar;
    int     nt,n1,n2,n3,ns,nr,nb;
    int     it,i1,i2,i3;
    float   dt,d1,d2,d3,id1,id2,id3,dt2;

    /* linear interpolation weights/indices */
    lint3d cs,cr;

    fdm3d    fdm;
    abcone3d abc;     /* abc */
    sponge spo;

    /* FD coefficients */
    float c1x,c1y,c1z,
          c2x,c2y,c2z,
          c3x,c3y,c3z;

    int ompchunk;
#ifdef _OPENMP
    int ompnth,ompath;
#endif

    sf_axis   ac1=NULL,ac2=NULL,ac3=NULL;
    int       nqz,nqx,nqy;
    float     oqz,oqx,oqy;
    float     dqz,dqx,dqy;
    float     ***uc=NULL;

    /* for benchmarking */
    clock_t start_t, end_t;
    float total_t;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);
    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;

    /* OpenMP data chunk size */
#ifdef _OPENMP
    if(! sf_getint("ompnth",  &ompnth))     ompnth=0;
    /* OpenMP available threads */
    #pragma omp parallel
    ompath=omp_get_num_threads();
    if(ompnth<1) ompnth=ompath;
    omp_set_num_threads(ompnth);
    sf_warning("using %d threads of a total of %d",ompnth,ompath);
#endif

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("dabc",&dabc)) dabc=false; /* Absorbing BC */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */

    Fwav = sf_input ("in" ); /* wavelet   */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */

    Fvel = sf_input ("vel"); /* velocity  */
    Fden = sf_input ("den"); /* density   */
    Fref = sf_input ("ref"); /* reflectivity */

    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */

    Fliw = sf_output("liw"); /* wavefield (scattered) */
    Flid = sf_output("lid"); /* data (scattered) */

    /* axes */
    at = sf_iaxa(Fwav,2);
    sf_setlabel(at,"t");
    if(verb) sf_raxa(at); /* time */
    as = sf_iaxa(Fsou,2);
    sf_setlabel(as,"s");
    if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(Frec,2);
    sf_setlabel(ar,"r");
    if(verb) sf_raxa(ar); /* receivers */
    a1 = sf_iaxa(Fvel,1);
    sf_setlabel(a1,"z");
    if(verb) sf_raxa(a1); /* z */
    a2 = sf_iaxa(Fvel,2);
    sf_setlabel(a2,"x");
    if(verb) sf_raxa(a2); /* x */
    a3 = sf_iaxa(Fvel,3);
    sf_setlabel(a3,"y");
    if(verb) sf_raxa(a3); /* y */

    nt = sf_n(at);
    dt = sf_d(at);
    ns = sf_n(as);
    nr = sf_n(ar);
    n1 = sf_n(a1);
    d1 = sf_d(a1);
    n2 = sf_n(a2);
    d2 = sf_d(a2);
    n3 = sf_n(a3);
    d3 = sf_d(a3);

    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
        if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC */
    if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;

    fdm=fdutil3d_init(verb,fsrf,a1,a2,a3,nb,ompchunk);

    sf_setn(a1,fdm->nzpad);
    sf_seto(a1,fdm->ozpad);
    if(verb) sf_raxa(a1);
    sf_setn(a2,fdm->nxpad);
    sf_seto(a2,fdm->oxpad);
    if(verb) sf_raxa(a2);
    sf_setn(a3,fdm->nypad);
    sf_seto(a3,fdm->oypad);
    if(verb) sf_raxa(a3);
    /*------------------------------------------------------------*/

    /* setup output data header */
    sf_oaxa(Fdat,ar,1);
    sf_oaxa(Flid,ar,1);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,2);
    sf_oaxa(Flid,at,2);

    /* setup output wavefield header */
    if(snap) {
        if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(a1);
        if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(a2);
        if(!sf_getint  ("nqy",&nqy)) nqy=sf_n(a3);

        if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(a1);
        if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(a2);
        if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(a3);

        dqz=sf_d(a1);
        dqx=sf_d(a2);
        dqy=sf_d(a3);

        ac1 = sf_maxa(nqz,oqz,dqz);
        ac2 = sf_maxa(nqx,oqx,dqx);
        ac3 = sf_maxa(nqy,oqy,dqy);

        /* check if the imaging window fits in the wavefield domain */

        uc=sf_floatalloc3(sf_n(ac1),sf_n(ac2),sf_n(ac3));

        ntsnap=0;
        for(it=0; it<nt; it++) {
            if(it%jsnap==0) ntsnap++;
        }
        sf_setn(at,  ntsnap);
        sf_setd(at,dt*jsnap);
        if(verb) sf_raxa(at);

        /*	sf_setn(at,nt/jsnap);
        sf_setd(at,dt*jsnap); */

        sf_oaxa(Fwfl,ac1,1);
        sf_oaxa(Fwfl,ac2,2);
        sf_oaxa(Fwfl,ac3,3);
        sf_oaxa(Fwfl,at, 4);

        sf_oaxa(Fliw,ac1,1);
        sf_oaxa(Fliw,ac2,2);
        sf_oaxa(Fliw,ac3,3);
        sf_oaxa(Fliw,at, 4);
    }


    /* source wavelet array allocation */
    ww = sf_floatalloc(ns);

    /* data array allocation*/
    bdd = sf_floatalloc(nr);
    sdd = sf_floatalloc(nr);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt3d*) sf_alloc(ns,sizeof(*ss));
    rr = (pt3d*) sf_alloc(nr,sizeof(*rr));

    pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */
    pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */

    cs = lint3d_make(ns,ss,fdm);
    cr = lint3d_make(nr,rr,fdm);

    fdbell3d_init(1);
    /*------------------------------------------------------------*/
    /* setup FD coefficients */
    dt2 = dt*dt;
    id1 = 1/d1;
    id2 = 1/d2;
    id3 = 1/d3;

    c1x = C1*id2;
    c1y = C1*id3;
    c1z = C1*id1;

    c2x = C2*id2;
    c2y = C2*id3;
    c2z = C2*id1;

    c3x = C3*id2;
    c3y = C3*id3;
    c3z = C3*id1;

    /*------------------------------------------------------------*/
    /* input density */
    roin = sf_floatalloc3(n1, n2, n3);
    ro   = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    iro  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);

    sf_floatread(roin[0][0],n1*n2*n3,Fden);
    expand3d(roin,ro,fdm);

    /* inverse density to avoid computation on the fly */
    /*
    there is 1 shell for i1=0 || i2=0 || i3=0 that is zero,
    no big deal but better to fix it
    */
    for 		(i3=1; i3<fdm->nypad; i3++) {
        for 	(i2=1; i2<fdm->nxpad; i2++) {
            for (i1=1; i1<fdm->nzpad; i1++) {
                iro[i3][i2][i1] = 6./(  3*ro[i3  ][i2  ][i1  ] +
                                        ro[i3  ][i2  ][i1-1] +
                                        ro[i3  ][i2-1][i1  ] +
                                        ro[i3-1][i2  ][i1  ] );
            }
        }
    }

    free(**roin);
    free(*roin);
    free(roin);

    /*------------------------------------------------------------*/
    /* input velocity */
    vpin = sf_floatalloc3(n1, n2, n3);
    vp   = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    vt   = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    sf_floatread(vpin[0][0],n1*n2*n3,Fvel);
    expand3d(vpin,vp,fdm);
    free(**vpin);
    free(*vpin);
    free(vpin);

    /*------------------------------------------------------------*/
    /* input reflectivity */
    rfin = sf_floatalloc3(n1, n2, n3);
    rf   = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    sf_floatread(rfin[0][0],n1*n2*n3,Fref);
    expand3d(rfin,rf,fdm);
    free(**rfin);
    free(*rfin);
    free(rfin);

    for 		(i3=0; i3<fdm->nypad; i3++) {
        for 	(i2=0; i2<fdm->nxpad; i2++) {
            for (i1=0; i1<fdm->nzpad; i1++) {
                vt[i3][i2][i1] = vp[i3][i2][i1] * vp[i3][i2][i1] * dt2;
            }
        }
    }

    /* free surface */
    if(fsrf) {
        fsrfbck = sf_floatalloc3(4*NOP, fdm->nxpad, fdm->nypad);
        fsrfsct = sf_floatalloc3(4*NOP, fdm->nxpad, fdm->nypad);
    }
    /*------------------------------------------------------------*/
    /* allocate wavefield arrays */
    bum  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    buo  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    bup  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    bua  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    buat = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);

    sum  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    suo  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    sup  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    sua  = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);
    suat = sf_floatalloc3(fdm->nzpad, fdm->nxpad, fdm->nypad);

    for 		(i3=0; i3<fdm->nypad; i3++) {
        for 	(i2=0; i2<fdm->nxpad; i2++) {
            for (i1=0; i1<fdm->nzpad; i1++) {
                bum[i3][i2][i1]=0;
                buo[i3][i2][i1]=0;
                bup[i3][i2][i1]=0;
                bua[i3][i2][i1]=0;

                sum[i3][i2][i1]=0;
                suo[i3][i2][i1]=0;
                sup[i3][i2][i1]=0;
                sua[i3][i2][i1]=0;
            }
        }
    }

    /*------------------------------------------------------------*/
    /* one-way abc setup */
    abc = abcone3d_make(NOP,dt,vp,fsrf,fdm);
    /* sponge abc setup */
    spo = sponge_make(fdm->nb);

    free(**vp);
    free(*vp);
    free(vp);
    /*--------------------------------------------------------------*/
    /* 																*/
    /*						MAIN LOOP								*/
    /*																*/
    /*--------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\nFORWARD BORN ACOUSTIC VARIABLE-DENSITY WAVE EXTRAPOLATION \n");
    /* extrapolation */
    start_t = clock();
    for (it=0; it<nt; it++) {
        if(verb) fprintf(stderr,"%d/%d  \r",it,nt);

#ifdef _OPENMP
        #pragma omp parallel private(i3,i2,i1)
#endif
        {

            if (fsrf) {
                /* free surface */
#ifdef _OPENMP
                #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
                for 		(i3=0; i3<fdm->nypad; i3++) {
                    for 	(i2=0; i2<fdm->nxpad; i2++) {
                        for (i1=nb; i1<nb+2*NOP; i1++) {
                            fsrfbck[i3][i2][2*NOP+(i1-nb)  ] =  buo[i3][i2][i1];
                            fsrfbck[i3][i2][2*NOP-(i1-nb)-1] = -buo[i3][i2][i1];

                            fsrfsct[i3][i2][2*NOP+(i1-nb)  ] =  suo[i3][i2][i1];
                            fsrfsct[i3][i2][2*NOP-(i1-nb)-1] = -suo[i3][i2][i1];
                        }
                    }
                }
            }


            // spatial derivatives z
#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {

                        // gather
                        buat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                c3z*(buo[i3][i2][i1+2] - buo[i3][i2][i1-3]) +
                                                c2z*(buo[i3][i2][i1+1] - buo[i3][i2][i1-2]) +
                                                c1z*(buo[i3][i2][i1  ] - buo[i3][i2][i1-1])
                                            );

                        suat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                c3z*(suo[i3][i2][i1+2] - suo[i3][i2][i1-3]) +
                                                c2z*(suo[i3][i2][i1+1] - suo[i3][i2][i1-2]) +
                                                c1z*(suo[i3][i2][i1  ] - suo[i3][i2][i1-1])
                                            );
                    }
                }
            }

            if (fsrf) {
                // free surface
#ifdef _OPENMP
                #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
                for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                    for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                        for (i1=nb-NOP; i1<nb+NOP; i1++) {

                            buat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                    c3z*(fsrfbck[i3][i2][2*NOP+(i1-nb)+2] - fsrfbck[i3][i2][2*NOP+(i1-nb)-3]) +
                                                    c2z*(fsrfbck[i3][i2][2*NOP+(i1-nb)+1] - fsrfbck[i3][i2][2*NOP+(i1-nb)-2]) +
                                                    c1z*(fsrfbck[i3][i2][2*NOP+(i1-nb)  ] - fsrfbck[i3][i2][2*NOP+(i1-nb)-1])
                                                );

                            suat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                    c3z*(fsrfsct[i3][i2][2*NOP+(i1-nb)+2] - fsrfsct[i3][i2][2*NOP+(i1-nb)-3]) +
                                                    c2z*(fsrfsct[i3][i2][2*NOP+(i1-nb)+1] - fsrfsct[i3][i2][2*NOP+(i1-nb)-2]) +
                                                    c1z*(fsrfsct[i3][i2][2*NOP+(i1-nb)  ] - fsrfsct[i3][i2][2*NOP+(i1-nb)-1])
                                                );
                        }
                    }
                }
            }


#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        // scatter
                        bua[i3][i2][i1] = c1z*(	buat[i3][i2][i1  ] -
                                                buat[i3][i2][i1+1]) +
                                          c2z*(	buat[i3][i2][i1-1] -
                                                  buat[i3][i2][i1+2]) +
                                          c3z*(	buat[i3][i2][i1-2] -
                                                  buat[i3][i2][i1+3]);

                        sua[i3][i2][i1] = c1z*(	suat[i3][i2][i1  ] -
                                                suat[i3][i2][i1+1]) +
                                          c2z*(	suat[i3][i2][i1-1] -
                                                  suat[i3][i2][i1+2]) +
                                          c3z*(	suat[i3][i2][i1-2] -
                                                  suat[i3][i2][i1+3]);
                    }
                }
            }

            // spatial derivatives x
#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        // gather
                        buat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                c3x*(buo[i3][i2+2][i1] - buo[i3][i2-3][i1]) +
                                                c2x*(buo[i3][i2+1][i1] - buo[i3][i2-2][i1]) +
                                                c1x*(buo[i3][i2  ][i1] - buo[i3][i2-1][i1])
                                            );

                        suat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                c3x*(suo[i3][i2+2][i1] - suo[i3][i2-3][i1]) +
                                                c2x*(suo[i3][i2+1][i1] - suo[i3][i2-2][i1]) +
                                                c1x*(suo[i3][i2  ][i1] - suo[i3][i2-1][i1])
                                            );
                    }
                }
            }

#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        // scatter
                        bua[i3][i2  ][i1] += c1x*(buat[i3][i2  ][i1] -
                                                  buat[i3][i2+1][i1]) +
                                             c2x*(buat[i3][i2-1][i1] -
                                                  buat[i3][i2+2][i1]) +
                                             c3x*(buat[i3][i2-2][i1] -
                                                  buat[i3][i2+3][i1]);

                        sua[i3][i2  ][i1] += c1x*(suat[i3][i2  ][i1] -
                                                  suat[i3][i2+1][i1]) +
                                             c2x*(suat[i3][i2-1][i1] -
                                                  suat[i3][i2+2][i1]) +
                                             c3x*(suat[i3][i2-2][i1] -
                                                  suat[i3][i2+3][i1]);
                    }
                }
            }

            // spatial derivatives y
#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        // gather
                        buat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                c3x*(buo[i3+2][i2][i1] - buo[i3-3][i2][i1]) +
                                                c2x*(buo[i3+1][i2][i1] - buo[i3-2][i2][i1]) +
                                                c1x*(buo[i3  ][i2][i1] - buo[i3-1][i2][i1])
                                            );

                        suat[i3][i2][i1]  = iro[i3][i2][i1]*(
                                                c3x*(suo[i3+2][i2][i1] - suo[i3-3][i2][i1]) +
                                                c2x*(suo[i3+1][i2][i1] - suo[i3-2][i2][i1]) +
                                                c1x*(suo[i3  ][i2][i1] - suo[i3-1][i2][i1])
                                            );
                    }
                }
            }

#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        // scatter
                        bua[i3][i2][i1] += c1y*(buat[i3  ][i2][i1] -
                                                buat[i3+1][i2][i1]) +
                                           c2y*(	buat[i3-1][i2][i1] -
                                                   buat[i3+2][i2][i1]) +
                                           c3y*(	buat[i3-2][i2][i1] -
                                                   buat[i3+3][i2][i1]);

                        sua[i3][i2][i1] += c1y*(suat[i3  ][i2][i1] -
                                                suat[i3+1][i2][i1]) +
                                           c2y*(	suat[i3-1][i2][i1] -
                                                   suat[i3+2][i2][i1]) +
                                           c3y*(	suat[i3-2][i2][i1] -
                                                   suat[i3+3][i2][i1]);
                    }
                }
            }

            /* step forward in time */
#ifdef _OPENMP
            #pragma omp for schedule(dynamic,fdm->ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        bup[i3][i2][i1] = 2*buo[i3][i2][i1]
                                          -					bum[i3][i2][i1]
                                          -					ro[i3][i2][i1]*vt[i3][i2][i1]*bua[i3][i2][i1];

                        sup[i3][i2][i1] = 2*suo[i3][i2][i1]
                                          -					sum[i3][i2][i1]
                                          -					ro[i3][i2][i1]*vt[i3][i2][i1]*sua[i3][i2][i1];

                    }
                }
            }

            /* single scattering */
#ifdef _OPENMP
            #pragma omp for schedule(dynamic,ompchunk)
#endif
            for 		(i3=NOP; i3<fdm->nypad-NOP; i3++) {
                for 	(i2=NOP; i2<fdm->nxpad-NOP; i2++) {
                    for (i1=NOP; i1<fdm->nzpad-NOP; i1++) {
                        sup[i3][i2][i1] -= 2*rf[i3][i2][i1]*ro[i3][i2][i1]*bua[i3][i2][i1]*dt2;
                    }
                }
            }



        }	/* end of the parallel section */

        /* inject acceleration source */
        sf_floatread(ww,ns,Fwav);
        lint3d_bell(bup,ww,cs);

        /* extract data */
        lint3d_extract(bup,bdd,cr);
        lint3d_extract(sup,sdd,cr);

        if(snap && it%jsnap==0) {
            cut3d(bup,uc,fdm,ac1,ac2,ac3);
            sf_floatwrite(uc[0][0],sf_n(ac1)*sf_n(ac2)*sf_n(ac3),Fwfl);

            cut3d(sup,uc,fdm,ac1,ac2,ac3);
            sf_floatwrite(uc[0][0],sf_n(ac1)*sf_n(ac2)*sf_n(ac3),Fliw);
        }
        if(        it%jdata==0) {
            sf_floatwrite(bdd,nr,Fdat);
            sf_floatwrite(sdd,nr,Flid);
        }

        /* one-way abc apply*/
        if (dabc) {
            abcone3d_apply(bup,buo,NOP,abc,fdm);
            sponge3d_apply(bup,        spo,fdm);
            sponge3d_apply(buo,        spo,fdm);

            abcone3d_apply(sup,suo,NOP,abc,fdm);
            sponge3d_apply(sup,        spo,fdm);
            sponge3d_apply(suo,        spo,fdm);
        }

        /* circulate wavefield arrays */
        but=bum;
        bum=buo;
        buo=bup;
        bup=but;

        sut=sum;
        sum=suo;
        suo=sup;
        sup=sut;

    } /* end time loop */
    end_t = clock();
    if(verb) fprintf(stderr,"\n");

    if (verb) {
        total_t = (float)(end_t - start_t) / CLOCKS_PER_SEC;
        fprintf(stderr,"Total time taken by CPU: %g\n", total_t  );
        fprintf(stderr,"Exiting of the program...\n");
    }


    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(**bum);
    free(*bum);
    free(bum);
    free(**buo);
    free(*buo);
    free(buo);
    free(**bup);
    free(*bup);
    free(bup);
    free(**bua);
    free(*bua);
    free(bua);
    free(**buat);
    free(*buat);
    free(buat);

    free(**sum);
    free(*sum);
    free(sum);
    free(**suo);
    free(*suo);
    free(suo);
    free(**sup);
    free(*sup);
    free(sup);
    free(**sua);
    free(*sua);
    free(sua);
    free(**suat);
    free(*suat);
    free(suat);

    if(snap) {
        free(**uc);
        free(*uc);
        free(uc);
    }

    if (fsrf) {
        free(**fsrfbck);
        free(*fsrfbck);
        free(fsrfbck);
        free(**fsrfsct);
        free(*fsrfsct);
        free(fsrfsct);
    }

    free(**vt);
    free(*vt);
    free(vt);

    free(**ro);
    free(*ro);
    free(ro);
    free(**iro);
    free(*iro);
    free(iro);

    free(**rf);
    free(*rf);
    free(rf);

    free(ww);
    free(ss);
    free(rr);

    free(bdd);
    free(sdd);

    if (dabc) {
        free(spo);
        free(abc);
    }
    free(fdm);
    /* ------------------------------------------------------------------------------------------ */
    /* CLOSE FILES AND EXIT */
    if (Fwav!=NULL) sf_fileclose(Fwav);

    if (Fsou!=NULL) sf_fileclose(Fsou);
    if (Frec!=NULL) sf_fileclose(Frec);

    if (Fvel!=NULL) sf_fileclose(Fvel);
    if (Fden!=NULL) sf_fileclose(Fden);

    if (Fref!=NULL) sf_fileclose(Fref);

    if (Fdat!=NULL) sf_fileclose(Fdat);

    if (Fwfl!=NULL) sf_fileclose(Fwfl);

    if (Fliw!=NULL) sf_fileclose(Fliw);
    if (Flid!=NULL) sf_fileclose(Flid);

    exit (0);
}
Ejemplo n.º 15
0
int main (int argc, char *argv[])
{
    bool verb;
    bool rays;

    sf_axis az,ax;  /* Cartesian coordinates */
    sf_axis at,ag;  /* Ray coordinates */
    int it,ig;
    int nz,nx,nt,ng;
    float dt,dg,ot,og;

    float xsou,zsou; /* source coordinates */

    sf_file Fv=NULL; /* velocity file */
    sf_file Fw=NULL; /* wavefronfs file */

    float **vv=NULL; /* velocity       */
    pt2d   *wm=NULL; /* wavefront it-1 */
    pt2d   *wo=NULL; /* wavefront it   */
    pt2d   *wp=NULL; /* wavefront it+1 */

    pt2d    Ro;    /* point  on wft it-1 */
    pt2d Pm,Po,Pp; /* points on wft it   */
    pt2d    Qo;    /* point  on wft it+1 */

    /*------------------------------------------------------------*/
    sf_init(argc,argv);
    if(! sf_getbool("verb",&verb)) verb=false;
    if(! sf_getbool("rays",&rays)) rays=false;

    /* velocity file */
    Fv = sf_input ("in");
    az = sf_iaxa(Fv,1); sf_setlabel(az,"z"); nz=sf_n(az); if(verb) sf_raxa(az);
    ax = sf_iaxa(Fv,2); sf_setlabel(ax,"x"); nx=sf_n(ax); if(verb) sf_raxa(ax);

    vv=sf_floatalloc2(nz,nx); 
    sf_floatread(vv[0],nz*nx,Fv);

    /* source location */
    if(! sf_getfloat("xsou",&xsou)) xsou=sf_o(ax) + nx*sf_d(ax)/2;
    if(! sf_getfloat("zsou",&zsou)) zsou=sf_o(az) + nz*sf_d(az)/2;
    if(verb) fprintf(stderr,"xsou=%f zsou=%f\n",xsou,zsou);

    /* time axis */
    if(! sf_getint  ("nt",&nt)) nt=100;
    if(! sf_getfloat("ot",&ot)) ot=0;
    if(! sf_getfloat("dt",&dt)) dt=0.001;
    at = sf_maxa(nt,ot,dt); sf_setlabel(at,"t");

    /* shooting angle axis */
    if(! sf_getint  ("ng",&ng)) ng= 360;
    if(! sf_getfloat("og",&og)) og=-180;
    if(! sf_getfloat("dg",&dg)) dg= 1;
    ag = sf_maxa(ng,og,dg); sf_setlabel(ag,"g");

    /*------------------------------------------------------------*/
    /* wavefronts file (g,t) */
    Fw = sf_output("out");
    sf_oaxa(Fw,ag,1); if(verb) sf_raxa(ag);
    sf_oaxa(Fw,at,2); if(verb) sf_raxa(at);

    /* set the output to complex */
    sf_putint(Fw,"esize",8);
    sf_settype(Fw,SF_COMPLEX);

    /*------------------------------------------------------------*/
    /* allocate wavefronts */
    wm = pt2dalloc1(ng);
    wo = pt2dalloc1(ng);
    wp = pt2dalloc1(ng);

    /* initialize wavefronts */
    for( ig=0; ig<ng; ig++) {
	wm[ig].x=wo[ig].x=wp[ig].x=0;
	wm[ig].z=wo[ig].z=wp[ig].z=0;
	wm[ig].v=wo[ig].v=wp[ig].v=0;
    }

    /*------------------------------------------------------------*/
    /* init HWT */
    hwt2d_init(vv,az,ax,at,ag);

    /*------------------------------------------------------------*/
    /* construct it=0 wavefront */
    it=0;
    for( ig=0; ig<ng; ig++) {
	wm[ig].x=xsou;
	wm[ig].z=zsou;
	wm[ig].v=hwt2d_getv(wm[ig]);
    }
    pt2dwrite1(Fw,wm,ng,2); /* write wavefront it=0 */

    /*------------------------------------------------------------*/
    /* construct it=1 wavefront */
    it=1;
    for( ig=0; ig<ng; ig++) {
	double d,g;

	d = dt * hwt2d_getv(wm[ig]);
	g = (og+ig*dg) * SF_PI/180;

	wo[ig].x=xsou + d*sin(g);
	wo[ig].z=zsou + d*cos(g);
	wo[ig].v=hwt2d_getv(wo[ig]);
    }
    pt2dwrite1(Fw,wo,ng,2); /* write wavefront it=1 */

    /*------------------------------------------------------------*/
    /* LOOP over time */
    for (it=2; it<nt; it++) {
	if(verb) fprintf(stderr,"it=%d\n",it);
	
	if(ng>3 && !rays) {
	    /* boundaries */
	    ig=0;      wp[ig] = hwt2d_raytr(wm[ig],wo[ig]);
	    ig=ng-1; wp[ig] = hwt2d_raytr(wm[ig],wo[ig]);

	    for (ig=1; ig<ng-1; ig++) {
		
		Pm = wo[ig-1];
		Po = wo[ig  ];  Qo = wm[ig];
		Pp = wo[ig+1];
		
		if(hwt2d_cusp(Qo,Pm,Po,Pp)) {
		    Ro = hwt2d_raytr(Qo,   Po   );
		} else {
		    Ro = hwt2d_wfttr(Qo,Pm,Po,Pp);
		}
		wp[ig] = Ro;
	    }
	} else {
	    for (ig=0; ig<ng; ig++) {
		Po = wo[ig];  
		Qo = wm[ig];
		Ro = hwt2d_raytr(Qo,Po);
		wp[ig] = Ro;
	    }
	}

	/* write wavefront it */
	pt2dwrite1(Fw,wp,ng,2);

	/* step in time */
	for( ig=0; ig<ng; ig++) {
	    wm[ig] = wo[ig];
	    wo[ig] = wp[ig];
	}
    } /* end it */

    /*------------------------------------------------------------*/

    exit (0);
}
Ejemplo n.º 16
0
int main(int argc, char* argv[])
{
    bool verb;
    int  pick; /* method for selecting multi-arrival times */
    int  fill; /* hole-filling parameter */

    int scaleray;
    int nray,jray,iray;

    sf_axis az,ax,ay; /* Cartesian coordinates */
    int iz,ix,iy;
    sf_axis at;       /* time */
    int it;

    float xsou,ysou,zsou; /* source coordinates */

    /* shooting directions */
    float gmin,gmax,g;
    float hmin,hmax,h;

    sf_file Fv; /*   velocity file */
    sf_file Ft; /* traveltime file */

    float ***vv=NULL;   /*   velocity cube */
    float ***tt=NULL,t; /* traveltime cube */
    float ***ll=NULL,l; /* ray length cube */

    pt3d Tm,To,Tp;
    vc3d TmTo;  /* Tm-To vector */

    int  seed;

    int n;
    float o,d;
/*------------------------------------------------------------*/

    sf_init(argc,argv);

    if(! sf_getbool(    "verb",&verb    ))     verb=false;
    if(! sf_getint (    "pick",&pick    ))     pick=2;
    if(! sf_getint (    "fill",&fill    ))     fill=1;
    if(! sf_getint ("scaleray",&scaleray)) scaleray=1.;
    if(! sf_getint (    "nray",&nray    ))     nray=1;
    if(! sf_getint (    "jray",&jray    ))     jray=1;

    if(! sf_getfloat("gmin",&gmin)) gmin=-90;
    if(! sf_getfloat("gmax",&gmax)) gmax=+90;
    if(! sf_getfloat("hmin",&hmin)) hmin=0;
    if(! sf_getfloat("hmax",&hmax)) hmax=180;

    if(verb) sf_warning("gmin=%g gmax=%g",gmin,gmax);
    if(verb) sf_warning("hmin=%g hmax=%g",hmin,hmax);

    /* time axis */
        if(! sf_getint  ("nt",&n)) n=100;
    if(! sf_getfloat("ot",&o)) o=0;
    if(! sf_getfloat("dt",&d)) d=0.001;
    at=sf_maxa(n,o,d);
    sf_setlabel(at,"t");

    /* velocity file */
    Fv = sf_input ("in");
    az=sf_iaxa(Fv,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az);
    ax=sf_iaxa(Fv,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax);
    ay=sf_iaxa(Fv,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay);

    vv=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay)); 
    sf_floatread(vv[0][0],sf_n(az)*sf_n(ax)*sf_n(ay),Fv);

    /* traveltime file */
    Ft = sf_output("out");
    sf_oaxa(Ft,az,1);
    sf_oaxa(Ft,ax,2);
    sf_oaxa(Ft,ay,3);

    tt=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay)); 
    ll=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay)); 

    for(iy=0;iy<sf_n(ay);iy++) {
	for(ix=0;ix<sf_n(ax);ix++) {
	    for(iz=0;iz<sf_n(az);iz++) {
		tt[iy][ix][iz] = MISSING;
		ll[iy][ix][iz] = MISSING;
	    }
	}
    }

    /* source location */
    if(! sf_getfloat("xsou",&xsou)) xsou=sf_o(ax) + sf_n(ax)*sf_d(ax)/2;
    if(! sf_getfloat("ysou",&ysou)) ysou=sf_o(ay) + sf_n(ay)*sf_d(ay)/2;
    if(! sf_getfloat("zsou",&zsou)) zsou=sf_o(az) + sf_n(az)*sf_d(az)/2;
    if(verb) sf_warning("xsou=%f ysou=%f zsou=%f",xsou,ysou,zsou);

/*------------------------------------------------------------*/

    /* init random numbers */
    if(! sf_getint("seed",&seed)) seed = time(NULL); /* random seed */
    init_genrand((unsigned long) seed);
    
/*------------------------------------------------------------*/

    /* init HWT */
    hwt3d_rand(az,ax,ay,at);
    
/*------------------------------------------------------------*/
    
    /* LOOP over rays */
    for(iray=0; iray<nray; iray++) {
	if(verb && iray%jray == 0) sf_warning("iray=%d of %d",iray,nray);

	/* init ray */
	g = gmin + genrand_real1() * (gmax-gmin); 
	h = hmin + genrand_real1() * (hmax-hmin);

	g *= SF_PI/180;
	h *= SF_PI/180;
	
	/* trace ray */
	l = 0;
	t = 0;

	Tm.x = xsou;
	Tm.y = ysou;
	Tm.z = zsou;
	Tm.v = hwt3d_getv(vv,Tm);

	if     (pick==2) hwt3d_lint(tt,ll,Tm,t,l);
	else if(pick==1) hwt3d_tint(tt,ll,Tm,t,l);
	else             hwt3d_nint(tt,ll,Tm,t,l);

	it=1;
	t = sf_o(at) + it * sf_d(at);

	To.x = Tm.x + Tm.v*sf_d(at) * sin(g)*cos(h);
	To.y = Tm.y + Tm.v*sf_d(at) * sin(g)*sin(h);
	To.z = Tm.z + Tm.v*sf_d(at) * cos(g);
	To.v = hwt3d_getv(vv,To);

	TmTo = vec3d(&Tm,&To);
	l   += len3d(&TmTo);
	if     (pick==2) hwt3d_lint(tt,ll,To,t,l);
	else if(pick==1) hwt3d_tint(tt,ll,To,t,l);
	else             hwt3d_nint(tt,ll,To,t,l);

	for(it=2; it<sf_n(at); it++) {
	    t = sf_o(at) + it * sf_d(at);

	    Tp = hwt3d_raytr(vv,Tm,To,scaleray);
	    Tm = To;
	    To = Tp;

	    TmTo = vec3d(&Tm,&To);
	    l   += len3d(&TmTo); 
	    if     (pick==2) hwt3d_lint(tt,ll,To,t,l);
	    else if(pick==1) hwt3d_tint(tt,ll,To,t,l);
	    else             hwt3d_nint(tt,ll,To,t,l);

	} /* end it */	

    } /* end iray */

    /* fill holes */
    if(fill>0) hwt3d_fill(tt,fill);
    
    /* write traveltime cube */
    sf_floatwrite(tt[0][0],sf_n(az)*sf_n(ax)*sf_n(ay),Ft);
    
/*------------------------------------------------------------*/

    free(**vv); free(*vv); free(vv);
    free(**tt); free(*tt); free(tt);
    free(**ll); free(*ll); free(ll);

    exit (0);
}
Ejemplo n.º 17
0
int main(int argc, char* argv[])
{
    bool verb;

    sf_file Fi,Fs,Fr;    /* I/O files */
    sf_axis at,az,ax,aa; /* cube axes */

    int     nt,nz,nx, nhz,nhx,nht;
    int     it,iz,ix, ihz,ihx,iht;
    int     jt,jz,jx;
    int     kt,kz,kx;

    float **ii=NULL, ***us=NULL,***ur=NULL; /* arrays */

    int ompchunk; 

    int lox,hix;
    int loz,hiz;
    int lot,hit;

    float ts,tr;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;  /* OpenMP data chunk size */
    if(! sf_getbool("verb",&verb)) verb=false;         /* verbosity flag */

    if(! sf_getint("nhz",&nhz)) nhz=0;
    if(! sf_getint("nhx",&nhx)) nhx=0;
    if(! sf_getint("nht",&nht)) nht=1;
    sf_warning("nht=%d nhx=%d nhz=%d",2*nht+1,2*nhx+1,2*nhz+1);

    Fs = sf_input ("in" ); /*   source wavefield */
    Fr = sf_input ("ur" ); /* receiver wavefield */
    Fi = sf_output("out"); /* image */

    /* read axes */
    az=sf_iaxa(Fs,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax=sf_iaxa(Fs,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* position */
    at=sf_iaxa(Fs,3); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */

    /* set output axes */
    aa=sf_maxa(1,0,1); 
    sf_oaxa(Fi,aa,3);

    nz = sf_n(az);
    nx = sf_n(ax);
    nt = sf_n(at);

    /* allocate work arrays */
    us=sf_floatalloc3(nz,nx,nt);
    ur=sf_floatalloc3(nz,nx,nt);
    ii=sf_floatalloc2(nz,nx);

    /* init output */
    for (iz=0; iz<nz; iz++) {
	for (ix=0; ix<nx; ix++) {
	    ii[ix][iz]=0;
	}
    }

    sf_floatread(us[0][0],nz*nx*nt,Fs);
    sf_floatread(ur[0][0],nz*nx*nt,Fr);

    if(verb) fprintf(stderr,"   t    x\n");
    if(verb) fprintf(stderr,"%4d %4d\n",nt,nx);
    for(        it=nht; it<nt-nht; it++) { lot=-nht; hit=nht+1;
	for(    ix=nhx; ix<nx-nhx; ix++) { lox=-nhx; hix=nhx+1;
	    if(verb) fprintf(stderr,"%4d %4d",it,ix);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(iz,iht,ihx,ihz,ts,tr,loz,hiz,jt,kt,jx,kx,jz,kz) shared(ur,nz,nhz,lot,hit,lox,hix)
#endif		
	    for(iz=nhz; iz<nz-nhz; iz++) { loz=-nhz; hiz=nhz+1;
/*		ts = us[it][ix][iz]*us[it][ix][iz];*/
		ts = us[it][ix][iz];
		tr = 0;
		
		for(        iht=lot; iht<hit; iht++) { jt=it-iht; kt=it+iht;
		    for(    ihx=lox; ihx<hix; ihx++) { jx=ix-ihx; kx=ix+ihx;
			for(ihz=loz; ihz<hiz; ihz++) { jz=iz-ihz; kz=iz+ihz;
			    tr += ur[jt][jx][jz]
				* ur[kt][kx][kz];
			} /* nhz */
		    } /* nhx */
		} /* nht */
		ii[ix][iz] += ts * tr;
	    } /* nz */
	    if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
	} /* nx */
    } /* nt */
    if(verb) fprintf(stderr,"\n");

    sf_floatwrite(ii[0],nz*nx,Fi);    

    exit (0);
}
Ejemplo n.º 18
0
int main(int argc, char* argv[])
{
    bool verb,fsrf,snap,ssou,dabc,opot;
    int  jsnap,ntsnap,jdata;

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */
    sf_file Fccc=NULL; /* velocity  */
    sf_file Fden=NULL; /* density   */
    sf_file Fdat=NULL; /* data      */
    sf_file Fwfl=NULL; /* wavefield */

    /* cube axes */
    sf_axis at,ax,ay,az;
    sf_axis as,ar,ac;

    int     nt,nz,nx,ny,ns,nr,nc,nb;
    int     it,iz,ix,iy;
    float   dt,dz,dx,dy,idz,idx,idy;

    /* FDM structure */
    fdm3d    fdm=NULL;
    abcone3d /* abcp=NULL, */ abcs=NULL;
    sponge   spo=NULL;

    /* I/O arrays */
    float***ww=NULL;           /* wavelet   */
    pt3d   *ss=NULL;           /* sources   */
    pt3d   *rr=NULL;           /* receivers */
    float **dd=NULL;           /* data      */

    /*------------------------------------------------------------*/
    float ***tt=NULL;
    float ***ro=NULL;           /* density */

    /* orthorombic footprint - 9 coefficients */
    /* c11 c12 c13 
       .   c22 c23 
       .   .   c33 
                  c44
                     c55
                        c66 */
    float ***c11=NULL;
    float ***c22=NULL;
    float ***c33=NULL;
    float ***c44=NULL;
    float ***c55=NULL;
    float ***c66=NULL;
    float ***c12=NULL;
    float ***c13=NULL;
    float ***c23=NULL;
    float ***vp,***vs;
    float ***qp=NULL,***qsx=NULL,***qsy=NULL,***qsz=NULL;

    /*------------------------------------------------------------*/
    /* displacement: um = U @ t-1; uo = U @ t; up = U @ t+1 */
    float ***umz,***uoz,***upz,***uaz,***utz; 
    float ***umx,***uox,***upx,***uax,***utx;
    float ***umy,***uoy,***upy,***uay,***uty;

    /* stress/strain tensor */ 
    float ***tzz,***txx,***tyy,***txy,***tyz,***tzx;       
    float    szz,   sxx,   syy,   sxy,   syz,   szx;

    /*------------------------------------------------------------*/
    /* linear interpolation weights/indices */
    lint3d cs,cr;

    /* Gaussian bell */
    int nbell;
    
    /* wavefield cut params */
    sf_axis   acz=NULL,acx=NULL,acy=NULL;
    int       nqz,nqx,nqy;
    float     oqz,oqx,oqy;
    float     dqz,dqx,dqy;
    float     ***uc=NULL;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    /*------------------------------------------------------------*/
    /* OMP parameters */
#ifdef _OPENMP
    omp_init();
#endif
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* execution flags */
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
    if(! sf_getbool("ssou",&ssou)) ssou=false; /* stress source */
    if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */
    if(! sf_getbool("opot",&opot)) opot=false; /* output potentials */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* I/O files */
    Fwav = sf_input ("in" ); /* wavelet   */
    Fccc = sf_input ("ccc"); /* stiffness */
    Fden = sf_input ("den"); /* density   */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */
    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* axes */
    at = sf_iaxa(Fwav,3); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space x */
    ay = sf_iaxa(Fccc,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space y */

    as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */

    nt = sf_n(at); dt = sf_d(at);
    nz = sf_n(az); dz = sf_d(az);
    nx = sf_n(ax); dx = sf_d(ax);
    ny = sf_n(ay); dy = sf_d(ay);

    ns = sf_n(as);
    nr = sf_n(ar);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* other execution parameters */
    if(! sf_getint("nbell",&nbell)) nbell=5;  /* bell size */
    if(verb) sf_warning("nbell=%d",nbell);
    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC */
    if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;

    fdm=fdutil3d_init(verb,fsrf,az,ax,ay,nb,1);
    fdbell3d_init(nbell);

    sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);
    sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if(verb) sf_raxa(ay);
    /*------------------------------------------------------------*/

    /* 3D vector components */
    nc=3;
    if(opot) {
	ac=sf_maxa(nc+1,0,1);
    } else {
	ac=sf_maxa(nc  ,0,1);
    }

    /*------------------------------------------------------------*/
    /* setup output data header */
    sf_oaxa(Fdat,ar,1);
    sf_oaxa(Fdat,ac,2);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,3);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(az);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax);
	if(!sf_getint  ("nqy",&nqy)) nqy=sf_n(ay);

	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax);
	if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay);

	dqz=sf_d(az);
	dqx=sf_d(ax);
	dqy=sf_d(ay);

	acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx);
	acy = sf_maxa(nqy,oqy,dqy); sf_raxa(acy);
	/* TODO: check if the imaging window fits in the wavefield domain */

	uc=sf_floatalloc3(sf_n(acz),sf_n(acx),sf_n(acy));

	ntsnap=0;
	for(it=0; it<nt; it++) {
	    if(it%jsnap==0) ntsnap++;
	}
	sf_setn(at,  ntsnap);
	sf_setd(at,dt*jsnap);
	if(verb) sf_raxa(at);

	sf_oaxa(Fwfl,acz,1);
	sf_oaxa(Fwfl,acx,2);
	sf_oaxa(Fwfl,acy,3);
	sf_oaxa(Fwfl,ac, 4);
	sf_oaxa(Fwfl,at, 5);
    }

    /*------------------------------------------------------------*/
    /* source array */
    ww=sf_floatalloc3(ns,nc,nt); 
    sf_floatread(ww[0][0],nt*nc*ns,Fwav);

    /* data array */
    if(opot) {
	dd=sf_floatalloc2(nr,nc+1);
    } else {
	dd=sf_floatalloc2(nr,nc  );
    }

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt3d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt3d*) sf_alloc(nr,sizeof(*rr)); 

    pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */
    pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */
    
    cs = lint3d_make(ns,ss,fdm);
    cr = lint3d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* setup FD coefficients */
/*    idz = 2/dz;*/
/*    idx = 2/dx;*/
/*    idy = 2/dy;*/

    idz = 1/dz;
    idx = 1/dx;
    idy = 1/dy;

    /*------------------------------------------------------------*/ 
    tt = sf_floatalloc3(nz,nx,ny); 
    
    ro =sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    c11=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c22=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c33=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c44=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c55=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c66=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c12=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c13=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
    c23=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);     

    /* input density */
    sf_floatread(tt[0][0],nz*nx*ny,Fden);     expand3d(tt,ro ,fdm);

    /* input stiffness */
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c11,fdm);
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c22,fdm);    
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c33,fdm);    
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c44,fdm);
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c55,fdm);    
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c66,fdm);
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c12,fdm);
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c13,fdm);
    sf_floatread(tt[0][0],nz*nx*ny,Fccc );    expand3d(tt,c23,fdm);

    free(**tt); free(*tt); free(tt);

    /*------------------------------------------------------------*/
    if(dabc) {
	/* one-way abc setup   */
	vp = sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
	vs = sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); 
	for        (iy=0; iy<fdm->nypad; iy++) {
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nzpad; iz++) {
		    vp[iy][ix][iz] = sqrt( c11[iy][ix][iz]/ro[iy][ix][iz] );
		    vs[iy][ix][iz] = sqrt( c55[iy][ix][iz]/ro[iy][ix][iz] );
		}
	    }
	}
/*	abcp = abcone3d_make(NOP,dt,vp,fsrf,fdm); */
	abcs = abcone3d_make(NOP,dt,vs,fsrf,fdm);
	free(**vp); free(*vp); free(vp);
	free(**vs); free(*vs); free(vs);

	/* sponge abc setup */
	spo = sponge_make(fdm->nb);
    }

    /*------------------------------------------------------------*/
    /* precompute 1/ro * dt^2 */
    for        (iy=0; iy<fdm->nypad; iy++) {
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		ro[iy][ix][iz] = dt*dt/ro[iy][ix][iz];
	    }
	}
     }

    /*------------------------------------------------------------*/
    /* allocate wavefield arrays */
    umz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uoz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    upz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uaz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);

    umx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uox=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    upx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uax=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);

    umy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uoy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    upy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uay=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);

    tzz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    tyy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    txx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    txy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    tyz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    tzx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);

    for        (iy=0; iy<fdm->nypad; iy++) {
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		umz[iy][ix][iz]=0; umx[iy][ix][iz]=0; umy[iy][ix][iz]=0;
		uoz[iy][ix][iz]=0; uox[iy][ix][iz]=0; uoy[iy][ix][iz]=0;
		upz[iy][ix][iz]=0; upx[iy][ix][iz]=0; upy[iy][ix][iz]=0;
		uaz[iy][ix][iz]=0; uax[iy][ix][iz]=0; uay[iy][ix][iz]=0;
	    }
	}
    }

    if(opot) {
	qp =sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
	qsx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
	qsy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
	qsz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    }

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);

	/*------------------------------------------------------------*/
	/* from displacement to strain                                */
	/*------------------------------------------------------------*/	
	/* 
	 * exx = Fx(ux)
	 * eyy = Fy(uy)
	 * ezz = Fz(uz)
	 * exy = By(ux) + Bx(uy)
	 * eyz = Bz(uy) + By(uz)
	 * ezx = Bx(uz) + Bz(ux)
	 */
#ifdef _OPENMP
#pragma omp parallel for					\
    schedule(dynamic,fdm->ompchunk)				\
    private(ix,iy,iz)						\
    shared(fdm,txx,tyy,tzz,txy,tyz,tzx,uox,uoy,uoz,idx,idy,idz)
#endif
	for        (iy=NOP; iy<fdm->nypad-NOP; iy++) {
	    for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
		for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
		    
		    txx[iy][ix][iz] = Dx(uox,ix,iy,iz,idx);
		    tyy[iy][ix][iz] = Dy(uoy,ix,iy,iz,idy);
		    tzz[iy][ix][iz] = Dz(uoz,ix,iy,iz,idz);
		    
		    txy[iy][ix][iz] = Dy(uox,ix,iy,iz,idy) + Dx(uoy,ix,iy,iz,idx);
		    tyz[iy][ix][iz] = Dz(uoy,ix,iy,iz,idz) + Dy(uoz,ix,iy,iz,idy);
		    tzx[iy][ix][iz] = Dx(uoz,ix,iy,iz,idx) + Dz(uox,ix,iy,iz,idz);
		}
	    }
	}
	
	/*------------------------------------------------------------*/
	/* from strain to stress                                      */
	/*------------------------------------------------------------*/
#ifdef _OPENMP
#pragma omp parallel for						\
    schedule(dynamic,fdm->ompchunk)					\
    private(ix,iy,iz,sxx,syy,szz,sxy,syz,szx)				\
    shared(fdm,txx,tyy,tzz,txy,tyz,tzx,c11,c22,c33,c44,c55,c66,c12,c13,c23)
#endif
	for        (iy=0; iy<fdm->nypad; iy++) {
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nzpad; iz++) {
		    
		    sxx = c11[iy][ix][iz] * txx[iy][ix][iz]
			+ c12[iy][ix][iz] * tyy[iy][ix][iz]
			+ c13[iy][ix][iz] * tzz[iy][ix][iz];
		    syy = c12[iy][ix][iz] * txx[iy][ix][iz]
			+ c22[iy][ix][iz] * tyy[iy][ix][iz]
			+ c23[iy][ix][iz] * tzz[iy][ix][iz];
		    szz = c13[iy][ix][iz] * txx[iy][ix][iz]
			+ c23[iy][ix][iz] * tyy[iy][ix][iz]
			+ c33[iy][ix][iz] * tzz[iy][ix][iz];
		    
		    sxy = c66[iy][ix][iz] * txy[iy][ix][iz];
		    syz = c44[iy][ix][iz] * tyz[iy][ix][iz];
		    szx = c55[iy][ix][iz] * tzx[iy][ix][iz];
		    
		    txx[iy][ix][iz] = sxx;
		    tyy[iy][ix][iz] = syy;
		    tzz[iy][ix][iz] = szz;

		    txy[iy][ix][iz] = sxy;
		    tyz[iy][ix][iz] = syz;
		    tzx[iy][ix][iz] = szx;
		}
	    }
	}

	/*------------------------------------------------------------*/
	/* free surface */
	/*------------------------------------------------------------*/
	if(fsrf) {
#ifdef _OPENMP
#pragma omp parallel for						\
    schedule(dynamic,fdm->ompchunk)					\
    private(ix,iy,iz)							\
    shared(fdm,txx,tyy,tzz,txy,tyz,tzx)
#endif
	    for        (iy=0; iy<fdm->nypad; iy++) {
		for    (ix=0; ix<fdm->nxpad; ix++) {
		    for(iz=0; iz<fdm->nb;    iz++) {
			txx[iy][ix][iz]=0;
			tyy[iy][ix][iz]=0;
			tzz[iy][ix][iz]=0;

			txy[iy][ix][iz]=0;
			tyz[iy][ix][iz]=0;
			tzx[iy][ix][iz]=0;
		    }
		}
	    }
	}

	/*------------------------------------------------------------*/
	/* inject stress source                                       */
	/*------------------------------------------------------------*/
	if(ssou) {
	    lint3d_bell(tzz,ww[it][0],cs);
	    lint3d_bell(txx,ww[it][1],cs);
	    lint3d_bell(tyy,ww[it][2],cs);
	}
	
	/*------------------------------------------------------------*/
	/* from stress to acceleration                                */
	/*------------------------------------------------------------*/
	/* 
	 * ax = Bx(txx) + Fy(txy) + Fz(txz)
	 * ay = Fx(txy) + By(tyy) + Fz(tyz)
	 * az = Fx(txz) + Fy(tyz) + Bz(tzz)
	 */	
#ifdef _OPENMP
#pragma omp parallel for					\
    schedule(dynamic,fdm->ompchunk)				\
    private(ix,iy,iz)						\
    shared(fdm,txx,tyy,tzz,txy,tyz,tzx,uax,uay,uaz,idx,idy,idz)
#endif
	for        (iy=NOP; iy<fdm->nypad-NOP; iy++) {
	    for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
		for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {		    
		    uax[iy][ix][iz] = Dx( txx,ix,iy,iz,idx ) + Dy( txy,ix,iy,iz,idy ) + Dz( tzx,ix,iy,iz,idz ) ;
		    uay[iy][ix][iz] = Dx( txy,ix,iy,iz,idx ) + Dy( tyy,ix,iy,iz,idy ) + Dz( tyz,ix,iy,iz,idz ) ;
		    uaz[iy][ix][iz] = Dx( tzx,ix,iy,iz,idx ) + Dy( tyz,ix,iy,iz,idy ) + Dz( tzz,ix,iy,iz,idz ) ;		    
		}
	    }
	}

	/*------------------------------------------------------------*/
	/* inject acceleration source                                 */
	/*------------------------------------------------------------*/
	if(!ssou) {
	    lint3d_bell(uaz,ww[it][0],cs);
	    lint3d_bell(uax,ww[it][1],cs);
	    lint3d_bell(uay,ww[it][2],cs);
	}

	/*------------------------------------------------------------*/
	/* step forward in time                                       */
	/*------------------------------------------------------------*/
#ifdef _OPENMP
#pragma omp parallel for						\
    schedule(dynamic,fdm->ompchunk)					\
    private(ix,iy,iz)							\
    shared(fdm,uox,uoy,uoz,umx,umy,umz,upx,upy,upz,uax,uay,uaz,ro)
#endif
	for        (iy=0; iy<fdm->nypad; iy++) {
	    for    (ix=0; ix<fdm->nxpad; ix++) {
		for(iz=0; iz<fdm->nzpad; iz++) {
		    upx[iy][ix][iz] = 2*uox[iy][ix][iz] 
			-               umx[iy][ix][iz] 
			+               uax[iy][ix][iz] * ro[iy][ix][iz]; 

		    upy[iy][ix][iz] = 2*uoy[iy][ix][iz] 
			-               umy[iy][ix][iz] 
			+               uay[iy][ix][iz] * ro[iy][ix][iz]; 

		    upz[iy][ix][iz] = 2*uoz[iy][ix][iz] 
			-               umz[iy][ix][iz] 
			+               uaz[iy][ix][iz] * ro[iy][ix][iz]; 
		    
		}
	    }
	}
	/* circulate wavefield arrays */
	utz=umz; uty=umy; utx=umx;
	umz=uoz; umy=uoy; umx=uox;
	uoz=upz; uoy=upy; uox=upx;
	upz=utz; upy=uty; upx=utx;
	
	if(dabc) {
	    /* one-way ABC */
	    /* abcone3d_apply(uoz,umz,NOP,abcp,fdm); */
	    /* abcone3d_apply(uox,umx,NOP,abcp,fdm); */
	    /* abcone3d_apply(uoy,umy,NOP,abcp,fdm); */
	    
	    abcone3d_apply(uoz,umz,NOP,abcs,fdm);
	    abcone3d_apply(uox,umx,NOP,abcs,fdm);
	    abcone3d_apply(uoy,umy,NOP,abcs,fdm);

	    /* sponge ABC */
	    sponge3d_apply(umz,spo,fdm);
	    sponge3d_apply(uoz,spo,fdm);
	    
	    sponge3d_apply(umx,spo,fdm);
	    sponge3d_apply(uox,spo,fdm);

	    sponge3d_apply(umy,spo,fdm);
	    sponge3d_apply(uoy,spo,fdm);
	}	    

	/*------------------------------------------------------------*/
	/* cut wavefield and save */
	/*------------------------------------------------------------*/
	if(opot) {
		
#ifdef _OPENMP
#pragma omp parallel for			\
    schedule(dynamic,fdm->ompchunk)		\
    private(ix,iy,iz)				\
    shared(fdm,uox,uoy,uoz,idx,idy,idz)
#endif
	    for        (iy=NOP; iy<fdm->nypad-NOP; iy++) {
		for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
		    for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {	
			
			qp [iy][ix][iz] = Dx( uox,ix,iy,iz,idx )
			    +             Dy( uoy,ix,iy,iz,idy )
			    +             Dz( uoz,ix,iy,iz,idz );
			
			qsx[iy][ix][iz] = Dy( uoz,ix,iy,iz,idy ) - Dz( uoy,ix,iy,iz,idz );
			qsy[iy][ix][iz] = Dz( uox,ix,iy,iz,idz ) - Dx( uoz,ix,iy,iz,idx );
			qsz[iy][ix][iz] = Dx( uoy,ix,iy,iz,idx ) - Dy( uox,ix,iy,iz,idy );
		    }
		}
	    }

	    if(snap && it%jsnap==0) {
		cut3d(qp ,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);

		cut3d(qsz,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);

		cut3d(qsx,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);

		cut3d(qsy,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);
	    }
	    
	    lint3d_extract(qp , dd[0],cr);
	    lint3d_extract(qsx, dd[1],cr);
	    lint3d_extract(qsy, dd[2],cr);
	    lint3d_extract(qsz, dd[3],cr);
	    if(it%jdata==0) sf_floatwrite(dd[0],nr*(nc+1),Fdat);

	} else {

	    if(snap && it%jsnap==0) {
		cut3d(uoz,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);
		
		cut3d(uox,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);
		
		cut3d(uoy,uc,fdm,acz,acx,acy);
		sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl);
	    }
	    
	    lint3d_extract(uoz,dd[0],cr);
	    lint3d_extract(uox,dd[1],cr);
	    lint3d_extract(uoy,dd[2],cr);
	    if(it%jdata==0) sf_floatwrite(dd[0],nr*nc,Fdat);
	}

    }
    if(verb) fprintf(stderr,"\n");    
    
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    
    free(**ww); free(*ww); free(ww);
    free(ss);
    free(rr);
    free(*dd);  free(dd);

    free(**ro);  free(*ro);  free(ro);
    free(**c11); free(*c11); free(c11);
    free(**c22); free(*c22); free(c22);
    free(**c33); free(*c33); free(c33);
    free(**c44); free(*c44); free(c44);
    free(**c55); free(*c55); free(c55);
    free(**c66); free(*c66); free(c66);
    free(**c12); free(*c12); free(c12);
    free(**c13); free(*c13); free(c13);
    free(**c23); free(*c23); free(c23);

    free(**umz); free(*umz); free(umz);
    free(**uoz); free(*uoz); free(uoz);
    free(**upz); free(*upz); free(upz);
    free(**uaz); free(*uaz); free(uaz);

    free(**umx); free(*umx); free(umx);
    free(**uox); free(*uox); free(uox);
    free(**upx); free(*upx); free(upx);
    free(**uax); free(*uax); free(uax);

    free(**umy); free(*umy); free(umy);
    free(**uoy); free(*uoy); free(uoy);
    free(**upy); free(*upy); free(upy);
    free(**uay); free(*uay); free(uay);

    free(**tzz); free(*tzz); free(tzz);
    free(**txx); free(*txx); free(txx);
    free(**tyy); free(*tyy); free(tyy);
    free(**txy); free(*txy); free(txy);
    free(**tyz); free(*tyz); free(tyz);
    free(**tzx); free(*tzx); free(tzx);

    if (snap) {
       free(**uc);  free(*uc);  free(uc);    
    }

    if(opot) {
	free(**qp);  free(*qp);  free(qp);    
	free(**qsx); free(*qsx); free(qsx);
	free(**qsy); free(*qsy); free(qsy);
	free(**qsz); free(*qsz); free(qsz);
    }
    /*------------------------------------------------------------*/


    exit (0);
}
Ejemplo n.º 19
0
int main(int argc, char* argv[])
{
    /*------------------------------------------------------------*/
    /* Execution control, I/O files and geometry                  */
    /*------------------------------------------------------------*/
    bool verb,fsrf,snap,back,esou; /* execution flags */
    int  jsnap,ntsnap,jdata; /* jump along axes */
    int  shft; /* time shift for wavefield matching in RTM */

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fdat=NULL; /* data      */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */
    sf_file Fccc=NULL; /* velocity  */
    sf_file Frkp=NULL; /* app. rank */
    sf_file Fltp=NULL; /* left mat  */
    sf_file Frtp=NULL; /* right mat */
    sf_file Fwfp=NULL; /* wavefield */
    sf_file Frks=NULL; /* app. rank */
    sf_file Flts=NULL; /* left mat  */
    sf_file Frts=NULL; /* right mat */
    sf_file Fwfs=NULL; /* wavefield */

    /* cube axes */
    sf_axis at,ax,ay,az; /* time, x, y, z */ 
    sf_axis asx,asy,arx,ary,ac;    /* sou, rec-x, rec-y, component */ 

    /* dimension, index and interval */
    int     nt,nz,nx,ny,ns,nr,nc,nb;
    int     it,iz,ix,iy;
    float   dt,dz,dx,dy;
    int     nxyz, nk;

    /* FDM and KSP structure */ //!!!JS
    fdm3d    fdm=NULL;
    dft3d    dft=NULL;
    clr3d    clr_p=NULL, clr_s=NULL;

    /* I/O arrays for sou & rec */
    sf_complex***ww=NULL;    /* wavelet   */
    pt3d        *ss=NULL;    /* sources   */
    pt3d        *rr=NULL;    /* receivers */
    sf_complex **dd=NULL;    /* data      */

    /*------------------------------------------------------------*/
    /* displacement: uo = U @ t; up = U @ t+1                     */
    /*------------------------------------------------------------*/
    sf_complex ***uox, ***uoy, ***uoz, **uo;
    sf_complex ***uox_p, ***uoy_p, ***uoz_p, **uo_p;
    sf_complex ***uox_s, ***uoy_s, ***uoz_s, **uo_s;
    /*sf_complex ***upx, ***upy, ***upz, **up;*/

    /*------------------------------------------------------------*/
    /* lowrank decomposition arrays                               */
    /*------------------------------------------------------------*/
    int ntmp, *n2s_p, *n2s_s;
    sf_complex **lt_p, **rt_p, **lt_s, **rt_s;

    /*------------------------------------------------------------*/
    /* linear interpolation weights/indices                       */
    /*------------------------------------------------------------*/
    lint3d cs,cr; /* for injecting source and extracting data */

    /* Gaussian bell */
    int nbell;
    
    /*------------------------------------------------------------*/
    /* wavefield cut params                                       */
    /*------------------------------------------------------------*/
    sf_axis   acz=NULL,acx=NULL,acy=NULL;
    int       nqz,nqx,nqy;
    float     oqz,oqx,oqy;
    float     dqz,dqx,dqy;
    sf_complex***uc=NULL; /* tmp array for output wavefield snaps */

    /*------------------------------------------------------------*/
    /* init RSF                                                   */
    /*------------------------------------------------------------*/
    sf_init(argc,argv);

    /*------------------------------------------------------------*/
    /* OMP parameters                                             */
    /*------------------------------------------------------------*/
#ifdef _OPENMP
    omp_init();
#endif

    /*------------------------------------------------------------*/
    /* read execution flags                                       */
    /*------------------------------------------------------------*/
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
    if(! sf_getbool("back",&back)) back=false; /* backward extrapolation flag (for rtm) */
    if(! sf_getbool("esou",&esou)) esou=false; /* explosive force source */

    /*------------------------------------------------------------*/
    /* I/O files                                                  */
    /*------------------------------------------------------------*/
    Fwav = sf_input ("in" ); /* wavelet   */
    Fdat = sf_output("out"); /* data      */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */
    Fccc = sf_input ("ccc"); /* stiffness */
    Frkp = sf_input ("rkp"); /* app. rank */
    Fltp = sf_input ("ltp"); /* left mat  */
    Frtp = sf_input ("rtp"); /* right mat */
    Fwfp = sf_output("wfp"); /* wavefield */
    Frks = sf_input ("rks"); /* app. rank */
    Flts = sf_input ("lts"); /* left mat  */
    Frts = sf_input ("rts"); /* right mat */
    Fwfs = sf_output("wfs"); /* wavefield */

    /*------------------------------------------------------------*/
    /* axes                                                       */
    /*------------------------------------------------------------*/
    at = sf_iaxa(Fwav,4); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space x */
    ay = sf_iaxa(Fccc,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space y */

    asx = sf_iaxa(Fsou,2); sf_setlabel(asx,"sx"); if(verb) sf_raxa(asx); /* sources x */
    asy = sf_iaxa(Fsou,3); sf_setlabel(asy,"sy"); if(verb) sf_raxa(asy); /* sources y */
    arx = sf_iaxa(Frec,2); sf_setlabel(arx,"rx"); if(verb) sf_raxa(arx); /* receivers x */
    ary = sf_iaxa(Frec,3); sf_setlabel(ary,"ry"); if(verb) sf_raxa(ary); /* receivers y */

    nt = sf_n(at); dt = sf_d(at);
    nz = sf_n(az); dz = sf_d(az);
    nx = sf_n(ax); dx = sf_d(ax);
    ny = sf_n(ay); dy = sf_d(ay);

    ns = sf_n(asx)*sf_n(asy);
    nr = sf_n(arx)*sf_n(ary);

    /*------------------------------------------------------------*/
    /* other execution parameters                                 */
    /*------------------------------------------------------------*/
    if(! sf_getint("nbell",&nbell)) nbell=NOP;  /* bell size */
    if(verb) sf_warning("nbell=%d",nbell);
    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }
    if(back) {
        shft = (nt-1)%jsnap;
        sf_warning("For backward extrapolation, make sure nbell(%d)=0",nbell);
    } else shft = 0;

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC                     */
    /*------------------------------------------------------------*/
    if( !sf_getint("nb",&nb)) nb=NOP;

    fdm=fdutil3d_init(verb,fsrf,az,ax,ay,nb,1);
    if(nbell) fdbell3d_init(nbell);

    sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);
    sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if(verb) sf_raxa(ay);

    /*------------------------------------------------------------*/
    /* 3D vector components                                       */
    /*------------------------------------------------------------*/
    nc=3;
    ac=sf_maxa(nc,0,1); /* output 3 cartesian components */

    /*------------------------------------------------------------*/
    /* setup output data header                                   */
    /*------------------------------------------------------------*/
    sf_settype(Fdat,SF_COMPLEX);
    sf_oaxa(Fdat,arx,1);
    sf_oaxa(Fdat,ary,2);
    sf_oaxa(Fdat,ac,3);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,4);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(az);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax);
	if(!sf_getint  ("nqy",&nqy)) nqy=sf_n(ay);

	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax);
	if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay);

	dqz=sf_d(az);
	dqx=sf_d(ax);
	dqy=sf_d(ay);

	acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx);
	acy = sf_maxa(nqy,oqy,dqy); sf_raxa(acy);

	uc=sf_complexalloc3(sf_n(acz),sf_n(acx),sf_n(acy));

	ntsnap=0; /* ntsnap = it/jsnap+1; */
	for(it=0; it<nt; it++) {
	    if(it%jsnap==0) ntsnap++;
	}
	sf_setn(at,  ntsnap);
	sf_setd(at,dt*jsnap);
	if(verb) sf_raxa(at);

        sf_settype(Fwfp,SF_COMPLEX);
	sf_oaxa(Fwfp,acz,1);
	sf_oaxa(Fwfp,acx,2);
	sf_oaxa(Fwfp,acy,3);
	sf_oaxa(Fwfp,ac, 4);
	sf_oaxa(Fwfp,at, 5);

        sf_settype(Fwfs,SF_COMPLEX);
	sf_oaxa(Fwfs,acz,1);
	sf_oaxa(Fwfs,acx,2);
	sf_oaxa(Fwfs,acy,3);
	sf_oaxa(Fwfs,ac, 4);
	sf_oaxa(Fwfs,at, 5);
    }

    /*------------------------------------------------------------*/
    /* source and data array                                      */
    /*------------------------------------------------------------*/
    ww=sf_complexalloc3(ns,nc,nt); /* Fast axis: n_sou > n_comp > n_time */
    sf_complexread(ww[0][0],nt*nc*ns,Fwav);

    dd=sf_complexalloc2(nr,nc);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates                          */
    /*------------------------------------------------------------*/
    ss = (pt3d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt3d*) sf_alloc(nr,sizeof(*rr)); 

    pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */
    pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */

    /* calculate 3d linear interpolation coef for sou & rec */
    cs = lint3d_make(ns,ss,fdm);
    cr = lint3d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* allocate and initialize wavefield arrays                   */
    /*------------------------------------------------------------*/
    /* z-component */
    uoz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uoz_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uoz_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    /*upz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/

    /* x-component */
    uox=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uox_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uox_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    /*upx=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/

    /* y-component */
    uoy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uoy_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    uoy_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);
    /*upy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/

    /* wavefield vector */
    uo = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));
    uo[0] = uox[0][0]; uo[1] = uoy[0][0]; uo[2] = uoz[0][0];
    uo_p = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));
    uo_p[0] = uox_p[0][0]; uo_p[1] = uoy_p[0][0]; uo_p[2] = uoz_p[0][0];
    uo_s = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));
    uo_s[0] = uox_s[0][0]; uo_s[1] = uoy_s[0][0]; uo_s[2] = uoz_s[0][0];
    /*up = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));*/
    /*up[0] = upx[0][0]; up[1] = upy[0][0]; up[2] = upz[0][0];*/

    /* initialize fft and lrk */
    dft = dft3d_init(1,false,false,fdm);
    nxyz= fdm->nypad*fdm->nxpad*fdm->nzpad;
    nk  = dft->nky  *dft->nkx  *dft->nkz;

    /*------------------------------------------------------------*/ 
    /* allocation I/O arrays                                      */
    /*------------------------------------------------------------*/ 
    n2s_p = sf_intalloc(9);
    sf_intread(n2s_p,9,Frkp);
    clr_p = clr3d_make2(n2s_p,fdm);

    n2s_s = sf_intalloc(9);
    sf_intread(n2s_s,9,Frks);
    clr_s = clr3d_make2(n2s_s,fdm);

    if (clr_p->n2_max > clr_s->n2_max) clr3d_init(fdm,dft,clr_p);
    else clr3d_init(fdm,dft,clr_s);

    /* check the dimension */
    if (!sf_histint(Fltp,"n1",&ntmp) || ntmp != nxyz)          sf_error("Need n1=%d in left",nxyz);
    if (!sf_histint(Fltp,"n2",&ntmp) || ntmp != clr_p->n2_sum) sf_error("Need n2=%d in left",clr_p->n2_sum);
    if (!sf_histint(Frtp,"n1",&ntmp) || ntmp != nk)            sf_error("Need n1=%d in right",nk);
    if (!sf_histint(Frtp,"n2",&ntmp) || ntmp != clr_p->n2_sum) sf_error("Need n2=%d in right",clr_p->n2_sum);
  
    lt_p = sf_complexalloc2(nxyz,clr_p->n2_sum); 
    rt_p = sf_complexalloc2(nk  ,clr_p->n2_sum); 
    sf_complexread(lt_p[0],nxyz*clr_p->n2_sum,Fltp);
    sf_complexread(rt_p[0],nk  *clr_p->n2_sum,Frtp);

    if (!sf_histint(Flts,"n1",&ntmp) || ntmp != nxyz)          sf_error("Need n1=%d in left",nxyz);
    if (!sf_histint(Flts,"n2",&ntmp) || ntmp != clr_s->n2_sum) sf_error("Need n2=%d in left",clr_s->n2_sum);
    if (!sf_histint(Frts,"n1",&ntmp) || ntmp != nk)            sf_error("Need n1=%d in right",nk);
    if (!sf_histint(Frts,"n2",&ntmp) || ntmp != clr_s->n2_sum) sf_error("Need n2=%d in right",clr_s->n2_sum);

    lt_s = sf_complexalloc2(nxyz,clr_s->n2_sum); 
    rt_s = sf_complexalloc2(nk  ,clr_s->n2_sum); 
    sf_complexread(lt_s[0],nxyz*clr_s->n2_sum,Flts);
    sf_complexread(rt_s[0],nk  *clr_s->n2_sum,Frts);

    /* initialize to zero */
#ifdef _OPENMP
#pragma omp parallel for              \
    schedule(dynamic,1)               \
    private(iy,ix,iz)                 \
    shared(fdm,uoz,uox,uoy,uoz_p,uox_p,uoy_p,uoz_s,uox_s,uoy_s)
#endif
    for        (iy=0; iy<fdm->nypad; iy++) {
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
		uoz  [iy][ix][iz]=sf_cmplx(0.,0.); uox  [iy][ix][iz]=sf_cmplx(0.,0.); uoy  [iy][ix][iz]=sf_cmplx(0.,0.);
		uoz_p[iy][ix][iz]=sf_cmplx(0.,0.); uox_p[iy][ix][iz]=sf_cmplx(0.,0.); uoy_p[iy][ix][iz]=sf_cmplx(0.,0.);
		uoz_s[iy][ix][iz]=sf_cmplx(0.,0.); uox_s[iy][ix][iz]=sf_cmplx(0.,0.); uoy_s[iy][ix][iz]=sf_cmplx(0.,0.);
		/*upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);*/
	    }
	}
    }

    /*------------------------------------------------------------*/ 
    /*------------------------ MAIN LOOP -------------------------*/ 
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
        if(verb) sf_warning("it=%d/%d;",it,nt); /*fprintf(stderr,"\b\b\b\b\b%d",it);*/

	/*------------------------------------------------------------*/
	/* apply lowrank matrix to wavefield vector                   */
        /*------------------------------------------------------------*/
        clr3d_apply(uo_p, uo, lt_p, rt_p, fdm, dft, clr_p);
        clr3d_apply(uo_s, uo, lt_s, rt_s, fdm, dft, clr_s);

	/*------------------------------------------------------------*/
        /* combine P and S wave modes                                 */
        /*------------------------------------------------------------*/
#ifdef _OPENMP
#pragma omp parallel for              \
        schedule(dynamic,1)               \
        private(iy,ix,iz)                 \
        shared(fdm,uoz,uox,uoy,uoz_p,uox_p,uoy_p,uoz_s,uox_s,uoy_s)
#endif
        for        (iy=0; iy<fdm->nypad; iy++) {
            for    (ix=0; ix<fdm->nxpad; ix++) {
                for(iz=0; iz<fdm->nzpad; iz++) {
                    uoz[iy][ix][iz] = uoz_p[iy][ix][iz] + uoz_s[iy][ix][iz];
                    uox[iy][ix][iz] = uox_p[iy][ix][iz] + uox_s[iy][ix][iz];
                    uoy[iy][ix][iz] = uoy_p[iy][ix][iz] + uoy_s[iy][ix][iz];
                    /*upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);*/
                }
            }
        }

	/*------------------------------------------------------------*/
	/* free surface */
	/*------------------------------------------------------------*/
	if(fsrf) { /* need to do something here */ }

        /*------------------------------------------------------------*/
	/* inject displacement source                                 */
	/*------------------------------------------------------------*/
        if(esou) {
            /* exploding force source */
            lint3d_expl_complex(uoz,uox,uoy,ww[it],cs);
        } else {
            if(nbell) {
                lint3d_bell_complex(uoz,ww[it][0],cs);
                lint3d_bell_complex(uox,ww[it][1],cs);
                lint3d_bell_complex(uoy,ww[it][2],cs);
            } else {
                lint3d_inject_complex(uoz,ww[it][0],cs);
                lint3d_inject_complex(uox,ww[it][1],cs);
                lint3d_inject_complex(uoy,ww[it][2],cs);
            }
        }

	/*------------------------------------------------------------*/
	/* cut wavefield and save */
	/*------------------------------------------------------------*/
        if(snap && (it-shft)%jsnap==0) {
            /* P wave */
            cut3d_complex(uoz_p,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp);

            cut3d_complex(uox_p,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp);

            cut3d_complex(uoy_p,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp);

            /* S wave */
            cut3d_complex(uoz_s,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs);

            cut3d_complex(uox_s,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs);

            cut3d_complex(uoy_s,uc,fdm,acz,acx,acy);
            sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs);
        }

        lint3d_extract_complex(uoz,dd[0],cr);
        lint3d_extract_complex(uox,dd[1],cr);
        lint3d_extract_complex(uoy,dd[2],cr);
        if(it%jdata==0) sf_complexwrite(dd[0],nr*nc,Fdat);

    }
    if(verb) sf_warning(".");
    if(verb) fprintf(stderr,"\n");    
    
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    
    free(dft);
    dft3d_finalize();
    free(clr_p); free(clr_s);
    clr3d_finalize();

    free(**ww); free(*ww); free(ww);
    free(ss);
    free(rr);
    free(*dd);  free(dd);

    free(n2s_p); free(n2s_s);
    free(*lt_p); free(lt_p); free(*rt_p); free(rt_p);
    free(*lt_s); free(lt_s); free(*rt_s); free(rt_s);

    free(**uoz  ); free(*uoz  ); free(uoz)  ;
    free(**uoz_p); free(*uoz_p); free(uoz_p);
    free(**uoz_s); free(*uoz_s); free(uoz_s);
    /*free(**upz); free(*upz); free(upz);*/
    free(uo); free(uo_p); free(uo_s);
    /*free(up);*/

    if (snap) {
       free(**uc);  free(*uc);  free(uc);    
    }

    /*------------------------------------------------------------*/


    exit (0);
}
Ejemplo n.º 20
0
int main(int argc, char* argv[])
{

	bool verb,conj,twin,pandq,Gtot,Htot;
	
    /* OMP parameters */
	#ifdef _OPENMP
    int ompnth;
	#endif 	
	
	float	*pplus0, *pplus, *pplusinv, *pplustemp, *Pplus, *Pplus_trace, *pminus, *Pminus, *Refl, *Gp, *Gm, *G, *H;
	float	*qplus, *qplusinv, *qplustemp, *Qplus, *Qplus_trace, *qminus, *Qminus;
	float	*window, *taper, pi;
	int		*tw;

    /* I/O files */
    sf_file Fplus;
    sf_file FRefl;
    sf_file FGp;
    sf_file FGm;
    sf_file FG;
    sf_file FH;
    sf_file Ftwin;
    sf_file Fp;
    sf_file Fq;

	char *filename1, filename2[256], filename3[256];
	
	/* Cube axes */
    sf_axis at,af,ax;

    int     nt,nf,ntr,mode,nshots,niter,len;
    int     i,it,ix,ishot,iter,i0;
	int		twc, twa, shift, n[2], rect[2], s[2];
    float   scale,eps,dt,df,dx,ot,of,a,b,c,d,e,f;

	sf_triangle tr;

    /*------------------------------------------------------------*/
    /* Initialize RSF parameters 								  */
    /*------------------------------------------------------------*/
    sf_init(argc,argv);	
	
    /*------------------------------------------------------------*/
    /* Initialize OMP parameters */
    /*------------------------------------------------------------*/
	#ifdef _OPENMP
    ompnth=omp_init();
	#endif	

	/*------------------------------------------------------------*/
	/* Flags 													  */
	/*------------------------------------------------------------*/
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("conj",&conj)) conj=false; /* complex conjugation (time-reversal) flag */
    if(! sf_getbool("twin",&twin)) twin=false; /* returns the timewindow as one of the outputs */
    if(! sf_getbool("pandq",&pandq)) pandq=false; /* pandq=true: returns p and q */
    if(! sf_getbool("Gtot",&Gtot)) Gtot=false; /* Gtot=true: returns G=Gp+Gm */
    if(! sf_getbool("Htot",&Htot)) Htot=false; /* Htot=true: returns H=Gp-Gm */
    if(! sf_getint("niter",&niter)) niter=1; /* number of iterations */
    if(! sf_getint("nshots",&nshots)) nshots=1; /* number of shots */
    if(! sf_getfloat("scale",&scale)) scale=1.0; /* scale factor */
	if(! sf_getfloat("eps",&eps)) eps=1e-4; /* threshold for the timewindow */
	if(! sf_getint("shift",&shift)) shift=5; /* shift in samples for the timewindow */
    
	if (verb) {
		fprintf(stderr,"This program was called with \"%s\".\n",argv[0]);
		/*fprintf(stderr,"Nr: %d Nx: %d Nt:%d\n",nr,nx,nt);*/
		
		if (argc > 1) {
			for (i = 1; i<argc; i++) {
				fprintf(stderr,"argv[%d] = %s\n", i, argv[i]);
			}
		}
		else {
			fprintf(stderr,"The command had no other arguments.\n");
    	}	
	}

    /*------------------------------------------------------------*/
    /* I/O files 												  */
    /*------------------------------------------------------------*/	
	/* "in" is the transposed version of p00plus_xxxx_xxxx.rsf
	   Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=ntr,n2=nt
	   Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=nt,n2=ntr */
	Fplus = sf_input("in");
	
	/* refl is REFL_000.rsf
	   It is used to read nf, df, of
	  Dimensions are: n1=nf,n2=ntr */
	/*FRefl = (sf_file)sf_alloc(1,sizeof(sf_file));*/	
	FRefl = sf_input("refl");

	FGp = sf_output("out");
	FGm = sf_output("Gm");
	
	if (Gtot) {
		FG  = sf_output("G");
	}
	
	if (Htot) {
		FH  = sf_output("H");
	}
	
	if (pandq) {
		Fp = sf_output("p");
		Fq = sf_output("q");
	}
	
	if (twin) {
		Ftwin = sf_output("window"); /* time window */
	}
	
	/*------------------------------------------------------------*/
	/* Axes */
	/*------------------------------------------------------------*/    
	at = sf_iaxa(Fplus,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */
	af = sf_iaxa(FRefl,1); sf_setlabel(af,"Frequency"); if(verb) sf_raxa(af); /* frequency */
	ax = sf_iaxa(Fplus,2); sf_setlabel(ax,"r"); if(verb) sf_raxa(ax); /* space */
    
	nt = sf_n(at); dt = sf_d(at); ot = sf_o(at);
	nf = sf_n(af); df = sf_d(af); of = sf_o(af);
	ntr = sf_n(ax); dx = sf_d(ax);
	
	if (verb) fprintf(stderr,"nt: %d nf: %d ntr:%d\n",nt,nf,ntr);

	sf_fileclose(FRefl);

    /*------------------------------------------------------------*/
    /* Setup output data and wavefield header					  */
    /*------------------------------------------------------------*/
	sf_oaxa(FGp,at,1);
	sf_oaxa(FGp,ax,2);
	sf_oaxa(FGm,at,1);
	sf_oaxa(FGm,ax,2);
	if (Gtot) {
		sf_oaxa(FG,at,1);
		sf_oaxa(FG,ax,2);
	}
	
	if (Htot) {
		sf_oaxa(FH,at,1);
		sf_oaxa(FH,ax,2);
	}
	
	if (pandq) {
		sf_oaxa(Fp,at,1);
		sf_oaxa(Fp,ax,2);
		sf_oaxa(Fq,at,1);
		sf_oaxa(Fq,ax,2);
	}

	if (twin) {
		sf_oaxa(Ftwin,at,1);
		sf_oaxa(Ftwin,ax,2);
	}

    /*------------------------------------------------------------*/
    /* Allocate arrays											  */
    /*------------------------------------------------------------*/
	
	/* Downgoing wavefields - Time */
	pplus0 = (float *)calloc(nt*ntr,sizeof(float));
	sf_floatread(pplus0,nt*ntr,Fplus);
	pplus = (float *)calloc(nt*ntr,sizeof(float));
	memcpy(pplus,pplus0,nt*ntr*sizeof(float));
	pplustemp = (float *)calloc(nt*ntr,sizeof(float));
	pplusinv = (float *)calloc(nt*ntr,sizeof(float));
	qplus = (float *)calloc(nt*ntr,sizeof(float));
	qplustemp = (float *)calloc(nt*ntr,sizeof(float));

	/* Downgoing wavefields - Frequency */
	Pplus = (float *)calloc(2*nf*ntr,sizeof(float));
	Qplus = (float *)calloc(2*nf*ntr,sizeof(float));

	/* The three flags of fft1 are: inv, sym, and opt */
	fft1(pplus0,Pplus,Fplus,0,0,1);
	memcpy(Qplus,Pplus,2*nf*ntr*sizeof(float));
	
	/* Upgoing wavefields - Time */
	pminus = (float *)calloc(nt*ntr,sizeof(float));
	qminus = (float *)calloc(nt*ntr,sizeof(float));

	/* Downgoing wavefields - Frequency */
	Pminus = (float *)calloc(2*nf*ntr,sizeof(float));
	Qminus = (float *)calloc(2*nf*ntr,sizeof(float));

	/* This is currently NOT used */
	/* Transpose of p00plus_xxxx_xxxx */
	for (ix=0; ix<ntr; ix++) {
		for (it=0; it<nt; it++) {
			pplusinv[ix*ntr+it]=pplus0[it*ntr+ix];
		}
	}

	/* Single trace (in frequency) of the downgoing wavefield */
	Pplus_trace = (float *)calloc(2*nf,sizeof(float));
	Qplus_trace = (float *)calloc(2*nf,sizeof(float));
	
	/* Output wavefields */
	Gp = (float *)calloc(nt*ntr,sizeof(float));
	Gm = (float *)calloc(nt*ntr,sizeof(float));
	if (Gtot) {
		G = (float *)calloc(nt*ntr,sizeof(float));
	}
	
	if (Htot) {
		H = (float *)calloc(nt*ntr,sizeof(float));
	}
	
	/* Time-reversal flag */
	if (conj) {
		mode = -1;
	}
	else {
		mode = +1;
	}
    
	/* Load the reflection response into the memory */
	if (verb) fprintf(stderr,"Before loading R %d\n",2*nf*ntr);
	Refl = (float *)calloc(2*nf*ntr*nshots,sizeof(float));
	
	/* Read REFL_000.rsf */
	filename1 = sf_getstring("refl");
	/* 000.rsf are 7 characters */
	len = strlen(filename1)-7;
	/* copy the filename without 000.rsf */
	strncpy(filename2,filename1,len);
	filename2[len] = '\0';
	if (verb) fprintf(stderr,"filename2 is: %s and len is: %d\n",filename2,len);
	/*if (NULL == filename1) {
		fprintf(stderr,"Cannot read header file %s",filename1);
	}*/
  
	for (ishot=0; ishot<nshots; ishot++) {
		
	  	/* write xxx.rsf in the string filename3 */
		sprintf(filename3,"%03d.rsf",ishot);
		for (i=0; i<7; i++)
			filename2[len+i] = filename3[i];
			filename2[len+7] = '\0';
		if (verb) fprintf(stderr,"Loading %s in memory\n",filename2);
	  	FRefl = sf_input(filename2);
    	sf_floatread(&Refl[ishot*2*nf*ntr],2*nf*ntr,FRefl);
		sf_fileclose(FRefl);
		/*if (verb) fprintf(stderr,"Iteration %d\n",ishot);*/
	}

	/* Build time-window */
	tw = (int *)calloc(ntr,sizeof(int));
	window = (float *)calloc(nt*ntr,sizeof(float));
	/*memset(window,0,nt*ntr*sizeof(float));*/
    /* I am not sure why I set it to this value */
	/*for (ix=0; ix<ntr; ix++) {
		tw[ix] = nt*dt+ot+0.15; 
	}*/
	
	if (verb) fprintf(stderr,"Build the time-window\n");
	for (ix=0; ix<ntr; ix++) {
		for (it=0; it<nt; it++) {
			if (pplus0[it+ix*nt]>eps) {
				/*tw[ix] = it*dt+ot;*/
				tw[ix] = it;
				/*if (verb) fprintf(stderr,"%d %d\n",ix,it);*/
				break;
			}
		}
	}
	for (ix=0; ix<ntr; ix++) {
		twc = (int)(tw[ix]-shift);
		twa = (int)(-twc+shift+nt);
		/*if (verb) fprintf(stderr,"%d %d\n",twc,twa);*/
		for (it=0; it<nt; it++) {
			if ((it<twc) || (it>twa)) {
				window[it+ix*nt] = 1.0;
			}
		}
	}

	/* Smoothing of the window */
	/* Should I implement flags for rect and iter? */
	/* Look at Msmooth.c to understand below */
	n[0] = nt;
	n[1] = ntr;
	s[0] = 1;
	s[1] = nt;
	rect[0] = 5;
	rect[1] = 5;

	for (ix=0; ix <= 1; ix++) {
		if (rect[ix] <= 1) continue;
		tr = sf_triangle_init (rect[ix],n[ix],false);
		for (it=0; it < (nt*ntr/n[ix]); it++) {
			i0 = sf_first_index (ix,it,1+1,n,s);
			for (iter=0; iter < 2; iter++) {
				sf_smooth2 (tr,i0,s[ix],false,window);
			}
		}
		sf_triangle_close(tr);
	}
	
	/* Tapering */
	pi = 4.0*atan(1.0);
	/*fprintf(stderr,"pi: %f\n",pi);
	fprintf(stderr,"ntr: %d\n",ntr);*/
	
	taper = (float *)calloc(ntr,sizeof(float));
	memset(taper,0,ntr*sizeof(float));

		
	for (ix=0; ix<151; ix++) {
		taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/300)));
		taper[ntr-ix-1] = taper[ix];
	}
	/*for (ix=(ntr-1); ix>(701-151-1); ix--) {
		taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/300)));

	}*/
	for (ix=151; ix<(ntr-151); ix++) {
		taper[ix] = 1.0;
	}
	/*for (ix=0; ix<ntr; ix++) {
		fprintf(stderr,"taper[%d]: %f\n",ix,taper[ix]);
	}*/
	
	FRefl = sf_input("refl");

	/*------------------------------------------------------------*/
	/* Loop over iterations */
	/*------------------------------------------------------------*/
	if (verb) fprintf(stderr,"Beginning of loop over iterations\n");
    for (iter=0; iter<niter; iter++) {
	
	/* Set Pminus and Qminus to 0 */
	memset(Pminus,0,2*nf*ntr*sizeof(float));
	memset(Qminus,0,2*nf*ntr*sizeof(float));

		/*------------------------------------------------------------*/
		/* Loop over shot positions */
		/*------------------------------------------------------------*/
		if (verb) fprintf(stderr,"Beginning of loop over shot positions\n");
		for (ishot=0; ishot<nshots; ishot++) {
   	
			/* Loop over receivers (traces) */
			#ifdef _OPENMP
			#pragma omp parallel for private(ix,it,a,b,c,d,e,f) \
				shared(Pminus,Qminus,Pplus,Qplus,taper,Refl)
			#endif 	
		  	for (ix=0; ix<ntr; ix++) {
				/* Loop over frequencies */
				#pragma ivdep
				for (it=0; it<2*nf; it=it+2) {
					
					/*(x + yi)(u + vi) = (xu - yv) + (xv + yu)i*/
					a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot];
					b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot];
					c = Pplus[ishot*2*nf+it];
					d = Pplus[ishot*2*nf+it+1];
					e = Qplus[ishot*2*nf+it];
					f = Qplus[ishot*2*nf+it+1];

					Pminus[ix*2*nf+it]   += a*c - mode*b*d;
					Pminus[ix*2*nf+it+1] += mode*a*d + b*c;
					
					Qminus[ix*2*nf+it]   += a*e - mode*b*f;
					Qminus[ix*2*nf+it+1] += mode*a*f + b*e;
				
				} /* End of loop over frequencies */	
			} /* End of loop over receivers (traces) */
			
			if (verb) if(ishot%50==0) fprintf(stderr,"Trace %d\n",ishot);

		} /* End of loop over shot positions */

		/* Save a copy of pplus and qplus before creating their next iteration */
		memcpy(pplustemp,pplus,nt*ntr*sizeof(float));
		memcpy(qplustemp,qplus,nt*ntr*sizeof(float));

		/* Build the next iteration of Pplus and Qplus */
		fft1(Pminus,pminus,FRefl,1,0,1);
		fft1(Qminus,qminus,FRefl,1,0,1);
		
		if (verb) fprintf(stderr,"Build the next iteration of pplus and qplus\n");
		#ifdef _OPENMP
		#pragma omp parallel for private(ix,it) \
			shared(pminus,qminus,pplus,qplus,pplus0,window)
		#endif
		for (ix=0; ix<ntr; ix++) {
			#pragma ivdep
			for (it=0; it<nt; it++) {
				pplus[it+ix*nt] = pplus0[it+ix*nt] - scale*window[it+ix*nt]*pminus[it+ix*nt];
				qplus[it+ix*nt] = pplus0[it+ix*nt] + scale*window[it+ix*nt]*qminus[it+ix*nt];
			}	
		}
		
		fft1(pplus,Pplus,Fplus,0,0,1);
		fft1(qplus,Qplus,Fplus,0,0,1);

		if (verb) fprintf(stderr,"%d %d\n",ix,it);

		if(iter%10==0) fprintf(stderr,"Iteration %d\n",iter);

	} /* End of loop over iterations */ 
	
	/* Build Gp and Gm */
	if (verb) fprintf(stderr,"Build Gp and Gm\n");
	#ifdef _OPENMP
	#pragma omp parallel for private(ix,it) \
		shared(Gp,Gm,G,H,pminus,qminus,pplustemp,qplustemp,pplus0)
	#endif
	for (ix=0; ix<ntr; ix++) {
		#pragma ivdep
		for (it=0; it<nt; it++) {
			Gp[it+ix*nt] = 0.5*( pplustemp[it+ix*nt] + scale*pminus[it+ix*nt] + qplustemp[it+ix*nt] - scale*qminus[it+ix*nt] );
			Gm[it+ix*nt] = 0.5*( pplustemp[it+ix*nt] + scale*pminus[it+ix*nt] - qplustemp[it+ix*nt] + scale*qminus[it+ix*nt] );
			if (Gtot) {
				G[it+ix*nt] = pplustemp[it+ix*nt] + scale*pminus[it+ix*nt];
			}
			if (Htot) {
				H[it+ix*nt] = qplustemp[it+ix*nt] - scale*qminus[it+ix*nt];
			}
			/*p[it+ix*nt] = 1.0*( pplus[it+ix*nt] + scale*pminus[it+ix*nt] );
			q[it+ix*nt] = 1.0*( qplus[it+ix*nt] - scale*qminus[it+ix*nt] );*/
		}	
	}

	/* Write the final result */
    /*FRefl = sf_input(argv[1]);*/
	/*fft1(Gp,,FRefl,1,0,0);
	fft1(Gm,,FRefl,1,0,0);*/
	
	sf_floatwrite(Gp,nt*ntr,FGp);
	sf_floatwrite(Gm,nt*ntr,FGm);
	if (Gtot) {
		sf_floatwrite(G,nt*ntr,FG);
		sf_fileclose(FG);
		free(G);
	}
	
	if (Htot) {
		sf_floatwrite(H,nt*ntr,FH);
		sf_fileclose(FH);
		free(H);
	}

	if (pandq) {
		sf_floatwrite(pplustemp,nt*ntr,Fp);
		sf_floatwrite(pminus,nt*ntr,Fq);
		sf_fileclose(Fp);
		sf_fileclose(Fq);
	}
	
	if (twin) {
		sf_floatwrite(window,nt*ntr,Ftwin);
		sf_fileclose(Ftwin);
	}
	sf_fileclose(Fplus);
	sf_fileclose(FRefl);
	sf_fileclose(FGp);
	sf_fileclose(FGm);
	
	free(Gp);
	free(Gm);
	free(pplus);
	free(pplusinv);
	free(pplustemp);
	free(Pplus);
	free(Pplus_trace);
	free(Pminus);
	free(qplus);
	free(qplustemp);
	free(Qplus);
	free(Qplus_trace);
	free(Qminus);
	free(Refl);
	free(window);
	free(tw);
	free(filename1);
	
    exit (0);
}
Ejemplo n.º 21
0
int main (int argc, char ** argv)
{
   /* RSF variables */
   bool verb;
   float *dat=NULL, **datf=NULL, **image=NULL, **tr=NULL, **ts=NULL, **trs=NULL;
   float **px=NULL, **pz=NULL;
   sf_file Fin=NULL, Fout=NULL, Ftt=NULL;
   sf_axis a1,a2,a1t,a2t,a3t,ax,az;
   int     nf,ntaper,i1,i2,i2t,itt;
   float   fmax,df,theta,dtheta,tg,tgtap,tmin,xs,xr,eps;
   aalias  aa;
   fslice  tabtt=NULL;
   int     n1,n2,n1t,n2t,n3t,nx,nz;
   float   o1,d1,o2,d2,o1t,d1t,o2t,d2t,o3t,d3t,ox,oz,dx,dz;

   sf_init(argc,argv);

   Fin  = sf_input  ("in" );
   Fout = sf_output ("out" );
   Ftt  = sf_input  ("ttfile" );

   /* axes */
   a1  = sf_iaxa(Fin,1);   /* data */
   a2  = sf_iaxa(Fin,2);   /* data */
   a1t = sf_iaxa(Ftt,1);   /* traveltime */
   a2t = sf_iaxa(Ftt,2);   /* traveltime */
   a3t = sf_iaxa(Ftt,3);   /* traveltime */

   o1  = sf_o(a1);  n1   = sf_n(a1);  d1  = sf_d(a1);
   o2  = sf_o(a2);  n2   = sf_n(a2);  d2  = sf_d(a2);
   o1t = sf_o(a1t); n1t  = sf_n(a1t); d1t = sf_d(a1t);
   o2t = sf_o(a2t); n2t  = sf_n(a2t); d2t = sf_d(a2t);
   o3t = sf_o(a3t); n3t  = sf_n(a3t); d3t = sf_d(a3t);

   /* migration parameters */
   if(! sf_getbool("verb",&verb))     verb = false; /* verbosity flag */
   if(! sf_getfloat("theta",&theta))  theta = 30.;  /* maximum dip */
   if(! sf_getfloat("dtheta",&dtheta))  dtheta = theta/3;  /* taper zone */
   if(dtheta>theta) dtheta=theta;
   if(! sf_getfloat("df",&df))        df = 5.;    /* anti-aliasing sampling */
   if(!sf_getfloat("fmax",&fmax)) fmax=.5/d1;
   if(fmax>(.5/d1)) fmax=.5/d1;
   if (!sf_getint("ntaper",&ntaper)) ntaper=11;
   if(!sf_getfloat("tmin",&tmin)) tmin=3*d1;
   if(!sf_getfloat("xs",&xs)) sf_error("missing xs parameter\n");

   /* image parameters */
   if (!sf_getint("nx",&nx))   nx=n2t;
   if(!sf_getfloat("ox",&ox))  ox=o2t;
   if(!sf_getfloat("dx",&dx))  dx=d2t;
   if (!sf_getint("nz",&nz))   nz=n1t;
   if(!sf_getfloat("oz",&oz))  oz=o1t;
   if(!sf_getfloat("dz",&dz))  dz=d1t;

   /* checking dimensions */
   if((dx!=d2t)||(dz!=d1t)) 
     sf_error("sampling interval have to be the same in:\n"
	      " image and traveltime file\n");
   if(ox<o2t) ox=o2t; 
   if(oz<o1t) oz=o1t;
   if((ox+(nx-1)*dx)>(o2t+(n2t-1)*d2t)) nx=floor(((o2t+(n2t-1)*d2t)-ox)/dx)+1;
   if((oz+(nz-1)*dz)>(o1t+(n1t-1)*d1t)) nz=floor(((o1t+(n1t-1)*d1t)-oz)/dz)+1;

   /* output axis */
   ax = sf_maxa(nx,ox,dx); if(verb) sf_raxa(ax);
   az = sf_maxa(nz,oz,dz); if(verb) sf_raxa(az);
   sf_oaxa(Fout,az,1);
   sf_oaxa(Fout,ax,2);

   /* anti-aliasing */
   /* df = fmax; */
   nf = initAalias(-1,verb,fmax,df,n1,d1,&aa);
   /* fprintf(stderr,"forcing nf=%d df=%f\n",nf,df); */

   /* aperture angle */
   tg    = tan(SF_PI*theta/180);
   tgtap = tan(SF_PI*(theta-dtheta)/180);
   if(verb) sf_warning("tgmax=%f tgtap=%f",tg,tgtap);

   /* allocating */
   dat   = sf_floatalloc(n1);
   image = sf_floatalloc2(nz,nx);
   datf  = sf_floatalloc2 (n1,nf);
   ts    = sf_floatalloc2(n1t,n2t);
   tr    = sf_floatalloc2(n1t,n2t);
   trs   = sf_floatalloc2(n1t,n2t);
   px    = sf_floatalloc2(n1t,n2t);
   pz    = sf_floatalloc2(n1t,n2t);

   if(verb) sf_warning("initializing traveltime loading");
   /* initializing traveltime maps */
   tabtt = fslice_init(n1t*n2t,n3t,sizeof(float));
   fslice_load(Ftt,tabtt,SF_FLOAT);
   if(verb) sf_warning("traveltime loading has finished");

   /* reading the source-slice from traveltime table */
   eps = .01*d2;
   itt = floor((xs+eps-o3t)/d3t);
   fslice_get(tabtt,itt,ts[0]);
   if(verb) sf_warning("traveltime table from source was read");

   for(i2=0;i2<n2;i2++) {

     sf_floatread(dat,n1,Fin);
     xr = o2+i2*d2;

     if((xr>=o3t)&&(xr<(o3t+n3t*d3t))) {

       for (i1=n1-ntaper;i1<n1;i1++) dat[i1]=(n1-i1-1)*dat[i1]/ntaper;

       loadBank(aa,dat,datf);

       /* reading the receiver-slice of traveltime table */
       /* itt = floor((o2-o2t)/d2)+i2; */
       itt = floor((xr+eps-o3t)/d3t);
       fslice_get(tabtt,itt,tr[0]);

       for(i2t=0;i2t<n2t;i2t++)
         for(i1=0;i1<n1t;i1++)
            trs[i2t][i1]=ts[i2t][i1]+tr[i2t][i1];

       derive_1(n1t,n2t,d1t,trs,pz);

       derive_2(n1t,n2t,d2t,trs,px);

       spreadSR(nf,fmax,df,tg,tgtap,
                n1,o1,d1, nx,ox,dx, nz,oz,dz, o1t,o2t, 
                px,pz,tr,ts,datf,image);

       if(verb) fprintf(stderr,"+");

     }else{ if(verb) fprintf(stderr,".");}

   }

   sf_floatwrite(image[0],n1*n2,Fout);
   fprintf( stderr," \n finished processing \n");

 
   exit(0);
}
Ejemplo n.º 22
0
int main(int argc, char* argv[])
{
    bool verb,isreversed;

    sf_file Fs,Fr,Fi,Fc;        /* I/O files */
    sf_axis az,ax,at,ac,aa;     /* cube axes */
    int     nz,nx,nt, nhx,  nhz, nht,nc;
    int           it, ihx,  ihz, iht,ic;
    int               nhx2,nhz2,nht2;
    off_t iseek;

    float ***us=NULL,***ur=NULL,****ii=NULL; 

    pt2d *cc=NULL;
    bool *ccin=NULL;
    float cxmin,czmin;
    float cxmax,czmax;
    int  icx, icz;
    int  mcx, mcz, mct;
    int  pcx, pcz, pct;

    int **mcxall, **pcxall;
    int **mczall, **pczall;
    int  *mctall,  *pctall;
    int lht;

    float scale;

    /* gaussian taper */
    bool gaus;
    float gsx,gsz,gst; /* std dev */
    float  gx, gz, gt;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    /* OMP parameters */
#ifdef _OPENMP
    omp_init();
#endif

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("isreversed",&isreversed)) isreversed=false; /* reversed rec wfld? */
    
    Fs = sf_input ("in" ); /*   source wavefield */
    Fr = sf_input ("ur" ); /* receiver wavefield */
    Fc = sf_input ("cc" ); /* CIP coordinates    */
    Fi = sf_output("out"); /* image */

    /*------------------------------------------------------------*/
    /* read axes */
    az=sf_iaxa(Fs,1); nz = sf_n(az);
    ax=sf_iaxa(Fs,2); nx = sf_n(ax);
    at=sf_iaxa(Fs,3); nt = sf_n(at);

    /* CIP coordinates */
    ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,"");
    nc = sf_n(ac); 

    if(! sf_getint("nhz",&nhz)) nhz=0; nhz2=2*nhz+1; /* z lags */
    if(! sf_getint("nhx",&nhx)) nhx=0; nhx2=2*nhx+1; /* x lags */
    if(! sf_getint("nht",&nht)) nht=0; nht2=2*nht+1; /* t lags */

    lht=2*nht;

    if(verb) {
	sf_warning("nhx=%3d nhz=%3d nht=%3d",nhx2,nhz2,nht2);

	sf_raxa(az);
	sf_raxa(ax);
	sf_raxa(at);
	sf_raxa(ac);
    }

    /* set output axes */
    aa=sf_maxa(nhz2,-nhz*sf_d(az),sf_d(az));
    sf_setlabel(aa,"hz"); sf_setunit(aa,"");
    if(verb) sf_raxa(aa);
    sf_oaxa(Fi,aa,1);
    
    aa=sf_maxa(nhx2,-nhx*sf_d(ax),sf_d(ax)); 
    sf_setlabel(aa,"hx"); sf_setunit(aa,"");
    if(verb) sf_raxa(aa);
    sf_oaxa(Fi,aa,2);

    aa=sf_maxa(nht2,-nht*sf_d(at),sf_d(at));
    sf_setlabel(aa,"ht"); sf_setunit(aa,"");
    if(verb) sf_raxa(aa);
    sf_oaxa(Fi,aa,3);

    sf_oaxa(Fi,ac,4);

    if(! sf_getbool("gaus",&gaus)) gaus=false; /* Gaussian taper flag */
    if(gaus) {
	if(! sf_getfloat("gsx",&gsx)) gsx=nhx*sf_d(ax); gsx=1./(2*gsx*gsx);
	if(! sf_getfloat("gsz",&gsz)) gsz=nhz*sf_d(az); gsz=1./(2*gsz*gsz);
	if(! sf_getfloat("gst",&gst)) gst=nht*sf_d(at); gst=1./(2*gst*gst);
    }

    /*------------------------------------------------------------*/
    /* allocate work arrays */
    us=sf_floatalloc3(nz,nx,nht2);
    ur=sf_floatalloc3(nz,nx,nht2);
    ii=sf_floatalloc4(nhz2,nhx2,nht2,nc);
    /* zero output */
    for(ic=0; ic<nc; ic++) {
	for        (iht=0; iht<nht2; iht++) {
	    for    (ihx=0; ihx<nhx2; ihx++) {
		for(ihz=0; ihz<nhz2; ihz++) {
		    ii[ic][iht][ihx][ihz] = 0;
		}
	    }
	}
    }

    /*------------------------------------------------------------*/
    /* CIP coordinates */
    cc= (pt2d*) sf_alloc(nc,sizeof(*cc));
    pt2dread1(Fc,cc,nc,2);

    mcxall=sf_intalloc2(nhx2,nc);
    pcxall=sf_intalloc2(nhx2,nc);
    mczall=sf_intalloc2(nhz2,nc);
    pczall=sf_intalloc2(nhz2,nc);

    ccin=sf_boolalloc(nc);

    cxmin = sf_o(ax) +             nhx *sf_d(ax);
    cxmax = sf_o(ax) + (sf_n(ax)-1-nhx)*sf_d(ax);
    czmin = sf_o(az) +             nhz *sf_d(az);
    czmax = sf_o(az) + (sf_n(az)-1-nhz)*sf_d(az);
    if(verb) {
	sf_warning("cxmin=%f,cxmax=%f",cxmin,cxmax);
	sf_warning("czmin=%f,czmax=%f",czmin,czmax);
    }

    for(ic=0; ic<nc; ic++) {
	ccin[ic]=(cc[ic].x>=cxmin && cc[ic].x<=cxmax &&
		  cc[ic].z>=czmin && cc[ic].z<=czmax)?true:false;
	
	if(ccin[ic]) {

	    icx = 0.5+(cc[ic].x-sf_o(ax))/sf_d(ax);
	    for(ihx=-nhx; ihx<nhx+1; ihx++) {
		mcxall[ic][nhx+ihx] = icx-ihx;
		pcxall[ic][nhx+ihx] = icx+ihx;
	    }

	    icz = 0.5+(cc[ic].z-sf_o(az))/sf_d(az);
	    for(ihz=-nhz; ihz<nhz+1; ihz++) {
		mczall[ic][nhz+ihz] = icz-ihz;
		pczall[ic][nhz+ihz] = icz+ihz;
	    }

	}
    }
       
    mctall=sf_intalloc(nht2);
    pctall=sf_intalloc(nht2);
    for (iht=0; iht<nht2; iht++) { 
	mctall[iht]=iht;
	pctall[iht]=2*nht-iht;
    }
    
    /*------------------------------------------------------------*/
    if(isreversed) { /* receiver wavefield is reversed */

	/* read wavefield @ [0...2nht-1]*/
	for(iht=0;iht<2*nht;iht++) {
	    sf_floatread(us[iht][0],nz*nx,Fs);
	    sf_floatread(ur[iht][0],nz*nx,Fr);
	}

	if(verb) fprintf(stderr,"nt\n");
	for(it=nht;it<nt-nht;it++) {
	    if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",it);
	    
	    /* read wavefield @ [2nht]*/
	    sf_floatread(us[ lht ][0],nz*nx,Fs);
	    sf_floatread(ur[ lht ][0],nz*nx,Fr);
	    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic)				\
    private(ic,								\
	    ihx,ihz,iht,						\
	    mcx,mcz,mct,						\
	    pcx,pcz,pct) 						\
    shared (nc,ii,us,ur,						\
	    nhx2,  nhz2,  nht2,						\
	    mcxall,mczall,mctall,					\
	    pcxall,pczall,pctall,ccin)
#endif
	    for(ic=0; ic<nc; ic++) {
		if(ccin[ic]) {
		    
		    for        (iht=0; iht<nht2; iht++) { mct=mctall    [iht]; pct=pctall    [iht];
			for    (ihx=0; ihx<nhx2; ihx++) { mcx=mcxall[ic][ihx]; pcx=pcxall[ic][ihx];
			    for(ihz=0; ihz<nhz2; ihz++) { mcz=mczall[ic][ihz]; pcz=pczall[ic][ihz];
				
				ii[ic][iht][ihx][ihz] += us[mct][mcx][mcz]*ur[pct][pcx][pcz];
				
			    } /* ihz */
			}     /* ihx */
		    }         /* iht */
		    
		}
	    } /* ic */
	    
	    /* update wavefield index (cycle) */
	    for(iht=0;iht<nht2;iht++) {
		mctall[iht] = (mctall[iht]+1) % nht2;
		pctall[iht] = (pctall[iht]+1) % nht2;
	    }
	    lht = (lht+1) % nht2;
	    
	} /* it */
	if(verb) fprintf(stderr,"\n");

    } else { /* receiver wavefield is NOT reversed */

	/* read wavefield @ [0...2nht-1]*/
	for(iht=0;iht<2*nht;iht++) {
	    sf_floatread(us[iht][0],nz*nx,Fs);
	    iseek = (off_t)(nt-1-iht)*nz*nx*sizeof(float);
	    sf_seek(Fr,iseek,SEEK_SET);
	    sf_floatread(ur[iht][0],nz*nx,Fr);
	}

	if(verb) fprintf(stderr,"nt\n");
	for(it=nht;it<nt-nht;it++) {
	    if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",nt-nht-1-it);
	   
	    /* read wavefield @ [2nht]*/
	    sf_floatread(us[ lht ][0],nz*nx,Fs);
	    iseek=(off_t)(nt-nht-1-it)*nz*nx*sizeof(float);
	    sf_seek(Fr,iseek,SEEK_SET);
	    sf_floatread(ur[ lht ][0],nz*nx,Fr);
	    
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic)				\
    private(ic,								\
	    ihx,ihz,iht,						\
	    mcx,mcz,mct,						\
	    pcx,pcz,pct) 						\
    shared (nc,ii,us,ur,						\
	    nhx2,  nhz2,  nht2,						\
	    mcxall,mczall,mctall,					\
	    pcxall,pczall,pctall,ccin)
#endif
	    for(ic=0; ic<nc; ic++) {
		if(ccin[ic]) {
		    
		    for        (iht=0; iht<nht2; iht++) { mct=mctall    [iht]; pct=pctall    [iht];
			for    (ihx=0; ihx<nhx2; ihx++) { mcx=mcxall[ic][ihx]; pcx=pcxall[ic][ihx];
			    for(ihz=0; ihz<nhz2; ihz++) { mcz=mczall[ic][ihz]; pcz=pczall[ic][ihz];
				
				ii[ic][iht][ihx][ihz] += us[mct][mcx][mcz]*ur[pct][pcx][pcz];
				
			    } /* ihz */
			}     /* ihx */
		    }         /* iht */
		    
		}
	    } /* ic */
	    
	    /* update wavefield index (cycle) */
	    for(iht=0;iht<nht2;iht++) {
		mctall[iht] = (mctall[iht]+1) % nht2;
		pctall[iht] = (pctall[iht]+1) % nht2;
	    }
	    lht = (lht+1) % nht2;

	} /* it */
	if(verb) fprintf(stderr,"\n");

    } /* end "is reversed" */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* scale image */
    scale = 1./nt;
    for(ic=0; ic<nc; ic++) {
	for        (iht=0; iht<nht2; iht++) {
	    for    (ihx=0; ihx<nhx2; ihx++) {
		for(ihz=0; ihz<nhz2; ihz++) {
		    ii[ic][iht][ihx][ihz] *= scale;
		}
	    }
	}
    }

    /*------------------------------------------------------------*/
    /* apply Gaussian taper */
    if(gaus) {
	for(ic=0; ic<nc; ic++) {
	    for        (iht=0;iht<nht2;iht++) { gt=(iht-nht)*sf_d(at); gt*=gt;
		for    (ihx=0;ihx<nhx2;ihx++) { gx=(ihx-nhx)*sf_d(ax); gx*=gx;
		    for(ihz=0;ihz<nhz2;ihz++) { gz=(ihz-nhz)*sf_d(az); gz*=gz;
			ii[ic][iht][ihx][ihz] *= exp(-gt*gst - gx*gsx - gz*gsz);
		    }
		}
	    }
	}
    }

    /*------------------------------------------------------------*/
    /* write image */
    sf_floatwrite(ii[0][0][0],nc*(nhx2)*(nhz2)*(nht2),Fi);
    
    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(***ii); free(**ii); free(*ii); free(ii);
    free(*us); free(us);
    free(*ur); free(ur);
 
    free(cc);
    free(ccin);
    
    free(*mcxall); free(mcxall);
    free(*pcxall); free(pcxall);
    free(*mczall); free(mczall);
    free(*pczall); free(pczall);
    /*------------------------------------------------------------*/   

    exit (0);
}
Ejemplo n.º 23
0
int main(int argc, char* argv[])
{
    bool verb,fsrf,snap,expl; 
    int  jsnap,ntsnap;
    int  jdata;

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */

    sf_file Fvel=NULL; /* velocity  */
    sf_file Fref=NULL; /* reflectivity */
    sf_file Fden=NULL; /* density   */

    sf_file Fdat=NULL; /* data (background)      */
    sf_file Fwfl=NULL; /* wavefield (background) */

    sf_file Flid=NULL; /* data (scattered)      */
    sf_file Fliw=NULL; /* wavefield (scattered) */

    /* I/O arrays */
    float  *ww=NULL;           /* wavelet   */
    pt2d   *ss=NULL;           /* sources   */
    pt2d   *rr=NULL;           /* receivers */

    float **vpin=NULL;         /* velocity  */
    float **roin=NULL;         /* density   */
    float **rfin=NULL;         /* reflectivity */

    float **vp=NULL;           /* velocity     in expanded domain */
    float **ro=NULL;           /* density      in expanded domain */
    float **ro1=NULL;          /* normalized 1st derivative of density on axis 1 */
    float **ro2=NULL;          /* normalized 1st derivative of density on axis 2 */

    float **rf=NULL;           /* reflectivity in expanded domain */

    float  *bdd=NULL;          /* data (background) */
    float  *sdd=NULL;          /* data (scattered)  */

    float **vt=NULL;           /* temporary vp*vp * dt*dt */

    float **bum,**buo,**bup,**bua,**but; /* wavefield: um = U @ t-1; uo = U @ t; up = U @ t+1 */
    float **sum,**suo,**sup,**sua,**sut; /* wavefield: um = U @ t-1; uo = U @ t; up = U @ t+1 */

    /* cube axes */
    sf_axis at,a1,a2,as,ar;
    int     nt,n1,n2,ns,nr,nb;
    int     it,i1,i2;
    float   dt,d1,d2,id1,id2,dt2;

    /* linear interpolation weights/indices */
    lint2d cs,cr;

    fdm2d    fdm;
    abcone2d abc;     /* abc */
    sponge spo;

    /* FD operator size */
    float co,ca2,cb2,ca1,cb1;

    int ompchunk; 
#ifdef _OPENMP
    int ompnth,ompath;
#endif

    sf_axis   ac1=NULL,ac2=NULL;
    int       nqz,nqx;
    float     oqz,oqx;
    float     dqz,dqx;
    float     **uc=NULL;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);
    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;  
    /* OpenMP data chunk size */
#ifdef _OPENMP
    if(! sf_getint("ompnth",  &ompnth))     ompnth=0;  
    /* OpenMP available threads */

#pragma omp parallel
    ompath=omp_get_num_threads();
    if(ompnth<1) ompnth=ompath;
    omp_set_num_threads(ompnth);
    sf_warning("using %d threads of a total of %d",ompnth,ompath);
#endif

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
    if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */

    Fwav = sf_input ("in" ); /* wavelet   */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */

    Fvel = sf_input ("vel"); /* velocity  */
    Fden = sf_input ("den"); /* density   */
    Fref = sf_input ("ref"); /* reflectivity */

    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */

    Fliw = sf_output("liw"); /* wavefield (scattered) */
    Flid = sf_output("lid"); /* data (scattered) */

    /* axes */
    at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */
    a1 = sf_iaxa(Fvel,1); sf_setlabel(a1,"z"); if(verb) sf_raxa(a1); /* depth */
    a2 = sf_iaxa(Fvel,2); sf_setlabel(a2,"x"); if(verb) sf_raxa(a2); /* space */

    nt = sf_n(at); dt = sf_d(at);
    ns = sf_n(as);
    nr = sf_n(ar);
    n1 = sf_n(a1); d1 = sf_d(a1);
    n2 = sf_n(a2); d2 = sf_d(a2);

    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;
    }


    /*------------------------------------------------------------*/

    /* setup output data header */
    sf_oaxa(Fdat,ar,1);
    sf_oaxa(Flid,ar,1);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,2);
    sf_oaxa(Flid,at,2);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(a1);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(a2);
	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(a1);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(a2);
	dqz=sf_d(a1);
	dqx=sf_d(a2);

	ac1 = sf_maxa(nqz,oqz,dqz);
	ac2 = sf_maxa(nqx,oqx,dqx);

	/* check if the imaging window fits in the wavefield domain */

	uc=sf_floatalloc2(sf_n(ac1),sf_n(ac2));

	ntsnap=0;
        for(it=0; it<nt; it++) {
            if(it%jsnap==0) ntsnap++;
        }
        sf_setn(at,  ntsnap);
        sf_setd(at,dt*jsnap);
        if(verb) sf_raxa(at);

/*	sf_setn(at,nt/jsnap);
	sf_setd(at,dt*jsnap); */

	sf_oaxa(Fwfl,ac1,1);
	sf_oaxa(Fwfl,ac2,2);
	sf_oaxa(Fwfl,at, 3);

	sf_oaxa(Fliw,ac1,1);
	sf_oaxa(Fliw,ac2,2);
	sf_oaxa(Fliw,at, 3);
    }

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC */
    if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;

    fdm=fdutil_init(verb,fsrf,a1,a2,nb,ompchunk);

    sf_setn(a1,fdm->nzpad); sf_seto(a1,fdm->ozpad); if(verb) sf_raxa(a1);
    sf_setn(a2,fdm->nxpad); sf_seto(a2,fdm->oxpad); if(verb) sf_raxa(a2);

    /*------------------------------------------------------------*/
    if(expl) ww = sf_floatalloc( 1);
    else     ww = sf_floatalloc(ns);
    bdd =sf_floatalloc(nr);
    sdd =sf_floatalloc(nr);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */
    pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */

    cs = lint2d_make(ns,ss,fdm);
    cr = lint2d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* setup FD coefficients */
    dt2 =    dt*dt;
    id1 = 1/d1;
    id2 = 1/d2;

    co = C0 * (id2*id2+id1*id1);
    ca2= CA *  id2*id2;
    cb2= CB *  id2*id2;
    ca1= CA *          id1*id1;
    cb1= CB *          id1*id1;

    /*------------------------------------------------------------*/ 
    /* input density */
    roin=sf_floatalloc2(n1,   n2   ); 
    ro  =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    ro1 =sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    ro2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad);

    sf_floatread(roin[0],n1*n2,Fden); 
    expand(roin,ro,fdm);

    /* normalized density derivatives */
    for    (i2=NOP; i2<fdm->nxpad-NOP; i2++) {
	for(i1=NOP; i1<fdm->nzpad-NOP; i1++) {
	    ro1[i2][i1] = D1(ro,i2,i1,id1) / ro[i2][i1];
	    ro2[i2][i1] = D2(ro,i2,i1,id2) / ro[i2][i1];
	}
    }

    free(*roin); free(roin);

    /*------------------------------------------------------------*/
    /* input velocity */
    vpin=sf_floatalloc2(n1,   n2   ); 
    vp  =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    vt  =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    sf_floatread(vpin[0],n1*n2,Fvel);
    expand(vpin,vp,fdm);
    free(*vpin); free(vpin);

    /*------------------------------------------------------------*/
    /* input reflectivity */
    rfin=sf_floatalloc2(n1,   n2   ); 
    rf  =sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    sf_floatread(rfin[0],n1*n2,Fref); 
    expand(rfin,rf,fdm);
    free(*rfin); free(rfin);

    for    (i2=0; i2<fdm->nxpad; i2++) {
	for(i1=0; i1<fdm->nzpad; i1++) {
	    vt[i2][i1] = vp[i2][i1] * vp[i2][i1] * dt2;
	}
    }

    /* free surface */
    if(fsrf) {
	for    (i2=0; i2<fdm->nxpad; i2++) {
	    for(i1=0; i1<fdm->nb; i1++) {
		vt[i2][i1]=0;
	    }
	}
    }

    /*------------------------------------------------------------*/
    /* allocate wavefield arrays */
    bum=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    buo=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    bup=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    bua=sf_floatalloc2(fdm->nzpad,fdm->nxpad);

    sum=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    suo=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    sup=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    sua=sf_floatalloc2(fdm->nzpad,fdm->nxpad);

    for    (i2=0; i2<fdm->nxpad; i2++) {
	for(i1=0; i1<fdm->nzpad; i1++) {
	    bum[i2][i1]=0;
	    buo[i2][i1]=0;
	    bup[i2][i1]=0;
	    bua[i2][i1]=0;

	    sum[i2][i1]=0;
	    suo[i2][i1]=0;
	    sup[i2][i1]=0;
	    sua[i2][i1]=0;
	}
    }

    /*------------------------------------------------------------*/
    /* one-way abc setup */
    abc = abcone2d_make(NOP,dt,vp,fsrf,fdm);
    /* sponge abc setup */
    spo = sponge_make(fdm->nb);

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);
	
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,fdm->ompchunk) private(i2,i1) shared(fdm,bua,buo,sua,suo,co,ca2,ca1,cb2,cb1,id2,id1)
#endif
	for    (i2=NOP; i2<fdm->nxpad-NOP; i2++) {
	    for(i1=NOP; i1<fdm->nzpad-NOP; i1++) {
		
		/* 4th order Laplacian operator */
		bua[i2][i1] = 
		    co * buo[i2  ][i1  ] + 
		    ca2*(buo[i2-1][i1  ] + buo[i2+1][i1  ]) +
		    cb2*(buo[i2-2][i1  ] + buo[i2+2][i1  ]) +
		    ca1*(buo[i2  ][i1-1] + buo[i2  ][i1+1]) +
		    cb1*(buo[i2  ][i1-2] + buo[i2  ][i1+2]);
		sua[i2][i1] = 
		    co * suo[i2  ][i1  ] + 
		    ca2*(suo[i2-1][i1  ] + suo[i2+1][i1  ]) +
		    cb2*(suo[i2-2][i1  ] + suo[i2+2][i1  ]) +
		    ca1*(suo[i2  ][i1-1] + suo[i2  ][i1+1]) +
		    cb1*(suo[i2  ][i1-2] + suo[i2  ][i1+2]);
		
		/* density term */
		bua[i2][i1] -= (
		    D1(buo,i2,i1,id1) * ro1[i2][i1] +
		    D2(buo,i2,i1,id2) * ro2[i2][i1] );
		sua[i2][i1] -= (
		    D1(suo,i2,i1,id1) * ro1[i2][i1] +
		    D2(suo,i2,i1,id2) * ro2[i2][i1] );
	    }
	}   
	
	/* inject acceleration source */
	if(expl) {
	    sf_floatread(ww, 1,Fwav);
	    lint2d_inject1(bua,ww[0],cs);
	} else {
	    sf_floatread(ww,ns,Fwav);	
	    lint2d_inject(bua,ww,cs);
	}

	/* single scattering */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) private(i2,i1) shared(fdm,buo,sua,rf)
#endif
	for     (i2=0; i2<fdm->nxpad; i2++) {
	    for (i1=0; i1<fdm->nzpad; i1++) {
		sua[i2][i1] -= bua[i2][i1] * 2*rf[i2][i1];
	    }
	}

	/* step forward in time */
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,fdm->ompchunk) private(i2,i1) shared(fdm,bua,buo,bum,bup,sua,suo,sum,sup,vt,dt2)
#endif
	for    (i2=0; i2<fdm->nxpad; i2++) {
	    for(i1=0; i1<fdm->nzpad; i1++) {
		bup[i2][i1] = 2*buo[i2][i1] 
		    -           bum[i2][i1] 
		    +           bua[i2][i1] * vt[i2][i1];

		sup[i2][i1] = 2*suo[i2][i1] 
		    -           sum[i2][i1] 
		    +           sua[i2][i1] * vt[i2][i1];

	    }
	}
	/* circulate wavefield arrays */
	but=bum;
	bum=buo;
	buo=bup;
	bup=but;

	sut=sum;
	sum=suo;
	suo=sup;
	sup=sut;
	
	/* one-way abc apply*/
	abcone2d_apply(buo,bum,NOP,abc,fdm);
	sponge2d_apply(bum,        spo,fdm);
	sponge2d_apply(buo,        spo,fdm);

	abcone2d_apply(suo,sum,NOP,abc,fdm);
	sponge2d_apply(sum,        spo,fdm);
	sponge2d_apply(suo,        spo,fdm);

	/* extract data at receivers */
	lint2d_extract(buo,bdd,cr);
	lint2d_extract(suo,sdd,cr);
	if(        it%jdata==0) {
	    sf_floatwrite(bdd,nr,Fdat);
	    sf_floatwrite(sdd,nr,Flid);
	}

	/* extract wavefield in the "box" */
	if(snap && it%jsnap==0) {
	    cut2d(buo,uc,fdm,ac1,ac2);
	    sf_floatwrite(uc[0],sf_n(ac1)*sf_n(ac2),Fwfl);

	    cut2d(suo,uc,fdm,ac1,ac2);
	    sf_floatwrite(uc[0],sf_n(ac1)*sf_n(ac2),Fliw);
	}

    }
    if(verb) fprintf(stderr,"\n");    

    exit (0);
}
Ejemplo n.º 24
0
int main(int argc, char* argv[])
{
    sf_file Fw_s=NULL, Fw_r=NULL;
    sf_file Fi=NULL, Fm=NULL, Fr=NULL;
    sf_axis ag,at,aw,ar;
    int ig,it,iw,ir;
    int ng,nt,nw,nr;

    int method;
    bool  verb;
    bool   adj;

    sf_complex **dat_s=NULL, *wfl_s=NULL;
    sf_complex **dat_r=NULL, *wfl_r=NULL;
    float      **img=NULL;
    float      **aa=NULL,**bb=NULL,**mm=NULL;

    sf_complex **ab=NULL;
    float      **a0=NULL,**b0=NULL;

    float w,ws,wr,w0,dw;
    char *met="";

    sf_init(argc,argv);

    if(! sf_getbool("verb", &verb))     verb=false;
    if(! sf_getint("method",&method)) method=0;    /* extrapolation method */
    if(! sf_getbool("adj",  &adj))       adj=false;/* y=modeling; n=migration */
    Fm = sf_input("abm");
    Fr = sf_input("abr");

    at=sf_iaxa(Fm,2); sf_setlabel(at,"t"); /* 'extrapolation axis' */
    ar=sf_iaxa(Fr,1); sf_setlabel(ar,"r"); /* a,b reference */
    if(method==0) sf_setn(ar,1); /* pure F-D */
    nr=sf_n(ar); 

    Fw_s = sf_input ( "in");
    if (SF_COMPLEX !=sf_gettype(Fw_s)) sf_error("Need complex source");

    /* 'position axis' (could be angle) */
    ag = sf_iaxa(Fw_s,1); ng=sf_n(ag); sf_setlabel(ag,"g"); 

    /* 'extrapolation axis' (could be time) */
    at = sf_iaxa(Fw_s,2); nt=sf_n(at); sf_setlabel(at,"t");
    aw = sf_iaxa(Fw_s,3); sf_setlabel(aw,"w"); /* frequency */

    if(adj) { /* modeling */
	Fw_r = sf_output("out"); 
	sf_settype(Fw_r,SF_COMPLEX);

	Fi   = sf_input ("img");
	if (SF_FLOAT !=sf_gettype(Fi)) sf_error("Need float image");

	sf_oaxa(Fw_r,ag,1);
	sf_oaxa(Fw_r,at,2);
	sf_oaxa(Fw_r,aw,3);
    } else {  /* migration */
	Fw_r = sf_input ("rwf");
	if (SF_COMPLEX !=sf_gettype(Fw_r)) sf_error("Need complex data");

	Fi   = sf_output("out"); 
	sf_settype(Fi,SF_FLOAT);
		
	sf_oaxa(Fi,ag,1);
	sf_oaxa(Fi,at,2);
	sf_putint(Fi,"n3",1);
    }

    img   = sf_floatalloc2  (ng,nt);
    dat_s = sf_complexalloc2(ng,nt);
    dat_r = sf_complexalloc2(ng,nt);
    wfl_s = sf_complexalloc (ng);
    wfl_r = sf_complexalloc (ng);

    if(verb) {
	sf_raxa(ag);
	sf_raxa(at);
	sf_raxa(aw);
	sf_raxa(ar);
    }

    /* read ABM */
    aa = sf_floatalloc2  (ng,nt);
    bb = sf_floatalloc2  (ng,nt);
    mm = sf_floatalloc2  (ng,nt);

    sf_floatread(aa[0],ng*nt,Fm); /* a coef */
    sf_floatread(bb[0],ng*nt,Fm); /* b coef */
    sf_floatread(mm[0],ng*nt,Fm); /* mask */

    /* read ABr */
    ab = sf_complexalloc2(nr,nt);
    a0 = sf_floatalloc2  (nr,nt);
    b0 = sf_floatalloc2  (nr,nt);

    sf_complexread(ab[0],nr*nt,Fr);
    for(it=0;it<nt;it++) {
	for(ir=0;ir<nr;ir++) {
	    a0[it][ir] = crealf(ab[it][ir]);
	    b0[it][ir] = cimagf(ab[it][ir]);
	}
    }

/*------------------------------------------------------------*/
    switch (method) {
	case 3: met="PSC"; break;
	case 2: met="FFD"; break;
	case 1: met="SSF"; break;
	case 0: met="XFD"; break;
    }

    /* from hertz to radian */
    nw = sf_n(aw);
    dw = sf_d(aw) * 2.*SF_PI; 
    w0 = sf_o(aw) * 2.*SF_PI;

    rweone_init(ag,at,aw,ar,method,verb);
    switch(method) {
	case 3: rweone_psc_coef(aa,bb,a0,b0); break;
	case 2: rweone_ffd_coef(aa,bb,a0,b0); break;
	case 1: ;/* SSF */                    break;
	case 0: rweone_xfd_coef(aa,bb);       break;
    }

    if(adj) { /* modeling */
	
    } else {  /* migration */
	for(it=0;it<nt;it++) {
	    for(ig=0;ig<ng;ig++) {
		img[it][ig] = 0.;
	    }
	}
    }

/*------------------------------------------------------------*/
    if( adj)  sf_floatread  (img[0],ng*nt,Fi);

    for(iw=0;iw<nw;iw++) {
	w=w0+iw*dw;
	sf_warning("%s %d %d",met,iw,nw);

	if(adj) {
	    sf_complexread(dat_s[0],ng*nt,Fw_s);

	    ws = -w; /*      causal */
	    for(ig=0;ig<ng;ig++) {
		wfl_s[ig] = sf_cmplx(0.,0.);
	    }
	    for(it=0;it<nt;it++) {
		for(ig=0;ig<ng;ig++) {
#ifdef SF_HAS_COMPLEX_H
		    wfl_s[ig] += dat_s[it][ig];
		    dat_r[it][ig] = wfl_s[ig]*img[it][ig];
#else
		    wfl_s[ig] = sf_cadd(wfl_s[ig],dat_s[it][ig]);
		    dat_r[it][ig] = sf_crmul(wfl_s[ig],img[it][ig]);
#endif
		}
		
		if(method!=0) rweone_fk(ws,wfl_s,aa[it],a0[it],b0[it],mm[it],it);
		else          rweone_fx(ws,wfl_s,aa[it],it);
		rweone_tap(wfl_s);
	    }

	    for(it=0;it<nt;it++) {
		for(ig=0;ig<ng;ig++) {
		    dat_s[it][ig] = dat_r[it][ig];
		}
	    }

	    wr = -w; /*      causal */
	    for(ig=0;ig<ng;ig++) {
		wfl_r[ig] = sf_cmplx(0.,0.);
	    }
	    for(it=nt-1;it>=0;it--) {
		for(ig=0;ig<ng;ig++) {
#ifdef SF_HAS_COMPLEX_H
		    wfl_r[ig] += dat_s[it][ig];
#else
		    wfl_r[ig] = sf_cadd(wfl_r[ig],dat_s[it][ig]);
#endif
		    dat_r[it][ig] = wfl_r[ig];
		}
		
		if(method!=0) rweone_fk(wr,wfl_r,aa[it],a0[it],b0[it],mm[it],it);
		else          rweone_fx(wr,wfl_r,aa[it],it);
		rweone_tap(wfl_r);
	    }

	    sf_complexwrite(dat_r[0],ng*nt,Fw_r);

	} else {
	    ws = -w; /*      causal */
	    wr = +w; /* anti-causal */

	    sf_complexread(dat_s[0],ng*nt,Fw_s);
	    sf_complexread(dat_r[0],ng*nt,Fw_r);

	    for(ig=0;ig<ng;ig++) {
		wfl_s[ig] = sf_cmplx(0,0);
		wfl_r[ig] = sf_cmplx(0,0);
	    }

	    for(it=0;it<=nt-2;it++) {
		for(ig=0;ig<ng;ig++) {
#ifdef SF_HAS_COMPLEX_H
		    wfl_s[ig] += dat_s[it][ig];
		    wfl_r[ig] += dat_r[it][ig];
#else
		    wfl_s[ig] = sf_cadd(wfl_s[ig],dat_s[it][ig]);
		    wfl_r[ig] = sf_cadd(wfl_r[ig],dat_r[it][ig]);
#endif
		}

		rweone_spi(wfl_s,wfl_r,img[it]);
		
		if(method!=0) {
		    rweone_fk(ws,wfl_s,aa[it],a0[it],b0[it],mm[it],it);
		    rweone_fk(wr,wfl_r,aa[it],a0[it],b0[it],mm[it],it);
		} else {
		    rweone_fx(ws,wfl_s,aa[it],it);
		    rweone_fx(wr,wfl_r,aa[it],it);
		}
	    }
	    it=nt-1; rweone_spi(wfl_s,wfl_r,img[it]);
	}
    }

    if(!adj)  sf_floatwrite  (img[0],ng*nt,Fi);
/*------------------------------------------------------------*/

    exit(0);
}
Ejemplo n.º 25
0
int main(int argc, char* argv[])
{
    bool verb;  /* verbosity flag */
    float clip; /* threshold (clip value) */
    sf_file Fc; /* cube file */
    sf_file Fl; /* list file */
    extern int fseeko(FILE *stream, off_t offset, int whence);

    sf_axis   ax,ay,az;
    sf_axis   aa;
    int   ix,iy,iz;
    int   nx,ny,nz,nj,na;
    int   nk=0,jk;

    float **cube;
    float dx,dy,dz;
    float x0,y0,z0;

    FILE* tfile;
    char* tname;
    float t2[3],t3[4];

    /*------------------------------------------------------------*/

    /* init RSF */
    sf_init(argc,argv);
    if(! sf_getbool("verb",&verb)) verb=false;
    if(! sf_getfloat("clip",&clip)) clip=0;

    Fc = sf_input ( "in"); /* input  cube */
    Fl = sf_output("out"); /* output list */

    /* read axes*/
    az=sf_iaxa(Fc,1);
    sf_setlabel(az,"z");
    ax=sf_iaxa(Fc,2);
    sf_setlabel(ax,"x");
    ay=sf_iaxa(Fc,3);
    sf_setlabel(ay,"y");

    nz=sf_n(az);
    z0=sf_o(az);
    dz=sf_d(az);
    nx=sf_n(ax);
    x0=sf_o(ax);
    dx=sf_d(ax);
    ny=sf_n(ay);
    y0=sf_o(ay);
    dy=sf_d(ay);

    na=0;
    if(ny>1) {
        if(verb) sf_warning("initiating 3D points");
        nj=4;
    } else {
        if(verb) sf_warning("initiating 2D points");
        nj=3;
    }
    /*------------------------------------------------------------*/

    cube = sf_floatalloc2(nz,nx);

    tfile = sf_tempfile(&(tname), "w+b");

    for (iy=0; iy<ny; iy++) {
        /*	if(verb) sf_warning("iy=%d",iy);*/

        sf_floatread(cube[0],nz*nx,Fc);

        nk=0;
        for (ix=0; ix<nx; ix++) {
            for (iz=0; iz<nz; iz++) {
                if( fabs(cube[ix][iz]) > clip) {
                    nk++;
                }
            }
        }

        if(ny>1) {
            jk=0;
            for (ix=0; ix<nx; ix++) {
                for (iz=0; iz<nz; iz++) {
                    if( fabs(cube[ix][iz]) > clip) {
                        t3[0] = x0 + ix * dx;
                        t3[1] = y0 + iy * dy;
                        t3[2] = z0 + iz * dz;
                        t3[3] = cube[ix][iz];

                        fseeko(tfile,jk*4*sizeof(float),SEEK_SET);
                        fwrite(   t3,     sizeof(float),4,tfile);
                        jk++;
                    }
                }
            }
        } else {
            jk=0;
            for (ix=0; ix<nx; ix++) {
                for (iz=0; iz<nz; iz++) {
                    if( fabs(cube[ix][iz]) > clip) {
                        t2[0] = x0 + ix * dx;
                        t2[1] = z0 + iz * dz;
                        t2[2] = cube[ix][iz];

                        fseeko(tfile,jk*3*sizeof(float),SEEK_SET);
                        fwrite(   t2,     sizeof(float),3,tfile);
                        jk++;
                    }
                }
            }
        } /* else ny=1 */

        na += nk;
    } /* iy */

    /* output axes */
    aa = sf_maxa(nj,0,1);
    sf_oaxa(Fl,aa,1);
    if(verb) sf_raxa(aa);
    free(aa);
    aa = sf_maxa(na,0,1);
    sf_oaxa(Fl,aa,2);
    if(verb) sf_raxa(aa);
    free(aa);

    if( ny>1) {
        for( jk=0; jk<nk; jk++) {
            fseeko(tfile,jk*4*sizeof(float),SEEK_SET);
            fread(    t3,     sizeof(float),4,tfile);
            /*	    if(verb) sf_warning("%d, %g %g %g %g",jk,t3[0],t3[1],t3[2],t3[3]);*/

            sf_floatwrite(t3,4,Fl);
        }
    } else {
        for( jk=0; jk<nk; jk++) {
            fseeko(tfile,jk*3*sizeof(float),SEEK_SET);
            fread(    t2,     sizeof(float),3,tfile);
            /*	    if(verb) sf_warning("%d, %g %g %g",jk,t2[0],t2[1],t2[2]);*/

            sf_floatwrite(t2,3,Fl);
        }
    }

    free(cube);
    unlink(tname);

    exit (0);
}
Ejemplo n.º 26
0
int main(int argc, char* argv[])
{
    bool verb,fsrf,snap,expl,dabc; 
    int  jsnap,ntsnap,jdata;

    /* OMP parameters */
#ifdef _OPENMP
    int ompnth;
#endif 

    /* I/O files */
    sf_file Fwav=NULL; /* wavelet   */
    sf_file Fsou=NULL; /* sources   */
    sf_file Frec=NULL; /* receivers */

    sf_file Fmag=NULL; /* magnetic permitivity */
    sf_file Fele=NULL; /* electric susceptibility */
    sf_file Fcdt=NULL; /* conductivity */

    sf_file Fdat=NULL; /* data      */
    sf_file Fwfl=NULL; /* wavefield */
    /*------------------------------------------------------------*/ 	
    /*------------------------------------------------------------*/
    /* cube axes */
    sf_axis at,az,ax;
    sf_axis as,ar;

    int     nt,nz,nx,ns,nr,nb;
    int     it,iz,ix;
    float   dt,dz,dx,idz,idx;

    /* FDM structure */
    fdm2d    fdm=NULL;
    abcone2d abc=NULL;
    sponge   spo=NULL;

    /* I/O arrays */
    float  *ww=NULL;           /* wavelet   */
    pt2d   *ss=NULL;           /* sources   */
    pt2d   *rr=NULL;           /* receivers */
    float  *dd=NULL;           /* data      */

    float **tt=NULL;

    float **vel=NULL;           /* velocity */
    float **mag=NULL;
    float **ele=NULL;
    float **cdt=NULL;

    float **cdtele=NULL;        /* temporary cdt*dt/2*ele */
    float **magele=NULL;	/* temporary dt*dt/mag*ele */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    float **um,**uo,**up,**ua,**ut; /* wavefield: um = U @ t-1; uo = U @ t; up = U @ t+1 */

    /* linear interpolation weights/indices */
    lint2d cs,cr;

    /* FD operator size */
    float co,cax,cbx,caz,cbz;

    /* wavefield cut params */
    sf_axis   acz=NULL,acx=NULL;
    int       nqz,nqx;
    float     oqz,oqx;
    float     dqz,dqx;
    float     **uc=NULL;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    /*------------------------------------------------------------*/
    /* OMP parameters */
#ifdef _OPENMP
    ompnth=omp_init();
#endif
    /*------------------------------------------------------------*/

    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */
    if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */
    if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */
    if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* I/O files */
    Fwav = sf_input ("in" ); /* wavelet   */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */

    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */

    /*------------------------------------------------------------*/
    Fmag = sf_input ("mag"); /* magnetic permitivity */
    Fele = sf_input ("ele"); /* electric susceptibility */
    Fcdt = sf_input ("cdt"); /* conductivity */
    /*------------------------------------------------------------*/
	    
    /*------------------------------------------------------------*/
    /* axes */
    at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(Fmag,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(Fmag,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */

    as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */

    nt = sf_n(at); dt = sf_d(at);
    nz = sf_n(az); dz = sf_d(az);
    nx = sf_n(ax); dx = sf_d(ax);

    ns = sf_n(as);
    nr = sf_n(ar);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* other execution parameters */
    if(! sf_getint("jdata",&jdata)) jdata=1;
    if(snap) {  /* save wavefield every *jsnap* time steps */
	if(! sf_getint("jsnap",&jsnap)) jsnap=nt;        
    }
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC 
       we exclude some of the code to maintain the same size of velocity model*/
    if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP;

    fdm=fdutil_init(verb,fsrf,az,ax,nb,1);

    /*sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);*/
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* setup output data header */
    sf_oaxa(Fdat,ar,1);

    sf_setn(at,nt/jdata);
    sf_setd(at,dt*jdata);
    sf_oaxa(Fdat,at,2);

    /* setup output wavefield header */
    if(snap) {
	if(!sf_getint  ("nqz",&nqz)) nqz=sf_n(az);
	if(!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax);

	if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az);
	if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax);

	dqz=sf_d(az);
	dqx=sf_d(ax);

	acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx);
	/* check if the imaging window fits in the wavefield domain */

	uc=sf_floatalloc2(sf_n(acz),sf_n(acx));

	ntsnap=0;
	for(it=0; it<nt; it++) {
	    if(it%jsnap==0) ntsnap++;
	}
	sf_setn(at,  ntsnap);
	sf_setd(at,dt*jsnap);
	if(verb) sf_raxa(at);

	sf_oaxa(Fwfl,acz,1);
	sf_oaxa(Fwfl,acx,2);
	sf_oaxa(Fwfl,at, 3);
    }

    if(expl) {
	ww = sf_floatalloc( 1);
    } else {
	ww = sf_floatalloc(ns);
    }
    dd = sf_floatalloc(nr);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); 
    rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); 

    pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */
    pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */

    cs = lint2d_make(ns,ss,fdm);
    cr = lint2d_make(nr,rr,fdm);

    /*------------------------------------------------------------*/
    /* setup FD coefficients */
    idz = 1/dz;
    idx = 1/dx;

    co = C0 * (idx*idx+idz*idz);
    cax= CA *  idx*idx;
    cbx= CB *  idx*idx;
    caz= CA *  idz*idz;
    cbz= CB *  idz*idz;

    /*------------------------------------------------------------*/ 
    tt = sf_floatalloc2(nz,nx); 

    vel = sf_floatalloc2(fdm->nzpad,fdm->nxpad); 
    mag = sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    ele = sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    cdt = sf_floatalloc2(fdm->nzpad,fdm->nxpad); 

    cdtele =sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    magele =sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* input magnetic susceptibility*/
    sf_floatread(tt[0],nz*nx,Fmag );    expand(tt,mag,fdm);

    /* input electric susceptibility*/
    sf_floatread(tt[0],nz*nx,Fele );    expand(tt,ele,fdm);
    
    /* input conductivity*/
    sf_floatread(tt[0],nz*nx,Fcdt );    expand(tt,cdt,fdm);

    /*------------------------------------------------------------*/
    /* cdtele = sigma*dt/2*epsilon */
    /* magele = dt*dt/mu*epsilon */
    for    (ix=0; ix<fdm->nxpad; ix++) {
	for(iz=0; iz<fdm->nzpad; iz++) {
	    cdtele[ix][iz] = cdt[ix][iz]*dt/(2*(ele[ix][iz]));
	    magele[ix][iz]  = dt*dt/(mag[ix][iz]*ele[ix][iz]);
	    vel[ix][iz] = 1./(sqrt(mag[ix][iz]*ele[ix][iz]));
	}
    }
    if(fsrf) { /* free surface */
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nb; iz++) {
		cdtele[ix][iz]=0;
		magele[ix][iz]=0;
	    }
	}
    }
    /*------------------------------------------------------------*/

    free(*tt); free(tt);    
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* allocate wavefield arrays */
    um=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    uo=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    up=sf_floatalloc2(fdm->nzpad,fdm->nxpad);
    ua=sf_floatalloc2(fdm->nzpad,fdm->nxpad);

    for    (ix=0; ix<fdm->nxpad; ix++) {
	for(iz=0; iz<fdm->nzpad; iz++) {
	    um[ix][iz]=0;
	    uo[ix][iz]=0;
	    up[ix][iz]=0;
	    ua[ix][iz]=0;
	}
    }

    /*------------------------------------------------------------*/
    if(dabc) {
	/* one-way abc setup */
	abc = abcone2d_make(NOP,dt,vel,fsrf,fdm);
	/* sponge abc setup */
	spo = sponge_make(fdm->nb);
    }

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr,"\n");
    for (it=0; it<nt; it++) {
	if(verb) fprintf(stderr,"\b\b\b\b\b%d",it);

#ifdef _OPENMP
#pragma omp parallel for				\
    schedule(dynamic,fdm->ompchunk)			\
    private(ix,iz)					\
    shared(fdm,ua,uo,co,cax,caz,cbx,cbz,idx,idz)
#endif
	for    (ix=NOP; ix<fdm->nxpad-NOP; ix++) {
	    for(iz=NOP; iz<fdm->nzpad-NOP; iz++) {
		
		/* 4th order Laplacian operator */
		ua[ix][iz] = 
		    co * uo[ix  ][iz  ] + 
		    cax*(uo[ix-1][iz  ] + uo[ix+1][iz  ]) +
		    cbx*(uo[ix-2][iz  ] + uo[ix+2][iz  ]) +
		    caz*(uo[ix  ][iz-1] + uo[ix  ][iz+1]) +
		    cbz*(uo[ix  ][iz-2] + uo[ix  ][iz+2]);
		
	    }
	}   

	/* inject acceleration source */
	if(expl) {
	    sf_floatread(ww, 1,Fwav);
	    lint2d_inject1(ua,ww[0],cs);
	} else {
	    sf_floatread(ww,ns,Fwav);	
	    lint2d_inject(ua,ww,cs);
	}

	/* step forward in time */
#ifdef _OPENMP
#pragma omp parallel for	    \
    schedule(dynamic,fdm->ompchunk) \
    private(ix,iz)		    \
    shared(fdm,ua,uo,um,up,cdtele,magele)
#endif
	for    (ix=0; ix<fdm->nxpad; ix++) {
	    for(iz=0; iz<fdm->nzpad; iz++) {
	   	up[ix][iz] = (2*uo[ix][iz] 
			   - (1-cdtele[ix][iz])*um[ix][iz] 
			   + ua[ix][iz]*(magele[ix][iz]))/(1+cdtele[ix][iz]);
	    }
	}
	/* circulate wavefield arrays */
	ut=um;
	um=uo;
	uo=up;
	up=ut;
	
	if(dabc) {
	    /* one-way abc apply */
	    abcone2d_apply(uo,um,NOP,abc,fdm);
	    sponge2d_apply(um,spo,fdm);
	    sponge2d_apply(uo,spo,fdm);
	    sponge2d_apply(up,spo,fdm);
	}

	/* extract data */
	lint2d_extract(uo,dd,cr);

	if(snap && it%jsnap==0) {
	    cut2d(uo,uc,fdm,acz,acx);
	    sf_floatwrite(uc[0],sf_n(acz)*sf_n(acx),Fwfl);
	}
	if(        it%jdata==0) 
	    sf_floatwrite(dd,nr,Fdat);
    }
    if(verb) fprintf(stderr,"\n");    

    /*------------------------------------------------------------*/
    /* deallocate arrays */
    free(*um); free(um);
    free(*up); free(up);
    free(*uo); free(uo);
    free(*ua); free(ua);
    free(*uc); free(uc);

    free(*mag); free(mag);
    free(*ele); free(ele);
    free(*cdt); free(cdt);
    free(*vel); free(vel);  
    free(*cdtele); free(cdtele);
    free(*magele);  free(magele);

    /*------------------------------------------------------------*/
    free(ww);
    free(ss);
    free(rr);
    free(dd);
    /*------------------------------------------------------------*/

    sf_close();
    exit (0);
}
Ejemplo n.º 27
0
int
main(int argc, char** argv)
{

    bool verb, fsrf, snap, expl, dabc, cden, adj;
    bool optfd, hybrid, sinc; 
    int jsnap, jdata;

    /* I/O files */
    sf_file file_wav=NULL; /* wavelet */
    sf_file file_vel=NULL; /* velocity */
    sf_file file_den=NULL; /* density */
    sf_file file_wfl=NULL; /* wavefield */
    sf_file file_dat=NULL; /* data */
    sf_file file_src=NULL; /* sources */
    sf_file file_rec=NULL; /* receivers */

    /* cube axes */
    sf_axis at = NULL, az = NULL, ax = NULL, ay = NULL;
    sf_axis as = NULL, ar = NULL;

    int nbd;  /* ABC boundary size */
    int fdorder;  /* finite difference spatial accuracy order */
    int nzpad,nxpad,nypad; /* boundary padded model size */
    int ix,iy,it,is,nx,ny,nz,nt,ns,nr;
    float dx,dy,dz,dt,dt2;
    float* damp=NULL; /* damping profile for hybrid bc */
    float* ws;  /* wavelet */
    float*** vel=NULL;  /* velocity */
    float*** rho=NULL; /* density */
    float*** u0=NULL;  /* wavefield array u@t-1 (u@t+1) */
    float*** u1=NULL;  /* wavefield array u@t */
    float* u_dat=NULL; /* output data */
    float*** ptr_tmp=NULL;   
    pt3d* src3d=NULL;  /* source position */
    pt3d* rec3d=NULL;  /*receiver position*/
    scoef3d cssinc = NULL, crsinc = NULL; 
    lint3d cslint = NULL, crlint = NULL;

    /* FDM structure */
    fdm3d fdm = NULL;
    abcone3d abc = NULL;
    sponge spo = NULL;

    int nbell;

    float* fdcoef_d2;
    float* fdcoef_d1;

    sf_axis acz = NULL, acx = NULL, acy = NULL;
    int nqz, nqx, nqy;
    float oqz, oqx, oqy, dqz, dqx, dqy;

    float** oslice = NULL; /* output 3D wavefield slice-by-slice */
    float*** tmp_array;

    double wall_clock_time_s, wall_clock_time_e;

    const int SECOND_DERIV = 2;
    const int FIRST_DERIV = 1;

    int nop;

#if defined _OPENMP && _DEBUG
    double tic;
    double toc;
#endif

    /* init RSF */
    sf_init(argc,argv);

#ifdef _OPENMP
    omp_init();
    wall_clock_time_s = omp_get_wtime();
#else
    wall_clock_time_s = (double) clock() / CLOCKS_PER_SEC;
#endif

    if (!sf_getbool("verb",&verb))  verb=false; /* Verbosity flag */
    if (!sf_getbool("snap",&snap))  snap=false; /* Wavefield snapshots flag */
    if (!sf_getbool("expl",&expl))  expl=false; /* Multiple sources, one wvlt*/
    if (!sf_getbool("dabc",&dabc))  dabc=false; /* Absorbing BC */
    if (!sf_getbool("cden",&cden))  cden=false; /* Constant density */
    if (!sf_getbool("adj",&adj))    adj=false; /* adjoint flag */

    if (!sf_getbool("free",&fsrf) && !sf_getbool("fsrf",&fsrf)) fsrf=false; /* Free surface flag */

    if (!sf_getint("nbell",&nbell)) nbell=5; /* gaussian for source injection */

    if (!sf_getbool("optfd",&optfd))  optfd=false; /* optimized FD coefficients flag */
    if (!sf_getint("fdorder",&fdorder))  fdorder=4; /* spatial FD order */
    if (!sf_getbool("hybridbc",&hybrid))  hybrid=false;  /* hybrid Absorbing BC */
    if (!sf_getbool("sinc",&sinc)) sinc=false; /* sinc source injection */
  
    /* Initialize variables */
    file_wav = sf_input("in"); /* wavelet */
    file_vel = sf_input("vel"); /* velocity */ 
    file_src = sf_input("sou"); /* sources */
    file_rec = sf_input("rec"); /* receivers */
    file_dat = sf_output("out"); /* data */

    if (snap)  file_wfl = sf_output("wfl"); /* wavefield */
    if (!cden) {
	if (sf_getstring("cden")) {
	    file_den = sf_input ("den"); /* density */
	} else {
	    cden = true;
	    if (verb) sf_warning("No density file provided, running with constant density");
	}
    }
  
    at = sf_iaxa(file_wav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */
    az = sf_iaxa(file_vel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */
    ax = sf_iaxa(file_vel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */
    ay = sf_iaxa(file_vel,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space */

    as = sf_iaxa(file_src,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */
    ar = sf_iaxa(file_rec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */

    nt = sf_n(at); dt = sf_d(at); 
    nz = sf_n(az); dz = sf_d(az); 
    nx = sf_n(ax); dx = sf_d(ax); 
    ny = sf_n(ay); dy = sf_d(ay); 
    ns = sf_n(as);
    nr = sf_n(ar);

    /* other execution parameters */
    if (snap) {
	if (!sf_getint("jsnap",&jsnap))  jsnap=nt;
	/* # of t steps at which to save wavefield */
    }
    if (!sf_getint("jdata",&jdata)) jdata=1;
    /* # of t steps at which to save receiver data */

    /* setup output data header */
    sf_oaxa(file_dat,ar,1);
    sf_setn(at,(nt-1)/jdata+1);
    sf_setd(at,dt*jdata);
    sf_oaxa(file_dat,at,2);

    /* wavefield cut params */
    /* setup output wavefield header */
    if (snap) {
	if (!sf_getint  ("nqz",&nqz)) nqz=sf_n(az); /* Saved wfld window nz */
	if (!sf_getint  ("nqx",&nqx)) nqx=sf_n(ax); /* Saved wfld window nx */
	if (!sf_getint  ("nqy",&nqy)) nqy=sf_n(ay); /* Saved wfld window ny */

	if (!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); /* Saved wfld window oz */
	if (!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); /* Saved wfld window ox */
	if (!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay); /* Saved wfld window oy */

	if (!sf_getfloat("dqz",&dqz)) dqz=sf_d(az); /* Saved wfld window dz */
	if (!sf_getfloat("dqx",&dqx)) dqx=sf_d(ax); /* Saved wfld window dx */
	if (!sf_getfloat("dqy",&dqy)) dqy=sf_d(ay); /* Saved wfld window dy */
    
	acz = sf_maxa(nqz,oqz,dqz); if (verb) sf_raxa(acz);
	acx = sf_maxa(nqx,oqx,dqx); if (verb) sf_raxa(acx);
	acy = sf_maxa(nqy,oqy,dqy); if (verb) sf_raxa(acy);
	/* check if the imaging window fits in the wavefield domain */
	sf_setn(at,(nt-1)/jsnap+1);
	sf_setd(at,dt*jsnap);
	if (verb) sf_raxa(at);
    
	sf_oaxa(file_wfl,acz,1);
	sf_oaxa(file_wfl,acx,2);
	sf_oaxa(file_wfl,acy,3);
	sf_oaxa(file_wfl,at,4);
    }

    /* 2-2N finite difference coefficient */
    nop = fdorder/2; /* fd half-length stencil */
    if (!sf_getint("nb",&nbd) || nbd<nop)  nbd=nop;
    if (dabc && hybrid && nbd<=nop) nbd = 2*nop;

    /* expand domain for FD operators and ABC */
    fdm = fdutil3d_init(verb,fsrf,az,ax,ay,nbd,1);

    sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if (verb) sf_raxa(az);
    sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if (verb) sf_raxa(ax);
    sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if (verb) sf_raxa(ay);

    /* Precompute coefficients */
    dt2 = dt*dt;
    nzpad = nz+2*nbd;  nxpad = nx+2*nbd;  nypad = ny+2*nbd;

    fdcoef_d2 = compute_fdcoef(nop,dz,dx,dy,optfd,SECOND_DERIV);
    fdcoef_d1 = compute_fdcoef(nop,dz,dx,dy,optfd,FIRST_DERIV);

    /* Allocate memories */
    if (expl) ws = sf_floatalloc(1);
    else      ws = sf_floatalloc(ns);
    vel = sf_floatalloc3(nzpad,nxpad,nypad);
    if (!cden) rho = sf_floatalloc3(nzpad,nxpad,nypad);
    u_dat = sf_floatalloc(nr);
    src3d = pt3dalloc1(ns);
    rec3d = pt3dalloc1(nr);
    if (snap) oslice = sf_floatalloc2(sf_n(acz),sf_n(acx));

    /* source and receiver position */
    pt3dread1(file_src,src3d,ns,3);  /* read format: (x,y,z) */
    if (sinc) cssinc = sinc3d_make(ns,src3d,fdm);
    else      cslint = lint3d_make(ns,src3d,fdm);

    pt3dread1(file_rec,rec3d,nr,3);  /* read format: (x,y,z) */
    if (sinc) crsinc = sinc3d_make(nr,rec3d,fdm);
    else      crlint = lint3d_make(nr,rec3d,fdm);

    if (!sinc) fdbell3d_init(nbell);

    /* temperary array */
    tmp_array = sf_floatalloc3(nz,nx,ny);

    /* read velocity and pad */
    sf_floatread(tmp_array[0][0],nz*nx*ny,file_vel);
    expand3d(tmp_array,vel,fdm);
    /* read density and pad */
    if (!cden) {
	sf_floatread(tmp_array[0][0],nz*nx*ny,file_den);
	expand3d(tmp_array,rho,fdm);
    }

    free(**tmp_array);  free(*tmp_array);  free(tmp_array);

    /* A1 one-way ABC implicit scheme coefficients  */
    if (dabc) {
	abc = abcone3d_make(nbd,dt,vel,fsrf,fdm);
	if (hybrid)
	    damp = damp_make(nbd-nop); /* compute damping profiles for hybrid bc */
	else
	    spo = sponge_make(fdm->nb);
    }

    /* allocate memory for wavefield variables */
    u0 = sf_floatalloc3(nzpad,nxpad,nypad);
    u1 = sf_floatalloc3(nzpad,nxpad,nypad);

    /* initialize variables */
    memset(u0[0][0],0,sizeof(float)*nzpad*nxpad*nypad);
    memset(u1[0][0],0,sizeof(float)*nzpad*nxpad*nypad);
    memset(u_dat,0,sizeof(float)*nr);

    /* v = (v*dt)^2 */
    for (ix=0;ix<nzpad*nxpad*nypad;ix++)
	*(vel[0][0]+ix) *= *(vel[0][0]+ix)*dt2;
    if (fsrf && !hybrid) {
	for (iy=0; iy<nypad; iy++)
	    for (ix=0; ix<nxpad; ix++)
		memset(vel[iy][ix],0,sizeof(float)*fdm->nb);
    }

    for (it=0; it<nt; it++) {
	if (verb)  sf_warning("it=%d;",it+1);
#if defined _OPENMP && _DEBUG
	tic=omp_get_wtime();
#endif
    
	step_forward(u0,u1,vel,rho,fdcoef_d2,fdcoef_d1,nop,nzpad,nxpad,nypad);
    
	if (adj) { /* backward inject source wavelet */
	    if (expl) {
		sf_seek(file_wav,(off_t)(nt-it-1)*sizeof(float),SEEK_SET);
		sf_floatread(ws,1,file_wav);
		ws[0] *= dt2;
		if (sinc) sinc3d_inject1(u0,ws[0],cssinc);
		else      lint3d_inject1(u0,ws[0],cslint);
	    } else { 
		sf_seek(file_wav,(off_t)(nt-it-1)*ns*sizeof(float),SEEK_SET);
		sf_floatread(ws,ns,file_wav);
		for (is=0; is<ns; is++) ws[is] *= dt2;
		if (sinc) sinc3d_inject(u0,ws,cssinc);
		else      lint3d_inject(u0,ws,cslint);
	    }
	} else { /* forward inject source wavelet */
	    if (expl) {
		sf_floatread(ws,1,file_wav);
		ws[0] *= dt2;
		if (sinc) sinc3d_inject1(u0,ws[0],cssinc);
		else      lint3d_inject1(u0,ws[0],cslint);
	    } else {
		sf_floatread(ws,ns,file_wav);
		for (is=0; is<ns; is++) ws[is] *= dt2;
		if (sinc) sinc3d_inject(u0,ws,cssinc);
		else      lint3d_inject(u0,ws,cslint);
	    }
	}

	/* apply abc */
	if (dabc) {
	    if (hybrid) apply_abc(u0,u1,nz,nx,ny,nbd,abc,nop,damp);
	    else {
		abcone3d_apply(u0,u1,nop,abc,fdm);
		sponge3d_apply(u0,spo,fdm);
		sponge3d_apply(u1,spo,fdm);
	    }
	}

	/* loop over pointers */
	ptr_tmp = u0;  u0 = u1;  u1 = ptr_tmp;

	/* extract snapshot */
	if (snap && it%jsnap==0) {
	    int fy = (floor)((sf_o(acy)-fdm->oypad)/fdm->dy);
	    int jy = floor(sf_d(acy)/fdm->dy);
	    float **ptr_slice;
	    for (iy=0; iy<sf_n(acy); iy++) {
		ptr_slice = u0[fy+iy*jy];
		cut3d_slice(ptr_slice,oslice,fdm,acz,acx);
		sf_floatwrite(oslice[0],sf_n(acz)*sf_n(acx),file_wfl);
	    }
	}

	/* extract receiver data */
	if (sinc) sinc3d_extract(u0,u_dat,crsinc);
	else      lint3d_extract(u0,u_dat,crlint);

	sf_floatwrite(u_dat,nr,file_dat);

#if defined _OPENMP && _DEBUG
	toc=omp_get_wtime(); 
	fprintf(stderr,"%5.2gs",(float)(toc-tic));
#endif
    }
#ifdef _OPENMP
    wall_clock_time_e = omp_get_wtime();
#else
    wall_clock_time_e = (double) clock() / CLOCKS_PER_SEC;
#endif
    if (verb)
	fprintf(stderr,"\nElapsed time: %lf s\n",wall_clock_time_e-wall_clock_time_s);

    free(**u0); free(*u0); free(u0);
    free(**u1); free(*u1); free(u1);
    free(**vel); free(*vel); free(vel);
    free(u_dat);
    free(ws);
    free(fdcoef_d2); free(fdcoef_d1);
    if (snap) { free(*oslice); free(oslice); }
    if(!cden) { free(**rho); free(*rho); free(rho); }
    if (hybrid) free(damp);
    free(src3d); free(rec3d);

    return 0;
}
Ejemplo n.º 28
0
int main (int argc, char* argv[])
{
    bool verb, fsrf, snap, expl, dabc; 
    int  jsnap, ntsnap, jdata;

    /* I/O files */
    sf_file Fwav = NULL; /* wavelet   */
    sf_file Fsou = NULL; /* sources   */
    sf_file Frec = NULL; /* receivers */
    sf_file Fvel = NULL; /* velocity  */
    sf_file Fden = NULL; /* density   */
    sf_file Fdat = NULL; /* data      */
    sf_file Fwfl = NULL; /* wavefield */

    /* cube axes */
    sf_axis at, az, ax;
    sf_axis as, ar;

    int cpuid;
    int nt, nz, nx, ns, nr;
    int it, ia;
    float dt, dz, dx;

    /* FDM structure */
    /* Wee keep these structures for compatibility,
       as soon as PETSC version is finished,
       this will be removed */
    fdm2d fdm = NULL;

    /* I/O arrays */
    float *ww = NULL;           /* wavelet   */
    pt2d  *ss = NULL;           /* sources   */
    pt2d  *rr = NULL;           /* receivers */
    float *dd = NULL;           /* data      */

    float **u,**v;

    /* linear interpolation weights/indices */
    lint2d cs,cr;

    /* wavefield cut params */
    sf_axis acz = NULL, acx = NULL;
    int   nqz, nqx;
    float oqz, oqx;
    float dqz, dqx;
    float **uc = NULL;

    sf_petsc_aimplfd2 aimplfd;
    PetscErrorCode ierr;
    /* PETSc Initialization */
    ierr = PetscInitialize (&argc, &argv, 0, 0); CHKERRQ(ierr);
    MPI_Comm_rank (MPI_COMM_WORLD, &cpuid);

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init (argc, argv);

    /*------------------------------------------------------------*/

    if (!sf_getbool ("verb", &verb)) verb = false; /* verbosity flag */
    if (!sf_getbool ("snap", &snap)) snap = false; /* wavefield snapshots flag */
    if (!sf_getbool ("free", &fsrf)) fsrf = false; /* free surface flag */
    if (!sf_getbool ("expl", &expl)) expl = false; /* "exploding reflector" */
    if (!sf_getbool ("dabc", &dabc)) dabc = false; /* absorbing BC */
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* I/O files */
    Fwav = sf_input ("in" ); /* wavelet   */
    Fvel = sf_input ("vel"); /* velocity  */
    Fsou = sf_input ("sou"); /* sources   */
    Frec = sf_input ("rec"); /* receivers */
    Fwfl = sf_output("wfl"); /* wavefield */
    Fdat = sf_output("out"); /* data      */
    Fden = sf_input ("den"); /* density   */

    /*------------------------------------------------------------*/
    /* axes */
    at = sf_iaxa (Fwav,2); sf_setlabel (at,"t"); if (verb && 0 == cpuid) sf_raxa (at); /* time */
    az = sf_iaxa (Fvel,1); sf_setlabel (az,"z"); if (verb && 0 == cpuid) sf_raxa (az); /* depth */
    ax = sf_iaxa (Fvel,2); sf_setlabel (ax,"x"); if (verb && 0 == cpuid) sf_raxa (ax); /* space */

    as = sf_iaxa (Fsou, 2); sf_setlabel (as, "s"); if (verb && 0 == cpuid) sf_raxa (as); /* sources */
    ar = sf_iaxa (Frec, 2); sf_setlabel (ar, "r"); if (verb && 0 == cpuid) sf_raxa (ar); /* receivers */

    nt = sf_n (at); dt = sf_d (at);
    nz = sf_n (az); dz = sf_d (az);
    nx = sf_n (ax); dx = sf_d (ax);

    ns = sf_n (as);
    nr = sf_n (ar);
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* other execution parameters */
    if (!sf_getint ("jdata", &jdata)) jdata = 1;
    if (snap) {  /* save wavefield every *jsnap* time steps */
        if (!sf_getint ("jsnap", &jsnap)) jsnap = nt;
    }
    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* expand domain for FD operators and ABC */
    /*if (!sf_getint("nb",&nb) || nb<NOP) nb=NOP;*/
    fdm = fdutil_init (verb, true, az, ax, 0, 1);

    /*------------------------------------------------------------*/

    if (0 == cpuid) {
        sf_setn (az, fdm->nzpad); sf_seto (az, fdm->ozpad); if(verb) sf_raxa (az);
        sf_setn (ax, fdm->nxpad); sf_seto (ax, fdm->oxpad); if(verb) sf_raxa (ax);
        /*------------------------------------------------------------*/

        /*------------------------------------------------------------*/
        /* setup output data header */
        sf_oaxa (Fdat, ar, 1);

        sf_setn (at, nt/jdata);
        sf_setd (at, dt*jdata);
        sf_oaxa (Fdat, at, 2);
    }

    /* setup output wavefield header */
    if (snap && 0 == cpuid) {
        if (!sf_getint ("nqz", &nqz)) nqz = sf_n (az);
        if (!sf_getint ("nqx", &nqx)) nqx = sf_n (ax);

        if (!sf_getfloat ("oqz", &oqz)) oqz = sf_o (az);
        if (!sf_getfloat ("oqx", &oqx)) oqx = sf_o (ax);

        dqz = sf_d (az);
        dqx = sf_d (ax);

        acz = sf_maxa (nqz, oqz, dqz); sf_raxa (acz);
        acx = sf_maxa (nqx, oqx, dqx); sf_raxa (acx);
        /* check if the imaging window fits in the wavefield domain */

        uc = sf_floatalloc2 (sf_n (acz), sf_n (acx));

        ntsnap = 0;
        for (it = 0; it < nt; it++) {
            if (it % jsnap == 0)
                ntsnap++;
        }
        sf_setn (at, ntsnap);
        sf_setd (at, dt*jsnap);
        if (verb)
            sf_raxa(at);

        sf_oaxa (Fwfl, acz, 1);
        sf_oaxa (Fwfl, acx, 2);
        sf_oaxa (Fwfl, at,  3);
    }

    if (expl) {
        ww = sf_floatalloc (1);
    } else {
        ww = sf_floatalloc (ns);
    }
    dd = sf_floatalloc (nr);

    /*------------------------------------------------------------*/
    /* setup source/receiver coordinates */
    ss = (pt2d*) sf_alloc (ns, sizeof (*ss));
    rr = (pt2d*) sf_alloc (nr, sizeof (*rr));

    pt2dread1 (Fsou, ss, ns, 2); /* read (x,z) coordinates */
    pt2dread1 (Frec, rr, nr, 2); /* read (x,z) coordinates */

    cs = lint2d_make (ns, ss, fdm);
    cr = lint2d_make (nr, rr, fdm);

    v = sf_floatalloc2 (nz, nx);
    u = sf_floatalloc2 (nz, nx);

    /* input velocity */
    sf_floatread (v[0], nz*nx, Fvel);

    /*------------------------------------------------------------*/
    PetscFPrintf (MPI_COMM_WORLD, stderr, "Initializing GMRES solver\n");   
    aimplfd = sf_petsc_aimplfd2_init (nz, nx, dz, dx, dt, &v[0][0], 100, true);

    /*------------------------------------------------------------*/
    /* 
     *  MAIN LOOP
     */
    /*------------------------------------------------------------*/
    for (it = 0; it < nt; it++) {
        PetscFPrintf (MPI_COMM_WORLD, stderr, "Timestep #%d, t=%f\n", it, it*dt);

        sf_petsc_aimplfd2_next_step (aimplfd);

        /* inject acceleration source */
        if (expl) {
            sf_floatread (ww, 1, Fwav);
            for (ia = 0; ia < cs->n; ia++) {
                sf_petsc_aimplfd2_add_source_ut1 (aimplfd, ww[0], cs->jz[ia], cs->jx[ia]);
            }
        } else {
            sf_floatread (ww, ns, Fwav);
            for (ia = 0; ia < cs->n; ia++) {
            /*
                PetscFPrintf (MPI_COMM_WORLD, stderr, "Source #%d [%d, %d], f=%f\n", ia, cs->jz[ia], cs->jx[ia], ww[0]);
                */
                sf_petsc_aimplfd2_add_source_ut1 (aimplfd, ww[ia], cs->jz[ia], cs->jx[ia]);
            }
        }
        sf_petsc_aimplfd2_get_wavefield_ut2 (aimplfd, &u[0][0]);

        /* extract data */
        /*
        lint2d_extract (u, dd, cr);
        */
        for (ia = 0; ia < cr->n; ia++) {
            dd[ia] = u[cr->jx[ia]][cr->jz[ia]];
        }

        if (snap && it % jsnap == 0 && 0 == cpuid) {
            cut2d (u, uc, fdm, acz, acx);
            sf_floatwrite (uc[0], sf_n(acz)*sf_n(acx), Fwfl);
        }
        if (it % jdata == 0 && 0 == cpuid)
            sf_floatwrite (dd, nr, Fdat);
    }

    exit (0);
}
Ejemplo n.º 29
0
int main(int argc, char* argv[])
{
    bool verb;

    sf_file Fu,Fw; /* I/O files */
    sf_axis a1,a2,a3; /* cube axes */

    float ***uu=NULL, ***ww=NULL;
    int *ii,ik;

    int nh1,nh2,nh3;
    int nb1,nb2,nb3;

    int ih1,ih2,ih3;
    int ib1,ib2,ib3;
    int  n3;
    int  i3;
    int  j1, j2, j3;
    int  k1, k2, k3;

    int ompchunk;

    int lo1,hi1;
    int lo2,hi2;
    int lo3,hi3;

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);

    if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1;  /* OpenMP data chunk size */
    if(! sf_getbool("verb",&verb)) verb=false;         /* verbosity flag */

    Fu = sf_input ("in" ); /*  input field */
    Fw = sf_output("out"); /* wigner distribution */

    /* read axes */
    a1=sf_iaxa(Fu,1); sf_setlabel(a1,"a1"); if(verb) sf_raxa(a1);
    a2=sf_iaxa(Fu,2); sf_setlabel(a2,"a2"); if(verb) sf_raxa(a2);
    a3=sf_iaxa(Fu,3); sf_setlabel(a3,"a3"); if(verb) sf_raxa(a3);
    n3 = sf_n(a3);

    if(! sf_getint("nh1",&nh1)) nh1=0;
    if(! sf_getint("nh2",&nh2)) nh2=0;
    if(! sf_getint("nh3",&nh3)) nh3=0;
    if(n3<=1) nh3=0;
    if(verb) sf_warning("nh1=%d nh2=%d nh3=%d",2*nh1+1,2*nh2+1,2*nh3+1);

    nb1 = sf_n(a1);
    nb2 = sf_n(a2);
    nb3=2*nh3+1;

    uu=sf_floatalloc3(nb1,nb2,nb3);
    ww=sf_floatalloc3(nb1,nb2,nb3); 

    ii = sf_intalloc(nb3);
    for(ib3=0;ib3<nb3;ib3++) {
	ii[ib3]=ib3;
    }

    /*------------------------------------------------------------*/
    /* low end on axis 3 */
    /*------------------------------------------------------------*/
    for(         ib3=0; ib3<nb3; ib3++) {
	for(     ib2=0; ib2<nb2; ib2++) {
	    for( ib1=0; ib1<nb1; ib1++) {
		ww[ib3][ib2][ib1] = 0.;
	    }
	}
    }

    sf_floatread(uu[0][0],nb1*nb2*nb3,Fu);

    for(        ih3=-nh3; ih3<nh3+1; ih3++) { lo3=SF_ABS(ih3); hi3=nb3-lo3;
	for(    ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2;
	    for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1;
		
		for(        ib3=lo3; ib3<nh3+1;ib3++) { j3=ii[ib3-ih3]; k3=ii[ib3+ih3];
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) \
    private(ib1,ib2,j2,j1,k2,k1) \
    shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww)
#endif
		    for(    ib2=lo2; ib2<hi2;  ib2++) { j2=ib2-ih2; k2=ib2+ih2;
			for(ib1=lo1; ib1<hi1;  ib1++) { j1=ib1-ih1; k1=ib1+ih1;
			    
			    ww[ib3][ib2][ib1] += uu[j3][j2][j1] 
				*                uu[k3][k2][k1];
			} /* nb1  */
		    }     /* nb2  */
		}         /* nb3  */

	    } /* nh1 */
	}     /* nh2 */
    }         /* nh3 */
    
    for(ih3=0;ih3<nh3+1;ih3++) {
	sf_floatwrite(ww[ih3][0],nb1*nb2,Fw);
    }

    /*------------------------------------------------------------*/
    /* loop on axis 3 */
    /*------------------------------------------------------------*/
    if(verb) fprintf(stderr," n3\n");
    if(verb) fprintf(stderr,"%5d\n",n3-1);
    for(i3=nh3+1;i3<n3-nh3;i3++) {
	if(verb) fprintf(stderr,"%5d",i3);

	/* zero WDF */
	ib3=nh3;
	for(     ib2=0; ib2<nb2; ib2++) {
	    for( ib1=0; ib1<nb1; ib1++) {
		ww[ib3][ib2][ib1] = 0.;
	    }
	}

	/* circulate index to slices */
	ik = ii[0];
	for(ib3=0;ib3<nb3-1;ib3++) {
	    ii[ib3]=ii[ib3+1];
	}
	ii[nb3-1]=ik;

	/* read new slice */
	sf_floatread(uu[ ii[nb3-1] ][0],nb1*nb2,Fu);

	for(        ih3=-nh3; ih3<nh3+1; ih3++) { 
	    for(    ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2;
		for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1;
		    
		    ib3=nh3; j3=ii[ib3-ih3]; k3=ii[ib3+ih3];
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) \
    private(ib1,ib2,j2,j1,k2,k1) \
    shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww)
#endif
		    for(    ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2;
			for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1;
			    
			    ww[ib3][ib2][ib1] += uu[j3][j2][j1] 
				*                uu[k3][k2][k1];
			} /* nb1  */
		    }     /* nb2  */
		    
		} /* nh1 */
	    }     /* nh2 */
	}         /* nh3 */
	
	sf_floatwrite(ww[nh3][0],nb1*nb2,Fw);

	if(verb) fprintf(stderr,"\b\b\b\b\b");
    }

    /*------------------------------------------------------------*/
    /* high-end on axis 3*/
    /*------------------------------------------------------------*/
    for(        ih3=-nh3; ih3<nh3+1; ih3++) { lo3=SF_ABS(ih3); hi3=nb3-lo3;
	for(    ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2;
	    for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1;
		
		for(        ib3=nh3+1; ib3<hi3; ib3++) { j3=ii[ib3-ih3]; k3=ii[ib3+ih3];
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic,ompchunk) \
    private(ib1,ib2,j2,j1,k2,k1) \
    shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww)
#endif
		    for(    ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2;
			for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1;
			    
			    ww[ib3][ib2][ib1] += uu[j3][j2][j1] 
				*                uu[k3][k2][k1];
			} /* nb1  */
		    }     /* nb2  */
		}         /* nb3  */
		
	    } /* nh1 */
	}     /* nh2 */
    }         /* nh3 */
    
    for(ih3=nh3+1;ih3<2*nh3+1;ih3++) {
	sf_floatwrite(ww[ih3][0],nb1*nb2,Fw);
    }

    /*------------------------------------------------------------*/

    exit (0);
}
Ejemplo n.º 30
0
int main(int argc, char* argv[])
{
    bool verb;
    bool stat;

    char *domain;
    
    /* I/O files */
    sf_file Fccc=NULL;      
       
    sf_file Fspk=NULL;  /* template for derivative operator */    
    sf_file Fzdel=NULL; /* z derivative */
    sf_file Fxdel=NULL; /* x derivative */
    sf_file Fydel=NULL; /* y derivative */


    sf_axis ax,ay,az;      /* cube axes */
    sf_axis sx,sy,sz;      /* cube axes */
    
    
    float ***c11,***c12,***c13,***c14,***c15,***c16;
    float ***c22,***c23,***c24,***c25,***c26;
    float ***c33,***c34,***c35,***c36;
    float ***c44,***c45,***c46;
    float ***c55,***c56;
    float ***c66;
    

    float ***xdel,***ydel,***zdel;                /* output derivative */
    
    int ix,iy,iz,nnn,ndel;
    wfs3d wfs3;
    
    /*------------------------------------------------------------*/
   



    /*------------------------------------------------------------*/

    /*------------------------------------------------------------*/
    /* init RSF */
    sf_init(argc,argv);
    if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */
    if(! sf_getbool("stat",&stat)) stat=true;  /* stationary operator */
    

    
    if(NULL== (domain=sf_getstring("domain"))) domain="k";
    if(verb) sf_warning("domain= %s",domain);
    /*------------------------------------------------------------*/
    /* input files and axes */
    Fccc = sf_input ("ccc");      
    az = sf_iaxa(Fccc,1);
    ax = sf_iaxa(Fccc,2);
    ay = sf_iaxa(Fccc,3);


    Fspk = sf_input ("in");
    sz = sf_iaxa(Fspk,1);
    sx = sf_iaxa(Fspk,2);
    sy = sf_iaxa(Fspk,3);

   

    if(verb) { 
	sf_raxa(sz); 
	sf_raxa(sx); 
	sf_raxa(sy); 
	sf_raxa(az); 
	sf_raxa(ax); 
	sf_raxa(ay);
    }

    /*------------------------------------------------------------*/
    /* output files*/
    Fzdel = sf_output ("zdel");
    Fxdel = sf_output ("xdel");
    Fydel = sf_output ("ydel");
    if(domain[0]=='k'){
	sf_oaxa(Fxdel,az,1);sf_oaxa(Fxdel,ax,2);sf_oaxa(Fxdel,ay,3);
	sf_oaxa(Fydel,az,1);sf_oaxa(Fydel,ax,2);sf_oaxa(Fydel,ay,3);	
	sf_oaxa(Fzdel,az,1);sf_oaxa(Fzdel,ax,2);sf_oaxa(Fzdel,ay,3);
	zdel=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
	ydel=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
	xdel=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
	wfs3 = wfsep3_init(ax,ay,az);
    }
    else{
	sf_oaxa(Fxdel,sz,1);sf_oaxa(Fxdel,sx,2);sf_oaxa(Fxdel,sy,3);
	sf_oaxa(Fydel,sz,1);sf_oaxa(Fydel,sx,2);sf_oaxa(Fydel,sy,3);
	sf_oaxa(Fzdel,sz,1);sf_oaxa(Fzdel,sx,2);sf_oaxa(Fzdel,sy,3);
	zdel=sf_floatalloc3(sf_n(sz),sf_n(sx),sf_n(sy));
	ydel=sf_floatalloc3(sf_n(sz),sf_n(sx),sf_n(sy));
	xdel=sf_floatalloc3(sf_n(sz),sf_n(sx),sf_n(sy));
	wfs3 = wfsep3_init(sx,sy,sz);
    }
        
    if(!stat) {
	sf_oaxa(Fzdel,az,4);sf_oaxa(Fzdel,ax,5);sf_oaxa(Fzdel,ay,6);
	sf_oaxa(Fxdel,az,4);sf_oaxa(Fxdel,ax,5);sf_oaxa(Fxdel,ay,6);
	sf_oaxa(Fydel,az,4);sf_oaxa(Fydel,ax,5);sf_oaxa(Fydel,ay,6);
    }

    /*------------------------------------------------------------*/
    
    /*------------------------------------------------------------*/
    /* read model params */
    c11=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c12=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c13=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c14=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c15=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c16=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c22=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c23=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c24=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c25=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c26=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c33=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c34=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c35=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c36=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c44=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c45=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c46=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c55=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c56=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
    c66=sf_floatalloc3(sf_n(az),sf_n(ax),sf_n(ay));
   
    

    nnn = sf_n(ax)*sf_n(ay)*sf_n(az);
    sf_floatread(c11[0][0],nnn,Fccc);
    sf_floatread(c12[0][0],nnn,Fccc);
    sf_floatread(c13[0][0],nnn,Fccc);
    sf_floatread(c14[0][0],nnn,Fccc);
    sf_floatread(c15[0][0],nnn,Fccc);
    sf_floatread(c16[0][0],nnn,Fccc);
    sf_floatread(c22[0][0],nnn,Fccc);
    sf_floatread(c23[0][0],nnn,Fccc);
    sf_floatread(c24[0][0],nnn,Fccc);
    sf_floatread(c25[0][0],nnn,Fccc);
    sf_floatread(c26[0][0],nnn,Fccc);
    sf_floatread(c33[0][0],nnn,Fccc);
    sf_floatread(c34[0][0],nnn,Fccc);
    sf_floatread(c35[0][0],nnn,Fccc);
    sf_floatread(c36[0][0],nnn,Fccc);
    sf_floatread(c44[0][0],nnn,Fccc);
    sf_floatread(c45[0][0],nnn,Fccc);
    sf_floatread(c46[0][0],nnn,Fccc);
    sf_floatread(c55[0][0],nnn,Fccc);
    sf_floatread(c56[0][0],nnn,Fccc);
    sf_floatread(c66[0][0],nnn,Fccc);
  
    /*------------------------------------------------------------*/
    /* compute derivatives */
    sf_warning("stiffness matrix");
    sf_warning("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f", 
	       c11[0][0][0],c12[0][0][0],c13[0][0][0],c14[0][0][0],c15[0][0][0],c16[0][0][0]);
    sf_warning("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f", 
 	       c12[0][0][0],c22[0][0][0],c23[0][0][0],c24[0][0][0],c25[0][0][0],c26[0][0][0]);
    sf_warning("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f", 
	       c13[0][0][0],c23[0][0][0],c33[0][0][0],c34[0][0][0],c35[0][0][0],c36[0][0][0]);
    sf_warning("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f", 
	       c14[0][0][0],c24[0][0][0],c34[0][0][0],c44[0][0][0],c45[0][0][0],c46[0][0][0]);
    sf_warning("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f", 
	       c15[0][0][0],c25[0][0][0],c35[0][0][0],c45[0][0][0],c55[0][0][0],c56[0][0][0]);
    sf_warning("%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f", 
	       c16[0][0][0],c26[0][0][0],c36[0][0][0],c46[0][0][0],c56[0][0][0],c66[0][0][0]);
	
    if(stat) {
		
	if(domain[0]=='k'){ 
	    sf_warning("stop0");
	    wfsepK3(xdel,ydel,zdel,ax,ay,az,		        
		    c11[0][0][0],c12[0][0][0],c13[0][0][0],c14[0][0][0],c15[0][0][0],c16[0][0][0],
		    c22[0][0][0],c23[0][0][0],c24[0][0][0],c25[0][0][0],c26[0][0][0],
		    c33[0][0][0],c34[0][0][0],c35[0][0][0],c36[0][0][0],
		    c44[0][0][0],c45[0][0][0],c46[0][0][0],
		    c55[0][0][0],c56[0][0][0],
		    c66[0][0][0],
		    wfs3);
	    ndel=sf_n(ax)*sf_n(ay)*sf_n(az);
	    sf_floatwrite(zdel[0][0],ndel,Fzdel);
	    sf_floatwrite(xdel[0][0],ndel,Fxdel);
	    sf_floatwrite(ydel[0][0],ndel,Fydel);
	}
	else{
	    wfsep3(xdel,ydel,zdel,sx,sy,sz,		        
		    c11[0][0][0],c12[0][0][0],c13[0][0][0],c14[0][0][0],c15[0][0][0],c16[0][0][0],
		    c22[0][0][0],c23[0][0][0],c24[0][0][0],c25[0][0][0],c26[0][0][0],
		    c33[0][0][0],c34[0][0][0],c35[0][0][0],c36[0][0][0],
		    c44[0][0][0],c45[0][0][0],c46[0][0][0],
		    c55[0][0][0],c56[0][0][0],
		    c66[0][0][0],
		    wfs3);
	    ndel=sf_n(sx)*sf_n(sy)*sf_n(sz);
	    sf_floatwrite(zdel[0][0],ndel,Fzdel);
	    sf_floatwrite(xdel[0][0],ndel,Fxdel);
	    sf_floatwrite(ydel[0][0],ndel,Fydel);
	}

    }
    
    
		
    else {

	
	if(domain[0]=='k'){
	    for(    iy=0;iy<sf_n(ay);iy++){
		for(    ix=0;ix<sf_n(ax);ix++){
		    for(iz=0;iz<sf_n(az);iz++){
			if(verb) fprintf(stderr,"%5d %5d %5d",iz,ix, iy);
			wfsepK3(xdel,ydel,zdel,ax,ay,az,		        
				c11[0][0][0],c12[0][0][0],c13[0][0][0],c14[0][0][0],c15[0][0][0],c16[0][0][0],
				c22[0][0][0],c23[0][0][0],c24[0][0][0],c25[0][0][0],c26[0][0][0],
				c33[0][0][0],c34[0][0][0],c35[0][0][0],c36[0][0][0],
				c44[0][0][0],c45[0][0][0],c46[0][0][0],
				c55[0][0][0],c56[0][0][0],
				c66[0][0][0],
				wfs3);
			
			ndel=sf_n(ax)*sf_n(ay)*sf_n(az);
			sf_floatwrite(zdel[0][0],ndel,Fzdel);
			sf_floatwrite(xdel[0][0],ndel,Fxdel);
			sf_floatwrite(ydel[0][0],ndel,Fydel);
			if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b ");
		    }   
		}
	    }
	}
	else{    
	    for(    iy=0;iy<sf_n(ay);iy++){
		for(    ix=0;ix<sf_n(ax);ix++){
		    for(iz=0;iz<sf_n(az);iz++){
			
			if(verb) fprintf(stderr,"%5d %5d %5d",iz,ix, iy);
			wfsep3(xdel,ydel,zdel,ax,ay,az,		        
				c11[0][0][0],c12[0][0][0],c13[0][0][0],c14[0][0][0],c15[0][0][0],c16[0][0][0],
				c22[0][0][0],c23[0][0][0],c24[0][0][0],c25[0][0][0],c26[0][0][0],
				c33[0][0][0],c34[0][0][0],c35[0][0][0],c36[0][0][0],
				c44[0][0][0],c45[0][0][0],c46[0][0][0],
				c55[0][0][0],c56[0][0][0],
				c66[0][0][0],
				wfs3);
			ndel=sf_n(sx)*sf_n(sy)*sf_n(sz);
			sf_floatwrite(zdel[0][0],ndel,Fzdel);
			sf_floatwrite(xdel[0][0],ndel,Fxdel);
			sf_floatwrite(ydel[0][0],ndel,Fydel);
			if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b\b\b\b ");
		    }
		}	    
	    }
	}
	/*------------------------------------------------------------*/
	sf_warning("stop1");
	wfsep3_close(wfs3);
	sf_warning("stop1");
	exit (0);
    }
    
    
}