int main (int argc, char *argv[]) { int m, k, l, seed; sf_file bshuffle,ashuffle; sf_axis ax,at,ap,av; int nx,nt,np,nv,iteration,*a1, *a2; float ***bsh, ***bsh2; sf_init(argc,argv); bshuffle=sf_input("in"); ashuffle=sf_output("out"); if (!sf_getint("iteration",&iteration)) iteration=1; if (!sf_getint("seed",&seed)) seed=2012; at=sf_iaxa( bshuffle,1); nt=sf_n(at); ax=sf_iaxa( bshuffle,3); nx=sf_n(ax); ap=sf_iaxa( bshuffle,2); np=sf_n(ap); av=sf_iaxa( bshuffle,4); nv=sf_n(av); bsh=sf_floatalloc3(nt,np,nx); bsh2=sf_floatalloc3(nt,np,nx); a1=sf_intalloc(np); a2=sf_intalloc(np); sf_warning("ntpx=%d",nt*np*nx); srand(seed); for (m=0; m<np; m++) { a1[m]=rand(); } bubble(a1, a2, np); for (k=0; k<nv; k++) { sf_floatread(bsh[0][0],nt*np*nx,bshuffle); for(l=0; l<nx; l++) { for (m=0; m<np; m++) { memcpy(bsh2[l][m], bsh[l][a2[m]], nt*sizeof(float)); } } sf_floatwrite(bsh2[0][0],nt*np*nx,ashuffle); } free(bsh[0][0]); free(bsh[0]); free(bsh); free(bsh2[0][0]); free(bsh2[0]); free(bsh2); free(a1); free(a2); exit (0); }
/*------------------------------------------------------------*/ lint2d lint2d_make( int na, pt2d *aa, fdm2d fdm) /*< init 2D linear interpolation >*/ { lint2d ca; int ia; float f1,f2; ca = (lint2d) sf_alloc(1,sizeof(*ca)); ca->n = na; ca->w00 = sf_floatalloc(na); ca->w01 = sf_floatalloc(na); ca->w10 = sf_floatalloc(na); ca->w11 = sf_floatalloc(na); ca->jz = sf_intalloc(na); ca->jx = sf_intalloc(na); for (ia=0; ia<na; ia++) { if(aa[ia].z >= fdm->ozpad && aa[ia].z < fdm->ozpad + (fdm->nzpad-1)*fdm->dz && aa[ia].x >= fdm->oxpad && aa[ia].x < fdm->oxpad + (fdm->nxpad-1)*fdm->dx ) { ca->jz[ia] = NINT((aa[ia].z-fdm->ozpad)/fdm->dz); ca->jx[ia] = NINT((aa[ia].x-fdm->oxpad)/fdm->dx); f1 = (aa[ia].z-fdm->ozpad)/fdm->dz - ca->jz[ia]; f2 = (aa[ia].x-fdm->oxpad)/fdm->dx - ca->jx[ia]; } else { ca->jz[ia] = 0; ca->jx[ia] = 0; f1 = 1; f2 = 0; } ca->w00[ia] = (1-f1)*(1-f2); ca->w01[ia] = ( f1)*(1-f2); ca->w10[ia] = (1-f1)*( f2); ca->w11[ia] = ( f1)*( f2); } return ca; }
/*------------------------------------------------------------*/ lint2d lint2d_make(int na, pt2d* aa, fdm2d fdm) /*< init 2D linear interpolation >*/ { lint2d ca; int ia; float f1,f2; ca = (lint2d) sf_alloc(1,sizeof(*ca)); ca->n = na; ca->w00 = sf_floatalloc(na); ca->w01 = sf_floatalloc(na); ca->w10 = sf_floatalloc(na); ca->w11 = sf_floatalloc(na); ca->j1 = sf_intalloc(na); ca->j2 = sf_intalloc(na); for (ia=0;ia<na;ia++) { if(aa[ia].z >= fdm->o1pad && aa[ia].z < fdm->o1pad + (fdm->n1pad-1)*fdm->d1 && aa[ia].x >= fdm->o2pad && aa[ia].x < fdm->o2pad + (fdm->n2pad-1)*fdm->d2 ) { ca->j1[ia] = (int)( (aa[ia].z-fdm->o1pad)/fdm->d1); ca->j2[ia] = (int)( (aa[ia].x-fdm->o2pad)/fdm->d2); f1 = (aa[ia].z-fdm->o1pad)/fdm->d1 - ca->j1[ia]; f2 = (aa[ia].x-fdm->o2pad)/fdm->d2 - ca->j2[ia]; } else { ca->j1[ia] = 0; ca->j2[ia] = 0; f1 = 1; f2 = 0; } ca->w00[ia] = (1-f1)*(1-f2); ca->w01[ia] = ( f1)*(1-f2); ca->w10[ia] = (1-f1)*( f2); ca->w11[ia] = ( f1)*( f2); } return ca; }
float wmedian(float *temp,float *weight,int nfw) /*< get a weighted median value >*/ { int i, j, pass, *index, b; float wm, a, *data; data = sf_floatalloc(nfw); index = sf_intalloc(nfw); for (j=0; j < nfw; j++) { data[j] = temp[j]*weight[j]; index[j] = j; } for(pass=1; pass < nfw; pass++) { for(i=0; i < nfw-pass; i++) { if(data[i] > data[i+1]) { a = data[i]; b = index[i]; data[i] = data[i+1]; index[i] = index[i+1]; data[i+1] = a; index[i+1] = b; } } } wm = temp[index[nfw/2]]; free(index); free(data); return wm; }
map4 stretch4_init (int n1, float o1, float d1 /* regular axis */, int nd /* data samples */, float eps /* regularization */) /*< initialize >*/ { int i; map4 str; str = (map4) sf_alloc (1, sizeof(*str)); str->nt = n1; str->t0 = o1; str->dt = d1; str->nd = nd; str->eps = eps; str->x = sf_intalloc (nd); str->m = sf_boolalloc (nd); str->w = sf_floatalloc2 (4,nd); str->diag = sf_floatalloc (str->nt); for (i = 0; i < 3; i++) { str->offd[i] = sf_floatalloc (str->nt-1-i); } str->slv = sf_banded_init (str->nt,3); str->tslv = sf_spline4_init(str->nt); return str; }
int mutingf(int nt, int nx, float dt, float dx, float dz, int isx, int isz, int gpz, float vel, int wd, float **dat) /*< muting function >*/ { float z2; /* source location */ float tt; int *t; int ix,it,cut; t = sf_intalloc(nx); z2= powf((isz-gpz)*dz,2); #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,tt,cut) #endif for (ix = 0; ix < nx; ix++) { tt = sqrtf( powf((isx-ix)*dx,2) + z2 ) / vel; cut = (int) (tt/dt) + wd; /* why cannot use int to convert */ if (cut > nt) cut = nt; t[ix] = cut; } #ifdef _OPENMP #pragma omp parallel for default(shared) private(ix,it) #endif for (ix = 0; ix < nx; ix++) { for (it = 0; it < t[ix]; it ++) { dat[ix][it] = 0.0f; } } return 0; }
sf_map sf_stretch_init (int n1, float o1, float d1 /* regular axis */, int nd /* data length */, float eps /* regularization */, bool narrow /* if zero boundary */) /*< initialize >*/ { sf_map str; str = (sf_map) sf_alloc (1, sizeof(*str)); str->nt = n1; str->t0 = o1; str->dt = d1; str->nd = nd; str->eps = eps; str->narrow = narrow; str->x = sf_intalloc (nd); str->m = sf_boolalloc (nd); str->w = sf_floatalloc (nd); str->diag = sf_floatalloc (n1); str->offd = sf_floatalloc (n1-1); str->slv = sf_tridiagonal_init (n1); return str; }
nfilter nallocate(int np /* number of patches */, int nd /* data size */, int *nh /* filter size [np] */, int *pch /* patching [nd] */) /*< allocate >*/ { nfilter aa; int ip, id; aa = (nfilter) sf_alloc(1,sizeof(*aa)); aa->np = np; aa->hlx = (sf_filter*) sf_alloc(np,sizeof(sf_filter)); for (ip=0; ip < np; ip++) { aa->hlx[ip] = sf_allocatehelix(nh[ip]); } aa->pch = sf_intalloc(nd); for (id=0; id < nd; id++) { aa->pch[id] = pch[id]; } aa->mis = NULL; return aa; }
sf_upgrad sf_upgrad_init(int mdim /* number of dimensions */, const int *mm /* [dim] data size */, const float *d /* [dim] data sampling */) /*< initialize >*/ { sf_upgrad upg; int i; if (mdim > 3) sf_error("%s: dim=%d > 3",__FILE__,mdim); ndim = mdim; nn = mm; nt = 1; for (i=0; i < ndim; i++) { ss[i] = nt; nt *= nn[i]; dd[i] = 1.0/(d[i]*d[i]); } upg = (sf_upgrad) sf_alloc(1,sizeof(*upg)); upg->update = sf_ucharalloc2(2,nt); upg->ww = sf_floatalloc2(ndim+1,nt); upg->order = sf_intalloc(nt); return upg; }
void fastmarch_init(int nx_init, int nz_init, int nt_init, float hx_init, float hz_init, float ht_init, float *x0_init, float *t0_init, float *v_init, float *s_init) /*< initialize >*/ { int i,j,ind, nxz; nx = nx_init; nz = nz_init; nt = nt_init; nt1 = nt-1; nx1 = nx-1; hx = hx_init; hz = hz_init; ht = ht_init; x0 = x0_init; t0 = t0_init; v = v_init; s = s_init; nxz = nx*nz; pup= sf_intalloc(nxz); ms= sf_charalloc(nxz); r= sf_intalloc(nxz); p= sf_intalloc(nxz); count=0; for( i=0;i<nx;i++ ) { *(pup+i)=2; *(ms+i)='a'; ind=i; for( j=1;j<nz;j++ ) { ind+=nx; *(pup+ind)=0; *(ms+ind)='u'; } } }
void vp_plot_init(int n2) { int i, len; dashtype = sf_floatalloc(n2); if (!sf_getfloats ("dash",dashtype,n2)) { /* line dash type 0 continuos (default) 1 fine dash 2 fine dot 3 dash 4 large dash 5 dot dash 6 large dash small dash 7 double dot 8 double dash 9 loose dash The part after the decimal point determines the pattern repetition interval */ for (i = 0; i < n2; i++) dashtype[i] = 0.; } fat = sf_intalloc(n2); if (!sf_getints("plotfat",fat,n2)) { /* line fatness */ for (i = 0; i < n2; i++) fat[i] = 0; } col = sf_intalloc(n2); if (!sf_getints("plotcol",col,n2)) { /* line color 7 white 6 yellow (default) 5 cyan 4 green 3 magenta 2 red 1 blue 0 black */ for (i = 0; i < n2; i++) col[i] = 6 - (i % 6); } }
void* mflt_init(int m1, int mw) /*< median filter initialize >*/ { struct tag_mflt *p; p=sf_alloc(1, sizeof(struct tag_mflt)); p->n1 = m1; p->nw = mw; p->bi = sf_floatalloc(m1+2*mw); p->pi = sf_intalloc(2*mw+1); return p; }
void medbin_init (float **coord /* coordinates [nd][2] */, float o1, float o2, float d1, float d2, int n1, int n2 /* axes */, int nd_in /* number of data points */) /*< initialize >*/ { int id, im, i1, i2, start; nd = nd_in; nm = n1*n2; ct = sf_intalloc(nm); pt = sf_intalloc(nm); k = sf_intalloc(nd); m = sf_boolalloc(nd); bd = sf_floatalloc(nd); for (im=0; im < nm; im++) { ct[im] = 0.; } for (id = 0; id < nd; id++) { i1 = 0.5 + (coord[id][0] - o1)/d1; i2 = 0.5 + (coord[id][1] - o2)/d2; m[id] = (bool) ((i1 >= 0) && (i1 < n1) && (i2 >= 0) && (i2 < n2)); if (m[id]) { im = i2*n1+i1; ct[im]++; k[id] = im; } } start = 0; for (im=0; im < nm; im++) { pt[im] = start; start += ct[im]; } }
sf_filter steep(int dim /* number of dimensions */, int *n /* data size [dim] */, int *a /* filter size [dim] */, float *d /* axis sampling [dim] */, float vel /* velocity */, float tgap /* time gap */) /*< define PEF >*/ { int *lag, c[SF_MAX_DIM], i, h, na, j; float x, t0; sf_filter aa; na = 1; for (j=0; j < dim; j++) { na *= a[j]; } lag = sf_intalloc(na); for (h=i=0; i < na; i++) { sf_line2cart(dim, a, i, c); for (j=0; j < dim-1; j++) { c[j] -= (a[j]-1)/2; } t0 = 0.; for (j=0; j < dim-1; j++) { x = d[j]*c[j]; t0 += x*x; } t0 = sqrtf(t0)/vel; if (t0 < tgap) t0 = tgap; x = d[dim-1]*c[dim-1]; if(x >= t0) { lag[h] = sf_cart2line(dim, n, c); h++; } } aa = sf_allocatehelix(h); for (i=0; i < h; i++) { aa->lag[i] = lag[i]; aa->flt[i] = 0.; } free(lag); return aa; }
int main(int argc, char*argv[]) { sf_file in, out ; int i1, i2, n1, n2, *v; float o1, d1, **u; char *l1, *u1; sf_axis ax; sf_init(argc, argv); in = sf_input("in"); /* delay file (int) */ out = sf_output("out"); if(!sf_histint(in, "n1", &n2)) sf_error("n1 needed"); sf_shiftdim(in, out, 1); if(!sf_getint("n1", &n1)) n1=1000; /* samples */ if(!sf_getfloat("o1", &o1)) o1=0.0; /* sampling interval */ if(!sf_getfloat("d1", &d1)) d1=0.004; /* original */ if((l1=sf_getstring("l1")) == NULL) l1="Time"; /* label "Time" */ if((u1=sf_getstring("u1")) == NULL) u1="s"; /* unit "s" */ ax = sf_maxa(n1, o1, d1); sf_setlabel(ax, l1); sf_setunit(ax, u1); sf_oaxa(out, ax, 1); sf_putint(out, "n2", n2); sf_settype(out, SF_FLOAT); v = sf_intalloc(n2); u = sf_floatalloc2(n1, n2); sf_intread(v, n2, in); for(i2=0; i2<n2; i2++) for(i1=0; i1<n1; i1++) if(i1==v[i2]) u[i2][i1] = 1; else u[i2][i1] = 0; sf_floatwrite(u[0], n1*n2, out); free(v); free(u[0]); free(u); return 0; }
void stretch_init ( int n1 /* trace length */, float o1 /* trace origin */, float d1 /* trace sampling */, int n2 /* number of data samples */) /*< initialization >*/ { nt = n1; t0 = o1; dt = d1; nd = n2; x = sf_intalloc(nd); m = sf_boolalloc(nd); w = sf_floatalloc(nd); }
int main(int argc, char* argv[]) { int i1, n1, i2, n2, nv, two, *trace; float o1, o2, d1, d2, x, y, **vert; sf_file inp, out, poly; sf_init(argc,argv); inp = sf_input("in"); out = sf_output("out"); if (!sf_histint(inp,"n1",&n1)) sf_error("No n1= in input"); if (!sf_histint(inp,"n2",&n2)) sf_error("No n2= in input"); if (!sf_histfloat(inp,"d1",&d1)) d1=1.; if (!sf_histfloat(inp,"d2",&d2)) d2=1.; if (!sf_histfloat(inp,"o1",&o1)) o1=0.; if (!sf_histfloat(inp,"o2",&o2)) o2=0.; poly = sf_input("poly"); /* list of polygon vertices */ if (SF_FLOAT != sf_gettype(poly)) sf_error("Need float type in poly"); if (!sf_histint(poly,"n1",&two) || 2 != two) sf_error("Need n1=2 in poly"); if (!sf_histint(poly,"n2",&nv)) sf_error("No n2= in poly"); trace = sf_intalloc(n1); vert = sf_floatalloc2(2,nv); sf_floatread(vert[0],2*nv,poly); sf_settype(out,SF_INT); for (i2=0; i2 < n2; i2++) { y = o2+i2*d2; for (i1=0; i1 < n1; i1++) { x = o1+i1*d1; trace[i1] = pnpoly(nv, vert, x, y); } sf_intwrite(trace,n1,out); } exit(0); }
/*------------------------------------------------------------*/ slo3d slow3_init(cub3d cub, sf_fslice slice_, /* slowness slice */ int nrmax, /* maximum number of references */ float dsmax, float twoway ) /*< initialize slowness >*/ { int imz, jj; int ompith=0; /*------------------------------------------------------------*/ slo3d slo; slo = (slo3d) sf_alloc(1,sizeof(*slo)); slo->slice=slice_; slo->nrmax=nrmax; slo->dsmax=dsmax; if(twoway) { slo->twoway = 2; } else { slo->twoway = 1; } slo->ss = sf_floatalloc3(cub->alx.n,cub->aly.n,cub->ompnth); /* slowness */ slo->so = sf_floatalloc3(cub->alx.n,cub->aly.n,cub->ompnth); /* slowness */ slo->sm = sf_floatalloc2(slo->nrmax,cub->amz.n); /* ref slowness squared */ slo->nr = sf_intalloc (cub->amz.n); /* nr of ref slownesses */ for (imz=0; imz<cub->amz.n; imz++) { sf_fslice_get(slo->slice,imz,slo->ss[0][0]); slow3_twoway(cub,slo,slo->ss,ompith); slo->nr[imz] = slow3(slo->nrmax, slo->dsmax, cub->alx.n*cub->aly.n, slo->ss[0][0], slo->sm[imz]); } for (imz=0; imz<cub->amz.n-1; imz++) { for (jj=0; jj<slo->nr[imz]; jj++) { slo->sm[imz][jj] = 0.5*(slo->sm[imz][jj]+slo->sm[imz+1][jj]); } } return slo; }
void sf_trianglen_init (int ndim /* number of dimensions */, int *nbox /* triangle radius [ndim] */, int *ndat /* data dimensions [ndim] */) /*< initialize >*/ { int i; dim = ndim; n = sf_intalloc(dim); tr = (sf_triangle*) sf_alloc(dim,sizeof(sf_triangle)); nd = 1; for (i=0; i < dim; i++) { tr[i] = (nbox[i] > 1)? sf_triangle_init (nbox[i],ndat[i],false): NULL; s[i] = nd; n[i] = ndat[i]; nd *= ndat[i]; } tmp = sf_floatalloc (nd); }
void sf_psss_init(int nw0,int nx0,int nz, float dw,float dx, float dz0, float *v0) /*< initialize >*/ { int i; nw=nw0; nx=nx0; dz=dz0*2.0*SF_PI/(dx*nx); vel=(float*)sf_floatalloc(nz); k2=(int*)sf_intalloc(nx); for(i=0; i<nx/2+1; i++) k2[i] = i*i; for(i=nx/2+1; i<nx; i++) k2[i] = (i-nx)*(i-nx); for(i=0; i<nz; i++) { vel[i] = dw*dx*nx/v0[i]; vel[i] *= vel[i]; } }
int main(int argc, char* argv[]) { bool verb; sf_file Fu,Fw; /* I/O files */ sf_axis a1,a2,a3; /* cube axes */ float ***uu=NULL, ***ww=NULL; int *ii,ik; int nh1,nh2,nh3; int nb1,nb2,nb3; int ih1,ih2,ih3; int ib1,ib2,ib3; int n3; int i3; int j1, j2, j3; int k1, k2, k3; int ompchunk; int lo1,hi1; int lo2,hi2; int lo3,hi3; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1; /* OpenMP data chunk size */ if(! sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ Fu = sf_input ("in" ); /* input field */ Fw = sf_output("out"); /* wigner distribution */ /* read axes */ a1=sf_iaxa(Fu,1); sf_setlabel(a1,"a1"); if(verb) sf_raxa(a1); a2=sf_iaxa(Fu,2); sf_setlabel(a2,"a2"); if(verb) sf_raxa(a2); a3=sf_iaxa(Fu,3); sf_setlabel(a3,"a3"); if(verb) sf_raxa(a3); n3 = sf_n(a3); if(! sf_getint("nh1",&nh1)) nh1=0; if(! sf_getint("nh2",&nh2)) nh2=0; if(! sf_getint("nh3",&nh3)) nh3=0; if(n3<=1) nh3=0; if(verb) sf_warning("nh1=%d nh2=%d nh3=%d",2*nh1+1,2*nh2+1,2*nh3+1); nb1 = sf_n(a1); nb2 = sf_n(a2); nb3=2*nh3+1; uu=sf_floatalloc3(nb1,nb2,nb3); ww=sf_floatalloc3(nb1,nb2,nb3); ii = sf_intalloc(nb3); for(ib3=0;ib3<nb3;ib3++) { ii[ib3]=ib3; } /*------------------------------------------------------------*/ /* low end on axis 3 */ /*------------------------------------------------------------*/ for( ib3=0; ib3<nb3; ib3++) { for( ib2=0; ib2<nb2; ib2++) { for( ib1=0; ib1<nb1; ib1++) { ww[ib3][ib2][ib1] = 0.; } } } sf_floatread(uu[0][0],nb1*nb2*nb3,Fu); for( ih3=-nh3; ih3<nh3+1; ih3++) { lo3=SF_ABS(ih3); hi3=nb3-lo3; for( ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2; for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1; for( ib3=lo3; ib3<nh3+1;ib3++) { j3=ii[ib3-ih3]; k3=ii[ib3+ih3]; #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) \ private(ib1,ib2,j2,j1,k2,k1) \ shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww) #endif for( ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2; for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1; ww[ib3][ib2][ib1] += uu[j3][j2][j1] * uu[k3][k2][k1]; } /* nb1 */ } /* nb2 */ } /* nb3 */ } /* nh1 */ } /* nh2 */ } /* nh3 */ for(ih3=0;ih3<nh3+1;ih3++) { sf_floatwrite(ww[ih3][0],nb1*nb2,Fw); } /*------------------------------------------------------------*/ /* loop on axis 3 */ /*------------------------------------------------------------*/ if(verb) fprintf(stderr," n3\n"); if(verb) fprintf(stderr,"%5d\n",n3-1); for(i3=nh3+1;i3<n3-nh3;i3++) { if(verb) fprintf(stderr,"%5d",i3); /* zero WDF */ ib3=nh3; for( ib2=0; ib2<nb2; ib2++) { for( ib1=0; ib1<nb1; ib1++) { ww[ib3][ib2][ib1] = 0.; } } /* circulate index to slices */ ik = ii[0]; for(ib3=0;ib3<nb3-1;ib3++) { ii[ib3]=ii[ib3+1]; } ii[nb3-1]=ik; /* read new slice */ sf_floatread(uu[ ii[nb3-1] ][0],nb1*nb2,Fu); for( ih3=-nh3; ih3<nh3+1; ih3++) { for( ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2; for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1; ib3=nh3; j3=ii[ib3-ih3]; k3=ii[ib3+ih3]; #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) \ private(ib1,ib2,j2,j1,k2,k1) \ shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww) #endif for( ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2; for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1; ww[ib3][ib2][ib1] += uu[j3][j2][j1] * uu[k3][k2][k1]; } /* nb1 */ } /* nb2 */ } /* nh1 */ } /* nh2 */ } /* nh3 */ sf_floatwrite(ww[nh3][0],nb1*nb2,Fw); if(verb) fprintf(stderr,"\b\b\b\b\b"); } /*------------------------------------------------------------*/ /* high-end on axis 3*/ /*------------------------------------------------------------*/ for( ih3=-nh3; ih3<nh3+1; ih3++) { lo3=SF_ABS(ih3); hi3=nb3-lo3; for( ih2=-nh2; ih2<nh2+1; ih2++) { lo2=SF_ABS(ih2); hi2=nb2-lo2; for(ih1=-nh1; ih1<nh1+1; ih1++) { lo1=SF_ABS(ih1); hi1=nb1-lo1; for( ib3=nh3+1; ib3<hi3; ib3++) { j3=ii[ib3-ih3]; k3=ii[ib3+ih3]; #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) \ private(ib1,ib2,j2,j1,k2,k1) \ shared(ib3,j3,k3,ih2,ih1,lo2,hi2,lo1,hi1,uu,ww) #endif for( ib2=lo2; ib2<hi2; ib2++) { j2=ib2-ih2; k2=ib2+ih2; for(ib1=lo1; ib1<hi1; ib1++) { j1=ib1-ih1; k1=ib1+ih1; ww[ib3][ib2][ib1] += uu[j3][j2][j1] * uu[k3][k2][k1]; } /* nb1 */ } /* nb2 */ } /* nb3 */ } /* nh1 */ } /* nh2 */ } /* nh3 */ for(ih3=nh3+1;ih3<2*nh3+1;ih3++) { sf_floatwrite(ww[ih3][0],nb1*nb2,Fw); } /*------------------------------------------------------------*/ exit (0); }
int main(int argc, char* argv[]) { bool verb; /* verbosity flag */ bool abc; /* absorbing boundary conditions flag */ bool free; /* free surface flag*/ bool snap; /* wavefield snapshots flag */ int jsnap;/* save wavefield every *jsnap* time steps */ /* cube axes */ sf_axis at,az,ax,as,ar,bt; int it,iz,ix,is,ir, iop; int nt,nz,nx,ns,nr,nz2,nx2; float z0,x0,dt,dx,dz, idx,idz,dt2; /* Laplacian */ int nop=2; /* Laplacian operator size */ float c0, c1, c2; /* Laplacian operator coefficients */ float co,c1x,c2x,c1z,c2z; int nbz,nbx; /* boundary size */ float tz, tx; /* sponge boundary decay coefficients */ float dp; float ws; /* injected data */ /* linear interpolation */ float *fzs,*fxs, *fzr,*fxr; int *jzs,*jxs, *jzr,*jxr; float *ws00,*ws01,*ws10,*ws11; float *wr00,*wr01,*wr10,*wr11; /* boundary */ float *bzl,*bzh,*bxl,*bxh; /* I/O files */ sf_file Fw,Fs,Fr; float *ww; /* wavelet */ pt2d *ss, *rr; /* source/receiver locations */ float **tt; /* taper */ /* background */ sf_file Bv,Bd,Bu; /* velocity, data, wavefield */ float **bvv,**bvo; /* velocity */ float *bdd; /* data */ float **bum,**buo,**bup,**bud; /* wavefields */ /* perturbation */ sf_file Pv,Pd,Pu; float **pvv,**pvo; float *pdd; float **pum,**puo,**pup,**pud; int ompchunk; /* OpenMP data chunk size */ /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); if(! sf_getint("ompchunk",&ompchunk)) ompchunk=1; if(! sf_getbool("verb",&verb)) verb=false; if(! sf_getbool( "abc",&abc )) abc=false; if(! sf_getbool("snap",&snap)) snap=false; if(! sf_getbool("free",&free)) free=false; Fw = sf_input ("in" ); /* wavelet */ Fs = sf_input ("sou"); /* sources */ Fr = sf_input ("rec"); /* receivers */ Bv = sf_input ("vel"); /* velocity */ Bu = sf_output("wfl"); /* wavefield */ Bd = sf_output("out"); /* data */ Pv = sf_input ("ref"); /* velocity */ Pu = sf_output("liw"); /* linearized wavefield */ Pd = sf_output("lid"); /* linearized data */ /* read axes*/ at=sf_iaxa(Fw,1); sf_setlabel(at,"t"); nt=sf_n(at); dt=sf_d(at); if(verb) sf_raxa(at); /* time */ as=sf_iaxa(Fs,2); sf_setlabel(as,"s"); ns=sf_n(as); if(verb) sf_raxa(as); /* sources */ ar=sf_iaxa(Fr,2); sf_setlabel(ar,"r"); nr=sf_n(ar); if(verb) sf_raxa(ar); /* receivers */ az=sf_iaxa(Bv,1); sf_setlabel(az,"z"); nz=sf_n(az); dz=sf_d(az); if(verb) sf_raxa(az); /* depth */ ax=sf_iaxa(Bv,2); sf_setlabel(ax,"x"); nx=sf_n(ax); dx=sf_d(ax); if(verb) sf_raxa(ax); /* space */ /* configure wavefield snapshots */ if(snap) { if(! sf_getint("jsnap",&jsnap)) jsnap=nt; } /*------------------------------------------------------------*/ /* expand domain for absorbing boundary conditions */ if(abc) { if(! sf_getint("nbz",&nbz)) nbz=nop; if(nbz<nop) nbz=nop; if(! sf_getint("nbx",&nbx)) nbx=nop; if(nbx<nop) nbx=nop; if(! sf_getfloat("tz",&tz)) tz=0.025; if(! sf_getfloat("tx",&tx)) tx=0.025; } else { nbz=nop; nbx=nop; } /* expanded domain ( az+2 nz, ax+2 nx ) */ nz2=nz+2*nbz; z0=sf_o(az)-nbz*dz; nx2=nx+2*nbx; x0=sf_o(ax)-nbx*dx; sf_setn(az,nz2); sf_seto(az,z0); if(verb) sf_raxa(az); sf_setn(ax,nx2); sf_seto(ax,x0); if(verb) sf_raxa(ax); /*------------------------------------------------------------*/ /* setup output wavefield header */ if(snap) { bt = sf_maxa(nt/jsnap,sf_o(at),dt*jsnap); sf_setlabel(bt,"t"); sf_oaxa(Bu,az,1); sf_oaxa(Bu,ax,2); sf_oaxa(Bu,bt,3); sf_oaxa(Pu,az,1); sf_oaxa(Pu,ax,2); sf_oaxa(Pu,bt,3); } /* setup output data header */ sf_oaxa(Bd,ar,1); sf_oaxa(Bd,at,2); sf_oaxa(Pd,ar,1); sf_oaxa(Pd,at,2); dt2 = dt*dt; idz = 1/(dz*dz); idx = 1/(dx*dx); /* Laplacian coefficients */ c0=-30./12.; c1=+16./12.; c2=- 1./12.; co = c0 * (idx+idz); c1x= c1 * idx; c2x= c2 * idx; c1z= c1 * idz; c2z= c2 * idz; /*------------------------------------------------------------*/ /* allocate arrays */ ww=sf_floatalloc (nt); sf_floatread(ww ,nt ,Fw); bvv=sf_floatalloc2(nz,nx); sf_floatread(bvv[0],nz*nx,Bv); pvv=sf_floatalloc2(nz,nx); sf_floatread(pvv[0],nz*nx,Pv); /* allocate source/receiver point arrays */ ss = (pt2d*) sf_alloc(ns,sizeof(*ss)); rr = (pt2d*) sf_alloc(nr,sizeof(*rr)); pt2dread1(Fs,ss,ns,3); /* read 3 elements (x,z,v) */ pt2dread1(Fr,rr,nr,2); /* read 2 elements (x,z) */ bdd=sf_floatalloc(nr); pdd=sf_floatalloc(nr); jzs=sf_intalloc(ns); fzs=sf_floatalloc(ns); jzr=sf_intalloc(nr); fzr=sf_floatalloc(nr); jxs=sf_intalloc(ns); fxs=sf_floatalloc(ns); jxr=sf_intalloc(nr); fxr=sf_floatalloc(nr); ws00 = sf_floatalloc(ns); wr00 = sf_floatalloc(nr); ws01 = sf_floatalloc(ns); wr01 = sf_floatalloc(nr); ws10 = sf_floatalloc(ns); wr10 = sf_floatalloc(nr); ws11 = sf_floatalloc(ns); wr11 = sf_floatalloc(nr); /*------------------------------------------------------------*/ for (is=0;is<ns;is++) { if(ss[is].z >= z0 && ss[is].z < z0 + (nz2-1)*dz && ss[is].x >= x0 && ss[is].x < x0 + (nx2-1)*dx) { jzs[is] = (int)( (ss[is].z-z0)/dz); fzs[is] = (ss[is].z-z0)/dz - jzs[is]; jxs[is] = (int)( (ss[is].x-x0)/dx); fxs[is] = (ss[is].x-x0)/dx - jxs[is]; } else { jzs[is] = 0; jxs[is] = 0; fzs[is] = 1; fxs[is] = 0; ss[is].v= 0; } ws00[is] = (1-fzs[is])*(1-fxs[is]); ws01[is] = ( fzs[is])*(1-fxs[is]); ws10[is] = (1-fzs[is])*( fxs[is]); ws11[is] = ( fzs[is])*( fxs[is]); } for (ir=0;ir<nr;ir++) { if(rr[ir].z >= z0 && rr[ir].z < z0 + (nz2-1)*dz && rr[ir].x >= x0 && rr[ir].x < x0 + (nx2-1)*dx) { jzr[ir] = (int)( (rr[ir].z-z0)/dz); fzr[ir] = (rr[ir].z-z0)/dz - jzr[ir]; jxr[ir] = (int)( (rr[ir].x-x0)/dx); fxr[ir] = (rr[ir].x-x0)/dx - jxr[ir]; rr[ir].v=1; } else { jzr[ir] = 0; fzr[ir] = 1; rr[ir].v= 0; } wr00[ir] = (1-fzr[ir])*(1-fxr[ir]); wr01[ir] = ( fzr[ir])*(1-fxr[ir]); wr10[ir] = (1-fzr[ir])*( fxr[ir]); wr11[ir] = ( fzr[ir])*( fxr[ir]); } /*------------------------------------------------------------*/ /* allocate temporary arrays */ bum=sf_floatalloc2(nz2,nx2); buo=sf_floatalloc2(nz2,nx2); bup=sf_floatalloc2(nz2,nx2); bud=sf_floatalloc2(nz2,nx2); pum=sf_floatalloc2(nz2,nx2); puo=sf_floatalloc2(nz2,nx2); pup=sf_floatalloc2(nz2,nx2); pud=sf_floatalloc2(nz2,nx2); tt=sf_floatalloc2(nz2,nx2); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nx2,nz2,bum,buo,bup,bud,pum,puo,pup,pud,tt) #endif for (iz=0; iz<nz2; iz++) { for (ix=0; ix<nx2; ix++) { bum[ix][iz]=pum[ix][iz]=0; buo[ix][iz]=puo[ix][iz]=0; bup[ix][iz]=pup[ix][iz]=0; bud[ix][iz]=pud[ix][iz]=0; tt[ix][iz]=1; } } /*------------------------------------------------------------*/ /* velocity in the expanded domain (vo=vv^2)*/ bvo=sf_floatalloc2(nz2,nx2); pvo=sf_floatalloc2(nz2,nx2); #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nz,nx,bvv,pvv,bvo,pvo) #endif for (iz=0; iz<nz; iz++) { for (ix=0; ix<nx; ix++) { bvo[nbx+ix][nbz+iz] = bvv[ix][iz] * bvv[ix][iz]; pvo[nbx+ix][nbz+iz] = pvv[ix][iz]; } } /* fill boundaries */ for (iz=0; iz<nbz; iz++) { for (ix=0; ix<nx2; ix++) { bvo[ix][ iz ] = bvo[ix][ nbz ]; bvo[ix][nz2-iz-1] = bvo[ix][nz2-nbz-1]; pvo[ix][ iz ] = pvo[ix][ nbz ]; pvo[ix][nz2-iz-1] = pvo[ix][nz2-nbz-1]; } } for (iz=0; iz<nz2; iz++) { for (ix=0; ix<nbx; ix++) { bvo[ ix ][iz] = bvo[ nbx ][iz]; bvo[nx2-ix-1][iz] = bvo[nx2-nbx-1][iz]; pvo[ ix ][iz] = pvo[ nbx ][iz]; pvo[nx2-ix-1][iz] = pvo[nx2-nbx-1][iz]; } } /*------------------------------------------------------------*/ /* free surface */ if(abc && free) { for (iz=0; iz<nbz; iz++) { for (ix=0; ix<nx2; ix++) { bvo[ix][iz]=0; pvo[ix][iz]=0; } } } /*------------------------------------------------------------*/ /* sponge ABC setup */ if(abc) { for (iz=0; iz<nbz; iz++) { for (ix=0; ix<nx2; ix++) { tt[ix][ iz ] = exp( - (tz*(nbz-iz))*(tz*(nbz-iz)) ); tt[ix][nz2-iz-1] = tt[ix][iz]; } } for (iz=0; iz<nz2; iz++) { for (ix=0; ix<nbx; ix++) { tt[ ix ][iz] = exp( - (tx*(nbx-ix))*(tx*(nbx-ix)) ); tt[nx2-ix-1][iz] = tt[ix][iz]; } } } /* one-way ABC setup */ bzl=sf_floatalloc(nx2); bzh=sf_floatalloc(nx2); bxl=sf_floatalloc(nz2); bxh=sf_floatalloc(nz2); for (ix=0;ix<nx2;ix++) { dp = bvo[ix][ nop ] *dt/dz; bzl[ix] = (1-dp)/(1+dp); dp = bvo[ix][nz2-nop-1] *dt/dz; bzh[ix] = (1-dp)/(1+dp); } for (iz=0;iz<nz2;iz++) { dp = bvo[ nop ][iz] *dt/dx; bxl[iz] = (1-dp)/(1+dp); dp = bvo[nx2-nop-1][iz] *dt/dx; bxh[iz] = (1-dp)/(1+dp); } /*------------------------------------------------------------*/ /* * MAIN LOOP */ if(verb) fprintf(stderr,"\n"); for (it=0; it<nt; it++) { if(verb) fprintf(stderr,"\b\b\b\b\b%d",it); /* 4th order Laplacian operator */ #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(iz,ix) shared(nop,nx2,nz2,bud,buo,pud,puo,co,c1x,c1z,c2x,c2z,idx,idz) #endif for (ix=nop; ix<nx2-nop; ix++) { for (iz=nop; iz<nz2-nop; iz++) { bud[ix][iz] = co * buo[ix ][iz ] + c1x*(buo[ix-1][iz ] + buo[ix+1][iz ]) + c2x*(buo[ix-2][iz ] + buo[ix+2][iz ]) + c1z*(buo[ix ][iz-1] + buo[ix ][iz+1]) + c2z*(buo[ix ][iz-2] + buo[ix ][iz+2]); pud[ix][iz] = co * puo[ix ][iz ] + c1x*(puo[ix-1][iz ] + puo[ix+1][iz ]) + c2x*(puo[ix-2][iz ] + puo[ix+2][iz ]) + c1z*(puo[ix ][iz-1] + puo[ix ][iz+1]) + c2z*(puo[ix ][iz-2] + puo[ix ][iz+2]); } } /* inject source */ for (is=0;is<ns;is++) { ws = ww[it] * ss[is].v; bud[ jxs[is] ][ jzs[is] ] -= ws * ws00[is]; bud[ jxs[is] ][ jzs[is]+1] -= ws * ws01[is]; bud[ jxs[is]+1][ jzs[is] ] -= ws * ws10[is]; bud[ jxs[is]+1][ jzs[is]+1] -= ws * ws11[is]; } #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,pud,bud,pvo) #endif for (ix=0; ix<nx2; ix++) { for (iz=0; iz<nz2; iz++) { pud[ix][iz] -= bud[ix][iz] * 2*pvo[ix][iz]; } } /* velocity scale */ #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,bud,pud,bvo) #endif for (ix=0; ix<nx2; ix++) { for (iz=0; iz<nz2; iz++) { bud[ix][iz] *= bvo[ix][iz]; pud[ix][iz] *= bvo[ix][iz]; } } /* time step */ #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,bud,buo,bum,bup,pud,puo,pum,pup,dt2) #endif for (ix=0; ix<nx2; ix++) { for (iz=0; iz<nz2; iz++) { bup[ix][iz] = 2*buo[ix][iz] - bum[ix][iz] + bud[ix][iz] * dt2; bum[ix][iz] = buo[ix][iz]; buo[ix][iz] = bup[ix][iz]; pup[ix][iz] = 2*puo[ix][iz] - pum[ix][iz] + pud[ix][iz] * dt2; pum[ix][iz] = puo[ix][iz]; puo[ix][iz] = pup[ix][iz]; } } /* one-way ABC apply */ if(abc) { #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz,iop) shared(nx2,nz2,nop,buo,bum,puo,pum,bzl,bzh) #endif for(ix=0;ix<nx2;ix++) { for(iop=0;iop<nop;iop++) { iz = nop-iop; buo [ix][iz ] = bum[ix][iz+1] +(bum[ix][iz ] - buo[ix][iz+1]) * bzl[ix]; puo [ix][iz ] = pum[ix][iz+1] +(pum[ix][iz ] - puo[ix][iz+1]) * bzl[ix]; iz = nz2-nop+iop-1; buo [ix][iz ] = bum[ix][iz-1] +(bum[ix][iz ] - buo[ix][iz-1]) * bzh[ix]; puo [ix][iz ] = pum[ix][iz-1] +(pum[ix][iz ] - puo[ix][iz-1]) * bzh[ix]; } } #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,1) private(ix,iz,iop) shared(nx2,nz2,nop,buo,bum,puo,pum,bzl,bzh) #endif for(iop=0;iop<nop;iop++) { for(iz=0;iz<nz2;iz++) { ix = nop-iop; buo [ix ][iz] = bum[ix+1][iz] +(bum[ix ][iz] - buo[ix+1][iz]) * bxl[iz]; puo [ix ][iz] = pum[ix+1][iz] +(pum[ix ][iz] - puo[ix+1][iz]) * bxl[iz]; ix = nx2-nop+iop-1; buo [ix ][iz] = bum[ix-1][iz] +(bum[ix ][iz] - buo[ix-1][iz]) * bxh[iz]; puo [ix ][iz] = pum[ix-1][iz] +(pum[ix ][iz] - puo[ix-1][iz]) * bxh[iz]; } } } /* sponge ABC apply */ if(abc) { #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,ompchunk) private(ix,iz) shared(nx2,nz2,buo,bum,bud,puo,pum,pud,tt) #endif for (ix=0; ix<nx2; ix++) { for (iz=0; iz<nz2; iz++) { buo[ix][iz] *= tt[ix][iz]; bum[ix][iz] *= tt[ix][iz]; bud[ix][iz] *= tt[ix][iz]; puo[ix][iz] *= tt[ix][iz]; pum[ix][iz] *= tt[ix][iz]; bud[ix][iz] *= tt[ix][iz]; } } } /* write wavefield */ if(snap && it%jsnap==0) { sf_floatwrite(buo[0],nz2*nx2,Bu); sf_floatwrite(puo[0],nz2*nx2,Pu); } /* write data */ #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,1) private(ir) shared(bdd,pdd,rr,buo,puo,jzr,wr00,wr01,wr10,wr11) #endif for (ir=0;ir<nr;ir++) { bdd[ir] = buo[ jxr[ir] ][ jzr[ir] ] * wr00[ir] + buo[ jxr[ir] ][ jzr[ir]+1] * wr01[ir] + buo[ jxr[ir]+1][ jzr[ir] ] * wr10[ir] + buo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir]; bdd[ir] *= rr[ir].v; pdd[ir] = puo[ jxr[ir] ][ jzr[ir] ] * wr00[ir] + puo[ jxr[ir] ][ jzr[ir]+1] * wr01[ir] + puo[ jxr[ir]+1][ jzr[ir] ] * wr10[ir] + puo[ jxr[ir]+1][ jzr[ir]+1] * wr11[ir]; pdd[ir] *= rr[ir].v; } /* write data */ sf_floatwrite(bdd,nr,Bd); sf_floatwrite(pdd,nr,Pd); } if(verb) fprintf(stderr,"\n"); exit (0); }
int main (int argc, char* argv[]) { int n1, n2, n3, n12, nfw, i1, i2, i3, j, m, *coor, xc, yc, nw, nrep; bool boundary, verb, gauss; float *input, *output, *dd, *data, ax, bx; sf_file in, out, dip; sf_init (argc, argv); in = sf_input("in"); out = sf_output("out"); dip = sf_input("dip"); if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input"); if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input"); n3 = sf_leftsize(in,2); n12 = n1*n2; /* get the trace length (n1) and the number of traces (n2) and n3*/ if (!sf_getint("nfw",&nfw)) sf_error("nfw needs integer input"); /* filter-window length (positive and odd integer) */ if (!sf_getint("nw",&nw)) sf_error("nw needs integer input"); /* data-window length (positive and odd integer) */ if (nw%2 ==0) nw +=1; m = (nw-1)/2; if (!sf_getbool("boundary",&boundary)) boundary = true; /* if y, boundary is data, whereas zero*/ if (!sf_getbool("verb",&verb)) verb = false; /* verbosity flag */ if (!sf_getfloat("bx",&bx)) sf_error("Need bx="); /* exponential weight for the domain distance (different if gaussian)*/ if (!sf_getbool("gauss",&gauss)) gauss=false; /* if y, Gaussian weight, whereas Triangle weight */ if (!sf_getint("repeat",&nrep)) nrep=1; /* repeat filtering several times */ if (gauss) { if (!sf_getfloat("ax",&ax)) sf_error("Need ax="); /* Gaussian weight for the range distance */ } input = sf_floatalloc(n12); output = sf_floatalloc(n12); dd = sf_floatalloc(n12); coor = sf_intalloc(nw*2); data = sf_floatalloc(nw); for(i3=0; i3 < n3; i3++) { if (verb) sf_warning("3rd axis: %d of %d",i3+1,n3); sf_floatread(input,n12,in); sf_floatread(dd,n12,dip); for(i2=0; i2 < n2; i2++) { if (verb && (10*i2)%n2==0) sf_warning("2nd axis: %d of %d",i2+1,n2); for(i1=0; i1 < n1; i1++) { coor[0*nw+m] = i1; coor[1*nw+m] = i2; data[m] = input[coor[1*nw+m]*n1+coor[0*nw+m]]; for(j=1; j < m+1; j++) { xc = (int) coor[0*nw+j+m-1]+dd[coor[1*nw+j+m-1]*n1+coor[0*nw+j+m-1]]+0.5; yc = coor[1*nw+j+m-1]+1; if (xc < 0 || xc >= n1 || yc < 0 || yc >= n2) { coor[1*nw+j+m] = coor[1*nw+j+m-1]; coor[0*nw+j+m] = coor[0*nw+j+m-1]; if (boundary) { data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]]; } else { data[j+m] = 0.; } } else { coor[1*nw+j+m] = yc; coor[0*nw+j+m] = xc; data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]]; } } for(j=-1; j > (-1*(m+1)); j--) { xc = (int) coor[0*nw+j+m+1]-dd[coor[1*nw+j+m+1]*n1+coor[0*nw+j+m+1]]+0.5; yc = coor[1*nw+j+m+1]-1; if (xc < 0 || xc >= n1 || yc < 0 || yc >= n2) { coor[1*nw+j+m] = coor[1*nw+j+m+1]; coor[0*nw+j+m] = coor[0*nw+j+m+1]; if (boundary) { data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]]; } else { data[j+m] = 0.; } } else { coor[1*nw+j+m] = yc; coor[0*nw+j+m] = xc; data[j+m] = input[coor[1*nw+j+m]*n1+coor[0*nw+j+m]]; } } output[i2*n1+i1] = bilateral(data,nfw,ax,bx,nw,nrep,gauss); } } sf_floatwrite(output,n12,out); } exit (0); }
void dsr2_init(int nz1, float dz1 /* depth */, int nh1, float dh1, float h01 /* half-offset */, int nx1, float dx1, float x01 /* midpoint */, int nu1, float du, float u0 /* slowness grid */, int ntx, int nth /* taper size */, int nr1 /* number of references */, int npad /* padding on nh */) /*< initialize >*/ { int ix, ih, iu; int jx, jh; float x, h; float kx, kh, k; float dx, dh; float x0, h0; nz = nz1; dz = dz1; nx = nx1; dx = 2.0*SF_PI/(nx*dx1); x0 = -SF_PI/ dx1 ; nh = nh1; nh2 = nh+npad; dh = 2.0*SF_PI/(nh2*dh1); h0 = -SF_PI/ dh1 ; nu = nu1; nrmax = nr1; fft2_init(nh2,nx); /* allocate workspace */ sz = sf_floatalloc2 (nrmax,nz); /* reference slowness */ sm = sf_floatalloc2 (nrmax,nz); /* reference slowness squared*/ nr = sf_intalloc ( nz); /* number of reference slownesses */ qq = sf_floatalloc2 (nh,nu); /* image */ ks = sf_floatalloc2 (nh2,nx); /* source wavenumber */ kr = sf_floatalloc2 (nh2,nx); /* receiver wavenumber */ is = sf_intalloc2 (nh, nx); /* source reference */ ir = sf_intalloc2 (nh, nx); /* receiver reference */ ii = sf_intalloc (nx); /* midpoint reference */ pk = sf_complexalloc2 (nh2,nx); /* padded wavefield */ wx = sf_complexalloc2 (nh, nx); /* x wavefield */ wk = sf_complexalloc2 (nh2,nx); /* k wavefield */ ms = sf_intalloc2 (nh, nx); /* MRS map source */ mr = sf_intalloc2 (nh, nx); /* MRS map receiver */ ma = sf_floatalloc2 (nh, nx); /* MRS mask */ skip = sf_boolalloc3 (nrmax,nrmax,nz); /* skip slowness combination */ /* precompute wavenumbers */ for (ix=0; ix<nx; ix++) { jx = (ix < nx/2)? ix + nx/2: ix - nx/2; kx = x0 + jx*dx; x = x01 + ix*dx1; iu = 0.5+(x-u0)/du; if (iu < 0) iu=0; else if (iu >= nu) iu=nu-1; ii[ix] = iu; for (ih=0; ih<nh; ih++) { h = h01 + ih*dh1; iu = 0.5+(x-h-u0)/du; if (iu < 0) iu=0; else if (iu >= nu) iu=nu-1; is[ix][ih] = iu; iu = 0.5+(x+h-u0)/du; if (iu < 0) iu=0; else if (iu >= nu) iu=nu-1; ir[ix][ih] = iu; } for (ih=0; ih<nh2; ih++) { jh = (ih < nh2/2)? ih + nh2/2: ih - nh2/2; kh = h0 + jh*dh; k = 0.5*(kx-kh); ks[ix][ih] = k*k; k = 0.5*(kx+kh); kr[ix][ih] = k*k; } } /* precompute taper array */ taper2_init(nx,nh,ntx,nth,true,false); mms = sf_fslice_init(nh*nx,nz,sizeof(int)); mmr = sf_fslice_init(nh*nx,nz,sizeof(int)); }
int main(int argc, char* argv[]) { sf_file in=NULL, out=NULL; int n1_traces; int n1_headers; char* header_format=NULL; sf_datatype typehead; /* kls do I need to add this? sf_datatype typein; */ float* fheader=NULL; float* intrace=NULL; int dim; off_t n_in[SF_MAX_DIM]; int iaxis; int dim_output; int *indx_of_keys; bool label_argparmread,n_argparmread,o_argparmread,d_argparmread; char parameter[13]; char* label[SF_MAX_DIM]; off_t n_output[SF_MAX_DIM]; off_t n_outheaders[SF_MAX_DIM]; float o_output[SF_MAX_DIM]; float d_output[SF_MAX_DIM]; sf_axis output_axa_array[SF_MAX_DIM]; sf_file output=NULL, outheaders=NULL; char* output_filename=NULL; char* outheaders_filename=NULL; sf_init (argc,argv); /*****************************/ /* initialize verbose switch */ /*****************************/ /* verbose flag controls ammount of print */ /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */ /* fprintf(stderr,"read verbose switch. getint reads command line.\n"); */ if(!sf_getint("verbose",&verbose))verbose=1; /* \n flag to control amount of print 0 terse, 1 informative, 2 chatty, 3 debug */ fprintf(stderr,"verbose=%d\n",verbose); /******************************************/ /* input and output data are stdin/stdout */ /******************************************/ if(verbose>0)fprintf(stderr,"read infile name\n"); in = sf_input ("in"); if(verbose>0) fprintf(stderr,"read outfile name (probably default to stdout\n"); out = sf_output ("out"); if (!sf_histint(in,"n1_traces",&n1_traces)) sf_error("input data not define n1_traces"); if (!sf_histint(in,"n1_headers",&n1_headers)) sf_error("input data does not define n1_headers"); header_format=sf_histstring(in,"header_format"); if(strcmp (header_format,"native_int")==0) typehead=SF_INT; else typehead=SF_FLOAT; fprintf(stderr,"allocate headers. n1_headers=%d\n",n1_headers); fheader = sf_floatalloc(n1_headers); fprintf(stderr,"allocate intrace. n1_traces=%d\n",n1_traces); intrace= sf_floatalloc(n1_traces); /* maybe I should add some validation that n1== n1_traces+n1_headers+2 and the record length read in the second word is consistent with n1. */ /* put the history from the input file to the output */ sf_fileflush(out,in); /********************************************************************/ /* set up the output and outheaders files for the traces and headers */ /********************************************************************/ output_filename=sf_getstring("output"); /* \n output trace filename. Required parameter with no default. */ if(NULL==output_filename) sf_error("output is a required parameter"); output=sf_output(output_filename); outheaders_filename=sf_getstring("outheaders"); /* \n Output trace header file name. Default is the input data file name, with the final .rsf changed to _hdr.rsf. */ if(outheaders_filename==NULL){ /* compute headers_filename from output_filename by replacing the final .rsf with _hdr.rsf */ if(!(0==strcmp(output_filename+strlen(output_filename)-4,".rsf"))){ fprintf(stderr,"parameter output, the name of the output file,\n"); fprintf(stderr,"does not end with .rsf, so header filename cannot\n"); fprintf(stderr,"be computed by replacing the final .rsf with\n"); fprintf(stderr,"_hdr.rsf.\n"); sf_error("default for outheaders parameter cannot be computed."); } outheaders_filename=malloc(strlen(output_filename)+60); strcpy(outheaders_filename,output_filename); strcpy(outheaders_filename+strlen(output_filename)-4,"_hdr.rsf\0"); if(verbose>1) fprintf(stderr,"parameter outheader defaulted. Computed to be #%s#\n", outheaders_filename); } if(verbose>1)fprintf(stderr,"parameter outheader input or computed #%s#\n", outheaders_filename); outheaders=sf_output(outheaders_filename); /* get each of the axis information: label2, n2, o2, d2, label3, n3, o3, d3, etc label1, n1, o1, d1 is always defaulted from input */ sf_putint (output ,"n1" ,n1_traces ); sf_putint (outheaders,"n1" ,n1_headers); sf_putfloat (outheaders,"d1" ,1 ); sf_putfloat (outheaders,"o1" ,0 ); sf_putstring(outheaders,"label1","none" ); sf_putstring(outheaders,"unit1" ,"none" ); dim_output=1; for (iaxis=1; iaxis<SF_MAX_DIM; iaxis++){ label_argparmread=n_argparmread=o_argparmread=d_argparmread=false; sprintf(parameter,"label%d",iaxis+1); fprintf(stderr,"try to read %s\n",parameter); if ((label[iaxis]=sf_getstring(parameter))) { /*(label#=(2,...) name of each of the axes. label1 is not changed from input. Each label must be a header key like cdp, cdpt, or ep. The trace header values are used to define the output trace location in the output file. )*/ fprintf(stderr,"got %s=%s\n",parameter,label[iaxis]); sf_putstring(output ,parameter,label[iaxis]); sf_putstring(outheaders,parameter,label[iaxis]); label_argparmread=true; } sprintf(parameter,"n%d",iaxis+1); fprintf(stderr,"try to read %s\n",parameter); if (sf_getlargeint (parameter,&n_output[iaxis])) { /*( n#=(2,...) number of locations in the #-th dimension )*/ fprintf(stderr,"got %s=%lld\n",parameter,(long long) n_output[iaxis]); sf_putint(output ,parameter,n_output[iaxis]); sf_putint(outheaders,parameter,n_output[iaxis]); n_argparmread=true; } sprintf(parameter,"o%d",iaxis+1); if (sf_getfloat(parameter,&o_output[iaxis])) { /*( o#=(2,...) origin of the #-th dimension )*/ sf_putfloat(output ,parameter,o_output[iaxis]); sf_putfloat(outheaders,parameter,o_output[iaxis]); o_argparmread=true; } sprintf(parameter,"d%d",iaxis+1); if (sf_getfloat(parameter,&d_output[iaxis])) { /*( d#=(2,...) delta in the #-th dimension )*/ sf_putfloat(output ,parameter,d_output[iaxis]); sf_putfloat(outheaders,parameter,d_output[iaxis]); d_argparmread=true; } if(!label_argparmread && !n_argparmread && ! o_argparmread && !d_argparmread){ /* none of the parameter were read you read all the parameters in the previous iteration compute the output dimension and exit the loop */ dim_output=iaxis; break; } if(label_argparmread && n_argparmread && o_argparmread && d_argparmread){ /* all the parameters for thisi axis were read. loop for next iaxis */ if(verbose>0){ fprintf(stderr,"label, n, i, and d read for iaxis%d\n",iaxis+1); } } else { sf_warning("working on iaxis=%d. Program expects to read\n",iaxis+1); sf_warning("label%d, n%d, i%d, and d%d from command line.\n", iaxis+1,iaxis+1,iaxis+1,iaxis+1); if(!label_argparmread) sf_error("unable to read label%d\n",iaxis+1); if(!n_argparmread ) sf_error("unable to read n%d \n",iaxis+1); if(!o_argparmread ) sf_error("unable to read o%d \n",iaxis+1); if(!d_argparmread ) sf_error("unable to read d$d \n",iaxis+1); } } /* if the input file is higher dimention than the output, then the size of all the higher dimensions must be set to 1. */ /* kls use sf_axis to get structure for each axis with n, o, d, l, and u this can be used to compute the index for sf_seek */ dim = sf_largefiledims(in,n_in); for (iaxis=dim_output; iaxis<dim; iaxis++){ sprintf(parameter,"n%d",iaxis+1); sf_putint(output,parameter,1); sf_putint(outheaders,parameter,1); } /* for a test zero n_output and dim_output and see it you can read the history file */ sf_largefiledims(output,n_output); sf_largefiledims(outheaders,n_outheaders); if(verbose>1){ for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){ fprintf(stderr,"from sf_largefiledims(output.. n%d=%lld outheaders=%lld\n", iaxis+1,(long long) n_output[iaxis],(long long) n_outheaders[iaxis]); } } /* the list header keys (including any extras) and get the index into the header vector */ segy_init(n1_headers,in); indx_of_keys=sf_intalloc(dim_output); for (iaxis=1; iaxis<dim_output; iaxis++){ /* kls need to check each of these key names are in the segy header and make error message for invalid keys. Of does segykey do this? NO, just segmentation fault. */ if(verbose>0)fprintf(stderr,"get index of key for %s\n",label[iaxis]); indx_of_keys[iaxis]=segykey(label[iaxis]); } sf_fileflush(output,in); sf_putint(outheaders,"n1",n1_headers); if(0==strcmp("native_int",sf_histstring(in,"header_format"))){ sf_settype(outheaders,SF_INT); } else { sf_settype(outheaders,SF_FLOAT); } sf_fileflush(outheaders,in); fprintf(stderr,"start trace loop\n"); /* kls maybe this should be in function sf_tahwritemapped_init */ { off_t file_offset; off_t i_output[SF_MAX_DIM]; float temp_float=0.0; for(iaxis=0; iaxis<SF_MAX_DIM; iaxis++){ i_output[iaxis]=n_output[iaxis]-1; } file_offset=sf_large_cart2line(dim_output,n_output,i_output)*sizeof(float); sf_seek(output,file_offset,SEEK_SET); sf_floatwrite(&temp_float,1,output); i_output[0]=n_outheaders[0]-1; file_offset=sf_large_cart2line(dim_output,n_outheaders,i_output)* sizeof(float); sf_seek(outheaders,file_offset,SEEK_SET); sf_floatwrite(&temp_float,1,outheaders); } for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){ /* sf_axis temp; */ output_axa_array[iaxis]=sf_iaxa(output,iaxis+1); if(verbose>2){ /* temp=output_axa_array[iaxis]; */ fprintf(stderr,"axis=%d sf_n(output_axa_array[iaxis])=%d\n", iaxis+1,sf_n(output_axa_array[iaxis])); } /* kls why does this fail? fprintf(stderr,"temp->n=%d\n",temp->n); */ } /***************************/ /* start trace loop */ /***************************/ fprintf(stderr,"start trace loop\n"); while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){ if(verbose>1){ for(iaxis=2; iaxis<dim_output; iaxis++){ fprintf(stderr,"label[%d]=%s", iaxis,label[iaxis]); if(typehead == SF_INT)fprintf(stderr,"%d", ((int*)fheader)[indx_of_keys[iaxis]]); else fprintf(stderr,"%f", fheader[indx_of_keys[iaxis]]); } fprintf(stderr,"\n"); } tahwritemapped(verbose,intrace, fheader, n1_traces, n1_headers, output, outheaders, typehead, output_axa_array, indx_of_keys, dim_output, n_output,n_outheaders); /**********************************************/ /* write trace and headers to the output pipe */ /**********************************************/ put_tah(intrace, fheader, n1_traces, n1_headers, out); } exit(0); }
int main(int argc, char* argv[]) { int nx, na, na2, ia, nz, order, maxsplit, ix, iz, *siz; float **place, *slow, **out, dx,dz, x0,z0, x[2]; float max1, min1, max2, min2; bool isvel, lsint; agrid grd; sf_file vel, outp, size, grid; sf_init (argc,argv); /* get 2-D grid parameters */ vel = sf_input("in"); if (!sf_histint(vel,"n1",&nz)) sf_error("No n1= in input"); if (!sf_histint(vel,"n2",&nx)) sf_error("No n2= in input"); if (!sf_histfloat(vel,"d1",&dz)) sf_error("No d1= in input"); if (!sf_histfloat(vel,"d2",&dx)) sf_error("No d2= in input"); if (!sf_histfloat(vel,"o1",&z0)) z0=0.; if (!sf_histfloat(vel,"o2",&x0)) x0=0.; outp = sf_output("out"); sf_putint(outp,"n4",nz); sf_putfloat(outp,"d4",dz); sf_putfloat(outp,"o4",z0); sf_putint(outp,"n3",nx); sf_putfloat(outp,"d3",dx); sf_putfloat(outp,"o3",x0); if (!sf_getint("na",&na)) na=60; /* number of angles */ if (!sf_getfloat("da",&da)) da=3.1; /* angle increment (in degrees) */ if (!sf_getfloat("a0",&a0)) a0=-90.; /* initial angle (in degrees) */ if (!sf_getint("maxsplit",&maxsplit)) maxsplit=10; /* maximum splitting for adaptive grid */ if (!sf_getfloat("minx",&min1)) min1=0.5*dx; /* parameters for adaptive grid */ if (!sf_getfloat("maxx",&max1)) max1=2.*dx; if (!sf_getfloat("mina",&min2)) min2=0.5*da; if (!sf_getfloat("maxa",&max2)) max2=2.*da; sf_putint(outp,"n2",na); sf_putfloat(outp,"d2",da); sf_putfloat(outp,"o2",a0); da *= (SF_PI/180.); a0 *= (SF_PI/180.); sf_putint(outp,"n1",5); size = sf_output("size"); sf_putint(size,"n1",nx); sf_putint(size,"n2",nz); sf_settype(size,SF_INT); grid = sf_output("grid"); /* additional parameters */ if(!sf_getbool("vel",&isvel)) isvel=true; /* y: velocity, n: slowness */ if(!sf_getint("order",&order)) order=3; /* velocity interpolation order */ if (!sf_getbool("lsint",&lsint)) lsint=false; /* if use least-squares interpolation */ slow = sf_floatalloc(nz*nx); place = sf_floatalloc2(5,na); siz = sf_intalloc(nx); sf_floatread(slow,nx*nz,vel); if (isvel) { for(ix = 0; ix < nx*nz; ix++){ slow[ix] = 1./slow[ix]; } } ct = sf_celltrace_init (lsint, order, nz*nx, nz, nx, dz, dx, z0, x0, slow); free (slow); grd = agrid_init (na, 5, maxsplit); agrid_set (grd,place); for (iz = 0; iz < nz; iz++) { x[0] = z0+iz*dz; sf_warning("depth %d of %d;",iz+1, nz); for (ix = 0; ix < nx; ix++) { x[1] = x0+ix*dx; fill_grid(grd,min1,max1,min2,max2,(void*) x, raytrace); na2 = grid_size (grd); out = write_grid (grd); siz[ix] = na2; for (ia=0; ia < na2; ia++) { if (ia < na) sf_floatwrite (place[ia], 5, outp); sf_floatwrite(out[ia], 5, grid); } free (out); } sf_intwrite (siz,nx,size); } sf_warning("."); exit (0); }
int main(int argc, char **argv) { int n1, n2, ninf, i1, i2, i, *inter2; char *label, *unit; float o1, d1, o2, d2, x, z; float *v0, *dvdx, *dvdz, *x0, *z0, *trace, **inter; sf_file model, surface; sf_init(argc, argv); surface = sf_input("in"); model = sf_output("out"); if (SF_FLOAT != sf_gettype(surface)) sf_error("Need float input"); if (!sf_histint(surface,"n1",&n2)) sf_error("No n1= in input"); if (!sf_histfloat(surface,"d1",&d2)) sf_error("No d1= in input"); if (!sf_histfloat(surface,"o1",&o2)) o2=0.; sf_shiftdim(surface, model, 1); sf_putint(model,"n3",1); if (!sf_histint(surface,"n2",&ninf)) ninf=1; if (!sf_getint("n1",&n1)) sf_error("Need n1="); /* Number of samples on the depth axis */ if (!sf_getfloat("d1",&d1)) sf_error("Need d1="); /* Sampling of the depth axis */ if (!sf_getfloat("o1",&o1)) o1=0.; /* Origin of the depth axis */ sf_putint(model,"n1",n1); sf_putfloat(model,"d1",d1); sf_putfloat(model,"o1",o1); if (NULL == (label = sf_getstring("label1"))) label="Depth"; /* depth axis label */ sf_putstring(model,"label1",label); if (NULL != (unit = sf_getstring("unit1"))) /* depth axis unit */ sf_putstring(model,"unit1",unit); inter = sf_floatalloc2(n2,ninf); inter2 = sf_intalloc(ninf); sf_floatread(inter[0],n2*ninf,surface); ninf++; /* more layers than interfaces */ v0 = sf_floatalloc(ninf); x0 = sf_floatalloc(ninf); z0 = sf_floatalloc(ninf); dvdx = sf_floatalloc(ninf); dvdz = sf_floatalloc(ninf); /* Input layer velocities and velocity derivatives */ if (!sf_getfloats("x0",x0,ninf)) for(i=0;i< ninf;i++) x0[i] = 0.; if (!sf_getfloats("z0",z0,ninf)) for(i=0;i< ninf;i++) z0[i] = 0.; if (!sf_getfloats("v00",v0,ninf)) for(i=0;i< ninf;i++) v0[i] = 1500.+ 500*i; if (!sf_getfloats("dvdx",dvdx,ninf)) for(i=0;i< ninf;i++) dvdx[i] = 0.; if (!sf_getfloats("dvdz",dvdz,ninf)) for(i=0;i< ninf;i++) dvdz[i] = 0.; trace = sf_floatalloc(n1); /* compute linear velocity */ for(i2=0; i2 < n2; i2++) { x = o2+i2*d2; for (i=0; i < ninf-1; i++) { inter2[i] = floorf(0.5+(inter[i][i2]-o1)/d1); } for(i1=0; i1 < n1; i1++) { z = o1+i1*d1; for (i=0; i < ninf-1; i++) { if (i1 < inter2[i]) break; } trace[i1] = v0[i] + (x-x0[i])*dvdx[i] + (z-z0[i])*dvdz[i]; } sf_floatwrite(trace,n1,model); } exit(0); }
int main(int argc, char *argv[]) { clock_t tstart, tend; double duration; int numprocs, rank; float *sendbuf, *recvbuf; MPI_Comm Comm=MPI_COMM_WORLD; bool verb, wantrecord, wantwf, onlyrecord; sf_file Ffvel, Ffden, Fbvel, Fbden; sf_file Fsrc, Frcd, Fimg1, Fimg2; sf_file FGx, FGz, Fsxx, Fsxz, Fszx, Fszz; sf_file Ftmpfwf, Ftmpbwf; sf_axis at, ax, az, atau; int shtbgn, shtinv, shtnmb, shtpad, shtnmb0; int snapturn, tmpint; float **fvel, **bvel; float ***fwf, ***record, **localrec; float ***img1, **img2, ***mig1, **mig2; float *tmpsxx, *tmpsxz, *tmpszx, *tmpszz; sf_init(argc, argv); MPI_Init(&argc, &argv); MPI_Comm_size(Comm, &numprocs); MPI_Comm_rank(Comm, &rank); tstart=clock(); if(rank==0) sf_warning("numprocs=%d", numprocs); if(!sf_getbool("verb", &verb)) verb=true; if(!sf_getbool("wantrecord", &wantrecord)) wantrecord=false; if(!sf_getbool("wantwf", &wantwf)) wantwf=false; if(!sf_getbool("onlyrecord", &onlyrecord)) onlyrecord=false; Fsrc=sf_input("-input"); Fimg1=sf_output("-output"); Fimg2=sf_output("img2"); Ffvel=sf_input("fvel"); Ffden=sf_input("fden"); Fbvel=sf_input("bvel"); Fbden=sf_input("bden"); if(wantrecord) Frcd=sf_input("record"); else Frcd=sf_output("record"); if(wantwf){ Ftmpfwf=sf_output("tmpfwf"); Ftmpbwf=sf_output("tmpbwf"); } FGx=sf_input("Gx"); FGz=sf_input("Gz"); Fsxx=sf_input("sxx"); Fsxz=sf_input("sxz"); Fszx=sf_input("szx"); Fszz=sf_input("szz"); at=sf_iaxa(Fsrc, 1); nt=sf_n(at); dt=sf_d(at); if(!sf_getbool("srcdecay", &srcdecay)) srcdecay=true; if(!sf_getint("srcrange", &srcrange)) srcrange=3; if(!sf_getfloat("srctrunc", &srctrunc)) srctrunc=0.2; if(!sf_getfloat("srcalpha", &srcalpha)) srcalpha=0.5; wavelet=sf_floatalloc(nt); sf_floatread(wavelet, nt, Fsrc); if(!sf_getint("pmlsize", &pmlsize)) pmlsize=30; if(!sf_getint("nfd", &nfd)) sf_error("Need half of the FD order!"); if(!sf_getfloat("pmld0", &pmld0)) pmld0=200; if(!sf_getint("shtnmb", &shtnmb)) sf_error("Need shot number!"); if(!sf_getint("shtinv", &shtinv)) sf_error("Need shot interval!"); if(!sf_getint("shtbgn", &shtbgn)) shtbgn=0; shtpad=numprocs-shtnmb%numprocs; shtnmb0=shtnmb+shtpad; az=sf_iaxa(Ffvel, 1); nzb=sf_n(az); ax=sf_iaxa(Ffvel, 2); nxb=sf_n(ax); nxzb=nxb*nzb; nz=nzb-2*nfd-2*pmlsize; nx=nxb-2*nfd-2*pmlsize; if(!sf_getint("snapturn", &snapturn)) snapturn=1; if(!sf_getint("ginv", &ginv)) ginv=1; if(!sf_getint("wfinv", &wfinv)) wfinv=1; if(!sf_getint("spz", &spz)) spz=6; if(!sf_getint("gp", &gp)) gp=0; ng=(nx-1)/ginv+1; wfnt=(nt-1)/wfinv+1; wfdt=dt*wfinv; if(!sf_getint("ntau", &ntau)) ntau=1; if(!sf_getfloat("dtau", &dtau)) dtau=wfdt; if(!sf_getfloat("tau0", &tau0)) tau0=0; atau=sf_iaxa(Fsrc, 1); sf_setn(atau, ntau); sf_setd(atau, dtau); sf_seto(atau, tau0); if(!sf_histint(FGx, "n1", &nxz)) sf_error("No n1= in FGx!"); if(nxz != nxzb) sf_error("Dimension error!"); if(!sf_histint(FGx, "n2", &lenx)) sf_error("No n2= in FGx!"); if(!sf_histint(FGz, "n2", &lenz)) sf_error("No n2= in FGz!"); Gx=sf_floatalloc3(nzb, nxb, lenx); Gz=sf_floatalloc3(nzb, nxb, lenz); sxx=sf_intalloc(lenx); sxz=sf_intalloc(lenx); szx=sf_intalloc(lenz); szz=sf_intalloc(lenz); tmpsxx=sf_floatalloc(lenx); tmpsxz=sf_floatalloc(lenx); tmpszx=sf_floatalloc(lenz); tmpszz=sf_floatalloc(lenz); sf_floatread(Gx[0][0], nxzb*lenx, FGx); sf_floatread(Gz[0][0], nxzb*lenz, FGz); sf_floatread(tmpsxx, lenx, Fsxx); sf_floatread(tmpsxz, lenx, Fsxz); sf_floatread(tmpszx, lenz, Fszx); sf_floatread(tmpszz, lenz, Fszz); for (ix=0; ix<lenx; ix++){ sxx[ix]=(int)tmpsxx[ix]; sxz[ix]=(int)tmpsxz[ix]; } for (iz=0; iz<lenz; iz++){ szx[iz]=(int)tmpszx[iz]; szz[iz]=(int)tmpszz[iz]; } fvel=sf_floatalloc2(nzb, nxb); fden=sf_floatalloc2(nzb, nxb); fc11=sf_floatalloc2(nzb, nxb); bvel=sf_floatalloc2(nzb, nxb); bden=sf_floatalloc2(nzb, nxb); bc11=sf_floatalloc2(nzb, nxb); sf_floatread(fvel[0], nxzb, Ffvel); sf_floatread(fden[0], nxzb, Ffden); sf_floatread(bvel[0], nxzb, Fbvel); sf_floatread(bden[0], nxzb, Fbden); for (ix=0; ix<nxb; ix++){ for (iz=0; iz<nzb; iz++){ fc11[ix][iz]=fden[ix][iz]*fvel[ix][iz]*fvel[ix][iz]; bc11[ix][iz]=bden[ix][iz]*bvel[ix][iz]*bvel[ix][iz]; } } if(wantrecord){ /* check record data */ sf_histint(Frcd, "n1", &tmpint); if(tmpint != nt) sf_error("Not matched dimensions!"); sf_histint(Frcd, "n2", &tmpint); if(tmpint != ng) sf_error("Not matched dimensions!"); sf_histint(Frcd, "n3", &tmpint); if(tmpint != shtnmb) sf_error("Not matched dimensions!"); } if(rank==0){ record=sf_floatalloc3(nt, ng, shtnmb0); if(wantrecord){ sf_floatread(record[0][0], nt*ng*shtnmb, Frcd); for(is=shtnmb; is<shtnmb0; is++) for(ix=0; ix<ng; ix++) for(it=0; it<nt; it++) record[is][ix][it]=0.0; } } img1=sf_floatalloc3(nz, nx, ntau); mig1=sf_floatalloc3(nz, nx, ntau); img2=sf_floatalloc2(nz, nx); mig2=sf_floatalloc2(nz, nx); zero3(img1, nz, nx, ntau); zero2(img2, nz, nx); sf_setn(az, nz); sf_setn(ax, ng); if(!wantrecord){ sf_oaxa(Frcd, at, 1); sf_oaxa(Frcd, ax, 2); sf_putint(Frcd, "n3", shtnmb); sf_putint(Frcd, "d3", shtinv); sf_putint(Frcd, "o3", shtbgn); } sf_setn(ax, nx); if(wantwf){ sf_setn(at, wfnt); sf_setd(at, wfdt); sf_oaxa(Ftmpfwf, az, 1); sf_oaxa(Ftmpfwf, ax, 2); sf_oaxa(Ftmpfwf, at, 3); sf_oaxa(Ftmpbwf, az, 1); sf_oaxa(Ftmpbwf, ax, 2); sf_oaxa(Ftmpbwf, at, 3); } sf_oaxa(Fimg1, az, 1); sf_oaxa(Fimg1, ax, 2); sf_oaxa(Fimg1, atau, 3); sf_oaxa(Fimg2, az, 1); sf_oaxa(Fimg2, ax, 2); fwf=sf_floatalloc3(nz, nx, wfnt); localrec=sf_floatalloc2(nt, ng); if(verb){ sf_warning("=================================="); sf_warning("nx=%d nz=%d nt=%d", nx, nz, nt); sf_warning("wfnt=%d wfdt=%f wfinv=%d dt=%f", wfnt, wfdt, wfinv, dt); sf_warning("nxb=%d nzb=%d pmlsize=%d nfd=%d", nxb, nzb, pmlsize, nfd); sf_warning("ntau=%d dtau=%f tau0=%f", ntau, dtau, tau0); sf_warning("shtnmb=%d shtbgn=%d shtinv=%d", shtnmb, shtbgn, shtinv); sf_warning("lenx=%d lenz=%d spz=%d gp=%d", lenx, lenz, spz, gp); sf_warning("=================================="); } init(); for(iturn=0; iturn*numprocs<shtnmb; iturn++){ is=iturn*numprocs+rank; if(is<shtnmb){ sf_warning("ishot/nshot: %d/%d", is+1, shtnmb); spx=is*shtinv+shtbgn; sglfdfor2(fwf, localrec, verb); } if(wantrecord){ recvbuf=localrec[0]; if(rank==0) sendbuf=record[iturn*numprocs][0]; else sendbuf=NULL; MPI_Scatter(sendbuf, ng*nt, MPI_FLOAT, recvbuf, ng*nt, MPI_FLOAT, 0, Comm); }else{ sendbuf=localrec[0]; if(rank==0) recvbuf=record[iturn*numprocs][0]; else recvbuf=NULL; MPI_Gather(sendbuf, ng*nt, MPI_FLOAT, recvbuf, ng*nt, MPI_FLOAT, 0, Comm); } if(wantwf && rank==0 && iturn==snapturn-1) wantwf=true; else wantwf=false; if(wantwf) sf_floatwrite(fwf[0][0], wfnt*nx*nz, Ftmpfwf); if(!onlyrecord && is<shtnmb){ sglfdback2(mig1, mig2, fwf, localrec, verb, wantwf, Ftmpbwf); for(itau=0; itau<ntau; itau++){ for(ix=0; ix<nx; ix++){ for(iz=0; iz<nz; iz++){ img1[itau][ix][iz]+=mig1[itau][ix][iz]; } } } for(ix=0; ix<nx; ix++){ for(iz=0; iz<nz; iz++){ img2[ix][iz]+=mig2[ix][iz]; } } } MPI_Barrier(Comm); } //end of iturn if(!onlyrecord){ if(rank==0){ sendbuf=(float *)MPI_IN_PLACE; recvbuf=img1[0][0]; }else{ sendbuf=img1[0][0]; recvbuf=NULL; } MPI_Reduce(sendbuf, recvbuf, ntau*nx*nz, MPI_FLOAT, MPI_SUM, 0, Comm); if(rank==0){ sendbuf=MPI_IN_PLACE; recvbuf=img2[0]; }else{ sendbuf=img2[0]; recvbuf=NULL; } MPI_Reduce(sendbuf, recvbuf, nx*nz, MPI_FLOAT, MPI_SUM, 0, Comm); } if(rank==0){ if(!wantrecord){ sf_floatwrite(record[0][0], shtnmb*ng*nt, Frcd); } sf_floatwrite(img1[0][0], ntau*nx*nz, Fimg1); sf_floatwrite(img2[0], nx*nz, Fimg2); } tend=clock(); duration=(double)(tend-tstart)/CLOCKS_PER_SEC; sf_warning(">>The CPU time of sfmpilfdrtm2 is: %f seconds<<", duration); MPI_Finalize(); exit(0); }
int main(int argc, char* argv[]) { int n[SF_MAX_DIM], a[SF_MAX_DIM], center[SF_MAX_DIM], gap[SF_MAX_DIM]; int *pch, *nh, dim, n123, nf, i, niter, nbf, nbp, id, ip, ig, np; int *kk, *pp; float *dd, eps, dabs, di; nfilter aa, bb; char varname[6], *lagfile; sf_file in, flt, lag, mask, patch, reg; sf_init(argc,argv); in = sf_input("in"); flt = sf_output("out"); dim = sf_filedims(in,n); if (NULL == (lagfile = sf_getstring("lag"))) sf_error("Need lag="); /* output file for filter lags */ lag = sf_output(lagfile); sf_settype(lag,SF_INT); sf_putstring(flt,"lag",lagfile); sf_putints(lag,"n",n,dim); if (!sf_getints("a",a,dim)) sf_error("Need a="); if (!sf_getints("center",center,dim)) { for (i=0; i < dim; i++) { center[i] = (i+1 < dim && a[i+1] > 1)? a[i]/2: 0; } } if (!sf_getints("gap",gap,dim)) { for (i=0; i < dim; i++) { gap[i] = 0; } } n123 = 1; for (i=0; i < dim; i++) { n123 *= n[i]; } dd = sf_floatalloc(n123); kk = sf_intalloc(n123); if (NULL != sf_getstring("maskin")) { /* optional input mask file */ mask = sf_input("maskin"); switch (sf_gettype(mask)) { case SF_INT: sf_intread (kk,n123,mask); break; case SF_FLOAT: sf_floatread (dd,n123,mask); for (i=0; i < n123; i++) { kk[i] = (dd[i] != 0.); } break; default: sf_error ("Wrong data type in maskin"); break; } sf_fileclose (mask); } else { for (i=0; i < n123; i++) { kk[i] = 1; } } sf_floatread(dd,n123,in); dabs = fabsf(dd[0]); for (i=1; i < n123; i++) { di = fabsf(dd[i]); if (di > dabs) dabs=di; } random_init(2004); for (i=0; i < n123; i++) { dd[i] = dd[i]/dabs+ 100.*FLT_EPSILON*(random0()-0.5);; } pp = sf_intalloc(n123); if (NULL != sf_getstring("pch")) { patch = sf_input("pch"); if (SF_INT != sf_gettype(patch)) sf_error("Need int pch"); sf_intread(pp,n123,patch); np = pp[0]; for (i=1; i < n123; i++) { if (pp[i] > np) np = pp[i]; } sf_fileclose(patch); } else { np = n123; for (i=0; i < n123; i++) { pp[i] = i; } } aa = createnhelix(dim, n, center, gap, a, pp); free (pp); nf = aa->hlx[0]->nh; nfind_mask(n123, kk, aa); if(!sf_getint("niter",&niter)) niter=100; /* number of iterations */ if (!sf_getfloat("epsilon",&eps)) eps=0.01; /* regularization parameter */ sf_putint(flt,"n1",nf); sf_putint(flt,"n2",np); sf_putint(lag,"n1",nf); sf_putint(lag,"n2",np); for (i=2; i < dim; i++) { sprintf(varname,"n%d",i+1); sf_putint(flt,varname,1); sf_putint(lag,varname,1); } for (ip=0; ip < np; ip++) { sf_intwrite(aa->hlx[ip]->lag,nf,lag); } sf_fileclose(lag); if (NULL != sf_getstring("maskout")) { /* optional output mask file */ mask = sf_output("maskout"); for (i=0; i < n123; i++) { kk[i] = aa->mis[i]? 0.: 1.; } sf_settype(mask,SF_INT); sf_intwrite (kk,n123,mask); } reg = sf_input("filt"); if (!sf_histint(reg,"n1",&nbf)) sf_error("No n1= in filt"); if (!sf_histint(reg,"n2",&nbp)) sf_error("No n2= in filt"); if (NULL != sf_getstring("filt_pch")) { patch = sf_input("filt_pch"); if (SF_INT != sf_gettype(patch)) sf_error("Need int filt_pch"); pp = sf_intalloc(np); sf_intread(pp,np,patch); } else { if (nbp != np) sf_error ("Wrong filter size: %d != %d",nbp,np); pp = NULL; } pch = sf_intalloc(nf*np); nh = sf_intalloc(nbp); for (i=0; i < nbp; i++) { nh[i] = nbf; } for (id=ig=0; ig < nf; ig++) { for (ip=0; ip < np; ip++, id++) { pch[id] = (NULL != pp)? pp[ip]: ip; } } bb = nallocate (nbp, nf*np, nh, pch); if (NULL == (lagfile = sf_getstring("filt_lag")) && NULL == (lagfile = sf_histstring(reg,"lag"))) sf_error("Need filt_lag="); /* input file for double-helix filter lags */ lag = sf_input(lagfile); if (SF_INT != sf_gettype(lag)) sf_error("Need int filt_lag"); for (ip=0; ip < nbp; ip++) { sf_intread (kk,nbf,lag); for (i=0; i < nbf; i++) { bb->hlx[ip]->lag[i] = kk[i]*nf; } } for (ip=0; ip < nbp; ip++) { sf_floatread (bb->hlx[ip]->flt,nbf,reg); } nfind_pef (n123, dd, aa, bb, niter, eps, nf); for (ip=0; ip < np; ip++) { sf_floatwrite (aa->hlx[ip]->flt,nf,flt); } exit(0); }
int main (int argc,char* argv[]) { int b1, b2, b3, n1, n2, n3, i, nshot, ndim, is,order,n123, *p; float br1, br2, br3, o1, o2, o3, d1, d2, d3, slow; float **s, *t, *v; char *sfile; bool isvel, sweep, plane[3]; sf_file vel, time, shots; sf_init (argc, argv); vel = sf_input("in"); time = sf_output("out"); if (SF_FLOAT != sf_gettype(vel)) sf_error("Need float input"); if(!sf_histint(vel,"n1",&n1)) sf_error("No n1= in input"); if(!sf_histint(vel,"n2",&n2)) sf_error("No n2= in input"); if(!sf_histint(vel,"n3",&n3)) n3=1; if(!sf_histfloat(vel,"d1",&d1)) sf_error("No d1= in input"); if(!sf_histfloat(vel,"d2",&d2)) sf_error("No d2= in input"); if(!sf_histfloat(vel,"d3",&d3)) d3=d2; if(!sf_histfloat(vel,"o1",&o1)) o1=0.; if(!sf_histfloat(vel,"o2",&o2)) o2=0.; if(!sf_histfloat(vel,"o3",&o3)) o3=0.; if(!sf_getbool("vel",&isvel)) isvel=true; /* if y, the input is velocity; n, slowness squared */ if(!sf_getint("order",&order)) order=2; /* [1,2] Accuracy order */ if (!sf_getbool("sweep",&sweep)) sweep=false; /* if y, use fast sweeping instead of fast marching */ if(!sf_getfloat("br1",&br1)) br1=d1; if(!sf_getfloat("br2",&br2)) br2=d2; if(!sf_getfloat("br3",&br3)) br3=d3; /* Constant-velocity box around the source (in physical dimensions) */ if(!sf_getbool("plane1",&plane[2])) plane[2]=false; if(!sf_getbool("plane2",&plane[1])) plane[1]=false; if(!sf_getbool("plane3",&plane[0])) plane[0]=false; /* plane-wave source */ if(!sf_getint("b1",&b1)) b1= plane[2]? n1: (int) (br1/d1+0.5); if(!sf_getint("b2",&b2)) b2= plane[1]? n2: (int) (br2/d2+0.5); if(!sf_getint("b3",&b3)) b3= plane[0]? n3: (int) (br3/d3+0.5); /* Constant-velocity box around the source (in samples) */ if( b1<1 ) b1=1; if( b2<1 ) b2=1; if( b3<1 ) b3=1; sfile = sf_getstring("shotfile"); /* File with shot locations (n2=number of shots, n1=3) */ if(NULL != sfile) { shots = sf_input("shotfile"); if (SF_FLOAT != sf_gettype(shots)) sf_error("Need float shotfile"); if(!sf_histint(shots,"n2",&nshot)) sf_error("No n2= in shotfile"); if(!sf_histint(shots,"n1",&ndim) || ndim != 3) sf_error("Need n1=3 in shotfile"); s = sf_floatalloc2 (ndim,nshot); sf_floatread(s[0],nshot*ndim,shots); sf_fileclose(shots); sf_putint (time,"n4",nshot); free (sfile); } else { nshot = 1; ndim = 3; s = sf_floatalloc2 (ndim,nshot); if(!sf_getfloat("zshot",&s[0][0]) ) s[0][0]=0.; /* Shot location (used if no shotfile) */ if(!sf_getfloat("yshot",&s[0][1])) s[0][1]=o2 + 0.5*(n2-1)*d2; if(!sf_getfloat("xshot",&s[0][2])) s[0][2]=o3 + 0.5*(n3-1)*d3; sf_warning("Shooting from zshot=%g yshot=%g xshot=%g", s[0][0],s[0][1],s[0][2]); } n123 = n1*n2*n3; t = sf_floatalloc (n123); v = sf_floatalloc (n123); p = sf_intalloc (n123); sf_floatread(v,n123,vel); if (isvel) { /* transform velocity to slowness squared */ for(i = 0; i < n123; i++) { slow = v[i]; v[i] = 1./(slow*slow); } } if (!sweep) fastmarch_init (n3,n2,n1); /* loop over shots */ for( is = 0; is < nshot; is++) { sf_warning("shot %d of %d;",is+1,nshot); if (sweep) { continue; } else { fastmarch(t,v,p, plane, n3,n2,n1, o3,o2,o1, d3,d2,d1, s[is][2],s[is][1],s[is][0], b3,b2,b1, order); } sf_floatwrite (t,n123,time); } sf_warning("."); exit (0); }