1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
import sys
from typing import Dict, Any
import numpy as np
from ase.calculators.calculator import (get_calculator_class,
names as calcnames,
PropertyNotImplementedError)
from ase.constraints import FixAtoms, UnitCellFilter
from ase.eos import EquationOfState
from ase.io import read, write, Trajectory
from ase.optimize import LBFGS
import ase.db as db
class CLICommand:
"""Run calculation with one of ASE's calculators.
Four types of calculations can be done:
* single point
* atomic relaxations
* unit cell + atomic relaxations
* equation-of-state
Examples of the four types of calculations:
ase run emt h2o.xyz
ase run emt h2o.xyz -f 0.01
ase run emt cu.traj -s 0.01
ase run emt cu.traj -E 5,2.0
"""
@staticmethod
def add_arguments(parser):
parser.add_argument('calculator',
help='Name of calculator to use. '
'Must be one of: {}.'
.format(', '.join(calcnames)))
CLICommand.add_more_arguments(parser)
@staticmethod
def add_more_arguments(parser):
add = parser.add_argument
add('name', nargs='?', default='-',
help='Read atomic structure from this file.')
add('-p', '--parameters', default='',
metavar='key=value,...',
help='Comma-separated key=value pairs of ' +
'calculator specific parameters.')
add('-t', '--tag',
help='String tag added to filenames.')
add('--properties', default='efsdMm',
help='Default value is "efsdMm" meaning calculate energy, ' +
'forces, stress, dipole moment, total magnetic moment and ' +
'atomic magnetic moments.')
add('-f', '--maximum-force', type=float,
help='Relax internal coordinates.')
add('--constrain-tags',
metavar='T1,T2,...',
help='Constrain atoms with tags T1, T2, ...')
add('-s', '--maximum-stress', type=float,
help='Relax unit-cell and internal coordinates.')
add('-E', '--equation-of-state',
help='Use "-E 5,2.0" for 5 lattice constants ranging from '
'-2.0 %% to +2.0 %%.')
add('--eos-type', default='sjeos', help='Selects the type of eos.')
add('-o', '--output', help='Write result to file (append mode).')
add('--modify', metavar='...',
help='Modify atoms with Python statement. ' +
'Example: --modify="atoms.positions[-1,2]+=0.1".')
add('--after', help='Perform operation after calculation. ' +
'Example: --after="atoms.calc.write(...)"')
@staticmethod
def run(args):
runner = Runner()
runner.parse(args)
runner.run()
class Runner:
def __init__(self):
self.args = None
self.calculator_name = None
def parse(self, args):
self.calculator_name = args.calculator
self.args = args
def run(self):
args = self.args
atoms = self.build(args.name)
if args.modify:
exec(args.modify, {'atoms': atoms, 'np': np})
if args.name == '-':
args.name = 'stdin'
self.set_calculator(atoms, args.name)
self.calculate(atoms, args.name)
def calculate(self, atoms, name):
args = self.args
if args.maximum_force or args.maximum_stress:
self.optimize(atoms, name)
if args.equation_of_state:
self.eos(atoms, name)
self.calculate_once(atoms)
if args.after:
exec(args.after, {'atoms': atoms})
if args.output:
write(args.output, atoms, append=True)
def build(self, name):
if name == '-':
con = db.connect(sys.stdin, 'json')
return con.get_atoms(add_additional_information=True)
else:
atoms = read(name)
if isinstance(atoms, list):
assert len(atoms) == 1
atoms = atoms[0]
return atoms
def set_calculator(self, atoms, name):
cls = get_calculator_class(self.calculator_name)
parameters = str2dict(self.args.parameters)
if getattr(cls, 'nolabel', False):
atoms.calc = cls(**parameters)
else:
atoms.calc = cls(label=self.get_filename(name), **parameters)
def calculate_once(self, atoms):
args = self.args
for p in args.properties or 'efsdMm':
property, method = {'e': ('energy', 'get_potential_energy'),
'f': ('forces', 'get_forces'),
's': ('stress', 'get_stress'),
'd': ('dipole', 'get_dipole_moment'),
'M': ('magmom', 'get_magnetic_moment'),
'm': ('magmoms', 'get_magnetic_moments')}[p]
try:
getattr(atoms, method)()
except PropertyNotImplementedError:
pass
def optimize(self, atoms, name):
args = self.args
if args.constrain_tags:
tags = [int(t) for t in args.constrain_tags.split(',')]
mask = [t in tags for t in atoms.get_tags()]
atoms.constraints = FixAtoms(mask=mask)
logfile = self.get_filename(name, 'log')
if args.maximum_stress:
optimizer = LBFGS(UnitCellFilter(atoms), logfile=logfile)
fmax = args.maximum_stress
else:
optimizer = LBFGS(atoms, logfile=logfile)
fmax = args.maximum_force
trajectory = Trajectory(self.get_filename(name, 'traj'), 'w', atoms)
optimizer.attach(trajectory)
optimizer.run(fmax=fmax)
def eos(self, atoms, name):
args = self.args
traj = Trajectory(self.get_filename(name, 'traj'), 'w', atoms)
N, eps = args.equation_of_state.split(',')
N = int(N)
eps = float(eps) / 100
strains = np.linspace(1 - eps, 1 + eps, N)
v1 = atoms.get_volume()
volumes = strains**3 * v1
energies = []
cell1 = atoms.cell
for s in strains:
atoms.set_cell(cell1 * s, scale_atoms=True)
energies.append(atoms.get_potential_energy())
traj.write(atoms)
traj.close()
eos = EquationOfState(volumes, energies, args.eos_type)
v0, e0, B = eos.fit()
atoms.set_cell(cell1 * (v0 / v1)**(1 / 3), scale_atoms=True)
from ase.parallel import parprint as p
p('volumes:', volumes)
p('energies:', energies)
p('fitted energy:', e0)
p('fitted volume:', v0)
p('bulk modulus:', B)
p('eos type:', args.eos_type)
def get_filename(self, name: str, ext: str = '') -> str:
if '.' in name:
name = name.rsplit('.', 1)[0]
if self.args.tag is not None:
name += '-' + self.args.tag
if ext:
name += '.' + ext
return name
def str2dict(s: str, namespace={}, sep: str = '=') -> Dict[str, Any]:
"""Convert comma-separated key=value string to dictionary.
Examples:
>>> str2dict('xc=PBE,nbands=200,parallel={band:4}')
{'xc': 'PBE', 'nbands': 200, 'parallel': {'band': 4}}
>>> str2dict('a=1.2,b=True,c=ab,d=1,2,3,e={f:42,g:cd}')
{'a': 1.2, 'c': 'ab', 'b': True, 'e': {'g': 'cd', 'f': 42}, 'd': (1, 2, 3)}
"""
def myeval(value):
try:
value = eval(value, namespace)
except (NameError, SyntaxError):
pass
return value
dct = {}
strings = (s + ',').split(sep)
for i in range(len(strings) - 1):
key = strings[i]
m = strings[i + 1].rfind(',')
value: Any = strings[i + 1][:m]
if value[0] == '{':
assert value[-1] == '}'
value = str2dict(value[1:-1], namespace, ':')
elif value[0] == '(':
assert value[-1] == ')'
value = [myeval(t) for t in value[1:-1].split(',')]
else:
value = myeval(value)
dct[key] = value
strings[i + 1] = strings[i + 1][m + 1:]
return dct
|