Fix extant references to former GPL licensing
[alexxy/gromacs.git] / src / tools / gmx_mk_angndx.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  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <math.h>
43 #include "typedefs.h"
44 #include "smalloc.h"
45 #include "copyrite.h"
46 #include "statutil.h"
47 #include "macros.h"
48 #include "string2.h"
49 #include "futil.h"
50 #include "gmx_fatal.h"
51 #include "gmx_ana.h"
52
53 static int calc_ntype(int nft, int *ft, t_idef *idef)
54 {
55     int  i, f, nf = 0;
56
57     for (i = 0; (i < idef->ntypes); i++)
58     {
59         for (f = 0; f < nft; f++)
60         {
61             if (idef->functype[i] == ft[f])
62             {
63                 nf++;
64             }
65         }
66     }
67
68     return nf;
69 }
70
71 static void fill_ft_ind(int nft, int *ft, t_idef *idef,
72                         int ft_ind[], char *grpnames[])
73 {
74     char buf[125];
75     int  i, f, ftype, ind = 0;
76
77     /* Loop over all the function types in the topology */
78     for (i = 0; (i < idef->ntypes); i++)
79     {
80         ft_ind[i] = -1;
81         /* Check all the selected function types */
82         for (f = 0; f < nft; f++)
83         {
84             ftype = ft[f];
85             if (idef->functype[i] == ftype)
86             {
87                 ft_ind[i] = ind;
88                 switch (ftype)
89                 {
90                     case F_ANGLES:
91                         sprintf(buf, "Theta=%.1f_%.2f", idef->iparams[i].harmonic.rA,
92                                 idef->iparams[i].harmonic.krA);
93                         break;
94                     case F_G96ANGLES:
95                         sprintf(buf, "Cos_th=%.1f_%.2f", idef->iparams[i].harmonic.rA,
96                                 idef->iparams[i].harmonic.krA);
97                         break;
98                     case F_UREY_BRADLEY:
99                         sprintf(buf, "UB_th=%.1f_%.2f2f", idef->iparams[i].u_b.thetaA,
100                                 idef->iparams[i].u_b.kthetaA);
101                         break;
102                     case F_QUARTIC_ANGLES:
103                         sprintf(buf, "Q_th=%.1f_%.2f_%.2f", idef->iparams[i].qangle.theta,
104                                 idef->iparams[i].qangle.c[0], idef->iparams[i].qangle.c[1]);
105                         break;
106                     case F_TABANGLES:
107                         sprintf(buf, "Table=%d_%.2f", idef->iparams[i].tab.table,
108                                 idef->iparams[i].tab.kA);
109                         break;
110                     case F_PDIHS:
111                         sprintf(buf, "Phi=%.1f_%d_%.2f", idef->iparams[i].pdihs.phiA,
112                                 idef->iparams[i].pdihs.mult, idef->iparams[i].pdihs.cpA);
113                         break;
114                     case F_IDIHS:
115                         sprintf(buf, "Xi=%.1f_%.2f", idef->iparams[i].harmonic.rA,
116                                 idef->iparams[i].harmonic.krA);
117                         break;
118                     case F_RBDIHS:
119                         sprintf(buf, "RB-A1=%.2f", idef->iparams[i].rbdihs.rbcA[1]);
120                         break;
121                     default:
122                         gmx_fatal(FARGS, "Unsupported function type '%s' selected",
123                                   interaction_function[ftype].longname);
124                 }
125                 grpnames[ind] = strdup(buf);
126                 ind++;
127             }
128         }
129     }
130 }
131
132 static void fill_ang(int nft, int *ft, int fac,
133                      int nr[], int *index[], int ft_ind[], t_topology *top,
134                      gmx_bool bNoH, real hq)
135 {
136     int         f, ftype, i, j, indg, nr_fac;
137     gmx_bool    bUse;
138     t_idef     *idef;
139     t_atom     *atom;
140     t_iatom    *ia;
141
142
143     idef = &top->idef;
144     atom = top->atoms.atom;
145
146     for (f = 0; f < nft; f++)
147     {
148         ftype = ft[f];
149         ia    = idef->il[ftype].iatoms;
150         for (i = 0; (i < idef->il[ftype].nr); )
151         {
152             indg = ft_ind[ia[0]];
153             if (indg == -1)
154             {
155                 gmx_incons("Routine fill_ang");
156             }
157             bUse = TRUE;
158             if (bNoH)
159             {
160                 for (j = 0; j < fac; j++)
161                 {
162                     if (atom[ia[1+j]].m < 1.5)
163                     {
164                         bUse = FALSE;
165                     }
166                 }
167             }
168             if (hq)
169             {
170                 for (j = 0; j < fac; j++)
171                 {
172                     if (atom[ia[1+j]].m < 1.5 && fabs(atom[ia[1+j]].q) < hq)
173                     {
174                         bUse = FALSE;
175                     }
176                 }
177             }
178             if (bUse)
179             {
180                 if (nr[indg] % 1000 == 0)
181                 {
182                     srenew(index[indg], fac*(nr[indg]+1000));
183                 }
184                 nr_fac = fac*nr[indg];
185                 for (j = 0; (j < fac); j++)
186                 {
187                     index[indg][nr_fac+j] = ia[j+1];
188                 }
189                 nr[indg]++;
190             }
191             ia += interaction_function[ftype].nratoms+1;
192             i  += interaction_function[ftype].nratoms+1;
193         }
194     }
195 }
196
197 static int *select_ftype(const char *opt, int *nft, int *mult)
198 {
199     int *ft = NULL, ftype;
200
201     if (opt[0] == 'a')
202     {
203         *mult = 3;
204         for (ftype = 0; ftype < F_NRE; ftype++)
205         {
206             if ((interaction_function[ftype].flags & IF_ATYPE) ||
207                 ftype == F_TABANGLES)
208             {
209                 (*nft)++;
210                 srenew(ft, *nft);
211                 ft[*nft-1] = ftype;
212             }
213         }
214     }
215     else
216     {
217         *mult = 4;
218         *nft  = 1;
219         snew(ft, *nft);
220         switch (opt[0])
221         {
222             case 'd':
223                 ft[0] = F_PDIHS;
224                 break;
225             case 'i':
226                 ft[0] = F_IDIHS;
227                 break;
228             case 'r':
229                 ft[0] = F_RBDIHS;
230                 break;
231             default:
232                 break;
233         }
234     }
235
236     return ft;
237 }
238
239 int gmx_mk_angndx(int argc, char *argv[])
240 {
241     static const char *desc[] = {
242         "[TT]mk_angndx[tt] makes an index file for calculation of",
243         "angle distributions etc. It uses a run input file ([TT].tpx[tt]) for the",
244         "definitions of the angles, dihedrals etc."
245     };
246     static const char *opt[] = { NULL, "angle", "dihedral", "improper", "ryckaert-bellemans", NULL };
247     static gmx_bool    bH    = TRUE;
248     static real        hq    = -1;
249     t_pargs            pa[]  = {
250         { "-type", FALSE, etENUM, {opt},
251           "Type of angle" },
252         { "-hyd", FALSE, etBOOL, {&bH},
253           "Include angles with atoms with mass < 1.5" },
254         { "-hq", FALSE, etREAL, {&hq},
255           "Ignore angles with atoms with mass < 1.5 and magnitude of their charge less than this value" }
256     };
257
258     output_env_t       oenv;
259     FILE              *out;
260     t_topology        *top;
261     int                i, j, ntype;
262     int                nft = 0, *ft, mult = 0;
263     int              **index;
264     int               *ft_ind;
265     int               *nr;
266     char             **grpnames;
267     t_filenm           fnm[] = {
268         { efTPX, NULL, NULL, ffREAD  },
269         { efNDX, NULL, "angle", ffWRITE }
270     };
271 #define NFILE asize(fnm)
272
273     CopyRight(stderr, argv[0]);
274     parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa,
275                       asize(desc), desc, 0, NULL, &oenv);
276
277
278     ft = select_ftype(opt[0], &nft, &mult);
279
280     top = read_top(ftp2fn(efTPX, NFILE, fnm), NULL);
281
282     ntype = calc_ntype(nft, ft, &(top->idef));
283     snew(grpnames, ntype);
284     snew(ft_ind, top->idef.ntypes);
285     fill_ft_ind(nft, ft, &top->idef, ft_ind, grpnames);
286
287     snew(nr, ntype);
288     snew(index, ntype);
289     fill_ang(nft, ft, mult, nr, index, ft_ind, top, !bH, hq);
290
291     out = ftp2FILE(efNDX, NFILE, fnm, "w");
292     for (i = 0; (i < ntype); i++)
293     {
294         if (nr[i] > 0)
295         {
296             fprintf(out, "[ %s ]\n", grpnames[i]);
297             for (j = 0; (j < nr[i]*mult); j++)
298             {
299                 fprintf(out, " %5d", index[i][j]+1);
300                 if ((j % 12) == 11)
301                 {
302                     fprintf(out, "\n");
303                 }
304             }
305             fprintf(out, "\n");
306         }
307     }
308     ffclose(out);
309
310     thanx(stderr);
311
312     return 0;
313 }