Exemplo n.º 1
0
def _set_force_constants(ph3py,
                         dataset=None,
                         fc3_filename=None,
                         fc2_filename=None,
                         forces_fc3_filename=None,
                         forces_fc2_filename=None,
                         fc_calculator=None):
    p2s_map = ph3py.primitive.p2s_map
    if fc3_filename is not None:
        fc3 = read_fc3_from_hdf5(filename=fc3_filename, p2s_map=p2s_map)
        ph3py.fc3 = fc3
    elif forces_fc3_filename is not None:
        pass
    elif os.path.isfile("fc3.hdf5"):
        ph3py.fc3 = read_fc3_from_hdf5(filename="fc3.hdf5", p2s_map=p2s_map)
    elif os.path.isfile("FORCES_FC3") and os.path.isfile("disp_fc3.yaml"):
        pass

    if fc2_filename is not None:
        fc2 = read_fc2_from_hdf5(filename=fc2_filename, p2s_map=p2s_map)
        ph3py.fc2 = fc2
    elif forces_fc2_filename is not None:
        pass
    elif os.path.isfile("fc2.hdf5"):
        ph3py.fc2 = read_fc2_from_hdf5(filename="fc2.hdf5", p2s_map=p2s_map)
    elif os.path.isfile("FORCES_FC2") and os.path.isfile("disp_fc2.yaml"):
        pass
Exemplo n.º 2
0
def _read_phono3py_fc3(phono3py, symmetrize_fc3r, input_filename, log_level):
    if input_filename is None:
        filename = "fc3.hdf5"
    else:
        filename = "fc3." + input_filename + ".hdf5"
    file_exists(filename, log_level)
    if log_level:
        print('Reading fc3 from "%s".' % filename)

    p2s_map = phono3py.primitive.p2s_map
    try:
        fc3 = read_fc3_from_hdf5(filename=filename, p2s_map=p2s_map)
    except RuntimeError:
        import traceback

        traceback.print_exc()
        if log_level:
            print_error()
        sys.exit(1)
    num_atom = phono3py.supercell.get_number_of_atoms()
    if fc3.shape[1] != num_atom:
        print("Matrix shape of fc3 doesn't agree with supercell size.")
        if log_level:
            print_error()
        sys.exit(1)

    if symmetrize_fc3r:
        set_translational_invariance_fc3(fc3)
        set_permutation_symmetry_fc3(fc3)

    phono3py.fc3 = fc3
Exemplo n.º 3
0
def _set_force_constants(
        ph3py,
        units,
        dataset=None,
        fc3_filename=None,
        fc2_filename=None,
        forces_fc3_filename=None,
        forces_fc2_filename=None,
        fc_calculator=None,
        fc_calculator_options=None,
        symmetrize_fc=True,
        is_compact_fc=False,
        log_level=0):
    p2s_map = ph3py.primitive.p2s_map
    if fc3_filename is not None:
        fc3 = read_fc3_from_hdf5(filename=fc3_filename, p2s_map=p2s_map)
        ph3py.fc3 = fc3
    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_fc3(ph3py,
                 units,
                 force_filename,
                 disp_filename,
                 symmetrize_fc,
                 is_compact_fc,
                 fc_calculator,
                 fc_calculator_options,
                 log_level)
    elif os.path.isfile("fc3.hdf5"):
        ph3py.fc3 = read_fc3_from_hdf5(filename="fc3.hdf5", p2s_map=p2s_map)
        if log_level:
            print("fc3 was loaded from \"fc3.hdf5\".")
    elif os.path.isfile("FORCES_FC3") and os.path.isfile("disp_fc3.yaml"):
        _set_fc3(ph3py,
                 units,
                 "FORCES_FC3",
                 "disp_fc3.yaml",
                 symmetrize_fc,
                 is_compact_fc,
                 fc_calculator,
                 fc_calculator_options,
                 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=p2s_map)
        ph3py.fc2 = fc2
    elif forces_fc2_filename is not None:
        if type(forces_fc2_filename) == str:
            force_filename = forces_fc2_filename
            disp_filename = None
        else:
            force_filename, disp_filename = forces_fc2_filename
        _set_fc2(ph3py,
                 units,
                 force_filename,
                 disp_filename,
                 symmetrize_fc,
                 is_compact_fc,
                 fc_calculator,
                 fc_calculator_options,
                 log_level)
    elif os.path.isfile("fc2.hdf5"):
        ph3py.fc2 = read_fc2_from_hdf5(filename="fc2.hdf5", p2s_map=p2s_map)
        if log_level:
            print("fc2 was loaded from \"fc2.hdf5\".")
    elif os.path.isfile("FORCES_FC2") and os.path.isfile("disp_fc2.yaml"):
        _set_fc2(ph3py,
                 units,
                 "FORCES_FC2",
                 "disp_fc2.yaml",
                 symmetrize_fc,
                 is_compact_fc,
                 fc_calculator,
                 fc_calculator_options,
                 log_level)
    if log_level and ph3py.fc2 is not None:
        show_drift_force_constants(ph3py.fc2,
                                   primitive=ph3py.phonon_primitive,
                                   name='fc2')
