Reputation: 23
i am struggling to plot streamlines with pyvista, and I will appreciate your help... I have a dataframe with: 'x','y','z','volt','I','Ix','Iy','Iz','resistivity','domain'. x, y, and z are the coodinates of centers of the grid elements.
First, I plot the mesh to show the different material properties:
'''
df = pd.read_csv('file.csv')
# create vector
vectors = df[["Ix", "Iy", "Iz"]].to_numpy()
# find corrdinate
x_coord = df['x'].unique()
y_coord = df['y'].unique()
z_coord = df['z'].unique()
# create grid
x,y,z = np.meshgrid(X_coord, Y_coord,Z_coord, indexing='xy')
grid = pyvista.StructuredGrid(x,y,z)
# grid.plot(show_edges=True)
grid.cell_data['domain'] = domaine
grid.cell_data['volt'] = volt
grid.cell_data['I'] = u
grid.cell_data['Ix'] = ux
grid.cell_data['Iy'] = uy
grid.cell_data['Iz'] = uz
grid.cell_data['resistivity'] = resistivity
#plot results
cmap = ["purple", "green", "red","red","orange","orange",'purple','purple','yellow','yellow','c','c']
grid.plot(scalars= domaine, show_edges=True,cmap=cmap)
'''
here is the result:
[![enter image description here][1]][1]
then I tried to plot the streamline:
'''
stream, src = grid.streamlines('vectors', return_source=True, max_steps=20000,
n_points=200, source_radius=25, source_center=(15, 0, 30))
pyvista.set_plot_theme("document")
p = pyvista.Plotter(window_size=(800, 600))
p.add_mesh(grid, color="k")
p.add_mesh(stream.tube(radius=0.1))
p.show()
'''
And I have this: ValueError: Empty meshes cannot be plotted. Input mesh has zero points.
Finally when i look at grid properties I have:
[![enter image description here][2]][2]
Does anybody know where is the issue(s)?
Thanks!
Pierre
here is the full code
# import relevant library
import matplotlib.pyplot as plt
import pyvista
import vtk
import pandas as pd
import numpy as np
# Opening the file with absolute path
file = open(r'meshmaker.txt')
# create list
x_spacings = []
y_spacings = []
z_spacings = []
# min depth of the model
min_depth = 0
# select all the x values, they are string!
x_flag = False
y_flag = False
z_flag = False
for line in file:
# select x spacing
if line.startswith("NX"):
x_flag=True
elif line.startswith('NY'):
x_flag=False
elif x_flag:
x1 = line[0:11].strip('\n ')
x2 = line[11:21].strip('\n ')
x3 = line[21:31].strip('\n ')
x4 = line[31:41].strip('\n ')
x5 = line[41:51].strip('\n ')
x6 = line[51:61].strip('\n ')
x7 = line[61:71].strip('\n ')
x8 = line[71:80].strip('\n ')
x_spacings.extend([x1,x2,x3,x4,x5,x6,x7,x8])
# select y spacing
if line.startswith("NY"):
y_flag=True
elif line.startswith('NZ'):
y_flag=False
elif y_flag:
y1 = line[0:11].strip('\n ')
y2 = line[11:21].strip('\n ')
y3 = line[21:31].strip('\n ')
y4 = line[31:41].strip('\n ')
y5 = line[41:51].strip('\n ')
y6 = line[51:61].strip('\n ')
y7 = line[61:71].strip('\n ')
y8 = line[71:80].strip('\n ')
y_spacings.extend([y1,y2,y3,y4,y5,y6,y7,y8])
# select z spacing
if line.startswith("NZ"):
z_flag=True
elif line.startswith('ENDCY'):
z_flag=False
elif z_flag:
z1 = line[0:11].strip('\n ')
z2 = line[11:21].strip('\n ')
z3 = line[21:31].strip('\n ')
z4 = line[31:41].strip('\n ')
z5 = line[41:51].strip('\n ')
z6 = line[51:61].strip('\n ')
z7 = line[61:71].strip('\n ')
z8 = line[71:80].strip('\n ')
z_spacings.extend([z1,z2,z3,z4,z5,z6,z7,z8])
# remove empty element from list
x_spacings = list(filter(None, x_spacings))
y_spacings = list(filter(None, y_spacings))
z_spacings = list(filter(None, z_spacings))
# convert all string to number
x_spacings = [float(x) for x in x_spacings]
y_spacings = [float(x) for x in y_spacings]
z_spacings = [float(x) for x in z_spacings]
# add orgine
x_spacings.insert(0,0)
y_spacings.insert(0,0)
# z_spacings = [x*-1 for x in z_spacings]
z_spacings.insert(0,0)
# print(x_spacing)
# print(y_spacing)
# print(z_spacing)
def Center_grid(lists):
center_list = []
length = len(lists)
for i in range(0, length):
center = sum(lists[0:i])+lists[i]/2
center_list.append(center)
return center_list
# calculate coordinates
X_coord = np.round(Center_grid(x_spacing),3)
Y_coord = np.round(Center_grid(y_spacing),3)
Z_coord = np.round(Center_grid(z_spacing),3)
df = pd.read_csv('df_resis_s2.csv')
df = df.drop(df.columns[0],axis=1)
df.loc[df["I"] <= 1, "Ix"] = 0
df.loc[df["I"] <= 1, "Iy"] = 0
df.loc[df["I"] <= 1, "Iz"] = 0
x_coord_stream = df['x'].unique()
y_coord_stream = df['y'].unique()
z_coord_stream = df['z'].unique()
vectors = df[["Ix", "Iy", "Iz"]].to_numpy()
df = df.sort_values(by = ['z','x'],ascending =[False,True])
domaine = df['domain'].tolist()
volt = df['volt'].tolist()
u = df['I'].tolist()
ux = df['Ix'].tolist()
uy = df['Iy'].tolist()
uz = df['Iz'].tolist()
resistivity = df['resistivity'].tolist()
nb_domain = len(df['domain'].unique())
print(nb_domain)
x,y,z = np.meshgrid(X_coord, Y_coord,Z_coord, indexing='xy')
grid = pyvista.StructuredGrid(x,y,z)
# grid.plot(show_edges=True)
grid.cell_data['domain'] = domaine
grid.cell_data['volt'] = volt
grid.cell_data['I'] = u
grid.cell_data['Ix'] = ux
grid.cell_data['Iy'] = uy
grid.cell_data['Iz'] = uz
grid.cell_data['resistivity'] = resistivity
grid.cell_data['vectors'] = vectors
# # save it into an exodus file
# pyvista.save_meshio('test5.exo',grid)
# save it into a vtk file
grid.save('test_well_design.vtk')
#plot results
cmap = ["purple", "green", "red", "red","orange","orange",'purple','purple','yellow','yellow','c','c']
grid.plot(scalars= domaine, show_edges=True,cmap=cmap)
stream, src = grid.streamlines('vectors', return_source=True, max_steps=20000,
n_points=200, source_radius=25, source_center=(15, 0, 30))
pyvista.set_plot_theme("document")
p = pyvista.Plotter(window_size=(800, 600))
p.add_mesh(grid, color="k")
p.add_mesh(stream.tube(radius=0.1))
p.show()
meshmaker.txt
Upvotes: 1
Views: 595