double react_system(struct sys33* sys) { double rnd = 1.0 - ((double)rand() / RAND_MAX); // interval: (0.0, 1.0] double dt = (-1.0) * log(rnd) / sys->props_tot; int next_react = react_search(sys); react_execute(sys, next_react); calc_props(sys); return dt; }
struct sys33* init_system(char** argv) { struct sys33* sys = calloc(1, sizeof(struct sys33)); if (sys == NULL) { printf("init_system() error: cannot allocate memory\n"); exit(EXIT_FAILURE); } for (int i = 0; i < NREACT; i++) { sys->rates[i] = strtod(argv[i + 1], NULL); } sys->W = strtod(argv[NREACT + 1], NULL); sys->X = strtod(argv[NREACT + 2], NULL); sys->Y = strtod(argv[NREACT + 3], NULL); sys->Z = strtod(argv[NREACT + 4], NULL); calc_props(sys); return sys; }
static double find_peak(float inf, fftview * fv, fftpart * fp) { int ii, lobin, hibin, maxbin = 0; float maxpow = 0.0; double inr, viewfrac = 0.05, newmaxr, newmaxz; rderivs derivs; fourierprops props; inr = inf * T; lobin = inr - (fv->numbins * 0.5 * viewfrac); hibin = inr + (fv->numbins * 0.5 * viewfrac); for (ii = lobin - fp->rlo; ii < hibin - fp->rlo + 1; ii++) { if (fp->rawpowers[ii] > maxpow) { maxpow = fp->rawpowers[ii]; maxbin = ii + fp->rlo; } } maxpow = max_rz_arr(fp->amps, fp->numamps, maxbin, 0.0, &newmaxr, &newmaxz, &derivs); newmaxr += fp->rlo; calc_props(derivs, newmaxr, newmaxz, 0.0, &props); print_candidate(&props, idata.dt, N, r0, 2); return newmaxr; }
void output_harmonics(GSList * list, accelobs * obs, infodata * idata) { int ii, jj, numcols = 13, numcands; int widths[13] = { 5, 4, 5, 15, 11, 18, 13, 12, 9, 12, 10, 10, 20 }; int errors[13] = { 0, 0, 0, 2, 0, 2, 0, 2, 0, 2, 2, 2, 0 }; char tmpstr[30], ctrstr[30], notes[21], *command; accelcand *cand; GSList *listptr; fourierprops props; rzwerrs errs; static char *titles1[] = { "", "", "", "Power /", "Raw", "FFT 'r'", "Pred 'r'", "FFT 'z'", "Pred 'z'", "Phase", "Centroid", "Purity", "" }; static char *titles2[] = { "Cand", "Harm", "Sigma", "Loc Pow", "Power", "(bin)", "(bin)", "(bins)", "(bins)", "(rad)", "(0-1)", "<p> = 1", "Notes" }; numcands = g_slist_length(list); listptr = list; /* Print the header */ for (ii = 0; ii < numcols - 1; ii++) { center_string(ctrstr, titles1[ii], widths[ii]); fprintf(obs->workfile, "%s ", ctrstr); } center_string(ctrstr, titles1[ii], widths[ii]); fprintf(obs->workfile, "%s\n", ctrstr); for (ii = 0; ii < numcols - 1; ii++) { if (obs->nph > 0.0 && ii == 3) /* HAAACK!!! */ center_string(ctrstr, "NumPhot", widths[ii]); else center_string(ctrstr, titles2[ii], widths[ii]); fprintf(obs->workfile, "%s ", ctrstr); } center_string(ctrstr, titles2[ii], widths[ii]); fprintf(obs->workfile, "%s\n", ctrstr); for (ii = 0; ii < numcols - 1; ii++) { memset(tmpstr, '-', widths[ii]); tmpstr[widths[ii]] = '\0'; fprintf(obs->workfile, "%s--", tmpstr); } memset(tmpstr, '-', widths[ii]); tmpstr[widths[ii]] = '\0'; fprintf(obs->workfile, "%s\n", tmpstr); /* Print the fundamentals */ for (ii = 0; ii < numcands; ii++) { cand = (accelcand *) (listptr->data); for (jj = 0; jj < cand->numharm; jj++) { if (obs->nph > 0.0) { double tmp_locpow; tmp_locpow = cand->derivs[jj].locpow; cand->derivs[jj].locpow = obs->nph; calc_props(cand->derivs[jj], cand->hirs[jj], cand->hizs[jj], 0.0, &props); cand->derivs[jj].locpow = tmp_locpow; } else { calc_props(cand->derivs[jj], cand->hirs[jj], cand->hizs[jj], 0.0, &props); } calc_rzwerrs(&props, obs->T, &errs); comp_psr_to_cand(&props, idata, notes, 0); if (jj == 0) sprintf(tmpstr, " %-4d", ii + 1); else sprintf(tmpstr, " "); center_string(ctrstr, tmpstr, widths[0]); fprintf(obs->workfile, "%s ", ctrstr); sprintf(tmpstr, "%-4d", jj + 1); center_string(ctrstr, tmpstr, widths[1]); fprintf(obs->workfile, "%s ", ctrstr); sprintf(tmpstr, "%.2f", candidate_sigma(props.pow, 1, 1)); center_string(ctrstr, tmpstr, widths[2]); fprintf(obs->workfile, "%s ", ctrstr); write_val_with_err(obs->workfile, props.pow, props.powerr, errors[3], widths[3]); sprintf(tmpstr, "%.3g", props.rawpow); center_string(ctrstr, tmpstr, widths[4]); fprintf(obs->workfile, "%s ", ctrstr); write_val_with_err(obs->workfile, props.r, props.rerr, errors[5], widths[5]); sprintf(tmpstr, "%.2f", cand->r * (jj + 1)); center_string(ctrstr, tmpstr, widths[6]); fprintf(obs->workfile, "%s ", ctrstr); write_val_with_err(obs->workfile, props.z, props.zerr, errors[7], widths[7]); sprintf(tmpstr, "%.2f", cand->z * (jj + 1)); center_string(ctrstr, tmpstr, widths[8]); fprintf(obs->workfile, "%s ", ctrstr); write_val_with_err(obs->workfile, props.phs, props.phserr, errors[9], widths[9]); write_val_with_err(obs->workfile, props.cen, props.cenerr, errors[10], widths[10]); write_val_with_err(obs->workfile, props.pur, props.purerr, errors[11], widths[11]); fprintf(obs->workfile, " %.20s\n", notes); fflush(obs->workfile); } listptr = listptr->next; } fprintf(obs->workfile, "\n\n"); fclose(obs->workfile); command = malloc(strlen(obs->rootfilenm) + strlen(obs->accelnm) + 20); sprintf(command, "cat %s.inf >> %s", obs->rootfilenm, obs->accelnm); system(command); free(command); }
int main(int argc, char *argv[]) { FILE *fftfile; double flook, dt, nph, t, maxz, pwr, hipow = 0.0; double zlo = -30.0, zhi = 30.0, dr, dz = 2.0; double hir = 0.0, hiz = 0.0, newhir, newhiz; fcomplex **ffdotplane, *data; float powargr, powargi; int startbin, numdata, nextbin, nr, nz, numkern; int i, j, realpsr, kernel_half_width, numbetween = 4; int n, corrsize = 1024; char filenm[80], compare[200]; rderivs derivs; fourierprops props; infodata idata; struct tms runtimes; double ttim, utim, stim, tott; tott = times(&runtimes) / (double) CLK_TCK; if (argc != 3) { printf("\nUsage: 'quicklook filename fftfreq dt'\n\n"); printf(" 'filename' = a string containing the FFT file's name.\n"); printf(" (do not include the '.fft' suffix)\n"); printf(" 'fftfreq' = the central fourier frequency to examine.\n"); printf(" Quicklook will search a region of the f-fdot plane\n"); printf(" of a file containing a long, single precision FFT\n"); printf(" using the Correlation method (i.e. Ransom and \n"); printf(" Eikenberry, 1997, unpublished as of yet).\n"); printf(" The search uses a spacing of 0.5 frequency bins in\n"); printf(" the fourier frequency (r) direction, and 2 'bins' in\n"); printf(" the fdot (z) direction. The routine will output\n"); printf(" statistics for the best candidate in the region.\n\n"); printf(" The routine was written as a quick but useful hack\n"); printf(" to show the power of the Correlation method, and the\n"); printf(" forthcoming power of Scott Ransom's Pulsar Finder\n"); printf(" Software.\n"); printf(" 2 March 2001\n\n"); exit(0); } printf("\n\n"); printf(" Quick-Look Pulsation Search\n"); printf(" With database lookup.\n"); printf(" by Scott M. Ransom\n"); printf(" 2 March, 2001\n\n"); /* Initialize our data: */ sprintf(filenm, "%s.fft", argv[1]); readinf(&idata, argv[1]); flook = atof(argv[2]); dt = idata.dt; dr = 1.0 / (double) numbetween; fftfile = chkfopen(filenm, "r"); nph = get_numphotons(fftfile); n = chkfilelen(fftfile, sizeof(float)); t = n * dt; nz = (int) ((zhi - zlo) / dz) + 1; /* Determine our starting frequency and get the data */ maxz = (fabs(zlo) < fabs(zhi)) ? zhi : zlo; kernel_half_width = z_resp_halfwidth(maxz, HIGHACC); numkern = 2 * numbetween * kernel_half_width; while (numkern > 2 * corrsize) corrsize *= 2; startbin = (int) (flook) - corrsize / (2 * numbetween); numdata = corrsize / numbetween; data = read_fcomplex_file(fftfile, startbin, numdata); /* Do the f-fdot plane correlations: */ ffdotplane = corr_rz_plane(data, numdata, numbetween, kernel_half_width, zlo, zhi, nz, corrsize, LOWACC, &nextbin); nr = corrsize - 2 * kernel_half_width * numbetween; /* Search the resulting data set: */ for (i = 0; i < nz; i++) { for (j = 0; j < nr; j++) { pwr = POWER(ffdotplane[i][j].r, ffdotplane[i][j].i); if (pwr > hipow) { hir = j * dr + kernel_half_width; hiz = i * dz + zlo; hipow = pwr; } } } /* Maximize the best candidate: */ hipow = max_rz_arr(data, numdata, hir, hiz, &newhir, &newhiz, &derivs); newhir += startbin; calc_props(derivs, newhir, newhiz, 0.0, &props); printf("Searched %d pts ", nz * nr); printf("(r: %.1f to %.1f, ", (double) startbin + kernel_half_width, (double) startbin + kernel_half_width + dr * (nr - 1)); printf("z: %.1f to %.1f)\n\n", zlo, zhi); printf("Timing summary:\n"); tott = times(&runtimes) / (double) CLK_TCK - tott; utim = runtimes.tms_utime / (double) CLK_TCK; stim = runtimes.tms_stime / (double) CLK_TCK; ttim = utim + stim; printf(" CPU time: %.3f sec (User: %.3f sec, System: %.3f sec)\n", ttim, utim, stim); printf(" Total time: %.3f sec\n\n", tott); printf("The best candidate is:\n"); print_candidate(&props, dt, n, nph, 2); realpsr = comp_psr_to_cand(&props, &idata, compare, 1); printf("%s\n", compare); fclose(fftfile); /* Cleanup and exit */ free(ffdotplane[0]); free(ffdotplane); free(data); exit(0); }
int main(int argc, char *argv[]) { int ii; double ttim, utim, stim, tott; struct tms runtimes; subharminfo **subharminfs; accelobs obs; infodata idata; GSList *cands = NULL; Cmdline *cmd; /* Prep the timer */ tott = times(&runtimes) / (double) CLK_TCK; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(1); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Fourier-Domain Acceleration Search Routine\n"); printf(" by Scott M. Ransom\n\n"); /* Create the accelobs structure */ create_accelobs(&obs, &idata, cmd, 1); /* Zap birdies if requested and if in memory */ if (cmd->zaplistP && !obs.mmap_file && obs.fft) { int numbirds; double *bird_lobins, *bird_hibins, hibin; /* Read the Standard bird list */ numbirds = get_birdies(cmd->zaplist, obs.T, cmd->baryv, &bird_lobins, &bird_hibins); /* Zap the birdies */ printf("Zapping them using a barycentric velocity of %.5gc.\n\n", cmd->baryv); hibin = obs.N / 2; for (ii = 0; ii < numbirds; ii++) { if (bird_lobins[ii] >= hibin) break; if (bird_hibins[ii] >= hibin) bird_hibins[ii] = hibin - 1; zapbirds(bird_lobins[ii], bird_hibins[ii], NULL, obs.fft); } free(bird_lobins); free(bird_hibins); } printf("Searching with up to %d harmonics summed:\n", 1 << (obs.numharmstages - 1)); printf(" f = %.1f to %.1f Hz\n", obs.rlo / obs.T, obs.rhi / obs.T); printf(" r = %.1f to %.1f Fourier bins\n", obs.rlo, obs.rhi); printf(" z = %.1f to %.1f Fourier bins drifted\n\n", obs.zlo, obs.zhi); /* Generate the correlation kernels */ printf("Generating correlation kernels:\n"); subharminfs = create_subharminfos(obs.numharmstages, (int) obs.zhi); printf("Done generating kernels.\n\n"); printf("Starting the search.\n"); /* Don't use the *.txtcand files on short in-memory searches */ if (!obs.dat_input) { printf(" Working candidates in a test format are in '%s'.\n\n", obs.workfilenm); } /* Start the main search loop */ { double startr = obs.rlo, lastr = 0, nextr = 0; ffdotpows *fundamental; while (startr + ACCEL_USELEN * ACCEL_DR < obs.highestbin) { /* Search the fundamental */ print_percent_complete(startr - obs.rlo, obs.highestbin - obs.rlo, "search", 0); nextr = startr + ACCEL_USELEN * ACCEL_DR; lastr = nextr - ACCEL_DR; fundamental = subharm_ffdot_plane(1, 1, startr, lastr, &subharminfs[0][0], &obs); cands = search_ffdotpows(fundamental, 1, &obs, cands); if (obs.numharmstages > 1) { /* Search the subharmonics */ int stage, harmtosum, harm; ffdotpows *subharmonic; for (stage = 1; stage < obs.numharmstages; stage++) { harmtosum = 1 << stage; for (harm = 1; harm < harmtosum; harm += 2) { subharmonic = subharm_ffdot_plane(harmtosum, harm, startr, lastr, &subharminfs[stage][harm - 1], &obs); add_ffdotpows(fundamental, subharmonic, harmtosum, harm); free_ffdotpows(subharmonic); } cands = search_ffdotpows(fundamental, harmtosum, &obs, cands); } } free_ffdotpows(fundamental); startr = nextr; } print_percent_complete(obs.highestbin - obs.rlo, obs.highestbin - obs.rlo, "search", 0); } printf("\n\nDone searching. Now optimizing each candidate.\n\n"); free_subharminfos(obs.numharmstages, subharminfs); { /* Candidate list trimming and optimization */ int numcands; GSList *listptr; accelcand *cand; fourierprops *props; numcands = g_slist_length(cands); if (numcands) { /* Sort the candidates according to the optimized sigmas */ cands = sort_accelcands(cands); /* Eliminate (most of) the harmonically related candidates */ if ((cmd->numharm > 1) && !(cmd->noharmremoveP)) eliminate_harmonics(cands, &numcands); /* Now optimize each candidate and its harmonics */ print_percent_complete(0, 0, NULL, 1); listptr = cands; for (ii = 0; ii < numcands; ii++) { print_percent_complete(ii, numcands, "optimization", 0); cand = (accelcand *) (listptr->data); optimize_accelcand(cand, &obs); listptr = listptr->next; } print_percent_complete(ii, numcands, "optimization", 0); /* Calculate the properties of the fundamentals */ props = (fourierprops *) malloc(sizeof(fourierprops) * numcands); listptr = cands; for (ii = 0; ii < numcands; ii++) { cand = (accelcand *) (listptr->data); /* In case the fundamental harmonic is not significant, */ /* send the originally determined r and z from the */ /* harmonic sum in the search. Note that the derivs are */ /* not used for the computations with the fundamental. */ calc_props(cand->derivs[0], cand->r, cand->z, 0.0, props + ii); /* Override the error estimates based on power */ props[ii].rerr = (float) (ACCEL_DR) / cand->numharm; props[ii].zerr = (float) (ACCEL_DZ) / cand->numharm; listptr = listptr->next; } /* Write the fundamentals to the output text file */ output_fundamentals(props, cands, &obs, &idata); /* Write the harmonics to the output text file */ output_harmonics(cands, &obs, &idata); /* Write the fundamental fourierprops to the cand file */ obs.workfile = chkfopen(obs.candnm, "wb"); chkfwrite(props, sizeof(fourierprops), numcands, obs.workfile); fclose(obs.workfile); free(props); printf("\n\n"); } else { printf("No candidates above sigma = %.2f were found.\n\n", obs.sigma); } } /* Finish up */ printf("Searched the following approx numbers of independent points:\n"); printf(" %d harmonic: %9lld\n", 1, obs.numindep[0]); for (ii = 1; ii < obs.numharmstages; ii++) printf(" %d harmonics: %9lld\n", 1 << ii, obs.numindep[ii]); printf("\nTiming summary:\n"); tott = times(&runtimes) / (double) CLK_TCK - tott; utim = runtimes.tms_utime / (double) CLK_TCK; stim = runtimes.tms_stime / (double) CLK_TCK; ttim = utim + stim; printf(" CPU time: %.3f sec (User: %.3f sec, System: %.3f sec)\n", ttim, utim, stim); printf(" Total time: %.3f sec\n\n", tott); printf("Final candidates in binary format are in '%s'.\n", obs.candnm); printf("Final Candidates in a text format are in '%s'.\n\n", obs.accelnm); free_accelobs(&obs); g_slist_foreach(cands, free_accelcand, NULL); g_slist_free(cands); return (0); }