forked from m3g/packmol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setsizes.f90
363 lines (298 loc) · 9.59 KB
/
setsizes.f90
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
!
! Written by Leandro Martínez, 2009-2011.
! Copyright (c) 2009-2018, Leandro Martínez, Jose Mario Martinez,
! Ernesto G. Birgin.
!
! Subroutine that sets the sizes of all allocatable arrays
!
subroutine setsizes()
use sizes
use compute_data
use input
use usegencan
use flashsort
implicit none
integer :: i, ival, ilast, iline, itype
integer :: ioerr
integer :: strlength
character(len=strl) :: record, word, blank, alltospace
logical :: inside_structure
! Instructions on how to run packmol
write(*,*) ' Packmol must be run with: packmol < inputfile.inp '
write(*,*)
write(*,*) ' Userguide at: http://m3g.iqm.unicamp.br/packmol '
write(*,*)
! Getting input lines from the input file
write(*,*) ' Reading input file... (Control-C aborts)'
do i = 1, strl
blank(i:i) = ' '
end do
nlines = 0
maxkeywords = 0
ntype = 0
do
read(5,str_format,iostat=ioerr) record
! Replace any strange blank character by spaces
record = alltospace(record)
if ( ioerr /= 0 ) exit
! Remove comments
i = 0
do while( i < strl )
i = i + 1
if ( record(i:i) == '#' ) exit
end do
i = i - 1
if ( i > 0 ) then
record = record(1:i)//blank(i+1:strl)
else
cycle
end if
if ( strlength(record) < 1 ) cycle
record = trim(record)
!
! Convert file name paths with spaces to single strings
!
! check for quotes and replace spaces by @
call parse_spaces(record)
! Number of lines of the input file
nlines = nlines + 1
! Check the number of keywords in this line
i = 0
ival = 0
do while(i < strl)
i = i + 1
ilast = i
do while(record(i:i) > ' ' .and. i < strl)
i = i + 1
end do
if(i > ilast) then
ival = ival + 1
maxkeywords = max(maxkeywords,ival)
end if
end do
end do
rewind(5)
allocate(inputfile(nlines),keyword(nlines,maxkeywords))
! Read input to inputfile array
iline = 0
do
read(5,str_format,iostat=ioerr) record
if ( ioerr /= 0 ) exit
! Convert all strange blank characters to spaces
record = alltospace(record)
call parse_spaces(record)
! Remove comments
i = 0
do while( i < strl )
i = i + 1
if ( record(i:i) == '#' ) exit
end do
i = i - 1
if ( i > 0 ) then
record = record(1:i)//blank(i+1:strl)
else
cycle
end if
if ( strlength(record) < 1 ) cycle
iline = iline + 1
inputfile(iline) = record
end do
! Read all keywods into keyword array
call getkeywords()
! Checking the filetype of coordinate files (default is pdb)
tinker = .false.
pdb = .false.
xyz = .false.
moldy = .false.
fbins = dsqrt(3.d0)
do i = 1, nlines
if(keyword(i,1).eq.'filetype') then
if(keyword(i,2).eq.'tinker') tinker = .true.
if(keyword(i,2).eq.'pdb') pdb = .true.
if(keyword(i,2).eq.'xyz') xyz = .true.
if(keyword(i,2).eq.'moldy') moldy = .true.
end if
if(keyword(i,1).eq.'fbins') then
record = keyword(i,2)
read(record,*,iostat=ioerr) fbins
if ( ioerr /= 0 ) then
write(*,*) ' ERROR: Invalid value for fbins. '
stop
end if
end if
end do
if(.not.pdb.and..not.tinker.and..not.xyz.and..not.moldy) then
pdb = .true.
write(*,*)
write(*,*)' WARNING: File type not (correctly?) specified, using PDB'
end if
! Getting the number of different types of molecules
ntype = 0
do iline = 1, nlines
if ( keyword(iline,1) == "structure" ) then
ntype = ntype + 1
if ( keyword(iline,2) == "none" ) then
write(*,*) ' ERROR: structure without filename. '
write(*,*) ' The syntax must be, for example: structure water.pdb '
stop
end if
end if
end do
allocate(nmols(ntype),natoms(ntype),idfirst(ntype),constrain_rot(ntype,3),&
rot_bound(ntype,3,2),dmax(ntype),&
cmxmin(ntype),cmymin(ntype),cmzmin(ntype),&
cmxmax(ntype),cmymax(ntype),cmzmax(ntype),&
comptype(ntype),compsafe(ntype),&
restart_from(0:ntype),restart_to(0:ntype),&
nloop_type(ntype),nloop0_type(ntype))
! Reading the number of molecules of each type, and the number of atoms
! of each molecule type
itype = 0
inside_structure = .false.
do iline = 1, nlines
if ( keyword(iline,1) == "structure" ) then
inside_structure = .true.
itype = itype + 1
natoms(itype) = 0
nmols(itype) = 0
nloop_type(itype) = 0
nloop0_type(itype) = 0
! Read the number of atoms of this type of molecule
open(10,file=keyword(iline,2),status='old',iostat=ioerr)
if( ioerr /= 0 ) call failopen(keyword(iline,2))
if ( pdb ) then
do
read(10,str_format,iostat=ioerr) record
if ( ioerr /= 0 ) exit
if ( record(1:4) == "ATOM" .or. record(1:6) == "HETATM" ) then
natoms(itype) = natoms(itype) + 1
end if
end do
end if
if ( tinker ) then
do
read(10,*,iostat=ioerr) i
if ( ioerr /= 0 ) cycle
natoms(itype) = i
exit
end do
end if
if ( xyz ) then
read(10,*,iostat=ioerr) i
if ( ioerr == 0 ) natoms(itype) = i
end if
if ( moldy ) then
read(10,*,iostat=ioerr) word, i
if ( ioerr == 0 ) natoms(itype) = i
end if
close(10)
if ( natoms(itype) == 0 ) then
write(*,*) ' ERROR: Could not read any atom from file: ', &
trim(adjustl(keyword(iline,2)))
end if
end if
if ( keyword(iline,1) == "end" .and. &
keyword(iline,2) == "structure" ) inside_structure = .false.
! Read number of molecules for each type
if ( keyword(iline,1) == "number" ) then
read(keyword(iline,2),*,iostat=ioerr) nmols(itype)
if ( ioerr /= 0 ) then
write(*,*) ' ERROR: Error reading number of molecules of type ', itype
stop
end if
if ( nmols(itype) < 1 ) then
write(*,*) ' ERROR: Number of molecules of type ', itype, ' set to less than 1 '
stop
end if
end if
! Read the (optional) number of gencan loops for this molecule
if ( keyword(iline,1) == "nloop" ) then
if ( inside_structure ) then
read(keyword(iline,2),*,iostat=ioerr) nloop_type(itype)
if ( ioerr /= 0 ) then
write(*,*) ' ERROR: Error reading number of loops of type ', itype
stop
end if
if ( nloop_type(itype) < 1 ) then
write(*,*) ' ERROR: Number of loops of type ', itype, ' set to less than 1 '
stop
end if
end if
end if
! Read the (optional) number of gencan loops for initial setup for this molecule
if ( keyword(iline,1) == "nloop0" ) then
if ( inside_structure ) then
read(keyword(iline,2),*,iostat=ioerr) nloop0_type(itype)
if ( ioerr /= 0 ) then
write(*,*) ' ERROR: Error reading number of loops-0 of type ', itype
stop
end if
if ( nloop0_type(itype) < 1 ) then
write(*,*) ' ERROR: Number of loops-0 of type ', itype, ' set to less than 1 '
stop
end if
end if
end if
end do
do itype = 1, ntype
if ( nmols(itype) == 0 ) then
write(*,*) ' Warning: Number of molecules not set for type '&
,itype,': assuming 1 '
nmols(itype) = 1
end if
end do
! Total number of atoms and molecules
ntotat = 0
ntotmol = 0
do itype = 1, ntype
ntotat = ntotat + nmols(itype)*natoms(itype)
ntotmol = ntotmol + nmols(itype)
end do
! The number of variables of the problem
nn = ntotmol*6
! The number of bins of the linked cell method in each direction
nbp = int((fbins*dble(ntotat))**(1.d0/3.d0)) + 1
! Allocate arrays depending on nbp parameter
allocate(latomfirst(0:nbp+1,0:nbp+1,0:nbp+1),&
latomfix(0:nbp+1,0:nbp+1,0:nbp+1),&
hasfree(0:nbp+1,0:nbp+1,0:nbp+1),&
lboxnext((nbp+2)**3))
! Checking the total number of restrictions defined
i = 0
do iline = 1, nlines
if ( keyword(iline,1) == 'fixed' .or. &
keyword(iline,1) == 'inside' .or. &
keyword(iline,1) == 'outside' .or. &
keyword(iline,1) == 'over' .or. &
keyword(iline,1) == 'above' .or. &
keyword(iline,1) == 'below' .or. &
keyword(iline,1) == 'constrain_rotation' ) then
i = i + 1
end if
end do
maxrest = i
mrperatom = i
! Allocate arrays depending on ntotat, nn, maxrest, and mrperatom
allocate(nratom(ntotat),iratom(ntotat,mrperatom),ibmol(ntotat),&
ibtype(ntotat),xcart(ntotat,3),coor(ntotat,3),&
radius(ntotat),radius_ini(ntotat),fscale(ntotat),&
use_short_radius(ntotat), short_radius(ntotat), short_radius_scale(ntotat),&
gxcar(ntotat,3),&
latomnext(ntotat),&
fdist_atom(ntotat), frest_atom(ntotat),&
fmol(ntotat),radiuswork(ntotat),&
fixedatom(ntotat))
allocate(ityperest(maxrest),restpars(maxrest,9))
allocate(xmol(nn))
! Allocate other arrays used for input and output data
allocate(nconnect(ntotat,9),maxcon(ntotat),&
amass(ntotat),charge(ntotat),ele(ntotat))
allocate(irestline(maxrest),linestrut(ntype,2),resnumbers(ntype),&
input_itype(ntype),changechains(ntype),chain(ntype),&
fixedoninput(ntype),pdbfile(ntype),name(ntype),&
segid(ntype),maxmove(ntype),connect(ntype))
! Allocate vectors for flashsort
allocate(indflash(ntotat),lflash(ntotat))
! Allocate arrays for GENCAN
allocate(l(nn),u(nn),wd(8*nn),wi(nn),g(nn))
end subroutine setsizes