24小时热门版块排行榜    

查看: 746  |  回复: 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()

回复此楼
好好学习,天天向上。
已阅   回复此楼   关注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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 应该是93bebmhtak前后十一个字符比较关键 +6 Lanmanbaby 2026-08-09 10/500 2026-08-09 17:23 by 天神眷顾
[基金申请] 关于代码变化问题,想知道的进来 +13 且听虎啸 2026-08-07 17/850 2026-08-09 15:08 by 天神眷顾
[基金申请] fileCode有新解读? +10 Tide man 2026-08-08 18/900 2026-08-09 12:55 by 仁砚薪传
[基金申请] 关于豆爷回答的JTJC与%2F数量 +5 yang182083 2026-08-06 7/350 2026-08-08 18:29 by zhanghaozhu
[基金申请] 2026国自然放榜时间 +3 布布和一二 2026-08-08 3/150 2026-08-08 18:15 by Lightingo
[基金申请] 国基金的申报应该改成非等额制,评价高的钱多评价低的钱少,但是增加资助率 +7 a089 2026-08-07 7/350 2026-08-08 18:05 by gltch
[基金申请] 好奇怪的filecode +3 布布和一二 2026-08-08 4/200 2026-08-08 17:46 by zhanghaozhu
[基金申请] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 qTvzQBRAHjCi 2026-08-07 4/200 2026-08-08 17:22 by oEVWOejN9taj
[基金申请] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 Vi50GxzrFcSG 2026-08-07 5/250 2026-08-08 16:27 by oEVWOejN9taj
[考博] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 Vi50GxzrFcSG 2026-08-07 4/200 2026-08-08 16:22 by oEVWOejN9taj
[论文投稿] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 WQyTGMbfH7Cx 2026-08-07 4/200 2026-08-08 15:02 by oEVWOejN9taj
[论文投稿] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 HEQlVqMTIA7d 2026-08-07 5/250 2026-08-08 14:22 by oEVWOejN9taj
[基金申请] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +4 KXLV3nuBVBY7 2026-08-07 5/250 2026-08-08 14:07 by oEVWOejN9taj
[硕博家园] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 Vi50GxzrFcSG 2026-08-07 6/300 2026-08-08 10:19 by 3OOjAIS77qg2
[基金申请] 关于filecode +4 布布和一二 2026-08-07 7/350 2026-08-07 22:55 by zhanghaozhu
[基金申请] 固定端突然变了,今天 +6 archvillain 2026-08-06 10/500 2026-08-07 16:03 by 医学老男孩
[基金申请] 大家散了吧,后缀研究没有意义,别浪费时间了,过好目前的每一天,不要焦虑 +5 Tide man 2026-08-06 7/350 2026-08-07 13:11 by 医学老男孩
[基金申请] filecode +8 布布和一二 2026-08-06 11/550 2026-08-06 20:41 by tangpu318
[基金申请] 求各位大神看下 100+6 hpkpkpkp 2026-08-05 33/1650 2026-08-06 14:49 by zhiyanjiang
[基金申请] 8月时间戳变的,举个手。玩一下,释放压力 +9 archvillain 2026-08-04 11/550 2026-08-05 20:06 by wlwhappy
信息提示
请填处理意见