Esempio n. 1
0
def create_FORCE_SETS_from_FORCES_FCx_then_exit(phonon_smat,
                                                input_filename: Optional[str],
                                                cell_filename: Optional[str],
                                                log_level):
    """Convert FORCES_FC3 or FORCES_FC2 to FORCE_SETS."""
    if cell_filename is not None:
        disp_filename = cell_filename
    elif input_filename is None:
        disp_filename = "phono3py_disp.yaml"
    else:
        disp_filename = f"phono3py_disp.{input_filename}.yaml"
    if phonon_smat is not None:
        forces_filename = "FORCES_FC2"
    else:
        forces_filename = "FORCES_FC3"

    if log_level:
        print(f'Displacement dataset is read from "{disp_filename}".')
        print(f'Forces are read from "{forces_filename}"')

    with open(forces_filename, "r") as f:
        len_first_line = get_length_of_first_line(f)

    if len_first_line == 3:
        file_exists(disp_filename, log_level)
        file_exists(forces_filename, log_level)
        ph3yml = Phono3pyYaml()
        ph3yml.read(disp_filename)
        if phonon_smat is None:
            dataset = copy.deepcopy(ph3yml.dataset)
            smat = ph3yml.supercell_matrix
        else:
            dataset = copy.deepcopy(ph3yml.phonon_dataset)
            smat = ph3yml.phonon_supercell_matrix

        if smat is None or (phonon_smat is not None and
                            (phonon_smat != smat).any()):
            if log_level:
                print("")
                print("Supercell matrix is inconsistent.")
                print(f'Supercell matrix read from "{disp_filename}":')
                print(smat)
                print("Supercell matrix given by --dim-fc2:")
                print(phonon_smat)
                print_error()
            sys.exit(1)

        parse_FORCES_FC2(dataset, filename=forces_filename)
        write_FORCE_SETS(dataset)

        if log_level:
            print("FORCE_SETS has been created.")
            print_end()
    else:
        if log_level:
            print("The file format of %s is already readable by phonopy." %
                  forces_filename)
            print_end()
    sys.exit(0)
Esempio n. 2
0
def parse_forces(natom,
                 force_to_eVperA,
                 distance_to_A,
                 cutoff_pair_distance=None,
                 force_filename="FORCES_FC3",
                 disp_filename=None,
                 is_fc2=False,
                 log_level=0):
    disp_dataset = _get_type2_dataset(natom, filename=force_filename)
    if disp_dataset:  # type2
        if log_level:
            print("%d snapshots were found in %s." %
                  (len(disp_dataset['displacements']), "FORCES_FC3"))
        if force_to_eVperA is not None:
            disp_dataset['forces'] *= force_to_eVperA
        if distance_to_A is not None:
            disp_dataset['displacements'] *= distance_to_A
    else:  # type1
        if log_level:
            print("Displacement dataset for %s is read from \"%s\"." %
                  ("fc2" if is_fc2 else "fc3", disp_filename))
        if is_fc2:
            disp_dataset = parse_disp_fc2_yaml(filename=disp_filename)
        else:
            disp_dataset = parse_disp_fc3_yaml(filename=disp_filename)
        if cutoff_pair_distance:
            if ('cutoff_distance' not in disp_dataset
                    or 'cutoff_distance' in disp_dataset and
                    cutoff_pair_distance < disp_dataset['cutoff_distance']):
                disp_dataset['cutoff_distance'] = cutoff_pair_distance
                if log_level:
                    print("Cutoff-pair-distance: %f" % cutoff_pair_distance)
        if disp_dataset['natom'] != natom:
            msg = ("Number of atoms in supercell is not consistent with "
                   "\"%s\"." % disp_filename)
            raise RuntimeError(msg)
        _convert_displacement_unit(disp_dataset, distance_to_A, is_fc2=is_fc2)
        if log_level:
            print("Sets of supercell forces are read from \"%s\"." %
                  force_filename)
            sys.stdout.flush()

        # forces are stored in disp_dataset.
        if is_fc2:
            parse_FORCES_FC2(disp_dataset,
                             filename=force_filename,
                             unit_conversion_factor=force_to_eVperA)
        else:
            parse_FORCES_FC3(disp_dataset,
                             filename=force_filename,
                             unit_conversion_factor=force_to_eVperA)

    return disp_dataset
