Merge release-5-0 into master
[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, 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 "gromacs/topology/atoms.h"
38
39 #include <cstring>
40
41 #include "gromacs/topology/symtab.h"
42 #include "gromacs/utility/smalloc.h"
43
44 void init_atom(t_atoms *at)
45 {
46     at->nr        = 0;
47     at->nres      = 0;
48     at->atom      = NULL;
49     at->resinfo   = NULL;
50     at->atomname  = NULL;
51     at->atomtype  = NULL;
52     at->atomtypeB = NULL;
53     at->pdbinfo   = NULL;
54 }
55
56 void init_atomtypes(t_atomtypes *at)
57 {
58     at->nr         = 0;
59     at->radius     = NULL;
60     at->vol        = NULL;
61     at->atomnumber = NULL;
62     at->gb_radius  = NULL;
63     at->S_hct      = NULL;
64 }
65
66 void done_atom(t_atoms *at)
67 {
68     at->nr       = 0;
69     at->nres     = 0;
70     sfree(at->atom);
71     sfree(at->resinfo);
72     sfree(at->atomname);
73     sfree(at->atomtype);
74     sfree(at->atomtypeB);
75     if (at->pdbinfo)
76     {
77         sfree(at->pdbinfo);
78     }
79 }
80
81 void done_atomtypes(t_atomtypes *atype)
82 {
83     atype->nr = 0;
84     sfree(atype->radius);
85     sfree(atype->vol);
86     sfree(atype->surftens);
87     sfree(atype->atomnumber);
88     sfree(atype->gb_radius);
89     sfree(atype->S_hct);
90 }
91
92 void add_t_atoms(t_atoms *atoms, int natom_extra, int nres_extra)
93 {
94     int i;
95
96     if (natom_extra > 0)
97     {
98         srenew(atoms->atomname, atoms->nr+natom_extra);
99         srenew(atoms->atom, atoms->nr+natom_extra);
100         if (NULL != atoms->pdbinfo)
101         {
102             srenew(atoms->pdbinfo, atoms->nr+natom_extra);
103         }
104         if (NULL != atoms->atomtype)
105         {
106             srenew(atoms->atomtype, atoms->nr+natom_extra);
107         }
108         if (NULL != atoms->atomtypeB)
109         {
110             srenew(atoms->atomtypeB, atoms->nr+natom_extra);
111         }
112         for (i = atoms->nr; (i < atoms->nr+natom_extra); i++)
113         {
114             atoms->atomname[i] = NULL;
115             memset(&atoms->atom[i], 0, sizeof(atoms->atom[i]));
116             if (NULL != atoms->pdbinfo)
117             {
118                 std::memset(&atoms->pdbinfo[i], 0, sizeof(atoms->pdbinfo[i]));
119             }
120             if (NULL != atoms->atomtype)
121             {
122                 atoms->atomtype[i] = NULL;
123             }
124             if (NULL != atoms->atomtypeB)
125             {
126                 atoms->atomtypeB[i] = NULL;
127             }
128         }
129         atoms->nr += natom_extra;
130     }
131     if (nres_extra > 0)
132     {
133         srenew(atoms->resinfo, atoms->nres+nres_extra);
134         for (i = atoms->nres; (i < atoms->nres+nres_extra); i++)
135         {
136             std::memset(&atoms->resinfo[i], 0, sizeof(atoms->resinfo[i]));
137         }
138         atoms->nres += nres_extra;
139     }
140 }
141
142 void init_t_atoms(t_atoms *atoms, int natoms, gmx_bool bPdbinfo)
143 {
144     atoms->nr   = natoms;
145     atoms->nres = 0;
146     snew(atoms->atomname, natoms);
147     atoms->atomtype  = NULL;
148     atoms->atomtypeB = NULL;
149     snew(atoms->resinfo, natoms);
150     snew(atoms->atom, natoms);
151     if (bPdbinfo)
152     {
153         snew(atoms->pdbinfo, natoms);
154     }
155     else
156     {
157         atoms->pdbinfo = NULL;
158     }
159 }
160
161 t_atoms *copy_t_atoms(t_atoms *src)
162 {
163     t_atoms *dst;
164     int      i;
165
166     snew(dst, 1);
167     init_t_atoms(dst, src->nr, (NULL != src->pdbinfo));
168     dst->nr = src->nr;
169     if (NULL != src->atomname)
170     {
171         snew(dst->atomname, src->nr);
172     }
173     if (NULL != src->atomtype)
174     {
175         snew(dst->atomtype, src->nr);
176     }
177     if (NULL != src->atomtypeB)
178     {
179         snew(dst->atomtypeB, src->nr);
180     }
181     for (i = 0; (i < src->nr); i++)
182     {
183         dst->atom[i] = src->atom[i];
184         if (NULL != src->pdbinfo)
185         {
186             dst->pdbinfo[i] = src->pdbinfo[i];
187         }
188         if (NULL != src->atomname)
189         {
190             dst->atomname[i]  = src->atomname[i];
191         }
192         if (NULL != src->atomtype)
193         {
194             dst->atomtype[i] = src->atomtype[i];
195         }
196         if (NULL != src->atomtypeB)
197         {
198             dst->atomtypeB[i] = src->atomtypeB[i];
199         }
200     }
201     dst->nres = src->nres;
202     for (i = 0; (i < src->nres); i++)
203     {
204         dst->resinfo[i] = src->resinfo[i];
205     }
206     return dst;
207 }
208
209 void t_atoms_set_resinfo(t_atoms *atoms, int atom_ind, t_symtab *symtab,
210                          const char *resname, int resnr, unsigned char ic,
211                          int chainnum, char chainid)
212 {
213     t_resinfo *ri;
214
215     ri           = &atoms->resinfo[atoms->atom[atom_ind].resind];
216     ri->name     = put_symtab(symtab, resname);
217     ri->rtp      = NULL;
218     ri->nr       = resnr;
219     ri->ic       = ic;
220     ri->chainnum = chainnum;
221     ri->chainid  = chainid;
222 }
223
224 void free_t_atoms(t_atoms *atoms, gmx_bool bFreeNames)
225 {
226     int i;
227
228     if (bFreeNames && atoms->atomname != NULL)
229     {
230         for (i = 0; i < atoms->nr; i++)
231         {
232             if (atoms->atomname[i] != NULL)
233             {
234                 sfree(*atoms->atomname[i]);
235                 *atoms->atomname[i] = NULL;
236             }
237         }
238     }
239     if (bFreeNames && atoms->resinfo != NULL)
240     {
241         for (i = 0; i < atoms->nres; i++)
242         {
243             if (atoms->resinfo[i].name != NULL)
244             {
245                 sfree(*atoms->resinfo[i].name);
246                 *atoms->resinfo[i].name = NULL;
247             }
248         }
249     }
250     if (bFreeNames && atoms->atomtype != NULL)
251     {
252         for (i = 0; i < atoms->nr; i++)
253         {
254             if (atoms->atomtype[i] != NULL)
255             {
256                 sfree(*atoms->atomtype[i]);
257                 *atoms->atomtype[i] = NULL;
258             }
259         }
260     }
261     if (bFreeNames && atoms->atomtypeB != NULL)
262     {
263         for (i = 0; i < atoms->nr; i++)
264         {
265             if (atoms->atomtypeB[i] != NULL)
266             {
267                 sfree(*atoms->atomtypeB[i]);
268                 *atoms->atomtypeB[i] = NULL;
269             }
270         }
271     }
272     sfree(atoms->atomname);
273     sfree(atoms->atomtype);
274     sfree(atoms->atomtypeB);
275     sfree(atoms->resinfo);
276     sfree(atoms->atom);
277     sfree(atoms->pdbinfo);
278     atoms->nr        = 0;
279     atoms->nres      = 0;
280     atoms->atomname  = NULL;
281     atoms->atomtype  = NULL;
282     atoms->atomtypeB = NULL;
283     atoms->resinfo   = NULL;
284     atoms->atom      = NULL;
285     atoms->pdbinfo   = NULL;
286 }