Пример #1
0
int psp3(float **wvfld, float **wvfld1, float **dat, float **dat1, float *img, float *vel, pspar par)
/*< pseudo-spectral de-migration/migration >*/
{
    /*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;
    float c, old;

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

    /*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);
    curr1  = sf_floatalloc(nzx2);
    prev   = sf_floatalloc(nzx2);
    prev1  = sf_floatalloc(nzx2);
    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);

    for (iz=0; iz < nzx; iz++) {
        vv[iz] = vel[iz]*dt;
    }
    vref *= dt;
    vref2 = vref*vref;
    for (iz=0; iz < nzx2; iz++) {
	curr[iz] = 0.;
	curr1[iz] = 0.;
	prev[iz] = 0.;
	prev1[iz] = 0.;
    }
    for (iz=0; iz < nz1*nx1; iz++) {
        img[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;
	}
    }

    /* step backward in time */
    it1 = nt-1;
    it2 = -1;
    its = -1;	

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

	/* inject data */
	if (NULL!=dat) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix)
#endif
	    for (ix = 0; ix < gpl; ix++) {
	        curr[gpz+(ix+gpx)*nz2] += dat[ix][it];
	    }
	}
	if (NULL!=dat1) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix)
#endif
	    for (ix = 0; ix < gpl; ix++) {
	        curr1[gpz+(ix+gpx)*nz2] += dat1[ix][it];
	    }
	}
	
        /* first receiver wavefield */
	/* 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]*vv[i];
		curr[j] = c;
	    }
	}

	/*apply abc*/
	if (abc) {
	  abc_apply(curr);
	  abc_apply(prev);
	}
	
	/* second receiver wavefield */
	/* matrix multiplication */
	fft2(curr1,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 = curr1[j];
		c += c - prev1[j];
		prev1[j] = old;
		c += wave[j]*vv[i]*vv[i];
		curr1[j] = c;
	    }
	}

	/*apply abc*/
	if (abc) {
	  abc_apply(curr1);
	  abc_apply(prev1);
	}

	/* cross-correlation imaging condition */
	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 */
		    img[i] += curr[j]*curr1[j];
		}
	    }
	}
	
	/* 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];
              wvfld1[it/snap][i] = curr1[j];
	    }
	  }
	}
    }
    if(verb) sf_warning(".");

    /*free up memory*/
    fft2_finalize();
    if (abc) abc_close();
    free(vv);
    free(lapl);   
    free(wave);
    free(curr);
    free(curr1);
    free(prev);
    free(prev1);
    free(cwave);
    free(cwavem);
    
    return 0;
}
Пример #2
0
int mcfft3_init(int pad1           /* padding on the first axis */,
	       int nx,   int ny,  int nz   /* input data size */, 
	       int *nx2, int *ny2, int *nz2 /* padded data size */,
               int *n_local, int *o_local /* local size & start */)
/*< initialize >*/
{
  int i, nth=1;
  int cpuid;

  MPI_Comm_rank(MPI_COMM_WORLD, &cpuid);

  fftwf_mpi_init();

  /* axis 1 */
  nk = n1 = kiss_fft_next_fast_size(nx*pad1);
  /* axis 2 */
  n2 = kiss_fft_next_fast_size(ny);
  /* axis 3 */
  n3 = kiss_fft_next_fast_size(nz);

  alloc_local = fftwf_mpi_local_size_2d_transposed(n3, n2*n1, MPI_COMM_WORLD, &local_n0, &local_0_start, &local_n1, &local_1_start);

  cc = sf_complexalloc(n1*n2*local_n0);

  /* kiss-fft */

#ifdef _OPENMP
#pragma omp parallel
  {nth = omp_get_num_threads();}
#endif

  cfg1  = (kiss_fft_cfg *) sf_alloc(nth,sizeof(kiss_fft_cfg));
  icfg1 = (kiss_fft_cfg *) sf_alloc(nth,sizeof(kiss_fft_cfg));
  cfg2  = (kiss_fft_cfg *) sf_alloc(nth,sizeof(kiss_fft_cfg));
  icfg2 = (kiss_fft_cfg *) sf_alloc(nth,sizeof(kiss_fft_cfg));
  cfg3  = (kiss_fft_cfg *) sf_alloc(nth,sizeof(kiss_fft_cfg));
  icfg3 = (kiss_fft_cfg *) sf_alloc(nth,sizeof(kiss_fft_cfg));

  for (i=0; i < nth; i++) {
    cfg1[i] = kiss_fft_alloc(n1,0,NULL,NULL);
    icfg1[i]= kiss_fft_alloc(n1,1,NULL,NULL);
    cfg2[i] = kiss_fft_alloc(n2,0,NULL,NULL);
    icfg2[i]= kiss_fft_alloc(n2,1,NULL,NULL);
    cfg3[i] = kiss_fft_alloc(n3,0,NULL,NULL);
    icfg3[i]= kiss_fft_alloc(n3,1,NULL,NULL);
  }

  ctrace2= (kiss_fft_cpx **) sf_complexalloc2(n2,nth);
  ctrace3= (kiss_fft_cpx **) sf_complexalloc2(n3,nth);

  //tmp = (kiss_fft_cpx *) sf_complexalloc(alloc_local);
  tmp =    (kiss_fft_cpx *) sf_alloc(alloc_local,sizeof(kiss_fft_cpx));
  tmp2= (sf_complex *) tmp;

  /* fftw for transpose */

  cfg = fftwf_mpi_plan_many_transpose(n3,n2*n1,2,
                              FFTW_MPI_DEFAULT_BLOCK,FFTW_MPI_DEFAULT_BLOCK,
                              (float *) tmp,
                              (float *) tmp,
                              MPI_COMM_WORLD,
                              FFTW_MEASURE);

  icfg= fftwf_mpi_plan_many_transpose(n2*n1,n3,2,
                              FFTW_MPI_DEFAULT_BLOCK,FFTW_MPI_DEFAULT_BLOCK,
                              (float *) tmp,
                              (float *) tmp,
                              MPI_COMM_WORLD,
                              FFTW_MEASURE);

  if (NULL == cfg || NULL == icfg) sf_error("FFTW failure.");

  *nx2 = n1;
  *ny2 = n2;
  *nz2 = n3;
  *n_local = (int) local_n0;
  *o_local = (int) local_0_start;
	
  wt =  1.0/(n3*n2*n1);

  return (nk*n2*n3);
}
Пример #3
0
int main(int argc, char* argv[])
{
    int i, iter, niter;
    sf_complex *buf, *buf2;
    double rn, rnp, alpha, beta;
    off_t nm, nd, msiz, dsiz, pos;
    size_t nbuf, mbuf, dbuf, len, iolen, cmdlen;
    FILE *xfile, *Rfile, *gfile, *Gfile, *sfile, *Sfile;
    char *x, *R, *g, *G, *s, *S, *prog, *cmdline, *iostring, *arg;
    sf_file mod, dat, x0;  /* input */
    sf_file Rrsf, Grsf, grsf, out; /* output */

    extern int fseeko(FILE *stream, off_t offset, int whence);
    extern off_t ftello (FILE *stream);

    sf_init(argc,argv);

    dat = sf_input("--input");
    mod = sf_input("mod");

    out = sf_output("--output");
    sf_fileflush(out,mod);
    sf_settype(out,SF_COMPLEX);

    if (SF_COMPLEX != sf_gettype(mod) ||
	SF_COMPLEX != sf_gettype(dat)) 
	sf_error("Need complex type in mod and dat");

    for (i=0; i < argc-1; i++) {
	argv[i]=argv[i+1];
    }
    for (i=0; i < argc-1; i++) {	
	/* find the program to run */
	if (NULL == strchr(argv[i],'=')) {
	    /* first one without the '=' */
	    prog = argv[0];
	    argv[0] = argv[i];
	    argv[i] = prog;
	    break;
	}
    }

    argv[argc-1] = sf_charalloc(6);
    snprintf(argv[argc-1],6,"adj=X");

    if (!sf_getint("niter",&niter)) niter=1;
    /* number of iterations */

    Rfile = sf_tempfile(&R,"w+"); 
    xfile = sf_tempfile(&x,"w+b"); 
    gfile = sf_tempfile(&g,"w+");
    Gfile = sf_tempfile(&G,"w+");
    sfile = sf_tempfile(&s,"w+b");
    Sfile = sf_tempfile(&S,"w+b");

    fclose(Rfile); Rrsf = sf_output(R); sf_readwrite(Rrsf,true); sf_fileflush(Rrsf,dat);
    fclose(xfile);
    fclose(gfile); 
    fclose(Gfile); 
    fclose(sfile);
    fclose(Sfile);

    nm = sf_filesize(mod);
    nd = sf_filesize(dat);

    /* I/O buffers */
    nbuf = BUFSIZ/sizeof(sf_complex);
    buf  = sf_complexalloc(nbuf);
    buf2 = sf_complexalloc(nbuf);

    cmdline = sf_charalloc(SF_CMDLEN);
    iostring = sf_charalloc(SF_CMDLEN);
    cmdlen = 0;
    for (i=0; i < argc; i++) {
	arg = argv[i];
	len = strlen(arg);
	if (cmdlen+len > SF_CMDLEN-2) sf_error("command line is too long");

	strncpy(cmdline+cmdlen,arg,len);
	cmdline[cmdlen+len]=' ';
	cmdlen += len+1;
    }

    if (NULL != sf_getstring("x0")) {
	x0 = sf_input("x0"); /* initial model */
    } else {
	x0 = NULL;
    }

    for (iter=0; iter < niter; iter++) {
	if (0 == iter) {
	    xfile = fopen(x,"wb");
	    
	    if (NULL == x0) {
		for (i=0; i < nbuf; i++) { buf[i] = sf_cmplx(0.0f,0.0f); }
	    }
	    
	    MLOOP( if (NULL != x0) sf_complexread(buf,mbuf,x0);
		   MWRITE(xfile); );
	    
	    fclose(xfile);
#ifdef SF_HAS_COMPLEX_H 
	    DLOOP( sf_complexread(buf,dbuf,dat); 
		   for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; }
		   sf_complexwrite(buf,dbuf,Rrsf); );
Пример #4
0
int lrosback2(sf_complex **img, sf_complex ***wavfld, float **sill, sf_complex **rcd, bool adj,
	      bool verb, bool wantwf, sf_complex **lt, sf_complex **rt, int m2,
              geopar geop, int pad1, bool illum)
/*< low-rank one-step backward propagation + imaging >*/
{
    int it,iz,im,ik,ix,i,j;     /* index variables */
    int nxb,nzb,gpz,gpx,gpl,snpint,wfit;
    int nt,nz,nx, nk, nz2, nx2, nzx2;
    sf_complex c;
    sf_complex *cwave, *cwavem, *currm;
    sf_complex **wave, *curr;
    sf_complex **ccr;

    nx = geop->nx;
    nz = geop->nz;
    nxb = geop->nxb;
    nzb = geop->nzb;
/*     dx = geop->dx;
       dz = geop->dz; */
    
    gpz  = geop->gpz;
    gpx  = geop->gpx;
    gpl  = geop->gpl;
    snpint = geop->snpint;
    
    nt = geop->nt;
    /* dt = geop->dt; */

    ccr = sf_complexalloc2(nz, nx);

    nk = cfft2_init(pad1,nzb,nxb,&nz2,&nx2);
    /* nzx = nzb*nxb; */
    nzx2 = nz2*nx2;

    curr = sf_complexalloc(nzx2);
    cwave  = sf_complexalloc(nk);
    wave = sf_complexalloc2(nzx2,m2);

    if (!adj) {
	currm  = sf_complexalloc(nzx2);
	icfft2_allocate(cwave);
    } else {
	cwavem = sf_complexalloc(nk);
	icfft2_allocate(cwavem);
    }

#ifdef _OPENMP
#pragma omp parallel for private(iz)
#endif
    for (iz=0; iz < nzx2; iz++) {
	curr[iz] = sf_cmplx(0.,0.);
    }

#ifdef _OPENMP
#pragma omp parallel for private(ix, iz)
#endif
    for (ix = 0; ix < nx; ix++) {
	for (iz = 0; iz < nz; iz++) {
	  ccr[ix][iz] = sf_cmplx(0.,0.);
	}
    }

    if (adj) { /* migration */
      /* step backward in time (PSPI) */
      /*Main loop*/
      wfit = (int)(nt-1)/snpint;
      for (it = nt-1; it>=0; it--) {
	if  (verb) sf_warning("Backward receiver it=%d/%d;", it, nt-1);
#ifdef _OPENMP
#pragma omp parallel for private(ix,j)
#endif
        for (ix=0; ix<gpl; ix++)  {
	  j = (gpz+geop->top)+(ix+gpx+geop->lft)*nz2; /* padded grid */
#ifdef SF_HAS_COMPLEX_H
	  curr[j]+=rcd[ix][it]; /* data injection */
#else
	  curr[j]=sf_cadd(curr[j],rcd[ix][it]);
#endif
	}
	/*matrix multiplication*/
	cfft2(curr,cwave);
	for (im = 0; im < m2; im++) {
#ifdef _OPENMP
#pragma omp parallel for private(ik)
#endif
	  for (ik = 0; ik < nk; ik++) {
#ifdef SF_HAS_COMPLEX_H
	    cwavem[ik] = cwave[ik]*rt[ik][im];
#else
	    cwavem[ik] = sf_cmul(cwave[ik],rt[ik][im]);
#endif
	  }
	  icfft2(wave[im],cwavem);
	}
	
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,i,j,im,c) shared(curr,lt,wave)
#endif
	for (ix = 0; ix < nxb; ix++) {
	  for (iz=0; iz < nzb; iz++) {
	    i = iz+ix*nzb;  /* original grid */
	    j = iz+ix*nz2; /* padded grid */
	    c = sf_cmplx(0.,0.); // initialize
	    for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
	      c += lt[im][i]*wave[im][j];
#else
	      c = sf_cadd(c,sf_cmul(lt[im][i], wave[im][j]));
#endif
	    }
	    curr[j] = c;
	  }
	}

	/*cross-correlation imaging condition*/
	if (it%snpint == 0 ) {
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,j)
#endif
	  for (ix=0; ix<nx; ix++) {
	    for (iz=0; iz<nz; iz++) {
	      j = (iz+geop->top)+(ix+geop->lft)*nz2; /* padded grid */
#ifdef SF_HAS_COMPLEX_H
	      ccr[ix][iz] += conjf(wavfld[wfit][ix][iz])*curr[j];
#else
	      ccr[ix][iz] = sf_cadd(ccr[ix][iz],sf_cmul(conjf(wavfld[wfit][ix][iz]),curr[j]));
#endif
	    }
	  }
	  wfit--;
	}
      } /*Main loop*/
      if (verb) sf_warning(".");
#ifdef _OPENMP
#pragma omp parallel for private(ix, iz)
#endif    
      for (ix=0; ix<nx; ix++) {
	for (iz=0; iz<nz; iz++) {
	  if (illum) {
#ifdef SF_HAS_COMPLEX_H
	    img[ix][iz] = ccr[ix][iz]/(sill[ix][iz]+SF_EPS);
#else
	    img[ix][iz] = sf_crmul(ccr[ix][iz],1./(sill[ix][iz]+SF_EPS));
#endif
	  } else img[ix][iz] = ccr[ix][iz];
	}
      } 
    } else { /* modeling */
      /* adjoint of source illumination */
#ifdef _OPENMP
#pragma omp parallel for private(ix, iz)
#endif    
      for (ix=0; ix<nx; ix++) {
	for (iz=0; iz<nz; iz++) {
	  if (illum) {
#ifdef SF_HAS_COMPLEX_H
	    ccr[ix][iz] = img[ix][iz]/(sill[ix][iz]+SF_EPS);
#else
	    ccr[ix][iz] = sf_crmul(img[ix][iz],1./(sill[ix][iz]+SF_EPS));
#endif
	  } else ccr[ix][iz] = img[ix][iz];
	}
      } 
      /* step forward in time (NSPS) */
      /*Main loop*/
      wfit=0;
      for (it=0; it<nt; it++) {
	if (verb) sf_warning("Forward receiver it=%d/%d;", it, nt-1);
	/*adjoint of cross-correlation imaging condition*/
	if (it%snpint == 0 ) {
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,j)
#endif
	  for (ix=0; ix<nx; ix++) {
	    for (iz=0; iz<nz; iz++) {
	      j = (iz+geop->top)+(ix+geop->lft)*nz2; /* padded grid */
#ifdef SF_HAS_COMPLEX_H
	      curr[j] += (wavfld[wfit][ix][iz])*ccr[ix][iz];/* adjoint of ccr[ix][iz] += conjf(wavfld[wfit][ix][iz])*curr[j]; ??? */
#else
	      curr[j] = sf_cadd(curr[j],sf_cmul((wavfld[wfit][ix][iz]),ccr[ix][iz]));
#endif
	    }
	  }
	  wfit++;
	}
	/*matrix multiplication*/
	for (im = 0; im < m2; im++) {
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,i,j) shared(currm,lt,curr)
#endif
	  for (ix = 0; ix < nxb; ix++) {
	    for (iz=0; iz < nzb; iz++) {
	      i = iz+ix*nzb;  /* original grid */
	      j = iz+ix*nz2; /* padded grid */
#ifdef SF_HAS_COMPLEX_H
	      currm[j] = conjf(lt[im][i])*curr[j];
#else
	      currm[j] = sf_cmul(conjf(lt[im][i]), curr[j]);
#endif
	    }
	  }
	  cfft2(currm,wave[im]);
	}
