0

三角形要素の座標と要素定義を 2 つの別々のテキスト ファイルからインポートする Python スクリプトがあります。

座標ファイルは次のようになります。

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

スクリプトでは、三角形要素の 2 つのエッジが同じ場所にあるときに、新しい要素 (長方形) を定義します。最初に、三角形要素ごとに一意のノードを定義します (要素が同じノードを共有しないようにするため)。次に、角にある 4 つのノードによって新しい要素を定義します。下の画像を参照してください

ここに画像の説明を入力

これは問題なく機能しますが、新しく定義された要素の厚さはゼロです。そして、私はそれらに物理的な厚さを持たせたいと思っています。そのため、三角形要素のノードの座標を調整し、それらを要素の重心にわずかに移動したいと考えています。

三角形要素の重心を見つけて、ノードの座標を要素の重心の方向に水平距離で 0.001、垂直距離で 0.001 の値に変更するにはどうすればよいですか?

私が現在持っているスクリプトは次のとおりです。

    #!/usr/bin/env python


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


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

1 に答える 1

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]

これらを使用すると、残りはいくらか簡単になります。

triangle = [[0,0], [1,0], [1,1]]

# finding the center of mass:
#   CM = (1/3) * (a + b + c)
# CM:       position vector to the center of mass
# a, b, c:  position vectors to the corners

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

# For every point of the triangle, find a vector that points towards its CM.
# Scale the vectors to 10% (in this instance).

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

# Make a new triangle, contracted by 10%.

new_triangle = []
for point, motion in zip(triangle, point_to_CM_vectors):
  new_triangle.append(add_vectors(point, mul_by_scalar(motion, 0.10)))

add_vectorsの機能をインライン化subtract_vectorsmul_by_scalar、操作を「手動で」実行する方法はかなり簡単にわかりますが、常に同じことを繰り返すことになり、コードがわかりにくくなります。

于 2013-04-12T18:09:17.340 に答える