-
Notifications
You must be signed in to change notification settings - Fork 0
/
ewald.cc
184 lines (154 loc) · 4.85 KB
/
ewald.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Simple implementation of Ewald Summation
Shane Fogerty, July 2017
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include "ewald.hpp"
#define PI 3.14159265358979323846264338328
using namespace std;
//TODO: load params from argument of script, or input deck?
//Define params
double alpha = 8.0;
double L = 1.0; //System length, assume cubic
double rcutoff = 0.49;//direct space cutoff less than 0.5*L
#define kmax 40
//Define params related to lattice
#define np 8 //Number of particles (cube)
double eps = 1.0; //Dielectric constant
//---------Set up arrays--------
double x[np], y[np], z[np]; //Particle coords
double q[np]; //Particle charges
static double expsum_re[2*kmax+1][2*kmax+1][2*kmax+1];
static double expsum_im[2*kmax+1][2*kmax+1][2*kmax+1];
static double G[kmax+1][kmax+1][kmax+1];
//-----------------------------
int main(void) {
//-----Set up NaCl lattice-----
//Should load this from input args or input deck
int i,j,k;
double sp = 0.5*L;//spacing
double E = 0.0;//total energy
double Er,Ek,Es;//energy components
int dimx,dimy,dimz;//lattice dimensions
dimx=dimy=dimz=2;//for a 2x2x2 cube of 8 particles
//NaCl lattice alternating +1 and -1 charges in box
for (i=0; i<dimx; i++) {
for (j=0; j<dimy; j++) {
for (k=0; k<dimz; k++) {
x[i+j*dimy+k*dimy*dimz] = i*sp;
y[i+j*dimy+k*dimy*dimz] = j*sp;
z[i+j*dimy+k*dimy*dimz] = k*sp;
q[i+j*dimy+k*dimy*dimz] = pow(-1.0,i+j*(dimy+1.0)+k*(dimy+1.0)*(dimz+1.0));
}
}
}
//-----------------------------
Er = realComponent(q,x,y,z);
cout << Er;
cout << " real component \n";
Ek = recipComponent(q,x,y,z);
cout << Ek;
cout << " k-space component \n";
Es = selfComponent(q);
cout << -Es;
cout << " self component \n";
E += Er + Ek - Es;
cout << E;
cout << " total energy \n";
cout << L*E/np;
cout << " Madelung \n";
return 0;
}
//TODO:Use this
void Setup_Ewald(double len, int numparticles, double realcutoff,
int kspacecutoff, double valuealpha)
{
}
double realComponent(double *q, double *x, double *y, double *z)
{
double xdist,ydist,zdist,r,sum;
int i,j;
sum=0.0;
//TODO: could loop through this faster I think
for (i=0; i<np-1; i++) {
for (j=i+1; j<np; j++) {
xdist = x[i] - x[j];
ydist = y[i] - y[j];
zdist = z[i] - z[j];
r = sqrt(xdist*xdist + ydist*ydist + zdist*zdist);
if (r <= rcutoff) {
sum += q[i]*q[j]*erfc(alpha*r)/r;
}
}
}
return 0.5*sum;
}
double recipComponent(double *q, double *x, double *y, double *z)
{
double inf, kr, ksum = 0.0;
double realpart, imagpart, inside, cosine, sine;
int i,kx,ky,kz;
//Zero out expsum
for (kx=-kmax; kx<=kmax; kx++){
for (ky=-kmax; ky<=kmax; ky++){
for (kz=-kmax; kz<=kmax; kz++){
expsum_re[kx+kmax][ky+kmax][kz+kmax] = 0.0;
expsum_im[kx+kmax][ky+kmax][kz+kmax] = 0.0;
}
}
}
influenceFunction();//compute influence funtion
//expsum is cos(2*PI*k.r/L) + i*sin(2*PI*k.r/L)
for (kx=-kmax; kx<=kmax; kx++) {
for (ky=-kmax; ky<=kmax; ky++) {
for (kz=-kmax; kz<=kmax; kz++) {
kr = (kx*kx) + (ky*ky) + (kz*kz);
for (i=0; i<np; i++) {
if (kr < kmax*kmax) {
inside = 2.0 * PI * (kx*x[i] + ky*y[i] + kz*z[i]) / L;
expsum_re[kx+kmax][ky+kmax][kz+kmax] += q[i]*cos(inside);
expsum_im[kx+kmax][ky+kmax][kz+kmax] -= q[i]*sin(inside);
}
}
for (i=0; i<np; i++) {
if (kr < kmax*kmax) {
inf = G[abs(kx)][abs(ky)][abs(kz)];//influence function
inside = 2.0*PI*(kx*x[i]+ky*y[i]+kz*z[i])/L;
cosine = cos(inside);
sine = sin(inside);
realpart = expsum_re[kx+kmax][ky+kmax][kz+kmax] * cosine;
imagpart = expsum_im[kx+kmax][ky+kmax][kz+kmax] * sine;
ksum += q[i]*inf*(realpart-imagpart);
}
}
}
}
}
return (ksum*L/(4.0*PI));
}
double selfComponent(double *q)
{
int i;
double sum = 0.0;
for (i=0; i<np; i++) {
sum += q[i]*q[i];
}
return alpha*sum/sqrt(PI);
}
void influenceFunction(void) {
int kx,ky,kz;
double kr, f1, f2;
f1 = 2.0/(L*L);
f2 = PI/(alpha*L);
for (kx=0; kx<=kmax; kx++){
for (ky=0; ky<=kmax; ky++){
for (kz=0; kz<=kmax; kz++){
kr = (kx*kx) + (ky*ky) + (kz*kz);
G[kx][ky][kz] = f1*(exp(-(f2*f2)*kr))/kr;
}
}
}
G[0][0][0] = 0.0;
}