#ifdef _OPENMP
#pragma omp parallel for private(ik,im,c) shared(wave,rt,cwave)
#endif	    
	for (ik = 0; ik < nk; ik++) {
	  c = sf_cmplx(0.,0.);
	  for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
	    c += wave[im][ik]*conjf(rt[ik][im]);
#else
	    c = sf_cadd(c,sf_cmul(wave[im][ik],conjf(rt[ik][im]))); 
#endif
	  }
	  cwave[ik] = c;
	}
	icfft2(curr,cwave);

#ifdef _OPENMP
#pragma omp parallel for private(ix,j)
#endif
        for (ix=0; ix<gpl; ix++)  {
	  j = (gpz+geop->top)+(ix+gpx+geop->lft)*nz2; /* padded grid */
	  rcd[ix][it]=curr[j];
	}
      } /*Main loop*/
    }
    cfft2_finalize();
    return 0;
}
Пример #5
0
int main(int argc, char* argv[])
{
    bool mig, sub;
    int it, nt, ix, nx, iz, nz, nx2, nz2, nzx, nzx2, ih, nh, nh2;
    int im, i, j, m2, it1, it2, its, ikz, ikx, ikh, n2, nk, snap;
    float dt, dx, dz, c, old, dh;
    float *curr, *prev, **img, **dat, **lft, **rht, **wave;
    sf_complex *cwave, *cwavem;
    sf_file data, image, left, right, snaps;

    /*MPI related*/
    int cpuid,numprocs;
    int provided;
    int n_local, o_local, nz_local;
    int ozx2;
    float *sendbuf, *recvbuf, *wave_all;
    int *rcounts, *displs;

    /*wall time*/
    double startTime, elapsedTime;
    double clockZero = 0.0;

    MPI_Init_thread(&argc,&argv,MPI_THREAD_FUNNELED,&provided);
    threads_ok = provided >= MPI_THREAD_FUNNELED;

    sf_init(argc,argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &cpuid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    if (!sf_getbool("mig",&mig)) mig=false;
    /* if n, modeling; if y, migration */

    if (!sf_getint("snap",&snap)) snap=0;
    /* interval for snapshots */

    snaps = (snap > 0)? sf_output("snaps"): NULL;
    /* (optional) snapshot file */

    if (mig) { /* migration */
	data = sf_input("input");
	image = sf_output("output");

	if (!sf_histint(data,"n1",&nh)) sf_error("No n1=");
	if (!sf_histfloat(data,"d1",&dh)) sf_error("No d1=");

	if (!sf_histint(data,"n2",&nx)) sf_error("No n2=");
	if (!sf_histfloat(data,"d2",&dx)) sf_error("No d2=");

	if (!sf_histint(data,"n3",&nt)) sf_error("No n3=");
	if (!sf_histfloat(data,"d3",&dt)) sf_error("No d3=");

	if (!sf_getint("nz",&nz)) sf_error("Need nz=");
	/* time samples (if migration) */
	if (!sf_getfloat("dz",&dz)) sf_error("Need dz=");
	/* time sampling (if migration) */
        
        if (cpuid==0) {
	sf_putint(image,"o1",0.);
	sf_putint(image,"n1",nz);
	sf_putfloat(image,"d1",dz);
	sf_putstring(image,"label1","Depth");
	sf_putint(image,"o2",0.);
	sf_putint(image,"n2",nx);
	sf_putfloat(image,"d2",dx);
	sf_putstring(image,"label2","Midpoint");
	sf_putint(image,"n3",1); /* stack for now */
        }
    } else { /* modeling */
	image = sf_input("input");
	data = sf_output("output");

	if (!sf_histint(image,"n1",&nz)) sf_error("No n1=");
	if (!sf_histfloat(image,"d1",&dz)) sf_error("No d1=");

	if (!sf_histint(image,"n2",&nx)) sf_error("No n2=");
	if (!sf_histfloat(image,"d2",&dx)) sf_error("No d2=");

	if (!sf_getint("nt",&nt)) sf_error("Need nt=");
	/* time samples (if modeling) */
	if (!sf_getfloat("dt",&dt)) sf_error("Need dt=");
	/* time sampling (if modeling) */

	if (!sf_getint("nh",&nh)) sf_error("Need nh=");
        /* offset samples (if modeling) */
	if (!sf_getfloat("dh",&dh)) sf_error("Need dh=");
	/* offset sampling (if modeling) */

        if (cpuid==0) {
	sf_putint(data,"n1",nh);
	sf_putfloat(data,"d1",dh);
	sf_putstring(data,"label1","Half-Offset");
	sf_putint(data,"o2",0.);
	sf_putint(data,"n2",nx);
	sf_putfloat(data,"d2",dx);
	sf_putstring(data,"label2","Midpoint");
	sf_putint(data,"n3",nt);
	sf_putfloat(data,"d3",dt);
	sf_putstring(data,"label3","Time");
	sf_putstring(data,"unit3","s");
        }
    }

    if (cpuid==0) {
    if (NULL != snaps) {
      sf_putint(snaps,"n1",nh);
      sf_putfloat(snaps,"d1",dh);
      sf_putstring(snaps,"label1","Half-Offset");

      sf_putint(snaps,"n2",nx);
      sf_putfloat(snaps,"d2",dx);
      sf_putstring(snaps,"label2","Midpoint");

      sf_putint(snaps,"n3",nz);
      sf_putfloat(snaps,"d3",dz);
      sf_putstring(snaps,"label3","Depth");

      sf_putint(snaps,"n4",nt/snap);
      sf_putfloat(snaps,"d4",dt*snap);
      if (mig) {
        sf_putfloat(snaps,"o4",(nt-1)*dt);
      } else {
        sf_putfloat(snaps,"o4",0.);
      }
      sf_putstring(snaps,"label4","Time");
    }
    }

    /* Mark the starting time. */
    startTime = walltime( &clockZero );

    nk = mcfft3_init(1,nh,nx,nz,&nh2,&nx2,&nz2,&n_local,&o_local);
    nz_local = (n_local < nz-o_local)? n_local:nz-o_local;
    sf_warning("Cpuid=%d,n2=%d,n1=%d,n0=%d,local_n0=%d,local_0_start=%d,nz_local=%d",cpuid,nh2,nx2,nz2,n_local,o_local,nz_local);
    if (cpuid==0)
      if (o_local!=0) sf_error("Cpuid and o_local inconsistant!");

    nzx = nz*nx*nh;
    //nzx2 = nz2*nx2*nh2;
    nzx2 = n_local*nx2*nh2;
    ozx2 = o_local*nx2*nh2;

    img = sf_floatalloc2(nz,nx);
    dat = sf_floatalloc2(nh,nx);

    /* propagator matrices */
    left = sf_input("left");
    right = sf_input("right");

    if (!sf_histbool(left,"sub",&sub) && !sf_getbool("sub",&sub)) sub=true;
    /* if -1 is included in the matrix */

    if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
    if (!sf_histint(left,"n2",&m2))  sf_error("No n2= in left");
    
    if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2);
    if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);
 
    lft = sf_floatalloc2(nzx,m2);
    rht = sf_floatalloc2(m2,nk);

    sf_floatread(lft[0],nzx*m2,left);
    sf_floatread(rht[0],m2*nk,right);

    curr = sf_floatalloc(nzx2);
    prev = sf_floatalloc(nzx2);

    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);
    wave = sf_floatalloc2(nzx2,m2);

#ifdef _OPENMP
#pragma omp parallel for default(shared) private(iz)
#endif
    for (iz=0; iz < nzx2; iz++) {
	curr[iz]=0.;
	prev[iz]=0.;
    }

    sendbuf = prev;
    if (cpuid==0) {
      wave_all = sf_floatalloc(nh2*nx2*nz2);
      recvbuf = wave_all;
      rcounts = sf_intalloc(numprocs);
      displs  = sf_intalloc(numprocs);
    } else {
      wave_all = NULL;
      recvbuf = NULL;
      rcounts = NULL;
      displs = NULL;
    }

    MPI_Gather(&nzx2, 1, MPI_INT, rcounts, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Gather(&ozx2, 1, MPI_INT, displs, 1, MPI_INT, 0, MPI_COMM_WORLD);

    if (mig) { /* migration */
	/* step backward in time */
	it1 = nt-1;
	it2 = -1;
	its = -1;	
    } else { /* modeling */
	sf_floatread(img[0],nz*nx,image);

	/* transpose and initialize at zero offset */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(iz,ix)
#endif
	for (iz=0; iz < nz_local; iz++) {
	    for (ix=0; ix < nx; ix++) {
		curr[nh2*(ix+iz*nx2)]=img[ix][iz+o_local];
	    }
	}
	
	/* step forward in time */
	it1 = 0;
	it2 = nt;
	its = +1;
    }

    /* time stepping */
    for (it=it1; it != it2; it += its) {
	sf_warning("it=%d;",it);

	if (mig) { /* migration <- read data */
	    sf_floatread(dat[0],nx*nh,data);
	} else {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,ih)
#endif
	    for (ix=0; ix < nx; ix++) {
		for (ih=0; ih < nh; ih++) {
		    dat[ix][ih] = 0.;
		}
	    }
	}
	
	if (NULL != snaps && 0 == it%snap) {
          MPI_Gatherv(sendbuf, nzx2, MPI_FLOAT, recvbuf, rcounts, displs, MPI_FLOAT, 0, MPI_COMM_WORLD);
          if (cpuid==0) {
            for (iz = 0; iz < nz; iz++)
              for (ix = 0; ix < nx; ix++)
                sf_floatwrite(wave_all+nh2*(ix+nx2*iz),nh,snaps);
          }
        }

	/* at z=0 */
        if (cpuid==0) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,ih)
#endif
	for (ix=0; ix < nx; ix++) {
	    for (ih=0; ih < nh; ih++) {
		if (mig) {
		    curr[ix*nh2+ih] += dat[ix][ih];
		} else {
		    dat[ix][ih] = curr[ix*nh2+ih];
		}
	    }
	}
        }

	/* matrix multiplication */
	mcfft3(curr,cwave);

	for (im = 0; im < m2; im++) {
          //for (ik = 0; ik < nk; ik++) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ikz,ikx,ikh,i,j)
#endif
          for (ikz = 0; ikz < n_local; ikz++) {
            for (ikx = 0; ikx < nx2; ikx++) {
              for (ikh = 0; ikh < nh2; ikh++) {
                i = ikh + ikx*nh2 + (o_local+ikz)*nx2*nh2;
                j = ikh + ikx*nh2 + ikz*nx2*nh2;
#ifdef SF_HAS_COMPLEX_H
		cwavem[j] = cwave[j]*rht[i][im];
#else
		cwavem[j] = sf_crmul(cwave[j],rht[i][im]);
#endif
              }
            }
          }
          imcfft3(wave[im],cwavem);
	}

#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,iz,ih,i,j,im,old,c)
#endif
        for (iz=0; iz < nz_local; iz++) {
	    for (ix = 0; ix < nx; ix++) {
		for (ih=0; ih < nh; ih++) {	
                    i = ih + ix*nh + (o_local+iz)*nx*nh;  /* original grid */
                    j = ih + ix*nh2+ iz*nx2*nh2; /* padded grid */
		
		    old = curr[j];

		    c = sub? 2*old: 0.0f;

		    c -= prev[j];

		    prev[j] = old;

		    for (im = 0; im < m2; im++) {
			c += lft[im][i]*wave[im][j];
		    }
		    
		    curr[j] = c;
		}
	    }
	}
	
	if (!mig) { /* modeling -> write out data */
          if (cpuid==0)
	    sf_floatwrite(dat[0],nx*nh,data);
	}
    }
    sf_warning(".");

    if (mig) {
      sendbuf = curr;
      MPI_Gatherv(sendbuf, nzx2, MPI_FLOAT, recvbuf, rcounts, displs, MPI_FLOAT, 0, MPI_COMM_WORLD);

      if (cpuid==0) {
        /* transpose */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(iz,ix)
#endif
        for (iz=0; iz < nz; iz++) {
          for (ix=0; ix < nx; ix++) {
            img[ix][iz] = wave_all[nh2*(ix+iz*nx2)];
          }
        }
	sf_floatwrite(img[0],nz*nx,image);
      }

    }

    mcfft3_finalize();

    /* Work's done. Get the elapsed wall time. */
    elapsedTime = walltime( &startTime );
    /* Print the wall time and terminate. */
    if (cpuid==0)
      printf("\nwall time = %.5fs\n", elapsedTime);

    MPI_Finalize();

    exit(0);
}
Пример #6
0
int main(int argc, char* argv[])
{
    bool verb;        
    int it,iz,im,ik,ix,iy,i,j, snap;     /* index variables */
    int nt,nz,nx,ny, m2, nk, nzx, nz2, nx2, ny2, nzx2, n2, pad1;
    float dt;
    sf_complex c;

    float  *rr;      /* I/O arrays*/
    sf_complex *cwave, *cwavem, *ww;
    sf_complex **wave, *curr;
    float *rcurr;

    sf_file Fw,Fr,Fo;    /* I/O files */
    sf_axis at,az,ax,ay;    /* cube axes */

    sf_complex **lt, **rt;
    sf_file left, right, snaps;

    sf_init(argc,argv);
    if(!sf_getbool("verb",&verb)) verb=true; /* verbosity */

    /* setup I/O files */
    Fw = sf_input ("in" );
    Fo = sf_output("out");
    Fr = sf_input ("ref");

    /* Read/Write axes */
    at = sf_iaxa(Fw,1); nt = sf_n(at); dt = sf_d(at); 
    az = sf_iaxa(Fr,1); nz = sf_n(az); 
    ax = sf_iaxa(Fr,2); nx = sf_n(ax); 
    ay = sf_iaxa(Fr,3); ny = sf_n(ay); 

    sf_oaxa(Fo,az,1); 
    sf_oaxa(Fo,ax,2);
    sf_oaxa(Fo,ay,3);
    
    sf_settype(Fo,SF_FLOAT);

    if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */

    if (!sf_getint("snap",&snap)) snap=0;
    /* interval for snapshots */
    
    if (snap > 0) {
	snaps = sf_output("snaps");
	/* (optional) snapshot file */
	
	sf_oaxa(snaps,az,1); 
	sf_oaxa(snaps,ax,2);
	sf_oaxa(snaps,ay,3);
	sf_oaxa(snaps,at,4);
	sf_settype(snaps,SF_FLOAT);
	sf_putint(snaps,"n4",nt/snap);
	sf_putfloat(snaps,"d4",dt*snap);
	sf_putfloat(snaps,"o4",0.);
    } else {
	snaps = NULL;
    }

    nk = cfft3_init(pad1,nz,nx,ny,&nz2,&nx2,&ny2);

    nzx = nz*nx*ny;
    nzx2 = nz2*nx2*ny2;

    /* propagator matrices */
    left = sf_input("left");
    right = sf_input("right");

    if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
    if (!sf_histint(left,"n2",&m2))  sf_error("Need n2=%d in left",m2);
    
    if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2);
    if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);
 
    lt = sf_complexalloc2(nzx,m2);
    rt = sf_complexalloc2(m2,nk);

    sf_complexread(lt[0],nzx*m2,left);
    sf_complexread(rt[0],m2*nk,right);

    /* read wavelet & reflectivity */
    ww=sf_complexalloc(nt);  sf_complexread(ww,nt ,Fw);
    rr=sf_floatalloc(nzx); sf_floatread(rr,nzx,Fr);

    curr = sf_complexalloc(nzx2);
    rcurr= sf_floatalloc(nzx2);

    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);
    wave = sf_complexalloc2(nzx2,m2);

    icfft3_allocate(cwavem);

    for (iz=0; iz < nzx2; iz++) {
	curr[iz]=sf_cmplx(0.,0.);
	rcurr[iz]=0.;
    }


    /* MAIN LOOP */
    for (it=0; it<nt; it++) {
	if(verb) sf_warning("it=%d;",it);

	/* matrix multiplication */
	cfft3(curr,cwave);

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

	for (iy = 0; iy < ny; iy++) {
	    for (ix = 0; ix < nx; ix++) {
		for (iz=0; iz < nz; iz++) {
		    i = iz+nz *(ix+nx *iy); /* original grid */
		    j = iz+nz2*(ix+nx2*iy); /* padded grid */
#ifdef SF_HAS_COMPLEX_H		
		    c = ww[it] * rr[i];
#else
		    c = sf_crmul(ww[it],rr[i]);
#endif

		    for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
			c += lt[im][i]*wave[im][j];
#else
			c += sf_cmul(lt[im][i],wave[im][j]);
#endif
		    }
		    
		    curr[j] = c;
		    rcurr[j]= crealf(c);
		}
	    }
	}

	if (NULL != snaps && 0 == it%snap) {
	    for (iy = 0; iy < ny; iy++) {
		for (ix = 0; ix < nx; ix++) {
		    sf_floatwrite(rcurr+nz2*(ix+nx2*iy),nz,snaps);
		}
	    }
	}
    }
    if(verb) sf_warning(".");    
	    	
    /* write wavefield to output */
    
    for (iy = 0; iy < ny; iy++) {
	for (ix = 0; ix < nx; ix++) {
	    sf_floatwrite(rcurr+nz2*(ix+nx2*iy),nz,Fo);
	}
    }
    
    cfft3_finalize();
    exit (0);
}
Пример #7
0
int main(int argc, char* argv[])
{
    map4 str;
    bool verb;
    int i1,i2, n1,n2,n3, nw, nx,nv, ix,iv, iy,ny;
    float d1,o1,d2,o2, eps, w,x, v0,v2,v,dv, dx, t, x0, dw;
    float *trace, *strace, *t2;
    sf_complex *ctrace, *ctrace2, shift;
    sf_file in, out;

#ifdef SF_HAS_FFTW
    fftwf_plan forw, invs;
#else
    kiss_fftr_cfg forw, invs;
#endif

    sf_init (argc,argv);
    in = sf_input("in");
    out = sf_output("out");

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&nx)) sf_error("No n2= in input");
    ny = sf_leftsize(in,2);

    if (!sf_getfloat("eps",&eps)) eps=0.01; /* regularization */
    if (!sf_getint("pad",&n2)) n2=n1; /* padding for stretch */
    if (!sf_getint("pad2",&n3)) n3=2*kiss_fft_next_fast_size((n2+1)/2);
    /* padding for FFT */

    if (!sf_getbool("verb",&verb)) verb=true;
    /* verbosity flag */

    nw = n3/2+1;

    strace = sf_floatalloc(n3);
    ctrace  = sf_complexalloc(nw);
    ctrace2 = sf_complexalloc(nw);

