Merge release-4-6 into release-5-0
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / gen_vsite.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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <math.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "gromacs/utility/cstringutil.h"
46 #include "gen_vsite.h"
47 #include "gromacs/utility/smalloc.h"
48 #include "resall.h"
49 #include "add_par.h"
50 #include "vec.h"
51 #include "toputil.h"
52 #include "physics.h"
53 #include "index.h"
54 #include "names.h"
55 #include "gromacs/fileio/futil.h"
56 #include "gpp_atomtype.h"
57 #include "fflibutil.h"
58 #include "macros.h"
59
60 #include "gmx_fatal.h"
61
62 #define MAXNAME 32
63 #define OPENDIR     '[' /* starting sign for directive          */
64 #define CLOSEDIR    ']' /* ending sign for directive            */
65
66 typedef struct {
67     char       atomtype[MAXNAME];  /* Type for the XH3/XH2 atom */
68     gmx_bool   isplanar;           /* If true, the atomtype above and the three connected
69                                     * ones are in a planar geometry. The two next entries
70                                     * are undefined in that case
71                                     */
72     int    nhydrogens;             /* number of connected hydrogens */
73     char   nextheavytype[MAXNAME]; /* Type for the heavy atom bonded to XH2/XH3 */
74     char   dummymass[MAXNAME];     /* The type of MNH* or MCH3* dummy mass to use */
75 } t_vsiteconf;
76
77
78 /* Structure to represent average bond and angles values in vsite aromatic
79  * residues. Note that these are NOT necessarily the bonds and angles from the
80  * forcefield; many forcefields (like Amber, OPLS) have some inherent strain in
81  * 5-rings (i.e. the sum of angles is !=540, but impropers keep it planar)
82  */
83 typedef struct {
84     char resname[MAXNAME];
85     int  nbonds;
86     int  nangles;
87     struct vsitetop_bond {
88         char   atom1[MAXNAME];
89         char   atom2[MAXNAME];
90         float  value;
91     } *bond; /* list of bonds */
92     struct vsitetop_angle {
93         char   atom1[MAXNAME];
94         char   atom2[MAXNAME];
95         char   atom3[MAXNAME];
96         float  value;
97     } *angle; /* list of angles */
98 } t_vsitetop;
99
100
101 enum {
102     DDB_CH3, DDB_NH3, DDB_NH2, DDB_PHE, DDB_TYR,
103     DDB_TRP, DDB_HISA, DDB_HISB, DDB_HISH, DDB_DIR_NR
104 };
105
106 typedef char t_dirname[STRLEN];
107
108 static const t_dirname ddb_dirnames[DDB_DIR_NR] = {
109     "CH3",
110     "NH3",
111     "NH2",
112     "PHE",
113     "TYR",
114     "TRP",
115     "HISA",
116     "HISB",
117     "HISH"
118 };
119
120 static int ddb_name2dir(char *name)
121 {
122     /* Translate a directive name to the number of the directive.
123      * HID/HIE/HIP names are translated to the ones we use in Gromacs.
124      */
125
126     int i, index;
127
128     index = -1;
129
130     for (i = 0; i < DDB_DIR_NR && index < 0; i++)
131     {
132         if (!gmx_strcasecmp(name, ddb_dirnames[i]))
133         {
134             index = i;
135         }
136     }
137
138     return index;
139 }
140
141
142 static void read_vsite_database(const char *ddbname,
143                                 t_vsiteconf **pvsiteconflist, int *nvsiteconf,
144                                 t_vsitetop **pvsitetoplist, int *nvsitetop)
145 {
146     /* This routine is a quick hack to fix the problem with hardcoded atomtypes
147      * and aromatic vsite parameters by reading them from a ff???.vsd file.
148      *
149      * The file can contain sections [ NH3 ], [ CH3 ], [ NH2 ], and ring residue names.
150      * For the NH3 and CH3 section each line has three fields. The first is the atomtype
151      * (nb: not bonded type) of the N/C atom to be replaced, the second field is
152      * the type of the next heavy atom it is bonded to, and the third field the type
153      * of dummy mass that will be used for this group.
154      *
155      * If the NH2 group planar (sp2 N) a different vsite construct is used, so in this
156      * case the second field should just be the word planar.
157      */
158
159     FILE        *ddb;
160     char         dirstr[STRLEN];
161     char         pline[STRLEN];
162     int          i, j, n, k, nvsite, ntop, curdir, prevdir;
163     t_vsiteconf *vsiteconflist;
164     t_vsitetop  *vsitetoplist;
165     char        *ch;
166     char         s1[MAXNAME], s2[MAXNAME], s3[MAXNAME], s4[MAXNAME];
167
168     ddb = libopen(ddbname);
169
170     nvsite        = *nvsiteconf;
171     vsiteconflist = *pvsiteconflist;
172     ntop          = *nvsitetop;
173     vsitetoplist  = *pvsitetoplist;
174
175     curdir = -1;
176
177     snew(vsiteconflist, 1);
178     snew(vsitetoplist, 1);
179
180     while (fgets2(pline, STRLEN-2, ddb) != NULL)
181     {
182         strip_comment(pline);
183         trim(pline);
184         if (strlen(pline) > 0)
185         {
186             if (pline[0] == OPENDIR)
187             {
188                 strncpy(dirstr, pline+1, STRLEN-2);
189                 if ((ch = strchr (dirstr, CLOSEDIR)) != NULL)
190                 {
191                     (*ch) = 0;
192                 }
193                 trim (dirstr);
194
195                 if (!gmx_strcasecmp(dirstr, "HID") ||
196                     !gmx_strcasecmp(dirstr, "HISD"))
197                 {
198                     sprintf(dirstr, "HISA");
199                 }
200                 else if (!gmx_strcasecmp(dirstr, "HIE") ||
201                          !gmx_strcasecmp(dirstr, "HISE"))
202                 {
203                     sprintf(dirstr, "HISB");
204                 }
205                 else if (!gmx_strcasecmp(dirstr, "HIP"))
206                 {
207                     sprintf(dirstr, "HISH");
208                 }
209
210                 curdir = ddb_name2dir(dirstr);
211                 if (curdir < 0)
212                 {
213                     gmx_fatal(FARGS, "Invalid directive %s in vsite database %s",
214                               dirstr, ddbname);
215                 }
216             }
217             else
218             {
219                 switch (curdir)
220                 {
221                     case -1:
222                         gmx_fatal(FARGS, "First entry in vsite database must be a directive.\n");
223                         break;
224                     case DDB_CH3:
225                     case DDB_NH3:
226                     case DDB_NH2:
227                         n = sscanf(pline, "%s%s%s", s1, s2, s3);
228                         if (n < 3 && !gmx_strcasecmp(s2, "planar"))
229                         {
230                             srenew(vsiteconflist, nvsite+1);
231                             strncpy(vsiteconflist[nvsite].atomtype, s1, MAXNAME-1);
232                             vsiteconflist[nvsite].isplanar         = TRUE;
233                             vsiteconflist[nvsite].nextheavytype[0] = 0;
234                             vsiteconflist[nvsite].dummymass[0]     = 0;
235                             vsiteconflist[nvsite].nhydrogens       = 2;
236                             nvsite++;
237                         }
238                         else if (n == 3)
239                         {
240                             srenew(vsiteconflist, (nvsite+1));
241                             strncpy(vsiteconflist[nvsite].atomtype, s1, MAXNAME-1);
242                             vsiteconflist[nvsite].isplanar = FALSE;
243                             strncpy(vsiteconflist[nvsite].nextheavytype, s2, MAXNAME-1);
244                             strncpy(vsiteconflist[nvsite].dummymass, s3, MAXNAME-1);
245                             if (curdir == DDB_NH2)
246                             {
247                                 vsiteconflist[nvsite].nhydrogens = 2;
248                             }
249                             else
250                             {
251                                 vsiteconflist[nvsite].nhydrogens = 3;
252                             }
253                             nvsite++;
254                         }
255                         else
256                         {
257                             gmx_fatal(FARGS, "Not enough directives in vsite database line: %s\n", pline);
258                         }
259                         break;
260                     case DDB_PHE:
261                     case DDB_TYR:
262                     case DDB_TRP:
263                     case DDB_HISA:
264                     case DDB_HISB:
265                     case DDB_HISH:
266                         i = 0;
267                         while ((i < ntop) && gmx_strcasecmp(dirstr, vsitetoplist[i].resname))
268                         {
269                             i++;
270                         }
271                         /* Allocate a new topology entry if this is a new residue */
272                         if (i == ntop)
273                         {
274                             srenew(vsitetoplist, ntop+1);
275                             ntop++; /* i still points to current vsite topology entry */
276                             strncpy(vsitetoplist[i].resname, dirstr, MAXNAME-1);
277                             vsitetoplist[i].nbonds = vsitetoplist[i].nangles = 0;
278                             snew(vsitetoplist[i].bond, 1);
279                             snew(vsitetoplist[i].angle, 1);
280                         }
281                         n = sscanf(pline, "%s%s%s%s", s1, s2, s3, s4);
282                         if (n == 3)
283                         {
284                             /* bond */
285                             k = vsitetoplist[i].nbonds++;
286                             srenew(vsitetoplist[i].bond, k+1);
287                             strncpy(vsitetoplist[i].bond[k].atom1, s1, MAXNAME-1);
288                             strncpy(vsitetoplist[i].bond[k].atom2, s2, MAXNAME-1);
289                             vsitetoplist[i].bond[k].value = strtod(s3, NULL);
290                         }
291                         else if (n == 4)
292                         {
293                             /* angle */
294                             k = vsitetoplist[i].nangles++;
295                             srenew(vsitetoplist[i].angle, k+1);
296                             strncpy(vsitetoplist[i].angle[k].atom1, s1, MAXNAME-1);
297                             strncpy(vsitetoplist[i].angle[k].atom2, s2, MAXNAME-1);
298                             strncpy(vsitetoplist[i].angle[k].atom3, s3, MAXNAME-1);
299                             vsitetoplist[i].angle[k].value = strtod(s4, NULL);
300                         }
301                         else
302                         {
303                             gmx_fatal(FARGS, "Need 3 or 4 values to specify bond/angle values in %s: %s\n", ddbname, pline);
304                         }
305                         break;
306                     default:
307                         gmx_fatal(FARGS, "Didnt find a case for directive %s in read_vsite_database\n", dirstr);
308                 }
309             }
310         }
311     }
312
313     *pvsiteconflist = vsiteconflist;
314     *pvsitetoplist  = vsitetoplist;
315     *nvsiteconf     = nvsite;
316     *nvsitetop      = ntop;
317
318     gmx_ffclose(ddb);
319 }
320
321 static int nitrogen_is_planar(t_vsiteconf vsiteconflist[], int nvsiteconf, char atomtype[])
322 {
323     /* Return 1 if atomtype exists in database list and is planar, 0 if not,
324      * and -1 if not found.
325      */
326     int      i, res;
327     gmx_bool found = FALSE;
328     for (i = 0; i < nvsiteconf && !found; i++)
329     {
330         found = (!gmx_strcasecmp(vsiteconflist[i].atomtype, atomtype) && (vsiteconflist[i].nhydrogens == 2));
331     }
332     if (found)
333     {
334         res = (vsiteconflist[i-1].isplanar == TRUE);
335     }
336     else
337     {
338         res = -1;
339     }
340
341     return res;
342 }
343
344 static char *get_dummymass_name(t_vsiteconf vsiteconflist[], int nvsiteconf, char atom[], char nextheavy[])
345 {
346     /* Return the dummy mass name if found, or NULL if not set in ddb database */
347     int      i;
348     gmx_bool found = FALSE;
349     for (i = 0; i < nvsiteconf && !found; i++)
350     {
351         found = (!gmx_strcasecmp(vsiteconflist[i].atomtype, atom) &&
352                  !gmx_strcasecmp(vsiteconflist[i].nextheavytype, nextheavy));
353     }
354     if (found)
355     {
356         return vsiteconflist[i-1].dummymass;
357     }
358     else
359     {
360         return NULL;
361     }
362 }
363
364
365
366 static real get_ddb_bond(t_vsitetop *vsitetop, int nvsitetop,
367                          const char res[],
368                          const char atom1[], const char atom2[])
369 {
370     int i, j;
371
372     i = 0;
373     while (i < nvsitetop && gmx_strcasecmp(res, vsitetop[i].resname))
374     {
375         i++;
376     }
377     if (i == nvsitetop)
378     {
379         gmx_fatal(FARGS, "No vsite information for residue %s found in vsite database.\n", res);
380     }
381     j = 0;
382     while (j < vsitetop[i].nbonds &&
383            ( strcmp(atom1, vsitetop[i].bond[j].atom1) || strcmp(atom2, vsitetop[i].bond[j].atom2)) &&
384            ( strcmp(atom2, vsitetop[i].bond[j].atom1) || strcmp(atom1, vsitetop[i].bond[j].atom2)))
385     {
386         j++;
387     }
388     if (j == vsitetop[i].nbonds)
389     {
390         gmx_fatal(FARGS, "Couldnt find bond %s-%s for residue %s in vsite database.\n", atom1, atom2, res);
391     }
392
393     return vsitetop[i].bond[j].value;
394 }
395
396
397 static real get_ddb_angle(t_vsitetop *vsitetop, int nvsitetop,
398                           const char res[], const char atom1[],
399                           const char atom2[], const char atom3[])
400 {
401     int i, j;
402
403     i = 0;
404     while (i < nvsitetop && gmx_strcasecmp(res, vsitetop[i].resname))
405     {
406         i++;
407     }
408     if (i == nvsitetop)
409     {
410         gmx_fatal(FARGS, "No vsite information for residue %s found in vsite database.\n", res);
411     }
412     j = 0;
413     while (j < vsitetop[i].nangles &&
414            ( strcmp(atom1, vsitetop[i].angle[j].atom1) ||
415              strcmp(atom2, vsitetop[i].angle[j].atom2) ||
416              strcmp(atom3, vsitetop[i].angle[j].atom3)) &&
417            ( strcmp(atom3, vsitetop[i].angle[j].atom1) ||
418              strcmp(atom2, vsitetop[i].angle[j].atom2) ||
419              strcmp(atom1, vsitetop[i].angle[j].atom3)))
420     {
421         j++;
422     }
423     if (j == vsitetop[i].nangles)
424     {
425         gmx_fatal(FARGS, "Couldnt find angle %s-%s-%s for residue %s in vsite database.\n", atom1, atom2, atom3, res);
426     }
427
428     return vsitetop[i].angle[j].value;
429 }
430
431
432 static void count_bonds(int atom, t_params *psb, char ***atomname,
433                         int *nrbonds, int *nrHatoms, int Hatoms[], int *Heavy,
434                         int *nrheavies, int heavies[])
435 {
436     int i, heavy, other, nrb, nrH, nrhv;
437
438     /* find heavy atom bound to this hydrogen */
439     heavy = NOTSET;
440     for (i = 0; (i < psb->nr) && (heavy == NOTSET); i++)
441     {
442         if (psb->param[i].AI == atom)
443         {
444             heavy = psb->param[i].AJ;
445         }
446         else if (psb->param[i].AJ == atom)
447         {
448             heavy = psb->param[i].AI;
449         }
450     }
451     if (heavy == NOTSET)
452     {
453         gmx_fatal(FARGS, "unbound hydrogen atom %d", atom+1);
454     }
455     /* find all atoms bound to heavy atom */
456     other = NOTSET;
457     nrb   = 0;
458     nrH   = 0;
459     nrhv  = 0;
460     for (i = 0; i < psb->nr; i++)
461     {
462         if (psb->param[i].AI == heavy)
463         {
464             other = psb->param[i].AJ;
465         }
466         else if (psb->param[i].AJ == heavy)
467         {
468             other = psb->param[i].AI;
469         }
470         if (other != NOTSET)
471         {
472             nrb++;
473             if (is_hydrogen(*(atomname[other])))
474             {
475                 Hatoms[nrH] = other;
476                 nrH++;
477             }
478             else
479             {
480                 heavies[nrhv] = other;
481                 nrhv++;
482             }
483             other = NOTSET;
484         }
485     }
486     *Heavy     = heavy;
487     *nrbonds   = nrb;
488     *nrHatoms  = nrH;
489     *nrheavies = nrhv;
490 }
491
492 static void print_bonds(FILE *fp, int o2n[],
493                         int nrHatoms, int Hatoms[], int Heavy,
494                         int nrheavies, int heavies[])
495 {
496     int i;
497
498     fprintf(fp, "Found: %d Hatoms: ", nrHatoms);
499     for (i = 0; i < nrHatoms; i++)
500     {
501         fprintf(fp, " %d", o2n[Hatoms[i]]+1);
502     }
503     fprintf(fp, "; %d Heavy atoms: %d", nrheavies+1, o2n[Heavy]+1);
504     for (i = 0; i < nrheavies; i++)
505     {
506         fprintf(fp, " %d", o2n[heavies[i]]+1);
507     }
508     fprintf(fp, "\n");
509 }
510
511 static int get_atype(int atom, t_atoms *at, int nrtp, t_restp rtp[],
512                      gmx_residuetype_t rt)
513 {
514     int      type;
515     gmx_bool bNterm;
516     int      j;
517     t_restp *rtpp;
518
519     if (at->atom[atom].m)
520     {
521         type = at->atom[atom].type;
522     }
523     else
524     {
525         /* get type from rtp */
526         rtpp   = get_restp(*(at->resinfo[at->atom[atom].resind].name), nrtp, rtp);
527         bNterm = gmx_residuetype_is_protein(rt, *(at->resinfo[at->atom[atom].resind].name)) &&
528             (at->atom[atom].resind == 0);
529         j    = search_jtype(rtpp, *(at->atomname[atom]), bNterm);
530         type = rtpp->atom[j].type;
531     }
532     return type;
533 }
534
535 static int vsite_nm2type(const char *name, gpp_atomtype_t atype)
536 {
537     int tp;
538
539     tp = get_atomtype_type(name, atype);
540     if (tp == NOTSET)
541     {
542         gmx_fatal(FARGS, "Dummy mass type (%s) not found in atom type database",
543                   name);
544     }
545
546     return tp;
547 }
548
549 static real get_amass(int atom, t_atoms *at, int nrtp, t_restp rtp[],
550                       gmx_residuetype_t rt)
551 {
552     real     mass;
553     gmx_bool bNterm;
554     int      j;
555     t_restp *rtpp;
556
557     if (at->atom[atom].m)
558     {
559         mass = at->atom[atom].m;
560     }
561     else
562     {
563         /* get mass from rtp */
564         rtpp   = get_restp(*(at->resinfo[at->atom[atom].resind].name), nrtp, rtp);
565         bNterm = gmx_residuetype_is_protein(rt, *(at->resinfo[at->atom[atom].resind].name)) &&
566             (at->atom[atom].resind == 0);
567         j    = search_jtype(rtpp, *(at->atomname[atom]), bNterm);
568         mass = rtpp->atom[j].m;
569     }
570     return mass;
571 }
572
573 static void my_add_param(t_params *plist, int ai, int aj, real b)
574 {
575     static real c[MAXFORCEPARAM] =
576     { NOTSET, NOTSET, NOTSET, NOTSET, NOTSET, NOTSET };
577
578     c[0] = b;
579     add_param(plist, ai, aj, c, NULL);
580 }
581
582 static void add_vsites(t_params plist[], int vsite_type[],
583                        int Heavy, int nrHatoms, int Hatoms[],
584                        int nrheavies, int heavies[])
585 {
586     int      i, j, ftype, other, moreheavy, bb;
587     gmx_bool bSwapParity;
588
589     for (i = 0; i < nrHatoms; i++)
590     {
591         ftype = vsite_type[Hatoms[i]];
592         /* Errors in setting the vsite_type should really be caugth earlier,
593          * because here it's not possible to print any useful error message.
594          * But it's still better to print a message than to segfault.
595          */
596         if (ftype == NOTSET)
597         {
598             gmx_incons("Undetected error in setting up virtual sites");
599         }
600         bSwapParity           = (ftype < 0);
601         vsite_type[Hatoms[i]] = ftype = abs(ftype);
602         if (ftype == F_BONDS)
603         {
604             if ( (nrheavies != 1) && (nrHatoms != 1) )
605             {
606                 gmx_fatal(FARGS, "cannot make constraint in add_vsites for %d heavy "
607                           "atoms and %d hydrogen atoms", nrheavies, nrHatoms);
608             }
609             my_add_param(&(plist[F_CONSTRNC]), Hatoms[i], heavies[0], NOTSET);
610         }
611         else
612         {
613             switch (ftype)
614             {
615                 case F_VSITE3:
616                 case F_VSITE3FD:
617                 case F_VSITE3OUT:
618                     if (nrheavies < 2)
619                     {
620                         gmx_fatal(FARGS, "Not enough heavy atoms (%d) for %s (min 3)",
621                                   nrheavies+1,
622                                   interaction_function[vsite_type[Hatoms[i]]].name);
623                     }
624                     add_vsite3_atoms(&plist[ftype], Hatoms[i], Heavy, heavies[0], heavies[1],
625                                      bSwapParity);
626                     break;
627                 case F_VSITE3FAD:
628                 {
629                     if (nrheavies > 1)
630                     {
631                         moreheavy = heavies[1];
632                     }
633                     else
634                     {
635                         /* find more heavy atoms */
636                         other = moreheavy = NOTSET;
637                         for (j = 0; (j < plist[F_BONDS].nr) && (moreheavy == NOTSET); j++)
638                         {
639                             if (plist[F_BONDS].param[j].AI == heavies[0])
640                             {
641                                 other = plist[F_BONDS].param[j].AJ;
642                             }
643                             else if (plist[F_BONDS].param[j].AJ == heavies[0])
644                             {
645                                 other = plist[F_BONDS].param[j].AI;
646                             }
647                             if ( (other != NOTSET) && (other != Heavy) )
648                             {
649                                 moreheavy = other;
650                             }
651                         }
652                         if (moreheavy == NOTSET)
653                         {
654                             gmx_fatal(FARGS, "Unbound molecule part %d-%d", Heavy+1, Hatoms[0]+1);
655                         }
656                     }
657                     add_vsite3_atoms(&plist[ftype], Hatoms[i], Heavy, heavies[0], moreheavy,
658                                      bSwapParity);
659                     break;
660                 }
661                 case F_VSITE4FD:
662                 case F_VSITE4FDN:
663                     if (nrheavies < 3)
664                     {
665                         gmx_fatal(FARGS, "Not enough heavy atoms (%d) for %s (min 4)",
666                                   nrheavies+1,
667                                   interaction_function[vsite_type[Hatoms[i]]].name);
668                     }
669                     add_vsite4_atoms(&plist[ftype],
670                                      Hatoms[0], Heavy, heavies[0], heavies[1], heavies[2]);
671                     break;
672
673                 default:
674                     gmx_fatal(FARGS, "can't use add_vsites for interaction function %s",
675                               interaction_function[vsite_type[Hatoms[i]]].name);
676             } /* switch ftype */
677         }     /* else */
678     }         /* for i */
679 }
680
681 #define ANGLE_6RING (DEG2RAD*120)
682
683 /* cosine rule: a^2 = b^2 + c^2 - 2 b c cos(alpha) */
684 /* get a^2 when a, b and alpha are given: */
685 #define cosrule(b, c, alpha) ( sqr(b) + sqr(c) - 2*b*c*cos(alpha) )
686 /* get cos(alpha) when a, b and c are given: */
687 #define acosrule(a, b, c) ( (sqr(b)+sqr(c)-sqr(a))/(2*b*c) )
688
689 static int gen_vsites_6ring(t_atoms *at, int *vsite_type[], t_params plist[],
690                             int nrfound, int *ats, real bond_cc, real bond_ch,
691                             real xcom, gmx_bool bDoZ)
692 {
693     /* these MUST correspond to the atnms array in do_vsite_aromatics! */
694     enum {
695         atCG, atCD1, atHD1, atCD2, atHD2, atCE1, atHE1, atCE2, atHE2,
696         atCZ, atHZ, atNR
697     };
698
699     int  i, nvsite;
700     real a, b, dCGCE, tmp1, tmp2, mtot, mG, mrest;
701     real xCG, yCG, xCE1, yCE1, xCE2, yCE2;
702     /* CG, CE1 and CE2 stay and each get a part of the total mass,
703      * so the c-o-m stays the same.
704      */
705
706     if (bDoZ)
707     {
708         if (atNR != nrfound)
709         {
710             gmx_incons("Generating vsites on 6-rings");
711         }
712     }
713
714     /* constraints between CG, CE1 and CE2: */
715     dCGCE = sqrt( cosrule(bond_cc, bond_cc, ANGLE_6RING) );
716     my_add_param(&(plist[F_CONSTRNC]), ats[atCG], ats[atCE1], dCGCE);
717     my_add_param(&(plist[F_CONSTRNC]), ats[atCG], ats[atCE2], dCGCE);
718     my_add_param(&(plist[F_CONSTRNC]), ats[atCE1], ats[atCE2], dCGCE);
719
720     /* rest will be vsite3 */
721     mtot   = 0;
722     nvsite = 0;
723     for (i = 0; i <  (bDoZ ? atNR : atHZ); i++)
724     {
725         mtot += at->atom[ats[i]].m;
726         if (i != atCG && i != atCE1 && i != atCE2 && (bDoZ || (i != atHZ && i != atCZ) ) )
727         {
728             at->atom[ats[i]].m    = at->atom[ats[i]].mB = 0;
729             (*vsite_type)[ats[i]] = F_VSITE3;
730             nvsite++;
731         }
732     }
733     /* Distribute mass so center-of-mass stays the same.
734      * The center-of-mass in the call is defined with x=0 at
735      * the CE1-CE2 bond and y=0 at the line from CG to the middle of CE1-CE2 bond.
736      */
737     xCG  = -bond_cc+bond_cc*cos(ANGLE_6RING);
738     yCG  = 0;
739     xCE1 = 0;
740     yCE1 = bond_cc*sin(0.5*ANGLE_6RING);
741     xCE2 = 0;
742     yCE2 = -bond_cc*sin(0.5*ANGLE_6RING);
743
744     mG                             = at->atom[ats[atCG]].m = at->atom[ats[atCG]].mB = xcom*mtot/xCG;
745     mrest                          = mtot-mG;
746     at->atom[ats[atCE1]].m         = at->atom[ats[atCE1]].mB =
747             at->atom[ats[atCE2]].m = at->atom[ats[atCE2]].mB = mrest / 2;
748
749     /* vsite3 construction: r_d = r_i + a r_ij + b r_ik */
750     tmp1  = dCGCE*sin(ANGLE_6RING*0.5);
751     tmp2  = bond_cc*cos(0.5*ANGLE_6RING) + tmp1;
752     tmp1 *= 2;
753     a     = b = -bond_ch / tmp1;
754     /* HE1 and HE2: */
755     add_vsite3_param(&plist[F_VSITE3],
756                      ats[atHE1], ats[atCE1], ats[atCE2], ats[atCG], a, b);
757     add_vsite3_param(&plist[F_VSITE3],
758                      ats[atHE2], ats[atCE2], ats[atCE1], ats[atCG], a, b);
759     /* CD1, CD2 and CZ: */
760     a = b = tmp2 / tmp1;
761     add_vsite3_param(&plist[F_VSITE3],
762                      ats[atCD1], ats[atCE2], ats[atCE1], ats[atCG], a, b);
763     add_vsite3_param(&plist[F_VSITE3],
764                      ats[atCD2], ats[atCE1], ats[atCE2], ats[atCG], a, b);
765     if (bDoZ)
766     {
767         add_vsite3_param(&plist[F_VSITE3],
768                          ats[atCZ], ats[atCG], ats[atCE1], ats[atCE2], a, b);
769     }
770     /* HD1, HD2 and HZ: */
771     a = b = ( bond_ch + tmp2 ) / tmp1;
772     add_vsite3_param(&plist[F_VSITE3],
773                      ats[atHD1], ats[atCE2], ats[atCE1], ats[atCG], a, b);
774     add_vsite3_param(&plist[F_VSITE3],
775                      ats[atHD2], ats[atCE1], ats[atCE2], ats[atCG], a, b);
776     if (bDoZ)
777     {
778         add_vsite3_param(&plist[F_VSITE3],
779                          ats[atHZ], ats[atCG], ats[atCE1], ats[atCE2], a, b);
780     }
781
782     return nvsite;
783 }
784
785 static int gen_vsites_phe(t_atoms *at, int *vsite_type[], t_params plist[],
786                           int nrfound, int *ats, t_vsitetop *vsitetop, int nvsitetop)
787 {
788     real bond_cc, bond_ch;
789     real xcom, mtot;
790     int  i;
791     /* these MUST correspond to the atnms array in do_vsite_aromatics! */
792     enum {
793         atCG, atCD1, atHD1, atCD2, atHD2, atCE1, atHE1, atCE2, atHE2,
794         atCZ, atHZ, atNR
795     };
796     real x[atNR], y[atNR];
797     /* Aromatic rings have 6-fold symmetry, so we only need one bond length.
798      * (angle is always 120 degrees).
799      */
800     bond_cc = get_ddb_bond(vsitetop, nvsitetop, "PHE", "CD1", "CE1");
801     bond_ch = get_ddb_bond(vsitetop, nvsitetop, "PHE", "CD1", "HD1");
802
803     x[atCG]  = -bond_cc+bond_cc*cos(ANGLE_6RING);
804     y[atCG]  = 0;
805     x[atCD1] = -bond_cc;
806     y[atCD1] = bond_cc*sin(0.5*ANGLE_6RING);
807     x[atHD1] = x[atCD1]+bond_ch*cos(ANGLE_6RING);
808     y[atHD1] = y[atCD1]+bond_ch*sin(ANGLE_6RING);
809     x[atCE1] = 0;
810     y[atCE1] = y[atCD1];
811     x[atHE1] = x[atCE1]-bond_ch*cos(ANGLE_6RING);
812     y[atHE1] = y[atCE1]+bond_ch*sin(ANGLE_6RING);
813     x[atCD2] = x[atCD1];
814     y[atCD2] = -y[atCD1];
815     x[atHD2] = x[atHD1];
816     y[atHD2] = -y[atHD1];
817     x[atCE2] = x[atCE1];
818     y[atCE2] = -y[atCE1];
819     x[atHE2] = x[atHE1];
820     y[atHE2] = -y[atHE1];
821     x[atCZ]  = bond_cc*cos(0.5*ANGLE_6RING);
822     y[atCZ]  = 0;
823     x[atHZ]  = x[atCZ]+bond_ch;
824     y[atHZ]  = 0;
825
826     xcom = mtot = 0;
827     for (i = 0; i < atNR; i++)
828     {
829         xcom += x[i]*at->atom[ats[i]].m;
830         mtot += at->atom[ats[i]].m;
831     }
832     xcom /= mtot;
833
834     return gen_vsites_6ring(at, vsite_type, plist, nrfound, ats, bond_cc, bond_ch, xcom, TRUE);
835 }
836
837 static void calc_vsite3_param(real xd, real yd, real xi, real yi, real xj, real yj,
838                               real xk, real yk, real *a, real *b)
839 {
840     /* determine parameters by solving the equation system, since we know the
841      * virtual site coordinates here.
842      */
843     real dx_ij, dx_ik, dy_ij, dy_ik;
844     real b_ij, b_ik;
845
846     dx_ij = xj-xi;
847     dy_ij = yj-yi;
848     dx_ik = xk-xi;
849     dy_ik = yk-yi;
850     b_ij  = sqrt(dx_ij*dx_ij+dy_ij*dy_ij);
851     b_ik  = sqrt(dx_ik*dx_ik+dy_ik*dy_ik);
852
853     *a = ( (xd-xi)*dy_ik - dx_ik*(yd-yi) ) / (dx_ij*dy_ik - dx_ik*dy_ij);
854     *b = ( yd - yi - (*a)*dy_ij ) / dy_ik;
855 }
856
857
858 static int gen_vsites_trp(gpp_atomtype_t atype, rvec *newx[],
859                           t_atom *newatom[], char ***newatomname[],
860                           int *o2n[], int *newvsite_type[], int *newcgnr[],
861                           t_symtab *symtab, int *nadd, rvec x[], int *cgnr[],
862                           t_atoms *at, int *vsite_type[], t_params plist[],
863                           int nrfound, int *ats, int add_shift,
864                           t_vsitetop *vsitetop, int nvsitetop)
865 {
866 #define NMASS 2
867     /* these MUST correspond to the atnms array in do_vsite_aromatics! */
868     enum {
869         atCB,  atCG,  atCD1, atHD1, atCD2, atNE1, atHE1, atCE2, atCE3, atHE3,
870         atCZ2, atHZ2, atCZ3, atHZ3, atCH2, atHH2, atNR
871     };
872     /* weights for determining the COM's of both rings (M1 and M2): */
873     real mw[NMASS][atNR] = {
874         {   0,     1,     1,     1,   0.5,     1,     1,   0.5,     0,     0,
875             0,     0,     0,     0,     0,     0 },
876         {   0,     0,     0,     0,   0.5,     0,     0,   0.5,     1,     1,
877             1,     1,     1,     1,     1,     1 }
878     };
879
880     real xi[atNR], yi[atNR];
881     real xcom[NMASS], ycom[NMASS], I, alpha;
882     real lineA, lineB, dist;
883     real b_CD2_CE2, b_NE1_CE2, b_CG_CD2, b_CH2_HH2, b_CE2_CZ2;
884     real b_NE1_HE1, b_CD2_CE3, b_CE3_CZ3, b_CB_CG;
885     real b_CZ2_CH2, b_CZ2_HZ2, b_CD1_HD1, b_CE3_HE3;
886     real b_CG_CD1, b_CZ3_HZ3;
887     real a_NE1_CE2_CD2, a_CE2_CD2_CG, a_CB_CG_CD2, a_CE2_CD2_CE3;
888     real a_CB_CG_CD1, a_CD2_CG_CD1, a_CE2_CZ2_HZ2, a_CZ2_CH2_HH2;
889     real a_CD2_CE2_CZ2, a_CD2_CE3_CZ3, a_CE3_CZ3_HZ3, a_CG_CD1_HD1;
890     real a_CE2_CZ2_CH2, a_HE1_NE1_CE2, a_CD2_CE3_HE3;
891     real xM[NMASS];
892     int  atM[NMASS], tpM, i, i0, j, nvsite;
893     real mwtot, mtot, mM[NMASS], dCBM1, dCBM2, dM1M2;
894     real a, b, c[MAXFORCEPARAM];
895     rvec r_ij, r_ik, t1, t2;
896     char name[10];
897
898     if (atNR != nrfound)
899     {
900         gmx_incons("atom types in gen_vsites_trp");
901     }
902     /* Get geometry from database */
903     b_CD2_CE2 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CD2", "CE2");
904     b_NE1_CE2 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "NE1", "CE2");
905     b_CG_CD1  = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CG", "CD1");
906     b_CG_CD2  = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CG", "CD2");
907     b_CB_CG   = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CB", "CG");
908     b_CE2_CZ2 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CE2", "CZ2");
909     b_CD2_CE3 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CD2", "CE3");
910     b_CE3_CZ3 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CE3", "CZ3");
911     b_CZ2_CH2 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CZ2", "CH2");
912
913     b_CD1_HD1 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CD1", "HD1");
914     b_CZ2_HZ2 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CZ2", "HZ2");
915     b_NE1_HE1 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "NE1", "HE1");
916     b_CH2_HH2 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CH2", "HH2");
917     b_CE3_HE3 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CE3", "HE3");
918     b_CZ3_HZ3 = get_ddb_bond(vsitetop, nvsitetop, "TRP", "CZ3", "HZ3");
919
920     a_NE1_CE2_CD2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "NE1", "CE2", "CD2");
921     a_CE2_CD2_CG  = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CE2", "CD2", "CG");
922     a_CB_CG_CD2   = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CB", "CG", "CD2");
923     a_CD2_CG_CD1  = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CD2", "CG", "CD1");
924     a_CB_CG_CD1   = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CB", "CG", "CD1");
925
926     a_CE2_CD2_CE3 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CE2", "CD2", "CE3");
927     a_CD2_CE2_CZ2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CD2", "CE2", "CZ2");
928     a_CD2_CE3_CZ3 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CD2", "CE3", "CZ3");
929     a_CE3_CZ3_HZ3 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CE3", "CZ3", "HZ3");
930     a_CZ2_CH2_HH2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CZ2", "CH2", "HH2");
931     a_CE2_CZ2_HZ2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CE2", "CZ2", "HZ2");
932     a_CE2_CZ2_CH2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CE2", "CZ2", "CH2");
933     a_CG_CD1_HD1  = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CG", "CD1", "HD1");
934     a_HE1_NE1_CE2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "HE1", "NE1", "CE2");
935     a_CD2_CE3_HE3 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TRP", "CD2", "CE3", "HE3");
936
937     /* Calculate local coordinates.
938      * y-axis (x=0) is the bond CD2-CE2.
939      * x-axis (y=0) is perpendicular to the bond CD2-CE2 and
940      * intersects the middle of the bond.
941      */
942     xi[atCD2] = 0;
943     yi[atCD2] = -0.5*b_CD2_CE2;
944
945     xi[atCE2] = 0;
946     yi[atCE2] = 0.5*b_CD2_CE2;
947
948     xi[atNE1] = -b_NE1_CE2*sin(a_NE1_CE2_CD2);
949     yi[atNE1] = yi[atCE2]-b_NE1_CE2*cos(a_NE1_CE2_CD2);
950
951     xi[atCG] = -b_CG_CD2*sin(a_CE2_CD2_CG);
952     yi[atCG] = yi[atCD2]+b_CG_CD2*cos(a_CE2_CD2_CG);
953
954     alpha    = a_CE2_CD2_CG + M_PI - a_CB_CG_CD2;
955     xi[atCB] = xi[atCG]-b_CB_CG*sin(alpha);
956     yi[atCB] = yi[atCG]+b_CB_CG*cos(alpha);
957
958     alpha     = a_CE2_CD2_CG + a_CD2_CG_CD1 - M_PI;
959     xi[atCD1] = xi[atCG]-b_CG_CD1*sin(alpha);
960     yi[atCD1] = yi[atCG]+b_CG_CD1*cos(alpha);
961
962     xi[atCE3] = b_CD2_CE3*sin(a_CE2_CD2_CE3);
963     yi[atCE3] = yi[atCD2]+b_CD2_CE3*cos(a_CE2_CD2_CE3);
964
965     xi[atCZ2] = b_CE2_CZ2*sin(a_CD2_CE2_CZ2);
966     yi[atCZ2] = yi[atCE2]-b_CE2_CZ2*cos(a_CD2_CE2_CZ2);
967
968     alpha     = a_CE2_CD2_CE3 + a_CD2_CE3_CZ3 - M_PI;
969     xi[atCZ3] = xi[atCE3]+b_CE3_CZ3*sin(alpha);
970     yi[atCZ3] = yi[atCE3]+b_CE3_CZ3*cos(alpha);
971
972     alpha     = a_CD2_CE2_CZ2 + a_CE2_CZ2_CH2 - M_PI;
973     xi[atCH2] = xi[atCZ2]+b_CZ2_CH2*sin(alpha);
974     yi[atCH2] = yi[atCZ2]-b_CZ2_CH2*cos(alpha);
975
976     /* hydrogens */
977     alpha     = a_CE2_CD2_CG + a_CD2_CG_CD1 - a_CG_CD1_HD1;
978     xi[atHD1] = xi[atCD1]-b_CD1_HD1*sin(alpha);
979     yi[atHD1] = yi[atCD1]+b_CD1_HD1*cos(alpha);
980
981     alpha     = a_NE1_CE2_CD2 + M_PI - a_HE1_NE1_CE2;
982     xi[atHE1] = xi[atNE1]-b_NE1_HE1*sin(alpha);
983     yi[atHE1] = yi[atNE1]-b_NE1_HE1*cos(alpha);
984
985     alpha     = a_CE2_CD2_CE3 + M_PI - a_CD2_CE3_HE3;
986     xi[atHE3] = xi[atCE3]+b_CE3_HE3*sin(alpha);
987     yi[atHE3] = yi[atCE3]+b_CE3_HE3*cos(alpha);
988
989     alpha     = a_CD2_CE2_CZ2 + M_PI - a_CE2_CZ2_HZ2;
990     xi[atHZ2] = xi[atCZ2]+b_CZ2_HZ2*sin(alpha);
991     yi[atHZ2] = yi[atCZ2]-b_CZ2_HZ2*cos(alpha);
992
993     alpha     = a_CD2_CE2_CZ2 + a_CE2_CZ2_CH2 - a_CZ2_CH2_HH2;
994     xi[atHZ3] = xi[atCZ3]+b_CZ3_HZ3*sin(alpha);
995     yi[atHZ3] = yi[atCZ3]+b_CZ3_HZ3*cos(alpha);
996
997     alpha     = a_CE2_CD2_CE3 + a_CD2_CE3_CZ3 - a_CE3_CZ3_HZ3;
998     xi[atHH2] = xi[atCH2]+b_CH2_HH2*sin(alpha);
999     yi[atHH2] = yi[atCH2]-b_CH2_HH2*cos(alpha);
1000
1001     /* Determine coeff. for the line CB-CG */
1002     lineA = (yi[atCB]-yi[atCG])/(xi[atCB]-xi[atCG]);
1003     lineB = yi[atCG]-lineA*xi[atCG];
1004
1005     /* Calculate masses for each ring and put it on the dummy masses */
1006     for (j = 0; j < NMASS; j++)
1007     {
1008         mM[j] = xcom[j] = ycom[j] = 0;
1009     }
1010     for (i = 0; i < atNR; i++)
1011     {
1012         if (i != atCB)
1013         {
1014             for (j = 0; j < NMASS; j++)
1015             {
1016                 mM[j]   += mw[j][i] * at->atom[ats[i]].m;
1017                 xcom[j] += xi[i] * mw[j][i] * at->atom[ats[i]].m;
1018                 ycom[j] += yi[i] * mw[j][i] * at->atom[ats[i]].m;
1019             }
1020         }
1021     }
1022     for (j = 0; j < NMASS; j++)
1023     {
1024         xcom[j] /= mM[j];
1025         ycom[j] /= mM[j];
1026     }
1027
1028     /* get dummy mass type */
1029     tpM = vsite_nm2type("MW", atype);
1030     /* make space for 2 masses: shift all atoms starting with CB */
1031     i0 = ats[atCB];
1032     for (j = 0; j < NMASS; j++)
1033     {
1034         atM[j] = i0+*nadd+j;
1035     }
1036     if (debug)
1037     {
1038         fprintf(stderr, "Inserting %d dummy masses at %d\n", NMASS, (*o2n)[i0]+1);
1039     }
1040     *nadd += NMASS;
1041     for (j = i0; j < at->nr; j++)
1042     {
1043         (*o2n)[j] = j+*nadd;
1044     }
1045     srenew(*newx, at->nr+*nadd);
1046     srenew(*newatom, at->nr+*nadd);
1047     srenew(*newatomname, at->nr+*nadd);
1048     srenew(*newvsite_type, at->nr+*nadd);
1049     srenew(*newcgnr, at->nr+*nadd);
1050     for (j = 0; j < NMASS; j++)
1051     {
1052         (*newatomname)[at->nr+*nadd-1-j] = NULL;
1053     }
1054
1055     /* Dummy masses will be placed at the center-of-mass in each ring. */
1056
1057     /* calc initial position for dummy masses in real (non-local) coordinates.
1058      * Cheat by using the routine to calculate virtual site parameters. It is
1059      * much easier when we have the coordinates expressed in terms of
1060      * CB, CG, CD2.
1061      */
1062     rvec_sub(x[ats[atCB]], x[ats[atCG]], r_ij);
1063     rvec_sub(x[ats[atCD2]], x[ats[atCG]], r_ik);
1064     calc_vsite3_param(xcom[0], ycom[0], xi[atCG], yi[atCG], xi[atCB], yi[atCB],
1065                       xi[atCD2], yi[atCD2], &a, &b);
1066     svmul(a, r_ij, t1);
1067     svmul(b, r_ik, t2);
1068     rvec_add(t1, t2, t1);
1069     rvec_add(t1, x[ats[atCG]], (*newx)[atM[0]]);
1070
1071     calc_vsite3_param(xcom[1], ycom[1], xi[atCG], yi[atCG], xi[atCB], yi[atCB],
1072                       xi[atCD2], yi[atCD2], &a, &b);
1073     svmul(a, r_ij, t1);
1074     svmul(b, r_ik, t2);
1075     rvec_add(t1, t2, t1);
1076     rvec_add(t1, x[ats[atCG]], (*newx)[atM[1]]);
1077
1078     /* set parameters for the masses */
1079     for (j = 0; j < NMASS; j++)
1080     {
1081         sprintf(name, "MW%d", j+1);
1082         (*newatomname)  [atM[j]]         = put_symtab(symtab, name);
1083         (*newatom)      [atM[j]].m       = (*newatom)[atM[j]].mB    = mM[j];
1084         (*newatom)      [atM[j]].q       = (*newatom)[atM[j]].qB    = 0.0;
1085         (*newatom)      [atM[j]].type    = (*newatom)[atM[j]].typeB = tpM;
1086         (*newatom)      [atM[j]].ptype   = eptAtom;
1087         (*newatom)      [atM[j]].resind  = at->atom[i0].resind;
1088         (*newatom)      [atM[j]].elem[0] = 'M';
1089         (*newatom)      [atM[j]].elem[1] = '\0';
1090         (*newvsite_type)[atM[j]]         = NOTSET;
1091         (*newcgnr)      [atM[j]]         = (*cgnr)[i0];
1092     }
1093     /* renumber cgnr: */
1094     for (i = i0; i < at->nr; i++)
1095     {
1096         (*cgnr)[i]++;
1097     }
1098
1099     /* constraints between CB, M1 and M2 */
1100     /* 'add_shift' says which atoms won't be renumbered afterwards */
1101     dCBM1 = sqrt( sqr(xcom[0]-xi[atCB]) + sqr(ycom[0]-yi[atCB]) );
1102     dM1M2 = sqrt( sqr(xcom[0]-xcom[1]) + sqr(ycom[0]-ycom[1]) );
1103     dCBM2 = sqrt( sqr(xcom[1]-xi[atCB]) + sqr(ycom[1]-yi[atCB]) );
1104     my_add_param(&(plist[F_CONSTRNC]), ats[atCB],       add_shift+atM[0], dCBM1);
1105     my_add_param(&(plist[F_CONSTRNC]), ats[atCB],       add_shift+atM[1], dCBM2);
1106     my_add_param(&(plist[F_CONSTRNC]), add_shift+atM[0], add_shift+atM[1], dM1M2);
1107
1108     /* rest will be vsite3 */
1109     nvsite = 0;
1110     for (i = 0; i < atNR; i++)
1111     {
1112         if (i != atCB)
1113         {
1114             at->atom[ats[i]].m    = at->atom[ats[i]].mB = 0;
1115             (*vsite_type)[ats[i]] = F_VSITE3;
1116             nvsite++;
1117         }
1118     }
1119
1120     /* now define all vsites from M1, M2, CB, ie:
1121        r_d = r_M1 + a r_M1_M2 + b r_M1_CB */
1122     for (i = 0; i < atNR; i++)
1123     {
1124         if ( (*vsite_type)[ats[i]] == F_VSITE3)
1125         {
1126             calc_vsite3_param(xi[i], yi[i], xcom[0], ycom[0], xcom[1], ycom[1], xi[atCB], yi[atCB], &a, &b);
1127             add_vsite3_param(&plist[F_VSITE3],
1128                              ats[i], add_shift+atM[0], add_shift+atM[1], ats[atCB], a, b);
1129         }
1130     }
1131     return nvsite;
1132 #undef NMASS
1133 }
1134
1135
1136 static int gen_vsites_tyr(gpp_atomtype_t atype, rvec *newx[],
1137                           t_atom *newatom[], char ***newatomname[],
1138                           int *o2n[], int *newvsite_type[], int *newcgnr[],
1139                           t_symtab *symtab, int *nadd, rvec x[], int *cgnr[],
1140                           t_atoms *at, int *vsite_type[], t_params plist[],
1141                           int nrfound, int *ats, int add_shift,
1142                           t_vsitetop *vsitetop, int nvsitetop)
1143 {
1144     int  nvsite, i, i0, j, atM, tpM;
1145     real dCGCE, dCEOH, dCGM, tmp1, a, b;
1146     real bond_cc, bond_ch, bond_co, bond_oh, angle_coh;
1147     real xcom, mtot;
1148     real vmass, vdist, mM;
1149     rvec r1;
1150     char name[10];
1151
1152     /* these MUST correspond to the atnms array in do_vsite_aromatics! */
1153     enum {
1154         atCG, atCD1, atHD1, atCD2, atHD2, atCE1, atHE1, atCE2, atHE2,
1155         atCZ, atOH, atHH, atNR
1156     };
1157     real xi[atNR], yi[atNR];
1158     /* CG, CE1, CE2 (as in general 6-ring) and OH and HH stay,
1159        rest gets virtualized.
1160        Now we have two linked triangles with one improper keeping them flat */
1161     if (atNR != nrfound)
1162     {
1163         gmx_incons("Number of atom types in gen_vsites_tyr");
1164     }
1165
1166     /* Aromatic rings have 6-fold symmetry, so we only need one bond length
1167      * for the ring part (angle is always 120 degrees).
1168      */
1169     bond_cc   = get_ddb_bond(vsitetop, nvsitetop, "TYR", "CD1", "CE1");
1170     bond_ch   = get_ddb_bond(vsitetop, nvsitetop, "TYR", "CD1", "HD1");
1171     bond_co   = get_ddb_bond(vsitetop, nvsitetop, "TYR", "CZ", "OH");
1172     bond_oh   = get_ddb_bond(vsitetop, nvsitetop, "TYR", "OH", "HH");
1173     angle_coh = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, "TYR", "CZ", "OH", "HH");
1174
1175     xi[atCG]  = -bond_cc+bond_cc*cos(ANGLE_6RING);
1176     yi[atCG]  = 0;
1177     xi[atCD1] = -bond_cc;
1178     yi[atCD1] = bond_cc*sin(0.5*ANGLE_6RING);
1179     xi[atHD1] = xi[atCD1]+bond_ch*cos(ANGLE_6RING);
1180     yi[atHD1] = yi[atCD1]+bond_ch*sin(ANGLE_6RING);
1181     xi[atCE1] = 0;
1182     yi[atCE1] = yi[atCD1];
1183     xi[atHE1] = xi[atCE1]-bond_ch*cos(ANGLE_6RING);
1184     yi[atHE1] = yi[atCE1]+bond_ch*sin(ANGLE_6RING);
1185     xi[atCD2] = xi[atCD1];
1186     yi[atCD2] = -yi[atCD1];
1187     xi[atHD2] = xi[atHD1];
1188     yi[atHD2] = -yi[atHD1];
1189     xi[atCE2] = xi[atCE1];
1190     yi[atCE2] = -yi[atCE1];
1191     xi[atHE2] = xi[atHE1];
1192     yi[atHE2] = -yi[atHE1];
1193     xi[atCZ]  = bond_cc*cos(0.5*ANGLE_6RING);
1194     yi[atCZ]  = 0;
1195     xi[atOH]  = xi[atCZ]+bond_co;
1196     yi[atOH]  = 0;
1197
1198     xcom = mtot = 0;
1199     for (i = 0; i < atOH; i++)
1200     {
1201         xcom += xi[i]*at->atom[ats[i]].m;
1202         mtot += at->atom[ats[i]].m;
1203     }
1204     xcom /= mtot;
1205
1206     /* first do 6 ring as default,
1207        except CZ (we'll do that different) and HZ (we don't have that): */
1208     nvsite = gen_vsites_6ring(at, vsite_type, plist, nrfound, ats, bond_cc, bond_ch, xcom, FALSE);
1209
1210     /* then construct CZ from the 2nd triangle */
1211     /* vsite3 construction: r_d = r_i + a r_ij + b r_ik */
1212     a = b = 0.5 * bond_co / ( bond_co - bond_cc*cos(ANGLE_6RING) );
1213     add_vsite3_param(&plist[F_VSITE3],
1214                      ats[atCZ], ats[atOH], ats[atCE1], ats[atCE2], a, b);
1215     at->atom[ats[atCZ]].m = at->atom[ats[atCZ]].mB = 0;
1216
1217     /* constraints between CE1, CE2 and OH */
1218     dCGCE = sqrt( cosrule(bond_cc, bond_cc, ANGLE_6RING) );
1219     dCEOH = sqrt( cosrule(bond_cc, bond_co, ANGLE_6RING) );
1220     my_add_param(&(plist[F_CONSTRNC]), ats[atCE1], ats[atOH], dCEOH);
1221     my_add_param(&(plist[F_CONSTRNC]), ats[atCE2], ats[atOH], dCEOH);
1222
1223     /* We also want to constrain the angle C-O-H, but since CZ is constructed
1224      * we need to introduce a constraint to CG.
1225      * CG is much further away, so that will lead to instabilities in LINCS
1226      * when we constrain both CG-HH and OH-HH distances. Instead of requiring
1227      * the use of lincs_order=8 we introduce a dummy mass three times further
1228      * away from OH than HH. The mass is accordingly a third, with the remaining
1229      * 2/3 moved to OH. This shouldnt cause any problems since the forces will
1230      * apply to the HH constructed atom and not directly on the virtual mass.
1231      */
1232
1233     vdist                   = 2.0*bond_oh;
1234     mM                      = at->atom[ats[atHH]].m/2.0;
1235     at->atom[ats[atOH]].m  += mM; /* add 1/2 of original H mass */
1236     at->atom[ats[atOH]].mB += mM; /* add 1/2 of original H mass */
1237     at->atom[ats[atHH]].m   = at->atom[ats[atHH]].mB = 0;
1238
1239     /* get dummy mass type */
1240     tpM = vsite_nm2type("MW", atype);
1241     /* make space for 1 mass: shift HH only */
1242     i0  = ats[atHH];
1243     atM = i0+*nadd;
1244     if (debug)
1245     {
1246         fprintf(stderr, "Inserting 1 dummy mass at %d\n", (*o2n)[i0]+1);
1247     }
1248     (*nadd)++;
1249     for (j = i0; j < at->nr; j++)
1250     {
1251         (*o2n)[j] = j+*nadd;
1252     }
1253     srenew(*newx, at->nr+*nadd);
1254     srenew(*newatom, at->nr+*nadd);
1255     srenew(*newatomname, at->nr+*nadd);
1256     srenew(*newvsite_type, at->nr+*nadd);
1257     srenew(*newcgnr, at->nr+*nadd);
1258     (*newatomname)[at->nr+*nadd-1] = NULL;
1259
1260     /* Calc the dummy mass initial position */
1261     rvec_sub(x[ats[atHH]], x[ats[atOH]], r1);
1262     svmul(2.0, r1, r1);
1263     rvec_add(r1, x[ats[atHH]], (*newx)[atM]);
1264
1265     strcpy(name, "MW1");
1266     (*newatomname)  [atM]         = put_symtab(symtab, name);
1267     (*newatom)      [atM].m       = (*newatom)[atM].mB    = mM;
1268     (*newatom)      [atM].q       = (*newatom)[atM].qB    = 0.0;
1269     (*newatom)      [atM].type    = (*newatom)[atM].typeB = tpM;
1270     (*newatom)      [atM].ptype   = eptAtom;
1271     (*newatom)      [atM].resind  = at->atom[i0].resind;
1272     (*newatom)      [atM].elem[0] = 'M';
1273     (*newatom)      [atM].elem[1] = '\0';
1274     (*newvsite_type)[atM]         = NOTSET;
1275     (*newcgnr)      [atM]         = (*cgnr)[i0];
1276     /* renumber cgnr: */
1277     for (i = i0; i < at->nr; i++)
1278     {
1279         (*cgnr)[i]++;
1280     }
1281
1282     (*vsite_type)[ats[atHH]] = F_VSITE2;
1283     nvsite++;
1284     /* assume we also want the COH angle constrained: */
1285     tmp1 = bond_cc*cos(0.5*ANGLE_6RING) + dCGCE*sin(ANGLE_6RING*0.5) + bond_co;
1286     dCGM = sqrt( cosrule(tmp1, vdist, angle_coh) );
1287     my_add_param(&(plist[F_CONSTRNC]), ats[atCG], add_shift+atM, dCGM);
1288     my_add_param(&(plist[F_CONSTRNC]), ats[atOH], add_shift+atM, vdist);
1289
1290     add_vsite2_param(&plist[F_VSITE2],
1291                      ats[atHH], ats[atOH], add_shift+atM, 1.0/2.0);
1292     return nvsite;
1293 }
1294
1295 static int gen_vsites_his(t_atoms *at, int *vsite_type[], t_params plist[],
1296                           int nrfound, int *ats, t_vsitetop *vsitetop, int nvsitetop)
1297 {
1298     int  nvsite, i;
1299     real a, b, alpha, dCGCE1, dCGNE2;
1300     real sinalpha, cosalpha;
1301     real xcom, ycom, mtot;
1302     real mG, mrest, mCE1, mNE2;
1303     real b_CG_ND1, b_ND1_CE1, b_CE1_NE2, b_CG_CD2, b_CD2_NE2;
1304     real b_ND1_HD1, b_NE2_HE2, b_CE1_HE1, b_CD2_HD2;
1305     real a_CG_ND1_CE1, a_CG_CD2_NE2, a_ND1_CE1_NE2, a_CE1_NE2_CD2;
1306     real a_NE2_CE1_HE1, a_NE2_CD2_HD2, a_CE1_ND1_HD1, a_CE1_NE2_HE2;
1307     char resname[10];
1308
1309     /* these MUST correspond to the atnms array in do_vsite_aromatics! */
1310     enum {
1311         atCG, atND1, atHD1, atCD2, atHD2, atCE1, atHE1, atNE2, atHE2, atNR
1312     };
1313     real x[atNR], y[atNR];
1314
1315     /* CG, CE1 and NE2 stay, each gets part of the total mass,
1316        rest gets virtualized */
1317     /* check number of atoms, 3 hydrogens may be missing: */
1318     /* assert( nrfound >= atNR-3 || nrfound <= atNR );
1319      * Don't understand the above logic. Shouldn't it be && rather than || ???
1320      */
1321     if ((nrfound < atNR-3) || (nrfound > atNR))
1322     {
1323         gmx_incons("Generating vsites for HIS");
1324     }
1325
1326     /* avoid warnings about uninitialized variables */
1327     b_ND1_HD1 = b_NE2_HE2 = b_CE1_HE1 = b_CD2_HD2 = a_NE2_CE1_HE1 =
1328                         a_NE2_CD2_HD2 = a_CE1_ND1_HD1 = a_CE1_NE2_HE2 = 0;
1329
1330     if (ats[atHD1] != NOTSET)
1331     {
1332         if (ats[atHE2] != NOTSET)
1333         {
1334             sprintf(resname, "HISH");
1335         }
1336         else
1337         {
1338             sprintf(resname, "HISA");
1339         }
1340     }
1341     else
1342     {
1343         sprintf(resname, "HISB");
1344     }
1345
1346     /* Get geometry from database */
1347     b_CG_ND1      = get_ddb_bond(vsitetop, nvsitetop, resname, "CG", "ND1");
1348     b_ND1_CE1     = get_ddb_bond(vsitetop, nvsitetop, resname, "ND1", "CE1");
1349     b_CE1_NE2     = get_ddb_bond(vsitetop, nvsitetop, resname, "CE1", "NE2");
1350     b_CG_CD2      = get_ddb_bond(vsitetop, nvsitetop, resname, "CG", "CD2");
1351     b_CD2_NE2     = get_ddb_bond(vsitetop, nvsitetop, resname, "CD2", "NE2");
1352     a_CG_ND1_CE1  = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "CG", "ND1", "CE1");
1353     a_CG_CD2_NE2  = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "CG", "CD2", "NE2");
1354     a_ND1_CE1_NE2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "ND1", "CE1", "NE2");
1355     a_CE1_NE2_CD2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "CE1", "NE2", "CD2");
1356
1357     if (ats[atHD1] != NOTSET)
1358     {
1359         b_ND1_HD1     = get_ddb_bond(vsitetop, nvsitetop, resname, "ND1", "HD1");
1360         a_CE1_ND1_HD1 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "CE1", "ND1", "HD1");
1361     }
1362     if (ats[atHE2] != NOTSET)
1363     {
1364         b_NE2_HE2     = get_ddb_bond(vsitetop, nvsitetop, resname, "NE2", "HE2");
1365         a_CE1_NE2_HE2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "CE1", "NE2", "HE2");
1366     }
1367     if (ats[atHD2] != NOTSET)
1368     {
1369         b_CD2_HD2     = get_ddb_bond(vsitetop, nvsitetop, resname, "CD2", "HD2");
1370         a_NE2_CD2_HD2 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "NE2", "CD2", "HD2");
1371     }
1372     if (ats[atHE1] != NOTSET)
1373     {
1374         b_CE1_HE1     = get_ddb_bond(vsitetop, nvsitetop, resname, "CE1", "HE1");
1375         a_NE2_CE1_HE1 = DEG2RAD*get_ddb_angle(vsitetop, nvsitetop, resname, "NE2", "CE1", "HE1");
1376     }
1377
1378     /* constraints between CG, CE1 and NE1 */
1379     dCGCE1   = sqrt( cosrule(b_CG_ND1, b_ND1_CE1, a_CG_ND1_CE1) );
1380     dCGNE2   = sqrt( cosrule(b_CG_CD2, b_CD2_NE2, a_CG_CD2_NE2) );
1381
1382     my_add_param(&(plist[F_CONSTRNC]), ats[atCG], ats[atCE1], dCGCE1);
1383     my_add_param(&(plist[F_CONSTRNC]), ats[atCG], ats[atNE2], dCGNE2);
1384     /* we already have a constraint CE1-NE2, so we don't add it again */
1385
1386     /* calculate the positions in a local frame of reference.
1387      * The x-axis is the line from CG that makes a right angle
1388      * with the bond CE1-NE2, and the y-axis the bond CE1-NE2.
1389      */
1390     /* First calculate the x-axis intersection with y-axis (=yCE1).
1391      * Get cos(angle CG-CE1-NE2) :
1392      */
1393     cosalpha = acosrule(dCGNE2, dCGCE1, b_CE1_NE2);
1394     x[atCE1] = 0;
1395     y[atCE1] = cosalpha*dCGCE1;
1396     x[atNE2] = 0;
1397     y[atNE2] = y[atCE1]-b_CE1_NE2;
1398     sinalpha = sqrt(1-cosalpha*cosalpha);
1399     x[atCG]  = -sinalpha*dCGCE1;
1400     y[atCG]  = 0;
1401     x[atHE1] = x[atHE2] = x[atHD1] = x[atHD2] = 0;
1402     y[atHE1] = y[atHE2] = y[atHD1] = y[atHD2] = 0;
1403
1404     /* calculate ND1 and CD2 positions from CE1 and NE2 */
1405
1406     x[atND1] = -b_ND1_CE1*sin(a_ND1_CE1_NE2);
1407     y[atND1] = y[atCE1]-b_ND1_CE1*cos(a_ND1_CE1_NE2);
1408
1409     x[atCD2] = -b_CD2_NE2*sin(a_CE1_NE2_CD2);
1410     y[atCD2] = y[atNE2]+b_CD2_NE2*cos(a_CE1_NE2_CD2);
1411
1412     /* And finally the hydrogen positions */
1413     if (ats[atHE1] != NOTSET)
1414     {
1415         x[atHE1] = x[atCE1] + b_CE1_HE1*sin(a_NE2_CE1_HE1);
1416         y[atHE1] = y[atCE1] - b_CE1_HE1*cos(a_NE2_CE1_HE1);
1417     }
1418     /* HD2 - first get (ccw) angle from (positive) y-axis */
1419     if (ats[atHD2] != NOTSET)
1420     {
1421         alpha    = a_CE1_NE2_CD2 + M_PI - a_NE2_CD2_HD2;
1422         x[atHD2] = x[atCD2] - b_CD2_HD2*sin(alpha);
1423         y[atHD2] = y[atCD2] + b_CD2_HD2*cos(alpha);
1424     }
1425     if (ats[atHD1] != NOTSET)
1426     {
1427         /* HD1 - first get (cw) angle from (positive) y-axis */
1428         alpha    = a_ND1_CE1_NE2 + M_PI - a_CE1_ND1_HD1;
1429         x[atHD1] = x[atND1] - b_ND1_HD1*sin(alpha);
1430         y[atHD1] = y[atND1] - b_ND1_HD1*cos(alpha);
1431     }
1432     if (ats[atHE2] != NOTSET)
1433     {
1434         x[atHE2] = x[atNE2] + b_NE2_HE2*sin(a_CE1_NE2_HE2);
1435         y[atHE2] = y[atNE2] + b_NE2_HE2*cos(a_CE1_NE2_HE2);
1436     }
1437     /* Have all coordinates now */
1438
1439     /* calc center-of-mass; keep atoms CG, CE1, NE2 and
1440      * set the rest to vsite3
1441      */
1442     mtot   = xcom = ycom = 0;
1443     nvsite = 0;
1444     for (i = 0; i < atNR; i++)
1445     {
1446         if (ats[i] != NOTSET)
1447         {
1448             mtot += at->atom[ats[i]].m;
1449             xcom += x[i]*at->atom[ats[i]].m;
1450             ycom += y[i]*at->atom[ats[i]].m;
1451             if (i != atCG && i != atCE1 && i != atNE2)
1452             {
1453                 at->atom[ats[i]].m    = at->atom[ats[i]].mB = 0;
1454                 (*vsite_type)[ats[i]] = F_VSITE3;
1455                 nvsite++;
1456             }
1457         }
1458     }
1459     if (nvsite+3 != nrfound)
1460     {
1461         gmx_incons("Generating vsites for HIS");
1462     }
1463
1464     xcom /= mtot;
1465     ycom /= mtot;
1466
1467     /* distribute mass so that com stays the same */
1468     mG    = xcom*mtot/x[atCG];
1469     mrest = mtot-mG;
1470     mCE1  = (ycom-y[atNE2])*mrest/(y[atCE1]-y[atNE2]);
1471     mNE2  = mrest-mCE1;
1472
1473     at->atom[ats[atCG]].m  = at->atom[ats[atCG]].mB = mG;
1474     at->atom[ats[atCE1]].m = at->atom[ats[atCE1]].mB = mCE1;
1475     at->atom[ats[atNE2]].m = at->atom[ats[atNE2]].mB = mNE2;
1476
1477     /* HE1 */
1478     if (ats[atHE1] != NOTSET)
1479     {
1480         calc_vsite3_param(x[atHE1], y[atHE1], x[atCE1], y[atCE1], x[atNE2], y[atNE2],
1481                           x[atCG], y[atCG], &a, &b);
1482         add_vsite3_param(&plist[F_VSITE3],
1483                          ats[atHE1], ats[atCE1], ats[atNE2], ats[atCG], a, b);
1484     }
1485     /* HE2 */
1486     if (ats[atHE2] != NOTSET)
1487     {
1488         calc_vsite3_param(x[atHE2], y[atHE2], x[atNE2], y[atNE2], x[atCE1], y[atCE1],
1489                           x[atCG], y[atCG], &a, &b);
1490         add_vsite3_param(&plist[F_VSITE3],
1491                          ats[atHE2], ats[atNE2], ats[atCE1], ats[atCG], a, b);
1492     }
1493
1494     /* ND1 */
1495     calc_vsite3_param(x[atND1], y[atND1], x[atNE2], y[atNE2], x[atCE1], y[atCE1],
1496                       x[atCG], y[atCG], &a, &b);
1497     add_vsite3_param(&plist[F_VSITE3],
1498                      ats[atND1], ats[atNE2], ats[atCE1], ats[atCG], a, b);
1499
1500     /* CD2 */
1501     calc_vsite3_param(x[atCD2], y[atCD2], x[atCE1], y[atCE1], x[atNE2], y[atNE2],
1502                       x[atCG], y[atCG], &a, &b);
1503     add_vsite3_param(&plist[F_VSITE3],
1504                      ats[atCD2], ats[atCE1], ats[atNE2], ats[atCG], a, b);
1505
1506     /* HD1 */
1507     if (ats[atHD1] != NOTSET)
1508     {
1509         calc_vsite3_param(x[atHD1], y[atHD1], x[atNE2], y[atNE2], x[atCE1], y[atCE1],
1510                           x[atCG], y[atCG], &a, &b);
1511         add_vsite3_param(&plist[F_VSITE3],
1512                          ats[atHD1], ats[atNE2], ats[atCE1], ats[atCG], a, b);
1513     }
1514     /* HD2 */
1515     if (ats[atHD2] != NOTSET)
1516     {
1517         calc_vsite3_param(x[atHD2], y[atHD2], x[atCE1], y[atCE1], x[atNE2], y[atNE2],
1518                           x[atCG], y[atCG], &a, &b);
1519         add_vsite3_param(&plist[F_VSITE3],
1520                          ats[atHD2], ats[atCE1], ats[atNE2], ats[atCG], a, b);
1521     }
1522     return nvsite;
1523 }
1524
1525 static gmx_bool is_vsite(int vsite_type)
1526 {
1527     if (vsite_type == NOTSET)
1528     {
1529         return FALSE;
1530     }
1531     switch (abs(vsite_type) )
1532     {
1533         case F_VSITE3:
1534         case F_VSITE3FD:
1535         case F_VSITE3OUT:
1536         case F_VSITE3FAD:
1537         case F_VSITE4FD:
1538         case F_VSITE4FDN:
1539             return TRUE;
1540         default:
1541             return FALSE;
1542     }
1543 }
1544
1545 static char atomnamesuffix[] = "1234";
1546
1547 void do_vsites(int nrtp, t_restp rtp[], gpp_atomtype_t atype,
1548                t_atoms *at, t_symtab *symtab, rvec *x[],
1549                t_params plist[], int *vsite_type[], int *cgnr[],
1550                real mHmult, gmx_bool bVsiteAromatics,
1551                const char *ffdir)
1552 {
1553 #define MAXATOMSPERRESIDUE 16
1554     int               i, j, k, m, i0, ni0, whatres, resind, add_shift, ftype, nvsite, nadd;
1555     int               ai, aj, ak, al;
1556     int               nrfound = 0, needed, nrbonds, nrHatoms, Heavy, nrheavies, tpM, tpHeavy;
1557     int               Hatoms[4], heavies[4], bb;
1558     gmx_bool          bWARNING, bAddVsiteParam, bFirstWater;
1559     matrix            tmpmat;
1560     gmx_bool         *bResProcessed;
1561     real              mHtot, mtot, fact, fact2;
1562     rvec              rpar, rperp, temp;
1563     char              name[10], tpname[32], nexttpname[32], *ch;
1564     rvec             *newx;
1565     int              *o2n, *newvsite_type, *newcgnr, ats[MAXATOMSPERRESIDUE];
1566     t_atom           *newatom;
1567     t_params         *params;
1568     char           ***newatomname;
1569     char             *resnm = NULL;
1570     int               ndb, f;
1571     char            **db;
1572     int               nvsiteconf, nvsitetop, cmplength;
1573     gmx_bool          isN, planarN, bFound;
1574     gmx_residuetype_t rt;
1575
1576     t_vsiteconf      *vsiteconflist;
1577     /* pointer to a list of CH3/NH3/NH2 configuration entries.
1578      * See comments in read_vsite_database. It isnt beautiful,
1579      * but it had to be fixed, and I dont even want to try to
1580      * maintain this part of the code...
1581      */
1582     t_vsitetop *vsitetop;
1583     /* Pointer to a list of geometry (bond/angle) entries for
1584      * residues like PHE, TRP, TYR, HIS, etc., where we need
1585      * to know the geometry to construct vsite aromatics.
1586      * Note that equilibrium geometry isnt necessarily the same
1587      * as the individual bond and angle values given in the
1588      * force field (rings can be strained).
1589      */
1590
1591     /* if bVsiteAromatics=TRUE do_vsites will specifically convert atoms in
1592        PHE, TRP, TYR and HIS to a construction of virtual sites */
1593     enum                    {
1594         resPHE, resTRP, resTYR, resHIS, resNR
1595     };
1596     const char *resnms[resNR]   = {   "PHE",  "TRP",  "TYR",  "HIS" };
1597     /* Amber03 alternative names for termini */
1598     const char *resnmsN[resNR]  = {  "NPHE", "NTRP", "NTYR", "NHIS" };
1599     const char *resnmsC[resNR]  = {  "CPHE", "CTRP", "CTYR", "CHIS" };
1600     /* HIS can be known as HISH, HIS1, HISA, HID, HIE, HIP, etc. too */
1601     gmx_bool    bPartial[resNR]  = {  FALSE,  FALSE,  FALSE,   TRUE  };
1602     /* the atnms for every residue MUST correspond to the enums in the
1603        gen_vsites_* (one for each residue) routines! */
1604     /* also the atom names in atnms MUST be in the same order as in the .rtp! */
1605     const char *atnms[resNR][MAXATOMSPERRESIDUE+1] = {
1606         { "CG", /* PHE */
1607           "CD1", "HD1", "CD2", "HD2",
1608           "CE1", "HE1", "CE2", "HE2",
1609           "CZ", "HZ", NULL },
1610         { "CB", /* TRP */
1611           "CG",
1612           "CD1", "HD1", "CD2",
1613           "NE1", "HE1", "CE2", "CE3", "HE3",
1614           "CZ2", "HZ2", "CZ3", "HZ3",
1615           "CH2", "HH2", NULL },
1616         { "CG", /* TYR */
1617           "CD1", "HD1", "CD2", "HD2",
1618           "CE1", "HE1", "CE2", "HE2",
1619           "CZ", "OH", "HH", NULL },
1620         { "CG", /* HIS */
1621           "ND1", "HD1", "CD2", "HD2",
1622           "CE1", "HE1", "NE2", "HE2", NULL }
1623     };
1624
1625     if (debug)
1626     {
1627         printf("Searching for atoms to make virtual sites ...\n");
1628         fprintf(debug, "# # # VSITES # # #\n");
1629     }
1630
1631     ndb           = fflib_search_file_end(ffdir, ".vsd", FALSE, &db);
1632     nvsiteconf    = 0;
1633     vsiteconflist = NULL;
1634     nvsitetop     = 0;
1635     vsitetop      = NULL;
1636     for (f = 0; f < ndb; f++)
1637     {
1638         read_vsite_database(db[f], &vsiteconflist, &nvsiteconf, &vsitetop, &nvsitetop);
1639         sfree(db[f]);
1640     }
1641     sfree(db);
1642
1643     bFirstWater = TRUE;
1644     nvsite      = 0;
1645     nadd        = 0;
1646     /* we need a marker for which atoms should *not* be renumbered afterwards */
1647     add_shift = 10*at->nr;
1648     /* make arrays where masses can be inserted into */
1649     snew(newx, at->nr);
1650     snew(newatom, at->nr);
1651     snew(newatomname, at->nr);
1652     snew(newvsite_type, at->nr);
1653     snew(newcgnr, at->nr);
1654     /* make index array to tell where the atoms go to when masses are inserted */
1655     snew(o2n, at->nr);
1656     for (i = 0; i < at->nr; i++)
1657     {
1658         o2n[i] = i;
1659     }
1660     /* make index to tell which residues were already processed */
1661     snew(bResProcessed, at->nres);
1662
1663     gmx_residuetype_init(&rt);
1664
1665     /* generate vsite constructions */
1666     /* loop over all atoms */
1667     resind = -1;
1668     for (i = 0; (i < at->nr); i++)
1669     {
1670         if (at->atom[i].resind != resind)
1671         {
1672             resind = at->atom[i].resind;
1673             resnm  = *(at->resinfo[resind].name);
1674         }
1675         /* first check for aromatics to virtualize */
1676         /* don't waste our effort on DNA, water etc. */
1677         /* Only do the vsite aromatic stuff when we reach the
1678          * CA atom, since there might be an X2/X3 group on the
1679          * N-terminus that must be treated first.
1680          */
1681         if (bVsiteAromatics &&
1682             !strcmp(*(at->atomname[i]), "CA") &&
1683             !bResProcessed[resind] &&
1684             gmx_residuetype_is_protein(rt, *(at->resinfo[resind].name)) )
1685         {
1686             /* mark this residue */
1687             bResProcessed[resind] = TRUE;
1688             /* find out if this residue needs converting */
1689             whatres = NOTSET;
1690             for (j = 0; j < resNR && whatres == NOTSET; j++)
1691             {
1692
1693                 cmplength = bPartial[j] ? strlen(resnm)-1 : strlen(resnm);
1694
1695                 bFound = ((gmx_strncasecmp(resnm, resnms[j], cmplength) == 0) ||
1696                           (gmx_strncasecmp(resnm, resnmsN[j], cmplength) == 0) ||
1697                           (gmx_strncasecmp(resnm, resnmsC[j], cmplength) == 0));
1698
1699                 if (bFound)
1700                 {
1701                     whatres = j;
1702                     /* get atoms we will be needing for the conversion */
1703                     nrfound = 0;
1704                     for (k = 0; atnms[j][k]; k++)
1705                     {
1706                         ats[k] = NOTSET;
1707                         for (m = i; m < at->nr && at->atom[m].resind == resind && ats[k] == NOTSET; m++)
1708                         {
1709                             if (gmx_strcasecmp(*(at->atomname[m]), atnms[j][k]) == 0)
1710                             {
1711                                 ats[k] = m;
1712                                 nrfound++;
1713                             }
1714                         }
1715                     }
1716
1717                     /* now k is number of atom names in atnms[j] */
1718                     if (j == resHIS)
1719                     {
1720                         needed = k-3;
1721                     }
1722                     else
1723                     {
1724                         needed = k;
1725                     }
1726                     if (nrfound < needed)
1727                     {
1728                         gmx_fatal(FARGS, "not enough atoms found (%d, need %d) in "
1729                                   "residue %s %d while\n             "
1730                                   "generating aromatics virtual site construction",
1731                                   nrfound, needed, resnm, at->resinfo[resind].nr);
1732                     }
1733                     /* Advance overall atom counter */
1734                     i++;
1735                 }
1736             }
1737             /* the enums for every residue MUST correspond to atnms[residue] */
1738             switch (whatres)
1739             {
1740                 case resPHE:
1741                     if (debug)
1742                     {
1743                         fprintf(stderr, "PHE at %d\n", o2n[ats[0]]+1);
1744                     }
1745                     nvsite += gen_vsites_phe(at, vsite_type, plist, nrfound, ats, vsitetop, nvsitetop);
1746                     break;
1747                 case resTRP:
1748                     if (debug)
1749                     {
1750                         fprintf(stderr, "TRP at %d\n", o2n[ats[0]]+1);
1751                     }
1752                     nvsite += gen_vsites_trp(atype, &newx, &newatom, &newatomname, &o2n,
1753                                              &newvsite_type, &newcgnr, symtab, &nadd, *x, cgnr,
1754                                              at, vsite_type, plist, nrfound, ats, add_shift, vsitetop, nvsitetop);
1755                     break;
1756                 case resTYR:
1757                     if (debug)
1758                     {
1759                         fprintf(stderr, "TYR at %d\n", o2n[ats[0]]+1);
1760                     }
1761                     nvsite += gen_vsites_tyr(atype, &newx, &newatom, &newatomname, &o2n,
1762                                              &newvsite_type, &newcgnr, symtab, &nadd, *x, cgnr,
1763                                              at, vsite_type, plist, nrfound, ats, add_shift, vsitetop, nvsitetop);
1764                     break;
1765                 case resHIS:
1766                     if (debug)
1767                     {
1768                         fprintf(stderr, "HIS at %d\n", o2n[ats[0]]+1);
1769                     }
1770                     nvsite += gen_vsites_his(at, vsite_type, plist, nrfound, ats, vsitetop, nvsitetop);
1771                     break;
1772                 case NOTSET:
1773                     /* this means this residue won't be processed */
1774                     break;
1775                 default:
1776                     gmx_fatal(FARGS, "DEATH HORROR in do_vsites (%s:%d)",
1777                               __FILE__, __LINE__);
1778             } /* switch whatres */
1779               /* skip back to beginning of residue */
1780             while (i > 0 && at->atom[i-1].resind == resind)
1781             {
1782                 i--;
1783             }
1784         } /* if bVsiteAromatics & is protein */
1785
1786         /* now process the rest of the hydrogens */
1787         /* only process hydrogen atoms which are not already set */
1788         if ( ((*vsite_type)[i] == NOTSET) && is_hydrogen(*(at->atomname[i])))
1789         {
1790             /* find heavy atom, count #bonds from it and #H atoms bound to it
1791                and return H atom numbers (Hatoms) and heavy atom numbers (heavies) */
1792             count_bonds(i, &plist[F_BONDS], at->atomname,
1793                         &nrbonds, &nrHatoms, Hatoms, &Heavy, &nrheavies, heavies);
1794             /* get Heavy atom type */
1795             tpHeavy = get_atype(Heavy, at, nrtp, rtp, rt);
1796             strcpy(tpname, get_atomtype_name(tpHeavy, atype));
1797
1798             bWARNING       = FALSE;
1799             bAddVsiteParam = TRUE;
1800             /* nested if's which check nrHatoms, nrbonds and atomname */
1801             if (nrHatoms == 1)
1802             {
1803                 switch (nrbonds)
1804                 {
1805                     case 2: /* -O-H */
1806                         (*vsite_type)[i] = F_BONDS;
1807                         break;
1808                     case 3: /* =CH-, -NH- or =NH+- */
1809                         (*vsite_type)[i] = F_VSITE3FD;
1810                         break;
1811                     case 4: /* --CH- (tert) */
1812                         /* The old type 4FD had stability issues, so
1813                          * all new constructs should use 4FDN
1814                          */
1815                         (*vsite_type)[i] = F_VSITE4FDN;
1816
1817                         /* Check parity of heavy atoms from coordinates */
1818                         ai = Heavy;
1819                         aj = heavies[0];
1820                         ak = heavies[1];
1821                         al = heavies[2];
1822                         rvec_sub((*x)[aj], (*x)[ai], tmpmat[0]);
1823                         rvec_sub((*x)[ak], (*x)[ai], tmpmat[1]);
1824                         rvec_sub((*x)[al], (*x)[ai], tmpmat[2]);
1825
1826                         if (det(tmpmat) > 0)
1827                         {
1828                             /* swap parity */
1829                             heavies[1] = aj;
1830                             heavies[0] = ak;
1831                         }
1832
1833                         break;
1834                     default: /* nrbonds != 2, 3 or 4 */
1835                         bWARNING = TRUE;
1836                 }
1837
1838             }
1839             else if ( /*(nrHatoms == 2) && (nrbonds == 2) && REMOVED this test
1840                          DvdS 19-01-04 */
1841                 (gmx_strncasecmp(*at->atomname[Heavy], "OW", 2) == 0) )
1842             {
1843                 bAddVsiteParam = FALSE; /* this is water: skip these hydrogens */
1844                 if (bFirstWater)
1845                 {
1846                     bFirstWater = FALSE;
1847                     if (debug)
1848                     {
1849                         fprintf(debug,
1850                                 "Not converting hydrogens in water to virtual sites\n");
1851                     }
1852                 }
1853             }
1854             else if ( (nrHatoms == 2) && (nrbonds == 4) )
1855             {
1856                 /* -CH2- , -NH2+- */
1857                 (*vsite_type)[Hatoms[0]] = F_VSITE3OUT;
1858                 (*vsite_type)[Hatoms[1]] = -F_VSITE3OUT;
1859             }
1860             else
1861             {
1862                 /* 2 or 3 hydrogen atom, with 3 or 4 bonds in total to the heavy atom.
1863                  * If it is a nitrogen, first check if it is planar.
1864                  */
1865                 isN = planarN = FALSE;
1866                 if ((nrHatoms == 2) && ((*at->atomname[Heavy])[0] == 'N'))
1867                 {
1868                     isN = TRUE;
1869                     j   = nitrogen_is_planar(vsiteconflist, nvsiteconf, tpname);
1870                     if (j < 0)
1871                     {
1872                         gmx_fatal(FARGS, "No vsite database NH2 entry for type %s\n", tpname);
1873                     }
1874                     planarN = (j == 1);
1875                 }
1876                 if ( (nrHatoms == 2) && (nrbonds == 3) && ( !isN || planarN ) )
1877                 {
1878                     /* =CH2 or, if it is a nitrogen NH2, it is a planar one */
1879                     (*vsite_type)[Hatoms[0]] = F_VSITE3FAD;
1880                     (*vsite_type)[Hatoms[1]] = -F_VSITE3FAD;
1881                 }
1882                 else if ( ( (nrHatoms == 2) && (nrbonds == 3) &&
1883                             ( isN && !planarN ) ) ||
1884                           ( (nrHatoms == 3) && (nrbonds == 4) ) )
1885                 {
1886                     /* CH3, NH3 or non-planar NH2 group */
1887                     int      Hat_vsite_type[3] = { F_VSITE3, F_VSITE3OUT, F_VSITE3OUT };
1888                     gmx_bool Hat_SwapParity[3] = { FALSE,    TRUE,        FALSE };
1889
1890                     if (debug)
1891                     {
1892                         fprintf(stderr, "-XH3 or nonplanar NH2 group at %d\n", i+1);
1893                     }
1894                     bAddVsiteParam = FALSE; /* we'll do this ourselves! */
1895                     /* -NH2 (umbrella), -NH3+ or -CH3 */
1896                     (*vsite_type)[Heavy]       = F_VSITE3;
1897                     for (j = 0; j < nrHatoms; j++)
1898                     {
1899                         (*vsite_type)[Hatoms[j]] = Hat_vsite_type[j];
1900                     }
1901                     /* get dummy mass type from first char of heavy atom type (N or C) */
1902
1903                     strcpy(nexttpname, get_atomtype_name(get_atype(heavies[0], at, nrtp, rtp, rt), atype));
1904                     ch = get_dummymass_name(vsiteconflist, nvsiteconf, tpname, nexttpname);
1905
1906                     if (ch == NULL)
1907                     {
1908                         if (ndb > 0)
1909                         {
1910                             gmx_fatal(FARGS, "Can't find dummy mass for type %s bonded to type %s in the virtual site database (.vsd files). Add it to the database!\n", tpname, nexttpname);
1911                         }
1912                         else
1913                         {
1914                             gmx_fatal(FARGS, "A dummy mass for type %s bonded to type %s is required, but no virtual site database (.vsd) files where found.\n", tpname, nexttpname);
1915                         }
1916                     }
1917                     else
1918                     {
1919                         strcpy(name, ch);
1920                     }
1921
1922                     tpM = vsite_nm2type(name, atype);
1923                     /* make space for 2 masses: shift all atoms starting with 'Heavy' */
1924 #define NMASS 2
1925                     i0  = Heavy;
1926                     ni0 = i0+nadd;
1927                     if (debug)
1928                     {
1929                         fprintf(stderr, "Inserting %d dummy masses at %d\n", NMASS, o2n[i0]+1);
1930                     }
1931                     nadd += NMASS;
1932                     for (j = i0; j < at->nr; j++)
1933                     {
1934                         o2n[j] = j+nadd;
1935                     }
1936
1937                     srenew(newx, at->nr+nadd);
1938                     srenew(newatom, at->nr+nadd);
1939                     srenew(newatomname, at->nr+nadd);
1940                     srenew(newvsite_type, at->nr+nadd);
1941                     srenew(newcgnr, at->nr+nadd);
1942
1943                     for (j = 0; j < NMASS; j++)
1944                     {
1945                         newatomname[at->nr+nadd-1-j] = NULL;
1946                     }
1947
1948                     /* calculate starting position for the masses */
1949                     mHtot = 0;
1950                     /* get atom masses, and set Heavy and Hatoms mass to zero */
1951                     for (j = 0; j < nrHatoms; j++)
1952                     {
1953                         mHtot                += get_amass(Hatoms[j], at, nrtp, rtp, rt);
1954                         at->atom[Hatoms[j]].m = at->atom[Hatoms[j]].mB = 0;
1955                     }
1956                     mtot              = mHtot + get_amass(Heavy, at, nrtp, rtp, rt);
1957                     at->atom[Heavy].m = at->atom[Heavy].mB = 0;
1958                     if (mHmult != 1.0)
1959                     {
1960                         mHtot *= mHmult;
1961                     }
1962                     fact2 = mHtot/mtot;
1963                     fact  = sqrt(fact2);
1964                     /* generate vectors parallel and perpendicular to rotational axis:
1965                      * rpar  = Heavy -> Hcom
1966                      * rperp = Hcom  -> H1   */
1967                     clear_rvec(rpar);
1968                     for (j = 0; j < nrHatoms; j++)
1969                     {
1970                         rvec_inc(rpar, (*x)[Hatoms[j]]);
1971                     }
1972                     svmul(1.0/nrHatoms, rpar, rpar); /* rpar = ( H1+H2+H3 ) / 3 */
1973                     rvec_dec(rpar, (*x)[Heavy]);     /*        - Heavy          */
1974                     rvec_sub((*x)[Hatoms[0]], (*x)[Heavy], rperp);
1975                     rvec_dec(rperp, rpar);           /* rperp = H1 - Heavy - rpar */
1976                     /* calc mass positions */
1977                     svmul(fact2, rpar, temp);
1978                     for (j = 0; (j < NMASS); j++) /* xM = xN + fact2 * rpar +/- fact * rperp */
1979                     {
1980                         rvec_add((*x)[Heavy], temp, newx[ni0+j]);
1981                     }
1982                     svmul(fact, rperp, temp);
1983                     rvec_inc(newx[ni0  ], temp);
1984                     rvec_dec(newx[ni0+1], temp);
1985                     /* set atom parameters for the masses */
1986                     for (j = 0; (j < NMASS); j++)
1987                     {
1988                         /* make name: "M??#" or "M?#" (? is atomname, # is number) */
1989                         name[0] = 'M';
1990                         for (k = 0; (*at->atomname[Heavy])[k] && ( k < NMASS ); k++)
1991                         {
1992                             name[k+1] = (*at->atomname[Heavy])[k];
1993                         }
1994                         name[k+1]              = atomnamesuffix[j];
1995                         name[k+2]              = '\0';
1996                         newatomname[ni0+j]     = put_symtab(symtab, name);
1997                         newatom[ni0+j].m       = newatom[ni0+j].mB    = mtot/NMASS;
1998                         newatom[ni0+j].q       = newatom[ni0+j].qB    = 0.0;
1999                         newatom[ni0+j].type    = newatom[ni0+j].typeB = tpM;
2000                         newatom[ni0+j].ptype   = eptAtom;
2001                         newatom[ni0+j].resind  = at->atom[i0].resind;
2002                         newatom[ni0+j].elem[0] = 'M';
2003                         newatom[ni0+j].elem[1] = '\0';
2004                         newvsite_type[ni0+j]   = NOTSET;
2005                         newcgnr[ni0+j]         = (*cgnr)[i0];
2006                     }
2007                     /* add constraints between dummy masses and to heavies[0] */
2008                     /* 'add_shift' says which atoms won't be renumbered afterwards */
2009                     my_add_param(&(plist[F_CONSTRNC]), heavies[0],  add_shift+ni0,  NOTSET);
2010                     my_add_param(&(plist[F_CONSTRNC]), heavies[0],  add_shift+ni0+1, NOTSET);
2011                     my_add_param(&(plist[F_CONSTRNC]), add_shift+ni0, add_shift+ni0+1, NOTSET);
2012
2013                     /* generate Heavy, H1, H2 and H3 from M1, M2 and heavies[0] */
2014                     /* note that vsite_type cannot be NOTSET, because we just set it */
2015                     add_vsite3_atoms  (&plist[(*vsite_type)[Heavy]],
2016                                        Heavy,     heavies[0], add_shift+ni0, add_shift+ni0+1,
2017                                        FALSE);
2018                     for (j = 0; j < nrHatoms; j++)
2019                     {
2020                         add_vsite3_atoms(&plist[(*vsite_type)[Hatoms[j]]],
2021                                          Hatoms[j], heavies[0], add_shift+ni0, add_shift+ni0+1,
2022                                          Hat_SwapParity[j]);
2023                     }
2024 #undef NMASS
2025                 }
2026                 else
2027                 {
2028                     bWARNING = TRUE;
2029                 }
2030
2031             }
2032             if (bWARNING)
2033             {
2034                 fprintf(stderr,
2035                         "Warning: cannot convert atom %d %s (bound to a heavy atom "
2036                         "%s with \n"
2037                         "         %d bonds and %d bound hydrogens atoms) to virtual site\n",
2038                         i+1, *(at->atomname[i]), tpname, nrbonds, nrHatoms);
2039             }
2040             if (bAddVsiteParam)
2041             {
2042                 /* add vsite parameters to topology,
2043                    also get rid of negative vsite_types */
2044                 add_vsites(plist, (*vsite_type), Heavy, nrHatoms, Hatoms,
2045                            nrheavies, heavies);
2046                 /* transfer mass of virtual site to Heavy atom */
2047                 for (j = 0; j < nrHatoms; j++)
2048                 {
2049                     if (is_vsite((*vsite_type)[Hatoms[j]]))
2050                     {
2051                         at->atom[Heavy].m    += at->atom[Hatoms[j]].m;
2052                         at->atom[Heavy].mB    = at->atom[Heavy].m;
2053                         at->atom[Hatoms[j]].m = at->atom[Hatoms[j]].mB = 0;
2054                     }
2055                 }
2056             }
2057             nvsite += nrHatoms;
2058             if (debug)
2059             {
2060                 fprintf(debug, "atom %d: ", o2n[i]+1);
2061                 print_bonds(debug, o2n, nrHatoms, Hatoms, Heavy, nrheavies, heavies);
2062             }
2063         } /* if vsite NOTSET & is hydrogen */
2064
2065     }     /* for i < at->nr */
2066
2067     gmx_residuetype_destroy(rt);
2068
2069     if (debug)
2070     {
2071         fprintf(debug, "Before inserting new atoms:\n");
2072         for (i = 0; i < at->nr; i++)
2073         {
2074             fprintf(debug, "%4d %4d %4s %4d %4s %6d %-10s\n", i+1, o2n[i]+1,
2075                     at->atomname[i] ? *(at->atomname[i]) : "(NULL)",
2076                     at->resinfo[at->atom[i].resind].nr,
2077                     at->resinfo[at->atom[i].resind].name ?
2078                     *(at->resinfo[at->atom[i].resind].name) : "(NULL)",
2079                     (*cgnr)[i],
2080                     ((*vsite_type)[i] == NOTSET) ?
2081                     "NOTSET" : interaction_function[(*vsite_type)[i]].name);
2082         }
2083         fprintf(debug, "new atoms to be inserted:\n");
2084         for (i = 0; i < at->nr+nadd; i++)
2085         {
2086             if (newatomname[i])
2087             {
2088                 fprintf(debug, "%4d %4s %4d %6d %-10s\n", i+1,
2089                         newatomname[i] ? *(newatomname[i]) : "(NULL)",
2090                         newatom[i].resind, newcgnr[i],
2091                         (newvsite_type[i] == NOTSET) ?
2092                         "NOTSET" : interaction_function[newvsite_type[i]].name);
2093             }
2094         }
2095     }
2096
2097     /* add all original atoms to the new arrays, using o2n index array */
2098     for (i = 0; i < at->nr; i++)
2099     {
2100         newatomname  [o2n[i]] = at->atomname [i];
2101         newatom      [o2n[i]] = at->atom     [i];
2102         newvsite_type[o2n[i]] = (*vsite_type)[i];
2103         newcgnr      [o2n[i]] = (*cgnr)      [i];
2104         copy_rvec((*x)[i], newx[o2n[i]]);
2105     }
2106     /* throw away old atoms */
2107     sfree(at->atom);
2108     sfree(at->atomname);
2109     sfree(*vsite_type);
2110     sfree(*cgnr);
2111     sfree(*x);
2112     /* put in the new ones */
2113     at->nr      += nadd;
2114     at->atom     = newatom;
2115     at->atomname = newatomname;
2116     *vsite_type  = newvsite_type;
2117     *cgnr        = newcgnr;
2118     *x           = newx;
2119     if (at->nr > add_shift)
2120     {
2121         gmx_fatal(FARGS, "Added impossible amount of dummy masses "
2122                   "(%d on a total of %d atoms)\n", nadd, at->nr-nadd);
2123     }
2124
2125     if (debug)
2126     {
2127         fprintf(debug, "After inserting new atoms:\n");
2128         for (i = 0; i < at->nr; i++)
2129         {
2130             fprintf(debug, "%4d %4s %4d %4s %6d %-10s\n", i+1,
2131                     at->atomname[i] ? *(at->atomname[i]) : "(NULL)",
2132                     at->resinfo[at->atom[i].resind].nr,
2133                     at->resinfo[at->atom[i].resind].name ?
2134                     *(at->resinfo[at->atom[i].resind].name) : "(NULL)",
2135                     (*cgnr)[i],
2136                     ((*vsite_type)[i] == NOTSET) ?
2137                     "NOTSET" : interaction_function[(*vsite_type)[i]].name);
2138         }
2139     }
2140
2141     /* now renumber all the interactions because of the added atoms */
2142     for (ftype = 0; ftype < F_NRE; ftype++)
2143     {
2144         params = &(plist[ftype]);
2145         if (debug)
2146         {
2147             fprintf(debug, "Renumbering %d %s\n", params->nr,
2148                     interaction_function[ftype].longname);
2149         }
2150         for (i = 0; i < params->nr; i++)
2151         {
2152             for (j = 0; j < NRAL(ftype); j++)
2153             {
2154                 if (params->param[i].a[j] >= add_shift)
2155                 {
2156                     if (debug)
2157                     {
2158                         fprintf(debug, " [%u -> %u]", params->param[i].a[j],
2159                                 params->param[i].a[j]-add_shift);
2160                     }
2161                     params->param[i].a[j] = params->param[i].a[j]-add_shift;
2162                 }
2163                 else
2164                 {
2165                     if (debug)
2166                     {
2167                         fprintf(debug, " [%u -> %d]", params->param[i].a[j],
2168                                 o2n[params->param[i].a[j]]);
2169                     }
2170                     params->param[i].a[j] = o2n[params->param[i].a[j]];
2171                 }
2172             }
2173             if (debug)
2174             {
2175                 fprintf(debug, "\n");
2176             }
2177         }
2178     }
2179     /* now check if atoms in the added constraints are in increasing order */
2180     params = &(plist[F_CONSTRNC]);
2181     for (i = 0; i < params->nr; i++)
2182     {
2183         if (params->param[i].AI > params->param[i].AJ)
2184         {
2185             j                   = params->param[i].AJ;
2186             params->param[i].AJ = params->param[i].AI;
2187             params->param[i].AI = j;
2188         }
2189     }
2190
2191     /* clean up */
2192     sfree(o2n);
2193
2194     /* tell the user what we did */
2195     fprintf(stderr, "Marked %d virtual sites\n", nvsite);
2196     fprintf(stderr, "Added %d dummy masses\n", nadd);
2197     fprintf(stderr, "Added %d new constraints\n", plist[F_CONSTRNC].nr);
2198 }
2199
2200 void do_h_mass(t_params *psb, int vsite_type[], t_atoms *at, real mHmult,
2201                gmx_bool bDeuterate)
2202 {
2203     int i, j, a;
2204
2205     /* loop over all atoms */
2206     for (i = 0; i < at->nr; i++)
2207     {
2208         /* adjust masses if i is hydrogen and not a virtual site */
2209         if (!is_vsite(vsite_type[i]) && is_hydrogen(*(at->atomname[i])) )
2210         {
2211             /* find bonded heavy atom */
2212             a = NOTSET;
2213             for (j = 0; (j < psb->nr) && (a == NOTSET); j++)
2214             {
2215                 /* if other atom is not a virtual site, it is the one we want */
2216                 if ( (psb->param[j].AI == i) &&
2217                      !is_vsite(vsite_type[psb->param[j].AJ]) )
2218                 {
2219                     a = psb->param[j].AJ;
2220                 }
2221                 else if ( (psb->param[j].AJ == i) &&
2222                           !is_vsite(vsite_type[psb->param[j].AI]) )
2223                 {
2224                     a = psb->param[j].AI;
2225                 }
2226             }
2227             if (a == NOTSET)
2228             {
2229                 gmx_fatal(FARGS, "Unbound hydrogen atom (%d) found while adjusting mass",
2230                           i+1);
2231             }
2232
2233             /* adjust mass of i (hydrogen) with mHmult
2234                and correct mass of a (bonded atom) with same amount */
2235             if (!bDeuterate)
2236             {
2237                 at->atom[a].m  -= (mHmult-1.0)*at->atom[i].m;
2238                 at->atom[a].mB -= (mHmult-1.0)*at->atom[i].m;
2239             }
2240             at->atom[i].m  *= mHmult;
2241             at->atom[i].mB *= mHmult;
2242         }
2243     }
2244 }