Added files to contrib directory.
[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.3.99_development_20071104
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2006, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * Groningen Machine for Chemical Simulation
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <math.h>
43 #include "main.h"
44 #include "macros.h"
45 #include "futil.h"
46 #include "statutil.h"
47 #include "copyrite.h"
48 #include "sysstuff.h"
49 #include "txtdump.h"
50 #include "gmx_fatal.h"
51 #include "xtcio.h"
52 #include "enxio.h"
53 #include "smalloc.h"
54 #include "gmxfio.h"
55 #include "tpxio.h"
56 #include "trnio.h"
57 #include "txtdump.h"
58 #include "vec.h"
59
60 static char *nm[5]  = { "OW", "HW1", "HW2", "DW", "SW" };
61   
62 static void list_trn(char *fn)
63 {
64   static real mass[5] = { 15.9994, 1.008, 1.008, 0.0, 0.0 };
65   int         i,j=0,m,fpread,fpwrite,nframe;
66   rvec        *x,*v,*f,fmol[2],xcm[2],torque[j],dx;
67   real        mmm,len;
68   matrix      box;
69   t_trnheader trn;
70   bool        bOK;
71
72   printf("Going to open %s\n",fn);
73   fpread  = open_trn(fn,"r"); 
74   fpwrite = open_tpx(NULL,"w");
75   gmx_fio_setdebug(fpwrite,TRUE);
76   
77   mmm=mass[0]+2*mass[1];
78   for(i=0; (i<5); i++) 
79     mass[i] /= mmm;
80   
81   nframe = 0;
82   while (fread_trnheader(fpread,&trn,&bOK)) {
83     snew(x,trn.natoms);
84     snew(v,trn.natoms);
85     snew(f,trn.natoms);
86     if (fread_htrn(fpread,&trn,
87                    trn.box_size ? box : NULL,
88                    trn.x_size   ? x : NULL,
89                    trn.v_size   ? v : NULL,
90                    trn.f_size   ? f : NULL)) {
91                    
92       if (trn.x_size && trn.f_size) {
93         printf("There are %d atoms\n",trn.natoms);
94         for(j=0; (j<2); j++) {
95           clear_rvec(xcm[j]);
96           clear_rvec(fmol[j]);
97           clear_rvec(torque[j]);
98           for(i=5*j; (i<5*j+5); i++) {
99             rvec_inc(fmol[j],f[i]);
100             for(m=0; (m<DIM); m++)
101               xcm[j][m] += mass[i%5]*x[i][m];
102           }
103           for(i=5*j; (i<5*j+5); i++) {
104             rvec_dec(x[i],xcm[j]);
105             cprod(x[i],f[i],dx);
106             rvec_inc(torque[j],dx);
107             rvec_inc(x[i],xcm[j]);
108           }
109         }
110         pr_rvecs(stdout,0,"FMOL  ",fmol,2);
111         pr_rvecs(stdout,0,"TORQUE",torque,2);
112         printf("Distance matrix Water1-Water2\n%5s","");
113         for(j=0; (j<5); j++) 
114           printf("  %10s",nm[j]);
115         printf("\n");
116         for(j=0; (j<5); j++) {
117           printf("%5s",nm[j]);
118           for(i=5; (i<10); i++) {
119             rvec_sub(x[i],x[j],dx);
120             len = sqrt(iprod(dx,dx));
121             printf("  %10.7f",len);
122           }
123           printf("\n");
124         }
125       }
126     }
127     sfree(x);
128     sfree(v);
129     sfree(f);
130     nframe++;
131   }
132   if (!bOK)
133     fprintf(stderr,"\nWARNING: Incomplete frame header: nr %d, t=%g\n",
134             nframe,trn.t);
135   close_tpx(fpwrite);
136   close_trn(fpread);
137 }
138
139 int main(int argc,char *argv[])
140 {
141   static char *desc[] = {
142     "gmxdump reads a run input file ([TT].tpa[tt]/[TT].tpr[tt]/[TT].tpb[tt]),",
143     "a trajectory ([TT].trj[tt]/[TT].trr[tt]/[TT].xtc[tt]) or an energy",
144     "file ([TT].ene[tt]/[TT].edr[tt]) and prints that to standard",
145     "output in a readable format. This program is essential for",
146     "checking your run input file in case of problems.[PAR]"
147   };
148   t_filenm fnm[] = {
149     { efTRN, "-f", NULL, ffOPTRD }
150   };
151 #define NFILE asize(fnm)
152   char *fn;
153   
154   /* Command line options */
155   
156   CopyRight(stdout,argv[0]);
157   parse_common_args(&argc,argv,0,NFILE,fnm,0,NULL,
158                     asize(desc),desc,0,NULL);
159   
160   if (ftp2bSet(efTRN,NFILE,fnm)) {
161     fn = ftp2fn(efTRN,NFILE,fnm);
162     printf("Going to open %s\n",fn);
163     list_trn(fn);
164   }
165   
166   thanx(stderr);
167
168   return 0;
169 }