<commit_before># Copyright (C) 2014 <NAME>
# All rights reserved.
#
# This file is part of phonopy.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
#
# * Neither the name of the phonopy project nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import os
import yaml
import numpy as np
from phonopy.interface.phonopy_yaml import PhonopyYaml
from phonopy.structure.dataset import get_displacements_and_forces
from phonopy.structure.cells import determinant
from phonopy.interface.vasp import sort_positions_by_symbols

calculator_info = {
    'abinit': {'option': {'name': "--abinit",
                          'help': "Invoke Abinit mode"}},
    'aims': {'option': {'name': "--aims",
                        'help': "Invoke FHI-aims mode"}},
    'cp2k': {'option': {'name': "--cp2k",
                        'help': "Invoke CP2K mode"}},
    'crystal': {'option': {'name': "--crystal",
                           'help': "Invoke CRYSTAL mode"}},
    'dftbp': {'option': {'name': "--dftb+",
                         'help': "Invoke dftb+ mode"}},
    'elk': {'option': {'name': "--elk",
                       'help': "Invoke elk mode"}},
    'qe': {'option': {'name': "--qe",
                      'help': "Invoke Quantum espresso (QE) mode"}},
    'siesta': {'option': {'name': "--siesta",
                          'help': "Invoke Siesta mode"}},
    'turbomole': {'option': {'name': "--turbomole",
                             'help': "Invoke TURBOMOLE mode"}},
    'vasp': {'option': {'name': "--vasp",
                        'help': "Invoke Vasp mode"}},
    'wien2k': {'option': {'name': "--wien2k",
                          'help': "Invoke Wien2k mode"}},
    'castep': {'option': {'name': "--castep",
                           'help': "Invoke CASTEP mode"}},
    'fleur': {'option': {'name': "--fleur",
                       'help': "Invoke Fleur mode"}},
}


def add_arguments_of_calculators(parser, calculator_info):
    for calculator in calculator_info:
        option = calculator_info[calculator]['option']
        parser.add_argument(
            option['name'], dest="%s_mode" % calculator, action="store_true",
            default=False, help=option['help'])


def get_interface_mode(args_dict):
    """Return calculator name

    The calculator name is obtained from command option arguments where
    argparse is used. The argument attribute name has to be
    "{calculator}_mode". Then this method returns "{calculator}".

    """

    for calculator in calculator_info:
        mode = "%s_mode" % calculator
        if mode in args_dict and args_dict[mode]:
            return calculator
    return None


def convert_crystal_structure(filename_in,
                              interface_in,
                              filename_out,
                              interface_out):
    cell, optional_structure_info = read_crystal_structure(
        filename=filename_in,
        interface_mode=interface_in)
    units_in = get_default_physical_units(interface_in)
    units_out = get_default_physical_units(interface_out)
    factor = units_in['distance_to_A'] / units_out['distance_to_A']
    cell.cell = cell.cell * factor
    write_crystal_structure(filename_out, cell, interface_mode=interface_out)


