def curves_to_vtk(curves, filename): from pyevtk.hl import polyLinesToVTK x = np.concatenate([c.gamma()[:, 0] for c in curves]) y = np.concatenate([c.gamma()[:, 1] for c in curves]) z = np.concatenate([c.gamma()[:, 2] for c in curves]) ppl = np.asarray([c.gamma().shape[0] for c in curves]) data = np.concatenate([i*np.ones((curves[i].gamma().shape[0], )) for i in range(len(curves))]) polyLinesToVTK(filename, x, y, z, pointsPerLine=ppl, pointData={'idx': data})
def visCoil(self,output_coil_vtk_path,coil_csv_path,coil_position_path=None): # print('please check grid length is correct, Lx='+str(self.Lx)+',Ly='+str(self.Ly) + ',Lz='+str(self.Lz) ) x,y,z,pointsPerLine = self.convCsv2xyz(coil_csv_path) print(polyLinesToVTK(output_coil_vtk_path,x,y,z,pointsPerLine=pointsPerLine)) if coil_position_path is not None: move_arr,rot_arr = self.getAffineMatrix(coil_position_path) x1, y1, z1 = np.dot(rot_arr, np.array([x, y, z])) x1, y1, z1 = x1+(move_arr[0]), y1+(move_arr[1]), z1+(move_arr[2]) # x1, y1, z1 = np.dot(self.affineMat, np.array([x, y, z])) save_path = polyLinesToVTK(output_coil_vtk_path,x1,y1,z1,pointsPerLine=pointsPerLine) print('with position file, vtk file be overwritten in: '+ save_path)
def particles_to_vtk(res_tys, filename): """ Export particle tracing or field lines to a vtk file. Expects that the xyz positions can be obtained by ``xyz[:, 1:4]``. """ from pyevtk.hl import polyLinesToVTK x = np.concatenate([xyz[:, 1] for xyz in res_tys]) y = np.concatenate([xyz[:, 2] for xyz in res_tys]) z = np.concatenate([xyz[:, 3] for xyz in res_tys]) ppl = np.asarray([xyz.shape[0] for xyz in res_tys]) data = np.concatenate([i*np.ones((res_tys[i].shape[0], )) for i in range(len(res_tys))]) polyLinesToVTK(filename, x, y, z, pointsPerLine=ppl, pointData={'idx': data})
def toVTK(self, vtkname, **kwargs): """Write the coil as a VTK file Args: vtkname (string): VTK filename """ from pyevtk.hl import polyLinesToVTK kwargs.setdefault("cellData", {}) kwargs["cellData"].setdefault("I", np.array([self.I])) polyLinesToVTK(vtkname, np.array(self.x), np.array(self.y), np.array(self.z), np.array([len(self.x)]), **kwargs) return
# Second line x[4], y[4], z[4] = 0.0, 0.0, 3.0 x[5], y[5], z[5] = 1.0, 1.0, 3.0 x[6], y[6], z[6] = 2.0, 0.0, 3.0 # Connectivity of the lines pointsPerLine = np.zeros(2) pointsPerLine[0] = 4 pointsPerLine[1] = 3 # Some variables pressure = np.random.rand(npoints) temp = np.random.rand(npoints) vel = np.zeros(6) vel[0:3] = 1.0 vel[4:6] = 5.0 polyLinesToVTK( "./poly_lines", x, y, z, pointsPerLine=pointsPerLine, cellData={"vel": vel}, pointData={ "temp": temp, "pressure": pressure }, )
def toVTK(self, vtkname, line=True, height=0.1, width=0.1, **kwargs): """Write entire coil set into a VTK file Args: vtkname (str): VTK filename. line (bool, optional): Save coils as polylines or surfaces. Defaults to True. height (float, optional): Rectangle height when expanded to a finite cross-section. Defaults to 0.1. width (float, optional): Rectangle width when expanded to a finite cross-section. Defaults to 0.1. kwargs (dict): Optional kwargs passed to "polyLinesToVTK" or "meshio.Mesh.write". """ from pyevtk.hl import polyLinesToVTK, gridToVTK if line: currents = [] groups = [] x = [] y = [] z = [] lx = [] for icoil in list(self): currents.append(icoil.I) groups.append(icoil.group) x.append(icoil.x) y.append(icoil.y) z.append(icoil.z) lx.append(len(icoil.x)) kwargs.setdefault("cellData", {}) kwargs["cellData"].setdefault("I", np.array(currents)) kwargs["cellData"].setdefault("Igroup", np.array(groups)) polyLinesToVTK(vtkname, np.concatenate(x), np.concatenate(y), np.concatenate(z), np.array(lx), **kwargs) else: import meshio points = [] hedrs = [] currents = [] groups = [] nums = [] start = 0 for i, icoil in enumerate(self): # example of meshio.Mesh can be found at https://github.com/nschloe/meshio xx, yy, zz = icoil.rectangle(width=width, height=height) xx = np.ravel(np.transpose(xx[0:4, :])) yy = np.ravel(np.transpose(yy[0:4, :])) zz = np.ravel(np.transpose(zz[0:4, :])) xyz = np.transpose([xx, yy, zz]) points += xyz.tolist() # number of cells is npoints-1 ncell = len(xx) // 4 - 1 ind = np.reshape(np.arange(4 * ncell + 4) + start, (-1, 4)) hedr = [ list(ind[j, :]) + list(ind[j + 1, :]) for j in range(ncell) ] hedrs += hedr currents += (icoil.I * np.ones(ncell)).tolist() groups += (icoil.group * np.ones(ncell, dtype=int)).tolist() nums += ((i + 1) * np.ones(ncell, dtype=int)).tolist() # update point index number start += len(xx) kwargs.setdefault("cell_data", {}) # coil currents kwargs["cell_data"].setdefault("I", [currents]) # current groups kwargs["cell_data"].setdefault("group", [groups]) # coil index, starting from 1 kwargs["cell_data"].setdefault("index", [nums]) data = meshio.Mesh(points=points, cells=[("hexahedron", hedrs)], **kwargs) data.write(vtkname) return