【问题标题】:calculating distance R from xyz data file从 xyz 数据文件计算距离 R
【发布时间】:2021-11-27 17:26:51
【问题描述】:

我正在尝试使用 .xyz 文件,并且已经打开并读取该文件。我需要为每行 xyz 数据计算 r(距离)(通过执行 r^2 = x^2+y^2+z^2)。但是,我未能将每个 xyz 值分隔在一行中,因为它将数据识别为单列 ex([-3.657420e-01 -1.434236e+00 1.086713e+00]) 是单行,理想情况下应该是由 x,y,z 值分隔。

我将脚本附加到打开/显示 xyz 文件和示例输出。谁能帮我计算每行数据的“r”?

谢谢

import numpy as np
filename = "lj-0200.xyz"
xyz_file = np.genfromtxt(fname=filename,skip_header=2,dtype='unicode')
xyz = open(filename)

atom_coordinates = (xyz_file[:,1:])
atom_coordinates = atom_coordinates.astype(float)
atom_quantity = int(xyz.readline())
title = xyz.readline()

print("Input filename is",filename, "Title is", title,"There are",atom_quantity,"atoms in this file")
print(atom_coordinates)

输出

Input filename is lj-0200.xyz Title is A Box of Atoms
 There are 200 atoms in this file
[[-3.657420e-01 -1.434236e+00  1.086713e+00]
 [ 1.758085e+00 -2.733505e+00 -4.553970e-01]
 [-2.777260e+00  8.659730e-01 -9.788090e-01]
 [-8.509290e-01  8.053680e-01 -1.050607e+00]
 [ 4.697280e-01 -1.262455e+00  1.770134e+00]
 [ 1.776954e+00  2.284919e+00  2.691217e+00]
 [ 1.564369e+00  2.100387e+00 -3.898060e-01]
 [ 2.153646e+00  1.233546e+00  1.145661e+00]
 [ 2.373994e+00  2.372510e+00  1.522204e+00]...

【问题讨论】:

    标签: python numpy math xyz


    【解决方案1】:

    阅读 Numpy 索引文档here

    r = np.linalg.norm(atom_coordinates, axis= 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2017-11-15
      • 2019-04-27
      • 2022-01-20
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多