Vpython and Chemistry

From ITRS
Jump to: navigation, search

簡介

有鑑於學習高中選修化學第二章(分子間作用力和固體的結構)時,

老師教學上很難敘述三維的分子結構,

自己學習上雖能夠聽懂,

但是某些狀況下卻是有一種「看不到東西就不能說服我」的感覺,

於是花約2小時的時間,

把正在學習中的python程式能力運用在此。

資料


Code

#!/usr/bin/env python
from visual import *

#welcome messenge
print
print "================================================="
print "||Welcome to Heron's Little Chemical Program!! ||"
print "================================================="
print

#cheating or not?
cheat_on = (raw_input("cheat or not ?? (y/n) >> ") == "y")
print

#choose atom type
atom_type=["","ion","metal"]
print "choose atom type"
for i in range(1,len(atom_type)):
    print str(i)+"."+str(atom_type[i])+" ",
atom_type_chosen=str(atom_type[int(raw_input(">> "))])
print

#choose structure type
print "choose structure type"
if atom_type_chosen == "ion":
    structure_type=["","Body-centered cubic","Face-centered cubic","Zincblende"]
elif atom_type_chosen == "metal":
    structure_type=["","hcp","Face-centered cubic","Body-centered cubic"]
else:
    print "System Error, call Heron now...!"
    raise SystemExit
for i in range(1,len(structure_type)):
    print str(i)+"."+str(structure_type[i])+" ",
structure_type_chosen = str(structure_type[int(raw_input(">> "))])
print

#insert length
l=float(raw_input("l= "))
if atom_type_chosen == "ion":
    r_p=float(raw_input("(r+)= "))
    r_m=float(raw_input("(r-)= "))
elif atom_type_chosen == "metal":
    r=float(raw_input("r= "))
    r_p=r_m=r
else:
    print "System Error, call Heron now...!"
    raise SystemExit

l_h=l/2

#print out messenge
print "-------------------------------------------"
print "atom type = " + atom_type_chosen
print "structure type = " + structure_type_chosen
print "l=%f r_p=%f r_m=%f" % (l,r_p,r_m)
print "-------------------------------------------"

if structure_type_chosen =="hcp":
    print "hcp structure is not complete yet...\n"
    print "Exit..."
    raise SystemExit

elif structure_type_chosen == "Body-centered cubic" or\
     structure_type_chosen == "Face-centered cubic":

    #start
    print "drawing ..."

    #center
    if not(atom_type_chosen == "metal" and\
           structure_type_chosen == "Face-center cubic"):
        center=sphere(radius=r_p,color=color.red,opacity=0.6)
        if atom_type_chosen == "metal":
            center.color=color.green
        if cheat_on : center.material=materials.earth

    #corner
        #position
    corners=[
        sphere(pos=( l_h, l_h, l_h)),
        sphere(pos=( l_h, l_h,-l_h)),
        sphere(pos=( l_h,-l_h, l_h)),
        sphere(pos=(-l_h, l_h, l_h)),
        sphere(pos=(-l_h,-l_h, l_h)),
        sphere(pos=( l_h,-l_h,-l_h)),
        sphere(pos=(-l_h, l_h,-l_h)),
        sphere(pos=(-l_h,-l_h,-l_h)),
        ]
        #settings
    for corner in corners:
        corner.color=color.green
        corner.opacity=0.6
        corner.radius=r_m
        if cheat_on:
            if atom_type_chosen == "ion":
                corner.material=materials.wood
            else:
                corner.material=materials.earth

    #face & edge for "Face-centered"
    if structure_type_chosen == "Face-centered cubic":
        #faces
            #position
        faces=[
                sphere(pos=( l_h,   0,   0)),
                sphere(pos=(   0, l_h,   0)),
                sphere(pos=(   0,   0, l_h)),
                sphere(pos=(-l_h,   0,   0)),
                sphere(pos=(   0,-l_h,   0)),
                sphere(pos=(   0,   0,-l_h))
                ]
            #setting
        for face in faces:
            face.color=corners[0].color
            face.radius=r_m
            face.opacity=0.5
            if cheat_on:
                if atom_type_chosen == "ion":
                    face.material=materials.marble
                else:
                    face.material=materials.earth

        #edge
            #position
        if atom_type_chosen != "metal":
            edges=[
                    sphere(pos=(   0, l_h, l_h)),
                    sphere(pos=(   0, l_h,-l_h)),
                    sphere(pos=(   0,-l_h, l_h)),
                    sphere(pos=(   0,-l_h,-l_h)),
                    sphere(pos=( l_h,   0, l_h)),
                    sphere(pos=( l_h,   0,-l_h)),
                    sphere(pos=(-l_h,   0, l_h)),
                    sphere(pos=(-l_h,   0,-l_h)),
                    sphere(pos=( l_h, l_h,   0)),
                    sphere(pos=( l_h,-l_h,   0)),
                    sphere(pos=(-l_h, l_h,   0)),
                    sphere(pos=(-l_h,-l_h,   0))
                    ]
                #setting
            for edge in edges:
                edge.color=color.red
                edge.opacity=0.5
                edge.radius=r_p

elif structure_type_chosen == "Zincblende":
    #center
        #position
    centers=[
            sphere(pos=( l/4, l/4, l/4)),
            sphere(pos=( l/4,-l/4,-l/4)),
            sphere(pos=(-l/4, l/4,-l/4)),
            sphere(pos=(-l/4,-l/4, l/4))
            ]
        #setting
    for center in centers:
        center.opacity=0.5
        center.radius=r_p
        if cheat_on:
            center.material=materials.earth
        else:
            center.color=color.blue

    #face
        #position
    faces=[
            sphere(pos=( l_h, l_h, l_h)),
            sphere(pos=( l_h, l_h,-l_h)),
            sphere(pos=( l_h,-l_h, l_h)),
            sphere(pos=(-l_h, l_h, l_h)),
            sphere(pos=(-l_h,-l_h, l_h)),
            sphere(pos=( l_h,-l_h,-l_h)),
            sphere(pos=(-l_h, l_h,-l_h)),
            sphere(pos=(-l_h,-l_h,-l_h))
            ]
        #setting
    for face in faces:
        face.color=color.yellow
        face.opacity=0.5
        face.radius=r_m
        if cheat_on:
            face.material=materials.wood

    #edge
        #position
    edges=[
            sphere(pos=( l_h,   0,   0)),
            sphere(pos=(   0, l_h,   0)),
            sphere(pos=(   0,   0, l_h)),
            sphere(pos=(-l_h,   0,   0)),
            sphere(pos=(   0,-l_h,   0)),
            sphere(pos=(   0,   0,-l_h)),
            ]
        #setting
    for edge in edges:
        edge.color=faces[0].color
        edge.optical=0.5
        edge.radius=r_m
        if cheat_on:
            edge.material=faces[0].material

#error messenge
else:
    print "System Error, call Heron now...!"
    raise SystemExit

print "done..."

Heron Yang 2010.6.13

安裝執行


See Also

Comments

blog comments powered by Disqus