-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathewald.f
387 lines (258 loc) · 13 KB
/
ewald.f
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
********************************************************************************
** FICHE F.22. ROUTINES TO PERFORM THE EWALD SUM **
** This FORTRAN code is intended to illustrate points made in the text. **
** To our knowledge it works correctly. However it is the responsibility of **
** the user to test it, if it is to be used in a research application. **
********************************************************************************
C *******************************************************************
C ** REAL-SPACE AND RECIPROCAL-SPACE PARTS OF EWALD SUM FOR IONS. **
C ** **
C ** REFERENCES: **
C ** **
C ** WOODCOCK AND SINGER, TRANS. FARADAY SOC. 67, 12, 1971. **
C ** DE LEEUW ET AL., PROC. ROY. SOC. A 373, 27, 1980. **
C ** HEYES, J. CHEM. PHYS. 74, 1924, 1981. **
C ** SEE ALSO FINCHAM, MDIONS, CCP5 PROGRAM LIBRARY. **
C ** **
C ** ROUTINES SUPPLIED: **
C ** **
C ** SUBROUTINE SETUP ( KAPPA ) **
C ** SETS UP THE WAVEVECTORS FOR USE IN THE EWALD SUM **
C ** SUBROUTINE RWALD ( KAPPA, VR ) **
C ** CALCULATES THE R-SPACE PART OF THE SUM **
C ** SUBROUTINE KWALD ( KAPPA, VK ) **
C ** CALCULATES THE K-SPACE PART OF THE SUM **
C ** REAL FUNCTION ERFC ( X ) **
C ** RETURNS THE COMPLEMENTARY ERROR FUNCTION **
C ** **
C ** PRINCIPAL VARIABLES: **
C ** **
C ** INTEGER TOTK THE TOTAL NUMBER OF K-VECTORS STORED **
C ** INTEGER MAXK MAXIMUM POSSIBLE NUMBER OF K-VECTORS **
C ** INTEGER KMAX MAX INTEGER COMPONENT OF THE K-VECTOR **
C ** INTEGER KSQMAX MAX SQUARE MOD OF THE K-VECTOR REQUIRED **
C ** REAL VR ENERGY FROM R-SPACE SUM **
C ** REAL VK ENERGY FROM K-SPACE SUM **
C ** REAL KVEC(MAXK) ARRAY USED TO STORE K-VECTORS **
C ** REAL KAPPA WIDTH OF CANCELLING DISTRIBUTION **
C ** **
C ** USAGE: **
C ** **
C ** SETUP IS CALLED ONCE AT THE BEGINNING OF THE SIMULATION **
C ** TO CALCULATE ALL THE K-VECTORS REQUIRED IN THE EWALD SUM. **
C ** THESE VECTORS ARE USED THROUGHOUT THE SIMULATION IN THE **
C ** SUBROUTINE KWALD TO CALCULATE THE K-SPACE CONTRIBUTION TO THE **
C ** POTENTIAL ENERGY AT EACH CONFIGURATION. THE SELF TERM IS **
C ** SUBTRACTED FROM THE K-SPACE CONTRIBUTION IN KWALD. **
C ** THE SURFACE TERM FOR SIMULATIONS IN VACUUM IS NOT INCLUDED. **
C ** ROUTINE RWALD RETURNS THE R-SPACE CONTRIBUTION TO THE EWALD **
C ** SUM AND IS CALLED FOR EACH CONFIGURATION IN THE SIMULATION. **
C ** A CUBIC BOX AND UNIT BOX LENGTH ARE ASSUMED THROUGHOUT. **
C *******************************************************************
SUBROUTINE SETUP ( KAPPA )
COMMON / BLOCK2 / KVEC
C *******************************************************************
C ** ROUTINE TO SET UP THE WAVE-VECTORS FOR THE EWALD SUM. **
C ** **
C ** THE WAVEVECTORS MUST FIT INTO A BOX OF UNIT LENGTH. **
C ** IN THIS EXAMPLE WE ALLOW A MAXIMUM OF 1000 WAVEVECTORS. **
C *******************************************************************
INTEGER MAXK
PARAMETER ( MAXK = 1000 )
REAL KVEC(MAXK), KAPPA
INTEGER KMAX, KSQMAX, KSQ, KX, KY, KZ, TOTK
REAL TWOPI, B, RKX, RKY, RKZ, RKSQ
PARAMETER ( KMAX = 5, KSQMAX = 27 , TWOPI = 6.2831853 )
C *******************************************************************
B = 1.0 / 4.0 / KAPPA / KAPPA
C ** LOOP OVER K-VECTORS. NOTE KX IS NON-NEGATIVE **
TOTK = 0
DO 100 KX = 0, KMAX
RKX = TWOPI * REAL ( KX )
DO 99 KY = -KMAX, KMAX
RKY = TWOPI * REAL ( KY )
DO 98 KZ = -KMAX, KMAX
RKZ = TWOPI * REAL ( KZ )
KSQ = KX * KX + KY * KY + KZ * KZ
IF ( ( KSQ .LT. KSQMAX ) .AND. ( KSQ .NE. 0 ) ) THEN
TOTK = TOTK + 1
IF ( TOTK .GT. MAXK ) STOP 'KVEC IS TOO SMALL'
RKSQ = RKX * RKX + RKY * RKY + RKZ * RKZ
KVEC(TOTK) = TWOPI * EXP ( -B * RKSQ ) / RKSQ
ENDIF
98 CONTINUE
99 CONTINUE
100 CONTINUE
WRITE( *, ' ( '' EWALD SUM SETUP COMPLETE '' ) ' )
WRITE( *, ' ( '' NUMBER OF WAVEVECTORS IS '', I5 ) ' ) TOTK
RETURN
END
SUBROUTINE RWALD ( KAPPA, VR )
COMMON / BLOCK1 / RX, RY, RZ, Z
C *******************************************************************
C ** CALCULATES R-SPACE PART OF POTENTIAL ENERGY BY EWALD METHOD. **
C ** **
C ** PRINCIPAL VARIABLES: **
C ** **
C ** INTEGER N NUMBER OF IONS **
C ** REAL RX(N),RY(N),RZ(N) POSITIONS OF IONS **
C ** REAL Z(N) IONIC CHARGES **
C ** REAL VR R-SPACE POTENTIAL ENERGY **
C ** **
C ** ROUTINE REFERENCED: **
C ** **
C ** REAL FUNCTION ERFC ( X ) **
C ** RETURNS THE COMPLEMENTARY ERROR FUNCTION **
C *******************************************************************
INTEGER N
PARAMETER ( N = 216 )
REAL RX(N), RY(N), RZ(N), Z(N)
REAL KAPPA, VR
REAL RXI, RYI, RZI, ZI, RXIJ, RYIJ, RZIJ
REAL RIJSQ, RIJ, KRIJ, ERFC, VIJ
INTEGER I, J
C *******************************************************************
VR = 0.0
DO 100 I = 1, N - 1
RXI = RX(I)
RYI = RY(I)
RZI = RZ(I)
ZI = Z(I)
DO 99 J = I + 1, N
RXIJ = RXI - RX(J)
RYIJ = RYI - RY(J)
RZIJ = RZI - RZ(J)
RXIJ = RXIJ - ANINT ( RXIJ )
RYIJ = RYIJ - ANINT ( RYIJ )
RZIJ = RZIJ - ANINT ( RZIJ )
RIJSQ = RXIJ * RXIJ + RYIJ * RYIJ + RZIJ * RZIJ
RIJ = SQRT ( RIJSQ )
KRIJ = KAPPA * RIJ
VIJ = ZI * Z(J) * ERFC ( KRIJ ) / RIJ
VR = VR + VIJ
99 CONTINUE
100 CONTINUE
RETURN
END
SUBROUTINE KWALD ( KAPPA, VK )
COMMON / BLOCK1 / RX, RY, RZ, Z
COMMON / BLOCK2 / KVEC
C *******************************************************************
C ** CALCULATES K-SPACE PART OF POTENTIAL ENERGY BY EWALD METHOD. **
C ** **
C ** THE SELF TERM IS SUBTRACTED. **
C ** IN ONE COORDINATE DIRECTION (X), SYMMETRY IS USED TO REDUCE **
C ** THE SUM TO INCLUDE ONLY POSITIVE K-VECTORS. **
C ** THE NEGATIVE VECTORS IN THIS DIRECTION ARE INCLUDED BY USE **
C ** OF THE MULTIPLICATIVE VARIABLE 'FACTOR'. **
C ** **
C ** PRINCIPAL VARIABLES: **
C ** **
C ** INTEGER N NUMBER OF IONS **
C ** REAL RX(N),RY(N),RZ(N) POSITIONS OF IONS **
C ** REAL Z(N) IONIC CHARGES **
C ** REAL VK K-SPACE POTENTIAL ENERGY **
C ** REAL VKS SELF PART OF K-SPACE SUM **
C *******************************************************************
INTEGER MAXK, N
PARAMETER ( MAXK = 1000, N = 216 )
REAL KVEC(MAXK), RX(N), RY(N), RZ(N), Z(N)
REAL KAPPA, VK
INTEGER TOTK
INTEGER KMAX, KX, KY, KZ, I, KSQMAX, KSQ
REAL TWOPI, FACTOR, VD, VS, RSQPI
PARAMETER ( KMAX = 5, KSQMAX = 27 )
PARAMETER ( TWOPI = 6.2831853, RSQPI = 0.5641896 )
COMPLEX EIKX(1:N, 0:KMAX)
COMPLEX EIKY(1:N, -KMAX:KMAX)
COMPLEX EIKZ(1:N, -KMAX:KMAX)
COMPLEX EIKR(N), SUM
C *******************************************************************
C ** CONSTRUCT EXP(IK.R) FOR ALL IONS AND K-VECTORS **
C ** CALCULATE KX, KY, KZ = 0 , -1 AND 1 EXPLICITLY **
DO 10 I = 1, N
EIKX(I, 0) = (1.0, 0.0)
EIKY(I, 0) = (1.0, 0.0)
EIKZ(I, 0) = (1.0, 0.0)
EIKX(I, 1) = CMPLX ( COS ( TWOPI * RX(I) ) ,
: SIN ( TWOPI * RX(I) ) )
EIKY(I, 1) = CMPLX ( COS ( TWOPI * RY(I) ) ,
: SIN ( TWOPI * RY(I) ) )
EIKZ(I, 1) = CMPLX ( COS ( TWOPI * RZ(I) ) ,
: SIN ( TWOPI * RZ(I) ) )
EIKY(I, -1) = CONJG ( EIKY(I, 1) )
EIKZ(I, -1) = CONJG ( EIKZ(I, 1) )
10 CONTINUE
C ** CALCULATE REMAINING KX, KY AND KZ BY RECURRENCE **
DO 12 KX = 2, KMAX
DO 11 I = 1, N
EIKX(I, KX) = EIKX(I, KX-1) * EIKX(I, 1)
11 CONTINUE
12 CONTINUE
DO 14 KY = 2, KMAX
DO 13 I = 1, N
EIKY(I, KY) = EIKY(I, KY-1) * EIKY(I, 1)
EIKY(I, -KY) = CONJG ( EIKY(I, KY) )
13 CONTINUE
14 CONTINUE
DO 16 KZ = 2, KMAX
DO 15 I = 1, N
EIKZ(I, KZ) = EIKZ(I, KZ-1) * EIKZ(I, 1)
EIKZ(I, -KZ) = CONJG ( EIKZ(I, KZ) )
15 CONTINUE
16 CONTINUE
C ** SUM OVER ALL VECTORS **
VD = 0.0
TOTK = 0
DO 24 KX = 0, KMAX
IF ( KX .EQ. 0 ) THEN
FACTOR = 1.0
ELSE
FACTOR = 2.0
ENDIF
DO 23 KY = -KMAX, KMAX
DO 22 KZ = -KMAX, KMAX
KSQ = KX * KX + KY * KY + KZ * KZ
IF ( ( KSQ .LT. KSQMAX ) .AND. ( KSQ .NE. 0 ) ) THEN
TOTK = TOTK + 1
SUM = (0.0, 0.0)
DO 21 I = 1, N
EIKR(I) = EIKX(I, KX) * EIKY(I, KY) * EIKZ(I, KZ)
SUM = SUM + Z(I) * EIKR(I)
21 CONTINUE
VD = VD + FACTOR * KVEC(TOTK) * CONJG ( SUM ) * SUM
ENDIF
22 CONTINUE
23 CONTINUE
24 CONTINUE
C ** CALCULATES SELF PART OF K-SPACE SUM **
VS = 0.0
DO 25 I = 1, N
VS = VS + Z(I) * Z(I)
25 CONTINUE
VS = RSQPI * KAPPA * VS
C ** CALCULATE THE TOTAL K-SPACE POTENTIAL **
VK = VD - VS
RETURN
END
REAL FUNCTION ERFC ( X )
C *******************************************************************
C ** APPROXIMATION TO THE COMPLEMENTARY ERROR FUNCTION **
C ** **
C ** REFERENCE: **
C ** **
C ** ABRAMOWITZ AND STEGUN, HANDBOOK OF MATHEMATICAL FUNCTIONS, **
C ** NATIONAL BUREAU OF STANDARDS, FORMULA 7.1.26 **
C *******************************************************************
REAL A1, A2, A3, A4, A5, P
PARAMETER ( A1 = 0.254829592, A2 = -0.284496736 )
PARAMETER ( A3 = 1.421413741, A4 = -1.453152027 )
PARAMETER ( A5 = 1.061405429, P = 0.3275911 )
REAL T, X, XSQ, TP
C *******************************************************************
T = 1.0 / ( 1.0 + P * X )
XSQ = X * X
TP = T * ( A1 + T * ( A2 + T * ( A3 + T * ( A4 + T * A5 ) ) ) )
ERFC = TP * EXP ( -XSQ )
RETURN
END