LoginSignup
14
8

More than 3 years have passed since last update.

pythonのpymatgenの使い方を頑張ってまとめてみる③

Last updated at Posted at 2019-06-12

はじめに

今回もpymatgenの使い方をまとめていきます。頑張っていきましょう。


structureオブジェクトの作り方をもっと細かく

pymatgenのStructure.from_fileを用いれば、cifやjson、poscarなどのファイルを扱うことができますが、その細かいパラメーターを与える場合にはpymatgen.ioを用いる必要があります。以下に、cifファイルをioを用いて扱う方法を示します。

from pymatgen.io.cif import CifParser
parser = CifParser('data_1-ICSD.cif')
structure = parser.get_structures()[0]

このようにすれば、pymatgen.ioを用いてstructureオブジェクトを作成することができます。しかし、どのような違いがあるかいまいち分かりませんね。CifParserの定義をみていきましょう。

def __init__(self, filename, occupancy_tolerance=1., site_tolerance=1e-4):

つまり、第二引数や第三引数に値を与えれば全体やサイトの許容因子(tolerance)をいじることができるわけです。気を付けてほしいのは、parser.get_structures()の戻り値がリスト型になっていることです。そのままでは扱いずらいため、インデックス指定をして変換する必要があります。


poscarから同様にstructureオブジェクトを生成する場合は、以下のようにします。

from pymatgen.io.vasp import Poscar
poscar = Poscar.from_file("POSCAR")
structure = poscar.structure

このようにすれば、poscarからstructureオブジェクトを作成することができます。Structure.from_fileとの違いをみてみようと思って定義を確認したのですが、いまいち利点が分かりませんでした。多分、読む人にposcarファイルを読み取っていることを分かりやすくするためでしょうか(適当)。理解でき次第追記していきます。


pymatgen.ioを用いたposcarとcifの変換

pymatgen.Structure.from_fileでstructureオブジェクトを作成し、structureオブジェクトのto
メソッドを使用して引数のfmtにposcarかcifをしていすれば各々のファイル形式を変換できますが、ioを用いても変換が可能です。具体例をみていきましょう。

from pymatgen.io.cif import CifWriter
from pymatgen.io.vasp import Poscar

poscar = Poscar.from_file("POSCAR")
w = CifWriter(poscar.structure)
w.write_file('mystructure.cif')

Poscar.from_fileでposcarオブジェクトを作成しています。poscarオブジェクトのstructure属性にはstructureオブジェクトが格納されているので、それをCifWriterクラスの引数に渡すと、cifwriterオブジェクトが生成されます。cifwriterオブジェクトのwrite_fileメソッドを用いることで、cifファイルを生成できます。CifWriterクラスの利点をみるために、定義を確認しましょう。

def __init__(self, struct, symprec=None, write_magmoms=False):

なんだかよく分からないのでArgsを確認しましょう。

Args:
struct (Structure): structure to write
symprec (float): If not none, finds the symmetry of the structure
and writes the cif with symmetry information. Passes symprec
to the SpacegroupAnalyzer
write_magmoms (bool): If True, will write magCIF file. Incompatible
with symprec

どうやら、symprecに引数を与えるとcifファイルに対称性の情報を与えることができるようです。magmomsについてはよく分からないですね。今度時間があるときに教授に聞いてみます。

structureオブジェクトの修正方法

structureオブジェクトの各サイトの原子を変換したり、変換した後に場所を指定したりもできます。具体例をみていきましょう。

structure[1] = 'F', [0.51, 0.51, 0.51]
print(structure.formula)
print(structure.frac_coords)

Cr3 Te8 O22 F1

[[0.3192 0.9981 0.8842]
[0.51 0.51 0.51 ]...]

このようにサイト番号1の原子をFに置換した後に、その原子の位置を指定することもできます。


今回はここまでになります。ここまで読んでいただきありがとうござました。

pythonのpymatgenの使い方を頑張ってまとめてみる①
pythonのpymatgenの使い方を頑張ってまとめてみる②

14
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
8