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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
"""This module defines an ASE interface to FHI-aims.
Felix Hanke hanke@liverpool.ac.uk
Jonas Bjork j.bjork@liverpool.ac.uk
Simon P. Rittmeyer simon.rittmeyer@tum.de
Edits on (24.11.2021) by Thomas A. R. Purcell purcell@fhi-berlin.mpg.de
"""
import os
import re
import numpy as np
from ase.calculators.genericfileio import (
BaseProfile,
CalculatorTemplate,
GenericFileIOCalculator,
read_stdout,
)
from ase.io.aims import write_aims, write_control
def get_aims_version(string):
match = re.search(r'\s*FHI-aims version\s*:\s*(\S+)', string, re.M)
return match.group(1)
class AimsProfile(BaseProfile):
configvars = {'default_species_directory'}
def __init__(self, command, default_species_directory=None, **kwargs):
super().__init__(command, **kwargs)
self.default_species_directory = default_species_directory
def get_calculator_command(self, inputfile):
return []
def version(self):
return get_aims_version(read_stdout(self._split_command))
class AimsTemplate(CalculatorTemplate):
_label = 'aims'
def __init__(self):
super().__init__(
'aims',
[
'energy',
'free_energy',
'forces',
'stress',
'stresses',
'dipole',
'magmom',
],
)
self.outputname = f'{self._label}.out'
self.errorname = f'{self._label}.err'
def update_parameters(self, properties, parameters):
"""Check and update the parameters to match the desired calculation
Parameters
----------
properties: list of str
The list of properties to calculate
parameters: dict
The parameters used to perform the calculation.
Returns
-------
dict
The updated parameters object
"""
parameters = dict(parameters)
property_flags = {
'forces': 'compute_forces',
'stress': 'compute_analytical_stress',
'stresses': 'compute_heat_flux',
}
# Ensure FHI-aims will calculate all desired properties
for property in properties:
aims_name = property_flags.get(property, None)
if aims_name is not None:
parameters[aims_name] = True
if 'dipole' in properties:
if 'output' in parameters and 'dipole' not in parameters['output']:
parameters['output'] = list(parameters['output'])
parameters['output'].append('dipole')
elif 'output' not in parameters:
parameters['output'] = ['dipole']
return parameters
def write_input(self, profile, directory, atoms, parameters, properties):
"""Write the geometry.in and control.in files for the calculation
Parameters
----------
directory : Path
The working directory to store the input files.
atoms : atoms.Atoms
The atoms object to perform the calculation on.
parameters: dict
The parameters used to perform the calculation.
properties: list of str
The list of properties to calculate
"""
parameters = self.update_parameters(properties, parameters)
ghosts = parameters.pop('ghosts', None)
geo_constrain = parameters.pop('geo_constrain', None)
scaled = parameters.pop('scaled', None)
write_velocities = parameters.pop('write_velocities', None)
if scaled is None:
scaled = np.all(atoms.pbc)
if write_velocities is None:
write_velocities = atoms.has('momenta')
if geo_constrain is None:
geo_constrain = scaled and 'relax_geometry' in parameters
have_lattice_vectors = atoms.pbc.any()
have_k_grid = (
'k_grid' in parameters
or 'kpts' in parameters
or 'k_grid_density' in parameters
)
if have_lattice_vectors and not have_k_grid:
raise RuntimeError('Found lattice vectors but no k-grid!')
if not have_lattice_vectors and have_k_grid:
raise RuntimeError('Found k-grid but no lattice vectors!')
geometry_in = directory / 'geometry.in'
write_aims(
geometry_in,
atoms,
scaled,
geo_constrain,
write_velocities=write_velocities,
ghosts=ghosts,
)
control = directory / 'control.in'
if (
'species_dir' not in parameters
and profile.default_species_directory is not None
):
parameters['species_dir'] = profile.default_species_directory
write_control(control, atoms, parameters)
def execute(self, directory, profile):
profile.run(directory, None, self.outputname,
errorfile=self.errorname)
def read_results(self, directory):
from ase.io.aims import read_aims_results
dst = directory / self.outputname
return read_aims_results(dst, index=-1)
def load_profile(self, cfg, **kwargs):
return AimsProfile.from_config(cfg, self.name, **kwargs)
def socketio_argv(self, profile, unixsocket, port):
return [profile.command]
def socketio_parameters(self, unixsocket, port):
if port:
use_pimd_wrapper = ('localhost', port)
else:
# (INET port number should be unused.)
use_pimd_wrapper = (f'UNIX:{unixsocket}', 31415)
return dict(use_pimd_wrapper=use_pimd_wrapper, compute_forces=True)
class Aims(GenericFileIOCalculator):
def __init__(
self,
profile=None,
directory='.',
**kwargs,
):
"""Construct the FHI-aims calculator.
The keyword arguments (kwargs) can be one of the ASE standard
keywords: 'xc', 'kpts' and 'smearing' or any of FHI-aims'
native keywords.
Arguments:
cubes: AimsCube object
Cube file specification.
tier: int or array of ints
Set basis set tier for all atomic species.
plus_u : dict
For DFT+U. Adds a +U term to one specific shell of the species.
kwargs : dict
Any of the base class arguments.
"""
super().__init__(
template=AimsTemplate(),
profile=profile,
parameters=kwargs,
directory=directory,
)
class AimsCube:
'Object to ensure the output of cube files, can be attached to Aims object'
def __init__(
self,
origin=(0, 0, 0),
edges=[(0.1, 0.0, 0.0), (0.0, 0.1, 0.0), (0.0, 0.0, 0.1)],
points=(50, 50, 50),
plots=(),
):
"""parameters:
origin, edges, points:
Same as in the FHI-aims output
plots:
what to print, same names as in FHI-aims"""
self.name = 'AimsCube'
self.origin = origin
self.edges = edges
self.points = points
self.plots = plots
def ncubes(self):
"""returns the number of cube files to output"""
return len(self.plots)
def move_to_base_name(self, basename):
"""when output tracking is on or the base namem is not standard,
this routine will rename add the base to the cube file output for
easier tracking"""
for plot in self.plots:
found = False
cube = plot.split()
if (
cube[0] == 'total_density'
or cube[0] == 'spin_density'
or cube[0] == 'delta_density'
):
found = True
old_name = cube[0] + '.cube'
new_name = basename + '.' + old_name
if cube[0] == 'eigenstate' or cube[0] == 'eigenstate_density':
found = True
state = int(cube[1])
s_state = cube[1]
for i in [10, 100, 1000, 10000]:
if state < i:
s_state = '0' + s_state
old_name = cube[0] + '_' + s_state + '_spin_1.cube'
new_name = basename + '.' + old_name
if found:
# XXX Should not use platform dependent commands!
os.system('mv ' + old_name + ' ' + new_name)
def add_plot(self, name):
"""in case you forgot one ..."""
self.plots += [name]
def write(self, file):
"""write the necessary output to the already opened control.in"""
file.write('output cube ' + self.plots[0] + '\n')
file.write(' cube origin ')
for ival in self.origin:
file.write(str(ival) + ' ')
file.write('\n')
for i in range(3):
file.write(' cube edge ' + str(self.points[i]) + ' ')
for ival in self.edges[i]:
file.write(str(ival) + ' ')
file.write('\n')
if self.ncubes() > 1:
for i in range(self.ncubes() - 1):
file.write('output cube ' + self.plots[i + 1] + '\n')
|