简体   繁体   中英

writing plotOverline csv data from paraview for all time steps with python script

I am trying to extract the data (as csv ) from a line for all the time steps with the PlotOverLine filter in Paraview. In the GUI, I load the foam file, use the PlotOverLine filter and save the spread sheet view as csv file and click the next button in the animation panel to load the next time step and repeat the above for the remaining time steps at the same location of the line source.(since its transient data, I need data over all the time steps at a fixed location.) I used the following script.

try: paraview.simple    
except: from paraview.simple import *
paraview.simple._DisableFirstRenderCameraReset()

my_foam = FindSource("case.foam") #loading my case file
SetActiveSource(my_foam)
tsteps = my_foam.TimestepValues # trying to read all time step directories
for TimeStepNum in range(0,len(tsteps)): # the loop?
  view = GetActiveView()
  view.ViewTime = tsteps[TimeStepNum]
  Render()
  
  PlotOverLine1 = PlotOverLine( Source="High Resolution Line Source" )
  DataRepresentation7 = Show()

  PlotOverLine1.Source.Point1 = [-0.052, 0.0, 0.0] #my fixed location
  PlotOverLine1.Source.Point2 = [0.0, 0.0, 0.0]
  source = PlotOverLine1
  writer = CreateWriter("file_%d.csv" %(TimeStepNum), source)
  writer.FieldAssociation = "Points"
  writer.UpdatePipeline()
  Render()
  del writer

Say If I have 5 time steps, the script when run as a macro on Paraview, produces file_0 to file_5.csv however, file_1 to file_4 have 'nan' as data in them instead of actual values. Where as, the file_0 and file_5 have the values as they should be. I am a newbie don't know where I am going wrong! Not sure if the time step is getting updated before plotting the next line data. Any help would be appreciated! There should be an easier way to update timesteps and then use the same filter at the same location I guess.

I didn't try to see what happens with your script, but if you don't need to change anything between the time steps you can just set the pipeline once, and then add writer.WriteAllTimeSteps = 1 . The writer will update the time steps itself and save the results (you don't need anymore the for loop).

Anyway, have you made sure that it's not a problem with the data? If you update the time step to 2, and then from the GUI open a spreadsheet layout, is the expected data there or nan?

I finally managed to get it right. So, the problem with the previous script is though it was moving on to the next time step once the PlotOverLine was complete, it was trying to pick a line within the line. I just tweaked the way the time loop takes place by initially creating a fixed line and then looping over the remaining time steps (which is how it should be done!). The working script:

try: paraview.simple
except: from paraview.simple import *
paraview.simple._DisableFirstRenderCameraReset()

my_foam = FindSource("case.foam")
SetActiveSource(my_foam)

tsteps = my_foam.TimestepValues
PlotOverLine1 = PlotOverLine( Source="High Resolution Line Source" )
DataRepresentation7 = Show()

PlotOverLine1.Source.Point1 = [-0.052, 0.0, 0.0]
PlotOverLine1.Source.Point2 = [0.0, 0.0, 0.0]

source = PlotOverLine1

for TimeStepNum in range(0,len(tsteps)):
    view = GetActiveView()
    view.ViewTime = tsteps[TimeStepNum]
    Render()
    writer = CreateWriter("file_%d.csv" %(TimeStepNum), source)
    writer.FieldAssociation = "Points"
    writer.UpdatePipeline()
    Render()
    del writer

This works perfectly!

I wanted the output data as a single csv file and I wanted to get a single component of velocity "U". This needed a little more work to get the data in an array format so I include the code here in case it's useful to anyone

from paraview.simple import *
import csv

paraview.simple._DisableFirstRenderCameraReset()

my_foam = FindSource("airFoil2D.OpenFOAM")
SetActiveSource(my_foam)

tsteps = my_foam.TimestepValues
line = PlotOverLine( Source="High Resolution Line Source" )
DataRepresentation7 = Show()

line.Source.Point1 = [5, 4., 0.025]
line.Source.Point2 = [18., 4.0, 0.025]
line.Source.Resolution = 50
component = 0

with open('file.csv', 'w') as f:
    writer = csv.writer(f)
    for TimeStepNum in range(0,len(tsteps)):
        view = GetActiveView()
        view.ViewTime = tsteps[TimeStepNum]
        Render()
        fetchData = paraview.servermanager.Fetch(line)
        pointData = fetchData.GetPointData()
        fieldData = pointData.GetArray("U")
        U = [fieldData.GetComponent(i,component) for i in range(fieldData.GetSize()/3)]
        writer.writerow(U)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM