示例#1
0
文件: load.py 项目: phonopy/phono3py
def _set_forces_fc2(
    ph3py: Phono3py,
    ph3py_yaml: typing.Union[Phono3pyYaml, None],
    force_filename,
    disp_filename,
    produce_fc,
    symmetrize_fc,
    is_compact_fc,
    fc_calculator,
    fc_calculator_options,
    fc_type,
    log_level,
):
    dataset = parse_forces(
        ph3py,
        ph3py_yaml=ph3py_yaml,
        force_filename=force_filename,
        disp_filename=disp_filename,
        fc_type=fc_type,
        log_level=log_level,
    )
    if fc_type == "phonon_fc2":
        ph3py.phonon_dataset = dataset
    else:
        ph3py.dataset = dataset

    if produce_fc:
        ph3py.produce_fc2(
            symmetrize_fc2=symmetrize_fc,
            is_compact_fc=is_compact_fc,
            fc_calculator=fc_calculator,
            fc_calculator_options=fc_calculator_options,
        )
        if log_level and symmetrize_fc:
            print("fc2 was symmetrized.")
示例#2
0
文件: load.py 项目: phonopy/phono3py
def set_dataset_and_force_constants(
    ph3py: Phono3py,
    ph3py_yaml: typing.Union[Phono3pyYaml, None] = None,
    fc3_filename=None,
    fc2_filename=None,
    forces_fc3_filename=None,
    forces_fc2_filename=None,
    fc_calculator=None,
    fc_calculator_options=None,
    produce_fc=True,
    symmetrize_fc=True,
    is_compact_fc=False,
    cutoff_pair_distance=None,
    log_level=0,
):
    """Set displacements, forces, and create force constants."""
    read_fc = {"fc2": False, "fc3": False}
    p2s_map = ph3py.primitive.p2s_map
    phonon_p2s_map = ph3py.phonon_primitive.p2s_map
    if fc3_filename is not None:
        fc3 = read_fc3_from_hdf5(filename=fc3_filename, p2s_map=p2s_map)
        ph3py.fc3 = fc3
        read_fc["fc3"] = True
        if log_level:
            print('fc3 was read from "%s".' % fc3_filename)
    elif forces_fc3_filename is not None:
        if type(forces_fc3_filename) is str:
            force_filename = forces_fc3_filename
            disp_filename = None
        else:
            force_filename, disp_filename = forces_fc3_filename
        _set_forces_fc3(
            ph3py,
            ph3py_yaml,
            force_filename,
            disp_filename,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            cutoff_pair_distance,
            log_level,
        )
    elif os.path.isfile("fc3.hdf5"):
        ph3py.fc3 = read_fc3_from_hdf5(filename="fc3.hdf5", p2s_map=p2s_map)
        read_fc["fc3"] = True
        if log_level:
            print('fc3 was read from "fc3.hdf5".')
    elif os.path.isfile("FORCES_FC3"):
        disp_filename = None
        if os.path.isfile("disp_fc3.yaml"):
            if ph3py_yaml is None:
                disp_filename = "disp_fc3.yaml"
            elif ph3py_yaml.dataset is None:
                disp_filename = "disp_fc3.yaml"
        _set_forces_fc3(
            ph3py,
            ph3py_yaml,
            "FORCES_FC3",
            disp_filename,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            cutoff_pair_distance,
            log_level,
        )
    elif (
        ph3py_yaml is not None
        and ph3py_yaml.dataset is not None
        and forces_in_dataset(ph3py_yaml.dataset)
    ):
        _set_forces_fc3(
            ph3py,
            ph3py_yaml,
            None,
            None,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            cutoff_pair_distance,
            log_level,
        )

    if log_level and ph3py.fc3 is not None:
        show_drift_fc3(ph3py.fc3, primitive=ph3py.primitive)

    if fc2_filename is not None:
        fc2 = read_fc2_from_hdf5(filename=fc2_filename, p2s_map=phonon_p2s_map)
        ph3py.fc2 = fc2
        read_fc["fc2"] = True
        if log_level:
            print('fc2 was read from "%s".' % fc2_filename)
    elif forces_fc2_filename is not None:
        if type(forces_fc2_filename) is str:
            force_filename = forces_fc2_filename
            disp_filename = None
        else:
            force_filename, disp_filename = forces_fc2_filename
        _set_forces_fc2(
            ph3py,
            ph3py_yaml,
            force_filename,
            disp_filename,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            "phonon_fc2",
            log_level,
        )
    elif os.path.isfile("fc2.hdf5"):
        ph3py.fc2 = read_fc2_from_hdf5(filename="fc2.hdf5", p2s_map=phonon_p2s_map)
        read_fc["fc2"] = True
        if log_level:
            print('fc2 was read from "fc2.hdf5".')
    elif os.path.isfile("FORCES_FC2"):
        disp_filename = None
        if os.path.isfile("disp_fc2.yaml"):
            if ph3py_yaml is None:
                disp_filename = "disp_fc2.yaml"
            elif ph3py_yaml.phonon_dataset is None:
                disp_filename = "disp_fc2.yaml"
        if (
            disp_filename is None
            and ph3py_yaml is not None
            and ph3py_yaml.phonon_dataset is None
        ):
            msg = (
                '"FORCES_FC2" was found. But phonon displacement dataset '
                f'was not found in "{ph3py_yaml.yaml_filename}".'
            )
            raise RuntimeError(msg)
        _set_forces_fc2(
            ph3py,
            ph3py_yaml,
            "FORCES_FC2",
            disp_filename,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            "phonon_fc2",
            log_level,
        )
    elif (
        ph3py.phonon_supercell_matrix is None
        and fc_calculator == "alm"
        and ph3py.fc2 is not None
    ):
        if log_level:
            print("fc2 that was fit simultaneously with fc3 by ALM is used.")
    elif (
        ph3py_yaml is not None
        and ph3py_yaml.phonon_dataset is not None
        and forces_in_dataset(ph3py_yaml.phonon_dataset)
    ):
        _set_forces_fc2(
            ph3py,
            ph3py_yaml,
            None,
            None,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            "phonon_fc2",
            log_level,
        )
    elif (
        ph3py_yaml is not None
        and ph3py_yaml.dataset is not None
        and forces_in_dataset(ph3py_yaml.dataset)
    ):
        _set_forces_fc2(
            ph3py,
            ph3py_yaml,
            None,
            None,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            "fc2",
            log_level,
        )
    elif os.path.isfile("FORCES_FC3"):
        # suppose fc3.hdf5 is read but fc2.hdf5 doesn't exist.
        disp_filename = None
        if os.path.isfile("disp_fc3.yaml"):
            if ph3py_yaml is None:
                disp_filename = "disp_fc3.yaml"
            elif ph3py_yaml.dataset is None:
                disp_filename = "disp_fc3.yaml"
        _set_forces_fc2(
            ph3py,
            ph3py_yaml,
            "FORCES_FC3",
            disp_filename,
            produce_fc,
            symmetrize_fc,
            is_compact_fc,
            fc_calculator,
            fc_calculator_options,
            "fc2",
            log_level,
        )
    if log_level and ph3py.fc2 is not None:
        show_drift_force_constants(
            ph3py.fc2, primitive=ph3py.phonon_primitive, name="fc2"
        )

    # Cases that dataset is in phono3py.yaml but not forces.
    if ph3py.dataset is None:
        if ph3py_yaml is not None and ph3py_yaml.dataset is not None:
            ph3py.dataset = ph3py_yaml.dataset
        if ph3py_yaml is not None and ph3py_yaml.phonon_dataset is not None:
            ph3py.phonon_dataset = ph3py_yaml.phonon_dataset

    return read_fc