Merge "Fix another bug in selection subexpression handling."
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_mk_angndx.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 "typedefs.h"
41 #include "smalloc.h"
42 #include "copyrite.h"
43 #include "statutil.h"
44 #include "macros.h"
45 #include "string2.h"
46 #include "futil.h"
47 #include "gmx_fatal.h"
48
49 static int calc_ntype(int nft, int *ft, t_idef *idef)
50 {
51     int  i, f, nf = 0;
52
53     for (i = 0; (i < idef->ntypes); i++)
54     {
55         for (f = 0; f < nft; f++)
56         {
57             if (idef->functype[i] == ft[f])
58             {
59                 nf++;
60             }
61         }
62     }
63
64     return nf;
65 }
66
67 static void fill_ft_ind(int nft, int *ft, t_idef *idef,
68                         int ft_ind[], char *grpnames[])
69 {
70     char buf[125];
71     int  i, f, ftype, ind = 0;
72
73     /* Loop over all the function types in the topology */
74     for (i = 0; (i < idef->ntypes); i++)
75     {
76         ft_ind[i] = -1;
77         /* Check all the selected function types */
78         for (f = 0; f < nft; f++)
79         {
80             ftype = ft[f];
81             if (idef->functype[i] == ftype)
82             {
83                 ft_ind[i] = ind;
84                 switch (ftype)
85                 {
86                     case F_ANGLES:
87                         sprintf(buf, "Theta=%.1f_%.2f", idef->iparams[i].harmonic.rA,
88                                 idef->iparams[i].harmonic.krA);
89                         break;
90                     case F_G96ANGLES:
91                         sprintf(buf, "Cos_th=%.1f_%.2f", idef->iparams[i].harmonic.rA,
92                                 idef->iparams[i].harmonic.krA);
93                         break;
94                     case F_UREY_BRADLEY:
95                         sprintf(buf, "UB_th=%.1f_%.2f2f", idef->iparams[i].u_b.thetaA,
96                                 idef->iparams[i].u_b.kthetaA);
97                         break;
98                     case F_QUARTIC_ANGLES:
99                         sprintf(buf, "Q_th=%.1f_%.2f_%.2f", idef->iparams[i].qangle.theta,
100                                 idef->iparams[i].qangle.c[0], idef->iparams[i].qangle.c[1]);
101                         break;
102                     case F_TABANGLES:
103                         sprintf(buf, "Table=%d_%.2f", idef->iparams[i].tab.table,
104                                 idef->iparams[i].tab.kA);
105                         break;
106                     case F_PDIHS:
107                         sprintf(buf, "Phi=%.1f_%d_%.2f", idef->iparams[i].pdihs.phiA,
108                                 idef->iparams[i].pdihs.mult, idef->iparams[i].pdihs.cpA);
109                         break;
110                     case F_IDIHS:
111                         sprintf(buf, "Xi=%.1f_%.2f", idef->iparams[i].harmonic.rA,
112                                 idef->iparams[i].harmonic.krA);
113                         break;
114                     case F_RBDIHS:
115                         sprintf(buf, "RB-A1=%.2f", idef->iparams[i].rbdihs.rbcA[1]);
116                         break;
117                     default:
118                         gmx_fatal(FARGS, "Unsupported function type '%s' selected",
119                                   interaction_function[ftype].longname);
120                 }
121                 grpnames[ind] = strdup(buf);
122                 ind++;
123             }
124         }
125     }
126 }
127
128 static void fill_ang(int nft, int *ft, int fac,
129                      int nr[], int *index[], int ft_ind[], t_topology *top,
130                      gmx_bool bNoH, real hq)
131 {
132     int         f, ftype, i, j, indg, nr_fac;
133     gmx_bool    bUse;
134     t_idef     *idef;
135     t_atom     *atom;
136     t_iatom    *ia;
137
138
139     idef = &top->idef;
140     atom = top->atoms.atom;
141
142     for (f = 0; f < nft; f++)
143     {
144         ftype = ft[f];
145         ia    = idef->il[ftype].iatoms;
146         for (i = 0; (i < idef->il[ftype].nr); )
147         {
148             indg = ft_ind[ia[0]];
149             if (indg == -1)
150             {
151                 gmx_incons("Routine fill_ang");
152             }
153             bUse = TRUE;
154             if (bNoH)
155             {
156                 for (j = 0; j < fac; j++)
157                 {
158                     if (atom[ia[1+j]].m < 1.5)
159                     {
160                         bUse = FALSE;
161                     }
162                 }
163             }
164             if (hq)
165             {
166                 for (j = 0; j < fac; j++)
167                 {
168                     if (atom[ia[1+j]].m < 1.5 && fabs(atom[ia[1+j]].q) < hq)
169                     {
170                         bUse = FALSE;
171                     }
172                 }
173             }
174             if (bUse)
175             {
176                 if (nr[indg] % 1000 == 0)
177                 {
178                     srenew(index[indg], fac*(nr[indg]+1000));
179                 }
180                 nr_fac = fac*nr[indg];
181                 for (j = 0; (j < fac); j++)
182                 {
183                     index[indg][nr_fac+j] = ia[j+1];
184                 }
185                 nr[indg]++;
186             }
187             ia += interaction_function[ftype].nratoms+1;
188             i  += interaction_function[ftype].nratoms+1;
189         }
190     }
191 }
192
193 static int *select_ftype(const char *opt, int *nft, int *mult)
194 {
195     int *ft = NULL, ftype;
196
197     if (opt[0] == 'a')
198     {
199         *mult = 3;
200         for (ftype = 0; ftype < F_NRE; ftype++)
201         {
202             if ((interaction_function[ftype].flags & IF_ATYPE) ||
203                 ftype == F_TABANGLES)
204             {
205                 (*nft)++;
206                 srenew(ft, *nft);
207                 ft[*nft-1] = ftype;
208             }
209         }
210     }
211     else
212     {
213         *mult = 4;
214         *nft  = 1;
215         snew(ft, *nft);
216         switch (opt[0])
217         {
218             case 'd':
219                 ft[0] = F_PDIHS;
220                 break;
221             case 'i':
222                 ft[0] = F_IDIHS;
223                 break;
224             case 'r':
225                 ft[0] = F_RBDIHS;
226                 break;
227             default:
228                 break;
229         }
230     }
231
232     return ft;
233 }
234
235 int gmx_mk_angndx(int argc, char *argv[])
236 {
237     static const char *desc[] = {
238         "[TT]mk_angndx[tt] makes an index file for calculation of",
239         "angle distributions etc. It uses a run input file ([TT].tpx[tt]) for the",
240         "definitions of the angles, dihedrals etc."
241     };
242     static const char *opt[] = { NULL, "angle", "dihedral", "improper", "ryckaert-bellemans", NULL };
243     static gmx_bool    bH    = TRUE;
244     static real        hq    = -1;
245     t_pargs            pa[]  = {
246         { "-type", FALSE, etENUM, {opt},
247           "Type of angle" },
248         { "-hyd", FALSE, etBOOL, {&bH},
249           "Include angles with atoms with mass < 1.5" },
250         { "-hq", FALSE, etREAL, {&hq},
251           "Ignore angles with atoms with mass < 1.5 and magnitude of their charge less than this value" }
252     };
253
254     output_env_t       oenv;
255     FILE              *out;
256     t_topology        *top;
257     int                i, j, ntype;
258     int                nft = 0, *ft, mult = 0;
259     int              **index;
260     int               *ft_ind;
261     int               *nr;
262     char             **grpnames;
263     t_filenm           fnm[] = {
264         { efTPX, NULL, NULL, ffREAD  },
265         { efNDX, NULL, "angle", ffWRITE }
266     };
267 #define NFILE asize(fnm)
268
269     parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa,
270                       asize(desc), desc, 0, NULL, &oenv);
271
272
273     ft = select_ftype(opt[0], &nft, &mult);
274
275     top = read_top(ftp2fn(efTPX, NFILE, fnm), NULL);
276
277     ntype = calc_ntype(nft, ft, &(top->idef));
278     snew(grpnames, ntype);
279     snew(ft_ind, top->idef.ntypes);
280     fill_ft_ind(nft, ft, &top->idef, ft_ind, grpnames);
281
282     snew(nr, ntype);
283     snew(index, ntype);
284     fill_ang(nft, ft, mult, nr, index, ft_ind, top, !bH, hq);
285
286     out = ftp2FILE(efNDX, NFILE, fnm, "w");
287     for (i = 0; (i < ntype); i++)
288     {
289         if (nr[i] > 0)
290         {
291             fprintf(out, "[ %s ]\n", grpnames[i]);
292             for (j = 0; (j < nr[i]*mult); j++)
293             {
294                 fprintf(out, " %5d", index[i][j]+1);
295                 if ((j % 12) == 11)
296                 {
297                     fprintf(out, "\n");
298                 }
299             }
300             fprintf(out, "\n");
301         }
302     }
303     ffclose(out);
304
305     thanx(stderr);
306
307     return 0;
308 }