0

在我的 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()

提前感谢您的帮助!

4

1 回答 1

0

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]))
于 2013-04-17T12:13:52.240 回答