Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / gpp_atomtype.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) 2011,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 <string.h>
43
44 #include "gromacs/utility/smalloc.h"
45 #include "topdirs.h"
46 #include "toputil.h"
47 #include "topdirs.h"
48 #include "toputil.h"
49 #include "symtab.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "txtdump.h"
52 #include "gpp_atomtype.h"
53
54 typedef struct gpp_atomtype {
55     int              nr;           /* The number of atomtypes           */
56     t_atom          *atom;         /* Array of atoms                    */
57     char          ***atomname;     /* Names of the atomtypes            */
58     t_param         *nb;           /* Nonbonded force default params    */
59     int             *bondatomtype; /* The bond_atomtype for each atomtype  */
60     real            *radius;       /* Radius for GBSA stuff                */
61     real            *vol;          /* Effective volume for GBSA            */
62     real            *surftens;     /* Surface tension with water, for GBSA */
63     real            *gb_radius;    /* Radius for Still model               */
64     real            *S_hct;        /* Overlap factor for HCT model         */
65     int             *atomnumber;   /* Atomic number, used for QM/MM        */
66 } t_gpp_atomtype;
67
68 int get_atomtype_type(const char *str, gpp_atomtype_t ga)
69 {
70     int i;
71
72     /* Atom types are always case sensitive */
73     for (i = 0; (i < ga->nr); i++)
74     {
75         if (strcmp(str, *(ga->atomname[i])) == 0)
76         {
77             return i;
78         }
79     }
80
81     return NOTSET;
82 }
83
84 int get_atomtype_ntypes(gpp_atomtype_t ga)
85 {
86     return ga->nr;
87 }
88
89 char *get_atomtype_name(int nt, gpp_atomtype_t ga)
90 {
91     if ((nt < 0) || (nt >= ga->nr))
92     {
93         return NULL;
94     }
95
96     return *(ga->atomname[nt]);
97 }
98
99 real get_atomtype_massA(int nt, gpp_atomtype_t ga)
100 {
101     if ((nt < 0) || (nt >= ga->nr))
102     {
103         return NOTSET;
104     }
105
106     return ga->atom[nt].m;
107 }
108
109 real get_atomtype_massB(int nt, gpp_atomtype_t ga)
110 {
111     if ((nt < 0) || (nt >= ga->nr))
112     {
113         return NOTSET;
114     }
115
116     return ga->atom[nt].mB;
117 }
118
119 real get_atomtype_qA(int nt, gpp_atomtype_t ga)
120 {
121     if ((nt < 0) || (nt >= ga->nr))
122     {
123         return NOTSET;
124     }
125
126     return ga->atom[nt].q;
127 }
128
129 real get_atomtype_qB(int nt, gpp_atomtype_t ga)
130 {
131     if ((nt < 0) || (nt >= ga->nr))
132     {
133         return NOTSET;
134     }
135
136     return ga->atom[nt].qB;
137 }
138
139 int get_atomtype_ptype(int nt, gpp_atomtype_t ga)
140 {
141     if ((nt < 0) || (nt >= ga->nr))
142     {
143         return NOTSET;
144     }
145
146     return ga->atom[nt].ptype;
147 }
148
149 int get_atomtype_batype(int nt, gpp_atomtype_t ga)
150 {
151     if ((nt < 0) || (nt >= ga->nr))
152     {
153         return NOTSET;
154     }
155
156     return ga->bondatomtype[nt];
157 }
158
159 int get_atomtype_atomnumber(int nt, gpp_atomtype_t ga)
160 {
161     if ((nt < 0) || (nt >= ga->nr))
162     {
163         return NOTSET;
164     }
165
166     return ga->atomnumber[nt];
167 }
168
169 real get_atomtype_radius(int nt, gpp_atomtype_t ga)
170 {
171     if ((nt < 0) || (nt >= ga->nr))
172     {
173         return NOTSET;
174     }
175
176     return ga->radius[nt];
177 }
178
179 real get_atomtype_vol(int nt, gpp_atomtype_t ga)
180 {
181     if ((nt < 0) || (nt >= ga->nr))
182     {
183         return NOTSET;
184     }
185
186     return ga->vol[nt];
187 }
188
189 real get_atomtype_surftens(int nt, gpp_atomtype_t ga)
190 {
191     if ((nt < 0) || (nt >= ga->nr))
192     {
193         return NOTSET;
194     }
195
196     return ga->surftens[nt];
197 }
198
199 real get_atomtype_gb_radius(int nt, gpp_atomtype_t ga)
200 {
201     if ((nt < 0) || (nt >= ga->nr))
202     {
203         return NOTSET;
204     }
205
206     return ga->gb_radius[nt];
207 }
208
209 real get_atomtype_S_hct(int nt, gpp_atomtype_t ga)
210 {
211     if ((nt < 0) || (nt >= ga->nr))
212     {
213         return NOTSET;
214     }
215
216     return ga->S_hct[nt];
217 }
218
219 real get_atomtype_nbparam(int nt, int param, gpp_atomtype_t ga)
220 {
221     if ((nt < 0) || (nt >= ga->nr))
222     {
223         return NOTSET;
224     }
225     if ((param < 0) || (param >= MAXFORCEPARAM))
226     {
227         return NOTSET;
228     }
229     return ga->nb[nt].c[param];
230 }
231
232 gpp_atomtype_t init_atomtype(void)
233 {
234     gpp_atomtype_t ga;
235
236     snew(ga, 1);
237
238     ga->nr           = 0;
239     ga->atom         = NULL;
240     ga->atomname     = NULL;
241     ga->nb           = NULL;
242     ga->bondatomtype = NULL;
243     ga->radius       = NULL;
244     ga->vol          = NULL;
245     ga->surftens     = NULL;
246     ga->atomnumber   = NULL;
247     ga->gb_radius    = NULL;
248     ga->S_hct        = NULL;
249
250     return ga;
251 }
252
253 int
254 set_atomtype_gbparam(gpp_atomtype_t ga, int i,
255                      real radius, real vol, real surftens,
256                      real gb_radius, real S_hct)
257 {
258     if ( (i < 0) || (i >= ga->nr))
259     {
260         return NOTSET;
261     }
262
263     ga->radius[i]    = radius;
264     ga->vol[i]       = vol;
265     ga->surftens[i]  = surftens;
266     ga->gb_radius[i] = gb_radius;
267     ga->S_hct[i]     = S_hct;
268
269     return i;
270 }
271
272
273 int set_atomtype(int nt, gpp_atomtype_t ga, t_symtab *tab,
274                  t_atom *a, const char *name, t_param *nb,
275                  int bondatomtype,
276                  real radius, real vol, real surftens, int atomnumber,
277                  real gb_radius, real S_hct)
278 {
279     if ((nt < 0) || (nt >= ga->nr))
280     {
281         return NOTSET;
282     }
283
284     ga->atom[nt]         = *a;
285     ga->atomname[nt]     = put_symtab(tab, name);
286     ga->nb[nt]           = *nb;
287     ga->bondatomtype[nt] = bondatomtype;
288     ga->radius[nt]       = radius;
289     ga->vol[nt]          = vol;
290     ga->surftens[nt]     = surftens;
291     ga->atomnumber[nt]   = atomnumber;
292     ga->gb_radius[nt]    = gb_radius;
293     ga->S_hct[nt]        = S_hct;
294
295     return nt;
296 }
297
298 int add_atomtype(gpp_atomtype_t ga, t_symtab *tab,
299                  t_atom *a, const char *name, t_param *nb,
300                  int bondatomtype,
301                  real radius, real vol, real surftens, real atomnumber,
302                  real gb_radius, real S_hct)
303 {
304     int i;
305
306     for (i = 0; (i < ga->nr); i++)
307     {
308         if (strcmp(*ga->atomname[i], name) == 0)
309         {
310             if (NULL != debug)
311             {
312                 fprintf(debug, "Trying to add atomtype %s again. Skipping it.\n", name);
313             }
314             break;
315         }
316     }
317     if (i == ga->nr)
318     {
319         ga->nr++;
320         srenew(ga->atom, ga->nr);
321         srenew(ga->atomname, ga->nr);
322         srenew(ga->nb, ga->nr);
323         srenew(ga->bondatomtype, ga->nr);
324         srenew(ga->radius, ga->nr);
325         srenew(ga->vol, ga->nr);
326         srenew(ga->surftens, ga->nr);
327         srenew(ga->atomnumber, ga->nr);
328         srenew(ga->gb_radius, ga->nr);
329         srenew(ga->S_hct, ga->nr);
330
331         return set_atomtype(ga->nr-1, ga, tab, a, name, nb, bondatomtype, radius,
332                             vol, surftens, atomnumber, gb_radius, S_hct);
333     }
334     else
335     {
336         return i;
337     }
338 }
339
340 void print_at (FILE * out, gpp_atomtype_t ga)
341 {
342     int         i;
343     t_atom     *atom = ga->atom;
344     t_param    *nb   = ga->nb;
345
346     fprintf (out, "[ %s ]\n", dir2str(d_atomtypes));
347     fprintf (out, "; %6s  %8s  %8s  %8s  %12s  %12s\n",
348              "type", "mass", "charge", "particle", "c6", "c12");
349     for (i = 0; (i < ga->nr); i++)
350     {
351         fprintf(out, "%8s  %8.3f  %8.3f  %8s  %12e  %12e\n",
352                 *(ga->atomname[i]), atom[i].m, atom[i].q, "A",
353                 nb[i].C0, nb[i].C1);
354     }
355
356     fprintf (out, "\n");
357 }
358
359 void done_atomtype(gpp_atomtype_t ga)
360 {
361     sfree(ga->atom);
362     sfree(ga->atomname);
363     sfree(ga->nb);
364     sfree(ga->bondatomtype);
365     sfree(ga->radius);
366     sfree(ga->vol);
367     sfree(ga->gb_radius);
368     sfree(ga->S_hct);
369     sfree(ga->surftens);
370     sfree(ga->atomnumber);
371     ga->nr = 0;
372     sfree(ga);
373 }
374
375 static int search_atomtypes(gpp_atomtype_t ga, int *n, int typelist[],
376                             int thistype,
377                             t_param param[], int ftype)
378 {
379     int      i, nn, nrfp, j, k, ntype, tli;
380     gmx_bool bFound = FALSE;
381
382     nn    = *n;
383     nrfp  = NRFP(ftype);
384     ntype = get_atomtype_ntypes(ga);
385
386     for (i = 0; (i < nn); i++)
387     {
388         if (typelist[i] == thistype)
389         {
390             /* This type number has already been added */
391             break;
392         }
393
394         /* Otherwise, check if the parameters are identical to any previously added type */
395
396         bFound = TRUE;
397         for (j = 0; j < ntype && bFound; j++)
398         {
399             /* Check nonbonded parameters */
400             for (k = 0; k < nrfp && bFound; k++)
401             {
402                 bFound = (param[ntype*typelist[i]+j].c[k] == param[ntype*thistype+j].c[k]);
403             }
404
405             /* Check radius, volume, surftens */
406             tli    = typelist[i];
407             bFound = bFound &&
408                 (get_atomtype_radius(tli, ga) == get_atomtype_radius(thistype, ga)) &&
409                 (get_atomtype_vol(tli, ga) == get_atomtype_vol(thistype, ga)) &&
410                 (get_atomtype_surftens(tli, ga) == get_atomtype_surftens(thistype, ga)) &&
411                 (get_atomtype_atomnumber(tli, ga) == get_atomtype_atomnumber(thistype, ga)) &&
412                 (get_atomtype_gb_radius(tli, ga) == get_atomtype_gb_radius(thistype, ga)) &&
413                 (get_atomtype_S_hct(tli, ga) == get_atomtype_S_hct(thistype, ga));
414         }
415         if (bFound)
416         {
417             break;
418         }
419     }
420
421     if (i == nn)
422     {
423         if (debug)
424         {
425             fprintf(debug, "Renumbering atomtype %d to %d\n", thistype, nn);
426         }
427         if (nn == ntype)
428         {
429             gmx_fatal(FARGS, "Atomtype horror n = %d, %s, %d", nn, __FILE__, __LINE__);
430         }
431         typelist[nn] = thistype;
432         nn++;
433     }
434     *n = nn;
435
436     return i;
437 }
438
439 void renum_atype(t_params plist[], gmx_mtop_t *mtop,
440                  int *wall_atomtype,
441                  gpp_atomtype_t ga, gmx_bool bVerbose)
442 {
443     int         i, j, k, l, molt, mi, mj, nat, nrfp, ftype, ntype;
444     t_atoms    *atoms;
445     t_param    *nbsnew;
446     int        *typelist;
447     real       *new_radius;
448     real       *new_vol;
449     real       *new_surftens;
450     real       *new_gb_radius;
451     real       *new_S_hct;
452     int        *new_atomnumber;
453     char     ***new_atomname;
454
455     ntype = get_atomtype_ntypes(ga);
456     snew(typelist, ntype);
457
458     if (bVerbose)
459     {
460         fprintf(stderr, "renumbering atomtypes...\n");
461     }
462
463     /* Since the bonded interactions have been assigned now,
464      * we want to reduce the number of atom types by merging
465      * ones with identical nonbonded interactions, in addition
466      * to removing unused ones.
467      *
468      * With Generalized-Born electrostatics, or implicit solvent
469      * we also check that the atomtype radius, effective_volume
470      * and surface tension match.
471      *
472      * With QM/MM we also check that the atom numbers match
473      */
474
475     /* Get nonbonded interaction type */
476     if (plist[F_LJ].nr > 0)
477     {
478         ftype = F_LJ;
479     }
480     else
481     {
482         ftype = F_BHAM;
483     }
484
485     /* Renumber atomtypes by first making a list of which ones are actually used.
486      * We provide the list of nonbonded parameters so search_atomtypes
487      * can determine if two types should be merged.
488      */
489     nat = 0;
490     for (molt = 0; molt < mtop->nmoltype; molt++)
491     {
492         atoms = &mtop->moltype[molt].atoms;
493         for (i = 0; (i < atoms->nr); i++)
494         {
495             atoms->atom[i].type =
496                 search_atomtypes(ga, &nat, typelist, atoms->atom[i].type,
497                                  plist[ftype].param, ftype);
498             atoms->atom[i].typeB =
499                 search_atomtypes(ga, &nat, typelist, atoms->atom[i].typeB,
500                                  plist[ftype].param, ftype);
501         }
502     }
503
504     for (i = 0; i < 2; i++)
505     {
506         if (wall_atomtype[i] >= 0)
507         {
508             wall_atomtype[i] = search_atomtypes(ga, &nat, typelist, wall_atomtype[i],
509                                                 plist[ftype].param, ftype);
510         }
511     }
512
513     snew(new_radius, nat);
514     snew(new_vol, nat);
515     snew(new_surftens, nat);
516     snew(new_atomnumber, nat);
517     snew(new_gb_radius, nat);
518     snew(new_S_hct, nat);
519     snew(new_atomname, nat);
520
521     /* We now have a list of unique atomtypes in typelist */
522
523     if (debug)
524     {
525         pr_ivec(debug, 0, "typelist", typelist, nat, TRUE);
526     }
527
528     /* Renumber nlist */
529     nbsnew = NULL;
530     snew(nbsnew, plist[ftype].nr);
531
532     nrfp  = NRFP(ftype);
533
534     for (i = k = 0; (i < nat); i++)
535     {
536         mi = typelist[i];
537         for (j = 0; (j < nat); j++, k++)
538         {
539             mj = typelist[j];
540             for (l = 0; (l < nrfp); l++)
541             {
542                 nbsnew[k].c[l] = plist[ftype].param[ntype*mi+mj].c[l];
543             }
544         }
545         new_radius[i]     = get_atomtype_radius(mi, ga);
546         new_vol[i]        = get_atomtype_vol(mi, ga);
547         new_surftens[i]   = get_atomtype_surftens(mi, ga);
548         new_atomnumber[i] = get_atomtype_atomnumber(mi, ga);
549         new_gb_radius[i]  = get_atomtype_gb_radius(mi, ga);
550         new_S_hct[i]      = get_atomtype_S_hct(mi, ga);
551         new_atomname[i]   = ga->atomname[mi];
552     }
553
554     for (i = 0; (i < nat*nat); i++)
555     {
556         for (l = 0; (l < nrfp); l++)
557         {
558             plist[ftype].param[i].c[l] = nbsnew[i].c[l];
559         }
560     }
561     plist[ftype].nr     = i;
562     mtop->ffparams.atnr = nat;
563
564     sfree(ga->radius);
565     sfree(ga->vol);
566     sfree(ga->surftens);
567     sfree(ga->atomnumber);
568     sfree(ga->gb_radius);
569     sfree(ga->S_hct);
570     /* Dangling atomname pointers ? */
571     sfree(ga->atomname);
572
573     ga->radius     = new_radius;
574     ga->vol        = new_vol;
575     ga->surftens   = new_surftens;
576     ga->atomnumber = new_atomnumber;
577     ga->gb_radius  = new_gb_radius;
578     ga->S_hct      = new_S_hct;
579     ga->atomname   = new_atomname;
580
581     ga->nr = nat;
582
583     sfree(nbsnew);
584     sfree(typelist);
585 }
586
587 void copy_atomtype_atomtypes(gpp_atomtype_t ga, t_atomtypes *atomtypes)
588 {
589     int i, ntype;
590
591     /* Copy the atomtype data to the topology atomtype list */
592     ntype         = get_atomtype_ntypes(ga);
593     atomtypes->nr = ntype;
594     snew(atomtypes->radius, ntype);
595     snew(atomtypes->vol, ntype);
596     snew(atomtypes->surftens, ntype);
597     snew(atomtypes->atomnumber, ntype);
598     snew(atomtypes->gb_radius, ntype);
599     snew(atomtypes->S_hct, ntype);
600
601     for (i = 0; i < ntype; i++)
602     {
603         atomtypes->radius[i]     = ga->radius[i];
604         atomtypes->vol[i]        = ga->vol[i];
605         atomtypes->surftens[i]   = ga->surftens[i];
606         atomtypes->atomnumber[i] = ga->atomnumber[i];
607         atomtypes->gb_radius[i]  = ga->gb_radius[i];
608         atomtypes->S_hct[i]      = ga->S_hct[i];
609     }
610 }