Merge branch 'master' of git@git.gromacs.org:gromacs
[gromacs/rigid-bodies.git] / src / kernel / nm2type.c
blob1d07db41d9b832f9331346d444b4a10b96079f63
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.3.3
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-2008, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
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.
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.
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.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Groningen Machine for Chemical Simulation
35 /* This file is completely threadsafe - keep it that way! */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
40 #include "assert.h"
41 #include "maths.h"
42 #include "macros.h"
43 #include "copyrite.h"
44 #include "bondf.h"
45 #include "string2.h"
46 #include "smalloc.h"
47 #include "sysstuff.h"
48 #include "confio.h"
49 #include "physics.h"
50 #include "statutil.h"
51 #include "vec.h"
52 #include "random.h"
53 #include "3dview.h"
54 #include "txtdump.h"
55 #include "readinp.h"
56 #include "names.h"
57 #include "toppush.h"
58 #include "pdb2top.h"
59 #include "gpp_nextnb.h"
60 #include "gpp_atomtype.h"
61 #include "x2top.h"
62 #include "fflibutil.h"
64 static void rd_nm2type_file(const char *fn,int *nnm,t_nm2type **nmp)
66 FILE *fp;
67 bool bCont;
68 char libfilename[128];
69 char format[128],f1[128];
70 char buf[1024],elem[16],type[16],nbbuf[16],**newbuf;
71 int i,nb,nnnm,line=1;
72 double qq,mm,*blen;
73 t_nm2type *nm2t=NULL;
75 fp = fflib_open(fn);
77 nnnm = *nnm;
78 nm2t = *nmp;
79 do {
80 /* Read a line from the file */
81 bCont = (fgets2(buf,1023,fp) != NULL);
83 if (bCont) {
84 /* Remove comment */
85 strip_comment(buf);
86 if (sscanf(buf,"%s%s%lf%lf%d",elem,type,&qq,&mm,&nb) == 5) {
87 /* If we can read the first four, there probably is more */
88 srenew(nm2t,nnnm+1);
89 snew(nm2t[nnnm].blen,nb);
90 if (nb > 0) {
91 snew(newbuf,nb);
92 strcpy(format,"%*s%*s%*s%*s%*s");
93 for(i=0; (i<nb); i++) {
94 /* Complicated format statement */
95 strcpy(f1,format);
96 strcat(f1,"%s%lf");
97 if (sscanf(buf,f1,nbbuf,&(nm2t[nnnm].blen[i])) != 2)
98 gmx_fatal(FARGS,"Error on line %d of %s",line,libfilename);
99 newbuf[i] = strdup(nbbuf);
100 strcat(format,"%*s%*s");
103 else
104 newbuf = NULL;
105 nm2t[nnnm].elem = strdup(elem);
106 nm2t[nnnm].type = strdup(type);
107 nm2t[nnnm].q = qq;
108 nm2t[nnnm].m = mm;
109 nm2t[nnnm].nbonds = nb;
110 nm2t[nnnm].bond = newbuf;
111 nnnm++;
113 line++;
115 } while(bCont);
116 ffclose(fp);
118 *nnm = nnnm;
119 *nmp = nm2t;
122 t_nm2type *rd_nm2type(const char *ffdir,int *nnm)
124 int nff,f;
125 char **ff;
126 t_nm2type *nm;
128 nff = fflib_search_file_end(ffdir,".n2t",FALSE,&ff);
129 *nnm = 0;
130 nm = NULL;
131 for(f=0; f<nff; f++) {
132 rd_nm2type_file(ff[f],nnm,&nm);
133 sfree(ff[f]);
135 sfree(ff);
137 return nm;
140 void dump_nm2type(FILE *fp,int nnm,t_nm2type nm2t[])
142 int i,j;
144 fprintf(fp,"; nm2type database\n");
145 for(i=0; (i<nnm); i++) {
146 fprintf(fp,"%-8s %-8s %8.4f %8.4f %-4d",
147 nm2t[i].elem,nm2t[i].type,
148 nm2t[i].q,nm2t[i].m,nm2t[i].nbonds);
149 for(j=0; (j<nm2t[i].nbonds); j++)
150 fprintf(fp," %-5s %6.4f",nm2t[i].bond[j],nm2t[i].blen[j]);
151 fprintf(fp,"\n");
155 enum { ematchNone, ematchWild, ematchElem, ematchExact, ematchNR };
157 static int match_str(const char *atom,const char *template_string)
159 if (!atom || !template_string)
160 return ematchNone;
161 else if (strcasecmp(atom,template_string) == 0)
162 return ematchExact;
163 else if (atom[0] == template_string[0])
164 return ematchElem;
165 else if (strcmp(template_string,"*") == 0)
166 return ematchWild;
167 else
168 return ematchNone;
171 int nm2type(int nnm,t_nm2type nm2t[],t_symtab *tab,t_atoms *atoms,
172 gpp_atomtype_t atype,int *nbonds,t_params *bonds)
174 int cur = 0;
175 #define prev (1-cur)
176 int i,j,k,m,n,nresolved,nb,maxbond,ai,aj,best,im,nqual[2][ematchNR];
177 int *bbb,*n_mask,*m_mask,**match,**quality;
178 char *aname_i,*aname_m,*aname_n,*type;
179 double qq,mm;
180 t_param *param;
182 snew(param,1);
183 maxbond = 0;
184 for(i=0; (i<atoms->nr); i++)
185 maxbond = max(maxbond,nbonds[i]);
186 if (debug)
187 fprintf(debug,"Max number of bonds per atom is %d\n",maxbond);
188 snew(bbb,maxbond);
189 snew(n_mask,maxbond);
190 snew(m_mask,maxbond);
191 snew(match,maxbond);
192 for(i=0; (i<maxbond); i++)
193 snew(match[i],maxbond);
195 nresolved = 0;
196 for(i=0; (i<atoms->nr); i++) {
197 aname_i = *atoms->atomname[i];
198 nb = 0;
199 for(j=0; (j<bonds->nr); j++) {
200 ai = bonds->param[j].AI;
201 aj = bonds->param[j].AJ;
202 if (ai == i)
203 bbb[nb++] = aj;
204 else if (aj == i)
205 bbb[nb++] = ai;
207 if (nb != nbonds[i])
208 gmx_fatal(FARGS,"Counting number of bonds nb = %d, nbonds[%d] = %d",
209 nb,i,nbonds[i]);
210 if(debug) {
211 fprintf(debug,"%4s has bonds to",aname_i);
212 for(j=0; (j<nb); j++)
213 fprintf(debug," %4s",*atoms->atomname[bbb[j]]);
214 fprintf(debug,"\n");
216 best = -1;
217 for(k=0; (k<ematchNR); k++)
218 nqual[prev][k] = 0;
220 /* First check for names */
221 for(k=0; (k<nnm); k++) {
222 if (nm2t[k].nbonds == nb) {
223 im = match_str(*atoms->atomname[i],nm2t[k].elem);
224 if (im > ematchWild) {
225 for(j=0; (j<ematchNR); j++)
226 nqual[cur][j] = 0;
228 /* Fill a matrix with matching quality */
229 for(m=0; (m<nb); m++) {
230 aname_m = *atoms->atomname[bbb[m]];
231 for(n=0; (n<nb); n++) {
232 aname_n = nm2t[k].bond[n];
233 match[m][n] = match_str(aname_m,aname_n);
236 /* Now pick the best matches */
237 for(m=0; (m<nb); m++) {
238 n_mask[m] = 0;
239 m_mask[m] = 0;
241 for(j=ematchNR-1; (j>0); j--) {
242 for(m=0; (m<nb); m++) {
243 for(n=0; (n<nb); n++) {
244 if ((n_mask[n] == 0) &&
245 (m_mask[m] == 0) &&
246 (match[m][n] == j)) {
247 n_mask[n] = 1;
248 m_mask[m] = 1;
249 nqual[cur][j]++;
254 if ((nqual[cur][ematchExact]+
255 nqual[cur][ematchElem]+
256 nqual[cur][ematchWild]) == nb) {
257 if ((nqual[cur][ematchExact] > nqual[prev][ematchExact]) ||
259 ((nqual[cur][ematchExact] == nqual[prev][ematchExact]) &&
260 (nqual[cur][ematchElem] > nqual[prev][ematchElem])) ||
262 ((nqual[cur][ematchExact] == nqual[prev][ematchExact]) &&
263 (nqual[cur][ematchElem] == nqual[prev][ematchElem]) &&
264 (nqual[cur][ematchWild] > nqual[prev][ematchWild]))) {
265 best = k;
266 cur = prev;
272 if (best != -1) {
273 int atomnr=0;
274 real alpha=0;
276 qq = nm2t[best].q;
277 mm = nm2t[best].m;
278 type = nm2t[best].type;
280 if ((k = get_atomtype_type(type,atype)) == NOTSET) {
281 atoms->atom[i].qB = alpha;
282 atoms->atom[i].m = atoms->atom[i].mB = mm;
283 k = add_atomtype(atype,tab,&(atoms->atom[i]),type,param,
284 atoms->atom[i].type,0,0,0,atomnr,0,0);
286 atoms->atom[i].type = k;
287 atoms->atom[i].typeB = k;
288 atoms->atom[i].q = qq;
289 atoms->atom[i].qB = qq;
290 atoms->atom[i].m = mm;
291 atoms->atom[i].mB = mm;
292 nresolved++;
294 else {
295 fprintf(stderr,"Can not find forcefield for atom %s-%d with %d bonds\n",
296 *atoms->atomname[i],i+1,nb);
299 sfree(bbb);
300 sfree(n_mask);
301 sfree(m_mask);
302 sfree(param);
304 return nresolved;