用户工具

站点工具


atk:如何为_quantumatk_创建新的附加组件

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
atk:如何为_quantumatk_创建新的附加组件 [2019/07/30 19:06] – [例三:读取电子密度的插件] xie.congweiatk:如何为_quantumatk_创建新的附加组件 [2019/09/01 08:34] (当前版本) – [如何为 QuantumATK 创建新的附加组件] dong.dong
行 1: 行 1:
 ====== 如何为 QuantumATK 创建新的附加组件 ====== ====== 如何为 QuantumATK 创建新的附加组件 ======
-^ **版本:** 2015\\ \\ //对于 QuantumATK,附加组件是包含一个或多个插件的 Python 模块,可用于向软件添加新功能。有几种类型的插件可供选择。本教程将关注允许 QuantumATK 读写新数据格式的插件类型。在本教程中包含了三个示例。第一个是从 XYZ 文件中读取分子构型的插件,第二个是读取电子密度的插件。//  ^ {{ :atk:section_addon-20190730.png?400 }}  ^+^ **版本:** 2015 
 + 
 +在 QuantumATK,附加组件是包含一个或多个插件的 Python 模块,可用于向软件添加新功能。有几种类型的插件可供选择。本教程将关注允许 QuantumATK 读写新数据格式的插件类型。在本教程中包含了三个示例。第一个是从 XYZ 文件中读取分子构型的插件,第二个是读取电子密度的插件。  ^ {{ :atk:section_addon-20190730.png?400 }}  ^
 ===== 附加组件模块的基本结构 ===== ===== 附加组件模块的基本结构 =====
  
