Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / contrib / sigeps.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.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
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  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Gromacs Runs On Most of All Computer Systems
35  */
36 static char *SRCID_sigeps_c = "$Id$";
37 #include <stdio.h>
38 #include <math.h>
39 #include "typedefs.h"
40 #include "statutil.h"
41 #include "copyrite.h"
42 #include "fatal.h"
43 #include "xvgr.h"
44 #include "pdbio.h"
45 #include "macros.h"
46 #include "smalloc.h"
47 #include "vec.h"
48 #include "pbc.h"
49 #include "physics.h"
50 #include "names.h"
51 #include "txtdump.h"
52 #include "trnio.h"
53 #include "symtab.h"
54 #include "confio.h"
55
56 real pot(real x,real qq,real c6,real c12)
57 {
58   return c12*pow(x,-12)-c6*pow(x,-6)+qq*ONE_4PI_EPS0/x;
59 }
60
61 real dpot(real x,real qq,real c6,real c12)
62 {
63   return -(12*c12*pow(x,-13)-6*c6*pow(x,-7)+qq*ONE_4PI_EPS0/sqr(x));
64 }
65
66 int main(int argc,char *argv[])
67 {
68   static char *desc[] = {
69     "Plot the potential"
70   };
71   static real c6=1.0e-3,c12=1.0e-6,qi=1,qj=2,sig=0.3,eps=1,sigfac=0.7;
72   t_pargs pa[] = {
73     { "-c6",   FALSE,  etREAL,  {&c6},  "c6"   },
74     { "-c12",  FALSE,  etREAL,  {&c12}, "c12"  },
75     { "-sig",  FALSE,  etREAL,  {&sig}, "sig"  },
76     { "-eps",  FALSE,  etREAL,  {&eps}, "eps"  },
77     { "-qi",   FALSE,  etREAL,  {&qi},  "qi"   },
78     { "-qj",   FALSE,  etREAL,  {&qj},  "qj"   },
79     { "-sigfac", FALSE, etREAL, {&sigfac}, "Factor in front of sigma for starting the plot" }
80   };
81   t_filenm fnm[] = {
82     { efXVG, "-o", "potje", ffWRITE }
83   };
84 #define NFILE asize(fnm)
85
86   FILE      *fp;
87   int       i;
88   real      qq,x,oldx,minimum,mval,dp[2],pp[2];
89   int       cur=0;
90 #define next (1-cur)
91   
92   /* CopyRight(stdout,argv[0]);*/
93   parse_common_args(&argc,argv,PCA_CAN_VIEW,
94                     FALSE,NFILE,fnm,asize(pa),pa,asize(desc),
95                     desc,0,NULL);
96
97   if (opt2parg_bSet("-sig",asize(pa),pa) ||
98       opt2parg_bSet("-eps",asize(pa),pa)) {
99     c6  = 4*eps*pow(sig,6);
100     c12 = 4*eps*pow(sig,12);
101   }
102   else if ((c6 != 0) && (c12 != 0)) {
103     sig = pow(c12/c6,1.0/6.0);
104     eps = c6*c6/(4*c12);
105   }
106   else {
107     sig = eps = 0;
108   }
109   printf("c6    = %12.5e, c12     = %12.5e\n",c6,c12);
110   printf("sigma = %12.5f, epsilon = %12.5f\n",sig,eps);
111   qq = qi*qj;
112       
113   fp = xvgropen(ftp2fn(efXVG,NFILE,fnm),"Potential","r (nm)","E (kJ/mol)");
114   if (sig == 0)
115     sig=0.25;
116   minimum = -1;
117   mval    = 0;
118   oldx    = 0;
119   for(i=0; (i<100); i++) {
120     x    = sigfac*sig+sig*i*0.02;
121     dp[next] = dpot(x,qq,c6,c12);
122     fprintf(fp,"%10g  %10g  %10g\n",x,pot(x,qq,c6,c12),
123             dp[next]);
124     if ((i > 0) && (dp[cur]*dp[next] < 0)) {
125       minimum = oldx + dp[cur]*(x-oldx)/(dp[cur]-dp[next]);
126       mval    = pot(minimum,qq,c6,c12);
127       /*fprintf(stdout,"dp[cur] = %g, dp[next] = %g  oldx = %g, dx = %g\n",
128         dp[cur],dp[next],oldx,x-oldx);*/
129       printf("Minimum at r = %g (nm). Value = %g (kJ/mol)\n",
130               minimum,mval);
131     }
132     cur = next;
133     oldx = x;
134       
135   }
136   fclose(fp);
137   
138   do_view(ftp2fn(efXVG,NFILE,fnm),NULL);
139
140   thanx(stderr);  
141                
142   return 0;
143 }
144
145