changed reading hint
[gromacs/adressmacs.git] / src / kernel / nm2type.c
blob0f26ed386ca4efb9ab98ecb124245d62c5a4a1c5
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 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * GRowing Old MAkes el Chrono Sweat
29 static char *SRCID_nm2type_c = "$Id$";
31 #include "maths.h"
32 #include "macros.h"
33 #include "copyrite.h"
34 #include "bondf.h"
35 #include "string2.h"
36 #include "smalloc.h"
37 #include "sysstuff.h"
38 #include "confio.h"
39 #include "physics.h"
40 #include "statutil.h"
41 #include "vec.h"
42 #include "random.h"
43 #include "3dview.h"
44 #include "txtdump.h"
45 #include "readinp.h"
46 #include "names.h"
47 #include "toppush.h"
48 #include "pdb2top.h"
49 #include "topexcl.h"
50 #include "x2top.h"
52 t_nm2type *rd_nm2type(char *ff,int *nnm)
54 FILE *fp;
55 bool bCont;
56 char libfilename[128];
57 char format[128],f1[128];
58 char buf[1024],elem[16],type[16],nbbuf[16],**newbuf;
59 int i,n,nb,nnnm,line=1;
60 t_nm2type *nm2t=NULL;
62 sprintf(libfilename,"%s.n2t",ff);
63 fp = libopen(libfilename);
65 nnnm = 0;
66 do {
67 /* Read a line from the file */
68 bCont = (fgets2(buf,1023,fp) != NULL);
70 if (bCont) {
71 /* Remove comment */
72 strip_comment(buf);
73 if ((n = sscanf(buf,"%s%s%d",elem,type,&nb)) == 3) {
74 /* If we can read the first three, there probably is more */
75 if (nb > 0) {
76 snew(newbuf,nb);
77 strcpy(format,"%*s%*s%*d");
78 for(i=0; (i<nb); i++) {
79 /* Complicated format statement */
80 strcpy(f1,format);
81 strcat(f1,"%s");
82 if (sscanf(buf,f1,nbbuf) != 1)
83 fatal_error(0,"Error on line %d of %s",line,libfilename);
84 newbuf[i] = strdup(nbbuf);
85 strcat(format,"%*s");
88 else
89 newbuf = NULL;
90 srenew(nm2t,nnnm+1);
91 nm2t[nnnm].elem = strdup(elem);
92 nm2t[nnnm].type = strdup(type);
93 nm2t[nnnm].nbonds = nb;
94 nm2t[nnnm].bond = newbuf;
95 nnnm++;
97 line++;
99 } while(bCont);
100 fclose(fp);
102 *nnm = nnnm;
104 return nm2t;
107 void dump_nm2type(FILE *fp,int nnm,t_nm2type nm2t[])
109 int i,j;
111 fprintf(fp,"; nm2type database\n");
112 for(i=0; (i<nnm); i++) {
113 fprintf(fp,"%-8s%-8s%-4d",nm2t[i].elem,nm2t[i].type,nm2t[i].nbonds);
114 for(j=0; (j<nm2t[i].nbonds); j++)
115 fprintf(fp,"%-5s",nm2t[i].bond[j]);
116 fprintf(fp,"\n");
120 char *nm2type(int nnm,t_nm2type nm2t[],char *nm,int nbonds)
122 int i;
124 /* First check for names */
125 for(i=0; (i<nnm); i++) {
126 if ((strcasecmp(nm2t[i].elem,nm) == 0) &&
127 (nm2t[i].nbonds == nbonds))
128 return nm2t[i].type;
130 /* Then for element */
131 for(i=0; (i<nnm); i++) {
132 if ((nm2t[i].elem[0] == nm[0]) &&
133 (nm2t[i].nbonds == nbonds))
134 return nm2t[i].type;
137 return NULL;