Skip to content

Commit

Permalink
flake8, black, pre-commit-hook
Browse files Browse the repository at this point in the history
The following settings were added:
- flack8 (88 line length)
- black (88 line length)
- pre-commit
  • Loading branch information
atztogo committed Sep 20, 2021
1 parent 778e3aa commit 41f0439
Show file tree
Hide file tree
Showing 24 changed files with 1,684 additions and 2,229 deletions.
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://pre-commit.com for more informatio
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
args:
- "--max-line-length=88"
- "--ignore=E203,W503"

- repo: https://github.com/psf/black
rev: 21.9b0
hooks:
- id: black
args:
- --line-length=88
81 changes: 49 additions & 32 deletions aiida_phonopy/calcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from aiida.plugins import DataFactory
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')
Dict = DataFactory("dict")
StructureData = DataFactory("structure")
ArrayData = DataFactory("array")
Float = DataFactory("float")
Bool = DataFactory("bool")


class BasePhonopyCalculation(CalcJob):
Expand All @@ -19,29 +19,42 @@ class BasePhonopyCalculation(CalcJob):
"""

_INPUT_NAC = 'BORN'
_INPUT_NAC = "BORN"

@classmethod
def define(cls, spec):
"""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('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))
spec.input('force_sets', valid_type=ArrayData, required=False,
help='Sets of forces in supercells')
spec.input('nac_params', valid_type=ArrayData, required=False,
help='NAC parameters')
spec.input('primitive', valid_type=StructureData, required=False,
help='Primitive cell structure')
spec.input('dataset', valid_type=(Dict, ArrayData), required=False,
help='Displacements and forces dataset')
spec.input("settings", valid_type=Dict, help="Phonopy parameters")
spec.input("structure", valid_type=StructureData, help="Unit cell structure")
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),
)
spec.input(
"force_sets",
valid_type=ArrayData,
required=False,
help="Sets of forces in supercells",
)
spec.input(
"nac_params", valid_type=ArrayData, required=False, help="NAC parameters"
)
spec.input(
"primitive",
valid_type=StructureData,
required=False,
help="Primitive cell structure",
)
spec.input(
"dataset",
valid_type=(Dict, ArrayData),
required=False,
help="Displacements and forces dataset",
)

def prepare_for_submission(self, folder):
"""Prepare calcinfo."""
Expand All @@ -56,18 +69,21 @@ def prepare_for_submission(self, folder):
# ================= prepare the python input files =================

# BORN
if (not self.inputs.fc_only and
'nac_params' in self.inputs and
'primitive' in self.inputs and
'symmetry_tolerance' in self.inputs): # noqa: E129
if (
not self.inputs.fc_only
and "nac_params" in self.inputs
and "primitive" in self.inputs
and "symmetry_tolerance" in self.inputs
): # noqa: E129
born_txt = get_BORN_txt(
self.inputs.nac_params,
self.inputs.primitive,
self.inputs.symmetry_tolerance)
with folder.open(self._INPUT_NAC, 'w', encoding='utf8') as handle:
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:
params.append('--nac')
params.append("--nac")

self.inputs.metadata.options.withmpi = False

Expand All @@ -83,8 +99,9 @@ def prepare_for_submission(self, folder):
calcinfo.retrieve_list = self._internal_retrieve_list

calcinfo.codes_info = []
for i, (default_params, additional_params) in enumerate(zip(
self._calculation_cmd, self._additional_cmd_params)):
for i, (default_params, additional_params) in enumerate(
zip(self._calculation_cmd, self._additional_cmd_params)
):
codeinfo = CodeInfo()
cmdline_params = default_params + additional_params
codeinfo.cmdline_params = cmdline_params
Expand Down
174 changes: 109 additions & 65 deletions aiida_phonopy/calcs/phonopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,89 @@
from aiida_phonopy.calcs.base import BasePhonopyCalculation
from aiida.common import InputValidationError
from aiida_phonopy.common.file_generators import (
get_FORCE_SETS_txt, get_phonopy_options, get_phonopy_yaml_txt)
get_FORCE_SETS_txt,
get_phonopy_options,
get_phonopy_yaml_txt,
)


