7d4b471ec9dbf6a2d90e46ee675573eec4c6cde4
[alexxy/gromacs.git] / src / gromacs / math / do_fit.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2012,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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "do_fit.h"
38
39 #include "gromacs/math/utilities.h"
40 #include "sysstuff.h"
41 #include "typedefs.h"
42 #include "vec.h"
43 #include "txtdump.h"
44 #include "smalloc.h"
45
46 #include "gromacs/linearalgebra/nrjac.h"
47
48 real calc_similar_ind(gmx_bool bRho, int nind, atom_id *index, real mass[],
49                       rvec x[], rvec xp[])
50 {
51     int  i, j, d;
52     real m, tm, xs, xd, rs, rd;
53
54     tm = 0;
55     rs = 0;
56     rd = 0;
57     for (j = 0; j < nind; j++)
58     {
59         if (index)
60         {
61             i = index[j];
62         }
63         else
64         {
65             i = j;
66         }
67         m   = mass[i];
68         tm += m;
69         for (d = 0; d < DIM; d++)
70         {
71             xd  = x[i][d] - xp[i][d];
72             rd += m * sqr(xd);
73             if (bRho)
74             {
75                 xs  = x[i][d] + xp[i][d];
76                 rs += m * sqr(xs);
77             }
78         }
79     }
80     if (bRho)
81     {
82         return 2*sqrt(rd/rs);
83     }
84     else
85     {
86         return sqrt(rd/tm);
87     }
88 }
89
90 real rmsdev_ind(int nind, atom_id index[], real mass[], rvec x[], rvec xp[])
91 {
92     return calc_similar_ind(FALSE, nind, index, mass, x, xp);
93 }
94
95 real rmsdev(int natoms, real mass[], rvec x[], rvec xp[])
96 {
97     return calc_similar_ind(FALSE, natoms, NULL, mass, x, xp);
98 }
99
100 real rhodev_ind(int nind, atom_id index[], real mass[], rvec x[], rvec xp[])
101 {
102     return calc_similar_ind(TRUE, nind, index, mass, x, xp);
103 }
104
105 real rhodev(int natoms, real mass[], rvec x[], rvec xp[])
106 {
107     return calc_similar_ind(TRUE, natoms, NULL, mass, x, xp);
108 }
109
110 void calc_fit_R(int ndim, int natoms, real *w_rls, rvec *xp, rvec *x, matrix R)
111 {
112     int      c, r, n, j, m, i, irot, s;
113     double **omega, **om;
114     double   d[2*DIM], xnr, xpc;
115     matrix   vh, vk, u;
116     real     mn;
117     int      index;
118     real     max_d;
119
120     if (ndim != 3 && ndim != 2)
121     {
122         gmx_fatal(FARGS, "calc_fit_R called with ndim=%d instead of 3 or 2", ndim);
123     }
124
125     snew(omega, 2*ndim);
126     snew(om, 2*ndim);
127     for (i = 0; i < 2*ndim; i++)
128     {
129         snew(omega[i], 2*ndim);
130         snew(om[i], 2*ndim);
131     }
132
133     for (i = 0; i < 2*ndim; i++)
134     {
135         d[i] = 0;
136         for (j = 0; j < 2*ndim; j++)
137         {
138             omega[i][j] = 0;
139             om[i][j]    = 0;
140         }
141     }
142
143     /*calculate the matrix U*/
144     clear_mat(u);
145     for (n = 0; (n < natoms); n++)
146     {
147         if ((mn = w_rls[n]) != 0.0)
148         {
149             for (c = 0; (c < ndim); c++)
150             {
151                 xpc = xp[n][c];
152                 for (r = 0; (r < ndim); r++)
153                 {
154                     xnr      = x[n][r];
155                     u[c][r] += mn*xnr*xpc;
156                 }
157             }
158         }
159     }
160
161     /*construct omega*/
162     /*omega is symmetric -> omega==omega' */
163     for (r = 0; r < 2*ndim; r++)
164     {
165         for (c = 0; c <= r; c++)
166         {
167             if (r >= ndim && c < ndim)
168             {
169                 omega[r][c] = u[r-ndim][c];
170                 omega[c][r] = u[r-ndim][c];
171             }
172             else
173             {
174                 omega[r][c] = 0;
175                 omega[c][r] = 0;
176             }
177         }
178     }
179
180     /*determine h and k*/
181     jacobi(omega, 2*ndim, d, om, &irot);
182     /*real   **omega = input matrix a[0..n-1][0..n-1] must be symmetric
183      * int     natoms = number of rows and columns
184      * real      NULL = d[0]..d[n-1] are the eigenvalues of a[][]
185      * real       **v = v[0..n-1][0..n-1] contains the vectors in columns
186      * int      *irot = number of jacobi rotations
187      */
188
189     if (debug && irot == 0)
190     {
191         fprintf(debug, "IROT=0\n");
192     }
193
194     index = 0; /* For the compiler only */
195
196     /* Copy only the first ndim-1 eigenvectors */
197     for (j = 0; j < ndim-1; j++)
198     {
199         max_d = -1000;
200         for (i = 0; i < 2*ndim; i++)
201         {
202             if (d[i] > max_d)
203             {
204                 max_d = d[i];
205                 index = i;
206             }
207         }
208         d[index] = -10000;
209         for (i = 0; i < ndim; i++)
210         {
211             vh[j][i] = M_SQRT2*om[i][index];
212             vk[j][i] = M_SQRT2*om[i+ndim][index];
213         }
214     }
215     if (ndim == 3)
216     {
217         /* Calculate the last eigenvector as the outer-product of the first two.
218          * This insures that the conformation is not mirrored and
219          * prevents problems with completely flat reference structures.
220          */
221         cprod(vh[0], vh[1], vh[2]);
222         cprod(vk[0], vk[1], vk[2]);
223     }
224     else if (ndim == 2)
225     {
226         /* Calculate the last eigenvector from the first one */
227         vh[1][XX] = -vh[0][YY];
228         vh[1][YY] =  vh[0][XX];
229         vk[1][XX] = -vk[0][YY];
230         vk[1][YY] =  vk[0][XX];
231     }
232
233     /* determine R */
234     clear_mat(R);
235     for (r = 0; r < ndim; r++)
236     {
237         for (c = 0; c < ndim; c++)
238         {
239             for (s = 0; s < ndim; s++)
240             {
241                 R[r][c] += vk[s][r]*vh[s][c];
242             }
243         }
244     }
245     for (r = ndim; r < DIM; r++)
246     {
247         R[r][r] = 1;
248     }
249
250     for (i = 0; i < 2*ndim; i++)
251     {
252         sfree(omega[i]);
253         sfree(om[i]);
254     }
255     sfree(omega);
256     sfree(om);
257 }
258
259 void do_fit_ndim(int ndim, int natoms, real *w_rls, rvec *xp, rvec *x)
260 {
261     int    i, j, m, r, c;
262     matrix R;
263     rvec   x_old;
264
265     /* Calculate the rotation matrix R */
266     calc_fit_R(ndim, natoms, w_rls, xp, x, R);
267
268     /*rotate X*/
269     for (j = 0; j < natoms; j++)
270     {
271         for (m = 0; m < DIM; m++)
272         {
273             x_old[m] = x[j][m];
274         }
275         for (r = 0; r < DIM; r++)
276         {
277             x[j][r] = 0;
278             for (c = 0; c < DIM; c++)
279             {
280                 x[j][r] += R[r][c]*x_old[c];
281             }
282         }
283     }
284 }
285
286 void do_fit(int natoms, real *w_rls, rvec *xp, rvec *x)
287 {
288     do_fit_ndim(3, natoms, w_rls, xp, x);
289 }
290
291 void reset_x_ndim(int ndim, int ncm, const atom_id *ind_cm,
292                   int nreset, const atom_id *ind_reset,
293                   rvec x[], const real mass[])
294 {
295     int  i, m, ai;
296     rvec xcm;
297     real tm, mm;
298
299     if (ndim > DIM)
300     {
301         gmx_incons("More than 3 dimensions not supported.");
302     }
303     tm = 0.0;
304     clear_rvec(xcm);
305     if (ind_cm != NULL)
306     {
307         for (i = 0; i < ncm; i++)
308         {
309             ai = ind_cm[i];
310             mm = mass[ai];
311             for (m = 0; m < ndim; m++)
312             {
313                 xcm[m] += mm*x[ai][m];
314             }
315             tm += mm;
316         }
317     }
318     else
319     {
320         for (i = 0; i < ncm; i++)
321         {
322             mm = mass[i];
323             for (m = 0; m < ndim; m++)
324             {
325                 xcm[m] += mm*x[i][m];
326             }
327             tm += mm;
328         }
329     }
330     for (m = 0; m < ndim; m++)
331     {
332         xcm[m] /= tm;
333     }
334
335     if (ind_reset != NULL)
336     {
337         for (i = 0; i < nreset; i++)
338         {
339             rvec_dec(x[ind_reset[i]], xcm);
340         }
341     }
342     else
343     {
344         for (i = 0; i < nreset; i++)
345         {
346             rvec_dec(x[i], xcm);
347         }
348     }
349 }
350
351 void reset_x(int ncm, const atom_id *ind_cm,
352              int nreset, const atom_id *ind_reset,
353              rvec x[], const real mass[])
354 {
355     reset_x_ndim(3, ncm, ind_cm, nreset, ind_reset, x, mass);
356 }