Updated docs for g_gyrate
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_gyrate.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) 2013,2014,2015, 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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <math.h>
42 #include <string.h>
43
44 #include "gromacs/commandline/pargs.h"
45 #include "sysstuff.h"
46 #include "typedefs.h"
47 #include "gromacs/utility/smalloc.h"
48 #include "macros.h"
49 #include "vec.h"
50 #include "pbc.h"
51 #include "gromacs/fileio/futil.h"
52 #include "index.h"
53 #include "mshift.h"
54 #include "xvgr.h"
55 #include "princ.h"
56 #include "rmpbc.h"
57 #include "txtdump.h"
58 #include "gromacs/fileio/tpxio.h"
59 #include "gromacs/fileio/trxio.h"
60 #include "gstat.h"
61 #include "gmx_ana.h"
62
63 #include "gromacs/legacyheaders/gmx_fatal.h"
64
65 real calc_gyro(rvec x[], int gnx, atom_id index[], t_atom atom[], real tm,
66                rvec gvec, rvec d, gmx_bool bQ, gmx_bool bRot, gmx_bool bMOI, matrix trans)
67 {
68     int    i, ii, m;
69     real   gyro, dx2, m0, Itot;
70     rvec   comp;
71
72     if (bRot)
73     {
74         principal_comp(gnx, index, atom, x, trans, d);
75         Itot = norm(d);
76         if (bMOI)
77         {
78             return Itot;
79         }
80         for (m = 0; (m < DIM); m++)
81         {
82             d[m] = sqrt(d[m]/tm);
83         }
84 #ifdef DEBUG
85         pr_rvecs(stderr, 0, "trans", trans, DIM);
86 #endif
87         /* rotate_atoms(gnx,index,x,trans); */
88     }
89     clear_rvec(comp);
90     for (i = 0; (i < gnx); i++)
91     {
92         ii = index[i];
93         if (bQ)
94         {
95             m0 = fabs(atom[ii].q);
96         }
97         else
98         {
99             m0 = atom[ii].m;
100         }
101         for (m = 0; (m < DIM); m++)
102         {
103             dx2      = x[ii][m]*x[ii][m];
104             comp[m] += dx2*m0;
105         }
106     }
107     gyro = comp[XX]+comp[YY]+comp[ZZ];
108
109     for (m = 0; (m < DIM); m++)
110     {
111         gvec[m] = sqrt((gyro-comp[m])/tm);
112     }
113
114     return sqrt(gyro/tm);
115 }
116
117 void calc_gyro_z(rvec x[], matrix box,
118                  int gnx, atom_id index[], t_atom atom[],
119                  int nz, real time, FILE *out)
120 {
121     static dvec   *inertia = NULL;
122     static double *tm      = NULL;
123     int            i, ii, j, zi;
124     real           zf, w, sdet, e1, e2;
125
126     if (inertia == NULL)
127     {
128         snew(inertia, nz);
129         snew(tm, nz);
130     }
131
132     for (i = 0; i < nz; i++)
133     {
134         clear_dvec(inertia[i]);
135         tm[i] = 0;
136     }
137
138     for (i = 0; (i < gnx); i++)
139     {
140         ii = index[i];
141         zf = nz*x[ii][ZZ]/box[ZZ][ZZ];
142         if (zf >= nz)
143         {
144             zf -= nz;
145         }
146         if (zf < 0)
147         {
148             zf += nz;
149         }
150         for (j = 0; j < 2; j++)
151         {
152             zi = zf + j;
153             if (zi == nz)
154             {
155                 zi = 0;
156             }
157             w               = atom[ii].m*(1 + cos(M_PI*(zf - zi)));
158             inertia[zi][0] += w*sqr(x[ii][YY]);
159             inertia[zi][1] += w*sqr(x[ii][XX]);
160             inertia[zi][2] -= w*x[ii][XX]*x[ii][YY];
161             tm[zi]         += w;
162         }
163     }
164     fprintf(out, "%10g", time);
165     for (j = 0; j < nz; j++)
166     {
167         for (i = 0; i < 3; i++)
168         {
169             inertia[j][i] /= tm[j];
170         }
171         sdet = sqrt(sqr(inertia[j][0] - inertia[j][1]) + 4*sqr(inertia[j][2]));
172         e1   = sqrt(0.5*(inertia[j][0] + inertia[j][1] + sdet));
173         e2   = sqrt(0.5*(inertia[j][0] + inertia[j][1] - sdet));
174         fprintf(out, " %5.3f %5.3f", e1, e2);
175     }
176     fprintf(out, "\n");
177 }
178
179
180 int gmx_gyrate(int argc, char *argv[])
181 {
182     const char     *desc[] = {
183         "[THISMODULE] computes the radius of gyration of a molecule",
184         "and the radii of gyration about the [IT]x[it]-, [IT]y[it]- and [IT]z[it]-axes,",
185         "as a function of time. The atoms are explicitly mass weighted.[PAR]",
186         "The axis components corresponds to the mass-weighted root-mean-square",
187         "of the radii components orthogonal to each axis, for example:[PAR]",
188         "Rg(x) = sqrt((sum_i m_i (R_i(y)^2 + R_i(z)^2))/(sum_i m_i)).[PAR]",
189         "With the [TT]-nmol[tt] option the radius of gyration will be calculated",
190         "for multiple molecules by splitting the analysis group in equally",
191         "sized parts.[PAR]",
192         "With the option [TT]-nz[tt] 2D radii of gyration in the [IT]x-y[it] plane",
193         "of slices along the [IT]z[it]-axis are calculated."
194     };
195     static int      nmol = 1, nz = 0;
196     static gmx_bool bQ   = FALSE, bRot = FALSE, bMOI = FALSE;
197     t_pargs         pa[] = {
198         { "-nmol", FALSE, etINT, {&nmol},
199           "The number of molecules to analyze" },
200         { "-q", FALSE, etBOOL, {&bQ},
201           "Use absolute value of the charge of an atom as weighting factor instead of mass" },
202         { "-p", FALSE, etBOOL, {&bRot},
203           "Calculate the radii of gyration about the principal axes." },
204         { "-moi", FALSE, etBOOL, {&bMOI},
205           "Calculate the moments of inertia (defined by the principal axes)." },
206         { "-nz", FALSE, etINT, {&nz},
207           "Calculate the 2D radii of gyration of this number of slices along the z-axis" },
208     };
209     FILE           *out;
210     t_trxstatus    *status;
211     t_topology      top;
212     int             ePBC;
213     rvec           *x, *x_s;
214     rvec            xcm, gvec, gvec1;
215     matrix          box, trans;
216     gmx_bool        bACF;
217     real          **moi_trans = NULL;
218     int             max_moi   = 0, delta_moi = 100;
219     rvec            d, d1; /* eigenvalues of inertia tensor */
220     real            t, t0, tm, gyro;
221     int             natoms;
222     char           *grpname, title[256];
223     int             i, j, m, gnx, nam, mol;
224     atom_id        *index;
225     output_env_t    oenv;
226     gmx_rmpbc_t     gpbc   = NULL;
227     const char     *leg[]  = { "Rg", "Rg\\sX\\N", "Rg\\sY\\N", "Rg\\sZ\\N" };
228     const char     *legI[] = { "Itot", "I1", "I2", "I3" };
229 #define NLEG asize(leg)
230     t_filenm        fnm[] = {
231         { efTRX, "-f",   NULL,       ffREAD },
232         { efTPS, NULL,   NULL,       ffREAD },
233         { efNDX, NULL,   NULL,       ffOPTRD },
234         { efXVG, NULL,   "gyrate",   ffWRITE },
235         { efXVG, "-acf", "moi-acf",  ffOPTWR },
236     };
237 #define NFILE asize(fnm)
238     int             npargs;
239     t_pargs        *ppa;
240
241     npargs = asize(pa);
242     ppa    = add_acf_pargs(&npargs, pa);
243
244     if (!parse_common_args(&argc, argv, PCA_CAN_TIME | PCA_CAN_VIEW | PCA_BE_NICE,
245                            NFILE, fnm, npargs, ppa, asize(desc), desc, 0, NULL, &oenv))
246     {
247         return 0;
248     }
249     bACF = opt2bSet("-acf", NFILE, fnm);
250     if (bACF && nmol != 1)
251     {
252         gmx_fatal(FARGS, "Can only do acf with nmol=1");
253     }
254     bRot = bRot || bMOI || bACF;
255     /*
256        if (nz > 0)
257        bMOI = TRUE;
258      */
259     if (bRot)
260     {
261         printf("Will rotate system along principal axes\n");
262         snew(moi_trans, DIM);
263     }
264     if (bMOI)
265     {
266         printf("Will print moments of inertia\n");
267         bQ = FALSE;
268     }
269     if (bQ)
270     {
271         printf("Will print radius normalised by charge\n");
272     }
273
274     read_tps_conf(ftp2fn(efTPS, NFILE, fnm), title, &top, &ePBC, &x, NULL, box, TRUE);
275     get_index(&top.atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &gnx, &index, &grpname);
276
277     if (nmol > gnx || gnx % nmol != 0)
278     {
279         gmx_fatal(FARGS, "The number of atoms in the group (%d) is not a multiple of nmol (%d)", gnx, nmol);
280     }
281     nam = gnx/nmol;
282
283     natoms = read_first_x(oenv, &status, ftp2fn(efTRX, NFILE, fnm), &t, &x, box);
284     snew(x_s, natoms);
285
286     j  = 0;
287     t0 = t;
288     if (bQ)
289     {
290         out = xvgropen(ftp2fn(efXVG, NFILE, fnm),
291                        "Radius of Charge (total and around axes)", "Time (ps)", "Rg (nm)", oenv);
292     }
293     else if (bMOI)
294     {
295         out = xvgropen(ftp2fn(efXVG, NFILE, fnm),
296                        "Moments of inertia (total and around axes)", "Time (ps)", "I (a.m.u. nm\\S2\\N)", oenv);
297     }
298     else
299     {
300         out = xvgropen(ftp2fn(efXVG, NFILE, fnm),
301                        "Radius of gyration (total and around axes)", "Time (ps)", "Rg (nm)", oenv);
302     }
303     if (bMOI)
304     {
305         xvgr_legend(out, NLEG, legI, oenv);
306     }
307     else
308     {
309         if (bRot)
310         {
311             if (output_env_get_print_xvgr_codes(oenv))
312             {
313                 fprintf(out, "@ subtitle \"Axes are principal component axes\"\n");
314             }
315         }
316         xvgr_legend(out, NLEG, leg, oenv);
317     }
318     if (nz == 0)
319     {
320         gpbc = gmx_rmpbc_init(&top.idef, ePBC, natoms);
321     }
322     do
323     {
324         if (nz == 0)
325         {
326             gmx_rmpbc_copy(gpbc, natoms, box, x, x_s);
327         }
328         gyro = 0;
329         clear_rvec(gvec);
330         clear_rvec(d);
331         for (mol = 0; mol < nmol; mol++)
332         {
333             tm    = sub_xcm(nz == 0 ? x_s : x, nam, index+mol*nam, top.atoms.atom, xcm, bQ);
334             if (nz == 0)
335             {
336                 gyro += calc_gyro(x_s, nam, index+mol*nam, top.atoms.atom,
337                                   tm, gvec1, d1, bQ, bRot, bMOI, trans);
338             }
339             else
340             {
341                 calc_gyro_z(x, box, nam, index+mol*nam, top.atoms.atom, nz, t, out);
342             }
343             rvec_inc(gvec, gvec1);
344             rvec_inc(d, d1);
345         }
346         if (nmol > 0)
347         {
348             gyro /= nmol;
349             svmul(1.0/nmol, gvec, gvec);
350             svmul(1.0/nmol, d, d);
351         }
352
353         if (nz == 0)
354         {
355             if (bRot)
356             {
357                 if (j >= max_moi)
358                 {
359                     max_moi += delta_moi;
360                     for (m = 0; (m < DIM); m++)
361                     {
362                         srenew(moi_trans[m], max_moi*DIM);
363                     }
364                 }
365                 for (m = 0; (m < DIM); m++)
366                 {
367                     copy_rvec(trans[m], moi_trans[m]+DIM*j);
368                 }
369                 fprintf(out, "%10g  %10g  %10g  %10g  %10g\n",
370                         t, gyro, d[XX], d[YY], d[ZZ]);
371             }
372             else
373             {
374                 fprintf(out, "%10g  %10g  %10g  %10g  %10g\n",
375                         t, gyro, gvec[XX], gvec[YY], gvec[ZZ]);
376             }
377         }
378         j++;
379     }
380     while (read_next_x(oenv, status, &t, x, box));
381     close_trj(status);
382     if (nz == 0)
383     {
384         gmx_rmpbc_done(gpbc);
385     }
386
387     gmx_ffclose(out);
388
389     if (bACF)
390     {
391         int mode = eacVector;
392
393         do_autocorr(opt2fn("-acf", NFILE, fnm), oenv,
394                     "Moment of inertia vector ACF",
395                     j, 3, moi_trans, (t-t0)/j, mode, FALSE);
396         do_view(oenv, opt2fn("-acf", NFILE, fnm), "-nxy");
397     }
398
399     do_view(oenv, ftp2fn(efXVG, NFILE, fnm), "-nxy");
400
401     return 0;
402 }