Ejemplo n.º 1
0
int main(int argc, char* argv[])
{
    bool adj,timer,verb;
    int nt, nx, nz, nx2, nz2, nzx, nzx2, ntx, pad1, snap, wfnt;
    int m2, n2, nk, nth=1;
    float dt, dx, dz, ox;
    sf_complex **img, **dat, **lt, **rt, ***wvfld, *ww;
    sf_file data, image, left, right, snaps, src;
    double time=0.,t0=0.,t1=0.;
    geopar geop;

    sf_init(argc,argv);

    if (!sf_getbool("adj",&adj)) adj=false;
    /* if n, modeling; if y, migration */
    if(! sf_getbool("timer",&timer)) timer=false;
    if (!sf_getbool("verb",&verb)) verb=false;
    if (!sf_getint("snap",&snap)) snap=0;
    /* interval for snapshots */
    if (!sf_getint("pad1",&pad1)) pad1=1;
    /* padding factor on the first axis */

    if (adj) { /* migration */
	data = sf_input("in"); // data here is just a refl file
	image = sf_output("out");
	sf_settype(image,SF_COMPLEX);

	if (!sf_histint(data,"n1",&nz)) sf_error("No n1= in input");
	if (!sf_histfloat(data,"d1",&dz)) sf_error("No d1= in input");
	if (!sf_histint(data,"n2",&nx)) sf_error("No n2= in input");
	if (!sf_histfloat(data,"d2",&dx)) sf_error("No d2= in input");
	if (!sf_histfloat(data,"o2",&ox)) ox=0.; 

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

	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",ox);
	sf_putstring(image,"label2","Distance");
    } else { /* modeling */
	image = sf_input("in");
	data = sf_output("out");
	src = sf_input("src");
	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",&ox)) ox=0.; 	

	if (!sf_getint("nt",&nt)) sf_error("Need nt=");
	/* time samples */
	if (!sf_getfloat("dt",&dt)) sf_error("Need dt=");
	/* time sampling */
	if (!sf_histint(src,"n1",&n2) || n2 != nt) sf_error("nt doesn't match!");

	sf_putint(data,"n1",nz);
	sf_putfloat(data,"d1",dz);
	sf_putfloat(data,"o1",0.);
	sf_putstring(data,"label1","Depth");
	sf_putint(data,"n2",nx);
	sf_putfloat(data,"d2",dx);
	sf_putfloat(data,"o2",ox);
	sf_putstring(data,"label2","Distance");
    }

    nz2 = kiss_fft_next_fast_size(nz*pad1);
    nx2 = kiss_fft_next_fast_size(nx);
    nk = nz2*nx2; /*wavenumber*/

    nzx = nz*nx;
    nzx2 = nz2*nx2;
    ntx = nt*nx;
    wfnt = (int)(nt-1)/snap+1;

    if (snap > 0) {
	snaps = sf_output("snaps");
	/* (optional) snapshot file */
	sf_settype(snaps,SF_COMPLEX);
	sf_putint(snaps,"n1",nz);
	sf_putfloat(snaps,"d1",dz);
	sf_putfloat(snaps,"o1",0.);
	sf_putstring(snaps,"label1","Depth");
	sf_putint(snaps,"n2",nx);
	sf_putfloat(snaps,"d2",dx);
	sf_putfloat(snaps,"o2",ox);
	sf_putstring(snaps,"label2","Distance");
	sf_putint(snaps,"n3",wfnt);
	sf_putfloat(snaps,"d3",dt*snap);
	sf_putfloat(snaps,"o3",0.);
	sf_putstring(snaps,"label3","Time");
    } else {
	snaps = NULL;
    }

    /* 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);
    
    lt = sf_complexalloc2(nzx,m2);
    rt = sf_complexalloc2(m2,nk);
    img = sf_complexalloc2(nz,nx);
    dat = sf_complexalloc2(nz,nx);
    if (snap > 0) wvfld = sf_complexalloc3(nz,nx,wfnt);
    else wvfld = NULL;
    geop = (geopar) sf_alloc(1, sizeof(*geop));
    if (!adj) {
	ww=sf_complexalloc(nt);
	sf_complexread(ww,nt,src);
    } else ww=NULL;
    sf_complexread(lt[0],nzx*m2,left);
    sf_complexread(rt[0],m2*nk,right);
    if (adj) sf_complexread(dat[0],nzx,data);
    else sf_complexread(img[0],nzx,image);

    /*close RSF files*/
    sf_fileclose(left);
    sf_fileclose(right);

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

    if (timer) t0 = gtod_timer();

    /*load constant geopar elements*/
    geop->nx  = nx;
    geop->nz  = nz;
    geop->dx  = dx;
    geop->dz  = dz;
    geop->ox  = ox;
    geop->nt  = nt;
    geop->dt  = dt;
    geop->snap= snap;
    geop->nzx2= nzx2;
    geop->nk  = nk;
    geop->m2  = m2;
    geop->wfnt= wfnt;

    lrexp(img, dat, adj, lt, rt, ww, geop, pad1, verb, snap, wvfld);

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

    if (adj) {
	sf_complexwrite(img[0],nzx,image);
    } else {
	sf_complexwrite(dat[0],ntx,data);
    }
    
    if (snap > 0 && NULL != snaps) {
	sf_complexwrite(wvfld[0][0],wfnt*nx*nz,snaps);
    }

    exit(0);
}
Ejemplo n.º 2
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 */
    bool fm; /* forward modeling */

    /*I/O*/
    sf_file Fvel;
    sf_file left, right, leftb, rightb;
    sf_file Fsrc, Frcd/*source and record*/;
    sf_file Ftmpwf;
    sf_file Fimg;
    sf_file mask;

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

    /*grid index variables*/
    int nx, nz, nt, wfnt;
    int nzx, nx2, nz2, n2, m2, pad1, nk;
    int ix, iz, it, is;
    int nxb, nzb;
    int snpint, wfint;
    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 **kill=NULL;
    int rectz,rectx,repeat; /*smoothing parameters*/
    float trunc;
    int sht0,shtbgn,shtend,shtnum,shtint;

    /*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=true; /*if n, receiver is independent of source location and gpl=nx*/
    if (!sf_getbool("fm", &fm)) fm=false; /* if n, Born modelling  */
    if (!sf_getbool("incom", &incom)) incom=false; /* if n, use complete data */
    /* 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;
    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 */
    if (!sf_getint("wfint", &wfint)) wfint=50;     /* snap interval */
    /*--- parameters of source ---*/
    if (!sf_getfloat("srctrunc", &trunc)) trunc=0.4;
    if (!sf_getint("rectz", &rectz)) rectz=2;
    if (!sf_getint("rectx", &rectx)) rectx=2;
    if (!sf_getint("repeat", &repeat)) repeat=2;
    /* 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;
    /* simultaneous sources parameter */
    if (!sf_getint("nsource", &nsource)) nsource=1;
    if (!sf_getint("dsource", &dsource)) dsource=0;
    if (!sf_getfloat("tdelay", &tdelay)) tdelay=0;
    if (!sf_getint("choose", &choose)) choose=nsource;

    /*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*/
    }
    if (incom) {
        mask=sf_input("mask"); /*mask operator*/
    }

    /*--- 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;
    ndelay=tdelay/dt;

    /* 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);

    /*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 != shtnum ) sf_error("Error parameter n3 in record!");
    }

    /*allocate memory*/
    ww=sf_complexalloc(nt);
    rr=sf_floatalloc2(nzx,nsource);
    lt = sf_complexalloc2(nzx,m2);
    rt = sf_complexalloc2(m2,nk);
    ltb = sf_complexalloc2(nzx,m2);
    rtb = sf_complexalloc2(m2,nk);
    geop = (geopar) sf_alloc(1, sizeof(*geop));
    mpip = (mpipar) sf_alloc(1, sizeof(*mpip));
    tmprec = sf_complexalloc2(nt, gpl);
    record = sf_complexalloc3(nt, gpl, shtnum);
    if (incom) {
        kill = sf_intalloc2(gpl, shtnum);
        sf_intread(kill[0],gpl*shtnum,mask);
    }
    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*m2,leftb);
    sf_complexread(rtb[0],m2*nk,rightb);
    if(!adj) sf_complexread(img[0],nx*nz,Fimg);

    if(adj && wantrecord) {
        sf_complexread(record[0][0], shtnum*gpl*nt, Frcd);
        if(incom) {
            for (is=0; is<shtnum; is++)
                for (ix=0; ix<gpl; ix++)
                    if(kill[is][ix]==0)
                        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,shtnum);
        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-1)/wfint+1);
            sf_setd(at, wfdt*wfint);

            sf_oaxa(Ftmpwf, az, 1);
            sf_oaxa(Ftmpwf, ax, 2);
            sf_oaxa(Ftmpwf, at, 3);
            sf_settype(Ftmpwf,SF_COMPLEX);
        }
    }

    tstart = clock();

    for (is=rank; is<shtnum; is+=nodes) {
        spx = shtbgn + shtint*is;
        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", is);
            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 shtnum=%d", sht0, shtbgn, shtend, 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)
#ifdef _OPENMP
            #pragma omp parallel for private(ix,it)
#endif
            for (ix=0; ix<gpl; ix++)
                for (it=0; it<nt; it++)
                    tmprec[ix][it] = record[is][ix][it];

        if(!fm) {
            lrosback2(img, wavefld, sill, tmprec, adj, verb, wantwf, ltb, rtb, m2, geop, pad1, illum);
        }

        if (adj) {
#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] += img[ix][iz];
        }

        if (!adj || !wantrecord)
#ifdef _OPENMP
            #pragma omp parallel for private(ix,it)
#endif
            for (ix=0; ix<gpl; ix++)
                for (it=0; it<nt; it++)
                    record[is][ix][it] = tmprec[ix][it];

        if (wantwf && is==0)
            for (it=0; it<wfnt; it++) {
                if (it%wfint == 0) {
                    sf_complexwrite(wavefld[it][0], nx*nz, Ftmpwf);
                }
            }
    } /*shot iteration*/

    MPI_Barrier(MPI_COMM_WORLD);
    /*write record/image*/
    if (adj) {
        if (rank==0) {
            sendbuf = (sf_complex *) MPI_IN_PLACE;
            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) {
            sendbuf = (sf_complex *) MPI_IN_PLACE;
            recvbuf = record[0][0];
        } else {
            sendbuf = record[0][0];
            recvbuf = NULL;
        }
        MPI_Reduce(sendbuf, recvbuf, shtnum*gpl*nt, MPI_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD);
        if (rank==0) {
            if(incom) {
                for (is=0; is<shtnum; is++)
                    for (ix=0; ix<gpl; ix++)
                        if(kill[is][ix]==0)
                            for (it=0; it<nt; it++)
                                record[is][ix][it]=sf_cmplx(0.,0.);
            }
            sf_complexwrite(record[0][0], shtnum*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);
    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);
    }
    if (incom) {
        free(* kill);
        free(kill);
    }

    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);
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
    int verbose;
    sf_file in=NULL, out=NULL;
    int n1_traces;
    int n1_headers;

    char* header_format=NULL;
    sf_datatype typehead;
    /* kls do I need to add this?  sf_datatype typein; */
    float* fheader=NULL;
    float* intrace=NULL;
    float* outtrace=NULL;
    float* bandlimittrace=NULL;
    float* rmsall=NULL;
    float* rmsband=NULL;
    float* scalars=NULL;
    float* sum_of_scalars=NULL;
    int indx_time;
    int itrace=0;
    int ntaper;
    int numxstart;
    int numtstart;
    float* taper;
    char **list_of_floats;
    float* xstart;
    float* tstart;
    int indx_of_offset;
    float offset;
    float d1;
    float o1;
    float time_start;
    int itime_start;
    int itime_stop;
    int indx_taper;
    float wagc;
    int lenagc;

    float fmin=5;
    float fmax=95;
    float finc=5;
    int num_centerfreq;
    int firstlive;
    int lastlive;
    float pnoise;

    kiss_fftr_cfg cfg,icfg;
    sf_complex *fft1;
    sf_complex *fft2;
    int nfft;
    int nf;
    float df;
    float scale;
    int ifreq;
    float cntrfreq;
    
    /*****************************/
    /* initialize verbose switch */
    /*****************************/
    sf_init (argc,argv);

    if(!sf_getint("verbose",&verbose))verbose=1;
    /* \n
       flag to control amount of print
       0 terse, 1 informative, 2 chatty, 3 debug
    */
    sf_warning("verbose=%d",verbose);
 
    /******************************************/
    /* input and output data are stdin/stdout */
    /******************************************/

    if(verbose>0)fprintf(stderr,"read in file name\n");  
    in = sf_input ("in");

    if(verbose>0)fprintf(stderr,"read out file name\n");
    out = sf_output ("out");

    if (!sf_histint(in,"n1_traces",&n1_traces))
	sf_error("input data not define n1_traces");
    if (!sf_histint(in,"n1_headers",&n1_headers)) 
	sf_error("input data does not define n1_headers");

    header_format=sf_histstring(in,"header_format");
    if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
    else                                       typehead=SF_FLOAT;

    if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
    fheader = sf_floatalloc(n1_headers);
 
    /*  allocate intrace and outtrace after initialize fft and 
	make them padded */
  
    /* maybe I should add some validation that n1== n1_traces+n1_headers+2
       and the record length read in the second word is consistent with 
       n1.  */

    /**********************************************************/
    /* end code block for standard tah Trace And Header setup */
    /* continue with any sf_puthist this tah program calls to */
    /* add to the history file                                */
    /**********************************************************/

    /* put the history from the input file to the output */
    sf_fileflush(out,in);

    /********************************************************/
    /* continue initialization specific to this tah program */
    /********************************************************/





    /* segy_init gets the list header keys required by segykey function  */
    segy_init(n1_headers,in);

    if(0==1) {  
      /* infuture may want to have offset dependent agc design start time */
      /* get the mute parameters */
      if(NULL==(list_of_floats=sf_getnstring("xstart",&numxstart))){
	xstart=NULL;
	sf_error("xstart is a required parameter in sftahagc");
      } else {
	xstart=sf_floatalloc(numxstart);
	if(!sf_getfloats("xstart",xstart,numxstart))sf_error("unable to read xstart");
      }
      if(NULL==(list_of_floats=sf_getnstring("tstart",&numtstart))){
	tstart=NULL;
	sf_error("xstart is a required parameter in sftahagc");
      } else {
	tstart=sf_floatalloc(numtstart);
	if(!sf_getfloats("tstart",tstart,numtstart))sf_error("unable to read tstart");
      }
      if(numxstart!=numtstart)sf_error("bad mute parameters: numxstart!=numtstart");
      if(!sf_getint("ntaper",&ntaper))ntaper=12;
    }
    /* \n
       length of the taper on the stack mute
    */
    /* is this of use for agc?
    taper=sf_floatalloc(ntaper);
    for(indx_time=0; indx_time<ntaper; indx_time++){
	float val_sin=sin((indx_time+1)*SF_PI/(2*ntaper));
	taper[indx_time]=val_sin*val_sin;
    }
    */
    indx_of_offset=segykey("offset");
    
    if (!sf_histfloat(in,"d1",&d1))
      sf_error("input data does not define d1");
    if (!sf_histfloat(in,"o1",&o1))
      sf_error("input data does not define o1");

    /* check wagc for reasonableness.  I input 250 (not .25 and ,250) */  
    if(!sf_getfloat("wagc",&wagc))wagc=-1;
    /* \n
       length of the agc window in seconds
    */
    if(wagc<0)sf_error("wagc is a required parameter in sftahagc");

    lenagc=wagc/d1+1.5; /* length of the agc window in samples */
 
    if (!sf_getfloat("pnoise",  &pnoise)) pnoise = 0.01;
    /* relative additive noise level */
    
    if (!sf_getfloat("fmin",&fmin))fmin=5;
    /* minimum frequency band */
    if (!sf_getfloat("fmax",&fmax))fmax=95;
    /* maximum frequency band */
    if (!sf_getfloat("finc",&finc))finc=5;
    /* frequency band increment */

    /* initialize kiss_fft kls */

    nfft  = kiss_fft_next_fast_size(n1_traces+200);
    nf    = nfft/2+1;
    df    = 1./(nfft*d1);
    scale = 1./nfft;
    cfg   = kiss_fftr_alloc(nfft,0,NULL,NULL);
    icfg  = kiss_fftr_alloc(nfft,1,NULL,NULL);
    fft1  = sf_complexalloc(nf);
    fft2  = sf_complexalloc(nf);
    if(verbose>0)fprintf(stderr,"fmax=%f\n",(nf-1)*df);

    /* allocate input and output traces with enough padding for
       ffts */
    if(verbose>0)
      fprintf(stderr,"allocate padded intrace.  n1_traces=%d nfft=%d\n",
	                                        n1_traces,   nfft);
    intrace       =sf_floatalloc(nfft); /* must be padded for input to fft */
    bandlimittrace=sf_floatalloc(nfft); /* must be padded for input to fft */
    outtrace= sf_floatalloc(n1_traces);
    rmsall        =sf_floatalloc(n1_traces);
    rmsband        =sf_floatalloc(n1_traces);
    scalars       =sf_floatalloc(n1_traces);
    sum_of_scalars=sf_floatalloc(n1_traces);
    num_centerfreq=(fmax-fmin)/finc+1.95;
    if(fabs(fmax-(fmin+(num_centerfreq-1)*finc))>.01){
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"fmin-fmax is not a multiple of finc\n");
      fprintf(stderr,"fmin=%f, fmax=%f, finc=%f\n",fmin,fmax,finc);
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"*************************************\n");
      sf_error("illegal combination of fmin,fmax,fminc");
    }

    /***************************/
    /* start trace loop        */
    /***************************/
    if(verbose>0)fprintf(stderr,"start trace loop\n");
 
    itrace=0;
    while (!(get_tah(intrace, fheader, n1_traces, n1_headers, in))){
	if(verbose>1 || (verbose==1 && itrace<5)){
	    fprintf(stderr,"process tah %d in sftahagc\n",itrace);
	}
	/********************/
	/* process the tah. */
	/********************/

	if(typehead == SF_INT)offset=((int  *)fheader)[indx_of_offset];
	else                  offset=((float*)fheader)[indx_of_offset];
	/* maybe latter add agc design start time
	intlin(numxstart,xstart,tstart,
	       tstart[0],tstart[numxstart-1],1,
	       &offset,&time_start);
	if(time_start<o1)time_start=o1;

	itime_start=(int)(((time_start-o1)/d1)+.5);
	*/
	/* find firstlive and lastlive */
        if(verbose>2)fprintf(stderr,"find firstlive and lastlive\n");
	for (firstlive=0; firstlive<n1_traces; firstlive++){
	  if(intrace[firstlive] != 0) break;
	}
	for (lastlive=n1_traces-1;lastlive>=0; lastlive--){
	  if(intrace[lastlive] != 0) break;
	}
	/* kls need to catch a zero trace */
        if(verbose>2)
	  fprintf(stderr,"firstlive=%d, lastlive=%d\n",firstlive,lastlive);
	/* zero the padded area on the input trace */
	for (indx_time=n1_traces; indx_time<nfft; indx_time++){
	  intrace[indx_time]=0;
	}
	
	/********************************************************/
	/* apply the SPECtral BALancing:                        */
	/* 1 compute the input trace rms in agclen gates        */
	/* 2 fft to frequency domain                            */
	/* 3 for each frequency band                            */
	/*   3.1 extract (ramp) the frequency band              */
	/*   3.2 convert freqency band to time,                 */
	/*   3.3 compute the rms of the frequency band          */
	/*   3.4 agc scalars 1/(freq_band_rms + whitenoise*rms) */ 
	/*   3.3 agc the frequency band                         */
	/*   3.4 sum to output                                  */
        /*   3.5 sum scalars to sum of scalars                  */
	/* 4 divide by the sum of scalars                       */
	/********************************************************/
	compute_rms(intrace, rmsall,
		    lenagc, n1_traces,
                    firstlive, lastlive);
	/* scale rms by whitenoise factor */
	if(verbose>2)fprintf(stderr,"put_tah rmsall\n");
	if(0) put_tah(intrace, fheader, n1_traces, n1_headers, out);
        if(0) put_tah(rmsall, fheader, n1_traces, n1_headers, out);
	for (indx_time=0; indx_time<n1_traces; indx_time++){
	  rmsall[indx_time]*=(1.0/num_centerfreq);
	}

	if(verbose>2)fprintf(stderr,"fftr\n");
	kiss_fftr(cfg, intrace, (kiss_fft_cpx*) fft1);

	/* zero the output trace and the sum_of_scalars */
	for (indx_time=0; indx_time<n1_traces; indx_time++){
	  outtrace[indx_time]=0.0;
	  sum_of_scalars[indx_time]=0.0;
	}
	for (cntrfreq=fmin; cntrfreq<=fmax; cntrfreq+=finc){
	  if(verbose>2)fprintf(stderr,"cntrfreq=%f\n",cntrfreq);
          /* zero frequencies before the band */
	  for (ifreq=0; ifreq<(int)((cntrfreq-finc)/df); ifreq++){
	    fft2[ifreq]=0.0;
	  }
	  /* triangular weight the selected  frequency band */
	  for (ifreq=(int)((cntrfreq-finc)/df); 
	       ifreq<(int)((cntrfreq+finc)/df) && ifreq<nf;
	       ifreq++){
            float weight;
	    if(ifreq>0){
	      weight=(1.0-fabs(ifreq*df-cntrfreq)/finc)/nfft;
	      fft2[ifreq]=weight*fft1[ifreq];
	    }
	  }
          /* zero frequencies larger than the band */
	  for (ifreq=(int)((cntrfreq+finc)/df); ifreq<nf; ifreq++){
	    fft2[ifreq]=0.0;
	  } 
	  /* inverse fft back to time domain */
          if(verbose>2)fprintf(stderr,"fftri\n");
	  kiss_fftri(icfg,(kiss_fft_cpx*) fft2, bandlimittrace);
          /* apply agc to the bandlimittrace and sum scalars to 
             sum_of_scalars */
	  if(verbose>2)fprintf(stderr,"agc_apply\n");
	  compute_rms(bandlimittrace, rmsband,
		    lenagc, n1_traces,
                    firstlive, lastlive);
	  if(verbose>2)fprintf(stderr,"sum to ouput\n");
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    /* old poor 
	       scalars[indx_time]=1.0/
                   (rmsband[indx_time]+pnoise*rmsall[indx_time]);
	    */
	    if(1)scalars[indx_time]=rmsband[indx_time]/
                     (rmsband[indx_time]*rmsband[indx_time]+
		      pnoise*rmsall[indx_time]*rmsall[indx_time]);
	    else scalars[indx_time]=1.0;
	  }
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    bandlimittrace[indx_time]*=scalars[indx_time];
	    outtrace[indx_time]+=bandlimittrace[indx_time];
	  }
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    sum_of_scalars[indx_time]+=scalars[indx_time];
	  }
          if(0)
	    put_tah(bandlimittrace, fheader, n1_traces, n1_headers, out);
	  if(0)
	    put_tah(rmsband, fheader, n1_traces, n1_headers, out);
	  if(0)
	    put_tah(scalars, fheader, n1_traces, n1_headers, out);
	}
        if(0)put_tah(sum_of_scalars, fheader, n1_traces, n1_headers, out);
        if(1){	
	   /* divide output by sum of scalars */
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    outtrace[indx_time]/=sum_of_scalars[indx_time];
	  }
	} 
	if (1)
	  put_tah(outtrace, fheader, n1_traces, n1_headers, out);
	itrace++;
    }

    exit(0);
}
Ejemplo n.º 4
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 */,
               int *n_local, int *o_local /* local size & start */)
