Tikhonova Polina. Task 4

In [14]:
import numpy
import scipy.special
import scipy.misc
from npy2cube import *

from xmlrpclib import ServerProxy
from IPython.display import Image

import os
PATH = os.getcwd()

cmd = ServerProxy(uri="http://localhost:9124/RPC2")
cmd.reinitialize()

Формулы

$$ r(x,y,z) = \sqrt{x^2+y^2+z^2} $$

$$ \theta(x,y,z) = \arccos\left(\frac{z}{r(x,y,z)}\right) $$

$$ \phi(x,y,z) = \arctan\left(\frac{y}{x}\right) $$

$$ a_0 = 1 $$

$$ w = \left(\frac{2r}{na_0}\right)^l \exp\left(-\frac{r}{na_0}\right) \cdot L_{n-l-1}^{2l+1}\left(\frac{2r}{na_0}\right) \cdot Y^m_l(\phi,\theta)$$

In [15]:
def w(n,l,m,d):    
    """
    Где n,l,m это основные квантовые числа:
        • n- основное число (1,2,3..)
        • l – орбитальное число (0,1,2.. n-1)
        • m – магнитное число (-l..+l)
    """

    x,y,z = numpy.mgrid[-d:d:30j,-d:d:30j,-d:d:30j]

    r = lambda x,y,z: numpy.sqrt(x**2+y**2+z**2)
    theta = lambda x,y,z: numpy.arccos(z/r(x,y,z))
    phi = lambda x,y,z: numpy.arctan(y/x)

    a0 = 1.

    R = lambda r,n,l: (2*r/n/a0)**l * numpy.exp(-r/n/a0) * scipy.special.genlaguerre(n-l-1,2*l+1)(2*r/n/a0)
    WF = lambda r,theta,phi,n,l,m: R(r,n,l) * scipy.special.sph_harm(m,l,phi,theta)
    absWF = lambda r,theta,phi,n,l,m: numpy.absolute(WF(r,theta,phi,n,l,m))**2

    return WF(r(x,y,z),theta(x,y,z),phi(x,y,z),n,l,m)

Создание cube файлов

In [29]:
d = 15
step= 2*d/29.

fold = 'attempt_3/'

# Зададим цикл по перебору квантовых чисел

for n in range(0,4):
    for l in range(0,n):
        for m in range(0,l+1,1):
            grid= w(n,l,m,d)
            name=fold+'%s-%s-%s' % (n,l,m)+'.cube'
            # для сохранения нужно задать координаты старта grid и шаг по каждому направлению
            npy2cube(grid,(-d,-d, -d),(step,step,step),name)

Отрисовка

In [57]:
# cmd.volume_ramp_new('name of profile', [\
#     point1:  x,   R,   G,   B,   y(opacity),\
#     point2:  x,   R,   G,   B,   y(opacity),\
#     point3:  x,   R,   G,   B,   y(opacity),\
#     .
#     .
#     .
#     pointn:  x,   R,   G,   B,   y(opacity),\
# ])
# (where x is smth like thickness, of distance from points in cube file)
cmd.reinitialize()
pml='''
cmd.volume_ramp_new('ramp1-0-0', [\
      0.00, 0.00, 0.00, 1.00, 0.00, \
      0.00, 0.00, 1.00, 1.00, 0.08, \
      0.00, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp2-0-0', [\
     -0.05, 0.00, 0.00, 1.00, 0.00, \
     -0.04, 0.00, 1.00, 1.00, 0.05, \
     -0.04, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp2-1-0', [\
      0.03, 0.00, 0.00, 1.00, 0.00, \
      0.03, 0.00, 1.00, 1.00, 0.10, \
      0.04, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp2-1-1', [\
     -0.02, 0.00, 0.00, 1.00, 0.00, \
     -0.01, 0.00, 1.00, 1.00, 0.04, \
     -0.01, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp3-0-0', [\
     -0.07, 0.00, 0.00, 1.00, 0.00, \
     -0.03, 0.00, 1.00, 1.00, 0.04, \
     -0.02, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp3-1-0', [\
      0.12, 0.00, 0.00, 1.00, 0.00, \
      0.15, 0.00, 1.00, 1.00, 0.05, \
      0.17, 0.00, 0.00, 1.00, 0.00, \
    ])


cmd.volume_ramp_new('ramp3-1-1', [\
      0.12, 0.00, 0.00, 1.00, 0.00, \
      0.12, 0.00, 1.00, 1.00, 0.07, \
      0.13, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp3-2-0', [\
      0.32, 0.00, 0.00, 1.00, 0.00, \
      0.35, 0.00, 1.00, 1.00, 0.04, \
      0.49, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp3-2-1', [\
      0.20, 0.00, 0.00, 1.00, 0.00, \
      0.25, 0.00, 1.00, 1.00, 0.09, \
      0.26, 0.00, 0.00, 1.00, 0.00, \
    ])

cmd.volume_ramp_new('ramp3-2-2', [\
      0.17, 0.00, 0.00, 1.00, 0.00, \
      0.17, 0.00, 1.00, 1.00, 0.03, \
      0.27, 0.00, 0.00, 1.00, 0.00, \
    ])
'''
cmd.do(pml)
In [58]:
pymol_actions_1 = lambda x: cmd.do('''
    load {1}/attempt_3/{0}.cube, {0}
    volume {0}_volume, {0}
    volume_color {0}_volume, ramp{0}
    pseudoatom foo, pos=[0, 12, 0],label="{0}"
    set label_color, 0xc3b8ff, foo
    set label_size, -2.5, foo
'''.format(x, PATH))
pymol_actions_2 = lambda x: cmd.do('''  
    png {1}/pictures/{0}.png, dpi=300, width=10cm
    '''.format(x, PATH))
