Merge branch release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / 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 #include "macros.h"
46
47 /* these MUST correspond to the enum in hackblock.h */
48 const char *btsNames[ebtsNR] = { "bonds", "angles", "dihedrals", "impropers", "exclusions", "cmap" };
49 const int   btsNiatoms[ebtsNR] = { 2,       3,        4,           4,           2,             5 };
50
51 static void free_t_bonded(t_rbonded *rb)
52 {
53     int i;
54
55     for (i = 0; i < MAXATOMLIST; i++)
56     {
57         sfree(rb->a[i]);
58     }
59     sfree(rb->s);
60 }
61
62 static void free_t_bondeds(t_rbondeds *rbs)
63 {
64     int i;
65
66     for (i = 0; i < rbs->nb; i++)
67     {
68         free_t_bonded(&rbs->b[i]);
69     }
70     sfree(rbs->b);
71     rbs->b  = NULL;
72     rbs->nb = 0;
73 }
74
75 void free_t_restp(int nrtp, t_restp **rtp)
76 {
77     int i, j;
78
79     for (i = 0; i < nrtp; i++)
80     {
81         sfree((*rtp)[i].resname);
82         sfree((*rtp)[i].atom);
83         for (j = 0; j < (*rtp)[i].natom; j++)
84         {
85             sfree(*(*rtp)[i].atomname[j]);
86             sfree((*rtp)[i].atomname[j]);
87         }
88         sfree((*rtp)[i].atomname);
89         sfree((*rtp)[i].cgnr);
90         for (j = 0; j < ebtsNR; j++)
91         {
92             free_t_bondeds(&(*rtp)[i].rb[j]);
93         }
94     }
95     free(*rtp);
96 }
97
98 void free_t_hack(int nh, t_hack **h)
99 {
100     int i, j;
101
102     for (i = 0; i < nh; i++)
103     {
104         sfree((*h)[i].oname);
105         sfree((*h)[i].nname);
106         sfree((*h)[i].atom);
107         for (j = 0; j < 4; j++)
108         {
109             sfree((*h)[i].a[j]);
110         }
111     }
112     sfree(*h);
113     *h = NULL;
114 }
115
116 void free_t_hackblock(int nhb, t_hackblock **hb)
117 {
118     int i, j;
119
120     for (i = 0; i < nhb; i++)
121     {
122         sfree((*hb)[i].name);
123         free_t_hack((*hb)[i].nhack, &(*hb)[i].hack);
124         for (j = 0; j < ebtsNR; j++)
125         {
126             free_t_bondeds(&(*hb)[i].rb[j]);
127         }
128     }
129     sfree(*hb);
130 }
131
132 void clear_t_hackblock(t_hackblock *hb)
133 {
134     int i;
135
136     hb->name    = NULL;
137     hb->nhack   = 0;
138     hb->maxhack = 0;
139     hb->hack    = NULL;
140     for (i = 0; i < ebtsNR; i++)
141     {
142         hb->rb[i].nb = 0;
143         hb->rb[i].b  = NULL;
144     }
145 }
146
147 void clear_t_hack(t_hack *hack)
148 {
149     int i;
150
151     hack->nr    = 0;
152     hack->oname = NULL;
153     hack->nname = NULL;
154     hack->atom  = NULL;
155     hack->cgnr  = NOTSET;
156     hack->tp    = 0;
157     hack->nctl  = 0;
158     for (i = 0; i < 4; i++)
159     {
160         hack->a[i]  = NULL;
161     }
162     for (i = 0; i < DIM; i++)
163     {
164         hack->newx[i] = NOTSET;
165     }
166 }
167
168 #define safe_strdup(str) ((str != NULL) ? strdup(str) : NULL)
169
170 static void copy_t_rbonded(t_rbonded *s, t_rbonded *d)
171 {
172     int i;
173
174     for (i = 0; i < MAXATOMLIST; i++)
175     {
176         d->a[i] = safe_strdup(s->a[i]);
177     }
178     d->s = safe_strdup(s->s);
179 }
180
181 static gmx_bool contains_char(t_rbonded *s, char c)
182 {
183     int      i;
184     gmx_bool bRet;
185
186     bRet = FALSE;
187     for (i = 0; i < MAXATOMLIST; i++)
188     {
189         if (s->a[i] && s->a[i][0] == c)
190         {
191             bRet = TRUE;
192         }
193     }
194
195     return bRet;
196 }
197
198 gmx_bool merge_t_bondeds(t_rbondeds s[], t_rbondeds d[], gmx_bool bMin, gmx_bool bPlus)
199 {
200     int      i, j;
201     gmx_bool bBondsRemoved;
202
203     bBondsRemoved = FALSE;
204     for (i = 0; i < ebtsNR; i++)
205     {
206         if (s[i].nb > 0)
207         {
208             /* make space */
209             srenew(d[i].b, d[i].nb + s[i].nb);
210             for (j = 0; j < s[i].nb; j++)
211             {
212                 if (!(bMin && contains_char(&s[i].b[j], '-'))
213                     && !(bPlus && contains_char(&s[i].b[j], '+')))
214                 {
215                     copy_t_rbonded(&s[i].b[j], &d[i].b[ d[i].nb ]);
216                     d[i].nb++;
217                 }
218                 else if (i == ebtsBONDS)
219                 {
220                     bBondsRemoved = TRUE;
221                 }
222             }
223         }
224     }
225
226     return bBondsRemoved;
227 }
228
229 void copy_t_restp(t_restp *s, t_restp *d)
230 {
231     int i;
232
233     *d         = *s;
234     d->resname = safe_strdup(s->resname);
235     snew(d->atom, s->natom);
236     for (i = 0; i < s->natom; i++)
237     {
238         d->atom[i] = s->atom[i];
239     }
240     snew(d->atomname, s->natom);
241     for (i = 0; i < s->natom; i++)
242     {
243         snew(d->atomname[i], 1);
244         *d->atomname[i] = safe_strdup(*s->atomname[i]);
245     }
246     snew(d->cgnr, s->natom);
247     for (i = 0; i < s->natom; i++)
248     {
249         d->cgnr[i] = s->cgnr[i];
250     }
251     for (i = 0; i < ebtsNR; i++)
252     {
253         d->rb[i].type = s->rb[i].type;
254         d->rb[i].nb   = 0;
255         d->rb[i].b    = NULL;
256     }
257     merge_t_bondeds(s->rb, d->rb, FALSE, FALSE);
258 }
259
260 void copy_t_hack(t_hack *s, t_hack *d)
261 {
262     int i;
263
264     *d       = *s;
265     d->oname = safe_strdup(s->oname);
266     d->nname = safe_strdup(s->nname);
267     if (s->atom)
268     {
269         snew(d->atom, 1);
270         *(d->atom) = *(s->atom);
271     }
272     else
273     {
274         d->atom = NULL;
275     }
276     for (i = 0; i < 4; i++)
277     {
278         d->a[i] = safe_strdup(s->a[i]);
279     }
280     copy_rvec(s->newx, d->newx);
281 }
282
283 void merge_hacks_lo(int ns, t_hack *s, int *nd, t_hack **d)
284 {
285     int i;
286
287     if (ns)
288     {
289         srenew(*d, *nd + ns);
290         for (i = 0; i < ns; i++)
291         {
292             copy_t_hack(&s[i], &(*d)[*nd + i]);
293         }
294         (*nd) += ns;
295     }
296 }
297
298 void merge_hacks(t_hackblock *s, t_hackblock *d)
299 {
300     merge_hacks_lo(s->nhack, s->hack, &d->nhack, &d->hack);
301 }
302
303 void merge_t_hackblock(t_hackblock *s, t_hackblock *d)
304 {
305     merge_hacks(s, d);
306     merge_t_bondeds(s->rb, d->rb, FALSE, FALSE);
307 }
308
309 void copy_t_hackblock(t_hackblock *s, t_hackblock *d)
310 {
311     int i;
312
313     *d       = *s;
314     d->name  = safe_strdup(s->name);
315     d->nhack = 0;
316     d->hack  = NULL;
317     for (i = 0; i < ebtsNR; i++)
318     {
319         d->rb[i].nb = 0;
320         d->rb[i].b  = NULL;
321     }
322     merge_t_hackblock(s, d);
323 }
324
325 #undef safe_strdup
326
327 void dump_hb(FILE *out, int nres, t_hackblock hb[])
328 {
329     int i, j, k, l;
330
331 #define SS(s) (s) ? (s) : "-"
332 #define SA(s) (s) ? "+" : ""
333     fprintf(out, "t_hackblock\n");
334     for (i = 0; i < nres; i++)
335     {
336         fprintf(out, "%3d %4s %2d %2d\n",
337                 i, SS(hb[i].name), hb[i].nhack, hb[i].maxhack);
338         if (hb[i].nhack)
339         {
340             for (j = 0; j < hb[i].nhack; j++)
341             {
342                 fprintf(out, "%d: %d %4s %4s %1s %2d %d %4s %4s %4s %4s\n",
343                         j, hb[i].hack[j].nr,
344                         SS(hb[i].hack[j].oname), SS(hb[i].hack[j].nname),
345                         SA(hb[i].hack[j].atom), hb[i].hack[j].tp, hb[i].hack[j].cgnr,
346                         SS(hb[i].hack[j].AI), SS(hb[i].hack[j].AJ),
347                         SS(hb[i].hack[j].AK), SS(hb[i].hack[j].AL) );
348             }
349         }
350         for (j = 0; j < ebtsNR; j++)
351         {
352             if (hb[i].rb[j].nb)
353             {
354                 fprintf(out, " %c %d:", btsNames[j][0], hb[i].rb[j].nb);
355                 for (k = 0; k < hb[i].rb[j].nb; k++)
356                 {
357                     fprintf(out, " [");
358                     for (l = 0; l < btsNiatoms[j]; l++)
359                     {
360                         fprintf(out, " %s", hb[i].rb[j].b[k].a[l]);
361                     }
362                     fprintf(out, " %s]", SS(hb[i].rb[j].b[k].s));
363                 }
364                 fprintf(out, "\n");
365             }
366         }
367         fprintf(out, "\n");
368     }
369 #undef SS
370 #undef SA
371 }
372
373 void init_t_protonate(t_protonate *protonate)
374 {
375     protonate->bInit = FALSE;
376 }