tAdded VTK output function - ns2dfd - 2D finite difference Navier Stokes solver for fluid dynamics
(HTM) git clone git://src.adamsgaard.dk/ns2dfd
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit a9c5e210c85110662bd2c31c3fd5d3c3c8c12291
(DIR) parent 502702ca8b1d45e5023f4e1f5fa5b5408a614ed4
(HTM) Author: Anders Damsgaard <anders.damsgaard@geo.au.dk>
Date: Tue, 4 Mar 2014 13:38:07 +0100
Added VTK output function
Diffstat:
M ns2dfd.py | 77 +++++++++++++++++++++++++++++++
M pressure_anomaly.py | 1 +
2 files changed, 78 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/ns2dfd.py b/ns2dfd.py
t@@ -2,6 +2,7 @@
import numpy
import subprocess
+import vtk
class fluid:
t@@ -264,3 +265,79 @@ class fluid:
'''
self.write()
subprocess.call('./ns2dfd ' + self.sim_id + '.dat', shell=True)
+
+ def writeVTK(self, folder = './', verbose = True):
+ '''
+ Writes a VTK file for the fluid grid to the current folder by default.
+ The file name will be in the format ``<self.sid>.vti``. The vti files
+ can be used for visualizing the fluid in ParaView.
+
+ The fluid grid is visualized by opening the vti files, and pressing
+ "Apply" to import all fluid field properties. To visualize the scalar
+ fields, such as the pressure, the porosity, the porosity change or the
+ velocity magnitude, choose "Surface" or "Surface With Edges" as the
+ "Representation". Choose the desired property as the "Coloring" field.
+ It may be desirable to show the color bar by pressing the "Show" button,
+ and "Rescale" to fit the color range limits to the current file. The
+ coordinate system can be displayed by checking the "Show Axis" field.
+ All adjustments by default require the "Apply" button to be pressed
+ before regenerating the view.
+
+ The fluid vector fields (e.g. the fluid velocity) can be visualizing by
+ e.g. arrows. To do this, select the fluid data in the "Pipeline
+ Browser". Press "Glyph" from the "Common" toolbar, or go to the
+ "Filters" mennu, and press "Glyph" from the "Common" list. Make sure
+ that "Arrow" is selected as the "Glyph type", and "Velocity" as the
+ "Vectors" value. Adjust the "Maximum Number of Points" to be at least as
+ big as the number of fluid cells in the grid. Press "Apply" to visualize
+ the arrows.
+
+ If several data files are generated for the same simulation (e.g. using
+ the :func:`writeVTKall()` function), it is able to step the
+ visualization through time by using the ParaView controls.
+
+ :param folder: The folder where to place the output binary file (default
+ (default = './')
+ :type folder: str
+ :param verbose: Show diagnostic information (default = True)
+ :type verbose: bool
+ '''
+ filename = folder + '/' + self.sim_id + '.vti' # image grid
+
+ # initalize VTK data structure
+ grid = vtk.vtkImageData()
+ grid.SetOrigin([0.0, 0.0, 0.0])
+ grid.SetSpacing([self.dx, self.dy, 1])
+ grid.SetDimensions([self.nx+2, self.ny+2, 1])
+
+ # array of scalars: hydraulic pressures
+ pres = vtk.vtkDoubleArray()
+ pres.SetName("Pressure")
+ pres.SetNumberOfComponents(1)
+ pres.SetNumberOfTuples(grid.GetNumberOfPoints())
+
+ # array of vectors: hydraulic velocities
+ vel = vtk.vtkDoubleArray()
+ vel.SetName("Velocity")
+ vel.SetNumberOfComponents(2)
+ vel.SetNumberOfTuples(grid.GetNumberOfPoints())
+
+ # insert values
+ for y in range(self.ny+2):
+ for x in range(self.nx+2):
+ idx = x + (self.nx+2)*y
+ pres.SetValue(idx, self.P[x,y])
+ vel.SetTuple(idx, [self.U[x,y], self.V[x,y]])
+
+ # add pres array to grid
+ grid.GetPointData().AddArray(pres)
+ grid.GetPointData().AddArray(vel)
+
+ # write VTK XML image data file
+ writer = vtk.vtkXMLImageDataWriter()
+ writer.SetFileName(filename)
+ writer.SetInput(grid)
+ writer.Update()
+ if (verbose == True):
+ print('Output file: {0}'.format(filename))
+
(DIR) diff --git a/pressure_anomaly.py b/pressure_anomaly.py
t@@ -6,3 +6,4 @@ sim.P[5,3] = 1.0
sim.run()
sim.read(sim.sim_id + '00004.dat')
print(sim.P)
+sim.writeVTK()