Merge branch 'release-4-6' into master
[alexxy/gromacs.git] / src / gromacs / gmxana / geminate.h
1 #ifndef _GEMINATE_H
2 #define _GEMINATE_H
3
4 enum {
5     gemNULL, gemNONE, gemDD, gemAD, gemAA, gemA4, gemNR
6 };
7 static const char *gemType[] = {NULL, "none", "dd", "ad", "aa", "a4", NULL};
8
9 /* The first few sections of this file contain functions that were adopted,
10  * and to some extent modified, by Erik Marklund (erikm[aT]xray.bmc.uu.se,
11  * http://folding.bmc.uu.se) from code written by Omer Markovitch (email, url).
12  * This is also the case with the function eq10v2() in geminate.c.
13  *
14  * The parts menetioned in the previous paragraph were contributed under a BSD license.
15  */
16
17 /* This first part is derived from complex.c which I recieved from Omer Markowitch.
18  * - Erik Marklund
19  *
20  * ------------- from complex.c ------------- */
21
22 #include <math.h>
23 /* definition of PI */
24 #ifndef PI
25 #define PI (acos(-1.0))
26 #endif
27
28 /* definition of the type complex */
29 typedef struct
30 {
31     double r, i;
32 } gem_complex;
33
34
35 /* ------------ end of complex.c ------------ */
36
37 /* This next part was derived from cerror.c and rerror.c,
38  * also received from Omer Markovitch.
39  * ------------- from [cr]error.c ------------- */
40
41 #ifndef sPI
42 #define sPI (sqrt(PI))
43 #endif
44
45 /* ------------ end of [cr]error.c ------------ */
46
47 /* ///////////////// REVERSIBLE GEMINATE RECOMBINATION ///////////////////
48  * Here follow routines and structs for reversible geminate recombination.
49  */
50
51 typedef struct {
52     size_t  n;
53     double *y;
54     double  tDelta;
55     int     nexp;
56 } balData;
57
58
59 typedef struct {
60     /* Used in the rewritten version of Omer's gem. recomb. analysis */
61     double ka, kd;               /* -- || -- results[]  */
62     double sigma;                /* -- || -- sigma      */
63     double D;                    /* The diffusion coefficient */
64     double kD;                   /* Replaces kD in analyse_corr_gem3d() */
65
66     /* The following members are for calcsquare() and takeAwayBallistic() */
67     double tDelta;            /* Time between frames */
68     /* double logAfterTime;        /\* Time after which we do the lsq calculations on a logarithmic timescale. *\/ */
69     int    nFitPoints;        /* Number of points to take from the ACF for fitting */
70     double begFit;            /* Fit from this time (ps) */
71     double endFit;            /* Fit up to this time (ps) */
72 /*   double logDelta; */
73 /*   double logPF; */
74 /* To get an equal number of points in the lin and log regime,
75  * we'll use logDelta to calculate where to sample the ACF.
76  * if i and j are indices in the linear and log regime, then:
77  *   j = Cexp(A(i+nLin)),
78  * where C = (nLin**2 / len) and A = log(len/nLin) / nLin.
79  * This expands to j = (nLin**2 / len) * exp((i+nLin) * log(len/nLin) / nLin).
80  * We'll save part of our brains and some computing time if we pre-calculate
81  *  1) logDelta = log(len/nLin) / nLin
82  *  2) logPF    = nLin**2 / len
83  * and get j = logPF * exp((i+nLin) * logDelta). */
84
85     /* I will redo this for a fit done entirely in log-log.
86      *  j' = j+1
87      *  nFitPoints' = nFitPoints-1
88      *
89      *  j' = Cexp(Ai)
90      *  (j'= 1 <=> i=0)
91      *     => C=1
92      *  (j'=len <=> i=nFitPoints')
93      *     => A=log(len)/nFitPoints'
94      *     => j = exp(i*log(len)/(nFitPoints-1)) -1
95      **/
96 /* #define GETLOGINDEX(i,params) (params)->logPF * exp(((i)+(params)->nLin) * (params)->logDelta)
97  */
98     double   logQuota;
99     int      nLin;          /* Number of timepoints in the linear regime */
100     int      len;           /* Length of time and ct arrays */
101     int      nExpFit;       /* Number of exponentials to fit */
102     real     ballistic;     /* Time before which the ballistic term should be fitted */
103     gmx_bool bDt;           /* TRUE =>  use time derivative at time 0
104                              *          to find fastest component.
105                              * FALSE => use coefficient in exponenetial
106                              *          to find fastest component. */
107 } t_gemParams;
108
109
110 typedef struct {
111     size_t       n;        /* Number of data points (lin-log) */
112     double      *y;        /* The datapoints */
113     double      *ctTheory; /* The theoretical ct which will be built by gemFunc_f. */
114     double      *LinLog;
115     double      *time;
116     double       ka;
117     double       kd;
118     double       tDelta; /* time difference between subsequent datapoints */
119     size_t       nData;  /* real size of the data */
120     int         *logtime;
121     double      *doubleLogTime;
122     t_gemParams *params;
123 } gemFitData;
124
125 extern void takeAwayBallistic(double *ct, double *t,
126                               int len, real tMax,
127                               int nexp, gmx_bool bDerivative);
128
129
130 extern t_gemParams *init_gemParams(const double sigma, const double D,
131                                    const real *t, const int len, const int nFitPoints,
132                                    const real begFit, const real endFit,
133                                    const real ballistic, const int nBalExp, const gmx_bool bDt);
134
135 /* Fit to geminate recombination model.
136    Returns root mean square error of fit. */
137 extern real fitGemRecomb(double *ct, double *time, double **ctFit,
138                          const int nData, t_gemParams *params);
139
140 extern void dumpN(const real *e, const int nn, char *fn);
141
142 /* Fix NaN that might appear in the theoretical acf. */
143 extern void fixGemACF(double *ct, int len);
144
145 #endif