Drkcore

31 03 2017 Python Tweet

pexpectとclickで劇的ビフォーアフター

何故か最近GAMESSでFMOを計算することに全力を注いでおりまして。

GAMESSでFMO計算を行う場合にはfmoutilという便利ツールを使うことが多いのだが、これは対話的な環境の上にデフォルトというものが用意されていないので、毎回毎回手打ちしないといけない。

それから、きれいな複合体データでないと上手く動かない(途中に欠損があると駄目)とか実践で使っていく場合には色々と不都合があるので、今は自分用の 超便利有能空気読みすぎpython版のプリプロセッサ をガシガシ書いている。

で、ヘッダーチェックのためにfmoutilをすごい重宝してるんだけどなんせ毎回手打ちで1日何十回も対話していると流石に疲れる。

でpexpectとclickを使ってこんな感じに実行できるようにしてみた。

$ ./fuu --help
Usage: fuu [OPTIONS] IFILE

  Create FMO Input

Options:
  -o, --ofile TEXT         output option (default: fmo.inp)
  -n, --node INTEGER       node option (default: 4)
  -c, --core INTEGER       core option (default: 8)
  -m, --memory INTEGER     memory option (default: 48000)
  -w, --wavefunction TEXT  wavefunction option (default: RHF)
  -b, --basis-set TEXT     basis set option (default: STO-3G)
  --help                   Show this message and exit.

コードは至ってシンプルなのにモダンなfmoutilに生まれ変わって満足

#!/usr/bin/python
import pexpect
import click

bs = {"STO-3G": 1, "3-21G": 2, "6-31G": 3, "6-31G*":4, "6-311G*":5}
wf = {"RHF": 1, "DFT": 2, "MP2": 3, "CC": 4, "MCSCF": 5}

@click.command(help="Create FMO Input")
@click.argument("ifile")
@click.option("--ofile",  "-o", default="fmo.inp", help="output option (default: fmo.inp)")
@click.option("--node",  "-n", default=4, help="node option (default: 4)")
@click.option("--core",  "-c", default=8, help="core option (default: 8)")
@click.option("--memory",  "-m", default=48000, help="memory option (default: 48000)")
@click.option("--wavefunction",  "-w", default="RHF", help="wavefunction option (default: RHF)")
@click.option("--basis-set",  "-b", default="STO-3G", help="basis set option (default: STO-3G)")
def cli(ifile, ofile, node, core, memory, wavefunction, basis_set):
    c = pexpect.spawn("./fmoutil")
    c.expect(".*") #   Enter Job # :     1. Generate FMO input data for GAMESS
    c.sendline("1")
    c.expect(".*") #   Enter Input PDB File(s) :
    c.sendline(ifile)
    c.expect(".*") #   Enter Output File : 
    c.sendline(ofile)
    c.expect(".*") # 1> How many computer nodes do you want to use ?
    c.sendline("{}".format(node))
    c.expect(".*") # 1> How many CPU cores does each node have ?
    c.sendline("{}".format(core))
    c.expect(".*") # 1> How much memory does each node have (in megabyte) ?
    c.sendline("{}".format(memory))
    c.expect(".*") # 1> Choose runtyp (1:energy, 2:gradient, 3:geometry optimization)
    c.sendline("1") 
    c.expect(".*") # 1> How many layers do you want to define (1-5) ?
    c.sendline("1")
    c.expect(".*") # 1> Choose wavefunction type (1:RHF, 2:DFT, 3:MP2, 4:CC, 5:MCSCF)
    c.sendline("{}".format(wf[wavefunction]))
    c.expect(".*") # 1> Enter basis set (1:STO-3G, 2:3-21G, 3:6-31G, 4:6-31G*, 5:6-311G*)
    c.sendline("{}".format(bs[basis_set]))
    c.expect(".*") # 1> Do you add diffuse functions on COO- groups ? (1:yes, 2:no)
    c.sendline("2")
    c.expect(".*") # 1> Enter the desired n-body expansion (2 or 3) ?
    c.sendline("2")
    c.expect(".*") # 1> Would you like to perform the pair analysis (PIEDA) ? (1:yes, 2:no)
    c.sendline("1")
    c.expect(".*") # 1> Would you like to print Mulliken charges (1:yes, 2:no) ?
    c.sendline("1")
    c.expect(".*") # 1> Would you like to produce a cube file with the total electron density ? (1:no, 2:standard, 3:sparse)
    c.sendline("1")
    c.expect(".*") # 1> Whould you like to use PCM ? (1:yes, 2:no)
    c.sendline("2")
    c.expect(".*") # 1> Enter fragment size (1:1res-per-frg, 2:2res-per-frg)
    c.sendline("1")
    c.expect(".*") # 1> are S-S bonded CYSs combined to one ? (1:yes, 2:no)
    c.sendline("1")
    c.expect(".*") # 1> is GLY combined to the neighbor ? (1:yes, 2:no)
    c.sendline("2")
    c.expect(".*") # Enter Job # :
    c.sendline("")
    c.close()

if __name__ == "__main__":
    cli()

以上、clickの提供でお送りしました☆

あと、clickで--helpしたときにデフォルトの値も表示させたかったんだけど、やりかたわからんかった。誰か知ってたら教えてください。尚、オプションにchoiceを入れてないのは僕の怠慢ですw

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021