Minor refactorings

This commit is contained in:
Atsushi Togo 2019-11-05 11:43:43 +09:00
parent b664c8a098
commit 85313fa49d
4 changed files with 111 additions and 77 deletions

View File

@ -37,4 +37,3 @@ from phonopy.api_phonopy import Phonopy
from phonopy.api_gruneisen import PhonopyGruneisen
from phonopy.api_qha import PhonopyQHA
from phonopy.cui.load import load
from phonopy.interface.phonopy_yaml import read_cell_yaml

View File

@ -32,10 +32,15 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import numpy as np
from phonopy.api_phonopy import Phonopy
from phonopy.file_IO import (
parse_FORCE_SETS, parse_FORCE_CONSTANTS,
read_force_constants_hdf5, read_physical_unit_in_force_constants_hdf5)
from phonopy.interface.phonopy_yaml import PhonopyYaml
from phonopy.interface.calculator import get_default_physical_units
from phonopy.interface.calculator import (
get_default_physical_units, get_force_constant_conversion_factor)
import phonopy.cui.load_helper as load_helper
@ -171,7 +176,7 @@ def load(phonopy_yaml=None, # phonopy.yaml-like must be the first argument.
smat = phpy_yaml.supercell_matrix
if smat is None:
smat = np.eye(3, dtype='intc', order='C')
if primitive_matrix is 'auto':
if primitive_matrix == 'auto':
pmat = 'auto'
else:
pmat = phpy_yaml.primitive_matrix
@ -197,13 +202,16 @@ def load(phonopy_yaml=None, # phonopy.yaml-like must be the first argument.
is_symmetry=is_symmetry,
calculator=calculator,
log_level=log_level)
load_helper.set_nac_params(phonon,
_nac_params,
born_filename,
is_nac,
units['nac_factor'])
_nac_params = load_helper.get_nac_params(phonon.primitive,
_nac_params,
born_filename,
is_nac,
units['nac_factor'])
if _nac_params is not None:
phonon.nac_params = _nac_params
if _fc is None:
load_helper.set_force_constants(
_set_force_constants(
phonon,
dataset=_dataset,
force_constants_filename=force_constants_filename,
@ -213,3 +221,69 @@ def load(phonopy_yaml=None, # phonopy.yaml-like must be the first argument.
else:
phonon.force_constants = _fc
return phonon
def read_force_constants_from_hdf5(filename='force_constants.hdf5',
p2s_map=None,
calculator=None):
"""Convert force constants physical unit
Each calculator interface has own default force constants physical unit.
This method reads the physical unit of force constants if it exists and
if the read physical unit is different from the default one, the force
constants values are converted to have the default physical units.
Note
----
This method is also used from phonopy script.
"""
fc = read_force_constants_hdf5(filename=filename, p2s_map=p2s_map)
fc_unit = read_physical_unit_in_force_constants_hdf5(
filename=filename)
if fc_unit is None:
return fc
else:
factor = get_force_constant_conversion_factor(fc_unit, calculator)
return fc * factor
def _set_force_constants(
phonon,
dataset=None,
force_constants_filename=None,
force_sets_filename=None,
calculator=None,
fc_calculator=None):
natom = phonon.supercell.get_number_of_atoms()
_dataset = None
if dataset is not None:
_dataset = dataset
elif force_constants_filename is not None:
dot_split = force_constants_filename.split('.')
p2s_map = phonon.primitive.get_primitive_to_supercell_map()
if len(dot_split) > 1 and dot_split[-1] == 'hdf5':
fc = read_force_constants_from_hdf5(
filename=force_constants_filename,
p2s_map=p2s_map,
calculator=calculator)
else:
fc = parse_FORCE_CONSTANTS(filename=force_constants_filename,
p2s_map=p2s_map)
phonon.set_force_constants(fc)
elif force_sets_filename is not None:
_dataset = parse_FORCE_SETS(natom=natom,
filename=force_sets_filename)
elif os.path.isfile("FORCE_SETS"):
_dataset = parse_FORCE_SETS(natom=natom)
if _dataset:
phonon.dataset = _dataset
try:
phonon.produce_force_constants(
calculate_full_force_constants=False,
fc_calculator=fc_calculator)
except RuntimeError:
pass

View File

@ -34,12 +34,9 @@
import os
import numpy as np
from phonopy.interface.calculator import (
read_crystal_structure, get_force_constant_conversion_factor)
from phonopy.interface.calculator import read_crystal_structure
from phonopy.structure.cells import get_primitive_matrix_by_centring
from phonopy.file_IO import (
parse_BORN, parse_FORCE_SETS, parse_FORCE_CONSTANTS,
read_force_constants_hdf5, read_physical_unit_in_force_constants_hdf5)
from phonopy.file_IO import parse_BORN
from phonopy.structure.atoms import PhonopyAtoms
@ -84,73 +81,21 @@ def get_cell_settings(phonopy_yaml=None,
return cell, smat, pmat
def set_nac_params(phonon, nac_params, born_filename, is_nac, nac_factor):
def get_nac_params(primitive, nac_params, born_filename, is_nac, nac_factor):
_nac_params = None
if nac_params is not None:
_nac_params = nac_params
elif born_filename is not None:
_nac_params = parse_BORN(phonon.primitive, filename=born_filename)
_nac_params = parse_BORN(primitive, filename=born_filename)
elif is_nac is True:
if os.path.isfile("BORN"):
_nac_params = parse_BORN(phonon.primitive, filename="BORN")
_nac_params = parse_BORN(primitive, filename="BORN")
if _nac_params is not None:
if _nac_params['factor'] is None:
_nac_params['factor'] = nac_factor
phonon.nac_params = _nac_params
def set_force_constants(
phonon,
dataset=None,
force_constants_filename=None,
force_sets_filename=None,
calculator=None,
fc_calculator=None):
natom = phonon.supercell.get_number_of_atoms()
_dataset = None
if dataset is not None:
_dataset = dataset
elif force_constants_filename is not None:
dot_split = force_constants_filename.split('.')
p2s_map = phonon.primitive.get_primitive_to_supercell_map()
if len(dot_split) > 1 and dot_split[-1] == 'hdf5':
fc = read_force_constants_from_hdf5(
filename=force_constants_filename,
p2s_map=p2s_map,
calculator=calculator)
else:
fc = parse_FORCE_CONSTANTS(filename=force_constants_filename,
p2s_map=p2s_map)
phonon.set_force_constants(fc)
elif force_sets_filename is not None:
_dataset = parse_FORCE_SETS(natom=natom,
filename=force_sets_filename)
elif os.path.isfile("FORCE_SETS"):
_dataset = parse_FORCE_SETS(natom=natom)
if _dataset:
phonon.dataset = _dataset
try:
phonon.produce_force_constants(
calculate_full_force_constants=False,
fc_calculator=fc_calculator)
except RuntimeError:
pass
def read_force_constants_from_hdf5(filename='force_constants.hdf5',
p2s_map=None,
calculator=None):
fc = read_force_constants_hdf5(filename=filename, p2s_map=p2s_map)
fc_unit = read_physical_unit_in_force_constants_hdf5(
filename=filename)
if fc_unit is None:
return fc
else:
factor = get_force_constant_conversion_factor(fc_unit, calculator)
return fc * factor
return _nac_params
def _get_supercell_matrix(smat):

View File

@ -43,7 +43,7 @@ from phonopy.file_IO import (
write_FORCE_CONSTANTS, write_force_constants_to_hdf5, parse_QPOINTS,
parse_disp_yaml)
from phonopy.cui.collect_cell_info import collect_cell_info
from phonopy.cui.load_helper import read_force_constants_from_hdf5
from phonopy.cui.load import read_force_constants_from_hdf5
from phonopy.cui.settings import PhonopyConfParser
from phonopy.cui.show_symmetry import check_symmetry
from phonopy.cui.phonopy_argparse import (
@ -638,9 +638,9 @@ if run_mode == 'displacements' and settings.get_temperatures() is None:
interface_mode,
optional_structure_info)
########################################
# Preparations for phonon calculations #
########################################
###################
# Force constants #
###################
if settings.get_read_force_constants():
if settings.get_is_hdf5() or settings.get_readfc_format() == 'hdf5':
try:
@ -798,7 +798,9 @@ if settings.get_write_force_constants():
if settings.get_is_rotational_invariance():
phonon.get_rotational_condition_of_fc()
# Non-analytical term correction (LO-TO splitting)
##################################
# Non-analytical term correction #
##################################
if settings.get_is_nac() or has_read_phonopy_yaml:
def read_nac_params_from_phonopy_yaml(unitcell_filename, log_level):
ph_yaml = PhonopyYaml()
@ -857,6 +859,9 @@ if settings.get_is_nac() or has_read_phonopy_yaml:
print("%s %12.7f %12.7f %12.7f" % ((text,) + tuple(v)))
print("-" * 76)
########
# Misc #
########
# Atomic species without mass case
symbols_with_no_mass = []
if primitive.get_masses() is None:
@ -877,7 +882,9 @@ if len(symbols_with_no_mass) > 0:
# Phonon calculations #
#######################
#
# QPOINTS mode
#
if run_mode == 'qpoints':
if settings.get_read_qpoints():
q_points = parse_QPOINTS()
@ -905,8 +912,9 @@ if run_mode == 'qpoints':
else:
phonon.write_yaml_qpoints_phonon()
#
# Band structure and mesh sampling
#
elif (run_mode == 'band' or
run_mode == 'mesh' or
run_mode == 'band_mesh' or
@ -1263,8 +1271,9 @@ elif (run_mode == 'band' or
else:
plot.show()
#
# Animation
#
elif run_mode == 'anime':
anime_type = settings.get_anime_type()
if anime_type == "v_sim":
@ -1294,7 +1303,9 @@ elif run_mode == 'anime':
print("band index: %d" % band_index)
print("Number of images: %d" % division)
#
# Modulation
#
elif run_mode == 'modulation':
mod_setting = settings.get_modulation()
phonon_modes = mod_setting['modulations']
@ -1313,7 +1324,9 @@ elif run_mode == 'modulation':
phonon.write_modulations()
phonon.write_yaml_modulations()
#
# Ir-representation
#
elif run_mode == 'irreps':
if phonon.set_irreps(settings.get_irreps_q_point(),
is_little_cogroup=settings.get_is_little_cogroup(),
@ -1323,6 +1336,9 @@ elif run_mode == 'irreps':
phonon.show_irreps(show_irreps)
phonon.write_yaml_irreps(show_irreps)
#
# Random displacements at finite temperature
#
elif (run_mode == 'displacements' and
settings.get_random_displacements() is not None):
phonon.generate_displacements(