342863854242244c3792786e2d80dba4b2e58b4d
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / topshake.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,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 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <ctype.h>
43 #include <math.h>
44
45 #include "gromacs/math/units.h"
46 #include "readir.h"
47 #include "gromacs/legacyheaders/typedefs.h"
48 #include "topshake.h"
49 #include "toppush.h"
50 #include "toputil.h"
51 #include "topdirs.h"
52 #include "gromacs/utility/smalloc.h"
53 #include "gromacs/utility/fatalerror.h"
54
55 static void copy_bond (t_params *pr, int to, int from)
56 /* copies an entry in a bond list to another position.
57  * does no allocing or freeing of memory
58  */
59 {
60     /*memcpy((char*) &(pr->param[to]),(char*) &(pr->param[from]),
61        (size_t)sizeof(pr->param[from]));*/
62     int i;
63
64     if (to != from)
65     {
66         range_check(to, 0, pr->nr);
67         range_check(from, 0, pr->nr);
68         for (i = 0; (i < MAXATOMLIST); i++)
69         {
70             pr->param[to].a[i] = pr->param[from].a[i];
71         }
72         for (i = 0; (i < MAXFORCEPARAM); i++)
73         {
74             pr->param[to].c[i] = pr->param[from].c[i];
75         }
76         for (i = 0; (i < MAXSLEN); i++)
77         {
78             pr->param[to].s[i] = pr->param[from].s[i];
79         }
80     }
81 }
82
83 static int count_hydrogens (char ***atomname, int nra, atom_id a[])
84 {
85     int  i, nh;
86
87     if (!atomname)
88     {
89         gmx_fatal(FARGS, "Cannot call count_hydrogens with no atomname (%s %d)",
90                   __FILE__, __LINE__);
91     }
92
93     nh = 0;
94     for (i = 0; (i < nra); i++)
95     {
96         if (toupper(**(atomname[a[i]])) == 'H')
97         {
98             nh++;
99         }
100     }
101
102     return nh;
103 }
104
105 void make_shake (t_params plist[], t_atoms *atoms, int nshake)
106 {
107     char          ***info = atoms->atomname;
108     t_params        *pr;
109     t_params        *bonds;
110     t_param          p, *bond, *ang;
111     real             b_ij, b_jk;
112     int              nb, b, i, j, ftype, ftype_a;
113     gmx_bool         bFound;
114
115     if (nshake != eshNONE)
116     {
117         switch (nshake)
118         {
119             case eshHBONDS:
120                 printf("turning H bonds into constraints...\n");
121                 break;
122             case eshALLBONDS:
123                 printf("turning all bonds into constraints...\n");
124                 break;
125             case eshHANGLES:
126                 printf("turning all bonds and H angles into constraints...\n");
127                 break;
128             case eshALLANGLES:
129                 printf("turning all bonds and angles into constraints...\n");
130                 break;
131             default:
132                 gmx_fatal(FARGS, "Invalid option for make_shake (%d)", nshake);
133         }
134
135         if ((nshake == eshHANGLES) || (nshake == eshALLANGLES))
136         {
137             /* Add all the angles with hydrogens to the shake list
138              * and remove them from the bond list
139              */
140             for (ftype = 0; (ftype < F_NRE); ftype++)
141             {
142                 if (interaction_function[ftype].flags & IF_BTYPE)
143                 {
144                     bonds = &(plist[ftype]);
145
146                     for (ftype_a = 0; (bonds->nr > 0 && ftype_a < F_NRE); ftype_a++)
147                     {
148                         if (interaction_function[ftype_a].flags & IF_ATYPE)
149                         {
150                             pr = &(plist[ftype_a]);
151
152                             for (i = 0; (i < pr->nr); )
153                             {
154                                 int numhydrogens;
155
156                                 ang = &(pr->param[i]);
157 #ifdef DEBUG
158                                 printf("Angle: %d-%d-%d\n", ang->AI, ang->AJ, ang->AK);
159 #endif
160                                 numhydrogens = count_hydrogens(info, 3, ang->a);
161                                 if ((nshake == eshALLANGLES) ||
162                                     (numhydrogens > 1) ||
163                                     (numhydrogens == 1 && toupper(**(info[ang->a[1]])) == 'O'))
164                                 {
165                                     /* Can only add hydrogen angle shake, if the two bonds
166                                      * are constrained.
167                                      * append this angle to the shake list
168                                      */
169                                     p.AI = ang->AI;
170                                     p.AJ = ang->AK;
171
172                                     /* Calculate length of constraint */
173                                     bFound = FALSE;
174                                     b_ij   = b_jk = 0.0;
175                                     for (j = 0; !bFound && (j < bonds->nr); j++)
176                                     {
177                                         bond = &(bonds->param[j]);
178                                         if (((bond->AI == ang->AI) &&
179                                              (bond->AJ == ang->AJ)) ||
180                                             ((bond->AI == ang->AJ) &&
181                                              (bond->AJ == ang->AI)))
182                                         {
183                                             b_ij = bond->C0;
184                                         }
185                                         if (((bond->AI == ang->AK) &&
186                                              (bond->AJ == ang->AJ)) ||
187                                             ((bond->AI == ang->AJ) &&
188                                              (bond->AJ == ang->AK)))
189                                         {
190                                             b_jk = bond->C0;
191                                         }
192                                         bFound = (b_ij != 0.0) && (b_jk != 0.0);
193                                     }
194                                     if (bFound)
195                                     {
196                                         /* apply law of cosines */
197                                         p.C0 = sqrt( b_ij*b_ij + b_jk*b_jk -
198                                                      2.0*b_ij*b_jk*cos(DEG2RAD*ang->C0) );
199                                         p.C1 = p.C0;
200 #ifdef DEBUG
201                                         printf("p: %d, q: %d, dist: %12.5e\n", p.AI, p.AJ, p.C0);
202 #endif
203                                         add_param_to_list (&(plist[F_CONSTR]), &p);
204                                         /* move the last bond to this position */
205                                         copy_bond (pr, i, pr->nr-1);
206                                         /* should free memory here!! */
207                                         pr->nr--;
208                                     }
209                                 }
210                                 else
211                                 {
212                                     i++;
213                                 }
214                             }
215                         } /* if IF_ATYPE */
216                     }     /* for ftype_A */
217                 }         /* if IF_BTYPE */
218             }             /* for ftype */
219         }                 /* if shake angles */
220
221         /* Add all the bonds with hydrogens to the shake list
222          * and remove them from the bond list
223          */
224         for (ftype = 0; (ftype < F_NRE); ftype++)
225         {
226             if (interaction_function[ftype].flags & IF_BTYPE)
227             {
228                 pr = &(plist[ftype]);
229                 j  = 0;
230                 for (i = 0; i < pr->nr; i++)
231                 {
232                     if ( (nshake != eshHBONDS) ||
233                          (count_hydrogens (info, 2, pr->param[i].a) > 0) )
234                     {
235                         /* append this bond to the shake list */
236                         p.AI = pr->param[i].AI;
237                         p.AJ = pr->param[i].AJ;
238                         p.C0 = pr->param[i].C0;
239                         p.C1 = pr->param[i].C2;
240                         add_param_to_list (&(plist[F_CONSTR]), &p);
241                     }
242                     else
243                     {
244                         copy_bond(pr, j++, i);
245                     }
246                 }
247                 pr->nr = j;
248             }
249         }
250     }
251 }