Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed May 29, 2021
1 parent 7b7fa1e commit 76c5dba
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 168 deletions.
37 changes: 15 additions & 22 deletions aiida_phonopy/calcs/base.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
"""Base class of PhonopyCalculation and Phono3pyCalculation."""

from aiida.engine import CalcJob
from aiida.common import CalcInfo, CodeInfo
from aiida.plugins import DataFactory
from aiida.orm import Bool
from aiida_phonopy.common.file_generators import get_BORN_txt

Dict = DataFactory('dict')
StructureData = DataFactory('structure')
ArrayData = DataFactory('array')
Float = DataFactory('float')
Bool = DataFactory('bool')


class BasePhonopyCalculation(CalcJob):
"""
A basic plugin for calculating force constants using Phonopy.
"""A basic plugin for calculating force constants using Phonopy.
Requirement: the node should be able to import phonopy if NAC is used
"""

_INPUT_NAC = 'BORN'

@classmethod
def define(cls, spec):
"""
dataset : Dict, ArrayData, optional
In Dict, this is similar to phonopy.dataset. This can be either
type-I or type-II. In ArrayData, this can contain arrays of forces
and displacements like type-II phonopy dataset, for which the array
names are 'forces' and 'displacements'.
"""

"""Define inputs, outputs, and outline."""
super().define(spec)

spec.input('settings', valid_type=Dict,
help='Phonopy parameters')
spec.input('structure', valid_type=StructureData,
help='Unit cell structure')
spec.input('qpoint_mesh', valid_type=Dict,
help='Q-point sampling mesh information')
spec.input('symmetry_tolerance', valid_type=Float,
default=lambda: Float(1e-5))
spec.input('fc_only', valid_type=Bool,
help='Only force constants are calculated.',
default=lambda: Bool(False))
Expand All @@ -50,12 +48,7 @@ def define(cls, spec):
help='Displacements and forces dataset')

def prepare_for_submission(self, folder):
"""Create the input files from the input nodes passed to this instance of the `CalcJob`.
:param folder: an `aiida.common.folders.Folder` to temporarily write files on disk
:return: `aiida.common.datastructures.CalcInfo` instance
"""

"""Prepare calcinfo."""
self.logger.info("prepare_for_submission")

# These three lists are updated in self._create_additional_files(folder)
Expand All @@ -70,11 +63,11 @@ def prepare_for_submission(self, folder):
if (not self.inputs.fc_only and
'nac_params' in self.inputs and
'primitive' in self.inputs and
'symmetry_tolerance' in self.inputs.settings.attributes):
'symmetry_tolerance' in self.inputs):
born_txt = get_BORN_txt(
self.inputs.nac_params,
self.inputs.primitive,
self.inputs.settings['symmetry_tolerance'])
self.inputs.symmetry_tolerance)
with folder.open(self._INPUT_NAC, 'w', encoding='utf8') as handle:
handle.write(born_txt)
for params in self._additional_cmd_params:
Expand Down
4 changes: 3 additions & 1 deletion aiida_phonopy/calcs/phonopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PhonopyCalculation(BasePhonopyCalculation):

@classmethod
def define(cls, spec):
"""Define inputs, outputs, and outline."""
super().define(spec)

spec.input('projected_dos_filename', valid_type=Str,
Expand Down Expand Up @@ -55,6 +56,7 @@ def define(cls, spec):
help='Calculated phonon band structure')

def prepare_for_submission(self, folder):
"""Prepare calcinfo."""
return super().prepare_for_submission(folder)

def _create_additional_files(self, folder):
Expand All @@ -63,7 +65,7 @@ def _create_additional_files(self, folder):
self._create_phonopy_yaml(folder)
self._create_FORCE_SETS(folder)
mesh_opts, fc_opts = get_phonopy_options(
self.inputs.settings.get_dict())
self.inputs.settings.get_dict(), self.inputs.qpoint_mesh.get_dict())

self._internal_retrieve_list = [self._INOUT_FORCE_CONSTANTS, ]
self._additional_cmd_params = [['--writefc', ] + fc_opts, ]
Expand Down
54 changes: 16 additions & 38 deletions aiida_phonopy/common/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@
StructureData = DataFactory('structure')
PotcarData = DataFactory('vasp.potcar')


@calcfunction
def get_calcjob_inputs(calculator_settings, cell):
"""Return builder inputs of a calculation"""
"""Return builder inputs of a calculation."""
return _get_calcjob_inputs(calculator_settings, cell)


@calcfunction
def get_force_calcjob_inputs(calculator_settings, supercell):
"""Return builder inputs of force calculations"""
"""Return builder inputs of force calculations."""
return _get_calcjob_inputs(calculator_settings, supercell, 'forces')


@calcfunction
def get_phonon_force_calcjob_inputs(calculator_settings, supercell):
"""Return builder inputs of force calculations for phono3py fc2"""
"""Return builder inputs of force calculations for phono3py fc2."""
return _get_calcjob_inputs(calculator_settings, supercell, 'phonon_forces')


@calcfunction
def get_nac_calcjob_inputs(calculator_settings, unitcell):
"""Return builder inputs of an NAC params calculation"""
"""Return builder inputs of an NAC params calculation."""
return _get_calcjob_inputs(calculator_settings, unitcell, 'nac')


def _get_calcjob_inputs(calculator_settings, supercell, calc_type=None):
"""Return builder inputs of a calculation"""
"""Return builder inputs of a calculation."""
if calc_type is None:
settings = calculator_settings.get_dict()
else:
Expand All @@ -44,17 +45,23 @@ def _get_calcjob_inputs(calculator_settings, supercell, calc_type=None):
'parameters': _get_vasp_parameters(settings),
'settings': _get_vasp_settings(settings),
'kpoints': _get_vasp_kpoints(settings, supercell)}
potential_family = Str(settings['potential_family'])
potential_mapping = Dict(dict=settings['potential_mapping'])
builder_inputs.update({'potential_family': potential_family,
'potential_mapping': potential_mapping})
else:
raise RuntimeError("Code could not be found.")

potential_family = Str(settings['potential_family'])
potential_mapping = Dict(dict=settings['potential_mapping'])
builder_inputs.update({'potential_family': potential_family,
'potential_mapping': potential_mapping})
return builder_inputs


def get_calcjob_builder(structure, code_string, builder_inputs, label=None):
"""Return process builder.
This method supposes createing a process builder of a force calculator
(VASP, QE, etc.).
"""
code = Code.get_from_string(code_string)
if code.get_input_plugin_name() == 'vasp.vasp':
VaspWorkflow = WorkflowFactory('vasp.vasp')
Expand Down Expand Up @@ -150,32 +157,3 @@ def _get_vasp_kpoints(settings_dict, structure):
'Define either kpoints_density or kpoints_mesh')

return kpoints


def _get_vasp_builder(structure, settings_dict, label=None):
"""
Generate the input paramemeters needed to run a calculation for VASP
:param structure: StructureData object containing the crystal structure
:param settings: dict object containing a dictionary with the
INCAR parameters
:return: Calculation process object, input dictionary
"""

code_string = settings_dict['code_string']
VaspWorkflow = WorkflowFactory('vasp.vasp')
builder = VaspWorkflow.get_builder()
if label:
builder.metadata.label = label
builder.code = Code.get_from_string(code_string)
builder.structure = structure
builder.options = _get_vasp_options(settings_dict)
builder.clean_workdir = Bool(False)
builder.settings = _get_vasp_settings(settings_dict)
builder.parameters = _get_vasp_parameters(settings_dict)
builder.potential_family = Str(settings_dict['potential_family'])
builder.potential_mapping = Dict(
dict=settings_dict['potential_mapping'])
builder.kpoints = _get_vasp_kpoints(settings_dict, structure)

return builder
9 changes: 5 additions & 4 deletions aiida_phonopy/common/file_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_BORN_txt(nac_data, structure, symmetry_tolerance):
epsilon = nac_data.get_array('epsilon')
pcell = phonopy_atoms_from_structure(structure)
lines = get_BORN_lines(pcell, born_charges, epsilon,
symprec=symmetry_tolerance)
symprec=symmetry_tolerance.value)

return "\n".join(lines)

Expand Down Expand Up @@ -68,10 +68,11 @@ def get_phonopy_yaml_txt(structure,
return str(phpy_yaml)


def get_phonopy_options(settings):
def get_phonopy_options(settings, qpoint_mesh):
"""Return phonopy command option strings."""
mesh_opts = []
if 'mesh' in settings:
mesh = settings['mesh']
if 'mesh' in qpoint_mesh:
mesh = qpoint_mesh['mesh']
try:
length = float(mesh)
mesh_opts.append('--mesh=%f' % length)
Expand Down

0 comments on commit 76c5dba

Please sign in to comment.