BandsData = DataFactory('array.bands')
ArrayData = DataFactory('array')
XyData = DataFactory('array.xy')
Dict = DataFactory('dict')
Str = DataFactory('str')
BandsData = DataFactory("array.bands")
ArrayData = DataFactory("array")
XyData = DataFactory("array.xy")
Dict = DataFactory("dict")
Str = DataFactory("str")


class PhonopyCalculation(BasePhonopyCalculation):
"""Phonopy calculation."""

_OUTPUT_PROJECTED_DOS = 'projected_dos.dat'
_OUTPUT_TOTAL_DOS = 'total_dos.dat'
_OUTPUT_THERMAL_PROPERTIES = 'thermal_properties.yaml'
_OUTPUT_BAND_STRUCTURE = 'band.yaml'
_INOUT_FORCE_CONSTANTS = 'force_constants.hdf5'
_INPUT_CELL = 'phonopy_cells.yaml'
_INPUT_FORCE_SETS = 'FORCE_SETS'
_OUTPUT_PROJECTED_DOS = "projected_dos.dat"
_OUTPUT_TOTAL_DOS = "total_dos.dat"
_OUTPUT_THERMAL_PROPERTIES = "thermal_properties.yaml"
_OUTPUT_BAND_STRUCTURE = "band.yaml"
_INOUT_FORCE_CONSTANTS = "force_constants.hdf5"
_INPUT_CELL = "phonopy_cells.yaml"
_INPUT_FORCE_SETS = "FORCE_SETS"

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

spec.input('projected_dos_filename', valid_type=Str,
default=lambda: Str(cls._OUTPUT_PROJECTED_DOS))
spec.input('total_dos_filename', valid_type=Str,
default=lambda: Str(cls._OUTPUT_TOTAL_DOS))
spec.input('thermal_properties_filename', valid_type=Str,
default=lambda: Str(cls._OUTPUT_THERMAL_PROPERTIES))
spec.input('band_structure_filename', valid_type=Str,
default=lambda: Str(cls._OUTPUT_BAND_STRUCTURE))
spec.input('force_constants_filename', valid_type=Str,
default=lambda: Str(cls._INOUT_FORCE_CONSTANTS))
spec.input(
"projected_dos_filename",
valid_type=Str,
default=lambda: Str(cls._OUTPUT_PROJECTED_DOS),
)
spec.input(
"total_dos_filename",
valid_type=Str,
default=lambda: Str(cls._OUTPUT_TOTAL_DOS),
)
spec.input(
"thermal_properties_filename",
valid_type=Str,
default=lambda: Str(cls._OUTPUT_THERMAL_PROPERTIES),
)
spec.input(
"band_structure_filename",
valid_type=Str,
default=lambda: Str(cls._OUTPUT_BAND_STRUCTURE),
)
spec.input(
"force_constants_filename",
valid_type=Str,
default=lambda: Str(cls._INOUT_FORCE_CONSTANTS),
)
# parser_name has to be set to invoke parsing.
spec.inputs['metadata']['options']['parser_name'].default = 'phonopy'
spec.inputs['metadata']['options']['output_filename'].default = 'phonopy.yaml'

spec.output('force_constants', valid_type=ArrayData, required=False,
help='Calculated force constants')
spec.output('dos', valid_type=XyData, required=False,
help='Calculated total DOS')
spec.output('pdos', valid_type=XyData, required=False,
help='Calculated projected DOS')
spec.output('thermal_properties', valid_type=XyData, required=False,
help='Calculated thermal properties')
spec.output('band_structure', valid_type=BandsData, required=False,
help='Calculated phonon band structure')
spec.output('version', valid_type=Str, required=False,
help='Version number')
spec.inputs["metadata"]["options"]["parser_name"].default = "phonopy"
spec.inputs["metadata"]["options"]["output_filename"].default = "phonopy.yaml"

spec.output(
"force_constants",
valid_type=ArrayData,
required=False,
help="Calculated force constants",
)
spec.output(
"dos", valid_type=XyData, required=False, help="Calculated total DOS"
)
spec.output(
"pdos", valid_type=XyData, required=False, help="Calculated projected DOS"
)
spec.output(
"thermal_properties",
valid_type=XyData,
required=False,
help="Calculated thermal properties",
)
spec.output(
"band_structure",
valid_type=BandsData,
required=False,
help="Calculated phonon band structure",
)
spec.output("version", valid_type=Str, required=False, help="Version number")

