LoginSignup
0
0

More than 3 years have passed since last update.

分子を表示させる雑記 その3

Last updated at Posted at 2019-06-10

classとかのオブジェクト指向に関してはまだ理解できていないので、

副関数ぽくだだだっとべたっと書き書き。

model.py
# -*- coding: utf-8 -*-
import os,sys
import numpy as np
import tkinter as tk
from tkinter import filedialog
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def fileread(x,a):
    with open(x,encoding='utf-8') as B1:
        txt = B1.read()
        c=txt.splitlines()
    b=0
    while b < a:
        del c[:(c.index('')+1)]
        b+=1
    if a==2:
        c=c[1:-1]
    else:
        c=c[:-1]
    return c

def plopoint(C,a,b):
    if a==0:#mol
        A = C[0].split()
        mol_number = int(A[0])
        mol_bond = int(A[1])
        MOL = [(C[i].split())[b] for i in range(1,mol_number)]
        return MOL
    else:
        mol_number = int(len(C))
        COM = [(C[i].split())[b] for i in range(mol_number)]
        return COM

def bond(wx):
    return  np.linalg.norm(wx)

def angle(x,y):
    cos=(np.dot(x,y))/(bond(x)*bond(y))
    return np.rad2deg(np.arccos(cos))


def btn_click1():#mol
    cood = tk.StringVar()
    typ = [('', '*')]
    dir = 'C:\\pg'
    fle = filedialog.askopenfilenames(filetypes = typ, initialdir = dir)#filenameだと階層ごとにばらばらに分割
    fle=list(fle)
    cood= fileread(fle[0],1)
    text.insert('1.0', cood)
    M = plopoint(cood,0,3)
    X = np.array(plopoint(cood,0,0), dtype=float)
    Y = np.array(plopoint(cood,0,2), dtype=float)
    Z = np.array(plopoint(cood,0,1), dtype=float)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X, Y, Z)
    plt.show()

def btn_click2():#com
    cood = tk.StringVar()
    typ = [('', '*')]
    dir = 'C:\\pg'
    fle = filedialog.askopenfilenames(filetypes = typ, initialdir = dir)
    fle=list(fle)
    cood= fileread(fle[0],2)
    text.insert('1.0', cood)
    M = plopoint(cood,1,0)
    X = np.array(plopoint(cood,1,1), dtype=float)
    Y = np.array(plopoint(cood,1,2), dtype=float)
    Z = np.array(plopoint(cood,1,3), dtype=float)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(X, Y, Z)
    plt.show()

def btn_click3():#com bond
    cood = tk.StringVar()
    typ = [('', '*')]
    dir = 'C:\\pg'
    fle = filedialog.askopenfilenames(filetypes = typ, initialdir = dir)
    fle=list(fle)
    cood= fileread(fle[0],2)
    M = plopoint(cood,1,0)
    X = np.array(plopoint(cood,1,1), dtype=float)
    Y = np.array(plopoint(cood,1,2), dtype=float)
    Z = np.array(plopoint(cood,1,3), dtype=float)
    mat = np.c_[X,Y,Z]
    a = txt1.get()
    b = txt2.get()
    ab = bond(mat[int(a)]-mat[int(b)])
    text.insert('1.0',ab)

def btn_click4():#com ang
    cood = tk.StringVar()
    typ = [('', '*')]
    dir = 'C:\\pg'
    fle = filedialog.askopenfilenames(filetypes = typ, initialdir = dir)
    fle=list(fle)
    cood= fileread(fle[0],2)
    M = plopoint(cood,1,0)
    X = np.array(plopoint(cood,1,1), dtype=float)
    Y = np.array(plopoint(cood,1,2), dtype=float)
    Z = np.array(plopoint(cood,1,3), dtype=float)
    mat = np.c_[X,Y,Z]
    a = txt3.get()
    b = txt4.get()
    c = txt5.get()
    abc = angle(mat[int(a)]-mat[int(b)],mat[int(c)]-mat[int(b)])
    text.insert('1.0',abc)

