如何使用matplotlib绘制二维FEM结果? [英] How can I plot 2d FEM results using matplotlib?

查看:123
本文介绍了如何使用matplotlib绘制二维FEM结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发2D平面有限元工具.功能之一是能够可视化特定对象上的应力.

I'm developing a 2D plane finite element tool. One of the features is the ability to visualize the stresses on a particular object.

此工具使用以下数据创建四边形网格:

This tool creates a quadrilateral mesh using the following data:

  • 节点:网格中每个节点的numpy数组[[x1 y1], [x2 y2], etc]-> xy坐标

  • nodes: numpy array [[x1 y1], [x2 y2], etc] -> x and y coordinates of every node in the mesh

元素:numpy数组[[1 2 3 4], [2 3 5 6]]->数组的每一行对应于网格中一个特定元素的4个点.

elements: numpy array [[1 2 3 4], [2 3 5 6]] -> every line of the array corresponds to the 4 points of one particular element of the mesh.

我能够实现绘制网格的方法:

I was able to implement a method that plots the mesh:

import matplotlib.pyplot as plt
import matplotlib.collections
import matplotlib.cm as cm

import numpy as np


def showMeshPlot(nodes, elements):

    y = nodes[:,0]
    z = nodes[:,1]

    #https://stackoverflow.com/questions/49640311/matplotlib-unstructered-quadrilaterals-instead-of-triangles
    def quatplot(y,z, quatrangles, ax=None, **kwargs):

        if not ax: ax=plt.gca()
        yz = np.c_[y,z]
        verts= yz[quatrangles]
        pc = matplotlib.collections.PolyCollection(verts, **kwargs)
        ax.add_collection(pc)
        ax.autoscale()

    plt.figure()
    plt.gca().set_aspect('equal')

    quatplot(y,z, np.asarray(elements), ax=None, color="crimson", facecolor="None")
    if nodes:            
        plt.plot(y,z, marker="o", ls="", color="crimson")

    plt.title('This is the plot for: quad')
    plt.xlabel('Y Axis')
    plt.ylabel('Z Axis')


    plt.show()

nodes = np.array([[0,0], [0,0.5],[0,1],[0.5,0], [0.5,0.5], [0.5,1], [1,0], 
                  [1,0.5],[1,1]])
elements = np.array([[0,3,4,1],[1,4,5,2],[3,6,7,4],[4,7,8,5]])
stresses = np.array([1,2,3,4])

showMeshPlot(nodes, elements)

哪个产生这样的情节:

现在,我有一个一维数组,对象上的应力与元素数组的长度相同.

Now, i have an 1D array with the stresses on the object, with the same length as the elements array.

我的问题是如何使用matplotlib可视化那些应力(使用标尺)?我调查了pcolormesh,但是我不明白它如何处理我的数据.这是我要达到的目标的一个示例(对robbievanleeuwen的积分):

My question is how can i visualize those stresses (with a scalar bar) using matplotlib? I looked into pcolormesh, but i couldn't understand how it could work with my data. Here's an example of what i want to achieve (credits to robbievanleeuwen):

注意:我无法复制上面的示例,因为他使用了三角形网格而不是四边形.

Note: I couldn't replicate the above example because he uses a triangular mesh instead of quads.

提前谢谢!

推荐答案

PolyCollection是ScalarMappable.它可以具有一个值数组,一个颜色图和一个规范化集.在这里,您可以将stresses数组提供给PolyCollection并选择一些要使用的颜色图. 其余的都对该功能进行了一些重新调整,以便可以将其他数据作为输入并创建一个颜色栏.

A PolyCollection is a ScalarMappable. It can have an array of values, a colormap and a normalization set. Here you would supply the stresses array to the PolyCollection and choose some colormap to use. The rest is rearanging the function a bit, such that can take the additional data as input and creates a colorbar.

import matplotlib.pyplot as plt
import matplotlib.collections
import numpy as np


def showMeshPlot(nodes, elements, values):

    y = nodes[:,0]
    z = nodes[:,1]

    def quatplot(y,z, quatrangles, values, ax=None, **kwargs):

        if not ax: ax=plt.gca()
        yz = np.c_[y,z]
        verts= yz[quatrangles]
        pc = matplotlib.collections.PolyCollection(verts, **kwargs)
        pc.set_array(values)
        ax.add_collection(pc)
        ax.autoscale()
        return pc

    fig, ax = plt.subplots()
    ax.set_aspect('equal')

    pc = quatplot(y,z, np.asarray(elements), values, ax=ax, 
             edgecolor="crimson", cmap="rainbow")
    fig.colorbar(pc, ax=ax)        
    ax.plot(y,z, marker="o", ls="", color="crimson")

    ax.set(title='This is the plot for: quad', xlabel='Y Axis', ylabel='Z Axis')

    plt.show()

nodes = np.array([[0,0], [0,0.5],[0,1],[0.5,0], [0.5,0.5], [0.5,1], [1,0], 
                  [1,0.5],[1,1]])
elements = np.array([[0,3,4,1],[1,4,5,2],[3,6,7,4],[4,7,8,5]])
stresses = np.array([1,2,3,4])

showMeshPlot(nodes, elements, stresses)

这篇关于如何使用matplotlib绘制二维FEM结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