Python:写入坐标numpy shap

2024-09-23 22:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

在python脚本中,我想将numpy形状的坐标写入文本文件。 我导入坐标和元素定义,然后使用numpy形状来调整坐标。在

然后我想写一个文本文件,在那里我写下调整后的坐标。在

但是,对于我当前的脚本,它只生成6个坐标。我想这是由于我对s = new_triangle.shape的定义(参见下面的脚本)

如何定义它,以便将所有新坐标写入输出文件?在

newcoords = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 1.0], [1.0, 1.0], [0.0, 2.0], [0.0, 2.0], [1.0, 1.0], [1.0, 2.0], [1.0, 1.0], [2.0, 1.0], [1.0, 2.0], [1.0, 2.0], [2.0, 1.0], [2.0, 2.0], [1.0, 1.0], [2.0, 0.0], [2.0, 1.0], [1.0, 0.0], [2.0, 0.0], [1.0, 1.0]]
newelems = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]

import numpy as np

#define triangles
triangles = np.array([[newcoords[e] for e in newelem] for newelem in newelems])

#find centroid of each triangle
CM = np.mean(triangles,axis=1)

#find vector from each point in triangle pointing towards centroid
point_to_CM_vectors = CM[:,np.newaxis] - triangles

#calculate similar triangles 1% smaller
new_triangle = triangles + 0.01*point_to_CM_vectors

#Define new coordinates
newcoord = []
newcoord.append(list(zip(*new_triangle)))
s = new_triangle.shape

print 'newcoords =', newcoords
print 'newcoord =', newcoord
print s

#generate output
fout = open('_PartInput4.inp','w')
fout.write('*Node-new_triangle\n')
for i, x in enumerate(new_triangle.reshape(s[1]*s[2], len(newelems))):
    fout.write("{}, {}, {}\n".format(i+1, x[0], x[1]))
fout.close()

提前感谢您的帮助!在


Tags: innumpy脚本newfor定义npcm
1条回答
网友
1楼 · 发布于 2024-09-23 22:21:13

Mr E之前给出了答案:

#generate output
with open('_PartInput3.inp','w') as fout:
    fout.write('*Node-new_triangle\n')
    s = new_triangle.shape
    for i, x in enumerate(new_triangle.reshape(s[0]*s[1], 2)):
        fout.write("{}, {}, {}\n".format(i+1, x[0], x[1]))

相关问题 更多 >