Reorganize shared unit test code
[alexxy/gromacs.git] / src / gromacs / gmxlib / nrama.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, 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 "sysstuff.h"
43 #include "smalloc.h"
44 #include "string2.h"
45 #include "typedefs.h"
46 #include "random.h"
47 #include "bondf.h"
48 #include "gromacs/fileio/futil.h"
49 #include "gmx_fatal.h"
50 #include "nrama.h"
51 #include "rmpbc.h"
52
53 static const char *pp_pat[] = { "C", "N", "CA", "C", "N" };
54 #define NPP (sizeof(pp_pat)/sizeof(pp_pat[0]))
55
56 static int d_comp(const void *a, const void *b)
57 {
58     t_dih *da, *db;
59
60     da = (t_dih *)a;
61     db = (t_dih *)b;
62
63     if (da->ai[1] < db->ai[1])
64     {
65         return -1;
66     }
67     else if (da->ai[1] == db->ai[1])
68     {
69         return (da->ai[2] - db->ai[2]);
70     }
71     else
72     {
73         return 1;
74     }
75 }
76
77
78 static void calc_dihs(t_xrama *xr)
79 {
80     int          i, t1, t2, t3;
81     rvec         r_ij, r_kj, r_kl, m, n;
82     real         sign;
83     t_dih       *dd;
84     gmx_rmpbc_t  gpbc = NULL;
85
86     gpbc = gmx_rmpbc_init(xr->idef, xr->ePBC, xr->natoms);
87     gmx_rmpbc(gpbc, xr->natoms, xr->box, xr->x);
88     gmx_rmpbc_done(gpbc);
89
90     for (i = 0; (i < xr->ndih); i++)
91     {
92         dd      = &(xr->dih[i]);
93         dd->ang = dih_angle(xr->x[dd->ai[0]], xr->x[dd->ai[1]],
94                             xr->x[dd->ai[2]], xr->x[dd->ai[3]],
95                             NULL,
96                             r_ij, r_kj, r_kl, m, n, &sign, &t1, &t2, &t3);
97     }
98 }
99
100 gmx_bool new_data(t_xrama *xr)
101 {
102     if (!read_next_x(xr->oenv, xr->traj, &xr->t, xr->x, xr->box))
103     {
104         return FALSE;
105     }
106
107     calc_dihs(xr);
108
109     return TRUE;
110 }
111
112 static int find_atom(const char *find, char ***names, int start, int nr)
113 {
114     int i;
115
116     for (i = start; (i < nr); i++)
117     {
118         if (strcmp(find, *names[i]) == 0)
119         {
120             return i;
121         }
122     }
123     return -1;
124 }
125
126 static void add_xr(t_xrama *xr, int ff[5], t_atoms *atoms)
127 {
128     char buf[12];
129     int  i;
130
131     srenew(xr->dih, xr->ndih+2);
132     for (i = 0; (i < 4); i++)
133     {
134         xr->dih[xr->ndih].ai[i] = ff[i];
135     }
136     for (i = 0; (i < 4); i++)
137     {
138         xr->dih[xr->ndih+1].ai[i] = ff[i+1];
139     }
140     xr->ndih += 2;
141
142     srenew(xr->pp, xr->npp+1);
143     xr->pp[xr->npp].iphi  = xr->ndih-2;
144     xr->pp[xr->npp].ipsi  = xr->ndih-1;
145     xr->pp[xr->npp].bShow = FALSE;
146     sprintf(buf, "%s-%d", *atoms->resinfo[atoms->atom[ff[1]].resind].name,
147             atoms->resinfo[atoms->atom[ff[1]].resind].nr);
148     xr->pp[xr->npp].label = strdup(buf);
149     xr->npp++;
150 }
151
152 static void get_dih(t_xrama *xr, t_atoms *atoms)
153 {
154     int    found, ff[NPP];
155     int    i;
156     size_t j;
157
158     for (i = 0; (i < atoms->nr); )
159     {
160         found = i;
161         for (j = 0; (j < NPP); j++)
162         {
163             if ((ff[j] = find_atom(pp_pat[j], atoms->atomname, found, atoms->nr)) == -1)
164             {
165                 break;
166             }
167             found = ff[j]+1;
168         }
169         if (j != NPP)
170         {
171             break;
172         }
173         add_xr(xr, ff, atoms);
174         i = ff[0]+1;
175     }
176     fprintf(stderr, "Found %d phi-psi combinations\n", xr->npp);
177 }
178
179 static int search_ff(int thisff[NPP], int ndih, int **ff)
180 {
181     int      j, k;
182     gmx_bool bFound = FALSE;
183
184     for (j = 0; (j < ndih); j++)
185     {
186         bFound = TRUE;
187         for (k = 1; (k <= 3); k++)
188         {
189             bFound = bFound && (thisff[k] == ff[j][k]);
190         }
191         if (bFound)
192         {
193             if (thisff[0] == -1)
194             {
195                 ff[j][4] = thisff[4];
196             }
197             else
198             {
199                 ff[j][0] = thisff[0];
200             }
201             break;
202         }
203     }
204     if (!bFound)
205     {
206         for (k = 0; (k < 5); k++)
207         {
208             ff[ndih][k] = thisff[k];
209         }
210         ndih++;
211     }
212     return ndih;
213 }
214
215 static void min_max(t_xrama *xr)
216 {
217     int ai, i, j;
218
219     xr->amin = xr->natoms;
220     xr->amax = 0;
221     for (i = 0; (i < xr->ndih); i++)
222     {
223         for (j = 0; (j < 4); j++)
224         {
225             ai = xr->dih[i].ai[j];
226             if (ai < xr->amin)
227             {
228                 xr->amin = ai;
229             }
230             else if (ai > xr->amax)
231             {
232                 xr->amax = ai;
233             }
234         }
235     }
236 }
237
238 static void get_dih_props(t_xrama *xr, t_idef *idef, int mult)
239 {
240     int      i, ft, ftype, nra;
241     t_iatom *ia;
242     t_dih   *dd, key;
243
244     ia = idef->il[F_PDIHS].iatoms;
245     for (i = 0; (i < idef->il[F_PDIHS].nr); )
246     {
247         ft    = ia[0];
248         ftype = idef->functype[ft];
249         nra   = interaction_function[ftype].nratoms;
250
251         if (ftype != F_PDIHS)
252         {
253             gmx_incons("ftype is not a dihedral");
254         }
255
256         key.ai[1] = ia[2];
257         key.ai[2] = ia[3];
258         if ((dd = (t_dih *)bsearch(&key, xr->dih, xr->ndih, (size_t)sizeof(key), d_comp))
259             != NULL)
260         {
261             dd->mult = idef->iparams[ft].pdihs.mult;
262             dd->phi0 = idef->iparams[ft].pdihs.phiA;
263         }
264
265         i  += nra+1;
266         ia += nra+1;
267     }
268     /* Fill in defaults for values not in the topology */
269     for (i = 0; (i < xr->ndih); i++)
270     {
271         if (xr->dih[i].mult == 0)
272         {
273             fprintf(stderr,
274                     "Dihedral around %d,%d not found in topology. Using mult=%d\n",
275                     xr->dih[i].ai[1], xr->dih[i].ai[2], mult);
276             xr->dih[i].mult = mult;
277             xr->dih[i].phi0 = 180;
278         }
279     }
280 }
281
282
283
284 t_topology *init_rama(const output_env_t oenv, const char *infile,
285                       const char *topfile, t_xrama *xr, int mult)
286 {
287     t_topology *top;
288     int         ePBC;
289     real        t;
290
291     top = read_top(topfile, &xr->ePBC);
292
293     /*get_dih2(xr,top->idef.functype,&(top->idef.bondeds),&(top->atoms));*/
294     get_dih(xr, &(top->atoms));
295     get_dih_props(xr, &(top->idef), mult);
296     xr->natoms = read_first_x(oenv, &xr->traj, infile, &t, &(xr->x), xr->box);
297     xr->idef   = &(top->idef);
298     xr->oenv   = oenv;
299
300     min_max(xr);
301     calc_dihs(xr);
302
303     return top;
304 }