2013-04-17 87 views
0

在我的Python脚本中,我想写一个numpy形状的坐标到文本文件。 我导入坐标和元素定义,然后使用numpy形状来调整坐标。Python:写坐标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() 

在此先感谢您的帮助!

+1

看到我的回答你的问题早。 's [1] * s [2]'应该是[0] * s [1]' – YXD

+0

非常感谢!那确实有诀窍。 – user1967364

回答

0

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]))