def create_phono3py_force_constants(phono3py,
                                    phonon_supercell_matrix,
                                    settings,
                                    force_to_eVperA=None,
                                    distance_to_A=None,
                                    compression=None,
                                    input_filename=None,
                                    output_filename=None,
                                    log_level=1):
    read_fc3 = settings.get_read_fc3()
    read_fc2 = settings.get_read_fc2()
    symmetrize_fc3r = (settings.get_is_symmetrize_fc3_r() or
                       settings.get_fc_symmetry())
    symmetrize_fc3q = settings.get_is_symmetrize_fc3_q()
    symmetrize_fc2 = (settings.get_is_symmetrize_fc2() or
                      settings.get_fc_symmetry())

    if settings.get_use_alm_fc2():
        symmetrize_fc2 = False
    if settings.get_use_alm_fc3():
        symmetrize_fc3r = False
    alm_options = None
    if settings.get_use_alm_fc3() or settings.get_use_alm_fc2():
        if settings.get_alm_options() is not None:
            alm_option_types = {'solver': str,
                                'cutoff_distance': float}
            alm_options = {}
            for option_str in settings.get_alm_options().split(","):
                key, val = [x.strip() for x in option_str.split('=')[:2]]
                if key.lower() in alm_option_types:
                    option_value = alm_option_types[key.lower()](val)
                    alm_options[key.lower()] = option_value

    if log_level:
        show_phono3py_force_constants_settings(read_fc3,
                                               read_fc2,
                                               symmetrize_fc3r,
                                               symmetrize_fc3q,
                                               symmetrize_fc2,
                                               settings)

    # fc3
    if (settings.get_is_joint_dos() or
        (settings.get_is_isotope() and
         not (settings.get_is_bterta() or settings.get_is_lbte())) or
        settings.get_read_gamma() or
        settings.get_read_pp() or
        settings.get_write_phonon() or
        settings.get_constant_averaged_pp_interaction() is not None):
        pass
    else:
        if read_fc3:  # Read fc3.hdf5
            if input_filename is None:
                filename = 'fc3.hdf5'
            else:
                filename = 'fc3.' + input_filename + '.hdf5'
            file_exists(filename, log_level)
            if log_level:
                print("Reading fc3 from %s" % filename)

            p2s_map = phono3py.get_primitive().get_primitive_to_supercell_map()
            try:
                fc3 = read_fc3_from_hdf5(filename=filename, p2s_map=p2s_map)
            except RuntimeError:
                import traceback
                traceback.print_exc()
                if log_level:
                    print_error()
                sys.exit(1)
            num_atom = phono3py.get_supercell().get_number_of_atoms()
            if fc3.shape[1] != num_atom:
                print("Matrix shape of fc3 doesn't agree with supercell size.")
                if log_level:
                    print_error()
                sys.exit(1)

            if symmetrize_fc3r:
                set_translational_invariance_fc3(fc3)
                set_permutation_symmetry_fc3(fc3)

            phono3py.set_fc3(fc3)
        else:  # fc3 from FORCES_FC3
            if not _create_phono3py_fc3(phono3py,
                                        force_to_eVperA,
                                        distance_to_A,
                                        symmetrize_fc3r,
                                        symmetrize_fc2,
                                        input_filename,
                                        output_filename,
                                        settings.get_is_compact_fc(),
                                        settings.get_use_alm_fc3(),
                                        alm_options,
                                        compression,
                                        log_level):
                    print("fc3 was not created properly.")
                    if log_level:
                        print_error()
                    sys.exit(1)

        cutoff_distance = settings.get_cutoff_fc3_distance()
        if cutoff_distance is not None and cutoff_distance > 0:
            if log_level:
                print("Cutting-off fc3 by zero (cut-off distance: %f)" %
                      cutoff_distance)
            phono3py.cutoff_fc3_by_zero(cutoff_distance)

        if log_level:
            show_drift_fc3(phono3py.get_fc3(),
                           primitive=phono3py.get_primitive())

    # fc2
    phonon_primitive = phono3py.get_phonon_primitive()
    phonon_supercell = phono3py.get_phonon_supercell()
    p2s_map = phonon_primitive.get_primitive_to_supercell_map()
    if read_fc2:
        if input_filename is None:
            filename = 'fc2.hdf5'
        else:
            filename = 'fc2.' + input_filename + '.hdf5'
        file_exists(filename, log_level)
        if log_level:
            print("Reading fc2 from %s" % filename)

        num_atom = phonon_supercell.get_number_of_atoms()
        try:
            phonon_fc2 = read_fc2_from_hdf5(filename=filename, p2s_map=p2s_map)
        except RuntimeError:
            import traceback
            traceback.print_exc()
            if log_level:
                print_error()
            sys.exit(1)

        if phonon_fc2.shape[1] != num_atom:
            print("Matrix shape of fc2 doesn't agree with supercell size.")
            if log_level:
                print_error()
            sys.exit(1)

        if symmetrize_fc2:
            if phonon_fc2.shape[0] == phonon_fc2.shape[1]:
                symmetrize_force_constants(phonon_fc2)
            else:
                symmetrize_compact_force_constants(phonon_fc2,
                                                   phonon_primitive)

        phono3py.set_fc2(phonon_fc2)
    else:
        if phonon_supercell_matrix is None:
            if not _create_phono3py_fc2(phono3py,
                                        force_to_eVperA,
                                        distance_to_A,
                                        symmetrize_fc2,
                                        input_filename,
                                        settings.get_is_compact_fc(),
                                        settings.get_use_alm_fc2(),
                                        alm_options,
                                        log_level):
                print("fc2 was not created properly.")
                if log_level:
                    print_error()
                sys.exit(1)
        else:
            if not _create_phono3py_phonon_fc2(phono3py,
                                               force_to_eVperA,
                                               distance_to_A,
                                               symmetrize_fc2,
                                               input_filename,
                                               settings.get_is_compact_fc(),
                                               settings.get_use_alm_fc2(),
                                               alm_options,
                                               log_level):
                    print("fc2 was not created properly.")
                    if log_level:
                        print_error()
                    sys.exit(1)
        if output_filename is None:
            filename = 'fc2.hdf5'
        else:
            filename = 'fc2.' + output_filename + '.hdf5'
        if log_level:
            print("Writing fc2 to %s" % filename)
        write_fc2_to_hdf5(phono3py.get_fc2(),
                          filename=filename,
                          p2s_map=p2s_map,
                          compression=compression)

    if log_level:
        show_drift_force_constants(phono3py.get_fc2(),
                                   primitive=phonon_primitive,
                                   name='fc2')
