Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Created May 11, 2015 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoguaro/cdb1eeb159bf74dfaeea to your computer and use it in GitHub Desktop.
Save nicoguaro/cdb1eeb159bf74dfaeea to your computer and use it in GitHub Desktop.
Plot a (finite element) mesh formed by 8 node (serendipity) elements.
from numpy import array
from matplotlib import pyplot as plt
def plot_msh(pts, els, shw_pts=True, shw_els=True,
pts_text=False, els_text=False):
"""
Plot a mesh of 8 nodes (serendipity) elements.
Parameters
----------
pts : (npts, 3) float array
numpy-array with the points, `npts` is the number of points.
els : (nels, 8) int array
numpy-array with the connectivity, `nels` is the number of elements.
shw_pts : (optional) boolean
Show the points of the mesh as circles.
shw_els : (optional) boolean
Show the edges of the elements as lines.
pts_text : (optional) boolean
Show the numbers for the nodes.
els_text : (optional) boolean
Show the numbers for the elements.
"""
els = els - 1
xmin = 1.1*min(pts[:,0])
xmax = 1.1*max(pts[:,0])
ymin = 1.1*min(pts[:,1])
ymax = 1.1*max(pts[:,1])
nels = els.shape[0]
for i in range(0, nels):
corx = array([pts[els[i, 0], 0], pts[els[i, 4], 0], pts[els[i, 1], 0],
pts[els[i, 5], 0], pts[els[i, 2], 0], pts[els[i, 6], 0],
pts[els[i, 3], 0], pts[els[i, 7], 0], pts[els[i, 0], 0]])
cory = array([pts[els[i, 0], 1], pts[els[i, 4], 1], pts[els[i, 1], 1],
pts[els[i, 5], 1], pts[els[i, 2], 1], pts[els[i, 6], 1],
pts[els[i, 3], 1], pts[els[i, 7], 1], pts[els[i, 0], 1]])
if shw_els == True:
plt.plot(corx, cory, 'k')
if els_text == True:
centroid = [sum(corx[0:8])/8.0, sum(cory[0:8])/8.0]
plt.text(centroid[0] - abs(xmax-xmin)/100.0, centroid[1] -
abs(ymax-ymin)/100.0, str(i+1),
bbox=dict(facecolor='red', alpha=0.5))
if shw_pts == True:
plt.plot(pts[:, 0], pts[:, 1], 'o')
plt.axis('equal')
plt.axis([xmin, xmax, ymin, ymax])
plt.axis('off')
npts = pts.shape[0]
if pts_text == True:
for i in range(0, npts):
plt.text(pts[i, 0] + abs(xmax-xmin)/100.0, pts[i, 1] +
abs(xmax-xmin)/100.0, str(i+1))
plt.show()
def ex_msh():
"""Return an example mesh for the function plt_msh
The mesh is for a plate with a hole.
"""
pts = array([
[ 1. , 0. ],
[ 1. , 1. ],
[ 0. , 1. ],
[-1. , 1. ],
[-1. , 0. ],
[-1. , -1. ],
[ 0. , -1. ],
[ 1. , -1. ],
[ 0.5 , 0. ],
[ 0. , 0.5 ],
[-0.5 , 0. ],
[ 0. , -0.5 ],
[ 0.35355, 0.35355],
[-0.35355, 0.35355],
[-0.35355, -0.35355],
[ 0.35355, -0.35355],
[ 1. , -0.35355],
[ 1. , 0.35355],
[ 0.35355, 1. ],
[-0.35355, 1. ],
[-1. , 0.35355],
[-1. , -0.35355],
[-0.35355, -1. ],
[ 0.35355, -1. ],
[-0.67678, -1. ],
[-0.83839, -1. ],
[-0.51517, -1. ],
[-0.17678, -1. ],
[ 0.17678, -1. ],
[ 0.67678, -1. ],
[ 0.51517, -1. ],
[ 0.83839, -1. ],
[ 1. , -0.67678],
[ 1. , -0.83839],
[ 1. , -0.51517],
[ 1. , -0.17678],
[ 1. , 0.17678],
[ 1. , 0.67678],
[ 1. , 0.51517],
[ 1. , 0.83839],
[ 0.67678, 1. ],
[ 0.83839, 1. ],
[ 0.51517, 1. ],
[ 0.17678, 1. ],
[-0.17678, 1. ],
[-0.67678, 1. ],
[-0.51517, 1. ],
[-0.83839, 1. ],
[-1. , 0.67678],
[-1. , 0.83839],
[-1. , 0.51517],
[-1. , 0.17678],
[-1. , -0.17678],
[-1. , -0.67678],
[-1. , -0.51517],
[-1. , -0.83839],
[ 0. , -0.75 ],
[ 0. , -0.875 ],
[ 0. , -0.625 ],
[ 0.75 , 0. ],
[ 0.875 , 0. ],
[ 0.625 , 0. ],
[ 0. , 0.75 ],
[ 0. , 0.875 ],
[ 0. , 0.625 ],
[-0.75 , 0. ],
[-0.875 , 0. ],
[-0.625 , 0. ],
[-0.67678, -0.35355],
[-0.83839, -0.35355],
[-0.51517, -0.35355],
[-0.35355, -0.67678],
[-0.35355, -0.83839],
[-0.35355, -0.51517],
[ 0.35355, -0.67678],
[ 0.35355, -0.83839],
[ 0.35355, -0.51517],
[ 0.67678, -0.35355],
[ 0.51517, -0.35355],
[ 0.83839, -0.35355],
[ 0.67678, 0.35355],
[ 0.51517, 0.35355],
[ 0.83839, 0.35355],
[ 0.35355, 0.67678],
[ 0.35355, 0.51517],
[ 0.35355, 0.83839],
[-0.67678, 0.35355],
[-0.83839, 0.35355],
[-0.51517, 0.35355],
[-0.35355, 0.67678],
[-0.35355, 0.51517],
[-0.35355, 0.83839],
[ 0.19134, -0.46194],
[ 0.46194, -0.19134],
[ 0.46194, 0.19134],
[ 0.19134, 0.46194],
[-0.19134, 0.46194],
[-0.46194, 0.19134],
[-0.46194, -0.19134],
[-0.19134, -0.46194],
[ 0.67678, -0.67678],
[ 0.51517, -0.67678],
[ 0.67678, -0.83839],
[ 0.83839, -0.67678],
[ 0.67678, -0.51517],
[ 0.71339, -0.17678],
[ 0.71339, 0.17678],
[ 0.67678, 0.67678],
[ 0.67678, 0.51517],
[ 0.51517, 0.67678],
[ 0.67678, 0.83839],
[ 0.83839, 0.67678],
[ 0.17678, 0.71339],
[-0.17678, 0.71339],
[-0.67678, 0.67678],
[-0.51517, 0.67678],
[-0.67678, 0.51517],
[-0.83839, 0.67678],
[-0.67678, 0.83839],
[-0.71339, 0.17678],
[-0.71339, -0.17678],
[-0.67678, -0.67678],
[-0.51517, -0.67678],
[-0.67678, -0.83839],
[-0.83839, -0.67678],
[-0.67678, -0.51517],
[-0.17678, -0.71339],
[ 0.17678, -0.71339]])
els = array([
[ 24, 75, 101, 30, 76, 102, 103, 31],
[ 30, 101, 33, 8, 103, 104, 34, 32],
[ 75, 16, 78, 101, 77, 79, 105, 102],
[101, 78, 17, 33, 105, 80, 35, 104],
[ 16, 78, 60, 9, 79, 106, 62, 94],
[ 78, 17, 1, 60, 80, 36, 61, 106],
[ 1, 60, 81, 18, 61, 107, 83, 37],
[ 60, 9, 13, 81, 62, 95, 82, 107],
[ 13, 81, 108, 84, 82, 109, 110, 85],
[ 84, 108, 41, 19, 110, 111, 43, 86],
[ 81, 18, 38, 108, 83, 39, 112, 109],
[108, 38, 2, 41, 112, 40, 42, 111],
[ 13, 84, 63, 10, 85, 113, 65, 96],
[ 84, 19, 3, 63, 86, 44, 64, 113],
[ 3, 63, 90, 20, 64, 114, 92, 45],
[ 63, 10, 14, 90, 65, 97, 91, 114],
[ 14, 90, 115, 87, 91, 116, 117, 89],
[ 87, 115, 49, 21, 117, 118, 51, 88],
[ 90, 20, 46, 115, 92, 47, 119, 116],
[115, 46, 4, 49, 119, 48, 50, 118],
[ 14, 11, 66, 87, 98, 68, 120, 89],
[ 87, 66, 5, 21, 120, 67, 52, 88],
[ 11, 15, 69, 66, 99, 71, 121, 68],
[ 66, 69, 22, 5, 121, 70, 53, 67],
[ 23, 72, 122, 25, 73, 123, 124, 27],
[ 25, 122, 54, 6, 124, 125, 56, 26],
[ 72, 15, 69, 122, 74, 71, 126, 123],
[122, 69, 22, 54, 126, 70, 55, 125],
[ 7, 57, 72, 23, 58, 127, 73, 28],
[ 57, 12, 15, 72, 59, 100, 74, 127],
[ 24, 75, 57, 7, 76, 128, 58, 29],
[ 75, 16, 12, 57, 77, 93, 59, 128]])
return pts, els
if __name__=="__main__":
pts, els = ex_msh()
plot_msh(pts, els)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment