Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxlib / ewald_util.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) 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 <math.h>
40 #include <stdio.h>
41
42 #include "gromacs/legacyheaders/coulomb.h"
43 #include "gromacs/legacyheaders/macros.h"
44 #include "gromacs/legacyheaders/names.h"
45 #include "gromacs/legacyheaders/txtdump.h"
46 #include "gromacs/legacyheaders/typedefs.h"
47 #include "gromacs/legacyheaders/types/commrec.h"
48 #include "gromacs/math/units.h"
49 #include "gromacs/math/utilities.h"
50 #include "gromacs/math/vec.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/smalloc.h"
53
54 real calc_ewaldcoeff_q(real rc, real dtol)
55 {
56     real x = 5, low, high;
57     int  n, i = 0;
58
59
60     do
61     {
62         i++;
63         x *= 2;
64     }
65     while (gmx_erfc(x*rc) > dtol);
66
67     n    = i+60; /* search tolerance is 2^-60 */
68     low  = 0;
69     high = x;
70     for (i = 0; i < n; i++)
71     {
72         x = (low+high)/2;
73         if (gmx_erfc(x*rc) > dtol)
74         {
75             low = x;
76         }
77         else
78         {
79             high = x;
80         }
81     }
82     return x;
83 }
84
85 static real ewald_function_lj(real x, real rc)
86 {
87     real xrc, xrc2, xrc4, factor;
88     xrc  = x*rc;
89     xrc2 = xrc*xrc;
90     xrc4 = xrc2*xrc2;
91 #ifdef GMX_DOUBLE
92     factor = exp(-xrc2)*(1 + xrc2 + xrc4/2.0);
93 #else
94     factor = expf(-xrc2)*(1 + xrc2 + xrc4/2.0);
95 #endif
96
97     return factor;
98 }
99
100 real calc_ewaldcoeff_lj(real rc, real dtol)
101 {
102     real x = 5, low, high;
103     int  n, i = 0;
104
105     do
106     {
107         i++;
108         x *= 2.0;
109     }
110     while (ewald_function_lj(x, rc) > dtol);
111
112     n    = i + 60; /* search tolerance is 2^-60 */
113     low  = 0;
114     high = x;
115     for (i = 0; i < n; ++i)
116     {
117         x = (low + high) / 2.0;
118         if (ewald_function_lj(x, rc) > dtol)
119         {
120             low = x;
121         }
122         else
123         {
124             high = x;
125         }
126     }
127     return x;
128 }
129
130 void ewald_LRcorrection(int start, int end,
131                         t_commrec *cr, int thread, t_forcerec *fr,
132                         real *chargeA, real *chargeB,
133                         real *C6A, real *C6B,
134                         real *sigmaA, real *sigmaB,
135                         real *sigma3A, real *sigma3B,
136                         gmx_bool calc_excl_corr,
137                         t_blocka *excl, rvec x[],
138                         matrix box, rvec mu_tot[],
139                         int ewald_geometry, real epsilon_surface,
140                         rvec *f, tensor vir_q, tensor vir_lj,
141                         real *Vcorr_q, real *Vcorr_lj,
142                         real lambda_q, real lambda_lj,
143                         real *dvdlambda_q, real *dvdlambda_lj)
144 {
145     int         i, i1, i2, j, k, m, iv, jv, q;
146     atom_id    *AA;
147     double      Vexcl_q, dvdl_excl_q, dvdl_excl_lj; /* Necessary for precision */
148     double      Vexcl_lj;
149     real        one_4pi_eps;
150     real        v, vc, qiA, qiB, dr2, rinv, enercorr;
151     real        Vself_q[2], Vself_lj[2], Vdipole[2], rinv2, ewc_q = fr->ewaldcoeff_q, ewcdr;
152     real        ewc_lj = fr->ewaldcoeff_lj, ewc_lj2 = ewc_lj * ewc_lj;
153     real        c6Ai   = 0, c6Bi = 0, c6A = 0, c6B = 0, ewcdr2, ewcdr4, c6L = 0, rinv6;
154     rvec        df, dx, mutot[2], dipcorrA, dipcorrB;
155     tensor      dxdf_q, dxdf_lj;
156     real        vol = box[XX][XX]*box[YY][YY]*box[ZZ][ZZ];
157     real        L1_q, L1_lj, dipole_coeff, qqA, qqB, qqL, vr0_q, vr0_lj = 0;
158     gmx_bool    bFreeEnergy  = (chargeB != NULL);
159     gmx_bool    bMolPBC      = fr->bMolPBC;
160     gmx_bool    bDoingLBRule = (fr->ljpme_combination_rule == eljpmeLB);
161
162     /* This routine can be made faster by using tables instead of analytical interactions
163      * However, that requires a thorough verification that they are correct in all cases.
164      */
165
166     one_4pi_eps   = ONE_4PI_EPS0/fr->epsilon_r;
167     vr0_q         = ewc_q*M_2_SQRTPI;
168     if (EVDW_PME(fr->vdwtype))
169     {
170         vr0_lj    = -pow(ewc_lj, 6)/6.0;
171     }
172
173     AA           = excl->a;
174     Vexcl_q      = 0;
175     Vexcl_lj     = 0;
176     dvdl_excl_q  = 0;
177     dvdl_excl_lj = 0;
178     Vdipole[0]   = 0;
179     Vdipole[1]   = 0;
180     L1_q         = 1.0-lambda_q;
181     L1_lj        = 1.0-lambda_lj;
182     /* Note that we have to transform back to gromacs units, since
183      * mu_tot contains the dipole in debye units (for output).
184      */
185     for (i = 0; (i < DIM); i++)
186     {
187         mutot[0][i] = mu_tot[0][i]*DEBYE2ENM;
188         mutot[1][i] = mu_tot[1][i]*DEBYE2ENM;
189         dipcorrA[i] = 0;
190         dipcorrB[i] = 0;
191     }
192     dipole_coeff = 0;
193     switch (ewald_geometry)
194     {
195         case eewg3D:
196             if (epsilon_surface != 0)
197             {
198                 dipole_coeff =
199                     2*M_PI*ONE_4PI_EPS0/((2*epsilon_surface + fr->epsilon_r)*vol);
200                 for (i = 0; (i < DIM); i++)
201                 {
202                     dipcorrA[i] = 2*dipole_coeff*mutot[0][i];
203                     dipcorrB[i] = 2*dipole_coeff*mutot[1][i];
204                 }
205             }
206             break;
207         case eewg3DC:
208             dipole_coeff = 2*M_PI*one_4pi_eps/vol;
209             dipcorrA[ZZ] = 2*dipole_coeff*mutot[0][ZZ];
210             dipcorrB[ZZ] = 2*dipole_coeff*mutot[1][ZZ];
211             break;
212         default:
213             gmx_incons("Unsupported Ewald geometry");
214             break;
215     }
216     if (debug)
217     {
218         fprintf(debug, "dipcorr = %8.3f  %8.3f  %8.3f\n",
219                 dipcorrA[XX], dipcorrA[YY], dipcorrA[ZZ]);
220         fprintf(debug, "mutot   = %8.3f  %8.3f  %8.3f\n",
221                 mutot[0][XX], mutot[0][YY], mutot[0][ZZ]);
222     }
223     clear_mat(dxdf_q);
224     if (EVDW_PME(fr->vdwtype))
225     {
226         clear_mat(dxdf_lj);
227     }
228     if ((calc_excl_corr || dipole_coeff != 0) && !bFreeEnergy)
229     {
230         for (i = start; (i < end); i++)
231         {
232             /* Initiate local variables (for this i-particle) to 0 */
233             qiA = chargeA[i]*one_4pi_eps;
234             if (EVDW_PME(fr->vdwtype))
235             {
236                 c6Ai = C6A[i];
237                 if (bDoingLBRule)
238                 {
239                     c6Ai *= sigma3A[i];
240                 }
241             }
242             if (calc_excl_corr)
243             {
244                 i1  = excl->index[i];
245                 i2  = excl->index[i+1];
246
247                 /* Loop over excluded neighbours */
248                 for (j = i1; (j < i2); j++)
249                 {
250                     k = AA[j];
251                     /*
252                      * First we must test whether k <> i, and then,
253                      * because the exclusions are all listed twice i->k
254                      * and k->i we must select just one of the two.  As
255                      * a minor optimization we only compute forces when
256                      * the charges are non-zero.
257                      */
258                     if (k > i)
259                     {
260                         qqA = qiA*chargeA[k];
261                         if (EVDW_PME(fr->vdwtype))
262                         {
263                             c6A  = c6Ai * C6A[k];
264                             if (bDoingLBRule)
265                             {
266                                 c6A *= pow(0.5*(sigmaA[i]+sigmaA[k]), 6)*sigma3A[k];
267                             }
268                         }
269                         if (qqA != 0.0 || c6A != 0.0)
270                         {
271                             real fscal;
272
273                             fscal = 0;
274                             rvec_sub(x[i], x[k], dx);
275                             if (bMolPBC)
276                             {
277                                 /* Cheap pbc_dx, assume excluded pairs are at short distance. */
278                                 for (m = DIM-1; (m >= 0); m--)
279                                 {
280                                     if (dx[m] > 0.5*box[m][m])
281                                     {
282                                         rvec_dec(dx, box[m]);
283                                     }
284                                     else if (dx[m] < -0.5*box[m][m])
285                                     {
286                                         rvec_inc(dx, box[m]);
287                                     }
288                                 }
289                             }
290                             dr2 = norm2(dx);
291                             /* Distance between two excluded particles
292                              * may be zero in the case of shells
293                              */
294                             if (dr2 != 0)
295                             {
296                                 rinv              = gmx_invsqrt(dr2);
297                                 rinv2             = rinv*rinv;
298                                 if (qqA != 0.0)
299                                 {
300                                     real dr;
301
302                                     dr       = 1.0/rinv;
303                                     ewcdr    = ewc_q*dr;
304                                     vc       = qqA*gmx_erf(ewcdr)*rinv;
305                                     Vexcl_q += vc;
306 #ifdef GMX_DOUBLE
307                                     /* Relative accuracy at R_ERF_R_INACC of 3e-10 */
308 #define       R_ERF_R_INACC 0.006
309 #else
310                                     /* Relative accuracy at R_ERF_R_INACC of 2e-5 */
311 #define       R_ERF_R_INACC 0.1
312 #endif
313                                     /* fscal is the scalar force pre-multiplied by rinv,
314                                      * to normalise the relative position vector dx */
315                                     if (ewcdr > R_ERF_R_INACC)
316                                     {
317                                         fscal = rinv2*(vc - qqA*ewc_q*M_2_SQRTPI*exp(-ewcdr*ewcdr));
318                                     }
319                                     else
320                                     {
321                                         /* Use a fourth order series expansion for small ewcdr */
322                                         fscal = ewc_q*ewc_q*qqA*vr0_q*(2.0/3.0 - 0.4*ewcdr*ewcdr);
323                                     }
324
325                                     /* The force vector is obtained by multiplication with
326                                      * the relative position vector
327                                      */
328                                     svmul(fscal, dx, df);
329                                     rvec_inc(f[k], df);
330                                     rvec_dec(f[i], df);
331                                     for (iv = 0; (iv < DIM); iv++)
332                                     {
333                                         for (jv = 0; (jv < DIM); jv++)
334                                         {
335                                             dxdf_q[iv][jv] += dx[iv]*df[jv];
336                                         }
337                                     }
338                                 }
339
340                                 if (c6A != 0.0)
341                                 {
342                                     rinv6     = rinv2*rinv2*rinv2;
343                                     ewcdr2    = ewc_lj2*dr2;
344                                     ewcdr4    = ewcdr2*ewcdr2;
345                                     /* We get the excluded long-range contribution from -C6*(1-g(r))
346                                      * g(r) is also defined in the manual under LJ-PME
347                                      */
348                                     vc        = -c6A*rinv6*(1.0 - exp(-ewcdr2)*(1 + ewcdr2 + 0.5*ewcdr4));
349                                     Vexcl_lj += vc;
350                                     /* The force is the derivative of the potential vc.
351                                      * fscal is the scalar force pre-multiplied by rinv,
352                                      * to normalise the relative position vector dx */
353                                     fscal     = 6.0*vc*rinv2 + c6A*rinv6*exp(-ewcdr2)*ewc_lj2*ewcdr4;
354
355                                     /* The force vector is obtained by multiplication with
356                                      * the relative position vector
357                                      */
358                                     svmul(fscal, dx, df);
359                                     rvec_inc(f[k], df);
360                                     rvec_dec(f[i], df);
361                                     for (iv = 0; (iv < DIM); iv++)
362                                     {
363                                         for (jv = 0; (jv < DIM); jv++)
364                                         {
365                                             dxdf_lj[iv][jv] += dx[iv]*df[jv];
366                                         }
367                                     }
368                                 }
369                             }
370                             else
371                             {
372                                 Vexcl_q  += qqA*vr0_q;
373                                 Vexcl_lj += c6A*vr0_lj;
374                             }
375                         }
376                     }
377                 }
378             }
379             /* Dipole correction on force */
380             if (dipole_coeff != 0)
381             {
382                 for (j = 0; (j < DIM); j++)
383                 {
384                     f[i][j] -= dipcorrA[j]*chargeA[i];
385                 }
386             }
387         }
388     }
389     else if (calc_excl_corr || dipole_coeff != 0)
390     {
391         for (i = start; (i < end); i++)
392         {
393             /* Initiate local variables (for this i-particle) to 0 */
394             qiA = chargeA[i]*one_4pi_eps;
395             qiB = chargeB[i]*one_4pi_eps;
396             if (EVDW_PME(fr->vdwtype))
397             {
398                 c6Ai = C6A[i];
399                 c6Bi = C6B[i];
400                 if (bDoingLBRule)
401                 {
402                     c6Ai *= sigma3A[i];
403                     c6Bi *= sigma3B[i];
404                 }
405             }
406             if (calc_excl_corr)
407             {
408                 i1  = excl->index[i];
409                 i2  = excl->index[i+1];
410
411                 /* Loop over excluded neighbours */
412                 for (j = i1; (j < i2); j++)
413                 {
414                     k = AA[j];
415                     if (k > i)
416                     {
417                         qqA = qiA*chargeA[k];
418                         qqB = qiB*chargeB[k];
419                         if (EVDW_PME(fr->vdwtype))
420                         {
421                             c6A = c6Ai*C6A[k];
422                             c6B = c6Bi*C6B[k];
423                             if (bDoingLBRule)
424                             {
425                                 c6A *= pow(0.5*(sigmaA[i]+sigmaA[k]), 6)*sigma3A[k];
426                                 c6B *= pow(0.5*(sigmaB[i]+sigmaB[k]), 6)*sigma3B[k];
427                             }
428                         }
429                         if (qqA != 0.0 || qqB != 0.0 || c6A != 0.0 || c6B != 0.0)
430                         {
431                             real fscal;
432
433                             fscal = 0;
434                             qqL   = L1_q*qqA + lambda_q*qqB;
435                             if (EVDW_PME(fr->vdwtype))
436                             {
437                                 c6L = L1_lj*c6A + lambda_lj*c6B;
438                             }
439                             rvec_sub(x[i], x[k], dx);
440                             if (bMolPBC)
441                             {
442                                 /* Cheap pbc_dx, assume excluded pairs are at short distance. */
443                                 for (m = DIM-1; (m >= 0); m--)
444                                 {
445                                     if (dx[m] > 0.5*box[m][m])
446                                     {
447                                         rvec_dec(dx, box[m]);
448                                     }
449                                     else if (dx[m] < -0.5*box[m][m])
450                                     {
451                                         rvec_inc(dx, box[m]);
452                                     }
453                                 }
454                             }
455                             dr2 = norm2(dx);
456                             if (dr2 != 0)
457                             {
458                                 rinv    = gmx_invsqrt(dr2);
459                                 rinv2   = rinv*rinv;
460                                 if (qqA != 0.0 || qqB != 0.0)
461                                 {
462                                     real dr;
463
464                                     dr           = 1.0/rinv;
465                                     v            = gmx_erf(ewc_q*dr)*rinv;
466                                     vc           = qqL*v;
467                                     Vexcl_q     += vc;
468                                     /* fscal is the scalar force pre-multiplied by rinv,
469                                      * to normalise the relative position vector dx */
470                                     fscal        = rinv2*(vc-qqL*ewc_q*M_2_SQRTPI*exp(-ewc_q*ewc_q*dr2));
471                                     dvdl_excl_q += (qqB - qqA)*v;
472
473                                     /* The force vector is obtained by multiplication with
474                                      * the relative position vector
475                                      */
476                                     svmul(fscal, dx, df);
477                                     rvec_inc(f[k], df);
478                                     rvec_dec(f[i], df);
479                                     for (iv = 0; (iv < DIM); iv++)
480                                     {
481                                         for (jv = 0; (jv < DIM); jv++)
482                                         {
483                                             dxdf_q[iv][jv] += dx[iv]*df[jv];
484                                         }
485                                     }
486                                 }
487
488                                 if ((c6A != 0.0 || c6B != 0.0) && EVDW_PME(fr->vdwtype))
489                                 {
490                                     rinv6         = rinv2*rinv2*rinv2;
491                                     ewcdr2        = ewc_lj2*dr2;
492                                     ewcdr4        = ewcdr2*ewcdr2;
493                                     v             = -rinv6*(1.0 - exp(-ewcdr2)*(1 + ewcdr2 + 0.5*ewcdr4));
494                                     vc            = c6L*v;
495                                     Vexcl_lj     += vc;
496                                     /* fscal is the scalar force pre-multiplied by rinv,
497                                      * to normalise the relative position vector dx */
498                                     fscal         = 6.0*vc*rinv2 + c6L*rinv6*exp(-ewcdr2)*ewc_lj2*ewcdr4;
499                                     dvdl_excl_lj += (c6B - c6A)*v;
500
501                                     /* The force vector is obtained by multiplication with
502                                      * the relative position vector
503                                      */
504                                     svmul(fscal, dx, df);
505                                     rvec_inc(f[k], df);
506                                     rvec_dec(f[i], df);
507                                     for (iv = 0; (iv < DIM); iv++)
508                                     {
509                                         for (jv = 0; (jv < DIM); jv++)
510                                         {
511                                             dxdf_lj[iv][jv] += dx[iv]*df[jv];
512                                         }
513                                     }
514                                 }
515                             }
516                             else
517                             {
518                                 Vexcl_q      += qqL*vr0_q;
519                                 dvdl_excl_q  += (qqB - qqA)*vr0_q;
520                                 Vexcl_lj     += c6L*vr0_lj;
521                                 dvdl_excl_lj += (c6B - c6A)*vr0_lj;
522                             }
523                         }
524                     }
525                 }
526             }
527             /* Dipole correction on force */
528             if (dipole_coeff != 0)
529             {
530                 for (j = 0; (j < DIM); j++)
531                 {
532                     f[i][j] -= L1_q*dipcorrA[j]*chargeA[i]
533                         + lambda_q*dipcorrB[j]*chargeB[i];
534                 }
535             }
536         }
537     }
538     for (iv = 0; (iv < DIM); iv++)
539     {
540         for (jv = 0; (jv < DIM); jv++)
541         {
542             vir_q[iv][jv]  += 0.5*dxdf_q[iv][jv];
543             vir_lj[iv][jv] += 0.5*dxdf_lj[iv][jv];
544         }
545     }
546
547     Vself_q[0]  = 0;
548     Vself_q[1]  = 0;
549     Vself_lj[0] = 0;
550     Vself_lj[1] = 0;
551
552     /* Global corrections only on master process */
553     if (MASTER(cr) && thread == 0)
554     {
555         for (q = 0; q < (bFreeEnergy ? 2 : 1); q++)
556         {
557             if (calc_excl_corr)
558             {
559                 /* Self-energy correction */
560                 Vself_q[q] = ewc_q*one_4pi_eps*fr->q2sum[q]*M_1_SQRTPI;
561                 if (EVDW_PME(fr->vdwtype))
562                 {
563                     Vself_lj[q] =  fr->c6sum[q]*0.5*vr0_lj;
564                 }
565             }
566
567             /* Apply surface dipole correction:
568              * correction = dipole_coeff * (dipole)^2
569              */
570             if (dipole_coeff != 0)
571             {
572                 if (ewald_geometry == eewg3D)
573                 {
574                     Vdipole[q] = dipole_coeff*iprod(mutot[q], mutot[q]);
575                 }
576                 else if (ewald_geometry == eewg3DC)
577                 {
578                     Vdipole[q] = dipole_coeff*mutot[q][ZZ]*mutot[q][ZZ];
579                 }
580             }
581         }
582     }
583     if (!bFreeEnergy)
584     {
585         *Vcorr_q = Vdipole[0] - Vself_q[0] - Vexcl_q;
586         if (EVDW_PME(fr->vdwtype))
587         {
588             *Vcorr_lj = -Vself_lj[0] - Vexcl_lj;
589         }
590     }
591     else
592     {
593         *Vcorr_q = L1_q*(Vdipole[0] - Vself_q[0])
594             + lambda_q*(Vdipole[1] - Vself_q[1])
595             - Vexcl_q;
596         *dvdlambda_q += Vdipole[1] - Vself_q[1]
597             - (Vdipole[0] - Vself_q[0]) - dvdl_excl_q;
598         if (EVDW_PME(fr->vdwtype))
599         {
600             *Vcorr_lj      = -(L1_lj*Vself_lj[0] + lambda_lj*Vself_lj[1]) - Vexcl_lj;
601             *dvdlambda_lj += -Vself_lj[1] + Vself_lj[0] - dvdl_excl_lj;
602         }
603     }
604
605     if (debug)
606     {
607         fprintf(debug, "Long Range corrections for Ewald interactions:\n");
608         fprintf(debug, "start=%d,natoms=%d\n", start, end-start);
609         fprintf(debug, "q2sum = %g, Vself_q=%g c6sum = %g, Vself_lj=%g\n",
610                 L1_q*fr->q2sum[0]+lambda_q*fr->q2sum[1], L1_q*Vself_q[0]+lambda_q*Vself_q[1], L1_lj*fr->c6sum[0]+lambda_lj*fr->c6sum[1], L1_lj*Vself_lj[0]+lambda_lj*Vself_lj[1]);
611         fprintf(debug, "Electrostatic Long Range correction: Vexcl=%g\n", Vexcl_q);
612         fprintf(debug, "Lennard-Jones Long Range correction: Vexcl=%g\n", Vexcl_lj);
613         if (MASTER(cr) && thread == 0)
614         {
615             if (epsilon_surface > 0 || ewald_geometry == eewg3DC)
616             {
617                 fprintf(debug, "Total dipole correction: Vdipole=%g\n",
618                         L1_q*Vdipole[0]+lambda_q*Vdipole[1]);
619             }
620         }
621     }
622 }
623
624 real ewald_charge_correction(t_commrec *cr, t_forcerec *fr, real lambda,
625                              matrix box,
626                              real *dvdlambda, tensor vir)
627
628 {
629     real vol, fac, qs2A, qs2B, vc, enercorr;
630     int  d;
631
632     if (MASTER(cr))
633     {
634         /* Apply charge correction */
635         vol = box[XX][XX]*box[YY][YY]*box[ZZ][ZZ];
636
637         fac = M_PI*ONE_4PI_EPS0/(fr->epsilon_r*2.0*vol*vol*sqr(fr->ewaldcoeff_q));
638
639         qs2A = fr->qsum[0]*fr->qsum[0];
640         qs2B = fr->qsum[1]*fr->qsum[1];
641
642         vc = (qs2A*(1 - lambda) + qs2B*lambda)*fac;
643
644         enercorr = -vol*vc;
645
646         *dvdlambda += -vol*(qs2B - qs2A)*fac;
647
648         for (d = 0; d < DIM; d++)
649         {
650             vir[d][d] += vc;
651         }
652
653         if (debug)
654         {
655             fprintf(debug, "Total charge correction: Vcharge=%g\n", enercorr);
656         }
657     }
658     else
659     {
660         enercorr = 0;
661     }
662
663     return enercorr;
664 }