VASP.vasp_input のソースコード

#import os
import sys
#import shutil
import glob
#import csv
#import re
import numpy as np
from numpy import exp, log, sin, cos, tan, arcsin, arccos, arctan, pi
#from scipy.interpolate import interp1d
#from pprint import pprint
#from matplotlib import pyplot as plt


from tklib.tkfile import tkFile
from tklib.tkutils import IsDir, IsFile, SplitFilePath
from tklib.tkutils import terminate, pint, pfloat, getarg, getintarg, getfloatarg
from tklib.tkcrystal.tkcif import tkCIF
from tklib.tkcrystal.tkcrystal import tkCrystal
from tklib.tkcrystal.tkatomtype import tkAtomType


"""
Manage VASP input files

このモジュールは、VASP (Vienna Ab initio Simulation Package) の入力ファイルを管理するための機能を提供します。
特に、CIF (Crystallographic Information File) 形式の結晶構造データから、VASPで利用可能なPOSCAR形式のファイルを生成します。

:doc:`vasp_input_usage`
"""

#================================
# global parameters
#================================
debug = 0

# mode: 'cif2poscar'
mode = 'cif2poscar'

# input cif path
cifpath    = '*.cif'
# output poscar path
poscarpath = 'POSCAR'

# cif read configuration
single = 1
findvalidstructure = 1


#=============================
# Treat argments
#=============================
[ドキュメント] def usage(): """ 概要: スクリプトの正しい使用方法を標準出力に表示します。 詳細説明: この関数は、プログラムのコマンドライン引数の正しい書式と例をユーザーに提示します。 主に不正な引数やヘルプ要求時に呼び出されます。 :returns: None """ global mode global cifpath, poscarpath print("") print("Usage:") print(" (a) python {} cif2poscar cif_path POSCAR_path".format(sys.argv[0])) print(" ex: python {} {} {} {}".format(sys.argv[0], 'cif2poscar', cifpath, poscarpath))
[ドキュメント] def updatevars(): """ 概要: コマンドライン引数に基づいてグローバル変数を更新します。 詳細説明: `sys.argv`からコマンドライン引数を読み込み、`mode`、`cifpath`、 `poscarpath`といったグローバル変数を設定します。 無効なモードが指定された場合は、`usage()`を表示してプログラムを終了します。 :returns: None """ global mode global cifpath, poscarpath mode = getarg(1, mode) if mode == 'cif2poscar': cifpath = getarg(2, cifpath) poscarpath = getarg(3, poscarpath) else: terminate("Error: Invalide mode [{}]".format(mode), usage = usage)
[ドキュメント] def cif2poscar(cifpath, poscarpath): """ 概要: CIFファイルを読み込み、VASP POSCAR形式で出力します。 詳細説明: 指定されたCIFファイルのパスを読み込み、結晶構造データを解析します。 `cifpath`がワイルドカード(例: `*.cif`)の場合、 マッチするファイルの中からソートされた最初のファイルを自動的に選択します。 解析した結晶データ(格子定数、格子ベクトル、原子種、原子座標)を基に、 VASPのPOSCAR形式で`poscarpath`にファイルを出力します。 POSCARにはSelective dynamicsとDirect座標が使用されます。 ファイルが見つからない、またはCIFデータの読み込みに失敗した場合は、エラーで終了します。 :param cifpath: str 入力CIFファイルのパス。ワイルドカードも指定可能です。 :param poscarpath: str 出力POSCARファイルのパス。 :returns: None """ # if cifpath is not a valied path, assume it represents wild card like *.cif. # Search file list by the wild card and take the first file of the sorted file list print("") print("cif path: ", cifpath) if not IsFile(cifpath): flist = glob.glob(cifpath) try: flist.sort() cifpath = flist[0] except: terminate("Error: Invalid ciffile [{}]".format(cifpath), usage = usage) if not IsFile(cifpath): terminate("Error: Invalid ciffile [{}]".format(cifpath), usage = usage) dirname, basename, sample_name, ext = SplitFilePath(cifpath) print("sample name: ", sample_name) print("") print("Read [{}]".format(cifpath)) cif = tkCIF() cif.debug = debug cifdata = cif.ReadCIF(cifpath, find_valid_structure = findvalidstructure) cif.Close() if not cifdata: terminate("Error: Could not get cifdat from ciffile [{}]".format(ciffile), usage = usage) cry = cifdata.GetCrystal() # cry.PrintInf() a, b, c, alpha, beta, gamm = cry.LatticeParameters() aij = cry.LatticeVectors() / a AtomTypes = cry.AtomTypeList() nAtomTypes = len(AtomTypes) AtomSites = cry.ExpandedAtomSiteList() nAtomSites = len(AtomSites) print("Write to [{}]".format(poscarpath)) out = tkFile(poscarpath, 'w') if not out: terminate("Error: Could not write to [{}]".format(poscarpath), usage = usage) out.Write(sample_name + '\n') out.Write("{}\n".format(a)) out.Write(" {:17.14f} {:17.14f} {:17.14f}\n".format(*aij[0])) out.Write(" {:17.14f} {:17.14f} {:17.14f}\n".format(*aij[1])) out.Write(" {:17.14f} {:17.14f} {:17.14f}\n".format(*aij[2])) for i in range(nAtomTypes): t = AtomTypes[i] #.AtomType() returns the full name of ion etc, like Ca2+ # typea = t.AtomType() #.AtomTypeOnly() returns the name of element, like Ca typeo = t.AtomTypeOnly() # charge = t.Charge() out.Write("{:>6} ".format(typeo)) out.Write("\n") for i in range(nAtomTypes): t = AtomTypes[i] typeo = t.AtomTypeOnly() nat = cry.count_by_type(typeo) out.Write("{:6} ".format(nat)) out.Write("\n") out.Write("Selective dynamics\n") out.Write("Direct\n") for it in range(nAtomTypes): for isite in range(nAtomSites): atom = AtomSites[isite] iAtomType = atom.iAtomType() if iAtomType == it: pos = atom.Position() out.Write(" {:17.14f} {:17.14f} {:17.14f} {:2} {:2} {:2}\n" .format(*pos, 'T', 'T', 'T')) out.Close() terminate(None, usage = usage, pause = True)
[ドキュメント] def main(): """ 概要: スクリプトの主要な処理フローを制御します。 詳細説明: コマンドライン引数の解析 (`updatevars()`) を行い、 設定された`mode`に基づいて適切な処理関数を呼び出します。 現在サポートされているモードは 'cif2poscar' のみです。 サポートされていないモードが指定された場合はエラーメッセージを表示し、終了します。 :returns: None """ global mode global cifpath, poscarpath updatevars() print("") print("=============== Manage VASP input files ============") print("") print("mode: ", mode) if mode == 'cif2poscar': cif2poscar(cifpath, poscarpath) else: terminate("Error: Invalide mode [{}]".format(mode), usage = usage, pause = True)
if __name__ == "__main__": main()