def collect_cell_info(supercell_matrix=None, primitive_matrix=None, interface_mode=None, cell_filename=None, chemical_symbols=None, enforce_primitive_matrix_auto=False, command_name="phonopy", symprec=1e-5): # In some cases, interface mode falls back to phonopy_yaml mode. fallback_reason = _fallback_to_phonopy_yaml(supercell_matrix, interface_mode, cell_filename) if fallback_reason: _interface_mode = 'phonopy_yaml' elif interface_mode is None: _interface_mode = None else: _interface_mode = interface_mode.lower() unitcell, optional_structure_info = read_crystal_structure( filename=cell_filename, interface_mode=_interface_mode, chemical_symbols=chemical_symbols, command_name=command_name) # Error check if unitcell is None: err_msg = _get_error_message(optional_structure_info, interface_mode, fallback_reason, cell_filename, command_name) return err_msg # Retrieve more information on cells (interface_mode_out, supercell_matrix_out, primitive_matrix_out) = _collect_cells_info( _interface_mode, optional_structure_info, command_name, interface_mode, supercell_matrix, primitive_matrix, enforce_primitive_matrix_auto) # Another error check msg_list = [ "Crystal structure was read from \"%s\"." % optional_structure_info[0], ] if supercell_matrix_out is None: msg_list.append( "Supercell matrix (DIM or --dim) information was not found.") return "\n".join(msg_list) if np.linalg.det(unitcell.get_cell()) < 0.0: msg_list.append("Lattice vectors have to follow the right-hand rule.") return "\n".join(msg_list) # Succeeded! if _interface_mode == 'phonopy_yaml': phpy_yaml = optional_structure_info[1] else: phpy_yaml = None return (unitcell, supercell_matrix_out, primitive_matrix_out, optional_structure_info, interface_mode_out, phpy_yaml)
def test_phonopy_atoms_behavior(sc_structure, tmpdir): print(tmpdir) tmpdir.chdir() # actual = structure_to_phonopy_atoms(sc_structure) sc_structure.to(fmt="poscar", filename="POSCAR") a, _ = read_crystal_structure("POSCAR") b = PhonopyAtoms(atoms=a) print(type(a.get_cell())) print(a.get_atomic_numbers()) assert_same_phonopy_atoms(a, b)
def get_cell_settings(phonopy_yaml=None, supercell_matrix=None, primitive_matrix=None, unitcell=None, supercell=None, unitcell_filename=None, supercell_filename=None, calculator=None, symprec=1e-5): if unitcell_filename is not None: cell, optional_structure_info = read_crystal_structure( filename=unitcell_filename, interface_mode=calculator) smat = get_supercell_matrix(supercell_matrix) pmat = primitive_matrix elif supercell_filename is not None: cell, optional_structure_info = read_crystal_structure( filename=supercell_filename, interface_mode=calculator) smat = np.eye(3, dtype='intc', order='C') if primitive_matrix is None or primitive_matrix == "auto": pmat = 'auto' elif unitcell is not None: cell = PhonopyAtoms(atoms=unitcell) smat = get_supercell_matrix(supercell_matrix) pmat = primitive_matrix elif supercell is not None: cell = PhonopyAtoms(atoms=supercell) smat = np.eye(3, dtype='intc', order='C') if primitive_matrix is None or primitive_matrix == "auto": pmat = 'auto' else: raise RuntimeError("Cell has to be specified.") if cell is None: filename = optional_structure_info[0] msg = "'%s' could not be found." % filename raise FileNotFoundError(msg) pmat = _get_primitive_matrix(pmat, cell, symprec) return cell, smat, pmat
def get_cell_settings( supercell_matrix=None, primitive_matrix=None, unitcell=None, supercell=None, unitcell_filename=None, supercell_filename=None, calculator=None, symprec=1e-5, log_level=0, ): """Return crystal structures.""" optional_structure_info = None if primitive_matrix is None or (type(primitive_matrix) is str and primitive_matrix == "auto"): # noqa E129 pmat = "auto" else: pmat = primitive_matrix if unitcell_filename is not None: cell, optional_structure_info = _read_crystal_structure( filename=unitcell_filename, interface_mode=calculator) smat = supercell_matrix if log_level: print('Unit cell structure was read from "%s".' % optional_structure_info[0]) elif supercell_filename is not None: cell, optional_structure_info = read_crystal_structure( filename=supercell_filename, interface_mode=calculator) smat = np.eye(3, dtype="intc", order="C") if log_level: print('Supercell structure was read from "%s".' % optional_structure_info[0]) elif unitcell is not None: cell = PhonopyAtoms(atoms=unitcell) smat = supercell_matrix elif supercell is not None: cell = PhonopyAtoms(atoms=supercell) smat = np.eye(3, dtype="intc", order="C") else: raise RuntimeError("Cell has to be specified.") if optional_structure_info is not None and cell is None: filename = optional_structure_info[0] msg = "'%s' could not be found." % filename raise FileNotFoundError(msg) pmat = get_primitive_matrix(pmat, symprec=symprec) return cell, smat, pmat
def get_cell_settings(phonopy_yaml=None, supercell_matrix=None, primitive_matrix=None, unitcell=None, supercell=None, unitcell_filename=None, supercell_filename=None, calculator=None, symprec=1e-5, log_level=0): optional_structure_info = None if (primitive_matrix is None or (type(primitive_matrix) is str and primitive_matrix == "auto")): pmat = 'auto' else: pmat = primitive_matrix if unitcell_filename is not None: cell, optional_structure_info = _read_crystal_structure( filename=unitcell_filename, interface_mode=calculator) smat = supercell_matrix if log_level: print("Unit cell structure was read from \"%s\"." % optional_structure_info[0]) elif supercell_filename is not None: cell, optional_structure_info = read_crystal_structure( filename=supercell_filename, interface_mode=calculator) smat = np.eye(3, dtype='intc', order='C') if log_level: print("Supercell structure was read from \"%s\"." % optional_structure_info[0]) elif unitcell is not None: cell = PhonopyAtoms(atoms=unitcell) smat = supercell_matrix elif supercell is not None: cell = PhonopyAtoms(atoms=supercell) smat = np.eye(3, dtype='intc', order='C') else: raise RuntimeError("Cell has to be specified.") if optional_structure_info is not None and cell is None: filename = optional_structure_info[0] msg = "'%s' could not be found." % filename raise FileNotFoundError(msg) pmat = _get_primitive_matrix(pmat, cell, symprec) return cell, smat, pmat
def _read_crystal_structure(filename=None, interface_mode=None): try: return read_crystal_structure(filename=filename, interface_mode=interface_mode) except FileNotFoundError: raise except Exception: msg = [ "============================ phonopy.load " "============================", " Reading crystal structure file failed in phonopy.load.", " Maybe phonopy.load(..., calculator='<calculator name>') " "expected?", "============================ phonopy.load " "============================", ] raise RuntimeError("\n".join(msg))
def _read_crystal_structure(filename=None, interface_mode=None): try: return read_crystal_structure(filename=filename, interface_mode=interface_mode) except FileNotFoundError: raise except: print( "============================ phonopy.load ============================" ) print(" Reading crystal structure file failed in phonopy.load.") print( " Maybe phonopy.load(..., calculator='<calculator name>') expected?" ) print( "============================ phonopy.load ============================" ) raise
import numpy as np import seekpath from phonopy import Phonopy from phonopy.structure.atoms import PhonopyAtoms from phonopy.interface.calculator import read_crystal_structure unitcell, _ = read_crystal_structure("POSCAR", interface_mode='vasp') cell=unitcell.get_cell() positions = unitcell.get_scaled_positions() numbers = np.unique(unitcell.get_chemical_symbols(), return_inverse=True)[1] path_data = seekpath.get_path((cell, positions, numbers)) labels = path_data['point_coords'] band_ranges = [] for set in path_data['path']: band_ranges.append([labels[set[0]], labels[set[1]]]) dict= {'ranges': band_range , 'labels': path_data['path']} with open("output.txt","w") as f : for i in range(len(dict['labels'])): f.writelines(str(dict['labels'][i])+":"+str(dict['ranges'][i]))
def collect_cell_info( supercell_matrix=None, primitive_matrix=None, interface_mode=None, cell_filename=None, chemical_symbols=None, enforce_primitive_matrix_auto=False, phonopy_yaml_cls=None, ): """Collect crystal structure information from inputs. Note ---- When the crystal structure is read from phonopy.yaml like file, ``supercell_matrix`` and ``primitive_matrix`` are ignored. Parameters ---------- supercell_matrix : array_like or None 3x3 transformation matrix or when it is a diagonal matrix, three diagonal elements. Default is None. See also shape_supercell_matrix. primitive_matrix : array_like, str, or None 3x3 transformation matrix or a character representing centring or None. Default is None. See also get_primitive_matrix. interface_mode : str or None Force calculator or crystal structure format name. cell_filename : str or None Input cell filename. chemical_symbols : list of str List of chemical symbols or unit cell. enforce_primitive_matrix_auto : bool Enforce primitive_matrix='auto' when True. Default is False. phonopy_yaml_cls : Class object PhonopyYaml like class name. This is used to return its instance when needed. Returns ------- dict : "unitcell": PhonopyAtoms Unit cell. "supercell_matrix": ndarray "primitive_matrix": ndarray "optional_structure_info": list See read_crystal_structure. "interface_mode": str Force calculator or crystal structure format name. "phonopy_yaml": None or instance of the class given by phonopy_yaml_cls Not None when crystal structure was read phonopy.yaml like file. "error_message" : str Unless error exists, this entry should not be in this dict. Otherwise, some error exists. The error message is storedin the string. """ # In some cases, interface mode falls back to phonopy_yaml mode. fallback_reason = _fallback_to_phonopy_yaml(supercell_matrix, interface_mode, cell_filename) if fallback_reason: _interface_mode = "phonopy_yaml" elif interface_mode is None: _interface_mode = None else: _interface_mode = interface_mode.lower() if phonopy_yaml_cls is None: _phonopy_yaml_cls = PhonopyYaml else: _phonopy_yaml_cls = phonopy_yaml_cls unitcell, optional_structure_info = read_crystal_structure( filename=cell_filename, interface_mode=_interface_mode, chemical_symbols=chemical_symbols, phonopy_yaml_cls=_phonopy_yaml_cls, ) # Error check if unitcell is None: err_msg = _get_error_message( optional_structure_info, fallback_reason, cell_filename, _phonopy_yaml_cls, ) return {"error_message": err_msg} # Retrieve more information on cells ( interface_mode_out, supercell_matrix_out, primitive_matrix_out, ) = _collect_cells_info( _interface_mode, optional_structure_info, interface_mode, supercell_matrix, primitive_matrix, ) err_msg = [] unitcell_filename = optional_structure_info[0] if supercell_matrix_out is None: err_msg.append( "Supercell matrix (DIM or --dim) information was not found.") if cell_filename is None and ( unitcell_filename == get_default_cell_filename(interface_mode_out)): err_msg += [ "", "Phonopy read the crystal structure from the file having the default " "filename ", "of each calculator. In this case, supercell matrix has to be " "specified.", "Because this is the old style way of using %s," % _phonopy_yaml_cls.command_name, ] filenames = [ '"%s"' % name for name in _phonopy_yaml_cls.default_filenames[:-1] ] err_msg += [ '"%s" was read being prefered to files such as ' % unitcell_filename, '%s, or "%s".' % (", ".join(filenames), _phonopy_yaml_cls.default_filenames[-1]), ] err_msg += [ "", 'If crystal structure is expected to be read from some "*.yaml" file,', 'Please rename "%s" to something else.' % unitcell_filename, ] if np.linalg.det(unitcell.cell) < 0.0: err_msg.append("Lattice vectors have to follow the right-hand rule.") if len(err_msg) > 0: err_msg = [ 'Crystal structure was read from "%s".' % unitcell_filename ] + err_msg return {"error_message": "\n".join(err_msg)} if enforce_primitive_matrix_auto: primitive_matrix_out = "auto" if _interface_mode == "phonopy_yaml": phpy_yaml: PhonopyYaml = optional_structure_info[1] else: phpy_yaml = None return { "unitcell": unitcell, "supercell_matrix": supercell_matrix_out, "primitive_matrix": primitive_matrix_out, "optional_structure_info": optional_structure_info, "interface_mode": interface_mode_out, "phonopy_yaml": phpy_yaml, }
parser.add_argument("--calc", action="store", type=str, dest="calc", default='vasp', help="Calculator (vasp,abinit,castep)") parser.add_argument("--tolerance", action="store", type=float, dest="tol", default=1e-05, help="Symmetry tolerance") args = parser.parse_args() cell, optional_structure_info = read_crystal_structure( filename=args.in_fn, interface_mode=args.calc) print("Cell:") for v in cell.cell: print("% 9.7f % 9.7f % 9.7f" % (v[0], v[1], v[2])) print("Species:") print(cell.get_chemical_symbols()) print('Atomic positions:') print(cell.get_scaled_positions()) structure = [cell.cell, cell.get_scaled_positions(), cell.get_atomic_numbers()] res = seekpath.getpaths.get_path(structure, with_time_reversal=True, recipe='hpkot',