-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abf29d0
commit 240d197
Showing
58 changed files
with
31,068 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .pymbd import * | ||
from .lib import mbd as lib | ||
from .lib import mbd_math as lib_math | ||
from .lib import mbd_repulsion as lib_coul |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
import json | ||
import sys | ||
import numpy as np | ||
|
||
from .lib import mbd as _mbd | ||
from .vdw_param import vdw_param as free_atom_db | ||
|
||
try: | ||
from mpi4py import MPI | ||
ntasks = MPI.COMM_WORLD.Get_size() | ||
myid = MPI.COMM_WORLD.Get_rank() | ||
except ImportError: | ||
ntasks = 1 | ||
myid = 0 | ||
|
||
_mbd.my_task = myid | ||
_mbd.n_tasks = ntasks | ||
|
||
bohr = _mbd.bohr | ||
|
||
|
||
def get_free_atom_data(species): | ||
return list(map( | ||
list, | ||
zip(*[ | ||
(at['alpha_0'], at['C6'], at['R_vdw']) for at in | ||
[free_atom_db[sp] for sp in species] | ||
]) | ||
)) | ||
|
||
|
||
def printout(s, each=False): | ||
if each or myid == 0: | ||
sys.stdout.write('{}\n'.format(s)) | ||
|
||
|
||
def printerr(s, each=False): | ||
if each or myid == 0: | ||
sys.stderr.write('{}\n'.format(s)) | ||
|
||
|
||
def get_damping(xc): | ||
return dict(zip(['ts_d', 'ts_s_r', 'mbd_scs_a', 'mbd_ts_a', | ||
'mbd_ts_erf_beta', 'mbd_ts_fermi_beta', 'mbd_rsscs_a', | ||
'mbd_rsscs_beta'], | ||
_mbd.get_damping_parameters(xc))) | ||
|
||
|
||
def scale_hirsh(hirsh, alpha, C6, R_vdw): | ||
hirsh = np.array(hirsh) | ||
return [np.array(q)*hirsh**factor | ||
for q, factor in zip([alpha, C6, R_vdw], [1, 2, 1/3])] | ||
|
||
|
||
def mbd_rsscs( | ||
coords, species, volumes, beta, lattice=None, k_grid=None, supercell=None, vacuum=None, | ||
custom_params=None, get_spectrum=False | ||
): | ||
_mbd.param_vacuum_axis = [False]*3 if vacuum is None else vacuum | ||
mode = 'P' if ntasks > 1 else '' | ||
params = get_free_atom_data(species) | ||
if custom_params: | ||
for i, specie in enumerate(species): | ||
if specie in custom_params: | ||
for j, paramname in enumerate(['alpha_0', 'C6', 'R_vdw']): | ||
params[j][i] = custom_params[specie][paramname] | ||
alpha_0, C6, R_vdw = scale_hirsh(volumes, *params) | ||
if lattice is not None: | ||
mode += 'C' | ||
alpha_scs_dyn = _mbd.run_scs( | ||
mode, 'fermi,dip,gg', coords, | ||
_mbd.alpha_dynamic_ts_all('C', _mbd.n_grid_omega, alpha_0, c6=C6), | ||
unit_cell=lattice, | ||
r_vdw=R_vdw, beta=beta, a=6. | ||
) | ||
C6_scs = _mbd.get_c6_from_alpha(alpha_scs_dyn) | ||
R_vdw_scs = R_vdw*(alpha_scs_dyn[0]/alpha_0)**(1/3) | ||
if get_spectrum: | ||
mode += 'EV' | ||
if k_grid is not None: | ||
mode = mode.replace('C', 'R') | ||
k_grid = _mbd.make_k_grid(_mbd.make_g_grid(*k_grid), lattice) | ||
ene, *spectrum = _mbd.get_reciprocal_mbd_energy( | ||
mode, 'fermi,dip', coords, | ||
alpha_scs_dyn[0], | ||
_mbd.omega_eff(C6_scs, alpha_scs_dyn[0]), | ||
k_grid, | ||
unit_cell=lattice, | ||
r_vdw=R_vdw_scs, beta=beta, a=6. | ||
) | ||
elif supercell is not None: | ||
ene, *spectrum = _mbd.get_supercell_mbd_energy( | ||
mode, 'fermi,dip', coords, | ||
alpha_scs_dyn[0], | ||
_mbd.omega_eff(C6_scs, alpha_scs_dyn[0]), | ||
unit_cell=lattice, | ||
supercell=supercell, | ||
r_vdw=R_vdw_scs, beta=beta, a=6. | ||
) | ||
else: | ||
ene, *spectrum = _mbd.get_single_mbd_energy( | ||
mode, 'fermi,dip', coords, | ||
alpha_scs_dyn[0], | ||
_mbd.omega_eff(C6_scs, alpha_scs_dyn[0]), | ||
r_vdw=R_vdw_scs, beta=beta, a=6. | ||
) | ||
if get_spectrum: | ||
return (ene, *spectrum) | ||
return ene | ||
|
||
|
||
def mbd_rsscs_deriv( | ||
coords, species, volumes, beta, lattice=None, k_grid=None, supercell=None, | ||
delta=1e-4 | ||
): | ||
forces = np.zeros(coords.shape) | ||
for i_atom in range(coords.shape[0]): | ||
for i_xyz in range(3): | ||
ene = [] | ||
for step in [-1, 1]: | ||
coords_diff = coords.copy() | ||
coords_diff[i_atom, i_xyz] += step*delta | ||
ene.append(mbd_rsscs( | ||
coords_diff, species, volumes, beta, lattice, k_grid, supercell | ||
)) | ||
forces[i_atom, i_xyz] = -(ene[1]-ene[0])/(2*delta) | ||
if lattice is None: | ||
return forces | ||
stress = np.zeros((3, 3)) | ||
for i_xyz in range(3): | ||
for j_xyz in range(3): | ||
ene = [] | ||
for step in [-1, 1]: | ||
strain = np.eye(3) | ||
strain[i_xyz, j_xyz] += step*delta | ||
coords_diff = coords.dot(strain) | ||
lattice_diff = lattice.dot(strain) | ||
ene.append(mbd_rsscs( | ||
coords_diff, species, volumes, beta, lattice_diff, k_grid, supercell | ||
)) | ||
stress[i_xyz, j_xyz] = -(ene[1]-ene[0])/(2*delta) | ||
return forces, stress | ||
|
||
|
||
def mbd_coul(coords, species, volumes, beta, lattice=None, k_grid=None, | ||
supercell=None, vacuum=None, custom_params=None): | ||
mode = 'P' | ||
_mbd.param_vacuum_axis = [False]*3 if vacuum is None else vacuum | ||
mode = 'P' if ntasks > 1 else '' | ||
params = get_free_atom_data(species) | ||
alpha_0, C6, R_vdw = scale_hirsh(volumes, *params) | ||
alpha_scs_dyn = _mbd.run_scs( | ||
mode, 'fermi,dip,gg', coords, | ||
_mbd.alpha_dynamic_ts_all('C', _mbd.n_grid_omega, alpha_0, c6=C6), | ||
unit_cell=lattice, | ||
r_vdw=R_vdw, beta=beta, a=6. | ||
) | ||
C6_scs = _mbd.get_c6_from_alpha(alpha_scs_dyn) | ||
R_vdw_scs = R_vdw*(alpha_scs_dyn[0]/alpha_0)**(1/3) | ||
ene = _mbd.get_single_mbd_energy( | ||
mode, 'fermi,dip', coords, | ||
alpha_scs_dyn[0], | ||
_mbd.omega_eff(C6_scs, alpha_scs_dyn[0]), | ||
r_vdw=R_vdw_scs, beta=beta, a=6. | ||
)[0] | ||
return ene | ||
|
||
|
||
class ArrayEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
try: | ||
return obj.tolist() | ||
except AttributeError: | ||
return super().default(obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import numpy as np | ||
from numpy import sqrt | ||
import unittest | ||
|
||
import pymbd | ||
|
||
|
||
class TestFreqGrid(unittest.TestCase): | ||
pass | ||
|
||
|
||
for n in range(15, 40, 5): | ||
def test(self, n=n): | ||
grid1 = -pymbd.lib.gauss_legendre(n)[0] | ||
grid2 = np.polynomial.legendre.leggauss(n)[0] | ||
self.assertAlmostEqual(0, np.linalg.norm(grid1-grid2), 8) | ||
if n > 25: | ||
test = unittest.expectedFailure(test) | ||
setattr(TestFreqGrid, 'test_frequency_grid_{}'.format(n), test) | ||
|
||
|
||
class TestMBD(unittest.TestCase): | ||
def setUp(self): | ||
pymbd.lib.init_grid(15) | ||
|
||
def test_argon_dimer(self): | ||
ene = pymbd.mbd_rsscs( | ||
[[0, 0, 0], [4.0/pymbd.bohr, 0, 0]], | ||
['Ar', 'Ar'], | ||
[1., 1.], | ||
0.83 | ||
) | ||
self.assertAlmostEqual(ene, -0.0002462647623815428, 10) | ||
|
||
def tearDown(self): | ||
pymbd.lib.destroy_grid() | ||
|
||
|
||
class TestCoulomb(unittest.TestCase): | ||
def test_basic(self): | ||
a = 1/sqrt(2) | ||
C = np.array([ | ||
[ 0, 0, -a, 0, -a, 0], # noqa | ||
[ 0, a, 0, a, 0, 0], # noqa | ||
[-a, 0, 0, 0, 0, -a], # noqa | ||
[ 0, 0, a, 0, -a, 0], # noqa | ||
[ 0, -a, 0, a, 0, 0], # noqa | ||
[-a, 0, 0, 0, 0, a], # noqa | ||
], dtype=float) | ||
coords = [[0, 0, 0], [0, 0, 1.1]] | ||
n = len(coords) | ||
e1, eatt, erep = pymbd.lib_coul.fullcoulomb( | ||
C, coords, n*[1], n*[1], 3*n*[2], n*[2] | ||
) | ||
self.assertAlmostEqual(e1, -0.058346493647401076, 10) | ||
self.assertAlmostEqual(eatt, 1.767623829681647, 10) | ||
self.assertAlmostEqual(erep, 0.800186426943337, 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Any copyright is dedicated to the Public Domain. | ||
# http://creativecommons.org/publicdomain/zero/1.0/ | ||
|
||
vdw_param = { | ||
"H": {"alpha_0": 4.5, "C6": 6.5, "R_vdw": 3.1}, | ||
"He": {"alpha_0": 1.38, "C6": 1.46, "R_vdw": 2.65}, | ||
"Li": {"alpha_0": 164.2, "C6": 1387., "R_vdw": 4.16}, | ||
"Be": {"alpha_0": 38., "C6": 214., "R_vdw": 4.17}, | ||
"B": {"alpha_0": 21., "C6": 99.5, "R_vdw": 3.89}, | ||
"C": {"alpha_0": 12., "C6": 46.6, "R_vdw": 3.59}, | ||
"N": {"alpha_0": 7.4, "C6": 24.2, "R_vdw": 3.34}, | ||
"O": {"alpha_0": 5.4, "C6": 15.6, "R_vdw": 3.19}, | ||
"F": {"alpha_0": 3.8, "C6": 9.52, "R_vdw": 3.04}, | ||
"Ne": {"alpha_0": 2.67, "C6": 6.38, "R_vdw": 2.91}, | ||
"Na": {"alpha_0": 13.6, "C6": 69.3, "R_vdw": 1.63}, | ||
"Mg": {"alpha_0": 71., "C6": 627., "R_vdw": 4.27}, | ||
"Al": {"alpha_0": 60., "C6": 528., "R_vdw": 4.33}, | ||
"Si": {"alpha_0": 37., "C6": 305., "R_vdw": 4.2}, | ||
"P": {"alpha_0": 25., "C6": 185., "R_vdw": 4.01}, | ||
"S": {"alpha_0": 19.6, "C6": 134., "R_vdw": 3.86}, | ||
"Cl": {"alpha_0": 15., "C6": 94.6, "R_vdw": 3.71}, | ||
"Ar": {"alpha_0": 11.1, "C6": 64.3, "R_vdw": 3.55}, | ||
"K": {"alpha_0": 292.9, "C6": 3897., "R_vdw": 3.71}, | ||
"Ca": {"alpha_0": 160., "C6": 2221., "R_vdw": 4.65}, | ||
"Sc": {"alpha_0": 120., "C6": 1383., "R_vdw": 4.59}, | ||
"Ti": {"alpha_0": 98., "C6": 1044., "R_vdw": 4.51}, | ||
"V": {"alpha_0": 84., "C6": 832., "R_vdw": 4.44}, | ||
"Cr": {"alpha_0": 78., "C6": 602., "R_vdw": 3.99}, | ||
"Mn": {"alpha_0": 63., "C6": 552., "R_vdw": 3.97}, | ||
"Fe": {"alpha_0": 56., "C6": 482., "R_vdw": 4.23}, | ||
"Co": {"alpha_0": 50., "C6": 408., "R_vdw": 4.18}, | ||
"Ni": {"alpha_0": 48., "C6": 373., "R_vdw": 3.82}, | ||
"Cu": {"alpha_0": 42., "C6": 253., "R_vdw": 3.76}, | ||
"Zn": {"alpha_0": 40., "C6": 284., "R_vdw": 4.02}, | ||
"Ga": {"alpha_0": 60., "C6": 498., "R_vdw": 4.19}, | ||
"Ge": {"alpha_0": 41., "C6": 354., "R_vdw": 4.2}, | ||
"As": {"alpha_0": 29., "C6": 246., "R_vdw": 4.11}, | ||
"Se": {"alpha_0": 25., "C6": 210., "R_vdw": 4.04}, | ||
"Br": {"alpha_0": 20., "C6": 162., "R_vdw": 3.93}, | ||
"Kr": {"alpha_0": 16.8, "C6": 129.6, "R_vdw": 3.82}, | ||
"Rb": {"alpha_0": 319.2, "C6": 4691., "R_vdw": 3.72}, | ||
"Sr": {"alpha_0": 199., "C6": 3170., "R_vdw": 4.54}, | ||
"Y": {"alpha_0": 126.7370, "C6": 1968.580, "R_vdw": 4.81510}, | ||
"Zr": {"alpha_0": 119.97, "C6": 1677.91, "R_vdw": 4.53}, | ||
"Nb": {"alpha_0": 101.603, "C6": 1263.61, "R_vdw": 4.2365}, | ||
"Mo": {"alpha_0": 88.4225785, "C6": 1028.73, "R_vdw": 4.099}, | ||
"Tc": {"alpha_0": 80.083, "C6": 1390.87, "R_vdw": 4.076}, | ||
"Ru": {"alpha_0": 65.8950, "C6": 609.754, "R_vdw": 3.99530}, | ||
"Rh": {"alpha_0": 56.1, "C6": 469.0, "R_vdw": 3.95}, | ||
"Pd": {"alpha_0": 23.68, "C6": 157.5, "R_vdw": 3.66}, | ||
"Ag": {"alpha_0": 50.6, "C6": 339., "R_vdw": 3.82}, | ||
"Cd": {"alpha_0": 39.7, "C6": 452.0, "R_vdw": 3.99}, | ||
"In": {"alpha_0": 70.22, "C6": 707.046, "R_vdw": 4.23198}, | ||
"Sn": {"alpha_0": 55.95, "C6": 587.417, "R_vdw": 4.303}, | ||
"Sb": {"alpha_0": 43.67197, "C6": 459.322, "R_vdw": 4.276}, | ||
"Te": {"alpha_0": 37.65, "C6": 396.0, "R_vdw": 4.22}, | ||
"I": {"alpha_0": 35., "C6": 385., "R_vdw": 4.17}, | ||
"Xe": {"alpha_0": 27.3, "C6": 285.9, "R_vdw": 4.08}, | ||
"Cs": {"alpha_0": 427.12, "C6": 6582.08, "R_vdw": 3.78}, | ||
"Ba": {"alpha_0": 275.0, "C6": 5727.0, "R_vdw": 4.77}, | ||
"Hf": {"alpha_0": 99.52, "C6": 1274.8, "R_vdw": 4.21}, | ||
"Ta": {"alpha_0": 82.53, "C6": 1019.92, "R_vdw": 4.15}, | ||
"W": {"alpha_0": 71.041, "C6": 847.93, "R_vdw": 4.08}, | ||
"Re": {"alpha_0": 63.04, "C6": 710.2, "R_vdw": 4.02}, | ||
"Os": {"alpha_0": 55.055, "C6": 596.67, "R_vdw": 3.84}, | ||
"Ir": {"alpha_0": 42.51, "C6": 359.1, "R_vdw": 4.00}, | ||
"Pt": {"alpha_0": 39.68, "C6": 347.1, "R_vdw": 3.92}, | ||
"Au": {"alpha_0": 36.5, "C6": 298.0, "R_vdw": 3.86}, | ||
"Hg": {"alpha_0": 33.9, "C6": 392.0, "R_vdw": 3.98}, | ||
"Tl": {"alpha_0": 69.92, "C6": 717.44, "R_vdw": 3.91}, | ||
"Pb": {"alpha_0": 61.8, "C6": 697.0, "R_vdw": 4.31}, | ||
"Bi": {"alpha_0": 49.02, "C6": 571.0, "R_vdw": 4.32}, | ||
"Po": {"alpha_0": 45.013, "C6": 530.92, "R_vdw": 4.097}, | ||
"At": {"alpha_0": 38.93, "C6": 457.53, "R_vdw": 4.07}, | ||
"Rn": {"alpha_0": 33.54, "C6": 390.63, "R_vdw": 4.23} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .pymbd import * | ||
from .lib import mbd as lib | ||
from .lib import mbd_math as lib_math | ||
from .lib import mbd_repulsion as lib_coul |
Oops, something went wrong.