#ifdef SF_HAS_FFTW
    forw = fftwf_plan_dft_r2c_1d(n3, strace, (fftwf_complex *) ctrace,
				 FFTW_ESTIMATE);
    invs = fftwf_plan_dft_c2r_1d(n3, (fftwf_complex *) ctrace2, strace,
				 FFTW_ESTIMATE);
#else
    forw = kiss_fftr_alloc(n3,0,NULL,NULL);
    invs = kiss_fftr_alloc(n3,1,NULL,NULL);
#endif
    if (NULL == forw || NULL == invs) sf_error("FFT allocation error");

    if (!sf_histfloat(in,"o1",&o1)) o1=0.;  
    o2 = o1*o1;

    if(!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    d2 = o1+(n1-1)*d1;
    d2 = (d2*d2 - o2)/(n2-1);
    dw = 16*SF_PI/(d2*n3); /* 2pi * 8 */

    if (!sf_getint("nv",&nv)) sf_error("Need nv=");
    /* velocity steps */
    if (!sf_getfloat("dv",&dv)) sf_error("Need dv=");
    /* velocity step size */
    if (!sf_getfloat("v0",&v0) && 
	!sf_histfloat(in,"v0",&v0)) sf_error("Need v0=");
    /*( v0 starting velocity )*/

    if(!sf_histfloat(in,"d2",&dx)) sf_error("No d2= in input");
    if(!sf_histfloat(in,"o2",&x0)) x0=0.;

    sf_putfloat(out,"o2",v0+dv);
    sf_putfloat(out,"d2",dv);
    sf_putint(out,"n2",nv);

    sf_putstring(out,"label2","Velocity");

    sf_shiftdim(in, out, 2);

    dx *= 2.*SF_PI;
    x0 *= 2.*SF_PI;

    trace = sf_floatalloc(n1);
    t2 = sf_floatalloc(n2);

    str = stretch4_init (n1, o1, d1, n2, eps);

    for (i2=0; i2 < n2; i2++) {
	t = o2+i2*d2;
	t2[i2] = sqrtf(t);
    }    

    stretch4_define (str,t2);

    for (iy=0; iy < ny; iy++) {
	for (ix=0; ix < nx; ix++) {
	    if (verb) sf_warning("wavenumber %d of %d (%d);", ix+1,nx,iy);
	    
	    x = x0+ix*dx; 
	    x *= x;
	    x *= 0.5;
	    
	    sf_floatread(trace,n1,in);
	    for (i1=0; i1 < n1; i1++) {
		trace[i1] /= n1;
	    }
	    stretch4_invert (false,str,strace,trace);
	    for (i2=n2; i2 < n3; i2++) {
		strace[i2] = 0.;
	    }
	    
#ifdef SF_HAS_FFTW
	    fftwf_execute(forw);
#else
	    kiss_fftr(forw,strace, (kiss_fft_cpx *) ctrace);
#endif
	    
	    for (iv=0; iv < nv; iv++) {
		v = v0 + (iv+1)*dv;
		v2 = x * ((v0*v0) - (v*v));
		
		ctrace2[0] = sf_cmplx(0.0f,0.0f); /* dc */
		
		for (i2=1; i2 < nw; i2++) {
		    w = i2*dw;
		    w = v2/w;
		    
		    shift = sf_cmplx(cosf(w),sinf(w));
		    
#ifdef SF_HAS_COMPLEX_H
		    ctrace2[i2] = ctrace[i2] * shift;
#else
		    ctrace2[i2] = sf_cmul(ctrace[i2],shift);
#endif
		} /* w */

#ifdef SF_HAS_FFTW
		fftwf_execute(invs);
#else
		kiss_fftri(invs,(const kiss_fft_cpx *) ctrace2, strace);
#endif
		stretch4_apply(false,str,strace,trace);
		sf_floatwrite (trace,n1,out);
	    } /* v  */
	} /* x */
	if (verb) sf_warning(".");
    } /* y */

    exit (0);
}
Пример #8
0
void ocpredict_step(bool adj           /* adjoint flag */,
		    bool forw          /* forward or backward */,
		    float dw, int nx,
		    float w            /* log-stretch frequency */,
		    float h, float h2  /* offset position */, 
		    sf_complex *trace  /* common offset slice */)
