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,2015, 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.
39 * Implements function to compute many autocorrelation functions
41 * \author David van der Spoel <david.vanderspoel@icm.uu.se>
42 * \ingroup module_correlationfunctions
55 #include "gromacs/correlationfunctions/expfit.h"
56 #include "gromacs/correlationfunctions/integrate.h"
57 #include "gromacs/correlationfunctions/manyautocorrelation.h"
58 #include "gromacs/correlationfunctions/polynomials.h"
59 #include "gromacs/fileio/xvgr.h"
60 #include "gromacs/legacyheaders/macros.h"
61 #include "gromacs/legacyheaders/names.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/utility/fatalerror.h"
64 #include "gromacs/utility/futil.h"
65 #include "gromacs/utility/real.h"
66 #include "gromacs/utility/smalloc.h"
68 /*! \brief Shortcut macro to select modes. */
69 #define MODE(x) ((mode & (x)) == (x))
73 int nrestart, nout, P, fitfn;
74 gmx_bool bFour, bNormalize;
75 real tbeginfit, tendfit;
78 /*! \brief Global variable set true if initialization routines are called. */
79 static gmx_bool bACFinit = FALSE;
81 /*! \brief Data structure for storing command line variables. */
88 /*! \brief Routine to compute ACF using FFT. */
89 static void low_do_four_core(int nfour, int nframes, real c1[], real cfour[],
97 for (i = 0; (i < nframes); i++)
103 for (i = 0; (i < nframes); i++)
105 cfour[i] = cos(c1[i]);
109 for (i = 0; (i < nframes); i++)
111 cfour[i] = sin(c1[i]);
115 gmx_fatal(FARGS, "nCos = %d, %s %d", nCos, __FILE__, __LINE__);
118 many_auto_correl(1, nframes, nfour, &cfour);
121 /*! \brief Routine to comput ACF without FFT. */
122 static void do_ac_core(int nframes, int nout,
123 real corr[], real c1[], int nrestart,
126 int j, k, j3, jk3, m, n;
132 printf("WARNING: setting number of restarts to 1\n");
138 "Starting do_ac_core: nframes=%d, nout=%d, nrestart=%d,mode=%lu\n",
139 nframes, nout, nrestart, mode);
142 for (j = 0; (j < nout); j++)
147 /* Loop over starting points. */
148 for (j = 0; (j < nframes); j += nrestart)
152 /* Loop over the correlation length for this starting point */
153 for (k = 0; (k < nout) && (j+k < nframes); k++)
157 /* Switch over possible ACF types.
158 * It might be more efficient to put the loops inside the switch,
159 * but this is more clear, and save development time!
163 corr[k] += c1[j]*c1[j+k];
165 else if (MODE(eacCos))
167 /* Compute the cos (phi(t)-phi(t+dt)) */
168 corr[k] += cos(c1[j]-c1[j+k]);
170 else if (MODE(eacIden))
172 /* Check equality (phi(t)==phi(t+dt)) */
173 corr[k] += (c1[j] == c1[j+k]) ? 1 : 0;
175 else if (MODE(eacP1) || MODE(eacP2) || MODE(eacP3))
179 for (m = 0; (m < DIM); m++)
184 cth = cos_angle(xj, xk);
186 if (cth-1.0 > 1.0e-15)
188 printf("j: %d, k: %d, xj:(%g,%g,%g), xk:(%g,%g,%g)\n",
189 j, k, xj[XX], xj[YY], xj[ZZ], xk[XX], xk[YY], xk[ZZ]);
196 else if (MODE(eacP3))
200 corr[k] += LegendreP(cth, mmm); /* 1.5*cth*cth-0.5; */
202 else if (MODE(eacRcross))
205 for (m = 0; (m < DIM); m++)
212 corr[k] += iprod(rr, rr);
214 else if (MODE(eacVector))
216 for (m = 0; (m < DIM); m++)
227 gmx_fatal(FARGS, "\nInvalid mode (%d) in do_ac_core", mode);
231 /* Correct for the number of points and copy results to the data array */
232 for (j = 0; (j < nout); j++)
234 n = (nframes-j+(nrestart-1))/nrestart;
239 /*! \brief Routine to normalize ACF, dividing by corr[0]. */
240 void normalize_acf(int nout, real corr[])
247 fprintf(debug, "Before normalization\n");
248 for (j = 0; (j < nout); j++)
250 fprintf(debug, "%5d %10f\n", j, corr[j]);
254 /* Normalisation makes that c[0] = 1.0 and that other points are scaled
257 if (fabs(corr[0]) < 1e-5)
265 for (j = 0; (j < nout); j++)
272 fprintf(debug, "After normalization\n");
273 for (j = 0; (j < nout); j++)
275 fprintf(debug, "%5d %10f\n", j, corr[j]);
280 /*! \brief Routine that averages ACFs. */
281 void average_acf(gmx_bool bVerbose, int n, int nitem, real **c1)
288 printf("Averaging correlation functions\n");
291 for (j = 0; (j < n); j++)
294 for (i = 0; (i < nitem); i++)
302 /*! \brief Normalize ACFs. */
303 void norm_and_scale_vectors(int nframes, real c1[], real scale)
308 for (j = 0; (j < nframes); j++)
312 for (m = 0; (m < DIM); m++)
319 /*! \brief Debugging */
320 static void dump_tmp(char *s, int n, real c[])
325 fp = gmx_ffopen(s, "w");
326 for (i = 0; (i < n); i++)
328 fprintf(fp, "%10d %10g\n", i, c[i]);
333 /*! \brief High level ACF routine. */
334 void do_four_core(unsigned long mode, int nfour, int nf2, int nframes,
335 real c1[], real csum[], real ctmp[])
346 /********************************************
348 ********************************************/
349 low_do_four_core(nfour, nf2, c1, csum, enNorm);
351 else if (MODE(eacCos))
353 /***************************************************
355 ***************************************************/
356 /* Copy the data to temp array. Since we need it twice
357 * we can't overwrite original.
359 for (j = 0; (j < nf2); j++)
364 /* Cosine term of AC function */
365 low_do_four_core(nfour, nf2, ctmp, cfour, enCos);
366 for (j = 0; (j < nf2); j++)
371 /* Sine term of AC function */
372 low_do_four_core(nfour, nf2, ctmp, cfour, enSin);
373 for (j = 0; (j < nf2); j++)
379 else if (MODE(eacP2))
381 /***************************************************
382 * Legendre polynomials
383 ***************************************************/
384 /* First normalize the vectors */
385 norm_and_scale_vectors(nframes, c1, 1.0);
387 /* For P2 thingies we have to do six FFT based correls
388 * First for XX^2, then for YY^2, then for ZZ^2
389 * Then we have to do XY, YZ and XZ (counting these twice)
390 * After that we sum them and normalise
391 * P2(x) = (3 * cos^2 (x) - 1)/2
392 * for unit vectors u and v we compute the cosine as the inner product
393 * cos(u,v) = uX vX + uY vY + uZ vZ
397 * C(t) = | (3 cos^2(u(t'),u(t'+t)) - 1)/2 dt'
402 * P2(u(0),u(t)) = [3 * (uX(0) uX(t) +
404 * uZ(0) uZ(t))^2 - 1]/2
405 * = [3 * ((uX(0) uX(t))^2 +
408 * 2(uX(0) uY(0) uX(t) uY(t)) +
409 * 2(uX(0) uZ(0) uX(t) uZ(t)) +
410 * 2(uY(0) uZ(0) uY(t) uZ(t))) - 1]/2
412 * = [(3/2) * (<uX^2> + <uY^2> + <uZ^2> +
413 * 2<uXuY> + 2<uXuZ> + 2<uYuZ>) - 0.5]
417 /* Because of normalization the number of -0.5 to subtract
418 * depends on the number of data points!
420 for (j = 0; (j < nf2); j++)
422 csum[j] = -0.5*(nf2-j);
425 /***** DIAGONAL ELEMENTS ************/
426 for (m = 0; (m < DIM); m++)
428 /* Copy the vector data in a linear array */
429 for (j = 0; (j < nf2); j++)
431 ctmp[j] = sqr(c1[DIM*j+m]);
435 sprintf(buf, "c1diag%d.xvg", m);
436 dump_tmp(buf, nf2, ctmp);
439 low_do_four_core(nfour, nf2, ctmp, cfour, enNorm);
443 sprintf(buf, "c1dfout%d.xvg", m);
444 dump_tmp(buf, nf2, cfour);
447 for (j = 0; (j < nf2); j++)
449 csum[j] += fac*(cfour[j]);
452 /******* OFF-DIAGONAL ELEMENTS **********/
453 for (m = 0; (m < DIM); m++)
455 /* Copy the vector data in a linear array */
457 for (j = 0; (j < nf2); j++)
459 ctmp[j] = c1[DIM*j+m]*c1[DIM*j+m1];
464 sprintf(buf, "c1off%d.xvg", m);
465 dump_tmp(buf, nf2, ctmp);
467 low_do_four_core(nfour, nf2, ctmp, cfour, enNorm);
470 sprintf(buf, "c1ofout%d.xvg", m);
471 dump_tmp(buf, nf2, cfour);
474 for (j = 0; (j < nf2); j++)
476 csum[j] += fac*cfour[j];
480 else if (MODE(eacP1) || MODE(eacVector))
482 /***************************************************
484 ***************************************************/
487 /* First normalize the vectors */
488 norm_and_scale_vectors(nframes, c1, 1.0);
491 /* For vector thingies we have to do three FFT based correls
492 * First for XX, then for YY, then for ZZ
493 * After that we sum them and normalise
495 for (j = 0; (j < nf2); j++)
499 for (m = 0; (m < DIM); m++)
501 /* Copy the vector data in a linear array */
502 for (j = 0; (j < nf2); j++)
504 ctmp[j] = c1[DIM*j+m];
506 low_do_four_core(nfour, nf2, ctmp, cfour, enNorm);
507 for (j = 0; (j < nf2); j++)
515 gmx_fatal(FARGS, "\nUnknown mode in do_autocorr (%d)", mode);
519 for (j = 0; (j < nf2); j++)
521 c1[j] = csum[j]/(real)(nframes-j);
525 void low_do_autocorr(const char *fn, const output_env_t oenv, const char *title,
526 int nframes, int nitem, int nout, real **c1,
527 real dt, unsigned long mode, int nrestart,
528 gmx_bool bAver, gmx_bool bNormalize,
529 gmx_bool bVerbose, real tbeginfit, real tendfit,
532 FILE *fp, *gp = NULL;
536 real c0, sum, Ct2av, Ctav;
537 gmx_bool bFour = acf.bFour;
539 /* Check flags and parameters */
540 nout = get_acfnout();
543 nout = acf.nout = (nframes+1)/2;
545 else if (nout > nframes)
550 if (MODE(eacCos) && MODE(eacVector))
552 gmx_fatal(FARGS, "Incompatible options bCos && bVector (%s, %d)",
555 if ((MODE(eacP3) || MODE(eacRcross)) && bFour)
559 fprintf(stderr, "Can't combine mode %lu with FFT, turning off FFT\n", mode);
563 if (MODE(eacNormal) && MODE(eacVector))
565 gmx_fatal(FARGS, "Incompatible mode bits: normal and vector (or Legendre)");
568 /* Print flags and parameters */
571 printf("Will calculate %s of %d thingies for %d frames\n",
572 title ? title : "autocorrelation", nitem, nframes);
573 printf("bAver = %s, bFour = %s bNormalize= %s\n",
574 bool_names[bAver], bool_names[bFour], bool_names[bNormalize]);
575 printf("mode = %lu, dt = %g, nrestart = %d\n", mode, dt, nrestart);
579 c0 = std::log(static_cast<double>(nframes))/std::log(2.0);
580 k = static_cast<int>(c0);
589 fprintf(debug, "Using FFT to calculate %s, #points for FFT = %d\n",
593 /* Allocate temp arrays */
599 nfour = 0; /* To keep the compiler happy */
604 /* Loop over items (e.g. molecules or dihedrals)
605 * In this loop the actual correlation functions are computed, but without
608 for (int i = 0; i < nitem; i++)
610 if (bVerbose && (((i % 100) == 0) || (i == nitem-1)))
612 fprintf(stderr, "\rThingie %d", i+1);
617 do_four_core(mode, nfour, nframes, nframes, c1[i], csum, ctmp);
621 do_ac_core(nframes, nout, ctmp, c1[i], nrestart, mode);
626 fprintf(stderr, "\n");
634 fp = xvgropen(fn, title, "Time (ps)", "C(t)", oenv);
645 average_acf(bVerbose, nframes, nitem, c1);
650 normalize_acf(nout, c1[0]);
653 if (eFitFn != effnNONE)
655 fit_acf(nout, eFitFn, oenv, fn != NULL, tbeginfit, tendfit, dt, c1[0], fit);
656 sum = print_and_integrate(fp, nout, dt, c1[0], fit, 1);
660 sum = print_and_integrate(fp, nout, dt, c1[0], NULL, 1);
664 printf("Correlation time (integral over corrfn): %g (ps)\n", sum);
669 /* Not averaging. Normalize individual ACFs */
673 gp = xvgropen("ct-distr.xvg", "Correlation times", "item", "time (ps)", oenv);
675 for (i = 0; i < nitem; i++)
679 normalize_acf(nout, c1[i]);
681 if (eFitFn != effnNONE)
683 fit_acf(nout, eFitFn, oenv, fn != NULL, tbeginfit, tendfit, dt, c1[i], fit);
684 sum = print_and_integrate(fp, nout, dt, c1[i], fit, 1);
688 sum = print_and_integrate(fp, nout, dt, c1[i], NULL, 1);
692 "CORRelation time (integral over corrfn %d): %g (ps)\n",
700 fprintf(gp, "%5d %.3f\n", i, sum);
711 printf("Average correlation time %.3f Std. Dev. %.3f Error %.3f (ps)\n",
712 Ctav, sqrt((Ct2av - sqr(Ctav))),
713 sqrt((Ct2av - sqr(Ctav))/(nitem-1)));
723 /*! \brief Legend for selecting Legendre polynomials. */
724 static const char *Leg[] = { NULL, "0", "1", "2", "3", NULL };
726 t_pargs *add_acf_pargs(int *npargs, t_pargs *pa)
729 { "-acflen", FALSE, etINT, {&acf.nout},
730 "Length of the ACF, default is half the number of frames" },
731 { "-normalize", FALSE, etBOOL, {&acf.bNormalize},
733 { "-fftcorr", FALSE, etBOOL, {&acf.bFour},
734 "HIDDENUse fast fourier transform for correlation function" },
735 { "-nrestart", FALSE, etINT, {&acf.nrestart},
736 "HIDDENNumber of frames between time origins for ACF when no FFT is used" },
737 { "-P", FALSE, etENUM, {Leg},
738 "Order of Legendre polynomial for ACF (0 indicates none)" },
739 { "-fitfn", FALSE, etENUM, {s_ffn},
741 { "-beginfit", FALSE, etREAL, {&acf.tbeginfit},
742 "Time where to begin the exponential fit of the correlation function" },
743 { "-endfit", FALSE, etREAL, {&acf.tendfit},
744 "Time where to end the exponential fit of the correlation function, -1 is until the end" },
750 snew(ppa, *npargs+npa);
751 for (i = 0; (i < *npargs); i++)
755 for (i = 0; (i < npa); i++)
757 ppa[*npargs+i] = acfpa[i];
765 acf.fitfn = effnEXP1;
767 acf.bNormalize = TRUE;
776 void do_autocorr(const char *fn, const output_env_t oenv, const char *title,
777 int nframes, int nitem, real **c1,
778 real dt, unsigned long mode, gmx_bool bAver)
782 printf("ACF data structures have not been initialised. Call add_acf_pargs\n");
785 /* Handle enumerated types */
786 sscanf(Leg[0], "%d", &acf.P);
787 acf.fitfn = sffn2effn(s_ffn);
804 low_do_autocorr(fn, oenv, title, nframes, nitem, acf.nout, c1, dt, mode,
805 acf.nrestart, bAver, acf.bNormalize,
806 bDebugMode(), acf.tbeginfit, acf.tendfit,
810 int get_acfnout(void)
814 gmx_fatal(FARGS, "ACF data not initialized yet");
820 int get_acffitfn(void)
824 gmx_fatal(FARGS, "ACF data not initialized yet");
827 return sffn2effn(s_ffn);