| 1 | {{{#!python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | """ |
| 4 | Created on Sun Mar 23 11:17:34 2014 |
| 5 | |
| 6 | @author: fuji |
| 7 | """ |
| 8 | |
| 9 | import vtk |
| 10 | import numpy as np |
| 11 | |
| 12 | # Strctured Grid |
| 13 | sGrid = vtk.vtkStructuredGrid() |
| 14 | |
| 15 | with open('officeFlow.txt', 'r') as CFDfile: |
| 16 | nx, ny, nz = CFDfile.readline().split() |
| 17 | dims = [int(nx),int(ny),int(nz)] |
| 18 | |
| 19 | sGrid.SetDimensions(dims[0],dims[1],dims[2]) |
| 20 | nodes = vtk.vtkPoints() |
| 21 | nodes.SetNumberOfPoints(dims[0] * dims[1] * dims[2]) |
| 22 | |
| 23 | scalArray = vtk.vtkDoubleArray() |
| 24 | scalArray.SetNumberOfComponents(1) |
| 25 | scalArray.SetName("Pressure") |
| 26 | scalArray.SetNumberOfTuples(dims[0]*dims[1]*dims[2]) |
| 27 | |
| 28 | velArray = vtk.vtkDoubleArray() |
| 29 | velArray.SetNumberOfComponents(3) |
| 30 | velArray.SetName("Velocity") |
| 31 | velArray.SetNumberOfTuples(dims[0]*dims[1]*dims[2]) |
| 32 | |
| 33 | for k in range(dims[2]): |
| 34 | kOffset = k * dims[0] * dims[1] |
| 35 | for j in range(dims[1]): |
| 36 | jOffset = j * dims[0] |
| 37 | for i in range(dims[0]): |
| 38 | offset = i + jOffset + kOffset |
| 39 | x,y,z,u,v,w,p = CFDfile.readline().split() |
| 40 | nodes.SetPoint(offset,[float(x), float(y), float(z)]) |
| 41 | vel = np.array([float(u), float(v), float(w)]) |
| 42 | scalArray.SetValue(offset,float(p)) |
| 43 | velArray.SetTupleValue(offset,vel) |
| 44 | |
| 45 | sGrid.SetPoints(nodes) |
| 46 | sGrid.GetPointData().SetVectors(velArray) |
| 47 | sGrid.GetPointData().SetScalars(scalArray) |
| 48 | # |
| 49 | # |
| 50 | writer = vtk.vtkXMLStructuredGridWriter() |
| 51 | writer.SetInput(sGrid) |
| 52 | |
| 53 | writer.SetFileName("officeFlow.vts") |
| 54 | writer.SetDataModeToAscii() |
| 55 | writer.Write() |
| 56 | }}} |