Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / contrib / test.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2006, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <stdio.h>
43 #include <math.h>
44 #include "typedefs.h"
45 #include "statutil.h"
46 #include "copyrite.h"
47 #include "gmx_fatal.h"
48 #include "xvgr.h"
49 #include "pdbio.h"
50 #include "macros.h"
51 #include "smalloc.h"
52 #include "vec.h"
53 #include "pbc.h"
54 #include "physics.h"
55 #include "names.h"
56 #include "txtdump.h"
57 #include "trnio.h"
58 #include "symtab.h"
59 #include "confio.h"
60
61 real pot(real x,real qq,real c6,real c12)
62 {
63   return c12*pow(x,-12)-c6*pow(x,-6)+qq*ONE_4PI_EPS0/x;
64 }
65
66 real dpot(real x,real qq,real c6,real c12)
67 {
68   return -(12*c12*pow(x,-13)-6*c6*pow(x,-7)+qq*ONE_4PI_EPS0/sqr(x));
69 }
70
71 int main(int argc,char *argv[])
72 {
73   static char *desc[] = {
74     "Plot the potential"
75   };
76   static real c6=1.0e-3,c12=1.0e-6,qi=1,qj=2,sig=0.3,eps=1,sigfac=0.7;
77   t_pargs pa[] = {
78     { "-c6",   FALSE,  etREAL,  {&c6},  "c6"   },
79     { "-c12",  FALSE,  etREAL,  {&c12}, "c12"  },
80     { "-sig",  FALSE,  etREAL,  {&sig}, "sig"  },
81     { "-eps",  FALSE,  etREAL,  {&eps}, "eps"  },
82     { "-qi",   FALSE,  etREAL,  {&qi},  "qi"   },
83     { "-qj",   FALSE,  etREAL,  {&qj},  "qj"   },
84     { "-sigfac", FALSE, etREAL, {&sigfac}, "Factor in front of sigma for starting the plot" }
85   };
86   t_filenm fnm[] = {
87     { efXVG, "-o", "potje", ffWRITE }
88   };
89 #define NFILE asize(fnm)
90
91   FILE      *fp;
92   int       i;
93   real      qq,x,oldx,minimum,mval,dp[2],pp[2];
94   int       cur=0;
95 #define next (1-cur)
96   
97   /* CopyRight(stdout,argv[0]);*/
98   parse_common_args(&argc,argv,PCA_CAN_VIEW,
99                     NFILE,fnm,asize(pa),pa,asize(desc),
100                     desc,0,NULL);
101
102   if (opt2parg_bSet("-sig",asize(pa),pa) ||
103       opt2parg_bSet("-eps",asize(pa),pa)) {
104     c6  = 4*eps*pow(sig,6);
105     c12 = 4*eps*pow(sig,12);
106   }
107   else if ((c6 != 0) && (c12 != 0)) {
108     sig = pow(c12/c6,1.0/6.0);
109     eps = c6*c6/(4*c12);
110   }
111   else {
112     sig = eps = 0;
113   }
114   printf("c6    = %12.5e, c12     = %12.5e\n",c6,c12);
115   printf("sigma = %12.5f, epsilon = %12.5f\n",sig,eps);
116   qq = qi*qj;
117       
118   fp = xvgropen(ftp2fn(efXVG,NFILE,fnm),"Potential","r (nm)","E (kJ/mol)");
119   if (sig == 0)
120     sig=0.25;
121   minimum = -1;
122   mval    = 0;
123   oldx    = 0;
124   for(i=0; (i<100); i++) {
125     x    = sigfac*sig+sig*i*0.02;
126     dp[next] = dpot(x,qq,c6,c12);
127     fprintf(fp,"%10g  %10g  %10g\n",x,pot(x,qq,c6,c12),
128             dp[next]);
129     if ((i > 0) && (dp[cur]*dp[next] < 0)) {
130       minimum = oldx + dp[cur]*(x-oldx)/(dp[cur]-dp[next]);
131       mval    = pot(minimum,qq,c6,c12);
132       /*fprintf(stdout,"dp[cur] = %g, dp[next] = %g  oldx = %g, dx = %g\n",
133         dp[cur],dp[next],oldx,x-oldx);*/
134       printf("Minimum at r = %g (nm). Value = %g (kJ/mol)\n",
135               minimum,mval);
136     }
137     cur = next;
138     oldx = x;
139       
140   }
141   ffclose(fp);
142   
143   do_view(ftp2fn(efXVG,NFILE,fnm),NULL);
144
145   thanx(stderr);  
146                
147   return 0;
148 }
149
150