24小时热门版块排行榜    

查看: 670  |  回复: 2

zyj8119

木虫 (著名写手)

[交流] 【原创】MAPS中构筑超晶胞的python程序 已有2人参与

CODE:
#!/usr/bin/python
###########################################################################
#                   buildCluster.py  -  description
#                   -------------------------------
#   begin                : Fri Dec 15 16:11:04 CET 2006
#   copyright            : (C) 2006 by Scienomics
#   email                : Joerg-Ruediger.Hill@scienomics.com
###########################################################################
#
###########################################################################
#                                                                         #
#   This program and all subroutines,  data, and  files used by it are    #
#   protected by copyright and hence may not be used, copied, modified    #
#   transmitted, inspected, or executed by any means including the use    #
#   of electronic data processing equipment,  xerography, or any other    #
#   methods  without the express  written permission  of the copyright    #
#   holder.                                                               #
#                                                                         #
#   Copyright (C) 2006 Scienomics S. A.                                   #
#                                                                         #
###########################################################################
#
# $Id: buildCluster.py,v 1.10 2010/03/29 15:15:56 jrh Exp $
# $Log: buildCluster.py,v $
# Revision 1.10  2010/03/29 15:15:56  jrh
# Moved to new way of retrieving plugin objects
#
# Revision 1.9  2008/10/20 09:25:06  lperi
# Clear undo stack (bug 2562).
#
# Revision 1.8  2008/10/14 15:21:17  jrh
# Fixed bug 2520
#
# Revision 1.7  2008/01/16 12:21:18  jrh
# Moved to Qt4
#
# Revision 1.6  2007/01/16 10:28:26  jrh
# Removed automatic creation of cell since this might crash
#
# Revision 1.5  2006/12/15 16:07:31  jrh
# Added header and documentation
#
#

import Maps
import MapsChemistryDataModel
import MapsViewer
from PyQt4 import *

class ClusterBuilder:
  """ This class implements a cluster generating algorithm
      by cutting atoms from a periodic system based on a
      distance criterion. The class takes either the active
      molecule or asks the user to load a molecule if no
      active molecule is found, puts a molecule into a box
      at density 1.0 if the system is not periodic already,
      and builds a cluster by copying all atoms which are
      within 5 Ang from the central atom to a new model.
  """

  app=Maps.MapsApp.getContainer()
  cdm=MapsChemistryDataModel.ChemistryDataModel.get()
  viewerPlugin=MapsViewer.ViewerPlugin.get()
  project=None
  molecule=None

  def loadMolecule(self):
    activeViewer=self.viewerPlugin.getActiveViewer()
    if activeViewer != None:
      self.molecule=activeViewer.getMolecule()
      projectName=self.cdm.findProjectNameForMolecule(self.molecule)
      self.project=self.cdm.findProjectByName(projectName)
      QtGui.qApp.processEvents()
      return(0)
    else:
      QtGui.QMessageBox.information(self.app, "Note", "Please load a molecule from the\nfollowing dialog.")
      # Load a molecule from an existing file
      dialog=self.app.fileDialog
      dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
      dialog.setWindowTitle("Open file")
      filters=QtCore.QStringList()
      filters.append("All files (*)")
      dialog.setFilters(filters)
      dialog.selectFile("")
      if dialog.exec_(Maps.FileDialog.DATA) == QtGui.QDialog.Accepted:
        fileName=str(dialog.selectedFiles()[0])
        self.app.openDocumentFile(fileName, None, "")
        projects=self.cdm.getProjects()
        self.project=projects[0]
        molecules=self.project.getMolecules()
        self.molecule=molecules[0]
        self.viewerPlugin.start(self.molecule)
        QtGui.qApp.processEvents()
        return(0)
      else:
        return(1)

  def createSuperCell(self):
    # Create a 3 x 3 x 3 super cell. If the system is not periodic tell
    # the user and exit
    if not self.molecule.isPeriodic():
      QtGui.QMessageBox.information(self.app, "Note", "Please use a periodic system with this script.")
      return(0);
    else:
      cell=self.molecule.getActiveConformation().getUnitcell()
    cell.setDuplicate(3, 3, 3, 1)
    cell.makeSuperCell(1)
    QtGui.qApp.processEvents()
    return(1)

  def cutCluster(self):
    # Cut a cluster by finding the atom closest to the center of the
    # super cell and including all atoms at a distance of up to
    # 5.0 Ang from this atom
    center=self.molecule.calculateCenterOfGravity(self.molecule.atoms)
    min=100.0
    centerAtom=None
    for atom in self.molecule.atoms:
      pos=atom.getCoordinates()
      diff=pos-center
      if diff.abs() < min:
        min=diff.abs()
        centerAtom=atom
    viewer=self.viewerPlugin.getActiveViewer()
    # Clear the undo stack to prevent a discontinuity
    # in the recorded editing actions which could
    # cause a crash
    viewer.clearUndoStack()
    viewer.setSelectionMode(MapsViewer.Viewer.DISTANCE, 5.0)
    viewer.selectAtoms(centerAtom, 1)
    viewer.copy()
    # Create a new model in the same project and paste the cluster
    # in there
    cluster=MapsChemistryDataModel.Molecule(self.cdm)
    cluster.setName("Cluster")
    self.project.addMolecule(cluster)
    self.viewerPlugin.start(cluster)
    clusterViewer=self.viewerPlugin.getActiveViewer()
    clusterViewer.paste()
    clusterViewer.centerMolecule()
    clusterViewer.getMoleculeRenderer().setSelection(cluster.atoms, 0)
    QtGui.qApp.processEvents()

  def run(self):
    if not self.loadMolecule():
      if self.createSuperCell():
        self.cutCluster()