pymol_actions_3 = lambda:cmd.do('delete all')
In [61]:
cmd.reinitialize()
for i in os.listdir('attempt_3/'):
    pymol_actions_1(i.split('.cube')[0])
    raw_input("Press Enter to continue...")
    pymol_actions_2(i.split('.cube')[0])
    pymol_actions_3()
print 'Finish'
Press Enter to continue...
Finish

Посмотрим, что получилось

In [95]:
from PIL import Image
result = Image.new("RGB", (900, 800))

index = 0
for n in range(0,4):
    for l in range(0,n):
        for m in range(0,l+1,1):
            name='pictures/%s-%s-%s.png' % (n,l,m)
            file_ = name
            path = os.path.expanduser(file_)
            img = Image.open(path)
            img.thumbnail((300, 200), Image.ANTIALIAS)
            y = index // 3 * 200
            x = index % 3 * 300
            w, h = img.size
            #print('pos {0},{1} :{2}'.format(x, y, path))
            result.paste(img, (x, y, x + w, y + h))
            index+=1
result.save('pictures/result.png')
Image.open('pictures/result.png')
Out[95]:

Сравним, что мы получили с orca

In [107]:
pymol_actions_1 = lambda x: cmd.do('''
    load {1}/tikhonova_server/{0}.cube, {0}
    volume {0}_volume, {0}
    pseudoatom foo, pos=[0, 2, 0],label="{0}"
    set label_color, 0xc3b8ff, foo
    set label_size, -0.5, foo
'''.format(x, PATH))
pymol_actions_2 = lambda x: cmd.do('''  
    png {1}/tikhonova_server/pictures/{0}.png, dpi=300, width=10cm
    '''.format(x, PATH))
pymol_actions_3 = lambda:cmd.do('delete all')
In [108]:
cmd.reinitialize()
for i in os.listdir('tikhonova_server/'):
    if i.endswith('.cube'):
        pymol_actions_1(i.split('.cube')[0])
        raw_input("Press Enter to continue...")
        pymol_actions_2(i.split('.cube')[0])
        pymol_actions_3()
print 'Finish'
Press Enter to continue...
Press Enter to continue...
Press Enter to continue...
Press Enter to continue...
Finish
In [112]:
result = Image.new("RGB", (400, 400))

index = 0
for n in range(0,4):
            name='tikhonova_server/pictures/H-%d.png' % (n+1)
            file_ = name
            path = os.path.expanduser(file_)
            img = Image.open(path)
            img.thumbnail((200, 200), Image.ANTIALIAS)
            y = index // 2 * 200
            x = index % 2 * 200
            w, h = img.size
            #print('pos {0},{1} :{2}'.format(x, y, path))
            result.paste(img, (x, y, x + w, y + h))
            index+=1
result.save('tikhonova_server/pictures/result.png')
Image.open('tikhonova_server/pictures/result.png')
Out[112]: