forked from dquigley533/mc_water_ls_mw
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.f90
More file actions
399 lines (358 loc) · 13.5 KB
/
init.f90
File metadata and controls
399 lines (358 loc) · 13.5 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
! -*- mode: F90 ; mode: font-lock ; column-number-mode: true ; vc-back-end: Git -*-
!=============================================================================!
! I N I T !
!=============================================================================!
! Allocates initial positions either randomly or by reading an Xmol file. !
! note that no checking is made to determine if the co-ordinates read from !
! file are consistent with the water model specified. !
!=============================================================================!
module init
use constants, only : dp !Minimal useage where practical
implicit none !Impose strong typing
private !Everything is private ...
!---------------------------------------------------------------------------!
! P u b l i c R o u t i n e s !
!---------------------------------------------------------------------------!
public :: read_xmol
!public :: continuation
!---------------------------------------------------------------------------!
! P u b l i c V a r i a b l e s !
!---------------------------------------------------------------------------!
!---------------------------------------------------------------------------!
! P r i v a t e R o u t i n e s !
!---------------------------------------------------------------------------!
!---------------------------------------------------------------------------!
! P r i v a t e V a r i a b l e s !
!---------------------------------------------------------------------------!
contains
subroutine read_xmol()
!------------------------------------------------------------------------------!
! Reads initial co-ordinates into the water data structures. Note that only !
! postions of the oxygen and hydrogen atoms are read. Any massless charge sites!
! are assigned in this routine. No checking of O-H distances or HOH angles is !
! implemented. !
!------------------------------------------------------------------------------!
! D.Quigley September 2006 !
!------------------------------------------------------------------------------!
use constants, only : ang_to_bohr
use comms, only : myrank,comms_bcastint,comms_bcastreal
use userparams, only : nwater,model_type,num_lattices
use model, only : ljr,ref_ljr,hmatrix, &
recip_matrix,ref_hmatrix,ref_recipmatrix
use util, only : util_recipmatrix
implicit none
integer,parameter :: xml = 40 ! Unit number for xmol files
character(1) :: elemid ! chemical symbol for the current atom
character(3) :: lattstr
integer :: ierr,imol,tmpN,ils
real(kind=dp),dimension(3) :: o_vec
do ils = 1,num_lattices
! open the correct xmol file for the current lattice number
write(lattstr,'(I3.3)')ils
if (myrank==0) open(unit=xml,file='input'//lattstr//'.xmol',status='old',iostat=ierr)
call comms_bcastint(ierr,1)
if (ierr/=0) stop 'Error opening input coords for reading'
! check the number of atoms in the xmol file is consistent with
! that specified in the input file
if (myrank==0) read(xml,*)tmpN
call comms_bcastint(tmpN,1)
if (tmpN/=nwater) stop 'Error wrong number of atoms in input.xmol'
! read the matrix of cell vectors
if (myrank==0) read(xml,*)hmatrix(:,:,ils)
call comms_bcastreal(hmatrix(1,1,ils),9)
!write(*,'(3F15.6)')hmatrix(:,1,ils)
!write(*,'(3F15.6)')hmatrix(:,2,ils)
!write(*,'(3F15.6)')hmatrix(:,3,ils)
hmatrix(:,:,ils) = hmatrix(:,:,ils)*ang_to_bohr
call util_recipmatrix(hmatrix(:,:,ils),recip_matrix(:,:,ils))
ref_hmatrix(:,:,ils) = hmatrix(:,:,ils)
ref_recipmatrix(:,:,ils) = recip_matrix(:,:,ils)
do imol = 1,Nwater
! assume arrangement of OHH
if (myrank==0) read(xml,*)elemid,o_vec
call comms_bcastreal(o_vec(1),3)
select case(trim(model_type))
case ('mW')
ljr(:,1,imol,ils) = o_vec(:)*ang_to_bohr
! .. relative to this configuration
ref_ljr(:,:,imol,ils) = ljr(:,:,imol,ils)
case default
write(*,*)'input from file not yet supported for selected'
write(*,*)'water model'
stop
end select
end do
end do
if (myrank==0) close(xml)
end subroutine read_xmol
!!$ subroutine renumber()
!!$
!!$ !------------------------------------------------------------------------------!
!!$ ! Attempts to renumber the molecules in lattice 2 such that the orientation !
!!$ ! of a particular molecule matches the corresponding molecule in lattice 1 as !
!!$ ! closely as possible. i.e. we minimise the difference between two vectors by !
!!$ ! re-ordering the second one only. !
!!$ !------------------------------------------------------------------------------!
!!$ ! D.Quigley October 2009 !
!!$ !------------------------------------------------------------------------------!
!!$ use constants, only : pi,bohr_to_ang,Pi
!!$ use model, only : ref_ljr,ref_csr,ljr,csr,ljspm,cspm,hmatrix
!!$ use userparams, only : nwater
!!$ use random, only : random_uniform_random
!!$ use io, only : io_hist_append
!!$ implicit none
!!$ integer :: iwater,jwater,iloop,ilj,ics,ilev=1
!!$
!!$ real(kind=dp) :: sumdiff,sumdiff_old,gsumdiff_old,mix
!!$ real(kind=dp),allocatable,dimension(:,:) :: tmp_csr,tmp_ljr
!!$ integer,dimension(2) :: ierr
!!$
!!$
!!$ real(kind=dp),dimension(3,3) :: a
!!$ real(kind=dp) :: tq0,tq1,tq2,tq3,u1,u2,u3,norm,sq1,sq2,sq3
!!$
!!$
!!$ !return
!!$
!!$ !call io_hist_append(0)
!!$
!!$ ierr = 0
!!$ allocate(tmp_ljr(1:3,1:ljspm),stat=ierr(1))
!!$ allocate(tmp_csr(1:3,1:cspm),stat=ierr(2))
!!$ if (any(ierr/=0)) stop 'Error allocating memory in renumber'
!!$
!!$ sumdiff_old = difference()
!!$ gsumdiff_old = sumdiff_old
!!$
!!$ !do
!!$
!!$ ! generate random quaternion
!!$ !u1 = random_uniform_random()
!!$ !u2 = random_uniform_random()
!!$ !u3 = random_uniform_random()
!!$
!!$ !tq0 = sqrt(1.0_dp-u1)*sin(2.0_dp*pi*u2)
!!$ !tq1 = sqrt(1.0_dp-u1)*cos(2.0_dp*pi*u2)
!!$ !tq2 = sqrt(u1)*sin(2.0_dp*pi*u3)
!!$ !tq3 = sqrt(u1)*cos(2.0_dp*pi*u3)
!!$
!!$ ! normalise
!!$ !norm = 1.0_dp/sqrt(tq0*tq0+tq1*tq1+tq2*tq2+tq3*tq3)
!!$ !tq0 = tq0*norm
!!$ !tq1 = tq1*norm
!!$ !tq2 = tq2*norm
!!$ !tq3 = tq3*norm
!!$
!!$ !mix = 0.05_dp/sqrt(real(ilev,kind=dp))
!!$
!!$ !tq0 = (1.0_dp-mix)*1.0 + mix*tq0
!!$ !tq1 = (1.0_dp-mix)*0.0 + mix*tq1
!!$ !tq2 = (1.0_dp-mix)*0.0 + mix*tq2
!!$ !tq3 = (1.0_dp-mix)*0.0 + mix*tq3
!!$
!!$
!!$ tq0 = cos(0.125*Pi)
!!$ tq1 = 1.0_dp*sin(0.125*Pi)
!!$ tq2 = 0.0_dp
!!$ tq3 = 0.0_dp
!!$
!!$ ! normalise
!!$ norm = 1.0_dp/sqrt(tq0*tq0+tq1*tq1+tq2*tq2+tq3*tq3)
!!$ tq0 = tq0*norm
!!$ tq1 = tq1*norm
!!$ tq2 = tq2*norm
!!$ tq3 = tq3*norm
!!$
!!$ ! build rotation matrix
!!$ a(1,1) = tq0**2 + tq1**2 - tq2**2 - tq3**2
!!$ a(1,2) = 2.0_dp * ( tq1*tq2 - tq0*tq3 )
!!$ a(1,3) = 2.0_dp * ( tq1*tq3 + tq0*tq2 )
!!$ a(2,1) = 2.0_dp * ( tq1*tq2 + tq0*tq3 )
!!$ a(2,2) = tq0**2 - tq1**2 + tq2**2 - tq3**2
!!$ a(2,3) = 2.0_dp * ( tq2*tq3 - tq0*tq1 )
!!$ a(3,1) = 2.0_dp * ( tq1*tq3 - tq0*tq2 )
!!$ a(3,2) = 2.0_dp * ( tq2*tq3 + tq0*tq1 )
!!$ a(3,3) = tq0**2 - tq1**2 - tq2**2 + tq3**2
!!$
!!$ !write(*,'(3F15.6)')a(1,:)
!!$ !write(*,'(3F15.6)')a(2,:)
!!$ !write(*,'(3F15.6)')a(3,:)
!!$
!!$ ! apply to everything in cell 2, including the cell itself
!!$ hmatrix(:,1,2) = matmul(a,hmatrix(:,1,2))
!!$ hmatrix(:,2,2) = matmul(a,hmatrix(:,2,2))
!!$ hmatrix(:,3,2) = matmul(a,hmatrix(:,3,2))
!!$
!!$ !write(*,'(3F15.6)')hmatrix(1,:,2)*bohr_to_ang
!!$ !write(*,'(3F15.6)')hmatrix(2,:,2)*bohr_to_ang
!!$ !write(*,'(3F15.6)')hmatrix(3,:,2)*bohr_to_ang
!!$
!!$
!!$ do iwater = 1,nwater
!!$ do ilj = 1,ljspm
!!$ ljr(:,ilj,iwater,2) = matmul(a,ljr(:,ilj,iwater,2))
!!$ ref_ljr(:,ilj,iwater,2) = ljr(:,ilj,iwater,2) !- ljr(:,1,iwater,2)
!!$ end do
!!$ do ics = 1,cspm
!!$ csr(:,ics,iwater,2) = matmul(a,csr(:,ics,iwater,2))
!!$ ref_csr(:,ics,iwater,2) = csr(:,ics,iwater,2) !- ljr(:,1,iwater,2)
!!$ end do
!!$ end do
!!$
!!$ !call io_hist_append(0)
!!$
!!$ do iloop = 1,100000
!!$
!!$ ! pick two molecules at random
!!$ iwater = int(random_uniform_random()*real(nwater,kind=dp))+1
!!$ jwater = iwater
!!$ do while (jwater==iwater)
!!$ jwater = int(random_uniform_random()*real(nwater,kind=dp))+1
!!$ end do
!!$
!!$ ! swap reference positions
!!$ tmp_ljr(:,:) = ref_ljr(:,:,iwater,2)
!!$ ref_ljr(:,:,iwater,2) = ref_ljr(:,:,jwater,2)
!!$ ref_ljr(:,:,jwater,2) = tmp_ljr(:,:)
!!$
!!$ tmp_csr(:,:) = ref_csr(:,:,iwater,2)
!!$ ref_csr(:,:,iwater,2) = ref_csr(:,:,jwater,2)
!!$ ref_csr(:,:,jwater,2) = tmp_csr(:,:)
!!$
!!$ sumdiff = difference()
!!$
!!$ if ( sumdiff > sumdiff_old ) then
!!$
!!$ ! reject
!!$ ref_ljr(:,:,jwater,2) = ref_ljr(:,:,iwater,2)
!!$ ref_ljr(:,:,iwater,2) = tmp_ljr(:,:)
!!$ ref_csr(:,:,jwater,2) = ref_csr(:,:,iwater,2)
!!$ ref_csr(:,:,iwater,2) = tmp_csr(:,:)
!!$
!!$ else
!!$
!!$ !write(*,'("Swapped ",I5," with ",I5)')iwater,jwater
!!$ sumdiff_old = sumdiff
!!$ print *,iloop,sumdiff
!!$
!!$ tmp_ljr(:,:) = ljr(:,:,iwater,2)
!!$ ljr(:,:,iwater,2) = ljr(:,:,jwater,2)
!!$ ljr(:,:,jwater,2) = tmp_ljr(:,:)
!!$
!!$ tmp_csr(:,:) = csr(:,:,iwater,2)
!!$ csr(:,:,iwater,2) = csr(:,:,jwater,2)
!!$ csr(:,:,jwater,2) = tmp_csr(:,:)
!!$
!!$
!!$ end if
!!$
!!$ end do
!!$
!!$ !do iwater = 1,nwater
!!$
!!$ !do ilj = 1,ljspm
!!$ ! ref_ljr(:,ilj,iwater,2) = ljr(:,ilj,iwater,2)
!!$ !end do
!!$ !do ics = 1,cspm
!!$ ! ref_csr(:,ics,iwater,2) = csr(:,ics,iwater,2)
!!$ !end do
!!$
!!$ !end do
!!$
!!$
!!$! write(*,'("Best permutation at current cell rotation gives sumdiff = ",F15.6)')sumdiff
!!$ !if (sumdiff<gsumdiff_old) then
!!$
!!$ ! gsumdiff_old = sumdiff
!!$ ! write(*,'("Best permutation at current cell rotation gives sumdiff = ",F15.6)')sumdiff
!!$ !call io_hist_append(ilev)
!!$
!!$ ! ilev = ilev + 1
!!$
!!$ !else
!!$
!!$ ! tq1 = -tq1
!!$ ! tq2 = -tq2
!!$ !tq3 = -tq3
!!$
!!$ ! build rotation matrix
!!$ !a(1,1) = tq0**2 + tq1**2 - tq2**2 - tq3**2
!!$ !a(1,2) = 2.0_dp * ( tq1*tq2 - tq0*tq3 )
!!$ !a(1,3) = 2.0_dp * ( tq1*tq3 + tq0*tq2 )
!!$ !a(2,1) = 2.0_dp * ( tq1*tq2 + tq0*tq3 )
!!$ !a(2,2) = tq0**2 - tq1**2 + tq2**2 - tq3**2
!!$ !a(2,3) = 2.0_dp * ( tq2*tq3 - tq0*tq1 )
!!$ !a(3,1) = 2.0_dp * ( tq1*tq3 - tq0*tq2 )
!!$ !a(3,2) = 2.0_dp * ( tq2*tq3 + tq0*tq1 )
!!$ !a(3,3) = tq0**2 - tq1**2 - tq2**2 + tq3**2
!!$
!!$ !write(*,'(3F15.6)')a(1,:)
!!$ !write(*,'(3F15.6)')a(2,:)
!!$ !write(*,'(3F15.6)')a(3,:)
!!$
!!$ ! apply to everything in cell 2, including the cell itself
!!$ !hmatrix(:,1,2) = matmul(a,hmatrix(:,1,2))
!!$ !hmatrix(:,2,2) = matmul(a,hmatrix(:,2,2))
!!$ !hmatrix(:,3,2) = matmul(a,hmatrix(:,3,2))
!!$
!!$ !write(*,'(3F15.6)')hmatrix(1,:,2)*bohr_to_ang
!!$ !write(*,'(3F15.6)')hmatrix(2,:,2)*bohr_to_ang
!!$ !write(*,'(3F15.6)')hmatrix(3,:,2)*bohr_to_ang
!!$
!!$
!!$ !do iwater = 1,nwater
!!$ ! do ilj = 1,ljspm
!!$ ! ljr(:,ilj,iwater,2) = matmul(a,ljr(:,ilj,iwater,2))
!!$ ! ref_ljr(:,ilj,iwater,2) = ljr(:,ilj,iwater,2) - ljr(:,1,iwater,2)
!!$ ! end do
!!$ ! do ics = 1,cspm
!!$ ! csr(:,ics,iwater,2) = matmul(a,csr(:,ics,iwater,2))
!!$ ! ref_csr(:,ics,iwater,2) = csr(:,ics,iwater,2) - ljr(:,1,iwater,2)
!!$ ! end do
!!$ !end do
!!$
!!$
!!$ !end if
!!$
!!$
!!$ ! end do
!!$
!!$ deallocate(tmp_ljr,tmp_csr,stat=ierr(1))
!!$ if(ierr(1)/=0) stop 'Error releasing memory in renumber'
!!$
!!$
!!$ contains
!!$
!!$ real(kind=dp) function difference()
!!$
!!$ implicit none
!!$ real(kind=dp),dimension(3) :: vec,vec1
!!$
!!$ integer :: ilj,ics,imol
!!$
!!$ difference = 0.0_dp
!!$
!!$ do imol = 1,nwater
!!$
!!$ !do ilj = 1,ljspm
!!$ ! vec = ref_ljr(:,ilj,imol,1) - ref_ljr(:,ilj,imol,2)
!!$ ! difference = difference + dot_product(vec,vec)
!!$ !end do
!!$
!!$ !do ics = 1,cspm
!!$ ! vec = ref_csr(:,ics,imol,1) - ref_csr(:,ics,imol,2)
!!$ ! difference = difference + dot_product(vec,vec)
!!$ !end do
!!$
!!$ vec = ref_ljr(:,1,imol,1) - ref_csr(:,1,imol,1)
!!$ vec1 = ref_ljr(:,1,imol,2) - ref_csr(:,1,imol,2)
!!$
!!$ difference = difference + abs(dot_product(vec,vec1))
!!$
!!$ end do
!!$
!!$ end function difference
!!$
!!$
!!$ end subroutine renumber
end module init