def btn_click5():#com dihe
    cood = tk.StringVar()
    typ = [('', '*')]
    dir = 'C:\\pg'
    fle = filedialog.askopenfilenames(filetypes = typ, initialdir = dir)
    fle=list(fle)
    cood= fileread(fle[0],2)
    M = plopoint(cood,1,0)
    X = np.array(plopoint(cood,1,1), dtype=float)
    Y = np.array(plopoint(cood,1,2), dtype=float)
    Z = np.array(plopoint(cood,1,3), dtype=float)
    mat = np.c_[X,Y,Z]
    a = txt6.get()
    b = txt7.get()
    c = txt8.get()
    d = txt9.get()
    bc=np.cross(mat[int(a)]-mat[int(b)],mat[int(c)]-mat[int(b)])
    cd=np.cross(mat[int(c)]-mat[int(b)],mat[int(d)]-mat[int(b)])
    abcd = angle(bc,cd)
    text.insert('1.0',abcd)

#全体画面の設定
root=tk.Tk()
root.title("GVtypeM")
root.geometry('1200x1200')
root.minsize(300,400)

#ファイルのボタン
button1 = tk.Button(root,text="mol", command=btn_click1)
button1.pack(anchor="n",side="left")
button2 = tk.Button(root,text="com", command=btn_click2)
button2.pack(anchor="n",side="left")

#計算(距離)
lbl = tk.Label(text='番号')
lbl.pack(fill = 'x', padx=20, side = 'top')
txt1 = tk.Entry(width=20)
txt1.pack(fill = 'x', padx=20, side = 'top')
txt2 = tk.Entry(width=20)
txt2.pack(fill = 'x', padx=20, side = 'top')
button3 = tk.Button(root,text="距離", command=btn_click3)
button3.pack(fill = 'x', padx=20, side = 'top')

#角度
lbl2 = tk.Label(text='番号')
lbl2.pack(fill = 'x', padx=20, side = 'top')
txt3 = tk.Entry(width=20)
txt3.pack(fill = 'x', padx=20, side = 'top')
txt4 = tk.Entry(width=20)
txt4.pack(fill = 'x', padx=20, side = 'top')
txt5 = tk.Entry(width=20)
txt5.pack(fill = 'x', padx=20, side = 'top')
button4 = tk.Button(root,text="角度", command=btn_click4)
button4.pack(fill = 'x', padx=20, side = 'top')

#二面角
lbl3 = tk.Label(text='番号')
lbl3.pack(fill = 'x', padx=20, side = 'top')
txt6 = tk.Entry(width=20)
txt6.pack(fill = 'x', padx=20, side = 'top')
txt7 = tk.Entry(width=20)
txt7.pack(fill = 'x', padx=20, side = 'top')
txt8 = tk.Entry(width=20)
txt8.pack(fill = 'x', padx=20, side = 'top')
txt9 = tk.Entry(width=20)
txt9.pack(fill = 'x', padx=20, side = 'top')
button5 = tk.Button(root,text="二面角", command=btn_click5)
button5.pack(fill = 'x', padx=20, side = 'top')


#原子の座標のリストを表示させる場所
text = tk.Text(root)
text.pack(fill = 'x', padx=20, side = 'bottom')

#gridならブロックで自動的に割り当ててくれる
#text_widget.grid(column=7, row=7, sticky=(tk.N, tk.S, tk.E, tk.W))
#root.columnconfigure(0, weight=1)
#root.rowconfigure(0, weight=1)

root.mainloop()

mol,comファイルから座標を取り出してプロットデータに突っ込んだり、

指定したところに計算を行うとかはできるようになった。

なお全てファイルのパスからなにまで全て手動の模様。

ただプロットなりにした点を選択して、選択した点の距離や角度に、ができてない。

さらなるグラフィカルな操作というかなんというか。

3Dグラフィックスとかの分野に片足を突っ込むのは分かるんですけど、

Pythonで何をすればいいのかが全く分からないんですよね。

GaussviewやWinmostarの分子モデリング部分って本当にPythonで作れるんですかね?

誰か教えてクレメンス

以上!





rev.py
    molnum  = list(range(len(cood)))
    for m, n, x, y, z in zip(molnum,M, X, Y, Z):
        label = m,n
        ax.text(x, y, z, label)
    plt.show()

を追加することでプロットした座標に原子と、

原子それぞれの要素の数字を表示することができるようになった。

が、メモリドカ食いで草生える。

原子ごとに色を変えるとかした方がメモリ的には優しいのかもしれない。

あとはカーソルで要素を拾ってくるとかが良いのかもしれない。

0
0
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
0
0