#-------------------------
if __name__ == '__main__':
  builder=ClusterBuilder()
  builder.run()

回复此楼

» 本帖已获得的红花(最新10朵)

» 猜你喜欢

好好学习,天天向上。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
送鲜花一朵
2楼2012-03-22 16:11:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

juwendy

铁虫 (正式写手)

顶一个,支持楼主。
3楼2012-03-22 21:09:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zyj8119 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 301求调剂 +5 yy要上岸呀 2026-03-17 5/250 2026-03-17 20:20 by peike
[考研] 281求调剂(0805) +3 烟汐忆海 2026-03-16 7/350 2026-03-17 20:16 by peike
[考研] 293求调剂 +6 世界首富 2026-03-11 6/300 2026-03-17 17:04 by ruiyingmiao
[考研] 材料与化工专硕调剂 +5 heming3743 2026-03-16 5/250 2026-03-17 14:03 by 勇敢太监王公公
[考研] 085600材料与化工 +4 安全上岸! 2026-03-16 4/200 2026-03-17 14:02 by 勇敢太监王公公
[考研] 一志愿南京大学,080500材料科学与工程,调剂 +4 Jy? 2026-03-16 4/200 2026-03-17 11:02 by gaoqiong
[考研] 286求调剂 +3 lemonzzn 2026-03-16 5/250 2026-03-16 20:43 by lemonzzn
[考研] 一志愿985,本科211,0817化学工程与技术319求调剂 +5 Liwangman 2026-03-15 5/250 2026-03-16 17:10 by 我的船我的海
[基金申请] 今年的国基金是打分制吗? 50+3 zhanghaozhu 2026-03-14 3/150 2026-03-16 17:07 by 北京莱茵润色
[考研] 304求调剂 +5 素年祭语 2026-03-15 5/250 2026-03-16 17:00 by 我的船我的海
[考研] 277材料科学与工程080500求调剂 +3 自由煎饼果子 2026-03-16 3/150 2026-03-16 14:10 by 运气yunqi
[考研] 材料与化工一志愿南昌大学327求调剂推荐 +7 Ncdx123456 2026-03-13 8/400 2026-03-16 12:15 by karry wen
[考研] 机械专硕调剂 +3 笨笨兔子 2026-03-12 3/150 2026-03-15 20:02 by 栗子粥?
[考研] 22408总分284求调剂 +3 InAspic 2026-03-13 3/150 2026-03-15 11:10 by zhq0425
[考研] 材料工程327求调剂 +3 xiaohe12w 2026-03-11 3/150 2026-03-14 20:20 by ms629
[考研] 265求调剂 +4 威化饼07 2026-03-12 4/200 2026-03-14 17:23 by userper
[考研] 328求调剂 +3 5201314Lsy! 2026-03-13 6/300 2026-03-14 15:31 by hyswxzs
[考研] 26调剂/材料科学与工程/总分295/求收留 +9 2026调剂侠 2026-03-12 9/450 2026-03-13 20:46 by 18595523086
[考研] 311求调剂 +3 冬十三 2026-03-13 3/150 2026-03-13 20:41 by JourneyLucky
[考研] 材料与化工085600调剂求老师收留 +9 jiaanl 2026-03-11 9/450 2026-03-13 20:22 by JourneyLucky
信息提示
请填处理意见