/*< initialize >*/
{
  int i, nth=1;
  int cpuid;
  ptrdiff_t n[2];
  
  MPI_Comm_rank(MPI_COMM_WORLD, &cpuid);

  fftwf_mpi_init();

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

  n[0]=n2; n[1]=n1;
  //alloc_local = fftwf_mpi_local_size_many_transposed(2, n, 2, FFTW_MPI_DEFAULT_BLOCK, FFTW_MPI_DEFAULT_BLOCK, MPI_COMM_WORLD, &local_n0, &local_0_start, &local_n1, &local_1_start);
  alloc_local = fftwf_mpi_local_size_2d_transposed(n2, n1, MPI_COMM_WORLD, &local_n0, &local_0_start, &local_n1, &local_1_start);

  cc = sf_complexalloc2(n1,local_n0);
  //cc = sf_complexalloc(alloc_local);

  /* 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));

  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);
  }

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

  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(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(n1,n2,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;
  *n_local = (int) local_n0;
  *o_local = (int) local_0_start;
	
  wt =  1.0/(n1*n2);
	
  return (nk*n2);
}
Ejemplo n.º 5
0
int main(int argc,char ** argv)
{
    int k;
    int nfft[32];
    int ndims = 1;
    int isinverse=0;
    int numffts=1000,i;
    kiss_fft_cpx * buf;
    kiss_fft_cpx * bufout;
    kiss_fft_cpx * invbufout;
    int real = 0;

    nfft[0] = 1024;// default

    while (1) {
        int c = getopt (argc, argv, "n:ix:r");
        if (c == -1)
            break;
        switch (c) {
            case 'r':
                real = 1;
                break;
            case 'n':
                ndims = getdims(nfft, optarg );
                if (nfft[0] != kiss_fft_next_fast_size(nfft[0]) ) {
                    int ng = kiss_fft_next_fast_size(nfft[0]);
                    fprintf(stderr,"warning: %d might be a better choice for speed than %d\n",ng,nfft[0]);
                }
                break;
            case 'x':
                numffts = atoi (optarg);
                break;
            case 'i':
                isinverse = 1;
                break;
        }
    }
    int nbytes = sizeof(kiss_fft_cpx);
    for (k=0;k<ndims;++k)
        nbytes *= nfft[k];

#ifdef USE_SIMD        
    numffts /= 4;
    fprintf(stderr,"since SIMD implementation does 4 ffts at a time, numffts is being reduced to %d\n",numffts);
#endif

    buf=(kiss_fft_cpx*)KISS_FFT_MALLOC(nbytes);
    bufout=(kiss_fft_cpx*)KISS_FFT_MALLOC(nbytes);
    invbufout = (kiss_fft_cpx*)KISS_FFT_MALLOC(nbytes);
    memset(buf,0,nbytes);
    if (2 == ndims){
        int i, j;
        for (i = 0; i < nfft[0]; i++){
            int row = i * nfft[1];
            for (j = 0; j < nfft[1]; j++){
                int idx = row + j;
                buf[idx].r = idx;
                buf[idx].i = 0;
            }
        }
    }
    pstats_init();

    if (ndims==1) {
        if (real) {
            kiss_fftr_cfg st = kiss_fftr_alloc( nfft[0] ,isinverse ,0,0);
            if (isinverse)
                for (i=0;i<numffts;++i)
                    kiss_fftri( st ,(kiss_fft_cpx*)buf,(kiss_fft_scalar*)bufout );
            else
                for (i=0;i<numffts;++i)
                    kiss_fftr( st ,(kiss_fft_scalar*)buf,(kiss_fft_cpx*)bufout );
            free(st);
        }else{
            kiss_fft_cfg st = kiss_fft_alloc( nfft[0] ,isinverse ,0,0);
            for (i=0;i<numffts;++i)
                kiss_fft( st ,buf,bufout );
            free(st);
        }
    }else{
        if (real) {
            kiss_fftndr_cfg st = kiss_fftndr_alloc( nfft,ndims ,isinverse ,0,0);
            if (isinverse)
                for (i=0;i<numffts;++i)
                    kiss_fftndri( st ,(kiss_fft_cpx*)buf,(kiss_fft_scalar*)bufout );
            else
                for (i=0;i<numffts;++i)
                    kiss_fftndr( st ,(kiss_fft_scalar*)buf,(kiss_fft_cpx*)bufout );
            free(st);
        }else{
            kiss_fftnd_cfg st= kiss_fftnd_alloc(nfft,ndims,isinverse ,0,0);
            kiss_fftnd( st ,buf,bufout );
            kiss_fftnd_cfg ist = kiss_fftnd_alloc(nfft, ndims, 1, 0, 0);
            kiss_fftnd(ist, bufout, invbufout);
            if (2 == ndims){
                int i, j;
                int val = nfft[0]*nfft[1];
                for (i = 0; i < nfft[0]; i++){
                    int row = i * nfft[1];
                    for (j = 0; j < nfft[1]; j++){
                        int idx = row + j;
                        invbufout[idx].r /= val;
                        invbufout[idx].i /= val;
                    }
                }
            }

            free(st);
            free(ist);
        }
    }

    free(buf); free(bufout); free(invbufout);

    fprintf(stderr,"KISS\tnfft=");
    for (k=0;k<ndims;++k)
        fprintf(stderr, "%d,",nfft[k]);
    fprintf(stderr,"\tnumffts=%d\n" ,numffts);
    pstats_report();

    kiss_fft_cleanup();

    return 0;
}
Ejemplo n.º 6
0
int main(int argc, char* argv[])
{
    bool adj,timer,verb,gmres;
    int nt, nx, nz, nx2, nz2, nzx, nzx2, ntx, pad1, snap, gpz, wfnt, i;
    int m2, n2, nk, nth=1;
    int niter, mem;
    float dt, dx, dz, ox;
    sf_complex *img, *imgout, *dat, **lt1, **rt1, **lt2, **rt2, ***wvfld;
    sf_file data, image, leftf, rightf, leftb, rightb, snaps;
    double time=0.,t0=0.,t1=0.;
    geopar geop;

    sf_init(argc,argv);

    /* essentially doing imaging */
    adj = true;

    if (!sf_getbool("gmres",&gmres)) gmres=false;
    if (gmres) {
      if (!sf_getint("niter",&niter)) niter=10;
      if (!sf_getint("mem",&mem)) mem=20;
    }

    if(! sf_getbool("timer",&timer)) timer=false;
    if (!sf_getbool("verb",&verb)) verb=false;
    if (!sf_getint("snap",&snap)) snap=0;
    /* interval for snapshots */
    if (!sf_getint("pad1",&pad1)) pad1=1;
    /* padding factor on the first axis */
    if(!sf_getint("gpz",&gpz)) gpz=0;
    /* geophone surface */

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

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

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

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

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

	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",ox);
	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",&ox)) ox=0.; 	

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

	sf_putint(data,"n1",nt);
	sf_putfloat(data,"d1",dt);
	sf_putfloat(data,"o1",0.);
	sf_putstring(data,"label1","Time");
	sf_putstring(data,"unit1","s");

	sf_putint(data,"n2",nx);
	sf_putfloat(data,"d2",dx);
	sf_putfloat(data,"o2",ox);
	sf_putstring(data,"label2","Distance");
    }

    nz2 = kiss_fft_next_fast_size(nz*pad1);
    nx2 = kiss_fft_next_fast_size(nx);
    nk = nz2*nx2; /*wavenumber*/

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

    if (snap > 0) {
        wfnt = (int)(nt-1)/snap+1;
	snaps = sf_output("snaps");
	/* (optional) snapshot file */
	sf_settype(snaps,SF_COMPLEX);
	sf_putint(snaps,"n1",nz);
	sf_putfloat(snaps,"d1",dz);
	sf_putfloat(snaps,"o1",0.);
	sf_putstring(snaps,"label1","Depth");
	sf_putint(snaps,"n2",nx);
	sf_putfloat(snaps,"d2",dx);
	sf_putfloat(snaps,"o2",ox);
	sf_putstring(snaps,"label2","Distance");
	sf_putint(snaps,"n3",wfnt);
	sf_putfloat(snaps,"d3",dt*snap);
	sf_putfloat(snaps,"o3",0.);
	sf_putstring(snaps,"label3","Time");
    } else {
        wfnt = 0;
	snaps = NULL;
    }

    /* propagator matrices */
    leftf = sf_input("leftf");
    rightf = sf_input("rightf");
    
    if (!sf_histint(leftf,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in leftf",nzx);
    if (!sf_histint(leftf,"n2",&m2))  sf_error("No n2= in leftf");
    if (!sf_histint(rightf,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in rightf",m2);
    if (!sf_histint(rightf,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in rightf",nk);

    leftb = sf_input("leftb");
    rightb = sf_input("rightb");
    
    if (!sf_histint(leftb,"n1",&n2) || n2 != nzx) sf_error("Need n1=%d in leftb",nzx);
    if (!sf_histint(leftb,"n2",&m2))  sf_error("No n2= in leftb");
    if (!sf_histint(rightb,"n1",&n2) || n2 != m2) sf_error("Need n1=%d in rightb",m2);
    if (!sf_histint(rightb,"n2",&n2) || n2 != nk) sf_error("Need n2=%d in rightb",nk);
    
    lt1 = sf_complexalloc2(nzx,m2); /* propagator for forward modeling */
    rt1 = sf_complexalloc2(m2,nk);
    lt2 = sf_complexalloc2(nzx,m2); /* propagator for backward imaging */
    rt2 = sf_complexalloc2(m2,nk);
    img = sf_complexalloc(nz*nx);
    dat = sf_complexalloc(nt*nx);
    imgout = sf_complexalloc(nz*nx);
    if (snap > 0) wvfld = sf_complexalloc3(nz,nx,wfnt);
    else wvfld = NULL;
    geop = (geopar) sf_alloc(1, sizeof(*geop));

    sf_complexread(lt1[0],nzx*m2,leftf);
    sf_complexread(rt1[0],m2*nk,rightf);
    sf_complexread(lt2[0],nzx*m2,leftb);
    sf_complexread(rt2[0],m2*nk,rightb);
    if (adj) sf_complexread(dat,ntx,data);
    else sf_complexread(img,nzx,image);

    /*close RSF files*/
    sf_fileclose(leftf);
    sf_fileclose(rightf);
    sf_fileclose(leftb);
    sf_fileclose(rightb);

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

    if (timer) t0 = gtod_timer();

    /*load constant geopar elements*/
    geop->nx  = nx;
    geop->nz  = nz;
    geop->dx  = dx;
    geop->dz  = dz;
    geop->ox  = ox;
    geop->gpz = gpz;
    geop->nt  = nt;
    geop->dt  = dt;
    geop->snap= snap;
    geop->nzx2= nzx2;
    geop->nk  = nk;
    geop->m2  = m2;
    geop->wfnt= wfnt;
    geop->pad1= pad1;
    geop->verb= verb;

    /* first get the Q-compensated image: B[d] */
    lrexp(img, dat, adj, lt2, rt2, geop, wvfld);

    /* performing the least-squares optimization: ||{BF}[m] - B[d]|| */
    if (gmres) {
      /* disabling snapshots */
      geop->snap= 0;
      lrexp_init(lt1,rt1,lt2,rt2);

      sf_warning(">>>>>> Using GMRES(m) <<<<<<");
      cgmres_init(nzx,mem);
      cgmres( img, imgout, lrexp_op, geop, niter, 0.01*SF_EPS, true);
      /*lrexp_op(nzx, img, imgout, geop);*/
      lrexp_close();
    } else {
      for (i=0; i<nzx; i++)
	imgout[i] = img[i];
    }

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

    if (adj) {
	sf_complexwrite(imgout,nzx,image);
    } else {
	sf_complexwrite(dat,ntx,data);
    }
    
    if (snap > 0 && NULL != snaps) {
	sf_complexwrite(wvfld[0][0],wfnt*nx*nz,snaps);
    }

    exit(0);
}
Ejemplo n.º 7
0
int main(int argc, char* argv[])
{
    map4 str;
    bool verb;
    int i1,i2, n1,n2,n3, nw, nx,ny,nv, ix,iy,iv;
    float d1,o1,d2,o2, eps, w,x,y, v0,v2,v,dv, dx,dy, t, x0,y0, 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");
    if (!sf_histint(in,"n3",&ny)) sf_error("No n3= in input");

    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.;

    if(!sf_histfloat(in,"d3",&dy)) sf_error("No d3= in input");
    if(!sf_histfloat(in,"o3",&y0)) y0=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;

    dy *= 2.*SF_PI;
    y0 *= 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++) {
	if (verb) sf_warning("wavenumber %d of %d;", iy+1,ny);

	y = y0+iy*dy; 
	y *= y * 0.5;

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

	    x += y;

	    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 */
    } /* y */
    if (verb) sf_warning(".");

    exit (0);
}
Ejemplo n.º 8
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);
}
Ejemplo n.º 9
0
int main (int argc, char *argv[])
{
    bool verb;
    int nt, nt2;	/* number of time samples */
    int nz;		/* number of migrated time samples */
    int nx, ny;		/* number of wavenumbers */
    int it,ix,iy,iz;          /* loop counters 	*/
    
    float dt;		/* time sampling interval 	*/
    float dz;		/* migrated time sampling interval */
    float dx,dy;	/* wave number sampling interval */
    float x,y;          /* wave number */
    float x0,y0;        /* wave number origin */
    float *vt, v0;	/* velocity v(t)		*/
    float *vz, vz0;	/* vertical velocity v(t)		*/
    float *n, n0;	/* eta		*/

    float *p,*q;	/* input, output data		*/

    bool inv;           /* modeling or migration        */
    bool depth;         /* time or depth migration      */
    float eps;          /* dip filter constant          */   

    char *rule;         /* phase-shuft interpolation rule */

    sf_file in, out, vel, velz, eta;

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

    if (!sf_getbool("inv",&inv)) inv = false;
    /* if y, modeling; if n, migration */
    if (!sf_getfloat("eps",&eps)) eps = 0.01;
    /* stabilization parameter */
    if (!sf_getbool("verb",&verb)) verb = false;
    /* verbosity flag */

    if (!sf_getbool("depth",&depth)) depth = false;
    /* if true, depth migration */

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

    if (!sf_histint(in,"n3",&ny)) ny = 1;
    if (!sf_histfloat(in,"d3",&dy)) dy=dx;
    if (!sf_histfloat(in,"o3",&y0)) y0=0.;

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

    if (NULL == sf_getstring("velocity")) {
	vel = NULL;
	velz = NULL;
	eta = NULL;
    } else {
	vel = sf_input("velocity");
	if (!sf_histint(vel,"n1",&nz)) 
	    sf_error ("No n1= in velocity");
	if (!sf_histfloat(vel,"d1",&dz)) 
	    sf_error ("No d1= in velocity");
	
	if (NULL == sf_getstring("velz")) {
	    velz = NULL;
	    eta = NULL;
	} else {
	    velz = sf_input("velz");
	    if (NULL == sf_getstring("eta")) sf_error("Need eta=");
	    eta = sf_input("eta");
	}
    }

    if (inv) { /* modeling */
	if (!sf_histint(in,"n1",&nz)) sf_error ("No n1= in input");
	if (!sf_histfloat(in,"d1",&dz)) sf_error ("No d1= in input");

	if (!sf_getint("nt",&nt)) {
	    /* Length of time axis (for modeling) */
	    if (depth) {
		sf_error ("nt= must be supplied");
	    } else {
		nt=nz;
	    }
	} else {
	    sf_putint(out,"n1",nt);
	}

	if (!sf_getfloat("dt",&dt)) {
	    /* Sampling of time axis (for modeling) */
	    if (depth) {
		sf_error ("dt= must be supplied");
	    } else {
		dt=dz;
	    }
	} else {
	    sf_putfloat(out,"d1",dt);
	}

	sf_putfloat(out,"o1",0.);
    } else { /* migration */
	if (!sf_histint(in,"n1",&nt)) sf_error ("No n1= in input");
	if (!sf_histfloat(in,"d1",&dt)) sf_error ("No d1= in input");

	if (NULL == vel) {
	    if (!sf_getint("nz",&nz)) {
		/* Length of depth axis (for migration, if no velocity file) */
		if (depth) {
		    sf_error("Need nz=");
		} else {
		    nz = nt;
		}
	    }
	    if (!sf_getfloat("dz",&dz)) {
		/* Sampling of depth axis (for migration, if no velocity file) */
		if (depth) {
		    sf_error("Need dz=");
		} else {
		    dz = dt;
		}
	    }
	}

	sf_putint(out,"n1",nz);
	sf_putfloat(out,"d1",dz);
	sf_putfloat(out,"o1",0.);
    }
    
    vt = sf_floatalloc(nz);
    vz = sf_floatalloc(nz);
    n = sf_floatalloc(nz);

    if (NULL == (rule = sf_getstring("rule"))) rule="simple";
    /* phase-shift interpolation rule (simple, midpoint, linear) */

    if (NULL == vel) {
	/* file with velocity */
	if (!sf_getfloat("vel",&v0)) sf_error ("vel= must be supplied");
	/* Constant velocity (if no velocity file) */

	if (!sf_getfloat("vz",&vz0)) vz0=v0;
	/* Constant vertical velocity (if no velocity file) */

	if (!sf_getfloat("n",&n0)) n0=0.0;
	/* Constant eta (if no velocity file) */	

	for (iz=0; iz < nz; iz++) {
	    vt[iz] = v0;
	    vz[iz] = vz0;
	    n[iz] = n0;
	}
    } else {
	sf_floatread(vt,nz,vel);
	sf_fileclose(vel);

	if ('a' == rule[0]) {
	    if (NULL == velz || NULL == eta) sf_error("Need velz= and eta=");
	    sf_floatread(vz,nz,velz);
	    sf_floatread(n,nz,eta);

	    sf_fileclose(velz);
	    sf_fileclose(eta);
	}  else {
	    for (iz=0; iz < nz; iz++) {
		vz[iz] = vt[iz];
		n[iz] = 0.0;
	    }
	}	
    }

    /* vt -> 1/4 vt^2 */ 
    for (iz=0; iz < nz; iz++) {
	vt[iz] *= 0.25*vt[iz];
	vz[iz] *= 0.25*vz[iz];

	if (depth) {
	    vt[iz] = 1./vt[iz];
	    vz[iz] = 1./vz[iz];
	}
    }

    /* determine frequency sampling */    
    if (!sf_getint("pad",&nt2)) nt2 = 2*kiss_fft_next_fast_size((nt+1)/2);

    p = sf_floatalloc(nt2);
    q = sf_floatalloc(nz);

    gazdag_init (eps, nt2, dt, nz, dz, vt, vz, n, depth, rule[0]);

    for (iy=0; iy < ny; iy++) {
	y = y0+iy*dy;
	y *= y;

	for (ix=0; ix < nx; ix++) {
	    x = x0+ix*dx;
	    x = x*x+y;
	
	    if (verb) sf_warning("wavenumber %d of %d;",iy*nx+ix+1,nx*ny);
	
	    if (inv) {
		sf_floatread(q,nz,in);
	    } else {
		sf_floatread(p,nt,in);
		if (nt != nt2) {
		    for (it=nt; it < nt2; it++) {
			p[it]=0.;
		    }
		}
	    }

	    gazdag(inv,x,p,q);

	    if (inv) {
		sf_floatwrite(p,nt,out);
	    } else {
		sf_floatwrite(q,nz,out);
	    }
	}
    } 
    if (verb) sf_warning(".");

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

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

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

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

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

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

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

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

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

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

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

    if (mig) { /* time-reversal propagation */
	/* step backward in time */
	it1 = nt-1;
	it2 = -1;
	its = -1;	
    } else { /* modeling */
	/* step forward in time */
	it1 = 0;
	it2 = nt;
	its = +1;
    }

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

	if (mig) {
	  /* 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!=dat_v) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(iz)
#endif
	    for (iz = 0; iz < gpl_v; iz++) {
	      curr[gpz_v+iz+(gpx_v)*nz2] += dat_v[iz][it];
	    }
	  }
	} else {
	  t = it*dt;
	  for (i=0; i<n_srcs; i++) {
	    for(ix=-1;ix<=1;ix++) {
	      for(iz=-1;iz<=1;iz++) {
		j = spz[i]+iz+nz2*(spx[i]+ix);
		if (src==0) {
		  curr[j] += rick[i][it]/(abs(ix)+abs(iz)+1);
		} else {
		  curr[j] += Ricker(t, f0[i], t0[i], A[i])/(abs(ix)+abs(iz)+1);
		}
	      }
	    }
	  }
	}
	
	/* 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);
	}

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

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

    if (mig) {
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(ix,iz)
#endif
      for (ix = 0; ix < nx1; ix++) {
	for (iz = 0; iz < nz1; iz++) {
	  img[iz + nz1*ix] = curr[iz+nbt + (ix+nbl)*nz2];
	}
      }
    }

    /*free up memory*/
    fft2_finalize();
    if (abc) abc_close();
    free(vv);
    free(lapl);   
    free(wave);
    free(curr);
    free(prev);
    free(cwave);
    free(cwavem);
    
    return 0;
}
Ejemplo n.º 11
0
int main (int argc, char* argv[]) {
    int i, j, n, nn, nt;
    float *trace;
    sf_file data, out;

    bool absoff, filter, kmah, diff, verb;

    sf_init (argc, argv);

    data = sf_input ("in");
    /* Common-shot 2-D data */
    out = sf_output ("out");

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

    if (!sf_getbool ("absoff", &absoff)) absoff = false;
    /* y - absolute offset (default - relative to shot axis) */
    if (!sf_getbool ("filter", &filter)) filter = true;
    /* y - antialiasing filter for data */
    if (!sf_getbool ("KMAH", &kmah)) kmah = true;
    /* y - account for phase shifts due to KMAH index */
    if (!sf_getbool ("diff", &diff)) diff = true;
    /* y - apply half-order differentiation */

    /* Data dimensions */
    if (!sf_histint (data, "n1", &nt)) sf_error ("No n1= in data");
    n = sf_leftsize (data, 1);

    /* Next suitable size for the differentiator */
    nn = 2*kiss_fft_next_fast_size ((nt + 1)/2);
    trace = sf_floatalloc (nn*2);

    if (kmah) { /* Make room for phase-shifted traces */
        sf_shiftdim (data, out, 2);
        sf_putint (out, "n2", 2);
        sf_putfloat (out, "o2", 0.0);
        sf_putfloat (out, "d2", 1.0);
        sf_putstring (out, "label1", "");
        sf_putstring (out, "unit1", "");
    }
    sf_putstring (out, "absoff", absoff ? "y" : "n");
    sf_putstring (out, "filter", filter ? "y" : "n");
    sf_putstring (out, "KMAH", kmah ? "y" : "n");

    /* Half-order differentiation object */
    if (diff)
        sf_halfint_init (true, nn, 1.-1./nt);

    for (i = 0; i < n; i++) { /* Loop over traces */
        if (verb && !(i % 10000LU))
            sf_warning ("Processing trace %lu of %lu (%g %%);",
                        i + 1LU, n, 100.0*(float)i/(float)n);
        sf_floatread (trace, nt, data);
        for (j = nt; j < nn; j++) {
            trace[j] = 0.;
        }
        /* Differentiate */
        if (diff)
            sf_halfint (true, trace);
        /* pi/2 phase shift */
        if (kmah)
            sf_cram_trace_hilbert (nt, trace, &trace[nn]);
        /* Causal and anti-causal integration for anti-aliasing filter */
        if (filter) {
            sf_cram_trace_cint (trace, nt);
            sf_cram_trace_acint (trace, nt);
            if (kmah) {
                sf_cram_trace_cint (&trace[nn], nt);
                sf_cram_trace_acint (&trace[nn], nt);
            }
        }
        sf_floatwrite (trace, nt, out);
        if (kmah)
            sf_floatwrite (&trace[nn], nt, out);
    }
    if (verb)
        sf_warning (".");

    free (trace);

    return 0;
}
Ejemplo n.º 12
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);
}
Ejemplo n.º 13
0
int main(int argc, char* argv[]) 
{
    clock_t tstart,tend;
    double duration;

    /*flag*/
    bool verb;
    bool wantwf;
    bool wantrecord; // actually means "need record"
    
    /*I/O*/
    sf_file Fvel;
    sf_file left, right, leftb, rightb;
    sf_file Fsrc,/*wave field*/ Frcd/*record*/;
    sf_file Ftmpwf, Ftmpbwf;
    sf_file Fimg1, Fimg2;

    sf_axis at, ax, az;

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

    /*source/geophone location*/
    float slx, slz;
    int   spx, spz;
    float gdep;
    int   gp;

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

    /*Data*/
    float ***wavefld;
    sf_complex **record;
    float **img1, **img2;
    int snpint;

    /*source*/
    bool srcdecay;
    int srcrange;
    float srctrunc;
    
    /*abc boundary*/
    int top,bot,lft,rht;

    /*memoray*/
//    float memneed;
    int tmpint;

    tstart = clock();
    sf_init(argc, argv);
    if (!sf_getbool("verb", &verb)) verb=false; /*verbosity*/
    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 */

    /*Set I/O file*/
    Fvel  = sf_input("vel");  /*velocity - just for model dimension*/
    Fsrc  = sf_input("in");   /*source wavelet*/
    if (wantrecord) {
	Frcd   = sf_input("rec");   /*record from elsewhere*/
    } else {
	Frcd   = sf_output("rec");   /*record produced by forward modeling*/
    }
    Fimg1  = sf_output("out");   /*Imaging*/
    Fimg2  = sf_output("img2");  /*Imaging*/

    if (wantwf) {
	Ftmpwf  = sf_output("tmpwf");/*wavefield snap*/
	Ftmpbwf = sf_output("tmpbwf");
    } else {
	Ftmpwf  = NULL;
	Ftmpbwf = NULL;
    }

    /*--- parameters of source ---*/
    srcpar srcp;
    srcp = createsrc();
    at = sf_iaxa(Fsrc, 1); nt = sf_n(at);  dt = sf_d(at);      
    if (!sf_getbool("srcdecay", &srcdecay)) srcdecay=SRCDECAY;
    /*source decay*/
    if (!sf_getint("srcrange", &srcrange)) srcrange=SRCRANGE;
    /*source decay range*/
    if (!sf_getfloat("srctrunc", &srctrunc)) srctrunc=SRCTRUNC;
    /*trunc source after srctrunc time (s)*/
    srcp->nt = nt; srcp->dt = dt; 
    srcp->decay = srcdecay; srcp->range=srcrange; srcp->trunc=srctrunc;
    loadsrc(srcp, Fsrc);

    /*--- Model axes ---*/
    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);

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

    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;

    nzx = nzb*nxb;
    /* nzx2 = nz2*nx2; */

    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);


    ltb = sf_complexalloc2(nzx,m2);
    rtb = sf_complexalloc2(m2,nk);
    sf_complexread(ltb[0],nzx*m2,leftb);
    sf_complexread(rtb[0],m2*nk,rightb);

