Пример #1
0
void mis2(int niter         /* number of iterations */, 
	  int nx            /* model size */, 
	  float *xx         /* model */, 
	  sf_filter aa      /* helix filter */, 
	  const bool *known /* mask for known data */,
	  float eps         /* regularization parameter */,
	  bool doprec       /* to apply preconditioning */) 
/*< interpolate >*/
{
    int ix;
    float *dd;

    if (doprec) {                          /*  preconditioned */
	sf_mask_init(known);
	sf_polydiv_init(nx, aa);
	sf_solver_prec(sf_mask_lop, sf_cgstep, sf_polydiv_lop, 
		       nx, nx, nx, xx, xx, niter, eps, "end");
	sf_polydiv_close();
    } else {                               /*  regularized */
	dd = sf_floatalloc(nx);
	for (ix=0; ix < nx; ix++) {
	    dd[ix]=0.;
	}

	sf_helicon_init(aa);
	sf_solver (sf_helicon_lop, sf_cgstep, nx, nx, xx, dd, niter, 
		   "known", known, "x0", xx, "end");
	free(dd);
    }
    sf_cgstep_close();
}
Пример #2
0
void smoothpwd(int niter     /* number of iterations */, 
	       int ncycle    /* number of cycles */, 
	       float* weight /* data weighting */, 
	       float* data   /* input data */, 
	       float* der    /* output derivative */,
	       bool verb     /* verbosity flag */,
	       float eps     /* regularization parameter */) 
/*< find the derivative >*/
{
    int i, iter;

    for (i=0; i < n; i++) {
	w[i] = 1.;
    }
    
    for (iter=0; iter < ncycle; iter++) {
	if (NULL != weight) {
	    sf_solver_prec (sf_repeat_lop,sf_cgstep,predict_smooth_lop,
			    n,n,n,der,data,niter,eps,
			    "wt",weight,"verb",verb,"mwt",w,"xp",p,"end");
	} else {
	    sf_solver_prec (sf_repeat_lop,sf_cgstep,predict_smooth_lop,
			    n,n,n,der,data,niter,eps,
			    "verb",verb,"mwt",w,"xp",p,"end");
	}
	sf_cgstep_close();
	
	for (i=0; i < n; i++) {
	    w[i] = p[i]; /* "Cauchy" weight + positivity */
	}	    
    }
}
Пример #3
0
void nmis(int niter         /* number of iterations */, 
	  int nf1, int nf2, 
	  int nf3, int nf4,
	  float *filt,
	  float *xx         /* model */, 
	  const bool *known /* mask for known data */,
	  float eps         /* regularization parameter */,
	  bool verb         /* verbosity flag */) 
/*< interpolate >*/
{
    int ix;
    float *dd;

    /*  regularized */
    dd = sf_floatalloc(nf3*nf4);
    for (ix=0; ix < nf3*nf4; ix++) {
	dd[ix]=0.;
    }
    
    mmmult_init(filt, nf1, nf2, nf3, nf4);
    sf_solver (mmmult_lop, sf_cgstep, nf3*nf4, nf3*nf4, xx, dd, niter, 
	       "known", known, "x0", xx, "verb", verb, "end");
    free(dd);
    sf_cgstep_close();

}
Пример #4
0
void signoi2_lop (bool adj, bool add, int n1, int n2, 
		 float *data, float *sign)
/*< alternative linear operator >*/
{
    helicon2_init (nd, nn);
    sf_helicon_init (ss); 

    sf_adjnull(adj,add,n1,n2,data,sign);

    helicon2_lop (false, false, n1, n1, data, dd);
    sf_solver_reg(helicon2_lop, sf_cgstep, sf_helicon_lop, 
		  nd, nd, nd, sign, dd, niter, eps, 
		  "verb", verb, "end");
    sf_cgstep_close();

    nn++;
    ss++;
}
Пример #5
0
void signoi_lop (bool adj, bool add, int n1, int n2, 
		 float *data, float *sign)
/*< linear operator >*/
{
    sf_helicon_init (nn);
    sf_polydiv_init (nd, ss); 

    sf_adjnull(adj,add,n1,n2,data,sign);

    sf_helicon_lop (false, false, n1, n1, data, dd);
    sf_solver_prec(sf_helicon_lop, sf_cgstep, sf_polydiv_lop, 
		   nd, nd, nd, sign, dd, niter, eps, 
		   "verb", verb, "end");
    sf_cgstep_close();

    nn++;
    ss++;
}
Пример #6
0
void levint2 (bool cdstep                /* if use conj. directions */,
	      int niter                  /* number of iterations */, 
	      int warmup                 /* initial iterations */,
	      int nd                     /* data size */,
	      float *x, float *y         /* data coordinates */, 
	      float *dd                  /* data */, 
	      float o1, float d1, int n1 /* inline */, 
	      float o2, float d2, int n2 /* crossline */, 
	      filter aa                  /* PEF */, 
	      float *rr                  /* residual */, 
	      float eps                  /* regularization parameter */) 