Exemplo n.º 5
0
def set_dataset_and_force_constants(ph3py,
                                    ph3py_yaml=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):
    read_fc = {'fc2': False, 'fc3': False}
    p2s_map = ph3py.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=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=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.dataset is None:
                disp_filename = "disp_fc2.yaml"
        _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_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
Exemplo n.º 6
0
def create_phono3py_force_constants(phono3py,
                                    phonon_supercell_matrix,
                                    settings,
                                    energy_to_eV=None,
                                    distance_to_A=None,
                                    input_filename=None,
                                    output_filename=None,
                                    log_level=1):
    read_fc3 = settings.get_read_fc3()
    read_fc2 = settings.get_read_fc2()
    symmetrize_fc3_r = settings.get_is_symmetrize_fc3_r()
    symmetrize_fc3_q = settings.get_is_symmetrize_fc3_q()
    symmetrize_fc2 = settings.get_is_symmetrize_fc2()
    if settings.get_is_translational_symmetry():
        tsym_type = 1
    elif settings.get_tsym_type() > 0:
        tsym_type = settings.get_tsym_type()
    else:
        tsym_type = 0

    if log_level:
        show_phono3py_force_constants_settings(read_fc3, read_fc2, tsym_type,
                                               symmetrize_fc3_r,
                                               symmetrize_fc3_q,
                                               symmetrize_fc2, settings)

    # fc3
    if (settings.get_is_joint_dos()
            or (settings.get_is_isotope()
                and not (settings.get_is_bterta() or settings.get_is_lbte()))
            or settings.get_read_gamma() or settings.get_read_amplitude()
            or settings.get_constant_averaged_pp_interaction() is not None):
        pass
    else:
        if read_fc3:  # Read fc3.hdf5
            if input_filename is None:
                filename = 'fc3.hdf5'
            else:
                filename = 'fc3.' + input_filename + '.hdf5'
            file_exists(filename, log_level)
            if log_level:
                print("Reading fc3 from %s" % filename)
            fc3 = read_fc3_from_hdf5(filename=filename)
            num_atom = phono3py.get_supercell().get_number_of_atoms()
            if fc3.shape[0] != num_atom:
                print("Matrix shape of fc3 doesn't agree with supercell size.")
                if log_level:
                    print_error()
                sys.exit(1)
            phono3py.set_fc3(fc3)
        else:  # fc3 from FORCES_FC3
            if not _create_phono3py_fc3(
                    phono3py, energy_to_eV, distance_to_A, tsym_type,
                    symmetrize_fc3_r, symmetrize_fc2,
                    settings.get_cutoff_fc3_distance(), input_filename,
                    output_filename, settings.get_use_alm(), log_level):
                print("fc3 was not created properly.")
                if log_level:
                    print_error()
                sys.exit(1)
        if log_level:
            show_drift_fc3(phono3py.get_fc3())

    # fc2
    if read_fc2:
        if input_filename is None:
            filename = 'fc2.hdf5'
        else:
            filename = 'fc2.' + input_filename + '.hdf5'
        file_exists(filename, log_level)
        if log_level:
            print("Reading fc2 from %s" % filename)
        num_atom = phono3py.get_phonon_supercell().get_number_of_atoms()
        phonon_fc2 = read_fc2_from_hdf5(filename=filename)
        if phonon_fc2.shape[0] != num_atom:
            print("Matrix shape of fc2 doesn't agree with supercell size.")
            if log_level:
                print_error()
            sys.exit(1)

        phono3py.set_fc2(phonon_fc2)
    else:
        if log_level:
            print("Solving fc2")

        if phonon_supercell_matrix is None:
            if phono3py.get_fc2() is None:
                if not _create_phono3py_fc2(phono3py, energy_to_eV,
                                            distance_to_A, tsym_type,
                                            symmetrize_fc2, input_filename,
                                            settings.get_use_alm(), log_level):
                    print("fc2 was not created properly.")
                    if log_level:
                        print_error()
                    sys.exit(1)

        else:
            if not _create_phono3py_phonon_fc2(
                    phono3py, energy_to_eV, distance_to_A,
                    tsym_type, symmetrize_fc2, input_filename,
                    settings.get_use_alm(), log_level):
                print("fc2 was not created properly.")
                if log_level:
                    print_error()
                sys.exit(1)
        if output_filename is None:
            filename = 'fc2.hdf5'
        else:
            filename = 'fc2.' + output_filename + '.hdf5'
        if log_level:
            print("Writing fc2 to %s" % filename)
        write_fc2_to_hdf5(phono3py.get_fc2(), filename=filename)

    if log_level:
        show_drift_force_constants(phono3py.get_fc2(), name='fc2')
Exemplo n.º 7
0
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