File: test_read_xyz.f90

package info (click to toggle)
mctc-lib 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: f90: 12,894; python: 23; ansic: 15; makefile: 2
file content (450 lines) | stat: -rw-r--r-- 14,011 bytes parent folder | download | duplicates (2)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
! This file is part of mctc-lib.
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
!     http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.

module test_read_xyz
   use mctc_env_testing, only : new_unittest, unittest_type, error_type, check
   use mctc_io_read_xyz
   use mctc_io_structure
   implicit none
   private

   public :: collect_read_xyz


contains


!> Collect all exported unit tests
subroutine collect_read_xyz(testsuite)

   !> Collection of tests
   type(unittest_type), allocatable, intent(out) :: testsuite(:)

   testsuite = [ &
      & new_unittest("valid1-xyz", test_valid1_xyz), &
      & new_unittest("valid2-xyz", test_valid2_xyz), &
      & new_unittest("valid3-xyz", test_valid3_xyz), &
      & new_unittest("valid4-xyz", test_valid4_xyz), &
      & new_unittest("valid5-xyz", test_valid5_xyz), &
      & new_unittest("invalid1-xyz", test_invalid1_xyz, should_fail=.true.), &
      & new_unittest("invalid2-xyz", test_invalid2_xyz, should_fail=.true.), &
      & new_unittest("invalid3-xyz", test_invalid3_xyz, should_fail=.true.), &
      & new_unittest("invalid4-xyz", test_invalid4_xyz, should_fail=.true.), &
      & new_unittest("invalid5-xyz", test_invalid5_xyz, should_fail=.true.), &
      & new_unittest("invalid6-xyz", test_invalid6_xyz, should_fail=.true.), &
      & new_unittest("invalid7-xyz", test_invalid7_xyz, should_fail=.true.) &
      & ]

end subroutine collect_read_xyz


subroutine test_valid1_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "9", &
      "WATER27, (H2O)3", &
      "O     1.1847029    1.1150792   -0.0344641 ", &
      "H     0.4939088    0.9563767    0.6340089 ", &
      "H     2.0242676    1.0811246    0.4301417 ", &
      "O    -1.1469443    0.0697649    1.1470196 ", &
      "H    -1.2798308   -0.5232169    1.8902833 ", &
      "H    -1.0641398   -0.4956693    0.3569250 ", &
      "O    -0.1633508   -1.0289346   -1.2401808 ", &
      "H     0.4914771   -0.3248733   -1.0784838 ", &
      "H    -0.5400907   -0.8496512   -2.1052499 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

   call check(error, allocated(struc%comment), "Comment line should be preserved")
   if (allocated(error)) return
   call check(error, struc%comment, "WATER27, (H2O)3")
   if (allocated(error)) return
   call check(error, struc%nat, 9, "Number of atoms does not match")
   if (allocated(error)) return
   call check(error, struc%nid, 2, "Number of species does not match")
   if (allocated(error)) return

end subroutine test_valid1_xyz


subroutine test_valid2_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "24", &
      "", &
      "C          1.07317        0.04885       -0.07573", &
      "N          2.51365        0.01256       -0.07580", &
      "C*         3.35199        1.09592       -0.07533", &
      "N          4.61898        0.73028       -0.07549", &
      "C*         4.57907       -0.63144       -0.07531", &
      "C          3.30131       -1.10256       -0.07524", &
      "C          2.98068       -2.48687       -0.07377", &
      "18O        1.82530       -2.90038       -0.07577", &
      "N          4.11440       -3.30433       -0.06936", &
      "C*         5.45174       -2.85618       -0.07235", &
      "O          6.38934       -3.65965       -0.07232", &
      "N          5.66240       -1.47682       -0.07487", &
      "C          7.00947       -0.93648       -0.07524", &
      "C          3.92063       -4.74093       -0.06158", &
      "D          0.73398        1.08786       -0.07503", &
      "D          0.71239       -0.45698        0.82335", &
      "D          0.71240       -0.45580       -0.97549", &
      "H          2.99301        2.11762       -0.07478", &
      "H          7.76531       -1.72634       -0.07591", &
      "H          7.14864       -0.32182        0.81969", &
      "H          7.14802       -0.32076       -0.96953", &
      "H          2.86501       -5.02316       -0.05833", &
      "H          4.40233       -5.15920        0.82837", &
      "H          4.40017       -5.16929       -0.94780"
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

   call check(error, .not.allocated(struc%comment), "Empty comment line should not be saved")
   if (allocated(error)) return
   call check(error, struc%nat, 24, "Number of atoms does not match")
   if (allocated(error)) return
   call check(error, struc%nid, 7, "Number of species does not match")
   if (allocated(error)) return

