559e16c23343dc683992aecde61afd7fff92350e
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_sigeps.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-2004, The GROMACS development team.
6  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <math.h>
42 #include <stdio.h>
43
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/commandline/pargs.h"
46 #include "gromacs/fileio/xvgr.h"
47 #include "gromacs/legacyheaders/viewit.h"
48 #include "gromacs/fileio/pdbio.h"
49 #include "gromacs/legacyheaders/macros.h"
50 #include "gromacs/utility/smalloc.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/math/units.h"
53 #include "gromacs/legacyheaders/names.h"
54 #include "gromacs/legacyheaders/txtdump.h"
55 #include "gromacs/fileio/trnio.h"
56 #include "gromacs/fileio/confio.h"
57
58 real pot(real x, real qq, real c6, real cn, int npow)
59 {
60     return cn*pow(x, -npow)-c6*pow(x, -6)+qq*ONE_4PI_EPS0/x;
61 }
62
63 real bhpot(real x, real A, real B, real C)
64 {
65     return A*exp(-B*x) - C*pow(x, -6.0);
66 }
67
68 real dpot(real x, real qq, real c6, real cn, int npow)
69 {
70     return -(npow*cn*pow(x, -npow-1)-6*c6*pow(x, -7)+qq*ONE_4PI_EPS0/sqr(x));
71 }
72
73 int gmx_sigeps(int argc, char *argv[])
74 {
75     const char   *desc[] = {
76         "[THISMODULE] is a simple utility that converts C6/C12 or C6/Cn combinations",
77         "to [GRK]sigma[grk] and [GRK]epsilon[grk], or vice versa. It can also plot the potential",
78         "in  file. In addition, it makes an approximation of a Buckingham potential",
79         "to a Lennard-Jones potential."
80     };
81     static real   c6   = 1.0e-3, cn = 1.0e-6, qi = 0, qj = 0, sig = 0.3, eps = 1, sigfac = 0.7;
82     static real   Abh  = 1e5, Bbh = 32, Cbh = 1e-3;
83     static int    npow = 12;
84     t_pargs       pa[] = {
85         { "-c6",   FALSE,  etREAL,  {&c6},  "C6"   },
86         { "-cn",   FALSE,  etREAL,  {&cn},  "Constant for repulsion"   },
87         { "-pow",  FALSE,  etINT,   {&npow}, "Power of the repulsion term" },
88         { "-sig",  FALSE,  etREAL,  {&sig}, "[GRK]sigma[grk]"  },
89         { "-eps",  FALSE,  etREAL,  {&eps}, "[GRK]epsilon[grk]"  },
90         { "-A",    FALSE,  etREAL,  {&Abh}, "Buckingham A" },
91         { "-B",    FALSE,  etREAL,  {&Bbh}, "Buckingham B" },
92         { "-C",    FALSE,  etREAL,  {&Cbh}, "Buckingham C" },
93         { "-qi",   FALSE,  etREAL,  {&qi},  "qi"   },
94         { "-qj",   FALSE,  etREAL,  {&qj},  "qj"   },
95         { "-sigfac", FALSE, etREAL, {&sigfac}, "Factor in front of [GRK]sigma[grk] for starting the plot" }
96     };
97     t_filenm      fnm[] = {
98         { efXVG, "-o", "potje", ffWRITE }
99     };
100     output_env_t  oenv;
101 #define NFILE asize(fnm)
102     const char   *legend[] = { "Lennard-Jones", "Buckingham" };
103     FILE         *fp;
104     int           i;
105     gmx_bool      bBham;
106     real          qq, x, oldx, minimum, mval, dp[2], pp[2];
107     int           cur = 0;
108 #define next (1-cur)
109
110     if (!parse_common_args(&argc, argv, PCA_CAN_VIEW,
111                            NFILE, fnm, asize(pa), pa, asize(desc),
112                            desc, 0, NULL, &oenv))
113     {
114         return 0;
115     }
116
117     bBham = (opt2parg_bSet("-A", asize(pa), pa) ||
118              opt2parg_bSet("-B", asize(pa), pa) ||
119              opt2parg_bSet("-C", asize(pa), pa));
120
121     if (bBham)
122     {
123         c6  = Cbh;
124         sig = pow((6.0/npow)*pow(npow/Bbh, npow-6.0), 1.0/(npow-6.0));
125         eps = c6/(4*pow(sig, 6.0));
126         cn  = 4*eps*pow(sig, npow);
127     }
128     else
129     {
130         if (opt2parg_bSet("-sig", asize(pa), pa) ||
131             opt2parg_bSet("-eps", asize(pa), pa))
132         {
133             c6  = 4*eps*pow(sig, 6);
134             cn  = 4*eps*pow(sig, npow);
135         }
136         else if (opt2parg_bSet("-c6", asize(pa), pa) ||
137                  opt2parg_bSet("-cn", asize(pa), pa) ||
138                  opt2parg_bSet("-pow", asize(pa), pa))
139         {
140             sig = pow(cn/c6, 1.0/(npow-6.0));
141             eps = 0.25*c6*pow(sig, -6.0);
142         }
143         else
144         {
145             sig = eps = 0;
146         }
147         printf("c6    = %12.5e, c%d    = %12.5e\n", c6, npow, cn);
148         printf("sigma = %12.5f, epsilon = %12.5f\n", sig, eps);
149
150         minimum = pow(npow/6.0*pow(sig, npow-6.0), 1.0/(npow-6));
151         printf("Van der Waals minimum at %g, V = %g\n\n",
152                minimum, pot(minimum, 0, c6, cn, npow));
153         printf("Fit of Lennard Jones (%d-6) to Buckingham:\n", npow);
154         Bbh = npow/minimum;
155         Cbh = c6;
156         Abh = 4*eps*pow(sig/minimum, npow)*exp(npow);
157         printf("A = %g, B = %g, C = %g\n", Abh, Bbh, Cbh);
158     }
159     qq = qi*qj;
160
161     fp = xvgropen(ftp2fn(efXVG, NFILE, fnm), "Potential", "r (nm)", "E (kJ/mol)",
162                   oenv);
163     xvgr_legend(fp, asize(legend), legend,
164                 oenv);
165     if (sig == 0)
166     {
167         sig = 0.25;
168     }
169     minimum = -1;
170     mval    = 0;
171     oldx    = 0;
172     for (i = 0; (i < 100); i++)
173     {
174         x        = sigfac*sig+sig*i*0.02;
175         dp[next] = dpot(x, qq, c6, cn, npow);
176         fprintf(fp, "%10g  %10g  %10g\n", x, pot(x, qq, c6, cn, npow),
177                 bhpot(x, Abh, Bbh, Cbh));
178         if (qq != 0)
179         {
180             if ((i > 0) && (dp[cur]*dp[next] < 0))
181             {
182                 minimum = oldx + dp[cur]*(x-oldx)/(dp[cur]-dp[next]);
183                 mval    = pot(minimum, qq, c6, cn, npow);
184                 printf("Van der Waals + Coulomb minimum at r = %g (nm). Value = %g (kJ/mol)\n",
185                        minimum, mval);
186             }
187         }
188         cur  = next;
189         oldx = x;
190
191     }
192     gmx_ffclose(fp);
193
194     do_view(oenv, ftp2fn(efXVG, NFILE, fnm), NULL);
195
196     return 0;
197 }