Python: how to build 2D discontinuous node-center data?

I have two-dimensional data and a two-dimensional quadriceps grid describing a domain divided into patches. Data is defined in each node grid. Data gaps exist at the patch boundaries, i.e. Data is propagated in the same place.

How can I use Python to build this data with linear interpolation between nodes and correctly represent discontinuous values ​​along each edge of the patch?

Below are three examples of elements or patches, each of which has six node values.

Figure of three example elements or patches, with six node values ​​each.

Node location and value data can be stored in the [Kx3x2] array, where K is the number of elements. For instance,

 x = np.array( [ [ [0.0, 1.0], [0.0, 1.0], [0.0, 1.0] ], #element 0 [ [1.0, 2.0], [1.0, 2.0], [1.0, 2.0] ], #element 1 [ [2.0, 3.0], [2.0, 3.0], [2.0, 3.0] ], #element 2 ] ) y = np.array( [ [ [0.0, 0.0], [0.5, 0.5], [1.0, 1.0] ], #element 0 [ [0.0, 1.0], [0.5, 1.5], [1.0, 2.0] ], #element 1 [ [1.0, 1.0], [1.5, 1.5], [2.0, 2.0] ], #element 2 ] ) z = np.array( [ [ [0.0, 0.5], [0.0, 0.8], [0.0, 1.0] ], #element 0 [ [0.3, 1.0], [0.6, 1.2], [0.8, 1.3] ], #element 1 [ [1.2, 1.5], [1.3, 1.4], [1.5, 1.7] ], #element 2 ] ) 

I reviewed pyplot.imshow() . It cannot view the entire domain at once and is still multi-valued intermittent nodes. This may work for calling imshow() separately for each patch. But how would I draw each patch image on the same axis? imshow() also problematic for non-rectangular patches, which is my common case.

I reviewed pyplot.pcolormesh() , but it seems to work exclusively with cell centered data.

+4
source share
1 answer

One option works by triangulating all the elements, and then using the matplotlib tripcolor() function that I opened. Two useful demos here and here .

Automatic triangulation of my global domain can be problematic, but Delaunay triangulation of a single quadrangle works very well: triangulation displayed for just the center element

I create a global triangulation by adding the triangulation of each element. This means that common nodes are actually duplicated in position arrays and value arrays. This allows intermittent data on the edges of elements. triangulation displayed for all elements

Linear interpolation and tearing can optionally be achieved using the tripcolor() function, supplying node locations and values ​​for each node. final solution pcolor

I was a little worried about how outline printing could work, since the edges of the elements are no longer logically connected. tricontour() still works. (shown here with overlapping triangulation) contour plot with triangulation overlaid

Reproduced by the following code:

 import numpy as np import matplotlib.pyplot as plt import matplotlib.tri as tri x = np.array( [ [ [0.0, 1.0], [0.0, 1.0], [0.0, 1.0] ], #element 0 [ [1.0, 2.0], [1.0, 2.0], [1.0, 2.0] ], #element 1 [ [2.0, 3.0], [2.0, 3.0], [2.0, 3.0] ], #element 2 ] ) y = np.array( [ [ [0.0, 0.0], [0.5, 0.5], [1.0, 1.0] ], #element 0 [ [0.0, 1.0], [0.5, 1.5], [1.0, 2.0] ], #element 1 [ [1.0, 1.0], [1.5, 1.5], [2.0, 2.0] ], #element 2 ] ) z = np.array( [ [ [0.0, 0.5], [0.0, 0.8], [0.0, 1.0] ], #element 0 [ [0.3, 1.0], [0.6, 1.2], [0.8, 1.3] ], #element 1 [ [1.2, 1.5], [1.3, 1.4], [1.5, 1.7] ], #element 2 ] ) global_num_pts = z.size global_x = np.zeros( global_num_pts ) global_y = np.zeros( global_num_pts ) global_z = np.zeros( global_num_pts ) global_triang_list = list() offset = 0; num_triangles = 0; #process triangulation element-by-element for k in range(z.shape[0]): points_x = x[k,...].flatten() points_y = y[k,...].flatten() z_element = z[k,...].flatten() num_points_this_element = points_x.size #auto-generate Delauny triangulation for the element, which should be flawless due to quadrilateral element shape triang = tri.Triangulation(points_x, points_y) global_triang_list.append( triang.triangles + offset ) #offseting triangle indices by start index of this element #store results for this element in global triangulation arrays global_x[offset:(offset+num_points_this_element)] = points_x global_y[offset:(offset+num_points_this_element)] = points_y global_z[offset:(offset+num_points_this_element)] = z_element num_triangles += triang.triangles.shape[0] offset += num_points_this_element #go back and turn all of the triangle indices into one global triangle array offset = 0 global_triang = np.zeros( (num_triangles, 3) ) for t in global_triang_list: global_triang[ offset:(offset+t.shape[0] )] = t offset += t.shape[0] plt.figure() plt.gca().set_aspect('equal') plt.tripcolor(global_x, global_y, global_triang, global_z, shading='gouraud' ) #plt.tricontour(global_x, global_y, global_triang, global_z ) #plt.triplot(global_x, global_y, global_triang, 'go-') #plot just the triangle mesh plt.xlim((-0.25, 3.25)) plt.ylim((-0.25, 2.25)) plt.show() 
+5
source

All Articles