def write_crystal_structure(filename,
                            cell,
                            interface_mode=None,
                            optional_structure_info=None):
    """Utility method to write out a crystal structure

    filename : str, optional
        File name to be used to write out the crystal structure.
    cell : PhonopyAtoms
        Crystal structure
    interface_mode : str, optional
        Calculator interface such as 'vasp', 'qe', ... Default is None,
        that is equivalent to 'vasp'.
    optional_structure_info : tuple, optional
        Information returned by the method ``read_crystal_structure``.
        See the docstring. Default is None.

    """

    if interface_mode is None or interface_mode == 'vasp':
        import phonopy.interface.vasp as vasp
        vasp.write_vasp(filename, cell)
    elif interface_mode == 'abinit':
        import phonopy.interface.abinit as abinit
        abinit.write_abinit(filename, cell)
    elif interface_mode == 'qe':
        import phonopy.interface.qe as qe
        pp_filenames = optional_structure_info[1]
        qe.write_pwscf(filename, cell, pp_filenames)
    elif interface_mode == 'wien2k':
        import phonopy.interface.wien2k as wien2k
        _, npts, r0s, rmts = optional_structure_info
        wien2k.write_wein2k(filename, cell, npts, r0s, rmts)
    elif interface_mode == 'elk':
        import phonopy.interface.elk as elk
        sp_filenames = optional_structure_info[1]
        elk.write_elk(filename, cell, sp_filenames)
    elif interface_mode == 'siesta':
        import phonopy.interface.siesta as siesta
        atypes = optional_structure_info[1]
        siesta.write_siesta(filename, cell, atypes)
    elif interface_mode == 'cp2k':
        import phonopy.interface.cp2k as cp2k
        _, tree = optional_structure_info
        cp2k.write_cp2k_by_filename(filename, cell, tree)
    elif interface_mode == 'crystal':
        import phonopy.interface.crystal as crystal
        conv_numbers = optional_structure_info[1]
        crystal.write_crystal(filename, cell, conv_numbers)
    elif interface_mode == 'dftbp':
        import phonopy.interface.dftbp as dftbp
        dftbp.write_dftbp(filename, cell)
    elif interface_mode == 'turbomole':
        import phonopy.interface.turbomole as turbomole
        turbomole.write_turbomole(filename, cell)
    elif interface_mode == 'aims':
        import phonopy.interface.aims as aims
        aims.write_aims(filename, cell)
    elif interface_mode == 'castep':
        import phonopy.interface.castep as castep
        castep.write_castep(filename, cell)
    elif interface_mode == 'fleur':
        import phonopy.interface.fleur as fleur
        speci, restlines = optional_structure_info
        fleur.write_fleur(filename, cell, speci, N, restlines)
    else:
        raise RuntimeError("No calculator interface was found.")