//    sf_fileclose(left);
//    sf_fileclose(right);

    /* 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;
    /* Width of abc layer */

    nz = nzb - top - bot;
    nx = nxb - lft - rht;

    /*Geometry parameters*/
    geopar geop;
    geop = creategeo();
 
    /*source loaction parameters*/
    
    if (!sf_getfloat("slx", &slx)) slx=-1.0; 
    /*source location x */
    if (!sf_getint("spx", &spx)) spx = -1;
    /*source location x (index)*/
    if((slx<0 && spx <0) || (slx>=0 && spx >=0 ))  sf_error("Need src location");
    if (slx >= 0 )    spx = (int)((slx-ox)/dx+0.5);
    
    if (!sf_getfloat("slz", &slz)) slz=-1.0;
    /* source location z */
    if (!sf_getint("spz", &spz)) spz = -1;
    /*source location z (index)*/
    if((slz<0 && spz <0) || (slz>=0 && spz >=0 ))  sf_error("Need src location");
    if (slz >= 0 )    spz = (int)((slz-ox)/dz+0.5);
    
    if (!sf_getfloat("gdep", &gdep)) gdep=-1.0;
    /* recorder depth on grid*/
    if (!sf_getint("gp", &gp)) gp=0;
    /* recorder depth on index*/
    if ( gdep>=oz) { gp = (int)((gdep-oz)/dz+0.5);}
    if (gp < 0.0) sf_error("gdep need to be >=oz");
    /*source and receiver location*/
    if (!sf_getint("snapinter", &snpint)) snpint=10;
    /* snap interval */

    /*check record data*/
    if (wantrecord){
	sf_histint(Frcd,"n1", &tmpint);
	if (tmpint != nt ) sf_error("Error parameter n1 in record!");
	sf_histint(Frcd,"n2", &tmpint);
	if (tmpint != nx ) sf_error("Error parameter n2 in record!");
    }
    
    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->spx = spx;
    geop->spz = spz;
    geop->gp = gp;
    geop->top = top;
    geop->bot = bot;
    geop->lft = lft;
    geop->rht = rht;

    /* wavefield and record  */
    wfnt = (int)(nt-1)/snpint+1;
    sf_warning("nt=%d; snpint=%d; wfnt=%d!!!\n",nt,snpint,wfnt);
    wfdt = dt*snpint;
    record = sf_complexalloc2(nt, nx);
    sf_warning("fine!!!");

    wavefld = sf_floatalloc3(nz, nx, wfnt);

    sf_warning("fine!!!");

