Merge "Fix bug in selection subexpression handling."
[alexxy/gromacs.git] / src / gromacs / gmxana / correl.c
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2008, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  *
31  * And Hey:
32  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <math.h>
42 #include "gmx_fft.h"
43 #include "smalloc.h"
44 #include "correl.h"
45
46 #define SWAP(a, b) tempr = (a); (a) = (b); (b) = tempr
47
48 void four1(real data[], int nn, int isign)
49 {
50     int    n, mmax, m, j, istep, i;
51     double wtemp, wr, wpr, wpi, wi, theta;
52     real   tempr, tempi;
53
54     n = nn << 1;
55     j = 1;
56     for (i = 1; i < n; i += 2)
57     {
58         if (j > i)
59         {
60             SWAP(data[j], data[i]);
61             SWAP(data[j+1], data[i+1]);
62         }
63         m = n >> 1;
64         while (m >= 2 && j > m)
65         {
66             j  -= m;
67             m >>= 1;
68         }
69         j += m;
70     }
71     mmax = 2;
72     while (n > mmax)
73     {
74         istep = 2*mmax;
75         theta = 6.28318530717959/(isign*mmax);
76         wtemp = sin(0.5*theta);
77         wpr   = -2.0*wtemp*wtemp;
78         wpi   = sin(theta);
79         wr    = 1.0;
80         wi    = 0.0;
81         for (m = 1; m < mmax; m += 2)
82         {
83             for (i = m; i <= n; i += istep)
84             {
85                 j          = i+mmax;
86                 tempr      = wr*data[j]-wi*data[j+1];
87                 tempi      = wr*data[j+1]+wi*data[j];
88                 data[j]    = data[i]-tempr;
89                 data[j+1]  = data[i+1]-tempi;
90                 data[i]   += tempr;
91                 data[i+1] += tempi;
92             }
93             wr = (wtemp = wr)*wpr-wi*wpi+wr;
94             wi = wi*wpr+wtemp*wpi+wi;
95         }
96         mmax = istep;
97     }
98 }
99
100 #undef SWAP
101
102 static void realft(real data[], int n, int isign)
103 {
104     int    i, i1, i2, i3, i4, n2p3;
105     real   c1 = 0.5, c2, h1r, h1i, h2r, h2i;
106     double wr, wi, wpr, wpi, wtemp, theta;
107
108     theta = 3.141592653589793/(double) n;
109     if (isign == 1)
110     {
111         c2 = -0.5;
112         four1(data, n, 1);
113     }
114     else
115     {
116         c2    = 0.5;
117         theta = -theta;
118     }
119     wtemp = sin(0.5*theta);
120     wpr   = -2.0*wtemp*wtemp;
121     wpi   = sin(theta);
122     wr    = 1.0+wpr;
123     wi    = wpi;
124     n2p3  = 2*n+3;
125     for (i = 2; i <= n/2; i++)
126     {
127         i4       = 1+(i3 = n2p3-(i2 = 1+(i1 = i+i-1)));
128         h1r      = c1*(data[i1]+data[i3]);
129         h1i      = c1*(data[i2]-data[i4]);
130         h2r      = -c2*(data[i2]+data[i4]);
131         h2i      = c2*(data[i1]-data[i3]);
132         data[i1] = h1r+wr*h2r-wi*h2i;
133         data[i2] = h1i+wr*h2i+wi*h2r;
134         data[i3] = h1r-wr*h2r+wi*h2i;
135         data[i4] = -h1i+wr*h2i+wi*h2r;
136         wr       = (wtemp = wr)*wpr-wi*wpi+wr;
137         wi       = wi*wpr+wtemp*wpi+wi;
138     }
139     if (isign == 1)
140     {
141         data[1] = (h1r = data[1])+data[2];
142         data[2] = h1r-data[2];
143     }
144     else
145     {
146         data[1] = c1*((h1r = data[1])+data[2]);
147         data[2] = c1*(h1r-data[2]);
148         four1(data, n, -1);
149     }
150 }
151
152 static void twofft(real data1[], real data2[], real fft1[], real fft2[], int n)
153 {
154     int  nn3, nn2, jj, j;
155     real rep, rem, aip, aim;
156
157     nn3 = 1+(nn2 = 2+n+n);
158     for (j = 1, jj = 2; j <= n; j++, jj += 2)
159     {
160         fft1[jj-1] = data1[j];
161         fft1[jj]   = data2[j];
162     }
163     four1(fft1, n, 1);
164     fft2[1] = fft1[2];
165     fft1[2] = fft2[2] = 0.0;
166     for (j = 3; j <= n+1; j += 2)
167     {
168         rep         = 0.5*(fft1[j]+fft1[nn2-j]);
169         rem         = 0.5*(fft1[j]-fft1[nn2-j]);
170         aip         = 0.5*(fft1[j+1]+fft1[nn3-j]);
171         aim         = 0.5*(fft1[j+1]-fft1[nn3-j]);
172         fft1[j]     = rep;
173         fft1[j+1]   = aim;
174         fft1[nn2-j] = rep;
175         fft1[nn3-j] = -aim;
176         fft2[j]     = aip;
177         fft2[j+1]   = -rem;
178         fft2[nn2-j] = aip;
179         fft2[nn3-j] = rem;
180     }
181 }
182
183 void correl(real data1[], real data2[], int n, real ans[])
184 {
185     int     no2, i;
186     real    dum, *fft;
187
188     snew(fft, 2*n+1);
189     twofft(data1, data2, fft, ans, n);
190     no2 = n/2;
191     for (i = 2; i <= n+2; i += 2)
192     {
193         dum      = ans[i-1];
194         ans[i-1] = (fft[i-1]*dum+fft[i]*ans[i])/no2;
195         ans[i]   = (fft[i]*dum-fft[i-1]*ans[i])/no2;
196     }
197     ans[2] = ans[n+1];
198     realft(ans, no2, -1);
199     sfree(fft);
200 }