行 260: 行 262:
 ==== 构造密度 ==== ==== 构造密度 ====
  
 +
 +此代码将定义电子密度并将其保存到文件 [[https://docs.quantumwise.com/_downloads/electron_density.npz|↓ electron_density.npz]]。以下将是我们的插件将读取的密度文件。
 +
 +<code python>
 +1   import numpy
 +2   
 +3   # Define a orthogonal cell.
 +4   cell = numpy.array( [ [ 5.0, 0.0, 0.0 ],
 +5                         [ 0.0, 5.0, 0.0 ],
 +6                         [ 0.0, 0.0, 5.0 ] ] )
 +7   # Create a 3D grid of points from 0 to 5 in x, y, and z.
 +8   x = numpy.linspace(0.0, 5.0)
 +9   y = numpy.linspace(0.0, 5.0)
 +10   z = numpy.linspace(0.0, 5.0)
 +11   xx, yy, zz = numpy.meshgrid(x, y, z, indexing='ij')
 +12   
 +13   # Define an electron density as a Gaussian centered at (2.5, 2.5, 2.5) times a
 +14   # sine wave in the x direction.
 +15   density = numpy.exp(-(xx-2.5)**2 - (yy-2.5)**2 - (zz-2.5)**2) * numpy.sin(yy-2.5)
 +16   
 +17   # Save the cell and density to a .npz file.
 +18   numpy.savez('electron_density.npz', cell=cell, density=density)
 +</code>
 +
 +这种电子密度不是物理意义上的,因为它在某些地方会是负的,但它可以让我们轻易地仔细检查数据是否被正确读取。可以在此处下载脚本 [[https://docs.quantumwise.com/_downloads/make_npz_electron_density.py|↓ source code]]。
 ==== 编写 NPZ 滤波器附加组件 ==== ==== 编写 NPZ 滤波器附加组件 ====
 +
 +附加组件的目录结构如下所示:
 +
 +<code python>  
 +NPZFilters/
 +    __init__.py
 +    NPZLabFloor.py
 +</code>
 +
 +与前面的示例一样,我们将首先关注 ''__init__.py'' 文件:
 +
 +<code python>  
 +1   import datetime
 +2   import NPZLabFloor
 +3   
 +4   __addon_description__ = "Plugin for reading a NPZ formatted electron density."
 +5   __addon_version__ = "1.0"
 +6   __addon_date__ = datetime.date(2014, 9, 1)
 +7   
 +8   __plugins__ = [NPZLabFloor.NPZLabFloor]
 +9   
 +10   def reloadPlugins():
 +11       reload(NPZLabFloor)
 +</code>
 +
 +这里没有什么新的内容。现在我们需要定义实际的插件类:
 +
 +<code python>  
 +1   import numpy
 +2   import os
 +3   
 +4   import NLEngine
 +5   
 +6   from API import LabFloorImporterPlugin
 +7   from API import LabFloorItem
 +8   from NL.Analysis.ElectronDensity import ElectronDensity
 +9   from NL.Analysis.GridValues import GridValues
 +10   from NL.ComputerScienceUtilities.NLFlag import Spin
 +11   from NL.CommonConcepts.PhysicalQuantity import Angstrom
 +12
 +13class NPZLabFloor(LabFloorImporterPlugin):
 +14    """
 +15    Class for handling the importing of NPZ-files as LabFloor items.
 +16    """
 +17
 +18    def scan(self, filename):
 +19        """
 +20        Scans a file to check if it is supported by the plugin
 +21
 +22        @param filename : The path to be scanned.
 +23        @type           : string
 +24
 +25        @return A list of LabFloorItems
 +26        """
 +27        # Setup a vector for the LabFloorItems that will be returned.
 +28        lab_floor_items = []
 +29
 +30        # Determine extension
 +31        basename = os.path.basename(filename)
 +32        no_extension_name, extension = os.path.splitext(basename)
 +33
 +34        # Return an empty list if the extension isn't ".npz"
 +35        if extension != '.npz':
 +36            return []
 +37
 +38        item = LabFloorItem(ElectronDensity,
 +39                            title='NPZ Electron Density',
 +40                            tool_tip='NPZ Electron Density')
 +41
 +42        # Add to the list of items.
 +43        lab_floor_items.append(item)
 +44
 +45        # Return the list of items.
 +46        return lab_floor_items
 +47
 +48    def load(self, filename):
 +49        """
 +50        Load the desired object in memory.
 +51
 +52        @param filename : The path of the NPZ-file.
 +53        @type           : string
 +54
 +55        @return Desired object (MoleculeConfiguration)
 +56        """
 +57
 +58        # Read the file
 +59        npz = numpy.load(filename)
 +60
 +61        # Create an "empty" ElectronDensity object.
 +62        electron_density = ElectronDensity.__new__(ElectronDensity)
 +63
 +64        # We will now fill out a dictionary that contains the information
 +65        # needed by the ElectronDensity class.
 +66        data = {}
 +67
 +68        # The "data" key is the electron density. The units must be given in the
 +69        # "data_unit" key. The array should have have the x-axis as the first
 +70        # dimension, the y-axis as the second, and the z-axis as the third.
 +71        data['data'] = npz['density']
 +72
 +73        # The data in "data" has no units so they are assigned here.
 +74        data['data_unit'] = 1.0 * Angstrom**-3
 +75
 +76        # Set the origin to be at zero.
 +77        data['origo'] = numpy.zeros(3)
 +78
 +79        # The cell must be given in Bohr units.
 +80        data['cell'] = npz['cell']
 +81
 +82        # The boundary conditions are expressed as a list of 6 numbers that should
 +83        # map to:
 +84        # { Dirichlet, Neumann, Periodic, Multipole };
 +85        # A value of 2 corresponds to "Periodic".
 +86        data['boundary_conditions'] = [2, 2, 2, 2, 2, 2]
 +87
 +88        # Construct the GridValues specific part of the object.
 +89        GridValues._populateFromDict(electron_density, data)
 +90
 +91        # Set the spin_type to unpolarized.
 +92        spin_type = NLEngine.UNPOLARIZED
 +93        electron_density._AnalysisSpin__spin_type = spin_type
 +94
 +95        sp = Spin.All
 +96        electron_density._AnalysisSpin__spin = sp
 +97        electron_density._setSupportedSpins(sp)
 +98
 +99        return electron_density
 +</code>
 +
 +这个附加组件的完整代码可在此处下载 [[https://docs.quantumwise.com/_downloads/NPZFilters.zip|↓ source code]]。
  
  
 ===== 如何安装附加组件 ===== ===== 如何安装附加组件 =====
  
 +有两种不同的安装附加组件的方法。第一种,设置环境变量 ''QUANTUM_ADDONS_PATH'' 到插件模块所在的目录。例如,在先前的章节中 NPZFilters 附件的路径为 ''$HOME/AddOns/NPZFilters'',然后设置环境变量为 ''QUANTUM_ADDONS_PATH=$HOME/AddOns'' 即可。
 +
 +另一种方法是压缩 Python 模块并通过图形界面安装。第一步是创建一个包含该模块的 zip 文件。按照上一段中的 NPZFilter 示例,可以通过运行 ''zip -r NPZFilters.zip $ HOME / AddOns / NPZFilters'' 来完成。然后,在 QuantumATK 中,//Help// 菜单下面有一个名为 //AddOn Manager// 的选项。打开 AddOn Manager 将会呈现出如下所示的窗口:
 +
 +
 +{{ :atk:addonmanager-20190730.png?700 |}}
 +
 +点击 //Local Install//,出现一个文件对话框。选择 ''NPZFilters.zip'',QuantumATK 将安装插件到 ''QUANTUM_ADDONS_PATH''
 ===== 测试 NPZ 滤波器附加组件 ===== ===== 测试 NPZ 滤波器附加组件 =====
  
 +按照上一节中的步骤操作后,现在应该已经安装上了 NPZFilters AddOn。您可以通过拉出 AddOn Manager 并查看列表中的 NPZFilters 再次确认已安装。如果我们在 QuantumATK 中创建一个新项目,且该文件夹包含我们创建的 ''electron_density.npz'' 文件,那么 ElectronDensity 对象就应该显示在 lab floor 上。
  
-===== 参考 ===== 
  
 +{{ :atk:npzlabfloor-20190730.png?900 |}}
 +
 +
 +点击右侧面板上的 //Viewer...// 按钮将电子密度可视化。在出现的对话框中,有 isosurface 和 cut plane 可供选择。选择 isosurface。默认的 isosurface 值是平均电荷密度,对于我们的电荷密度来说为零(因为一半是负,一半是正)。单击右侧面板中的 Properties ... 按钮,将 //Isovalue ...// 滑块拖动到接近 1 的地方。
 +
 +{{ :atk:isovalue-20190730.png?650 |}}
 +
 +
 +得到的等值面现在应该看起来像哑铃,两种不同的颜色分别代表负密度和正密度的区域,以及通过 x-z 轴的零密度平面。这证实了我们在模型密度函数中的读取结果正确。
 +
 +
 +{{ :atk:isosurface-20190730.png?650 |}}
 +
 +===== 参考 =====
  
  
 +  * 英文原文:https://docs.quantumwise.com/tutorials/addons/addons.html
  
  
atk/如何为_quantumatk_创建新的附加组件.1564484785.txt.gz · 最后更改: 2019/07/30 19:06 由 xie.congwei

© 2014-2022 费米科技(京ICP备14023855号