def write_supercells_with_displacements(interface_mode,
                                        supercell,
                                        cells_with_disps,
                                        optional_structure_info,
                                        displacement_ids=None,
                                        zfill_width=3,
                                        additional_info=None):
    """Utility method to write out supercell structures with displacements

    interface_mode : str
        Calculator interface such as 'vasp', 'qe', ...
    supercell : Supercell
        Supercell.
    cells_with_disps : list of PhonopyAtoms
        Supercells with displacements.<commit_msg>Define variable that was missing.<commit_after># Copyright (C) 2014 <NAME>
# All rights reserved.
#
# This file is part of phonopy.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
#
# * Neither the name of the phonopy project nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import os
import yaml
import numpy as np
from phonopy.interface.phonopy_yaml import PhonopyYaml
from phonopy.structure.dataset import get_displacements_and_forces
from phonopy.structure.cells import determinant
from phonopy.interface.vasp import sort_positions_by_symbols

calculator_info = {
    'abinit': {'option': {'name': "--abinit",
                          'help': "Invoke Abinit mode"}},
    'aims': {'option': {'name': "--aims",
                        'help': "Invoke FHI-aims mode"}},
    'cp2k': {'option': {'name': "--cp2k",
                        'help': "Invoke CP2K mode"}},
    'crystal': {'option': {'name': "--crystal",
                           'help': "Invoke CRYSTAL mode"}},
    'dftbp': {'option': {'name': "--dftb+",
                         'help': "Invoke dftb+ mode"}},
    'elk': {'option': {'name': "--elk",
                       'help': "Invoke elk mode"}},
    'qe': {'option': {'name': "--qe",
                      'help': "Invoke Quantum espresso (QE) mode"}},
    'siesta': {'option': {'name': "--siesta",
                          'help': "Invoke Siesta mode"}},
    'turbomole': {'option': {'name': "--turbomole",
                             'help': "Invoke TURBOMOLE mode"}},
    'vasp': {'option': {'name': "--vasp",
                        'help': "Invoke Vasp mode"}},
    'wien2k': {'option': {'name': "--wien2k",
                          'help': "Invoke Wien2k mode"}},
    'castep': {'option': {'name': "--castep",
                           'help': "Invoke CASTEP mode"}},
    'fleur': {'option': {'name': "--fleur",
                       'help': "Invoke Fleur mode"}},
}


def add_arguments_of_calculators(parser, calculator_info):
    for calculator in calculator_info:
        option = calculator_info[calculator]['option']
        parser.add_argument(
            option['name'], dest="%s_mode" % calculator, action="store_true",
            default=False, help=option['help'])


def get_interface_mode(args_dict):
    """Return calculator name

    The calculator name is obtained from command option arguments where
    argparse is used. The argument attribute name has to be
    "{calculator}_mode". Then this method returns "{calculator}".

    """

    for calculator in calculator_info:
        mode = "%s_mode" % calculator
        if mode in args_dict and args_dict[mode]:
            return calculator
    return None


def convert_crystal_structure(filename_in,
                              interface_in,
                              filename_out,
                              interface_out):
    cell, optional_structure_info = read_crystal_structure(
        filename=filename_in,
        interface_mode=interface_in)
    units_in = get_default_physical_units(interface_in)
    units_out = get_default_physical_units(interface_out)
    factor = units_in['distance_to_A'] / units_out['distance_to_A']
    cell.cell = cell.cell * factor
    write_crystal_structure(filename_out, cell, interface_mode=interface_out)


def write_crystal_structure(filename,
                            cell,
                            interface_mode=None,
                            optional_structure_info=None):
    """Utility method to write out a crystal structure

    filename : str, optional
        File name to be used to write out the crystal structure.
    cell : PhonopyAtoms
        Crystal structure
    interface_mode : str, optional
        Calculator interface such as 'vasp', 'qe', ... Default is None,
        that is equivalent to 'vasp'.
    optional_structure_info : tuple, optional
        Information returned by the method ``read_crystal_structure``.
        See the docstring. Default is None.

    """

    if interface_mode is None or interface_mode == 'vasp':
        import phonopy.interface.vasp as vasp
        vasp.write_vasp(filename, cell)
    elif interface_mode == 'abinit':
        import phonopy.interface.abinit as abinit
        abinit.write_abinit(filename, cell)
    elif interface_mode == 'qe':
        import phonopy.interface.qe as qe
        pp_filenames = optional_structure_info[1]
        qe.write_pwscf(filename, cell, pp_filenames)
    elif interface_mode == 'wien2k':
        import phonopy.interface.wien2k as wien2k
        _, npts, r0s, rmts = optional_structure_info
        wien2k.write_wein2k(filename, cell, npts, r0s, rmts)
    elif interface_mode == 'elk':
        import phonopy.interface.elk as elk
        sp_filenames = optional_structure_info[1]
        elk.write_elk(filename, cell, sp_filenames)
    elif interface_mode == 'siesta':
        import phonopy.interface.siesta as siesta
        atypes = optional_structure_info[1]
        siesta.write_siesta(filename, cell, atypes)
    elif interface_mode == 'cp2k':
        import phonopy.interface.cp2k as cp2k
        _, tree = optional_structure_info
        cp2k.write_cp2k_by_filename(filename, cell, tree)
    elif interface_mode == 'crystal':
        import phonopy.interface.crystal as crystal
        conv_numbers = optional_structure_info[1]
        crystal.write_crystal(filename, cell, conv_numbers)
    elif interface_mode == 'dftbp':
        import phonopy.interface.dftbp as dftbp
        dftbp.write_dftbp(filename, cell)
    elif interface_mode == 'turbomole':
        import phonopy.interface.turbomole as turbomole
        turbomole.write_turbomole(filename, cell)
    elif interface_mode == 'aims':
        import phonopy.interface.aims as aims
        aims.write_aims(filename, cell)
    elif interface_mode == 'castep':
        import phonopy.interface.castep as castep
        castep.write_castep(filename, cell)
    elif interface_mode == 'fleur':
        import phonopy.interface.fleur as fleur
        speci, restlines = optional_structure_info
        N = abs(determinant(additional_info['supercell_matrix']))
        fleur.write_fleur(filename, cell, speci, N, restlines)
    else:
        raise RuntimeError("No calculator interface was found.")


def write_supercells_with_displacements(interface_mode,
                                        supercell,
                                        cells_with_disps,
                                        optional_structure_info,
                                        displacement_ids=None,
                                        zfill_width=3,
                                        additional_info=None):
    """Utility method to write out supercell structures with displacements

    interface_mode : str
        Calculator interface such as 'vasp', 'qe', ...
    supercell : Supercell
        Supercell.
    cells_with_disps : list of PhonopyAtoms
        Supercells with displacements.
{'fraction_non_alphanumeric': 0.09341929205692738, 'fraction_numerical': 0.003284271986376353, 'mean_word_length': 2.583133580300719, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2246055', 'n_tokens_mistral': 4696, 'n_tokens_neox': 4426, 'n_words': 1403}