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