0

I have 3 1D arrays (node x-coordinates, node y-coordinates and Von-Mises stress scalar) exported from an FEA solver.

I want to create 2D contour plots as shown below in Python:

Stress plot example

I have managed to create such plot as shown below: Stress plot result

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri

for orient in ['top', 'bot', 'side']: 
    x = []
    y = []
    z = []
    stress = []
    with open('data.txt') as file:
        for line in file:
            cur_line = line.split('\t')
            cur_x_old = cur_line[0]
            cur_y_old = cur_line[1]
            cur_z_old = cur_line[2]
            cur_s_old = cur_line[3]
            
            if cur_x_old == 'X Location (mm)':
                pass
            else:
                cur_x = cur_x_old.replace(",",".")
                cur_y = cur_y_old.replace(",",".")
                cur_z = cur_z_old.replace(",",".")
                cur_s = cur_s_old.replace(",",".")
                
                x.append(float(cur_x))
                y.append(float(cur_y))
                z.append(float(cur_z))
                stress.append(float(cur_s))
                
    stress = np.array(stress)
    x = np.array(x)
    y = np.array(y)
    z = np.array(z)
    
    levels=np.linspace(stress.min(), stress.max(), num=100)
    triang = tri.Triangulation(x, y)
    
    if orient == 'side':
        plt.figure(figsize = (max(x)/50, abs(min(y))/50))
        plt.tricontourf(triang, stress, cmap = 'jet', norm = mpl.colors.Normalize(0, 100), levels = levels, extend = 'max')
        plt.scatter(x, y, color = 'k')
    else:
        plt.figure(figsize = (max(x)/50, max(z)*2/50))
        plt.tricontourf(x, z, stress, cmap = 'jet', norm = mpl.colors.Normalize(0, 100), levels = levels)

My problem is that by triangulating the data, unwanted triangles are generated at the edge of the mesh (see Stress plot result). The black dots are the scatter plot from x and y coordinates. I want the colour plot to be only inside the boundaries of the grid. Is there a way to remove these unwanted triangles?

5
  • Hi. Why are you using triangulation? You can simply use plt.scatter(x,y,c). If you really wanted to plot good stuff, tell us what FEA solver are you using? You can probably export vtk and plot using pyvista. These packages are tailored for such plot and they have evolved over years. Commented Aug 22, 2022 at 18:26
  • Hi. I want a smooth contour plot rather than small dots with colours everywhere. I am using ANSYS and I am exporting nodal x,y,z and stress values into a text file. Commented Aug 23, 2022 at 8:24
  • Ok. It is easy to export the VTK file if you know APDL. Otherwise Ansys is proprietary software so there is no direct way to do this. Commented Aug 23, 2022 at 8:30
  • The easiest way is I think using pyMAPDL. Or may also try this Commented Aug 23, 2022 at 8:32
  • or the native way which I used months ago was to export the output file, which contains the nodal coordinates and the mesh with solutions. You just need to write a complex python script to generate a vtk file. Otherwise it is better to switch to open source FEM solvers such as FeniCS. Commented Aug 23, 2022 at 8:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.