Fix cppcheck 1.64 warnings
[alexxy/gromacs.git] / src / gromacs / gmxana / edittop.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 "gromacs/utility/smalloc.h"
42 #include "gromacs/utility/fatalerror.h"
43 #include "symtab.h"
44
45 void replace_atom(t_topology *top, int inr, char *anm, char *resnm,
46                   real q, real m, int type)
47 {
48     t_atoms *atoms;
49
50     atoms = &(top->atoms);
51
52     /* Replace important properties of an atom by other properties */
53     if ((inr < 0) || (inr > atoms->nr))
54     {
55         gmx_fatal(FARGS, "Replace_atom: inr (%d) not in %d .. %d", inr, 0, atoms->nr);
56     }
57     if (debug)
58     {
59         fprintf(debug, "Replacing atom %d ... ", inr);
60     }
61     /* Charge, mass and type */
62     atoms->atom[inr].q    = atoms->atom[inr].qB    = q;
63     atoms->atom[inr].m    = atoms->atom[inr].mB    = m;
64     atoms->atom[inr].type = atoms->atom[inr].typeB = type;
65
66     /* Residue name */
67     atoms->resinfo[atoms->atom[inr].resind].name = put_symtab(&top->symtab, resnm);
68     /* Atom name */
69     atoms->atomname[inr] = put_symtab(&top->symtab, anm);
70     if (debug)
71     {
72         fprintf(debug, " done\n");
73     }
74 }
75
76 static void delete_from_interactions(t_idef *idef, int inr)
77 {
78     int      i, j, k, nra, nnr;
79     t_iatom *niatoms;
80     gmx_bool bDel;
81
82     /* Delete interactions including atom inr from lists */
83     for (i = 0; (i < F_NRE); i++)
84     {
85         nra = interaction_function[i].nratoms;
86         nnr = 0;
87         snew(niatoms, idef->il[i].nr);
88         for (j = 0; (j < idef->il[i].nr); j += nra+1)
89         {
90             bDel = FALSE;
91             for (k = 0; (k < nra); k++)
92             {
93                 if (idef->il[i].iatoms[j+k+1] == inr)
94                 {
95                     bDel = TRUE;
96                 }
97             }
98             if (!bDel)
99             {
100                 /* If this does not need to be deleted, then copy it to temp array */
101                 for (k = 0; (k < nra+1); k++)
102                 {
103                     niatoms[nnr+k] = idef->il[i].iatoms[j+k];
104                 }
105                 nnr += nra+1;
106             }
107         }
108         /* Copy temp array back */
109         for (j = 0; (j < nnr); j++)
110         {
111             idef->il[i].iatoms[j] = niatoms[j];
112         }
113         idef->il[i].nr = nnr;
114         /* cppcheck-suppress uninitvar Fixed in cppcheck 1.65 */
115         sfree(niatoms);
116     }
117 }
118
119 static void delete_from_block(t_block *block, int inr)
120 {
121     /* Update block data structure */
122     int i, i1, j1, j, k;
123
124     for (i = 0; (i < block->nr); i++)
125     {
126         for (j = block->index[i]; (j < block->index[i+1]); j++)
127         {
128             if (j == inr)
129             {
130                 /* This atom has to go */
131                 /* Change indices too */
132                 for (i1 = i+1; (i1 <= block->nr); i1++)
133                 {
134                     block->index[i1]--;
135                 }
136             }
137         }
138     }
139 }
140
141 static void delete_from_blocka(t_blocka *block, int inr)
142 {
143     /* Update block data structure */
144     int i, i1, j1, j, k;
145
146     for (i = 0; (i < block->nr); i++)
147     {
148         for (j = block->index[i]; (j < block->index[i+1]); j++)
149         {
150             k = block->a[j];
151             if (k == inr)
152             {
153                 /* This atom has to go */
154                 for (j1 = j; (j1 < block->nra-1); j1++)
155                 {
156                     block->a[j1] = block->a[j1+1];
157                 }
158                 block->nra--;
159                 /* Change indices too */
160                 for (i1 = i+1; (i1 <= block->nr); i1++)
161                 {
162                     block->index[i1]--;
163                 }
164             }
165         }
166     }
167 }
168
169 static void delete_from_atoms(t_atoms *atoms, int inr)
170 {
171     int i;
172
173     /* Shift the atomnames down */
174     for (i = inr; (i < atoms->nr-1); i++)
175     {
176         atoms->atomname[i] = atoms->atomname[i+1];
177     }
178
179     /* Shift the atom struct down */
180     for (i = inr; (i < atoms->nr-1); i++)
181     {
182         atoms->atom[i] = atoms->atom[i+1];
183     }
184
185     if (atoms->pdbinfo)
186     {
187         /* Shift the pdbatom struct down */
188         for (i = inr; (i < atoms->nr-1); i++)
189         {
190             atoms->pdbinfo[i] = atoms->pdbinfo[i+1];
191         }
192     }
193     atoms->nr--;
194 }
195
196 void delete_atom(t_topology *top, int inr)
197 {
198     int k;
199
200     if ((inr < 0) || (inr >= top->atoms.nr))
201     {
202         gmx_fatal(FARGS, "Delete_atom: inr (%d) not in %d .. %d", inr, 0,
203                   top->atoms.nr);
204     }
205     if (debug)
206     {
207         fprintf(debug, "Deleting atom %d ...", inr);
208     }
209
210     /* First remove bonds etc. */
211     delete_from_interactions(&top->idef, inr);
212     /* Now charge groups etc. */
213     delete_from_block(&(top->cgs), inr);
214     delete_from_block(&(top->mols), inr);
215     delete_from_blocka(&(top->excls), inr);
216     /* Now from the atoms struct */
217     delete_from_atoms(&top->atoms, inr);
218     if (debug)
219     {
220         fprintf(debug, " done\n");
221     }
222 }