| 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 |
module mbd_density |
| 5 |
|
| 6 |
use mbd_constants |
| 7 |
use mbd_geom, only: geom_t |
| 8 |
use mbd_linalg, only: diag |
| 9 |
use mbd_lapack, only: eigvalsh, inverse |
| 10 |
|
| 11 |
implicit none |
| 12 |
|
| 13 |
contains |
| 14 |
|
| 15 |
function eval_mbd_nonint_density(geom, pts, charges, masses, omegas) result(rho) |
| 16 |
type(geom_t), intent(in) :: geom |
| 17 |
real(dp), intent(in) :: pts(:, :), charges(:), masses(:), omegas(:) |
| 18 |
real(dp) :: rho(size(pts, 2)) |
| 19 |
|
| 20 |
integer :: i_pt, i_atom, n_atoms |
| 21 |
real(dp), dimension(:), allocatable :: pre, kernel, rsq |
| 22 |
|
| 23 |
pre = charges * (masses * omegas / pi)**(3.d0 / 2) |
| 24 |
kernel = masses * omegas |
| 25 |
n_atoms = geom%siz() |
| 26 |
rho(:) = 0.d0 |
| 27 |
allocate (rsq(n_atoms)) |
| 28 |
do i_pt = 1, size(pts, 2) |
| 29 |
do concurrent(i_atom=1:n_atoms) |
| 30 |
rsq(i_atom) = sum((pts(:, i_pt) - geom%coords(:, i_atom))**2) |
| 31 |
end do |
| 32 |
rho(i_pt) = sum(pre * exp(-kernel * rsq)) |
| 33 |
end do |
| 34 |
end function |
| 35 |
|
| 36 |
function eval_mbd_int_density(geom, pts, charges, masses, omegas, modes) result(rho) |
| 37 |
type(geom_t), intent(in) :: geom |
| 38 |
real(dp), intent(in) :: pts(:, :), charges(:), masses(:), omegas(:), modes(:, :) |
| 39 |
real(dp) :: rho(size(pts, 2)) |
| 40 |
|
| 41 |
integer :: i_pt, i_atom, n_atoms, i, i_xyz, j_xyz, self(3) |
| 42 |
real(dp) :: rdiffsq(3, 3), rdiff(3) |
| 43 |
integer, allocatable :: other(:) |
| 44 |
real(dp), allocatable :: pre(:), factor(:), omegas_p(:, :), kernel(:, :, :) |
| 45 |
|
| 46 |
omegas_p = matmul(matmul(modes, diag(omegas)), transpose(modes)) |
| 47 |
n_atoms = geom%siz() |
| 48 |
allocate (kernel(3, 3, n_atoms), source=0.d0) |
| 49 |
allocate (pre(n_atoms), source=0.d0) |
| 50 |
do i_atom = 1, n_atoms |
| 51 |
self = (/(3 * (i_atom - 1) + i, i=1, 3)/) |
| 52 |
other = (/(i, i=1, 3 * (i_atom - 1)), (i, i=3 * i_atom + 1, 3 * n_atoms)/) |
| 53 |
kernel(:, :, i_atom) = masses(i_atom) & |
| 54 |
* (omegas_p(self, self) & |
| 55 |
- matmul(matmul(omegas_p(self, other), inverse(omegas_p(other, other))), & |
| 56 |
omegas_p(other, self))) |
| 57 |
pre(i_atom) = charges(i_atom) * (masses(i_atom) / pi)**(3.d0 / 2) & |
| 58 |
* sqrt(product(omegas) / product(eigvalsh(omegas_p(other, other)))) |
| 59 |
end do |
| 60 |
rho(:) = 0.d0 |
| 61 |
allocate (factor(n_atoms)) |
| 62 |
do i_pt = 1, size(pts, 2) |
| 63 |
do i_atom = 1, n_atoms |
| 64 |
rdiff = pts(:, i_pt) - geom%coords(:, i_atom) |
| 65 |
do concurrent(i_xyz=1:3, j_xyz=1:3) |
| 66 |
rdiffsq(i_xyz, j_xyz) = rdiff(i_xyz) * rdiff(j_xyz) |
| 67 |
end do |
| 68 |
factor(i_atom) = sum(kernel(:, :, i_atom) * rdiffsq(:, :)) |
| 69 |
end do |
| 70 |
rho(i_pt) = sum(pre * exp(-factor)) |
| 71 |
end do |
| 72 |
end function |
| 73 |
|
| 74 |
end module |