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