/*
    if (wantwf)
	wavefld2= sf_floatalloc3(nz, nx, wfnt);
    else
	wavefld2=NULL;
*/

    /*image*/
    img1 = sf_floatalloc2(nz, nx);
    img2 = sf_floatalloc2(nz, nx);

    if (verb) {
	sf_warning("============================");
	sf_warning("nx=%d nz=%d nt=%d", geop->nx, geop->nz, srcp->nt);
	sf_warning("nxb=%d nzb=%d ", geop->nxb, geop->nzb);
	sf_warning("dx=%f dz=%f dt=%f", geop->dx, geop->dz, srcp->dt);
	sf_warning("top=%d bot=%d lft=%d rht=%d", geop->top, geop->bot, geop->lft, geop->rht);
	sf_warning("srcdecay=%d srcrange=%d",srcp->decay,srcp->range);
	sf_warning("spx=%d spz=%d gp=%d snpint=%d", spx, spz, gp, snpint);
	sf_warning("wfdt=%f wfnt=%d ", wfdt, wfnt);
	sf_warning("============================");
    }

    /* write record */
    sf_setn(ax, nx);
    sf_setn(az, nz);
    
    if(!wantrecord) {
	sf_oaxa(Frcd, at, 1);
	sf_oaxa(Frcd, ax, 2);
	sf_settype(Frcd,SF_COMPLEX);
    }

    if (wantwf) {
	/*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_FLOAT);
	
	/*write temp wavefield */
	sf_oaxa(Ftmpbwf, az, 1);
	sf_oaxa(Ftmpbwf, ax, 2);
	sf_oaxa(Ftmpbwf, at, 3);
	sf_settype(Ftmpbwf,SF_FLOAT);
    }

    /*write image*/
    sf_oaxa(Fimg1, az, 1);
    sf_oaxa(Fimg1, ax, 2);
    sf_settype(Fimg1,SF_FLOAT);
    sf_oaxa(Fimg2, az, 1);
    sf_oaxa(Fimg2, ax, 2);
    sf_settype(Fimg2,SF_FLOAT);

    lrosfor2(wavefld, record, verb, lt, rt, m2, geop, srcp, pad1);
    if(wantrecord) {
	sf_complexread(record[0], nx*nt, Frcd);
    }
    //    lrosback2(img1, img2, wavefld, record, verb, wantwf, ltb, rtb, m2, geop, srcp, pad1, wavefld2);
    
    if (!wantrecord) {
	for (ix=0; ix<nx; ix++) 
	    sf_complexwrite(record[ix], nt, Frcd);
    }
    
    if (wantwf) {
	for (it=0; it<wfnt; it++)
	    for ( ix=0; ix<nx; ix++) {
		sf_floatwrite(wavefld[it][ix], nz, Ftmpwf);
		//		sf_floatwrite(wavefld2[it][ix],nz, Ftmpbwf);
	    }
    }

    for (ix=0; ix<nx; ix++) 
	sf_floatwrite(img1[ix], nz, Fimg1);
    for (ix=0; ix<nx; ix++) 
	sf_floatwrite(img2[ix], nz, Fimg2);
      
    tend = clock();
    duration=(double)(tend-tstart)/CLOCKS_PER_SEC;
    sf_warning(">> The CPU time of single shot migration is: %f seconds << ", duration);
    exit(0);

}
Ejemplo n.º 14
0
int main (int argc, char *argv[])
{
  bool verb, snap;
  bool abc, adj;
  int nz, nx, nt, ns, nr;
  float dz, dx, dt, oz, ox;
  int nz0, nx0, nb;
  float oz0, ox0;
  int nkz, nkx;
  int nzpad, nxpad;
  
  float **u1, **u0;
  float *ws, *wr;
  
  sf_file file_src = NULL, file_rec = NULL;
  sf_file file_inp = NULL, file_out = NULL;
  sf_file file_mdl = NULL;
  sf_axis az = NULL, ax = NULL, at = NULL, as = NULL, ar = NULL;
  pt2d *src2d = NULL;
  pt2d *rec2d = NULL;
  scoef2d cssinc = NULL;
  scoef2d crsinc = NULL;
  float *wi = NULL, *wo = NULL;
  sf_axis ai = NULL, ao = NULL;
  scoef2d cisinc = NULL, cosinc = NULL;
  bool spt = false, rpt = false;
  bool ipt = false, opt = false;
  
  sf_init(argc, argv);
  
  if (!sf_getbool("verb", &verb)) verb = false;
  if (!sf_getbool("snap", &snap)) snap = false;
  if (!sf_getbool("adj", &adj)) adj = false;
  if (!sf_getint("nb", &nb)) nb = 4;
  if (sf_getstring("sou") != NULL) { 
    spt = true;
    if (adj) opt = true;
    else     ipt = true;
  }
  if (sf_getstring("rec") != NULL) {
    rpt = true;
    if (adj) ipt = true;
    else     opt = true;
  }
  
  file_inp = sf_input("in");
  file_mdl = sf_input("model");
  if (spt) file_src = sf_input("sou");
  if (rpt) file_rec = sf_input("rec");
  file_out = sf_output("out");

  if (ipt) at = sf_iaxa(file_inp, 2);
  else     at = sf_iaxa(file_inp, 3);
  if (spt) as = sf_iaxa(file_src, 2);
  if (rpt) ar = sf_iaxa(file_rec, 2);
  az = sf_iaxa(file_mdl, 1);
  ax = sf_iaxa(file_mdl, 2);
  nt = sf_n(at);  dt = sf_d(at);  //ot = sf_o(at);
  nz0 = sf_n(az);  dz = sf_d(az);  oz0 = sf_o(az);
  nx0 = sf_n(ax);  dx = sf_d(ax);  ox0 = sf_o(ax);

  if (spt) ns = sf_n(as);
  if (rpt) nr = sf_n(ar);
  nz = nz0 + 2 * nb;
  nx = nx0 + 2 * nb;
  oz = oz0 - nb * dz;
  ox = ox0 - nb * dx;
  abc = nb ? true : false;
  // sf_error("ox=%f ox0=%f oz=%f oz0=%f",ox,ox0,oz,oz0);
  
  nzpad = kiss_fft_next_fast_size( ((nz+1)>>1)<<1 );
  nkx = nxpad = kiss_fft_next_fast_size(nx);
  nkz = nzpad / 2 + 1;
  /* float okx = - 0.5f / dx; */
  float okx = 0.f;
  float okz = 0.f;
  float dkx = 1.f / (nxpad * dx);
  float dkz = 1.f / (nzpad * dz);

  float **vp, **eps, **del;
  vp  = sf_floatalloc2(nz, nx);
  eps = sf_floatalloc2(nz, nx);
  del = sf_floatalloc2(nz, nx);
  float **tmparray = sf_floatalloc2(nz0, nx0);
  sf_floatread(tmparray[0], nz0*nx0, file_mdl); expand2d(vp[0], tmparray[0], nz, nx, nz0, nx0);
  sf_floatread(tmparray[0], nz0*nx0, file_mdl); expand2d(eps[0], tmparray[0], nz, nx, nz0, nx0);
  sf_floatread(tmparray[0], nz0*nx0, file_mdl); expand2d(del[0], tmparray[0], nz, nx, nz0, nx0);

  float **vn, **vh;  
  float **eta, **lin_eta;
  lin_eta = NULL, vh = NULL;
 
  vn = sf_floatalloc2(nz, nx);
  vh = sf_floatalloc2(nz, nx);
  eta = sf_floatalloc2(nz, nx);
  lin_eta = sf_floatalloc2(nz, nx);

  for (int ix=0; ix<nx; ix++) {
    for (int iz=0; iz<nz; iz++){
      vp[ix][iz] *= vp[ix][iz];
      vn[ix][iz] = vp[ix][iz] * (1.f + 2.f * del[ix][iz]);
      vh[ix][iz] = vp[ix][iz] * (1.f + 2.f * eps[ix][iz]);
      eta[ix][iz] = (eps[ix][iz] - del[ix][iz]) / (1.f + 2.f * del[ix][iz]);
      lin_eta[ix][iz] = eta[ix][iz] * (1.f + 2.f * del[ix][iz]);
    }
  }


  float *kx = sf_floatalloc(nkx);
  float *kz = sf_floatalloc(nkz);
  for (int ikx=0; ikx<nkx; ++ikx) {
    kx[ikx] = okx + ikx * dkx;
    /* if (ikx >= nkx/2) kx[ikx] = (nkx - ikx) * dkx; */
    if (ikx >= nkx/2) kx[ikx] = (ikx - nkx) * dkx;
    kx[ikx] *= 2 * SF_PI;
    kx[ikx] *= kx[ikx];
  }
  for (int ikz=0; ikz<nkz; ++ikz) {
    kz[ikz] = okz + ikz * dkz;
    kz[ikz] *= 2 * SF_PI;
    kz[ikz] *= kz[ikz];
  }

  if (adj) {
    ai = ar; ao = as;
  } else {
    ai = as; ao = ar;
  }

  if (opt) {
    sf_oaxa(file_out, ao, 1);
    sf_oaxa(file_out, at, 2);
  } else {
    sf_oaxa(file_out, az, 1);
    sf_oaxa(file_out, ax, 2);
    sf_oaxa(file_out, at, 3);
  }
  sf_fileflush(file_out, NULL);

  if (spt) {
    src2d = pt2dalloc1(ns);
    pt2dread1(file_src, src2d, ns, 2);
    cssinc = sinc2d_make(ns, src2d, nz, nx, dz, dx, oz, ox);
    ws = sf_floatalloc(ns);
    if (adj) { cosinc = cssinc;  wo = ws; }
    else     { cisinc = cssinc;  wi = ws; }
  }
  if (rpt) {
    rec2d = pt2dalloc1(nr);
    pt2dread1(file_rec, rec2d, nr, 2);
    crsinc = sinc2d_make(nr, rec2d, nz, nx, dz, dx, oz, ox);
    wr = sf_floatalloc(nr);
    if (adj) { cisinc = crsinc;  wi = wr; }
    else     { cosinc = crsinc;  wo = wr; }
  }

  u0 = sf_floatalloc2(nz, nx);
  u1 = sf_floatalloc2(nz, nx);
  float *rwave = (float *) fftwf_malloc(nzpad*nxpad*sizeof(float));
  float *rwavem = (float *) fftwf_malloc(nzpad*nxpad*sizeof(float));
  fftwf_complex *cwave = (fftwf_complex *) fftwf_malloc(nkz*nkx*sizeof(fftwf_complex));
  fftwf_complex *cwavem = (fftwf_complex *) fftwf_malloc(nkz*nkx*sizeof(fftwf_complex));
  /* float *rwavem = (float *) fftwf_malloc(nzpad*nxpad*sizeof(float));
  fftwf_complex *cwave = (fftwf_complex *) fftwf_malloc(nkz*nkx*sizeof(fftwf_complex));
  fftwf_complex *cwavem = (fftwf_complex *) fftwf_malloc(nkz*nkx*sizeof(fftwf_complex)); */

  /* boundary conditions */
  float **ucut = NULL;
  float *damp = NULL;
  if (!(ipt &&opt)) ucut = sf_floatalloc2(nz0, nx0);
  damp = damp_make(nb);
    
  float wt = 1./(nxpad * nzpad);
  wt *= dt * dt;
  fftwf_plan forward_plan;
  fftwf_plan inverse_plan;
#ifdef _OPENMP
#ifdef SF_HAS_FFTW_OMP
  fftwf_init_threads();
  fftwf_plan_with_nthreads(omp_get_max_threads());
#endif
#endif
  forward_plan = fftwf_plan_dft_r2c_2d(nxpad, nzpad,
              rwave, cwave, FFTW_MEASURE); 
#ifdef _OPENMP
#ifdef SF_HAS_FFTW_OMP
  fftwf_plan_with_nthreads(omp_get_max_threads());
#endif
#endif
  inverse_plan = fftwf_plan_dft_c2r_2d(nxpad, nzpad,
              cwavem, rwavem, FFTW_MEASURE); 
  int itb, ite, itc;
  if (adj) {
    itb = nt -1; ite = -1; itc = -1;
  } else {
    itb = 0; ite = nt; itc = 1;
  }

  if (adj) {
    for (int it=0; it<nt; it++) {
      if (opt) sf_floatwrite(wo, sf_n(ao), file_out);
      else     sf_floatwrite(ucut[0], nz0*nx0, file_out);
    }
    sf_seek(file_out, 0, SEEK_SET);
  }

  float **ptrtmp = NULL;
  memset(u0[0], 0, sizeof(float)*nz*nx);
  memset(u1[0], 0, sizeof(float)*nz*nx);
  memset(rwave, 0, sizeof(float)*nzpad*nxpad);
  memset(rwavem, 0, sizeof(float)*nzpad*nxpad);
  memset(cwave, 0, sizeof(float)*nkz*nkx*2);
  memset(cwavem, 0, sizeof(float)*nkz*nkx*2);

  for (int it=itb; it!=ite; it+=itc) { if (verb) sf_warning("it = %d;",it);
#ifdef _OPENMP
    double tic = omp_get_wtime();
#endif
    if (ipt) {
      if (adj) sf_seek(file_inp, (off_t)(it)*sizeof(float)*sf_n(ai), SEEK_SET);
      sf_floatread(wi, sf_n(ai), file_inp);
      for (int i=0; i<sf_n(ai); i++)
        wi[i] *= dt* dt;
    } else {
      if (adj) sf_seek(file_inp, (off_t)(it)*sizeof(float)*nz0*nx0, SEEK_SET);
      sf_floatread(ucut[0], nz0*nx0, file_inp);
      for (int j=0; j<nx0; j++)
      for (int i=0; i<nz0; i++)
        ucut[j][i] *= dt * dt;
    }

    /* apply absorbing boundary condition: E \times u@n-1 */
    damp2d_apply(u0, damp, nz, nx, nb);
    fft_stepforward(u0, u1, rwave, rwavem, cwave, cwavem,
        vp, vn, eta, vh, eps, lin_eta, kz, kx,
        forward_plan, inverse_plan,
        nz, nx, nzpad, nxpad, nkz, nkx, wt, adj);

    // sinc2d_inject1(u0, ws[it][s_idx], cssinc[s_idx]);
    if (ipt) sinc2d_inject(u0, wi, cisinc);
    else     wfld2d_inject(u0, ucut, nz0, nx0, nb);

    /* apply absorbing boundary condition: E \times u@n+1 */
    damp2d_apply(u0, damp, nz, nx, nb);

    /* loop over pointers */
    ptrtmp = u0;  u0 = u1;  u1 = ptrtmp;
    
    if (opt) {
      if (adj) sf_seek(file_out, (off_t)(it)*sizeof(float)*sf_n(ao),SEEK_SET);
      sinc2d_extract(u0, wo, cosinc);
      sf_floatwrite(wo, sf_n(ao), file_out);
    } else {
      if (adj) sf_seek(file_out, (off_t)(it)*sizeof(float)*nz0*nx0,SEEK_SET);
      wwin2d(ucut, u0, nz0, nx0, nb);
      sf_floatwrite(ucut[0], nz0*nx0, file_out);
    }

#ifdef _OPENMP
    double toc = omp_get_wtime();
    if (verb) fprintf(stderr," clock = %lf;", toc-tic);
#endif
  } /* END OF TIME LOOP */
  return 0;
}
Ejemplo n.º 15
0
int main(int argc, char* argv[])
{
    bool phase;
    char *label;
    int i1, n1, iw, nt, nw, i2, n2, rect, niter;
    float t, d1, w, w0, dw, *trace, *bs, *bc, *ss, *cc, *mm;
    sf_file time, timefreq, mask;
    
    sf_init(argc,argv);
    time = sf_input("in");
    timefreq = sf_output("out");

    if (!sf_histint(time,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histfloat(time,"d1",&d1)) d1=1.;
    n2 = sf_leftsize(time,1);

    if (!sf_getint("nw",&nw)) { /* number of frequencies */
		nt = 2*kiss_fft_next_fast_size((n1+1)/2);
		nw = nt/2+1;
		dw = 1./(nt*d1);
		w0 = 0.;
    } else {
		if (	!sf_getfloat("dw",&dw)) {
			/* f	requency step */
			nt =	2*kiss_fft_next_fast_size((n1+1)/2);
			dw =	1./(nt*d1);
		}
		if (!sf_getfloat("w0",&w0)) w0=0.;
		/* first frequency */
    }

    sf_shiftdim(time, timefreq, 2);
    
    sf_putint(timefreq,"n2",nw);
    sf_putfloat(timefreq,"d2",dw);
    sf_putfloat(timefreq,"o2",w0);

    if (NULL != (label = sf_histstring(time,"label1")) && !sf_fft_label(2,label,timefreq)) 
		sf_putstring(timefreq,"label2","Wavenumber");
    sf_fft_unit(2,sf_histstring(time,"unit1"),timefreq);

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

    trace = sf_floatalloc(n1);
    bs    = sf_floatalloc(n1);
    bc    = sf_floatalloc(n1);
    ss    = sf_floatalloc(n1);
    cc    = sf_floatalloc(n1);
    
    if (!sf_getint("rect",&rect)) rect=10;
    /* smoothing radius */
    if (!sf_getint("niter",&niter)) niter=100;
    /* number of inversion iterations */

    if (!sf_getbool("phase",&phase)) phase=false;
    /* output phase instead of amplitude */

    sf_divn_init(1,n1,&n1,&rect,niter,false);

    if (NULL != sf_getstring("mask")) {
		mask = sf_input("mask");
		mm = sf_floatalloc(n1);
    } else {
		mask = NULL;
		mm = NULL;
    }

    for (i2=0; i2 < n2; i2++) {
		sf_floatread(trace,n1,time);
		if (NULL != mm) {
			sf_floatread(mm,n1,mask);
			for (i1=0; i1 < n1; i1++) {
				trace[i1] *= mm[i1];
			}
		}

		for (iw=0; iw < nw; iw++) {
			w = w0 + iw*dw;

			if (0.==w) { /* zero frequency */
				for (i1=0; i1 < n1; i1++) {
					ss[i1] = 0.;
					bc[i1] = 0.5;
					if (NULL != mm) bc[i1] *= mm[i1];
				}
				sf_divn(trace,bc,cc);
			} else {
				for (i1=0; i1 < n1; i1++) {
					t = i1*d1;
					bs[i1] = sinf(w*t);
					bc[i1] = cosf(w*t);
					if (NULL != mm) {
						bs[i1] *= mm[i1];
						bc[i1] *= mm[i1];
					}
				}
				sf_divn(trace,bs,ss);
				sf_divn(trace,bc,cc);
			}

			for (i1=0; i1 < n1; i1++) {
				ss[i1] = phase? atan2f(ss[i1],cc[i1]): hypotf(ss[i1],cc[i1]);
			}

			sf_floatwrite(ss,n1,timefreq);
		}
    }
    

    exit(0);
}
Ejemplo n.º 16
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);
}