diff options
-rw-r--r-- | src/fit.c | 27 |
1 files changed, 24 insertions, 3 deletions
@@ -5111,12 +5111,13 @@ void guess_direction(event *ev, double *pos, double *theta, double *phi) *phi = atan2(dir[1],dir[0]); } -int fit_event(event *ev, double *xopt, double *fmin, int id) +int fit_event(event *ev, double *xopt, double *fmin, int id, double maxtime) { size_t i; fitParams fpars; double x[9], ss[9], lb[9], ub[9], fval, n, qhs_sum, x0[9], T0, Tmin, mass; struct timeval tv_start, tv_stop; + double time_elapsed; opt = nlopt_create(NLOPT_LN_BOBYQA, 9); nlopt_set_min_objective(opt,nlopt_nll,&fpars); @@ -5319,20 +5320,34 @@ int fit_event(event *ev, double *xopt, double *fmin, int id) nlopt_set_ftol_abs(opt, 1e-5); nlopt_set_xtol_rel(opt, 1e-2); nlopt_set_maxeval(opt, 1000); + nlopt_set_maxtime(opt, maxtime); memcpy(x,xopt,sizeof(x)); + gettimeofday(&tv_start, NULL); nlopt_optimize(opt,x,&fval); + gettimeofday(&tv_stop, NULL); + + time_elapsed = tv_stop.tv_sec - tv_start.tv_sec + (tv_stop.tv_usec - tv_start.tv_usec)/1e6; + + if (time_elapsed > maxtime) goto end; if (stop) goto end; do { *fmin = fval; memcpy(xopt,x,sizeof(x)); + + nlopt_set_maxtime(opt, maxtime-time_elapsed); + + gettimeofday(&tv_start, NULL); nlopt_optimize(opt,x,&fval); + gettimeofday(&tv_stop, NULL); + + time_elapsed = tv_stop.tv_sec - tv_start.tv_sec + (tv_stop.tv_usec - tv_start.tv_usec)/1e6; if (stop) goto end; - } while (fval < *fmin && fabs(fval-*fmin) > 1e-2); + } while (fval < *fmin && fabs(fval-*fmin) > 1e-2 && time_elapsed < maxtime); if (fval < *fmin) { *fmin = fval; @@ -5353,6 +5368,8 @@ void usage(void) { fprintf(stderr,"Usage: ./fit [options] FILENAME\n"); fprintf(stderr," -o output file\n"); + fprintf(stderr," --max-time maximum time in seconds per fit\n" + " (default: 3600)\n"); fprintf(stderr," --skip-second-event only fit the first event after a " " MAST bank\n"); fprintf(stderr," -h display this help message\n"); @@ -5450,12 +5467,16 @@ int main(int argc, char **argv) struct timeval tv_start, tv_stop; long long elapsed; double fmin_best; + double maxtime = 3600.0; for (i = 1; i < argc; i++) { if (strlen(argv[i]) >= 2 && !strncmp(argv[i], "--", 2)) { if (!strcmp(argv[i]+2,"skip-second-event")) { skip_second_event = 1; continue; + } else if (!strcmp(argv[i]+2,"max-time")) { + maxtime = strtod(argv[++i],NULL); + continue; } } else if (argv[i][0] == '-') { switch (argv[i][1]) { @@ -5612,7 +5633,7 @@ int main(int argc, char **argv) for (i = 0; i < LEN(particles); i++) { gettimeofday(&tv_start, NULL); - if (fit_event(&ev,xopt,&fmin,particles[i]) == NLOPT_FORCED_STOP) { + if (fit_event(&ev,xopt,&fmin,particles[i],maxtime) == NLOPT_FORCED_STOP) { printf("ctrl-c caught. quitting...\n"); goto end; } |