def _create_phono3py_fc2(phono3py, energy_to_eV, distance_to_A, tsym_type,
                         symmetrize_fc2, input_filename, use_alm, log_level):
    if input_filename is None:
        filename = 'disp_fc3.yaml'
    else:
        filename = 'disp_fc3.' + input_filename + '.yaml'
    if log_level:
        print("Displacement dataset is read from %s." % filename)
    file_exists(filename, log_level)
    disp_dataset = parse_disp_fc3_yaml(filename=filename)
    num_atom = phono3py.get_supercell().get_number_of_atoms()
    if disp_dataset['natom'] != num_atom:
        print("Number of atoms in supercell is not consistent with %s" %
              filename)
        if log_level:
            print_error()
        sys.exit(1)
    _convert_displacement_unit(disp_dataset, distance_to_A, is_fc2=True)

    if log_level:
        print("Sets of supercell forces are read from %s." % "FORCES_FC3")
    file_exists("FORCES_FC3", log_level)
    forces_fc2 = parse_FORCES_FC2(disp_dataset, filename="FORCES_FC3")
    if not forces_fc2:
        return False

    _convert_force_unit(forces_fc2, energy_to_eV, distance_to_A)

    phono3py.produce_fc2(forces_fc2,
                         displacement_dataset=disp_dataset,
                         is_permutation_symmetry=symmetrize_fc2,
                         translational_symmetry_type=tsym_type,
                         use_alm=use_alm)

    return True
def _create_phono3py_fc2(phono3py, force_to_eVperA, distance_to_A,
                         symmetrize_fc2, input_filename, is_compact_fc,
                         fc_calculator, fc_calculator_options, log_level):
    file_exists("FORCES_FC3", log_level)
    natom = phono3py.supercell.get_number_of_atoms()
    disp_dataset = _get_type2_dataset(natom, filename="FORCES_FC3")
    if disp_dataset:
        if log_level:
            print("%d snapshots were found in %s." %
                  (len(disp_dataset['displacements']), "FORCES_FC3"))
        if force_to_eVperA is not None:
            disp_dataset['forces'] *= force_to_eVperA
        if distance_to_A is not None:
            disp_dataset['displacements'] *= distance_to_A
    else:
        if input_filename is None:
            filename = 'disp_fc3.yaml'
        else:
            filename = 'disp_fc3.' + input_filename + '.yaml'
        if log_level:
            print("Displacement dataset for fc2 is read from %s." % filename)
        file_exists(filename, log_level)
        disp_dataset = parse_disp_fc3_yaml(filename=filename)
        if disp_dataset['natom'] != natom:
            print("Number of atoms in supercell is not consistent with %s" %
                  filename)
            if log_level:
                print_error()
            sys.exit(1)
        _convert_displacement_unit(disp_dataset, distance_to_A, is_fc2=True)

        if log_level:
            print("Sets of supercell forces are read from %s." % "FORCES_FC3")

        # forces are stored in disp_dataset.
        parse_FORCES_FC2(disp_dataset,
                         filename="FORCES_FC3",
                         unit_conversion_factor=force_to_eVperA)

    phono3py.produce_fc2(displacement_dataset=disp_dataset,
                         symmetrize_fc2=symmetrize_fc2,
                         is_compact_fc=is_compact_fc,
                         fc_calculator=fc_calculator,
                         fc_calculator_options=fc_calculator_options)

    return True
Esempio n. 5
0
def create_FORCE_SETS_from_FORCES_FCx_then_exit(phonon_smat, input_filename,
                                                log_level):
    if phonon_smat is not None:
        if input_filename is None:
            disp_filename = 'disp_fc2.yaml'
        else:
            disp_filename = 'disp_fc2.' + input_filename + '.yaml'
        forces_filename = "FORCES_FC2"
    else:
        if input_filename is None:
            disp_filename = 'disp_fc3.yaml'
        else:
            disp_filename = 'disp_fc3.' + input_filename + '.yaml'
        forces_filename = "FORCES_FC3"

    with open(forces_filename, 'r') as f:
        len_first_line = get_length_of_first_line(f)

    if len_first_line == 3:
        file_exists(disp_filename, log_level)
        disp_dataset = parse_disp_fc2_yaml(filename=disp_filename)
        file_exists(forces_filename, log_level)
        parse_FORCES_FC2(disp_dataset, filename=forces_filename)
        if log_level:
            print("Displacement dataset was read from \"%s\"." % disp_filename)
        write_FORCE_SETS(disp_dataset)

        if log_level:
            print("FORCE_SETS has been created.")
            print_end()
    else:
        if log_level:
            print("The file format of %s is already readable by phonopy." %
                  forces_filename)
            print_end()
    sys.exit(0)
