Upped the version to 3.2.0
[gromacs.git] / src / tools / edittop.c
blob812fd3e80351774483bc694eb71deb4482fa491d
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
33 * And Hey:
34 * Green Red Orange Magenta Azure Cyan Skyblue
36 #include "smalloc.h"
37 #include "string2.h"
38 #include "fatal.h"
39 #include "assert.h"
40 #include "symtab.h"
42 void replace_atom(t_topology *top,int inr,char *anm,char *resnm,
43 real q,real m,int type)
45 t_atoms *atoms;
47 atoms = &(top->atoms);
49 /* Replace important properties of an atom by other properties */
50 if ((inr < 0) || (inr > atoms->nr))
51 fatal_error(0,"Replace_atom: inr (%d) not in %d .. %d",inr,0,atoms->nr);
52 if (debug)
53 fprintf(debug,"Replacing atom %d ... ",inr);
54 /* Charge, mass and type */
55 atoms->atom[inr].q = atoms->atom[inr].qB = q;
56 atoms->atom[inr].m = atoms->atom[inr].mB = m;
57 atoms->atom[inr].type = atoms->atom[inr].typeB = type;
59 /* Residue name */
60 atoms->resname[atoms->atom[inr].resnr] = put_symtab(&top->symtab,resnm);
61 /* Atom name */
62 atoms->atomname[inr] = put_symtab(&top->symtab,anm);
63 if (debug)
64 fprintf(debug," done\n");
67 static void delete_from_interactions(t_idef *idef,int inr)
69 int i,j,k,nra,nnr;
70 t_iatom *niatoms;
71 bool bDel;
73 /* Delete interactions including atom inr from lists */
74 for(i=0; (i<F_NRE); i++) {
75 nra = interaction_function[i].nratoms;
76 nnr = 0;
77 snew(niatoms,idef->il[i].nr);
78 for(j=0; (j<idef->il[i].nr); j+=nra+1) {
79 bDel = FALSE;
80 for(k=0; (k<nra); k++)
81 if (idef->il[i].iatoms[j+k+1] == inr)
82 bDel = TRUE;
83 if (!bDel) {
84 /* If this does not need to be deleted, then copy it to temp array */
85 for(k=0; (k<nra+1); k++)
86 niatoms[nnr+k] = idef->il[i].iatoms[j+k];
87 nnr+=nra+1;
90 /* Copy temp array back */
91 for(j=0; (j<nnr); j++)
92 idef->il[i].iatoms[j] = niatoms[j];
93 idef->il[i].nr = nnr;
94 sfree(niatoms);
96 /* Reduce multinr if necessary */
97 for(j=0; (j<MAXNODES); j++)
98 if (idef->il[i].multinr[j] >= nnr)
99 idef->il[i].multinr[j] = nnr;
103 static void delete_from_block(t_block *block,int inr)
105 /* Update block data structure */
106 int i,i1,j1,j,k;
108 for(i=0; (i<block->nr); i++) {
109 for(j=block->index[i]; (j<block->index[i+1]); j++) {
110 k = block->a[j];
111 if (k == inr) {
112 /* This atom has to go */
113 for(j1=j; (j1<block->nra-1); j1++)
114 block->a[j1] = block->a[j1+1];
115 block->nra--;
116 /* Change indices too */
117 for(i1=i+1; (i1<=block->nr); i1++)
118 block->index[i1]--;
124 static void delete_from_atoms(t_atoms *atoms,int inr)
126 int i,nrei,ind0;
128 /* Delete inr as an exclusion from other atoms */
129 delete_from_block(&(atoms->excl),inr);
130 /* Now delete the exclusions with inr as i atom */
131 ind0 = atoms->excl.index[inr];
132 nrei = atoms->excl.index[inr+1]-ind0;
133 for(i=ind0+nrei; (i<atoms->excl.nra); i++)
134 atoms->excl.a[i-nrei] = atoms->excl.a[i];
135 atoms->excl.nra -= nrei;
136 for(i=inr; (i<atoms->excl.nr); i++)
137 atoms->excl.index[i] = atoms->excl.index[i+1] - nrei;
138 atoms->excl.nr--;
139 assert(atoms->excl.index[atoms->excl.nr] == atoms->excl.nra);
141 /* Shift the atomnames down */
142 for(i=inr; (i<atoms->nr-1); i++)
143 atoms->atomname[i] = atoms->atomname[i+1];
145 /* Shift the atom struct down */
146 for(i=inr; (i<atoms->nr-1); i++)
147 atoms->atom[i] = atoms->atom[i+1];
149 if (atoms->pdbinfo) {
150 /* Shift the pdbatom struct down */
151 for(i=inr; (i<atoms->nr-1); i++)
152 atoms->pdbinfo[i] = atoms->pdbinfo[i+1];
154 atoms->nr--;
157 void delete_atom(t_topology *top,int inr)
159 int k;
161 if ((inr < 0) || (inr >= top->atoms.nr))
162 fatal_error(0,"Delete_atom: inr (%d) not in %d .. %d",inr,0,
163 top->atoms.nr);
164 if (debug)
165 fprintf(debug,"Deleting atom %d ...",inr);
167 /* First remove bonds etc. */
168 delete_from_interactions(&top->idef,inr);
169 /* Now charge groups etc. */
170 for(k=0; (k<ebNR); k++)
171 delete_from_block(&(top->blocks[k]),inr);
172 /* Now from the atoms struct */
173 delete_from_atoms(&top->atoms,inr);
174 if (debug)
175 fprintf(debug," done\n");