TEST(testEndToEnd,checkIdeal){ pulsar _psr; pulsar *psr = &_psr; MAX_PSR=1; char timFile[MAX_PSR][MAX_FILELEN],parFile[MAX_PSR][MAX_FILELEN]; char** argv=NULL; int npsr=1; initialise(psr,0); /* Initialise all */ strcpy(parFile[0],DATDIR "/test3.par"); strcpy(timFile[0],DATDIR "/test3.tim"); readParfile(psr,parFile,timFile,npsr); readTimfile(psr,timFile,npsr); preProcessSimple(psr); psr->noWarnings=2; formBatsAll(psr,npsr); formResiduals(psr,npsr,0); for(int iobs = 0; iobs < psr->nobs; iobs++){ ASSERT_LT(static_cast<double>(fabsl(psr->obsn[iobs].residual)),TEST_DELTA) << DATDIR "/test3.par test3.tim do not give idealised ToAs"; } doFitAll(psr,npsr,"NULL"); formBatsAll(psr,npsr); formResiduals(psr,npsr,0); for(int iobs = 0; iobs < psr->nobs; iobs++){ ASSERT_LT(static_cast<double>(fabsl(psr->obsn[iobs].residual)),TEST_DELTA) << "Fitting has caused error in ideal"; } }
TEST(testEndToEnd,gracefulBadFit){ pulsar _psr; pulsar *psr = &_psr; MAX_PSR=1; char timFile[MAX_PSR][MAX_FILELEN],parFile[MAX_PSR][MAX_FILELEN]; char** argv=NULL; int npsr=1; initialise(psr,0); /* Initialise all */ strcpy(parFile[0],DATDIR "/test3c.par"); strcpy(timFile[0],DATDIR "/test3.tim"); readParfile(psr,parFile,timFile,npsr); readTimfile(psr,timFile,npsr); preProcessSimple(psr); psr->noWarnings=2; formBatsAll(psr,npsr); formResiduals(psr,npsr,0); TK_errorCount=0; doFitAll(psr,npsr,"NULL"); // ASSERT_GT(TK_errorCount,1u); ASSERT_EQ(1,1); TK_errorCount=0; }
void callFit(pulsar *psr,int npsr) { int iteration; double globalParameter = 0.0; for (iteration=0;iteration<2;iteration++) { formBatsAll(psr,npsr); formResiduals(psr,npsr,1); /* Do the fitting */ if (iteration==0) doFitAll(psr,npsr,covarFuncFile2); else textOutput(psr,npsr,globalParameter,0,0,0,""); } }
/* The main function has to be called this, even if no * graphics are involved ;) */ extern "C" int graphicalInterface(int argc, char *argv[], pulsar *psr, int *npsr) { char parFile[MAX_PSR][MAX_FILELEN]; char timFile[MAX_PSR][MAX_FILELEN]; *npsr = 1; // Necessary, I guess? printf("Chi2 grid plugin for tempo2\n"); printf("Author: Paul Demorest\n"); printf("Version: 0.0\n"); parFile[0][0]='\0'; timFile[0][0]='\0'; // Parse command line.. there has to be a better way to do this.. double m2_min = 0.0, m2_max = 2.0; double sini_min = 0.5, sini_max = 1.0; int m2_steps = 128, sini_steps = 128; for (unsigned i=1; i<argc; i++) { if (strcmp(argv[i], "-f")==0) { strcpy(parFile[0], argv[i+1]); strcpy(timFile[0], argv[i+2]); } else if (strcmp(argv[i], "-sini_min")==0) { sini_min = atof(argv[i+1]); } else if (strcmp(argv[i], "-sini_max")==0) { sini_max = atof(argv[i+1]); } else if (strcmp(argv[i], "-sini_steps")==0) { sini_steps = atoi(argv[i+1]); } else if (strcmp(argv[i], "-m2_min")==0) { m2_min = atof(argv[i+1]); } else if (strcmp(argv[i], "-m2_max")==0) { m2_max = atof(argv[i+1]); } else if (strcmp(argv[i], "-m2_steps")==0) { m2_steps = atoi(argv[i+1]); } } if (parFile[0][0]=='\0' || timFile[0][0]=='\0') { fprintf(stderr, "Please provide .par and .tim files using -f\n"); fprintf(stderr, "Optional arguments:\n"); fprintf(stderr, " -sini_min N Minimum sin(i) value (0.5)\n"); fprintf(stderr, " -sini_max N Maximum sin(i) value (1.0)\n"); fprintf(stderr, " -sini_steps N Number of sin(i) steps (128)\n"); fprintf(stderr, " -m2_min N Minimum m2 value (0.0)\n"); fprintf(stderr, " -m2_max N Maximum m2 value (2.0)\n"); fprintf(stderr, " -m2_steps N Number of m2 steps (128)\n"); exit(1); } // Read the stuff readParfile(psr, parFile, timFile, *npsr); readTimfile(psr, timFile, *npsr); preProcess(psr, *npsr, argc, argv); // Do an initial fit to get best-fit params to use as a starting // place. formBatsAll(psr, *npsr); formResiduals(psr, *npsr, 0); // Final arg is "removeMean" doFit(psr, *npsr, 0); // Final arg is "writeModel" formBatsAll(psr, *npsr); // Run these again to get post-fit formResiduals(psr, *npsr, 0); // residuals. // Get initial chi2, etc. double init_chi2 = psr_chi2(psr); // step size, etc double delta_m2 = (m2_max - m2_min)/(double)m2_steps; double delta_sini = (sini_max - sini_min)/(double)sini_steps; // Store current parameters in psr[1], trial params in psr[0] store_params(psr); // Print header lines printf("# chi2_0=%.5e\n", init_chi2); printf("# SINI M2 chi2-chi2_0\n"); // Main iteration loop signal(SIGINT, cc); double cur_m2, cur_sini, cur_chi2; for (cur_sini=sini_min; cur_sini<sini_max; cur_sini+=delta_sini) { for (cur_m2=m2_min; cur_m2<m2_max; cur_m2+=delta_m2) { reset_params(psr); psr[0].param[param_sini].val[0] = cur_sini; psr[0].param[param_sini].paramSet[0] = 1; psr[0].param[param_m2].val[0] = cur_m2; psr[0].param[param_m2].paramSet[0] = 1; // updateBats or FormBats? int ok=1; updateBatsAll(psr, *npsr); try { formResiduals(psr, *npsr, 0); doFit(psr, *npsr, 0); } catch (int e) { // Note, this is a hack to ignore spots in // M2-sini parameter space where the fit does not // converge. Plain tempo2 exit()'s here, killing // the whole process. I've modified my copy to // throw an error instead so we can continue on // with the gridding. if (e==5) { ok=0; } } if (ok) { updateBatsAll(psr, *npsr); formResiduals(psr, *npsr, 0); cur_chi2 = psr_chi2(psr); } else { cur_chi2 = 1e6; } printf("%.9f %.5f %.10e\n", cur_sini, cur_m2, cur_chi2-init_chi2); if (run==0) break; } if (run==0) break; } }
extern "C" int graphicalInterface(int argc,char *argv[],pulsar *psr,int *npsr) { char parFile[MAX_PSR][MAX_FILELEN]; char timFile[MAX_PSR][MAX_FILELEN]; int i,nit,j,p; char fname[MAX_FILELEN]; double globalParameter; long double result; long seed = TKsetSeed(); // // For the output file // toasim_header_t* header; toasim_header_t* read_header; FILE* file; double offsets[MAX_OBSN]; // Will change to doubles - should use malloc char incdm=0; // include the dm error? // Create a set of corrections. toasim_corrections_t* corr = (toasim_corrections_t*)malloc(sizeof(toasim_corrections_t)); corr->offsets=offsets; corr->params=""; // Normally leave as NULL. Can store this along with each realisation. // Same length string in every iteration - defined in r_param_length see below corr->a0=0; // constant corr->a1=0; // a1*x corr->a2=0; // a2*x*X *npsr = 0; nit = 1; printf("Graphical Interface: addGaussian\n"); printf("Author: G. Hobbs, M. Keith\n"); printf("Version: 1.0\n"); /* Obtain all parameters from the command line */ for (i=2;i<argc;i++) { if (strcmp(argv[i],"-incdm")==0){ incdm=1; printf("NOTICE: Including DM error in white noise\n"); } if (strcmp(argv[i],"-nreal")==0){ nit=atoi(argv[++i]); } if (strcmp(argv[i],"-f")==0) { strcpy(parFile[*npsr],argv[++i]); strcpy(timFile[*npsr],argv[++i]); (*npsr)++; } } readParfile(psr,parFile,timFile,*npsr); /* Load the parameters */ // Now read in all the .tim files readTimfile(psr,timFile,*npsr); /* Load the arrival times */ if(incdm){ preProcess(psr,*npsr,argc,argv); formBatsAll(psr,*npsr); } for (p=0;p<*npsr;p++) { if(incdm)updateDMvals(psr,p); printf("NTOA = %d\n",psr[p].nobs); header = toasim_init_header(); strcpy(header->short_desc,"addGaussian"); strcpy(header->invocation,argv[0]); strcpy(header->timfile_name,timFile[p]); strcpy(header->parfile_name,"Unknown"); header->idealised_toas="NotSet"; // What should this be header->orig_parfile="NA"; header->gparam_desc=""; // Global parameters header->gparam_vals=""; header->rparam_desc=""; // Desciprtion of the parameters header->rparam_len=0; // Size of the string header->seed = seed; header->ntoa = psr[p].nobs; header->nrealisations = nit; // First we write the header... sprintf(fname,"%s.addGauss",timFile[p]); file = toasim_write_header(header,fname); for (i=0;i<nit;i++) { if(i%10 == 0){ printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); printf("Iteration %d/%d",i+1,nit); fflush(stdout); } for (j=0;j<psr[p].nobs;j++) if(incdm) offsets[j] = (double)(psr[p].obsn[j].toaErr*1.0e-6*TKgaussDev(&seed)); else offsets[j] = (double)(psr[p].obsn[j].origErr*1.0e-6*TKgaussDev(&seed)); toasim_write_corrections(corr,header,file); } printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); printf("Iteration %d/%d\n",i,nit); printf("Close file\n"); fclose(file); } return 0; }
void polyco(pulsar *psr,int npsr,longdouble polyco_MJD1,longdouble polyco_MJD2,int nspan,int ncoeff, longdouble maxha,char *sitename,longdouble freq,longdouble coeff[MAX_COEFF],int trueDM,char* polyco_file) { longdouble tsid = 1.0L/1.002737909L; longdouble tmidMJD; double doppler,rms; int dspan; char binPhase[32]; int i; longdouble afmjd; longdouble val[800]; char date[12]; longdouble tmin[800]; double globalParameter,utc; int nsets; const char *CVS_verNum = "$Revision: 1.8 $"; /* buffers for three polyco outputs -- any input from the user is * prepended to the default file names */ char fname1[256]; char fname2[256]; char fname3[256]; strcpy(fname1,polyco_file); strcpy(fname2,polyco_file); strcpy(fname3,polyco_file); strcat(fname1,"polyco_new.dat"); strcat(fname2,"newpolyco.dat"); strcat(fname3,"polyco.tim"); /* erase any existing files */ fclose(fopen(fname1,"w")); fclose(fopen(fname2,"w")); /* special flag for barycenter polycos */ bool bary = strcmp(sitename,"@")==0 || strcasecmp(sitename,"bat")==0; if (displayCVSversion == 1) CVSdisplayVersion("polyco.C","polyco()",CVS_verNum); binPhase[0]='\0'; /* Set some defaults */ psr[0].param[param_track].paramSet[0]=0; globalParameter=0; // Zap the output files so we can append days to them later fclose(fopen("polyco_new.dat","w")); fclose(fopen("newpolyco.dat","w")); for (afmjd=polyco_MJD1; afmjd <= polyco_MJD2; afmjd+=tsid) { /* Create some arrival times spread throughout the day */ atimfake(psr,npsr,nspan,ncoeff,maxha,sitename,freq,afmjd,tmin,&tmidMJD,&dspan,&nsets,val); for (i=0;i<psr[0].nobs;i++) { psr->obsn[i].clockCorr=(!bary); psr->obsn[i].delayCorr=(!bary); psr->obsn[i].phaseOffset = 0.0; } /* Use reference phase TZR information unless at bary.*/ if (strcmp(psr->tzrsite,"@")!=0) { psr->obsn[0].clockCorr=1; psr->obsn[0].delayCorr=1; } else { psr->obsn[0].clockCorr=0; psr->obsn[0].delayCorr=0; } writeTim(fname3,psr,"tempo2"); formBatsAll(psr,npsr); formResiduals(psr,npsr,0); tzFit(psr,npsr,tmin,&doppler,&rms,&utc,tmidMJD,ncoeff,coeff,binPhase,nsets,afmjd,sitename,nspan, freq,date,val,trueDM,polyco_file); } if (psr->tempo1==0) printf("WARNING: Should probably use -tempo1 option\n"); }
extern "C" int pluginFitFunc(pulsar *psr,int npsr,int writeModel) { int p,i,k,j; int count=0; int flags[MAX_PSR]; longdouble* preobs[MAX_PSR]; printf(" <DMMODEL> Reset DM component of error to 0\n"); for (p=0; p < npsr; p++){ if (psr[p].param[param_dmmodel].fitFlag[0]==1){ flags[p]=1; count++; for(i=0; i < psr[p].nobs; i++){ psr[p].obsn[i].toaErr=psr[p].obsn[i].origErr; psr[p].obsn[i].toaDMErr=0; } } else flags[p]=0; } strcpy(psr[0].fitFunc,"default"); printf(" <DMMODEL> First fit...\n"); if (strcmp(dcmFile,"NULL")==0 && strcmp(covarFuncFile,"NULL")==0) doFit(psr,npsr,writeModel); else doFitDCM(psr,dcmFile,covarFuncFile,npsr,0); for (p=0; p < npsr; p++){ char** labels = (char**)malloc(sizeof(char*)*MAX_PARAMS); for (i=0;i<MAX_PARAMS;i++){ labels[i]= (char*)malloc(80); strcpy(labels[i],"UNK_PARAM"); } getFitLabels(psr,p,labels); double** cvm=psr[p].covar; int npol = psr[p].nFit - psr[p].fitNfree; bool warn=false; for (i=0;i<npol;i++){ for (j=0;j<i;j++){ double cv=fabs(cvm[i][j]/sqrt((cvm[j][j])*(cvm[i][i]))); if(cv > 0.5){ if(!warn){ printf(" <DMMODEL> Warning: highly covariant parameters in fit!\n"); warn=true; } printf(" % 15s % 15s %lg\n",labels[i],labels[j],cv); } } } for (i=0;i<MAX_PARAMS;i++)free(labels[i]); free(labels); } if(count){ printf(" <DMMODEL> Disable DMMODEL\n"); // we also save the pre-fit residuals so it appears as if only one fit has happened for (p=0; p < npsr; p++){ preobs[p] = (longdouble*)malloc(sizeof(longdouble)*psr[p].nobs); for(i=0; i < psr[p].nobs; i++){ preobs[p][i]=psr[p].obsn[i].prefitResidual; } psr[p].param[param_dmmodel].fitFlag[0]=0; } formBatsAll(psr,npsr); formResiduals(psr,npsr,0); printf(" <DMMODEL> Second Fit...\n"); if (strcmp(dcmFile,"NULL")==0 && strcmp(covarFuncFile,"NULL")==0) doFit(psr,npsr,writeModel); else doFitDCM(psr,dcmFile,covarFuncFile,npsr,0); printf(" <DMMODEL> Enable DMMODEL\n"); for (p=0; p < npsr; p++){ psr[p].param[param_dmmodel].fitFlag[0]=flags[p]; // restore the pre-fit residuals for(i=0; i < psr[p].nobs; i++){ psr[p].obsn[i].prefitResidual=preobs[p][i]; } } } strcpy(psr[0].fitFunc,"dmmodel"); return 0; }