aboutsummaryrefslogtreecommitdiff
path: root/src/fit.c
diff options
context:
space:
mode:
authortlatorre <tlatorre@uchicago.edu>2018-12-04 09:33:07 -0600
committertlatorre <tlatorre@uchicago.edu>2018-12-04 09:33:07 -0600
commit1b0c0eadaba36736b6d82f27668434ab23f6216e (patch)
tree66026ba81d7d935a7ba3b981ec972b61eff4de80 /src/fit.c
parent48753d15f3ae697bd44f6ba7ddeec60661470816 (diff)
downloadsddm-1b0c0eadaba36736b6d82f27668434ab23f6216e.tar.gz
sddm-1b0c0eadaba36736b6d82f27668434ab23f6216e.tar.bz2
sddm-1b0c0eadaba36736b6d82f27668434ab23f6216e.zip
add a command line parameter to control the maximum time of the fit
This commit adds a parameter to stop the fit if it takes longer than a certain period of time in seconds. This parameter can be set on the command line. For example, to limit fits to 10 minutes: $ ./fit FILENAME --max-time 600.0
Diffstat (limited to 'src/fit.c')
-rw-r--r--src/fit.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/fit.c b/src/fit.c
index b9b83d6..c519667 100644
--- a/src/fit.c
+++ b/src/fit.c
@@ -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;
}