end subroutine test_valid2_xyz


subroutine test_valid3_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "24", &
      "#", &
      "c  1.07317  0.04885 -0.07573 -0.05445590", &
      "n  2.51365  0.01256 -0.07580 -0.00457526", &
      "c  3.35199  1.09592 -0.07533  0.08391889", &
      "n  4.61898  0.73028 -0.07549 -0.27870751", &
      "c  4.57907 -0.63144 -0.07531  0.11914924", &
      "c  3.30131 -1.10256 -0.07524 -0.02621044", &
      "c  2.98068 -2.48687 -0.07377  0.26115960", &
      "o  1.82530 -2.90038 -0.07577 -0.44071824", &
      "n  4.11440 -3.30433 -0.06936 -0.10804747", &
      "c  5.45174 -2.85618 -0.07235  0.30411699", &
      "o  6.38934 -3.65965 -0.07232 -0.44083760", &
      "n  5.66240 -1.47682 -0.07487 -0.07457706", &
      "c  7.00947 -0.93648 -0.07524 -0.04790859", &
      "c  3.92063 -4.74093 -0.06158 -0.03738239", &
      "h  0.73398  1.08786 -0.07503  0.06457802", &
      "h  0.71239 -0.45698  0.82335  0.08293905", &
      "h  0.71240 -0.45580 -0.97549  0.08296802", &
      "h  2.99301  2.11762 -0.07478  0.05698136", &
      "h  7.76531 -1.72634 -0.07591  0.09025556", &
      "h  7.14864 -0.32182  0.81969  0.07152988", &
      "h  7.14802 -0.32076 -0.96953  0.07159003", &
      "h  2.86501 -5.02316 -0.05833  0.08590674", &
      "h  4.40233 -5.15920  0.82837  0.06906357", &
      "h  4.40017 -5.16929 -0.94780  0.06926350"
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

   call check(error, struc%nat, 24, "Number of atoms does not match")
   if (allocated(error)) return
   call check(error, struc%nid, 4, "Number of species does not match")
   if (allocated(error)) return

end subroutine test_valid3_xyz


subroutine test_valid4_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "3", &
      "WATER27, H2O", &
      "O     1.1847029    1.1150792   -0.0344641 ", &
      "H     0.4939088    0.9563767    0.6340089 ", &
      "H     2.0242676    1.0811246    0.4301417 ", &
      "3", &
      "WATER27, H2O", &
      "O    -1.1469443    0.0697649    1.1470196 ", &
      "H    -1.2798308   -0.5232169    1.8902833 ", &
      "H    -1.0641398   -0.4956693    0.3569250 ", &
      "3", &
      "WATER27, H2O", &
      "O    -0.1633508   -1.0289346   -1.2401808 ", &
      "H     0.4914771   -0.3248733   -1.0784838 ", &
      "H    -0.5400907   -0.8496512   -2.1052499 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   if (.not.allocated(error)) then
      call read_xyz(struc, unit, error)
   end if
   close(unit)
   if (allocated(error)) return

   call check(error, struc%nat, 3, "Number of atoms does not match")
   if (allocated(error)) return
   call check(error, struc%nid, 2, "Number of species does not match")
   if (allocated(error)) return

end subroutine test_valid4_xyz


