Merge libgmxana into libgromacs.
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_principal.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  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Green Red Orange Magenta Azure Cyan Skyblue
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <math.h>
40 #include <string.h>
41
42 #include "statutil.h"
43 #include "sysstuff.h"
44 #include "typedefs.h"
45 #include "smalloc.h"
46 #include "macros.h"
47 #include "vec.h"
48 #include "pbc.h"
49 #include "copyrite.h"
50 #include "futil.h"
51 #include "statutil.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 "tpxio.h"
59 #include "gstat.h"
60 #include "gmx_ana.h"
61
62
63 void
64 calc_principal_axes(t_topology *   top,
65                     rvec *         x,
66                     atom_id *      index,
67                     int            n,
68                     matrix         axes,
69                     rvec           inertia)
70 {
71     rvec   xcm;
72
73     sub_xcm(x, n, index, top->atoms.atom, xcm, FALSE);
74     principal_comp(n, index, top->atoms.atom, x, axes, inertia);
75 }
76
77 int gmx_principal(int argc, char *argv[])
78 {
79     const char     *desc[] = {
80         "[TT]g_principal[tt] calculates the three principal axes of inertia for a group",
81         "of atoms.",
82     };
83     static gmx_bool foo = FALSE;
84
85     t_pargs         pa[] = {
86         { "-foo",      FALSE, etBOOL, {&foo}, "Dummy option to avoid empty array" }
87     };
88     t_trxstatus    *status;
89     t_topology      top;
90     int             ePBC;
91     real            t;
92     rvec      *     x;
93
94     int             natoms;
95     char           *grpname, title[256];
96     int             i, j, m, gnx, nam, mol;
97     atom_id        *index;
98     rvec            a1, a2, a3, moi;
99     FILE      *     axis1;
100     FILE      *     axis2;
101     FILE      *     axis3;
102     FILE      *     fmoi;
103     matrix          axes, box;
104     output_env_t    oenv;
105     gmx_rmpbc_t     gpbc = NULL;
106
107
108     t_filenm fnm[] = {
109         { efTRX, "-f",   NULL,       ffREAD },
110         { efTPS, NULL,   NULL,       ffREAD },
111         { efNDX, NULL,   NULL,       ffOPTRD },
112         { efDAT, "-a1",  "axis1",    ffWRITE },
113         { efDAT, "-a2",  "axis2",    ffWRITE },
114         { efDAT, "-a3",  "axis3",    ffWRITE },
115         { efDAT, "-om",  "moi",      ffWRITE }
116     };
117 #define NFILE asize(fnm)
118
119     parse_common_args(&argc, argv,
120                       PCA_CAN_TIME | PCA_TIME_UNIT | PCA_CAN_VIEW | PCA_BE_NICE,
121                       NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv);
122
123     axis1 = ffopen(opt2fn("-a1", NFILE, fnm), "w");
124     axis2 = ffopen(opt2fn("-a2", NFILE, fnm), "w");
125     axis3 = ffopen(opt2fn("-a3", NFILE, fnm), "w");
126     fmoi  = ffopen(opt2fn("-om", NFILE, fnm), "w");
127
128     read_tps_conf(ftp2fn(efTPS, NFILE, fnm), title, &top, &ePBC, NULL, NULL, box, TRUE);
129
130     get_index(&top.atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &gnx, &index, &grpname);
131
132     natoms = read_first_x(oenv, &status, ftp2fn(efTRX, NFILE, fnm), &t, &x, box);
133
134     gpbc = gmx_rmpbc_init(&top.idef, ePBC, natoms, box);
135
136     do
137     {
138         gmx_rmpbc(gpbc, natoms, box, x);
139
140         calc_principal_axes(&top, x, index, gnx, axes, moi);
141
142         fprintf(axis1, "%15.10f     %15.10f  %15.10f  %15.10f\n", t, axes[XX][XX], axes[YY][XX], axes[ZZ][XX]);
143         fprintf(axis2, "%15.10f     %15.10f  %15.10f  %15.10f\n", t, axes[XX][YY], axes[YY][YY], axes[ZZ][YY]);
144         fprintf(axis3, "%15.10f     %15.10f  %15.10f  %15.10f\n", t, axes[XX][ZZ], axes[YY][ZZ], axes[ZZ][ZZ]);
145         fprintf(fmoi,  "%15.10f     %15.10f  %15.10f  %15.10f\n", t, moi[XX], moi[YY], moi[ZZ]);
146     }
147     while (read_next_x(oenv, status, &t, natoms, x, box));
148
149     gmx_rmpbc_done(gpbc);
150
151
152     close_trj(status);
153     ffclose(axis1);
154     ffclose(axis2);
155     ffclose(axis3);
156     ffclose(fmoi);
157
158     thanx(stderr);
159
160     return 0;
161 }