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