/*< offset continuation prediction step >*/
{
    int ix, k;
    float w2;
    sf_complex diag, diag2, *in, offd, offd2, c1, c2;
    ctris slv;
 
    in = sf_complexalloc(nx);
    slv = ctridiagonal_init (nx);

    w2 = w*w;

    if (fabsf(w) < dw) {
	return;
/*	for (ix=0; ix < nx; ix++) {
	    trace[ix]=sf_cmplx(0.,0.);
	}
*/   }    
    
#ifdef SF_HAS_COMPLEX_H		
    c1 = 3.*sf_cmplx(9. + w2,4.*w)/(w2*sf_cmplx(3.,- w));
    c2 = 3.*sf_cmplx(w2 - 27.,8.*w)/(w2*sf_cmplx(3.,- w));
#else
    c1 = sf_cdiv(sf_cmplx(3.*(9. + w2),3.*4.*w),
		 sf_cmplx(w2*3.,-w2*w));
    c2 = sf_cdiv(sf_cmplx(3.*(w2 - 27.),3.*8.*w),
		 sf_cmplx(w2*3.,-w2*w));
#endif
    
    for (ix=0; ix < nx; ix++) {
	in[ix] = trace[ix];
    }
    h *= h;
    h2 *= h2; 
    if (forw) {
#ifdef SF_HAS_COMPLEX_H	
	offd  = 1. - c1*h2 + c2*h;
	offd2 = 1. - c1*h  + c2*h2;
	diag  = 12. - 2.*offd;
	diag2 = 12. - 2.*offd2;
#else
	offd  = sf_cadd(sf_cmplx(1.,0.),
			sf_cadd(sf_crmul(c1,-h2),
				sf_crmul(c2,h)));
	offd2 = sf_cadd(sf_cmplx(1.,0.),
			sf_cadd(sf_crmul(c1,-h),
				sf_crmul(c2,h2)));
	diag  = sf_cadd(sf_cmplx(12.,0.),sf_crmul(offd,-2.));
	diag2 = sf_cadd(sf_cmplx(12.,0.),sf_crmul(offd2,-2.));
#endif
    } else {
#ifdef SF_HAS_COMPLEX_H	
	offd  = 1. - c1*h  + c2*h2;
	offd2 = 1. - c1*h2 + c2*h;
	diag  = 12. - 2.*offd;
	diag2 = 12. - 2.*offd2;
#else
	offd  = sf_cadd(sf_cmplx(1.,0.),
			sf_cadd(sf_crmul(c1,-h),
				sf_crmul(c2,h2)));
	offd2 = sf_cadd(sf_cmplx(1.,0.),
			sf_cadd(sf_crmul(c1,-h2),
				sf_crmul(c2,h)));
	diag  = sf_cadd(sf_cmplx(12.,0.),sf_crmul(offd,-2.));
	diag2 = sf_cadd(sf_cmplx(12.,0.),sf_crmul(offd2,-2.));
#endif    
    }

    ctridiagonal_const_define (slv, diag2, offd2);
    
#ifdef SF_HAS_COMPLEX_H	
    trace[0] = diag*in[0] + offd*in[1];
#else
    trace[0] = sf_cadd(sf_cmul(diag,in[0]),
			     sf_cmul(offd,in[1]));
#endif
    for (k = 1; k < nx - 1; k++) {
#ifdef SF_HAS_COMPLEX_H	
	trace[k] = diag*in[k] + offd*(in[k+1]+in[k-1]);
#else
	trace[k] = sf_cadd(sf_cmul(diag,in[k]),
			   sf_cmul(offd,sf_cadd(in[k+1],in[k-1])));
#endif
    }
#ifdef SF_HAS_COMPLEX_H	
    trace[nx-1] = diag*in[nx-1] + offd*in[nx-2];
#else
    trace[nx-1] = sf_cadd(sf_cmul(diag,in[nx-1]),
			  sf_cmul(offd,in[nx-2]));
#endif
    ctridiagonal_solve (slv, trace);

}
Пример #9
0
int main(int argc, char* argv[])
{
    bool verb,complx,sub,os;
    int it,iz,im,ik,ix,i,j;     /* index variables */
    int nt,nz,nx, m2, nk, nzx, nz2, nx2, nzx2, n2, pad1,nth;
    sf_complex c,old;

    /* I/O arrays*/
    sf_complex *ww,*curr,*prev,*cwave,*cwavem,**wave,**lt, **rt;
    float *rcurr,*rr;
    
    sf_file Fw,Fr,Fo;    /* I/O files */
    sf_axis at,az,ax;    /* cube axes */
    sf_file left, right;


    sf_init(argc,argv);
    if(!sf_getbool("verb",&verb)) verb=false; /* verbosity */
    if(!sf_getbool("cmplx",&complx)) complx=true; /* outputs complex wavefield */
    if(!sf_getbool("os",&os)) os=true; /* one-step flag */
    if (os) {
      sf_warning("One-step wave extrapolation");
      if(!sf_getbool("sub",&sub)) sub=false; /* subtraction flag */
    } else {
      sf_warning("Two-step wave extrapolation");
      if(!sf_getbool("sub",&sub)) sub=true; /* subtraction flag */
    }

    /* setup I/O files */
    Fw = sf_input ("in" );
    Fo = sf_output("out");
    Fr = sf_input ("ref");

    if (SF_COMPLEX != sf_gettype(Fw)) sf_error("Need complex input");
    if (SF_FLOAT != sf_gettype(Fr)) sf_error("Need float ref");

    if(complx)
	sf_settype(Fo,SF_COMPLEX);
    else
	sf_settype(Fo,SF_FLOAT);

    /* Read/Write axes */
    at = sf_iaxa(Fw,1); nt = sf_n(at); 
    az = sf_iaxa(Fr,1); nz = sf_n(az); 
    ax = sf_iaxa(Fr,2); nx = sf_n(ax); 

    sf_oaxa(Fo,az,1); 
    sf_oaxa(Fo,ax,2); 
    sf_oaxa(Fo,at,3);
    
    if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */

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

    nk = cfft2_init(pad1,nz,nx,&nz2,&nx2);

    nzx = nz*nx;
    nzx2 = nz2*nx2;

    /* propagator matrices */
    left = sf_input("left");
    right = sf_input("right");

    if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
    if (!sf_histint(left,"n2",&m2))  sf_error("Need n2= in left");
    
    if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2);
    if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);
  
    lt = sf_complexalloc2(nzx,m2);
    rt = sf_complexalloc2(m2,nk);

    sf_complexread(lt[0],nzx*m2,left);
    sf_complexread(rt[0],m2*nk,right);

    sf_fileclose(left);
    sf_fileclose(right);

    /* read wavelet & reflectivity */
    ww=sf_complexalloc(nt);  
    sf_complexread(ww,nt ,Fw);

    rr=sf_floatalloc(nzx); 
    sf_floatread(rr,nzx,Fr);

    curr   = sf_complexalloc(nzx2);
    if (!os) prev = sf_complexalloc(nzx2);
    else prev = NULL;
    if(!complx) rcurr  = sf_floatalloc(nzx2);
    else rcurr=NULL;

    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);
    wave   = sf_complexalloc2(nzx2,m2);

    icfft2_allocate(cwavem);

    for (iz=0; iz < nzx2; iz++) {
	curr[iz] = sf_cmplx(0.,0.);
	if (!os) prev[iz] = sf_cmplx(0.,0.);
	if(!complx) rcurr[iz]= 0.;
    }

    /* MAIN LOOP */
    for (it=0; it<nt; it++) {
	if(verb) sf_warning("it=%d;",it);

	/* matrix multiplication */
	cfft2(curr,cwave);

	for (im = 0; im < m2; im++) {
	    for (ik = 0; ik < nk; ik++) {
#ifdef SF_HAS_COMPLEX_H
		cwavem[ik] = cwave[ik]*rt[ik][im];
#else
		cwavem[ik] = sf_cmul(cwave[ik],rt[ik][im]); //complex multiplies complex
#endif
	    }
	    icfft2(wave[im],cwavem);
	}

	for (ix = 0; ix < nx; ix++) {
	    for (iz=0; iz < nz; iz++) {
	        i = iz+ix*nz;  /* original grid */
		j = iz+ix*nz2; /* padded grid */
#ifdef SF_HAS_COMPLEX_H
		c = ww[it] * rr[i]; // source term
#else
		c = sf_crmul(ww[it], rr[i]); // source term
#endif
		if (sub) c += curr[j];
		if (!os) {
		  old = curr[j];
#ifdef SF_HAS_COMPLEX_H
		  c += sub? (old-prev[j]) : -prev[j];
#else
		  c = sf_cadd(c,sub? sf_csub(old,prev[j]) : sf_cneg(prev[j]));
#endif
		  prev[j] = old;
		}
		for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
		    c += lt[im][i]*wave[im][j];
#else
		    c += sf_cmul(lt[im][i], wave[im][j]);
#endif
		}

		curr[j] = c;
		if (!complx) rcurr[j] = crealf(c);

	    }

	    /* write wavefield to output */
	    if (complx)
		sf_complexwrite(curr+ix*nz2,nz,Fo);
	    else
		sf_floatwrite(rcurr+ix*nz2,nz,Fo);
	}
    }
    if(verb) sf_warning("."); 
    cfft2_finalize();
    exit (0);
}
Пример #10
0
int main(int argc, char* argv[])
{
  bool verb;        
  int it,iz,im,ikz,ikx,iky,ix,iy,i,j,snap;     /* index variables */
  int nt,nz,nx,ny, m2, nk, nzx, nz2, nx2, ny2, nzx2, n2, pad1;
  float dt;
  sf_complex c;

  float  *rr;      /* I/O arrays*/
  sf_complex *cwave, *cwavem, *ww;
  sf_complex **wave, *curr;
  float *rcurr, *rcurr_all;

  sf_file Fw,Fr,Fo;    /* I/O files */
  sf_axis at,az,ax,ay;    /* cube axes */

  sf_complex **lt, **rt;
  sf_file left, right, snaps;

  /*MPI related*/
  int cpuid,numprocs;
  int provided;
  int n_local, o_local;
  int ozx2;
  float *sendbuf, *recvbuf;
  int *rcounts, *displs;

  MPI_Init_thread(&argc,&argv,MPI_THREAD_FUNNELED,&provided);
  threads_ok = provided >= MPI_THREAD_FUNNELED;

  sf_init(argc,argv);

  MPI_Comm_rank(MPI_COMM_WORLD, &cpuid);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

  if(!sf_getbool("verb",&verb)) verb=true; /* verbosity */

  /* setup I/O files */
  Fw = sf_input ("--input" );
  Fo = sf_output("--output");
  Fr = sf_input ("ref");

  /* Read/Write axes */
  at = sf_iaxa(Fw,1); nt = sf_n(at); dt = sf_d(at); 
  az = sf_iaxa(Fr,1); nz = sf_n(az); 
  ax = sf_iaxa(Fr,2); nx = sf_n(ax); 
  ay = sf_iaxa(Fr,3); ny = sf_n(ay); 

  if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */

  if (!sf_getint("snap",&snap)) snap=0;
  /* interval for snapshots */
    
  if (cpuid==0) {

    sf_oaxa(Fo,az,1); 
    sf_oaxa(Fo,ax,2);
    sf_oaxa(Fo,ay,3);
    
    sf_settype(Fo,SF_FLOAT);

    if (snap > 0) {
      snaps = sf_output("snaps");
      /* (optional) snapshot file */
	
      sf_oaxa(snaps,az,1); 
      sf_oaxa(snaps,ax,2);
      sf_oaxa(snaps,ay,3);
      sf_oaxa(snaps,at,4);
      sf_settype(snaps,SF_FLOAT);
      sf_putint(snaps,"n4",nt/snap);
      sf_putfloat(snaps,"d4",dt*snap);
      sf_putfloat(snaps,"o4",0.);
    } else {
      snaps = NULL;
    }

  }

  //nk = cfft3_init(pad1,nz,nx,ny,&nz2,&nx2,&ny2);
  //n_local = ny2;
  //o_local = 0;
  nk = mcfft3_init(pad1,nz,nx,ny,&nz2,&nx2,&ny2,&n_local,&o_local);
  sf_warning("Cpuid=%d,n2=%d,n1=%d,n0=%d,local_n0=%d,local_0_start=%d",cpuid,nz2,nx2,ny2,n_local,o_local);

  nzx = nz*nx*ny;
  //nzx2 = nz2*nx2*ny2;
  nzx2 = n_local*nz2*nx2;
  ozx2 = o_local*nz2*nx2;

  /* propagator matrices */
  left = sf_input("left");
  right = sf_input("right");

  if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
  if (!sf_histint(left,"n2",&m2))  sf_error("Need n2=%d in left",m2);
    
  if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2);
  if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);
 
  lt = sf_complexalloc2(nzx,m2);
  rt = sf_complexalloc2(m2,nk);

  sf_complexread(lt[0],nzx*m2,left);
  sf_complexread(rt[0],m2*nk,right);

  /* read wavelet & reflectivity */
  ww=sf_complexalloc(nt);  sf_complexread(ww,nt ,Fw);
  rr=sf_floatalloc(nzx); sf_floatread(rr,nzx,Fr);

  curr = sf_complexalloc(nzx2);
  rcurr= sf_floatalloc(nzx2);

  cwave  = sf_complexalloc(nzx2);
  cwavem = sf_complexalloc(nzx2);
  wave = sf_complexalloc2(nzx2,m2);

  //icfft3_allocate(cwavem);

  for (iz=0; iz < nzx2; iz++) {
    curr[iz]=sf_cmplx(0.,0.);
    rcurr[iz]=0.;
  }

  sendbuf = rcurr;
  if (cpuid==0) {
    rcurr_all = sf_floatalloc(nz2*nx2*ny2);
    recvbuf = rcurr_all;
    rcounts = sf_intalloc(numprocs);
    displs  = sf_intalloc(numprocs);
  } else {
    rcurr_all = NULL;
    recvbuf = NULL;
    rcounts = NULL;
    displs = NULL;
  }

  MPI_Gather(&nzx2, 1, MPI_INT, rcounts, 1, MPI_INT, 0, MPI_COMM_WORLD);
  MPI_Gather(&ozx2, 1, MPI_INT, displs, 1, MPI_INT, 0, MPI_COMM_WORLD);

  /* MAIN LOOP */
  for (it=0; it<nt; it++) {
    if(verb) sf_warning("it=%d;",it);

    /* matrix multiplication */
    mcfft3(curr,cwave);

    for (im = 0; im < m2; im++) {
      for (iky = 0; iky < n_local; iky++) {
        for (ikx = 0; ikx < nx2; ikx++) {
          for (ikz = 0; ikz < nz2; ikz++) {
            i = ikz + ikx*nz2 + (o_local+iky)*nx2*nz2;
            j = ikz + ikx*nz2 + iky*nx2*nz2;
#ifdef SF_HAS_COMPLEX_H
            cwavem[j] = cwave[j]*rt[i][im];
#else
            cwavem[j] = sf_cmul(cwave[j],rt[i][im]);
#endif
          }
        }
      }
      imcfft3(wave[im],cwavem);
    }

    for (iy = 0; iy < n_local && (iy+o_local)<ny; iy++) {
      for (ix = 0; ix < nx; ix++) {
        for (iz=0; iz < nz; iz++) {
          i = iz + ix*nz + (o_local+iy)*nx*nz;  /* original grid */
          j = iz + ix*nz2+ iy*nx2*nz2; /* padded grid */
#ifdef SF_HAS_COMPLEX_H		
          c = ww[it] * rr[i];
#else
          c = sf_crmul(ww[it],rr[i]);
#endif

          for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
            c += lt[im][i]*wave[im][j];
#else
            c += sf_cmul(lt[im][i],wave[im][j]);
#endif
          }
		    
          curr[j] = c;
          rcurr[j]= crealf(c);
        }
      }
    }

    /* output movie */
    if (NULL != snaps && 0 == it%snap) {
      MPI_Gatherv(sendbuf, nzx2, MPI_FLOAT, recvbuf, rcounts, displs, MPI_FLOAT, 0, MPI_COMM_WORLD);

      if (cpuid==0) {
        for (iy = 0; iy < ny; iy++)
          for (ix = 0; ix < nx; ix++)
            sf_floatwrite(rcurr_all+nz2*(ix+nx2*iy),nz,snaps);
      }
    }

  }
  if(verb) sf_warning(".");    
	    	
  /* write wavefield to output */
  MPI_Gatherv(sendbuf, nzx2, MPI_FLOAT, recvbuf, rcounts, displs, MPI_FLOAT, 0, MPI_COMM_WORLD);
  if (cpuid==0) {
    for (iy = 0; iy < ny; iy++)
      for (ix = 0; ix < nx; ix++)
        sf_floatwrite(rcurr_all+nz2*(ix+nx2*iy),nz,Fo);
  }
    
  mcfft3_finalize();

  MPI_Finalize();
  exit (0);
}
Пример #11
0
int main(int argc, char* argv[])
{
    /* Frequency axis = axis 1 of input, output */ 
    int   nw, iw;
    float dw;

    /* Axis 2 of input */
    int   nx, ix;
    float ox, dx, x;
    float p;    
    
    float *wp = NULL; 
    
    sf_complex *tr = NULL;
    
    sf_file in, out;

    /**********************************************************************/

    /* Read parameters of input file */

    sf_init (argc,argv);

    in  = sf_input ("in" );   
    out = sf_output("out");

    if (SF_COMPLEX != sf_gettype(in)) sf_error("Need complex input");

    if(!sf_histint  (in,"n1",&nw)) sf_error ("No n1= in input"); 
    if(!sf_histfloat(in,"d1",&dw)) sf_error ("No d1= in input");
    if(!sf_histint  (in,"n2",&nx)) sf_error ("No n2= in input");
    if(!sf_histfloat(in,"o2",&ox)) sf_error ("No o2= in input");
    if(!sf_histfloat(in,"d2",&dx)) sf_error ("No d2= in input");

    /* Read parameters from the command line */

    if (!sf_getfloat("p", &p)) sf_error("Need p=");
    /* Slope of LMO */

    /* The frequency axis */

    wp = sf_floatalloc( nw );
    for (iw=0; iw < nw; iw++) {
        wp[iw] = p * iw * dw * 2*SF_PI;
    }

    tr = sf_complexalloc( nw );

    for (ix=0; ix < nx; ix++) {

        x = ox + ix * dx;
        
        sf_complexread ( tr, nw, in ); /* Read the gather */

        for (iw=1; iw < nw; iw++) {
#ifdef SF_HAS_COMPLEX_H
            tr[iw] *= cexpf(sf_cmplx(0.,-wp[iw] * x ));
#else
	    tr[iw] = sf_cmul(tr[iw],cexpf(sf_cmplx(0.,-wp[iw] * x )));
#endif
        }
    
        sf_complexwrite( tr, nw , out );

    }
}
Пример #12
0
int main (int argc, char* argv[]) 
{
    int n1, n2, n, nk, i, j, k, nlags;
    float *data, wt;
    sf_complex **fft, *dataf; 
    sf_file inp, out;
#ifdef SF_HAS_FFTW
    fftwf_plan cfg=NULL, icfg=NULL;
#else
    kiss_fftr_cfg cfg=NULL, icfg=NULL;
#endif

    sf_init(argc, argv);

    inp = sf_input("in");
    out = sf_output("out");

    if (SF_FLOAT != sf_gettype(inp)) sf_error("Need float input");
    if (!sf_histint(inp,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(inp,"n2",&n2)) sf_error("No n2= in input");

    if (!sf_getint("nlags",&nlags)) nlags=100; /* number of lags */

    sf_putint(out,"n1",nlags);
    sf_putint(out,"n3",n2);

    nk = kiss_fft_next_fast_size((n1+1)/2)+1;
    n = 2*(nk-1);

    wt = 1.0/n;

    data = sf_floatalloc(n);
    dataf = sf_complexalloc(nk);
    fft = sf_complexalloc2(nk,n2);

#ifdef SF_HAS_FFTW
    cfg = fftwf_plan_dft_r2c_1d(n, data, (fftwf_complex *) dataf,
				FFTW_MEASURE);
    icfg = fftwf_plan_dft_c2r_1d(n, (fftwf_complex *) dataf, data,
				 FFTW_MEASURE);
    if (NULL == cfg || NULL == icfg) sf_error("FFT allocation failure");
#else
    cfg  = kiss_fftr_alloc(n,0,NULL,NULL);
    icfg  = kiss_fftr_alloc(n,1,NULL,NULL);
#endif
    
    for (i=0; i < n2; i++) {
	sf_floatread(data,n1,inp);
	for (k=n1; k < n; k++) {
	    data[k] = 0.0f;
	}
#ifdef SF_HAS_FFTW
	fftwf_execute(cfg);
#else
	kiss_fftr (cfg,data,(kiss_fft_cpx *) dataf);
#endif
	for (k=0; k < nk; k++) {
	    fft[i][k] = dataf[k];
	}
    }

/*************************************************
*                                                *
*  cross-correlate every trace with every other  *
*                                                *
*************************************************/

    for (i=0; i < n2; i++) {
	for (j=0; j < n2; j++) {
	    for (k=0; k < nk; k++) {
#ifdef SF_HAS_COMPLEX_H
		dataf[k] = fft[i][k] * conjf(fft[j][k]);
#else
		dataf[k] = sf_cmul(fft[i][k],conjf(fft[j][k]));
#endif
	    }

#ifdef SF_HAS_FFTW
	    fftwf_execute(icfg);
#else
	    kiss_fftri(icfg,(kiss_fft_cpx *) dataf,data);
#endif

	    for (k=0; k < nlags; k++) {
		data[k] *= wt;
	    }

	    sf_floatwrite(data,nlags,out);
	}
    }

    exit(0);
}
Пример #13
0
int main(int argc, char* argv[])
{
  /*define variables*/
  int nx,nx1,nt;
  int n1,n2;
  float d1,o1,d2,o2;
  int padt,padx;
  int ntfft,*n,nw,nk;
  float **d,*wavelet,**shot,**ds,**vel,**vmig,**M,v_ave;
  float *kx,*omega,dkx,dw;
  sf_complex **m,**ms,**mr,*in2a,*in2b,*cs,*cr,*c,czero;
  sf_complex Ls;
  float fmin,fmax,f_low,f_high;
  int if_low,if_high;
  int ix,iw,ik;
  float dt,dx,ox,dz,zmax;
  fftwf_plan p2a,p2b;
  sf_file in,out,velfile,source_wavelet;
  int iz,nz;
  int ishot,max_num_shot,ig,ng,it,index;
  int iswavelet;
  /*define sf input output*/
  sf_init (argc,argv);
  in = sf_input("in");
  out = sf_output("out");
  velfile = sf_input("velfile");
  if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
  if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
  if (!sf_histfloat(in,"o1",&o1)) o1=0.;
  if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in vel");
  if (!sf_histfloat(in,"d2",&d2)) sf_error("No d2= in input");
  if (!sf_histfloat(in,"o2",&o2)) o2=0.;

  dt = d1;
  dx = d2;
  ox = o2;
  nx1 = n2;
  nt = n1;
  if (!sf_histint(velfile,"n1",&nz)) sf_error("No n1= in vel");
  if (!sf_histfloat(velfile,"d1",&dz)) sf_error("No n1= in vel");
  if (!sf_histint(velfile,"n2",&n2)) sf_error("No n2= in vel");	
   if (!sf_getint("iswavelet",&iswavelet)) iswavelet = 0;
  source_wavelet=sf_input("source_wavelet");

  max_num_shot=100;
  ng=700;
  nx=n2;
  padt = 2;
  padx = 2;
  ntfft = padt*nt;
  nw=ntfft/2+1;
  nk = padx*nx;
  dw = 2*PI/ntfft/dt;
  dkx = 2*PI/nk/dx;

  sf_putint(out,"n1",nz);
  sf_putint(out,"n2",nx);
  sf_putfloat(out,"d1",dz);
  sf_putstring(out,"label1","z");
  sf_putstring(out,"unit1","m");
  sf_putstring(out,"title","migrated");

  if (!sf_getfloat("fmax",&fmax)) fmax = 0.5/d1; /* max frequency to process */
  if (fmax > 0.5/d1) fmax = 0.5/d1;
  if (!sf_getfloat("fmin",&fmin)) fmin = 0.1; /* min frequency to process */
  if (!sf_getfloat("Zmax",&zmax)) zmax = (nz-1)*dz; /* max Depth to migrate */

  
  /*define axis variables*/
 
  dkx=(float) 2*PI/nk/dx;
  dw=(float) 2*PI/ntfft/dt;
  /*allocate memory to dynamic arrays*/
  d = sf_floatalloc2(nt,nx1);
  shot=sf_floatalloc2(nt,ng);
  ds=sf_floatalloc2(nt,nx);
  vel = sf_floatalloc2(nz,nx);
  wavelet=sf_floatalloc(nt);
  vmig = sf_floatalloc2(nz,nx);
  m = sf_complexalloc2(nw,nx);
  ms = sf_complexalloc2(nw,nx);
  mr = sf_complexalloc2(nw,nx);
  kx= sf_floatalloc (nk);
  omega= sf_floatalloc (nw);
  in2a = sf_complexalloc(nk);
  in2b = sf_complexalloc(nk);
  n = sf_intalloc(1);
  M= sf_floatalloc2(nz,nx);
  c = sf_complexalloc(nx);
  cs = sf_complexalloc(nx);
  cr = sf_complexalloc(nx);
  /*read input files*/ 
  sf_floatread(d[0],nx1*nt,in);
  sf_floatread(vel[0],nx*nz,velfile);
/* If there is no wavelet use delta as default
If there is a wavelet use it*/
   if (iswavelet==0) {
	for (it=0; it<nt; it++) wavelet[it] = 0.0;
	wavelet[0]=1;
 	 }

   if (iswavelet==1) sf_floatread(wavelet,nt,source_wavelet);

  /* This part is important: we need to define the horizontal wavenumber and frequency axes right.*/

dw = 2*PI/ntfft/dt;

 dkx = 2*PI/nk/dx;

for (iw=0;iw<nw;iw++){

    omega[iw] = dw*iw;

}
for (ik=0;ik<nk;ik++){ 

  if (ik<nk/2) kx[ik] = dkx*ik;

  else         kx[ik] = -(dkx*nk - dkx*ik);

}

  /* Define minimum and maximum frequency index to process*/   
  
  f_low = fmin;   /* min frequency to process */
  f_high = fmax;  /* max frequency to process */
  
  if(f_low>0){
  	if_low = trunc(f_low*dt*ntfft);
  }	
  else{ 
  	if_low = 0;
  }
  if(f_high*dt*ntfft+1<nw){
  	if_high = trunc(f_high*dt*ntfft)+1;
  }
  else{
 	 if_high = nw;
  }
  
  
  __real__ czero = 0;
  __imag__ czero = 0;
  n[0] = nk;
  p2a = fftwf_plan_dft(1, n, (fftwf_complex*)in2a, (fftwf_complex*)in2a, FFTW_FORWARD, FFTW_ESTIMATE);
  p2b = fftwf_plan_dft(1, n, (fftwf_complex*)in2b, (fftwf_complex*)in2b, FFTW_BACKWARD, FFTW_ESTIMATE);

fftwf_execute(p2a); /* FFT x to k */
fftwf_execute(p2b); /* FFT x to k */

  /* Define initial migrated model and source field as zeros*/
 
  for (iz=0; iz<nz; iz++) {	
    for (ix=0; ix<nx; ix++) M[ix][iz] = 0.0;
  }

   for (it=0; it<nt; it++) {	
    for (ix=0; ix<nx; ix++) ds[ix][it] = 0.0;
  }

  for (iz=0; iz<nz;iz++){
    for (ix=0;ix<nx;ix++) vmig[ix][iz]=vel[ix][iz];
    }
  
  /* loop over shots*/
for (ishot=0;ishot<max_num_shot;ishot++){
	for (ig=0;ig<ng;ig++){
		for (it=0; it<nt; it++)
		shot[ig][it]=d[ishot*ng+ig][it];
	}
	 for (it=0; it<nt; it++) {	
    		for (ix=0; ix<nx; ix++) ds[ix][it] = 0.0;
 	 }
	index=ishot*nx/max_num_shot;
	for (it=0; it<nt; it++) ds[index][it]=wavelet[it];
/* apply fourier transform in time direction t-x ---> w-x*/
  my_forward_fft(ms,mr,shot,ds,nt,dt,nx,padt);

  for (iw=if_low;iw<if_high;iw++){
     for (iz=0; iz<nz;iz++){
		v_ave=vmig[0][iz];	
	        my_v_ave (v_ave,vmig,iz,nx);
/*Apply phase shift to source side*/
	        my_phase_shift(ms,czero,iw,iz,omega,kx,nk,nx,v_ave,in2a,in2b,p2a,p2b,dz,0);
			for (ix=0;ix<nx;ix++) {
					cs[ix]= in2b[ix];
					}
/*Apply phase shift to receiver side*/
		 my_phase_shift(mr,czero,iw,iz,omega,kx,nk,nx,v_ave,in2a,in2b,p2a,p2b,dz,1);
			for (ix=0;ix<nx;ix++) {
					cr[ix]= in2b[ix];
					}
/*Apply split step correction to source and receiver side wavefields*/
         	my_split_step_correction (ms,cs,vmig,v_ave,iz,dz,iw,dw,nx,0);
		my_split_step_correction (mr,cr,vmig,v_ave,iz,dz,iw,dw,nx,1);
	/* Apply cross corrolation as an imaging condition*/
		for (ix=0;ix<nx;ix++){
		__real__ Ls=crealf(ms[ix][iw]);
		__imag__ Ls=- cimagf(ms[ix][iw]);
		m[ix][iw]=mr[ix][iw]*Ls;
		}
   			/* Update migrated model by stacking*/	
		for (ix=0;ix<nx;ix++) M[ix][iz]=M[ix][iz]+2*crealf(m[ix][iw]);
      }
   }
fprintf(stderr,"\r progress = %6.2f%%",(float) 100*(ishot)/(max_num_shot));
  }


  sf_floatwrite(M[0],nz*nx,out);

  
  fftwf_destroy_plan(p2a);
  fftwf_free(in2a); 
  fftwf_destroy_plan(p2b);
  fftwf_free(in2b);	
  
  exit (0);
}
Пример #14
0
/* main function */
int main(int argc, char* argv[]) 
{
    clock_t tstart,tend;
    double duration;

    /*flags*/
    bool verb, adj; /* migration(adjoint) flag */
    bool wantwf; /* outputs wavefield snapshots */
    bool wantrecord; /* actually means "need record" */
    bool illum; /* source illumination flag*/
    bool roll; /* survey strategy */
    
    /*I/O*/
    sf_file Fvel;
    sf_file left, right, leftb, rightb;
    sf_file Fsrc, Frcd/*source and record*/;
    sf_file Ftmpwf;
    sf_file Fimg;

    /*axis*/
    sf_axis at, ax, az, as;

    /*grid index variables*/
    int nx, nz, nt, wfnt;
    int nzx, nx2, nz2, n2, m2, m2b, pad1, nk;
    int ix, iz, it, is;
    int nxb, nzb;
    int snpint;
    float dt, dx, dz, wfdt;
    float ox, oz;

    /*source/geophone location*/
    int   spx, spz;
    int   gpz,gpx,gpl; /*geophone depth/x-crd/length*/

    /*Model*/
    sf_complex **lt, **rt;
    sf_complex **ltb, **rtb;

    /*Data*/
    sf_complex ***wavefld;
    sf_complex ***record, **tmprec, **img, **imgsum;
    float **sill;

    /*source*/
    sf_complex *ww;
    float *rr;
    int rectz,rectx,repeat; /*smoothing parameters*/
    float trunc;
    int sht0,shtbgn,shtend,shtnum,shtnum0,shtint,shtcur;

    /*abc boundary*/
    int top,bot,lft,rht;

    /*tmp*/
    int tmpint;

    /*parameter structs*/
    geopar geop;
    mpipar mpip;

    /*MPI*/
    int rank, nodes;
    sf_complex *sendbuf, *recvbuf;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nodes);

    sf_init(argc, argv);

    if(rank==0) sf_warning("nodes=%d",nodes);

    if (!sf_getbool("verb", &verb)) verb=false; /*verbosity*/
    if (!sf_getbool("adj", &adj)) adj=true; /*migration*/
    if (!sf_getbool("wantwf", &wantwf)) wantwf=false; /*output forward and backward wavefield*/
    if (!sf_getbool("wantrecord", &wantrecord)) wantrecord=true; /*if n, using record data generated by this program */
    if (!sf_getbool("illum", &illum)) illum=false; /*if n, no source illumination applied */
    if (!sf_getbool("roll", &roll)) roll=false; /*if n, receiver is independent of source location and gpl=nx*/
    /* source/receiver info */
    if (!sf_getint("shtbgn", &shtbgn)) sf_error("Need shot starting location on grid!");
    if (!sf_getint("sht0", &sht0)) sht0=shtbgn; /*actual shot origin on grid*/
    if (!sf_getint("shtend", &shtend)) sf_error("Need shot ending location on grid!");
    if (!sf_getint("shtint", &shtint)) sf_error("Need shot interval on grid!");
    shtnum = (int)((shtend-shtbgn)/shtint) + 1;
    shtnum0 = shtnum;
    if (!sf_getint("spz", &spz)) sf_error("Need source depth!");
    if (!sf_getint("gpz", &gpz)) sf_error("Need receiver depth!");
    if (roll) if (!sf_getint("gpl", &gpl)) sf_error("Need receiver length");
    if (!sf_getint("snapinter", &snpint)) snpint=1;     /* snap interval */
    /*--- parameters of source ---*/
    if (!sf_getfloat("srctrunc", &trunc)) trunc=0.4;
    if (!sf_getint("rectz", &rectz)) rectz=1;
    if (!sf_getint("rectx", &rectx)) rectx=1;
    if (!sf_getint("repeat", &repeat)) repeat=0;
    /* abc parameters */
    if (!sf_getint("top", &top)) top=40;
    if (!sf_getint("bot", &bot)) bot=40;
    if (!sf_getint("lft", &lft)) lft=40;
    if (!sf_getint("rht", &rht)) rht=40;

    /*Set I/O file*/
    if (adj) { /* migration */
      if (wantrecord) {
	Frcd = sf_input("input"); /*record from elsewhere*/
	Fsrc  = sf_input("src");   /*source wavelet*/      
      } else {
	Frcd = sf_output("rec"); /*record produced by forward modeling*/
	Fsrc = sf_input("input");   /*source wavelet*/
      }
      Fimg  = sf_output("output");
    } else { /* modeling */
      Fimg = sf_input("input");
      Frcd = sf_output("output");
      Fsrc  = sf_input("src");   /*source wavelet*/      
    }
    left  = sf_input("left");
    right = sf_input("right");
    leftb  = sf_input("leftb");
    rightb = sf_input("rightb");
    Fvel  = sf_input("vel");  /*velocity - just for model dimension*/
    if (wantwf) {
	Ftmpwf  = sf_output("tmpwf");/*wavefield snap*/
    } else {
	Ftmpwf  = NULL;
    }

    /*--- Axes parameters ---*/
    at = sf_iaxa(Fsrc, 1); nt = sf_n(at);  dt = sf_d(at);      
    az = sf_iaxa(Fvel, 1); nzb = sf_n(az); dz = sf_d(az); oz = sf_o(az);
    ax = sf_iaxa(Fvel, 2); nxb = sf_n(ax); dx = sf_d(ax); ox = sf_o(ax);
    nzx = nzb*nxb;
    nz = nzb - top - bot;
    nx = nxb - lft - rht;
    if (!roll) gpl = nx; /* global survey setting */
    /* wavefield axis */
    wfnt = (int)(nt-1)/snpint+1;
    wfdt = dt*snpint;

    /* propagator matrices */
    if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */
    nz2 = kiss_fft_next_fast_size(nzb*pad1);
    nx2 = kiss_fft_next_fast_size(nxb);
    nk = nz2*nx2; /*wavenumber*/
    if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
    if (!sf_histint(left,"n2",&m2))  sf_error("Need n2= in left");
    if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2);
    if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);

    if (!sf_histint(leftb,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
    if (!sf_histint(leftb,"n2",&m2b))  sf_error("Need n2= in left");
    if (!sf_histint(rightb,"n1",&n2) || n2 != m2b) sf_error("Need n1=%d in right",m2b);
    if (!sf_histint(rightb,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);

    /*check record data*/
    if (adj && wantrecord){
	sf_histint(Frcd,"n1", &tmpint);
	if (tmpint != nt ) sf_error("Error parameter n1 in record!");
	sf_histint(Frcd,"n2", &tmpint);
	if (tmpint != gpl ) sf_error("Error parameter n2 in record!");
	sf_histint(Frcd,"n3", &tmpint);
	if (tmpint != shtnum0 ) sf_error("Error parameter n3 in record!");
    }

    /*allocate memory*/
    ww=sf_complexalloc(nt);
    rr=sf_floatalloc(nzx);
    lt = sf_complexalloc2(nzx,m2);
    rt = sf_complexalloc2(m2,nk);
    ltb = sf_complexalloc2(nzx,m2b);
    rtb = sf_complexalloc2(m2b,nk);
    geop = (geopar) sf_alloc(1, sizeof(*geop));
    mpip = (mpipar) sf_alloc(1, sizeof(*mpip));
    tmprec = sf_complexalloc2(nt, gpl);
    if (shtnum%nodes!=0) {
      shtnum += nodes-shtnum%nodes;
      if (verb) sf_warning("Total shot number is not divisible by total number of nodes! shunum padded to %d.", shtnum);
    }
    if (rank==0) {
      record = sf_complexalloc3(nt, gpl, shtnum);
    } else record = NULL;
    wavefld = sf_complexalloc3(nz, nx, wfnt);
    if (illum) sill = sf_floatalloc2(nz, nx);
    else sill = NULL;
    img = sf_complexalloc2(nz, nx);
    if (adj) {
      imgsum = sf_complexalloc2(nz, nx);
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz)
#endif
	for (ix=0; ix<nx; ix++)
	  for (iz=0; iz<nz; iz++)
	    imgsum[ix][iz] = sf_cmplx(0.,0.);
    }
    /*read from files*/
    sf_complexread(ww,nt,Fsrc);
    sf_complexread(lt[0],nzx*m2,left);
    sf_complexread(rt[0],m2*nk,right);
    sf_complexread(ltb[0],nzx*m2b,leftb);
    sf_complexread(rtb[0],m2b*nk,rightb);
    if(!adj) sf_complexread(img[0],nx*nz,Fimg);
    if (rank==0) {
      if(adj && wantrecord) {
	sf_complexread(record[0][0], shtnum0*gpl*nt, Frcd);
	if (shtnum0%nodes!=0) {
#ifdef _OPENMP
#pragma omp parallel for private(is,ix,it)
#endif
	  for (is=shtnum0; is<shtnum; is++)
	    for (ix=0; ix<gpl; ix++)
	      for (it=0; it<nt; it++)
		record[is][ix][it] = sf_cmplx(0.,0.);
	}
      } else {
#ifdef _OPENMP
#pragma omp parallel for private(is,ix,it)
#endif
	for (is=0; is<shtnum; is++)
	  for (ix=0; ix<gpl; ix++)
	    for (it=0; it<nt; it++)
	      record[is][ix][it] = sf_cmplx(0.,0.);
      }
    }
    
    /*close RSF files*/
    sf_fileclose(Fsrc);
    sf_fileclose(left);
    sf_fileclose(right);
    sf_fileclose(leftb);
    sf_fileclose(rightb);

    /*load constant geopar elements*/
    mpip->cpuid=rank;
    mpip->numprocs=nodes;
    /*load constant geopar elements*/
    geop->nx  = nx;
    geop->nz  = nz;
    geop->nxb = nxb;
    geop->nzb = nzb;
    geop->dx  = dx;
    geop->dz  = dz;
    geop->ox  = ox;
    geop->oz  = oz;
    geop->snpint = snpint;
    geop->spz = spz;
    geop->gpz = gpz;
    geop->gpl = gpl;
    geop->top = top;
    geop->bot = bot;
    geop->lft = lft;
    geop->rht = rht;
    geop->nt = nt;
    geop->dt = dt;
    geop->trunc = trunc;
    geop->shtnum = shtnum;

    /* output RSF files */

    if (rank==0) {
      sf_setn(ax, gpl);
      sf_setn(az, nz);
      as = sf_iaxa(Fvel, 2);
      sf_setn(as,shtnum0);
      sf_setd(as,shtint*dx);
      sf_seto(as,shtbgn*dx+ox);
      
      if (adj) { /* migration */
	if(!wantrecord) {
	  sf_oaxa(Frcd, at, 1);
	  sf_oaxa(Frcd, ax, 2);
	  sf_oaxa(Frcd, as, 3);
	  sf_settype(Frcd,SF_COMPLEX);	
	}
	sf_setn(ax, nx);
	/*write image*/
	sf_oaxa(Fimg, az, 1);
	sf_oaxa(Fimg, ax, 2);
	sf_settype(Fimg,SF_COMPLEX);
      } else { /* modeling */
	sf_oaxa(Frcd, at, 1);
	sf_oaxa(Frcd, ax, 2);
	sf_oaxa(Frcd, as ,3);
	sf_settype(Frcd,SF_COMPLEX);
      }
      
      if (wantwf) {
	sf_setn(ax, nx);
	/*write temp wavefield */
	sf_setn(at, wfnt);
	sf_setd(at, wfdt);
	
	sf_oaxa(Ftmpwf, az, 1);
	sf_oaxa(Ftmpwf, ax, 2);
	sf_oaxa(Ftmpwf, at, 3);
	sf_settype(Ftmpwf,SF_COMPLEX);
      }
    }
    
    tstart = clock();

    for (is=0; is*nodes<shtnum; is++){

      shtcur = is*nodes+rank; // current shot index

      if (shtcur<shtnum0) {
	spx = shtbgn + shtint*(shtcur);
	if (roll)
	  gpx = spx - (int)(gpl/2);
	else
	  gpx = 0;
	geop->spx = spx;
	geop->gpx = gpx;
	
	if (verb) {
	  sf_warning("============================");
	  sf_warning("processing shot #%d", shtcur);
	  sf_warning("nx=%d nz=%d nt=%d", geop->nx, geop->nz, geop->nt);
	  sf_warning("nxb=%d nzb=%d ", geop->nxb, geop->nzb);
	  sf_warning("dx=%f dz=%f dt=%f", geop->dx, geop->dz, geop->dt);
	  sf_warning("top=%d bot=%d lft=%d rht=%d", geop->top, geop->bot, geop->lft, geop->rht);
	  sf_warning("rectz=%d rectx=%d repeat=%d srctrunc=%f",rectz,rectx,repeat,geop->trunc);
	  sf_warning("spz=%d spx=%d gpz=%d gpx=%d gpl=%d", spz, spx, gpz, gpx, gpl);
	  sf_warning("snpint=%d wfdt=%f wfnt=%d ", snpint, wfdt, wfnt);
	  sf_warning("sht0=%d shtbgn=%d shtend=%d shtnum0=%d shtnum=%d", sht0, shtbgn, shtend, shtnum0, shtnum);
	  if (roll) sf_warning("Rolling survey!");
	  else sf_warning("Global survey (gpl=nx)!");
	  if (illum) sf_warning("Using source illumination!");
	  else sf_warning("No source illumination!");
	  sf_warning("============================");
	}
	
	/*generate reflectivity map*/
	reflgen(nzb, nxb, spz+top, spx+lft, rectz, rectx, repeat, rr);
	
	lrosfor2(wavefld, sill, tmprec, verb, lt, rt, m2, geop, ww, rr, pad1, illum);
      }

      if(adj && wantrecord) {
	if (rank==0) sendbuf = record[is*nodes][0];
	else sendbuf = NULL;
	recvbuf = tmprec[0];
	MPI_Scatter(sendbuf, gpl*nt, MPI_COMPLEX, recvbuf, gpl*nt, MPI_COMPLEX, 0, MPI_COMM_WORLD); // tmprec[ix][it] = record[is][ix][it];
      }
      
      if (shtcur<shtnum0) {
	lrosback2(img, wavefld, sill, tmprec, adj, verb, wantwf, ltb, rtb, m2b, geop, pad1, illum);
	if (adj) { /*local image reduction*/
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz)
#endif
	  for (ix=0; ix<nx; ix++) {
	    for (iz=0; iz<nz; iz++) {
#ifdef SF_HAS_COMPLEX_H
	      imgsum[ix][iz] += img[ix][iz];
#else
	      imgsum[ix][iz] = sf_cadd(imgsum[ix][iz],img[ix][iz]);
#endif      
	    }
	  }
	}
      }

      if (!adj || !wantrecord) {
	//	MPI_Barrier(MPI_COMM_WORLD);
	if (rank==0) recvbuf = record[is*nodes][0];
	else recvbuf = NULL;
	sendbuf = tmprec[0];
	MPI_Gather(sendbuf, gpl*nt, MPI_COMPLEX, recvbuf, gpl*nt, MPI_COMPLEX, 0, MPI_COMM_WORLD); // record[is][ix][it] = tmprec[ix][it];
      }

      if (wantwf && shtcur==0)
	sf_complexwrite(wavefld[0][0], wfnt*nx*nz, Ftmpwf);
    } /*shot iteration*/

    MPI_Barrier(MPI_COMM_WORLD);
    /*write record/image*/
    if (adj) {
      if (rank==0) {
#if MPI_VERSION >= 2
	sendbuf = (sf_complex *) MPI_IN_PLACE;
#else /* will fail */
	sendbuf = NULL;
#endif 
	recvbuf = imgsum[0];
      } else {
	sendbuf = imgsum[0];
      	recvbuf = NULL;
      }
      MPI_Reduce(sendbuf, recvbuf, nx*nz, MPI_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD); 
      if (rank==0)
	sf_complexwrite(imgsum[0], nx*nz, Fimg);
    }

    if (!adj || !wantrecord) {
      if (rank==0)
	sf_complexwrite(record[0][0], shtnum0*gpl*nt, Frcd);
    }

    /*free memory*/
    free(ww); free(rr);
    free(*lt); free(lt);
    free(*rt); free(rt);
    free(*ltb);free(ltb);
    free(*rtb);free(rtb);
    free(geop);free(mpip);
    free(*tmprec); free(tmprec);
    if (rank==0) {free(**record); free(*record); free(record);}
    free(**wavefld); free(*wavefld); free(wavefld);
    if (illum) {
      free(*sill); free(sill);
    }
    free(*img); free(img);
    if (adj) {
      free(*imgsum); free(imgsum);
    }

    tend = clock();
    duration=(double)(tend-tstart)/CLOCKS_PER_SEC;
    sf_warning(">> The CPU time of single shot migration is: %f seconds << ", duration);

    MPI_Finalize();
    exit(0);
}
Пример #15
0
int main(int argc, char* argv[])
{
    bool mig,timer;
    int it, nt, ix, nx, iz, nz, nx2, nz2, nzx, nzx2, pad1;
    int im, i, j, m2, it1, it2, its, ik, n2, nk;
    float dt, dx, dz,x0;
    sf_complex *curr, **img, *dat, **lft, **rht, **wave, *cwave, *cwavem, c;
    sf_file data, image, left, right;
    double time=0.,t0=0.,t1=0.;

    sf_init(argc,argv);

    if (!sf_getbool("mig",&mig)) mig=false;
    /* if n, modeling; if y, migration */
    if(! sf_getbool("timer",&timer)) timer=false;

    if (!sf_getint("pad1",&pad1)) pad1=1; /* padding factor on the first axis */

    if (mig) { /* migration */
	data = sf_input("in");
	image = sf_output("out");
	sf_settype(image,SF_COMPLEX);

	if (!sf_histint(data,"n1",&nx)) sf_error("No n1= in input");
	if (!sf_histfloat(data,"d1",&dx)) sf_error("No d1= in input");
	if (!sf_histfloat(data,"o1",&x0)) x0=0.; 

	if (!sf_histint(data,"n2",&nt)) sf_error("No n2= in input");
	if (!sf_histfloat(data,"d2",&dt)) sf_error("No d2= in input");

	if (!sf_getint("nz",&nz)) sf_error("Need nz=");
	/* depth samples (if migration) */
	if (!sf_getfloat("dz",&dz)) sf_error("Need dz=");
	/* depth sampling (if migration) */

	sf_putint(image,"n1",nz);
	sf_putfloat(image,"d1",dz);
	sf_putfloat(image,"o1",0.);
	sf_putstring(image,"label1","Depth");

	sf_putint(image,"n2",nx);
	sf_putfloat(image,"d2",dx);
	sf_putfloat(image,"o2",x0);
	sf_putstring(image,"label2","Distance");
    } else { /* modeling */
	image = sf_input("in");
	data = sf_output("out");
	sf_settype(data,SF_COMPLEX);

	if (!sf_histint(image,"n1",&nz)) sf_error("No n1= in input");
	if (!sf_histfloat(image,"d1",&dz)) sf_error("No d1= in input");

	if (!sf_histint(image,"n2",&nx))  sf_error("No n2= in input");
	if (!sf_histfloat(image,"d2",&dx)) sf_error("No d2= in input");
	if (!sf_histfloat(image,"o2",&x0)) x0=0.; 	

	if (!sf_getint("nt",&nt)) sf_error("Need nt=");
	/* time samples (if modeling) */
	if (!sf_getfloat("dt",&dt)) sf_error("Need dt=");
	/* time sampling (if modeling) */

	sf_putint(data,"n1",nx);
	sf_putfloat(data,"d1",dx);
	sf_putfloat(data,"o1",x0);
	sf_putstring(data,"label1","Distance");

	sf_putint(data,"n2",nt);
	sf_putfloat(data,"d2",dt);
	sf_putfloat(data,"o2",0.);
	sf_putstring(data,"label2","Time");
	sf_putstring(data,"unit2","s");
    }

    nk = cfft2_init(pad1,nx,nz,&nx2,&nz2);

    nzx = nz*nx;
    nzx2 = nz2*nx2;

    img = sf_complexalloc2(nz,nx);
    dat = sf_complexalloc(nx);

    /* propagator matrices */
    left = sf_input("left");
    right = sf_input("right");

    if (!sf_histint(left,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in left",nzx);
    if (!sf_histint(left,"n2",&m2))  sf_error("No n2= in left");
    
    if (!sf_histint(right,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in right",m2);
    if (!sf_histint(right,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in right",nk);
 
    lft = sf_complexalloc2(nzx,m2);
    rht = sf_complexalloc2(m2,nk);

    sf_complexread(lft[0],nzx*m2,left);
    sf_complexread(rht[0],m2*nk,right);

    curr = sf_complexalloc(nzx2);

    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);
    wave = sf_complexalloc2(nzx2,m2);

    icfft2_allocate(cwavem);

    for (iz=0; iz < nzx2; iz++) {
	curr[iz] = sf_cmplx(0.,0.);
    }


    if (mig) { /* migration */
	/* step backward in time */
	it1 = nt-1;
	it2 = -1;
	its = -1;	
    } else { /* modeling */
	sf_complexread(img[0],nzx,image);

	/* transpose */
	for (ix=0; ix < nx; ix++) {
	    for (iz=0; iz < nz; iz++) {
		curr[ix+iz*nx2]=img[ix][iz];
	    }
	}
	
	/* step forward in time */
	it1 = 0;
	it2 = nt;
	its = +1;
    }

    if (timer) t0 = gtod_timer();

    /* time stepping */
    for (it=it1; it != it2; it += its) {
	sf_warning("it=%d;",it);

	if (mig) { /* migration <- read data */
	    sf_complexread(dat,nx,data);
	} else {
	    for (ix=0; ix < nx; ix++) {
		dat[ix] = sf_cmplx(0.,0.);
	    }
	}

	for (ix=0; ix < nx; ix++) {
	    if (mig) {
#ifdef SF_HAS_COMPLEX_H
		curr[ix] += dat[ix];
#else
		curr[ix] = sf_cadd(curr[ix],dat[ix]);
#endif
	    } else {
		dat[ix] = curr[ix];
	    }
	}

	/* matrix multiplication */
	cfft2(curr,cwave);

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


#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,i,j,im,c) shared(curr,lft,wave)
#endif
	for (ix = 0; ix < nx; ix++) {
	    for (iz=0; iz < nz; iz++) {
		i = ix+iz*nx;  /* original grid */
		j = ix+iz*nx2; /* padded grid */
		
		c = sf_cmplx(0.,0.); /* initialize */

		for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
		    c += lft[im][i]*wave[im][j];
#else
		    c = sf_cadd(c,sf_cmul(lft[im][i], wave[im][j]));
#endif	    
		}

		curr[j] = c;
	    }
	}
	
	if (!mig) { /* modeling -> write out data */
	    sf_complexwrite(dat,nx,data);
	}
    }
    sf_warning(".");

    if (timer)
      {
        t1 = gtod_timer();
        time = t1-t0;
        sf_warning("Time = %lf\n",time);
      }

    if (mig) {
	/* transpose */
	for (ix=0; ix < nx; ix++) {
	    for (iz=0; iz < nz; iz++) {
		img[ix][iz] = curr[ix+iz*nx2];
	    }
	}

	sf_complexwrite(img[0],nzx,image);
    }

    cfft2_finalize();
    exit(0);
}
Пример #16
0
int cfft2_init(int pad1           /* padding on the first axis */,
	       int nx,   int ny   /* input data size */, 
	       int *nx2, int *ny2 /* padded data size */)
/*< initialize >*/
{

#ifdef SF_HAS_FFTW
#ifdef _OPENMP
    fftwf_init_threads();
    if (false)
      sf_warning("Using threaded FFTW3! \n");
    fftwf_plan_with_nthreads(omp_get_max_threads());
#else
    if (false)
      sf_warning("Using FFTW3! \n");
#endif
#else
    if (false)
      sf_warning("Using KissFFT! \n");
#endif

#ifndef SF_HAS_FFTW
    int i2;
#endif

    nk = n1 = kiss_fft_next_fast_size(nx*pad1);
    n2 = kiss_fft_next_fast_size(ny);

    cc = sf_complexalloc2(n1,n2);
    
#ifdef SF_HAS_FFTW
    dd = sf_complexalloc2(nk,n2);

    cfg = fftwf_plan_dft_2d(n2,n1,
			    (fftwf_complex *) cc[0], 
			    (fftwf_complex *) dd[0],
			    FFTW_FORWARD, FFTW_MEASURE);

    icfg = fftwf_plan_dft_2d(n2,n1,
			     (fftwf_complex *) dd[0], 
			     (fftwf_complex *) cc[0],
			     FFTW_BACKWARD, FFTW_MEASURE);

    if (NULL == cfg || NULL == icfg) sf_error("FFTW failure.");
#else
    cfg1  = kiss_fft_alloc(n1,0,NULL,NULL);
    icfg1 = kiss_fft_alloc(n1,1,NULL,NULL);

    cfg2  = kiss_fft_alloc(n2,0,NULL,NULL);
    icfg2 = kiss_fft_alloc(n2,1,NULL,NULL);
 	
    tmp =    (kiss_fft_cpx **) sf_alloc(n2,sizeof(*tmp));
    tmp[0] = (kiss_fft_cpx *)  sf_alloc(nk*n2,sizeof(kiss_fft_cpx));
#ifdef _OPENMP
#pragma omp parallel for private(i2) default(shared)
#endif
    for (i2=0; i2 < n2; i2++) {
	tmp[i2] = tmp[0]+i2*nk;
    }
	
    trace2 = sf_complexalloc(n2);
    ctrace2 = (kiss_fft_cpx *) trace2;
#endif

    *nx2 = n1;
    *ny2 = n2;
	
    wt =  1.0/(n1*n2);
	
    return (nk*n2);
}
Пример #17
0
int main(int argc, char* argv[])
{
    bool verb;
    int nx,nv,np,nw,nh, ix,iv,ip,iw,ih;
    float v0,v2,v1,v,dv, dx,dp,dw,dh, x0,p0,w0,h0, x,p,w,h;
    sf_complex *ctrace, **ctrace2, shift;
    sf_file in, out;

    sf_init (argc,argv);
    in = sf_input("in");
    out = sf_output("out");

    if (SF_COMPLEX != sf_gettype(in)) sf_error("Need complex input");
    if (!sf_histint(in,"n1",&nw)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&nh)) sf_error("No n2= in input");
    if (!sf_histint(in,"n3",&nx)) sf_error("No n3= in input");
    if (!sf_histint(in,"n4",&np)) sf_error("No n4= in input");

    if (!sf_getbool("verb",&verb)) verb=true;
    /* verbosity flag */

    if (!sf_histfloat(in,"o1",&w0)) w0=0.;  
    if (!sf_histfloat(in,"d1",&dw)) sf_error("No d1= in input");

    if (!sf_getint("nv",&nv)) sf_error("Need nv=");
    /* velocity steps */
    if (!sf_getfloat("dv",&dv)) sf_error("Need dv=");
    /* velocity step size */
    if (!sf_getfloat("v0",&v0) && 
	!sf_histfloat(in,"v0",&v0)) sf_error("Need v0=");
    /*( v0 starting velocity )*/

    if(!sf_histfloat(in,"d2",&dh)) sf_error("No d2= in input");
    if(!sf_histfloat(in,"d3",&dx)) sf_error("No d3= in input");
    if(!sf_histfloat(in,"d4",&dp)) sf_error("No d4= in input");

    if(!sf_histfloat(in,"o2",&h0)) h0=0.;
    if(!sf_histfloat(in,"o3",&x0)) x0=0.;
    if(!sf_histfloat(in,"o4",&p0)) sf_error("No o4= in input");

    sf_putfloat(out,"o2",v0+dv);
    sf_putfloat(out,"d2",dv);
    sf_putint(out,"n2",nv);

    sf_putstring(out,"label2","Velocity");

    dx *= 2.*SF_PI;
    x0 *= 2.*SF_PI;

    dw *= 2.*SF_PI;
    w0 *= 2.*SF_PI;

    ctrace  = sf_complexalloc(nw);
    ctrace2 = sf_complexalloc2(nw,nv);

    for (ip=0; ip < np; ip++) {
	if (verb) sf_warning("slope %d of %d;", ip+1,np);
	p = p0+ip*dp;

	for (ix=0; ix < nx; ix++) {
	    x = x0+ix*dx; 

	    for (iv=0; iv < nv; iv++) {
		for (iw=0; iw < nw; iw++) {
		    ctrace2[iv][iw] = sf_cmplx(0.,0.);
		}
	    }

	    for (ih=0; ih < nh; ih++) {
		h = h0+ih*dh; 
		h *= 4.0f * h;
		
		sf_complexread(ctrace,nw,in);
 
		for (iv=0; iv < nv; iv++) {
		    v = v0 + (iv+1)*dv;
		    v2 = ((v0*v0) - (v*v)); 
		    v1 = h*(1.0/(v0*v0) - 1.0/(v*v)); 
		
		    for (iw=0; iw < nw; iw++) {
			w = w0+iw*dw;
			w = - 0.25f * 0.25f * (v2 * p * (p * w + 2.0f * x) + w*v1);
		    
			shift = sf_cmplx(cosf(w),sinf(w));
		    
#ifdef SF_HAS_COMPLEX_H
			ctrace2[iv][iw] += ctrace[iw] * shift;
#else
			ctrace2[iv][iw] = sf_cadd(ctrace2[iv][iw],sf_cmul(ctrace[iw],shift));
#endif
		    } /* w */
		} /* v */
	    } /* h */

	    sf_complexwrite(ctrace2[0],nw*nv,out);
 	} /* x */
    } /* p */
    if (verb) sf_warning(".");

    exit (0);
}
Пример #18
0
int main(int argc, char *argv[])
{
    int nw, n1, n2, n4, iw, i1, i2, i4, s1, x1, s2, x2, m, fold;
    int jumpo, jumps, newn1, newn2, tn;
    float d1, d2, newd1, newd2;
    bool verb, stack, both;

    sf_complex *dd, *ref=NULL, *mm=NULL, *mtemp=NULL;
    sf_file in, out, dif=NULL;

    sf_init(argc,argv);

    in = sf_input("in");
    out = sf_output("out");

    if (SF_COMPLEX != sf_gettype(in)) sf_error("Need complex input");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    if (!sf_histint(in,"n3",&nw)) sf_error("No n3= in input");

    if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"d2",&d2)) sf_error("No d2= in input");
    
    if (d1!=d2) sf_error("Need d1==d2");

    n4 = sf_leftsize(in,3);
    fold = 0;

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

    if (!sf_getbool("stack",&stack)) stack=false;
    /* stack flag, if y, no common multiple gather */

    if (!sf_getbool("both",&both)) both=false;
    /* receiver flag, if y, receiver with both sides */

    if (!stack) {
	if (!sf_getint("jumpo",&jumpo)) jumpo=1;
	/* jump in offset dimension, only for stack=n */
	
	if (!sf_getint("jumps",&jumps)) jumps=1;
	/* jump in shot dimension, only for stack=n  */
    }
    newn1 = n1;
    newn2 = n2;
    if (!stack) {
	if (!both) {
	    sf_putint(out,"n1",(2*n1-1));
	    sf_putfloat(out,"d1",d1);
	    sf_putfloat(out,"o1",(1-n1)*d1);
	} else {
	    sf_putint(out,"n1",n1);
	    sf_putfloat(out,"d1",d1);
	    sf_putfloat(out,"o1",(-1*n1/2)*d1);
	}
	(void) sf_shiftdim(in, out, 1);
	if (n1%jumpo == 0) {
	    newn1 = n1 / jumpo;
	} else {
	    newn1 = n1 / jumpo +1;
	}
	if (n2%jumps == 0) {
	    newn2 = n2 / jumps;
	} else {
	    newn2 = n2 / jumps +1;
	}
	newd1 = (float) (d1 * jumpo);
	newd2 = (float) (d2 * jumps);
	sf_putint(out,"n2",newn1);
	sf_putfloat(out,"d2",newd1);
	sf_putint(out,"n3",newn2);
	sf_putfloat(out,"d3",newd2);
    }

    if (NULL != sf_getstring ("dif")) {
	dif = sf_input("dif");
	ref = sf_complexalloc(n1*n2);
    } else {
	dif = NULL;
    }	
    
    dd = sf_complexalloc(n1*n2);

    if (stack) {
	mm = sf_complexalloc(n1*n2);
    } else {
	if (!both) {
	    mm = sf_complexalloc((2*n1-1)*n1*n2);
	    mtemp = sf_complexalloc((2*n1-1)*newn1*newn2);
	} else {
	    mm = sf_complexalloc(n1*n1*n2);
	    mtemp = sf_complexalloc(n1*newn1*newn2);
	}
    }

    /* loop over n4 */
    for (i4=0; i4 < n4; i4++) {
	if (verb) sf_warning("slice %d of %d",i4+1,n4);
	for (iw=0; iw < nw; iw++) { /* loop over frequency */
	    if (verb) sf_warning("frequency %d of %d;",iw+1,nw);
	    sf_complexread(dd,n1*n2,in);
	    if (NULL != dif) {
		sf_complexread(ref,n1*n2,dif);
	    }
	    if (!both) {
		for (i2=0; i2 < n2; i2++) {
		    for (i1=0; i1 < n1; i1++) {
			mm[i2*n1+i1] = sf_cmplx(0.,0.);
			fold = 0;
			for (m=(-1*n1+1); m < n1; m++) {
			    if (m < 0) {
				x1 = -1*m;
				s1 = i2 + m;
				x2 = i1 - m;
				s2 = m + i2;
			    } else if ((i1-m) < 0) {
				x1 = m;
				s1 = i2;
				x2 = m - i1;
				s2 = i2 + i1;
			    } else {
				x1 = m;
				s1 = i2;
				x2 = i1 - m;
				s2 = m + i2;
			    }
			    if (stack) {
				if (s1 >= 0 && s1 < n2 && x1 >= 0 && x1 < n1 && 
				    s2 >= 0 && s2 < n2 && x2 >= 0 && x2 < n1 ) {
#ifdef SF_HAS_COMPLEX_H
				    if(NULL != dif) {
					mm[i2*n1+i1] += dd[s1*n1+x1]*ref[s2*n1+x2];
				    } else {
					mm[i2*n1+i1] += dd[s1*n1+x1]*dd[s2*n1+x2];
				    }
#else
				    if(NULL != dif) {
					mm[i2*n1+i1] = sf_cadd(mm[i2*n1+i1],sf_cmul(dd[s1*n1+x1],ref[s2*n1+x2]));
				    } else {
					mm[i2*n1+i1] = sf_cadd(mm[i2*n1+i1],sf_cmul(dd[s1*n1+x1],dd[s2*n1+x2]));
				    }
#endif
				} else {
				    mm[i2*n1+i1] = sf_cmplx(0.,0.);
				}
				if (0.0 != cabsf(mm[i2*n1+i1])) fold++;	
			    } else {
				if (s1 >= 0 && s1 < n2 && x1 >= 0 && x1 < n1 && 
				    s2 >= 0 && s2 < n2 && x2 >= 0 && x2 < n1 ) {
#ifdef SF_HAS_COMPLEX_H
				    if(NULL != dif) {
					mm[i2*(2*n1-1)*n1+i1*(2*n1-1)+m+n1-1] = dd[s1*n1+x1]*ref[s2*n1+x2];
				    } else {
					mm[i2*(2*n1-1)*n1+i1*(2*n1-1)+m+n1-1] = dd[s1*n1+x1]*dd[s2*n1+x2];
				    }
#else
				    if(NULL != dif) {
					mm[i2*(2*n1-1)*n1+i1*(2*n1-1)+m+n1-1] = sf_cmul(dd[s1*n1+x1],ref[s2*n1+x2]);
				    } else {
					mm[i2*(2*n1-1)*n1+i1*(2*n1-1)+m+n1-1] = sf_cmul(dd[s1*n1+x1],dd[s2*n1+x2]);
				    }
#endif
				} else {
				    mm[i2*(2*n1-1)*n1+i1*(2*n1-1)+m+n1-1] = sf_cmplx(0.,0.);
				}
			    }
			}

			if (stack) {
#ifdef SF_HAS_COMPLEX_H
			    mm[i2*n1+i1] = mm[i2*n1+i1]/(fold+SF_EPS);
#else
			    mm[i2*n1+i1] = sf_crmul(mm[i2*n1+i1],1.0/(fold+SF_EPS));
#endif
			}

		    }
		}
		
		if (!stack) {
		    tn = 0;
		    for (i2=0; i2 < n2; i2++) {
			for (i1=0; i1 < n1; i1++) {
			    if ((i2 % jumps == 0) && (i1 % jumpo == 0)) {
				for (m=(-1*n1+1); m < n1; m++) {
				    mtemp[tn] = mm[i2*(2*n1-1)*n1+i1*(2*n1-1)+m+n1-1];
				    tn ++;
				}
			    }
			}
		    }
		    if (tn!=(2*n1-1)*newn1*newn2) sf_error("jump error!");		    
		    sf_complexwrite(mtemp,tn,out);
		} else {
		    sf_complexwrite(mm,n1*n2,out);
		}
	    } else {
		for (i2=0; i2 < n2; i2++) {
		    for (i1=0; i1 < n1; i1++) {
			mm[i2*n1+i1] = sf_cmplx(0.,0.);
			fold = 0;
			for (m=0; m < n1; m++) {
			    x1 = m;
			    s1 = i2;
			    x2 = i1 - m + n1/2;
			    s2 = m + i2 - n1/2;
			    if (stack) {
				if (s1 >= 0 && s1 < n2 && x1 >= 0 && x1 < n1 && 
				    s2 >= 0 && s2 < n2 && x2 >= 0 && x2 < n1 ) {
#ifdef SF_HAS_COMPLEX_H
				    if(NULL != dif) {			
					mm[i2*n1+i1] += dd[s1*n1+x1]*ref[s2*n1+x2];
				    } else {
					mm[i2*n1+i1] += dd[s1*n1+x1]*dd[s2*n1+x2];
				    }
#else
				    if(NULL != dif) {	
					mm[i2*n1+i1] = sf_cadd(mm[i2*n1+i1],sf_cmul(dd[s1*n1+x1],ref[s2*n1+x2]));
				    } else {
					mm[i2*n1+i1] = sf_cadd(mm[i2*n1+i1],sf_cmul(dd[s1*n1+x1],dd[s2*n1+x2]));
				    }
#endif
				} else {
				    mm[i2*n1+i1] = sf_cmplx(0.,0.);
				}
				if (0.0 != cabsf(mm[i2*n1+i1])) fold++;				
			    } else {
				if (s1 >= 0 && s1 < n2 && x1 >= 0 && x1 < n1 && 
				    s2 >= 0 && s2 < n2 && x2 >= 0 && x2 < n1 ) {
#ifdef SF_HAS_COMPLEX_H
				    if(NULL != dif) {			
					mm[i2*n1*n1+i1*n1+m] = dd[s1*n1+x1]*ref[s2*n1+x2];
				    } else {
					mm[i2*n1*n1+i1*n1+m] = dd[s1*n1+x1]*dd[s2*n1+x2];
				    }
#else
				    if(NULL != dif) {	
					mm[i2*n1*n1+i1*n1+m] = sf_cmul(dd[s1*n1+x1],ref[s2*n1+x2]);
				    } else {
					mm[i2*n1*n1+i1*n1+m] = sf_cmul(dd[s1*n1+x1],dd[s2*n1+x2]);
				    }
#endif
				} else {
				    mm[i2*n1*n1+i1*n1+m] = sf_cmplx(0.,0.);
				}
			    }
			}
			if (stack) {
#ifdef SF_HAS_COMPLEX_H
			    mm[i2*n1+i1] = mm[i2*n1+i1]/(fold+SF_EPS);
#else
			    mm[i2*n1+i1] = sf_crmul(mm[i2*n1+i1],1.0/(fold+SF_EPS));
#endif
			}

		    }
		}
		if (!stack) {
		    tn = 0;
		    for (i2=0; i2 < n2; i2++) {
			for (i1=0; i1 < n1; i1++) {
			    if (i2 % jumps == 0 && i1 % jumpo == 0) {
				for (m=0; m < n1; m++) {
				    mtemp[tn] = mm[i2*n1*n1+i1*n1+m];
				    tn ++;
				}
			    }
			}
		    }	
		    if (tn!=n1*newn1*newn2) sf_error("jump error!");
		    sf_complexwrite(mtemp,tn,out);
		} else {
		    sf_complexwrite(mm,n1*n2,out);
		}
	    }		
	}   
	if (verb) sf_warning(".");
    
    }

    exit(0);
}
Пример #19
0
int main(int argc, char* argv[])
{
    bool inv, dip, verb, decomp;
    int i, i1, n1, iw, nw, i2, n2, rect, niter, n12, nt, ip, np;
    char *label;
    float t, d1, w, w0, dw, p0, dp, p;
    sf_complex *trace, *kbsc, *sscc=NULL;
    sf_file in, out, basis;
	
    sf_init(argc,argv);
    in = sf_input("in");
    out = sf_output("out");
	
    if (SF_COMPLEX != sf_gettype(in)) sf_error("Need complex input");
	
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histfloat(in,"d1",&d1)) d1=1.;
    label = sf_histstring(in,"label1");
	
    if (!sf_getbool("inv",&inv)) inv=false;
    /* if y, do inverse transform */
	
    if (!sf_getbool("verb",&verb)) verb = false;
    /* verbosity flag */
	
    if (!sf_getbool("dip",&dip)) dip = false;
    /* if y, do dip decomposition */

    if (!sf_getbool("decompose",&decomp)) decomp = false;
    /* if y, output decomposition */
	
    if (NULL != sf_getstring("basis")) {
	basis = sf_output("basis");
    } else {
	basis = NULL;
    }
	
    if (!inv) {
	n2 = sf_leftsize(in,1);
	sf_shiftdim(in, out, 2);
		
	if (!sf_getint("rect",&rect)) rect=10;
	/* smoothing radius (in time, samples) */
	if (!sf_getint("niter",&niter)) niter=100;
	/* number of inversion iterations */
		
	if (dip) {
	    if (!sf_getint("np",&np)) sf_error("Need np=");
	    /* number of slopes */
			
	    if (!sf_getfloat("dp",&dp)) sf_error("Need dp=");
	    /* slope step */
			
	    if (!sf_getfloat("p0",&p0)) sf_error("Need p0=");
	    /* first slope */
			
	    sf_putint(out,"n2",np);
	    sf_putfloat(out,"d2",dp);
	    sf_putfloat(out,"o2",p0);
	    sf_putstring(out,"label2","Slope");
	    sf_putstring(out,"unit2","");
			
            if (!sf_histint(in,"n2",&nw)) nw=1;
	    if (!sf_histfloat(in,"d2",&dw)) dw=1.;
	    if (!sf_histfloat(in,"o2",&w0)) w0=0.;
	} else {
	    if (!sf_getint("nw",&nw)) nw = kiss_fft_next_fast_size(n1);
	    /* number of frequencies */
			
	    if (!sf_getfloat("dw",&dw)) dw = 1./(nw*d1);
	    /* frequency step */
			
	    if (!sf_getfloat("w0",&w0)) w0=-0.5/d1;
	    /* first frequency */
			
	    sf_putint(out,"n2",nw);
	    sf_putfloat(out,"d2",dw);
	    sf_putfloat(out,"o2",w0);
			
	    if (NULL != label && !sf_fft_label(2,label,out)) 
		sf_putstring(out,"label2","Wavenumber");
	    sf_fft_unit(2,sf_histstring(in,"unit1"),out);
	}
    } else {
	n2 = sf_leftsize(in,2);
		
	if (dip) {
	    if (!sf_histint(in,"n2",&np)) sf_error("No n2= in input");
	    if (!sf_histfloat(in,"d2",&dp)) sf_error("No d2= in input");
	    if (!sf_histfloat(in,"o2",&p0)) sf_error("No o2= in input");
			
            if (!sf_histint(in,"n2",&nw)) nw=1;
	    if (!sf_histfloat(in,"d3",&dw)) dw=1.;
	    if (!sf_histfloat(in,"o3",&w0)) w0=0.;
	} else {
	    if (!sf_histint(in,"n2",&nw)) sf_error("No n2= in input");
	    if (!sf_histfloat(in,"d2",&dw)) sf_error("No d2= in input");
	    if (!sf_histfloat(in,"o2",&w0)) sf_error("No o2= in input");
	}
		
	sf_unshiftdim(in, out, 2);
    }
	
    if (NULL != basis) {
	sf_shiftdim(in, basis, 2);
	if (dip) {
	    sf_putint(basis,"n2",np);
	    sf_putfloat(basis,"d2",dp);
	    sf_putfloat(basis,"o2",p0);
	    sf_putstring(basis,"label2","Slope");
	    sf_putstring(basis,"unit2","");
	} else {
	    sf_putint(basis,"n2",nw);
	    sf_putfloat(basis,"d2",dw);
	    sf_putfloat(basis,"o2",w0);
	    if (NULL != label && !sf_fft_label(2,label,basis)) 
		sf_putstring(basis,"label2","Wavenumber");
	    sf_fft_unit(2,sf_histstring(in,"unit1"),out);
	}
    }
    
    nt = dip? np:nw;
	
    n12 = nt*n1;
    dw *= 2.*SF_PI;
    w0 *= 2.*SF_PI;
	
    trace = sf_complexalloc(n1);
    kbsc = sf_complexalloc(n12);
    sscc = sf_complexalloc(n12);
    
    if (!dip) {
	/* basis functions */
	for (iw=0; iw < nw; iw++) {
	    w = w0 + iw*dw;
			
	    for (i1=0; i1 < n1; i1++) {
		t = i1*d1;
				
		kbsc[iw*n1+i1] = sf_cmplx(cosf(w*t),sinf(w*t));
	    }
	}
		
	if (NULL != basis) {
	    sf_complexwrite(kbsc,n12,basis);
	}
    }
	
    if (!inv) cmultidivn_init(nt, 1, n1, &n1, &rect, kbsc, (bool) (verb && (n2 < 500)));
	
    for (i2=0; i2 < n2; i2++) {
	sf_warning("slice %d of %d;",i2+1,n2);
		
	if (dip) {
	    w = w0 + (i2 % nw)*dw;
	    for (ip=0; ip < np; ip++) {
		p = -w*(p0 + ip*dp);
				
		for (i1=0; i1 < n1; i1++) {
		    t = i1*d1;
					
		    kbsc[ip*n1+i1] = sf_cmplx(cosf(p*t),sinf(p*t));
		}
	    }
			
	    if (NULL != basis) {
		sf_complexwrite(kbsc,n12,basis);
	    }
	}
		
	if (!inv) {
	    sf_complexread(trace,n1,in);
	    cmultidivn (trace,sscc,niter);

	    if (decomp) {
		for (iw=0; iw < nt; iw++) {
		    for (i1=0; i1 < n1; i1++) {
			i = iw*n1+i1;
					
#ifdef SF_HAS_COMPLEX_H
			sscc[i] *= kbsc[i];
#else
			sscc[i1] = sf_cmul(sscc[i],kbsc[i]);
#endif
		    }
		}
	    }

	    sf_complexwrite(sscc,n12,out);
	} else {
	    for (i1=0; i1 < n1; i1++) {
		trace[i1] = sf_cmplx(0.,0.);
	    }
	    sf_complexread(sscc,n12,in);
	    for (iw=0; iw < nt; iw++) {
		for (i1=0; i1 < n1; i1++) {
		    i = iw*n1+i1;
					
#ifdef SF_HAS_COMPLEX_H
		    trace[i1] += sscc[i]*kbsc[i];
#else
		    trace[i1] = sf_cadd(trace[i1],sf_cmul(sscc[i],kbsc[i]));
#endif
		}
	    }
	    sf_complexwrite(trace,n1,out);
	}
    }
    sf_warning(".");
	
    exit(0);
}
Пример #20
0
int main(int argc, char* argv[])
{
    int nw,nh,nx, iw,ix,ih, k;
    float dw, h0,dh,dx, w0,w,w2, h,h2;
    sf_complex diag, diag2, *in=NULL, *out=NULL, offd, offd2, c1, c2;
    bool all;
    ctris slv;
    sf_file input=NULL, output=NULL;

    sf_init (argc,argv);
    input = sf_input("in");
    output = sf_output("out");

    if (SF_COMPLEX != sf_gettype(input)) sf_error("Need complex input");

    if (!sf_histint(input,"n1",&nx)) sf_error("No n1= in input");
    if (!sf_histint(input,"n2",&nw)) sf_error("No n2= in input");
    if (!sf_histfloat(input,"d1",&dx)) sf_error("No d1= in input");
    if (!sf_histfloat(input,"d2",&dw)) sf_error("No d2= in input");
    if (!sf_histfloat(input,"o2",&w0)) sf_error("No o2= in input");

    if (!sf_getint("nh",&nh)) sf_error("Need nh=");
    /* Number of steps in offset */
    if (!sf_getfloat("dh",&dh)) sf_error("Need dh=");
    /* Offset step size */
    if (!sf_getfloat("h0",&h0)) sf_error("Need h0=");
    /* Initial offset */

    if (!sf_getbool("all",&all)) all=false;
    /* if y, output all offsets */

    if (all) {
	sf_putint(output,"n2",nh+1);
	sf_putfloat(output,"d2",dh);
	sf_putfloat(output,"o2",h0);
	sf_putint(output,"n3",nw);
	sf_putfloat(output,"d3",dw);
	sf_putfloat(output,"o3",w0);
    }

    dh /= dx;
    h0 /= dx;

    w0 *= 2.*SF_PI;
    dw *= 2.*SF_PI;

    in = sf_complexalloc(nx);
    out = sf_complexalloc(nx);

    slv = ctridiagonal_init (nx);

    for (iw=0; iw < nw; iw++) {
	sf_warning("frequency %d of %d;",iw+1,nw);

	w = w0+iw*dw; 
	w2 = w*w;

	sf_complexread(out,nx,input);

	if (fabsf(w) < dw) {
	    for (ix=0; ix < nx; ix++) {
		out[ix]=sf_cmplx(0.,0.);
	    }
	    if (all) {
		for (ih=0; ih < nh; ih++) {
		    sf_complexwrite (out,nx,output);
		}
	    } 
	    sf_complexwrite (out,nx,output);
	    continue;
	}

#ifdef SF_HAS_COMPLEX_H
	c1 = 3.*sf_cmplx(9. + w2,4.*w)/(w2*sf_cmplx(3.,- w));
	c2 = 3.*sf_cmplx(w2 - 27.,8.*w)/(w2*sf_cmplx(3.,- w));
#else
	c1 = sf_cdiv(sf_cmplx(3.*(9. + w2),3.*4.*w),
		     sf_cmplx(w2*3.,-w2*w));
	c2 = sf_cdiv(sf_cmplx(3.*(w2 - 27.),3.*8.*w),
		     sf_cmplx(w2*3.,-w2*w));
#endif

	for (ih=0; ih < nh; ih++) {
	    if (all) sf_complexwrite (out,nx,output);

	    for (ix=0; ix < nx; ix++) {
		in[ix] = out[ix];
	    }

	    h = h0 + ih*dh; 
	    h2 = h+dh;
	    h *= h;
	    h2 *= h2; 

#ifdef SF_HAS_COMPLEX_H	
	    offd  = 1. - c1*h2 + c2*h;
	    offd2 = 1. - c1*h  + c2*h2;
	    diag  = 12. - 2.*offd;
	    diag2 = 12. - 2.*offd2;
#else
	    offd  = sf_cadd(sf_cmplx(1.,0.),
			    sf_cadd(sf_crmul(c1,-h2),
				    sf_crmul(c2,h)));
	    offd2 = sf_cadd(sf_cmplx(1.,0.),
			    sf_cadd(sf_crmul(c1,-h),
				    sf_crmul(c2,h2)));
	    diag  = sf_cadd(sf_cmplx(12.,0.),sf_crmul(offd,-2.));
	    diag2 = sf_cadd(sf_cmplx(12.,0.),sf_crmul(offd2,-2.));
#endif

	    ctridiagonal_const_define (slv, diag2, offd2);

#ifdef SF_HAS_COMPLEX_H	
	    out[0] = diag*in[0] + offd*in[1];
#else
	    out[0] = sf_cadd(sf_cmul(diag,in[0]),
			     sf_cmul(offd,in[1]));
#endif
	    for (k = 1; k < nx - 1; k++) {
#ifdef SF_HAS_COMPLEX_H	
		out[k] = diag*in[k] + offd*(in[k+1]+in[k-1]);
#else
		out[k] = sf_cadd(sf_cmul(diag,in[k]),
				 sf_cmul(offd,sf_cadd(in[k+1],in[k-1])));
#endif
	    }
#ifdef SF_HAS_COMPLEX_H	
	    out[nx-1] = diag*in[nx-1] + offd*in[nx-2];
#else
	    out[nx-1] = sf_cadd(sf_cmul(diag,in[nx-1]),
				sf_cmul(offd,in[nx-2]));
#endif
	    ctridiagonal_solve (slv, out);
	}
	sf_complexwrite (out,nx,output);
    }
    sf_warning(".");

    exit(0);
}
Пример #21
0
int main(int argc,char**argv)
{
	void* h=NULL;
	sf_file in, out;
	float w0, f0, t0, v0, a0, qv, qa, wvp[4];
	int event, wvtype, a0ref;
	float d1, d2, o1, o2;
	int n1, n2, i2, nfft;
	sf_complex * buf;

	sf_init(argc,argv);

	in  = sf_input("in");
	out = sf_output("out");

    if (!sf_histint(in, "n1", &n1)) sf_error("No n2= in input");
    if (!sf_histint(in, "n2", &n2)) sf_error("No n2= in input");
    if (!sf_histfloat(in, "d1", &d1)) sf_error("No d1= in input");
    if (!sf_histfloat(in, "d2", &d2)) sf_error("No d2= in input");
    if (!sf_histfloat(in, "o1", &o1)) sf_error("No o1= in input");
    if (!sf_histfloat(in, "o2", &o2)) sf_error("No o2= in input");

	if(!sf_getint("wvtype", &wvtype)) wvtype =0;
	/* 0: ricker; 1: sinc; x: not support */
	switch(wvtype)
	{
	case 0:
	case 1:
		if(!sf_getfloat("w0", &w0) ) w0 = 35.0;
		/* central frequency of Ricker wavelet or bandwidth of sinc wavelet */
		wvp[0] = w0;
		break;
	}

	if(!sf_getint("event", &event)) event =2;
	/* 0: linear; 1: parabolic; 2:hyperbolic */
	if(!sf_getint("nfft", &nfft)) sf_error("nfft= must given");
	/* fft length */
	if(!sf_getfloat("t0", &t0)) t0 =0.3;
	/* event travel time at x=0 */
	if(!sf_getfloat("v0", &v0)) v0 =1500.0;
	/* event velocity at x=0, for reference frequency f0 */
	if(!sf_getfloat("a0", &a0)) a0 =1.0;
	/* event amplitude at t=a0ref (x=0) */
	if(!sf_getfloat("qv", &qv)) qv = -1.0;
	/* Q factor for velocity dispersion */
	if(!sf_getfloat("qa", &qa) ) qa = qv;
	/* Q factor for amplitude attenuation */
	if(!sf_getfloat("f0", &f0)) f0 = w0;
	/* reference frequency for velocity dispersion and amplitude attenuation */
	if(!sf_getint("a0ref", &a0ref) ) a0ref = 0;
	/* reference point for a0: 0 - t0; 1 - a0 */

	h = sf_addevent_init(nfft, o1,d1,
		wvtype, wvp,
		event, t0, v0, a0,
		qv, qa, f0, a0ref);

	buf = sf_complexalloc(n1);

	for(i2=0;i2<n2;i2++)
	{
		sf_complexread(buf, n1, in);
		sf_addevent(h , fabs(d2*i2+o2), buf);
		sf_complexwrite(buf, n1, out);
	}
	sf_addevent_close(h);

	return 0;
}
Пример #22
0
int lrosfor2(sf_complex ***wavfld, float **sill, sf_complex **rcd, bool verb,
	     sf_complex **lt, sf_complex **rt, int m2,
	     geopar geop, sf_complex *ww, float *rr, int pad1, bool illum)
