Changes between Initial Version and Version 1 of cypress/Example VTK-python


Ignore:
Timestamp:
08/20/15 19:30:52 (9 years ago)
Author:
fuji
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • cypress/Example VTK-python

    v1 v1  
     1{{{#!python
     2# -*- coding: utf-8 -*-
     3"""
     4Created on Sun Mar 23 11:17:34 2014
     5
     6@author: fuji
     7"""
     8
     9import vtk
     10import numpy as np
     11
     12# Strctured Grid
     13sGrid = vtk.vtkStructuredGrid()
     14
     15with 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#
     50writer = vtk.vtkXMLStructuredGridWriter()
     51writer.SetInput(sGrid)
     52
     53writer.SetFileName("officeFlow.vts")
     54writer.SetDataModeToAscii()
     55writer.Write()
     56}}}