Fixed include statements such that double precision version of genborn.c
[gromacs/rigid-bodies.git] / src / mdlib / ewald.c
blob192a9d93573114982c352f8f01cc891c266f7858
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.2.0
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-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
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.
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.
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.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROwing Monsters And Cloning Shrimps
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <stdio.h>
40 #include <math.h>
41 #include "typedefs.h"
42 #include "vec.h"
43 #include "gmxcomplex.h"
44 #include "smalloc.h"
45 #include "futil.h"
46 #include "gmx_fatal.h"
47 #include "physics.h"
48 #include "coulomb.h"
50 #define TOL 2e-5
52 struct ewald_tab
54 int nx,ny,nz,kmax;
55 cvec **eir;
56 t_complex *tab_xy, *tab_qxyz;
57 };
61 /* TODO: fix thread-safety */
63 /* the other routines are in complex.h */
64 static t_complex conjmul(t_complex a,t_complex b)
66 t_complex c;
68 c.re = a.re*b.re + a.im*b.im;
69 c.im = a.im*b.re - a.re*b.im;
71 return c;
77 static void tabulate_eir(int natom,rvec x[],int kmax,cvec **eir,rvec lll)
79 int i,j,m;
81 if (kmax < 1) {
82 printf("Go away! kmax = %d\n",kmax);
83 exit(1);
86 for(i=0; (i<natom); i++) {
87 for(m=0; (m<3); m++) {
88 eir[0][i][m].re = 1;
89 eir[0][i][m].im = 0;
92 for(m=0; (m<3); m++) {
93 eir[1][i][m].re = cos(x[i][m]*lll[m]);
94 eir[1][i][m].im = sin(x[i][m]*lll[m]);
96 for(j=2; (j<kmax); j++)
97 for(m=0; (m<3); m++)
98 eir[j][i][m] = cmul(eir[j-1][i][m],eir[1][i][m]);
102 void init_ewald_tab(ewald_tab_t *et, const t_commrec *cr, const t_inputrec *ir,
103 FILE *fp)
105 int n;
107 snew(*et, 1);
108 if (fp)
109 fprintf(fp,"Will do ordinary reciprocal space Ewald sum.\n");
111 (*et)->nx = ir->nkx+1;
112 (*et)->ny = ir->nky+1;
113 (*et)->nz = ir->nkz+1;
114 (*et)->kmax = max((*et)->nx,max((*et)->ny,(*et)->nz));
115 (*et)->eir = NULL;
116 (*et)->tab_xy = NULL;
117 (*et)->tab_qxyz = NULL;
122 real do_ewald(FILE *log, bool bVerbose,
123 t_inputrec *ir,
124 rvec x[], rvec f[],
125 real chargeA[], real chargeB[],
126 rvec box,
127 t_commrec *cr, int natoms,
128 matrix lrvir, real ewaldcoeff,
129 real lambda, real *dvdlambda,
130 ewald_tab_t et)
132 real factor=-1.0/(4*ewaldcoeff*ewaldcoeff);
133 real scaleRecip =4.0*M_PI/(box[XX]*box[YY]*box[ZZ])*ONE_4PI_EPS0/ir->epsilon_r; /* 1/(Vol*e0) */
134 real *charge,energy_AB[2],energy;
135 rvec lll;
136 int lowiy,lowiz,ix,iy,iz,n,q;
137 real tmp,cs,ss,ak,akv,mx,my,mz,m2,scale;
138 bool bFreeEnergy;
140 if (cr != NULL)
142 if (cr->nnodes > 1 || cr->nthreads>1)
143 gmx_fatal(FARGS,"No parallel Ewald. Use PME instead.\n");
147 if (!et->eir) /* allocate if we need to */
149 snew(et->eir,et->kmax);
150 for(n=0;n<et->kmax;n++)
151 snew(et->eir[n],natoms);
152 snew(et->tab_xy,natoms);
153 snew(et->tab_qxyz,natoms);
156 bFreeEnergy = (ir->efep != efepNO);
158 clear_mat(lrvir);
160 calc_lll(box,lll);
161 /* make tables for the structure factor parts */
162 tabulate_eir(natoms,x,et->kmax,et->eir,lll);
164 for(q=0; q<(bFreeEnergy ? 2 : 1); q++) {
165 if (!bFreeEnergy) {
166 charge = chargeA;
167 scale = 1.0;
168 } else if (q==0) {
169 charge = chargeA;
170 scale = 1.0 - lambda;
171 } else {
172 charge = chargeB;
173 scale = lambda;
175 lowiy=0;
176 lowiz=1;
177 energy_AB[q]=0;
178 for(ix=0;ix<et->nx;ix++) {
179 mx=ix*lll[XX];
180 for(iy=lowiy;iy<et->ny;iy++) {
181 my=iy*lll[YY];
182 if(iy>=0)
183 for(n=0;n<natoms;n++)
184 et->tab_xy[n]=cmul(et->eir[ix][n][XX],et->eir[iy][n][YY]);
185 else
186 for(n=0;n<natoms;n++)
187 et->tab_xy[n]=conjmul(et->eir[ix][n][XX],et->eir[-iy][n][YY]);
188 for(iz=lowiz;iz<et->nz;iz++) {
189 mz=iz*lll[ZZ];
190 m2=mx*mx+my*my+mz*mz;
191 ak=exp(m2*factor)/m2;
192 akv=2.0*ak*(1.0/m2-factor);
193 if(iz>=0)
194 for(n=0;n<natoms;n++)
195 et->tab_qxyz[n]=rcmul(charge[n],cmul(et->tab_xy[n],
196 et->eir[iz][n][ZZ]));
197 else
198 for(n=0;n<natoms;n++)
199 et->tab_qxyz[n]=rcmul(charge[n],conjmul(et->tab_xy[n],
200 et->eir[-iz][n][ZZ]));
202 cs=ss=0;
203 for(n=0;n<natoms;n++) {
204 cs+=et->tab_qxyz[n].re;
205 ss+=et->tab_qxyz[n].im;
207 energy_AB[q]+=ak*(cs*cs+ss*ss);
208 tmp=scale*akv*(cs*cs+ss*ss);
209 lrvir[XX][XX]-=tmp*mx*mx;
210 lrvir[XX][YY]-=tmp*mx*my;
211 lrvir[XX][ZZ]-=tmp*mx*mz;
212 lrvir[YY][YY]-=tmp*my*my;
213 lrvir[YY][ZZ]-=tmp*my*mz;
214 lrvir[ZZ][ZZ]-=tmp*mz*mz;
215 for(n=0;n<natoms;n++) {
216 /*tmp=scale*ak*(cs*tab_qxyz[n].im-ss*tab_qxyz[n].re);*/
217 tmp=scale*ak*(cs*et->tab_qxyz[n].im-ss*et->tab_qxyz[n].re);
218 f[n][XX]+=tmp*mx*2*scaleRecip;
219 f[n][YY]+=tmp*my*2*scaleRecip;
220 f[n][ZZ]+=tmp*mz*2*scaleRecip;
221 #if 0
222 f[n][XX]+=tmp*mx;
223 f[n][YY]+=tmp*my;
224 f[n][ZZ]+=tmp*mz;
225 #endif
227 lowiz=1-et->nz;
229 lowiy=1-et->ny;
234 if (!bFreeEnergy) {
235 energy = energy_AB[0];
236 } else {
237 energy = (1.0 - lambda)*energy_AB[0] + lambda*energy_AB[1];
238 *dvdlambda += scaleRecip*(energy_AB[1] - energy_AB[0]);
241 lrvir[XX][XX]=-0.5*scaleRecip*(lrvir[XX][XX]+energy);
242 lrvir[XX][YY]=-0.5*scaleRecip*(lrvir[XX][YY]);
243 lrvir[XX][ZZ]=-0.5*scaleRecip*(lrvir[XX][ZZ]);
244 lrvir[YY][YY]=-0.5*scaleRecip*(lrvir[YY][YY]+energy);
245 lrvir[YY][ZZ]=-0.5*scaleRecip*(lrvir[YY][ZZ]);
246 lrvir[ZZ][ZZ]=-0.5*scaleRecip*(lrvir[ZZ][ZZ]+energy);
248 lrvir[YY][XX]=lrvir[XX][YY];
249 lrvir[ZZ][XX]=lrvir[XX][ZZ];
250 lrvir[ZZ][YY]=lrvir[YY][ZZ];
252 energy*=scaleRecip;
254 return energy;