python:调整导入的坐标

时间:2013-04-13 13:20:30

标签: python coordinates

我有一个python脚本,我在其中导入三角形元素的坐标,以及两个单独文本文件中的元素定义。我首先为每个triangluar元素定义唯一的节点,然后在三角形元素之间定义一个新的矩形元素(这个python脚本为FEM计算写入一个输入文件)。

坐标文件如下所示:

id,x,y,
  1,  0,   0
  2,  0,   1
  3,  0,   2
  4,  1,   0
  5,  1,   1
  6,  1,   2
  7,  2,   0
  8,  2,   1
  9,  2,   2

元素文件如下所示:

id, n1, n2, n3
 1, 1, 2, 4
 2, 1, 2, 5
 3, 2, 3, 5
 4, 3, 5, 6
 5, 5, 6, 8
 6, 6, 8, 9
 7, 5, 7, 8
 8, 4, 5, 7

我想要发生的是,三角形元素的节点的坐标被移动到三角形元素的质心上,这样三角形元素之间的矩形元素将获得一个物理厚度。

enter image description here

但是我的python脚本出错了(见下文)。 脚本中水平线之间的部分应该改变坐标。 但是对于我的脚本,添加了额外的坐标,而不是(这里的例子)9个初始坐标。 并且坐标也没有朝正确的方向移动。

为什么会发生这种情况,我该如何解决?

#!/usr/bin/env python

# Inlezen coordinaten
open("D://Documents//SkyDrive//afstuderen//99 EEM - Abaqus 6.11.2//scripting//_COORDINATEN.txt", "r")
import csv
import itertools

with open("_COORDINATEN.txt") as file:
    data = csv.reader(file)
    next(data)
    coords = []
    coords = ([[float(x) for x in line[1:]] for line in data])

#inlezen elementen
open("D://Documents//SkyDrive//afstuderen//99 EEM - Abaqus 6.11.2//scripting//_ELEMENTEN.txt", "r")
import csv
import itertools

with open("_ELEMENTEN.txt") as file:
    data2 = csv.reader(file)
    next(data2)
    elems = []
    elems = ([[int(x)-1 for x in line[1:]] for line in data2])


#Flip the original elements if required
for i,elem in enumerate(elems):
    ecoords = [coords[e] for e in elem]

    a = [x2-x1 for x1,x2 in zip(ecoords[0],ecoords[1])]
    b = [x2-x1 for x1,x2 in zip(ecoords[1],ecoords[2])]

    n = a[0]*b[1]-a[1]*b[0]

    if n < 0:
        elems[i] = [ elem[0], elem[2], elem[1] ]

#bewerking elementen
newcoords = []
newelems  = []
for elem in elems:
    ecoords = [coords[e] for e in elem]
    newelem = range( len(newcoords), len(newcoords)+len(ecoords) )

    newcoords += ecoords
    newelems.append( newelem )

cohelems = []
for e,elem in enumerate(elems):
  for edge in [[0,1],[1,2],[2,0]]:

    eedge = [elem[i] for i in edge]

    for e2,elem2 in enumerate(elems[e+1:]):

      e2 += e+1

      for edge2 in [[0,1],[1,2],[2,0]]:

        eedge2 = [elem2[i] for i in edge2]

        if all([i in eedge2 for i in eedge]):

          newedge  = [newelems[e][i] for i in edge ]
          newedge += [newelems[e2][i] for i in edge2]

          cohelems.append( newedge[-1::-1] )
#---------------------------------------------------------------------
def add_vectors(*points):
  new_x = 0.0
  new_y = 0.0
  for point in points:
    new_x += point[0]
    new_y += point[1]
  return [new_x, new_y]

def subtract_vectors(a, b):
  new_x = a[0] - b[0]
  new_y = a[1] - b[1]
  return [new_x, new_y]


def mul_by_scalar(vector, scalar):
  new_x = vector[0] * scalar
  new_y = vector[1] * scalar
  return [new_x, new_y]

new_triangles = []
for elem in elems:
    new_triangles += [coords[e] for e in elem]

print 'new_triangles =', new_triangles

CM = mul_by_scalar(add_vectors(*new_triangles), 1.0/3)

point_to_CM_vectors = []
for point in new_triangles:
  point_to_CM_vectors.append(subtract_vectors(CM, point))

new_triangle2 = []
#for e,elem in enumerate(elems):
for elem in elems:
    for point, motion in zip(new_triangles, point_to_CM_vectors):
      new_triangle2.append(add_vectors(point, mul_by_scalar(motion, 0.01)))
#      new_triangle2 += [add_vectors(point, mul_by_scalar(motion, 0.01))]

print 'new_triangle2 =', new_triangle2
#---------------------------------------------------------------------

先谢谢大家的帮助!

0 个答案:

没有答案