void rgradient_init(char* type, int horder, int m1, int m2) /*< initialize >*/ { float **p; int i; order = horder; nf = 2*order+1; c = sf_floatalloc2(nf, 2); p = lphpoly(order, order, type); for(i=0; i<2*nf; i++) c[0][i] = p[0][i]; free(p[0]); free(p); mode = type[0]; n1 = m1; n2 = m2; b1 = sf_floatalloc2(n1, n2); b2 = sf_floatalloc2(n1, n2); g = sf_floatalloc3(n1*3, n2, nf); b = g[0]; memset(g[0][0], 0, n1*n2*nf*sizeof(float)); #ifdef _OPENMP omp_init(); #endif }
int main(int argc, char* argv[]) { bool verb,fsrf,snap,expl,dabc; int jsnap,ntsnap,jdata; /* OMP parameters */ #ifdef _OPENMP int ompnth; #endif /* I/O files */ sf_file Fwav=NULL; /* wavelet */ sf_file Fsou=NULL; /* sources */ sf_file Frec=NULL; /* receivers */ sf_file Fmag=NULL; /* magnetic permitivity */ sf_file Fele=NULL; /* electric susceptibility */ sf_file Fcdt=NULL; /* conductivity */ sf_file Fdat=NULL; /* data */ sf_file Fwfl=NULL; /* wavefield */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* cube axes */ sf_axis at,az,ax; sf_axis as,ar; int nt,nz,nx,ns,nr,nb; int it,iz,ix; float dt,dz,dx,idz,idx; /* FDM structure */ fdm2d fdm=NULL; abcone2d abc=NULL; sponge spo=NULL; /* I/O arrays */ float *ww=NULL; /* wavelet */ pt2d *ss=NULL; /* sources */ pt2d *rr=NULL; /* receivers */ float *dd=NULL; /* data */ float **tt=NULL; float **vel=NULL; /* velocity */ float **mag=NULL; float **ele=NULL; float **cdt=NULL; float **cdtele=NULL; /* temporary cdt*dt/2*ele */ float **magele=NULL; /* temporary dt*dt/mag*ele */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ float **um,**uo,**up,**ua,**ut; /* wavefield: um = U @ t-1; uo = U @ t; up = U @ t+1 */ /* linear interpolation weights/indices */ lint2d cs,cr; /* FD operator size */ float co,cax,cbx,caz,cbz; /* wavefield cut params */ sf_axis acz=NULL,acx=NULL; int nqz,nqx; float oqz,oqx; float dqz,dqx; float **uc=NULL; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /*------------------------------------------------------------*/ /* OMP parameters */ #ifdef _OPENMP ompnth=omp_init(); #endif /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */ if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */ if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */ if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* I/O files */ Fwav = sf_input ("in" ); /* wavelet */ Fsou = sf_input ("sou"); /* sources */ Frec = sf_input ("rec"); /* receivers */ Fwfl = sf_output("wfl"); /* wavefield */ Fdat = sf_output("out"); /* data */ /*------------------------------------------------------------*/ Fmag = sf_input ("mag"); /* magnetic permitivity */ Fele = sf_input ("ele"); /* electric susceptibility */ Fcdt = sf_input ("cdt"); /* conductivity */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* axes */ at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(Fmag,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(Fmag,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */ as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */ ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ns = sf_n(as); nr = sf_n(ar); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* other execution parameters */ if(! sf_getint("jdata",&jdata)) jdata=1; if(snap) { /* save wavefield every *jsnap* time steps */ if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* expand domain for FD operators and ABC we exclude some of the code to maintain the same size of velocity model*/ if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP; fdm=fdutil_init(verb,fsrf,az,ax,nb,1); /*sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax);*/ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* setup output data header */ sf_oaxa(Fdat,ar,1); sf_setn(at,nt/jdata); sf_setd(at,dt*jdata); sf_oaxa(Fdat,at,2); /* setup output wavefield header */ if(snap) { if(!sf_getint ("nqz",&nqz)) nqz=sf_n(az); if(!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); dqz=sf_d(az); dqx=sf_d(ax); acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx); /* check if the imaging window fits in the wavefield domain */ uc=sf_floatalloc2(sf_n(acz),sf_n(acx)); ntsnap=0; for(it=0; it<nt; it++) { if(it%jsnap==0) ntsnap++; } sf_setn(at, ntsnap); sf_setd(at,dt*jsnap); if(verb) sf_raxa(at); sf_oaxa(Fwfl,acz,1); sf_oaxa(Fwfl,acx,2); sf_oaxa(Fwfl,at, 3); } if(expl) { ww = sf_floatalloc( 1); } else { ww = sf_floatalloc(ns); } dd = sf_floatalloc(nr); /*------------------------------------------------------------*/ /* setup source/receiver coordinates */ ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */ pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */ cs = lint2d_make(ns,ss,fdm); cr = lint2d_make(nr,rr,fdm); /*------------------------------------------------------------*/ /* setup FD coefficients */ idz = 1/dz; idx = 1/dx; co = C0 * (idx*idx+idz*idz); cax= CA * idx*idx; cbx= CB * idx*idx; caz= CA * idz*idz; cbz= CB * idz*idz; /*------------------------------------------------------------*/ tt = sf_floatalloc2(nz,nx); vel = sf_floatalloc2(fdm->nzpad,fdm->nxpad); mag = sf_floatalloc2(fdm->nzpad,fdm->nxpad); ele = sf_floatalloc2(fdm->nzpad,fdm->nxpad); cdt = sf_floatalloc2(fdm->nzpad,fdm->nxpad); cdtele =sf_floatalloc2(fdm->nzpad,fdm->nxpad); magele =sf_floatalloc2(fdm->nzpad,fdm->nxpad); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* input magnetic susceptibility*/ sf_floatread(tt[0],nz*nx,Fmag ); expand(tt,mag,fdm); /* input electric susceptibility*/ sf_floatread(tt[0],nz*nx,Fele ); expand(tt,ele,fdm); /* input conductivity*/ sf_floatread(tt[0],nz*nx,Fcdt ); expand(tt,cdt,fdm); /*------------------------------------------------------------*/ /* cdtele = sigma*dt/2*epsilon */ /* magele = dt*dt/mu*epsilon */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { cdtele[ix][iz] = cdt[ix][iz]*dt/(2*(ele[ix][iz])); magele[ix][iz] = dt*dt/(mag[ix][iz]*ele[ix][iz]); vel[ix][iz] = 1./(sqrt(mag[ix][iz]*ele[ix][iz])); } } if(fsrf) { /* free surface */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nb; iz++) { cdtele[ix][iz]=0; magele[ix][iz]=0; } } } /*------------------------------------------------------------*/ free(*tt); free(tt); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* allocate wavefield arrays */ um=sf_floatalloc2(fdm->nzpad,fdm->nxpad); uo=sf_floatalloc2(fdm->nzpad,fdm->nxpad); up=sf_floatalloc2(fdm->nzpad,fdm->nxpad); ua=sf_floatalloc2(fdm->nzpad,fdm->nxpad); for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { um[ix][iz]=0; uo[ix][iz]=0; up[ix][iz]=0; ua[ix][iz]=0; } } /*------------------------------------------------------------*/ if(dabc) { /* one-way abc setup */ abc = abcone2d_make(NOP,dt,vel,fsrf,fdm); /* sponge abc setup */ spo = sponge_make(fdm->nb); } /*------------------------------------------------------------*/ /* * MAIN LOOP */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",it); #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,ua,uo,co,cax,caz,cbx,cbz,idx,idz) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { /* 4th order Laplacian operator */ ua[ix][iz] = co * uo[ix ][iz ] + cax*(uo[ix-1][iz ] + uo[ix+1][iz ]) + cbx*(uo[ix-2][iz ] + uo[ix+2][iz ]) + caz*(uo[ix ][iz-1] + uo[ix ][iz+1]) + cbz*(uo[ix ][iz-2] + uo[ix ][iz+2]); } } /* inject acceleration source */ if(expl) { sf_floatread(ww, 1,Fwav); lint2d_inject1(ua,ww[0],cs); } else { sf_floatread(ww,ns,Fwav); lint2d_inject(ua,ww,cs); } /* step forward in time */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,ua,uo,um,up,cdtele,magele) #endif for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { up[ix][iz] = (2*uo[ix][iz] - (1-cdtele[ix][iz])*um[ix][iz] + ua[ix][iz]*(magele[ix][iz]))/(1+cdtele[ix][iz]); } } /* circulate wavefield arrays */ ut=um; um=uo; uo=up; up=ut; if(dabc) { /* one-way abc apply */ abcone2d_apply(uo,um,NOP,abc,fdm); sponge2d_apply(um,spo,fdm); sponge2d_apply(uo,spo,fdm); sponge2d_apply(up,spo,fdm); } /* extract data */ lint2d_extract(uo,dd,cr); if(snap && it%jsnap==0) { cut2d(uo,uc,fdm,acz,acx); sf_floatwrite(uc[0],sf_n(acz)*sf_n(acx),Fwfl); } if( it%jdata==0) sf_floatwrite(dd,nr,Fdat); } if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /* deallocate arrays */ free(*um); free(um); free(*up); free(up); free(*uo); free(uo); free(*ua); free(ua); free(*uc); free(uc); free(*mag); free(mag); free(*ele); free(ele); free(*cdt); free(cdt); free(*vel); free(vel); free(*cdtele); free(cdtele); free(*magele); free(magele); /*------------------------------------------------------------*/ free(ww); free(ss); free(rr); free(dd); /*------------------------------------------------------------*/ sf_close(); exit (0); }
int main(int argc, char* argv[]) { int it,i1,i2; /* index variables */ int nt,n12,ft,jt; float dt,d1,d2,dt2; float *ww,**vv,**rr; float **u0,**u1,**u2,**ud; sf_file Fw,Fv,Fr,Fo; /* I/O files */ /* initialize Madagascar */ sf_init(argc,argv); /* initialize OpenMP support */ omp_init(); /* setup I/O files */ Fr = sf_input ("in"); /* source position */ Fo = sf_output("out"); /* output wavefield */ Fw = sf_input ("wav"); /* source wavelet */ Fv = sf_input ("v"); /* velocity */ /* Read/Write axes */ if (!sf_histint(Fr,"n1",&n1)) sf_error("No n1= in inp"); if (!sf_histint(Fr,"n2",&n2)) sf_error("No n2= in inp"); if (!sf_histfloat(Fr,"d1",&d1)) sf_error("No d1= in inp"); if (!sf_histfloat(Fr,"d2",&d2)) sf_error("No d2= in inp"); if (!sf_histint(Fw,"n1",&nt)) sf_error("No n1= in wav"); if (!sf_histfloat(Fw,"d1",&dt)) sf_error("No d1= in wav"); n12 = n1*n2; if (!sf_getint("ft",&ft)) ft=0; /* first recorded time */ if (!sf_getint("jt",&jt)) jt=1; /* time interval */ sf_putint(Fo,"n3",(nt-ft)/jt); sf_putfloat(Fo,"d3",jt*dt); sf_putfloat(Fo,"o3",ft*dt); dt2 = dt*dt; /* set Laplacian coefficients */ d1 = 1.0/(d1*d1); d2 = 1.0/(d2*d2); c11 = 4.0*d1/3.0; c12= -d1/12.0; c21 = 4.0*d2/3.0; c22= -d2/12.0; c0 = -2.0 * (c11+c12+c21+c22); /* read wavelet, velocity & source position */ ww=sf_floatalloc(nt); sf_floatread(ww ,nt ,Fw); vv=sf_floatalloc2(n1,n2); sf_floatread(vv[0],n12,Fv); rr=sf_floatalloc2(n1,n2); sf_floatread(rr[0],n12,Fr); /* allocate temporary arrays */ u0=sf_floatalloc2(n1,n2); u1=sf_floatalloc2(n1,n2); u2=sf_floatalloc2(n1,n2); ud=sf_floatalloc2(n1,n2); for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { u0[i2][i1]=0.0; u1[i2][i1]=0.0; u2[i2][i1]=0.0; ud[i2][i1]=0.0; vv[i2][i1] *= vv[i2][i1]*dt2; } } /* Time loop */ for (it=0; it<nt; it++) { laplacian(u1,ud); #ifdef _OPENMP #pragma omp parallel for \ private(i2,i1) \ shared(ud,vv,ww,it,rr,u2,u1,u0) #endif for (i2=0; i2<n2; i2++) { for (i1=0; i1<n1; i1++) { /* scale by velocity */ ud[i2][i1] *= vv[i2][i1]; /* inject wavelet */ ud[i2][i1] += ww[it] * rr[i2][i1]; /* time step */ u2[i2][i1] = 2*u1[i2][i1] - u0[i2][i1] + ud[i2][i1]; u0[i2][i1] = u1[i2][i1]; u1[i2][i1] = u2[i2][i1]; } } /* write wavefield to output */ if (it >= ft && 0 == (it-ft)%jt) { sf_warning("%d;",it+1); sf_floatwrite(u1[0],n12,Fo); } } sf_warning("."); exit (0); }
int main(int argc, char* argv[]) { bool verb,conj,twin,Pf1,PG; /* OMP parameters */ #ifdef _OPENMP int ompnth; #endif float *F_arrival, *Refl, *G, *Gm, *Gp, *f1pS, *f1p, *F1pS, *F1p; float *MS, *MS1, *F1m_0, *f1m_0, *F1m, *F1m1, *f1m, *ms, *gm, *gp; float *MS_0, *ms_0, *ms_2, *MS2; float *gp1, *gm1; float *window, *taper, pi; int *tw,allocated=0; /* I/O files */ sf_file FF_arrival; sf_file FRefl; sf_file FGp; sf_file FGm; sf_file FG; sf_file Ff1m; sf_file Ff1p; sf_file Ftwin; char *filename1, filename2[256], filename3[256]; /* Cube axes */ sf_axis at,af,ax,at1; int nt,nf,ntr,mode,nshots,niter,len; int i,it,ix,ishot,iter,i0; int twc, twa, shift, n[2], rect[2], s[2], tap; float scale,eps,dt,df,dx,ot,of,a,b,c,d,e,f,r; sf_triangle tr; /*------------------------------------------------------------*/ /* Initialize RSF parameters */ /*------------------------------------------------------------*/ sf_init(argc,argv); /*------------------------------------------------------------*/ /* Initialize OMP parameters */ /*------------------------------------------------------------*/ #ifdef _OPENMP ompnth=omp_init(); #endif /*------------------------------------------------------------*/ /* Flags */ /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("conj",&conj)) conj=false; /* complex conjugation (time-reversal) flag */ if(! sf_getbool("twin",&twin)) twin=false; /* returns the timewindow as one of the outputs */ if(! sf_getbool("Pf1",&Pf1)) Pf1=false; /* Htot=true: returns H=Gp-Gm */ if(! sf_getbool("PG",&PG)) PG=false; /* Htot=true: returns H=Gp-Gm */ if(! sf_getint("niter",&niter)) niter=1; /* number of iterations */ if(! sf_getint("nshots",&nshots)) nshots=1; /* number of shots */ if(! sf_getfloat("r",&r)) r=-1; /* reflection coefficient if flux normalised r=-1 */ if(! sf_getfloat("scale",&scale)) scale=1.0; /* scale factor */ if(! sf_getfloat("eps",&eps)) eps=1e-4; /* threshold for the timewindow */ if(! sf_getint("shift",&shift)) shift=5; /* shift in samples for the timewindow */ if(! sf_getint("tap",&tap)) tap=20; /* taper of R */ if (verb) { fprintf(stderr,"This program was called with \"%s\".\n",argv[0]); /*fprintf(stderr,"Nr: %d Nx: %d Nt:%d\n",nr,nx,nt);*/ if (argc > 1) { for (i = 1; i<argc; i++) { fprintf(stderr,"argv[%d] = %s\n", i, argv[i]); } } else { fprintf(stderr,"The command had no other arguments.\n"); } } /*------------------------------------------------------------*/ /* I/O files */ /*------------------------------------------------------------*/ /* "in" is the transposed version of p00plus_xxxx_xxxx.rsf Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=ntr,n2=nt Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=nt,n2=ntr */ FF_arrival = sf_input("in"); /* refl is REFL_000.rsf It is used to read nf, df, of Dimensions are: n1=nf,n2=ntr */ /*FRefl = (sf_file)sf_alloc(1,sizeof(sf_file));*/ FRefl = sf_input("refl"); FGp = sf_output("out"); FGm = sf_output("Gm"); if (PG) { FG = sf_output("G"); } if (Pf1) { Ff1p = sf_output("f1p"); Ff1m = sf_output("f1m"); } if (twin) { Ftwin = sf_output("window"); /* time window */ } /*------------------------------------------------------------*/ /* Axes */ /*------------------------------------------------------------*/ at = sf_iaxa(FF_arrival,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */ at1 = sf_iaxa(FF_arrival,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */ af = sf_iaxa(FRefl,1); sf_setlabel(af,"Frequency"); if(verb) sf_raxa(af); /* frequency */ ax = sf_iaxa(FF_arrival,2); sf_setlabel(ax,"r"); if(verb) sf_raxa(ax); /* space */ nt = sf_n(at); dt = sf_d(at); ot = sf_o(at); nf = sf_n(af); df = sf_d(af); of = sf_o(af); ntr = sf_n(ax); dx = sf_d(ax); int nt2=(nt/2); sf_setn(at1,nt2); if (verb) fprintf(stderr,"nt: %d nf: %d ntr:%d\n",nt,nf,ntr); sf_fileclose(FRefl); /*------------------------------------------------------------*/ /* Setup output data and wavefield header */ /*------------------------------------------------------------*/ sf_oaxa(FGp,at1,1); sf_oaxa(FGp,ax,2); sf_oaxa(FGm,at1,1); sf_oaxa(FGm,ax,2); if (PG) { sf_oaxa(FG,at1,1); sf_oaxa(FG,ax,2); } if (Pf1) { sf_oaxa(Ff1p,at,1); sf_oaxa(Ff1p,ax,2); sf_oaxa(Ff1m,at,1); sf_oaxa(Ff1m,ax,2); } if (twin) { sf_oaxa(Ftwin,at,1); sf_oaxa(Ftwin,ax,2); } /*------------------------------------------------------------*/ /* Allocate arrays */ /*------------------------------------------------------------*/ /* First arrival - Time */ F_arrival = (float *)calloc(nt*ntr,sizeof(float)); allocated+=nt*ntr; sf_floatread(F_arrival,nt*ntr,FF_arrival); ms = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; ms_0 = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; ms_2 = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; f1m_0= (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; f1m = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; f1pS = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; f1p = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; memcpy(ms,F_arrival,nt*ntr*sizeof(float));allocated+=nt*ntr; /* Allocate for coda M of f2 - Frequency */ MS = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; MS_0 = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; MS1 = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; MS2 = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; F1m_0= (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; F1m = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; F1m1 = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; F1pS = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; F1p = (float *)calloc(2*nf*ntr,sizeof(float));allocated+=2*nf*ntr; /* The three flags of fft1 are: inv, sym, and opt */ fft1_init (nt, dt, ot, true, false); fft1_2D_fwd(F_arrival,MS,ntr); //sf_warning("passed fft? yes"); //fft1(F_arrival,MS,FF_arrival,0,0,1); /* memcpy(FA,2*nf*ntr*sizeof(float));*/ fprintf(stderr,"nt2 is %d\n",nt2); /* Output wavefields */ G = (float *)calloc(nt2*ntr,sizeof(float));allocated+=nt2*ntr; gp1= (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; gp= (float *)calloc(nt2*ntr,sizeof(float));allocated+=nt2*ntr; gm1= (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; gm= (float *)calloc(nt2*ntr,sizeof(float));allocated+=nt2*ntr; Gp= (float *)calloc(2*nf*ntr,sizeof(float));allocated+=nf*ntr; Gm= (float *)calloc(2*nf*ntr,sizeof(float));allocated+=nf*ntr; /* Time-reversal flag */ if (conj) { mode = -1; } else { mode = +1; } /* Load the reflection response into the memory */ if (verb) fprintf(stderr,"Before loading R %d\n",2*nf*ntr); Refl = (float *)calloc(2*nf*ntr*nshots,sizeof(float));allocated+=2*nf*ntr*nshots; /* Read REFL_000.rsf */ FRefl = sf_input("refl"); sf_warning("reading refl"); sf_floatread(Refl,2*nf*nshots*ntr,FRefl); sf_warning("read refl"); /* Build time-window */ tw = (int *)calloc(ntr,sizeof(int)); allocated+=ntr; window = (float *)calloc(nt*ntr,sizeof(float));allocated+=nt*ntr; /*memset(window,0,nt*ntr*sizeof(float));*/ /* I am not sure why I set it to this value */ /*for (ix=0; ix<ntr; ix++) { tw[ix] = nt*dt+ot+0.15; }*/ if (verb) fprintf(stderr,"---> Build time-window?\n"); // checking time sample corresponding to muting time for (ix=0; ix<ntr; ix++) { for (it=0; it<nt; it++) { if ((F_arrival[it+ix*nt]*F_arrival[it+ix*nt])>eps*eps) { /*tw[ix] = it*dt+ot;*/ tw[ix] = it; break; } } } if (verb) fprintf(stderr,"---> Build time-window1\n"); for (ix=0; ix<ntr; ix++) { twc = (int)(tw[ix]-shift-10); twa = (int)(-twc+nt); /*if (verb) fprintf(stderr,"%d %d\n",twc,twa);*/ for (it=0; it<nt; it++) { /* if ((it>twa) || (it<twc)) {*/ if ((it>twa) && (it<twc)) { window[it+ix*nt] = 1.0; // building windowing function W from Filippo's paper } } } if (verb) fprintf(stderr,"---> Build time-window2\n"); /* Smoothing of the window */ /* Should I implement flags for rect and iter? */ /* Look at Msmooth.c to understand below */ n[0] = nt; n[1] = ntr; s[0] = 1; s[1] = nt; rect[0] = 5; rect[1] = 5; for (ix=0; ix <= 1; ix++) { if (rect[ix] <= 1) continue; tr = sf_triangle_init (rect[ix],n[ix],false); for (it=0; it < (nt*ntr/n[ix]); it++) { i0 = sf_first_index (ix,it,1+1,n,s); for (iter=0; iter < 2; iter++) { sf_smooth2 (tr,i0,s[ix],false,window ); } } sf_triangle_close(tr); } if (verb) fprintf(stderr,"---> Here\n"); /* Tapering */ pi = 4.0*atan(1.0); taper = (float *)calloc(ntr,sizeof(float));allocated+=ntr; sf_warning("estimated memory: %f bytes",allocated*4.0f); memset(taper,0,ntr*sizeof(float)); for (ix=0; ix<tap; ix++) { taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/(2*tap)))); taper[ntr-ix-1] = taper[ix]; } for (ix=tap; ix<(ntr-tap); ix++) { taper[ix] = 1.0; } if (verb) fprintf(stderr,"---> taper finish\n"); FRefl = sf_input("refl"); /*------------------------------------------------------------*/ /* Loop over iterations */ /*------------------------------------------------------------*/ if (verb) fprintf(stderr,"---> Begin to iterative solve for f1p and f1m\n"); /*starting iteration for f1m */ memset(F1m_0,0,2*nf*ntr*sizeof(float)); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d) \ shared(MS,taper,Refl,F1m_0,MS2) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = MS[ishot*2*nf+it]; d = MS[ishot*2*nf+it+1]; F1m_0[ix*2*nf+it] += (a*c - mode*b*d); F1m_0[ix*2*nf+it+1] += (mode*a*d + b*c); MS2 [ix*2*nf+it] += r*(a*c - b*d); // rTd* R MS2 [ix*2*nf+it+1] += r*(a*d + b*c); } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ } fft1_2D_inv (F1m_0, f1m_0,ntr); fft1_2D_inv (MS2, ms_2,ntr); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(f1m, f1m_0, window,ms_2) #endif /* window to get f1m_0 */ for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { f1m_0[it+ix*nt] = scale*window[it+ix*nt]*f1m_0[it+ix*nt]; ms_2[it+ix*nt] = scale*window[it+ix*nt]*ms_2[it+ix*nt]; //f1m_0[it+ix*nt] = scale*f1m_0[it+ix*nt]; f1m[it+ix*nt] = f1m_0[it+ix*nt]; } } fft1_2D_fwd(f1m,F1m,ntr); //fft1(f1m,F1m,FF_arrival,0,0,1); /* initialise MS the coda for f1+ */ memset(MS_0,0,2*nf*ntr*sizeof(float)); memset(MS,0,2*nf*ntr*sizeof(float)); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d) \ shared(MS_0,taper,Refl,F1m) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = F1m[ishot*2*nf+it]; d = F1m[ishot*2*nf+it+1]; MS_0[ix*2*nf+it] += (a*c - mode*b*d); MS_0[ix*2*nf+it+1] += (mode*a*d + b*c); } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ } fft1_2D_inv(MS_0,ms_0,ntr); //fft1(MS_0,ms_0,FRefl,1,0,1); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(ms, ms_0, window,ms_2) #endif /* window to get f1m_0 */ for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { ms[it+ix*nt] =-ms_2[it+ix*nt] +scale*window[it+ix*nt]*ms_0[it+ix*nt]; //f1m_0[it+ix*nt] = scale*f1m_0[it+ix*nt]; //ms[it+ix*nt] = ms_0[it+ix*nt]; } } fft1_2D_fwd(ms,MS,ntr); if (verb) fprintf(stderr,"---> Beginning Iteration\n"); for (iter=0; iter<niter; iter++) { /* initialise MS1 and f1m1 the coda for f1+ */ memset(MS1,0,2*nf*ntr*sizeof(float)); memset(F1m1,0,2*nf*ntr*sizeof(float)); /*------------------------------------------------------------*/ /* Loop over shot positions */ /*------------------------------------------------------------*/ for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d,e,f) \ shared(MS,taper,Refl,F1m,F1m1) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/ /*(a + bi)(e + fi) = (ae - bf) + (af + be)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = MS[ishot*2*nf+it]; d = MS[ishot*2*nf+it+1]; e = F1m[ishot*2*nf+it]; f = F1m[ishot*2*nf+it+1]; F1m1[ix*2*nf+it] += (a*c - mode*b*d) - r*(a*e - b*f); F1m1[ix*2*nf+it+1] += (mode*a*d + b*c) - r*(a*f + b*e); } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ } /* End of loop over shot positions */ /* Get time domain output of f1m and ms */ fft1_2D_inv(F1m1,f1m,ntr); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(f1m, f1m_0, window) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { f1m[it+ix*nt] = f1m_0[it+ix*nt] + scale*window[it+ix*nt]*(f1m[it+ix*nt]); } } fft1_2D_fwd(f1m,F1m,ntr); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d,e,f) \ shared(MS,MS1,taper,Refl,F1m) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/ /*(a + bi)(e + fi) = (ae - bf) + (af + be)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = MS[ishot*2*nf+it]; d = MS[ishot*2*nf+it+1]; e = F1m[ishot*2*nf+it]; f = F1m[ishot*2*nf+it+1]; MS1[ix*2*nf+it] += (a*e - mode*b*f) - r*(a*c - b*d); MS1[ix*2*nf+it+1] += (mode*a*f + b*e) - r*(a*d + b*c); } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ } /* End of loop over shot positions */ /* Get time domain output of f1m and ms */ fft1_2D_inv(MS1,ms,ntr); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(ms, window) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { ms[it+ix*nt] =-ms_2[it+ix*nt]+ scale*window[it+ix*nt]*(ms[it+ix*nt]); } } fft1_2D_fwd(ms,MS,ntr); if(iter%4==0) fprintf(stderr,"Iteration %d\n",iter); } /* End of loop over iterations */ /* Build f1p* by adding Tinv to coda M */ #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(f1pS,F_arrival,ms) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { f1pS[it+ix*nt] = F_arrival[it+ix*nt] + ms[it+ix*nt]; /* note this is the time reverse version of f1p */ } } fft1_2D_fwd(f1pS,F1pS,ntr); /* to get G by looping over shots */ memset(Gp,0,2*nf*ntr*sizeof(float)); memset(Gm,0,2*nf*ntr*sizeof(float)); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d,e,f) \ shared(F1pS, F1m, taper, Refl, Gp, Gm) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = F1pS[ishot*2*nf+it]; d = F1pS[ishot*2*nf+it+1]; e = F1m[ishot*2*nf+it]; f = F1m[ishot*2*nf+it+1]; Gm[ix*2*nf+it] += (a*c -mode* b*d) -r*(a*e - b*f); Gm[ix*2*nf+it+1] += (mode*a*d + b*c) -r*(a*f + b*e); Gp[ix*2*nf+it] += -(a*e - mode*b*f) + r*(a*c - b*d); Gp[ix*2*nf+it+1] += -(mode*a*f + b*e) + r*(a*d + b*c); } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ } fft1_2D_inv(Gp,gp1,ntr); fft1_2D_inv(Gp,gm1,ntr); if (Pf1) { if (verb) fprintf(stderr,"---> Build f1p\n"); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,c,d) \ shared(F1pS, F1p) #endif #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(a + bi)(c + di) = (ac - bd) + (ad + bc)i*/ c = F1pS[ishot*2*nf+it]; d = F1pS[ishot*2*nf+it+1]; F1p[ishot*2*nf+it] = c; F1p[ishot*2*nf+it+1] = -d; } /* End of loop over frequencies */ } } fft1_2D_inv(F1p,f1p,ntr); if (verb) fprintf(stderr,"Build Gp, Gm and G\n"); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(f1m, f1pS, gp, gm, gp1, gm1, G) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt2; it++) { gm[it+ix*nt2] = ((scale*gm1[it+nt2+ix*nt]) - f1m[ it+nt2+ix*nt])*(1.0 - window[it+nt2+ix*nt]); gp[it+ix*nt2] = ((scale*gp1[it+nt2+ix*nt]) + f1pS[it+nt2+ix*nt])*(1.0 - window[it+nt2+ix*nt]); G[ it+ix*nt2] = 0.5*(gp[it+ix*nt2] + gm[it+ix*nt2]); } } fprintf(stderr,"Build Gp, Gm and G\n"); /* Write the final result */ /*FRefl = sf_input(argv[1]);*/ /*fft1(Gp,,FRefl,1,0,0); fft1(Gm,,FRefl,1,0,0);*/ sf_floatwrite(gp,nt2*ntr,FGp); sf_fileclose(FGp); sf_floatwrite(gm,nt2*ntr,FGm); sf_fileclose(FGm); if (PG) { sf_floatwrite(G,nt2*ntr,FG); sf_fileclose(FG); } if (Pf1) { sf_floatwrite(f1p,nt*ntr,Ff1p); sf_fileclose(Ff1p); sf_floatwrite(f1m,nt*ntr,Ff1m); sf_fileclose(Ff1m); } if (twin) { sf_floatwrite(window,nt*ntr,Ftwin); sf_fileclose(Ftwin); } sf_fileclose(FRefl); sf_fileclose(FF_arrival); free(G); free(Gm); free(Gp); free(Refl); free(f1pS); free(F1pS); free(f1p); free(F1p); free(tw); free(filename1); free(MS); free(MS_0); free(MS1); free(MS2); free(F1m); free(F1m1); free(f1m_0); free(F1m_0); free(f1m); free(gp); free(gp1); free(gm); free(gm1); free(ms); free(ms_0); free(ms_2); free(F_arrival); free(window); exit (0); }
int main(int argc, char** argv) { bool verb, fsrf, snap, expl, dabc, cden, adj; bool optfd, hybrid, sinc; int jsnap, jdata; /* I/O files */ sf_file file_wav=NULL; /* wavelet */ sf_file file_vel=NULL; /* velocity */ sf_file file_den=NULL; /* density */ sf_file file_wfl=NULL; /* wavefield */ sf_file file_dat=NULL; /* data */ sf_file file_src=NULL; /* sources */ sf_file file_rec=NULL; /* receivers */ /* cube axes */ sf_axis at = NULL, az = NULL, ax = NULL, ay = NULL; sf_axis as = NULL, ar = NULL; int nbd; /* ABC boundary size */ int fdorder; /* finite difference spatial accuracy order */ int nzpad,nxpad,nypad; /* boundary padded model size */ int ix,iy,it,is,nx,ny,nz,nt,ns,nr; float dx,dy,dz,dt,dt2; float* damp=NULL; /* damping profile for hybrid bc */ float* ws; /* wavelet */ float*** vel=NULL; /* velocity */ float*** rho=NULL; /* density */ float*** u0=NULL; /* wavefield array u@t-1 (u@t+1) */ float*** u1=NULL; /* wavefield array u@t */ float* u_dat=NULL; /* output data */ float*** ptr_tmp=NULL; pt3d* src3d=NULL; /* source position */ pt3d* rec3d=NULL; /*receiver position*/ scoef3d cssinc = NULL, crsinc = NULL; lint3d cslint = NULL, crlint = NULL; /* FDM structure */ fdm3d fdm = NULL; abcone3d abc = NULL; sponge spo = NULL; int nbell; float* fdcoef_d2; float* fdcoef_d1; sf_axis acz = NULL, acx = NULL, acy = NULL; int nqz, nqx, nqy; float oqz, oqx, oqy, dqz, dqx, dqy; float** oslice = NULL; /* output 3D wavefield slice-by-slice */ float*** tmp_array; double wall_clock_time_s, wall_clock_time_e; const int SECOND_DERIV = 2; const int FIRST_DERIV = 1; int nop; #if defined _OPENMP && _DEBUG double tic; double toc; #endif /* init RSF */ sf_init(argc,argv); #ifdef _OPENMP omp_init(); wall_clock_time_s = omp_get_wtime(); #else wall_clock_time_s = (double) clock() / CLOCKS_PER_SEC; #endif if (!sf_getbool("verb",&verb)) verb=false; /* Verbosity flag */ if (!sf_getbool("snap",&snap)) snap=false; /* Wavefield snapshots flag */ if (!sf_getbool("expl",&expl)) expl=false; /* Multiple sources, one wvlt*/ if (!sf_getbool("dabc",&dabc)) dabc=false; /* Absorbing BC */ if (!sf_getbool("cden",&cden)) cden=false; /* Constant density */ if (!sf_getbool("adj",&adj)) adj=false; /* adjoint flag */ if (!sf_getbool("free",&fsrf) && !sf_getbool("fsrf",&fsrf)) fsrf=false; /* Free surface flag */ if (!sf_getint("nbell",&nbell)) nbell=5; /* gaussian for source injection */ if (!sf_getbool("optfd",&optfd)) optfd=false; /* optimized FD coefficients flag */ if (!sf_getint("fdorder",&fdorder)) fdorder=4; /* spatial FD order */ if (!sf_getbool("hybridbc",&hybrid)) hybrid=false; /* hybrid Absorbing BC */ if (!sf_getbool("sinc",&sinc)) sinc=false; /* sinc source injection */ /* Initialize variables */ file_wav = sf_input("in"); /* wavelet */ file_vel = sf_input("vel"); /* velocity */ file_src = sf_input("sou"); /* sources */ file_rec = sf_input("rec"); /* receivers */ file_dat = sf_output("out"); /* data */ if (snap) file_wfl = sf_output("wfl"); /* wavefield */ if (!cden) { if (sf_getstring("cden")) { file_den = sf_input ("den"); /* density */ } else { cden = true; if (verb) sf_warning("No density file provided, running with constant density"); } } at = sf_iaxa(file_wav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(file_vel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(file_vel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */ ay = sf_iaxa(file_vel,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space */ as = sf_iaxa(file_src,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */ ar = sf_iaxa(file_rec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ny = sf_n(ay); dy = sf_d(ay); ns = sf_n(as); nr = sf_n(ar); /* other execution parameters */ if (snap) { if (!sf_getint("jsnap",&jsnap)) jsnap=nt; /* # of t steps at which to save wavefield */ } if (!sf_getint("jdata",&jdata)) jdata=1; /* # of t steps at which to save receiver data */ /* setup output data header */ sf_oaxa(file_dat,ar,1); sf_setn(at,(nt-1)/jdata+1); sf_setd(at,dt*jdata); sf_oaxa(file_dat,at,2); /* wavefield cut params */ /* setup output wavefield header */ if (snap) { if (!sf_getint ("nqz",&nqz)) nqz=sf_n(az); /* Saved wfld window nz */ if (!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); /* Saved wfld window nx */ if (!sf_getint ("nqy",&nqy)) nqy=sf_n(ay); /* Saved wfld window ny */ if (!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); /* Saved wfld window oz */ if (!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); /* Saved wfld window ox */ if (!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay); /* Saved wfld window oy */ if (!sf_getfloat("dqz",&dqz)) dqz=sf_d(az); /* Saved wfld window dz */ if (!sf_getfloat("dqx",&dqx)) dqx=sf_d(ax); /* Saved wfld window dx */ if (!sf_getfloat("dqy",&dqy)) dqy=sf_d(ay); /* Saved wfld window dy */ acz = sf_maxa(nqz,oqz,dqz); if (verb) sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); if (verb) sf_raxa(acx); acy = sf_maxa(nqy,oqy,dqy); if (verb) sf_raxa(acy); /* check if the imaging window fits in the wavefield domain */ sf_setn(at,(nt-1)/jsnap+1); sf_setd(at,dt*jsnap); if (verb) sf_raxa(at); sf_oaxa(file_wfl,acz,1); sf_oaxa(file_wfl,acx,2); sf_oaxa(file_wfl,acy,3); sf_oaxa(file_wfl,at,4); } /* 2-2N finite difference coefficient */ nop = fdorder/2; /* fd half-length stencil */ if (!sf_getint("nb",&nbd) || nbd<nop) nbd=nop; if (dabc && hybrid && nbd<=nop) nbd = 2*nop; /* expand domain for FD operators and ABC */ fdm = fdutil3d_init(verb,fsrf,az,ax,ay,nbd,1); sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if (verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if (verb) sf_raxa(ax); sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if (verb) sf_raxa(ay); /* Precompute coefficients */ dt2 = dt*dt; nzpad = nz+2*nbd; nxpad = nx+2*nbd; nypad = ny+2*nbd; fdcoef_d2 = compute_fdcoef(nop,dz,dx,dy,optfd,SECOND_DERIV); fdcoef_d1 = compute_fdcoef(nop,dz,dx,dy,optfd,FIRST_DERIV); /* Allocate memories */ if (expl) ws = sf_floatalloc(1); else ws = sf_floatalloc(ns); vel = sf_floatalloc3(nzpad,nxpad,nypad); if (!cden) rho = sf_floatalloc3(nzpad,nxpad,nypad); u_dat = sf_floatalloc(nr); src3d = pt3dalloc1(ns); rec3d = pt3dalloc1(nr); if (snap) oslice = sf_floatalloc2(sf_n(acz),sf_n(acx)); /* source and receiver position */ pt3dread1(file_src,src3d,ns,3); /* read format: (x,y,z) */ if (sinc) cssinc = sinc3d_make(ns,src3d,fdm); else cslint = lint3d_make(ns,src3d,fdm); pt3dread1(file_rec,rec3d,nr,3); /* read format: (x,y,z) */ if (sinc) crsinc = sinc3d_make(nr,rec3d,fdm); else crlint = lint3d_make(nr,rec3d,fdm); if (!sinc) fdbell3d_init(nbell); /* temperary array */ tmp_array = sf_floatalloc3(nz,nx,ny); /* read velocity and pad */ sf_floatread(tmp_array[0][0],nz*nx*ny,file_vel); expand3d(tmp_array,vel,fdm); /* read density and pad */ if (!cden) { sf_floatread(tmp_array[0][0],nz*nx*ny,file_den); expand3d(tmp_array,rho,fdm); } free(**tmp_array); free(*tmp_array); free(tmp_array); /* A1 one-way ABC implicit scheme coefficients */ if (dabc) { abc = abcone3d_make(nbd,dt,vel,fsrf,fdm); if (hybrid) damp = damp_make(nbd-nop); /* compute damping profiles for hybrid bc */ else spo = sponge_make(fdm->nb); } /* allocate memory for wavefield variables */ u0 = sf_floatalloc3(nzpad,nxpad,nypad); u1 = sf_floatalloc3(nzpad,nxpad,nypad); /* initialize variables */ memset(u0[0][0],0,sizeof(float)*nzpad*nxpad*nypad); memset(u1[0][0],0,sizeof(float)*nzpad*nxpad*nypad); memset(u_dat,0,sizeof(float)*nr); /* v = (v*dt)^2 */ for (ix=0;ix<nzpad*nxpad*nypad;ix++) *(vel[0][0]+ix) *= *(vel[0][0]+ix)*dt2; if (fsrf && !hybrid) { for (iy=0; iy<nypad; iy++) for (ix=0; ix<nxpad; ix++) memset(vel[iy][ix],0,sizeof(float)*fdm->nb); } for (it=0; it<nt; it++) { if (verb) sf_warning("it=%d;",it+1); #if defined _OPENMP && _DEBUG tic=omp_get_wtime(); #endif step_forward(u0,u1,vel,rho,fdcoef_d2,fdcoef_d1,nop,nzpad,nxpad,nypad); if (adj) { /* backward inject source wavelet */ if (expl) { sf_seek(file_wav,(off_t)(nt-it-1)*sizeof(float),SEEK_SET); sf_floatread(ws,1,file_wav); ws[0] *= dt2; if (sinc) sinc3d_inject1(u0,ws[0],cssinc); else lint3d_inject1(u0,ws[0],cslint); } else { sf_seek(file_wav,(off_t)(nt-it-1)*ns*sizeof(float),SEEK_SET); sf_floatread(ws,ns,file_wav); for (is=0; is<ns; is++) ws[is] *= dt2; if (sinc) sinc3d_inject(u0,ws,cssinc); else lint3d_inject(u0,ws,cslint); } } else { /* forward inject source wavelet */ if (expl) { sf_floatread(ws,1,file_wav); ws[0] *= dt2; if (sinc) sinc3d_inject1(u0,ws[0],cssinc); else lint3d_inject1(u0,ws[0],cslint); } else { sf_floatread(ws,ns,file_wav); for (is=0; is<ns; is++) ws[is] *= dt2; if (sinc) sinc3d_inject(u0,ws,cssinc); else lint3d_inject(u0,ws,cslint); } } /* apply abc */ if (dabc) { if (hybrid) apply_abc(u0,u1,nz,nx,ny,nbd,abc,nop,damp); else { abcone3d_apply(u0,u1,nop,abc,fdm); sponge3d_apply(u0,spo,fdm); sponge3d_apply(u1,spo,fdm); } } /* loop over pointers */ ptr_tmp = u0; u0 = u1; u1 = ptr_tmp; /* extract snapshot */ if (snap && it%jsnap==0) { int fy = (floor)((sf_o(acy)-fdm->oypad)/fdm->dy); int jy = floor(sf_d(acy)/fdm->dy); float **ptr_slice; for (iy=0; iy<sf_n(acy); iy++) { ptr_slice = u0[fy+iy*jy]; cut3d_slice(ptr_slice,oslice,fdm,acz,acx); sf_floatwrite(oslice[0],sf_n(acz)*sf_n(acx),file_wfl); } } /* extract receiver data */ if (sinc) sinc3d_extract(u0,u_dat,crsinc); else lint3d_extract(u0,u_dat,crlint); sf_floatwrite(u_dat,nr,file_dat); #if defined _OPENMP && _DEBUG toc=omp_get_wtime(); fprintf(stderr,"%5.2gs",(float)(toc-tic)); #endif } #ifdef _OPENMP wall_clock_time_e = omp_get_wtime(); #else wall_clock_time_e = (double) clock() / CLOCKS_PER_SEC; #endif if (verb) fprintf(stderr,"\nElapsed time: %lf s\n",wall_clock_time_e-wall_clock_time_s); free(**u0); free(*u0); free(u0); free(**u1); free(*u1); free(u1); free(**vel); free(*vel); free(vel); free(u_dat); free(ws); free(fdcoef_d2); free(fdcoef_d1); if (snap) { free(*oslice); free(oslice); } if(!cden) { free(**rho); free(*rho); free(rho); } if (hybrid) free(damp); free(src3d); free(rec3d); return 0; }
int main (int argc, char *argv[]) { bool verb; /* verbosity */ int ompnth=1; /* number of threads */ bool down; /* adjoint flag */ bool causal; sf_file Fslo=NULL; /* slowness file S(nlx,nly,nz ) */ sf_file Fsou=NULL; /* data file Ds(nmx,nmy, nw) */ sf_file Fwfl=NULL; /* data file Dr(nmx,nmy, nw) */ weicub3d cub; /* hypercube */ weitap3d tap; /* tapering */ weissr3d ssr; /* SSR operator */ weislo3d slo; /* slowness */ weiop3d weop; /* WEI operator */ /*------------------------------------------------------------*/ sf_init(argc,argv); #ifdef _OPENMP ompnth=omp_init(); /* OMP parameters */ #endif if (!sf_getbool( "verb",&verb )) verb = false; /* verbosity flag */ if (!sf_getbool( "down",&down )) down = true; /* up/down flag */ if (!sf_getbool("causal",&causal)) sf_error("Specify causal!"); /* causality flag */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init cube..."); cub = wei_cube(verb,ompnth); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ Fslo = sf_input ("slo"); if (SF_FLOAT !=sf_gettype(Fslo)) sf_error("Need float slowness"); weislo_inp(cub,Fslo); /* input slowness */ /*------------------------------------------------------------*/ Fsou = sf_input ( "in"); if (SF_COMPLEX !=sf_gettype(Fsou)) sf_error("Need complex sdat"); weiwfl_inp(cub,Fsou); /* input adjoint source dimensions */ /*------------------------------------------------------------*/ Fwfl = sf_output("out"); sf_settype(Fwfl,SF_COMPLEX); /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init slo..."); slo = weislo_init(cub,Fslo); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init tap..."); tap = weitap_init(cub); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init ssr..."); ssr = weissr_init(cub,slo); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"init weop..."); weop = weiwfl_init(cub); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ adjwfl(weop,cub,ssr,tap,slo,Fsou,Fwfl,down,causal); weiwfl_close(weop); /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"close structures..."); weislo_close(slo); weissr_close(cub,ssr); weitap_close(tap); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* close files */ if (Fsou!=NULL) sf_fileclose(Fsou); if (Fwfl!=NULL) sf_fileclose(Fwfl); if (Fslo!=NULL) sf_fileclose(Fslo); /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { bool verb; bool adj; bool anis; sf_file Fcip=NULL; /* lag-domain CIPs */ sf_file Fang=NULL; /* angle-domain CIPs */ sf_file Fvel=NULL; /* velocity @ CIPs */ sf_file Fnor=NULL; /* normals @ CIPs */ sf_file Ftlt=NULL; /* tilt @ CIPs */ sf_file Fani=NULL; /* anisotropy @ CIPs */ sf_axis ahx,ahy,ahz,aht,ac,ath,aph,aps,aj; int ihx,ihy,ihz,iht,ic,ith,iph; /* angle parameters */ int nth,nph,nps,nhx,nhy,nhz,nht; float oth,oph,ops,ohx,ohy,ohz,oht; float dth,dph,dps,dhx,dhy,dhz,dht; float phi; float tht; float psi; float v_s,v_r; float cosum,codif,sitovel; /* arrays 1 2 3 4 */ float ****cip; /* nhx-nhy-nhz-nht */ float **ang; /* nph-nth */ float *vep; /* nc */ float *ves; /* nc */ float *eps=NULL; /* nc */ float *dlt=NULL; /* nc */ vc3d vv; /* azimuth reference vector */ vc3d *nn; /* normal vectors */ vc3d *tt=NULL; /* tilt vectors */ vc3d *aa; /* in-plane reference vector */ vc3d qq; vc3d jk; /* temp vector */ float hx,hy,hz; float tau; /* time lag */ int jht; /* tau axis index */ float fht; /* tau axis weight */ float ssn; /* slant-stack normalization */ float *ttipar; /*-----------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); #ifdef _OPENMP omp_init(); /* OMP parameters */ #endif if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("anis",&anis)) anis=false; /* anisotropy flag */ if(! sf_getbool("adj", &adj)) adj=true; /* adj flag */ /* * ADJ: cip to ang * FOR: ang to cip */ sf_warning("verb=%d",verb); sf_warning("anis=%d",anis); /* select anisotropy model */ if(anis) sf_warning("ANI model"); else sf_warning("ISO model"); if(adj) { Fcip=sf_input ( "in"); /* CIP file */ Fang=sf_output("out"); /* ANG file */ } else { Fcip=sf_output("out"); /* CIP file */ Fang=sf_input ("in"); /* ANG file */ } Fvel=sf_input ("vel"); /* velocity file */ Fnor=sf_input ("nor"); /* normal vectors */ if(anis) { Ftlt=sf_input ("tlt"); /* tilt vectors */ Fani=sf_input ("ani"); /* anisotropy */ } aj = sf_maxa(1,0,1); if(adj) { /* input axes */ ahx = sf_iaxa(Fcip,1); sf_setlabel(ahx,"hx"); ahy = sf_iaxa(Fcip,2); sf_setlabel(ahy,"hy"); ahz = sf_iaxa(Fcip,3); sf_setlabel(ahz,"hz"); aht = sf_iaxa(Fcip,4); sf_setlabel(aht,"ht"); /* CIP axis */ ac = sf_iaxa(Fcip,5); sf_setlabel(ac ,"c "); /* reflection angle */ if(! sf_getint ("nth",&nth)) nth=90; if(! sf_getfloat("oth",&oth)) oth=0; if(! sf_getfloat("dth",&dth)) dth=1.; ath = sf_maxa(nth,oth,dth); sf_setlabel(ath,"th"); sf_setunit (ath,"deg"); /* azimuth angle */ if(! sf_getint ("nph",&nph)) nph=360; if(! sf_getfloat("oph",&oph)) oph=-180; if(! sf_getfloat("dph",&dph)) dph=1.; aph = sf_maxa(nph,oph,dph); sf_setlabel(aph,"ph"); sf_setunit (aph,"deg"); /* output axes */ sf_oaxa(Fang,ath,1); sf_oaxa(Fang,aph,2); sf_oaxa(Fang,ac ,3); sf_oaxa(Fang,aj ,4); sf_oaxa(Fang,aj ,5); } else { /* lag in x */ if(! sf_getint ("nhx",&nhx)) nhx=1; if(! sf_getfloat("ohx",&ohx)) ohx=0; if(! sf_getfloat("dhx",&dhx)) dhx=1.; ahx = sf_maxa(nhx,ohx,dhx); sf_setlabel(ahx,"hx"); sf_setunit (ahx,""); /* lag in y */ if(! sf_getint ("nhy",&nhy)) nhy=1; if(! sf_getfloat("ohy",&ohy)) ohy=0; if(! sf_getfloat("dhy",&dhy)) dhy=1.; ahy = sf_maxa(nhy,ohy,dhy); sf_setlabel(ahy,"hy"); sf_setunit (ahy,""); /* lag in z */ nhz=1; ohz=0.; dhz=1.; ahz = sf_maxa(nhz,ohz,dhz); sf_setlabel(ahz,"hz"); sf_setunit (ahz,""); /* lag in t */ if(! sf_getint ("nht",&nht)) nht=1; if(! sf_getfloat("oht",&oht)) oht=0.; if(! sf_getfloat("dht",&dht)) dht=1.; aht = sf_maxa(nht,oht,dht); sf_setlabel(aht,"ht"); sf_setunit (aht,""); /* reflection angle */ ath = sf_iaxa(Fang,1); sf_setlabel(ath,"th"); /* azimuth angle */ aph = sf_iaxa(Fang,2); sf_setlabel(aph,"ph"); /* CIP axis */ ac = sf_iaxa(Fang,3); sf_setlabel(ac ,"c "); /* output axes */ sf_oaxa(Fcip,ahx,1); sf_oaxa(Fcip,ahy,2); sf_oaxa(Fcip,ahz,3); sf_oaxa(Fcip,aht,4); sf_oaxa(Fcip,ac ,5); } if (verb){ sf_raxa(ahx); sf_raxa(ahy); sf_raxa(ahz); sf_raxa(aht); sf_raxa(ac); sf_raxa(ath); sf_raxa(aph); } if(anis) { /* deviation angle */ if(! sf_getint ("nps",&nps)) nps=251; if(! sf_getfloat("ops",&ops)) ops=-25; if(! sf_getfloat("dps",&dps)) dps=0.2; aps = sf_maxa(nps,ops,dps); sf_setlabel(aps,"ps"); sf_setunit (aps,"deg"); if(verb) sf_raxa(aps); } else { aps = NULL; } /*------------------------------------------------------------*/ /* allocate arrays */ cip = sf_floatalloc4 (sf_n(ahx),sf_n(ahy),sf_n(ahz),sf_n(aht)); ang = sf_floatalloc2 (sf_n(ath),sf_n(aph)); /* read velocity */ vep = sf_floatalloc (sf_n(ac)); sf_floatread(vep,sf_n(ac),Fvel); ves = sf_floatalloc (sf_n(ac)); sf_floatread(ves,sf_n(ac),Fvel); /*------------------------------------------------------------*/ /* read normals */ nn = (vc3d*) sf_alloc(sf_n(ac),sizeof(*nn)); /* normals */ vc3dread1(Fnor,nn,sf_n(ac)); if(anis) { /* read anisotropy */ eps = sf_floatalloc (sf_n(ac)); sf_floatread(eps,sf_n(ac),Fani); dlt = sf_floatalloc (sf_n(ac)); sf_floatread(dlt,sf_n(ac),Fani); /* read tilts */ tt = (vc3d*) sf_alloc(sf_n(ac),sizeof(*tt)); vc3dread1(Ftlt,tt,sf_n(ac)); } /*------------------------------------------------------------*/ /* in-plane azimuth reference */ vv.dx=1; vv.dy=0; vv.dz=0; aa = (vc3d*) sf_alloc(sf_n(ac),sizeof(*aa)); for(ic=0;ic<sf_n(ac);ic++) { jk =vcp3d(&nn[ic],&vv); aa[ic]=vcp3d(&jk,&nn[ic]); } /*------------------------------------------------------------*/ ssn = 1./sqrt(sf_n(ahx)*sf_n(ahy)*sf_n(ahz)); /*------------------------------------------------------------*/ /* loop over CIPs */ /* if(verb) fprintf(stderr,"ic\n");*/ for(ic=0;ic<sf_n(ac);ic++) { /* if(verb) fprintf(stderr,"\b\b\b\b\b%d",ic);*/ if(adj) { /* read CIP */ sf_floatread(cip[0][0][0],sf_n(ahx)*sf_n(ahy)*sf_n(ahz)*sf_n(aht),Fcip); /* init ANG */ for (iph=0;iph<sf_n(aph);iph++) { for(ith=0;ith<sf_n(ath);ith++) { ang[iph][ith]=0; } } } else { /* init CIP */ for (iht=0;iht<sf_n(aht);iht++) { for (ihz=0;ihz<sf_n(ahz);ihz++) { for (ihy=0;ihy<sf_n(ahy);ihy++) { for(ihx=0;ihx<sf_n(ahx);ihx++) { cip[iht][ihz][ihy][ihx]=0; } } } } /* read ANG */ sf_floatread(ang[0],sf_n(ath)*sf_n(aph),Fang); } /* phi loop */ nph = sf_n(aph); #ifdef _OPENMP #pragma omp parallel for schedule(static) \ private(iph,phi,jk,qq, \ ith,tht, \ ihy,ihx,hy,hx,hz, \ tau,jht,fht,cosum,codif,v_s,v_r,psi,sitovel) \ shared( nph,aph,ath,aps,ahy,ahx,aht,cip,ang,vep,ves,eps,dlt) #endif for(iph=0;iph<nph;iph++) { phi=(180+sf_o(aph)+iph*sf_d(aph))/180.*SF_PI; /* use '180' to reverse illumination direction: */ /* at a CIP, look toward the source */ /* reflection azimuth vector */ jk = rot3d(nn,aa,phi); qq = nor3d(&jk); /* theta loop */ for(ith=0;ith<sf_n(ath);ith++) { tht=(sf_o(ath)+ith*sf_d(ath))/180.*SF_PI; if(anis) { ttipar = psitti(nn,&qq,tt,aa, tht,phi,aps, vep[ic],ves[ic],eps[ic],dlt[ic]); psi = ttipar[0]; v_s = ttipar[1]; v_r = ttipar[2]; psi *= SF_PI/180.; cosum = cosf(tht+psi); codif = cosf(tht-psi); sitovel = sinf(2*tht)/(v_s*cosum + v_r*codif); } else { sitovel = sinf(tht)/vep[ic]; } /* lag loops */ if(adj) { for (ihy=0;ihy<sf_n(ahy);ihy++) { hy=sf_o(ahy)+ihy*sf_d(ahy); for(ihx=0;ihx<sf_n(ahx);ihx++) { hx=sf_o(ahx)+ihx*sf_d(ahx); hz = -(hx*(nn[ic].dx)+hy*(nn[ic].dy))/(nn[ic].dz); tau = -((qq.dx)*hx+(qq.dy)*hy+(qq.dz)*hz)*sitovel; jht=0.5+(tau-sf_o(aht))/sf_d(aht); if(jht>=0 && jht<sf_n(aht)-1) { fht= (tau-sf_o(aht))/sf_d(aht)-jht; ang[iph][ith] += (1-fht)*ssn*cip[jht ][0][ihy][ihx] + fht *ssn*cip[jht+1][0][ihy][ihx]; } } /* hx */ } /* hy */ } else { for (ihy=0;ihy<sf_n(ahy);ihy++) { hy=sf_o(ahy)+ihy*sf_d(ahy); for(ihx=0;ihx<sf_n(ahx);ihx++) { hx=sf_o(ahx)+ihx*sf_d(ahx); hz = -(hx*(nn[ic].dx)+hy*(nn[ic].dx))/(nn[ic].dz); tau = -((qq.dx)*hx+(qq.dy)*hy+(qq.dz)*hz)*sitovel; jht=0.5+(tau-sf_o(aht))/sf_d(aht); if(jht>=0 && jht<sf_n(aht)-1) { fht= (tau-sf_o(aht))/sf_d(aht)-jht; cip[jht ][0][ihy][ihx] += (1-fht)*ssn*ang[iph][ith]; cip[jht+1][0][ihy][ihx] += fht *ssn*ang[iph][ith]; } } /* hx */ } /* hy */ } } /* th */ } /* ph */ if(adj) { /* write ANG */ sf_floatwrite(ang[0],sf_n(ath)*sf_n(aph),Fang); } else { /* write CIP */ sf_floatwrite(cip[0][0][0],sf_n(ahx)*sf_n(ahy)*sf_n(ahz)*sf_n(aht),Fcip); } } if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"free memory..."); free(***cip);free(**cip);free(*cip);free(cip); ; free(*ang);free(ang); ; free(vep); ; free (nn); ; free (aa); if(anis) { free(ves); free(eps); free(dlt); free(tt); } if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ exit(0); }
int main(int argc, char* argv[]) { /* Laplacian coefficients */ float c0=-30./12.,c1=+16./12.,c2=- 1./12.; bool verb,free, ifoneway, ifsponge; /* verbose flag */ sf_file Fw=NULL,Fv=NULL,Fr=NULL,Fo=NULL; /* I/O files */ sf_axis at,az,ax; /* cube axes */ int it,iz,ix,nb; /* index variables */ int nt,nz,nx; float dt,dz,dx,idx,idz,dt2; float *ww,**vv,**rr; /* I/O arrays*/ float **um,**uo,**up,**ud;/* tmp arrays */ #ifdef _OPENMP /* Testing for OpenMP */ double start_time, end_time; #endif sf_init(argc,argv); /* OMP parameters */ #ifdef _OPENMP omp_init(); #endif if(! sf_getbool("verb",&verb)) verb=0; if(! sf_getbool("free",&free)) free=false; if(! sf_getbool("ifoneway",&ifoneway)) ifoneway=true; if(! sf_getbool("ifsponge",&ifsponge)) ifsponge=true; if(! sf_getint("nb",&nb)) nb=5; /* setup I/O files */ Fw = sf_input ("in" ); Fo = sf_output("out"); Fv = sf_input ("vel"); Fr = sf_input ("ref"); /* Read/Write axes */ at = sf_iaxa(Fw,1); nt = sf_n(at); dt = sf_d(at); az = sf_iaxa(Fv,1); nz = sf_n(az); dz = sf_d(az); ax = sf_iaxa(Fv,2); nx = sf_n(ax); dx = sf_d(ax); sf_oaxa(Fo,az,1); sf_oaxa(Fo,ax,2); sf_oaxa(Fo,at,3); dt2 = dt*dt; idz = 1/(dz*dz); idx = 1/(dx*dx); /* read wavelet, velocity & reflectivity */ ww=sf_floatalloc(nt); sf_floatread(ww ,nt ,Fw); vv=sf_floatalloc2(nz,nx); sf_floatread(vv[0],nz*nx,Fv); rr=sf_floatalloc2(nz,nx); sf_floatread(rr[0],nz*nx,Fr); /* allocate temporary arrays */ um=sf_floatalloc2(nz,nx); uo=sf_floatalloc2(nz,nx); up=sf_floatalloc2(nz,nx); ud=sf_floatalloc2(nz,nx); for (iz=0; iz<nz; iz++) { for (ix=0; ix<nx; ix++) { um[ix][iz]=0; uo[ix][iz]=0; up[ix][iz]=0; ud[ix][iz]=0; } } /* MAIN LOOP */ if(verb) fprintf(stderr,"\n"); /* Starting timer */ #ifdef _OPENMP start_time = omp_get_wtime(); #endif for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",it); /* 4th order laplacian */ if(ifoneway) { #ifdef _OPENMP #pragma omp parallel for default(none) \ private(ix,iz) \ shared(ud,nb,uo,c0,c1,c2,nx,nz,idx,idz) #endif for (iz=nb; iz<nz-nb; iz++) { for (ix=nb; ix<nx-nb; ix++) { ud[ix][iz] = c0* uo[ix ][iz ] * (idx+idz) + c1*(uo[ix-1][iz ] + uo[ix+1][iz ])*idx + c2*(uo[ix-2][iz ] + uo[ix+2][iz ])*idx + c1*(uo[ix ][iz-1] + uo[ix ][iz+1])*idz + c2*(uo[ix ][iz-2] + uo[ix ][iz+2])*idz; } } }else { #ifdef _OPENMP #pragma omp parallel for default(none) \ private(ix,iz) \ shared(ud,uo,c0,c1,c2,nx,nz,idx,idz) #endif for (iz=2; iz<nz-2; iz++) { for (ix=2; ix<nx-2; ix++) { ud[ix][iz] = c0* uo[ix ][iz ] * (idx+idz) + c1*(uo[ix-1][iz ] + uo[ix+1][iz ])*idx + c2*(uo[ix-2][iz ] + uo[ix+2][iz ])*idx + c1*(uo[ix ][iz-1] + uo[ix ][iz+1])*idz + c2*(uo[ix ][iz-2] + uo[ix ][iz+2])*idz; } } } /* inject wavelet */ #ifdef _OPENMP #pragma omp parallel for default(none) \ private(ix,iz) \ shared(nx,nz,ww,rr,ud,it) #endif for (iz=0; iz<nz; iz++) { for (ix=0; ix<nx; ix++) { ud[ix][iz] -= ww[it] * rr[ix][iz]; } } /* scale by velocity */ #ifdef _OPENMP #pragma omp parallel for default(none) \ private(ix,iz) \ shared(ud,vv,nx,nz) #endif for (iz=0; iz<nz; iz++) { for (ix=0; ix<nx; ix++) { ud[ix][iz] *= vv[ix][iz]*vv[ix][iz]; } } /* time step */ #ifdef _OPENMP #pragma omp parallel for default(none) \ private(ix,iz) \ shared(up,uo,um,ud,nx,nz,dt2) #endif for (iz=0; iz<nz; iz++) { for (ix=0; ix<nx; ix++) { up[ix][iz] = 2*uo[ix][iz] - um[ix][iz] + ud[ix][iz] * dt2; um[ix][iz] = uo[ix][iz]; uo[ix][iz] = up[ix][iz]; } } /* one-way abc apply */ if(ifoneway) {oneway_abc(uo,um,vv,nx,nz,nb,dx,dz,dt,free);} if(ifsponge) {sponge_abc(um,nx,nz,nb); sponge_abc(uo,nx,nz,nb);} /* write wavefield to output */ sf_floatwrite(uo[0],nz*nx,Fo); } /* Ending timer */ #ifdef _OPENMP end_time = omp_get_wtime(); sf_warning("Elapsed time is %f.",end_time-start_time); #endif if(verb) fprintf(stderr,"\n"); sf_close(); exit (0); }
int main(int argc, char* argv[]) { bool verb,fsrf,snap,expl,dabc,sout,uses; int jsnap,ntsnap,jdata; char *atype; #ifdef _OPENMP int ompnth=1; #endif /* I/O files */ sf_file Fwav=NULL; /* wavelet */ sf_file Fsou=NULL; /* sources */ sf_file Frec=NULL; /* receivers */ sf_file Fvel=NULL; /* velocity */ sf_file Fang=NULL; /* angles */ sf_file Fdat=NULL; /* data */ sf_file Fwfl=NULL; /* wavefield */ /* cube axes */ sf_axis at,az,ax; sf_axis as,ar; int nt,nz,nx,ns,nr,nb; int it,iz,ix; float dt,dz,dx,dt2; /* FDM structure */ fdm2d fdm=NULL; abcone2d abc=NULL; sponge spo=NULL; /* I/O arrays */ float *ww=NULL; /* wavelet */ pt2d *ss=NULL; /* sources */ pt2d *rr=NULL; /* receivers */ float *dd=NULL; /* data */ float **tt=NULL; float **vp=NULL; /* velocity */ float **vpn=NULL; float **vpz=NULL; float **vpx=NULL; float **vsz=NULL; float **tht=NULL,**sit=NULL,**cot=NULL; float st,ct; float **pm=NULL,**po=NULL,**pp=NULL,**pa=NULL,**pt=NULL; /* main wavefield */ float **qm=NULL,**qo=NULL,**qp=NULL,**qa=NULL,**qt=NULL; /* auxiliary wavefield */ float **sf=NULL; /* "stress" field */ /* linear inteppolation weights/indices */ lint2d cs,cr; /* FD operator size */ float cox,cax,cbx,c1x,c2x; float coz,caz,cbz,c1z,c2z; /* wavefield cut params */ sf_axis acz=NULL,acx=NULL; int nqz,nqx; float oqz,oqx; float dqz,dqx; float **pc=NULL; float H1p,H2p,H1q,H2q; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /* select anisotropy model */ if (NULL == (atype = sf_getstring("atype"))) atype = "i"; switch(atype[0]) { case 't': sf_warning("TTI model"); break; case 'v': sf_warning("VTI model"); break; case 'i': default: sf_warning("ISO model"); break; } /*------------------------------------------------------------*/ /* OMP parameters */ #ifdef _OPENMP ompnth=omp_init(); #endif /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity */ if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots */ if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface */ if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */ if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */ if(! sf_getbool("sout",&sout)) sout=false; /* stress output */ if(! sf_getbool("uses",&uses)) uses=false; /* use vsz */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* I/O files */ Fwav = sf_input ("in" ); /* wavelet */ Fvel = sf_input ("vel"); /* velocity */ Fsou = sf_input ("sou"); /* sources */ Frec = sf_input ("rec"); /* receivers */ Fang = sf_input ("ang"); /* angles */ Fwfl = sf_output("wfl"); /* wavefield */ Fdat = sf_output("out"); /* data */ /*------------------------------------------------------------*/ /* axes */ at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(Fvel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(Fvel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */ as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */ ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ns = sf_n(as); nr = sf_n(ar); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* other execution parameters */ if(! sf_getint("jdata",&jdata)) jdata=1; if(snap) { /* save wavefield every *jsnap* time steps */ if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* setup output data header */ sf_oaxa(Fdat,ar,1); sf_setn(at,nt/jdata); sf_setd(at,dt*jdata); sf_oaxa(Fdat,at,2); /* setup output wavefield header */ if(snap) { if(!sf_getint ("nqz",&nqz)) nqz=sf_n(az); if(!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); dqz=sf_d(az); dqx=sf_d(ax); acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx); /* check if the imaging window fits in the wavefield domain */ pc=sf_floatalloc2(sf_n(acz),sf_n(acx)); ntsnap=0; for(it=0; it<nt; it++) { if(it%jsnap==0) ntsnap++; } sf_setn(at, ntsnap); sf_setd(at,dt*jsnap); if(verb) sf_raxa(at); sf_oaxa(Fwfl,acz,1); sf_oaxa(Fwfl,acx,2); sf_oaxa(Fwfl,at, 3); } /*------------------------------------------------------------*/ /* expand domain for FD operators and ABC */ if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP; fdm=fdutil_init(verb,fsrf,az,ax,nb,1); sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax); /*------------------------------------------------------------*/ if(expl) { ww = sf_floatalloc( 1); } else { ww = sf_floatalloc(ns); } dd = sf_floatalloc(nr); /*------------------------------------------------------------*/ /* setup source/receiver coordinates */ ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */ pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */ cs = lint2d_make(ns,ss,fdm); cr = lint2d_make(nr,rr,fdm); /*------------------------------------------------------------*/ /* setup FD coefficients */ cox = C0 / (dx*dx); cax = CA / (dx*dx); cbx = CB / (dx*dx); c1x = C1 / dx; c2x = C2 / dx; coz = C0 / (dz*dz); caz = CA / (dz*dz); cbz = CB / (dz*dz); c1z = C1 / dz; c2z = C2 / dz; /* precompute dt^2*/ dt2 = dt*dt; /*------------------------------------------------------------*/ tt = sf_floatalloc2(nz,nx); /*------------------------------------------------------------*/ /* input velocity */ vp =sf_floatalloc2(fdm->nzpad,fdm->nxpad); vpz =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vpz,fdm); /* VPz */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { vp [ix][iz] = vpz[ix][iz]; vpz[ix][iz] = vpz[ix][iz] * vpz[ix][iz]; } } if(fsrf) { /* free surface */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nb; iz++) { vpz[ix][iz]=0; } } } if(atype[0] != 'i') { vpn =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vpn,fdm); /* VPn */ vpx =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vpx,fdm); /* VPx */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { vpn[ix][iz] = vpn[ix][iz] * vpn[ix][iz]; vpx[ix][iz] = vpx[ix][iz] * vpx[ix][iz]; } } if(fsrf) { /* free surface */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nb; iz++) { vpn[ix][iz]=0; vpx[ix][iz]=0; } } } if(uses) { vsz =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vsz,fdm); /* VSz */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { vsz[ix][iz] = vsz[ix][iz] * vsz[ix][iz]; } } } } /*------------------------------------------------------------*/ if( atype[0]=='t') { /* input tilt angle */ tht =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sit =sf_floatalloc2(fdm->nzpad,fdm->nxpad); cot =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sf_floatread(tt[0],nz*nx,Fang); expand(tt,tht,fdm); for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { tht[ix][iz] *= SF_PI/180.; sit[ix][iz] = sinf(tht[ix][iz]); cot[ix][iz] = cosf(tht[ix][iz]); } } free(*tht); free(tht); } /*------------------------------------------------------------*/ free(*tt); free(tt); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* allocate wavefield arrays */ pm=sf_floatalloc2(fdm->nzpad,fdm->nxpad); po=sf_floatalloc2(fdm->nzpad,fdm->nxpad); pp=sf_floatalloc2(fdm->nzpad,fdm->nxpad); pa=sf_floatalloc2(fdm->nzpad,fdm->nxpad); for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { pm[ix][iz]=0; po[ix][iz]=0; pp[ix][iz]=0; pa[ix][iz]=0; } } if(atype[0] != 'i') { qm=sf_floatalloc2(fdm->nzpad,fdm->nxpad); qo=sf_floatalloc2(fdm->nzpad,fdm->nxpad); qp=sf_floatalloc2(fdm->nzpad,fdm->nxpad); qa=sf_floatalloc2(fdm->nzpad,fdm->nxpad); for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { qm[ix][iz]=0; qo[ix][iz]=0; qp[ix][iz]=0; qa[ix][iz]=0; } } if(sout) sf=sf_floatalloc2(fdm->nzpad,fdm->nxpad); } /*------------------------------------------------------------*/ if(dabc) { abc = abcone2d_make(NOP,dt,vp,fsrf,fdm); /* one-way */ spo = sponge_make(fdm->nb); /* sponge */ } /*------------------------------------------------------------*/ /* * MAIN LOOP */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",it); /* compute acceleration */ switch(atype[0]) { case 't': if(uses) { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz,H1p,H2p,H1q,H2q,st,ct) \ shared(fdm,pa,po,qa,qo, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z, \ vpn,vpz,vpx,vsz, \ sit,cot) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { st=sit[ix][iz]; ct=cot[ix][iz]; H1p = H1(po,ix,iz, \ st,ct, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z); H2p = H2(po,ix,iz, \ st,ct, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z); H1q = H1(qo,ix,iz, \ st,ct, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z); H2q = H2(qo,ix,iz, \ st,ct, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z); /* p - main field */ pa[ix][iz] = H1p * vsz[ix][iz] + H2p * vpx[ix][iz] + H1q * vpz[ix][iz] - H1q * vsz[ix][iz]; /* q - auxiliary field */ qa[ix][iz] = H2p * vpn[ix][iz] - H2p * vsz[ix][iz] + H1q * vpz[ix][iz] + H2q * vsz[ix][iz]; } } } else { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz,H2p,H1q,st,ct) \ shared(fdm,pa,po,qa,qo, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z, \ vpn,vpz,vpx, \ sit,cot) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { st=sit[ix][iz]; ct=cot[ix][iz]; H2p = H2(po,ix,iz, \ st,ct, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z); H1q = H1(qo,ix,iz, \ st,ct, \ cox,cax,cbx,c1x,c2x, \ coz,caz,cbz,c1z,c2z); /* p - main field */ pa[ix][iz] = H2p * vpx[ix][iz] + H1q * vpz[ix][iz]; /* q - auxiliary field */ qa[ix][iz] = H2p * vpn[ix][iz] + H1q * vpz[ix][iz]; } } } break; case 'v': if(uses) { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz,H1p,H2p,H1q,H2q) \ shared(fdm,pa,po,qa,qo, \ cox,cax,cbx, \ coz,caz,cbz, \ vpn,vpz,vpx,vsz) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { H1p = Dzz(po,ix,iz,coz,caz,cbz); H1q = Dzz(qo,ix,iz,coz,caz,cbz); H2p = Dxx(po,ix,iz,cox,cax,cbx); H2q = Dxx(qo,ix,iz,cox,cax,cbx); /* p - main field */ pa[ix][iz] = H1p * vsz[ix][iz] + H2p * vpx[ix][iz] + H1q * vpz[ix][iz] - H1q * vsz[ix][iz]; /* q - auxiliary field */ qa[ix][iz] = H2p * vpn[ix][iz] - H2p * vsz[ix][iz] + H1q * vpz[ix][iz] + H2q * vsz[ix][iz]; } } } else { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz,H2p,H1q) \ shared(fdm,pa,po,qa,qo, \ cox,cax,cbx, \ coz,caz,cbz, \ vpn,vpx,vpz) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { H1q = Dzz(qo,ix,iz,coz,caz,cbz); H2p = Dxx(po,ix,iz,cox,cax,cbx); /* p - main field */ pa[ix][iz] = H2p * vpx[ix][iz] + H1q * vpz[ix][iz]; /* q - auxiliary field */ qa[ix][iz] = H2p * vpn[ix][iz] + H1q * vpz[ix][iz]; } } } break; case 'i': default: #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,pa,po, \ cox,cax,cbx, \ coz,caz,cbz, \ vpz) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { pa[ix][iz] = ( Dxx(po,ix,iz,cox,cax,cbx) + Dzz(po,ix,iz,coz,caz,cbz) ) * vpz[ix][iz]; } } break; } /* inject acceleration source */ if(expl) { sf_floatread(ww, 1,Fwav); ; lint2d_inject1(pa,ww[0],cs); if(atype[0] != 'i') lint2d_inject1(qa,ww[0],cs); } else { sf_floatread(ww,ns,Fwav); ; lint2d_inject(pa,ww,cs); if(atype[0] != 'i') lint2d_inject(qa,ww,cs); } /* step forward in time */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,pa,po,pm,pp,dt2) #endif for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { pp[ix][iz] = 2*po[ix][iz] - pm[ix][iz] + pa[ix][iz] * dt2; } } /* circulate wavefield arrays */ pt=pm; pm=po; po=pp; pp=pt; if(atype[0] != 'i') { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,qa,qo,qm,qp,dt2) #endif for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { qp[ix][iz] = 2*qo[ix][iz] - qm[ix][iz] + qa[ix][iz] * dt2; } } /* circulate wavefield arrays */ qt=qm; qm=qo; qo=qp; qp=qt; } /* one-way abc apply */ if(dabc) { abcone2d_apply(po,pm,NOP,abc,fdm); sponge2d_apply(pm,spo,fdm); sponge2d_apply(po,spo,fdm); if(atype[0] != 'i') { abcone2d_apply(qo,qm,NOP,abc,fdm); sponge2d_apply(qm,spo,fdm); sponge2d_apply(qo,spo,fdm); } } /* compute stress */ if(sout && (atype[0] != 'i')) { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,po,qo,sf) #endif for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { sf[ix][iz] = po[ix][iz] + qo[ix][iz]; } } } /* extract data at receivers */ if(sout && (atype[0] != 'i')) {lint2d_extract(sf,dd,cr); } else { lint2d_extract(po,dd,cr);} if(it%jdata==0) sf_floatwrite(dd,nr,Fdat); /* extract wavefield in the "box" */ if(snap && it%jsnap==0) { if(sout && (atype[0] != 'i')) {cut2d(sf,pc,fdm,acz,acx); } else { cut2d(po,pc,fdm,acz,acx);} sf_floatwrite(pc[0],sf_n(acz)*sf_n(acx),Fwfl); } } if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /* deallocate arrays */ free(*pm); free(pm); free(*pp); free(pp); free(*po); free(po); free(*pa); free(pa); free(*pc); free(pc); free(*vp); free(vp); free(*vpz); free(vpz); if(atype[0] != 'i') { free(*qm); free(qm); free(*qp); free(qp); free(*qo); free(qo); free(*qa); free(qa); free(*vpn); free(vpn); free(*vpx); free(vpx); if(uses){ free(*vsz); free(vsz); } if(sout){ free(*sf); free(sf); } } if(atype[0] == 't') { free(*sit); free(sit); free(*cot); free(cot); } free(ww); free(ss); free(rr); free(dd); /*------------------------------------------------------------*/ exit (0); }
int main (int argc, char *argv[]) { bool verb; /* verbosity */ int ompnth=1; /* number of threads */ bool conj; /* complex conjugate flag */ char *irun; /* I run... */ sf_file Faso=NULL; /* output wavefield wfl(x,y,z,w) */ sf_file Fbwf=NULL; /* input wavefield wfl(x,y,z,w) */ sf_file Feic=NULL; /* EIC image file R(hx,hy,hz,tt,c) */ sf_file Fcoo=NULL; /* coord file C(nc) */ weicub3d cub; /* hypercube */ weiop3d weop; /* WEI operator */ weico3d eico; /* eic coordinates */ /*------------------------------------------------------------*/ sf_init(argc,argv); #ifdef _OPENMP ompnth=omp_init(); /* OMP parameters */ #endif if (!sf_getbool( "verb",&verb )) verb = false; /* verbosity flag */ if (!sf_getbool( "conj",&conj )) sf_error("Specify whether complex conjugate!"); /* flag */ if (NULL == (irun = sf_getstring("irun"))) irun = "e"; if(verb) fprintf(stderr,"init cube..."); cub = wei_cube(verb,ompnth); if(verb) fprintf(stderr,"OK\n"); /*------------------------------------------------------------*/ Fbwf = sf_input("bwf"); if (SF_COMPLEX !=sf_gettype(Fbwf)) sf_error("Need complex wavefield"); weiwfl_inp(cub,Fbwf); /* input data dimensions */ /*------------------------------------------------------------*/ Feic = sf_input( "in"); /*------------------------------------------------------------*/ Fcoo = sf_input ("coo"); /* input coordinates for EIC */ if (SF_FLOAT !=sf_gettype(Fcoo)) sf_error("Need float coordinates"); /* load EIC dimensions from file */ mvahic_inp(cub,Feic,Fcoo); eico = weicoo_init(cub,Fcoo); /*------------------------------------------------------------*/ Faso = sf_output("out"); sf_settype(Faso,SF_COMPLEX); weiwfl_out(cub,Faso); /*------------------------------------------------------------*/ switch(irun[0]) { case 'h': /* HIC: hx-hy-1 */ if(verb) sf_warning("HIC AJS"); weop = weihicajs_init(cub); weihicajs(weop,cub,eico,Feic,Fbwf,Faso,conj); weihicajs_close(weop); break; case 'e': /* EIC hx-hy-hz */ default: if(verb) sf_warning("EIC AJS"); adjsou(cub,eico,Feic,Fbwf,Faso,conj); break; } weicoo_close(eico); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* close files */ if (Fbwf!=NULL) sf_fileclose(Fbwf); if (Faso!=NULL) sf_fileclose(Faso); if (Fcoo!=NULL) sf_fileclose(Fcoo); if (Feic!=NULL) sf_fileclose(Feic); /*------------------------------------------------------------*/ exit (0); }
void prtm2d_init(bool verb_, bool csdgather_, float dz_, float dx_, float dt_, float amp, float fm, int nz_, int nx_, int nb_, int nt_, int ns_, int ng_, int sxbeg_, int szbeg_, int jsx_, int jsz_, int gxbeg_, int gzbeg_, int jgx_, int jgz_, float **v0, float *mod_, float *dat_) /*< allocate variables and initialize parameters >*/ { #ifdef _OPENMP omp_init(); /* initialize OpenMP support */ #endif float t; int i1, i2, it,ib; t = 1.0/(dz_*dz_); c11 = 4.0*t/3.0; c12= -t/12.0; t = 1.0/(dx_*dx_); c21 = 4.0*t/3.0; c22= -t/12.0; c0=-2.0*(c11+c12+c21+c22); verb=verb_; csdgather=csdgather_; ns=ns_; ng=ng_; nb=nb_; nz=nz_; nx=nx_; nt=nt_; sxbeg=sxbeg_; szbeg=szbeg_; jsx=jsx_; jsz=jsz_; gxbeg=gxbeg_; gzbeg=gzbeg_; jgx=jgx_; jgz=jgz_; nzpad=nz+2*nb; nxpad=nx+2*nb; /* allocate temporary arrays */ bndr=sf_floatalloc(nb); sp0=sf_floatalloc2(nzpad,nxpad); sp1=sf_floatalloc2(nzpad,nxpad); gp0=sf_floatalloc2(nzpad,nxpad); gp1=sf_floatalloc2(nzpad,nxpad); vv=sf_floatalloc2(nzpad,nxpad); wlt=sf_floatalloc(nt); sxz=sf_intalloc(ns); gxz=sf_intalloc(ng); rwbndr=sf_floatalloc(nt*4*(nx+nz)); /* initialized sponge ABC coefficients */ for(ib=0;ib<nb;ib++){ t=0.015*(nb-1-ib); bndr[ib]=expf(-t*t); } mod=mod_; dat=dat_; for (i2=0; i2<nx; i2++) for (i1=0; i1<nz; i1++) { t=v0[i2][i1]*dt_; vv[i2+nb][i1+nb] = t*t; } for (i2=0; i2<nxpad; i2++) for (i1=0; i1<nb; i1++) { vv[i2][ i1 ] =vv[i2][ nb ]; vv[i2][nzpad-i1-1] =vv[i2][nzpad-nb-1]; } for (i2=0; i2<nb; i2++) for (i1=0; i1<nzpad; i1++) { vv[ i2 ][i1] =vv[ nb ][i1]; vv[nxpad-i2-1][i1] =vv[nxpad-nb-1][i1]; } for(it=0; it<nt; it++){ t=SF_PI*fm*(it*dt_-1.0/fm);t=t*t; wlt[it]=amp*(1.0-2.0*t)*expf(-t); } /* configuration of sources and geophones */ if (!(sxbeg>=0 && szbeg>=0 && sxbeg+(ns-1)*jsx<nx && szbeg+(ns-1)*jsz<nz)) { sf_warning("sources exceeds the computing zone!"); exit(1);} sg_init(sxz, szbeg, sxbeg, jsz, jsx, ns); distx=sxbeg-gxbeg; distz=szbeg-gzbeg; if (!(gxbeg>=0 && gzbeg>=0 && gxbeg+(ng-1)*jgx<nx && gzbeg+(ng-1)*jgz<nz)) { sf_warning("geophones exceeds the computing zone!"); exit(1);} if (csdgather && !( (sxbeg+(ns-1)*jsx)+(ng-1)*jgx-distx <nx && (szbeg+(ns-1)*jsz)+(ng-1)*jgz-distz <nz)) { sf_warning("geophones exceeds the computing zone!"); exit(1); } sg_init(gxz, gzbeg, gxbeg, jgz, jgx, ng); }
int main(int argc, char* argv[]) { bool verb,fsrf,snap,expl,dabc; int jsnap,ntsnap,jdata; /* OMP parameters */ #ifdef _OPENMP int ompnth; #endif /* I/O files */ sf_file Fwav=NULL; /* wavelet */ sf_file Fsou=NULL; /* sources */ sf_file Frec=NULL; /* receivers */ sf_file Fvel=NULL; /* velocity */ sf_file Fdat=NULL; /* data */ sf_file Fwfl=NULL; /* wavefield */ /* cube axes */ sf_axis at,az,ax; sf_axis as,ar; int nt,nz,nx,ns,nr,nb; int it,iz,ix; float dt,dz,dx,idz,idx,dt2; /* FDM structure */ fdm2d fdm=NULL; abcone2d abc=NULL; sponge spo=NULL; /* I/O arrays */ float *ww=NULL; /* wavelet */ pt2d *ss=NULL; /* sources */ pt2d *rr=NULL; /* receivers */ float *dd=NULL; /* data */ float **tt=NULL; float **vp=NULL; /* velocity */ float **vp2=NULL; float **vv2=NULL; float **vh2=NULL; float **rm,**ro,**rp,**ra,**rt; /* main wavefield */ float **qm,**qo,**qp,**qa,**qt; /* auxiliary wavefield */ /* linear interpolation weights/indices */ lint2d cs,cr; /* FD operator size */ float cox,coz,cax,cbx,caz,cbz; /* wavefield cut params */ sf_axis acz=NULL,acx=NULL; int nqz,nqx; float oqz,oqx; float dqz,dqx; float **qc=NULL; float H2q,H1r; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /*------------------------------------------------------------*/ /* OMP parameters */ #ifdef _OPENMP ompnth=omp_init(); #endif /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */ if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */ if(! sf_getbool("expl",&expl)) expl=false; /* "exploding reflector" */ if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* I/O files */ Fwav = sf_input ("in" ); /* wavelet */ Fvel = sf_input ("vel"); /* velocity */ Fsou = sf_input ("sou"); /* sources */ Frec = sf_input ("rec"); /* receivers */ Fwfl = sf_output("wfl"); /* wavefield */ Fdat = sf_output("out"); /* data */ /*------------------------------------------------------------*/ /* axes */ at = sf_iaxa(Fwav,2); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(Fvel,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(Fvel,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space */ as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */ ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ns = sf_n(as); nr = sf_n(ar); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* other execution parameters */ if(! sf_getint("jdata",&jdata)) jdata=1; if(snap) { /* save wavefield every *jsnap* time steps */ if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* expand domain for FD operators and ABC */ if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP; fdm=fdutil_init(verb,fsrf,az,ax,nb,1); sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* setup output data header */ sf_oaxa(Fdat,ar,1); sf_setn(at,nt/jdata); sf_setd(at,dt*jdata); sf_oaxa(Fdat,at,2); /* setup output wavefield header */ if(snap) { if(!sf_getint ("nqz",&nqz)) nqz=sf_n(az); if(!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); dqz=sf_d(az); dqx=sf_d(ax); acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx); /* check if the imaging window fits in the wavefield domain */ qc=sf_floatalloc2(sf_n(acz),sf_n(acx)); ntsnap=0; for(it=0; it<nt; it++) { if(it%jsnap==0) ntsnap++; } sf_setn(at, ntsnap); sf_setd(at,dt*jsnap); if(verb) sf_raxa(at); sf_oaxa(Fwfl,acz,1); sf_oaxa(Fwfl,acx,2); sf_oaxa(Fwfl,at, 3); } if(expl) { ww = sf_floatalloc( 1); } else { ww = sf_floatalloc(ns); } dd = sf_floatalloc(nr); /*------------------------------------------------------------*/ /* setup source/receiver coordinates */ ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); pt2dread1(Fsou,ss,ns,2); /* read (x,z) coordinates */ pt2dread1(Frec,rr,nr,2); /* read (x,z) coordinates */ cs = lint2d_make(ns,ss,fdm); cr = lint2d_make(nr,rr,fdm); /*------------------------------------------------------------*/ /* setup FD coefficients */ idz = 1/dz; idx = 1/dx; cox= C0 * (idx*idx); cax= CA * idx*idx; cbx= CB * idx*idx; coz= C0 * (idz*idz); caz= CA * idz*idz; cbz= CB * idz*idz; /* precompute dt^2*/ dt2 = dt*dt; /*------------------------------------------------------------*/ tt = sf_floatalloc2(nz,nx); /*------------------------------------------------------------*/ /* input velocity */ vp =sf_floatalloc2(fdm->nzpad,fdm->nxpad); vp2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad); vv2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad); vh2 =sf_floatalloc2(fdm->nzpad,fdm->nxpad); sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vv2,fdm); /* vertical v */ sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vp2,fdm); /* NMO v */ sf_floatread(tt[0],nz*nx,Fvel ); expand(tt,vh2,fdm); /* horizontal v */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { vp2[ix][iz] = vp2[ix][iz] * vp2[ix][iz]; vv2[ix][iz] = vv2[ix][iz] * vv2[ix][iz]; vh2[ix][iz] = vh2[ix][iz] * vh2[ix][iz]; } } if(fsrf) { /* free surface */ for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nb; iz++) { vp2[ix][iz]=0; vv2[ix][iz]=0; vh2[ix][iz]=0; } } } /*------------------------------------------------------------*/ free(*tt); free(tt); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* allocate wavefield arrays */ qm=sf_floatalloc2(fdm->nzpad,fdm->nxpad); qo=sf_floatalloc2(fdm->nzpad,fdm->nxpad); qp=sf_floatalloc2(fdm->nzpad,fdm->nxpad); qa=sf_floatalloc2(fdm->nzpad,fdm->nxpad); rm=sf_floatalloc2(fdm->nzpad,fdm->nxpad); ro=sf_floatalloc2(fdm->nzpad,fdm->nxpad); rp=sf_floatalloc2(fdm->nzpad,fdm->nxpad); ra=sf_floatalloc2(fdm->nzpad,fdm->nxpad); for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { qm[ix][iz]=0; qo[ix][iz]=0; qp[ix][iz]=0; qa[ix][iz]=0; rm[ix][iz]=0; ro[ix][iz]=0; rp[ix][iz]=0; ra[ix][iz]=0; } } /*------------------------------------------------------------*/ if(dabc) { /* one-way abc setup */ abc = abcone2d_make(NOP,dt,vp,fsrf,fdm); /* sponge abc setup */ spo = sponge_make(fdm->nb); } /*------------------------------------------------------------*/ /* * MAIN LOOP */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",it); #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz,H2q,H1r) \ shared(fdm,ra,ro,qa,qo,cox,coz,cax,caz,cbx,cbz,idx,idz,vp2) #endif for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { H2q = Dxx(qo,ix,iz,cox,cax,cbx); H1r = Dzz(ro,ix,iz,coz,caz,cbz); /* main field - q */ qa[ix][iz] = H2q * vh2[ix][iz] + H1r * vv2[ix][iz] ; /* auxiliary field - r */ ra[ix][iz] = H2q * vp2[ix][iz] + H1r * vv2[ix][iz] ; } } /* inject acceleration source */ if(expl) { sf_floatread(ww, 1,Fwav); lint2d_inject1(ra,ww[0],cs); lint2d_inject1(qa,ww[0],cs); } else { sf_floatread(ww,ns,Fwav); lint2d_inject(ra,ww,cs); lint2d_inject(qa,ww,cs); } /* step forward in time */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iz) \ shared(fdm,ra,ro,rm,rp,qa,qo,qm,qp,dt2) #endif for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { qp[ix][iz] = 2*qo[ix][iz] - qm[ix][iz] + qa[ix][iz] * dt2; rp[ix][iz] = 2*ro[ix][iz] - rm[ix][iz] + ra[ix][iz] * dt2; } } /* circulate wavefield arrays */ qt=qm; qm=qo; qo=qp; qp=qt; rt=rm; rm=ro; ro=rp; rp=rt; if(dabc) { /* one-way abc apply */ abcone2d_apply(qo,qm,NOP,abc,fdm); sponge2d_apply(qm,spo,fdm); sponge2d_apply(qo,spo,fdm); sponge2d_apply(qp,spo,fdm); /* one-way abc apply */ abcone2d_apply(ro,rm,NOP,abc,fdm); sponge2d_apply(rm,spo,fdm); sponge2d_apply(ro,spo,fdm); sponge2d_apply(rp,spo,fdm); } /* extract data */ lint2d_extract(qo,dd,cr); if(snap && it%jsnap==0) { cut2d(qo,qc,fdm,acz,acx); sf_floatwrite(qc[0],sf_n(acz)*sf_n(acx),Fwfl); } if( it%jdata==0) sf_floatwrite(dd,nr,Fdat); } if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /* deallocate arrays */ free(*rm); free(rm); free(*rp); free(rp); free(*ro); free(ro); free(*ra); free(ra); free(*qm); free(qm); free(*qp); free(qp); free(*qo); free(qo); free(*qa); free(qa); free(*qc); free(qc); free(*vp); free(vp); free(*vp2); free(vp2); free(*vh2); free(vh2); free(*vv2); free(vv2); free(ww); free(ss); free(rr); free(dd); exit (0); }
int main(int argc, char* argv[]) { bool adj; int ix, iz; float dz, dx, dt, dr, ds; float z0, x0, t0, r0, s0; float zr, zs, padx0, padz0; double dt2, idx2, idz2; float **mm; sf_file vel, wavelet; sf_init(argc, argv); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &cpuid); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); omp_init(); if(cpuid==0) sf_warning("numprocs=%d", numprocs); if(!sf_getbool("adj", &adj)) adj=true; if(!sf_getbool("verb", &verb)) verb=false; if(!sf_getbool("snap", &snap)) snap=false; in=sf_input("input"); out=sf_output("output"); vel=sf_input("velocity"); wavelet=sf_input("wavelet"); 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(!sf_histfloat(vel, "o1", &z0)) sf_error("No o1= in velocity"); if(!sf_histint(vel, "n2", &nvx)) sf_error("No n2= in velocity"); if(!sf_histfloat(vel, "d2", &dx)) sf_error("No d2= in velocity"); if(!sf_histfloat(vel, "o2", &x0)) sf_error("No o2= in velocity"); if(!sf_histint(wavelet, "n1", &nt)) sf_error("No n1= in wavelet"); if(!sf_histfloat(wavelet, "d1", &dt)) sf_error("No d1= in wavelet"); if(!sf_histfloat(wavelet, "o1", &t0)) sf_error("No o1= in wavelet"); if(adj){/* migration */ if(!sf_histint(in, "n2", &nr)) sf_error("No n2= in input"); if(!sf_histfloat(in, "d2", &dr)) sf_error("No d2= in input"); if(!sf_histfloat(in, "o2", &r0)) sf_error("No o2= in input"); if(!sf_histint(in, "n3", &ns)) sf_error("No n3= in input"); if(!sf_histfloat(in, "d3", &ds)) sf_error("No d3= in input"); if(!sf_histfloat(in, "o3", &s0)) sf_error("No o3= in input"); if(cpuid==0){ sf_putint(out, "n1", nz); sf_putfloat(out, "d1", dz); sf_putfloat(out, "o1", z0); sf_putint(out, "n2", nvx); sf_putfloat(out, "d2", dx); sf_putfloat(out, "o2", x0); sf_putint(out, "n3", 1); sf_putstring(out, "label1", "Depth"); sf_putstring(out, "unit1", "m"); sf_putstring(out, "label2", "Distance"); sf_putstring(out, "unit2", "m");} }else{/* modeling */ if(!sf_getint("nr", &nr)) sf_error("No nr="); if(!sf_getfloat("dr", &dr)) sf_error("No dr="); if(!sf_getfloat("r0", &r0)) sf_error("No r0="); if(!sf_getint("ns", &ns)) sf_error("No ns="); if(!sf_getfloat("ds", &ds)) sf_error("No ds="); if(!sf_getfloat("s0", &s0)) sf_error("No s0="); if(cpuid==0){ sf_putint(out, "n1", nt); sf_putfloat(out, "d1", dt); sf_putfloat(out, "o1", t0); sf_putint(out, "n2", nr); sf_putfloat(out, "d2", dr); sf_putfloat(out, "o2", r0); sf_putint(out, "n3", ns); sf_putfloat(out, "d3", ds); sf_putfloat(out, "o3", s0); sf_putstring(out, "label1", "Time"); sf_putstring(out, "unit1", "s"); sf_putstring(out, "label2", "Distance"); sf_putstring(out, "unit2", "m"); sf_putstring(out, "label3", "Shot"); sf_putstring(out, "unit3", "m");} } if(!sf_getfloat("zr", &zr)) zr=0.0; if(!sf_getfloat("zs", &zs)) zs=0.0; if(!sf_getint("jt", &jt)) jt=100; if(!sf_getint("nx", &nx)) sf_error("Need nx"); if(!sf_getint("padz", &padz)) sf_error("Need padz"); if(!sf_getint("padx", &padx)) sf_error("Need padx"); if((s0-x0)/dx*2+1 != nx) sf_error("Need to reset nx"); padnx=nx+2*padx; padnz=nz+2*padz; padx0=x0-dx*padx; padz0=z0-dz*padz; dr_v=(dr/dx)+0.5; r0_v=(r0-padx0)/dx+0.5; zr_v=(zr-padz0)/dz+0.5; ds_v=(ds/dx)+0.5; sx=(s0-padx0)/dx+0.5; zs_v=(zs-padz0)/dz+0.5; mm=sf_floatalloc2(nz, nvx); vv=sf_floatalloc2(nz, nvx); padvv=sf_floatalloc2(padnz, padnx); ww=sf_floatalloc(nt); sf_floatread(ww, nt, wavelet); sf_floatread(vv[0], nz*nvx, vel); dt2=dt*dt; for(ix=0; ix<nvx; ix++) for(iz=0; iz<nz; iz++) vv[ix][iz]=vv[ix][iz]*vv[ix][iz]*dt2; if(snap){ snapshot=sf_output("snapshot"); if(cpuid==0){ sf_putint(snapshot, "n1", padnz); sf_putfloat(snapshot, "d1", dz); sf_putfloat(snapshot, "o1", padz0); sf_putint(snapshot, "n2", padnx); sf_putfloat(snapshot, "d2", dx); sf_putfloat(snapshot, "o2", padx0); sf_putint(snapshot, "n3", 1+(nt-1)/jt); sf_putfloat(snapshot, "d3", jt*dt); sf_putfloat(snapshot, "o3", t0); sf_putstring(snapshot, "label1", "Depth"); sf_putstring(snapshot, "unit1", "m"); sf_putstring(snapshot, "label2", "Distance"); sf_putstring(snapshot, "unit2", "m"); sf_putstring(snapshot, "label3", "Time"); sf_putstring(snapshot, "unit3", "s");} } if(cpuid==0){ sf_warning("nx=%d nvx=%d nz=%d nt=%d nr=%d ns=%d", nx, nvx, nz, nt, nr, ns); sf_warning("padnx=%d padnz=%d padx0=%.3f padz0=%.3f", padnx, padnz, padx0, padz0); sf_warning("dx=%.3f dz=%.3f dt=%.3f dr=%.3f ds=%.3f", dx, dz, dt, dr, ds); sf_warning("x0=%.3f z0=%.3f t0=%.3f r0=%.3f s0=%.3f", x0, z0, t0, r0, s0); sf_warning("dr_v=%d r0_v=%d zr_v=%d ds_v=%d sx=%d zs_v=%d", dr_v, r0_v, zr_v, ds_v, sx, zs_v);} idz2=1./(dz*dz); idx2=1./(dx*dx); // c0=-5.0/2.; // c1=4.0/3.; // c2=-1.0/12.; c11=4.0*idz2/3.0; c12=-idz2/12.0; c21=4.0*idx2/3.0; c22=-idx2/12.0; c0=-2*(c11+c12+c21+c22); if(adj){/* migration */ prertm2_oper(adj, mm); if(cpuid==0) sf_floatwrite(mm[0], nz*nvx, out); }else{ sf_floatread(mm[0], nz*nvx, in); prertm2_oper(adj, mm); } sf_fileclose(in); sf_fileclose(out); free(*vv); free(*padvv); free(vv); free(padvv); free(ww); MPI_Finalize(); exit(0); }
int main (int argc, char* argv[]) { /* ----------------------------------------------------------------------------------*/ // Variable declaration // ------------------------ #ifdef _OPENMP int ompnth=1; #endif // logical variables bool source, verb; // axes sf_axis awx,awz,awt; /* wavefield axes */ sf_axis atau; /* eic axes: time-lag and extended images */ sf_axis ar; /* coordinate file */ int nr,nx,nz,nt; // integer int i1, i2,i3,it, itau,itaux; /* integer var for loops */ int ix,iz; /*integers for extracting windowed wavefield */ float tmin, tmax,taumin,taumax,t,tau,tbmin,tbmax; float *uaux; // arrays float ***uo; /* wavefield array */ float *tgathers; /* time lag gathers */ float **adjsrc; /* adjoint source [nt] */ // coordinate arrays pt2d *rr=NULL; /* extended images coordinates */ /* ----------------------*/ /* I/O files */ sf_file Feic=NULL; sf_file Fxcr=NULL; sf_file Fadj=NULL; sf_file Fwfl=NULL; /* ----------------------*/ // End of variables declaration /* ----------------------------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /*------------------------------------------------------------*/ /* OMP parameters */ #ifdef _OPENMP ompnth=omp_init(); sf_warning("number of threads assigned by OMP: %d",ompnth); #endif /*------------------------------------------------------------*/ /* ------- I/O declaration ---------------*/ Fwfl = sf_input("in"); /* Wavefield used for the adjoint computation, for adj source side pass the receiver wavefield, for receiver side pass the source wavefield */ Feic = sf_input("eic"); /* Penalized extended image (apply P(\tau) twice) */ Fxcr = sf_input("coord"); /* coordinates of every extended image, in 2d should be organized in (x1,z1),(x2,z2),...,(xn,zn) */ Fadj = sf_output("out"); /* adjoint source to be injected on each coordinate */ /* --------------------------------------- */ //Get parameters from command line: if (! sf_getbool("source",&source)) source=true; /* Source side [default] or receiver side adjoint? */ if (! sf_getbool("verb",&verb)) verb=true; /* verbosity flag, mainly debug printout */ /* at this time */ if(verb) sf_warning("===================================================="); if(verb) sf_warning("====== Calculating adjoint source ======="); if(source){ if(verb) sf_warning("====== source side adjoint source ======="); }else{ if(verb) sf_warning("====== receiver side adjoint source ======="); } if(verb) sf_warning("===================================================="); // ------------------------------------------------- // Some file type checking if (SF_FLOAT != sf_gettype(Fwfl)) sf_error("Need float input for wavefield"); if (SF_FLOAT != sf_gettype(Feic)) sf_error("Need float input for eic"); if (SF_FLOAT != sf_gettype(Fxcr)) sf_error("Need float input for coordinates"); // ------------------------------------------------- // -------------------------------------------------------------------------------------- // Axes geometry loading and writing // -------------------------------------------------------------------------------------- // From wavefield awz = sf_iaxa(Fwfl,1); awx = sf_iaxa(Fwfl,2); awt = sf_iaxa(Fwfl,3); // From coordinates ar = sf_iaxa(Fxcr,2); // From extended images atau = sf_iaxa(Feic,1); taumin = SF_MIN(sf_o(atau),(sf_n(atau)-1)*sf_d(atau)+sf_o(atau)); taumax = SF_MAX(sf_o(atau),(sf_n(atau)-1)*sf_d(atau)+sf_o(atau)); // To adjoint source sf_oaxa(Fadj,awt,1); sf_oaxa(Fadj,ar,2); sf_putint(Fadj,"n3",1); nr=sf_n(ar); /*-------------------------------------------------------------------------------------*/ /* setup source/receiver coordinates */ /*-------------------------------------------------------------------------------------*/ rr = pt2dalloc1(nr); pt2dread1(Fxcr,rr,nr,2); if(verb) sf_warning("===================================================="); if(verb) sf_warning("reading %d extended image coordinates points",nr); if(verb) sf_warning("===================================================="); if(verb) sf_warning("allocating and reading wavefield"); if(verb) sf_warning("z axis: n1=%-10d o1=%-10.3f d1=%-10.3f",sf_n(awz),sf_o(awz),sf_d(awz)); if(verb) sf_warning("x axis: n2=%-10d o2=%-10.3f d2=%-10.3f",sf_n(awx),sf_o(awx),sf_d(awx)); if(verb) sf_warning("t axis: n3=%-10d o3=%-10.3f d3=%-10.3f",sf_n(awt),sf_o(awt),sf_d(awt)); if(verb) sf_warning("===================================================="); // allocate wavefield space uo = sf_floatalloc3(sf_n(awz),sf_n(awx),sf_n(awt)); if(uo==NULL) sf_error("not enough memory to read wavefield"); uaux = sf_floatalloc(1); nz=sf_n(awz); nx=sf_n(awx); nt=sf_n(awt); // read wavefield for (i3=0 ; i3<nt; i3++){ for (i2=0 ; i2<nx; i2++){ for (i1=0 ; i1<nz; i1++){ sf_floatread(uaux,1,Fwfl); uo[i3][i2][i1]=uaux[0]; }}} //------------------------------------------------------------------------------------- // reading extended images //------------------------------------------------------------------------------------- if(verb) sf_warning("reading %d extended images points",nr); if(verb) sf_warning("====================================================="); if(verb) sf_warning(""); tgathers = sf_floatalloc(sf_n(atau)); adjsrc = sf_floatalloc2(sf_n(awt),1); if(source) { for (i1=0; i1<sf_n(ar); i1++) { sf_floatread(tgathers,sf_n(atau),Feic); ix= 0.5+(rr[i1].x - sf_o(awx))/sf_d(awx); iz= 0.5+(rr[i1].z - sf_o(awz))/sf_d(awz); fprintf(stderr,"\b\b\b\b\b\b%d",i1+1); #ifdef _OPENMP #pragma omp parallel for \ private(it,t,tmin,tmax,itau,tbmin,tbmax,tau,itaux) \ shared(tgathers,uo,) #endif for (it=0; it<sf_n(awt); it++){ adjsrc[0][it]=0.0; // get the limits of tau such that we avoid // segmentation fault on the wavefield uo: // 0 < t-2*tau < tmax //Lower tau bound t=it*sf_d(awt)+sf_o(awt); tmin = SF_MIN(t+2*taumax,t+2*taumin); tmin = SF_MAX(tmin,0); //Upper tau bound tmax = SF_MAX(t+2*taumax,t+2*taumin); tmax = SF_MIN(tmax,sf_o(awt)+sf_d(awt)*(sf_n(awt)-1) ); tbmin =SF_MIN( -(t - tmax)*0.5,-(t - tmin)*0.5); tbmax =SF_MAX( -(t - tmax)*0.5,-(t - tmin)*0.5); for (itau = 1.5 + (tbmin - sf_o(atau))/sf_d(atau) ; itau< (tbmax - sf_o(atau))/sf_d(atau) ; itau++) { tau=itau*sf_d(atau)+sf_o(atau) ; itaux = 0.5 + ((t + 2*tau)-sf_o(awt))/sf_d(awt); adjsrc[0][it] += tgathers[itau]*uo[itaux][ix][iz]; } } sf_floatwrite(adjsrc[0],nt,Fadj); } }else{ for (i1=0; i1<sf_n(ar); i1++) { sf_floatread(tgathers,sf_n(atau),Feic); ix= 0.5+(rr[i1].x - sf_o(awx))/sf_d(awx); iz= 0.5+(rr[i1].z - sf_o(awz))/sf_d(awz); fprintf(stderr,"\b\b\b\b\b\b%d",i1+1); //if (verb) if(verb) fprintf(stderr,"\b\b\b\b\b%03d/%03d",i1+1,sf_n(ar)); #ifdef _OPENMP #pragma omp parallel for \ private(it,t,tmin,tmax,itau,tbmin,tbmax,tau,itaux) \ shared(tgathers,uo,) #endif for (it=0; it<sf_n(awt); it++){ adjsrc[0][it]=0.0; // get the limits of tau such that we avoid // segmentation fault on the wavefield uo: // 0 < t-2*tau < tmax //Lower tau bound t=it*sf_d(awt)+sf_o(awt); tmin = SF_MIN(t-2*taumax,t-2*taumin); tmin = SF_MAX(tmin,0); //Upper tau bound tmax = SF_MAX(t-2*taumax,t-2*taumin); tmax = SF_MIN(tmax,sf_o(awt)+sf_d(awt)*(sf_n(awt)-1) ); tbmin =SF_MIN( (t - tmax)*0.5,(t - tmin)*0.5); tbmax =SF_MAX( (t - tmax)*0.5,(t - tmin)*0.5); for (itau = 1.5 + (tbmin - sf_o(atau))/sf_d(atau) ; itau< (tbmax - sf_o(atau))/sf_d(atau) ; itau++) { tau=itau*sf_d(atau)+sf_o(atau) ; itaux = 0.5 + ((t - 2*tau)-sf_o(awt))/sf_d(awt); adjsrc[0][it] += tgathers[itau]*uo[itaux][ix][iz]; } } sf_floatwrite(adjsrc[0],nt,Fadj); } } if (verb) if(verb) fprintf(stderr,"\n"); free(**uo);free(*uo);free(uo); free(*adjsrc);free(adjsrc); free(tgathers); exit(0); }
int main(int argc, char* argv[]) { int function, media, ntmp; bool verb; sf_mpi mpipar; sf_sou soupar; sf_acqui acpar; sf_vec array; sf_fwi fwipar; sf_optim optpar=NULL; sf_pas paspar=NULL; MPI_Comm comm=MPI_COMM_WORLD; sf_file Fv, Fq, Fw, Fdat, Fimg, Finv=NULL, Fgrad, Fsrc, Fmwt=NULL; MPI_Init(&argc, &argv); MPI_Comm_rank(comm, &mpipar.cpuid); MPI_Comm_size(comm, &mpipar.numprocs); sf_init(argc, argv); #ifdef _OPENMP omp_init(); #endif Fv=sf_input("Fvel"); /* velocity model */ Fq=sf_input("Fq"); /* quality factor */ Fw=sf_input("Fwavelet"); /* wavelet */ soupar=(sf_sou)sf_alloc(1, sizeof(*soupar)); acpar=(sf_acqui)sf_alloc(1, sizeof(*acpar)); array=(sf_vec)sf_alloc(1, sizeof(*array)); /* parameters I/O */ if(!sf_getint("media", &media)) media=1; /* if 1, acoustic media; if 2, visco-acoustic media */ if(!sf_getint("function", &function)) function=2; /* if 1, forward modeling; if 2, FWI; if 3, RTM */ if(!sf_histint(Fv, "n1", &acpar->nz)) sf_error("No n1= in Fv"); if(!sf_histint(Fv, "n2", &acpar->nx)) sf_error("No n2= in Fv"); if(!sf_histfloat(Fv, "d1", &acpar->dz)) sf_error("No d1= in Fv"); if(!sf_histfloat(Fv, "d2", &acpar->dx)) sf_error("No d2= in Fv"); if(!sf_histfloat(Fv, "o1", &acpar->z0)) sf_error("No o1= in Fv"); if(!sf_histfloat(Fv, "o2", &acpar->x0)) sf_error("No o2= in Fv"); if(!sf_histint(Fw, "n1", &acpar->nt)) sf_error("No n1= in Fw"); if(!sf_histfloat(Fw, "d1", &acpar->dt)) sf_error("No d1= in Fw"); if(!sf_histfloat(Fw, "o1", &acpar->t0)) sf_error("No o1= in Fw"); if(!sf_getbool("verb", &verb)) verb=false; /* verbosity flag */ if(!sf_getint("nb", &acpar->nb)) acpar->nb=100; /* boundary width */ if(!sf_getfloat("coef", &acpar->coef)) acpar->coef=0.003; /* absorbing boundary coefficient */ if(!sf_getint("acqui_type", &acpar->acqui_type)) acpar->acqui_type=1; /* if 1, fixed acquisition; if 2, marine acquisition; if 3, symmetric acquisition */ if(!sf_getint("ns", &acpar->ns)) sf_error("shot number required"); /* shot number */ if(!sf_getfloat("ds", &acpar->ds)) sf_error("shot interval required"); /* shot interval */ if(!sf_getfloat("s0", &acpar->s0)) sf_error("shot origin required"); /* shot origin */ if(!sf_getint("sz", &acpar->sz)) acpar->sz=5; /* source depth */ if(!sf_getint("nr", &acpar->nr)) acpar->nr=acpar->nx; /* number of receiver */ if(!sf_getfloat("dr", &acpar->dr)) acpar->dr=acpar->dx; /* receiver interval */ if(!sf_getfloat("r0", &acpar->r0)) acpar->r0=acpar->x0; /* receiver origin */ if(!sf_getint("rz", &acpar->rz)) acpar->rz=1; /* receiver depth */ if(!sf_getfloat("f0", &acpar->f0)) sf_error("reference frequency required"); /* reference frequency */ if(!sf_getint("interval", &acpar->interval)) acpar->interval=1; /* wavefield storing interval */ if(!sf_getfloat("fhi", &soupar->fhi)) soupar->fhi=0.5/acpar->dt; if(!sf_getfloat("flo", &soupar->flo)) soupar->flo=0.; soupar->rectx=2; soupar->rectz=2; /* get prepared */ preparation(Fv, Fq, Fw, acpar, soupar, array); switch (function) { case 1: /* Modeling */ Fdat=sf_output("output"); /* shot data */ /* dimension set up */ sf_putint(Fdat, "n1", acpar->nt); sf_putfloat(Fdat, "d1", acpar->dt); sf_putfloat(Fdat, "o1", acpar->t0); sf_putstring(Fdat, "label1", "Time"); sf_putstring(Fdat, "unit1", "s"); sf_putint(Fdat, "n2", acpar->nr); sf_putfloat(Fdat, "d2", acpar->dr); sf_putfloat(Fdat, "o2", acpar->r0); sf_putstring(Fdat, "label2", "Receiver"); sf_putstring(Fdat, "unit2", "km"); sf_putint(Fdat, "n3", acpar->ns); sf_putfloat(Fdat, "d3", acpar->ds); sf_putfloat(Fdat, "o3", acpar->s0); sf_putstring(Fdat, "label3", "Shot"); sf_putstring(Fdat, "unit3", "km"); if(media==1) forward_modeling_a(Fdat, &mpipar, soupar, acpar, array, verb); else forward_modeling(Fdat, &mpipar, soupar, acpar, array, verb); sf_fileclose(Fdat); break; case 2: /* FWI */ fwipar=(sf_fwi)sf_alloc(1, sizeof(*fwipar)); if(!sf_getbool("onlygrad", &fwipar->onlygrad)) fwipar->onlygrad=false; /* only want gradident */ fwipar->grad_type=1; fwipar->misfit_type=1; fwipar->opt_type=1; if(!sf_getfloat("wt1", &fwipar->wt1)) fwipar->wt1=acpar->t0; if(!sf_getfloat("wt2", &fwipar->wt2)) fwipar->wt2=acpar->t0+(acpar->nt-1)*acpar->dt; if(!sf_getfloat("woff1", &fwipar->woff1)) fwipar->woff1=acpar->r0; if(!sf_getfloat("woff2", &fwipar->woff2)) fwipar->woff2=acpar->r0+(acpar->nr-1)*acpar->dr; if(!sf_getbool("oreo", &fwipar->oreo)) fwipar->oreo=false; /* keep oreo or keep cream */ if(!sf_getint("waterz", &fwipar->waterz)) fwipar->waterz=51; /* water layer depth */ if(!sf_getint("grectx", &fwipar->rectx)) fwipar->rectx=3; /* gradient smoothing radius in x */ if(!sf_getint("grectz", &fwipar->rectz)) fwipar->rectz=3; /* gradient smoothing radius in z */ Fdat=sf_input("Fdat"); /* input data */ if(!fwipar->onlygrad) Finv=sf_output("output"); /* FWI result */ Fgrad=sf_output("Fgrad"); /* FWI gradient at first iteration */ /* dimension set up */ if(Finv != NULL){ sf_putint(Finv, "n1", acpar->nz); sf_putfloat(Finv, "d1", acpar->dz); sf_putfloat(Finv, "o1", acpar->z0); sf_putstring(Finv, "label1", "Depth"); sf_putstring(Finv, "unit1", "km"); sf_putint(Finv, "n2", acpar->nx); sf_putfloat(Finv, "d2", acpar->dx); sf_putfloat(Finv, "o2", acpar->x0); sf_putstring(Finv, "label2", "Distance"); sf_putstring(Finv, "unit2", "km"); if(fwipar->grad_type==3) sf_putint(Finv, "n3", 2); } sf_putint(Fgrad, "n1", acpar->nz); sf_putfloat(Fgrad, "d1", acpar->dz); sf_putfloat(Fgrad, "o1", acpar->z0); sf_putstring(Fgrad, "label1", "Depth"); sf_putstring(Fgrad, "unit1", "km"); sf_putint(Fgrad, "n2", acpar->nx); sf_putfloat(Fgrad, "d2", acpar->dx); sf_putfloat(Fgrad, "o2", acpar->x0); sf_putstring(Fgrad, "label2", "Distance"); sf_putstring(Fgrad, "unit2", "km"); if(fwipar->grad_type==3) sf_putint(Fgrad, "n3", 2); if(!fwipar->onlygrad){ optpar=(sf_optim)sf_alloc(1, sizeof(*optpar)); if(!sf_getint("niter", &optpar->niter)) sf_error("iteration number required"); /* iteration number */ if(!sf_getfloat("conv_error", &optpar->conv_error)) sf_error("convergence error required"); /* final convergence error */ optpar->npair=20; /* number of l-BFGS pairs */ optpar->nls=20; /* line search number */ if(!sf_getfloat("c1", &optpar->c1)) optpar->c1=1e-4; if(!sf_getfloat("c2", &optpar->c2)) optpar->c2=0.9; optpar->factor=10; if(!sf_getfloat("v1", &optpar->v1)) optpar->v1=0.; if(!sf_getfloat("v2", &optpar->v2)) optpar->v2=10.; } fwi(Fdat, Finv, Fgrad, &mpipar, soupar, acpar, array, fwipar, optpar, verb, media); if(!fwipar->onlygrad) sf_fileclose(Finv); sf_fileclose(Fgrad); break; case 3: /* RTM */ Fdat=sf_input("Fdat"); /* input data */ Fimg=sf_output("output"); /* rtm image */ /* dimension set up */ sf_putint(Fimg, "n1", acpar->nz); sf_putfloat(Fimg, "d1", acpar->dz); sf_putfloat(Fimg, "o1", acpar->z0); sf_putstring(Fimg, "label1", "Depth"); sf_putstring(Fimg, "unit1", "km"); sf_putint(Fimg, "n2", acpar->nx); sf_putfloat(Fimg, "d2", acpar->dx); sf_putfloat(Fimg, "o2", acpar->x0); sf_putstring(Fimg, "label2", "Distance"); sf_putstring(Fimg, "unit2", "km"); if(media==1) rtm_a(Fdat, Fimg, &mpipar, soupar, acpar, array, verb); else rtm(Fdat, Fimg, &mpipar, soupar, acpar, array, verb); sf_fileclose(Fimg); break; case 4: /* Passive FWI */ paspar = passive_init(acpar); if (paspar->inv) { Fdat=sf_input("Fdat"); if(!sf_histint(Fdat, "n3", &acpar->ns)) acpar->ns=1; if (!paspar->onlysrc) { fwipar=(sf_fwi)sf_alloc(1, sizeof(*fwipar)); if(!sf_getbool("onlygrad", &fwipar->onlygrad)) fwipar->onlygrad=false; /* only want gradident */ fwipar->grad_type=1; fwipar->misfit_type=1; fwipar->opt_type=1; if(!sf_getfloat("wt1", &fwipar->wt1)) fwipar->wt1=acpar->t0; if(!sf_getfloat("wt2", &fwipar->wt2)) fwipar->wt2=acpar->t0+(acpar->nt-1)*acpar->dt; if(!sf_getfloat("woff1", &fwipar->woff1)) fwipar->woff1=acpar->r0; if(!sf_getfloat("woff2", &fwipar->woff2)) fwipar->woff2=acpar->r0+(acpar->nr-1)*acpar->dr; if(!sf_getbool("oreo", &fwipar->oreo)) fwipar->oreo=false; /* keep oreo or keep cream */ if(!sf_getint("waterz", &fwipar->waterz)) fwipar->waterz=0; /* water layer depth */ if(!sf_getint("waterzb", &fwipar->waterzb)) fwipar->waterzb=0; /* water layer depth from bottom up */ if(!sf_getint("grectx", &fwipar->rectx)) fwipar->rectx=3; /* gradient smoothing radius in x */ if(!sf_getint("grectz", &fwipar->rectz)) fwipar->rectz=3; /* gradient smoothing radius in z */ if(!fwipar->onlygrad) Finv=sf_output("output"); /* FWI result */ Fgrad=sf_output("Fgrad"); /* FWI gradient at first iteration */ /* dimension set up */ if(Finv != NULL){ sf_putint(Finv, "n1", acpar->nz); sf_putfloat(Finv, "d1", acpar->dz); sf_putfloat(Finv, "o1", acpar->z0); sf_putstring(Finv, "label1", "Depth"); sf_putstring(Finv, "unit1", "km"); sf_putint(Finv, "n2", acpar->nx); sf_putfloat(Finv, "d2", acpar->dx); sf_putfloat(Finv, "o2", acpar->x0); sf_putstring(Finv, "label2", "Distance"); sf_putstring(Finv, "unit2", "km"); /*if(fwipar->grad_type==3) sf_putint(Finv, "n3", 2);*/ } sf_putint(Fgrad, "n1", acpar->nz); sf_putfloat(Fgrad, "d1", acpar->dz); sf_putfloat(Fgrad, "o1", acpar->z0); sf_putstring(Fgrad, "label1", "Depth"); sf_putstring(Fgrad, "unit1", "km"); sf_putint(Fgrad, "n2", acpar->nx); sf_putfloat(Fgrad, "d2", acpar->dx); sf_putfloat(Fgrad, "o2", acpar->x0); sf_putstring(Fgrad, "label2", "Distance"); sf_putstring(Fgrad, "unit2", "km"); /*if(fwipar->grad_type==3) sf_putint(Fgrad, "n3", 2);*/ if(!fwipar->onlygrad){ optpar=(sf_optim)sf_alloc(1, sizeof(*optpar)); if(!sf_getint("niter", &optpar->niter)) sf_error("iteration number required"); /* iteration number */ if(!sf_getint("repeat", &optpar->repeat)) optpar->repeat=1; /* repeat resetting alpha */ if(!sf_getfloat("conv_error", &optpar->conv_error)) sf_error("convergence error required"); /* final convergence error */ optpar->npair=20; /* number of l-BFGS pairs */ optpar->nls=20; /* line search number */ if(!sf_getfloat("c1", &optpar->c1)) optpar->c1=1e-4; if(!sf_getfloat("c2", &optpar->c2)) optpar->c2=0.9; optpar->factor=10; if(!sf_getfloat("v1", &optpar->v1)) optpar->v1=0.; if(!sf_getfloat("v2", &optpar->v2)) optpar->v2=10.; } } /* if !onlysrc */ if (!paspar->onlyvel) { Fsrc=sf_output("Fsrc"); sf_putint (Fsrc, "n1", acpar->nz); sf_putfloat (Fsrc, "o1", acpar->z0); sf_putfloat (Fsrc, "d1", acpar->dz); sf_putstring(Fsrc, "label1", "Depth"); sf_putstring(Fsrc, "unit1" , "km"); sf_putint (Fsrc, "n2", acpar->nx); sf_putfloat (Fsrc, "o2", acpar->x0); sf_putfloat (Fsrc, "d2", acpar->dx); sf_putstring(Fsrc, "label2", "Distance"); sf_putstring(Fsrc, "unit2" , "km"); sf_putint (Fsrc, "n3", acpar->nt); sf_putfloat (Fsrc, "o3", acpar->t0); sf_putfloat (Fsrc, "d3", acpar->dt); sf_putstring(Fsrc, "label3", "Time"); sf_putstring(Fsrc, "unit3" , "s"); sf_putint (Fsrc, "n4", acpar->ns); sf_putfloat (Fsrc, "d4", 1.0f); sf_putfloat (Fsrc, "o4", 0.0f); sf_putstring(Fsrc, "label4", "Stage"); Fmwt=sf_output("Fmwt"); /* output data */ sf_putint (Fmwt, "n1", acpar->nz); sf_putfloat (Fmwt, "o1", acpar->z0); sf_putfloat (Fmwt, "d1", acpar->dz); sf_putstring(Fmwt, "label1", "Depth"); sf_putstring(Fmwt, "unit1" , "km"); sf_putint (Fmwt, "n2", acpar->nx); sf_putfloat (Fmwt, "o2", acpar->x0); sf_putfloat (Fmwt, "d2", acpar->dx); sf_putstring(Fmwt, "label2", "Distance"); sf_putstring(Fmwt, "unit2" , "km"); sf_putint (Fmwt, "n3", acpar->nt); sf_putfloat (Fmwt, "o3", acpar->t0); sf_putfloat (Fmwt, "d3", acpar->dt); sf_putstring(Fmwt, "label3", "Time"); sf_putstring(Fmwt, "unit3" , "s"); } else { Fsrc=sf_input("Fsrc"); if(!sf_histint(Fsrc, "n4", &ntmp)) ntmp=1; if (ntmp!=acpar->ns) sf_error("Shot dimension mismatch!"); } } else { /* modeling */ Fsrc=sf_input("Fsrc"); if(!sf_histint(Fsrc, "n4", &acpar->ns)) acpar->ns=1; Fdat=sf_output("output"); /* output data */ sf_putint (Fdat, "n1", acpar->nt); sf_putfloat (Fdat, "o1", acpar->t0); sf_putfloat (Fdat, "d1", acpar->dt); sf_putstring(Fdat, "label1", "Time"); sf_putstring(Fdat, "unit1" , "s"); sf_putint (Fdat, "n2", acpar->nx); sf_putfloat (Fdat, "o2", acpar->x0); sf_putfloat (Fdat, "d2", acpar->dx); sf_putstring(Fdat, "label2", "Distance"); sf_putstring(Fdat, "unit2" , "km"); sf_putint (Fdat, "n3", acpar->ns); sf_putfloat (Fdat, "d3", 1.0f); sf_putfloat (Fdat, "o3", 0.0f); sf_putstring(Fdat, "label3", "Stage"); } if (paspar->inv) { if (paspar->onlysrc) { /* only inverting for source */ lstri(Fdat, Fmwt, Fsrc, &mpipar, acpar, array, paspar, verb); sf_fileclose(Fsrc); sf_fileclose(Fmwt); } else { /* inverting for velocity ( and source ) */ pfwi(Fdat, Finv, Fgrad, Fmwt, Fsrc, &mpipar, soupar, acpar, array, fwipar, optpar, paspar, verb); if(!fwipar->onlygrad) sf_fileclose(Finv); sf_fileclose(Fgrad); if (!paspar->onlyvel) { sf_fileclose(Fsrc); sf_fileclose(Fmwt); } } } else { lstri(Fdat, Fmwt, Fsrc, &mpipar, acpar, array, paspar, verb); sf_fileclose(Fdat); } break; default: sf_warning("Please specify a valid function"); } /* switch */ MPI_Finalize(); exit(0); }
int main(int argc, char* argv[]) { /*------------------------------------------------------------*/ /* Execution control, I/O files and geometry */ /*------------------------------------------------------------*/ bool verb,fsrf,snap,back,esou; /* execution flags */ int jsnap,ntsnap,jdata; /* jump along axes */ int shft; /* time shift for wavefield matching in RTM */ /* I/O files */ sf_file Fwav=NULL; /* wavelet */ sf_file Fdat=NULL; /* data */ sf_file Fsou=NULL; /* sources */ sf_file Frec=NULL; /* receivers */ sf_file Fccc=NULL; /* velocity */ sf_file Frkp=NULL; /* app. rank */ sf_file Fltp=NULL; /* left mat */ sf_file Frtp=NULL; /* right mat */ sf_file Fwfp=NULL; /* wavefield */ sf_file Frks=NULL; /* app. rank */ sf_file Flts=NULL; /* left mat */ sf_file Frts=NULL; /* right mat */ sf_file Fwfs=NULL; /* wavefield */ /* cube axes */ sf_axis at,ax,ay,az; /* time, x, y, z */ sf_axis asx,asy,arx,ary,ac; /* sou, rec-x, rec-y, component */ /* dimension, index and interval */ int nt,nz,nx,ny,ns,nr,nc,nb; int it,iz,ix,iy; float dt,dz,dx,dy; int nxyz, nk; /* FDM and KSP structure */ //!!!JS fdm3d fdm=NULL; dft3d dft=NULL; clr3d clr_p=NULL, clr_s=NULL; /* I/O arrays for sou & rec */ sf_complex***ww=NULL; /* wavelet */ pt3d *ss=NULL; /* sources */ pt3d *rr=NULL; /* receivers */ sf_complex **dd=NULL; /* data */ /*------------------------------------------------------------*/ /* displacement: uo = U @ t; up = U @ t+1 */ /*------------------------------------------------------------*/ sf_complex ***uox, ***uoy, ***uoz, **uo; sf_complex ***uox_p, ***uoy_p, ***uoz_p, **uo_p; sf_complex ***uox_s, ***uoy_s, ***uoz_s, **uo_s; /*sf_complex ***upx, ***upy, ***upz, **up;*/ /*------------------------------------------------------------*/ /* lowrank decomposition arrays */ /*------------------------------------------------------------*/ int ntmp, *n2s_p, *n2s_s; sf_complex **lt_p, **rt_p, **lt_s, **rt_s; /*------------------------------------------------------------*/ /* linear interpolation weights/indices */ /*------------------------------------------------------------*/ lint3d cs,cr; /* for injecting source and extracting data */ /* Gaussian bell */ int nbell; /*------------------------------------------------------------*/ /* wavefield cut params */ /*------------------------------------------------------------*/ sf_axis acz=NULL,acx=NULL,acy=NULL; int nqz,nqx,nqy; float oqz,oqx,oqy; float dqz,dqx,dqy; sf_complex***uc=NULL; /* tmp array for output wavefield snaps */ /*------------------------------------------------------------*/ /* init RSF */ /*------------------------------------------------------------*/ sf_init(argc,argv); /*------------------------------------------------------------*/ /* OMP parameters */ /*------------------------------------------------------------*/ #ifdef _OPENMP omp_init(); #endif /*------------------------------------------------------------*/ /* read execution flags */ /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */ if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */ if(! sf_getbool("back",&back)) back=false; /* backward extrapolation flag (for rtm) */ if(! sf_getbool("esou",&esou)) esou=false; /* explosive force source */ /*------------------------------------------------------------*/ /* I/O files */ /*------------------------------------------------------------*/ Fwav = sf_input ("in" ); /* wavelet */ Fdat = sf_output("out"); /* data */ Fsou = sf_input ("sou"); /* sources */ Frec = sf_input ("rec"); /* receivers */ Fccc = sf_input ("ccc"); /* stiffness */ Frkp = sf_input ("rkp"); /* app. rank */ Fltp = sf_input ("ltp"); /* left mat */ Frtp = sf_input ("rtp"); /* right mat */ Fwfp = sf_output("wfp"); /* wavefield */ Frks = sf_input ("rks"); /* app. rank */ Flts = sf_input ("lts"); /* left mat */ Frts = sf_input ("rts"); /* right mat */ Fwfs = sf_output("wfs"); /* wavefield */ /*------------------------------------------------------------*/ /* axes */ /*------------------------------------------------------------*/ at = sf_iaxa(Fwav,4); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space x */ ay = sf_iaxa(Fccc,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space y */ asx = sf_iaxa(Fsou,2); sf_setlabel(asx,"sx"); if(verb) sf_raxa(asx); /* sources x */ asy = sf_iaxa(Fsou,3); sf_setlabel(asy,"sy"); if(verb) sf_raxa(asy); /* sources y */ arx = sf_iaxa(Frec,2); sf_setlabel(arx,"rx"); if(verb) sf_raxa(arx); /* receivers x */ ary = sf_iaxa(Frec,3); sf_setlabel(ary,"ry"); if(verb) sf_raxa(ary); /* receivers y */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ny = sf_n(ay); dy = sf_d(ay); ns = sf_n(asx)*sf_n(asy); nr = sf_n(arx)*sf_n(ary); /*------------------------------------------------------------*/ /* other execution parameters */ /*------------------------------------------------------------*/ if(! sf_getint("nbell",&nbell)) nbell=NOP; /* bell size */ if(verb) sf_warning("nbell=%d",nbell); if(! sf_getint("jdata",&jdata)) jdata=1; if(snap) { /* save wavefield every *jsnap* time steps */ if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } if(back) { shft = (nt-1)%jsnap; sf_warning("For backward extrapolation, make sure nbell(%d)=0",nbell); } else shft = 0; /*------------------------------------------------------------*/ /* expand domain for FD operators and ABC */ /*------------------------------------------------------------*/ if( !sf_getint("nb",&nb)) nb=NOP; fdm=fdutil3d_init(verb,fsrf,az,ax,ay,nb,1); if(nbell) fdbell3d_init(nbell); sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax); sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if(verb) sf_raxa(ay); /*------------------------------------------------------------*/ /* 3D vector components */ /*------------------------------------------------------------*/ nc=3; ac=sf_maxa(nc,0,1); /* output 3 cartesian components */ /*------------------------------------------------------------*/ /* setup output data header */ /*------------------------------------------------------------*/ sf_settype(Fdat,SF_COMPLEX); sf_oaxa(Fdat,arx,1); sf_oaxa(Fdat,ary,2); sf_oaxa(Fdat,ac,3); sf_setn(at,nt/jdata); sf_setd(at,dt*jdata); sf_oaxa(Fdat,at,4); /* setup output wavefield header */ if(snap) { if(!sf_getint ("nqz",&nqz)) nqz=sf_n(az); if(!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); if(!sf_getint ("nqy",&nqy)) nqy=sf_n(ay); if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay); dqz=sf_d(az); dqx=sf_d(ax); dqy=sf_d(ay); acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx); acy = sf_maxa(nqy,oqy,dqy); sf_raxa(acy); uc=sf_complexalloc3(sf_n(acz),sf_n(acx),sf_n(acy)); ntsnap=0; /* ntsnap = it/jsnap+1; */ for(it=0; it<nt; it++) { if(it%jsnap==0) ntsnap++; } sf_setn(at, ntsnap); sf_setd(at,dt*jsnap); if(verb) sf_raxa(at); sf_settype(Fwfp,SF_COMPLEX); sf_oaxa(Fwfp,acz,1); sf_oaxa(Fwfp,acx,2); sf_oaxa(Fwfp,acy,3); sf_oaxa(Fwfp,ac, 4); sf_oaxa(Fwfp,at, 5); sf_settype(Fwfs,SF_COMPLEX); sf_oaxa(Fwfs,acz,1); sf_oaxa(Fwfs,acx,2); sf_oaxa(Fwfs,acy,3); sf_oaxa(Fwfs,ac, 4); sf_oaxa(Fwfs,at, 5); } /*------------------------------------------------------------*/ /* source and data array */ /*------------------------------------------------------------*/ ww=sf_complexalloc3(ns,nc,nt); /* Fast axis: n_sou > n_comp > n_time */ sf_complexread(ww[0][0],nt*nc*ns,Fwav); dd=sf_complexalloc2(nr,nc); /*------------------------------------------------------------*/ /* setup source/receiver coordinates */ /*------------------------------------------------------------*/ ss = (pt3d*) sf_alloc(ns,sizeof(*ss)); rr = (pt3d*) sf_alloc(nr,sizeof(*rr)); pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */ pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */ /* calculate 3d linear interpolation coef for sou & rec */ cs = lint3d_make(ns,ss,fdm); cr = lint3d_make(nr,rr,fdm); /*------------------------------------------------------------*/ /* allocate and initialize wavefield arrays */ /*------------------------------------------------------------*/ /* z-component */ uoz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoz_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoz_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /*upz=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/ /* x-component */ uox=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uox_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uox_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /*upx=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/ /* y-component */ uoy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoy_p=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoy_s=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /*upy=sf_complexalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad);*/ /* wavefield vector */ uo = (sf_complex**) sf_alloc(3,sizeof(sf_complex*)); uo[0] = uox[0][0]; uo[1] = uoy[0][0]; uo[2] = uoz[0][0]; uo_p = (sf_complex**) sf_alloc(3,sizeof(sf_complex*)); uo_p[0] = uox_p[0][0]; uo_p[1] = uoy_p[0][0]; uo_p[2] = uoz_p[0][0]; uo_s = (sf_complex**) sf_alloc(3,sizeof(sf_complex*)); uo_s[0] = uox_s[0][0]; uo_s[1] = uoy_s[0][0]; uo_s[2] = uoz_s[0][0]; /*up = (sf_complex**) sf_alloc(3,sizeof(sf_complex*));*/ /*up[0] = upx[0][0]; up[1] = upy[0][0]; up[2] = upz[0][0];*/ /* initialize fft and lrk */ dft = dft3d_init(1,false,false,fdm); nxyz= fdm->nypad*fdm->nxpad*fdm->nzpad; nk = dft->nky *dft->nkx *dft->nkz; /*------------------------------------------------------------*/ /* allocation I/O arrays */ /*------------------------------------------------------------*/ n2s_p = sf_intalloc(9); sf_intread(n2s_p,9,Frkp); clr_p = clr3d_make2(n2s_p,fdm); n2s_s = sf_intalloc(9); sf_intread(n2s_s,9,Frks); clr_s = clr3d_make2(n2s_s,fdm); if (clr_p->n2_max > clr_s->n2_max) clr3d_init(fdm,dft,clr_p); else clr3d_init(fdm,dft,clr_s); /* check the dimension */ if (!sf_histint(Fltp,"n1",&ntmp) || ntmp != nxyz) sf_error("Need n1=%d in left",nxyz); if (!sf_histint(Fltp,"n2",&ntmp) || ntmp != clr_p->n2_sum) sf_error("Need n2=%d in left",clr_p->n2_sum); if (!sf_histint(Frtp,"n1",&ntmp) || ntmp != nk) sf_error("Need n1=%d in right",nk); if (!sf_histint(Frtp,"n2",&ntmp) || ntmp != clr_p->n2_sum) sf_error("Need n2=%d in right",clr_p->n2_sum); lt_p = sf_complexalloc2(nxyz,clr_p->n2_sum); rt_p = sf_complexalloc2(nk ,clr_p->n2_sum); sf_complexread(lt_p[0],nxyz*clr_p->n2_sum,Fltp); sf_complexread(rt_p[0],nk *clr_p->n2_sum,Frtp); if (!sf_histint(Flts,"n1",&ntmp) || ntmp != nxyz) sf_error("Need n1=%d in left",nxyz); if (!sf_histint(Flts,"n2",&ntmp) || ntmp != clr_s->n2_sum) sf_error("Need n2=%d in left",clr_s->n2_sum); if (!sf_histint(Frts,"n1",&ntmp) || ntmp != nk) sf_error("Need n1=%d in right",nk); if (!sf_histint(Frts,"n2",&ntmp) || ntmp != clr_s->n2_sum) sf_error("Need n2=%d in right",clr_s->n2_sum); lt_s = sf_complexalloc2(nxyz,clr_s->n2_sum); rt_s = sf_complexalloc2(nk ,clr_s->n2_sum); sf_complexread(lt_s[0],nxyz*clr_s->n2_sum,Flts); sf_complexread(rt_s[0],nk *clr_s->n2_sum,Frts); /* initialize to zero */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,1) \ private(iy,ix,iz) \ shared(fdm,uoz,uox,uoy,uoz_p,uox_p,uoy_p,uoz_s,uox_s,uoy_s) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { uoz [iy][ix][iz]=sf_cmplx(0.,0.); uox [iy][ix][iz]=sf_cmplx(0.,0.); uoy [iy][ix][iz]=sf_cmplx(0.,0.); uoz_p[iy][ix][iz]=sf_cmplx(0.,0.); uox_p[iy][ix][iz]=sf_cmplx(0.,0.); uoy_p[iy][ix][iz]=sf_cmplx(0.,0.); uoz_s[iy][ix][iz]=sf_cmplx(0.,0.); uox_s[iy][ix][iz]=sf_cmplx(0.,0.); uoy_s[iy][ix][iz]=sf_cmplx(0.,0.); /*upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);*/ } } } /*------------------------------------------------------------*/ /*------------------------ MAIN LOOP -------------------------*/ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) sf_warning("it=%d/%d;",it,nt); /*fprintf(stderr,"\b\b\b\b\b%d",it);*/ /*------------------------------------------------------------*/ /* apply lowrank matrix to wavefield vector */ /*------------------------------------------------------------*/ clr3d_apply(uo_p, uo, lt_p, rt_p, fdm, dft, clr_p); clr3d_apply(uo_s, uo, lt_s, rt_s, fdm, dft, clr_s); /*------------------------------------------------------------*/ /* combine P and S wave modes */ /*------------------------------------------------------------*/ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,1) \ private(iy,ix,iz) \ shared(fdm,uoz,uox,uoy,uoz_p,uox_p,uoy_p,uoz_s,uox_s,uoy_s) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { uoz[iy][ix][iz] = uoz_p[iy][ix][iz] + uoz_s[iy][ix][iz]; uox[iy][ix][iz] = uox_p[iy][ix][iz] + uox_s[iy][ix][iz]; uoy[iy][ix][iz] = uoy_p[iy][ix][iz] + uoy_s[iy][ix][iz]; /*upz[iy][ix][iz]=sf_cmplx(0.,0.); upx[iy][ix][iz]=sf_cmplx(0.,0.); upy[iy][ix][iz]=sf_cmplx(0.,0.);*/ } } } /*------------------------------------------------------------*/ /* free surface */ /*------------------------------------------------------------*/ if(fsrf) { /* need to do something here */ } /*------------------------------------------------------------*/ /* inject displacement source */ /*------------------------------------------------------------*/ if(esou) { /* exploding force source */ lint3d_expl_complex(uoz,uox,uoy,ww[it],cs); } else { if(nbell) { lint3d_bell_complex(uoz,ww[it][0],cs); lint3d_bell_complex(uox,ww[it][1],cs); lint3d_bell_complex(uoy,ww[it][2],cs); } else { lint3d_inject_complex(uoz,ww[it][0],cs); lint3d_inject_complex(uox,ww[it][1],cs); lint3d_inject_complex(uoy,ww[it][2],cs); } } /*------------------------------------------------------------*/ /* cut wavefield and save */ /*------------------------------------------------------------*/ if(snap && (it-shft)%jsnap==0) { /* P wave */ cut3d_complex(uoz_p,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp); cut3d_complex(uox_p,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp); cut3d_complex(uoy_p,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfp); /* S wave */ cut3d_complex(uoz_s,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs); cut3d_complex(uox_s,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs); cut3d_complex(uoy_s,uc,fdm,acz,acx,acy); sf_complexwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfs); } lint3d_extract_complex(uoz,dd[0],cr); lint3d_extract_complex(uox,dd[1],cr); lint3d_extract_complex(uoy,dd[2],cr); if(it%jdata==0) sf_complexwrite(dd[0],nr*nc,Fdat); } if(verb) sf_warning("."); if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /* deallocate arrays */ free(dft); dft3d_finalize(); free(clr_p); free(clr_s); clr3d_finalize(); free(**ww); free(*ww); free(ww); free(ss); free(rr); free(*dd); free(dd); free(n2s_p); free(n2s_s); free(*lt_p); free(lt_p); free(*rt_p); free(rt_p); free(*lt_s); free(lt_s); free(*rt_s); free(rt_s); free(**uoz ); free(*uoz ); free(uoz) ; free(**uoz_p); free(*uoz_p); free(uoz_p); free(**uoz_s); free(*uoz_s); free(uoz_s); /*free(**upz); free(*upz); free(upz);*/ free(uo); free(uo_p); free(uo_s); /*free(up);*/ if (snap) { free(**uc); free(*uc); free(uc); } /*------------------------------------------------------------*/ exit (0); }
int main (int argc, char *argv[]) { int adj; /* forward or adjoint */ int eic; /* EIC or CIC */ bool verb; /* verbosity */ float eps; /* dip filter constant */ int nrmax; /* number of reference velocities */ float dtmax; /* time error */ int pmx,pmy; /* padding in the k domain */ int tmx,tmy; /* boundary taper size */ int nhx, nhy, nhz, nht, nc; int nhx2,nhy2,nhz2,nht2; float dht, oht; sf_axis amx,amy,az; sf_axis alx,aly; sf_axis aw,ae,ac,aa; sf_axis ahx,ahy,ahz,aht; /* I/O files */ sf_file Bws=NULL; /* background wavefield file Bws */ sf_file Bwr=NULL; /* background wavefield file Bwr */ sf_file Bs=NULL; /* background slowness file Bs */ sf_file Ps=NULL; /* slowness perturbation file Ps */ sf_file Pi=NULL; /* image perturbation file Pi */ sf_file Fc=NULL; /* CIP coordinates */ sf_file Pws=NULL; /* perturbed wavefield file Pws */ sf_file Pwr=NULL; /* perturbed wavefield file Pwr */ sf_file Pti=NULL; int ompnth=1; wexcub3d cub; /* wavefield hypercube */ wexcip3d cip; /* CIP gathers */ wextap3d tap; /* tapering */ wexssr3d ssr; /* SSR operator */ wexlsr3d lsr; /* LSR operator */ wexslo3d slo; /* slowness */ wexmvaop3d mva; float dsmax; /*------------------------------------------------------------*/ sf_init(argc,argv); /* OMP parameters */ #ifdef _OPENMP ompnth=omp_init(); #endif if (!sf_getbool( "verb",&verb )) verb = true; /* verbosity flag */ if (!sf_getint( "adj",&adj )) sf_error("Specify adjoint!"); /* y=ADJ Back-projection; n=FWD Forward Scattering */ if (!sf_getint( "feic",&eic )) sf_error("Specify EIC!"); /* extended I.C. flag */ if (!sf_getfloat( "eps",&eps )) eps = 0.01; /* stability parameter */ if (!sf_getint( "nrmax",&nrmax)) nrmax = 1; /* max number of refs */ if (!sf_getfloat("dtmax",&dtmax)) dtmax = 0.004; /* max time error */ if (!sf_getint( "pmx",&pmx )) pmx = 0; /* padding on x */ if (!sf_getint( "pmy",&pmy )) pmy = 0; /* padding on y */ if (!sf_getint( "tmx",&tmx )) tmx = 0; /* taper on x */ if (!sf_getint( "tmy",&tmy )) tmy = 0; /* taper on y */ ae = sf_maxa(1,0,1); nhx=nhy=nhz=nht=nc=nhx2=nhy2=nhz2=nht2=0; oht = dht = 0.0; /*------------------------------------------------------------*/ /* slowness */ Bs = sf_input("slo"); alx = sf_iaxa(Bs,1); sf_setlabel(alx,"lx"); aly = sf_iaxa(Bs,2); sf_setlabel(aly,"ly"); az = sf_iaxa(Bs,3); sf_setlabel(az, "z"); /*------------------------------------------------------------*/ /* input file */ if(adj) Pi = sf_input("in"); else Ps = sf_input("in"); /*------------------------------------------------------------*/ /* wavefield */ Bws = sf_input("swfl"); Bwr = sf_input("rwfl"); amx = sf_iaxa(Bws,1); sf_setlabel(amx,"mx"); amy = sf_iaxa(Bws,2); sf_setlabel(amy,"my"); aw = sf_iaxa(Bws,4); sf_setlabel(aw ,"w" ); Pws = sf_tmpfile(NULL); sf_settype(Pws,SF_COMPLEX); Pwr = sf_tmpfile(NULL); sf_settype(Pwr,SF_COMPLEX); /*------------------------------------------------------------*/ cub = wex_cube(verb, amx,amy,az, alx,aly, aw, ae, eps, ompnth); dsmax = dtmax/cub->az.d; /*------------------------------------------------------------*/ /* init structures */ tap = wextap_init(cub->amx.n, cub->amy.n, 1, SF_MIN(tmx,cub->amx.n-1), /* tmx */ SF_MIN(tmy,cub->amy.n-1), /* tmy */ 0, true,true,false); slo = wexslo_init(cub,Bs,nrmax,dsmax); ssr = wexssr_init(cub,slo,pmx,pmy,tmx,tmy,dsmax); lsr = wexlsr_init(cub,pmx,pmy,dsmax); /*------------------------------------------------------------*/ Pti = sf_tmpfile(NULL); sf_settype(Pti,SF_COMPLEX); /*------------------------------------------------------------*/ /* WEMVA */ if(adj) { sf_warning("adjoint operator..."); if(eic){ ahx = sf_iaxa(Pi,1); sf_setlabel(ahx,"hx"); ahy = sf_iaxa(Pi,2); sf_setlabel(ahy,"hy"); ahz = sf_iaxa(Pi,3); sf_setlabel(ahz,"hz"); aht = sf_iaxa(Pi,4); sf_setlabel(aht,"ht"); dht = sf_d(aht); oht = sf_o(aht); nhx2 = sf_n(ahx); nhx = (nhx2-1)/2; nhy2 = sf_n(ahy); nhy = (nhy2-1)/2; nhz2 = sf_n(ahz); nhz = (nhz2-1)/2; nht2 = sf_n(aht); nht = (nht2-1)/2; /* CIP coordinates */ Fc = sf_input ("cc" ); ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,""); nc = sf_n(ac); } cip = wexcip_init(cub,nhx,nhy,nhz,nht,nhx2,nhy2,nhz2,nht2,nc,dht,oht,Fc,eic); mva = wexmva_init(cub,cip); Ps = sf_output("out"); sf_settype(Ps,SF_COMPLEX); sf_oaxa(Ps,amx,1); sf_oaxa(Ps,amy,2); sf_oaxa(Ps,az, 3); if(eic){ sf_oaxa(Ps,ae, 4); sf_oaxa(Ps,ae, 5);} /* Adjoint I.C. operator, dI -> dW */ wexcip_adj(cub,cip,Bwr,Pws,Pi,eic,1,1); /* Ws dR */ wexcip_adj(cub,cip,Bws,Pwr,Pi,eic,0,0); /* Wr dR */ sf_filefresh(Pws); sf_filefresh(Pwr); /* Adjoint WEMVA operator, dW -> dS */ wexmva(mva,adj,cub,ssr,lsr,tap,slo,Bws,Bwr,Pws,Pwr,Ps); } else { /* set up the I/O of output CIP gathers */ Pi = sf_output("out"); sf_settype(Pi,SF_COMPLEX); if(eic){ /* CIP coordinates */ Fc = sf_input ("cc" ); ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,""); nc = sf_n(ac); if(! sf_getint("nhx",&nhx)) nhx=0; /* number of lags on the x axis */ if(! sf_getint("nhy",&nhy)) nhy=0; /* number of lags on the y axis */ if(! sf_getint("nhz",&nhz)) nhz=0; /* number of lags on the z axis */ if(! sf_getint("nht",&nht)) nht=0; /* number of lags on the t axis */ if(! sf_getfloat("dht",&dht)) sf_error("need dht"); oht = -nht*dht; nhx2=2*nhx+1; nhy2=2*nhy+1; nhz2=2*nhz+1; nht2=2*nht+1; aa=sf_maxa(nhx2,-nhx*cub->amx.d,cub->amx.d); sf_setlabel(aa,"hx"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,1); aa=sf_maxa(nhy2,-nhy*cub->amy.d,cub->amy.d); sf_setlabel(aa,"hy"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,2); aa=sf_maxa(nhz2,-nhz*cub->az.d,cub->az.d); sf_setlabel(aa,"hz"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,3); aa=sf_maxa(nht2,-nht*dht,dht); sf_setlabel(aa,"ht"); sf_setunit(aa,"s"); if(verb) sf_raxa(aa); sf_oaxa(Pi,aa,4); sf_oaxa(Pi,ac,5); } else{ sf_oaxa(Pi,amx,1); sf_oaxa(Pi,amy,2); sf_oaxa(Pi,az, 3); } cip = wexcip_init(cub,nhx,nhy,nhz,nht,nhx2,nhy2,nhz2,nht2,nc,dht,oht,Fc,eic); mva = wexmva_init(cub,cip); /* WEMVA operator, dS -> dW */ wexmva(mva,adj,cub,ssr,lsr,tap,slo,Bws,Bwr,Pws,Pwr,Ps); sf_filefresh(Pws); sf_filefresh(Pwr); /* I.C. operator, dW -> dI */ wexcip_for(cub,cip,Bws,Pwr,Pti,eic,0,0); /* CONJ( Ws) dWr */ sf_seek(Pti,(off_t)0,SEEK_SET); wexcip_for(cub,cip,Pws,Bwr,Pti,eic,0,1); /* CONJ(dWs) Wr */ sf_filefresh(Pti); sf_filecopy(Pi,Pti,SF_COMPLEX); } /*------------------------------------------------------------*/ /* close structures */ wexslo_close(slo); wexssr_close(cub,ssr); wextap2D_close(tap); wexmva_close(mva); wexcip_close(cip,eic); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* close files */ if (Ps!=NULL) sf_fileclose(Ps); if (Fc!=NULL) sf_fileclose(Fc); if (Pi!=NULL) sf_fileclose(Pi); if (Bws!=NULL) sf_fileclose(Bws); if (Bwr!=NULL) sf_fileclose(Bwr); if (Pws!=NULL) sf_tmpfileclose(Pws); if (Pwr!=NULL) sf_tmpfileclose(Pwr); if (Pti!=NULL) sf_tmpfileclose(Pti); /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { bool adj, snap; /* adjoint flag */ int ix, iz, it; /* index variables */ int nt, nx, nz, n0, jt, n12, padx, padz, n2; float dt, dx, dz, dt2, idz2, idx2; float **dd, **mm, **vv; float **u0, **u1, **u2, **tmp; /* temporary arrays */ sf_file in, out, vel, wave; /* I/O files */ /* initialize Madagascar */ sf_init(argc,argv); /* initialize OpenMP support */ #ifdef _OPENMP omp_init(); #endif if(!sf_getbool("adj", &adj)) adj=true; /* adjoint flag, 0: modeling, 1: migration */ if(!sf_getbool("snap", &snap)) snap=false; /* wavefield snapshot flag */ if(!sf_getint("n0", &n0)) n0=0; /* surface */ if(!sf_getint("jt", &jt)) jt=50; /* time interval of wavefield snapshot */ /* setup I/O files */ in=sf_input("in"); out=sf_output("out"); vel=sf_input("velocity"); /* velocity model */ /* Dimensions */ if(!sf_histint(vel, "n1", &nz)) sf_error("No n1= in velocity"); if(!sf_histint(vel, "n2", &nx)) sf_error("No n2= in velocity"); if(!sf_histfloat(vel, "d1", &dz)) sf_error("No d1= in velocity"); if(!sf_histfloat(vel, "d2", &dx)) sf_error("No d2= in velocity"); if(adj){ /* migration */ if(!sf_histint(in, "n1", &nt)) sf_error("No n1= in data"); if(!sf_histfloat(in, "d1", &dt)) sf_error("No d1= in data"); if(!sf_histint(in, "n2", &n2) || n2!=nx) sf_error("Need n2=%d in data", nx); sf_putint(out, "n1", nz); sf_putfloat(out, "d1", dz); sf_putfloat(out, "o1", 0.0); sf_putstring(out, "label1", "Depth"); sf_putstring(out, "unit1", "km"); sf_putstring(out, "label2", "Lateral"); sf_putstring(out, "unit2", "km"); }else{ /* modeling */ if(!sf_getint("nt", &nt)) sf_error("Need nt="); if(!sf_getfloat("dt", &dt)) sf_error("Need dt="); sf_putint(out, "n1", nt); sf_putfloat(out, "d1", dt); sf_putfloat(out, "o1", 0.0); sf_putstring(out, "label1", "Time"); sf_putstring(out, "unit1", "s"); sf_putstring(out, "label2", "Lateral"); sf_putstring(out, "unit2", "km"); } /* lengths of padding boundary */ if(!sf_getint("padx", &padx)) padx=nz/2; if(!sf_getint("padz", &padz)) padz=nz/2; padnx=nx+2*padx; padnz=nz+2*padz; n0=n0+padz; n12=padnz*padnx; /* set Laplacian coefficients */ idz2=1.0/(dz*dz); idx2=1.0/(dx*dx); c11=4.0*idz2/3.0; c12=-idz2/12.0; c21=4.0*idx2/3.0; c22=-idx2/12.0; c0=-2.0*(c11+c12+c21+c22); /* wavefield snapshot */ if(snap){ wave=sf_output("wave"); sf_putint(wave, "n1", padnz); sf_putint(wave, "d1", 1); sf_putint(wave, "o1", -padz); sf_putint(wave, "n2", padnx); sf_putint(wave, "d2", 1); sf_putint(wave, "o2", -padx); sf_putint(wave, "n3", 1+(nt-1)/jt); if(adj){ sf_putfloat(wave, "d3", -jt*dt); sf_putfloat(wave, "o3", (nt-1)*dt); }else{ sf_putfloat(wave, "d3", jt*dt); sf_putfloat(wave, "o3", 0.0); } } /* allocate arrays */ vv=sf_floatalloc2(nz, nx); dd=sf_floatalloc2(nt, nx); mm=sf_floatalloc2(nz, nx); padvv=sf_floatalloc2(padnz, padnx); u0=sf_floatalloc2(padnz, padnx); u1=sf_floatalloc2(padnz, padnx); u2=sf_floatalloc2(padnz, padnx); /* read velocity */ sf_floatread(vv[0], nz*nx, vel); /* pad boundary */ dt2=dt*dt; for(ix=0; ix<nx; ix++) for(iz=0; iz<nz; iz++) padvv[ix+padx][iz+padz]=vv[ix][iz]*vv[ix][iz]*dt2; for(iz=0; iz<padz; iz++){ for(ix=padx; ix<nx+padx; ix++){ padvv[ix][iz]=padvv[ix][padz]; padvv[ix][iz+nz+padz]=padvv[ix][nz+padz-1]; } } for(ix=0; ix<padx; ix++){ for(iz=0; iz<padnz; iz++){ padvv[ix][iz]=padvv[padx][iz]; padvv[ix+nx+padx][iz]=padvv[nx+padx-1][iz]; } } memset(u0[0], 0.0, n12*sizeof(float)); memset(u1[0], 0.0, n12*sizeof(float)); memset(u2[0], 0.0, n12*sizeof(float)); if(adj){ /* migration */ /* read data */ sf_floatread(dd[0], nt*nx, in); for(it=nt-1; it>=0; it--){ sf_warning("Migration: %d;", it); laplacian(adj, u0, u1, u2); tmp=u0; u0=u1; u1=u2; u2=tmp; #ifdef _OPENMP #pragma omp parallel for default(none) private(ix) shared(padx, nx, dd, u1, it, n0) #endif for(ix=padx; ix<padx+nx; ix++) /* inject data */ u1[ix][n0]+=dd[ix-padx][it]; if(snap && it%jt==0) sf_floatwrite(u1[0], n12, wave); } sf_warning("."); /* output image */ for(ix=0; ix<nx; ix++) for(iz=0; iz<nz; iz++) mm[ix][iz]=u1[ix+padx][iz+padz]; sf_floatwrite(mm[0], nz*nx, out); }else{/* modeling */ /* read reflector */ sf_floatread(mm[0], nz*nx, in); for(ix=0; ix<nx; ix++) for(iz=0; iz<nz; iz++) u1[ix+padx][iz+padz]=mm[ix][iz]; for(it=0; it<nt; it++){ sf_warning("Modeling: %d;", it); if(snap && it%jt==0) sf_floatwrite(u1[0], n12, wave); #ifdef _OPENMP #pragma omp parallel for default(none) private(ix) shared(padx, nx, dd, u1, it, n0) #endif for(ix=padx; ix<padx+nx; ix++) /* record data */ dd[ix-padx][it]=u1[ix][n0]; laplacian(adj, u0, u1, u2); tmp=u0; u0=u1; u1=u2; u2=tmp; } sf_warning("."); /* output data */ sf_floatwrite(dd[0], nt*nx, out); } free(*padvv); free(padvv); free(*vv); free(vv); free(*dd); free(dd); free(*mm); free(mm); free(*u0); free(u0); free(*u1); free(u1); free(*u2); free(u2); exit (0); }
int main(int argc, char* argv[]) { bool verb,fsrf,snap,ssou,dabc,opot; int jsnap,ntsnap,jdata; /* I/O files */ sf_file Fwav=NULL; /* wavelet */ sf_file Fsou=NULL; /* sources */ sf_file Frec=NULL; /* receivers */ sf_file Fccc=NULL; /* velocity */ sf_file Fden=NULL; /* density */ sf_file Fdat=NULL; /* data */ sf_file Fwfl=NULL; /* wavefield */ /* cube axes */ sf_axis at,ax,ay,az; sf_axis as,ar,ac; int nt,nz,nx,ny,ns,nr,nc,nb; int it,iz,ix,iy; float dt,dz,dx,dy,idz,idx,idy; /* FDM structure */ fdm3d fdm=NULL; abcone3d /* abcp=NULL, */ abcs=NULL; sponge spo=NULL; /* I/O arrays */ float***ww=NULL; /* wavelet */ pt3d *ss=NULL; /* sources */ pt3d *rr=NULL; /* receivers */ float **dd=NULL; /* data */ /*------------------------------------------------------------*/ float ***tt=NULL; float ***ro=NULL; /* density */ /* orthorombic footprint - 9 coefficients */ /* c11 c12 c13 . c22 c23 . . c33 c44 c55 c66 */ float ***c11=NULL; float ***c22=NULL; float ***c33=NULL; float ***c44=NULL; float ***c55=NULL; float ***c66=NULL; float ***c12=NULL; float ***c13=NULL; float ***c23=NULL; float ***vp,***vs; float ***qp=NULL,***qsx=NULL,***qsy=NULL,***qsz=NULL; /*------------------------------------------------------------*/ /* displacement: um = U @ t-1; uo = U @ t; up = U @ t+1 */ float ***umz,***uoz,***upz,***uaz,***utz; float ***umx,***uox,***upx,***uax,***utx; float ***umy,***uoy,***upy,***uay,***uty; /* stress/strain tensor */ float ***tzz,***txx,***tyy,***txy,***tyz,***tzx; float szz, sxx, syy, sxy, syz, szx; /*------------------------------------------------------------*/ /* linear interpolation weights/indices */ lint3d cs,cr; /* Gaussian bell */ int nbell; /* wavefield cut params */ sf_axis acz=NULL,acx=NULL,acy=NULL; int nqz,nqx,nqy; float oqz,oqx,oqy; float dqz,dqx,dqy; float ***uc=NULL; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /*------------------------------------------------------------*/ /* OMP parameters */ #ifdef _OPENMP omp_init(); #endif /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* execution flags */ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("snap",&snap)) snap=false; /* wavefield snapshots flag */ if(! sf_getbool("free",&fsrf)) fsrf=false; /* free surface flag */ if(! sf_getbool("ssou",&ssou)) ssou=false; /* stress source */ if(! sf_getbool("dabc",&dabc)) dabc=false; /* absorbing BC */ if(! sf_getbool("opot",&opot)) opot=false; /* output potentials */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* I/O files */ Fwav = sf_input ("in" ); /* wavelet */ Fccc = sf_input ("ccc"); /* stiffness */ Fden = sf_input ("den"); /* density */ Fsou = sf_input ("sou"); /* sources */ Frec = sf_input ("rec"); /* receivers */ Fwfl = sf_output("wfl"); /* wavefield */ Fdat = sf_output("out"); /* data */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* axes */ at = sf_iaxa(Fwav,3); sf_setlabel(at,"t"); if(verb) sf_raxa(at); /* time */ az = sf_iaxa(Fccc,1); sf_setlabel(az,"z"); if(verb) sf_raxa(az); /* depth */ ax = sf_iaxa(Fccc,2); sf_setlabel(ax,"x"); if(verb) sf_raxa(ax); /* space x */ ay = sf_iaxa(Fccc,3); sf_setlabel(ay,"y"); if(verb) sf_raxa(ay); /* space y */ as = sf_iaxa(Fsou,2); sf_setlabel(as,"s"); if(verb) sf_raxa(as); /* sources */ ar = sf_iaxa(Frec,2); sf_setlabel(ar,"r"); if(verb) sf_raxa(ar); /* receivers */ nt = sf_n(at); dt = sf_d(at); nz = sf_n(az); dz = sf_d(az); nx = sf_n(ax); dx = sf_d(ax); ny = sf_n(ay); dy = sf_d(ay); ns = sf_n(as); nr = sf_n(ar); /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* other execution parameters */ if(! sf_getint("nbell",&nbell)) nbell=5; /* bell size */ if(verb) sf_warning("nbell=%d",nbell); if(! sf_getint("jdata",&jdata)) jdata=1; if(snap) { /* save wavefield every *jsnap* time steps */ if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* expand domain for FD operators and ABC */ if( !sf_getint("nb",&nb) || nb<NOP) nb=NOP; fdm=fdutil3d_init(verb,fsrf,az,ax,ay,nb,1); fdbell3d_init(nbell); sf_setn(az,fdm->nzpad); sf_seto(az,fdm->ozpad); if(verb) sf_raxa(az); sf_setn(ax,fdm->nxpad); sf_seto(ax,fdm->oxpad); if(verb) sf_raxa(ax); sf_setn(ay,fdm->nypad); sf_seto(ay,fdm->oypad); if(verb) sf_raxa(ay); /*------------------------------------------------------------*/ /* 3D vector components */ nc=3; if(opot) { ac=sf_maxa(nc+1,0,1); } else { ac=sf_maxa(nc ,0,1); } /*------------------------------------------------------------*/ /* setup output data header */ sf_oaxa(Fdat,ar,1); sf_oaxa(Fdat,ac,2); sf_setn(at,nt/jdata); sf_setd(at,dt*jdata); sf_oaxa(Fdat,at,3); /* setup output wavefield header */ if(snap) { if(!sf_getint ("nqz",&nqz)) nqz=sf_n(az); if(!sf_getint ("nqx",&nqx)) nqx=sf_n(ax); if(!sf_getint ("nqy",&nqy)) nqy=sf_n(ay); if(!sf_getfloat("oqz",&oqz)) oqz=sf_o(az); if(!sf_getfloat("oqx",&oqx)) oqx=sf_o(ax); if(!sf_getfloat("oqy",&oqy)) oqy=sf_o(ay); dqz=sf_d(az); dqx=sf_d(ax); dqy=sf_d(ay); acz = sf_maxa(nqz,oqz,dqz); sf_raxa(acz); acx = sf_maxa(nqx,oqx,dqx); sf_raxa(acx); acy = sf_maxa(nqy,oqy,dqy); sf_raxa(acy); /* TODO: check if the imaging window fits in the wavefield domain */ uc=sf_floatalloc3(sf_n(acz),sf_n(acx),sf_n(acy)); ntsnap=0; for(it=0; it<nt; it++) { if(it%jsnap==0) ntsnap++; } sf_setn(at, ntsnap); sf_setd(at,dt*jsnap); if(verb) sf_raxa(at); sf_oaxa(Fwfl,acz,1); sf_oaxa(Fwfl,acx,2); sf_oaxa(Fwfl,acy,3); sf_oaxa(Fwfl,ac, 4); sf_oaxa(Fwfl,at, 5); } /*------------------------------------------------------------*/ /* source array */ ww=sf_floatalloc3(ns,nc,nt); sf_floatread(ww[0][0],nt*nc*ns,Fwav); /* data array */ if(opot) { dd=sf_floatalloc2(nr,nc+1); } else { dd=sf_floatalloc2(nr,nc ); } /*------------------------------------------------------------*/ /* setup source/receiver coordinates */ ss = (pt3d*) sf_alloc(ns,sizeof(*ss)); rr = (pt3d*) sf_alloc(nr,sizeof(*rr)); pt3dread1(Fsou,ss,ns,3); /* read (x,y,z) coordinates */ pt3dread1(Frec,rr,nr,3); /* read (x,y,z) coordinates */ cs = lint3d_make(ns,ss,fdm); cr = lint3d_make(nr,rr,fdm); /*------------------------------------------------------------*/ /* setup FD coefficients */ /* idz = 2/dz;*/ /* idx = 2/dx;*/ /* idy = 2/dy;*/ idz = 1/dz; idx = 1/dx; idy = 1/dy; /*------------------------------------------------------------*/ tt = sf_floatalloc3(nz,nx,ny); ro =sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c11=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c22=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c33=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c44=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c55=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c66=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c12=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c13=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); c23=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); /* input density */ sf_floatread(tt[0][0],nz*nx*ny,Fden); expand3d(tt,ro ,fdm); /* input stiffness */ sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c11,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c22,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c33,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c44,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c55,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c66,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c12,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c13,fdm); sf_floatread(tt[0][0],nz*nx*ny,Fccc ); expand3d(tt,c23,fdm); free(**tt); free(*tt); free(tt); /*------------------------------------------------------------*/ if(dabc) { /* one-way abc setup */ vp = sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); vs = sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { vp[iy][ix][iz] = sqrt( c11[iy][ix][iz]/ro[iy][ix][iz] ); vs[iy][ix][iz] = sqrt( c55[iy][ix][iz]/ro[iy][ix][iz] ); } } } /* abcp = abcone3d_make(NOP,dt,vp,fsrf,fdm); */ abcs = abcone3d_make(NOP,dt,vs,fsrf,fdm); free(**vp); free(*vp); free(vp); free(**vs); free(*vs); free(vs); /* sponge abc setup */ spo = sponge_make(fdm->nb); } /*------------------------------------------------------------*/ /* precompute 1/ro * dt^2 */ for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { ro[iy][ix][iz] = dt*dt/ro[iy][ix][iz]; } } } /*------------------------------------------------------------*/ /* allocate wavefield arrays */ umz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); upz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uaz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); umx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uox=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); upx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uax=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); umy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uoy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); upy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); uay=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); tzz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); tyy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); txx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); txy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); tyz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); tzx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { umz[iy][ix][iz]=0; umx[iy][ix][iz]=0; umy[iy][ix][iz]=0; uoz[iy][ix][iz]=0; uox[iy][ix][iz]=0; uoy[iy][ix][iz]=0; upz[iy][ix][iz]=0; upx[iy][ix][iz]=0; upy[iy][ix][iz]=0; uaz[iy][ix][iz]=0; uax[iy][ix][iz]=0; uay[iy][ix][iz]=0; } } } if(opot) { qp =sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); qsx=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); qsy=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); qsz=sf_floatalloc3(fdm->nzpad,fdm->nxpad,fdm->nypad); } /*------------------------------------------------------------*/ /* * MAIN LOOP */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",it); /*------------------------------------------------------------*/ /* from displacement to strain */ /*------------------------------------------------------------*/ /* * exx = Fx(ux) * eyy = Fy(uy) * ezz = Fz(uz) * exy = By(ux) + Bx(uy) * eyz = Bz(uy) + By(uz) * ezx = Bx(uz) + Bz(ux) */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iy,iz) \ shared(fdm,txx,tyy,tzz,txy,tyz,tzx,uox,uoy,uoz,idx,idy,idz) #endif for (iy=NOP; iy<fdm->nypad-NOP; iy++) { for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { txx[iy][ix][iz] = Dx(uox,ix,iy,iz,idx); tyy[iy][ix][iz] = Dy(uoy,ix,iy,iz,idy); tzz[iy][ix][iz] = Dz(uoz,ix,iy,iz,idz); txy[iy][ix][iz] = Dy(uox,ix,iy,iz,idy) + Dx(uoy,ix,iy,iz,idx); tyz[iy][ix][iz] = Dz(uoy,ix,iy,iz,idz) + Dy(uoz,ix,iy,iz,idy); tzx[iy][ix][iz] = Dx(uoz,ix,iy,iz,idx) + Dz(uox,ix,iy,iz,idz); } } } /*------------------------------------------------------------*/ /* from strain to stress */ /*------------------------------------------------------------*/ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iy,iz,sxx,syy,szz,sxy,syz,szx) \ shared(fdm,txx,tyy,tzz,txy,tyz,tzx,c11,c22,c33,c44,c55,c66,c12,c13,c23) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { sxx = c11[iy][ix][iz] * txx[iy][ix][iz] + c12[iy][ix][iz] * tyy[iy][ix][iz] + c13[iy][ix][iz] * tzz[iy][ix][iz]; syy = c12[iy][ix][iz] * txx[iy][ix][iz] + c22[iy][ix][iz] * tyy[iy][ix][iz] + c23[iy][ix][iz] * tzz[iy][ix][iz]; szz = c13[iy][ix][iz] * txx[iy][ix][iz] + c23[iy][ix][iz] * tyy[iy][ix][iz] + c33[iy][ix][iz] * tzz[iy][ix][iz]; sxy = c66[iy][ix][iz] * txy[iy][ix][iz]; syz = c44[iy][ix][iz] * tyz[iy][ix][iz]; szx = c55[iy][ix][iz] * tzx[iy][ix][iz]; txx[iy][ix][iz] = sxx; tyy[iy][ix][iz] = syy; tzz[iy][ix][iz] = szz; txy[iy][ix][iz] = sxy; tyz[iy][ix][iz] = syz; tzx[iy][ix][iz] = szx; } } } /*------------------------------------------------------------*/ /* free surface */ /*------------------------------------------------------------*/ if(fsrf) { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iy,iz) \ shared(fdm,txx,tyy,tzz,txy,tyz,tzx) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nb; iz++) { txx[iy][ix][iz]=0; tyy[iy][ix][iz]=0; tzz[iy][ix][iz]=0; txy[iy][ix][iz]=0; tyz[iy][ix][iz]=0; tzx[iy][ix][iz]=0; } } } } /*------------------------------------------------------------*/ /* inject stress source */ /*------------------------------------------------------------*/ if(ssou) { lint3d_bell(tzz,ww[it][0],cs); lint3d_bell(txx,ww[it][1],cs); lint3d_bell(tyy,ww[it][2],cs); } /*------------------------------------------------------------*/ /* from stress to acceleration */ /*------------------------------------------------------------*/ /* * ax = Bx(txx) + Fy(txy) + Fz(txz) * ay = Fx(txy) + By(tyy) + Fz(tyz) * az = Fx(txz) + Fy(tyz) + Bz(tzz) */ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iy,iz) \ shared(fdm,txx,tyy,tzz,txy,tyz,tzx,uax,uay,uaz,idx,idy,idz) #endif for (iy=NOP; iy<fdm->nypad-NOP; iy++) { for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { uax[iy][ix][iz] = Dx( txx,ix,iy,iz,idx ) + Dy( txy,ix,iy,iz,idy ) + Dz( tzx,ix,iy,iz,idz ) ; uay[iy][ix][iz] = Dx( txy,ix,iy,iz,idx ) + Dy( tyy,ix,iy,iz,idy ) + Dz( tyz,ix,iy,iz,idz ) ; uaz[iy][ix][iz] = Dx( tzx,ix,iy,iz,idx ) + Dy( tyz,ix,iy,iz,idy ) + Dz( tzz,ix,iy,iz,idz ) ; } } } /*------------------------------------------------------------*/ /* inject acceleration source */ /*------------------------------------------------------------*/ if(!ssou) { lint3d_bell(uaz,ww[it][0],cs); lint3d_bell(uax,ww[it][1],cs); lint3d_bell(uay,ww[it][2],cs); } /*------------------------------------------------------------*/ /* step forward in time */ /*------------------------------------------------------------*/ #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iy,iz) \ shared(fdm,uox,uoy,uoz,umx,umy,umz,upx,upy,upz,uax,uay,uaz,ro) #endif for (iy=0; iy<fdm->nypad; iy++) { for (ix=0; ix<fdm->nxpad; ix++) { for(iz=0; iz<fdm->nzpad; iz++) { upx[iy][ix][iz] = 2*uox[iy][ix][iz] - umx[iy][ix][iz] + uax[iy][ix][iz] * ro[iy][ix][iz]; upy[iy][ix][iz] = 2*uoy[iy][ix][iz] - umy[iy][ix][iz] + uay[iy][ix][iz] * ro[iy][ix][iz]; upz[iy][ix][iz] = 2*uoz[iy][ix][iz] - umz[iy][ix][iz] + uaz[iy][ix][iz] * ro[iy][ix][iz]; } } } /* circulate wavefield arrays */ utz=umz; uty=umy; utx=umx; umz=uoz; umy=uoy; umx=uox; uoz=upz; uoy=upy; uox=upx; upz=utz; upy=uty; upx=utx; if(dabc) { /* one-way ABC */ /* abcone3d_apply(uoz,umz,NOP,abcp,fdm); */ /* abcone3d_apply(uox,umx,NOP,abcp,fdm); */ /* abcone3d_apply(uoy,umy,NOP,abcp,fdm); */ abcone3d_apply(uoz,umz,NOP,abcs,fdm); abcone3d_apply(uox,umx,NOP,abcs,fdm); abcone3d_apply(uoy,umy,NOP,abcs,fdm); /* sponge ABC */ sponge3d_apply(umz,spo,fdm); sponge3d_apply(uoz,spo,fdm); sponge3d_apply(umx,spo,fdm); sponge3d_apply(uox,spo,fdm); sponge3d_apply(umy,spo,fdm); sponge3d_apply(uoy,spo,fdm); } /*------------------------------------------------------------*/ /* cut wavefield and save */ /*------------------------------------------------------------*/ if(opot) { #ifdef _OPENMP #pragma omp parallel for \ schedule(dynamic,fdm->ompchunk) \ private(ix,iy,iz) \ shared(fdm,uox,uoy,uoz,idx,idy,idz) #endif for (iy=NOP; iy<fdm->nypad-NOP; iy++) { for (ix=NOP; ix<fdm->nxpad-NOP; ix++) { for(iz=NOP; iz<fdm->nzpad-NOP; iz++) { qp [iy][ix][iz] = Dx( uox,ix,iy,iz,idx ) + Dy( uoy,ix,iy,iz,idy ) + Dz( uoz,ix,iy,iz,idz ); qsx[iy][ix][iz] = Dy( uoz,ix,iy,iz,idy ) - Dz( uoy,ix,iy,iz,idz ); qsy[iy][ix][iz] = Dz( uox,ix,iy,iz,idz ) - Dx( uoz,ix,iy,iz,idx ); qsz[iy][ix][iz] = Dx( uoy,ix,iy,iz,idx ) - Dy( uox,ix,iy,iz,idy ); } } } if(snap && it%jsnap==0) { cut3d(qp ,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); cut3d(qsz,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); cut3d(qsx,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); cut3d(qsy,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); } lint3d_extract(qp , dd[0],cr); lint3d_extract(qsx, dd[1],cr); lint3d_extract(qsy, dd[2],cr); lint3d_extract(qsz, dd[3],cr); if(it%jdata==0) sf_floatwrite(dd[0],nr*(nc+1),Fdat); } else { if(snap && it%jsnap==0) { cut3d(uoz,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); cut3d(uox,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); cut3d(uoy,uc,fdm,acz,acx,acy); sf_floatwrite(uc[0][0],sf_n(acx)*sf_n(acy)*sf_n(acz),Fwfl); } lint3d_extract(uoz,dd[0],cr); lint3d_extract(uox,dd[1],cr); lint3d_extract(uoy,dd[2],cr); if(it%jdata==0) sf_floatwrite(dd[0],nr*nc,Fdat); } } if(verb) fprintf(stderr,"\n"); /*------------------------------------------------------------*/ /* deallocate arrays */ free(**ww); free(*ww); free(ww); free(ss); free(rr); free(*dd); free(dd); free(**ro); free(*ro); free(ro); free(**c11); free(*c11); free(c11); free(**c22); free(*c22); free(c22); free(**c33); free(*c33); free(c33); free(**c44); free(*c44); free(c44); free(**c55); free(*c55); free(c55); free(**c66); free(*c66); free(c66); free(**c12); free(*c12); free(c12); free(**c13); free(*c13); free(c13); free(**c23); free(*c23); free(c23); free(**umz); free(*umz); free(umz); free(**uoz); free(*uoz); free(uoz); free(**upz); free(*upz); free(upz); free(**uaz); free(*uaz); free(uaz); free(**umx); free(*umx); free(umx); free(**uox); free(*uox); free(uox); free(**upx); free(*upx); free(upx); free(**uax); free(*uax); free(uax); free(**umy); free(*umy); free(umy); free(**uoy); free(*uoy); free(uoy); free(**upy); free(*upy); free(upy); free(**uay); free(*uay); free(uay); free(**tzz); free(*tzz); free(tzz); free(**txx); free(*txx); free(txx); free(**tyy); free(*tyy); free(tyy); free(**txy); free(*txy); free(txy); free(**tyz); free(*tyz); free(tyz); free(**tzx); free(*tzx); free(tzx); if (snap) { free(**uc); free(*uc); free(uc); } if(opot) { free(**qp); free(*qp); free(qp); free(**qsx); free(*qsx); free(qsx); free(**qsy); free(*qsy); free(qsy); free(**qsz); free(*qsz); free(qsz); } /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { bool verb,conj,twin,pandq,Gtot,Htot; /* OMP parameters */ #ifdef _OPENMP int ompnth; #endif float *pplus0, *pplus, *pplusinv, *pplustemp, *Pplus, *Pplus_trace, *pminus, *Pminus, *Refl, *Gp, *Gm, *G, *H; float *qplus, *qplusinv, *qplustemp, *Qplus, *Qplus_trace, *qminus, *Qminus; float *window, *taper, pi; int *tw; /* I/O files */ sf_file Fplus; sf_file FRefl; sf_file FGp; sf_file FGm; sf_file FG; sf_file FH; sf_file Ftwin; sf_file Fp; sf_file Fq; char *filename1, filename2[256], filename3[256]; /* Cube axes */ sf_axis at,af,ax; int nt,nf,ntr,mode,nshots,niter,len; int i,it,ix,ishot,iter,i0; int twc, twa, shift, n[2], rect[2], s[2]; float scale,eps,dt,df,dx,ot,of,a,b,c,d,e,f; sf_triangle tr; /*------------------------------------------------------------*/ /* Initialize RSF parameters */ /*------------------------------------------------------------*/ sf_init(argc,argv); /*------------------------------------------------------------*/ /* Initialize OMP parameters */ /*------------------------------------------------------------*/ #ifdef _OPENMP ompnth=omp_init(); #endif /*------------------------------------------------------------*/ /* Flags */ /*------------------------------------------------------------*/ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("conj",&conj)) conj=false; /* complex conjugation (time-reversal) flag */ if(! sf_getbool("twin",&twin)) twin=false; /* returns the timewindow as one of the outputs */ if(! sf_getbool("pandq",&pandq)) pandq=false; /* pandq=true: returns p and q */ if(! sf_getbool("Gtot",&Gtot)) Gtot=false; /* Gtot=true: returns G=Gp+Gm */ if(! sf_getbool("Htot",&Htot)) Htot=false; /* Htot=true: returns H=Gp-Gm */ if(! sf_getint("niter",&niter)) niter=1; /* number of iterations */ if(! sf_getint("nshots",&nshots)) nshots=1; /* number of shots */ if(! sf_getfloat("scale",&scale)) scale=1.0; /* scale factor */ if(! sf_getfloat("eps",&eps)) eps=1e-4; /* threshold for the timewindow */ if(! sf_getint("shift",&shift)) shift=5; /* shift in samples for the timewindow */ if (verb) { fprintf(stderr,"This program was called with \"%s\".\n",argv[0]); /*fprintf(stderr,"Nr: %d Nx: %d Nt:%d\n",nr,nx,nt);*/ if (argc > 1) { for (i = 1; i<argc; i++) { fprintf(stderr,"argv[%d] = %s\n", i, argv[i]); } } else { fprintf(stderr,"The command had no other arguments.\n"); } } /*------------------------------------------------------------*/ /* I/O files */ /*------------------------------------------------------------*/ /* "in" is the transposed version of p00plus_xxxx_xxxx.rsf Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=ntr,n2=nt Dimensions of p00plus_xxxx_xxxx.rsf BEFORE sftransp: n1=nt,n2=ntr */ Fplus = sf_input("in"); /* refl is REFL_000.rsf It is used to read nf, df, of Dimensions are: n1=nf,n2=ntr */ /*FRefl = (sf_file)sf_alloc(1,sizeof(sf_file));*/ FRefl = sf_input("refl"); FGp = sf_output("out"); FGm = sf_output("Gm"); if (Gtot) { FG = sf_output("G"); } if (Htot) { FH = sf_output("H"); } if (pandq) { Fp = sf_output("p"); Fq = sf_output("q"); } if (twin) { Ftwin = sf_output("window"); /* time window */ } /*------------------------------------------------------------*/ /* Axes */ /*------------------------------------------------------------*/ at = sf_iaxa(Fplus,1); sf_setlabel(at,"Time"); if(verb) sf_raxa(at); /* time */ af = sf_iaxa(FRefl,1); sf_setlabel(af,"Frequency"); if(verb) sf_raxa(af); /* frequency */ ax = sf_iaxa(Fplus,2); sf_setlabel(ax,"r"); if(verb) sf_raxa(ax); /* space */ nt = sf_n(at); dt = sf_d(at); ot = sf_o(at); nf = sf_n(af); df = sf_d(af); of = sf_o(af); ntr = sf_n(ax); dx = sf_d(ax); if (verb) fprintf(stderr,"nt: %d nf: %d ntr:%d\n",nt,nf,ntr); sf_fileclose(FRefl); /*------------------------------------------------------------*/ /* Setup output data and wavefield header */ /*------------------------------------------------------------*/ sf_oaxa(FGp,at,1); sf_oaxa(FGp,ax,2); sf_oaxa(FGm,at,1); sf_oaxa(FGm,ax,2); if (Gtot) { sf_oaxa(FG,at,1); sf_oaxa(FG,ax,2); } if (Htot) { sf_oaxa(FH,at,1); sf_oaxa(FH,ax,2); } if (pandq) { sf_oaxa(Fp,at,1); sf_oaxa(Fp,ax,2); sf_oaxa(Fq,at,1); sf_oaxa(Fq,ax,2); } if (twin) { sf_oaxa(Ftwin,at,1); sf_oaxa(Ftwin,ax,2); } /*------------------------------------------------------------*/ /* Allocate arrays */ /*------------------------------------------------------------*/ /* Downgoing wavefields - Time */ pplus0 = (float *)calloc(nt*ntr,sizeof(float)); sf_floatread(pplus0,nt*ntr,Fplus); pplus = (float *)calloc(nt*ntr,sizeof(float)); memcpy(pplus,pplus0,nt*ntr*sizeof(float)); pplustemp = (float *)calloc(nt*ntr,sizeof(float)); pplusinv = (float *)calloc(nt*ntr,sizeof(float)); qplus = (float *)calloc(nt*ntr,sizeof(float)); qplustemp = (float *)calloc(nt*ntr,sizeof(float)); /* Downgoing wavefields - Frequency */ Pplus = (float *)calloc(2*nf*ntr,sizeof(float)); Qplus = (float *)calloc(2*nf*ntr,sizeof(float)); /* The three flags of fft1 are: inv, sym, and opt */ fft1(pplus0,Pplus,Fplus,0,0,1); memcpy(Qplus,Pplus,2*nf*ntr*sizeof(float)); /* Upgoing wavefields - Time */ pminus = (float *)calloc(nt*ntr,sizeof(float)); qminus = (float *)calloc(nt*ntr,sizeof(float)); /* Downgoing wavefields - Frequency */ Pminus = (float *)calloc(2*nf*ntr,sizeof(float)); Qminus = (float *)calloc(2*nf*ntr,sizeof(float)); /* This is currently NOT used */ /* Transpose of p00plus_xxxx_xxxx */ for (ix=0; ix<ntr; ix++) { for (it=0; it<nt; it++) { pplusinv[ix*ntr+it]=pplus0[it*ntr+ix]; } } /* Single trace (in frequency) of the downgoing wavefield */ Pplus_trace = (float *)calloc(2*nf,sizeof(float)); Qplus_trace = (float *)calloc(2*nf,sizeof(float)); /* Output wavefields */ Gp = (float *)calloc(nt*ntr,sizeof(float)); Gm = (float *)calloc(nt*ntr,sizeof(float)); if (Gtot) { G = (float *)calloc(nt*ntr,sizeof(float)); } if (Htot) { H = (float *)calloc(nt*ntr,sizeof(float)); } /* Time-reversal flag */ if (conj) { mode = -1; } else { mode = +1; } /* Load the reflection response into the memory */ if (verb) fprintf(stderr,"Before loading R %d\n",2*nf*ntr); Refl = (float *)calloc(2*nf*ntr*nshots,sizeof(float)); /* Read REFL_000.rsf */ filename1 = sf_getstring("refl"); /* 000.rsf are 7 characters */ len = strlen(filename1)-7; /* copy the filename without 000.rsf */ strncpy(filename2,filename1,len); filename2[len] = '\0'; if (verb) fprintf(stderr,"filename2 is: %s and len is: %d\n",filename2,len); /*if (NULL == filename1) { fprintf(stderr,"Cannot read header file %s",filename1); }*/ for (ishot=0; ishot<nshots; ishot++) { /* write xxx.rsf in the string filename3 */ sprintf(filename3,"%03d.rsf",ishot); for (i=0; i<7; i++) filename2[len+i] = filename3[i]; filename2[len+7] = '\0'; if (verb) fprintf(stderr,"Loading %s in memory\n",filename2); FRefl = sf_input(filename2); sf_floatread(&Refl[ishot*2*nf*ntr],2*nf*ntr,FRefl); sf_fileclose(FRefl); /*if (verb) fprintf(stderr,"Iteration %d\n",ishot);*/ } /* Build time-window */ tw = (int *)calloc(ntr,sizeof(int)); window = (float *)calloc(nt*ntr,sizeof(float)); /*memset(window,0,nt*ntr*sizeof(float));*/ /* I am not sure why I set it to this value */ /*for (ix=0; ix<ntr; ix++) { tw[ix] = nt*dt+ot+0.15; }*/ if (verb) fprintf(stderr,"Build the time-window\n"); for (ix=0; ix<ntr; ix++) { for (it=0; it<nt; it++) { if (pplus0[it+ix*nt]>eps) { /*tw[ix] = it*dt+ot;*/ tw[ix] = it; /*if (verb) fprintf(stderr,"%d %d\n",ix,it);*/ break; } } } for (ix=0; ix<ntr; ix++) { twc = (int)(tw[ix]-shift); twa = (int)(-twc+shift+nt); /*if (verb) fprintf(stderr,"%d %d\n",twc,twa);*/ for (it=0; it<nt; it++) { if ((it<twc) || (it>twa)) { window[it+ix*nt] = 1.0; } } } /* Smoothing of the window */ /* Should I implement flags for rect and iter? */ /* Look at Msmooth.c to understand below */ n[0] = nt; n[1] = ntr; s[0] = 1; s[1] = nt; rect[0] = 5; rect[1] = 5; for (ix=0; ix <= 1; ix++) { if (rect[ix] <= 1) continue; tr = sf_triangle_init (rect[ix],n[ix],false); for (it=0; it < (nt*ntr/n[ix]); it++) { i0 = sf_first_index (ix,it,1+1,n,s); for (iter=0; iter < 2; iter++) { sf_smooth2 (tr,i0,s[ix],false,window); } } sf_triangle_close(tr); } /* Tapering */ pi = 4.0*atan(1.0); /*fprintf(stderr,"pi: %f\n",pi); fprintf(stderr,"ntr: %d\n",ntr);*/ taper = (float *)calloc(ntr,sizeof(float)); memset(taper,0,ntr*sizeof(float)); for (ix=0; ix<151; ix++) { taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/300))); taper[ntr-ix-1] = taper[ix]; } /*for (ix=(ntr-1); ix>(701-151-1); ix--) { taper[ix] = (float)(0.5*(1.0-cos(2.0*pi*(ix-0.0)/300))); }*/ for (ix=151; ix<(ntr-151); ix++) { taper[ix] = 1.0; } /*for (ix=0; ix<ntr; ix++) { fprintf(stderr,"taper[%d]: %f\n",ix,taper[ix]); }*/ FRefl = sf_input("refl"); /*------------------------------------------------------------*/ /* Loop over iterations */ /*------------------------------------------------------------*/ if (verb) fprintf(stderr,"Beginning of loop over iterations\n"); for (iter=0; iter<niter; iter++) { /* Set Pminus and Qminus to 0 */ memset(Pminus,0,2*nf*ntr*sizeof(float)); memset(Qminus,0,2*nf*ntr*sizeof(float)); /*------------------------------------------------------------*/ /* Loop over shot positions */ /*------------------------------------------------------------*/ if (verb) fprintf(stderr,"Beginning of loop over shot positions\n"); for (ishot=0; ishot<nshots; ishot++) { /* Loop over receivers (traces) */ #ifdef _OPENMP #pragma omp parallel for private(ix,it,a,b,c,d,e,f) \ shared(Pminus,Qminus,Pplus,Qplus,taper,Refl) #endif for (ix=0; ix<ntr; ix++) { /* Loop over frequencies */ #pragma ivdep for (it=0; it<2*nf; it=it+2) { /*(x + yi)(u + vi) = (xu - yv) + (xv + yu)i*/ a = Refl[ix*2*nf+it+ishot*2*nf*ntr]*taper[ishot]; b = Refl[ix*2*nf+it+1+ishot*2*nf*ntr]*taper[ishot]; c = Pplus[ishot*2*nf+it]; d = Pplus[ishot*2*nf+it+1]; e = Qplus[ishot*2*nf+it]; f = Qplus[ishot*2*nf+it+1]; Pminus[ix*2*nf+it] += a*c - mode*b*d; Pminus[ix*2*nf+it+1] += mode*a*d + b*c; Qminus[ix*2*nf+it] += a*e - mode*b*f; Qminus[ix*2*nf+it+1] += mode*a*f + b*e; } /* End of loop over frequencies */ } /* End of loop over receivers (traces) */ if (verb) if(ishot%50==0) fprintf(stderr,"Trace %d\n",ishot); } /* End of loop over shot positions */ /* Save a copy of pplus and qplus before creating their next iteration */ memcpy(pplustemp,pplus,nt*ntr*sizeof(float)); memcpy(qplustemp,qplus,nt*ntr*sizeof(float)); /* Build the next iteration of Pplus and Qplus */ fft1(Pminus,pminus,FRefl,1,0,1); fft1(Qminus,qminus,FRefl,1,0,1); if (verb) fprintf(stderr,"Build the next iteration of pplus and qplus\n"); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(pminus,qminus,pplus,qplus,pplus0,window) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { pplus[it+ix*nt] = pplus0[it+ix*nt] - scale*window[it+ix*nt]*pminus[it+ix*nt]; qplus[it+ix*nt] = pplus0[it+ix*nt] + scale*window[it+ix*nt]*qminus[it+ix*nt]; } } fft1(pplus,Pplus,Fplus,0,0,1); fft1(qplus,Qplus,Fplus,0,0,1); if (verb) fprintf(stderr,"%d %d\n",ix,it); if(iter%10==0) fprintf(stderr,"Iteration %d\n",iter); } /* End of loop over iterations */ /* Build Gp and Gm */ if (verb) fprintf(stderr,"Build Gp and Gm\n"); #ifdef _OPENMP #pragma omp parallel for private(ix,it) \ shared(Gp,Gm,G,H,pminus,qminus,pplustemp,qplustemp,pplus0) #endif for (ix=0; ix<ntr; ix++) { #pragma ivdep for (it=0; it<nt; it++) { Gp[it+ix*nt] = 0.5*( pplustemp[it+ix*nt] + scale*pminus[it+ix*nt] + qplustemp[it+ix*nt] - scale*qminus[it+ix*nt] ); Gm[it+ix*nt] = 0.5*( pplustemp[it+ix*nt] + scale*pminus[it+ix*nt] - qplustemp[it+ix*nt] + scale*qminus[it+ix*nt] ); if (Gtot) { G[it+ix*nt] = pplustemp[it+ix*nt] + scale*pminus[it+ix*nt]; } if (Htot) { H[it+ix*nt] = qplustemp[it+ix*nt] - scale*qminus[it+ix*nt]; } /*p[it+ix*nt] = 1.0*( pplus[it+ix*nt] + scale*pminus[it+ix*nt] ); q[it+ix*nt] = 1.0*( qplus[it+ix*nt] - scale*qminus[it+ix*nt] );*/ } } /* Write the final result */ /*FRefl = sf_input(argv[1]);*/ /*fft1(Gp,,FRefl,1,0,0); fft1(Gm,,FRefl,1,0,0);*/ sf_floatwrite(Gp,nt*ntr,FGp); sf_floatwrite(Gm,nt*ntr,FGm); if (Gtot) { sf_floatwrite(G,nt*ntr,FG); sf_fileclose(FG); free(G); } if (Htot) { sf_floatwrite(H,nt*ntr,FH); sf_fileclose(FH); free(H); } if (pandq) { sf_floatwrite(pplustemp,nt*ntr,Fp); sf_floatwrite(pminus,nt*ntr,Fq); sf_fileclose(Fp); sf_fileclose(Fq); } if (twin) { sf_floatwrite(window,nt*ntr,Ftwin); sf_fileclose(Ftwin); } sf_fileclose(Fplus); sf_fileclose(FRefl); sf_fileclose(FGp); sf_fileclose(FGm); free(Gp); free(Gm); free(pplus); free(pplusinv); free(pplustemp); free(Pplus); free(Pplus_trace); free(Pminus); free(qplus); free(qplustemp); free(Qplus); free(Qplus_trace); free(Qminus); free(Refl); free(window); free(tw); free(filename1); exit (0); }
int main(int argc, char* argv[]) { bool verb,pas,adj,abc; /* execution flags */ int ix, iz, it; /* index variables */ int nt, nx, nz, depth, nzxpad, nb, n2, snap; float ox, oz, dx, dz, dt, dt2, idz2, idx2, cb; int nxpad, nzpad; float **vvpad; float **dd, **mm, **vv, ***ww; float **u0, **u1, **u2, **tmp; /* temporary arrays */ sf_file in, out, vel, wave; /* I/O files */ /* initialize Madagascar */ sf_init(argc,argv); /* initialize OpenMP support */ #ifdef _OPENMP omp_init(); #endif if(!sf_getbool("verb", &verb)) verb=false; /* verbosity flag */ if(!sf_getbool("adj", &adj)) adj=false; /* adjoint flag, 0: modeling, 1: migration */ if(!sf_getbool("pas", &pas)) pas=false; /* passive flag, 0: exploding reflector rtm, 1: passive seismic imaging */ if(!sf_getbool("abc",&abc)) abc = false; /* absorbing boundary condition */ if(!sf_getint("snap", &snap)) snap=0; /* wavefield snapshot flag */ if(!sf_getint("depth", &depth)) depth=0; /* surface */ /* setup I/O files */ in = sf_input("in"); out = sf_output("out"); vel = sf_input("velocity"); /* velocity model */ /* Dimensions */ if(!sf_histint (vel, "n1", &nz)) sf_error("No n1= in velocity"); if(!sf_histint (vel, "n2", &nx)) sf_error("No n2= in velocity"); if(!sf_histfloat(vel, "o1", &oz)) sf_error("No o1= in velocity"); if(!sf_histfloat(vel, "o2", &ox)) sf_error("No o2= in velocity"); if(!sf_histfloat(vel, "d1", &dz)) sf_error("No d1= in velocity"); if(!sf_histfloat(vel, "d2", &dx)) sf_error("No d2= in velocity"); if(adj){ /* migration */ if(!sf_histint(in, "n1", &nt)) sf_error("No n1= in data"); if(!sf_histfloat(in, "d1", &dt)) sf_error("No d1= in data"); if(!sf_histint(in, "n2", &n2) || n2!=nx) sf_error("Need n2=%d in data", nx); sf_putint (out, "n1", nz); sf_putfloat (out, "o1", oz); sf_putfloat (out, "d1", dz); sf_putstring(out, "label1", "Depth"); sf_putstring(out, "unit1" , "km"); sf_putint (out, "n2", nx); sf_putfloat (out, "o2", ox); sf_putfloat (out, "d2", dx); sf_putstring(out, "label2", "Distance"); sf_putstring(out, "unit2" , "km"); if (pas) { sf_putint (out, "n3", nt); sf_putfloat (out, "d3", dt); sf_putfloat (out, "o3", 0.0f); sf_putstring(out, "label3", "Time"); sf_putstring(out, "unit3" , "s"); } }else{ /* modeling */ if(!sf_getint("nt", &nt)) sf_error("Need nt="); if(!sf_getfloat("dt", &dt)) sf_error("Need dt="); sf_putint (out, "n1", nt); sf_putfloat (out, "d1", dt); sf_putfloat (out, "o1", 0.0); sf_putstring(out, "label1", "Time"); sf_putstring(out, "unit1" , "s"); sf_putint (out, "n2", nx); sf_putfloat (out, "o2", ox); sf_putfloat (out, "d2", dx); sf_putstring(out, "label2", "Distance"); sf_putstring(out, "unit2" , "km"); if (pas) { sf_putint (out, "n3", 1); } } /* dimension of padded boundary */ if(!sf_getint("nb", &nb) || nb<NOP) nb = NOP; if(!sf_getfloat("cb", &cb)) cb = 0.0f; nxpad = nx+2*nb; nzpad = nz+2*nb; nzxpad = nzpad*nxpad; depth = depth+nb; /* set Laplacian coefficients */ idz2 = 1.0f/(dz*dz); idx2 = 1.0f/(dx*dx); /* wavefield snapshot */ if(snap){ wave = sf_output("wave"); sf_putint(wave, "n1", nzpad); sf_putfloat(wave, "d1", dz); sf_putfloat(wave, "o1", oz-nb*dz); sf_putint(wave, "n2", nxpad); sf_putfloat(wave, "d2", dx); sf_putfloat(wave, "o2", ox-nb*dx); sf_putint(wave, "n3", 1+(nt-1)/snap); if(adj){ sf_putfloat(wave, "d3", -snap*dt); sf_putfloat(wave, "o3", (nt-1)*dt); }else{ sf_putfloat(wave, "d3", snap*dt); sf_putfloat(wave, "o3", 0.0f); } } /* allocate arrays */ vv = sf_floatalloc2(nz, nx); dd = sf_floatalloc2(nt, nx); vvpad = sf_floatalloc2(nzpad, nxpad); u0 = sf_floatalloc2(nzpad, nxpad); u1 = sf_floatalloc2(nzpad, nxpad); u2 = sf_floatalloc2(nzpad, nxpad); if (pas) { mm = NULL; ww = sf_floatalloc3(nz, nx, nt); } else { mm = sf_floatalloc2(nz, nx); ww = NULL; } /* read velocity */ sf_floatread(vv[0], nz*nx, vel); /* pad boundary */ dt2 = dt*dt; for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) vvpad[ix+nb][iz+nb] = vv[ix][iz]*vv[ix][iz]*dt2; for (ix=0; ix<nxpad; ix++){ for (iz=0; iz<nb; iz++){ vvpad[ix][ iz ] = vvpad[ix][ nb ]; vvpad[ix][nzpad-iz-1] = vvpad[ix][nzpad-nb-1]; } } for (ix=0; ix<nb; ix++){ for (iz=0; iz<nzpad; iz++){ vvpad[ ix ][iz]=vvpad[ nb ][iz]; vvpad[nxpad-ix-1][iz]=vvpad[nxpad-nb-1][iz]; } } memset(u0[0], 0.0f, nzxpad*sizeof(float)); memset(u1[0], 0.0f, nzxpad*sizeof(float)); memset(u2[0], 0.0f, nzxpad*sizeof(float)); /* absorbing boundary condition */ if (abc) { if (verb) sf_warning("absorbing boundary condition"); abc_init(nzpad,nxpad,nzpad,nxpad,nb,nb,nb,nb,cb,cb,cb,cb); } if(adj){ /* migration */ /* read data */ sf_floatread(dd[0], nt*nx, in); for (it=nt-1; it>-1; it--){ if (verb) sf_warning("Migration: %d/%d;", it, 0); /* time stepping */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix, iz) #endif for (ix=NOP; ix<nxpad-NOP; ix++){ for (iz=NOP; iz<nzpad-NOP; iz++){ u2[ix][iz] = LapT(u1,ix,iz,idx2,idz2,vvpad) + 2.0f*u1[ix][iz] - u0[ix][iz]; } } /* rotate pointers */ tmp=u0; u0=u1; u1=u2; u2=tmp; if (abc) abc_apply(u1[0]); if (abc) abc_apply(u0[0]); /* inject data */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix) #endif for (ix=nb; ix<nb+nx; ix++) u1[ix][depth] += dd[ix-nb][it]; if (pas) { /* image source */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix, iz) #endif for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) ww[it][ix][iz] = u1[ix+nb][iz+nb]; } if (snap && it%snap==0) sf_floatwrite(u1[0], nzxpad, wave); } if (verb) sf_warning("."); if (!pas) { /* output image */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix, iz) #endif for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) mm[ix][iz] = u1[ix+nb][iz+nb]; sf_floatwrite(mm[0], nz*nx, out); } else { /* output source */ sf_floatwrite(ww[0][0], nz*nx*nt, out); } }else{/* modeling */ if (pas) { /* read source */ sf_floatread(ww[0][0], nz*nx*nt, in); } else { /* read image */ sf_floatread(mm[0], nz*nx, in); #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix, iz) #endif for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) u1[ix+nb][iz+nb] = mm[ix][iz]; } for (it=0; it<nt; it++){ if (verb) sf_warning("Modeling: %d/%d;", it, nt-1); if(snap && it%snap==0) sf_floatwrite(u1[0], nzxpad, wave); if (pas){ /* inject source */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix, iz) #endif for (ix=0; ix<nx; ix++) for (iz=0; iz<nz; iz++) u1[ix+nb][iz+nb] += ww[it][ix][iz]; } /* record data */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix) #endif for (ix=nb; ix<nb+nx; ix++) dd[ix-nb][it] = u1[ix][depth]; if (abc) abc_apply(u0[0]); if (abc) abc_apply(u1[0]); /* time stepping */ #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix, iz) #endif for (ix=NOP; ix<nxpad-NOP; ix++){ for (iz=NOP; iz<nzpad-NOP; iz++){ u2[ix][iz] = Lap (u1,ix,iz,idx2,idz2,vvpad) + 2.0f*u1[ix][iz] - u0[ix][iz]; } } /* rotate pointers */ tmp=u0; u0=u1; u1=u2; u2=tmp; } if (verb) sf_warning("."); /* output data */ sf_floatwrite(dd[0], nt*nx, out); } if(pas) { free(**ww); free(*ww); free(ww); } else { free(*mm); free(mm); } if (abc) abc_close(); free(*vvpad); free(vvpad); free(*vv); free(vv); free(*dd); free(dd); free(*u0); free(u0); free(*u1); free(u1); free(*u2); free(u2); exit (0); }
int main(int argc, char* argv[]) { bool verb,isreversed; sf_file Fs,Fr,Fi,Fc; /* I/O files */ sf_axis az,ax,at,ac,aa; /* cube axes */ int nz,nx,nt, nhx, nhz, nht,nc; int it, ihx, ihz, iht,ic; int nhx2,nhz2,nht2; off_t iseek; float ***us=NULL,***ur=NULL,****ii=NULL; pt2d *cc=NULL; bool *ccin=NULL; float cxmin,czmin; float cxmax,czmax; int icx, icz; int mcx, mcz, mct; int pcx, pcz, pct; int **mcxall, **pcxall; int **mczall, **pczall; int *mctall, *pctall; int lht; float scale; /* gaussian taper */ bool gaus; float gsx,gsz,gst; /* std dev */ float gx, gz, gt; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /* OMP parameters */ #ifdef _OPENMP omp_init(); #endif if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("isreversed",&isreversed)) isreversed=false; /* reversed rec wfld? */ Fs = sf_input ("in" ); /* source wavefield */ Fr = sf_input ("ur" ); /* receiver wavefield */ Fc = sf_input ("cc" ); /* CIP coordinates */ Fi = sf_output("out"); /* image */ /*------------------------------------------------------------*/ /* read axes */ az=sf_iaxa(Fs,1); nz = sf_n(az); ax=sf_iaxa(Fs,2); nx = sf_n(ax); at=sf_iaxa(Fs,3); nt = sf_n(at); /* CIP coordinates */ ac = sf_iaxa(Fc,2); sf_setlabel(ac,"cc"); sf_setunit(ac,""); nc = sf_n(ac); if(! sf_getint("nhz",&nhz)) nhz=0; nhz2=2*nhz+1; /* z lags */ if(! sf_getint("nhx",&nhx)) nhx=0; nhx2=2*nhx+1; /* x lags */ if(! sf_getint("nht",&nht)) nht=0; nht2=2*nht+1; /* t lags */ lht=2*nht; if(verb) { sf_warning("nhx=%3d nhz=%3d nht=%3d",nhx2,nhz2,nht2); sf_raxa(az); sf_raxa(ax); sf_raxa(at); sf_raxa(ac); } /* set output axes */ aa=sf_maxa(nhz2,-nhz*sf_d(az),sf_d(az)); sf_setlabel(aa,"hz"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Fi,aa,1); aa=sf_maxa(nhx2,-nhx*sf_d(ax),sf_d(ax)); sf_setlabel(aa,"hx"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Fi,aa,2); aa=sf_maxa(nht2,-nht*sf_d(at),sf_d(at)); sf_setlabel(aa,"ht"); sf_setunit(aa,""); if(verb) sf_raxa(aa); sf_oaxa(Fi,aa,3); sf_oaxa(Fi,ac,4); if(! sf_getbool("gaus",&gaus)) gaus=false; /* Gaussian taper flag */ if(gaus) { if(! sf_getfloat("gsx",&gsx)) gsx=nhx*sf_d(ax); gsx=1./(2*gsx*gsx); if(! sf_getfloat("gsz",&gsz)) gsz=nhz*sf_d(az); gsz=1./(2*gsz*gsz); if(! sf_getfloat("gst",&gst)) gst=nht*sf_d(at); gst=1./(2*gst*gst); } /*------------------------------------------------------------*/ /* allocate work arrays */ us=sf_floatalloc3(nz,nx,nht2); ur=sf_floatalloc3(nz,nx,nht2); ii=sf_floatalloc4(nhz2,nhx2,nht2,nc); /* zero output */ for(ic=0; ic<nc; ic++) { for (iht=0; iht<nht2; iht++) { for (ihx=0; ihx<nhx2; ihx++) { for(ihz=0; ihz<nhz2; ihz++) { ii[ic][iht][ihx][ihz] = 0; } } } } /*------------------------------------------------------------*/ /* CIP coordinates */ cc= (pt2d*) sf_alloc(nc,sizeof(*cc)); pt2dread1(Fc,cc,nc,2); mcxall=sf_intalloc2(nhx2,nc); pcxall=sf_intalloc2(nhx2,nc); mczall=sf_intalloc2(nhz2,nc); pczall=sf_intalloc2(nhz2,nc); ccin=sf_boolalloc(nc); cxmin = sf_o(ax) + nhx *sf_d(ax); cxmax = sf_o(ax) + (sf_n(ax)-1-nhx)*sf_d(ax); czmin = sf_o(az) + nhz *sf_d(az); czmax = sf_o(az) + (sf_n(az)-1-nhz)*sf_d(az); if(verb) { sf_warning("cxmin=%f,cxmax=%f",cxmin,cxmax); sf_warning("czmin=%f,czmax=%f",czmin,czmax); } for(ic=0; ic<nc; ic++) { ccin[ic]=(cc[ic].x>=cxmin && cc[ic].x<=cxmax && cc[ic].z>=czmin && cc[ic].z<=czmax)?true:false; if(ccin[ic]) { icx = 0.5+(cc[ic].x-sf_o(ax))/sf_d(ax); for(ihx=-nhx; ihx<nhx+1; ihx++) { mcxall[ic][nhx+ihx] = icx-ihx; pcxall[ic][nhx+ihx] = icx+ihx; } icz = 0.5+(cc[ic].z-sf_o(az))/sf_d(az); for(ihz=-nhz; ihz<nhz+1; ihz++) { mczall[ic][nhz+ihz] = icz-ihz; pczall[ic][nhz+ihz] = icz+ihz; } } } mctall=sf_intalloc(nht2); pctall=sf_intalloc(nht2); for (iht=0; iht<nht2; iht++) { mctall[iht]=iht; pctall[iht]=2*nht-iht; } /*------------------------------------------------------------*/ if(isreversed) { /* receiver wavefield is reversed */ /* read wavefield @ [0...2nht-1]*/ for(iht=0;iht<2*nht;iht++) { sf_floatread(us[iht][0],nz*nx,Fs); sf_floatread(ur[iht][0],nz*nx,Fr); } if(verb) fprintf(stderr,"nt\n"); for(it=nht;it<nt-nht;it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",it); /* read wavefield @ [2nht]*/ sf_floatread(us[ lht ][0],nz*nx,Fs); sf_floatread(ur[ lht ][0],nz*nx,Fr); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ic, \ ihx,ihz,iht, \ mcx,mcz,mct, \ pcx,pcz,pct) \ shared (nc,ii,us,ur, \ nhx2, nhz2, nht2, \ mcxall,mczall,mctall, \ pcxall,pczall,pctall,ccin) #endif for(ic=0; ic<nc; ic++) { if(ccin[ic]) { for (iht=0; iht<nht2; iht++) { mct=mctall [iht]; pct=pctall [iht]; for (ihx=0; ihx<nhx2; ihx++) { mcx=mcxall[ic][ihx]; pcx=pcxall[ic][ihx]; for(ihz=0; ihz<nhz2; ihz++) { mcz=mczall[ic][ihz]; pcz=pczall[ic][ihz]; ii[ic][iht][ihx][ihz] += us[mct][mcx][mcz]*ur[pct][pcx][pcz]; } /* ihz */ } /* ihx */ } /* iht */ } } /* ic */ /* update wavefield index (cycle) */ for(iht=0;iht<nht2;iht++) { mctall[iht] = (mctall[iht]+1) % nht2; pctall[iht] = (pctall[iht]+1) % nht2; } lht = (lht+1) % nht2; } /* it */ if(verb) fprintf(stderr,"\n"); } else { /* receiver wavefield is NOT reversed */ /* read wavefield @ [0...2nht-1]*/ for(iht=0;iht<2*nht;iht++) { sf_floatread(us[iht][0],nz*nx,Fs); iseek = (off_t)(nt-1-iht)*nz*nx*sizeof(float); sf_seek(Fr,iseek,SEEK_SET); sf_floatread(ur[iht][0],nz*nx,Fr); } if(verb) fprintf(stderr,"nt\n"); for(it=nht;it<nt-nht;it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",nt-nht-1-it); /* read wavefield @ [2nht]*/ sf_floatread(us[ lht ][0],nz*nx,Fs); iseek=(off_t)(nt-nht-1-it)*nz*nx*sizeof(float); sf_seek(Fr,iseek,SEEK_SET); sf_floatread(ur[ lht ][0],nz*nx,Fr); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ic, \ ihx,ihz,iht, \ mcx,mcz,mct, \ pcx,pcz,pct) \ shared (nc,ii,us,ur, \ nhx2, nhz2, nht2, \ mcxall,mczall,mctall, \ pcxall,pczall,pctall,ccin) #endif for(ic=0; ic<nc; ic++) { if(ccin[ic]) { for (iht=0; iht<nht2; iht++) { mct=mctall [iht]; pct=pctall [iht]; for (ihx=0; ihx<nhx2; ihx++) { mcx=mcxall[ic][ihx]; pcx=pcxall[ic][ihx]; for(ihz=0; ihz<nhz2; ihz++) { mcz=mczall[ic][ihz]; pcz=pczall[ic][ihz]; ii[ic][iht][ihx][ihz] += us[mct][mcx][mcz]*ur[pct][pcx][pcz]; } /* ihz */ } /* ihx */ } /* iht */ } } /* ic */ /* update wavefield index (cycle) */ for(iht=0;iht<nht2;iht++) { mctall[iht] = (mctall[iht]+1) % nht2; pctall[iht] = (pctall[iht]+1) % nht2; } lht = (lht+1) % nht2; } /* it */ if(verb) fprintf(stderr,"\n"); } /* end "is reversed" */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* scale image */ scale = 1./nt; for(ic=0; ic<nc; ic++) { for (iht=0; iht<nht2; iht++) { for (ihx=0; ihx<nhx2; ihx++) { for(ihz=0; ihz<nhz2; ihz++) { ii[ic][iht][ihx][ihz] *= scale; } } } } /*------------------------------------------------------------*/ /* apply Gaussian taper */ if(gaus) { for(ic=0; ic<nc; ic++) { for (iht=0;iht<nht2;iht++) { gt=(iht-nht)*sf_d(at); gt*=gt; for (ihx=0;ihx<nhx2;ihx++) { gx=(ihx-nhx)*sf_d(ax); gx*=gx; for(ihz=0;ihz<nhz2;ihz++) { gz=(ihz-nhz)*sf_d(az); gz*=gz; ii[ic][iht][ihx][ihz] *= exp(-gt*gst - gx*gsx - gz*gsz); } } } } } /*------------------------------------------------------------*/ /* write image */ sf_floatwrite(ii[0][0][0],nc*(nhx2)*(nhz2)*(nht2),Fi); /*------------------------------------------------------------*/ /* deallocate arrays */ free(***ii); free(**ii); free(*ii); free(ii); free(*us); free(us); free(*ur); free(ur); free(cc); free(ccin); free(*mcxall); free(mcxall); free(*pcxall); free(pcxall); free(*mczall); free(mczall); free(*pczall); free(pczall); /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { int ompnth=1; sf_file in, out; /* Input and output files */ sf_axis az,ax,a3; int nz,nx,n3; int box1,box2,klo1,khi1,klo2,khi2,kmid2,kmid1; int ix,iz,i3; float *sumG,***d,***dat,***neg,***dN; float neg1,AmpNorm,h,h1; double scalea, logs; /*---------------------------------------------------------*/ /* Initialize RSF */ sf_init(argc,argv); /* standard input */ in = sf_input("in"); /* standard output */ out = sf_output("out"); #ifdef _OPENMP ompnth = omp_init(); #endif /* parameters from input file*/ az=sf_iaxa(in,1); sf_setlabel(az,"z"); nz = sf_n(az); ax=sf_iaxa(in,2); sf_setlabel(ax,"x"); nx = sf_n(ax); a3=sf_iaxa(in,3); sf_setlabel(a3,"y"); n3 = sf_n(a3); /* parameter from the command line (i.e. box1=50 box2=50 ) */ if (!sf_getint("box1",&box1)) sf_error("Need box1="); if (!sf_getint("box2",&box2)) sf_error("Need box2="); /* allocate floating point array */ dat = sf_floatalloc3 (nz,nx,n3); /* initialise the size of the searching box*/ int s1= nz-(box1); int s2= nx-(box2); int bm1=box1/2; int bm2=box2/2; /*initialise the mid-point of each box) */ sumG =sf_floatalloc (n3); d =sf_floatalloc3 (nz,nx,n3); dN =sf_floatalloc3 (nz,nx,n3); neg =sf_floatalloc3 (nz,nx,n3); sf_floatread(dat[0][0],nz*nx*n3,in); // Global Sum for (i3=0 ; i3<n3; ++i3){ sumG[i3]=0; for (ix=0; ix<nx; ++ix){ for (iz=0; iz<nz; ++iz){ d[i3][ix][iz] = dat[i3][ix][iz] * dat[i3][ix][iz]; //make all amplitudes positive sumG[i3] += d[i3][ix][iz]; } } } /* #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(klo2,klo1,kmid2,kmid1,khi1,khi2,neg1,AmpNorm,scalea,logs,h1,h) #endif */ //initialise the box for negentropy for (i3=0 ; i3<n3 ; ++i3 ){ klo2=0;klo1=0; for (klo2=0; klo2<(s2); ++klo2){ for (klo1=0; klo1<(s1); ++klo1){ //intialise parameters neg1=0.0;scalea=0.0;logs=0.0; AmpNorm=0.0;h=0,h1=0; //Set upper limit of searching box and midpoints khi2=klo2+box2; khi1=klo1+box1; kmid2=klo2+bm2; kmid1=klo1+bm1; //Sum values in each box for (ix=klo2; ix<khi2; ++ix){ for (iz=klo1; iz<khi1; ++iz){ AmpNorm =(d[i3][ix][iz])/(sumG[i3]); dN[i3][ix][iz]=AmpNorm; //ai //Gaussian operator h =(((iz -kmid1) * (iz -kmid1)) + \ ((ix -kmid2) * (ix -kmid2))) / (2*box1*box2*1.41); h1=(((box1*0.5 ) * (box1*0.5 )) + \ ((box2*0.5 ) * (box2*0.5 )))/ (2*box1*box2*1.41); h =exp(-4*h ); h1=exp(-4*h1); if (h1>= h) scalea=0; else scalea = box1 * box2 * AmpNorm * (h-h1); //qi if (AmpNorm==0) logs= 0; else logs= scalea*scalea; /* logs can be different functions: else {logs=log(scalea);} logs=log(scalea); */ neg1 += (scalea*logs); } } neg[i3][kmid2][kmid1] = neg1/(box1*box2); } } } sf_floatwrite(neg[0][0] ,nz*nx*n3 ,out); //write negentropy exit(0); }
int main(int argc, char* argv[]) { int it,kt,ia,is,i1,i2,tdmute,jsx,jsz,jgx,jgz,sxbeg,szbeg,gxbeg,gzbeg, distx, distz; int *sxz, *gxz; float tmp, amp, vmax; float *wlt, *d2x, *d1z, *bndr; float **v0, **vv, **dcal, **den; float **sp, **spz, **spx, **svz, **svx, **gp, **gpz, **gpx, **gvz, **gvx; float ***num, ***adcig; sf_file vmodl, rtmadcig, vecx, vecz; /* I/O files */ sf_init(argc,argv); #ifdef _OPENMP omp_init(); #endif /*< set up I/O files >*/ vmodl = sf_input ("in"); /* velocity model, unit=m/s */ rtmadcig = sf_output("out"); /* ADCIG obtained by Poynting vector */ vecx=sf_output("vecx"); vecz=sf_output("vecz"); /* get parameters for RTM */ if (!sf_histint(vmodl,"n1",&nz)) sf_error("no n1"); if (!sf_histint(vmodl,"n2",&nx)) sf_error("no n2"); if (!sf_histfloat(vmodl,"d1",&dz)) sf_error("no d1"); if (!sf_histfloat(vmodl,"d2",&dx)) sf_error("no d2"); if (!sf_getfloat("amp",&)) amp=1.e3; /* maximum amplitude of ricker wavelet*/ if (!sf_getfloat("fm",&fm)) sf_error("no fm"); /* dominant freq of ricker */ if (!sf_getfloat("dt",&dt)) sf_error("no dt"); /* time interval */ if (!sf_getint("nt",&nt)) sf_error("no nt"); /* total modeling time steps */ if (!sf_getint("ns",&ns)) sf_error("no ns"); /* total shots */ if (!sf_getint("ng",&ng)) sf_error("no ng"); /* total receivers in each shot */ if (!sf_getint("nb",&nb)) nb=20; /* thickness of split PML */ if (!sf_getint("na",&na)) na=30; /* number of angles*/ if (!sf_getint("kt",&kt)) kt=200; /* record poynting vector at kt */ if (!sf_getint("jsx",&jsx)) sf_error("no jsx"); /* source x-axis jump interval */ if (!sf_getint("jsz",&jsz)) jsz=0; /* source z-axis jump interval */ if (!sf_getint("jgx",&jgx)) jgx=1; /* receiver x-axis jump interval */ if (!sf_getint("jgz",&jgz)) jgz=0; /* receiver z-axis jump interval */ if (!sf_getint("sxbeg",&sxbeg)) sf_error("no sxbeg"); /* x-begining index of sources, starting from 0 */ if (!sf_getint("szbeg",&szbeg)) sf_error("no szbeg"); /* z-begining index of sources, starting from 0 */ if (!sf_getint("gxbeg",&gxbeg)) sf_error("no gxbeg"); /* x-begining index of receivers, starting from 0 */ if (!sf_getint("gzbeg",&gzbeg)) sf_error("no gzbeg"); /* z-begining index of receivers, starting from 0 */ if (!sf_getbool("csdgather",&csdgather)) csdgather=true; /* default, common shot-gather; if n, record at every point*/ if (!sf_getfloat("vmute",&vmute)) vmute=1500; /* muting velocity to remove the low-freq noise, unit=m/s*/ if (!sf_getint("tdmute",&tdmute)) tdmute=2./(fm*dt); /* number of deleyed time samples to mute */ _dx=1./dx; _dz=1./dz; nzpad=nz+2*nb; nxpad=nx+2*nb; da=SF_PI/(float)na;/* angle unit, rad; */ var=da/3.; var=2.0*var*var; sf_putint(rtmadcig,"n1",nz); sf_putint(rtmadcig,"n2",nx); sf_putfloat(rtmadcig,"n3",na); sf_putfloat(rtmadcig,"d1",dz); sf_putfloat(rtmadcig,"d2",dx); sf_putfloat(rtmadcig,"d3",90./(float)na); /* allocate variables */ wlt=sf_floatalloc(nt); v0=sf_floatalloc2(nz,nx); vv=sf_floatalloc2(nzpad, nxpad); sp =sf_floatalloc2(nzpad, nxpad); spz=sf_floatalloc2(nzpad, nxpad); spx=sf_floatalloc2(nzpad, nxpad); svz=sf_floatalloc2(nzpad, nxpad); svx=sf_floatalloc2(nzpad, nxpad); gp =sf_floatalloc2(nzpad, nxpad); gpz=sf_floatalloc2(nzpad, nxpad); gpx=sf_floatalloc2(nzpad, nxpad); gvz=sf_floatalloc2(nzpad, nxpad); gvx=sf_floatalloc2(nzpad, nxpad); d1z=sf_floatalloc(nzpad); d2x=sf_floatalloc(nxpad); sxz=sf_intalloc(ns); gxz=sf_intalloc(ng); dcal=sf_floatalloc2(ng,nt); bndr=(float*)malloc(nt*8*(nx+nz)*sizeof(float)); den=sf_floatalloc2(nz,nx); num=sf_floatalloc3(nz,nx,na); adcig=sf_floatalloc3(nz,nx,na); /* initialize variables */ for(it=0;it<nt;it++){ tmp=SF_PI*fm*(it*dt-1.0/fm);tmp*=tmp; wlt[it]=amp*(1.0-2.0*tmp)*expf(-tmp); } sf_floatread(v0[0],nz*nx,vmodl); expand2d(vv, v0); memset(sp [0],0,nzpad*nxpad*sizeof(float)); memset(spx[0],0,nzpad*nxpad*sizeof(float)); memset(spz[0],0,nzpad*nxpad*sizeof(float)); memset(svx[0],0,nzpad*nxpad*sizeof(float)); memset(svz[0],0,nzpad*nxpad*sizeof(float)); memset(gp [0],0,nzpad*nxpad*sizeof(float)); memset(gpx[0],0,nzpad*nxpad*sizeof(float)); memset(gpz[0],0,nzpad*nxpad*sizeof(float)); memset(gvx[0],0,nzpad*nxpad*sizeof(float)); memset(gvz[0],0,nzpad*nxpad*sizeof(float)); vmax=v0[0][0]; for(i2=0; i2<nx; i2++) for(i1=0; i1<nz; i1++) vmax=SF_MAX(v0[i2][i1],vmax); pmlcoeff_init(d1z, d2x, vmax); if (!(sxbeg>=0 && szbeg>=0 && sxbeg+(ns-1)*jsx<nx && szbeg+(ns-1)*jsz<nz)) { sf_error("sources exceeds the computing zone!"); exit(1);} sg_init(sxz, szbeg, sxbeg, jsz, jsx, ns); distx=sxbeg-gxbeg; distz=szbeg-gzbeg; if (csdgather) { if (!(gxbeg>=0 && gzbeg>=0 && gxbeg+(ng-1)*jgx<nx && gzbeg+(ng-1)*jgz<nz && (sxbeg+(ns-1)*jsx)+(ng-1)*jgx-distx <nx && (szbeg+(ns-1)*jsz)+(ng-1)*jgz-distz <nz)) { sf_error("geophones exceeds the computing zone!"); exit(1);} }else{ if (!(gxbeg>=0 && gzbeg>=0 && gxbeg+(ng-1)*jgx<nx && gzbeg+(ng-1)*jgz<nz)) { sf_error("geophones exceeds the computing zone!"); exit(1);} } sg_init(gxz, gzbeg, gxbeg, jgz, jgx, ng); memset(adcig[0][0], 0, na*nz*nx*sizeof(float)); for(is=0; is<ns; is++) { wavefield_init(sp, spz, spx, svz, svx); if (csdgather) { gxbeg=sxbeg+is*jsx-distx; sg_init(gxz, gzbeg, gxbeg, jgz, jgx, ng); } for(it=0; it<nt; it++) { add_source(&sxz[is], sp, 1, &wlt[it], true); step_forward(sp, spz, spx, svz, svx, vv, d1z, d2x); bndr_rw(false, svz, svx, &bndr[it*8*(nx+nz)]); record_seis(dcal[it], gxz, sp, ng); muting(dcal[it], gzbeg, szbeg, gxbeg, sxbeg+is*jsx, jgx, it, tdmute); } wavefield_init(gp, gpz, gpx, gvz, gvx); memset(num[0][0], 0, na*nz*nx*sizeof(float)); memset(den[0], 0, nz*nx*sizeof(float)); for(it=nt-1; it>-1; it--) { add_source(gxz, gp, ng, dcal[it], true); step_forward(gp, gpz, gpx, gvz, gvx, vv, d1z, d2x); if(it==kt) { window2d(v0,svx); sf_floatwrite(v0[0],nz*nx,vecx); window2d(v0,svz); sf_floatwrite(v0[0],nz*nx,vecz); } bndr_rw(true, svz, svx, &bndr[it*8*(nx+nz)]); cross_correlation(num, den, sp, gp, svz, svx, gvz, gvx); step_backward(sp, svz, svx, vv); add_source(&sxz[is], sp, 1, &wlt[it], false); } for(ia=0; ia<na; ia++) for(i2=0; i2<nx; i2++) for(i1=0; i1<nz; i1++) adcig[ia][i2][i1]+=num[ia][i2][i1]/(den[i2][i1]+SF_EPS); } sf_floatwrite(adcig[0][0], na*nz*nx,rtmadcig); free(wlt); free(*v0); free(v0); free(*vv); free(vv); free(*sp); free(sp); free(*spx); free(spx); free(*spz); free(spz); free(*svx); free(svx); free(*svz); free(svz); free(*gp); free(gp); free(*gpx); free(gpx); free(*gpz); free(gpz); free(*gvx); free(gvx); free(*gvz); free(gvz); free(d1z); free(d2x); free(sxz); free(gxz); free(bndr); free(*den); free(den); free(**num); free(*num); free(num); free(**adcig); free(*adcig); free(adcig); exit(0); }
int main(int argc, char* argv[]) { bool verb,isreversed; sf_file Fs,Fr,Fi; /* I/O files */ sf_axis az,ax,at,aa; /* cube axes */ int iz,ix,it; int nz,nx,nt; off_t iseek; float **us=NULL,**ur=NULL,**ii=NULL; float scale; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); /* OMP parameters */ #ifdef _OPENMP omp_init(); #endif if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if(! sf_getbool("isreversed",&isreversed)) isreversed=false; /* received wavefield */ Fs = sf_input ("in" ); Fr = sf_input ("ur" ); Fi = sf_output("out"); /*------------------------------------------------------------*/ /* read axes */ az=sf_iaxa(Fs,1); nz = sf_n(az); ax=sf_iaxa(Fs,2); nx = sf_n(ax); at=sf_iaxa(Fs,3); nt = sf_n(at); aa=sf_maxa(1,0,1); sf_setlabel(aa,""); sf_setunit (aa,""); if(verb) { sf_raxa(az); sf_raxa(ax); sf_raxa(at); } /* write axes */ sf_oaxa(Fi,az,1); sf_oaxa(Fi,ax,2); sf_oaxa(Fi,aa,3); /*------------------------------------------------------------*/ /* allocate work arrays */ ii = sf_floatalloc2(nz,nx); us = sf_floatalloc2(nz,nx); ur = sf_floatalloc2(nz,nx); for (ix=0; ix<nx; ix++) { for(iz=0; iz<nz; iz++) { ii[ix][iz]=0.; } } /*------------------------------------------------------------*/ if(isreversed) { /* receiver wavefield is reversed */ if(verb) fprintf(stderr,"nt\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b\b\b\b\b%04d",it); sf_floatread(us[0],nz*nx,Fs); sf_floatread(ur[0],nz*nx,Fr); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ix,iz) \ shared (ii,us,ur,nx,nz) #endif for (ix=0; ix<nx; ix++) { for(iz=0; iz<nz; iz++) { ii[ix][iz] += us[ix][iz]*ur[ix][iz]; } } } /* it */ if(verb) fprintf(stderr,"\n"); } else { /* receiver wavefield is NOT reversed */ if(verb) fprintf(stderr,"nt\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",(nt-it-1)); sf_floatread(us[0],nz*nx,Fs); iseek=(off_t)(nt-1-it)*nz*nx*sizeof(float); sf_seek(Fr,iseek,SEEK_SET); sf_floatread(ur[0],nz*nx,Fr); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ix,iz) \ shared (ii,us,ur,nx,nz) #endif for (ix=0; ix<nx; ix++) { for(iz=0; iz<nz; iz++) { ii[ix][iz] += us[ix][iz]*ur[ix][iz]; } } } /* it */ if(verb) fprintf(stderr,"\n"); } /* end "is reversed" */ /*------------------------------------------------------------*/ /*------------------------------------------------------------*/ /* scale image */ scale = 1./nt; for (ix=0; ix<nx; ix++) { for(iz=0; iz<nz; iz++) { ii[ix][iz] *=scale; } } /*------------------------------------------------------------*/ /* write image */ sf_floatwrite(ii[0],nx*nz,Fi); /*------------------------------------------------------------*/ /* deallocate arrays */ free(*ii); free(ii); free(*us); free(us); free(*ur); free(ur); /*------------------------------------------------------------*/ exit (0); }
/*------------------------------------------------------------*/ int main(int argc, char* argv[]) { bool verb; /* verbosity flag */ bool pos; /* direction of spraying */ bool adj; /* adjoint operator flag */ bool wflcausal, oprcausal; /* causal wfl?, opr? */ sf_file Fopr, Fwfl, Fimg, Fcip; /* I/O files */ float ****opr=NULL,****wfl=NULL,*****img=NULL; int itO,itW; sf_axis az,ax,ay,at,ac,aa; /* wfld axes */ int nz,nx,ny,nt,nc; int iz,ix,iy,it,ic; sf_axis ahx, ahy, ahz, aht; /* EIC axes */ int nhx, nhy, nhz, nht; int ihx, ihy, ihz, iht; float dhx, dhy, dhz, dht; pt3d *cc=NULL; bool *ccin=NULL; float cxmin,czmin,cymin; float cxmax,czmax,cymax; int icx, icz, icy; int mcx, mcz, mcy, mct; int pcx, pcz, pcy, pct; int **mcxall, **pcxall; int **mcyall, **pcyall; int **mczall, **pczall; int *mctall, *pctall; int lht,fht; /* last buffer index */ float scale; /* time summation scaling */ int nslice; /* wavefield slice size */ bool gaus; /* gaussian taper */ float gsx,gsy,gsz,gst; /* std dev */ /*------------------------------------------------------------*/ sf_init(argc,argv); #ifdef _OPENMP omp_init(); #endif if(! sf_getbool( "verb",&verb )) verb=false; /* verbosity flag */ if(! sf_getbool( "positive",&pos )) pos=true; /* if positive sprays opr to positive shits, else, sprays to negative shifts */ if(! sf_getbool( "adj",&adj )) adj=false; /* adjoint flag */ if(! sf_getbool("wflcausal",&wflcausal)) wflcausal=false; /* causal wfl? */ if(! sf_getbool("oprcausal",&oprcausal)) oprcausal=false; /* causal opr? */ /*------------------------------------------------------------*/ Fopr = sf_input ("opr" ); /* operator */ az=sf_iaxa(Fopr,1); if(verb) sf_raxa(az); nz = sf_n(az); ax=sf_iaxa(Fopr,2); if(verb) sf_raxa(ax); nx = sf_n(ax); ay=sf_iaxa(Fopr,3); if(verb) sf_raxa(ay); ny = sf_n(ay); at=sf_iaxa(Fopr,4); if(verb) sf_raxa(at); nt = sf_n(at); scale = 1./nt; /* time summation scaling */ nslice = nz*nx*ny*sizeof(float); /* wavefield slice */ Fcip = sf_input ("cip" ); /* CIP coordinates */ ac = sf_iaxa(Fcip,2); sf_setlabel(ac,"c"); sf_setunit(ac,""); if(verb) sf_raxa(ac); nc = sf_n(ac); /*------------------------------------------------------------*/ /* setup output */ if(adj) { Fimg = sf_input ("in"); /* read img */ ahz=sf_iaxa(Fimg,1); nhz=(sf_n(ahz)-1)/2; if(verb) sf_raxa(ahz); ahx=sf_iaxa(Fimg,2); nhx=(sf_n(ahx)-1)/2; if(verb) sf_raxa(ahx); ahy=sf_iaxa(Fimg,3); nhy=(sf_n(ahy)-1)/2; if(verb) sf_raxa(ahy); aht=sf_iaxa(Fimg,4); nht=(sf_n(aht)-1)/2; if(verb) sf_raxa(aht); aa=sf_maxa(1,0,1); sf_setlabel(aa,""); sf_setunit(aa,""); /* set output axes */ Fwfl = sf_output("out"); /* write wfl */ sf_oaxa(Fwfl,az,1); sf_oaxa(Fwfl,ax,2); sf_oaxa(Fwfl,ay,3); sf_oaxa(Fwfl,at,4); sf_oaxa(Fwfl,aa,5); } else { Fwfl = sf_input ( "in"); /* read wfl */ if(! sf_getint("nhz",&nhz)) nhz=0; /* z lags */ dhz=2*sf_d(az); ahz=sf_maxa(2*nhz+1,-nhz*dhz,dhz); sf_setlabel(ahz,"hz"); sf_setunit(ahz,""); if(verb) sf_raxa(ahz); if(! sf_getint("nhx",&nhx)) nhx=0; /* x lags */ dhx=2*sf_d(ax); ahx=sf_maxa(2*nhx+1,-nhx*dhx,dhx); sf_setlabel(ahx,"hx"); sf_setunit(ahx,""); if(verb) sf_raxa(ahx); if(! sf_getint("nhy",&nhy)) nhy=0; /* y lags */ dhy=2*sf_d(ay); ahy=sf_maxa(2*nhy+1,-nhy*dhy,dhy); sf_setlabel(ahy,"hy"); sf_setunit(ahy,""); if(verb) sf_raxa(ahy); if(! sf_getint("nht",&nht)) nht=0; /* t lags */ dht=2*sf_d(at); aht=sf_maxa(2*nht+1,-nht*dht,dht); sf_setlabel(aht,"ht"); sf_setunit(aht,""); if(verb) sf_raxa(aht); Fimg = sf_output("out"); /* write img */ sf_oaxa(Fimg,ahz,1); sf_oaxa(Fimg,ahx,2); sf_oaxa(Fimg,ahy,3); sf_oaxa(Fimg,aht,4); sf_oaxa(Fimg, ac,5); } /*------------------------------------------------------------*/ if(! sf_getbool("gaus",&gaus)) gaus=false; /* Gaussian taper */ if(gaus) { if(! sf_getfloat("gsx",&gsx)) gsx=0.25*sf_n(ahx)*sf_d(ahx); gsx=(nhx==0)?1:1./(2*gsx*gsx); if(! sf_getfloat("gsy",&gsy)) gsy=0.25*sf_n(ahy)*sf_d(ahy); gsy=(nhy==0)?1:1./(2*gsy*gsy); if(! sf_getfloat("gsz",&gsz)) gsz=0.25*sf_n(ahz)*sf_d(ahz); gsz=(nhz==0)?1:1./(2*gsz*gsz); if(! sf_getfloat("gst",&gst)) gst=0.25*sf_n(aht)*sf_d(aht); gst=(nht==0)?1:1./(2*gst*gst); } /*------------------------------------------------------------*/ /* allocate arrays */ opr=sf_floatalloc4(nz,nx,ny,sf_n(aht)); wfl=sf_floatalloc4(nz,nx,ny,sf_n(aht)); img=sf_floatalloc5(sf_n(ahz),sf_n(ahx),sf_n(ahy),sf_n(aht),sf_n(ac)); /*------------------------------------------------------------*/ /* CIP coordinates */ cc= (pt3d*) sf_alloc(nc,sizeof(*cc)); pt3dread1(Fcip,cc,nc,3); mcxall=sf_intalloc2(sf_n(ahx),sf_n(ac)); pcxall=sf_intalloc2(sf_n(ahx),sf_n(ac)); mcyall=sf_intalloc2(sf_n(ahy),sf_n(ac)); pcyall=sf_intalloc2(sf_n(ahy),sf_n(ac)); mczall=sf_intalloc2(sf_n(ahz),sf_n(ac)); pczall=sf_intalloc2(sf_n(ahz),sf_n(ac)); ccin=sf_boolalloc(sf_n(ac)); cxmin = sf_o(ax) + nhx *sf_d(ax); cxmax = sf_o(ax) + (sf_n(ax)-1-nhx)*sf_d(ax); cymin = sf_o(ay) + nhy *sf_d(ay); cymax = sf_o(ay) + (sf_n(ay)-1-nhy)*sf_d(ay); czmin = sf_o(az) + nhz *sf_d(az); czmax = sf_o(az) + (sf_n(az)-1-nhz)*sf_d(az); for(ic=0;ic<nc;ic++) { ccin[ic]=(cc[ic].x>=cxmin && cc[ic].x<=cxmax && cc[ic].y>=cymin && cc[ic].y<=cymax && cc[ic].z>=czmin && cc[ic].z<=czmax)?true:false; if(ccin[ic]) { icx = 0.5+(cc[ic].x-sf_o(ax))/sf_d(ax); for(ihx=-nhx; ihx<nhx+1; ihx++) { mcxall[ic][nhx+ihx] = icx-ihx; pcxall[ic][nhx+ihx] = icx+ihx; } icy = 0.5+(cc[ic].y-sf_o(ay))/sf_d(ay); for(ihy=-nhy; ihy<nhy+1; ihy++) { mcyall[ic][nhy+ihy] = icy-ihy; pcyall[ic][nhy+ihy] = icy+ihy; } icz = 0.5+(cc[ic].z-sf_o(az))/sf_d(az); for(ihz=-nhz; ihz<nhz+1; ihz++) { mczall[ic][nhz+ihz] = icz-ihz; pczall[ic][nhz+ihz] = icz+ihz; } } } mctall=sf_intalloc(sf_n(aht)); pctall=sf_intalloc(sf_n(aht)); for (iht=0; iht<sf_n(aht); iht++) { mctall[iht]= iht; pctall[iht]=sf_n(aht)-1-iht; } if(adj) { /* ADJIONT OPERATOR */ for(iht=0;iht<sf_n(aht);iht++) CICLOOP( wfl[iht][iy][ix][iz]=0; ); /* zero wfl */ for(it=0;it<nt;it++) sf_floatwrite(wfl[0][0][0],nz*nx*ny,Fwfl); /* reserve wfl */ sf_seek(Fwfl,0,SEEK_SET); /* seek back */ sf_floatread(img[0][0][0][0],sf_n(ac)*sf_n(ahy)*sf_n(ahx)*sf_n(ahz)*sf_n(aht),Fimg); /* read img */ ; applyScaling (img,ac,aht,ahx,ahy,ahz,scale); /* scaling */ if(gaus) applyGaussian(img,ac,aht,ahx,ahy,ahz,gst,gsx,gsy,gsz); /* Gaussian */ lht=0; itO=-999999; itW=-999999; for(it=-nht;it<nt+nht;it++) { if(verb) fprintf(stderr,"\b\b\b\b\b\b%04d",it); fht=(lht+1) % sf_n(aht); if(it<nt-nht) { itO = it + nht; if( !oprcausal ) sf_seek(Fopr,(off_t)(nt-1-itO)*nslice,SEEK_SET); else sf_seek(Fopr,(off_t) itO *nslice,SEEK_SET); sf_floatread(opr[ lht ][0][0],nz*nx*ny,Fopr); } for(iht=0;iht<sf_n(aht);iht++) { mctall[iht] = (mctall[iht]+1) % sf_n(aht); /* cycle iht index */ pctall[iht] = (pctall[iht]+1) % sf_n(aht); } if(it>=0+nht && it<nt-nht) { #ifdef _OPENMP #pragma omp parallel for schedule(dynamic) \ private(ic, ihx,ihy,ihz,iht,mcx, mcy, mcz, mct, pcx, pcy, pcz, pct) \ shared (nc,ccin,ahx,ahy,ahz,aht,mcxall,mcyall,mczall,mctall,pcxall,pcyall,pczall,pctall) #endif for(ic=0;ic<nc;ic++){ if(ccin[ic]) { /* sum over c only! */ if(pos){ EICLOOP( wfl [mct][mcy][mcx][mcz] += opr [pct][pcy][pcx][pcz] * img[ic][iht][ihy][ihx][ihz]; ); }else{ EICLOOP( wfl [pct][pcy][pcx][pcz] += opr [mct][mcy][mcx][mcz] * img[ic][iht][ihy][ihx][ihz]; ); } } }