Partial commit of the project to remove all static variables.
[alexxy/gromacs.git] / src / contrib / anaf.c
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.
16  * 
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.
23  * 
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.
26  * 
27  * For more info, check our website at http://www.gromacs.org
28  * 
29  * And Hey:
30  * Great Red Owns Many ACres of Sand 
31  */
32
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 "gmxfio.h"
49 #include "tpxio.h"
50 #include "trnio.h"
51 #include "txtdump.h"
52 #include "vec.h"
53
54 static char *nm[5]  = { "OW", "HW1", "HW2", "DW", "SW" };
55   
56 static void list_trn(char *fn)
57 {
58   static real mass[5] = { 15.9994, 1.008, 1.008, 0.0, 0.0 };
59   int         i,j=0,m,fpread,fpwrite,nframe;
60   rvec        *x,*v,*f,fmol[2],xcm[2],torque[j],dx;
61   real        mmm,len;
62   matrix      box;
63   t_trnheader trn;
64   bool        bOK;
65
66   printf("Going to open %s\n",fn);
67   fpread  = open_trn(fn,"r"); 
68   fpwrite = open_tpx(NULL,"w");
69   fio_setdebug(fpwrite,TRUE);
70   
71   mmm=mass[0]+2*mass[1];
72   for(i=0; (i<5); i++) 
73     mass[i] /= mmm;
74   
75   nframe = 0;
76   while (fread_trnheader(fpread,&trn,&bOK)) {
77     snew(x,trn.natoms);
78     snew(v,trn.natoms);
79     snew(f,trn.natoms);
80     if (fread_htrn(fpread,&trn,
81                    trn.box_size ? box : NULL,
82                    trn.x_size   ? x : NULL,
83                    trn.v_size   ? v : NULL,
84                    trn.f_size   ? f : NULL)) {
85                    
86       if (trn.x_size && trn.f_size) {
87         printf("There are %d atoms\n",trn.natoms);
88         for(j=0; (j<2); j++) {
89           clear_rvec(xcm[j]);
90           clear_rvec(fmol[j]);
91           clear_rvec(torque[j]);
92           for(i=5*j; (i<5*j+5); i++) {
93             rvec_inc(fmol[j],f[i]);
94             for(m=0; (m<DIM); m++)
95               xcm[j][m] += mass[i%5]*x[i][m];
96           }
97           for(i=5*j; (i<5*j+5); i++) {
98             rvec_dec(x[i],xcm[j]);
99             oprod(x[i],f[i],dx);
100             rvec_inc(torque[j],dx);
101             rvec_inc(x[i],xcm[j]);
102           }
103         }
104         pr_rvecs(stdout,0,"FMOL  ",fmol,2);
105         pr_rvecs(stdout,0,"TORQUE",torque,2);
106         printf("Distance matrix Water1-Water2\n%5s","");
107         for(j=0; (j<5); j++) 
108           printf("  %10s",nm[j]);
109         printf("\n");
110         for(j=0; (j<5); j++) {
111           printf("%5s",nm[j]);
112           for(i=5; (i<10); i++) {
113             rvec_sub(x[i],x[j],dx);
114             len = sqrt(iprod(dx,dx));
115             printf("  %10.7f",len);
116           }
117           printf("\n");
118         }
119       }
120     }
121     sfree(x);
122     sfree(v);
123     sfree(f);
124     nframe++;
125   }
126   if (!bOK)
127     fprintf(stderr,"\nWARNING: Incomplete frame header: nr %d, t=%g\n",
128             nframe,trn.t);
129   close_tpx(fpwrite);
130   close_trn(fpread);
131 }
132
133 int main(int argc,char *argv[])
134 {
135   static char *desc[] = {
136     "gmxdump reads a run input file ([TT].tpa[tt]/[TT].tpr[tt]/[TT].tpb[tt]),",
137     "a trajectory ([TT].trj[tt]/[TT].trr[tt]/[TT].xtc[tt]) or an energy",
138     "file ([TT].ene[tt]/[TT].edr[tt]) and prints that to standard",
139     "output in a readable format. This program is essential for",
140     "checking your run input file in case of problems.[PAR]"
141   };
142   t_filenm fnm[] = {
143     { efTRN, "-f", NULL, ffOPTRD }
144   };
145 #define NFILE asize(fnm)
146   char *fn;
147   
148   /* Command line options */
149   
150   CopyRight(stdout,argv[0]);
151   parse_common_args(&argc,argv,0,NFILE,fnm,0,NULL,
152                     asize(desc),desc,0,NULL);
153   
154   if (ftp2bSet(efTRN,NFILE,fnm)) {
155     fn = ftp2fn(efTRN,NFILE,fnm);
156     printf("Going to open %s\n",fn);
157     list_trn(fn);
158   }
159   
160   thanx(stderr);
161
162   return 0;
163 }