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.
42 #include "gromacs/legacyheaders/typedefs.h"
43 #include "gromacs/utility/smalloc.h"
44 #include "gromacs/fileio/xvgr.h"
45 #include "gromacs/utility/futil.h"
47 #include "gromacs/math/vec.h"
49 #include "gromacs/utility/fatalerror.h"
51 const int nfp_ffn[effnNR] = { 0, 1, 2, 3, 2, 5, 7, 9, 4, 3};
53 const char *s_ffn[effnNR+2] = {
54 NULL, "none", "exp", "aexp", "exp_exp", "vac",
55 "exp5", "exp7", "exp9", "erffit", NULL, NULL
57 /* We don't allow errest as a choice on the command line */
59 const char *longs_ffn[effnNR] = {
63 "y = a2 exp(-x/a1) + (1-a2) exp(-x/a3)",
64 "y = exp(-v) (cosh(wv) + 1/w sinh(wv)), v = x/(2 a1), w = sqrt(1 - a2)",
65 "y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5",
66 "y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7",
67 "y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7 exp(-x/a8) + a9",
68 "y = 1/2*(a1+a2) - 1/2*(a1-a2)*erf( (x-a3) /a4^2)",
69 "y = a2*ee(a1,x) + (1-a2)*ee(a2,x)"
72 extern gmx_bool mrqmin_new(real x[], real y[], real sig[], int ndata, real a[],
73 int ia[], int ma, real **covar, real **alpha, real *chisq,
74 void (*funcs)(real, real [], real *, real []),
77 static real myexp(real x, real A, real tau)
79 if ((A == 0) || (tau == 0))
86 void erffit (real x, real a[], real *y, real dyda[])
89 * y=(a1+a2)/2-(a1-a2)/2*erf((x-a3)/a4^2)
97 erfarg = (x-a[3])/(a[4]*a[4]);
98 erfarg2 = erfarg*erfarg;
99 erfval = gmx_erf(erfarg)/2;
100 derf = M_2_SQRTPI*(a[1]-a[2])/2*exp(-erfarg2)/(a[4]*a[4]);
101 *y = (a[1]+a[2])/2-(a[1]-a[2])*erfval;
102 dyda[1] = 1/2-erfval;
103 dyda[2] = 1/2+erfval;
105 dyda[4] = 2*derf*erfarg;
110 static void exp_one_parm(real x, real a[], real *y, real dyda[])
122 dyda[1] = x*e1/sqr(a[1]);
125 static void exp_two_parm(real x, real a[], real *y, real dyda[])
137 dyda[1] = x*a[2]*e1/sqr(a[1]);
141 static void exp_3_parm(real x, real a[], real *y, real dyda[])
145 * y = a2 exp(-x/a1) + (1-a2) exp(-x/a3)
153 *y = a[2]*e1 + (1-a[2])*e2;
154 dyda[1] = x*a[2]*e1/sqr(a[1]);
156 dyda[3] = x*(1-a[2])*e2/sqr(a[3]);
159 static void exp_5_parm(real x, real a[], real *y, real dyda[])
163 * y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5
171 *y = a[1]*e1 + a[3]*e2 + a[5];
175 fprintf(debug, "exp_5_parm called: x = %10.3f y = %10.3f\n"
176 "a = ( %8.3f %8.3f %8.3f %8.3f %8.3f)\n",
177 x, *y, a[1], a[2], a[3], a[4], a[5]);
180 dyda[2] = x*e1/sqr(a[2]);
182 dyda[4] = x*e2/sqr(a[4]);
186 static void exp_7_parm(real x, real a[], real *y, real dyda[])
190 * y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7
199 *y = a[1]*e1 + a[3]*e2 + a[5]*e3 + a[7];
202 dyda[2] = x*e1/sqr(a[2]);
204 dyda[4] = x*e2/sqr(a[4]);
206 dyda[6] = x*e3/sqr(a[6]);
210 static void exp_9_parm(real x, real a[], real *y, real dyda[])
214 * y = a1 exp(-x/a2) + a3 exp(-x/a4) + a5 exp(-x/a6) + a7
224 *y = a[1]*e1 + a[3]*e2 + a[5]*e3 + a[7]*e4 + a[9];
227 dyda[2] = x*e1/sqr(a[2]);
229 dyda[4] = x*e2/sqr(a[4]);
231 dyda[6] = x*e3/sqr(a[6]);
233 dyda[8] = x*e4/sqr(a[8]);
237 static void vac_2_parm(real x, real a[], real *y, real dyda[])
241 * y = 1/2 (1 - 1/w) exp(-(1+w)v) + 1/2 (1 + 1/w) exp(-(1-w)v)
243 * = exp(-v) (cosh(wv) + 1/w sinh(wv))
248 * For tranverse current autocorrelation functions:
250 * a2 = 4 tau (eta/rho) k^2
254 double v, det, omega, omega2, em, ec, es;
262 omega = sqrt(omega2);
265 ec = em*0.5*(exp(omega*v)+exp(-omega*v));
266 es = em*0.5*(exp(omega*v)-exp(-omega*v))/omega;
270 ec = em*cos(omega*v);
271 es = em*sin(omega*v)/omega;
274 dyda[2] = (v/det*ec+(v-1/det)*es)/(-2.0);
275 dyda[1] = (1-det)*v/a[1]*es;
280 dyda[2] = -v*v*em*(0.5+v/6);
281 dyda[1] = v*v/a[1]*em;
285 static void errest_3_parm(real x, real a[], real *y, real dyda[])
291 e1 = exp(-x/a[1]) - 1;
299 e2 = exp(-x/a[3]) - 1;
308 v1 = 2*a[1]*(e1*a[1]/x + 1);
309 v2 = 2*a[3]*(e2*a[3]/x + 1);
310 *y = a[2]*v1 + (1-a[2])*v2;
311 dyda[1] = 2* a[2] *(v1/a[1] + e1);
312 dyda[3] = 2*(1 - a[2])*(v2/a[3] + e2);
324 typedef void (*myfitfn)(real x, real a[], real *y, real dyda[]);
325 myfitfn mfitfn[effnNR] = {
326 exp_one_parm, exp_one_parm, exp_two_parm, exp_3_parm, vac_2_parm,
327 exp_5_parm, exp_7_parm, exp_9_parm, erffit, errest_3_parm
330 real fit_function(int eFitFn, real *parm, real x)
332 static real y, dum[8];
334 mfitfn[eFitFn](x, parm-1, &y, dum);
339 /* lmfit_exp supports up to 3 parameter fitting of exponential functions */
340 static gmx_bool lmfit_exp(int nfit, real x[], real y[], real dy[], real ftol,
341 real parm[], real dparm[], gmx_bool bVerbose,
344 real chisq, ochisq, alamda;
345 real *a, **covar, **alpha, *dum;
347 int i, j, ma, mfit, *lista, *ia;
349 if ((eFitFn < 0) || (eFitFn >= effnNR))
351 gmx_fatal(FARGS, "fitfn = %d, should be in 0..%d (%s,%d)",
352 effnNR-1, eFitFn, __FILE__, __LINE__);
355 ma = mfit = nfp_ffn[eFitFn]; /* number of parameters to fit */
362 for (i = 1; (i < ma+1); i++)
365 ia[i] = 1; /* fixed bug B.S.S 19/11 */
366 snew(covar[i], ma+1);
367 snew(alpha[i], ma+1);
373 fprintf(stderr, "Will keep parameters fixed during fit procedure: %d\n",
376 for (i = 0; i < ma; i++)
386 fprintf(debug, "%d parameter fit\n", mfit);
390 alamda = -1; /* Starting value */
392 for (i = 0; (i < mfit); i++)
400 fprintf(stderr, "%4s %10s %10s %10s %10s %10s %10s\n",
401 "Step", "chi^2", "Lambda", "A1", "A2", "A3", "A4");
406 /* mrqmin(x-1,y-1,dy-1,nfit,a,ma,lista,mfit,covar,alpha,
407 * &chisq,expfn[mfit-1],&alamda)
409 if (!mrqmin_new(x-1, y-1, dy-1, nfit, a, ia, ma, covar, alpha, &chisq,
410 mfitfn[eFitFn], &alamda))
417 fprintf(stderr, "%4d %10.5e %10.5e %10.5e",
418 j, chisq, alamda, a[1]);
421 fprintf(stderr, " %10.5e", a[2]);
425 fprintf(stderr, " %10.5e", a[3]);
429 fprintf(stderr, " %10.5e", a[4]);
431 fprintf(stderr, "\n");
434 bCont = ((fabs(ochisq - chisq) > fabs(ftol*chisq)) ||
435 ((ochisq == chisq)));
437 while (bCont && (alamda != 0.0) && (j < 50));
440 fprintf(stderr, "\n");
443 /* Now get the covariance matrix out */
446 /* mrqmin(x-1,y-1,dy-1,nfit,a,ma,lista,mfit,covar,alpha,
447 * &chisq,expfn[mfit-1],&alamda)
449 if (!mrqmin_new(x-1, y-1, dy-1, nfit, a, ia, ma, covar, alpha, &chisq,
450 mfitfn[eFitFn], &alamda))
455 for (j = 0; (j < mfit); j++)
458 dparm[j] = covar[j+1][j+1];
461 for (i = 0; (i < ma+1); i++)
475 real do_lmfit(int ndata, real c1[], real sig[], real dt, real x0[],
476 real begintimefit, real endtimefit, const output_env_t oenv,
477 gmx_bool bVerbose, int eFitFn, real fitparms[], int fix)
482 int i, j, nparm, nfitpnts;
488 nparm = nfp_ffn[eFitFn];
491 fprintf(debug, "There are %d points to fit %d vars!\n", ndata, nparm);
492 fprintf(debug, "Fit to function %d from %g through %g, dt=%g\n",
493 eFitFn, begintimefit, endtimefit, dt);
501 for (i = 0; i < ndata; i++)
503 ttt = x0 ? x0[i] : dt*i;
504 if (ttt >= begintimefit && ttt <= endtimefit)
509 /* mrqmin does not like sig to be zero */
520 fprintf(debug, "j= %d, i= %d, x= %g, y= %g, dy= %g\n",
521 j, i, x[j], y[j], dy[j]);
528 if (nfitpnts < nparm)
530 fprintf(stderr, "Not enough data points for fitting!\n");
538 for (i = 0; (i < nparm); i++)
540 parm[i] = fitparms[i];
544 if (!lmfit_exp(nfitpnts, x, y, dy, ftol, parm, dparm, bVerbose, eFitFn, fix))
546 fprintf(stderr, "Fit failed!\n");
550 /* Compute the integral from begintimefit */
553 integral = (parm[0]*myexp(begintimefit, parm[1], parm[0]) +
554 parm[2]*myexp(begintimefit, 1-parm[1], parm[2]));
558 integral = parm[0]*myexp(begintimefit, parm[1], parm[0]);
562 integral = parm[0]*myexp(begintimefit, 1, parm[0]);
566 gmx_fatal(FARGS, "nparm = %d in file %s, line %d",
567 nparm, __FILE__, __LINE__);
570 /* Generate THE output */
573 fprintf(stderr, "FIT: # points used in fit is: %d\n", nfitpnts);
574 fprintf(stderr, "FIT: %21s%21s%21s\n",
575 "parm0 ", "parm1 (ps) ", "parm2 (ps) ");
576 fprintf(stderr, "FIT: ------------------------------------------------------------\n");
577 fprintf(stderr, "FIT: %8.3g +/- %8.3g%9.4g +/- %8.3g%8.3g +/- %8.3g\n",
578 parm[0], dparm[0], parm[1], dparm[1], parm[2], dparm[2]);
579 fprintf(stderr, "FIT: Integral (calc with fitted function) from %g ps to inf. is: %g\n",
580 begintimefit, integral);
582 sprintf(buf, "test%d.xvg", nfitpnts);
583 fp = xvgropen(buf, "C(t) + Fit to C(t)", "Time (ps)", "C(t)", oenv);
584 fprintf(fp, "# parm0 = %g, parm1 = %g, parm2 = %g\n",
585 parm[0], parm[1], parm[2]);
586 for (j = 0; (j < nfitpnts); j++)
588 ttt = x0 ? x0[j] : dt*j;
589 fprintf(fp, "%10.5e %10.5e %10.5e\n",
590 ttt, c1[j], fit_function(eFitFn, parm, ttt));
597 for (i = 0; (i < nparm); i++)
599 fitparms[i] = parm[i];
613 void do_expfit(int ndata, real c1[], real dt, real begintimefit, real endtimefit)
617 real aa, bb, saa, sbb, A, tau, dA, dtau;
619 fprintf(stderr, "Will fit data from %g (ps) to %g (ps).\n",
620 begintimefit, endtimefit);
622 snew(x, ndata); /* allocate the maximum necessary space */
627 for (i = 0; (i < ndata); i++)
629 if ( (dt*i >= begintimefit) && (dt*i <= endtimefit) )
634 fprintf(stderr, "n= %d, i= %d, x= %g, y= %g\n", n, i, x[n], y[n]);
638 fprintf(stderr, "# of data points used in the fit is : %d\n\n", n);
639 expfit(n, x, y, Dy, &aa, &saa, &bb, &sbb);
645 fprintf(stderr, "Fitted to y=exp(a+bx):\n");
646 fprintf(stderr, "a = %10.5f\t b = %10.5f", aa, bb);
647 fprintf(stderr, "\n");
648 fprintf(stderr, "Fitted to y=Aexp(-x/tau):\n");
649 fprintf(stderr, "A = %10.5f\t tau = %10.5f\n", A, tau);
650 fprintf(stderr, "dA = %10.5f\t dtau = %10.5f\n", dA, dtau);
654 void expfit(int n, real *x, real *y, real *Dy, real *a, real *sa,
657 real *w, *ly, A, SA, B, SB;
659 real sum, xbar, ybar, Sxx, Sxy, wr2, chi2, gamma, Db;
666 #define sqr(x) ((x)*(x))
672 /* Calculate weights and values of ln(y). */
673 for (i = 0; (i < n); i++)
675 w[i] = sqr(y[i]/Dy[i]);
679 /* The weighted averages of x and y: xbar and ybar. */
683 for (i = 0; (i < n); i++)
692 /* The centered product sums Sxx and Sxy, and hence A and B. */
695 for (i = 0; (i < n); i++)
697 Sxx += w[i]*sqr(x[i]-xbar);
698 Sxy += w[i]*(x[i]-xbar)*(ly[i]-ybar);
703 /* Chi-squared (chi2) and gamma. */
706 for (i = 0; (i < n); i++)
708 wr2 = w[i]*sqr(ly[i]-A-B*x[i]);
710 gamma += wr2*(x[i]-xbar);
713 /* Refined values of A and B. Also SA and SB. */
714 Db = -ONEP5*gamma/Sxx;
716 A -= ONEP5*chi2/sum-xbar*Db;
717 SB = sqrt((chi2/(n-2))/Sxx);
718 SA = SB*sqrt(Sxx/sum+sqr(xbar));