-
Notifications
You must be signed in to change notification settings - Fork 103
/
config_io_module.f90
351 lines (278 loc) · 15.3 KB
/
config_io_module.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
! config_io_module.f90
! Routines for atomic/molecular configuration I/O
MODULE config_io_module
!------------------------------------------------------------------------------------------------!
! This software was written in 2016/17 !
! by Michael P. Allen <[email protected]>/<[email protected]> !
! and Dominic J. Tildesley <[email protected]> ("the authors"), !
! to accompany the book "Computer Simulation of Liquids", second edition, 2017 ("the text"), !
! published by Oxford University Press ("the publishers"). !
! !
! LICENCE !
! Creative Commons CC0 Public Domain Dedication. !
! To the extent possible under law, the authors have dedicated all copyright and related !
! and neighboring rights to this software to the PUBLIC domain worldwide. !
! This software is distributed without any warranty. !
! You should have received a copy of the CC0 Public Domain Dedication along with this software. !
! If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. !
! !
! DISCLAIMER !
! The authors and publishers make no warranties about the software, and disclaim liability !
! for all uses of the software, to the fullest extent permitted by applicable law. !
! The authors and publishers do not recommend use of this software for any purpose. !
! It is made freely available, solely to clarify points made in the text. When using or citing !
! the software, you should not imply endorsement by the authors or publishers. !
!------------------------------------------------------------------------------------------------!
USE, INTRINSIC :: iso_fortran_env, ONLY : error_unit, iostat_end, iostat_eor
IMPLICIT NONE
PRIVATE
! Public routines
PUBLIC :: read_cnf_atoms, write_cnf_atoms, read_cnf_mols, write_cnf_mols
CONTAINS
SUBROUTINE read_cnf_atoms ( filename, n, box, r, v ) ! Read in atomic configuration
IMPLICIT NONE
CHARACTER(len=*), INTENT(in) :: filename ! Supplied filename
INTEGER, INTENT(inout) :: n ! Number of atoms
REAL, INTENT(out) :: box ! Simulation box length (assumed cubic)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: r ! Atomic positions (3,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: v ! Atomic velocities (3,n)
! The first call of this routine is just to get n and box, used to allocate arrays
! The second call attempts to read in the atomic positions and optionally velocities
INTEGER :: cnf_unit, ioerr, i
! Open given filename, will terminate on any errors
OPEN ( newunit=cnf_unit, file=filename, status='old', action='read', iostat=ioerr )
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error opening ', filename, ioerr
STOP 'Error in read_cnf_atoms'
END IF
! Read number of atoms from first record
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) n
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading n from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_atoms'
END IF
! Read box length from second record
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) box
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading box from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_atoms'
END IF
! Expected format is one record per atom containing either r(:,i), v(:,i) or just r(:,i)
IF ( PRESENT ( r ) ) THEN
IF ( n > SIZE ( r, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of r ', n, SIZE ( r, dim=2 )
STOP 'Error in read_cnf_atoms'
END IF
IF ( PRESENT ( v ) ) THEN
IF ( n > SIZE ( v, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of v ', n, SIZE ( v, dim=2 )
STOP 'Error in read_cnf_atoms'
END IF
! Read positions, velocities
DO i = 1, n
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) r(:,i), v(:,i)
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading r, v from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_atoms'
END IF
END DO
ELSE
! Read positions
DO i = 1, n
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) r(:,i)
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading r from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_atoms'
END IF
END DO
END IF
END IF
CLOSE ( unit=cnf_unit )
END SUBROUTINE read_cnf_atoms
SUBROUTINE write_cnf_atoms ( filename, n, box, r, v ) ! Write out atomic configuration
IMPLICIT NONE
CHARACTER(len=*), INTENT(in) :: filename ! Supplied filename
INTEGER, INTENT(in) :: n ! Number of atoms
REAL, INTENT(in) :: box ! Simulation box length (assumed cubic)
REAL, DIMENSION(:,:), INTENT(in) :: r ! Atomic positions (3,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(in) :: v ! Atomic velocities
INTEGER :: cnf_unit, ioerr, i
! Open given filename, replacing it if it already exists, will terminate on any errors
OPEN ( newunit=cnf_unit, file=filename, status='replace', iostat=ioerr )
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error opening ', filename, ioerr
STOP 'Error in write_cnf_atoms'
END IF
! Write number of atoms to first record, box length to second record
WRITE ( unit=cnf_unit, fmt='(i15)' ) n
WRITE ( unit=cnf_unit, fmt='(f15.8)') box
IF ( n > SIZE ( r, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of r ', n, SIZE ( r, dim=2 )
STOP 'Error in write_cnf_atoms'
END IF
IF ( PRESENT ( v ) ) THEN
IF ( n > SIZE ( v, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of v ', n, SIZE ( v, dim=2 )
STOP 'Error in write_cnf_atoms'
END IF
! Write positions, velocities
DO i = 1, n
WRITE ( unit=cnf_unit, fmt='(*(f15.10))' ) r(:,i), v(:,i)
END DO
ELSE
! Write positions
DO i = 1, n
WRITE ( unit=cnf_unit, fmt='(*(f15.10))' ) r(:,i)
END DO
END IF
CLOSE ( unit=cnf_unit )
END SUBROUTINE write_cnf_atoms
SUBROUTINE read_cnf_mols ( filename, n, box, r, e, v, w ) ! Read in molecular configuration
IMPLICIT NONE
CHARACTER(len=*), INTENT(in) :: filename ! Supplied filename
INTEGER, INTENT(inout) :: n ! Number of molecules
REAL, INTENT(out) :: box ! Simulation box length (assumed cubic)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: r ! Molecular positions (3,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: e ! Orientation vectors (3,n) or quaternions (4,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: v ! Molecular velocities (3,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: w ! Angular velocities/momenta (3,n)
! The first call of this routine is just to get n and box, used to allocate arrays
! The second call attempts to read in the molecular positions, orientations
! and optionally velocities, angular velocities/momenta
INTEGER :: cnf_unit, ioerr, i
! Open given filename, will terminate on any errors
OPEN ( newunit=cnf_unit, file=filename, status='old', action='read', iostat=ioerr )
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error opening ', filename, ioerr
STOP 'Error opening file in read_cnf_mols'
END IF
! Read number of molecules from first record
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) n
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading n from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_mols'
END IF
! Read box length from second record
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) box
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading box from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_mols'
END IF
! Expected format is one line per atom containing either r(:,i), e(:,i), v(:,i), w(:,i) or just r(:,i), e(:,i)
! The first dimension of the e array can be 3 elements (vector) or 4 elements (quaternion)
IF ( PRESENT ( r ) ) THEN
IF ( .NOT. PRESENT ( e ) ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'r and e arguments must be present together'
STOP 'Error in read_cnf_mols'
END IF
IF ( n > SIZE ( r, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of r ', n, SIZE ( r, dim=2 )
STOP 'Error in read_cnf_mols'
END IF
IF ( n > SIZE ( e, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of e ', n, SIZE ( e, dim=2 )
STOP 'Error in read_cnf_mols'
END IF
IF ( PRESENT ( v ) ) THEN
IF ( .NOT. PRESENT ( w ) ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'v and w arguments must be present together'
STOP 'Error in read_cnf_mols'
END IF
IF ( n > SIZE ( v, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of v ', n, SIZE ( v, dim=2 )
STOP 'Error in read_cnf_mols'
END IF
IF ( n > SIZE ( w, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of w ', n, SIZE ( w, dim=2 )
STOP 'Error in read_cnf_mols'
END IF
! Read positions, orientation vectors or quaternions, velocities, angular velocities/momenta
DO i = 1, n
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) r(:,i), e(:,i), v(:,i), w(:,i)
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading r, e, v, w from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_mols'
END IF
END DO
ELSE
! Read positions, orientation vectors or quaternions
DO i = 1, n
READ ( unit=cnf_unit, fmt=*, iostat=ioerr ) r(:,i), e(:,i)
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error reading r, e from ', filename, ioerr
IF ( ioerr == iostat_eor ) WRITE ( unit=error_unit, fmt='(a)') 'End of record'
IF ( ioerr == iostat_end ) WRITE ( unit=error_unit, fmt='(a)') 'End of file'
STOP 'Error in read_cnf_mols'
END IF
END DO
END IF
END IF
CLOSE ( unit=cnf_unit )
END SUBROUTINE read_cnf_mols
SUBROUTINE write_cnf_mols ( filename, n, box, r, e, v, w ) ! Write out molecular configuration
IMPLICIT NONE
CHARACTER(len=*), INTENT(in) :: filename ! Supplied filename
INTEGER, INTENT(in) :: n ! Number of molecules
REAL, INTENT(in) :: box ! Simulation box length (assumed cubic)
REAL, DIMENSION(:,:), INTENT(in) :: r ! Molecular positions (3,n)
REAL, DIMENSION(:,:), INTENT(in) :: e ! Orientation vectors (3,n) or quaternions (0:3,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(in) :: v ! Molecular velocities (3,n)
REAL, DIMENSION(:,:), OPTIONAL, INTENT(in) :: w ! Angular velocities/momenta (3,n)
INTEGER :: cnf_unit, ioerr, i
! Open given filename, replacing it if it already exists, will terminate on any errors
OPEN ( newunit=cnf_unit, file=filename, status='replace', iostat=ioerr )
IF ( ioerr /= 0 ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'Error opening ', filename, ioerr
STOP 'Error in write_cnf_mols'
END IF
! Write number of molecules to first record, box length to second record
WRITE(cnf_unit,'(i15)' ) n
WRITE(cnf_unit,'(f15.8)') box
IF ( n > SIZE ( r, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of r ', n, SIZE ( r, dim=2 )
STOP 'Error in write_cnf_mols'
END IF
IF ( n > SIZE ( e, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of e ', n, SIZE ( e, dim=2 )
STOP 'Error in write_cnf_mols'
END IF
IF ( PRESENT ( v ) ) THEN
IF ( .NOT. PRESENT ( w ) ) THEN
WRITE ( unit=error_unit, fmt='(a,a,i15)') 'v and w arguments must be present together'
STOP 'Error in write_cnf_mols'
END IF
IF ( n > SIZE ( v, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of v ', n, SIZE ( v, dim=2 )
STOP 'Error in write_cnf_mols'
END IF
IF ( n > SIZE ( w, dim=2 ) ) THEN
WRITE ( unit=error_unit, fmt='(a,2i15)') 'Error in size of w ', n, SIZE ( w, dim=2 )
STOP 'Error in write_cnf_mols'
END IF
! Write positions, orientation vectors or quaternions, velocities, angular velocities/momenta
DO i = 1, n
WRITE ( unit=cnf_unit, fmt='(*(f15.10))') r(:,i), e(:,i), v(:,i), w(:,i)
END DO
ELSE
! Write positions, orientation vectors or quaternions
DO i = 1, n
WRITE ( unit=cnf_unit, fmt='(*(f15.10))') r(:,i), e(:,i)
END DO
END IF
CLOSE ( unit=cnf_unit )
END SUBROUTINE write_cnf_mols
END MODULE config_io_module