subroutine test_valid5_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "3", &
      "WATER27, H2O", &
      "8     1.1847029    1.1150792   -0.0344641 ", &
      "1     0.4939088    0.9563767    0.6340089 ", &
      "1     2.0242676    1.0811246    0.4301417 ", &
      "3", &
      "WATER27, H2O", &
      "8    -1.1469443    0.0697649    1.1470196 ", &
      "1    -1.2798308   -0.5232169    1.8902833 ", &
      "1    -1.0641398   -0.4956693    0.3569250 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   if (.not.allocated(error)) then
      call read_xyz(struc, unit, error)
   end if
   close(unit)
   if (allocated(error)) return

   call check(error, struc%nat, 3, "Number of atoms does not match")
   if (allocated(error)) return
   call check(error, struc%nid, 2, "Number of species does not match")
   if (allocated(error)) return

end subroutine test_valid5_xyz


subroutine test_invalid1_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "9", &
      "WATER27, (H2O)3", &
      "O     1.1847029    1.1150792   -0.0344641 ", &
      "H     0.4939088    0.9563767    0.6340089 ", &
      "H     2.0242676    1.0811246    0.4301417 ", &
      "O    -1.1469443    0.0697649    1.1470196 ", &
      "H    -1.2798308   -0.5232169    1.8902833 ", &
      "H    -1.0641398   -0.4956693    0.3569250 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

end subroutine test_invalid1_xyz


subroutine test_invalid2_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "", &
      "WATER27, (H2O)3", &
      "O     1.1847029    1.1150792   -0.0344641 ", &
      "H     0.4939088    0.9563767    0.6340089 ", &
      "H     2.0242676    1.0811246    0.4301417 ", &
      "O    -1.1469443    0.0697649    1.1470196 ", &
      "H    -1.2798308   -0.5232169    1.8902833 ", &
      "H    -1.0641398   -0.4956693    0.3569250 ", &
      "O    -0.1633508   -1.0289346   -1.2401808 ", &
      "H     0.4914771   -0.3248733   -1.0784838 ", &
      "H    -0.5400907   -0.8496512   -2.1052499 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

end subroutine test_invalid2_xyz


subroutine test_invalid3_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      & "120"
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

end subroutine test_invalid3_xyz


subroutine test_invalid4_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "-3", &
      "H2O", &
      "O     1.1847029    1.1150792   -0.0344641 ", &
      "H     0.4939088    0.9563767    0.6340089 ", &
      "H     2.0242676    1.0811246    0.4301417 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

end subroutine test_invalid4_xyz


subroutine test_invalid5_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "3", &
      "H2O", &
      "****o   1.1847029    1.1150792   -0.0344641 ", &
      "****h   0.4939088    0.9563767    0.6340089 ", &
      "****h   2.0242676    1.0811246    0.4301417 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

end subroutine test_invalid5_xyz


subroutine test_invalid6_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "3", &
      "H2O", &
      "1.1847029    1.1150792   -0.0344641      O", &
      "0.4939088    0.9563767    0.6340089      H", &
      "2.0242676    1.0811246    0.4301417      H"
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)
   if (allocated(error)) return

end subroutine test_invalid6_xyz


subroutine test_invalid7_xyz(error)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   type(structure_type) :: struc
   integer :: unit

   open(status='scratch', newunit=unit)
   write(unit, '(a)') &
      "nine", &
      "WATER27, (H2O)3", &
      "O     1.1847029    1.1150792   -0.0344641 ", &
      "H     0.4939088    0.9563767    0.6340089 ", &
      "H     2.0242676    1.0811246    0.4301417 ", &
      "O    -1.1469443    0.0697649    1.1470196 ", &
      "H    -1.2798308   -0.5232169    1.8902833 ", &
      "H    -1.0641398   -0.4956693    0.3569250 ", &
      "O    -0.1633508   -1.0289346   -1.2401808 ", &
      "H     0.4914771   -0.3248733   -1.0784838 ", &
      "H    -0.5400907   -0.8496512   -2.1052499 "
   rewind(unit)

   call read_xyz(struc, unit, error)
   close(unit)

end subroutine test_invalid7_xyz


end module test_read_xyz