/*< 2-D inverse interpolation with PEF estimation >*/
{
    int nm, nr;

    nm = n1*n2;
    nr = nm+aa->nh;

    lint2_init (n1,o1,d1, n2,o2,d2, x, y);
    pefhel_init (aa, nm, rr);

    if (cdstep) {
	sf_cdstep_init();
	sf_solver_reg (lint2_lop, sf_cdstep, helicon_lop, nm, nm, nd, rr, dd, 
		       warmup, eps, "x0", rr, "nmem", warmup, "end");
	sf_cdstep_close();
	/*
	  sf_solver_reg (lint2_lop, sf_cdstep, pefhel_lop, nm, nr, nd, rr, dd, 
	  niter, eps, "x0", rr, "nlreg", helicon_lop, "nmem", 3, "end");
	  sf_cdstep_close();
	*/
    } else {
	sf_solver_reg (lint2_lop, sf_cgstep, helicon_lop, nm, nm, nd, rr, dd, 
		       warmup, eps, "x0", rr, "end");
	sf_cgstep_close();
	/*
	  sf_solver_reg (lint2_lop, sf_cgstep, pefhel_lop, nm, nr, nd, rr, dd, 
	  niter, eps, "x0", rr, "nlreg", helicon_lop,"end");
	  sf_cgstep_close();
	*/
    }
}
Пример #7
0
int main(int argc, char* argv[])
{
    int i, n1, n2, n12, n3, nk, n12k, niter, nliter, iter, i3, order;
    float eps, *d, *s, ***pp, *w=NULL, *p=NULL;
    bool verb;
    sf_file in, out, dips, weight=NULL;

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

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

    if (!sf_histint(dips,"n1",&i) || i != n1) sf_error("Wrong n1= in dips");
    if (!sf_histint(dips,"n2",&i) || i != n2) sf_error("Wrong n2= in dips");
    if (sf_histint(dips,"n4",&i) && i != n3) sf_error("Wrong n4= in dips");

    if (!sf_histint(dips,"n3",&nk)) nk=1;
    sf_putint (out,"n3",nk);
    sf_putint (out,"n4",n3);
    
    n12 = n1*n2;
    n12k = n12*nk;
    
    if (!sf_getint ("niter",&niter)) niter=50;
    /* maximum number of iterations */

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

    if (!sf_getfloat ("eps",&eps)) eps=0.;
    /* regularization parameter */

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

    if (!sf_getint("order",&order)) order=1;
    /* accuracy order */

    s = sf_floatalloc(n12k);
    d = sf_floatalloc(n12);
    pp = sf_floatalloc3(n1,n2,nk);

    if (nliter > 1) {
	w = sf_floatalloc(n12k);
	p = sf_floatalloc(n12k);

	if (NULL != sf_getstring("weight")) {
	    weight = sf_output("weight"); 
	    sf_putint(weight,"n3",nk);
	    sf_putint(weight,"n4",n3);
	}
    }

    predk_init(nk,n1,n2,0.0001,order,pp);
    copyk_init(nk,n12);

    for (i3=0; i3 < n3; i3++) {
	if (verb) sf_warning("slice %d of %d",i3+1,n3);

	sf_floatread (d,n12,in);
	sf_floatread (pp[0][0],n12k,dips);
	
	if (1 == nliter) {
	    sf_solver_prec (copyk_lop,sf_cgstep,predk_lop,
			    n12k,n12k,n12,s,d,niter,eps,"verb",verb,"end");
	    sf_cgstep_close();
	} else {
	    for (i=0; i < n12k; i++) {
		w[i] = 1.;
	    }
	    for (iter=0; iter < nliter; iter++) {
		sf_solver_prec (copyk_lop,sf_cgstep,predk_lop,
				n12k,n12k,n12,s,d,niter,eps,
				"verb",verb,"mwt",w,"xp",p,"end");
		sf_cgstep_close();
		
		if (iter < nliter-1) {
		    for (i=0; i < n12k; i++) {
			w[i] = fabsf(p[i]); /* "Cauchy" weight */
		    }	    
		} else {
		    for (i=0; i < n12k; i++) {
			w[i] *= p[i];
		    }
		}
	    }
	    
	    if (NULL != weight) sf_floatwrite(w,n12k,weight);
	}
	
	sf_floatwrite(s,n12k,out);
    }

    exit(0);
}
Пример #8
0
    int main(int argc, char* argv[])
{
    int i, n1, n2, n12, nj1, nj2, niter, nw, n3, i3;
    float eps, *d, *s, *nd, **nn, **ss;
    bool verb;
    sf_file in, out, ndip, sdip;

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

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

    n3 = sf_leftsize(in,2);
    if (1==n3) sf_putint (out,"n3",2);

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

    if (!sf_getfloat ("eps",&eps)) eps=1.;
    /* regularization parameter */

    if (!sf_getint("order",&nw)) nw=1;
    /* [1,2,3] accuracy order */
    if (nw < 1 || nw > 3) 
	sf_error ("Unsupported nw=%d, choose between 1 and 3",nw);
    if (!sf_getint("nj1",&nj1)) nj1=1;
    /* antialiasing for noise dip */
    if (!sf_getint("nj2",&nj2)) nj2=1;
    /* antialiasing for signal dip */

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

    nd = sf_floatalloc(2*n12);
    s = sf_floatalloc(n12);
    d = sf_floatalloc(n12);
    nn = sf_floatalloc2(n1,n2);
    ss = sf_floatalloc2(n1,n2);

    for (i=0; i < n12; i++) {
	nd[n12+i] = 0.;
    }

    planesignoi_init (nw, nj1,nj2, n1,n2, nn, ss, eps);

    for (i3=0; i3 < n3; i3++) {
	sf_floatread (d,n12,in);
	sf_floatread (nn[0],n12,ndip);
	sf_floatread (ss[0],n12,sdip);

	allpass22_init(allpass2_init(nw,nj1,n1,n2,nn));
	allpass21_lop (false,false,n12,n12,d,nd);
	

	sf_solver (planesignoi_lop, sf_cgstep, n12, n12*2, s, nd, niter, 
		   "verb", verb, "end");

	sf_cgstep_close();

	sf_floatwrite(s,n12,out);

	if (1==n3) {
	    for (i=0; i < n12; i++) {
		d[i] -= s[i];
	    }
	    sf_floatwrite(d,n12,out);
	}
    }

    exit(0);
}
Пример #9
0
int main(int argc, char* argv[])
{
    bool velocity, verb, shape;
    int dim, i, j, n[3], rect[3], it, nt, order, nt0, nx0;
    int iter, niter, iline, nline, cgiter, *f0, *m0=NULL;
    float d[3], o[3], dt0, dx0, ot0, ox0, eps, tol, *p=NULL, *p0=NULL, thres;
    float *vd, *vdt, *vdx, *s, *t0, *x0, *ds, *rhs, *rhs0, *rhs1=NULL, error0, error1, error, scale;
    char key[6];
    sf_file in, out, dix, t_0=NULL, x_0=NULL, f_0=NULL, grad=NULL, cost=NULL, mini=NULL, prec=NULL;

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

    /* read input dimension */
    dim = sf_filedims(in,n);

    nt = 1;
    for (i=0; i < dim; i++) {
	sprintf(key,"d%d",i+1);
	if (!sf_histfloat(in,key,d+i)) sf_error("No %s= in input",key);
	sprintf(key,"o%d",i+1);
	if (!sf_histfloat(in,key,o+i)) o[i]=0.;
	nt *= n[i];
    }
    if (dim < 3) {
	n[2] = 1; d[2] = d[1]; o[2] = o[1];
    }

    /* read initial guess */
    s = sf_floatalloc(nt);
    sf_floatread(s,nt,in);

    if (!sf_getbool("velocity",&velocity)) velocity=true;
    /* y, input is velocity / n, slowness-squared */

    if (velocity) {
	for (it=0; it < nt; it++) {
	    s[it] = 1./s[it]*1./s[it];
	}
    }

    /* read Dix velocity */
    if (NULL == sf_getstring("dix")) sf_error("No Dix input dix=");
    dix = sf_input("dix");

    if(!sf_histint(dix,"n1",&nt0)) sf_error("No n1= in dix");
    if(!sf_histint(dix,"n2",&nx0)) sf_error("No n2= in dix");
    if(!sf_histfloat(dix,"d1",&dt0)) sf_error("No d1= in dix");
    if(!sf_histfloat(dix,"d2",&dx0)) sf_error("No d2= in dix");
    if(!sf_histfloat(dix,"o1",&ot0)) sf_error("No o1= in dix");
    if(!sf_histfloat(dix,"o2",&ox0)) sf_error("No o2= in dix");

    vd = sf_floatalloc(nt0*nx0);
    sf_floatread(vd,nt0*nx0,dix);
    sf_fileclose(dix);

    /* Dix velocity derivative in t0 (2nd order FD) */
    vdt = sf_floatalloc(nt0*nx0);
    for (i=0; i < nt0; i++) {
	for (j=0; j < nx0; j++) {
	    if (i == 0)
		vdt[j*nt0+i] = (-vd[j*nt0+i+2]+4.*vd[j*nt0+i+1]-3.*vd[j*nt0+i])/(2.*dt0);
	    else if (i == nt0-1)
		vdt[j*nt0+i] = (3.*vd[j*nt0+i]-4.*vd[j*nt0+i-1]+vd[j*nt0+i-2])/(2.*dt0);
	    else
		vdt[j*nt0+i] = (vd[j*nt0+i+1]-vd[j*nt0+i-1])/(2.*dt0);
	}
    }

    /* Dix velocity derivative in x0 (2nd order FD) */
    vdx = sf_floatalloc(nt0*nx0);
    for (j=0; j < nx0; j++) {
	for (i=0; i < nt0; i++) {
	    if (j == 0)
		vdx[j*nt0+i] = (-vd[(j+2)*nt0+i]+4.*vd[(j+1)*nt0+i]-3.*vd[j*nt0+i])/(2.*dx0);
	    else if (j == nx0-1)
		vdx[j*nt0+i] = (3.*vd[j*nt0+i]-4.*vd[(j-1)*nt0+i]+vd[(j-2)*nt0+i])/(2.*dx0);
	    else
		vdx[j*nt0+i] = (vd[(j+1)*nt0+i]-vd[(j-1)*nt0+i])/(2.*dx0);
	}
    }

    if (!sf_getint("order",&order)) order=1;
    /* fastmarch accuracy order */

    if (!sf_getfloat("thres",&thres)) thres=10.;
    /* thresholding for caustics */

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

    if (!sf_getint("cgiter",&cgiter)) cgiter=200;
    /* number of CG iterations */

    if (!sf_getbool("shape",&shape)) shape=false;
    /* regularization (default Tikhnov) */

    if (!sf_getfloat("eps",&eps)) eps=0.1;
    /* regularization parameter */

    if (!sf_getint("nline",&nline)) nline=0;
    /* maximum number of line search (default turned-off) */

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

    if (shape) {
	if (!sf_getfloat("tol",&tol)) tol=1.e-6;
	/* tolerance for shaping regularization */

	for (i=0; i < dim; i++) {
	    sprintf(key,"rect%d",i+1);
	    if (!sf_getint(key,rect+i)) rect[i]=1;
	    /*( rect#=(1,1,...) smoothing radius on #-th axis )*/
	}
	
	/* triangle smoothing operator */
	sf_trianglen_init(dim,rect,n);
	sf_repeat_init(nt,1,sf_trianglen_lop);
	
	sf_conjgrad_init(nt,nt,nt,nt,eps,tol,verb,false);
	p = sf_floatalloc(nt);
    } else {
	/* initialize 2D gradient operator */
	sf_igrad2_init(n[0],n[1]);
    }
    
    /* allocate memory for fastmarch */
    t0 = sf_floatalloc(nt);
    x0 = sf_floatalloc(nt);
    f0 = sf_intalloc(nt);

    /* allocate memory for update */
    ds  = sf_floatalloc(nt);
    rhs = sf_floatalloc(nt);

    /* output transformation matrix */
    if (NULL != sf_getstring("t0")) {
	t_0 = sf_output("t0");
	sf_putint(t_0,"n3",niter+1);
    }
    if (NULL != sf_getstring("x0")) {
	x_0 = sf_output("x0");
	sf_putint(x_0,"n3",niter+1);
    }

    /* output auxiliary label */
    if (NULL != sf_getstring("f0")) {
	f_0 = sf_output("f0");
	sf_settype(f_0,SF_INT);
	sf_putint(f_0,"n3",niter+1);
    }

    /* output gradient */
    if (NULL != sf_getstring("grad")) {
	grad = sf_output("grad");
	sf_putint(grad,"n3",niter);
    }

    /* output cost */
    if (NULL != sf_getstring("cost")) {
	cost = sf_output("cost");
	sf_putint(cost,"n3",niter+1);
    }

    /* read mask (desired minimum) */
    m0 = sf_intalloc(nt);

    if (NULL != sf_getstring("mask")) {
	mini = sf_input("mask");
	sf_intread(m0,nt,mini);
	sf_fileclose(mini);
    } else {
	for (it=0; it < nt; it++) m0[it] = -1;
    }

    /* read cost (desired minimum) */
    rhs0 = sf_floatalloc(nt);

    if (NULL != sf_getstring("mval")) {
	mini = sf_input("mval");
	sf_floatread(rhs0,nt,mini);
	sf_fileclose(mini);
    } else {
	for (it=0; it < nt; it++) rhs0[it] = 0.;
    }

    /* read preconditioner */
    if (NULL != sf_getstring("prec")) {
	prec = sf_input("prec");
	p0 = sf_floatalloc(nt);
	sf_floatread(p0,nt,prec);
	sf_fileclose(prec);

	rhs1 = sf_floatalloc(nt);
    }

    /* fastmarch initialization */
    fastmarch_init(n,o,d,order);

    /* update initialization */
    t2d_init(dim,n,d,nt0,dt0,ot0,nx0,dx0,ox0);

    /* fastmarch */
    fastmarch(t0,x0,f0,s);

    /* caustic region (2D) */
    t2d_caustic(x0,f0,n,d,thres);

    /* set up operator */
    t2d_set(t0,x0,f0,s,vd,vdt,vdx,m0,p0);

    /* evaluate cost */
    t2d_cost(rhs);

    for (it=0; it < nt; it++) {
	if (f0[it] >= 0 || m0[it] >= 0)
	    rhs[it] = 0.;
	else
	    rhs[it] -= rhs0[it];
    }

    if (p0 == NULL) {
	error0 = error1 = cblas_snrm2(nt,rhs,1);
    } else {
	for (it=0; it < nt; it++) rhs1[it] = p0[it]*rhs[it];
	error0 = error1 = cblas_snrm2(nt,rhs1,1);
    }

    /* write optional outputs */    
    if (NULL!=t_0)  sf_floatwrite(t0,nt,t_0);
    if (NULL!=x_0)  sf_floatwrite(x0,nt,x_0);
    if (NULL!=f_0)  sf_intwrite(f0,nt,f_0);
    if (NULL!=cost) sf_floatwrite(rhs,nt,cost);

    sf_warning("Start conversion, cost %g",1.);

    /* nonlinear loop */
    for (iter=0; iter < niter; iter++) {
	
	/* solve ds */
	if (shape) {
	    if (p0 == NULL)
		sf_conjgrad(NULL,t2d_oper,sf_repeat_lop,p,ds,rhs,cgiter);
	    else
		sf_conjgrad(t2d_prec,t2d_oper,sf_repeat_lop,p,ds,rhs,cgiter);
	} else {
	    sf_solver_reg(t2d_oper,sf_cgstep,sf_igrad2_lop,2*nt,nt,nt,ds,rhs,cgiter,eps,"verb",verb,"end");
	    sf_cgstep_close();
	}

	/* add ds */
	for (it=0; it < nt; it++) {
	    s[it] = s[it]+ds[it]+0.25*ds[it]*ds[it]/s[it];
	}

	/* fastmarch */
	fastmarch(t0,x0,f0,s);

	/* caustic region (2D) */
	t2d_caustic(x0,f0,n,d,thres);

	/* set up operator */
	t2d_set(t0,x0,f0,s,vd,vdt,vdx,m0,p0);

	/* evaluate cost */
	t2d_cost(rhs);

	for (it=0; it < nt; it++) {
	    if (f0[it] >= 0 || m0[it] >= 0)
		rhs[it] = 0.;
	    else
		rhs[it] -= rhs0[it];
	}
	
	if (p0 == NULL) {
	    error = cblas_snrm2(nt,rhs,1);
	} else {
	    for (it=0; it < nt; it++) rhs1[it] = p0[it]*rhs[it];
	    error = cblas_snrm2(nt,rhs1,1);
	}

	error = cblas_snrm2(nt,rhs,1);

	/* line search */
	if (nline > 0 && error >= error1) {

	    scale = 0.5;
	    for (iline=0; iline < nline; iline++) {

		for (it=0; it < nt; it++) {
		    s[it] = s[it]+(scale*ds[it])+0.25*(scale*ds[it])*(scale*ds[it])/s[it];
		}
		
		fastmarch(t0,x0,f0,s);
		t2d_caustic(x0,f0,n,d,thres);

		t2d_set(t0,x0,f0,s,vd,vdt,vdx,m0,p0);
		t2d_cost(rhs);

		for (it=0; it < nt; it++) {
		    if (f0[it] >= 0 || m0[it] >= 0)
			rhs[it] = 0.;
		    else
			rhs[it] -= rhs0[it];
		}
		
		if (p0 == NULL) {
		    error = cblas_snrm2(nt,rhs,1);
		} else {
		    for (it=0; it < nt; it++) rhs1[it] = p0[it]*rhs[it];
		    error = cblas_snrm2(nt,rhs1,1);
		}
		
		error = cblas_snrm2(nt,rhs,1);
		if (error < error1) {
		    sf_warning("Exist line search %d of %d",iline+1,nline);
		} else {
		    scale *= 0.5;
		}
	    }
	}

	error1 = error;

	/* write optional outputs */
	if (NULL!=t_0)  sf_floatwrite(t0,nt,t_0);
	if (NULL!=x_0)  sf_floatwrite(x0,nt,x_0);
	if (NULL!=f_0)  sf_intwrite(f0,nt,f_0);
	if (NULL!=cost) sf_floatwrite(rhs,nt,cost);
	if (NULL!=grad) sf_floatwrite(ds,nt,grad);

	sf_warning("Cost after iteration %d: %g",iter+1,error/error0);
    }

    /* write output */
    if (velocity) {
	for (it=0; it < nt; it++) {
	    s[it] = 1./sqrtf(s[it]);
	}
    }

    sf_floatwrite(s,nt,out);

    exit(0);
}
Пример #10
0
int main(int argc, char* argv[])
{
    bool adj, velocity, l1norm, plane[3], verb;
    int dim, i, count, n[SF_MAX_DIM], it, nt, **m, nrhs, is, nshot=1, *flag, order, iter, niter, stiter, *k, nfreq, nmem;
    float o[SF_MAX_DIM], d[SF_MAX_DIM], **t, *t0, *s, *temps, **source, *rhs, *ds;
    float rhsnorm, rhsnorm0, rhsnorm1, rate, eps, gama;
    char key[4], *what;
    sf_file sinp, sout, shot, time, reco, rece, topo, grad, norm;
    sf_weight weight=NULL;

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

    if (NULL == (what = sf_getstring("what"))) what="tomo";
    /* what to compute (default tomography) */

    switch (what[0]) {
	case 'l': /* linear operator */

	    if (NULL == sf_getstring("time"))
		sf_error("Need time=");
	    time = sf_input("time");

	    /* read operator dimension from time table */
	    dim = sf_filedims(time,n);
	    
	    nt = 1;
	    for (i=0; i < 3; i++) {
		sprintf(key,"d%d",i+1);
		if (!sf_histfloat(time,key,d+i)) sf_error("No %s= in input",key);
		sprintf(key,"o%d",i+1);
		if (!sf_histfloat(time,key,o+i)) o[i]=0.;
		nt *= n[i]; plane[i] = false;
	    }
	    if (dim < 3) {
		n[2] = 1; o[2] = o[1]; d[2] = d[1]; plane[2] = false;
	    }

	    dim = 2;

	    /* read in shot file */
	    if (NULL == sf_getstring("shot"))
		sf_error("Need source shot=");
	    shot = sf_input("shot");
	    
	    if (!sf_histint(shot,"n2",&nshot)) nshot=1;
	    sf_fileclose(shot);

	    /* read in receiver file */
	    m = sf_intalloc2(nt,nshot);
	    if (NULL == sf_getstring("receiver")) {
		for (is=0; is < nshot; is++) {
		    for (it=0; it < nt; it++) {
			m[is][it] = 1;
		    }
		}
	    } else {
		rece = sf_input("receiver");
		sf_intread(m[0],nt*nshot,rece);
		sf_fileclose(rece);
	    }

	    /* number of right-hand side */
	    nrhs = 0;
	    for (is=0; is < nshot; is++) {
		for (it=0; it < nt; it++) {
		    if (m[is][it] == 1) nrhs++;
		}
	    }
	    rhs = sf_floatalloc(nrhs);

	    t = sf_floatalloc2(nt,nshot);
	    sf_floatread(t[0],nt*nshot,time);

	    if (!sf_getbool("adj",&adj)) adj=false;
	    /* adjoint flag (for what=linear) */

	    /* initialize fatomo */
	    fatomo_init(dim,n,d,nshot);

	    /* set operators */
	    fatomo_set(t,m);

	    t0 = sf_floatalloc(nt);

	    if (adj) {
		sf_floatread(rhs,nrhs,sinp);

		fatomo_lop(true,false,nt,nrhs,t0,rhs);

		sf_putint(sout,"n1",nt);
		sf_putint(sout,"n2",1);
		sf_putint(sout,"n3",1);
		sf_floatwrite(t0,nt,sout);
	    } else {
		sf_floatread(t0,nt,sinp);

		fatomo_lop(false,false,nt,nrhs,t0,rhs);
		
		sf_putint(sout,"n1",nrhs);
		sf_putint(sout,"n2",1);
		sf_putint(sout,"n3",1);
		sf_floatwrite(rhs,nrhs,sout);
	    }

	    break;
	    
	case 't': /* tomography */

	    /* read input dimension */
	    dim = sf_filedims(sinp,n);
	    
	    nt = 1;
	    for (i=0; i < dim; i++) {
		sprintf(key,"d%d",i+1);
		if (!sf_histfloat(sinp,key,d+i)) sf_error("No %s= in input",key);
		sprintf(key,"o%d",i+1);
		if (!sf_histfloat(sinp,key,o+i)) o[i]=0.;
		nt *= n[i]; plane[i] = false;
	    }
	    if (dim < 3) {
		n[2] = 1; o[2] = o[1]; d[2] = d[1]; plane[2] = false;
	    }
	    
	    /* read initial guess */
	    s = sf_floatalloc(nt);
	    sf_floatread(s,nt,sinp);
	    
	    if (!sf_getbool("velocity",&velocity)) velocity=true;
	    /* if y, the input is velocity; n, slowness squared */

	    if (velocity) {
		for (it=0; it < nt; it++) {
		    s[it] = 1./s[it]*1./s[it];
		}
	    }

	    /* allocate memory for temporary data */
	    ds    = sf_floatalloc(nt);
	    flag  = sf_intalloc(nt);
	    
	    temps = sf_floatalloc(nt);
	    for (it=0; it < nt; it++) {
		temps[it] = s[it];
	    }
	    
	    if (!sf_getbool("l1norm",&l1norm)) l1norm=false;
	    /* norm for minimization (default L2 norm) */

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

	    /* read in shot file */
	    if (NULL == sf_getstring("shot"))
		sf_error("Need source shot=");
	    shot = sf_input("shot");

	    if (!sf_histint(shot,"n2",&nshot)) nshot=1;

	    source = sf_floatalloc2(3,nshot);
	    sf_floatread(source[0],3*nshot,shot);
	    sf_fileclose(shot);

	    /* allocate memory for time table */
	    t = sf_floatalloc2(nt,nshot);

	    /* read in receiver file */
	    m = sf_intalloc2(nt,nshot);
	    if (NULL == sf_getstring("receiver")) {
		for (is=0; is < nshot; is++) {
		    for (it=0; it < nt; it++) {
			m[is][it] = 1;
		    }
		}
	    } else {
		rece = sf_input("receiver");
		sf_intread(m[0],nt*nshot,rece);
		sf_fileclose(rece);
	    }
	    
	    /* number of right-hand side */
	    nrhs = 0;
	    for (is=0; is < nshot; is++) {
		for (it=0; it < nt; it++) {
		    if (m[is][it] == 1) nrhs++;
		}
	    }
	    rhs = sf_floatalloc(nrhs);
	    
	    /* read in record file */
	    if (NULL == sf_getstring("record"))
		sf_error("Need data record=");
	    reco = sf_input("record");

	    t0 = sf_floatalloc(nrhs);
	    sf_floatread(t0,nrhs,reco);
	    sf_fileclose(reco);

	    /* read in topography file */
	    if (NULL != sf_getstring("topo")) {
		topo = sf_input("topo");
		k = sf_intalloc(nt);
		sf_intread(k,nt,topo);
		sf_fileclose(topo);
	    } else {
		k = NULL;
	    }
	    
	    if (!sf_getint("order",&order)) order=2;
	    /* fast marching accuracy order */

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

	    if (!sf_getint("stiter",&stiter)) stiter=200;
	    /* number of step iterations */

	    if (!sf_getfloat("eps",&eps)) eps=0.;
	    /* regularization parameter */

	    /* output gradient at each iteration */
	    if (NULL != sf_getstring("gradient")) {
		grad = sf_output("gradient");
		sf_putint(grad,"n3",n[2]);
		sf_putfloat(grad,"d3",d[2]);
		sf_putfloat(grad,"o3",o[2]);
		sf_putint(grad,"n4",niter);
	    } else {
		grad = NULL;
	    }

	    /* output misfit L2 norm at each iteration */
	    if (NULL != sf_getstring("misnorm")) {
		norm = sf_output("misnorm");
		sf_putint(norm,"n1",niter+1);
		sf_putfloat(norm,"d1",1.);
		sf_putfloat(norm,"o1",0.);
		sf_putint(norm,"n2",1);
		sf_putint(norm,"n3",1);
	    } else {
		norm = NULL;
	    }

	    /* initialize fatomo */
	    fatomo_init(dim,n,d,nshot);

	    /* initialize 2D gradient operator */
	    sf_igrad2_init(n[0],n[1]);

	    if (l1norm) {
		/*
		if (!sf_getfloat("perc",&perc)) perc=90.;

		l1_init(nt,stiter,perc,false);
		*/
		if (!sf_getint("nfreq",&nfreq)) nfreq=1;
		/* l1-norm weighting nfreq */

		if (!sf_getint("nmem",&nmem)) nmem=1;
		/* l1-norm weighting nmem */

		weight = sf_l1;
		sf_irls_init(nt);
	    }

	    /* initial misfit */
	    fastmarch_init(n[2],n[1],n[0]);
	    
	    i = 0;
	    for (is=0; is < nshot; is++) {
		fastmarch(t[is],s,flag,plane,
			  n[2],n[1],n[0],o[2],o[1],o[0],d[2],d[1],d[0],
			  source[is][2],source[is][1],source[is][0],1,1,1,order);

		for (it=0; it < nt; it++) {
		    if (m[is][it] == 1) {
			rhs[i] = t0[i]-t[is][it];
			i++;
		    }
		}
	    }
	    
	    fastmarch_close();
	    
	    /* calculate L2 data-misfit */
	    rhsnorm0 = cblas_snrm2(nrhs,rhs,1);
	    rhsnorm = rhsnorm0;
	    rhsnorm1 = rhsnorm;
	    rate = rhsnorm1/rhsnorm0;

	    if (l1norm)
		sf_warning("L1 misfit after iteration 0 of %d: %g",niter,rate);
	    else
		sf_warning("L2 misfit after iteration 0 of %d: %g",niter,rate);

	    if (norm != NULL) sf_floatwrite(&rate,1,norm);

	    /* iterations over inversion */
	    for (iter=0; iter < niter; iter++) {
		
		/* clean-up */
		for (it=0; it < nt; it++) {
		    ds[it] = 0.;
		}

		/* prepare for CG */
		fatomo_set(t,m);

		/* solve ds */
		if (l1norm) {
		    /*
		      sf_solver_reg(fatomo_lop,l1step,sf_igrad2_lop,2*nt, nt,nrhs,ds,rhs,stiter,eps,"verb",verb,"end");
		    */
		    sf_solver_reg(fatomo_lop,sf_cgstep,sf_igrad2_lop,2*nt,nt,nrhs,ds,rhs,stiter,eps,"wght",weight,"nfreq",nfreq,"nmem",nmem,"verb",verb,"end");
		    
		    /*
		      l1step_close();
		    */
		    
		    sf_cgstep_close();
		} else {
		    sf_solver_reg(fatomo_lop,sf_cgstep,sf_igrad2_lop,2*nt,nt,nrhs,ds,rhs,stiter,eps,"verb",verb,"end");
			
		    sf_cgstep_close();
		}
		
		/* line search */
		gama = 1.;
		for (count=0; count < 10; count++) {
		    
		    /* update slowness */
		    for (it=0; it < nt; it++) {
			if (k == NULL || k[it] != 1)
			    temps[it] = (s[it]+gama*ds[it])*(s[it]+gama*ds[it])/s[it];
		    }

		    /* forward fast-marching for stencil time */
		    fastmarch_init(n[2],n[1],n[0]);
		    
		    i = 0;
		    for (is=0; is < nshot; is++) {
			fastmarch(t[is],temps,flag,plane,
				  n[2],n[1],n[0],o[2],o[1],o[0],d[2],d[1],d[0],
				  source[is][2],source[is][1],source[is][0],1,1,1,order);
			
			for (it=0; it < nt; it++) {
			    if (m[is][it] == 1) {
				rhs[i] = t0[i]-t[is][it];
				i++;
			    }
			}
		    }
		    
		    fastmarch_close();
		    
		    rhsnorm = cblas_snrm2(nrhs,rhs,1);
		    rate = rhsnorm/rhsnorm1;
		    
		    if (rate < 1.) {
			for (it=0; it < nt; it++) {
			    s[it] = temps[it];
			}
			rhsnorm1 = rhsnorm;
			rate = rhsnorm1/rhsnorm0;
			break;
		    }
		    
		    gama *= 0.5;
		}
		
		if (count == 10) {
		    sf_warning("Line-search Failure. Iteration terminated at %d of %d.",iter+1,niter);
		    sf_warning("Dimensions for GRAD and NORM need to be fixed before read.");
		    break;
		}

		if (l1norm)
		    sf_warning("L1 misfit after iteration %d of %d: %g (line-search %d)",iter+1,niter,rate,count);
		else
		    sf_warning("L2 misfit after iteration %d of %d: %g (line-search %d)",iter+1,niter,rate,count);
		
		if (grad != NULL) sf_floatwrite(ds,nt,grad);
		if (norm != NULL) sf_floatwrite(&rate,1,norm);
	    }
	    
	    /* convert to velocity */
	    if (velocity) {
		for (it=0; it < nt; it++) {
		    s[it] = 1./sqrtf(s[it]);
		}
	    }
	    
	    sf_floatwrite(s,nt,sout);

       	    break;
    }
    
    exit(0);
}
Пример #11
0
int main(int argc, char* argv[])
{
    int i1, i2, n1, n2, n12, niter, iter, nliter;
    float *data, *modl, *wght, eps, **nr;
    bool verb;
    sf_file in, out, rect;

    sf_init(argc,argv);
    in = sf_input("in");
    out = sf_output("out");
    rect = sf_input("rect");
    
    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (SF_FLOAT != sf_gettype(rect)) sf_error("Need float rect");

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);
    n12 = n1*n2;

    data = sf_floatalloc(n12);
    modl = sf_floatalloc(n12);
    wght = sf_floatalloc(n12);
    nr = sf_floatalloc2(n1,n2);

    sf_floatread(data,n12,in);
    sf_floatread(nr[0],n12,rect);

    for (i1=0; i1 < n12; i1++) {
	wght[i1] = 1.;
    }

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

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

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

    if (!sf_getfloat("eps",&eps)) eps=0.;
    /* regularization parameter */

    nsmooth1_init(n1,n2,nr);
    sf_weight_init(wght);
    sf_hilbert_init(n1, 10, 1.);

    for (iter=0; iter < nliter; iter++) {
	sf_solver_prec(nsmooth1_lop,sf_cgstep,sf_weight_lop,
		       n12,n12,n12,modl,data,niter,eps,
		       "verb",verb,"end");
	sf_cgstep_close();

	for (i2=0; i2 < n2; i2++) {
	    sf_hilbert(modl+i2*n1,wght+i2*n1);
	    for (i1=0; i1 < n1; i1++) {
		wght[i1+i2*n1] = hypotf(modl[i1+i2*n1],wght[i1+i2*n1]);
	    }
	}
    }

    sf_floatwrite(modl,n12,out);

    exit(0);
}
Пример #12
0
int main (int argc, char* argv[]) {

    sf_file in, out, fileDweight, fileRefl, fileDiff;

	// data
    int tn; float to, td;
	int dipn;  float dipo, dipd;
    int sdipn; float sdipo, sdipd;    
	// reflection model
	int dip0n;  float dip0o, dip0d; // dips in x-direction
	int sdip0n; float sdip0o, sdip0d; // dips in y-direction
	// diffraction model
    int xin;  float xio, xid; // xi in x-direction
    int sxin; float sxio, sxid; // xi in y-direction

    int xn, yn, dagNum;
    float *data = NULL, *model = NULL, *dweight = NULL;
    int im, ix;
    int invMod;

    int   niter, liter, iter;
    bool  adj, verb, isAA;
    float eps;
    float *w, *p;

    int dataSize, diffSize, reflSize, modelSize, id;

    sf_init (argc, argv);

    in  = sf_input  ("in");
    out = sf_output ("out");
	
    fileDweight = NULL;
    if ( NULL != sf_getstring ("dweight") ) {
		/* input file containing data weights */ 
		fileDweight = sf_input ("dweight");
    }

    if (!sf_histint   (in, "n1", &tn) ) sf_error ("No n1= in input");
    if (!sf_histfloat (in, "o1", &to) ) sf_error ("No o1= in input");
    if (!sf_histfloat (in, "d1", &td) ) sf_error ("No d1= in input");

    if (!sf_histint   (in, "n4", &xn) ) sf_error ("No n4= in input");
    if (!sf_histint   (in, "n5", &yn) ) sf_error ("No n5= in input");
	dagNum = xn * yn;
		

    if (!sf_getbool ("verb", &verb)) verb=false; /* verbosity flag */
    if (!sf_getbool ( "adj", &adj )) adj=false;  /* adjoint flag */
    if (!sf_getbool ("isAA", &isAA)) isAA = false;
    /* if y, apply anti-aliasing */

    if (!sf_getint ("liter",&liter)) liter=100; /* number of linear iterations (for inversion) */
    if (!sf_getint ("niter",&niter)) niter=0; /* number of nonlinear iterations (for inversion) */
    if (!sf_getfloat ("eps",&eps)) eps=0.;    /* regularization parameter */
    if (!sf_getbool("verb",&verb)) verb = false; /* verbosity flag */
    if (!sf_getint ("invMod", &invMod)) invMod=2; /* number of nonlinear iterations (for inversion) */

    if (adj) { // data -> model

		fileDiff = out;
		fileRefl = sf_output ("reflMod");

		if (!sf_histfloat (in, "o2", &dipo) ) sf_error ("No o2= in input");
		if (!sf_histfloat (in, "d2", &dipd) ) sf_error ("No d2= in input");
		if (!sf_histint   (in, "n2", &dipn) ) sf_error ("No n2= in input");

		if (!sf_histfloat (in, "o3", &sdipo) ) sf_error ("No o3= in input");
		if (!sf_histfloat (in, "d3", &sdipd) ) sf_error ("No d3= in input");
		if (!sf_histint   (in, "n3", &sdipn) ) sf_error ("No n3= in input");

		// reflection model
		if (!sf_getint ("dip0n", &dip0n) ) sf_error ("Need dip0n=");
		/* number of dip0 values (if adj=y) */
		if (!sf_getfloat ("dip0d", &dip0d)) sf_error("Need dip0d=");
		/* dip0 sampling (if adj=y) */
		if (!sf_getfloat("dip0o",&dip0o)) sf_error("Need dip0d0=");
		/* dip0 origin (if adj=y) */
		if (!sf_getint ("sdip0n", &sdip0n) ) sf_error ("Need sdip0n=");
		/* number of sdip0 values (if adj=y) */
		if (!sf_getfloat ("sdip0d", &sdip0d)) sf_error ("Need sdip0d=");
		/* sdip0 sampling (if adj=y) */
		if (!sf_getfloat("sdip0o", &sdip0o)) sf_error ("Need sdip0d0=");
		/* sdip0 origin (if adj=y) */

		// diffraction model
		if (!sf_getint ("xin", &xin) ) sf_error ("Need xin=");
		/* number of xi values (if adj=y) */
		if (!sf_getfloat ("xid", &xid)) sf_error("Need xid=");
		/* xi sampling (if adj=y) */
		if (!sf_getfloat("xio",&xio)) sf_error("Need xio=");
		/* xi origin (if adj=y) */
		if (!sf_getint ("sxin", &sxin) ) sf_error ("Need sxin=");
		/* number of xi values (if adj=y) */
		if (!sf_getfloat ("sxid", &sxid)) sf_error("Need sxid=");
		/* xi sampling (if adj=y) */
		if (!sf_getfloat("sxio", &sxio)) sf_error("Need sxio=");
		/* xi origin (if adj=y) */

		// diffraction-model file
		sf_putint    (fileDiff,  "n2", xin);
		sf_putfloat  (fileDiff,  "d2", xid);
		sf_putfloat  (fileDiff,  "o2", xio);
		sf_putstring (fileDiff, "label2", "xi");
		sf_putstring (fileDiff, "unit2", "");

		sf_putint    (fileDiff,  "n3", sxin);
		sf_putfloat  (fileDiff,  "d3", sxid);
		sf_putfloat  (fileDiff,  "o3", sxio);
		sf_putstring (fileDiff, "label3", "xi");
		sf_putstring (fileDiff, "unit3", "");

		// reflection-model file
		sf_putint    (fileRefl, "n2", dip0n);
		sf_putfloat  (fileRefl, "d2", dip0d);
		sf_putfloat  (fileRefl, "o2", dip0o);
		sf_putstring (fileRefl, "label2", "dip angle");
		sf_putstring (fileRefl, "unit2", "deg");

		sf_putint    (fileRefl, "n3", sdip0n);
		sf_putfloat  (fileRefl, "d3", sdip0d);
		sf_putfloat  (fileRefl, "o3", sdip0o);
		sf_putstring (fileRefl, "label3", "dip angle");
		sf_putstring (fileRefl, "unit3", "deg");

    } else { // model -> data

		fileDiff = in;
		fileRefl = sf_input ("reflMod");

		// reflection model
		if ( !sf_histint   (fileRefl, "n2", &dip0n) ) sf_error ("No n2= in reflection-model file");	
		if ( !sf_histfloat (fileRefl, "d2", &dip0d) ) sf_error ("No d2= in reflection-model file");	
		if ( !sf_histfloat (fileRefl, "o2", &dip0o) ) sf_error ("No o2= in reflection-model file");

		if ( !sf_histint   (fileRefl, "n3", &sdip0n) ) sf_error ("No n3= in reflection-model file");	
		if ( !sf_histfloat (fileRefl, "d3", &sdip0d) ) sf_error ("No d3= in reflection-model file");	
		if ( !sf_histfloat (fileRefl, "o3", &sdip0o) ) sf_error ("No o3= in reflection-model file");

		// diffraction model
		if ( !sf_histint   (fileDiff, "n2", &xin) ) sf_error ("No n2= in diffraction-model file");	
		if ( !sf_histfloat (fileDiff, "d2", &xid) ) sf_error ("No d2= in diffraction-model file");	
		if ( !sf_histfloat (fileDiff, "o2", &xio) ) sf_error ("No o2= in diffraction-model file");	

		if ( !sf_histint   (fileDiff, "n3", &sxin) ) sf_error ("No n3= in diffraction-model file");		
		if ( !sf_histfloat (fileDiff, "d3", &sxid) ) sf_error ("No d3= in diffraction-model file");		
		if ( !sf_histfloat (fileDiff, "o3", &sxio) ) sf_error ("No o3= in diffraction-model file");	

		// run parameters
		if (!sf_getint ("dipn", &dipn)) sf_error ("Need dipn=");
		/* number of dips in x-direction */
		if (!sf_getfloat ("dipo", &dipo)) sf_error ("Need dipo=");
		/* dip origin in x-direction */
		if (!sf_getfloat ("dipd", &dipd)) sf_error ("Need dipd=");
		/* dip sampling in x-direction */
		if (!sf_getint ("sdipn", &sdipn)) sf_error ("Need sdipn=");
		/* number of dips in y-direction */
		if (!sf_getfloat ("sdipo", &sdipo)) sf_error ("Need sdipo=");
		/* dip origin in y-direction */
		if (!sf_getfloat ("sdipd", &sdipd)) sf_error ("Need sdipd=");
		/* dip sampling in y-direction */

		// output file
		sf_putint    (out, "n2", dipn);
		sf_putfloat  (out, "o2", dipo);
		sf_putfloat  (out, "d2", dipd);
		sf_putstring (out, "label2", "x-dip angle");
		sf_putstring (out, "unit2", "deg");

		sf_putint    (out, "n3", sdipn);
		sf_putfloat  (out, "o3", sdipo);
		sf_putfloat  (out, "d3", sdipd);
		sf_putstring (out, "label3", "y-dip angle");
		sf_putstring (out, "unit3", "deg");
    }

	// data size
    dataSize = tn * dipn * sdipn;
    // model sizes
    diffSize = tn * xin * sxin; 
    reflSize = tn * dip0n * sdip0n;
    
	modelSize = -1;
    switch (invMod) {
		case 0: modelSize = diffSize; break;
		case 1: modelSize = reflSize; break;
		case 2: modelSize = diffSize + reflSize; break;
    }

    data    = sf_floatalloc (dataSize);
    model  = sf_floatalloc (modelSize);

    w = (0 == niter) ? NULL : sf_floatalloc (modelSize);
    p = (0 == niter) ? NULL : sf_floatalloc (modelSize);

    ditime3d_init (dipo, dipd, dipn, 
				   sdipo, sdipd, sdipn, 
				   xio, xid, xin, 
				   sxio, sxid, sxin, 
				   dip0o, dip0d, dip0n, 
				   sdip0o, sdip0d, sdip0n, 
				   to, td, tn, 
				   isAA, invMod);

	// main loop	
	
    for (ix = 0; ix < dagNum; ++ix) { 
		if (verb) sf_warning ("i=%d of %d", ix + 1, dagNum);

		// read data	
		if (adj) { // data -> model
		    sf_floatread (data, dataSize, in);
		    dweight = sf_floatalloc (dataSize);
		    if (fileDweight) {
				sf_floatread (dweight, dataSize, fileDweight);
			} else {
				for (id = 0; id < dataSize; ++id) dweight[id] = 1.f;
			}
		} else { // model -> data
			if (0 == invMod) { // difffraction
			    sf_floatread (model, diffSize, fileDiff);
			} else if (1 == invMod) { // reflection
				sf_floatread (model, reflSize, fileRefl);
			} else { // diffraction + reflection
			    sf_floatread (model, diffSize, fileDiff);
			    sf_floatread (model + diffSize, reflSize, fileRefl);
			}
		}
		
		// perform transform
		if (!adj || 0 == niter) {
		    ditime3d_lop (adj, false, modelSize, dataSize, model, data);
		} else {
		    // initital model weights
		    for (im = 0; im < modelSize; ++im) {
				w[im] = 1.f;
		    }

		    for (iter = 0; iter < niter; ++iter) {
				sf_solver_prec (ditime3d_lop, sf_cgstep, sf_copy_lop,
								modelSize, modelSize, dataSize, model, data, liter, eps,
								"verb", verb, "mwt", w, "xp", p, "wt", dweight, "end");
				sf_cgstep_close ();

				for (im = 0; im < modelSize; ++im) {
				    w[im] = fabsf (p[im]); /* weight for sparsity */
				}
		    }
		}

		// write result
		if (adj) { // data -> model
			if (0 == invMod) { // difffraction
			    sf_floatwrite (model, diffSize, fileDiff);
			} else if (1 == invMod) { // reflection
				sf_floatwrite (model, reflSize, fileRefl);
			} else { // diffraction + reflection
			    sf_floatwrite (model, diffSize, fileDiff);
			    sf_floatwrite (model + diffSize, reflSize, fileRefl);
			}
		} else { // model -> data
		    sf_floatwrite (data, dataSize, out);
		} 
    }

	// finish
	ditime3d_close ();

	if (w) free (w);
	if (p) free (p);
	if (dweight) free (dweight);    

    free (data);
    free (model);

    sf_fileclose (in);
    sf_fileclose (out);
    sf_fileclose (fileRefl);
    if (fileDweight) sf_fileclose (fileDweight);

    return 0;
}
Пример #13
0
int main(int argc, char* argv[])
{
    bool adj, inv;
    int n, n2, i2, niter, nliter, iter;
    float *x, *y, *w, **a, **b, eps;
    sf_file in, out, mat1, mat2;

    sf_init(argc,argv);
    in = sf_input("in");
    out = sf_output("out");
    
    mat1 = sf_input("mat1");
    mat2 = sf_input("mat2");
    
    if (SF_FLOAT != sf_gettype(in) ||
	SF_FLOAT != sf_gettype(mat1) ||
	SF_FLOAT != sf_gettype(mat2)) sf_error("Need float input");

    if (!sf_histint(in,"n1",&n)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2) || n2 != n) sf_error("Need n2=%d",n);
    n2 = n*n;

    if (!sf_getbool("adj",&adj)) adj=false;
    /* adjoint flag */
    if (!sf_getbool("inv",&inv)) inv=false;
    /* inversion flag */
    if (!sf_getint("niter",&niter)) niter=100;
    /* maximum number of iterations */
    if (!sf_getfloat("eps",&eps)) eps=0.;
    /* regularization */
    if (!sf_getint("nliter",&nliter)) nliter=1;
    /* number of nonlinear iterations */

    a = sf_floatalloc2(n,n);
    b = sf_floatalloc2(n,n);

    x = sf_floatalloc(n2);
    y = sf_floatalloc(n2);
    w = sf_floatalloc(n2);
    
    kron_init(n,a,b);

    sf_floatread(x,n2,in);
    sf_floatread(a[0],n2,mat1);
    sf_floatread(b[0],n2,mat2);

    if (adj) {
	if (inv) {
	    sf_weight_init(w);
	    for (i2=0; i2 < n2; i2++) {
		w[i2] = 1.;
	    }
	    for (iter=0; iter < nliter; iter++) {
		sf_solver_prec (kron_lop, sf_cgstep, sf_weight_lop, n2, n2, n2,
				y, x, niter,  eps, "verb", true, "end");
		sf_cgstep_close();
		for (i2=0; i2 < n2; i2++) {
		    w[i2] = fabsf(y[i2]);
		}
	    }
	} else {
	    kron_lop(true,false,n2,n2,y,x);
	} 
    } else {
	kron_lop(false,false,n2,n2,x,y);
    }

    sf_floatwrite(y,n2,out);


