Add missing Ewald correction for pme-user
[alexxy/gromacs.git] / src / gromacs / topology / atoms.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016, 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 #include "gmxpre.h"
38
39 #include "atoms.h"
40
41 #include <cstdio>
42 #include <cstring>
43
44 #include <algorithm>
45
46 #include "gromacs/topology/symtab.h"
47 #include "gromacs/utility/smalloc.h"
48 #include "gromacs/utility/txtdump.h"
49
50 const char *ptype_str[eptNR+1] = {
51     "Atom", "Nucleus", "Shell", "Bond", "VSite", NULL
52 };
53
54 void init_atom(t_atoms *at)
55 {
56     at->nr        = 0;
57     at->nres      = 0;
58     at->atom      = NULL;
59     at->resinfo   = NULL;
60     at->atomname  = NULL;
61     at->atomtype  = NULL;
62     at->atomtypeB = NULL;
63     at->pdbinfo   = NULL;
64 }
65
66 void init_atomtypes(t_atomtypes *at)
67 {
68     at->nr         = 0;
69     at->radius     = NULL;
70     at->vol        = NULL;
71     at->atomnumber = NULL;
72     at->gb_radius  = NULL;
73     at->S_hct      = NULL;
74 }
75
76 void done_atom(t_atoms *at)
77 {
78     sfree(at->atom);
79     sfree(at->resinfo);
80     sfree(at->atomname);
81     sfree(at->atomtype);
82     sfree(at->atomtypeB);
83     sfree(at->pdbinfo);
84     init_atom(at);
85 }
86
87 void done_atomtypes(t_atomtypes *atype)
88 {
89     atype->nr = 0;
90     sfree(atype->radius);
91     sfree(atype->vol);
92     sfree(atype->surftens);
93     sfree(atype->atomnumber);
94     sfree(atype->gb_radius);
95     sfree(atype->S_hct);
96 }
97
98 void add_t_atoms(t_atoms *atoms, int natom_extra, int nres_extra)
99 {
100     int i;
101
102     if (natom_extra > 0)
103     {
104         srenew(atoms->atomname, atoms->nr+natom_extra);
105         srenew(atoms->atom, atoms->nr+natom_extra);
106         if (NULL != atoms->pdbinfo)
107         {
108             srenew(atoms->pdbinfo, atoms->nr+natom_extra);
109         }
110         if (NULL != atoms->atomtype)
111         {
112             srenew(atoms->atomtype, atoms->nr+natom_extra);
113         }
114         if (NULL != atoms->atomtypeB)
115         {
116             srenew(atoms->atomtypeB, atoms->nr+natom_extra);
117         }
118         for (i = atoms->nr; (i < atoms->nr+natom_extra); i++)
119         {
120             atoms->atomname[i] = NULL;
121             memset(&atoms->atom[i], 0, sizeof(atoms->atom[i]));
122             if (NULL != atoms->pdbinfo)
123             {
124                 std::memset(&atoms->pdbinfo[i], 0, sizeof(atoms->pdbinfo[i]));
125             }
126             if (NULL != atoms->atomtype)
127             {
128                 atoms->atomtype[i] = NULL;
129             }
130             if (NULL != atoms->atomtypeB)
131             {
132                 atoms->atomtypeB[i] = NULL;
133             }
134         }
135         atoms->nr += natom_extra;
136     }
137     if (nres_extra > 0)
138     {
139         srenew(atoms->resinfo, atoms->nres+nres_extra);
140         for (i = atoms->nres; (i < atoms->nres+nres_extra); i++)
141         {
142             std::memset(&atoms->resinfo[i], 0, sizeof(atoms->resinfo[i]));
143         }
144         atoms->nres += nres_extra;
145     }
146 }
147
148 void init_t_atoms(t_atoms *atoms, int natoms, gmx_bool bPdbinfo)
149 {
150     atoms->nr   = natoms;
151     atoms->nres = 0;
152     snew(atoms->atomname, natoms);
153     atoms->atomtype  = NULL;
154     atoms->atomtypeB = NULL;
155     snew(atoms->resinfo, natoms);
156     snew(atoms->atom, natoms);
157     if (bPdbinfo)
158     {
159         snew(atoms->pdbinfo, natoms);
160     }
161     else
162     {
163         atoms->pdbinfo = NULL;
164     }
165 }
166
167 void gmx_pdbinfo_init_default(t_pdbinfo *pdbinfo)
168 {
169     pdbinfo->type         = epdbATOM;
170     pdbinfo->atomnr       = 0;
171     pdbinfo->altloc       = ' ';
172     pdbinfo->atomnm[0]    = '\0';
173     pdbinfo->occup        = 1.0;
174     pdbinfo->bfac         = 0.0;
175     pdbinfo->bAnisotropic = FALSE;
176     std::fill(pdbinfo->uij, pdbinfo->uij+6, 0.0);
177 }
178
179 t_atoms *copy_t_atoms(const t_atoms *src)
180 {
181     t_atoms *dst;
182     int      i;
183
184     snew(dst, 1);
185     init_t_atoms(dst, src->nr, (NULL != src->pdbinfo));
186     dst->nr = src->nr;
187     if (NULL != src->atomname)
188     {
189         snew(dst->atomname, src->nr);
190     }
191     if (NULL != src->atomtype)
192     {
193         snew(dst->atomtype, src->nr);
194     }
195     if (NULL != src->atomtypeB)
196     {
197         snew(dst->atomtypeB, src->nr);
198     }
199     for (i = 0; (i < src->nr); i++)
200     {
201         dst->atom[i] = src->atom[i];
202         if (NULL != src->pdbinfo)
203         {
204             dst->pdbinfo[i] = src->pdbinfo[i];
205         }
206         if (NULL != src->atomname)
207         {
208             dst->atomname[i]  = src->atomname[i];
209         }
210         if (NULL != src->atomtype)
211         {
212             dst->atomtype[i] = src->atomtype[i];
213         }
214         if (NULL != src->atomtypeB)
215         {
216             dst->atomtypeB[i] = src->atomtypeB[i];
217         }
218     }
219     dst->nres = src->nres;
220     for (i = 0; (i < src->nres); i++)
221     {
222         dst->resinfo[i] = src->resinfo[i];
223     }
224     return dst;
225 }
226
227 void t_atoms_set_resinfo(t_atoms *atoms, int atom_ind, t_symtab *symtab,
228                          const char *resname, int resnr, unsigned char ic,
229                          int chainnum, char chainid)
230 {
231     t_resinfo *ri;
232
233     ri           = &atoms->resinfo[atoms->atom[atom_ind].resind];
234     ri->name     = put_symtab(symtab, resname);
235     ri->rtp      = NULL;
236     ri->nr       = resnr;
237     ri->ic       = ic;
238     ri->chainnum = chainnum;
239     ri->chainid  = chainid;
240 }
241
242 static void pr_atom(FILE *fp, int indent, const char *title, const t_atom *atom, int n)
243 {
244     int i;
245
246     if (available(fp, atom, indent, title))
247     {
248         indent = pr_title_n(fp, indent, title, n);
249         for (i = 0; i < n; i++)
250         {
251             pr_indent(fp, indent);
252             fprintf(fp, "%s[%6d]={type=%3hu, typeB=%3hu, ptype=%8s, m=%12.5e, "
253                     "q=%12.5e, mB=%12.5e, qB=%12.5e, resind=%5d, atomnumber=%3d}\n",
254                     title, i, atom[i].type, atom[i].typeB, ptype_str[atom[i].ptype],
255                     atom[i].m, atom[i].q, atom[i].mB, atom[i].qB,
256                     atom[i].resind, atom[i].atomnumber);
257         }
258     }
259 }
260
261 static void pr_strings2(FILE *fp, int indent, const char *title,
262                         char ***nm, char ***nmB, int n, gmx_bool bShowNumbers)
263 {
264     int i;
265
266     if (available(fp, nm, indent, title))
267     {
268         indent = pr_title_n(fp, indent, title, n);
269         for (i = 0; i < n; i++)
270         {
271             pr_indent(fp, indent);
272             fprintf(fp, "%s[%d]={name=\"%s\",nameB=\"%s\"}\n",
273                     title, bShowNumbers ? i : -1, *(nm[i]), *(nmB[i]));
274         }
275     }
276 }
277
278 static void pr_resinfo(FILE *fp, int indent, const char *title, const t_resinfo *resinfo, int n,
279                        gmx_bool bShowNumbers)
280 {
281     int i;
282
283     if (available(fp, resinfo, indent, title))
284     {
285         indent = pr_title_n(fp, indent, title, n);
286         for (i = 0; i < n; i++)
287         {
288             pr_indent(fp, indent);
289             fprintf(fp, "%s[%d]={name=\"%s\", nr=%d, ic='%c'}\n",
290                     title, bShowNumbers ? i : -1,
291                     *(resinfo[i].name), resinfo[i].nr,
292                     (resinfo[i].ic == '\0') ? ' ' : resinfo[i].ic);
293         }
294     }
295 }
296
297 void pr_atoms(FILE *fp, int indent, const char *title, const t_atoms *atoms,
298               gmx_bool bShownumbers)
299 {
300     if (available(fp, atoms, indent, title))
301     {
302         indent = pr_title(fp, indent, title);
303         pr_atom(fp, indent, "atom", atoms->atom, atoms->nr);
304         pr_strings(fp, indent, "atom", atoms->atomname, atoms->nr, bShownumbers);
305         pr_strings2(fp, indent, "type", atoms->atomtype, atoms->atomtypeB, atoms->nr, bShownumbers);
306         pr_resinfo(fp, indent, "residue", atoms->resinfo, atoms->nres, bShownumbers);
307     }
308 }
309
310
311 void pr_atomtypes(FILE *fp, int indent, const char *title, const t_atomtypes *atomtypes,
312                   gmx_bool bShowNumbers)
313 {
314     int i;
315     if (available(fp, atomtypes, indent, title))
316     {
317         indent = pr_title(fp, indent, title);
318         for (i = 0; i < atomtypes->nr; i++)
319         {
320             pr_indent(fp, indent);
321             fprintf(fp,
322                     "atomtype[%3d]={radius=%12.5e, volume=%12.5e, gb_radius=%12.5e, surftens=%12.5e, atomnumber=%4d, S_hct=%12.5e)}\n",
323                     bShowNumbers ? i : -1, atomtypes->radius[i], atomtypes->vol[i],
324                     atomtypes->gb_radius[i],
325                     atomtypes->surftens[i], atomtypes->atomnumber[i], atomtypes->S_hct[i]);
326         }
327     }
328 }