Fixed a bug in the pdb-writing code.
[gromacs.git] / src / kernel / gmxdump.c
blob9b7360c067ce30002cf453e5ad75ce7804b6f87b
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.1
11 * Copyright (c) 1991-2001, University of Groningen, The Netherlands
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * If you want to redistribute modifications, please consider that
18 * scientific software is very special. Version control is crucial -
19 * bugs must be traceable. We will be happy to consider code for
20 * inclusion in the official distribution, but derived work must not
21 * be called official GROMACS. Details are found in the README & COPYING
22 * files - if they are missing, get the official version at www.gromacs.org.
24 * To help us fund GROMACS development, we humbly ask that you cite
25 * the papers on the package - you can find them in the top README file.
27 * For more info, check our website at http://www.gromacs.org
29 * And Hey:
30 * GROningen Mixture of Alchemy and Childrens' Stories
32 static char *SRCID_gmxdump_c = "$Id$";
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36 #include "main.h"
37 #include "macros.h"
38 #include "futil.h"
39 #include "statutil.h"
40 #include "copyrite.h"
41 #include "sysstuff.h"
42 #include "txtdump.h"
43 #include "fatal.h"
44 #include "xtcio.h"
45 #include "enxio.h"
46 #include "assert.h"
47 #include "smalloc.h"
48 #include "names.h"
49 #include "gmxfio.h"
50 #include "tpxio.h"
51 #include "trnio.h"
52 #include "txtdump.h"
54 static void list_tpx(char *fn)
56 int step,natoms,fp,indent,i,j,**gcount,atot;
57 real t,lambda;
58 rvec *x,*v,*f;
59 matrix box;
60 t_inputrec ir;
61 t_tpxheader tpx;
62 t_topology top;
64 read_tpxheader(fn,&tpx);
65 snew(x,tpx.natoms);
66 snew(v,tpx.natoms);
67 snew(f,tpx.natoms);
68 read_tpx(fn,&step,&t,&lambda,
69 tpx.bIr ? &ir : NULL,
70 tpx.bBox ? box : NULL,
71 &natoms,
72 tpx.bX ? x : NULL,
73 tpx.bV ? v : NULL,
74 tpx.bF ? f : NULL,
75 tpx.bTop ? &top: NULL);
77 if (available(stdout,&tpx,fn)) {
78 indent=0;
79 indent=pr_title(stdout,indent,fn);
80 pr_header(stdout,indent,"header",&(tpx));
81 pr_inputrec(stdout,indent,"ir",&(ir));
82 pr_rvecs(stdout,indent,"box",box,DIM);
83 pr_rvecs(stdout,indent,"x",x,natoms);
84 pr_rvecs(stdout,indent,"v",v,natoms);
85 pr_rvecs(stdout,indent,"f",f,natoms);
86 pr_top(stdout,indent,"topology",&(top));
89 snew(gcount,egcNR);
90 for(i=0; (i<egcNR); i++)
91 snew(gcount[i],top.atoms.grps[i].nr);
93 for(i=0; (i<top.atoms.nr); i++) {
94 for(j=0; (j<egcNR); j++)
95 gcount[j][top.atoms.atom[i].grpnr[j]]++;
97 printf("Group statistics\n");
98 for(i=0; (i<egcNR); i++) {
99 atot=0;
100 printf("%-12s: ",gtypes[i]);
101 for(j=0; (j<top.atoms.grps[i].nr); j++) {
102 printf(" %5d",gcount[i][j]);
103 atot+=gcount[i][j];
105 printf(" (total %d atoms)\n",atot);
106 sfree(gcount[i]);
108 sfree(gcount);
110 sfree(x);
111 sfree(v);
112 sfree(f);
115 static void list_trn(char *fn)
117 int fpread,fpwrite,nframe,indent;
118 char buf[256];
119 rvec *x,*v,*f;
120 matrix box;
121 t_trnheader trn;
122 bool bOK;
124 fpread = open_trn(fn,"r");
125 fpwrite = open_tpx(NULL,"w");
126 fio_setdebug(fpwrite,TRUE);
128 nframe = 0;
129 while (fread_trnheader(fpread,&trn,&bOK)) {
130 snew(x,trn.natoms);
131 snew(v,trn.natoms);
132 snew(f,trn.natoms);
133 if (fread_htrn(fpread,&trn,
134 trn.box_size ? box : NULL,
135 trn.x_size ? x : NULL,
136 trn.v_size ? v : NULL,
137 trn.f_size ? f : NULL)) {
138 sprintf(buf,"%s frame %d",fn,nframe);
139 indent=0;
140 indent=pr_title(stdout,indent,buf);
141 pr_indent(stdout,indent);
142 fprintf(stdout,"natoms=%10d step=%10d time=%10g lambda=%10g\n",
143 trn.natoms,trn.step,trn.t,trn.lambda);
144 if (trn.box_size)
145 pr_rvecs(stdout,indent,"box",box,DIM);
146 if (trn.x_size)
147 pr_rvecs(stdout,indent,"x",x,trn.natoms);
148 if (trn.v_size)
149 pr_rvecs(stdout,indent,"v",v,trn.natoms);
150 if (trn.f_size)
151 pr_rvecs(stdout,indent,"f",f,trn.natoms);
153 else
154 fprintf(stderr,"\nWARNING: Incomplete frame: nr %d, t=%g\n",
155 nframe,trn.t);
157 sfree(x);
158 sfree(v);
159 sfree(f);
160 nframe++;
162 if (!bOK)
163 fprintf(stderr,"\nWARNING: Incomplete frame header: nr %d, t=%g\n",
164 nframe,trn.t);
165 close_tpx(fpwrite);
166 close_trn(fpread);
169 void list_xtc(char *fn, bool bXVG)
171 int xd,indent;
172 char buf[256];
173 rvec *x;
174 matrix box;
175 int nframe,natoms,step;
176 real prec,time;
177 bool bOK;
179 xd = open_xtc(fn,"r");
180 read_first_xtc(xd,&natoms,&step,&time,box,&x,&prec,&bOK);
182 nframe=0;
183 do {
184 if (bXVG) {
185 int i,d;
187 fprintf(stdout,"%g",time);
188 for(i=0; i<natoms; i++)
189 for(d=0; d<DIM; d++)
190 fprintf(stdout," %g",x[i][d]);
191 fprintf(stdout,"\n");
192 } else {
193 sprintf(buf,"%s frame %d",fn,nframe);
194 indent=0;
195 indent=pr_title(stdout,indent,buf);
196 pr_indent(stdout,indent);
197 fprintf(stdout,"natoms=%10d step=%10d time=%10g prec=%10g\n",
198 natoms,step,time,prec);
199 pr_rvecs(stdout,indent,"box",box,DIM);
200 pr_rvecs(stdout,indent,"x",x,natoms);
202 nframe++;
203 } while (read_next_xtc(xd,natoms,&step,&time,box,x,&prec,&bOK));
204 if (!bOK)
205 fprintf(stderr,"\nWARNING: Incomplete frame at time %g\n",time);
206 close_xtc(xd);
209 void list_trx(char *fn,bool bXVG)
211 int ftp;
213 ftp = fn2ftp(fn);
214 if (ftp == efXTC)
215 list_xtc(fn,bXVG);
216 else if ((ftp == efTRR) || (ftp == efTRJ))
217 list_trn(fn);
218 else
219 fprintf(stderr,"File %s not supported. Try using more %s\n",
220 fn,fn);
223 void list_ene(char *fn)
225 int in,ndr;
226 bool bCont;
227 t_enxframe *fr;
228 int i,nre,b;
229 real rav,minthird;
230 char **enm=NULL;
232 printf("gmxdump: %s\n",fn);
233 in = open_enx(fn,"r");
234 do_enxnms(in,&nre,&enm);
236 printf("energy components:\n");
237 for(i=0; (i<nre); i++)
238 printf("%5d %s\n",i,enm[i]);
240 minthird=-1.0/3.0;
241 snew(fr,1);
242 do {
243 bCont=do_enx(in,fr);
244 assert(fr->nre==nre);
246 if (bCont) {
247 printf("\n%24s %12.5e %12s %12d\n","time:",
248 fr->t,"step:",fr->step);
249 printf("%24s %12s %12s %12s\n",
250 "Component","Energy","Av. Energy","Sum Energy");
251 for(i=0; (i<nre); i++)
252 printf("%24s %12.5e %12.5e %12.5e\n",
253 enm[i],fr->ener[i].e,fr->ener[i].eav,fr->ener[i].esum);
254 if (fr->ndisre > 0) {
255 printf("Distance restraint %8s %8s\n","r(t)","< r >");
256 for(i=0; i<fr->ndisre; i++) {
257 rav=pow(fr->rav[i],minthird);
258 printf("%17d %8.4f %8.4f\n",i,fr->rt[i],rav);
261 for(b=0; b<fr->nblock; b++)
262 if (fr->nr[b] > 0) {
263 printf("Block data %2d (%4d elm.) %8s\n",b,fr->nr[b],"value");
264 for(i=0; i<fr->nr[b]; i++)
265 printf("%24d %8.4f\n",i,fr->block[b][i]);
268 } while (bCont);
270 close_enx(in);
272 free_enxframe(fr);
273 sfree(fr);
274 sfree(enm);
277 int main(int argc,char *argv[])
279 static char *desc[] = {
280 "gmxdump reads a run input file ([TT].tpa[tt]/[TT].tpr[tt]/[TT].tpb[tt]),",
281 "a trajectory ([TT].trj[tt]/[TT].trr[tt]/[TT].xtc[tt]) or an energy",
282 "file ([TT].ene[tt]/[TT].edr[tt]) and prints that to standard",
283 "output in a readable format. This program is essential for",
284 "checking your run input file in case of problems.[PAR]"
286 t_filenm fnm[] = {
287 { efTPX, "-s", NULL, ffOPTRD },
288 { efTRX, "-f", NULL, ffOPTRD },
289 { efENX, "-e", NULL, ffOPTRD },
291 #define NFILE asize(fnm)
293 /* Command line options */
294 static bool bXVG=FALSE;
295 static bool bShowNumbers=TRUE;
296 t_pargs pa[] = {
297 { "-xvg", FALSE, etBOOL, {&bXVG}, "HIDDENXVG layout for xtc" },
298 { "-nr",FALSE, etBOOL, {&bShowNumbers},"Show index numbers in output (leaving them out makes comparison easier, but creates a useless topology)" }
301 CopyRight(stderr,argv[0]);
302 parse_common_args(&argc,argv,0,NFILE,fnm,asize(pa),pa,
303 asize(desc),desc,0,NULL);
305 pr_shownumbers(bShowNumbers);
306 if (ftp2bSet(efTPX,NFILE,fnm))
307 list_tpx(ftp2fn(efTPX,NFILE,fnm));
309 if (ftp2bSet(efTRX,NFILE,fnm))
310 list_trx(ftp2fn(efTRX,NFILE,fnm),bXVG);
312 if (ftp2bSet(efENX,NFILE,fnm))
313 list_ene(ftp2fn(efENX,NFILE,fnm));
315 thanx(stderr);
317 return 0;