def prepare_for_submission(self, folder):
"""Prepare calcinfo."""
Expand All @@ -67,53 +98,67 @@ 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['postprocess_parameters'])
self.inputs.settings["postprocess_parameters"]
)

self._internal_retrieve_list = [
self._INOUT_FORCE_CONSTANTS,
self.inputs.metadata.options.output_filename]
self.inputs.metadata.options.output_filename,
]
self._additional_cmd_params = [
['--writefc', '--writefc-format=hdf5'] + fc_opts,
['--readfc', '--readfc-format=hdf5'],
['--readfc', '--readfc-format=hdf5']]
["--writefc", "--writefc-format=hdf5"] + fc_opts,
["--readfc", "--readfc-format=hdf5"],
["--readfc", "--readfc-format=hdf5"],
]

# First run with --writefc, and with --readfc for remaining runs
if self.inputs.fc_only:
self._calculation_cmd = [['-c', self._INPUT_CELL], ]
self._calculation_cmd = [
["-c", self._INPUT_CELL],
]
else:
self._calculation_cmd = [
['-c', self._INPUT_CELL, '--pdos=auto'] + mesh_opts,
['-c', self._INPUT_CELL, '-t', '--dos'] + mesh_opts,
['-c', self._INPUT_CELL, '--band=auto', '--band-points=101',
'--band-const-interval']]
["-c", self._INPUT_CELL, "--pdos=auto"] + mesh_opts,
["-c", self._INPUT_CELL, "-t", "--dos"] + mesh_opts,
[
"-c",
self._INPUT_CELL,
"--band=auto",
"--band-points=101",
"--band-const-interval",
],
]
self._internal_retrieve_list += [
self._OUTPUT_TOTAL_DOS,
self._OUTPUT_PROJECTED_DOS,
self._OUTPUT_THERMAL_PROPERTIES,
self._OUTPUT_BAND_STRUCTURE]
self._OUTPUT_BAND_STRUCTURE,
]

def _create_phonopy_yaml(self, folder):
phpy_yaml_txt = get_phonopy_yaml_txt(
self.inputs.structure,
supercell_matrix=self.inputs.settings['supercell_matrix'])
with folder.open(self._INPUT_CELL, 'w', encoding='utf8') as handle:
supercell_matrix=self.inputs.settings["supercell_matrix"],
)
with folder.open(self._INPUT_CELL, "w", encoding="utf8") as handle:
handle.write(phpy_yaml_txt)

def _create_FORCE_SETS(self, folder):
if 'force_sets' in self.inputs:
if "force_sets" in self.inputs:
force_sets = self.inputs.force_sets
else:
force_sets = None
if 'displacement_dataset' in self.inputs.settings.keys():
dataset = self.inputs.settings['displacement_dataset']
elif 'dataset' in self.inputs.settings.keys():
dataset = self.inputs.settings['dataset']
elif ('dataset' in self.inputs and
'displacements' in self.inputs.dataset.get_arraynames()):
dataset = {'displacements':
self.inputs.dataset.get_array('displacements')}
if 'forces' in self.inputs.dataset.get_arraynames():
dataset['forces'] = self.inputs.dataset.get_array('forces')
if "displacement_dataset" in self.inputs.settings.keys():
dataset = self.inputs.settings["displacement_dataset"]
elif "dataset" in self.inputs.settings.keys():
dataset = self.inputs.settings["dataset"]
elif (
"dataset" in self.inputs
and "displacements" in self.inputs.dataset.get_arraynames()
):
dataset = {"displacements": self.inputs.dataset.get_array("displacements")}
if "forces" in self.inputs.dataset.get_arraynames():
dataset["forces"] = self.inputs.dataset.get_array("forces")
if force_sets is not None:
force_sets = None
else:
Expand All @@ -122,9 +167,8 @@ def _create_FORCE_SETS(self, folder):
# can work both for type-I and type-II
force_sets_txt = get_FORCE_SETS_txt(dataset, force_sets=force_sets)
if force_sets_txt is None:
msg = ("Displacements or forces were not found.")
msg = "Displacements or forces were not found."
raise InputValidationError(msg)

with folder.open(
self._INPUT_FORCE_SETS, 'w', encoding='utf8') as handle:
with folder.open(self._INPUT_FORCE_SETS, "w", encoding="utf8") as handle:
handle.write(force_sets_txt)

0 comments on commit 41f0439

Please sign in to comment.