def _create_phono3py_phonon_fc2(phono3py,
                                force_to_eVperA,
                                distance_to_A,
                                symmetrize_fc2,
                                input_filename,
                                is_compact_fc,
                                use_alm,
                                alm_options,
                                log_level):
    if input_filename is None:
        filename = 'disp_fc2.yaml'
    else:
        filename = 'disp_fc2.' + input_filename + '.yaml'
    if log_level:
        print("Displacement dataset is read from %s." % filename)
    file_exists(filename, log_level)
    disp_dataset = parse_disp_fc2_yaml(filename=filename)
    num_atom = phono3py.get_phonon_supercell().get_number_of_atoms()
    if disp_dataset['natom'] != num_atom:
        print("Number of atoms in supercell is not consistent with %s" %
              filename)
        if log_level:
            print_error()
        sys.exit(1)
    _convert_displacement_unit(disp_dataset, distance_to_A, is_fc2=True)

    if log_level:
        print("Sets of supercell forces are read from %s." %
              "FORCES_FC2")
    file_exists("FORCES_FC2", log_level)
    forces_fc2 = parse_FORCES_FC2(disp_dataset)
    if not forces_fc2:
        return False

    _convert_force_unit(forces_fc2, force_to_eVperA)

    phono3py.produce_fc2(
        forces_fc2,
        displacement_dataset=disp_dataset,
        symmetrize_fc2=symmetrize_fc2,
        is_compact_fc=is_compact_fc,
        use_alm=use_alm,
        alm_options=alm_options)

    return True
Esempio n. 7
0
def parse_forces(
    phono3py,
    ph3py_yaml=None,
    cutoff_pair_distance=None,
    force_filename="FORCES_FC3",
    disp_filename=None,
    fc_type=None,
    log_level=0,
):
    """Read displacements and forces."""
    filename_read_from = None

    if fc_type == "phonon_fc2":
        natom = len(phono3py.phonon_supercell)
    else:
        natom = len(phono3py.supercell)

    # Get dataset from ph3py_yaml. dataset can be None.
    dataset = _extract_datast_from_ph3py_yaml(ph3py_yaml, fc_type)
    if dataset:
        filename_read_from = ph3py_yaml.yaml_filename

    # Try to read FORCES_FC* if type-2 and return dataset.
    # None is returned unless type-2.
    # can emit FileNotFoundError.
    if dataset is None or dataset is not None and not forces_in_dataset(
            dataset):
        _dataset = _get_type2_dataset(natom,
                                      phono3py.calculator,
                                      filename=force_filename,
                                      log_level=log_level)
        # Do not overwrite dataset when _dataset is None.
        if _dataset:
            filename_read_from = force_filename
            dataset = _dataset

    if dataset is None:
        # Displacement dataset is obtained from disp_filename.
        # can emit FileNotFoundError.
        dataset = _read_disp_fc_yaml(disp_filename, fc_type)
        filename_read_from = disp_filename

    if "natom" in dataset and dataset["natom"] != natom:
        msg = ("Number of atoms in supercell is not consistent with "
               '"%s".' % filename_read_from)
        raise RuntimeError(msg)

    if log_level and filename_read_from is not None:
        print('Displacement dataset for %s was read from "%s".' %
              (fc_type, filename_read_from))

    # Overwrite dataset['cutoff_distance'] when necessary.
    if cutoff_pair_distance:
        if "cutoff_distance" not in dataset or (
                "cutoff_distance" in dataset
                and cutoff_pair_distance < dataset["cutoff_distance"]):
            dataset["cutoff_distance"] = cutoff_pair_distance
            if log_level:
                print("Cutoff-pair-distance: %f" % cutoff_pair_distance)

    # Type-1 FORCES_FC*.
    # dataset comes either from disp_fc*.yaml or phono3py*.yaml.
    if not forces_in_dataset(dataset):
        if fc_type == "phonon_fc2":
            parse_FORCES_FC2(dataset, filename=force_filename)
        else:
            parse_FORCES_FC3(dataset, filename=force_filename)

        if log_level:
            print('Sets of supercell forces were read from "%s".' %
                  force_filename)
            sys.stdout.flush()

    _convert_unit_in_dataset(dataset, phono3py.calculator)

    return dataset