簡體   English   中英

如何在 3D 桁架上顯示位移和節點編號?

[英]How can I display displacement and node numbers on a 3D truss?

我試圖在 3D 桁架示例上顯示位移但是我遇到了一個錯誤。我在下面簡化了我的代碼。我能夠在二維問題上顯示位移但是我無法解決 3D 問題。我也在嘗試顯示每個節點的節點號。我設法放置節點(綠色)但是即使在我使用“plt.annotate”命令后數字也沒有顯示。有人可以幫我顯示位移和節點號嗎?謝謝你提前。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys
np.set_printoptions(threshold=sys.maxsize)
def plot_truss(nodes, elements, areas,forces):
    # plot nodes in 3d 
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x = [i[0] for i in nodes.values()]
    y = [i[1] for i in nodes.values()]
    z = [i[2] for i in nodes.values()]
    # size = 400
    # ax.scatter(x, y, z, c='r', marker='o', s=size, zorder=5)
    size = 400
    offset = size / 4000
    ax.scatter(x, y, z, c='y', s=size, zorder=5)
    for i, location in enumerate(zip(x, y, z)):
        plt.annotate(i + 1, (location[0] - offset, location[1] - offset), zorder=10)
    # plot elements in 3d 
    for element in elements:
        fromPoint = np.array(nodes[elements[element][0]])
        toPoint = np.array(nodes[elements[element][1]])
        x1 = fromPoint[0]
        y1 = fromPoint[1]
        z1 = fromPoint[2]
        x2 = toPoint[0]
        y2 = toPoint[1]
        z2 = toPoint[2]
        ax.plot([x1, x2], [y1, y2], zs=[z1, z2], c='b', linestyle='-', linewidth=5*areas[element], zorder=1)
nodes = {1: [0, 10, 0], 2: [0, 0, 0], 3: [10, 5, 0], 4: [0, 10, 10]}
areas = {1: 1.0, 2: 2.0, 3: 2.0}
elements = {1: [1, 3], 2: [2, 3], 3: [4, 3]}
forces = {1: [0, 0, 0], 2: [0, 0, 0], 3: [0, -200, 0], 4: [0, 0, 0]}
disps = {1: [0, 0, 0], 2: [0, 0, 0], 3: [ 3, -2,  4], 4: [0, 0, 0]}
def plt_displacement(nodes,elements,disps color="red"):
    nodes_disp = np.copy(nodes)
    nodes_disp[:, 0] += disp[::2, 0]
    nodes_disp[:, 1] += disp[1::2, 0]
    plt.scatter(nodes_disp[:, 0], nodes_disp[:, 1], color=color)
    for e in elements:
        x_tmp = [nodes_disp[e[0], 0], nodes_disp[e[1], 0]]
        y_tmp = [nodes_disp[e[0], 1], nodes_disp[e[1], 1]]
        plt.plot(x_tmp, y_tmp, color=color)
plt_displacement(nodes,elements,disps)       
plot_truss(nodes, elements, areas, forces)
plt.show()

當我運行代碼時,出現以下錯誤;

<ipython-input-47-758895b259be> in plt_displacement(elements, nodes, disp, color)
     31 def plt_displacement(elements, nodes, disp, color="red"):
     32     nodes_disp = np.copy(nodes)
---> 33     nodes_disp[:, 0] += disp[::2, 0]
     34     nodes_disp[:, 1] += disp[1::2, 0]
     35     plt.scatter(nodes_disp[:, 0], nodes_disp[:, 1], color=color)

IndexError: too many indices for array

看起來您可能在調用 plt_displacement() (第 3 行和第 12 行到最后一行)與您的定義之間切換了“節點”和“元素”。

plt_displacement(nodes,elements,disps)


def plt_displacement(elements, nodes, disp, color="red"):

我不確定 plt_displacement 到底應該做什么。 但是看看 nodes_disp 它是一個沒有形狀的數組,所以切片是行不通的。

>>> nodes_disp = np.copy(nodes)
>>> nodes_disp
array({1: [0, 10, 0], 2: [0, 0, 0], 3: [10, 5, 0], 4: [0, 10, 10]}, dtype=object)

>>> nodes_disp.shape
()

您可以將值更改為數組並將其切片,如下所示:

>>> npdisp = np.copy(list(disps.values()))

>>> nodes_disp
array([[ 0, 10,  0],
       [ 0,  0,  0],
       [10,  5,  0],
       [ 0, 10, 10]])

但我不確定這是否是你的意圖。 同樣明智的是,您必須將 disp 的類型更改為數組才能對其進行切片,因為它是字典

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM