Example #1
0
/*
  Add a PSD to another.
*/
void add_psd2(dmat **out, const dmat *in){
    if(!*out){
	*out=ddup(in);
    }else{
	dmat *tmp=*out;
	*out=add_psd(tmp, in);
	dfree(tmp);
    }
}
Example #2
0
void *thread (void *arg)
{
    thd_t *t = arg;
    char *key, *fence_name = NULL;
    int i, flags = 0;
    struct timespec t0;
    uint32_t rank;
    flux_future_t *f;
    flux_kvs_txn_t *txn;

    if (!(t->h = flux_open (NULL, 0))) {
        log_err ("%d: flux_open", t->n);
        goto done;
    }
    if (flux_get_rank (t->h, &rank) < 0) {
        log_err ("%d: flux_get_rank", t->n);
        goto done;
    }
    for (i = 0; i < count; i++) {
        if (!(txn = flux_kvs_txn_create ()))
            log_err_exit ("flux_kvs_txn_create");
        key = xasprintf ("%s.%"PRIu32".%d.%d", prefix, rank, t->n, i);
        if (fopt)
            fence_name = xasprintf ("%s-%d", prefix, i);
        if (sopt)
            monotime (&t0);
        if (flux_kvs_txn_pack (txn, 0, key, "i", 42) < 0)
            log_err_exit ("%s", key);
        if (nopt && (i % nopt_divisor) == 0)
            flags |= FLUX_KVS_NO_MERGE;
        else
            flags = 0;
        if (fopt) {
            if (!(f = flux_kvs_fence (t->h, flags, fence_name,
                                                   fence_nprocs, txn))
                    || flux_future_get (f, NULL) < 0)
                log_err_exit ("flux_kvs_fence");
            flux_future_destroy (f);
        } else {
            if (!(f = flux_kvs_commit (t->h, flags, txn))
                    || flux_future_get (f, NULL) < 0)
                log_err_exit ("flux_kvs_commit");
            flux_future_destroy (f);
        }
        if (sopt && zlist_append (t->perf, ddup (monotime_since (t0))) < 0)
            oom ();
        free (key);
        free (fence_name);
        flux_kvs_txn_destroy (txn);
    }
done:
    if (t->h)
        flux_close (t->h);
    return NULL;
}
Example #3
0
void setup_aster_lsr(ASTER_S *aster, STAR_S *star, const PARMS_S *parms){
    int ndtrat=parms->skyc.ndtrat;
    if(aster->pgm){
	dcellfree(aster->pgm);
	dcellfree(aster->sigman);
    }
    aster->pgm=cellnew(ndtrat,1);
    aster->sigman=cellnew(ndtrat,1);
    dmat *gm=ddup(aster->gm);
    if(aster->nwfs==1 && parms->maos.nmod==6 && gm->nx==8){
	info2("set 3rd column of gm to zero\n");
	memset(gm->p+gm->nx*2, 0, sizeof(double)*gm->nx);
    }

    for(int idtrat=0; idtrat<ndtrat; idtrat++){
	dcell *nea=cellnew(aster->nwfs, 1);
	for(int iwfs=0; iwfs<aster->nwfs; iwfs++){
	    nea->p[iwfs]=ddup(aster->wfs[iwfs].pistat->sanea->p[idtrat]);
	    dcwpow(nea->p[iwfs], 2);
	}
	dmat *neam=dcell2m(nea);//measurement error covariance
	dcellfree(nea); 
	dcwpow(neam, -1);//inverse
	/*Reconstructor */
	aster->pgm->p[idtrat]=dpinv(gm, neam);
	/*sigman is error due to noise. */
	dcwpow(neam, -1);//inverse again
	aster->sigman->p[idtrat]=calc_recon_error(aster->pgm->p[idtrat],neam,parms->maos.mcc);
	dfree(neam);
    }	
    if(parms->skyc.dbg){
	writebin(gm,"%s/aster%d_gm",dirsetup,aster->iaster);
	writebin(aster->pgm,    "%s/aster%d_pgm", dirsetup,aster->iaster);
	writebin(aster->sigman, "%s/aster%d_sigman", dirsetup,aster->iaster);
    }
    dfree(gm);
}
Example #4
0
File: servo.c Project: bitursa/maos
/**
   Update servo parameters
*/
void servo_update(SERVO_T *st, const dmat *ep){
    dfree(st->ep);
    if(ep->nx!=3){//type I
	st->ep=ddup(ep);
    }else{//type II. convert data format
	st->ep=dnew(ep->nx, ep->ny);
	for(int i=0; i<ep->ny; i++){
	    st->ep->p[i*3]=ep->p[i*3];
	    double a=ep->p[1+i*3];
	    double T=ep->p[2+i*3];
	    st->ep->p[1+i*3]=exp(-st->dt/(a*T));
	    st->ep->p[2+i*3]=exp(-st->dt/T);
	}
    }
}
Example #5
0
/**
   Compute Modal to gradient operator by copying from the stars. Using average
gradients. Similar to Z tilt since the mode is low order */
void setup_aster_g(ASTER_S *aster, STAR_S *star, POWFS_S *powfs, const PARMS_S *parms){
    /*2010-06-08: Tested against MATLAB skycoverage code. */
    aster->g=cellnew(aster->nwfs,1);
    if(parms->maos.nmod<5 || parms->maos.nmod>6){
	error("Not compatible with the number of NGS modes\n");
    }
    aster->ngs=calloc(aster->nwfs, sizeof(long));
    aster->tsa=0;
    for(int iwfs=0; iwfs<aster->nwfs; iwfs++){
	const int istar=aster->wfs[iwfs].istar;
	const int ipowfs=aster->wfs[iwfs].ipowfs;
	const long nsa=parms->maos.nsa[ipowfs];
	aster->g->p[iwfs]=ddup(star[istar].g->p[ipowfs]);
	aster->tsa+=nsa;
	aster->ngs[iwfs]=nsa*2;
    }    
    /*
      aster->g is also used for simulation. Do not zero columns here.
    */
    aster->gm=dcell2m(aster->g);
}
Example #6
0
File: servo.c Project: bitursa/maos
/**
   Initialize. al is additional latency
*/
SERVO_T *servo_new(dcell *merr, const dmat *ap, int al, double dt, const dmat *ep){
    SERVO_T *st=calloc(1, sizeof(SERVO_T));
    if(ap){
	st->ap=ddup(ap);
    }else{
	st->ap=dnew(2,1);
	st->ap->p[0]=1;
    }
    if(st->ap->nx<2){
	dresize(st->ap, 2, 1);//2 element to ensure we keep integrator history.
    }
    st->mint=cellnew(st->ap->nx, 1);
    st->dt=dt;
    st->al=al;
    st->merrhist=cellnew(st->al+1, 1);
    servo_update(st, ep);
    if(merr && merr->nx!=0 && merr->ny!=0 && merr->p[0]){
	servo_init(st, merr);
    }
    return st;
}
Example #7
0
File: psd.c Project: lianqiw/maos
/*
  Add a PSD scaled by scale to another. The first column of each dmat is the
   frequency nu, and the second column is PSD.
*/
void add_psd2(dmat **pout, const dmat *in, double scale){
    if(!*pout){
	*pout=ddup(in);
    }else{
	dmat *out=*pout;
	double *p1=PCOL(out,1);
	dmat *p2new=0;
	const long nx=out->nx;
	const double *p2=0;
	if(check_psd_match(out, in)){
	    p2=PCOL(in, 1);
	}else{
	    dmat *nu1=dsub(out,0, nx,0,1);
	    p2new=dinterp1(in, 0, nu1, 1e-40);
	    p2=PCOL(p2new,0);
	    dfree(nu1);
	}
	
	for(long i=0; i<nx; i++){
	    p1[i]+=p2[i]*scale;
	}
	dfree(p2new);
    }
}
Example #8
0
static void setup_aster_kalman(SIM_S *simu, ASTER_S *aster, STAR_S *star, const PARMS_S *parms){
    int ndtrat=parms->skyc.ndtrat;
    if(parms->skyc.multirate){
	aster->res_ngs=dnew(1,1);
	//assemble neam
	if(aster->neam) error("neam is already set?\n");
	aster->neam=calloc(1, sizeof(dcell*));
	aster->neam[0]=cellnew(aster->nwfs, aster->nwfs);
	for(int iwfs=0; iwfs<aster->nwfs; iwfs++){
	    int ng=aster->g->p[iwfs]->nx;
	    aster->neam[0]->p[iwfs+aster->nwfs*iwfs]=dnew(ng, ng);
	}
	aster->dtrats=dnew(aster->nwfs, 1);
	aster->idtrats=dnew(aster->nwfs, 1);
	int wfs0_min=0, wfs0_max=0;
	PISTAT_S *pistat0=&star[aster->wfs[0].istar].pistat[aster->wfs[0].ipowfs];
	for(int idtrat=0; idtrat<parms->skyc.ndtrat; idtrat++){
	    if(wfs0_min==0 && pistat0->snr->p[idtrat]>3){
		wfs0_min=idtrat;
	    }
	    if(pistat0->snr->p[idtrat]>=parms->skyc.snrmin->p[idtrat]){
		wfs0_max=idtrat;
	    }
	}
	aster->kalman=calloc(1, sizeof(kalman_t*));
	double resmin=INFINITY;
	kalman_t *kalman_min=0;
	int idtrat_min=0;
	//Try progressively lower sampling frequencies until performance starts to degrades
	for(int idtrat_limit=wfs0_max; idtrat_limit>wfs0_min; idtrat_limit--){
	    setup_aster_kalman_dtrat(aster, star, parms, idtrat_limit);
	    aster->kalman[0]=sde_kalman(simu->sdecoeff, parms->maos.dt, aster->dtrats, aster->g, aster->neam[0], 0);
	    dmat *rests=0;
#if 1   //more accurate
	    dmat *res=skysim_sim(parms->skyc.dbg?&rests:0, simu->mideal, simu->mideal_oa, simu->rmsol,
				 aster, 0, parms, -1, 1, -1);
	    double res0=res?res->p[0]:simu->rmsol;
	    dfree(res);
#else
	    rests=kalman_test(aster->kalman[0], simu->mideal);
	    double res0=calc_rms(rests, parms->maos.mcc, parms->skyc.evlstart);
#endif
	    if(parms->skyc.dbg){
		writebin(rests, "isky%d_iaster%d_dtrat%d_rest", simu->isky, aster->iaster, idtrat_limit);
	    }
	    if(parms->skyc.verbose) info2("res0=%g, resmin=%g\n", sqrt(res0)*1e9, sqrt(resmin)*1e9);
	    dfree(rests);
	    if(res0<resmin-100e-18){//better by 10 nm
		resmin=res0;
		kalman_free(kalman_min);
		kalman_min=aster->kalman[0];
		aster->kalman[0]=0;
		idtrat_min=idtrat_limit;
	    }else{
		kalman_free(aster->kalman[0]);aster->kalman[0]=0;
		if(isfinite(resmin) && res0>resmin*2){//stop trying.
		    break;
		}
	    }
	}
	setup_aster_kalman_dtrat(aster, star, parms, idtrat_min);
	if(parms->skyc.verbose) info2("selected\n");
	aster->res_ngs->p[0]=resmin;
	aster->kalman[0]=kalman_min;
    }else{
	if(aster->neam) dcellfreearr(aster->neam, ndtrat);
	aster->neam=calloc(ndtrat, sizeof(dcell*));
	aster->res_ngs=dnew(ndtrat,3);
	PDMAT(aster->res_ngs, pres_ngs);
	aster->kalman=calloc(ndtrat, sizeof(kalman_t*));
	dmat *dtrats=dnew(aster->nwfs,1);
	for(int idtrat=0; idtrat<ndtrat; idtrat++){
	    //assemble neam
	    //TIC;tic;
	    aster->neam[idtrat]=cellnew(aster->nwfs, aster->nwfs);
	    for(int iwfs=0; iwfs<aster->nwfs; iwfs++){
		dmat *tmp=ddup(aster->wfs[iwfs].pistat->sanea->p[idtrat]);/*in rad */
		dcwpow(tmp, 2);
		dsp *tmp2=dspnewdiag(tmp->nx, tmp->p, 1);
		dspfull(&aster->neam[idtrat]->p[iwfs+aster->nwfs*iwfs], tmp2,'n',1);
		dfree(tmp); dspfree(tmp2);
	    }

	    int dtrat=parms->skyc.dtrats->p[idtrat];
	    for(int iwfs=0; iwfs<aster->nwfs; iwfs++){
		dtrats->p[iwfs]=dtrat;
	    }
	    aster->kalman[idtrat]=sde_kalman(simu->sdecoeff, parms->maos.dt, dtrats, aster->g, aster->neam[idtrat], 0);
	    //toc("kalman");
#if 1
	    dmat *res=skysim_sim(0, simu->mideal, simu->mideal_oa, simu->rmsol, aster, 0, parms, idtrat, 1, -1);
	    double rms=res?res->p[0]:simu->rmsol;
	    dfree(res);
#else
	    dmat *res=kalman_test(aster->kalman[idtrat], simu->mideal);
	    double rms=calc_rms(res, parms->maos.mcc, parms->skyc.evlstart);
	    dfree(res);
#endif
	    //toc("estimate");
	    pres_ngs[0][idtrat]=rms;
	}
	dfree(dtrats);
    }
}
Example #9
0
/**
   Compute Von Karman covariance function at separations computed from the grid
   size nx and sampling dx, with Fried parameter of r0, and outerscale of L0.  
   ninit is the initial side size of the atm array to start with.
*/
static vkcov_t* vkcov_calc(double r0, double L0, double dx, long n, long ninit){
    if(L0>9000) L0=INFINITY;/*L0 bigger than 9000 is treated as infinity. */
    vkcov_t *node=vkcov_get(r0, L0, dx, n, ninit);
    if(node) return node;
    node=mycalloc(1,vkcov_t);
    node->r0=r0;
    node->L0=L0;
    node->dx=dx;
    node->n=n;
    node->ninit=ninit;
    long nroot=(long)round(log2((double)n-1));
    node->next=head;
    if(r0>=L0){
	error("Illegal parameter: r0=%g, L0=%g\n", r0, L0);
    }
    head=node;
    dmat *r=dnew(2, nroot+2);
    double sqrt2=sqrt(2);
    IND(r,0,0)=0;
    IND(r,1,0)=0;
    for(long i=0; i<=nroot; i++){
	long j=1<<i;
	IND(r,0,i+1)=j*dx;
	IND(r,1,i+1)=j*dx*sqrt2;
    }
    double D=(n-1)*dx;
    node->cov=turbcov(r, D*sqrt(2), r0, L0);
    dfree(r);
    dmat *rc0=dnew(ninit*ninit, ninit*ninit);
    dmat*  rc=rc0;
    double dx2=dx*(n-1)/(ninit-1);
    for(long j=0; j<ninit; j++){
	double y=dx2*j;
	for(long i=0; i<ninit; i++){
	    double x=dx2*i;
	    long k=i+j*ninit;
	    for(long j2=0; j2<ninit; j2++){
		double y2=dx2*j2;
		for(long i2=0; i2<ninit; i2++){
		    double x2=dx2*i2;
		    long k2=i2+j2*ninit;
		    IND(rc,k2,k)=sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));
		}
	    }
	}
    }
    node->C=turbcov(rc0, D*sqrt(2), r0, L0);
    dfree(rc0);
    dmat *u=NULL, *s=NULL, *v=NULL;
    dsvd(&u, &s, &v, node->C);
    dcwpow(s, 1./2.);
    node->K=ddup(u);
    dmuldiag(node->K, s);

    dcwpow(s, -1);
    dmuldiag(u, s);
    node->KI=dtrans(u);
    dfree(u);
    dfree(v);
    dfree(s);
    /*we have: K*K'==C */
    return node;
}
Example #10
0
int main(int argc, char **argv)
{
    dkhash_table *tx, *t;
    unsigned int x;
    int ret, r2;
    struct test_data s;
    char *p1, *p2;
    char *strs[10];
    char tmp[32];

    t_set_colors(0);
    t_start("dkhash basic test");
    t = dkhash_create(512);

    p1 = strdup("a not-so secret value");
    dkhash_insert(t, "nisse", NULL, p1);
    ok_int(dkhash_num_entries_max(t), 1, "Added one entry, so that's max");
    ok_int(dkhash_num_entries_added(t), 1, "Added one entry, so one added");
    ok_int(dkhash_table_size(t), 512, "Table must be sized properly");
    ok_int(dkhash_collisions(t), 0, "One entry, so zero collisions");
    p2 = dkhash_get(t, "nisse", NULL);
    test(p1 == p2, "get should get what insert set");
    dkhash_insert(t, "kalle", "bananas", p1);
    p2 = dkhash_get(t, "kalle", "bezinga");
    test(p1 != p2, "we should never get the wrong key");
    ok_int(2, dkhash_num_entries(t), "should be 2 entries after 2 inserts");
    p2 = dkhash_remove(t, "kalle", "bezinga");
    ok_int(2, dkhash_num_entries(t), "should be 2 entries after 2 inserts and 1 failed remove");
    ok_int(0, dkhash_num_entries_removed(t), "should be 0 removed entries after failed remove");
    p2 = dkhash_remove(t, "kalle", "bananas");
    test(p1 == p2, "dkhash_remove() should return removed data");
    ok_int(dkhash_num_entries(t), 1, "should be 1 entries after 2 inserts and 1 successful remove");
    p2 = dkhash_remove(t, "nisse", NULL);
    test(p1 == p2, "dkhash_remove() should return removed data");
    ret = t_end();

    t_reset();
    /* lots of tests below, so we shut up while they're running */
    t_verbose = 0;

    t_start("dkhash_walk_data() test");
    memset(&s, 0, sizeof(s));
    /* first we set up the dkhash-tables */
    tx = dkhash_create(16);
    for (x = 0; x < ARRAY_SIZE(keys); x++) {
        dkhash_insert(tx, keys[x].k1, NULL, ddup(x, 0, 0));
        dkhash_insert(tx, keys[x].k2, NULL, ddup(x, 0, 0));
        dkhash_insert(tx, keys[x].k1, keys[x].k2, ddup(x, 0, 0));
        s.x += 3;
        ok_int(s.x, dkhash_num_entries(tx), "x table adding");
    }

    ok_int(s.x, dkhash_num_entries(tx), "x table done adding");

    for (x = 0; x < ARRAY_SIZE(keys); x++) {
        del.x = x;
        del.i = del.j = 0;

        ok_int(s.x, dkhash_num_entries(tx), "x table pre-delete");
        s.x -= 3;
        dkhash_walk_data(tx, del_matching);
        ok_int(s.x, dkhash_num_entries(tx), "x table post-delete");
    }

    test(0 == dkhash_num_entries(tx), "x table post all ops");
    test(0 == dkhash_check_table(tx), "x table consistency post all ops");
    dkhash_debug_table(tx, 0);
    r2 = t_end();
    ret = r2 ? r2 : ret;

    t_reset();
    for (x = 0; x < 10; x++) {
        sprintf(tmp, "string %d", x);
        strs[x] = strdup(tmp);
    }

    t_start("dkhash single bucket add remove forward");

    t = dkhash_create(1);
    for (x = 0; x < 10; x++) {
        dkhash_insert(t, strs[x], NULL, strs[x]);
    }
    for (x = 0; x < 10; x++) {
        p1 = strs[x];
        p2 = dkhash_remove(t, p1, NULL);
        test(p1 == p2, "remove should return a value");
    }
    r2 = t_end();
    ret = r2 ? r2 : ret;
    t_reset();

    t_start("dkhash single bucket add remove backward");

    t = dkhash_create(1);
    for (x = 0; x < 10; x++) {
        dkhash_insert(t, strs[x], NULL, strs[x]);
    }
    for (x = 9; x; x--) {
        p1 = strs[x];
        p2 = dkhash_remove(t, p1, NULL);
        test(p1 == p2, "remove should return a value");
    }

    dkhash_destroy(t);

    r2 = t_end();
    return r2 ? r2 : ret;
}
Example #11
0
File: psd.c Project: lianqiw/maos
/**
   Add two PSDs. The first column of each dmat is the frequency nu, and the
   second column is PSD*/
dmat *add_psd(const dmat *psd1, const dmat *psd2, double scale2){
    dmat *out=ddup(psd1);
    add_psd2(&out, psd2, scale2);
    return out;
}