| 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 |
#ifndef LEGENDRE_PREC |
| 5 |
#define LEGENDRE_PREC 15 |
| 6 |
#endif |
| 7 |
|
| 8 |
module mbd_geom |
| 9 |
!! Representing a molecule or a crystal unit cell. |
| 10 |
|
| 11 |
use mbd_constants |
| 12 |
use mbd_defaults |
| 13 |
use mbd_formulas, only: alpha_dyn_qho, C6_from_alpha, omega_qho |
| 14 |
use mbd_gradients, only: grad_t, grad_request_t |
| 15 |
use mbd_lapack, only: eigvals, inverse |
| 16 |
use mbd_utils, only: & |
| 17 |
shift_idx, atom_index_t, quad_pt_t, exception_t, tostr, clock_t, printer, & |
| 18 |
printer_i, logger_t |
| 19 |
use mbd_vdw_param, only: ts_vdw_params |
| 20 |
#ifdef WITH_SCALAPACK |
| 21 |
use mbd_blacs, only: blacs_desc_t, blacs_grid_t |
| 22 |
#endif |
| 23 |
#ifdef WITH_MPI |
| 24 |
use mbd_mpi |
| 25 |
#endif |
| 26 |
|
| 27 |
implicit none |
| 28 |
|
| 29 |
private |
| 30 |
public :: supercell_circum, get_freq_grid |
| 31 |
|
| 32 |
type, public :: param_t |
| 33 |
!! Calculation-wide paramters. |
| 34 |
real(dp) :: dipole_cutoff = 400d0 * ang ! used only when Ewald is off |
| 35 |
real(dp) :: ewald_real_cutoff_scaling = 1d0 |
| 36 |
real(dp) :: ewald_rec_cutoff_scaling = 1d0 |
| 37 |
real(dp) :: k_grid_shift = K_GRID_SHIFT |
| 38 |
logical :: ewald_on = .true. |
| 39 |
logical :: zero_negative_eigvals = .false. |
| 40 |
logical :: rpa_rescale_eigs = .false. |
| 41 |
integer :: rpa_order_max = 10 |
| 42 |
integer :: n_freq = N_FREQUENCY_GRID |
| 43 |
end type |
| 44 |
|
| 45 |
type, public :: geom_t |
| 46 |
!! Represents a molecule or a crystal unit cell. |
| 47 |
!! |
| 48 |
!! The documented variables should be set before calling the initializer. |
| 49 |
real(dp), allocatable :: coords(:, :) |
| 50 |
!! (\(3\times N\), a.u.) Atomic coordinates. |
| 51 |
real(dp), allocatable :: lattice(:, :) |
| 52 |
!! (\(3\times 3\), a.u.) Lattice vectors in columns, unallocated if not |
| 53 |
!! periodic. |
| 54 |
integer, allocatable :: k_grid(:) |
| 55 |
!! Number of \(k\)-points along reciprocal axes. |
| 56 |
real(dp), allocatable :: custom_k_pts(:, :) |
| 57 |
!! Custom \(k\)-point grid. |
| 58 |
character(len=10) :: parallel_mode = 'auto' |
| 59 |
!! Type of parallelization: |
| 60 |
!! |
| 61 |
!! - `atoms`: distribute matrices over all MPI tasks using ScaLAPACK, |
| 62 |
!! solve eigenproblems sequentialy. |
| 63 |
!! - `k_points`: parallelize over k-points (each MPI task solves entire |
| 64 |
!! eigenproblems for its k-points) |
| 65 |
logical :: get_eigs = .false. |
| 66 |
!! Whether to keep MBD eigenvalues |
| 67 |
logical :: get_modes = .false. |
| 68 |
!! Whether to calculate MBD eigenvectors |
| 69 |
logical :: do_rpa = .false. |
| 70 |
!! Whether to calculate MBD energy by frequency integration |
| 71 |
logical :: get_rpa_orders = .false. |
| 72 |
!! Whether to calculate RPA orders |
| 73 |
type(logger_t) :: log |
| 74 |
!! Used for logging |
| 75 |
#ifdef WITH_MPI |
| 76 |
# ifdef WITH_MPIF08 |
| 77 |
type(MPI_Comm) :: mpi_comm = MPI_COMM_WORLD |
| 78 |
# else |
| 79 |
integer :: mpi_comm = MPI_COMM_WORLD |
| 80 |
# endif |
| 81 |
!! MPI communicator |
| 82 |
#endif |
| 83 |
#ifdef WITH_SCALAPACK |
| 84 |
integer :: max_atoms_per_block = MAX_ATOMS_PER_BLOCK |
| 85 |
#endif |
| 86 |
! The following components are set by the initializer and should be |
| 87 |
! considered read-only |
| 88 |
type(clock_t) :: timer |
| 89 |
type(exception_t) :: exc |
| 90 |
type(quad_pt_t), allocatable :: freq(:) |
| 91 |
real(dp) :: gamm = 0d0 |
| 92 |
real(dp) :: real_space_cutoff |
| 93 |
real(dp) :: rec_space_cutoff |
| 94 |
type(param_t) :: param |
| 95 |
type(atom_index_t) :: idx |
| 96 |
#ifdef WITH_SCALAPACK |
| 97 |
! TODO makes these two private (see use in mbd_methods, mbd_dipole) |
| 98 |
type(blacs_desc_t) :: blacs |
| 99 |
type(blacs_grid_t) :: blacs_grid |
| 100 |
#endif |
| 101 |
#ifdef WITH_MPI |
| 102 |
integer :: mpi_size = -1 |
| 103 |
integer :: mpi_rank = -1 |
| 104 |
#endif |
| 105 |
contains |
| 106 |
procedure :: init => geom_init |
| 107 |
procedure :: destroy => geom_destroy |
| 108 |
procedure :: siz => geom_siz |
| 109 |
procedure :: has_exc => geom_has_exc |
| 110 |
#ifdef WITH_MPI |
| 111 |
procedure :: sync_exc => geom_sync_exc |
| 112 |
#endif |
| 113 |
procedure :: clock => geom_clock |
| 114 |
end type |
| 115 |
|
| 116 |
contains |
| 117 |
|
| 118 |
subroutine geom_init(this) |
| 119 |
class(geom_t), intent(inout) :: this |
| 120 |
|
| 121 |
integer :: i_atom |
| 122 |
real(dp) :: volume, freq_grid_err |
| 123 |
logical :: is_parallel |
| 124 |
character(len=10) :: log_level_str |
| 125 |
#ifdef WITH_MPI |
| 126 |
logical :: can_parallel_kpts |
| 127 |
integer :: ierr, n_kpts |
| 128 |
#endif |
| 129 |
|
| 130 |
if (.not. associated(this%log%printer)) this%log%printer => printer |
| 131 |
call get_environment_variable('LIBMBD_LOG_LEVEL', log_level_str) |
| 132 |
if (log_level_str /= '') read (log_level_str, *) this%log%level |
| 133 |
associate (n => this%param%n_freq) |
| 134 |
allocate (this%freq(0:n)) |
| 135 |
call get_freq_grid(n, this%freq(1:n)%val, this%freq(1:n)%weight) |
| 136 |
end associate |
| 137 |
this%freq(0)%val = 0d0 |
| 138 |
this%freq(0)%weight = 0d0 |
| 139 |
freq_grid_err = test_frequency_grid(this%freq) |
| 140 |
call this%log%info('Frequency grid relative error: '//tostr(freq_grid_err)) |
| 141 |
call this%timer%init(100) |
| 142 |
if (allocated(this%lattice)) then |
| 143 |
volume = abs(dble(product(eigvals(this%lattice)))) |
| 144 |
if (this%param%ewald_on) then |
| 145 |
this%gamm = 2.5d0 / volume**(1d0 / 3) |
| 146 |
this%real_space_cutoff = 6d0 / this%gamm * this%param%ewald_real_cutoff_scaling |
| 147 |
this%rec_space_cutoff = 10d0 * this%gamm * this%param%ewald_rec_cutoff_scaling |
| 148 |
else |
| 149 |
this%real_space_cutoff = this%param%dipole_cutoff |
| 150 |
end if |
| 151 |
end if |
| 152 |
#ifdef WITH_MPI |
| 153 |
call MPI_COMM_SIZE(this%mpi_comm, this%mpi_size, ierr) |
| 154 |
call MPI_COMM_RANK(this%mpi_comm, this%mpi_rank, ierr) |
| 155 |
if (allocated(this%custom_k_pts)) then |
| 156 |
n_kpts = size(this%custom_k_pts, 2) |
| 157 |
else if (allocated(this%k_grid)) then |
| 158 |
n_kpts = product(this%k_grid) |
| 159 |
else |
| 160 |
n_kpts = -1 |
| 161 |
end if |
| 162 |
can_parallel_kpts = allocated(this%lattice) .and. n_kpts > 0 .and. this%mpi_size > 1 |
| 163 |
if (this%parallel_mode == 'auto' .and. can_parallel_kpts .and. this%siz()**2 < n_kpts) then |
| 164 |
this%parallel_mode = 'k_points' |
| 165 |
end if |
| 166 |
#endif |
| 167 |
#ifdef WITH_SCALAPACK |
| 168 |
this%idx%parallel = .false. |
| 169 |
if (this%parallel_mode == 'auto' .or. this%parallel_mode == 'atoms') then |
| 170 |
# ifdef WITH_MPI |
| 171 |
# ifdef WITH_MPIF08 |
| 172 |
call this%blacs_grid%init(this%mpi_size, this%mpi_comm%mpi_val) |
| 173 |
# else |
| 174 |
call this%blacs_grid%init(this%mpi_size, this%mpi_comm) |
| 175 |
# endif |
| 176 |
# else |
| 177 |
call this%blacs_grid%init() |
| 178 |
# endif |
| 179 |
call this%blacs%init(this%siz(), this%blacs_grid, this%max_atoms_per_block) |
| 180 |
# ifdef WITH_MPI |
| 181 |
# ifdef WITH_MPIF08 |
| 182 |
this%blacs%comm = this%mpi_comm%mpi_val |
| 183 |
# else |
| 184 |
this%blacs%comm = this%mpi_comm |
| 185 |
# endif |
| 186 |
# endif |
| 187 |
if (allocated(this%blacs%i_atom)) then |
| 188 |
this%parallel_mode = 'atoms' |
| 189 |
this%idx%parallel = .true. |
| 190 |
this%idx%i_atom = this%blacs%i_atom |
| 191 |
this%idx%j_atom = this%blacs%j_atom |
| 192 |
else |
| 193 |
call this%blacs_grid%destroy() |
| 194 |
end if |
| 195 |
end if |
| 196 |
#endif |
| 197 |
#ifdef WITH_MPI |
| 198 |
if (this%parallel_mode == 'auto' .and. can_parallel_kpts) then |
| 199 |
this%parallel_mode = 'k_points' |
| 200 |
end if |
| 201 |
#endif |
| 202 |
if (this%parallel_mode == 'auto') this%parallel_mode = 'none' |
| 203 |
#ifdef WITH_SCALAPACK |
| 204 |
is_parallel = this%idx%parallel |
| 205 |
#else |
| 206 |
is_parallel = .false. |
| 207 |
#endif |
| 208 |
if (.not. is_parallel) then |
| 209 |
this%idx%i_atom = [(i_atom, i_atom=1, this%siz())] |
| 210 |
this%idx%j_atom = this%idx%i_atom |
| 211 |
end if |
| 212 |
this%idx%n_atoms = this%siz() |
| 213 |
call this%log%info('Will use parallel mode: '//this%parallel_mode) |
| 214 |
#ifdef WITH_SCALAPACK |
| 215 |
if (this%idx%parallel) then |
| 216 |
call this%log%info( & |
| 217 |
'BLACS grid: '//trim(tostr(this%blacs_grid%nprows))//' x ' & |
| 218 |
//trim(tostr(this%blacs_grid%npcols)) & |
| 219 |
) |
| 220 |
call this%log%info('BLACS block size: '//tostr(this%blacs%blocksize)) |
| 221 |
end if |
| 222 |
#endif |
| 223 |
end subroutine |
| 224 |
|
| 225 |
subroutine geom_destroy(this) |
| 226 |
class(geom_t), intent(inout) :: this |
| 227 |
|
| 228 |
#ifdef WITH_SCALAPACK |
| 229 |
if (this%idx%parallel) call this%blacs_grid%destroy() |
| 230 |
#endif |
| 231 |
deallocate (this%freq) |
| 232 |
deallocate (this%timer%timestamps) |
| 233 |
deallocate (this%timer%counts) |
| 234 |
deallocate (this%timer%levels) |
| 235 |
end subroutine |
| 236 |
|
| 237 |
integer function geom_siz(this) result(siz) |
| 238 |
class(geom_t), intent(in) :: this |
| 239 |
|
| 240 |
if (allocated(this%coords)) then |
| 241 |
siz = size(this%coords, 2) |
| 242 |
else |
| 243 |
siz = 0 |
| 244 |
end if |
| 245 |
end function |
| 246 |
|
| 247 |
logical function geom_has_exc(this) result(has_exc) |
| 248 |
class(geom_t), intent(in) :: this |
| 249 |
|
| 250 |
has_exc = this%exc%code /= 0 |
| 251 |
end function |
| 252 |
|
| 253 |
#ifdef WITH_MPI |
| 254 |
subroutine geom_sync_exc(this) |
| 255 |
class(geom_t), intent(inout) :: this |
| 256 |
|
| 257 |
integer, allocatable :: codes(:) |
| 258 |
integer :: err, rank |
| 259 |
|
| 260 |
allocate (codes(this%mpi_size)) |
| 261 |
call MPI_ALLGATHER(this%exc%code, 1, MPI_INTEGER, codes, 1, MPI_INTEGER, this%mpi_comm, err) |
| 262 |
do rank = 0, size(codes) - 1 |
| 263 |
if (codes(rank + 1) /= 0) then |
| 264 |
call MPI_BCAST(this%exc%code, 1, MPI_INTEGER, rank, this%mpi_comm, err) |
| 265 |
call MPI_BCAST( & |
| 266 |
this%exc%msg, len(this%exc%msg), MPI_CHARACTER, rank, this%mpi_comm, err & |
| 267 |
) |
| 268 |
call MPI_BCAST( & |
| 269 |
this%exc%origin, len(this%exc%origin), MPI_CHARACTER, rank, this%mpi_comm, err & |
| 270 |
) |
| 271 |
exit |
| 272 |
end if |
| 273 |
end do |
| 274 |
end subroutine |
| 275 |
#endif |
| 276 |
|
| 277 |
function supercell_circum(lattice, radius) result(sc) |
| 278 |
real(dp), intent(in) :: lattice(3, 3) |
| 279 |
real(dp), intent(in) :: radius |
| 280 |
integer :: sc(3) |
| 281 |
|
| 282 |
real(dp) :: ruc(3, 3), layer_sep(3) |
| 283 |
integer :: i |
| 284 |
|
| 285 |
ruc = 2 * pi * inverse(transpose(lattice)) |
| 286 |
do concurrent(i=1:3) |
| 287 |
layer_sep(i) = sum(lattice(:, i) * ruc(:, i) / sqrt(sum(ruc(:, i)**2))) |
| 288 |
end do |
| 289 |
sc = ceiling(radius / layer_sep + 0.5d0) |
| 290 |
end function |
| 291 |
|
| 292 |
subroutine geom_clock(this, id) |
| 293 |
class(geom_t), intent(inout) :: this |
| 294 |
integer, intent(in) :: id |
| 295 |
|
| 296 |
call this%timer%clock(id) |
| 297 |
end subroutine |
| 298 |
|
| 299 |
subroutine get_freq_grid(n, x, w, L) |
| 300 |
integer, intent(in) :: n |
| 301 |
real(dp), intent(out) :: x(n), w(n) |
| 302 |
real(dp), intent(in), optional :: L |
| 303 |
|
| 304 |
real(dp) :: L_ |
| 305 |
|
| 306 |
if (present(L)) then |
| 307 |
L_ = L |
| 308 |
else |
| 309 |
L_ = 0.6d0 |
| 310 |
end if |
| 311 |
call gauss_legendre(n, x, w) |
| 312 |
w = 2 * L_ / (1 - x)**2 * w |
| 313 |
x = L_ * (1 + x) / (1 - x) |
| 314 |
w = w(n:1:-1) |
| 315 |
x = x(n:1:-1) |
| 316 |
end subroutine |
| 317 |
|
| 318 |
subroutine gauss_legendre(n, r, w) |
| 319 |
integer, intent(in) :: n |
| 320 |
real(dp), intent(out) :: r(n), w(n) |
| 321 |
|
| 322 |
integer, parameter :: q = selected_real_kind(LEGENDRE_PREC) |
| 323 |
integer, parameter :: n_iter = 1000 |
| 324 |
real(q) :: x, f, df, dx |
| 325 |
integer :: k, iter, i |
| 326 |
real(q) :: Pk(0:n), Pk1(0:n - 1), Pk2(0:n - 2) |
| 327 |
|
| 328 |
if (n == 1) then |
| 329 |
r(1) = 0d0 |
| 330 |
w(1) = 2d0 |
| 331 |
return |
| 332 |
end if |
| 333 |
Pk2(0) = 1._q ! k = 0 |
| 334 |
Pk1(0:1) = [0._q, 1._q] ! k = 1 |
| 335 |
do k = 2, n |
| 336 |
Pk(0:k) = ((2 * k - 1)* & |
| 337 |
[0._q, Pk1(0:k - 1)] - (k - 1)*[Pk2(0:k - 2), 0._q, 0._q]) / k |
| 338 |
if (k < n) then |
| 339 |
Pk2(0:k - 1) = Pk1(0:k - 1) |
| 340 |
Pk1(0:k) = Pk(0:k) |
| 341 |
end if |
| 342 |
end do |
| 343 |
! now Pk contains k-th Legendre polynomial |
| 344 |
do i = 1, n |
| 345 |
x = cos(pi * (i - 0.25_q) / (n + 0.5_q)) |
| 346 |
do iter = 1, n_iter |
| 347 |
df = 0._q |
| 348 |
f = Pk(n) |
| 349 |
do k = n - 1, 0, -1 |
| 350 |
df = f + x * df |
| 351 |
f = Pk(k) + x * f |
| 352 |
end do |
| 353 |
dx = f / df |
| 354 |
x = x - dx |
| 355 |
if (abs(dx) < 10 * epsilon(dx)) exit |
| 356 |
end do |
| 357 |
r(i) = dble(x) |
| 358 |
w(i) = dble(2 / ((1 - x**2) * df**2)) |
| 359 |
end do |
| 360 |
end subroutine |
| 361 |
|
| 362 |
real(dp) function test_frequency_grid(freq) result(error) |
| 363 |
!! Calculate relative quadrature error in C6 of a carbon atom |
| 364 |
type(quad_pt_t), intent(in) :: freq(0:) |
| 365 |
|
| 366 |
real(dp) :: alpha(1, 0:ubound(freq, 1)), C6(1), C6_ref(1), w(1), a0(1) |
| 367 |
type(grad_t), allocatable :: dalpha(:) |
| 368 |
type(grad_request_t) :: grad |
| 369 |
|
| 370 |
a0(1) = ts_vdw_params(1, 6) |
| 371 |
C6_ref(1) = ts_vdw_params(2, 6) |
| 372 |
w = omega_qho(C6_ref, a0) |
| 373 |
alpha = alpha_dyn_qho(a0, w, freq, dalpha, grad) |
| 374 |
C6 = C6_from_alpha(alpha, freq) |
| 375 |
error = abs(C6(1) / C6_ref(1) - 1d0) |
| 376 |
end function |
| 377 |
|
| 378 |
end module |