/*
  p = sf_floatalloc(n2);	    
  sf_conjgrad_init(n2,n2,n2,n2,eps,FLT_EPSILON,true,false);
  sf_conjgrad(NULL,kron_lop,sf_copy_lop,p,y,x,niter);
*/

    exit(0);
}
Пример #14
0
int main(int argc, char* argv[])
{
    bool velocity, causal, limit, verb, shape;
    int dimw, dimt, i, n[SF_MAX_DIM], rect[SF_MAX_DIM], iw, nw, ir, nr;
    long nt, *order;
    int iter, niter, cgiter, count;
    int *ff, *dp, *mp, nloop;
    float o[SF_MAX_DIM], d[SF_MAX_DIM], *dt, *dw, *dv, *t, *w, *t0, *w1, *p=NULL;
    float eps, tol, thres, rhsnorm, rhsnorm0, rhsnorm1, rate, gama;
    char key[6];
    sf_file in, out, reco, grad, mask, prec;

    sf_init(argc,argv);
    in  = sf_input("in");
    out = sf_output("out");
   
    /* read dimension */
    dimw = sf_filedims(in,n);
	    
    nw = 1;
    for (i=0; i < dimw; i++) {
	sprintf(key,"d%d",i+1);
	if (!sf_histfloat(in,key,d+i)) sf_error("No %s= in input.",key);
	sprintf(key,"o%d",i+1);
	if (!sf_histfloat(in,key,o+i)) o[i]=0.;
	nw *= n[i];
    }
	    
    if (dimw > 2) sf_error("Only works for 2D now.");
	    
    n[2] = n[1]; d[2] = d[1]; o[2] = o[1];
    dimt = 3; nr = n[1]*n[2]; nt = nw*n[2];

    /* read initial velocity */
    w = sf_floatalloc(nw);
    sf_floatread(w,nw,in);
	    
    if (!sf_getbool("velocity",&velocity)) velocity=true;
    /* if y, the input is velocity; n, slowness-squared */
	    	    
    /* convert to slowness-squared */
    if (velocity) {
	for (iw=0; iw < nw; iw++)
	    w[iw] = 1./w[iw]*1./w[iw];
	
	dv = sf_floatalloc(nw);
    } else {
	dv = NULL;
    }
	    
    if (!sf_getbool("limit",&limit)) limit=false;
    /* if y, limit computation within receiver coverage */

    if (!sf_getbool("shape",&shape)) shape=false;
    /* shaping regularization (default no) */
	    
    /* read record */
    if (NULL == sf_getstring("reco"))
	sf_error("Need record reco=");
    reco = sf_input("reco");
    
    t0 = sf_floatalloc(nr);
    sf_floatread(t0,nr,reco);
    sf_fileclose(reco);
	    
    /* read receiver mask */	    
    if (NULL == sf_getstring("mask")) {
	mask = NULL;
	dp = NULL;
    } else {
	mask = sf_input("mask");
	dp = sf_intalloc(nr);
	sf_intread(dp,nr,mask);
	sf_fileclose(mask);
    }
	    
    /* read model mask */
    if (NULL == sf_getstring("prec")) {
	prec = NULL;
	mp = NULL;
    } else {
	prec = sf_input("prec");
	mp = sf_intalloc(nw);
	sf_intread(mp,nw,prec);
	sf_fileclose(prec);
    }

    if (!sf_getbool("verb",&verb)) verb=false;
    /* verbosity flag */
	    
    if (!sf_getint("niter",&niter)) niter=5;
    /* number of inversion iterations */
	    
    if (!sf_getint("cgiter",&cgiter)) cgiter=10;
    /* number of conjugate-gradient iterations */
	    
    if (!sf_getfloat("thres",&thres)) thres=5.e-5;
    /* threshold (percentage) */
	    
    if (!sf_getfloat("tol",&tol)) tol=1.e-3;
    /* tolerance for bisection root-search */

    if (!sf_getint("nloop",&nloop)) nloop=10;
    /* number of bisection root-search */

    /* output gradient at each iteration */
    if (NULL != sf_getstring("grad")) {
	grad = sf_output("grad");
	sf_putint(grad,"n3",niter);
    } else {
	grad = NULL;
    }
	    
    if (!sf_getfloat("eps",&eps)) eps=0.;
    /* regularization parameter */

    if (shape) {
	for (i=0; i < dimw; i++) {
	    sprintf(key,"rect%d",i+1);
	    if (!sf_getint(key,rect+i)) rect[i]=1;
	    /*( rect#=(1,1,...) smoothing radius on #-th axis )*/
	}
	
	/* triangle smoothing operator */
	sf_trianglen_init(dimw,rect,n);
	sf_repeat_init(nw,1,sf_trianglen_lop);
	
	sf_conjgrad_init(nw,nw,nr,nr,eps,1.e-6,verb,false);
	p = sf_floatalloc(nw);
    } else {
	/* initialize 2D gradient operator */
	sf_igrad2_init(n[0],n[1]);
    }

    /* allocate temporary array */
    t  = sf_floatalloc(nt);
    dw = sf_floatalloc(nw);
    dt = sf_floatalloc(nr);
    w1 = sf_floatalloc(nw);
    ff = sf_intalloc(nt);

    if (!sf_getbool("causal",&causal)) causal=true;
    /* if y, neglect non-causal branches of DSR */

    /* initialize eikonal */
    dsreiko_init(n,o,d,
		 thres,tol,nloop,
		 causal,limit,dp);
	    
    /* initialize operator */
    dsrtomo_init(dimt,n,d);

    /* upwind order */
    order = dsrtomo_order();

    /* initial misfit */
    dsreiko_fastmarch(t,w,ff,order);
    dsreiko_mirror(t);

    /* calculate L2 data-misfit */
    for (ir=0; ir < nr; ir++) {
	if (dp == NULL || dp[ir] == 1) {
	    dt[ir] = t0[ir]-t[(long) ir*n[0]];
	} else {
	    dt[ir] = 0.;
	}
    }

    rhsnorm0 = cblas_snrm2(nr,dt,1);
    rhsnorm = rhsnorm0;
    rhsnorm1 = rhsnorm;
    rate = rhsnorm1/rhsnorm0;
	    
    sf_warning("L2 misfit after iteration 0 of %d: %g",niter,rate);
    
    /* iterations over inversion */
    for (iter=0; iter < niter; iter++) {
	
	/* clean-up */
	for (iw=0; iw < nw; iw++) dw[iw] = 0.;
	
	/* set operator */
	dsrtomo_set(t,w,ff,dp,mp);
	
	/* solve dw */
	if (shape) {
	    sf_conjgrad(NULL,dsrtomo_oper,sf_repeat_lop,p,dw,dt,cgiter);
	} else {
	    sf_solver_reg(dsrtomo_oper,sf_cgstep,sf_igrad2_lop,2*nw,nw,nr,dw,dt,cgiter,eps,"verb",verb,"end");
	    sf_cgstep_close();
	}
	
	/* output gradient */
	if (grad != NULL) {
	    if (velocity) {
		for (iw=0; iw < nw; iw++) {
		    dv[iw] = -dw[iw]/(2.*sqrtf(w[iw])*(w[iw]+dw[iw]/2.));
		}
		sf_floatwrite(dv,nw,grad);
	    } else {
		sf_floatwrite(dw,nw,grad);
	    }
	}
	
	/* line search */
	gama = 0.5;
	for (count=0; count < 5; count++) {
	    
	    /* update slowness */
	    for (iw=0; iw < nw; iw++) 
		w1[iw] = (w[iw]+gama*dw[iw])*(w[iw]+gama*dw[iw])/w[iw];
	    
	    /* compute new misfit */
	    dsreiko_fastmarch(t,w1,ff,order);
	    dsreiko_mirror(t);
	    
	    for (ir=0; ir < nr; ir++) {
		if (dp == NULL || dp[ir] == 1) {
		    dt[ir] = t0[ir]-t[(long) ir*n[0]];
		} else {
		    dt[ir] = 0.;
		}
	    }

	    rhsnorm = cblas_snrm2(nr,dt,1);
	    rate = rhsnorm/rhsnorm1;
	    
	    if (rate < 1.) {
		for (iw=0; iw < nw; iw++) w[iw] = w1[iw];
		
		rhsnorm1 = rhsnorm;
		rate = rhsnorm1/rhsnorm0;
		break;
	    }
	    
	    gama *= 0.5;
	}
	
	if (count == 5) {
	    sf_warning("Line-search failure at iteration %d of %d.",iter+1,niter);
	    break;
	}
	
	sf_warning("L2 misfit after iteration %d of %d: %g (line-search %d)",iter+1,niter,rate,count);
    }
    
    /* convert to velocity */
    if (velocity) {
	for (iw=0; iw < nw; iw++) {
	    w[iw] = 1./sqrtf(w[iw]);
	}
    }
    
    sf_floatwrite(w,nw,out);
       
    exit(0);
}