Remove more uses of typedefs.h
[alexxy/gromacs.git] / src / gromacs / gmxlib / topsort.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-2008, The GROMACS development team.
6  * Copyright (c) 2013,2014, 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
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "typedefs.h"
43 #include "topsort.h"
44 #include "gromacs/utility/smalloc.h"
45 #include "gromacs/utility/fatalerror.h"
46
47 static gmx_bool ip_pert(int ftype, const t_iparams *ip)
48 {
49     gmx_bool bPert;
50     int      i;
51
52     if (NRFPB(ftype) == 0)
53     {
54         return FALSE;
55     }
56
57     switch (ftype)
58     {
59         case F_BONDS:
60         case F_G96BONDS:
61         case F_HARMONIC:
62         case F_ANGLES:
63         case F_G96ANGLES:
64         case F_IDIHS:
65             bPert = (ip->harmonic.rA  != ip->harmonic.rB ||
66                      ip->harmonic.krA != ip->harmonic.krB);
67             break;
68         case F_MORSE:
69             bPert = (ip->morse.b0A  != ip->morse.b0B ||
70                      ip->morse.cbA  != ip->morse.cbB ||
71                      ip->morse.betaA  != ip->morse.betaB);
72             break;
73         case F_RESTRBONDS:
74             bPert = (ip->restraint.lowA  != ip->restraint.lowB ||
75                      ip->restraint.up1A  != ip->restraint.up1B ||
76                      ip->restraint.up2A  != ip->restraint.up2B ||
77                      ip->restraint.kA    != ip->restraint.kB);
78             break;
79         case F_UREY_BRADLEY:
80             bPert = (ip->u_b.thetaA  != ip->u_b.thetaB  ||
81                      ip->u_b.kthetaA != ip->u_b.kthetaB ||
82                      ip->u_b.r13A    != ip->u_b.r13B    ||
83                      ip->u_b.kUBA    != ip->u_b.kUBB);
84             break;
85         case F_PDIHS:
86         case F_PIDIHS:
87         case F_ANGRES:
88         case F_ANGRESZ:
89             bPert = (ip->pdihs.phiA != ip->pdihs.phiB ||
90                      ip->pdihs.cpA  != ip->pdihs.cpB);
91             break;
92         case F_RBDIHS:
93             bPert = FALSE;
94             for (i = 0; i < NR_RBDIHS; i++)
95             {
96                 if (ip->rbdihs.rbcA[i] != ip->rbdihs.rbcB[i])
97                 {
98                     bPert = TRUE;
99                 }
100             }
101             break;
102         case F_TABBONDS:
103         case F_TABBONDSNC:
104         case F_TABANGLES:
105         case F_TABDIHS:
106             bPert = (ip->tab.kA != ip->tab.kB);
107             break;
108         case F_POSRES:
109             bPert = FALSE;
110             for (i = 0; i < DIM; i++)
111             {
112                 if (ip->posres.pos0A[i] != ip->posres.pos0B[i] ||
113                     ip->posres.fcA[i]   != ip->posres.fcB[i])
114                 {
115                     bPert = TRUE;
116                 }
117             }
118             break;
119         case F_DIHRES:
120             bPert = ((ip->dihres.phiA != ip->dihres.phiB) ||
121                      (ip->dihres.dphiA != ip->dihres.dphiB) ||
122                      (ip->dihres.kfacA != ip->dihres.kfacB));
123             break;
124         case F_LJ14:
125             bPert = (ip->lj14.c6A  != ip->lj14.c6B ||
126                      ip->lj14.c12A != ip->lj14.c12B);
127             break;
128         case F_CMAP:
129             bPert = FALSE;
130             break;
131         case F_RESTRANGLES:
132         case F_RESTRDIHS:
133         case F_CBTDIHS:
134             bPert = FALSE;
135             gmx_fatal(FARGS, "Function type %s does not support currentely free energy calculations",
136                       interaction_function[ftype].longname);
137         default:
138             bPert = FALSE;
139             gmx_fatal(FARGS, "Function type %s not implemented in ip_pert",
140                       interaction_function[ftype].longname);
141     }
142
143     return bPert;
144 }
145
146 static gmx_bool ip_q_pert(int ftype, const t_iatom *ia,
147                           const t_iparams *ip, const real *qA, const real *qB)
148 {
149     /* 1-4 interactions do not have the charges stored in the iparams list,
150      * so we need a separate check for those.
151      */
152     return (ip_pert(ftype, ip+ia[0]) ||
153             (ftype == F_LJ14 && (qA[ia[1]] != qB[ia[1]] ||
154                                  qA[ia[2]] != qB[ia[2]])));
155 }
156
157 gmx_bool gmx_mtop_bondeds_free_energy(const gmx_mtop_t *mtop)
158 {
159     const gmx_ffparams_t *ffparams;
160     int                   i, ftype;
161     int                   mb;
162     t_atom               *atom;
163     t_ilist              *il;
164     t_iatom              *ia;
165     gmx_bool              bPert;
166
167     ffparams = &mtop->ffparams;
168
169     /* Loop over all the function types and compare the A/B parameters */
170     bPert = FALSE;
171     for (i = 0; i < ffparams->ntypes; i++)
172     {
173         ftype = ffparams->functype[i];
174         if (interaction_function[ftype].flags & IF_BOND)
175         {
176             if (ip_pert(ftype, &ffparams->iparams[i]))
177             {
178                 bPert = TRUE;
179             }
180         }
181     }
182
183     /* Check perturbed charges for 1-4 interactions */
184     for (mb = 0; mb < mtop->nmolblock; mb++)
185     {
186         atom = mtop->moltype[mtop->molblock[mb].type].atoms.atom;
187         il   = &mtop->moltype[mtop->molblock[mb].type].ilist[F_LJ14];
188         ia   = il->iatoms;
189         for (i = 0; i < il->nr; i += 3)
190         {
191             if (atom[ia[i+1]].q != atom[ia[i+1]].qB ||
192                 atom[ia[i+2]].q != atom[ia[i+2]].qB)
193             {
194                 bPert = TRUE;
195             }
196         }
197     }
198
199     return bPert;
200 }
201
202 void gmx_sort_ilist_fe(t_idef *idef, const real *qA, const real *qB)
203 {
204     int        ftype, nral, i, ic, ib, a;
205     t_iparams *iparams;
206     t_ilist   *ilist;
207     t_iatom   *iatoms;
208     gmx_bool   bPert;
209     t_iatom   *iabuf;
210     int        iabuf_nalloc;
211
212     if (qB == NULL)
213     {
214         qB = qA;
215     }
216
217     iabuf_nalloc = 0;
218     iabuf        = NULL;
219
220     iparams = idef->iparams;
221
222     for (ftype = 0; ftype < F_NRE; ftype++)
223     {
224         if (interaction_function[ftype].flags & IF_BOND)
225         {
226             ilist  = &idef->il[ftype];
227             iatoms = ilist->iatoms;
228             nral   = NRAL(ftype);
229             ic     = 0;
230             ib     = 0;
231             i      = 0;
232             while (i < ilist->nr)
233             {
234                 /* Check if this interaction is perturbed */
235                 if (ip_q_pert(ftype, iatoms+i, iparams, qA, qB))
236                 {
237                     /* Copy to the perturbed buffer */
238                     if (ib + 1 + nral > iabuf_nalloc)
239                     {
240                         iabuf_nalloc = over_alloc_large(ib+1+nral);
241                         srenew(iabuf, iabuf_nalloc);
242                     }
243                     for (a = 0; a < 1+nral; a++)
244                     {
245                         iabuf[ib++] = iatoms[i++];
246                     }
247                 }
248                 else
249                 {
250                     /* Copy in place */
251                     for (a = 0; a < 1+nral; a++)
252                     {
253                         iatoms[ic++] = iatoms[i++];
254                     }
255                 }
256             }
257             /* Now we now the number of non-perturbed interactions */
258             ilist->nr_nonperturbed = ic;
259
260             /* Copy the buffer with perturbed interactions to the ilist */
261             for (a = 0; a < ib; a++)
262             {
263                 iatoms[ic++] = iabuf[a];
264             }
265
266             if (debug)
267             {
268                 fprintf(debug, "%s non-pert %d pert %d\n",
269                         interaction_function[ftype].longname,
270                         ilist->nr_nonperturbed,
271                         ilist->nr-ilist->nr_nonperturbed);
272             }
273         }
274     }
275
276     sfree(iabuf);
277
278     idef->ilsort = ilsortFE_SORTED;
279 }