2013-04-17 132 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]))