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