Esempio n. 1
0
Wavelet::Wavelet(const ModelSettings * modelSettings,
                 const float         * reflCoef,
                 float                 theta,
                 int                   dim,
                 float                 peakFrequency,
                 int                 & errCode)
  : rAmp_(NULL),
    cAmp_(NULL),
    theta_(theta),
    inFFTorder_(false),
    isReal_(true),
    dim_(dim),
    scale_(1),
    pre_scale_(1),
    shiftGrid_(NULL),
    gainGrid_(NULL)
{
  coeff_[0]  = reflCoef[0];
  coeff_[1]  = reflCoef[1];
  coeff_[2]  = reflCoef[2];
  int length = 119;

  dz_ = 1.0;
  nz_ = 2*length + 1;
  // cz_   =  static_cast<int>(floor((fabs(shift/dz_))+0.5));
  cz_ = length;
  nzp_  = nz_;
  cnzp_ = nzp_/2+1;
  rnzp_ = 2*cnzp_;
  rAmp_ = static_cast<fftw_real*>(fftw_malloc(sizeof(float)*rnzp_)); //new fftw_real[rnzp_];
  cAmp_ = reinterpret_cast<fftw_complex*>(rAmp_);
  norm_ = RMISSING;
  double t;
  for (int i = 0; i < nz_; ++i) {
    t = (i-cz_) * dz_;
    rAmp_[i] = static_cast<fftw_real>(Ricker(t, peakFrequency));
  }
  formats_       = modelSettings->getWaveletFormatFlag();
  waveletLength_ = findWaveletLength(modelSettings->getMinRelWaveletAmp(),
                                     modelSettings->getWaveletTaperingL());

  if(errCode == 0) {
    for(int i=0; i < rnzp_ ;i++) {
      if(i < nzp_)
        rAmp_[i]*=scale_;
      else
        rAmp_[i]=RMISSING;
    }//end for i
  }
}
Esempio n. 2
0
int main(int  argc,char **argv)
{
    int   isx,isy,isz,bd;

    int   i,j,k,im,jm,it;
	int   nth, rank;
    float t;
    float fx,fy,fz,dt2;

    float ***c11, ***c22, ***c33, ***c12, ***c13, ***c23, ***c44, ***c55, ***c66;
    float ***phaix, ***phaiy, ***phaiz;

    sf_init(argc,argv);

    sf_file Fo1, Fo2, Fo3;

    float f0=40;         // main frequency of the wavelet(usually 30Hz)
    float t0=0.04;       // time delay of the wavelet(if f0=30Hz, t0=0.04s)*/
    float A=1.0;           // the amplitude of wavelet 

    clock_t t1, t2, t3;
    float   timespent;

    t1=clock();

    /* time samping paramter */
    if (!sf_getint("nt",&nt))   nt=301;
    if (!sf_getfloat("dt",&dt)) dt=0.001;
    if (!sf_getint("bd",&bd)) bd=20;

    sf_warning("nt=%d dt=%f",nt,dt);

    /* setup I/O files */
    sf_file Fc11, Fc22, Fc33, Fc12, Fc13, Fc23, Fc44, Fc55, Fc66;
    sf_file Fphiz, Fphiy, Fphix;

    Fc11 = sf_input ("c11");  /* c11 using standard input */
    Fc22 = sf_input ("c22");  /* c22 */
    Fc33 = sf_input ("c33");  /* c33 */
    Fc12 = sf_input ("c12");  /* c12 */
    Fc13 = sf_input ("c13");  /* c13 */
    Fc23 = sf_input ("c23");  /* c23 */
    Fc44 = sf_input ("c44");  /* c44 */
    Fc55 = sf_input ("c55");  /* c55 */
    Fc66 = sf_input ("c66");  /* c66 */
    Fphix = sf_input ("phix");  /* phix x ccw*/
    Fphiy = sf_input ("phiy");  /* phiy y ccw*/
    Fphiz = sf_input ("phiz");  /* phiz z ccw */

    /* Read/Write axes */
    sf_axis az, ax, ay;
    az = sf_iaxa(Fc11,1); nz = sf_n(az); dz = sf_d(az)*1000.0;
    ax = sf_iaxa(Fc11,2); nx = sf_n(ax); dx = sf_d(ax)*1000.0;
    ay = sf_iaxa(Fc11,3); ny = sf_n(ay); dy = sf_d(ay)*1000.0;
    fy=sf_o(ay)*1000.0;
    fx=sf_o(ax)*1000.0;
    fz=sf_o(az)*1000.0;

    int nxpad, nypad, nzpad;

    nxpad=nx+2*bd;
    nypad=ny+2*bd;
    nzpad=nz+2*bd;

    sf_warning("nxpad=%d nypad=%d nzpad=%d ",nxpad,nypad,nzpad);
    sf_warning("dx=%f dy=%f dz=%f ",dx,dy,dz);

    c11=sf_floatalloc3(nzpad,nxpad,nypad);	
    c22=sf_floatalloc3(nzpad,nxpad,nypad);
    c33=sf_floatalloc3(nzpad,nxpad,nypad);	
    c12=sf_floatalloc3(nzpad,nxpad,nypad);
    c13=sf_floatalloc3(nzpad,nxpad,nypad);	
    c23=sf_floatalloc3(nzpad,nxpad,nypad);
    c44=sf_floatalloc3(nzpad,nxpad,nypad);
    c55=sf_floatalloc3(nzpad,nxpad,nypad);	
    c66=sf_floatalloc3(nzpad,nxpad,nypad);	
    phaix=sf_floatalloc3(nzpad,nxpad,nypad);
    phaiy=sf_floatalloc3(nzpad,nxpad,nypad);
    phaiz=sf_floatalloc3(nzpad,nxpad,nypad);

    /* read velocity model */
    for(i=bd;i<nypad-bd;i++)
        for(j=bd;j<nxpad-bd;j++){
          sf_floatread(&c11[i][j][bd],nz,Fc11);
          sf_floatread(&c22[i][j][bd],nz,Fc22);
          sf_floatread(&c33[i][j][bd],nz,Fc33);
          sf_floatread(&c12[i][j][bd],nz,Fc12);
          sf_floatread(&c13[i][j][bd],nz,Fc13);
          sf_floatread(&c23[i][j][bd],nz,Fc23);
          sf_floatread(&c44[i][j][bd],nz,Fc44);
          sf_floatread(&c55[i][j][bd],nz,Fc55);
          sf_floatread(&c66[i][j][bd],nz,Fc66);
          sf_floatread(&phaix[i][j][bd],nz,Fphix);
          sf_floatread(&phaiy[i][j][bd],nz,Fphiy);
          sf_floatread(&phaiz[i][j][bd],nz,Fphiz);
       }

    vmodelboundary3d(c11, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c22, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c33, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c12, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c13, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c23, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c44, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c55, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(c66, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(phaix, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(phaiy, nx, ny, nz, nxpad, nypad, nzpad, bd);
    vmodelboundary3d(phaiz, nx, ny, nz, nxpad, nypad, nzpad, bd);

    for(i=0;i<nypad;i++)
        for(j=0;j<nxpad;j++)
          for(k=0;k<nzpad;k++){
             phaix[i][j][k] *= SF_PI/180.0;
             phaiy[i][j][k] *= SF_PI/180.0;
             phaiz[i][j][k] *= SF_PI/180.0;
          }
    sf_warning("Read velocity model parameters ok !");

    int mm=2*_m+1;
    int mmix=2*_mix+1;
 
    sf_warning("m=%d mix=%d",_m,_mix);

    float *coeff_2dx,*coeff_2dy,*coeff_2dz,*coeff_1dx,*coeff_1dy,*coeff_1dz;

    coeff_2dy=sf_floatalloc(mm);
    coeff_2dx=sf_floatalloc(mm);
    coeff_2dz=sf_floatalloc(mm);
    coeff_1dy=sf_floatalloc(mmix);
    coeff_1dx=sf_floatalloc(mmix);
    coeff_1dz=sf_floatalloc(mmix);

    coeff2d(coeff_2dx,dx);
    coeff2d(coeff_2dy,dy);
    coeff2d(coeff_2dz,dz);

    coeff1dmix(coeff_1dx, dx);
    coeff1dmix(coeff_1dy, dy);
    coeff1dmix(coeff_1dz, dz);

    float*** p1=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** p2=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** p3=sf_floatalloc3(nzpad,nxpad,nypad);

    zero3float(p1,nzpad,nxpad,nypad);
    zero3float(p2,nzpad,nxpad,nypad);
    zero3float(p3,nzpad,nxpad,nypad);
    
    float*** q1=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** q2=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** q3=sf_floatalloc3(nzpad,nxpad,nypad);

    zero3float(q1,nzpad,nxpad,nypad);
    zero3float(q2,nzpad,nxpad,nypad);
    zero3float(q3,nzpad,nxpad,nypad);

    float*** r1=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** r2=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** r3=sf_floatalloc3(nzpad,nxpad,nypad);

    zero3float(r1,nzpad,nxpad,nypad);
    zero3float(r2,nzpad,nxpad,nypad);
    zero3float(r3,nzpad,nxpad,nypad);

    t2=clock();

    /* setup I/O files */
    Fo1 = sf_output("out");      /* original elasticwave iLine x-component */
    Fo2 = sf_output("Elasticy"); /* original elasticwave iLine y-component */
    Fo3 = sf_output("Elasticz"); /* original elasticwave xLine z-component */

    puthead3x(Fo1, nz, nx, ny, dz/1000.0, dx/1000.0, dy/1000.0, 0.0, 0.0, 0.0);
    puthead3x(Fo2, nz, nx, ny, dz/1000.0, dx/1000.0, dy/1000.0, 0.0, 0.0, 0.0);
    puthead3x(Fo3, nz, nx, ny, dz/1000.0, dx/1000.0, dy/1000.0, 0.0, 0.0, 0.0);

    /* source definition */
    isy=nypad/2;
    isx=nxpad/2;
    isz=nzpad/2;

    dt2=dt*dt;

#ifdef _OPENMP
    #pragma omp parallel
	{
	  nth = omp_get_num_threads();
	  rank = omp_get_thread_num();
	  sf_warning("Using %d threads, this is %dth thread",nth, rank);
	}
#endif

    float*** px_tmp=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** pz_tmp=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** qx_tmp=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** qz_tmp=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** rx_tmp=sf_floatalloc3(nzpad,nxpad,nypad);
    float*** rz_tmp=sf_floatalloc3(nzpad,nxpad,nypad);

	/*********the kernel calculation ************/
	for(it=0;it<nt;it++)
	{
	     t=it*dt;
             
         /* source Type 0: oriented 45 degree to vertical and 45 degree azimuth: Yan & Sava (2012) */
         p2[isy][isx][isz]+=Ricker(t, f0, t0, A);  // x-component
         q2[isy][isx][isz]+=Ricker(t, f0, t0, A);  // y-component
         r2[isy][isx][isz]+=Ricker(t, f0, t0, A);  // z-component

             // 3D exploding force source (e.g., Wu's PhD

/*               for(k=-1;k<=1;k++)*/
/*               for(i=-1;i<=1;i++)*/
/*               for(j=-1;j<=1;j++)*/
/*               {*/
/*                if(fabs(i)+fabs(j)+fabs(k)==3)*/
/*                {*/
/*                     p2[isy+k][isx+i][isz+j]+=i*Ricker(t, f0, t0, A);  // x-component*/
/*                     q2[isy+k][isx+i][isz+j]+=k*Ricker(t, f0, t0, A);  // y-component*/
/*                     r2[isy+k][isx+i][isz+j]+=j*Ricker(t, f0, t0, A);  // z-component*/
/*                }*/
/*               }*/
               
  	     fwportelastic3d(dt2,p1,p2,p3,q1,q2,q3,r1,r2,r3,
				         px_tmp,pz_tmp,
				         qx_tmp,qz_tmp,
				         rx_tmp,rz_tmp,
                         coeff_2dx,coeff_2dy,coeff_2dz,
                         coeff_1dx,coeff_1dy,coeff_1dz,
                         dx,dy,dz,nxpad,nypad,nzpad,
			 c11,c22,c33,c12,c13,c23,c44,c55,c66,phaix,phaiy,phaiz);

         if(it==nt-1) // output snapshot
         {
            // output iLine 
	     	for(i=0;i<ny;i++)
                {
                    im=i+bd;
		            for(j=0;j<nx;j++)
                    {
                        jm=j+bd;
                        sf_floatwrite(&p3[im][jm][bd],nz,Fo1);
                        sf_floatwrite(&q3[im][jm][bd],nz,Fo2);
                        sf_floatwrite(&r3[im][jm][bd],nz,Fo3);
                    }
                }
             }
            for(i=0;i<nypad;i++)
            for(j=0;j<nxpad;j++)
            for(k=0;k<nzpad;k++)
            {
                    p1[i][j][k]=p2[i][j][k];
                    p2[i][j][k]=p3[i][j][k];

                    q1[i][j][k]=q2[i][j][k];
                    q2[i][j][k]=q3[i][j][k];

                    r1[i][j][k]=r2[i][j][k];
                    r2[i][j][k]=r3[i][j][k];
           }

           sf_warning("forward propagation...  it= %d   t=%f",it,t);
     }

    printf("ok3\n");

    t3=clock();
    timespent=(float)(t3-t2)/CLOCKS_PER_SEC;
    sf_warning("CPU time for 3D ORT elastic modeling: %f(second)",timespent);

    free(**p1);
    free(**p2);
    free(**p3);
    free(**q1);
    free(**q2);
    free(**q3);
    free(**r1);
    free(**r2);
    free(**r3);
    free(**px_tmp);
    free(**qx_tmp);
    free(**rx_tmp);
    free(**pz_tmp);
    free(**qz_tmp);
    free(**rz_tmp);

    free(**c11);
    free(**c33);
    free(**c13);
    free(**c55);
    free(**c66);
    free(**phaiz);
    free(**phaiy);
    free(**phaix);
		
    return 0;
}
Esempio n. 3
0
int main(int argc, char* argv[])
{
	int	ix, iz, jx, jz,ixx,izz,ixf,izf,i,j,im, jm,nx,nz,nxf,nzf,nxpad,nzpad,it,ii,jj;
	float   kxmax,kzmax;

        float   f0, t, t0, dx, dz, dxf, dzf,dt, dkx, dkz, dt2;
        int     mm, nvx, nvz, ns;
        int     hnkx, hnkz, nkx, nkz, nxz, nkxz;
        int     hnkx1, hnkz1, nkx1, nkz1;
        int     isx, isz, isxm, iszm; /*source location */

        int     itaper; /* tapering or not for spectrum of oprtator*/
        int     nstep;            /* every nstep in spatial grids to calculate filters sparsely*/

        float   *coeff_1dx, *coeff_1dz, *coeff_2dx, *coeff_2dz; /* finite-difference coefficient */

        float **apx, **apz, **apxx, **apzz;        /* polarization operator of P-wave for a location */
        float **apxs, **apzs, **apxxs, **apzzs;    /* polarization operator of SV-wave for a location */

        float ****ex, ****ez;                      /* operator for whole model for P-wave*/
        float ****exs, ****ezs;                    /* operator for whole model for SV-wave*/
        float **exx, **ezz;                        /* operator for constant model for P-wave*/
        float **exxs, **ezzs;                      /* operator for constant model for SV-wave*/

        float **vp0, **vs0, **epsi, **del, **theta;         /* velocity model */
        float **p1, **p2, **p3, **q1, **q2, **q3, **p3c, **q3c, **sum;  /* wavefield array */

        float *kx, *kz, *kkx, *kkz, *kx2, *kz2, **taper;

        clock_t t1, t2, t3, t4;
        float   timespent; 
        float   A, fx, fz;

        int     isep=1;
        int     ihomo=1;

        char    *tapertype;

	double  vp2, vs2, ep2, de2, the;

        sf_init(argc,argv);

        sf_file Fo1, Fo2, Fo3, Fo4, Fo5, Fo6, Fo7, Fo8, Fo9, Fo10, Fo11, Fo12;
       
        t1=clock();
 
        /*  wavelet parameter for source definition */
        f0=30.0;                  
        t0=0.04;                  
        A=1.0;                  

        /* time samping paramter */
        if (!sf_getint("ns",&ns)) ns=301;
        if (!sf_getfloat("dt",&dt)) dt=0.001;
        if (!sf_getint("isep",&isep)) isep=0;             /* if isep=1, separate wave-modes */ 
        if (!sf_getint("ihomo",&ihomo)) ihomo=0;          /* if ihomo=1, homogeneous medium */
        if (NULL== (tapertype=sf_getstring("tapertype"))) tapertype="D"; /* taper type*/
        if (!sf_getint("nstep",&nstep)) nstep=1; /* grid step to calculate operators: 1<=nstep<=5 */

        sf_warning("isep=%d",isep);
        sf_warning("ihomo=%d",ihomo);
        sf_warning("tapertype=%s",tapertype);
        sf_warning("nstep=%d",nstep);

        sf_warning("ns=%d dt=%f",ns,dt);
        sf_warning("read velocity model parameters");

        /* setup I/O files */
        sf_file Fvp0, Fvs0, Feps, Fdel, Fthe;

        Fvp0 = sf_input ("in");  /* vp0 using standard input */
        Fvs0 = sf_input ("vs0");  /* vs0 */
        Feps = sf_input ("epsi");  /* epsi */
        Fdel = sf_input ("del");  /* delta */
        Fthe = sf_input ("the");  /* theta */

        /* Read/Write axes */
        sf_axis az, ax;
        az = sf_iaxa(Fvp0,1); nvz = sf_n(az); dz = sf_d(az)*1000.0;
        ax = sf_iaxa(Fvp0,2); nvx = sf_n(ax); dx = sf_d(ax)*1000.0;
        fx=sf_o(ax)*1000.0;
        fz=sf_o(az)*1000.0;

        /* source definition */
        isx=nvx/2;
        isz=nvz/2;
        //isz=nvz*2/5;

        /* wave modeling space */
	nx=nvx;
	nz=nvz;
        nxpad=nx+2*m;
        nzpad=nz+2*m;

        sf_warning("fx=%f fz=%f dx=%f dz=%f",fx,fz,dx,dz);

        sf_warning("nx=%d nz=%d nxpad=%d nzpad=%d", nx,nz,nxpad,nzpad);

	vp0=sf_floatalloc2(nz,nx);	
	vs0=sf_floatalloc2(nz,nx);	
	epsi=sf_floatalloc2(nz,nx);	
	del=sf_floatalloc2(nz,nx);	
	theta=sf_floatalloc2(nz,nx);	

        nxz=nx*nz;
        mm=2*m+1;

        dt2=dt*dt;
        isxm=isx+m;  /* source's x location */
        iszm=isz+m;  /* source's z-location */

        /* read velocity model */
        sf_floatread(vp0[0],nxz,Fvp0);
        sf_floatread(vs0[0],nxz,Fvs0);
        sf_floatread(epsi[0],nxz,Feps);
        sf_floatread(del[0],nxz,Fdel);
        sf_floatread(theta[0],nxz,Fthe);

        for(i=0;i<nx;i++)
        for(j=0;j<nz;j++)
           theta[i][j] *= PI/180.0;

        t2=clock();

        Fo1 = sf_output("out"); /* Elastic-wave x-component */
        Fo2 = sf_output("Elasticz"); /* Elastic-wave z-component */
        /* setup I/O files */
        puthead3(Fo1, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);
        puthead3(Fo2, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);

        /*****************************************************************************
         *  Calculating polarization operator for wave-mode separation
         * ***************************************************************************/
        if(isep==1)
        {
           sf_warning("==================================================");
           sf_warning("==      Calculating Polarization Operator       ==");
           sf_warning("==================================================");
           /* calculate spatial steps for operater in sparsely sampling grid point */
           dxf=dx*nstep;
           dzf=dz*nstep;
           nxf=nx/nstep+1;
           nzf=nz/nstep+1;

           /* operators length for calculation */
           hnkx=400.0/dx;
           hnkz=400.0/dz;
           nkx=2*hnkx+1;   /* operator length in kx-direction */
           nkz=2*hnkz+1;   /* operator length in kz-direction */

           /* truncated spatial operators length for filtering*/
           hnkx1=200.0/dx;
           hnkz1=200.0/dz;
           nkx1=2*hnkx1+1;
           nkz1=2*hnkz1+1;

           sf_warning("nx=%d nz=%d nxf=%d nzf=%d", nx,nz,nxf,nzf);
           sf_warning("dx=%f dz=%f dxf=%f dzf=%f", dx,dz,dxf,dzf);

           sf_warning("hnkx=%d hnkz=%d nkx=%d nkz=%d", hnkx, hnkz, nkx, nkz);
           sf_warning("hnkx1=%d hnkz1=%d nkx1=%d nkz1=%d", hnkx1, hnkz1, nkx1, nkz1);

           dkx=2*PI/dx/nkx;
           dkz=2*PI/dz/nkz;
	   kxmax=PI/dx;
	   kzmax=PI/dz;

           kx=sf_floatalloc(nkx);
           kz=sf_floatalloc(nkx);
           kkx=sf_floatalloc(nkx);
           kkz=sf_floatalloc(nkx);
           kx2=sf_floatalloc(nkx);
           kz2=sf_floatalloc(nkx);

           taper=sf_floatalloc2(nkz, nkx);

           // define axis samples and taper in wavenumber domain 
           kxkztaper(kx, kz, kkx, kkz, kx2, kz2, taper, nkx, nkz, hnkx, hnkz, dkx, dkz, kxmax, kzmax, tapertype);

           nkxz=nkx*nkz;

           /* truncation of spatial filter */
           if(ihomo==1)
           {
              exx=sf_floatalloc2(nkz1, nkx1);
              ezz=sf_floatalloc2(nkz1, nkx1);
              exxs=sf_floatalloc2(nkz1, nkx1);
              ezzs=sf_floatalloc2(nkz1, nkx1);
           }else{
	      ex=sf_floatalloc4(nkz1, nkx1, nz, nx);
	      ez=sf_floatalloc4(nkz1, nkx1, nz, nx);
	      exs=sf_floatalloc4(nkz1, nkx1, nz, nx);
	      ezs=sf_floatalloc4(nkz1, nkx1, nz, nx);
           }
           /*****************************************************************************
           *  Calculating polarization operator for wave-mode separation
           * ***************************************************************************/
	    apx=sf_floatalloc2(nkz, nkx);
	    apz=sf_floatalloc2(nkz, nkx);

	    apxs=sf_floatalloc2(nkz, nkx);
	    apzs=sf_floatalloc2(nkz, nkx);

	    apxx=sf_floatalloc2(nkz, nkx);
	    apzz=sf_floatalloc2(nkz, nkx);

	    apxxs=sf_floatalloc2(nkz, nkx);
	    apzzs=sf_floatalloc2(nkz, nkx);

            /* setup I/O files for wavenumber-domain operators */
            Fo3 = sf_output("apx"); /*  P-wave's polarization x-comp */
            Fo4 = sf_output("apz"); /*  P-wave's polarization z-comp */
            Fo5 = sf_output("apxs"); /*  SV-wave's polarization x-comp */
            Fo6 = sf_output("apzs"); /*  SV-wave's polarization z-comp */

            puthead1(Fo3, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
            puthead1(Fo4, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
            puthead1(Fo5, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
            puthead1(Fo6, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
 
            /* setup I/O files for space-domain operators */
            Fo7 = sf_output("apxx");  /* P-wave's polarization x-comp in (x,z) domain */
            Fo8 = sf_output("apzz");  /* P-wave's polarization z-comp in (x,z) domain */
            Fo9 = sf_output("apxxs"); /* SV-wave's polarization x-comp in (x,z) domain */
            Fo10 = sf_output("apzzs"); /* SV-wave's polarization z-comp in (x,z) domain */

            puthead2(Fo7, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
            puthead2(Fo8, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
            puthead2(Fo9, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
            puthead2(Fo10, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);

	    /*************calculate projection deviation grid-point by grid-point **********/
           for(ix=0,ixf=0;ix<nx;ix+=nstep,ixf++)
           {
              for(iz=0,izf=0;iz<nz;iz+=nstep,izf++)
              {
	        vp2=vp0[ix][iz]*vp0[ix][iz];
	        vs2=vs0[ix][iz]*vs0[ix][iz];
	        ep2=1.0+2*epsi[ix][iz];
	        de2=1.0+2*del[ix][iz];
                the=theta[ix][iz];

               if(ixf%10==0&&izf%100==0) sf_warning("Operator: nxf=%d ixf=%d izf=%d vp2=%f vs2=%f",nxf, ixf,izf,vp2,vs2);

   	        /*************calculate projection operrate with tapering **********/
                zero2float(apx, nkz, nkx);
                zero2float(apz, nkz, nkx);
                zero2float(apxs, nkz, nkx);
                zero2float(apzs, nkz, nkx);
               
                /* polvtipsv: P- and SV-wave polarization operators in VTI media  */
                itaper=1;
                polttipsv(apx,apz,apxs,apzs,kx,kz,kkx,kkz,taper,hnkx,hnkz,dkx,dkz,
                          vp2,vs2,ep2,de2,the,itaper);

                ikxkz2xz(apx, apxx, hnkx, hnkz, nkx, nkz);
                ikxkz2xz(apz, apzz, hnkx, hnkz, nkx, nkz);
                ikxkz2xz(apxs, apxxs, hnkx, hnkz, nkx, nkz);
                ikxkz2xz(apzs, apzzs, hnkx, hnkz, nkx, nkz);

                // truncation and saving of operator in space-domain
                if(ihomo==1)
                {
                   for(jx=-hnkx1,ixx=hnkx-hnkx1;jx<=hnkx1;jx++,ixx++) 
                   for(jz=-hnkz1,izz=hnkz-hnkz1;jz<=hnkz1;jz++,izz++) 
                   {
                     exx[jx+hnkx1][jz+hnkz1]=apxx[ixx][izz]; 
                     ezz[jx+hnkx1][jz+hnkz1]=apzz[ixx][izz]; 
                     exxs[jx+hnkx1][jz+hnkz1]=apxxs[ixx][izz]; 
                     ezzs[jx+hnkx1][jz+hnkz1]=apzzs[ixx][izz]; 
                   }
                }else{
                   for(jx=-hnkx1,ixx=hnkx-hnkx1;jx<=hnkx1;jx++,ixx++) 
                   for(jz=-hnkz1,izz=hnkz-hnkz1;jz<=hnkz1;jz++,izz++) 
                   {
                     ex[ixf][izf][jx+hnkx1][jz+hnkz1]=apxx[ixx][izz]; 
                     ez[ixf][izf][jx+hnkx1][jz+hnkz1]=apzz[ixx][izz]; 
                     exs[ixf][izf][jx+hnkx1][jz+hnkz1]=apxxs[ixx][izz]; 
                     ezs[ixf][izf][jx+hnkx1][jz+hnkz1]=apzzs[ixx][izz]; 
                   }
                }
                
                if((ixf==nxf/2&&izf==nzf/2&&ihomo==0)||ihomo==1)
                {
                   //write-disk operators in kx-kz domain
	           sf_floatwrite(apx[0], nkxz, Fo3);
	           sf_floatwrite(apz[0], nkxz, Fo4);
	           sf_floatwrite(apxs[0], nkxz, Fo5);
	           sf_floatwrite(apzs[0], nkxz, Fo6);

                   //write-disk operators in x-z domain
	           sf_floatwrite(apxx[0], nkxz, Fo7);
	           sf_floatwrite(apzz[0], nkxz, Fo8);
	           sf_floatwrite(apxxs[0], nkxz, Fo9);
	           sf_floatwrite(apzzs[0], nkxz, Fo10);
                }
                if(ihomo==1) goto loop;
             }// iz loop
           }//ix loop
           loop:;

           free(kx);
           free(kz);
           free(kx2);
           free(kz2);
           free(kkx);
           free(kkz);

           free(*taper);

           free(*apx);
           free(*apz);
           free(*apxs);
           free(*apzs);
           free(*apxx);
           free(*apzz);
           free(*apxxs);
           free(*apzzs);
        }// isep loop
	/****************End of Calculating Projection Deviation Operator****************/
        t3=clock();
        timespent=(float)(t3-t2)/CLOCKS_PER_SEC;
        sf_warning("Computation time (operators): %f (second)",timespent);
 
	/****************begin to calculate wavefield****************/
	/****************begin to calculate wavefield****************/
        sf_warning("==================================================");
        sf_warning("==      Propagation Using Elastic Wave Eq.      ==");
        sf_warning("==================================================");
       coeff_2dx=sf_floatalloc(mm);
       coeff_2dz=sf_floatalloc(mm);
       coeff_1dx=sf_floatalloc(mm);
       coeff_1dz=sf_floatalloc(mm);

        coeff2d(coeff_2dx,dx);
        coeff2d(coeff_2dz,dz);

	p1=sf_floatalloc2(nzpad, nxpad);
	p2=sf_floatalloc2(nzpad, nxpad);
	p3=sf_floatalloc2(nzpad, nxpad);

	q1=sf_floatalloc2(nzpad, nxpad);
	q2=sf_floatalloc2(nzpad, nxpad);
	q3=sf_floatalloc2(nzpad, nxpad);

        zero2float(p1, nzpad, nxpad);
        zero2float(p2, nzpad, nxpad);
        zero2float(p3, nzpad, nxpad);
      
        zero2float(q1, nzpad, nxpad);
        zero2float(q2, nzpad, nxpad);
        zero2float(q3, nzpad, nxpad);
        
        coeff1dmix(coeff_1dx,dx);
        coeff1dmix(coeff_1dz,dz);

        if(isep==1)
        {
            Fo11 = sf_output("ElasticSepP"); /*  scalar wavefield using P-wave's polarization projection oprtator*/
            Fo12 = sf_output("ElasticSepSV"); /*  scalar wavefield using SV-wave's polarization projection oprtator*/

            puthead3(Fo11, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);
            puthead3(Fo12, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);

	   p3c=sf_floatalloc2(nz,nx);
	   q3c=sf_floatalloc2(nz,nx);
           sum=sf_floatalloc2(nz,nx);

        }

	for(it=0;it<ns;it++)
	{
		t=it*dt;

                // 2D exploding force source (e.g., Wu's PhD
                for(i=-1;i<=1;i++)
                for(j=-1;j<=1;j++)
                {
                     if(fabs(i)+fabs(j)==2)
                     {
                          p2[isxm+i][iszm+j]+=i*Ricker(t, f0, t0, A);
                          q2[isxm+i][iszm+j]+=j*Ricker(t, f0, t0, A);
                     }
                }
        // 2D equil-energy force source (e.g., Wu's PhD)
        /*
        for(i=-1;i<=1;i++)
        for(j=-1;j<=1;j++)
        {
             if(fabs(i)+fabs(j)==2)
             {
                  if(i==-1&&j==1)  
                    q2[isxm+i][iszm+j]+=sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==-1&&j==-1) 
                   p2[isxm+i][iszm+j]+=-sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==1&&j==1)  
                   p2[isxm+i][iszm+j]+=sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==1&&j==-1) 
                    q2[isxm+i][iszm+j]+=-sqrt(2.0)*Ricker(t, f0, t0, A);
             }
        }
        */
                /* fwpvtielastic: forward-propagating using original elastic equation of displacement in VTI media*/
		fwpttielastic(dt2, p1, p2, p3, q1, q2, q3, coeff_2dx, coeff_2dz, coeff_1dx, coeff_1dz,
                              dx, dz, nx, nz, nxpad, nzpad, vp0, vs0, epsi, del, theta);

               /******* output wavefields: component and divergence *******/
	       if(it==ns-1)
		{
		     for(i=0;i<nx;i++)
                     {
                        im=i+m;
		        for(j=0;j<nz;j++)
		        {
                            jm=j+m;
	                    sf_floatwrite(&p3[im][jm],1,Fo1);
	                    sf_floatwrite(&q3[im][jm],1,Fo2);
			}
                     }/* i loop*/

                       
                     if(isep==1)
                     {
                        //////////////////////////////////////////////////////////////////////////////////////////
                        /* applying P-wave polarization projection operator in spatial domain */
	                zero2float(p3c,nz,nx);
	                zero2float(q3c,nz,nx);
                        zero2float(sum, nz, nx);

                        if(ihomo==1)
                            filter2dsepglobal(p3, q3, p3c, q3c, exx, ezz, nx, nz, hnkx1, hnkz1);
                        else
                            filter2dsep(p3, q3, p3c, q3c, ex, ez, nx, nz, nstep, hnkx1, hnkz1);

			for(i=0;i<nx;i++)
			for(j=0;j<nz;j++)
			   sum[i][j]=p3c[i][j]+q3c[i][j];

			sf_floatwrite(sum[0],nx*nz, Fo11);
          
                        //////////////////////////////////////////////////////////////////////////////////////////
                        /* applying SV-wave polarization projection operator in spatial domain */
	                zero2float(p3c,nz,nx);
	                zero2float(q3c,nz,nx);
                        zero2float(sum, nz, nx);

                        if(ihomo==1)
                            filter2dsepglobal(p3, q3, p3c, q3c, exxs, ezzs, nx, nz, hnkx1, hnkz1);
                        else
                            filter2dsep(p3, q3, p3c, q3c, exs, ezs, nx, nz, nstep, hnkx1, hnkz1);

			for(i=0;i<nx;i++)
			for(j=0;j<nz;j++)
			   sum[i][j]=p3c[i][j]+q3c[i][j];

			sf_floatwrite(sum[0],nx*nz, Fo12);
                     }// isep==1

                }/* (it+1)%ntstep==0 */

                /**************************************/
 	        for(i=0,ii=m;i<nx;i++,ii++)
	        for(j=0,jj=m;j<nz;j++,jj++)
		{
				p1[ii][jj]=p2[ii][jj];	
				p2[ii][jj]=p3[ii][jj];	

				q1[ii][jj]=q2[ii][jj];	
				q2[ii][jj]=q3[ii][jj];	
		}

		if(it%100==0)
			sf_warning("Elastic: it= %d",it);
        }/* it loop */
        t4=clock();
        timespent=(float)(t4-t3)/CLOCKS_PER_SEC;
        sf_warning("Computation time (propagation + separation): %f (second)",timespent);

        if(isep==1)
        {
          free(*p3c);
          free(*q3c);
          free(*sum);

          if(ihomo==1)
          {
              free(*exx);
              free(*ezz);
              free(*exxs);
              free(*ezzs);
          }else{
              free(***ex);
              free(***ez);
              free(***exs);
              free(***ezs);
          }
        }

        free(*p1);
        free(*p2);
        free(*p3);
        free(*q1);
        free(*q2);
        free(*q3);

        free(*vp0);
        free(*vs0);
        free(*epsi);
        free(*del);
        free(*theta);

	exit(0);
}
Esempio n. 4
0
int main(int argc, char* argv[])
{
     	int	i,j,im,jm,nx,nz,nxpad,nzpad,it,ii,jj;

        float   f0, t, t0, dx, dz,dt, dt2;
        int     mm, nvx, nvz, ns;
        int     isx, isz, isxm, iszm; /*source location */

        float   *coeff_1dx, *coeff_1dz, *coeff_2dx, *coeff_2dz; /* finite-difference coefficient */

        float **vp0, **vs0, **epsi, **del, **theta;         /* velocity model */
        float **p1, **p2, **p3, **q1, **q2, **q3;  /* wavefield array */

        float   A, fx, fz;
		clock_t t1, t2, t3;

        sf_init(argc,argv);

        sf_file Fo1, Fo2;
		float timespent;
       
		t1 = clock();

        /*  wavelet parameter for source definition */
        f0=30.0;                  
        t0=0.04;                  
        A=1.0;                  

        /* time samping paramter */
        if (!sf_getint("ns",&ns)) ns=301;
        if (!sf_getfloat("dt",&dt)) dt=0.001;

        sf_warning("ns=%d dt=%f",ns,dt);
        sf_warning("read velocity model parameters");

        /* setup I/O files */
        sf_file Fvp0, Fvs0, Feps, Fdel, Fthe;

        Fvp0 = sf_input ("in");  /* vp0 using standard input */
        Fvs0 = sf_input ("vs0");  /* vs0 */
        Feps = sf_input ("epsi");  /* epsi */
        Fdel = sf_input ("del");  /* delta */
        Fthe = sf_input ("the");  /* theta */

        /* Read/Write axes */
        sf_axis az, ax;
        az = sf_iaxa(Fvp0,1); nvz = sf_n(az); dz = sf_d(az)*1000.0;
        ax = sf_iaxa(Fvp0,2); nvx = sf_n(ax); dx = sf_d(ax)*1000.0;
        fx=sf_o(ax)*1000.0;
        fz=sf_o(az)*1000.0;

        /* source definition */
        isx=nvx/2;
        isz=nvz/2;
        //isz=nvz*2/5;

        /* wave modeling space */
	nx=nvx;
	nz=nvz;
        nxpad=nx+2*_m;
        nzpad=nz+2*_m;

        sf_warning("fx=%f fz=%f dx=%f dz=%f",fx,fz,dx,dz);

        sf_warning("nx=%d nz=%d nxpad=%d nzpad=%d", nx,nz,nxpad,nzpad);

	vp0=sf_floatalloc2(nz,nx);	
	vs0=sf_floatalloc2(nz,nx);	
	epsi=sf_floatalloc2(nz,nx);	
	del=sf_floatalloc2(nz,nx);	
	theta=sf_floatalloc2(nz,nx);	

        int nxz=nx*nz;
        mm=2*_m+1;

        dt2=dt*dt;
        isxm=isx+_m;  /* source's x location */
        iszm=isz+_m;  /* source's z-location */

        /* read velocity model */
        sf_floatread(vp0[0],nxz,Fvp0);
        sf_floatread(vs0[0],nxz,Fvs0);
        sf_floatread(epsi[0],nxz,Feps);
        sf_floatread(del[0],nxz,Fdel);
        sf_floatread(theta[0],nxz,Fthe);

        for(i=0;i<nx;i++)
        for(j=0;j<nz;j++)
           theta[i][j] *= SF_PI/180.0;

        Fo1 = sf_output("out"); /* Elastic-wave x-component */
        Fo2 = sf_output("Elasticz"); /* Elastic-wave z-component */
        /* setup I/O files */
        puthead3(Fo1, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);
        puthead3(Fo2, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz/1000.0, fx/1000.0, 0.0);

	/****************begin to calculate wavefield****************/
	/****************begin to calculate wavefield****************/
        sf_warning("==================================================");
        sf_warning("==      Porpagation Using Elastic Wave Eq.      ==");
        sf_warning("==================================================");
       coeff_2dx=sf_floatalloc(mm);
       coeff_2dz=sf_floatalloc(mm);
       coeff_1dx=sf_floatalloc(mm);
       coeff_1dz=sf_floatalloc(mm);

        coeff2d(coeff_2dx,dx);
        coeff2d(coeff_2dz,dz);

	p1=sf_floatalloc2(nzpad, nxpad);
	p2=sf_floatalloc2(nzpad, nxpad);
	p3=sf_floatalloc2(nzpad, nxpad);

	q1=sf_floatalloc2(nzpad, nxpad);
	q2=sf_floatalloc2(nzpad, nxpad);
	q3=sf_floatalloc2(nzpad, nxpad);

        zero2float(p1, nzpad, nxpad);
        zero2float(p2, nzpad, nxpad);
        zero2float(p3, nzpad, nxpad);
      
        zero2float(q1, nzpad, nxpad);
        zero2float(q2, nzpad, nxpad);
        zero2float(q3, nzpad, nxpad);
        
        coeff1dmix(coeff_1dx,dx);
        coeff1dmix(coeff_1dz,dz);

		t2 = clock();
	for(it=0;it<ns;it++)
	{
		t=it*dt;

                // 2D exploding force source (e.g., Wu's PhD
                for(i=-1;i<=1;i++)
                for(j=-1;j<=1;j++)
                {
                     if(fabs(i)+fabs(j)==2)
                     {
                          p2[isxm+i][iszm+j]+=sqrt(2.0)*i*Ricker(t, f0, t0, A);
                          q2[isxm+i][iszm+j]+=sqrt(2.0)*j*Ricker(t, f0, t0, A);
                     }
                }
        // 2D equil-energy force source (e.g., Wu's PhD)
        /*
        for(i=-1;i<=1;i++)
        for(j=-1;j<=1;j++)
        {
             if(fabs(i)+fabs(j)==2)
             {
                  if(i==-1&&j==1)  
                    q2[isxm+i][iszm+j]+=sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==-1&&j==-1) 
                   p2[isxm+i][iszm+j]+=-sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==1&&j==1)  
                   p2[isxm+i][iszm+j]+=sqrt(2.0)*Ricker(t, f0, t0, A);
                  if(i==1&&j==-1) 
                    q2[isxm+i][iszm+j]+=-sqrt(2.0)*Ricker(t, f0, t0, A);
             }
        }
        */
                /* fwpvtielastic: forward-propagating using original elastic equation of displacement in VTI media*/
		fwpttielastic(dt2, p1, p2, p3, q1, q2, q3, coeff_2dx, coeff_2dz, coeff_1dx, coeff_1dz,
                              dx, dz, nx, nz, nxpad, nzpad, vp0, vs0, epsi, del, theta);

               /******* output wavefields: component and divergence *******/
	       if(it==ns-1)
		{
		     for(i=0;i<nx;i++)
                     {
                        im=i+_m;
		        for(j=0;j<nz;j++)
		        {
                            jm=j+_m;
	                    sf_floatwrite(&p3[im][jm],1,Fo1);
	                    sf_floatwrite(&q3[im][jm],1,Fo2);
			}
                     }/* i loop*/
                       
                }/* (it+1)%ntstep==0 */

                /**************************************/
 	        for(i=0,ii=_m;i<nx;i++,ii++)
	        for(j=0,jj=_m;j<nz;j++,jj++)
		{
				p1[ii][jj]=p2[ii][jj];	
				p2[ii][jj]=p3[ii][jj];	

				q1[ii][jj]=q2[ii][jj];	
				q2[ii][jj]=q3[ii][jj];	
		}

		if(it%100==0)
			sf_warning("Elastic: it= %d",it);
        }/* it loop */
   t3=clock();
   timespent=(float)(t3-t2)/CLOCKS_PER_SEC;
   sf_warning("CPU time for wavefield extrapolation.: %f(second)",timespent);

   timespent=(float)(t3-t1)/(ns*CLOCKS_PER_SEC);
   sf_warning("All CPU time: %f(second)",timespent);

        free(*p1);
        free(*p2);
        free(*p3);
        free(*q1);
        free(*q2);
        free(*q3);

        free(*vp0);
        free(*vs0);
        free(*epsi);
        free(*del);
        free(*theta);

	exit(0);
}
Esempio n. 5
0
File: psp.c Progetto: 1014511134/src
int psp4(float **wvfld, float **wvfld0, float **dat, float **dat_v, float *vel, pspar par)
/*< pseudo-spectral wave extrapolation using wavefield injection >*/
{
    /*survey parameters*/
    int   nx, nz;
    float dx, dz;
    int   n_srcs;
    int   *spx, *spz;
    int   gpz, gpx, gpl;
    int   gpz_v, gpx_v, gpl_v;
    int   snap;
    /*fft related*/
    bool  cmplx;
    int   pad1;
    /*absorbing boundary*/
    bool abc;
    int nbt, nbb, nbl, nbr;
    float ct,cb,cl,cr;
    /*source parameters*/
    int src; /*source type*/
    int nt;
    float dt,*f0,*t0,*A;
    /*misc*/
    bool verb, ps;
    float vref;
    
    int nx1, nz1; /*domain of interest*/
    int it,iz,ik,ix,i,j;     /* index variables */
    int nk,nzx,nz2,nx2,nzx2,nkz,nth;
    int it1, it2, its;
    float dkx,dkz,kx0,kz0,vref2,kx,kz,k,t;
    float c, old;

    /*wave prop arrays*/
    float *vv;
    sf_complex *cwave,*cwavem;
    float *wave,*curr,*prev,*lapl;

    /*source*/
    float **rick;
    float freq;
    int fft_size;

    /*passing the parameters*/
    nx    = par->nx;
    nz    = par->nz;
    dx    = par->dx;
    dz    = par->dz;
    n_srcs= par->n_srcs;
    spx   = par->spx;
    spz   = par->spz;
    gpz   = par->gpz;
    gpx   = par->gpx;
    gpl   = par->gpl;
    gpz_v = par->gpz_v;
    gpx_v = par->gpx_v;
    gpl_v = par->gpl_v;
    snap  = par->snap;
    cmplx = par->cmplx;
    pad1  = par->pad1;
    abc   = par->abc;
    nbt   = par->nbt;
    nbb   = par->nbb;
    nbl   = par->nbl;
    nbr   = par->nbr;
    ct    = par->ct;
    cb    = par->cb;
    cl    = par->cl;
    cr    = par->cr;
    src   = par->src;
    nt    = par->nt;
    dt    = par->dt;
    f0    = par->f0;
    t0    = par->t0;
    A     = par->A;
    verb  = par->verb;
    ps    = par->ps;
    vref  = par->vref;
    

#ifdef _OPENMP
#pragma omp parallel
    {
      nth = omp_get_num_threads();
    }
#else
    nth = 1;
#endif
    if (verb) sf_warning(">>>> Using %d threads <<<<<", nth);

    nz1 = nz-nbt-nbb;
    nx1 = nx-nbl-nbr;

    nk = fft2_init(cmplx,pad1,nz,nx,&nz2,&nx2);
    nzx = nz*nx;
    nzx2 = nz2*nx2;
    
    dkz = 1./(nz2*dz); kz0 = (cmplx)? -0.5/dz:0.;
    dkx = 1./(nx2*dx); kx0 = -0.5/dx;
    nkz = (cmplx)? nz2:(nz2/2+1);
    if(nk!=nx2*nkz) sf_error("wavenumber dimension mismatch!");
    sf_warning("dkz=%f,dkx=%f,kz0=%f,kx0=%f",dkz,dkx,kz0,kx0);
    sf_warning("nk=%d,nkz=%d,nz2=%d,nx2=%d",nk,nkz,nz2,nx2);

    if(abc)
      abc_init(nz,nx,nz2,nx2,nbt,nbb,nbl,nbr,ct,cb,cl,cr);

    /* allocate and read/initialize arrays */
    vv     = sf_floatalloc(nzx); 
    lapl   = sf_floatalloc(nk);
    wave   = sf_floatalloc(nzx2);
    curr   = sf_floatalloc(nzx2);
    prev   = sf_floatalloc(nzx2);
    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);
    if (src==0) {
      rick = sf_floatalloc2(nt,n_srcs);
      for (i=0; i<n_srcs; i++) {
	for (it=0; it<nt; it++) {
	  rick[i][it] = 0.f;
	}
	rick[i][(int)(t0[i]/dt)] = A[i]; /*time delay*/
	freq = f0[i]*dt;           /*peak frequency*/
	fft_size = 2*kiss_fft_next_fast_size((nt+1)/2);
	ricker_init(fft_size, freq, 0);
	sf_freqfilt(nt,rick[i]);
	ricker_close();
      }
    } else rick = NULL;

    for (iz=0; iz < nzx; iz++) {
        vv[iz] = vel[iz]*vel[iz]*dt*dt;
    }
    vref *= dt;
    vref2 = vref*vref;
    for (iz=0; iz < nzx2; iz++) {
	curr[iz] = 0.;
	prev[iz] = 0.;
    }

    /* constructing the pseudo-analytical op */
    for (ix=0; ix < nx2; ix++) {
	kx = kx0+ix*dkx;
	for (iz=0; iz < nkz; iz++) {
	    kz = kz0+iz*dkz;
	    k = 2*SF_PI*hypot(kx,kz);
	    if (ps) lapl[iz+ix*nkz] = -k*k;
	    else lapl[iz+ix*nkz] = 2.*(cos(vref*k)-1.)/vref2;
	}
    }

    /* modeling */
    /* step forward in time */
    it1 = 0;
    it2 = nt;
    its = +1;

    /* MAIN LOOP */
    for (it=it1; it!=it2; it+=its) {
      
        if(verb) sf_warning("it=%d/%d;",it,nt);
	
	/* matrix multiplication */
	fft2(curr,cwave);

	for (ik = 0; ik < nk; ik++) {
#ifdef SF_HAS_COMPLEX_H
	  cwavem[ik] = cwave[ik]*lapl[ik];
#else
	  cwavem[ik] = sf_cmul(cwave[ik],lapl[ik]);
#endif
	}
	
	ifft2(wave,cwavem);

#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,iz,i,j,old,c)
#endif
	for (ix = 0; ix < nx; ix++) {
	    for (iz=0; iz < nz; iz++) {
		i = iz+ix*nz;  /* original grid */
		j = iz+ix*nz2; /* padded grid */

		old = c = curr[j];
		c += c - prev[j];
		prev[j] = old;
		c += wave[j]*vv[i];
		curr[j] = c;
	    }
	}

        if (NULL!=wvfld0) { /* wavefield injection */
          if (snap > 0 && it%snap==0) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,iz,i,j,ik)
#endif
            for (ix=0; ix<nx1; ix++) {
              for (iz=0; iz<nz1; iz++) {
                i = iz + nz1*ix;
                j = iz+nbt + (ix+nbl)*nz2; /* padded grid */
                ik = iz+nbt + (ix+nbl)*nz; /* padded grid */
                curr[j] += vv[ik]*wvfld0[it/snap][i];
              }
            }
          }
        } else { /* source injection */
          t = it*dt;
          for (i=0; i<n_srcs; i++) {
            for(ix=-1;ix<=1;ix++) {
              for(iz=-1;iz<=1;iz++) {
                ik = spz[i]+iz+nz*(spx[i]+ix);
                j = spz[i]+iz+nz2*(spx[i]+ix);
                if (src==0) {
                  curr[j] += vv[ik]*rick[i][it]/(abs(ix)+abs(iz)+1);
                } else {
                  curr[j] += vv[ik]*Ricker(t, f0[i], t0[i], A[i])/(abs(ix)+abs(iz)+1);
                }
              }
            }
          }
        }

	/*apply abc*/
	if (abc) {
	  abc_apply(curr);
	  abc_apply(prev);
	}

        /* record data */
        if (NULL!=dat) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix)
#endif
            for (ix = 0; ix < gpl; ix++) {
                dat[ix][it] = curr[gpz+(ix+gpx)*nz2];
            }
        }
        if (NULL!=dat_v) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(iz)
#endif
            for (iz = 0; iz < gpl_v; iz++) {
                dat_v[iz][it] = curr[gpz_v+iz+(gpx_v)*nz2];
            }
        }

	/* save wavefield */
	if (snap > 0 && it%snap==0) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,iz,i,j)
#endif
	  for (ix=0; ix<nx1; ix++) {
	    for (iz=0; iz<nz1; iz++) {
	      i = iz + nz1*ix;
	      j = iz+nbt + (ix+nbl)*nz2; /* padded grid */
	      wvfld[it/snap][i] = curr[j];
	    }
	  }
	}
    }
    if(verb) sf_warning(".");

    /*free up memory*/
    fft2_finalize();
    if (abc) abc_close();
    free(vv);
    free(lapl);   
    free(wave);
    free(curr);
    free(prev);
    free(cwave);
    free(cwavem);
    
    return 0;
}
Esempio n. 6
0
int main(int argc, char* argv[])
{
    int	ix, iz, jx, jz, ixf, izf, ixx, izz, i,j,im, jm,nx,nz,nxf,nzf,nxpad,nzpad,it,ii,jj;
    float   kxmax,kzmax;

    float   A, f0, t, t0, dx, dz, dxf, dzf, dt, dkx, dkz, dt2, div;
    int     mm, nvx, nvz, ns;
    int     hnkx, hnkz, nkx, nkz, nxz, nkxz;
    int     hnkx1=1, hnkz1=1, nkx1, nkz1;
    int     isx, isz, isxm, iszm; /*source location */
    int     itaper; /* tapering or not for spectrum of oprtator*/

    int     nstep;            /* every nstep in spatial grids to calculate filters sparsely*/

    float   *coeff_1dx, *coeff_1dz, *coeff_2dx, *coeff_2dz; /* finite-difference coefficient */

    float **apvx, **apvz, **apvxx, **apvzz;    /* projection deviation operator of P-wave for a location */

    float ****ex=NULL, ****ez=NULL;                      /* operator for whole model for P-wave*/
    float **exx=NULL, **ezz=NULL;                        /* operator for constant model for P-wave*/

    float **vp0, **vs0, **epsi, **del, **theta;         /* velocity model */
    float **p1, **p2, **p3, **q1, **q2, **q3, **p3c=NULL, **q3c=NULL, **sum=NULL;  /* wavefield array */
    float *kx, *kz, *kkx, *kkz, *kx2, *kz2, **taper;

    clock_t t2, t3, t4, t5;
    float   timespent, fx,fz; 
    char    *tapertype;

    int     isep=1;
    int     ihomo=1;

    double  vp2, vs2, ep2, de2, the;

    sf_file Fo1, Fo2, Fo3, Fo4, Fo5, Fo6, Fo7, Fo8;
    sf_file Fvp0, Fvs0, Feps, Fdel, Fthe;

    sf_axis az, ax;
       
    sf_init(argc,argv);

    /* t1=clock(); */
 
    /*  wavelet parameter for source definition */
    f0=30.0;                  
    t0=0.04;                  
    A=1.0;                  

    /* time samping paramter */
    if (!sf_getint("ns",&ns)) ns=301;
    if (!sf_getfloat("dt",&dt)) dt=0.001;
    if (!sf_getint("isep",&isep)) isep=0;             /* if isep=1, separate wave-modes */
    if (!sf_getint("ihomo",&ihomo)) ihomo=0;          /* if ihomo=1, homogeneous medium */
    if (NULL== (tapertype=sf_getstring("tapertype"))) tapertype="D"; /* taper type*/
    if (!sf_getint("nstep",&nstep)) nstep=1; /* grid step to calculate operators: 1<=nstep<=5 */

    sf_warning("isep=%d",isep);
    sf_warning("ihomo=%d",ihomo);
    sf_warning("tapertype=%s",tapertype);
    sf_warning("nstep=%d",nstep);

    sf_warning("ns=%d dt=%f",ns,dt);
    sf_warning("read velocity model parameters");

    /* setup I/O files */
    Fvp0 = sf_input ("in");  /* vp0 using standard input */
    Fvs0 = sf_input ("vs0");  /* vs0 */
    Feps = sf_input ("epsi");  /* epsi */
    Fdel = sf_input ("del");  /* delta */
    Fthe = sf_input ("the");  /* theta */

    /* Read/Write axes */
    az = sf_iaxa(Fvp0,1); nvz = sf_n(az); dz = sf_d(az)*1000.0;
    ax = sf_iaxa(Fvp0,2); nvx = sf_n(ax); dx = sf_d(ax)*1000.0;
    fx=sf_o(ax);
    fz=sf_o(az);

    /* source definition */
    isx=nvx/2;
    isz=nvz/2;
    /* isz=nvz*2/5; */

    /* wave modeling space */
    nx=nvx;
    nz=nvz;
    nxpad=nx+2*_m;
    nzpad=nz+2*_m;

    sf_warning("dx=%f dz=%f",dx,dz);

    sf_warning("nx=%d nz=%d nxpad=%d nzpad=%d", nx,nz,nxpad,nzpad);

    vp0=sf_floatalloc2(nz,nx);	
    vs0=sf_floatalloc2(nz,nx);	
    epsi=sf_floatalloc2(nz,nx);	
    del=sf_floatalloc2(nz,nx);	
    theta=sf_floatalloc2(nz,nx);	

    nxz=nx*nz;
    mm=2*_m+1;

    dt2=dt*dt;
    isxm=isx+_m;  /* source's x location */
    iszm=isz+_m;  /* source's z-location */

    /* read velocity model */
    sf_floatread(vp0[0],nxz,Fvp0);
    sf_floatread(vs0[0],nxz,Fvs0);
    sf_floatread(epsi[0],nxz,Feps);
    sf_floatread(del[0],nxz,Fdel);
    sf_floatread(theta[0],nxz,Fthe);

    for(i=0;i<nx;i++)
        for(j=0;j<nz;j++)
	    theta[i][j] *= SF_PI/180.0;

    t2=clock();

    /* setup I/O files */
    Fo1 = sf_output("out"); /* pseudo-pure P-wave x-component */
    Fo2 = sf_output("PseudoPurePz"); /* pseudo-pure P-wave z-component */
    Fo3 = sf_output("PseudoPureP"); /* scalar P-wave field using divergence operator */
    puthead3(Fo1, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz, fx, 0.0);
    puthead3(Fo2, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz, fx, 0.0);
    puthead3(Fo3, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz, fx, 0.0);

    /*****************************************************************************
     *  Calculating polarization deviation operator for wave-mode separation
     * ***************************************************************************/
    if(isep==1)
    {
	/* calculate spatial steps for operater in sparsely sampling grid point */
	dxf=dx*nstep;
	dzf=dz*nstep;
	nxf=nx/nstep+1;
	nzf=nz/nstep+1;

	/* operators length for calculation */
	hnkx=400.0/dx;
	hnkz=400.0/dz;
	nkx=2*hnkx+1;   /* operator length in kx-direction */
	nkz=2*hnkz+1;   /* operator length in kz-direction */

	/* truncated spatial operators length for filtering*/
	hnkx1=200.0/dx;
	hnkz1=200.0/dz;
	nkx1=2*hnkx1+1;
	nkz1=2*hnkz1+1;

	sf_warning("nx=%d nz=%d nxf=%d nzf=%d", nx,nz,nxf,nzf);
	sf_warning("dx=%f dz=%f dxf=%f dzf=%f", dx,dz,dxf,dzf);

	sf_warning("hnkx=%d hnkz=%d nkx=%d nkz=%d", hnkx, hnkz, nkx, nkz);
	sf_warning("hnkx1=%d hnkz1=%d nkx1=%d nkz1=%d", hnkx1, hnkz1, nkx1, nkz1);

	dkx=2*SF_PI/dx/nkx;
	dkz=2*SF_PI/dz/nkz;
	kxmax=SF_PI/dx;
	kzmax=SF_PI/dz;

	kx=sf_floatalloc(nkx);
	kz=sf_floatalloc(nkx);
	kkx=sf_floatalloc(nkx);
	kkz=sf_floatalloc(nkx);
	kx2=sf_floatalloc(nkx);
	kz2=sf_floatalloc(nkx);

	taper=sf_floatalloc2(nkz, nkx);

	/* define axis samples and taper in wavenumber domain */
	kxkztaper(kx, kz, kkx, kkz, kx2, kz2, taper, nkx, nkz, hnkx, hnkz, dkx, dkz, kxmax, kzmax, tapertype);
           
	/* truncation of spatial filter */
	p3c=sf_floatalloc2(nz,nx);
	q3c=sf_floatalloc2(nz,nx);
	sum=sf_floatalloc2(nz,nx);

	if(ihomo==1)
	{
	    exx=sf_floatalloc2(nkz1, nkx1);
	    ezz=sf_floatalloc2(nkz1, nkx1);
	}
	else{ /* to store spatail varaied operators */
	    ex=sf_floatalloc4(nkz1, nkx1, nzf, nxf);
	    ez=sf_floatalloc4(nkz1, nkx1, nzf, nxf);
	}
	nkxz=nkx*nkz;

	apvx=sf_floatalloc2(nkz, nkx);
	apvz=sf_floatalloc2(nkz, nkx);
	apvxx=sf_floatalloc2(nkz, nkx);
	apvzz=sf_floatalloc2(nkz, nkx);

	/* setup I/O files */
	Fo4 = sf_output("apvx"); /* P-wave projection deviation x-comp */
	Fo5 = sf_output("apvz"); /* P-wave projection deviation z-comp */
	Fo6 = sf_output("apvxx"); /* P-wave projection deviation x-comp in (x,z) domain */
	Fo7 = sf_output("apvzz"); /* P-wave projection deviation z-comp in (x,z) domain */

	puthead1(Fo4, nkz, nkx, dkz, -kzmax, dkx, -kxmax);
	puthead1(Fo5, nkz, nkx, dkz, -kzmax, dkx, -kxmax);

	puthead2(Fo6, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);
	puthead2(Fo7, nkz, nkx, dz/1000.0, 0.0, dx/1000.0, 0.0);

	/*************calculate projection deviation grid-point by grid-point **********/
	for(ix=0,ixf=0;ix<nx;ix+=nstep,ixf++)
	{
	    for(iz=0,izf=0;iz<nz;iz+=nstep,izf++)
	    {
	        vp2=vp0[ix][iz]*vp0[ix][iz];
	        vs2=vs0[ix][iz]*vs0[ix][iz];
	        ep2=1.0+2*epsi[ix][iz];
	        de2=1.0+2*del[ix][iz];
	        the=theta[ix][iz];

                if(ixf%10==0&&izf%100==0) sf_warning("Operator: nxf=%d ixf=%d izf=%d vp2=%f vs2=%f",nxf, ixf,izf,vp2,vs2);

   	        /*************calculate projection deviation without tapering **********/
                itaper=0;
                /* devvtip: projection deviation operators for P-wave in TTI media */
                devttip(apvx,apvz,kx,kz,kkx,kkz,taper,hnkx,hnkz,dkx,dkz,vp2,vs2,ep2,de2,the,itaper);

                /* inverse Fourier transform */
                kxkz2xz(apvx, apvxx, hnkx, hnkz, nkx, nkz);
                kxkz2xz(apvz, apvzz, hnkx, hnkz, nkx, nkz);

                /* truncation and saving of operator in space-domain */
                if(ihomo==1)
                {
		    for(jx=-hnkx1,ixx=hnkx-hnkx1;jx<=hnkx1;jx++,ixx++)
			for(jz=-hnkz1,izz=hnkz-hnkz1;jz<=hnkz1;jz++,izz++)
			{
			    exx[jx+hnkx1][jz+hnkz1]=apvxx[ixx][izz];
			    ezz[jx+hnkx1][jz+hnkz1]=apvzz[ixx][izz];
			}
                }else{
		    for(jx=-hnkx1,ixx=hnkx-hnkx1;jx<=hnkx1;jx++,ixx++)
			for(jz=-hnkz1,izz=hnkz-hnkz1;jz<=hnkz1;jz++,izz++)
			{
			    ex[ixf][izf][jx+hnkx1][jz+hnkz1]=apvxx[ixx][izz];
			    ez[ixf][izf][jx+hnkx1][jz+hnkz1]=apvzz[ixx][izz];
			}
                }
                if((ix==nx/2&&iz==nz/2&&ihomo==0)||ihomo==1)
                {
		    sf_floatwrite(apvx[0], nkxz, Fo4);
		    sf_floatwrite(apvz[0], nkxz, Fo5);

		    sf_floatwrite(apvxx[0], nkxz, Fo6);
		    sf_floatwrite(apvzz[0], nkxz, Fo7);
                }
                if(ihomo==1) goto loop;

	    }/* iz loop */
	}/* ix loop */
    loop:;

	free(kx);
	free(kz);
	free(kx2);
	free(kz2);
	free(kkx);
	free(kkz);

	free(*taper);

	free(*apvx);
	free(*apvz);
	free(*apvxx);
	free(*apvzz);
    }/* isep */
    /****************End of Calculating Projection Deviation Operator****************/
    t3=clock();
    timespent=(float)(t3-t2)/CLOCKS_PER_SEC;
    sf_warning("Computation time (operators): %f (second)",timespent);

    /****************begin to calculate wavefield****************/
    coeff_2dx=sf_floatalloc(mm);
    coeff_2dz=sf_floatalloc(mm);
    coeff_1dx=sf_floatalloc(mm);
    coeff_1dz=sf_floatalloc(mm);

    coeff2d(coeff_2dx,dx);
    coeff2d(coeff_2dz,dz);
    coeff1d(coeff_1dx,dx);
    coeff1d(coeff_1dz,dz);

    p1=sf_floatalloc2(nzpad, nxpad);
    p2=sf_floatalloc2(nzpad, nxpad);
    p3=sf_floatalloc2(nzpad, nxpad);

    q1=sf_floatalloc2(nzpad, nxpad);
    q2=sf_floatalloc2(nzpad, nxpad);
    q3=sf_floatalloc2(nzpad, nxpad);

    zero2float(p1, nzpad, nxpad);
    zero2float(p2, nzpad, nxpad);
    zero2float(p3, nzpad, nxpad);
        
    zero2float(q1, nzpad, nxpad);
    zero2float(q2, nzpad, nxpad);
    zero2float(q3, nzpad, nxpad);
        
    if(isep==1)
    {
        /* setup I/O files */
        Fo8 = sf_output("PseudoPureSepP"); /* scalar P-wave field using polarization projection oprtator*/

        puthead3(Fo8, nz, nx, 1, dz/1000.0, dx/1000.0, dt, fz, fx, 0.0);
    } else {
	Fo8 = NULL;
    }

    sf_warning("==================================================");
    sf_warning("==  Propagation Using Pseudo-Pure P-Wave Eq.    ==");
    sf_warning("==================================================");
    t4=clock();
    for(it=0;it<ns;it++)
    {
	t=it*dt;

	p2[isxm][iszm]+=Ricker(t, f0, t0, A);
	q2[isxm][iszm]+=Ricker(t, f0, t0, A);

	/* fwpttipseudop: forward-propagating in TTI media with pseudo-pure P-wave equation */
	fwpttipseudop(dt2, p1, p2, p3, q1, q2, q3, coeff_2dx, coeff_2dz,
		      dx, dz, nx, nz, nxpad, nzpad, vp0, vs0, epsi, del, theta);

	/******* output wavefields: component and divergence *******/
	if(it==ns-1)
	{
	    for(i=0;i<nx;i++)
	    {
		im=i+_m;
		for(j=0;j<nz;j++)
		{
		    jm=j+_m;
		    sf_floatwrite(&p3[im][jm],1,Fo1);
		    sf_floatwrite(&q3[im][jm],1,Fo2);

		    div=p3[im][jm]+q3[im][jm];

		    sf_floatwrite(&div,1,Fo3);
		}
	    }/* i loop*/

	    /* correct projection error for accurate separate qP wave in spatial domain */
	    if(isep==1)
	    {
		zero2float(p3c,nz,nx);
		zero2float(q3c,nz,nx);
		zero2float(sum, nz, nx);

		if(ihomo==1)
		    filter2dsepglobal(p3, q3, p3c, q3c, exx, ezz, nx, nz, hnkx1, hnkz1);
		else
		    filter2dsep(p3, q3, p3c, q3c, ex, ez, nx, nz, nstep, hnkx1, hnkz1);

		for(i=0;i<nx;i++)
		    for(j=0;j<nz;j++)
			sum[i][j]=p3c[i][j]+q3c[i][j];

		sf_floatwrite(sum[0],nx*nz, Fo8);
	    }
	}/* (it+1)%ntstep==0 */

	/**************************************/
	for(i=0,ii=_m;i<nx;i++,ii++)
	    for(j=0,jj=_m;j<nz;j++,jj++)
	    {
		p1[ii][jj]=p2[ii][jj];	
		p2[ii][jj]=p3[ii][jj];	

		q1[ii][jj]=q2[ii][jj];	
		q2[ii][jj]=q3[ii][jj];	
	    }

	if(it%50==0)
	    sf_warning("Pseudo: it= %d",it);
    }/* it loop */
    t5=clock();
    timespent=(float)(t5-t4)/CLOCKS_PER_SEC;
    sf_warning("Computation time (propagation + separation): %f (second)",timespent);

    if(isep==1)
    {
	free(*p3c);
	free(*q3c);
	free(*sum);

	if(ihomo==1)
	{
	    free(*exx);
	    free(*ezz);
	}else{
	    free(***ex);
	    free(***ez);
	}
    }

    free(*p1);
    free(*p2);
    free(*p3);
    free(*q1);
    free(*q2);
    free(*q3);

    free(*vp0);
    free(*vs0);
    free(*epsi);
    free(*del);
    free(*theta);

    exit(0);
}