Skip to content

Commit

Permalink
IterHA follows the change of PhonopyWorkChain.
Browse files Browse the repository at this point in the history
  • Loading branch information
atztogo committed Jun 16, 2021
1 parent 9d0184b commit 6fe5775
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 0 additions & 2 deletions aiida_phonopy/calcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ def define(cls, spec):
default=lambda: Bool(False))
spec.input('force_sets', valid_type=ArrayData, required=False,
help='Sets of forces in supercells')
spec.input('dataset', valid_type=ArrayData, required=False,
help='Sets of displacements and forces in supercells')
spec.input('nac_params', valid_type=ArrayData, required=False,
help='NAC parameters')
spec.input('primitive', valid_type=StructureData, required=False,
Expand Down
9 changes: 9 additions & 0 deletions aiida_phonopy/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
BandsData = DataFactory('array.bands')


@calcfunction
def get_remote_fc_calculation_settings(phonon_settings):
"""Create remote force constants phonopy calculation setting."""
ph_settings = _get_setting_info(phonon_settings)
params = _get_phonopy_postprocess_info(phonon_settings)
ph_settings['postprocess_parameters'] = params
return Dict(dict=ph_settings)


@calcfunction
def setup_phonopy_calculation(phonon_settings,
structure,
Expand Down
31 changes: 15 additions & 16 deletions aiida_phonopy/workflows/iter_ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from aiida.plugins import WorkflowFactory, DataFactory
from aiida.orm import Bool, Float, Int, QueryBuilder, Group, load_node, Code
from aiida.engine import while_, if_, calcfunction
from aiida_phonopy.common.utils import phonopy_atoms_from_structure
from aiida_phonopy.common.utils import (
phonopy_atoms_from_structure, get_remote_fc_calculation_settings)

Dict = DataFactory('dict')
ArrayData = DataFactory('array')
Expand Down Expand Up @@ -50,7 +51,7 @@ def get_random_displacements(structure,
phonon_setting_info,
dataset_for_fc,
random_seed=None):
"""Generate supercells with random displacemens
"""Generate supercells with random displacemens.
The random displacements are generated from phonons and harmonic
oscillator distribution function of canonical ensemble. The input
Expand All @@ -64,7 +65,6 @@ def get_random_displacements(structure,
Displacement datasets to run force calculations.
"""

# Calculate force constants by fitting using ALM
smat = phonon_setting_info['supercell_matrix']
ph = Phonopy(phonopy_atoms_from_structure(structure),
Expand All @@ -91,7 +91,7 @@ def collect_dataset(number_of_steps_for_fitting,
include_ratio,
linear_decay,
**data):
"""Collect supercell displacements, forces, and energies
"""Collect supercell displacements, forces, and energies.
Returns
-------
Expand All @@ -106,7 +106,6 @@ def collect_dataset(number_of_steps_for_fitting,
indexed by concatenating list of list in step (3).
"""

nitems = max([int(key.split('_')[-1])
for key in data.keys() if 'forces' in key])

Expand Down Expand Up @@ -136,6 +135,7 @@ def collect_dataset(number_of_steps_for_fitting,

def create_dataset(displacements, forces, energies,
max_items=None, ratio=None, linear_decay=False):
"""Collect data for force constants calculation."""
included = _choose_snapshots_by_linear_decay(
displacements, forces, max_items=max_items, linear_decay=linear_decay)

Expand Down Expand Up @@ -177,7 +177,7 @@ def _extract_dataset_from_db(forces_in_db, ph_info_in_db):
def _choose_snapshots_by_linear_decay(displacements, forces,
max_items=None,
linear_decay=False):
"""Choose snapshots by linear_decay
"""Choose snapshots by linear_decay.
With linear_decay=True, numbers of snapshots to be taken
are biased. Older snapshots are taken lesser. The fraction
Expand All @@ -191,7 +191,6 @@ def _choose_snapshots_by_linear_decay(displacements, forces,
included at maximum.
"""

assert len(forces) == len(displacements)

nitems = len(forces)
Expand Down Expand Up @@ -234,7 +233,7 @@ def _include_snapshots(displacements, forces, energies, included):


def _remove_high_energy_snapshots(energies, included, ratio):
"""Reject high energy snapshots
"""Reject high energy snapshots.
Parameters
----------
Expand All @@ -253,7 +252,6 @@ def _remove_high_energy_snapshots(energies, included, ratio):
Rejected snapshots are turned to False from 'included'.
"""

concat_included = np.concatenate(included)
concat_energies = np.concatenate(energies)
included_energies = concat_energies[concat_included]
Expand All @@ -277,7 +275,7 @@ def _remove_high_energy_snapshots(energies, included, ratio):


def _modify_force_constants(ph):
"""Apply treatment to imaginary modes
"""Apply treatment to imaginary modes.
This method modifies force constants to make phonon frequencies
be real from imaginary. This treatment is expected to be finally
Expand All @@ -291,7 +289,6 @@ def _modify_force_constants(ph):
are shifted by |freqs| + 1.
"""

# temperature=300 is used just to invoke this feature.
ph.generate_displacements(number_of_snapshots=1, temperature=300)
rd = ph.random_displacements
Expand All @@ -318,7 +315,7 @@ def _generate_random_displacements(ph,


class IterHarmonicApprox(WorkChain):
""" Workchain for harmonic force constants by iterative approach
"""Workchain for harmonic force constants by iterative approach.
By default, the calculation starts with normal phonon calculation,
i.e., in this context, which corresponds to roughly 0K force constants.
Expand Down Expand Up @@ -435,6 +432,9 @@ def initialize(self):
self.report("initialize (%s)" % self.uuid)
self.ctx.iteration = 0
self.ctx.prev_nodes = []
if self.remote_phonopy():
self.ctx.ph_settings = get_remote_fc_calculation_settings(
self.inputs.phonon_settings)

def is_loop_finished(self):
qb = QueryBuilder()
Expand Down Expand Up @@ -503,16 +503,15 @@ def generate_displacements_local(self):
self.ctx.dataset = dataset

def run_force_constants_calculation_remote(self):
"""Run force constants calculation by PhonopyCalculation"""

"""Run force constants calculation by PhonopyCalculation."""
self.report('remote force constants calculation %d' %
self.ctx.iteration)

code_string = self.inputs.code_string.value
builder = Code.get_from_string(code_string).get_builder()
builder.structure = self.inputs.structure
builder.settings = self.inputs.phonon_settings
builder.metadata.options.update(self.inputs.options)
builder.settings = self.ctx.ph_settings
builder.metadata.options.update(self.inputs.phonon_settings['options'])
builder.metadata.label = ("Force constants calculation %d" %
self.ctx.iteration)
builder.dataset = self.ctx.dataset_for_fc
Expand Down

0 comments on commit 6fe5775

Please sign in to comment.