Version bumps after new release
[alexxy/gromacs.git] / src / contrib / do_shift.c
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.99_development_20071104
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-2006, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
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.
19  * 
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.
26  * 
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.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * Groningen Machine for Chemical Simulation
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <stdlib.h>
40 #include "errno.h"
41 #include "sysstuff.h"
42 #include "typedefs.h"
43 #include "gromacs/utility/cstringutil.h"
44 #include "macros.h"
45 #include "gromacs/utility/smalloc.h"
46 #include "mshift.h"
47 #include "gromacs/commandline/pargs.h"
48 #include "copyrite.h"
49 #include "gromacs/fileio/confio.h"
50 #include "gmx_fatal.h"
51 #include "xvgr.h"
52 #include "gstat.h"
53 #include "index.h"
54 #include "gromacs/fileio/pdbio.h"
55
56 void cat(FILE *out,char *fn,real t)
57 {
58   FILE *in;
59   char *ptr,buf[256];
60   int    anr,rnr;
61   char   anm[24],rnm[24];
62   double f1,f2,f3,f4,f5,f6;
63    
64   in=gmx_ffopen(fn,"r");
65   while ((ptr=fgets2(buf,255,in)) != NULL) {
66     sscanf(buf,"%d%d%s%s%lf%lf%lf%lf%lf%lf",
67            &anr,&rnr,rnm,anm,&f1,&f2,&f3,&f4,&f5,&f6);
68     fprintf(out,"%8g  %10g  %10g  %10g  %10g  %10g  %10g  %s%d-%s%d\n",
69             t,f6,f1,f2,f3,f4,f5,rnm,rnr,anm,anr);
70   }
71   /*if ((int)strlen(buf) > 0) 
72     fprintf(out,"%s\n",buf);*/
73   fflush(out);
74   gmx_ffclose(in);
75 }
76
77 int main(int argc,char *argv[])
78 {
79   static char *desc[] = {
80     "[TT]do_shift[tt] reads a trajectory file and computes the chemical",
81     "shift for each time frame (or every [BB]dt[bb] ps) by",
82     "calling the 'total' program. If you do not have the total program,",
83     "get it. do_shift assumes that the total executable is in",
84     "[TT]/home/mdgroup/total/total[tt]. If that is not the case, then you should",
85     "set an environment variable [BB]GMX_TOTAL[bb] as in: [PAR]",
86     "[TT]setenv GMX_TOTAL /usr/local/bin/total[tt][PAR]",
87     "where the right hand side should point to the total executable.[PAR]",
88     "Output is printed in files [TT]shift.out[tt] where t is the time of the frame.[PAR]",
89     "The program also needs an input file called [BB]random.dat[bb] which",
90     "contains the random coil chemical shifts of all protons."
91   };
92   static real dt=0.0;
93   t_pargs pa[] = {
94     { "-dt", FALSE, etREAL, { &dt }, "Time interval between frames." }
95   };
96   static char *bugs[] = {
97     "The program is very slow"
98   };
99   static     char *OXYGEN="O";
100   FILE       *out,*tot,*fp;
101   t_topology *top;
102   t_atoms    *atoms;
103   int        status,nres;
104   real       t,nt;
105   int        i,natoms,nframe=0;
106   matrix     box;
107   int        gnx;
108   char       *grpnm,*randf;
109   atom_id    *index;
110   rvec       *x,*x_s;
111   char       pdbfile[32],tmpfile[32];
112   char       total[256],*dptr;
113   t_filenm   fnm[] = {
114     { efTRX, "-f",   NULL,     ffREAD },
115     { efTPX, NULL,   NULL,     ffREAD },
116     { efNDX, NULL,   NULL,     ffREAD },
117     { efOUT, "-o",   "shift",  ffWRITE },
118     { efDAT, "-d",   "random", ffREAD }
119   };
120   char *leg[] = { "shift","ring","anisCO","anisCN","sigmaE","sum" };
121 #define NFILE asize(fnm)
122
123   CopyRight(stdout,argv[0]);
124   parse_common_args(&argc,argv,PCA_CAN_TIME | PCA_BE_NICE ,NFILE,fnm,
125                     asize(pa),pa,asize(desc),desc,asize(bugs),bugs);
126                     
127   top=read_top(ftp2fn(efTPX,NFILE,fnm));
128   atoms=&(top->atoms);
129   nres=atoms->nres;
130   for(i=0; (i<atoms->nr); i++)
131     if ((strcmp(*atoms->atomname[i],"O1") == 0) ||
132         (strcmp(*atoms->atomname[i],"O2") == 0) ||
133         (strcmp(*atoms->atomname[i],"OXT") == 0) ||
134         (strcmp(*atoms->atomname[i],"OT") == 0))
135       atoms->atomname[i]=&OXYGEN;
136   rd_index(ftp2fn(efNDX,NFILE,fnm),1,&gnx,&index,&grpnm);
137   
138   snew(x_s,atoms->nr);
139
140   strcpy(pdbfile,"dsXXXXXX");
141   gmx_tmpnam(pdbfile);
142   strcpy(tmpfile,"dsXXXXXX");
143   gmx_tmpnam(tmpfile);
144   fprintf(stderr,"pdbfile = %s\ntmpfile = %s\n",pdbfile,tmpfile);
145   
146   if ((dptr=getenv("GMX_TOTAL")) == NULL)
147     dptr="/home/mdgroup/total/total";
148   sprintf(total,"%s > /dev/null",dptr);
149   fprintf(stderr,"total cmd='%s'\n",total);
150   randf=ftp2fn(efDAT,NFILE,fnm);
151   
152   natoms=read_first_x(&status,ftp2fn(efTRX,NFILE,fnm),&t,&x,box);
153   if (natoms != atoms->nr) 
154     gmx_fatal(FARGS,"Trajectory does not match topology!");
155   out=ftp2FILE(efOUT,NFILE,fnm,"w");
156   xvgr_legend(out,asize(leg),leg);
157   nt=t;
158   do {
159     if (t >= nt) {
160       rm_pbc(&(top->idef),top->atoms.nr,box,x,x_s);
161       fp=gmx_ffopen(pdbfile,"w");
162       write_pdbfile_indexed(fp,"Generated by do_shift",
163                             atoms,x_s,box,0,-1,gnx,index);
164       gmx_ffclose(fp);
165       
166       if ((tot=popen(total,"w")) == NULL)
167         perror("opening pipe to total");
168       fprintf(tot,"%s\n",pdbfile);
169       fprintf(tot,"%s\n",tmpfile);
170       fprintf(tot,"3\n");
171       fprintf(tot,"N\n");
172       fprintf(tot,"%s\n",randf);
173       fprintf(tot,"N\n");
174       fprintf(tot,"N\n");
175       if (pclose(tot) != 0)
176         perror("closing pipe to total");
177       cat(out,tmpfile,t);
178       remove(pdbfile);
179       remove(tmpfile);
180       nt+=dt;
181       nframe++;
182     }
183   } while(read_next_x(status,&t,natoms,x,box));
184   close_trj(status);
185   gmx_ffclose(out);
186   
187   gmx_thanx(stderr);
188   
189   return 0;
190 }