2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
39 #include "gromacs/linearalgebra/nrjac.h"
40 #include "gromacs/math/utilities.h"
41 #include "gromacs/math/vec.h"
42 #include "gromacs/utility/fatalerror.h"
43 #include "gromacs/utility/smalloc.h"
45 real calc_similar_ind(gmx_bool bRho, int nind, atom_id *index, real mass[],
49 real m, tm, xs, xd, rs, rd;
54 for (j = 0; j < nind; j++)
66 for (d = 0; d < DIM; d++)
68 xd = x[i][d] - xp[i][d];
72 xs = x[i][d] + xp[i][d];
87 real rmsdev_ind(int nind, atom_id index[], real mass[], rvec x[], rvec xp[])
89 return calc_similar_ind(FALSE, nind, index, mass, x, xp);
92 real rmsdev(int natoms, real mass[], rvec x[], rvec xp[])
94 return calc_similar_ind(FALSE, natoms, NULL, mass, x, xp);
97 real rhodev_ind(int nind, atom_id index[], real mass[], rvec x[], rvec xp[])
99 return calc_similar_ind(TRUE, nind, index, mass, x, xp);
102 real rhodev(int natoms, real mass[], rvec x[], rvec xp[])
104 return calc_similar_ind(TRUE, natoms, NULL, mass, x, xp);
107 void calc_fit_R(int ndim, int natoms, real *w_rls, rvec *xp, rvec *x, matrix R)
109 int c, r, n, j, m, i, irot, s;
110 double **omega, **om;
111 double d[2*DIM], xnr, xpc;
117 if (ndim != 3 && ndim != 2)
119 gmx_fatal(FARGS, "calc_fit_R called with ndim=%d instead of 3 or 2", ndim);
124 for (i = 0; i < 2*ndim; i++)
126 snew(omega[i], 2*ndim);
130 for (i = 0; i < 2*ndim; i++)
133 for (j = 0; j < 2*ndim; j++)
140 /*calculate the matrix U*/
142 for (n = 0; (n < natoms); n++)
144 if ((mn = w_rls[n]) != 0.0)
146 for (c = 0; (c < ndim); c++)
149 for (r = 0; (r < ndim); r++)
152 u[c][r] += mn*xnr*xpc;
159 /*omega is symmetric -> omega==omega' */
160 for (r = 0; r < 2*ndim; r++)
162 for (c = 0; c <= r; c++)
164 if (r >= ndim && c < ndim)
166 omega[r][c] = u[r-ndim][c];
167 omega[c][r] = u[r-ndim][c];
177 /*determine h and k*/
178 jacobi(omega, 2*ndim, d, om, &irot);
179 /*real **omega = input matrix a[0..n-1][0..n-1] must be symmetric
180 * int natoms = number of rows and columns
181 * real NULL = d[0]..d[n-1] are the eigenvalues of a[][]
182 * real **v = v[0..n-1][0..n-1] contains the vectors in columns
183 * int *irot = number of jacobi rotations
186 if (debug && irot == 0)
188 fprintf(debug, "IROT=0\n");
191 index = 0; /* For the compiler only */
193 /* Copy only the first ndim-1 eigenvectors */
194 for (j = 0; j < ndim-1; j++)
197 for (i = 0; i < 2*ndim; i++)
206 for (i = 0; i < ndim; i++)
208 vh[j][i] = M_SQRT2*om[i][index];
209 vk[j][i] = M_SQRT2*om[i+ndim][index];
214 /* Calculate the last eigenvector as the outer-product of the first two.
215 * This insures that the conformation is not mirrored and
216 * prevents problems with completely flat reference structures.
218 cprod(vh[0], vh[1], vh[2]);
219 cprod(vk[0], vk[1], vk[2]);
223 /* Calculate the last eigenvector from the first one */
224 vh[1][XX] = -vh[0][YY];
225 vh[1][YY] = vh[0][XX];
226 vk[1][XX] = -vk[0][YY];
227 vk[1][YY] = vk[0][XX];
232 for (r = 0; r < ndim; r++)
234 for (c = 0; c < ndim; c++)
236 for (s = 0; s < ndim; s++)
238 R[r][c] += vk[s][r]*vh[s][c];
242 for (r = ndim; r < DIM; r++)
247 for (i = 0; i < 2*ndim; i++)
256 void do_fit_ndim(int ndim, int natoms, real *w_rls, rvec *xp, rvec *x)
262 /* Calculate the rotation matrix R */
263 calc_fit_R(ndim, natoms, w_rls, xp, x, R);
266 for (j = 0; j < natoms; j++)
268 for (m = 0; m < DIM; m++)
272 for (r = 0; r < DIM; r++)
275 for (c = 0; c < DIM; c++)
277 x[j][r] += R[r][c]*x_old[c];
283 void do_fit(int natoms, real *w_rls, rvec *xp, rvec *x)
285 do_fit_ndim(3, natoms, w_rls, xp, x);
288 void reset_x_ndim(int ndim, int ncm, const atom_id *ind_cm,
289 int nreset, const atom_id *ind_reset,
290 rvec x[], const real mass[])
298 gmx_incons("More than 3 dimensions not supported.");
304 for (i = 0; i < ncm; i++)
308 for (m = 0; m < ndim; m++)
310 xcm[m] += mm*x[ai][m];
317 for (i = 0; i < ncm; i++)
320 for (m = 0; m < ndim; m++)
322 xcm[m] += mm*x[i][m];
327 for (m = 0; m < ndim; m++)
332 if (ind_reset != NULL)
334 for (i = 0; i < nreset; i++)
336 rvec_dec(x[ind_reset[i]], xcm);
341 for (i = 0; i < nreset; i++)
348 void reset_x(int ncm, const atom_id *ind_cm,
349 int nreset, const atom_id *ind_reset,
350 rvec x[], const real mass[])
352 reset_x_ndim(3, ncm, ind_cm, nreset, ind_reset, x, mass);