2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/utility/smalloc.h"
46 #include "gromacs/fileio/xvgr.h"
47 #include "gromacs/utility/futil.h"
49 #include "gromacs/math/vec.h"
51 #include "gromacs/utility/fatalerror.h"
53 const int nfp_ffn[effnNR] = { 0, 1, 2, 3, 2, 5, 7, 9, 4, 3};
55 const char *s_ffn[effnNR+2] = {
56 NULL, "none", "exp", "aexp", "exp_exp", "vac",
57 "exp5", "exp7", "exp9", "erffit", NULL, NULL
59 /* We don't allow errest as a choice on the command line */
61 const char *longs_ffn[effnNR] = {
65 "y = a2 exp(-x/a1) + (1-a2) exp(-x/a3)",
66 "y = exp(-v) (cosh(wv) + 1/w sinh(wv)), v = x/(2 a1), w = sqrt(1 - a2)",
67 "y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5",
68 "y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7",
69 "y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7 exp(-x/a8) + a9",
70 "y = 1/2*(a1+a2) - 1/2*(a1-a2)*erf( (x-a3) /a4^2)",
71 "y = a2*ee(a1,x) + (1-a2)*ee(a2,x)"
74 extern gmx_bool mrqmin_new(real x[], real y[], real sig[], int ndata, real a[],
75 int ia[], int ma, real **covar, real **alpha, real *chisq,
76 void (*funcs)(real, real [], real *, real []),
79 static real myexp(real x, real A, real tau)
81 if ((A == 0) || (tau == 0))
88 void erffit (real x, real a[], real *y, real dyda[])
91 * y=(a1+a2)/2-(a1-a2)/2*erf((x-a3)/a4^2)
99 erfarg = (x-a[3])/(a[4]*a[4]);
100 erfarg2 = erfarg*erfarg;
101 erfval = gmx_erf(erfarg)/2;
102 derf = M_2_SQRTPI*(a[1]-a[2])/2*exp(-erfarg2)/(a[4]*a[4]);
103 *y = (a[1]+a[2])/2-(a[1]-a[2])*erfval;
104 dyda[1] = 1/2-erfval;
105 dyda[2] = 1/2+erfval;
107 dyda[4] = 2*derf*erfarg;
112 static void exp_one_parm(real x, real a[], real *y, real dyda[])
124 dyda[1] = x*e1/sqr(a[1]);
127 static void exp_two_parm(real x, real a[], real *y, real dyda[])
139 dyda[1] = x*a[2]*e1/sqr(a[1]);
143 static void exp_3_parm(real x, real a[], real *y, real dyda[])
147 * y = a2 exp(-x/a1) + (1-a2) exp(-x/a3)
155 *y = a[2]*e1 + (1-a[2])*e2;
156 dyda[1] = x*a[2]*e1/sqr(a[1]);
158 dyda[3] = x*(1-a[2])*e2/sqr(a[3]);
161 static void exp_5_parm(real x, real a[], real *y, real dyda[])
165 * y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5
173 *y = a[1]*e1 + a[3]*e2 + a[5];
177 fprintf(debug, "exp_5_parm called: x = %10.3f y = %10.3f\n"
178 "a = ( %8.3f %8.3f %8.3f %8.3f %8.3f)\n",
179 x, *y, a[1], a[2], a[3], a[4], a[5]);
182 dyda[2] = x*e1/sqr(a[2]);
184 dyda[4] = x*e2/sqr(a[4]);
188 static void exp_7_parm(real x, real a[], real *y, real dyda[])
192 * y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7
201 *y = a[1]*e1 + a[3]*e2 + a[5]*e3 + a[7];
204 dyda[2] = x*e1/sqr(a[2]);
206 dyda[4] = x*e2/sqr(a[4]);
208 dyda[6] = x*e3/sqr(a[6]);
212 static void exp_9_parm(real x, real a[], real *y, real dyda[])
216 * y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7
226 *y = a[1]*e1 + a[3]*e2 + a[5]*e3 + a[7]*e4 + a[9];
229 dyda[2] = x*e1/sqr(a[2]);
231 dyda[4] = x*e2/sqr(a[4]);
233 dyda[6] = x*e3/sqr(a[6]);
235 dyda[8] = x*e4/sqr(a[8]);
239 static void vac_2_parm(real x, real a[], real *y, real dyda[])
243 * y = 1/2 (1 - 1/w) exp(-(1+w)v) + 1/2 (1 + 1/w) exp(-(1-w)v)
245 * = exp(-v) (cosh(wv) + 1/w sinh(wv))
250 * For tranverse current autocorrelation functions:
252 * a2 = 4 tau (eta/rho) k^2
256 double v, det, omega, omega2, em, ec, es;
264 omega = sqrt(omega2);
267 ec = em*0.5*(exp(omega*v)+exp(-omega*v));
268 es = em*0.5*(exp(omega*v)-exp(-omega*v))/omega;
272 ec = em*cos(omega*v);
273 es = em*sin(omega*v)/omega;
276 dyda[2] = (v/det*ec+(v-1/det)*es)/(-2.0);
277 dyda[1] = (1-det)*v/a[1]*es;
282 dyda[2] = -v*v*em*(0.5+v/6);
283 dyda[1] = v*v/a[1]*em;
287 static void errest_3_parm(real x, real a[], real *y, real dyda[])
293 e1 = exp(-x/a[1]) - 1;
301 e2 = exp(-x/a[3]) - 1;
310 v1 = 2*a[1]*(e1*a[1]/x + 1);
311 v2 = 2*a[3]*(e2*a[3]/x + 1);
312 *y = a[2]*v1 + (1-a[2])*v2;
313 dyda[1] = 2* a[2] *(v1/a[1] + e1);
314 dyda[3] = 2*(1 - a[2])*(v2/a[3] + e2);
326 typedef void (*myfitfn)(real x, real a[], real *y, real dyda[]);
327 myfitfn mfitfn[effnNR] = {
328 exp_one_parm, exp_one_parm, exp_two_parm, exp_3_parm, vac_2_parm,
329 exp_5_parm, exp_7_parm, exp_9_parm, erffit, errest_3_parm
332 real fit_function(int eFitFn, real *parm, real x)
334 static real y, dum[8];
336 mfitfn[eFitFn](x, parm-1, &y, dum);
341 /* lmfit_exp supports up to 3 parameter fitting of exponential functions */
342 static gmx_bool lmfit_exp(int nfit, real x[], real y[], real dy[], real ftol,
343 real parm[], real dparm[], gmx_bool bVerbose,
346 real chisq, ochisq, alamda;
347 real *a, **covar, **alpha, *dum;
349 int i, j, ma, mfit, *lista, *ia;
351 if ((eFitFn < 0) || (eFitFn >= effnNR))
353 gmx_fatal(FARGS, "fitfn = %d, should be in 0..%d (%s,%d)",
354 effnNR-1, eFitFn, __FILE__, __LINE__);
357 ma = mfit = nfp_ffn[eFitFn]; /* number of parameters to fit */
364 for (i = 1; (i < ma+1); i++)
367 ia[i] = 1; /* fixed bug B.S.S 19/11 */
368 snew(covar[i], ma+1);
369 snew(alpha[i], ma+1);
375 fprintf(stderr, "Will keep parameters fixed during fit procedure: %d\n",
378 for (i = 0; i < ma; i++)
388 fprintf(debug, "%d parameter fit\n", mfit);
392 alamda = -1; /* Starting value */
394 for (i = 0; (i < mfit); i++)
402 fprintf(stderr, "%4s %10s %10s %10s %10s %10s %10s\n",
403 "Step", "chi^2", "Lambda", "A1", "A2", "A3", "A4");
408 /* mrqmin(x-1,y-1,dy-1,nfit,a,ma,lista,mfit,covar,alpha,
409 * &chisq,expfn[mfit-1],&alamda)
411 if (!mrqmin_new(x-1, y-1, dy-1, nfit, a, ia, ma, covar, alpha, &chisq,
412 mfitfn[eFitFn], &alamda))
419 fprintf(stderr, "%4d %10.5e %10.5e %10.5e",
420 j, chisq, alamda, a[1]);
423 fprintf(stderr, " %10.5e", a[2]);
427 fprintf(stderr, " %10.5e", a[3]);
431 fprintf(stderr, " %10.5e", a[4]);
433 fprintf(stderr, "\n");
436 bCont = ((fabs(ochisq - chisq) > fabs(ftol*chisq)) ||
437 ((ochisq == chisq)));
439 while (bCont && (alamda != 0.0) && (j < 50));
442 fprintf(stderr, "\n");
445 /* Now get the covariance matrix out */
448 /* mrqmin(x-1,y-1,dy-1,nfit,a,ma,lista,mfit,covar,alpha,
449 * &chisq,expfn[mfit-1],&alamda)
451 if (!mrqmin_new(x-1, y-1, dy-1, nfit, a, ia, ma, covar, alpha, &chisq,
452 mfitfn[eFitFn], &alamda))
457 for (j = 0; (j < mfit); j++)
460 dparm[j] = covar[j+1][j+1];
463 for (i = 0; (i < ma+1); i++)
477 real do_lmfit(int ndata, real c1[], real sig[], real dt, real x0[],
478 real begintimefit, real endtimefit, const output_env_t oenv,
479 gmx_bool bVerbose, int eFitFn, real fitparms[], int fix)
484 int i, j, nparm, nfitpnts;
490 nparm = nfp_ffn[eFitFn];
493 fprintf(debug, "There are %d points to fit %d vars!\n", ndata, nparm);
494 fprintf(debug, "Fit to function %d from %g through %g, dt=%g\n",
495 eFitFn, begintimefit, endtimefit, dt);
503 for (i = 0; i < ndata; i++)
505 ttt = x0 ? x0[i] : dt*i;
506 if (ttt >= begintimefit && ttt <= endtimefit)
511 /* mrqmin does not like sig to be zero */
522 fprintf(debug, "j= %d, i= %d, x= %g, y= %g, dy= %g\n",
523 j, i, x[j], y[j], dy[j]);
530 if (nfitpnts < nparm)
532 fprintf(stderr, "Not enough data points for fitting!\n");
540 for (i = 0; (i < nparm); i++)
542 parm[i] = fitparms[i];
546 if (!lmfit_exp(nfitpnts, x, y, dy, ftol, parm, dparm, bVerbose, eFitFn, fix))
548 fprintf(stderr, "Fit failed!\n");
552 /* Compute the integral from begintimefit */
555 integral = (parm[0]*myexp(begintimefit, parm[1], parm[0]) +
556 parm[2]*myexp(begintimefit, 1-parm[1], parm[2]));
560 integral = parm[0]*myexp(begintimefit, parm[1], parm[0]);
564 integral = parm[0]*myexp(begintimefit, 1, parm[0]);
568 gmx_fatal(FARGS, "nparm = %d in file %s, line %d",
569 nparm, __FILE__, __LINE__);
572 /* Generate THE output */
575 fprintf(stderr, "FIT: # points used in fit is: %d\n", nfitpnts);
576 fprintf(stderr, "FIT: %21s%21s%21s\n",
577 "parm0 ", "parm1 (ps) ", "parm2 (ps) ");
578 fprintf(stderr, "FIT: ------------------------------------------------------------\n");
579 fprintf(stderr, "FIT: %8.3g +/- %8.3g%9.4g +/- %8.3g%8.3g +/- %8.3g\n",
580 parm[0], dparm[0], parm[1], dparm[1], parm[2], dparm[2]);
581 fprintf(stderr, "FIT: Integral (calc with fitted function) from %g ps to inf. is: %g\n",
582 begintimefit, integral);
584 sprintf(buf, "test%d.xvg", nfitpnts);
585 fp = xvgropen(buf, "C(t) + Fit to C(t)", "Time (ps)", "C(t)", oenv);
586 fprintf(fp, "# parm0 = %g, parm1 = %g, parm2 = %g\n",
587 parm[0], parm[1], parm[2]);
588 for (j = 0; (j < nfitpnts); j++)
590 ttt = x0 ? x0[j] : dt*j;
591 fprintf(fp, "%10.5e %10.5e %10.5e\n",
592 ttt, c1[j], fit_function(eFitFn, parm, ttt));
599 for (i = 0; (i < nparm); i++)
601 fitparms[i] = parm[i];
615 void do_expfit(int ndata, real c1[], real dt, real begintimefit, real endtimefit)
619 real aa, bb, saa, sbb, A, tau, dA, dtau;
621 fprintf(stderr, "Will fit data from %g (ps) to %g (ps).\n",
622 begintimefit, endtimefit);
624 snew(x, ndata); /* allocate the maximum necessary space */
629 for (i = 0; (i < ndata); i++)
631 if ( (dt*i >= begintimefit) && (dt*i <= endtimefit) )
636 fprintf(stderr, "n= %d, i= %d, x= %g, y= %g\n", n, i, x[n], y[n]);
640 fprintf(stderr, "# of data points used in the fit is : %d\n\n", n);
641 expfit(n, x, y, Dy, &aa, &saa, &bb, &sbb);
647 fprintf(stderr, "Fitted to y=exp(a+bx):\n");
648 fprintf(stderr, "a = %10.5f\t b = %10.5f", aa, bb);
649 fprintf(stderr, "\n");
650 fprintf(stderr, "Fitted to y=Aexp(-x/tau):\n");
651 fprintf(stderr, "A = %10.5f\t tau = %10.5f\n", A, tau);
652 fprintf(stderr, "dA = %10.5f\t dtau = %10.5f\n", dA, dtau);
656 void expfit(int n, real *x, real *y, real *Dy, real *a, real *sa,
659 real *w, *ly, A, SA, B, SB;
661 real sum, xbar, ybar, Sxx, Sxy, wr2, chi2, gamma, Db;
668 #define sqr(x) ((x)*(x))
674 /* Calculate weights and values of ln(y). */
675 for (i = 0; (i < n); i++)
677 w[i] = sqr(y[i]/Dy[i]);
681 /* The weighted averages of x and y: xbar and ybar. */
685 for (i = 0; (i < n); i++)
694 /* The centered product sums Sxx and Sxy, and hence A and B. */
697 for (i = 0; (i < n); i++)
699 Sxx += w[i]*sqr(x[i]-xbar);
700 Sxy += w[i]*(x[i]-xbar)*(ly[i]-ybar);
705 /* Chi-squared (chi2) and gamma. */
708 for (i = 0; (i < n); i++)
710 wr2 = w[i]*sqr(ly[i]-A-B*x[i]);
712 gamma += wr2*(x[i]-xbar);
715 /* Refined values of A and B. Also SA and SB. */
716 Db = -ONEP5*gamma/Sxx;
718 A -= ONEP5*chi2/sum-xbar*Db;
719 SB = sqrt((chi2/(n-2))/Sxx);
720 SA = SB*sqrt(Sxx/sum+sqr(xbar));