| 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 DO_COMPLEX_TYPE |
| 5 |
module mbd_matrix |
| 6 |
|
| 7 |
use mbd_constants |
| 8 |
use mbd_lapack, only: mmul, invh, invh, eigh, eigvals, eigvalsh |
| 9 |
use mbd_utils, only: findval, exception_t, atom_index_t, is_true, clock_t |
| 10 |
# ifdef WITH_SCALAPACK |
| 11 |
use mbd_blacs, only: blacs_desc_t, blacs_all_reduce |
| 12 |
use mbd_scalapack, only: pmmul, pinvh, pinvh, peigh, peigvalsh |
| 13 |
# endif |
| 14 |
# ifdef WITH_ELSI |
| 15 |
use mbd_elsi, only: elsi_eigh, elsi_eigvalsh |
| 16 |
# endif |
| 17 |
|
| 18 |
implicit none |
| 19 |
|
| 20 |
private |
| 21 |
public :: contract_cross_33 |
| 22 |
|
| 23 |
type, public :: matrix_re_t |
| 24 |
real(dp), allocatable :: val(:, :) |
| 25 |
type(atom_index_t) :: idx |
| 26 |
# ifdef WITH_SCALAPACK |
| 27 |
type(blacs_desc_t) :: blacs |
| 28 |
# endif |
| 29 |
contains |
| 30 |
procedure :: siz => matrix_re_siz |
| 31 |
procedure :: init => matrix_re_init |
| 32 |
procedure :: add_diag => matrix_re_add_diag |
| 33 |
procedure :: add_diag_scalar => matrix_re_add_diag_scalar |
| 34 |
procedure :: mult_cross => matrix_re_mult_cross |
| 35 |
procedure :: mult_rows => matrix_re_mult_rows |
| 36 |
procedure :: mult_cols_3n => matrix_re_mult_cols_3n |
| 37 |
procedure :: mult_col => matrix_re_mult_col |
| 38 |
procedure :: mmul => matrix_re_mmul |
| 39 |
procedure :: invh => matrix_re_invh |
| 40 |
procedure :: eigh => matrix_re_eigh |
| 41 |
procedure :: eigvals => matrix_re_eigvals |
| 42 |
procedure :: eigvalsh => matrix_re_eigvalsh |
| 43 |
procedure :: sum_all => matrix_re_sum_all |
| 44 |
procedure :: contract_n_transp => matrix_re_contract_n_transp |
| 45 |
procedure :: contract_n33diag_cols => matrix_re_contract_n33diag_cols |
| 46 |
procedure :: contract_n33_rows => matrix_re_contract_n33_rows |
| 47 |
procedure :: copy_from => matrix_re_copy_from |
| 48 |
procedure :: move_from => matrix_re_move_from |
| 49 |
procedure :: init_from => matrix_re_init_from |
| 50 |
procedure :: alloc_from => matrix_re_alloc_from |
| 51 |
end type |
| 52 |
|
| 53 |
type, public :: matrix_cplx_t |
| 54 |
complex(dp), allocatable :: val(:, :) |
| 55 |
type(atom_index_t) :: idx |
| 56 |
# ifdef WITH_SCALAPACK |
| 57 |
type(blacs_desc_t) :: blacs |
| 58 |
# endif |
| 59 |
contains |
| 60 |
procedure :: siz => matrix_cplx_siz |
| 61 |
procedure :: init => matrix_cplx_init |
| 62 |
procedure :: add_diag => matrix_cplx_add_diag |
| 63 |
procedure :: add_diag_scalar => matrix_cplx_add_diag_scalar |
| 64 |
procedure :: mult_cross => matrix_cplx_mult_cross |
| 65 |
procedure :: mult_rows => matrix_cplx_mult_rows |
| 66 |
procedure :: mult_cols_3n => matrix_cplx_mult_cols_3n |
| 67 |
procedure :: mult_col => matrix_cplx_mult_col |
| 68 |
procedure :: mmul => matrix_cplx_mmul |
| 69 |
procedure :: eigh => matrix_cplx_eigh |
| 70 |
procedure :: eigvalsh => matrix_cplx_eigvalsh |
| 71 |
procedure :: sum_all => matrix_cplx_sum_all |
| 72 |
procedure :: contract_n_transp => matrix_cplx_contract_n_transp |
| 73 |
procedure :: contract_n33diag_cols => matrix_cplx_contract_n33diag_cols |
| 74 |
procedure :: contract_n33_rows => matrix_cplx_contract_n33_rows |
| 75 |
procedure :: copy_from => matrix_cplx_copy_from |
| 76 |
procedure :: move_from => matrix_cplx_move_from |
| 77 |
procedure :: init_from => matrix_cplx_init_from |
| 78 |
procedure :: alloc_from => matrix_cplx_alloc_from |
| 79 |
end type |
| 80 |
|
| 81 |
interface contract_cross_33 |
| 82 |
module procedure contract_cross_33_real |
| 83 |
module procedure contract_cross_33_complex |
| 84 |
end interface |
| 85 |
|
| 86 |
contains |
| 87 |
|
| 88 |
#endif |
| 89 |
|
| 90 |
#ifndef DO_COMPLEX_TYPE |
| 91 |
integer function matrix_re_siz(this, ndim) result(siz) |
| 92 |
class(matrix_re_t), intent(in) :: this |
| 93 |
#else |
| 94 |
integer function matrix_cplx_siz(this, ndim) result(siz) |
| 95 |
class(matrix_cplx_t), intent(in) :: this |
| 96 |
#endif |
| 97 |
integer, intent(in) :: ndim |
| 98 |
|
| 99 |
siz = size(this%val, ndim) |
| 100 |
end function |
| 101 |
|
| 102 |
#ifndef DO_COMPLEX_TYPE |
| 103 |
# ifdef WITH_SCALAPACK |
| 104 |
subroutine matrix_re_init(this, idx, blacs) |
| 105 |
# else |
| 106 |
subroutine matrix_re_init(this, idx) |
| 107 |
# endif |
| 108 |
class(matrix_re_t), intent(out) :: this |
| 109 |
#else |
| 110 |
# ifdef WITH_SCALAPACK |
| 111 |
subroutine matrix_cplx_init(this, idx, blacs) |
| 112 |
# else |
| 113 |
subroutine matrix_cplx_init(this, idx) |
| 114 |
# endif |
| 115 |
class(matrix_cplx_t), intent(out) :: this |
| 116 |
#endif |
| 117 |
type(atom_index_t), intent(in) :: idx |
| 118 |
#ifdef WITH_SCALAPACK |
| 119 |
type(blacs_desc_t), intent(in) :: blacs |
| 120 |
#endif |
| 121 |
|
| 122 |
this%idx = idx |
| 123 |
#ifdef WITH_SCALAPACK |
| 124 |
this%blacs = blacs |
| 125 |
#endif |
| 126 |
end subroutine |
| 127 |
|
| 128 |
#ifndef DO_COMPLEX_TYPE |
| 129 |
subroutine matrix_re_init_from(this, other) |
| 130 |
class(matrix_re_t), intent(out) :: this |
| 131 |
type(matrix_re_t), intent(in) :: other |
| 132 |
#else |
| 133 |
subroutine matrix_cplx_init_from(this, other) |
| 134 |
class(matrix_cplx_t), intent(out) :: this |
| 135 |
type(matrix_cplx_t), intent(in) :: other |
| 136 |
#endif |
| 137 |
|
| 138 |
this%idx = other%idx |
| 139 |
#ifdef WITH_SCALAPACK |
| 140 |
this%blacs = other%blacs |
| 141 |
#endif |
| 142 |
end subroutine |
| 143 |
|
| 144 |
#ifndef DO_COMPLEX_TYPE |
| 145 |
subroutine matrix_re_copy_from(this, other) |
| 146 |
class(matrix_re_t), intent(out) :: this |
| 147 |
type(matrix_re_t), intent(in) :: other |
| 148 |
#else |
| 149 |
subroutine matrix_cplx_copy_from(this, other) |
| 150 |
class(matrix_cplx_t), intent(out) :: this |
| 151 |
type(matrix_cplx_t), intent(in) :: other |
| 152 |
#endif |
| 153 |
|
| 154 |
call this%init_from(other) |
| 155 |
this%val = other%val |
| 156 |
end subroutine |
| 157 |
|
| 158 |
#ifndef DO_COMPLEX_TYPE |
| 159 |
subroutine matrix_re_move_from(this, other) |
| 160 |
class(matrix_re_t), intent(out) :: this |
| 161 |
type(matrix_re_t), intent(inout) :: other |
| 162 |
#else |
| 163 |
subroutine matrix_cplx_move_from(this, other) |
| 164 |
class(matrix_cplx_t), intent(out) :: this |
| 165 |
type(matrix_cplx_t), intent(inout) :: other |
| 166 |
#endif |
| 167 |
|
| 168 |
call this%init_from(other) |
| 169 |
call move_alloc(other%val, this%val) |
| 170 |
end subroutine |
| 171 |
|
| 172 |
#ifndef DO_COMPLEX_TYPE |
| 173 |
subroutine matrix_re_alloc_from(this, other) |
| 174 |
class(matrix_re_t), intent(out) :: this |
| 175 |
type(matrix_re_t), intent(in) :: other |
| 176 |
#else |
| 177 |
subroutine matrix_cplx_alloc_from(this, other) |
| 178 |
class(matrix_cplx_t), intent(out) :: this |
| 179 |
type(matrix_cplx_t), intent(in) :: other |
| 180 |
#endif |
| 181 |
|
| 182 |
integer :: n1, n2 |
| 183 |
|
| 184 |
call this%init_from(other) |
| 185 |
n1 = other%siz(1) |
| 186 |
n2 = other%siz(2) |
| 187 |
allocate (this%val(n1, n2)) |
| 188 |
end subroutine |
| 189 |
|
| 190 |
#ifndef DO_COMPLEX_TYPE |
| 191 |
subroutine matrix_re_add_diag_scalar(this, d) |
| 192 |
class(matrix_re_t), intent(inout) :: this |
| 193 |
#else |
| 194 |
subroutine matrix_cplx_add_diag_scalar(this, d) |
| 195 |
class(matrix_cplx_t), intent(inout) :: this |
| 196 |
#endif |
| 197 |
real(dp), intent(in) :: d |
| 198 |
|
| 199 |
integer :: i |
| 200 |
|
| 201 |
call this%add_diag([(d, i=1, this%idx%n_atoms)]) |
| 202 |
end subroutine |
| 203 |
|
| 204 |
#ifndef DO_COMPLEX_TYPE |
| 205 |
subroutine matrix_re_add_diag(this, d) |
| 206 |
class(matrix_re_t), intent(inout) :: this |
| 207 |
#else |
| 208 |
subroutine matrix_cplx_add_diag(this, d) |
| 209 |
class(matrix_cplx_t), intent(inout) :: this |
| 210 |
#endif |
| 211 |
real(dp), intent(in) :: d(:) |
| 212 |
|
| 213 |
integer :: my_i_atom, my_j_atom, i |
| 214 |
|
| 215 |
do my_i_atom = 1, size(this%idx%i_atom) |
| 216 |
do my_j_atom = 1, size(this%idx%j_atom) |
| 217 |
associate ( & |
| 218 |
i_atom => this%idx%i_atom(my_i_atom), & |
| 219 |
j_atom => this%idx%j_atom(my_j_atom), & |
| 220 |
this_diag => this%val(3 * (my_i_atom - 1) + 1:, 3 * (my_j_atom - 1) + 1:) & |
| 221 |
) |
| 222 |
if (i_atom /= j_atom) cycle |
| 223 |
do i = 1, 3 |
| 224 |
this_diag(i, i) = this_diag(i, i) + d(i_atom) |
| 225 |
end do |
| 226 |
end associate |
| 227 |
end do |
| 228 |
end do |
| 229 |
end subroutine |
| 230 |
|
| 231 |
#ifndef DO_COMPLEX_TYPE |
| 232 |
subroutine matrix_re_mult_cross(this, b, c) |
| 233 |
class(matrix_re_t), intent(inout) :: this |
| 234 |
#else |
| 235 |
subroutine matrix_cplx_mult_cross(this, b, c) |
| 236 |
class(matrix_cplx_t), intent(inout) :: this |
| 237 |
#endif |
| 238 |
real(dp), intent(in) :: b(:) |
| 239 |
real(dp), intent(in), optional :: c(:) |
| 240 |
|
| 241 |
integer :: my_i_atom, my_j_atom |
| 242 |
|
| 243 |
do my_i_atom = 1, size(this%idx%i_atom) |
| 244 |
do my_j_atom = 1, size(this%idx%j_atom) |
| 245 |
associate ( & |
| 246 |
i_atom => this%idx%i_atom(my_i_atom), & |
| 247 |
j_atom => this%idx%j_atom(my_j_atom), & |
| 248 |
this_sub => this%val(3 * (my_i_atom - 1) + 1:, 3 * (my_j_atom - 1) + 1:) & |
| 249 |
) |
| 250 |
if (present(c)) then |
| 251 |
this_sub(:3, :3) = this_sub(:3, :3) * & |
| 252 |
(b(i_atom) * c(j_atom) + c(i_atom) * b(j_atom)) |
| 253 |
else |
| 254 |
this_sub(:3, :3) = this_sub(:3, :3) * b(i_atom) * b(j_atom) |
| 255 |
end if |
| 256 |
end associate |
| 257 |
end do |
| 258 |
end do |
| 259 |
end subroutine |
| 260 |
|
| 261 |
#ifndef DO_COMPLEX_TYPE |
| 262 |
subroutine matrix_re_mult_rows(this, b) |
| 263 |
class(matrix_re_t), intent(inout) :: this |
| 264 |
#else |
| 265 |
subroutine matrix_cplx_mult_rows(this, b) |
| 266 |
class(matrix_cplx_t), intent(inout) :: this |
| 267 |
#endif |
| 268 |
real(dp), intent(in) :: b(:) |
| 269 |
|
| 270 |
integer :: my_i_atom |
| 271 |
|
| 272 |
do my_i_atom = 1, size(this%idx%i_atom) |
| 273 |
associate ( & |
| 274 |
i_atom => this%idx%i_atom(my_i_atom), & |
| 275 |
this_sub => this%val(3 * (my_i_atom - 1) + 1:, :) & |
| 276 |
) |
| 277 |
this_sub(:3, :) = this_sub(:3, :) * b(i_atom) |
| 278 |
end associate |
| 279 |
end do |
| 280 |
end subroutine |
| 281 |
|
| 282 |
#ifndef DO_COMPLEX_TYPE |
| 283 |
subroutine matrix_re_mult_cols_3n(this, b) |
| 284 |
class(matrix_re_t), intent(inout) :: this |
| 285 |
#else |
| 286 |
subroutine matrix_cplx_mult_cols_3n(this, b) |
| 287 |
class(matrix_cplx_t), intent(inout) :: this |
| 288 |
#endif |
| 289 |
real(dp), intent(in) :: b(:) |
| 290 |
|
| 291 |
integer :: my_j_atom, i |
| 292 |
|
| 293 |
do my_j_atom = 1, size(this%idx%j_atom) |
| 294 |
associate ( & |
| 295 |
b_sub => b(3 * (this%idx%j_atom(my_j_atom) - 1) + 1:), & |
| 296 |
this_sub => this%val(:, 3 * (my_j_atom - 1) + 1:) & |
| 297 |
) |
| 298 |
! TODO should be do-concurrent, but this crashes IBM XL 16.1.1, |
| 299 |
! see issue #16 |
| 300 |
do i = 1, 3 |
| 301 |
this_sub(:, i) = this_sub(:, i) * b_sub(i) |
| 302 |
end do |
| 303 |
end associate |
| 304 |
end do |
| 305 |
end subroutine |
| 306 |
|
| 307 |
#ifndef DO_COMPLEX_TYPE |
| 308 |
subroutine matrix_re_mult_col(this, idx, a) |
| 309 |
class(matrix_re_t), intent(inout) :: this |
| 310 |
#else |
| 311 |
subroutine matrix_cplx_mult_col(this, idx, a) |
| 312 |
class(matrix_cplx_t), intent(inout) :: this |
| 313 |
#endif |
| 314 |
integer, intent(in) :: idx |
| 315 |
real(dp), intent(in) :: a(:) |
| 316 |
|
| 317 |
integer :: my_i_atom, my_j_atom |
| 318 |
|
| 319 |
do my_j_atom = 1, size(this%idx%j_atom) |
| 320 |
if (this%idx%j_atom(my_j_atom) /= idx) cycle |
| 321 |
do my_i_atom = 1, size(this%idx%i_atom) |
| 322 |
associate ( & |
| 323 |
i_atom => this%idx%i_atom(my_i_atom), & |
| 324 |
this_sub => this%val(3 * (my_i_atom - 1) + 1:, 3 * (my_j_atom - 1) + 1:) & |
| 325 |
) |
| 326 |
this_sub(:3, :3) = this_sub(:3, :3) * a(i_atom) |
| 327 |
end associate |
| 328 |
end do |
| 329 |
end do |
| 330 |
end subroutine |
| 331 |
|
| 332 |
#ifndef DO_COMPLEX_TYPE |
| 333 |
subroutine matrix_re_eigh(A, eigs, exc, src, vals_only, clock) |
| 334 |
class(matrix_re_t), intent(inout) :: A |
| 335 |
type(matrix_re_t), intent(in), optional :: src |
| 336 |
#else |
| 337 |
subroutine matrix_cplx_eigh(A, eigs, exc, src, vals_only, clock) |
| 338 |
class(matrix_cplx_t), intent(inout) :: A |
| 339 |
type(matrix_cplx_t), intent(in), optional :: src |
| 340 |
#endif |
| 341 |
real(dp), intent(out) :: eigs(:) |
| 342 |
type(exception_t), intent(out), optional :: exc |
| 343 |
logical, intent(in), optional :: vals_only |
| 344 |
type(clock_t), intent(inout), optional :: clock |
| 345 |
|
| 346 |
#ifdef WITH_SCALAPACK |
| 347 |
if (A%idx%parallel) then |
| 348 |
# ifdef WITH_ELSI |
| 349 |
if (present(clock)) call clock%clock(18) |
| 350 |
call elsi_eigh(A%val, A%blacs, eigs, exc, src%val, vals_only) |
| 351 |
if (present(clock)) call clock%clock(-18) |
| 352 |
# else |
| 353 |
call peigh(A%val, A%blacs, eigs, exc, src%val, vals_only, clock) |
| 354 |
# endif |
| 355 |
return |
| 356 |
end if |
| 357 |
#endif |
| 358 |
call eigh(A%val, eigs, exc, src%val, vals_only) |
| 359 |
end subroutine |
| 360 |
|
| 361 |
#ifndef DO_COMPLEX_TYPE |
| 362 |
function matrix_re_eigvalsh(A, exc, destroy, clock) result(eigs) |
| 363 |
class(matrix_re_t), intent(inout) :: A |
| 364 |
#else |
| 365 |
function matrix_cplx_eigvalsh(A, exc, destroy, clock) result(eigs) |
| 366 |
class(matrix_cplx_t), intent(inout) :: A |
| 367 |
#endif |
| 368 |
type(exception_t), intent(out), optional :: exc |
| 369 |
logical, intent(in), optional :: destroy |
| 370 |
real(dp) :: eigs(3 * A%idx%n_atoms) |
| 371 |
type(clock_t), intent(inout), optional :: clock |
| 372 |
|
| 373 |
#ifdef WITH_SCALAPACK |
| 374 |
if (A%idx%parallel) then |
| 375 |
# ifdef WITH_ELSI |
| 376 |
eigs = elsi_eigvalsh(A%val, A%blacs, exc, destroy) |
| 377 |
# else |
| 378 |
eigs = peigvalsh(A%val, A%blacs, exc, destroy, clock) |
| 379 |
# endif |
| 380 |
return |
| 381 |
end if |
| 382 |
#endif |
| 383 |
eigs = eigvalsh(A%val, exc, destroy) |
| 384 |
end function |
| 385 |
|
| 386 |
#ifndef DO_COMPLEX_TYPE |
| 387 |
real(dp) function matrix_re_sum_all(this) result(res) |
| 388 |
class(matrix_re_t), intent(in) :: this |
| 389 |
#else |
| 390 |
complex(dp) function matrix_cplx_sum_all(this) result(res) |
| 391 |
class(matrix_cplx_t), intent(in) :: this |
| 392 |
#endif |
| 393 |
|
| 394 |
res = sum(this%val) |
| 395 |
#ifdef WITH_SCALAPACK |
| 396 |
if (this%idx%parallel) call blacs_all_reduce(res, this%blacs) |
| 397 |
#endif |
| 398 |
end function |
| 399 |
|
| 400 |
#ifndef DO_COMPLEX_TYPE |
| 401 |
subroutine matrix_re_contract_n_transp(this, dir, res) |
| 402 |
class(matrix_re_t), intent(in) :: this |
| 403 |
real(dp), intent(out), target :: res(:, :) |
| 404 |
#else |
| 405 |
subroutine matrix_cplx_contract_n_transp(this, dir, res) |
| 406 |
class(matrix_cplx_t), intent(in) :: this |
| 407 |
complex(dp), intent(out), target :: res(:, :) |
| 408 |
#endif |
| 409 |
character(len=*), intent(in) :: dir |
| 410 |
|
| 411 |
integer :: my_i_atom, my_j_atom |
| 412 |
#ifndef DO_COMPLEX_TYPE |
| 413 |
real(dp), pointer :: res_sub(:, :) |
| 414 |
#else |
| 415 |
complex(dp), pointer :: res_sub(:, :) |
| 416 |
#endif |
| 417 |
|
| 418 |
res(:, :) = 0d0 |
| 419 |
do my_i_atom = 1, size(this%idx%i_atom) |
| 420 |
do my_j_atom = 1, size(this%idx%j_atom) |
| 421 |
select case (dir(1:1)) |
| 422 |
case ('R') |
| 423 |
res_sub => res(:, 3 * (this%idx%i_atom(my_i_atom) - 1) + 1:) |
| 424 |
case ('C') |
| 425 |
res_sub => res(3 * (this%idx%j_atom(my_j_atom) - 1) + 1:, :) |
| 426 |
end select |
| 427 |
associate ( & |
| 428 |
this_sub => this%val(3 * (my_i_atom - 1) + 1:, 3 * (my_j_atom - 1) + 1:) & |
| 429 |
) |
| 430 |
res_sub(:3, :3) = res_sub(:3, :3) + transpose(this_sub(:3, :3)) |
| 431 |
end associate |
| 432 |
end do |
| 433 |
end do |
| 434 |
#ifdef WITH_SCALAPACK |
| 435 |
if (this%idx%parallel) call blacs_all_reduce(res, this%blacs) |
| 436 |
#endif |
| 437 |
end subroutine |
| 438 |
|
| 439 |
#ifndef DO_COMPLEX_TYPE |
| 440 |
function contract_cross_33_real(k_atom, A, A_prime, B, B_prime) result(res) |
| 441 |
type(matrix_re_t), intent(in) :: A, B |
| 442 |
real(dp), intent(in) :: A_prime(:, :), B_prime(:, :) |
| 443 |
real(dp) :: res(A%idx%n_atoms) |
| 444 |
#else |
| 445 |
function contract_cross_33_complex(k_atom, A, A_prime, B, B_prime) result(res) |
| 446 |
type(matrix_cplx_t), intent(in) :: A, B |
| 447 |
complex(dp), intent(in) :: A_prime(:, :), B_prime(:, :) |
| 448 |
complex(dp) :: res(A%idx%n_atoms) |
| 449 |
#endif |
| 450 |
integer, intent(in) :: k_atom |
| 451 |
|
| 452 |
integer :: my_i_atom, my_j_atom, i_atom, j_atom |
| 453 |
|
| 454 |
res(:) = 0d0 |
| 455 |
my_i_atom = findval(A%idx%i_atom, k_atom) |
| 456 |
if (my_i_atom > 0) then |
| 457 |
do my_j_atom = 1, size(A%idx%j_atom) |
| 458 |
j_atom = A%idx%j_atom(my_j_atom) |
| 459 |
associate ( & |
| 460 |
A_sub => A%val(3 * (my_i_atom - 1) + 1:, 3 * (my_j_atom - 1) + 1:), & |
| 461 |
A_prime_sub => A_prime(:, 3 * (j_atom - 1) + 1:) & |
| 462 |
) |
| 463 |
res(j_atom) = -1d0 / 3 * sum(A_sub(:3, :3) * A_prime_sub(:, :3)) |
| 464 |
end associate |
| 465 |
end do |
| 466 |
end if |
| 467 |
my_j_atom = findval(A%idx%j_atom, k_atom) |
| 468 |
if (my_j_atom > 0) then |
| 469 |
do my_i_atom = 1, size(A%idx%i_atom) |
| 470 |
i_atom = A%idx%i_atom(my_i_atom) |
| 471 |
associate ( & |
| 472 |
B_sub => B%val(3 * (my_i_atom - 1) + 1:, 3 * (my_j_atom - 1) + 1:), & |
| 473 |
B_prime_sub => B_prime(3 * (i_atom - 1) + 1:, :) & |
| 474 |
) |
| 475 |
res(i_atom) = res(i_atom) + & |
| 476 |
(-1d0 / 3) * sum(B_prime_sub(:3, :) * B_sub(:3, :3)) |
| 477 |
end associate |
| 478 |
end do |
| 479 |
end if |
| 480 |
#ifdef WITH_SCALAPACK |
| 481 |
if (A%idx%parallel) call blacs_all_reduce(res, A%blacs) |
| 482 |
#endif |
| 483 |
end function |
| 484 |
|
| 485 |
#ifndef DO_COMPLEX_TYPE |
| 486 |
function matrix_re_contract_n33diag_cols(A) result(res) |
| 487 |
class(matrix_re_t), intent(in) :: A |
| 488 |
real(dp) :: res(A%idx%n_atoms) |
| 489 |
#else |
| 490 |
function matrix_cplx_contract_n33diag_cols(A) result(res) |
| 491 |
class(matrix_cplx_t), intent(in) :: A |
| 492 |
complex(dp) :: res(A%idx%n_atoms) |
| 493 |
#endif |
| 494 |
|
| 495 |
integer :: i_xyz, my_j_atom, j_atom |
| 496 |
|
| 497 |
res(:) = 0d0 |
| 498 |
do my_j_atom = 1, size(A%idx%j_atom) |
| 499 |
j_atom = A%idx%j_atom(my_j_atom) |
| 500 |
do i_xyz = 1, 3 |
| 501 |
res(j_atom) = res(j_atom) + & |
| 502 |
sum(A%val(i_xyz::3, 3 * (my_j_atom - 1) + i_xyz)) |
| 503 |
end do |
| 504 |
end do |
| 505 |
res = res / 3 |
| 506 |
#ifdef WITH_SCALAPACK |
| 507 |
if (A%idx%parallel) call blacs_all_reduce(res, A%blacs) |
| 508 |
#endif |
| 509 |
end function |
| 510 |
|
| 511 |
#ifndef DO_COMPLEX_TYPE |
| 512 |
function matrix_re_contract_n33_rows(A) result(res) |
| 513 |
class(matrix_re_t), intent(in) :: A |
| 514 |
real(dp) :: res(A%idx%n_atoms) |
| 515 |
#else |
| 516 |
function matrix_cplx_contract_n33_rows(A) result(res) |
| 517 |
class(matrix_cplx_t), intent(in) :: A |
| 518 |
complex(dp) :: res(A%idx%n_atoms) |
| 519 |
#endif |
| 520 |
|
| 521 |
integer :: my_i_atom, i_atom |
| 522 |
|
| 523 |
res(:) = 0d0 |
| 524 |
do my_i_atom = 1, size(A%idx%i_atom) |
| 525 |
i_atom = A%idx%i_atom(my_i_atom) |
| 526 |
associate (A_sub => A%val(3 * (my_i_atom - 1) + 1:, :)) |
| 527 |
res(i_atom) = res(i_atom) + sum(A_sub(:3, :)) |
| 528 |
end associate |
| 529 |
end do |
| 530 |
#ifdef WITH_SCALAPACK |
| 531 |
if (A%idx%parallel) call blacs_all_reduce(res, A%blacs) |
| 532 |
#endif |
| 533 |
end function |
| 534 |
|
| 535 |
#ifndef DO_COMPLEX_TYPE |
| 536 |
type(matrix_re_t) function matrix_re_mmul( & |
| 537 |
A, B, transA, transB) result(C) |
| 538 |
class(matrix_re_t), intent(in) :: A |
| 539 |
type(matrix_re_t), intent(in) :: B |
| 540 |
#else |
| 541 |
type(matrix_cplx_t) function matrix_cplx_mmul( & |
| 542 |
A, B, transA, transB) result(C) |
| 543 |
class(matrix_cplx_t), intent(in) :: A |
| 544 |
type(matrix_cplx_t), intent(in) :: B |
| 545 |
#endif |
| 546 |
character, intent(in), optional :: transA, transB |
| 547 |
|
| 548 |
C%idx = A%idx |
| 549 |
#ifdef WITH_SCALAPACK |
| 550 |
C%blacs = A%blacs |
| 551 |
if (.not. A%idx%parallel) then |
| 552 |
C%val = mmul(A%val, B%val, transA, transB) |
| 553 |
else |
| 554 |
C%val = pmmul(A%val, A%blacs, B%val, B%blacs, transA, transB, C%blacs) |
| 555 |
end if |
| 556 |
#else |
| 557 |
C%val = mmul(A%val, B%val, transA, transB) |
| 558 |
#endif |
| 559 |
end function |
| 560 |
|
| 561 |
#ifndef DO_COMPLEX_TYPE |
| 562 |
# define DO_COMPLEX_TYPE |
| 563 |
#include "mbd_matrix.F90" |
| 564 |
|
| 565 |
subroutine matrix_re_invh(A, exc, src, clock) |
| 566 |
class(matrix_re_t), intent(inout) :: A |
| 567 |
type(matrix_re_t), intent(in), optional :: src |
| 568 |
type(exception_t), intent(out), optional :: exc |
| 569 |
type(clock_t), intent(inout), optional :: clock |
| 570 |
|
| 571 |
#ifdef WITH_SCALAPACK |
| 572 |
if (.not. A%idx%parallel) then |
| 573 |
if (present(src)) then |
| 574 |
call invh(A%val, exc, src%val) |
| 575 |
else |
| 576 |
call invh(A%val, exc) |
| 577 |
end if |
| 578 |
else |
| 579 |
if (present(src)) then |
| 580 |
call pinvh(A%val, A%blacs, exc, src%val, clock=clock) |
| 581 |
else |
| 582 |
call pinvh(A%val, A%blacs, exc, clock=clock) |
| 583 |
end if |
| 584 |
end if |
| 585 |
#else |
| 586 |
if (present(src)) then |
| 587 |
call invh(A%val, exc, src%val) |
| 588 |
else |
| 589 |
call invh(A%val, exc) |
| 590 |
end if |
| 591 |
#endif |
| 592 |
end subroutine |
| 593 |
|
| 594 |
function matrix_re_eigvals(A, exc, destroy) result(eigs) |
| 595 |
class(matrix_re_t), target, intent(in) :: A |
| 596 |
type(exception_t), intent(out), optional :: exc |
| 597 |
logical, intent(in), optional :: destroy |
| 598 |
complex(dp) :: eigs(3 * A%idx%n_atoms) |
| 599 |
|
| 600 |
#ifdef WITH_SCALAPACK |
| 601 |
if (A%idx%parallel) then |
| 602 |
exc%code = MBD_EXC_UNIMPL |
| 603 |
exc%msg = 'Complex general matrix diagonalization not implemented for scalapack' |
| 604 |
else |
| 605 |
eigs = eigvals(A%val, exc, destroy) |
| 606 |
end if |
| 607 |
#else |
| 608 |
eigs = eigvals(A%val, exc, destroy) |
| 609 |
#endif |
| 610 |
end function |
| 611 |
|
| 612 |
end module |
| 613 |
|
| 614 |
#endif |