Fixing copyright issues and code contributors
[alexxy/gromacs.git] / src / tools / 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  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012,2013, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "smalloc.h"
43 #include "string2.h"
44 #include "gmx_fatal.h"
45 #include "symtab.h"
46
47 void replace_atom(t_topology *top,int inr,char *anm,char *resnm,
48                   real q,real m,int type)
49 {
50   t_atoms *atoms;
51
52   atoms = &(top->atoms);
53   
54   /* Replace important properties of an atom by other properties */  
55   if ((inr < 0) || (inr > atoms->nr))
56     gmx_fatal(FARGS,"Replace_atom: inr (%d) not in %d .. %d",inr,0,atoms->nr);
57   if (debug)
58     fprintf(debug,"Replacing atom %d ... ",inr);
59   /* Charge, mass and type */
60   atoms->atom[inr].q    = atoms->atom[inr].qB    = q;
61   atoms->atom[inr].m    = atoms->atom[inr].mB    = m;
62   atoms->atom[inr].type = atoms->atom[inr].typeB = type;
63   
64   /* Residue name */
65   atoms->resinfo[atoms->atom[inr].resind].name = put_symtab(&top->symtab,resnm);
66   /* Atom name */
67   atoms->atomname[inr] = put_symtab(&top->symtab,anm);
68   if (debug)
69     fprintf(debug," done\n");
70 }
71
72 static void delete_from_interactions(t_idef *idef,int inr)
73 {
74   int  i,j,k,nra,nnr;
75   t_iatom *niatoms;
76   gmx_bool bDel;
77   
78   /* Delete interactions including atom inr from lists */
79   for(i=0; (i<F_NRE); i++) {
80     nra = interaction_function[i].nratoms;
81     nnr = 0;
82     snew(niatoms,idef->il[i].nr);
83     for(j=0; (j<idef->il[i].nr); j+=nra+1) {
84       bDel = FALSE;
85       for(k=0; (k<nra); k++)
86         if (idef->il[i].iatoms[j+k+1] == inr)
87           bDel = TRUE;
88       if (!bDel) {
89         /* If this does not need to be deleted, then copy it to temp array */
90         for(k=0; (k<nra+1); k++)
91           niatoms[nnr+k] = idef->il[i].iatoms[j+k];
92         nnr+=nra+1;
93       }
94     }
95     /* Copy temp array back */
96     for(j=0; (j<nnr); j++)
97       idef->il[i].iatoms[j] = niatoms[j];
98     idef->il[i].nr = nnr;
99     sfree(niatoms);
100   }
101 }
102
103 static void delete_from_block(t_block *block,int inr) 
104 {
105   /* Update block data structure */
106   int i,i1,j1,j,k;
107   
108   for(i=0; (i<block->nr); i++) {
109     for(j=block->index[i]; (j<block->index[i+1]); j++) {
110       if (j == inr) {
111         /* This atom has to go */
112         /* Change indices too */
113         for(i1=i+1; (i1<=block->nr); i1++)
114           block->index[i1]--;
115       }
116     }
117   }
118 }
119
120 static void delete_from_blocka(t_blocka *block,int inr) 
121 {
122   /* Update block data structure */
123   int i,i1,j1,j,k;
124   
125   for(i=0; (i<block->nr); i++) {
126     for(j=block->index[i]; (j<block->index[i+1]); j++) {
127       k = block->a[j];
128       if (k == inr) {
129         /* This atom has to go */
130         for(j1=j; (j1<block->nra-1); j1++)
131           block->a[j1] = block->a[j1+1];
132         block->nra--;
133         /* Change indices too */
134         for(i1=i+1; (i1<=block->nr); i1++)
135           block->index[i1]--;
136       }
137     }
138   }
139 }
140
141 static void delete_from_atoms(t_atoms *atoms,int inr)
142 {
143   int i;
144   
145   /* Shift the atomnames down */
146   for(i=inr; (i<atoms->nr-1); i++)
147     atoms->atomname[i] = atoms->atomname[i+1];
148   
149   /* Shift the atom struct down */
150   for(i=inr; (i<atoms->nr-1); i++)
151     atoms->atom[i] = atoms->atom[i+1];
152     
153   if (atoms->pdbinfo) {
154     /* Shift the pdbatom struct down */
155     for(i=inr; (i<atoms->nr-1); i++)
156       atoms->pdbinfo[i] = atoms->pdbinfo[i+1];
157   }
158   atoms->nr--;
159 }
160
161 void delete_atom(t_topology *top,int inr)
162 {
163   int k;
164   
165   if ((inr < 0) || (inr >= top->atoms.nr))
166     gmx_fatal(FARGS,"Delete_atom: inr (%d) not in %d .. %d",inr,0,
167                 top->atoms.nr);
168   if (debug)
169     fprintf(debug,"Deleting atom %d ...",inr);
170
171   /* First remove bonds etc. */
172   delete_from_interactions(&top->idef,inr);
173   /* Now charge groups etc. */
174   delete_from_block(&(top->cgs),inr);
175   delete_from_block(&(top->mols),inr);
176   delete_from_blocka(&(top->excls),inr);
177   /* Now from the atoms struct */
178   delete_from_atoms(&top->atoms,inr);
179   if (debug)
180     fprintf(debug," done\n");
181 }