| 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 |
|
| 5 |
module mbd_utils |
| 6 |
!! Utility types, interfaces, and procedures. |
| 7 |
|
| 8 |
use mbd_constants |
| 9 |
use mbd_gradients, only: grad_t |
| 10 |
#ifdef WITH_MPI |
| 11 |
use mbd_mpi |
| 12 |
#endif |
| 13 |
|
| 14 |
implicit none |
| 15 |
|
| 16 |
private |
| 17 |
public :: tostr, diff3, diff5, lower, diff7, findval, shift_idx, & |
| 18 |
is_true, printer_i, printer |
| 19 |
|
| 20 |
integer, parameter :: i8 = selected_int_kind(18) |
| 21 |
|
| 22 |
interface tostr |
| 23 |
module procedure tostr_int |
| 24 |
module procedure tostr_real |
| 25 |
end interface |
| 26 |
|
| 27 |
interface findval |
| 28 |
module procedure findval_int |
| 29 |
end interface |
| 30 |
|
| 31 |
abstract interface |
| 32 |
subroutine printer_i(str) |
| 33 |
character(len=*), intent(in) :: str |
| 34 |
end subroutine |
| 35 |
end interface |
| 36 |
|
| 37 |
type, public :: logger_t |
| 38 |
integer :: level = MBD_LOG_LVL_WARN |
| 39 |
! TODO cannot use printer() as default because of PGI 19.4 |
| 40 |
procedure(printer_i), nopass, pointer :: printer => null() |
| 41 |
contains |
| 42 |
procedure :: info => logger_info |
| 43 |
procedure :: debug => logger_debug |
| 44 |
procedure :: warn => logger_warn |
| 45 |
procedure :: error => logger_error |
| 46 |
end type |
| 47 |
|
| 48 |
type, public :: exception_t |
| 49 |
!! Represents an exception. |
| 50 |
integer :: code = 0 |
| 51 |
character(50) :: origin = '(unknown)' |
| 52 |
character(150) :: msg = '' |
| 53 |
end type |
| 54 |
|
| 55 |
type, public :: result_t |
| 56 |
!! Stores results from an MBD calculation |
| 57 |
real(dp) :: energy |
| 58 |
type(grad_t) :: dE |
| 59 |
real(dp), allocatable :: mode_eigs(:) |
| 60 |
real(dp), allocatable :: modes(:, :) |
| 61 |
real(dp), allocatable :: rpa_orders(:) |
| 62 |
real(dp), allocatable :: mode_eigs_k(:, :) |
| 63 |
complex(dp), allocatable :: modes_k(:, :, :) |
| 64 |
complex(dp), allocatable :: modes_k_single(:, :) |
| 65 |
real(dp), allocatable :: rpa_orders_k(:, :) |
| 66 |
real(dp), allocatable :: alpha_0(:) |
| 67 |
real(dp), allocatable :: C6(:) |
| 68 |
end type |
| 69 |
|
| 70 |
type, public :: atom_index_t |
| 71 |
!! Maps from atom indexes to positions in matrices. |
| 72 |
integer, allocatable :: i_atom(:) |
| 73 |
integer, allocatable :: j_atom(:) |
| 74 |
integer :: n_atoms |
| 75 |
# ifdef WITH_SCALAPACK |
| 76 |
logical :: parallel |
| 77 |
# endif |
| 78 |
end type |
| 79 |
|
| 80 |
type, public :: clock_t |
| 81 |
!! Used for measuring performance. |
| 82 |
logical :: active = .true. |
| 83 |
integer :: level = 0 |
| 84 |
integer(i8), allocatable :: timestamps(:), counts(:) |
| 85 |
integer, allocatable :: levels(:) |
| 86 |
contains |
| 87 |
procedure :: init => clock_init |
| 88 |
procedure :: clock => clock_clock |
| 89 |
procedure :: print => clock_print |
| 90 |
end type |
| 91 |
|
| 92 |
type, public :: quad_pt_t |
| 93 |
!! Represents a 1D quadrature point |
| 94 |
real(dp) :: val |
| 95 |
real(dp) :: weight |
| 96 |
end type |
| 97 |
|
| 98 |
contains |
| 99 |
|
| 100 |
character(len=50) elemental function tostr_int(k, format) result(s) |
| 101 |
integer, intent(in) :: k |
| 102 |
character(len=*), intent(in), optional :: format |
| 103 |
|
| 104 |
if (present(format)) then |
| 105 |
write (s, format) k |
| 106 |
else |
| 107 |
write (s, "(i20)") k |
| 108 |
end if |
| 109 |
s = adjustl(s) |
| 110 |
end function |
| 111 |
|
| 112 |
character(len=50) elemental function tostr_real(x, format) result(s) |
| 113 |
real(dp), intent(in) :: x |
| 114 |
character(*), intent(in), optional :: format |
| 115 |
|
| 116 |
if (present(format)) then |
| 117 |
write (s, format) x |
| 118 |
else |
| 119 |
write (s, "(g50.17e3)") x |
| 120 |
end if |
| 121 |
s = adjustl(s) |
| 122 |
end function |
| 123 |
|
| 124 |
real(dp) pure function diff3(x, delta) |
| 125 |
real(dp), intent(in) :: x(-1:) |
| 126 |
real(dp), intent(in) :: delta |
| 127 |
|
| 128 |
diff3 = (x(1) - x(-1)) / (2 * delta) |
| 129 |
end function |
| 130 |
|
| 131 |
real(dp) pure function diff5(x, delta) |
| 132 |
real(dp), intent(in) :: x(-2:) |
| 133 |
real(dp), intent(in) :: delta |
| 134 |
|
| 135 |
diff5 = (1.d0 / 12 * x(-2) - 2.d0 / 3 * x(-1) + 2.d0 / 3 * x(1) - 1.d0 / 12 * x(2)) / delta |
| 136 |
end function |
| 137 |
|
| 138 |
real(dp) pure function diff7(x, delta) |
| 139 |
real(dp), intent(in) :: x(-3:) |
| 140 |
real(dp), intent(in) :: delta |
| 141 |
|
| 142 |
diff7 = ( & |
| 143 |
-1.d0 / 60 * x(-3) & |
| 144 |
+ 3.d0 / 20 * x(-2) & |
| 145 |
- 3.d0 / 4 * x(-1) & |
| 146 |
+ 3.d0 / 4 * x(1) & |
| 147 |
- 3.d0 / 20 * x(2) & |
| 148 |
+ 1.d0 / 60 * x(3) & |
| 149 |
) / delta |
| 150 |
end function |
| 151 |
|
| 152 |
pure function lower(str) |
| 153 |
character(len=*), intent(in) :: str |
| 154 |
character(len=len(str)) :: lower |
| 155 |
|
| 156 |
integer :: i |
| 157 |
|
| 158 |
do i = 1, len(str) |
| 159 |
select case (str(i:i)) |
| 160 |
case ('A':'Z') |
| 161 |
lower(i:i) = achar(iachar(str(i:i)) + 32) |
| 162 |
case default |
| 163 |
lower(i:i) = str(i:i) |
| 164 |
end select |
| 165 |
end do |
| 166 |
end function |
| 167 |
|
| 168 |
integer pure function findval_int(array, val) result(idx) |
| 169 |
integer, intent(in) :: array(:), val |
| 170 |
|
| 171 |
integer :: i |
| 172 |
|
| 173 |
idx = 0 |
| 174 |
do i = 1, size(array) |
| 175 |
if (val == array(i)) then |
| 176 |
idx = i |
| 177 |
return |
| 178 |
end if |
| 179 |
end do |
| 180 |
end function |
| 181 |
|
| 182 |
subroutine shift_idx(idx, start, finish) |
| 183 |
integer, intent(inout) :: idx(:) |
| 184 |
integer, intent(in) :: start(:), finish(:) |
| 185 |
|
| 186 |
integer :: i_dim, i |
| 187 |
|
| 188 |
do i_dim = size(idx), 1, -1 |
| 189 |
i = idx(i_dim) + 1 |
| 190 |
if (i <= finish(i_dim)) then |
| 191 |
idx(i_dim) = i |
| 192 |
return |
| 193 |
else |
| 194 |
idx(i_dim) = start(i_dim) |
| 195 |
end if |
| 196 |
end do |
| 197 |
end subroutine |
| 198 |
|
| 199 |
subroutine clock_init(this, n) |
| 200 |
class(clock_t), intent(inout) :: this |
| 201 |
integer, intent(in) :: n |
| 202 |
|
| 203 |
allocate (this%timestamps(n), source=0_i8) |
| 204 |
allocate (this%counts(n), source=0_i8) |
| 205 |
allocate (this%levels(n), source=0) |
| 206 |
end subroutine |
| 207 |
|
| 208 |
subroutine clock_clock(this, id) |
| 209 |
class(clock_t), intent(inout) :: this |
| 210 |
integer, intent(in) :: id |
| 211 |
|
| 212 |
integer(i8) :: cnt, rate, cnt_max |
| 213 |
integer :: id_ |
| 214 |
|
| 215 |
if (.not. this%active) return |
| 216 |
call system_clock(cnt, rate, cnt_max) |
| 217 |
id_ = abs(id) |
| 218 |
if (id > 0) then |
| 219 |
this%timestamps(id_) = this%timestamps(id_) - cnt |
| 220 |
this%levels(id_) = this%level |
| 221 |
this%level = this%level + 1 |
| 222 |
else |
| 223 |
this%timestamps(id_) = this%timestamps(id_) + cnt |
| 224 |
this%counts(id_) = this%counts(id_) + 1 |
| 225 |
this%level = this%level - 1 |
| 226 |
end if |
| 227 |
end subroutine |
| 228 |
|
| 229 |
subroutine clock_print(this) |
| 230 |
class(clock_t), intent(inout) :: this |
| 231 |
|
| 232 |
integer(i8) :: cnt, rate, cnt_max |
| 233 |
integer :: i |
| 234 |
character(len=20) :: label |
| 235 |
|
| 236 |
#ifdef WITH_MPI |
| 237 |
if (mpi_get_rank() /= 0) return |
| 238 |
#endif |
| 239 |
call system_clock(cnt, rate, cnt_max) |
| 240 |
|
| 241 |
print '(A5,A7,A20,A10,A16)', 'id', 'level', 'label', 'count', 'time (s)' |
| 242 |
do i = 1, size(this%counts) |
| 243 |
if (this%counts(i) == 0) cycle |
| 244 |
select case (i) |
| 245 |
case (11); label = 'dipmat real' |
| 246 |
case (12); label = 'dipmat rec' |
| 247 |
case (13); label = 'P_EVR' |
| 248 |
case (14); label = 'mmul' |
| 249 |
case (15); label = 'force contractions' |
| 250 |
case (16); label = 'PDGETRF' |
| 251 |
case (17); label = 'PDGETRI' |
| 252 |
case (18); label = 'ELSI ev' |
| 253 |
case (20); label = 'MBD dipole' |
| 254 |
case (21); label = 'MBD eig' |
| 255 |
case (22); label = 'MBD forces' |
| 256 |
case (23); label = 'RPA eig' |
| 257 |
case (30); label = 'SCS dipole' |
| 258 |
case (32); label = 'SCS inv' |
| 259 |
case (33); label = 'SCS forces' |
| 260 |
case (50); label = 'SCS' |
| 261 |
case (51); label = 'MBD k-point' |
| 262 |
case (52); label = 'MBD' |
| 263 |
case (90); label = 'MBD@rsSCS' |
| 264 |
case (91); label = 'MBD@rsSCS forces' |
| 265 |
case default |
| 266 |
label = '('//trim(tostr(i))//')' |
| 267 |
end select |
| 268 |
print '(I5,I7," ",A20,I10,F16.6)', i, this%levels(i), label, this%counts(i), & |
| 269 |
dble(this%timestamps(i)) / rate |
| 270 |
end do |
| 271 |
end subroutine |
| 272 |
|
| 273 |
subroutine printer(str) |
| 274 |
character(len=*), intent(in) :: str |
| 275 |
|
| 276 |
#ifdef WITH_MPI |
| 277 |
if (mpi_get_rank() /= 0) return |
| 278 |
#endif |
| 279 |
print *, str |
| 280 |
end subroutine |
| 281 |
|
| 282 |
subroutine logger_debug(this, str) |
| 283 |
class(logger_t), intent(in) :: this |
| 284 |
character(len=*), intent(in) :: str |
| 285 |
|
| 286 |
if (this%level <= MBD_LOG_LVL_DEBUG) call this%printer(str) |
| 287 |
end subroutine |
| 288 |
|
| 289 |
subroutine logger_info(this, str) |
| 290 |
class(logger_t), intent(in) :: this |
| 291 |
character(len=*), intent(in) :: str |
| 292 |
|
| 293 |
if (this%level <= MBD_LOG_LVL_INFO) call this%printer(str) |
| 294 |
end subroutine |
| 295 |
|
| 296 |
subroutine logger_warn(this, str) |
| 297 |
class(logger_t), intent(in) :: this |
| 298 |
character(len=*), intent(in) :: str |
| 299 |
|
| 300 |
if (this%level <= MBD_LOG_LVL_WARN) call this%printer(str) |
| 301 |
end subroutine |
| 302 |
|
| 303 |
subroutine logger_error(this, str) |
| 304 |
class(logger_t), intent(in) :: this |
| 305 |
character(len=*), intent(in) :: str |
| 306 |
|
| 307 |
if (this%level <= MBD_LOG_LVL_ERROR) call this%printer(str) |
| 308 |
end subroutine |
| 309 |
|
| 310 |
logical function is_true(val) result(res) |
| 311 |
logical, intent(in), optional :: val |
| 312 |
|
| 313 |
res = .false. |
| 314 |
if (present(val)) res = val |
| 315 |
end function |
| 316 |
|
| 317 |
end module |