Debian Codegraph @

tests/mbd_api_tests.F90

raw
1 ! This Source Code Form is subject to the terms of the Mozilla Public
2 ! License, v. 2.0. If a copy of the MPL was not distributed with this
3 ! file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 program mbd_api_tests
5
6 use mbd_constants
7 use mbd, only: mbd_input_t, mbd_calc_t
8 #ifdef WITH_MPI
9 use mbd_mpi
10 #endif
11
12 implicit none
13
14 logical :: failed
15 character(len=50) :: test_name
16
17 #ifdef WITH_MPI
18 integer :: err
19
20 call MPI_INIT(err)
21 #endif
22
23 call get_command_argument(1, test_name)
24 failed = .false.
25 call test(test_name)
26
27 #ifdef WITH_MPI
28 call MPI_FINALIZE(err)
29 #endif
30
31 if (failed) stop 1
32
33 contains
34
35 subroutine test(test_name)
36 character(len=*), intent(in) :: test_name
37
38 type(mbd_input_t) :: inp
39 type(mbd_calc_t) :: calc
40 real(dp) :: energy
41 real(dp), allocatable :: gradients(:, :)
42 integer :: code
43 character(200) :: origin, msg
44 #ifdef WITH_MPI
45 integer :: mpi_size, mpi_rank, color
46 # ifdef WITH_MPIF08
47 type(MPI_Comm) :: subcomm
48 # else
49 integer :: subcomm
50 # endif
51 #endif
52
53 inp%atom_types = ['Ar', 'Ar']
54 inp%coords = reshape([0d0, 0d0, 0d0, 0d0, 0d0, 4d0 * ang], [3, 2])
55 inp%log_level = MBD_LOG_LVL_DEBUG
56 inp%xc = 'pbe'
57 select case (test_name)
58 case ('exception')
59 inp%xc = 'xxx'
60 call calc%init(inp)
61 call calc%get_exception(code, origin, msg)
62 if (code /= MBD_EXC_DAMPING) failed = .true.
63 case ('energy')
64 call calc%init(inp)
65 call calc%update_vdw_params_custom([11d0, 11d0], [63.525d0, 63.525d0], [3.55d0, 3.55d0])
66 call calc%evaluate_vdw_method(energy)
67 call check(energy, -2.4329456747018696d-4, 1d-10)
68 case ('energy_subcomm')
69 #ifdef WITH_MPI
70 # ifdef WITH_SCALAPACK
71 call MPI_COMM_SIZE(MPI_COMM_WORLD, mpi_size, err)
72 call MPI_COMM_RANK(MPI_COMM_WORLD, mpi_rank, err)
73 if (mpi_size > 1) then
74 ! Split the world into two halves so that each subcommunicator is a
75 ! strict subset of MPI_COMM_WORLD, exercising the communicator ->
76 ! BLACS context translation and grid sizing on a sub-communicator.
77 if (mpi_rank < mpi_size / 2) then
78 color = 0
79 else
80 color = 1
81 end if
82 call MPI_COMM_SPLIT(MPI_COMM_WORLD, color, mpi_rank, subcomm, err)
83 # ifdef WITH_MPIF08
84 inp%comm = subcomm%mpi_val
85 # else
86 inp%comm = subcomm
87 # endif
88 end if
89 inp%parallel_mode = 'atoms'
90 call calc%init(inp)
91 call calc%update_vdw_params_custom([11d0, 11d0], [63.525d0, 63.525d0], [3.55d0, 3.55d0])
92 call calc%evaluate_vdw_method(energy)
93 call check(energy, -2.4329456747018696d-4, 1d-10)
94 if (mpi_size > 1) call MPI_COMM_FREE(subcomm, err)
95 # else
96 call calc%init(inp)
97 call calc%update_vdw_params_custom([11d0, 11d0], [63.525d0, 63.525d0], [3.55d0, 3.55d0])
98 call calc%evaluate_vdw_method(energy)
99 call check(energy, -2.4329456747018696d-4, 1d-10)
100 # endif
101 #else
102 call calc%init(inp)
103 call calc%update_vdw_params_custom([11d0, 11d0], [63.525d0, 63.525d0], [3.55d0, 3.55d0])
104 call calc%evaluate_vdw_method(energy)
105 call check(energy, -2.4329456747018696d-4, 1d-10)
106 #endif
107 case ('gradients')
108 call calc%init(inp)
109 call calc%update_vdw_params_custom([11d0, 11d0], [63.525d0, 63.525d0], [3.55d0, 3.55d0])
110 call calc%evaluate_vdw_method(energy)
111 allocate (gradients(3, 2))
112 call calc%get_gradients(gradients)
113 call check(sum(abs(gradients)), 2.3279742219399908d-4, 1d-10)
114 case ('energy_from_ratios')
115 call calc%init(inp)
116 call calc%update_vdw_params_from_ratios([1d0, 1d0])
117 call calc%evaluate_vdw_method(energy)
118 call check(energy, -0.0002462647623815428d0, 1d-10)
119 case ('hirshfeld_gradients')
120 inp%calculate_vdw_params_gradients = .true.
121 call calc%init(inp)
122 call calc%update_vdw_params_from_ratios([1d0, 1d0])
123 call calc%evaluate_vdw_method(energy)
124 allocate (gradients(2, 1))
125 call calc%get_vdw_params_ratios_gradients(gradients(:, 1))
126 call check(sum(abs(gradients)), 2.9686524938125897d-004, 1d-10)
127 case ('ts_gradients')
128 inp%method = 'ts'
129 call calc%init(inp)
130 call calc%update_vdw_params_from_ratios([1d0, 1d0])
131 call calc%evaluate_vdw_method(energy)
132 allocate (gradients(3, 2))
133 call calc%get_gradients(gradients)
134 call check(sum(abs(gradients)), 3.8405275467790542d-4, 1d-10)
135 end select
136 call calc%destroy()
137 end subroutine
138
139 subroutine check(val, ref, rel)
140 real(dp), intent(in) :: val, ref, rel
141
142 if (.not. abs((val - ref) / ref) < rel) then
143 failed = .true.
144 end if
145 end subroutine
146
147 end program