Merge branch release-5-0 into release-5-1
[alexxy/gromacs.git] / src / contrib / test.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 <stdio.h>
40 #include <math.h>
41 #include "typedefs.h"
42 #include "gromacs/commandline/pargs.h"
43 #include "copyrite.h"
44 #include "gromacs/utility/fatalerror.h"
45 #include "gromacs/fileio/xvgr.h"
46 #include "viewit.h"
47 #include "gromacs/fileio/pdbio.h"
48 #include "macros.h"
49 #include "gromacs/utility/smalloc.h"
50 #include "gromacs/math/vec.h"
51 #include "gromacs/math/units.h"
52 #include "names.h"
53 #include "txtdump.h"
54 #include "gromacs/fileio/trnio.h"
55 #include "gromacs/fileio/confio.h"
56
57 real pot(real x,real qq,real c6,real c12)
58 {
59   return c12*pow(x,-12)-c6*pow(x,-6)+qq*ONE_4PI_EPS0/x;
60 }
61
62 real dpot(real x,real qq,real c6,real c12)
63 {
64   return -(12*c12*pow(x,-13)-6*c6*pow(x,-7)+qq*ONE_4PI_EPS0/sqr(x));
65 }
66
67 int main(int argc,char *argv[])
68 {
69   static char *desc[] = {
70     "Plot the potential"
71   };
72   static real c6=1.0e-3,c12=1.0e-6,qi=1,qj=2,sig=0.3,eps=1,sigfac=0.7;
73   t_pargs pa[] = {
74     { "-c6",   FALSE,  etREAL,  {&c6},  "c6"   },
75     { "-c12",  FALSE,  etREAL,  {&c12}, "c12"  },
76     { "-sig",  FALSE,  etREAL,  {&sig}, "sig"  },
77     { "-eps",  FALSE,  etREAL,  {&eps}, "eps"  },
78     { "-qi",   FALSE,  etREAL,  {&qi},  "qi"   },
79     { "-qj",   FALSE,  etREAL,  {&qj},  "qj"   },
80     { "-sigfac", FALSE, etREAL, {&sigfac}, "Factor in front of sigma for starting the plot" }
81   };
82   t_filenm fnm[] = {
83     { efXVG, "-o", "potje", ffWRITE }
84   };
85 #define NFILE asize(fnm)
86
87   FILE      *fp;
88   int       i;
89   real      qq,x,oldx,minimum,mval,dp[2],pp[2];
90   int       cur=0;
91 #define next (1-cur)
92   
93   /* CopyRight(stdout,argv[0]);*/
94   parse_common_args(&argc,argv,PCA_CAN_VIEW,
95                     NFILE,fnm,asize(pa),pa,asize(desc),
96                     desc,0,NULL);
97
98   if (opt2parg_bSet("-sig",asize(pa),pa) ||
99       opt2parg_bSet("-eps",asize(pa),pa)) {
100     c6  = 4*eps*pow(sig,6);
101     c12 = 4*eps*pow(sig,12);
102   }
103   else if ((c6 != 0) && (c12 != 0)) {
104     sig = pow(c12/c6,1.0/6.0);
105     eps = c6*c6/(4*c12);
106   }
107   else {
108     sig = eps = 0;
109   }
110   printf("c6    = %12.5e, c12     = %12.5e\n",c6,c12);
111   printf("sigma = %12.5f, epsilon = %12.5f\n",sig,eps);
112   qq = qi*qj;
113       
114   fp = xvgropen(ftp2fn(efXVG,NFILE,fnm),"Potential","r (nm)","E (kJ/mol)");
115   if (sig == 0)
116     sig=0.25;
117   minimum = -1;
118   mval    = 0;
119   oldx    = 0;
120   for(i=0; (i<100); i++) {
121     x    = sigfac*sig+sig*i*0.02;
122     dp[next] = dpot(x,qq,c6,c12);
123     fprintf(fp,"%10g  %10g  %10g\n",x,pot(x,qq,c6,c12),
124             dp[next]);
125     if ((i > 0) && (dp[cur]*dp[next] < 0)) {
126       minimum = oldx + dp[cur]*(x-oldx)/(dp[cur]-dp[next]);
127       mval    = pot(minimum,qq,c6,c12);
128       /*fprintf(stdout,"dp[cur] = %g, dp[next] = %g  oldx = %g, dx = %g\n",
129         dp[cur],dp[next],oldx,x-oldx);*/
130       printf("Minimum at r = %g (nm). Value = %g (kJ/mol)\n",
131               minimum,mval);
132     }
133     cur = next;
134     oldx = x;
135       
136   }
137   gmx_ffclose(fp);
138   
139   do_view(ftp2fn(efXVG,NFILE,fnm),NULL);
140
141   gmx_thanx(stderr);
142                
143   return 0;
144 }
145
146