c7086b9762a494733680ee9808dc9654edbfb9ba
[alexxy/gromacs.git] / src / kernel / hackblock.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  * 
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
34  */
35 /* This file is completely threadsafe - keep it that way! */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <string.h>
41 #include "hackblock.h"
42 #include "smalloc.h"
43 #include "vec.h"
44 #include "string2.h"
45
46 /* these MUST correspond to the enum in hackblock.h */
47 const char *btsNames[ebtsNR] = { "bonds", "angles", "dihedrals", "impropers", "exclusions", "cmap" };
48 const int btsNiatoms[ebtsNR] = { 2,       3,        4,           4,           2,             5 };
49
50 static void free_t_bonded(t_rbonded *rb)
51 {
52   int i;
53   
54   for (i=0; i<MAXATOMLIST; i++)
55     sfree(rb->a[i]);
56   sfree(rb->s);
57 }
58
59 static void free_t_bondeds(t_rbondeds *rbs)
60 {
61   int i;
62   
63   for(i=0; i<rbs->nb; i++)
64     free_t_bonded(&rbs->b[i]);
65   sfree(rbs->b);
66   rbs->b=NULL;
67   rbs->nb=0;
68 }
69
70 void free_t_restp(int nrtp, t_restp **rtp)
71 {
72   int i,j;
73   
74   for(i=0; i<nrtp; i++) {
75     sfree((*rtp)[i].resname);
76     sfree((*rtp)[i].atom);
77     for(j=0; j<(*rtp)[i].natom; j++) {
78       sfree(*(*rtp)[i].atomname[j]);
79       sfree((*rtp)[i].atomname[j]);
80     }
81     sfree((*rtp)[i].atomname);
82     sfree((*rtp)[i].cgnr);
83     for(j=0; j<ebtsNR; j++)
84       free_t_bondeds(&(*rtp)[i].rb[j]);
85   }
86   free(*rtp);
87 }
88
89 void free_t_hack(int nh, t_hack **h)
90 {
91   int i, j;
92   
93   for(i=0; i<nh; i++) {
94     sfree((*h)[i].oname);
95     sfree((*h)[i].nname);
96     sfree((*h)[i].atom);
97     for(j=0; j<4; j++)
98       sfree((*h)[i].a[j]);
99   }
100   sfree(*h);
101   *h=NULL;
102 }
103
104 void free_t_hackblock(int nhb, t_hackblock **hb)
105 {
106   int i, j;
107   
108   for(i=0; i<nhb; i++) {
109     sfree((*hb)[i].name);
110     free_t_hack((*hb)[i].nhack, &(*hb)[i].hack);
111     for(j=0; j<ebtsNR; j++)
112       free_t_bondeds(&(*hb)[i].rb[j]);
113   }
114   sfree(*hb);
115 }
116
117 void clear_t_hackblock(t_hackblock *hb)
118 {
119   int i;
120   
121   hb->name   = NULL;
122   hb->nhack  = 0;
123   hb->maxhack= 0;
124   hb->hack   = NULL;
125   for(i=0; i<ebtsNR; i++) {
126     hb->rb[i].nb=0;
127     hb->rb[i].b=NULL;
128   }
129 }
130
131 void clear_t_hack(t_hack *hack)
132 {
133   int i;
134   
135   hack->nr    = 0;
136   hack->oname = NULL;
137   hack->nname = NULL;
138   hack->atom  = NULL;
139   hack->cgnr  = NOTSET;
140   hack->tp    = 0;
141   hack->nctl  = 0;
142   for(i=0; i<4; i++)
143     hack->a[i]  = NULL;
144   for(i=0; i<DIM; i++)
145     hack->newx[i] = NOTSET;
146 }
147
148 #define safe_strdup(str) ((str != NULL) ? strdup(str) : NULL)
149
150 static void copy_t_rbonded(t_rbonded *s, t_rbonded *d)
151 {
152   int i;
153   
154   for(i=0; i<MAXATOMLIST; i++)
155     d->a[i] = safe_strdup(s->a[i]);
156   d->s = safe_strdup(s->s);
157 }
158
159 static gmx_bool contains_char(t_rbonded *s,char c)
160 {
161   int i;
162   gmx_bool bRet;
163   
164   bRet = FALSE;
165   for(i=0; i<MAXATOMLIST; i++)
166     if (s->a[i] && s->a[i][0]==c)
167       bRet = TRUE;
168   
169   return bRet;
170 }
171
172 gmx_bool merge_t_bondeds(t_rbondeds s[], t_rbondeds d[],gmx_bool bMin,gmx_bool bPlus)
173 {
174   int i, j;
175   gmx_bool bBondsRemoved;
176   
177   bBondsRemoved = FALSE;
178   for(i=0; i < ebtsNR; i++) {
179     if ( s[i].nb > 0 ) {
180       /* make space */
181       srenew(d[i].b, d[i].nb + s[i].nb);
182       for(j=0; j < s[i].nb; j++)
183         if (!(bMin && contains_char(&s[i].b[j],'-'))
184             && !(bPlus && contains_char(&s[i].b[j],'+'))) {
185           copy_t_rbonded(&s[i].b[j], &d[i].b[ d[i].nb ]);
186           d[i].nb ++;
187         } else if (i == ebtsBONDS) {
188           bBondsRemoved = TRUE;
189         }
190     }
191   }
192
193   return bBondsRemoved;
194 }
195
196 void copy_t_restp(t_restp *s, t_restp *d)
197 {
198   int i;
199   
200   *d = *s;
201   d->resname = safe_strdup(s->resname);
202   snew(d->atom, s->natom);
203   for(i=0; i < s->natom; i++)
204     d->atom[i] = s->atom[i];
205   snew(d->atomname, s->natom);
206   for(i=0; i < s->natom; i++) {
207     snew(d->atomname[i],1);
208     *d->atomname[i] = safe_strdup(*s->atomname[i]);
209   }
210   snew(d->cgnr, s->natom);
211   for(i=0; i < s->natom; i++)
212     d->cgnr[i] = s->cgnr[i];
213   for(i=0; i < ebtsNR; i++) {
214     d->rb[i].type = s->rb[i].type;
215     d->rb[i].nb = 0;
216     d->rb[i].b = NULL;
217   }
218   merge_t_bondeds(s->rb, d->rb,FALSE,FALSE);
219 }
220
221 void copy_t_hack(t_hack *s, t_hack *d)
222 {
223   int i;
224   
225   *d = *s;
226   d->oname = safe_strdup(s->oname);
227   d->nname = safe_strdup(s->nname);
228   if (s->atom) {
229     snew(d->atom, 1);
230     *(d->atom) = *(s->atom);
231   } else
232     d->atom = NULL;
233   for(i=0; i<4; i++)
234     d->a[i] = safe_strdup(s->a[i]);
235   copy_rvec(s->newx, d->newx);
236 }
237
238 void merge_hacks_lo(int ns, t_hack *s, int *nd, t_hack **d)
239 {
240   int i;
241   
242   if (ns) {
243     srenew(*d, *nd + ns);
244     for(i=0; i < ns; i++)
245       copy_t_hack(&s[i], &(*d)[*nd + i]);
246     (*nd) += ns;
247   }
248 }
249
250 void merge_hacks(t_hackblock *s, t_hackblock *d)
251 {
252   merge_hacks_lo(s->nhack, s->hack, &d->nhack, &d->hack);
253 }
254
255 void merge_t_hackblock(t_hackblock *s, t_hackblock *d)
256 {
257   merge_hacks(s, d);
258   merge_t_bondeds(s->rb, d->rb,FALSE,FALSE);
259 }
260
261 void copy_t_hackblock(t_hackblock *s, t_hackblock *d)
262 {
263   int i;
264   
265   *d       = *s;
266   d->name  = safe_strdup(s->name);
267   d->nhack = 0;
268   d->hack  = NULL;
269   for(i=0; i<ebtsNR; i++) {
270     d->rb[i].nb=0;
271     d->rb[i].b=NULL;
272   }
273   merge_t_hackblock(s, d);
274 }
275
276 #undef safe_strdup
277
278 void dump_hb(FILE *out, int nres, t_hackblock hb[])
279 {
280   int i,j,k,l;
281   
282 #define SS(s) (s) ? (s) : "-"
283 #define SA(s) (s) ? "+" : ""
284   fprintf(out,"t_hackblock\n");
285   for(i=0; i<nres; i++) {
286     fprintf(out, "%3d %4s %2d %2d\n",
287             i, SS(hb[i].name), hb[i].nhack, hb[i].maxhack);
288     if (hb[i].nhack)
289       for(j=0; j<hb[i].nhack; j++) {
290         fprintf(out, "%d: %d %4s %4s %1s %2d %d %4s %4s %4s %4s\n", 
291                 j, hb[i].hack[j].nr, 
292                 SS(hb[i].hack[j].oname), SS(hb[i].hack[j].nname),
293                 SA(hb[i].hack[j].atom), hb[i].hack[j].tp, hb[i].hack[j].cgnr,
294                 SS(hb[i].hack[j].AI), SS(hb[i].hack[j].AJ),
295                 SS(hb[i].hack[j].AK), SS(hb[i].hack[j].AL) );
296       }
297     for(j=0; j<ebtsNR; j++)
298       if (hb[i].rb[j].nb) {
299         fprintf(out, " %c %d:", btsNames[j][0], hb[i].rb[j].nb);
300         for(k=0; k<hb[i].rb[j].nb; k++) {
301           fprintf(out, " [");
302           for(l=0; l<btsNiatoms[j]; l++)
303             fprintf(out, " %s",hb[i].rb[j].b[k].a[l]);
304           fprintf(out, " %s]",SS(hb[i].rb[j].b[k].s));
305         }
306         fprintf(out,"\n");
307       }
308     fprintf(out,"\n");
309   }
310 #undef SS
311 #undef SA
312 }
313
314 void init_t_protonate(t_protonate *protonate)
315 {
316   protonate->bInit=FALSE;
317 }