| 1 |
#!/usr/bin/env python3 |
| 2 |
import sys |
| 3 |
from argparse import ArgumentParser |
| 4 |
from functools import partial |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
|
| 8 |
from pymbd import __version__ as _version, ang |
| 9 |
from pymbd.fortran import LIBMBD_VERSION, MBDGeom, with_mpi |
| 10 |
|
| 11 |
__all__ = () |
| 12 |
|
| 13 |
# fmt: off |
| 14 |
unit_cell = ( |
| 15 |
np.array([ |
| 16 |
(4.083, 5.700, 2.856), (0.568, 0.095, 4.217), (0.470, 4.774, 3.551), |
| 17 |
(4.181, 1.022, 3.522), (5.572, 5.587, 1.892), (-0.920, 0.209, 5.181), |
| 18 |
(3.663, 3.255, 2.585), (0.988, 2.541, 4.488), (3.834, 4.011, 0.979), |
| 19 |
(0.816, 1.785, 6.094), (2.223, 1.314, 1.108), (2.428, 4.481, 5.965), |
| 20 |
(1.177, 0.092, 0.406), (3.474, 5.703, 6.667), (4.911, 5.036, 2.573), |
| 21 |
(-0.260, 0.759, 4.500), (4.358, 3.787, 1.918), (0.293, 2.009, 5.155), |
| 22 |
(0.205, 1.729, 1.101), (4.446, 4.067, 5.972), (1.285, 0.947, 0.957), |
| 23 |
(3.366, 4.848, 6.116), (0.485, 2.901, 1.709), (4.165, 2.895, 5.364), |
| 24 |
(4.066, 1.426, 0.670), (0.585, 4.369, 6.403)]) * ang, |
| 25 |
np.array([ |
| 26 |
(5.008, 0.018, -0.070), |
| 27 |
(1.630, 6.759, 0.064), |
| 28 |
(-1.987, -0.981, 7.079)]) * ang, |
| 29 |
list('HHHHHHHHHHHHHHCCCCCCNNOOOO'), |
| 30 |
[0.703, 0.703, 0.726, 0.726, 0.731, 0.731, |
| 31 |
0.727, 0.727, 0.754, 0.754, 0.750, 0.750, |
| 32 |
0.755, 0.755, 0.809, 0.809, 0.827, 0.827, |
| 33 |
0.834, 0.834, 0.840, 0.840, 0.886, 0.886, |
| 34 |
0.892, 0.892]) |
| 35 |
# fmt: on |
| 36 |
|
| 37 |
RANK = None |
| 38 |
|
| 39 |
|
| 40 |
def parse(output): |
| 41 |
blocks = [ |
| 42 |
[l.split() for l in block.strip().split('\n')] |
| 43 |
for block in output.split('--------------')[1:-1] |
| 44 |
] |
| 45 |
n_atoms = int(blocks[0][0][-1]) |
| 46 |
version_pymbd = blocks[0][1][-1] |
| 47 |
version_libmbd = blocks[0][2][-1] |
| 48 |
block_size, grid = None, None |
| 49 |
block = iter(blocks[1]) |
| 50 |
for words in block: |
| 51 |
if words[0] == 'id': |
| 52 |
break |
| 53 |
elif words[0] == 'BLACS': |
| 54 |
if words[1] == 'block': |
| 55 |
block_size = int(words[-1]) |
| 56 |
else: |
| 57 |
grid = [int(words[-3]), int(words[-1])] |
| 58 |
timing = [ |
| 59 |
{ |
| 60 |
'id': int(words[0]), |
| 61 |
'level': int(words[1]), |
| 62 |
'label': ' '.join(words[2:-2]), |
| 63 |
'count': int(words[-2]), |
| 64 |
'time': float(words[-1]), |
| 65 |
} |
| 66 |
for words in block |
| 67 |
] |
| 68 |
energy = float(blocks[2][0][-1]) |
| 69 |
return { |
| 70 |
'version_pymbd': version_pymbd, |
| 71 |
'version_libmbd': version_libmbd, |
| 72 |
'n_atoms': n_atoms, |
| 73 |
'timing': timing, |
| 74 |
'energy': energy, |
| 75 |
'block_size': block_size, |
| 76 |
'grid': grid, |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
def make_supercell(coords, lattice, species, vol_ratios, sc): |
| 81 |
sc = np.array(sc) |
| 82 |
n_uc = np.prod(sc) |
| 83 |
c = np.stack( |
| 84 |
np.meshgrid(range(sc[0]), range(sc[1]), range(sc[2])), axis=-1 |
| 85 |
).reshape(-1, 3) |
| 86 |
coords = ((c @ lattice)[:, None, :] + coords).reshape(-1, 3) |
| 87 |
lattice = lattice * sc[:, None] |
| 88 |
species = n_uc * species |
| 89 |
vol_ratios = n_uc * vol_ratios |
| 90 |
return coords, lattice, species, vol_ratios |
| 91 |
|
| 92 |
|
| 93 |
def _print(*args): |
| 94 |
if not RANK: |
| 95 |
print(*args) |
| 96 |
|
| 97 |
|
| 98 |
def run(supercell, k_grid, finite, force, method, early_return, repeat): |
| 99 |
if with_mpi: |
| 100 |
from mpi4py import MPI |
| 101 |
|
| 102 |
global RANK |
| 103 |
RANK = MPI.COMM_WORLD.Get_rank() |
| 104 |
_print('--------------') |
| 105 |
coords, lattice, species, vol_ratios = make_supercell(*unit_cell, supercell) |
| 106 |
_print('number of atoms:', len(coords)) |
| 107 |
_print('pyMBD version:', '.'.join(map(str, _version))) |
| 108 |
_print('libMBD version:', '{}.{}.{}-{}'.format(*LIBMBD_VERSION)) |
| 109 |
_print('--------------') |
| 110 |
if early_return: |
| 111 |
return |
| 112 |
geom = MBDGeom(coords) if finite else MBDGeom(coords, lattice, k_grid) |
| 113 |
if method == 'mbd@rsscs': |
| 114 |
func = geom.mbd_energy_species |
| 115 |
elif method == 'mbd': |
| 116 |
func = partial(geom.mbd_energy_species, variant='plain') |
| 117 |
elif method == 'ts': |
| 118 |
func = geom.ts_energy_species |
| 119 |
with geom: |
| 120 |
for _ in range(repeat): |
| 121 |
res = func(species, vol_ratios, 0.83, force=force) |
| 122 |
geom.print_timing() |
| 123 |
if force: |
| 124 |
ene, grad, *_ = res |
| 125 |
else: |
| 126 |
ene = res |
| 127 |
ene = ene / len(coords) |
| 128 |
_print('--------------') |
| 129 |
_print('energy:', ene) |
| 130 |
if force: |
| 131 |
_print('grad[0]:', grad[0]) |
| 132 |
_print('--------------') |
| 133 |
|
| 134 |
|
| 135 |
def main(args): |
| 136 |
parser = ArgumentParser() |
| 137 |
parser.add_argument( |
| 138 |
'--parse', |
| 139 |
action='store_true', |
| 140 |
help='parse stdin and output JSON', |
| 141 |
) |
| 142 |
parser.add_argument( |
| 143 |
'--supercell', |
| 144 |
nargs=3, |
| 145 |
type=int, |
| 146 |
default=[1, 1, 1], |
| 147 |
metavar='N', |
| 148 |
help='supercell definition', |
| 149 |
) |
| 150 |
parser.add_argument( |
| 151 |
'--k-grid', |
| 152 |
nargs=3, |
| 153 |
type=int, |
| 154 |
default=[1, 1, 1], |
| 155 |
metavar='N', |
| 156 |
help='k-grid definition', |
| 157 |
) |
| 158 |
parser.add_argument( |
| 159 |
'--finite', |
| 160 |
action='store_true', |
| 161 |
help='run a finite system', |
| 162 |
) |
| 163 |
parser.add_argument( |
| 164 |
'--no-force', |
| 165 |
action='store_false', |
| 166 |
dest='force', |
| 167 |
help='switch off calculation of gradients', |
| 168 |
) |
| 169 |
parser.add_argument( |
| 170 |
'--method', |
| 171 |
default='mbd@rsscs', |
| 172 |
choices=['mbd@rsscs', 'mbd', 'ts'], |
| 173 |
help='method to run', |
| 174 |
) |
| 175 |
parser.add_argument( |
| 176 |
'--early-return', |
| 177 |
action='store_true', |
| 178 |
help='return before doing any work', |
| 179 |
) |
| 180 |
parser.add_argument( |
| 181 |
'--repeat', |
| 182 |
type=int, |
| 183 |
default=1, |
| 184 |
help='run benchmark repeatedly', |
| 185 |
) |
| 186 |
args = vars(parser.parse_args(args)) |
| 187 |
if args.pop('parse', False): |
| 188 |
from pprint import pprint |
| 189 |
|
| 190 |
pprint(parse(sys.stdin.read())) |
| 191 |
else: |
| 192 |
run(**args) |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == '__main__': |
| 196 |
main(sys.argv[1:]) |