/*< low-rank one-step forward modeling >*/
{
    int it,iz,im,ik,ix,i,j;     /* index variables */
    int nxb,nzb,gpz,gpx,gpl,snpint,wfit;
    int nt,nz,nx, nk, nz2, nx2, nzx2;
    float dt;
    sf_complex c;
    sf_complex *cwave, *cwavem;
    sf_complex **wave, *curr;
#ifdef _OPENMP
    int nth;
#endif

    nx = geop->nx;
    nz = geop->nz;
    nxb = geop->nxb;
    nzb = geop->nzb;
/*    dx = geop->dx;
      dz = geop->dz; */

/*    spx = geop->spx;
      spz = geop->spz; */
    gpz  = geop->gpz;
    gpx  = geop->gpx;
    gpl  = geop->gpl;
    snpint = geop->snpint;
    
    nt = geop->nt;
    dt = geop->dt;

#ifdef _OPENMP
#pragma omp parallel  
{
    nth = omp_get_num_threads();
}
    sf_warning(">>>> Using %d threads <<<<<", nth);
#endif
    
    /*Matrix dimensions*/
    nk = cfft2_init(pad1,nzb,nxb,&nz2,&nx2);
/*    nzx = nzb*nxb; */
    nzx2 = nz2*nx2;

    curr   = sf_complexalloc(nzx2);
    cwave  = sf_complexalloc(nk);
    cwavem = sf_complexalloc(nk);
    wave   = sf_complexalloc2(nzx2,m2);

    icfft2_allocate(cwavem);

#ifdef _OPENMP
#pragma omp parallel for private(iz)
#endif
    for (iz=0; iz < nzx2; iz++) {
	curr[iz] = sf_cmplx(0.,0.);
    }

    if (illum) {
#ifdef _OPENMP
#pragma omp parallel for private(iz)
#endif
      for (ix=0; ix < nx; ix++) {
	for (iz=0; iz < nz; iz++) {
	  sill[ix][iz] = 0.f;
	}
      }
    }

    /*Main loop*/
    wfit = 0;
    for (it = 0; it < nt; it++) {
	if (verb) sf_warning("Forward source it=%d/%d;", it, nt-1);
	
	/*matrix multiplication*/
	cfft2(curr,cwave);

	for (im = 0; im < m2; im++) {
#ifdef _OPENMP
#pragma omp parallel for private(ik)
#endif
	    for (ik = 0; ik < nk; ik++) {
#ifdef SF_HAS_COMPLEX_H
		cwavem[ik] = cwave[ik]*rt[ik][im];
#else
		cwavem[ik] = sf_cmul(cwave[ik],rt[ik][im]);
#endif
	    }
	    icfft2(wave[im],cwavem);
	}

#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,i,j,im,c) shared(curr,lt,wave)
#endif
	for (ix = 0; ix < nxb; ix++) {
	    for (iz=0; iz < nzb; iz++) {
		i = iz+ix*nzb;  /* original grid */
		j = iz+ix*nz2; /* padded grid */
		if ((it*dt)<=geop->trunc) {
#ifdef SF_HAS_COMPLEX_H
		  c = ww[it] * rr[i]; // source term
#else
		  c = sf_crmul(ww[it], rr[i]); // source term
#endif
		} else {
		  c = sf_cmplx(0.,0.);
		}
		for (im = 0; im < m2; im++) {
#ifdef SF_HAS_COMPLEX_H
		    c += lt[im][i]*wave[im][j];
#else
		    c = sf_cadd(c,sf_cmul(lt[im][i], wave[im][j]));
#endif
		}
		curr[j] = c;
	    }
	}

#ifdef _OPENMP
#pragma omp parallel for private(ix,j)
#endif	 
	for ( ix =0 ; ix < gpl; ix++) {
	    j = (gpz+geop->top)+(ix+gpx+geop->lft)*nz2; /* padded grid */
	    rcd[ix][it] = curr[j];
	}
	
	if ( it%snpint == 0 ) {
#ifdef _OPENMP
#pragma omp parallel for private(ix,iz,j)
#endif
	    for ( ix = 0; ix < nx; ix++) {
		for ( iz = 0; iz<nz; iz++ ) { 
		    j = (iz+geop->top)+(ix+geop->lft)*nz2; /* padded grid */
		    wavfld[wfit][ix][iz] = curr[j];
		    if (illum) sill[ix][iz] += pow(hypotf(crealf(curr[j]),cimagf(curr[j])),2);
		}
	    }
	    wfit++;
	}
    } /*Main loop*/
    if (verb) sf_warning(".");
    cfft2_finalize();
    return wfit;
}