Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- from paraview.util.vtkAlgorithm import *
- import numpy as np
- import vtk
- from vtkmodules.numpy_interface import algorithms as algs
- from vtkmodules.vtkCommonDataModel import vtkPolyData
- from vtkmodules.util.vtkAlgorithm import VTKPythonAlgorithmBase
- from vtkmodules.numpy_interface import dataset_adapter as dsa
- from vtkmodules.vtkCommonTransforms import vtkTransform
- from vtkmodules.vtkFiltersGeneral import vtkTransformFilter
- import random
- from vtkmodules.vtkFiltersCore import vtkAppendPolyData
- from vtkmodules import vtkCommonCore
- import sys
- @smproxy.filter(label="New Pathlines")
- @smproperty.input(name="Input")
- @smproperty.xml("""
- <OutputPort index="0" name='Transform_Z_0.01'/>
- <OutputPort index="1" name='Landmass'/>
- <OutputPort index="2" name='Water'/>
- <OutputPort index="3" name='Velocity_Field'/>
- <OutputPort index="4" name='Seed'/>
- <OutputPort index="5" name='ParticleTracer' />
- <OutputPort index="6" name='Pathlines' />
- <Hints>
- <ShowInMenu category="pyParaOcean Filters"/>
- </Hints>
- """)
- class Pathlines(VTKPythonAlgorithmBase):
- # the rest of the code here is unchanged.
- def __init__(self):
- print("---__init__ Called---")
- from vtkmodules.vtkCommonExecutionModel import vtkTrivialProducer
- self.z1 = 70
- self.z2 = 72
- self.salinity_variable_name = "so"
- self.temperature_variable_name = "thetao"
- #variables set through UI elements
- self.inputVectorStr= "" # default value of the input vector field for vector weighted seeding
- self.inputScalarStr= "" # default value of scalar
- self.seedingStrategy = 1 # default value of seeding strategy
- self.numSeeds = 300 # default number of seeds
- self.calculated = False
- self.start_time_set = False
- self.particle_tracer = vtk.vtkParticleTracer()
- self.particle_tracer.SetStaticSeeds(False)
- self.particle_tracer.SetIgnorePipelineTime(False)
- self.seeds = vtkPolyData()
- self.seed_producer = vtkTrivialProducer()
- self.seeded = False
- super().__init__(nInputPorts = 1, nOutputPorts = 7)
- # textbox to enter flow field vector
- @smproperty.stringvector(name ="Enter Flow Vector Components (comma separated)",
- default_values='uo,vo')
- def SetVectorField(self, name):
- self.inputVectorStr = name.split(",")
- self.Modified()
- # dropdown to select seeding strategy
- @smproperty.xml("""
- <IntVectorProperty
- name="Seeding Strategy"
- command="SetSeedingStrategy"
- number_of_elements="1"
- default_values="1">
- <EnumerationDomain name="enum">
- <Entry value="1" text="Uniform"/>
- <Entry value="2" text="Chosen Scalar's Magnitude"/>
- <Entry value="3" text="Curl"/>
- <Entry value="4" text="Vorticity"/>
- <Entry value="5" text="Okubo-Weiss"/>
- <Entry value="6" text="Flow Magnitude"/>
- </EnumerationDomain>
- <Documentation>
- This property indicates which seeding strategy will be used.
- </Documentation>
- </IntVectorProperty>
- """)
- def SetSeedingStrategy(self, n):
- self.seedingStrategy = n
- self.Modified()
- def get_slice(self, pdi, depth):
- plane = vtk.vtkPlane()
- plane.SetOrigin(0, 0, depth)
- plane.SetNormal(0, 0, 1)
- cutter = vtk.vtkCutter()
- cutter.SetCutFunction(plane)
- cutter.SetInputData(pdi)
- cutter.Update()
- return cutter
- @smproperty.stringvector(name = "Salinity Variable Name", default_values = "so")
- def SetSalinityName(self, x):
- self.salinity_variable_name = x
- self.Modified()
- @smproperty.stringvector(name = "Temperature Variable Name", default_values = "thetao")
- def SetTemperatureName(self, x):
- self.temperature_variable_name = x
- self.Modified()
- def Tranform_z(self, data):
- t = vtkTransform()
- t.Scale(1, 1, 0.01)
- tf = vtkTransformFilter()
- tf.SetTransform(t)
- tf.SetInputData(data)
- tf.Update()
- return tf
- # textbox to enter number of seeding points
- @smproperty.intvector(name ="Number of seeds", default_values=5000)
- def SetNumSeeds(self, n):
- self.numSeeds = n
- self.Modified()
- #TODO: deprecate this helper function
- def point_generate(self,input0,id,n):
- ip = vtkPolyData()
- ps1 = vtk.vtkPointSource()
- pt = input0.GetPoint(int(id))
- ps1.SetCenter(pt)
- ps1.SetNumberOfPoints(n)
- ps1.SetRadius(0.4)
- ps1.Update()
- ip.ShallowCopy(ps1.GetOutput())
- return ip
- def FillOutputPortInformation(self, port, info):
- print("---FillOutputPortInformation Called---")
- if port in [0, 1, 2, 3]:
- info.Set(vtk.vtkDataObject.DATA_TYPE_NAME(), "vtkUnstructuredGrid")
- elif port in [4, 5, 6]:
- info.Set(vtk.vtkDataObject.DATA_TYPE_NAME(), "vtkPolyData")
- return 1
- def RequestInformation(self, request, inInfo, outInfo):
- return 1
- def RequestUpdateExtent(self, request, inInfo, outInfo):
- return 1
- def RequestDataObject(self, request, inInfo, outInfo):
- print("---RequestDataObject Called---")
- from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkRectilinearGrid
- for i in range(self.GetNumberOfOutputPorts()):
- if i in [0, 1, 2, 3]:
- outInfo.GetInformationObject(i).Set(vtk.vtkDataObject.DATA_OBJECT(), vtkUnstructuredGrid())
- elif i in [4, 5, 6]:
- outInfo.GetInformationObject(i).Set(vtk.vtkDataObject.DATA_OBJECT(), vtkPolyData())
- return 1
- def RequestData(self, request, inInfo, outInfo):
- print("---RequestData Called---")
- from vtkmodules.vtkCommonDataModel import vtkRectilinearGrid
- from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkDataSet
- from vtkmodules.vtkFiltersCore import vtkThreshold
- from vtkmodules.vtkFiltersGeneral import vtkTransformFilter, vtkRectilinearGridToPointSet
- from vtkmodules.vtkFiltersFlowPaths import vtkParticleTracer
- from vtkmodules.vtkCommonExecutionModel import vtkStreamingDemandDrivenPipeline as SDSP
- from vtkmodules.vtkCommonExecutionModel import vtkTrivialProducer
- # Get input/output objects
- input_data = vtkRectilinearGrid.GetData(inInfo[0])
- output_transformed_z = vtkUnstructuredGrid.GetData(outInfo, 0)
- output_data_land = vtkUnstructuredGrid.GetData(outInfo, 1)
- output_data_water = vtkUnstructuredGrid.GetData(outInfo, 2)
- output_velocity_field = dsa.WrapDataObject(vtkDataSet.GetData(outInfo, 3))
- output_seeds = vtkPolyData.GetData(outInfo, 4)
- particle_tracer_object = vtkPolyData.GetData(outInfo, 5)
- pathlines = vtkPolyData.GetData(outInfo, 6)
- if output_data_land is None or output_data_water is None or output_transformed_z is None:
- raise RuntimeError("Output object is None. ParaView did not allocate it.")
- # Convert to vtkPointSet using vtkRectilinearGridToPointSet
- converter = vtkRectilinearGridToPointSet()
- converter.SetInputData(input_data)
- converter.Update()
- pointset = converter.GetOutput()
- # Apply Transform
- transform = vtkTransform()
- transform.Scale(1.0, 1.0, 0.01) # example translation
- transform_filter = vtkTransformFilter()
- transform_filter.SetTransform(transform)
- transform_filter.SetInputData(pointset)
- transform_filter.Update()
- transformed = transform_filter.GetOutput()
- # Apply Threshold
- threshold_land = vtkThreshold()
- threshold_land.SetInputData(transformed)
- threshold_land.SetInputArrayToProcess(0, 0, 0, 0, self.salinity_variable_name)
- threshold_land.SetLowerThreshold(-100)
- threshold_land.SetUpperThreshold(0.0)
- threshold_land.SetThresholdFunction(threshold_land.THRESHOLD_BETWEEN)
- threshold_land.Update()
- threshold_water = vtkThreshold()
- threshold_water.SetInputData(transformed)
- threshold_water.SetInputArrayToProcess(0, 0, 0, 0, self.salinity_variable_name)
- threshold_water.SetLowerThreshold(0.0)
- threshold_water.SetUpperThreshold(100.0)
- threshold_water.SetThresholdFunction(threshold_water.THRESHOLD_BETWEEN)
- threshold_water.Update()
- # Output
- output_data_land.ShallowCopy(threshold_land.GetOutput())
- output_data_water.ShallowCopy(threshold_water.GetOutput())
- input_data_np = dsa.WrapDataObject(vtkDataSet.GetData(inInfo[0]))
- output_data_water_np = dsa.WrapDataObject(output_data_water)
- # Put vector field in velocity
- vec = []
- for i in self.inputVectorStr:
- vec.append(output_data_water_np.PointData[i])
- try:
- velocity = algs.make_vector(vec[0],vec[1],vec[2])
- except:
- velocity = algs.make_vector(vec[0],vec[1],np.zeros(len(vec[0])))
- #print("velocity type: ", type(velocity))
- #print("velocity: ", velocity)
- # Compute jacobian
- J = algs.gradient(velocity) # Jacobian
- ux, uy, uz = J[:,0,0], J[:,0,1], J[:,0,2]
- vx, vy, vz = J[:,1,0], J[:,1,1], J[:,1,2]
- wx, wy, wz = J[:,2,0], J[:,2,1], J[:,2,2]
- # Compute seeding distribution
- criterion = None
- if self.seedingStrategy == 1:
- criterion = None
- elif self.seedingStrategy == 2:
- scalarMag = algs.abs(output_data_water_np.PointData[self.inputScalarStr])
- criterion = scalarMag
- elif self.seedingStrategy == 3:
- curlSqrMag = (wy - vz)**2 + (uz - wx)**2 + (vx - uy)**2
- criterion = curlSqrMag
- elif self.seedingStrategy == 4:
- vorSqrMag = (vx - uy)**2
- criterion = vorSqrMag
- elif self.seedingStrategy == 5:
- okuboWeiss = np.abs((ux - vy)**2 + (vx + uy)**2 - (vx - uy)**2)
- criterion = okuboWeiss
- elif self.seedingStrategy == 6:
- flowMag = algs.mag(velocity)
- criterion = flowMag
- else:
- criterion = None
- # Mask unviable points
- validPointIds = np.where((vx - uy)**2 > 0)[0]
- if criterion is not None:
- criterion = criterion[validPointIds]
- # Sample from set of valid points in domain
- seedPointIds = random.choices(validPointIds, weights=criterion, k=self.numSeeds)
- newSeedPoints = vtkAppendPolyData()
- for idval in seedPointIds:
- newSeedPoints.AddInputData(self.point_generate(output_data_water_np, idval, 1))
- newSeedPoints.Update()
- # Populate output ports for velocity and seeds
- output_velocity_field.ShallowCopy(output_data_water_np.VTKObject)
- output_velocity_field.PointData.append(velocity, "velocity")
- output_seeds.ShallowCopy(newSeedPoints.GetOutput())
- if not self.seeded:
- self.seeds.ShallowCopy(newSeedPoints.GetOutput())
- # -------------------------------------------------------------------- ParticleTracer --------------------------------------------------------------------
- print("----------------------------------")
- # Get input info object from main input (index 0)
- input_info = self.GetExecutive().GetInputInformation(0, 0)
- output_info = output_velocity_field.GetInformation()
- output_info_seeds = self.seeds.GetInformation()
- # Forward TIME_STEPS
- if input_info.Has(SDSP.TIME_STEPS()):
- time_steps = input_info.Get(SDSP.TIME_STEPS())
- output_info.Set(SDSP.TIME_STEPS(), time_steps, len(time_steps))
- output_info_seeds.Set(SDSP.TIME_STEPS(), time_steps, len(time_steps))
- # Forward TIME_RANGE
- if input_info.Has(SDSP.TIME_RANGE()):
- time_range = input_info.Get(SDSP.TIME_RANGE())
- output_info.Set(SDSP.TIME_RANGE(), time_range, 2)
- output_info_seeds.Set(SDSP.TIME_RANGE(), time_range, 2)
- # Forward UPDATE_TIME_STEP (optional, sometimes needed)
- if input_info.Has(SDSP.UPDATE_TIME_STEP()):
- current_time = input_info.Get(SDSP.UPDATE_TIME_STEP())
- output_info.Set(SDSP.UPDATE_TIME_STEP(), current_time)
- output_info.Set(vtk.vtkDataObject.DATA_TIME_STEP(), current_time)
- output_info_seeds.Set(SDSP.UPDATE_TIME_STEP(), current_time)
- output_info_seeds.Set(vtk.vtkDataObject.DATA_TIME_STEP(), current_time)
- print("------")
- if not self.seeded:
- self.seeded = True
- self.seed_producer.SetOutput(self.seeds)
- print("seed_producer.SetOutput")
- print("------")
- print("------")
- if output_velocity_field.GetInformation().Has(SDSP.TIME_STEPS()):
- time_steps = output_velocity_field.GetInformation().Get(SDSP.TIME_STEPS())
- print("TIME_STEPS in output_velocity_field:", time_steps)
- else:
- print("No TIME_STEPS in output_velocity_field")
- if output_velocity_field.GetInformation().Has(SDSP.TIME_RANGE()):
- time_steps = output_velocity_field.GetInformation().Get(SDSP.TIME_RANGE())
- print("TIME_RANGE in output_velocity_field:", time_steps)
- else:
- print("No TIME_RANGE in output_velocity_field")
- if output_velocity_field.GetInformation().Has(SDSP.UPDATE_TIME_STEP()):
- time_steps = output_velocity_field.GetInformation().Get(SDSP.UPDATE_TIME_STEP())
- print("UPDATE_TIME_STEP in output_velocity_field:", time_steps)
- else:
- print("No UPDATE_TIME_STEP in output_velocity_field")
- if output_velocity_field.GetInformation().Has(vtk.vtkDataObject.DATA_TIME_STEP()):
- time_steps = output_velocity_field.GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())
- print("DATA_TIME_STEP in output_velocity_field:", time_steps)
- else:
- print("No DATA_TIME_STEP in output_velocity_field")
- velocity_field_producer = vtkTrivialProducer()
- velocity_field_producer.SetOutput(output_velocity_field.VTKObject)
- velocity_field_producer.Update()
- info = output_velocity_field.VTKObject.GetInformation()
- time = info.Get(SDSP.TIME_STEPS())
- velocity_field_producer.GetOutputInformation(0).Set(SDSP.TIME_STEPS(), time, len(time_steps))
- self.seed_producer.GetOutputInformation(0).Set(SDSP.TIME_STEPS(), time, len(time_steps))
- time = info.Get(SDSP.TIME_RANGE())
- velocity_field_producer.GetOutputInformation(0).Set(SDSP.TIME_RANGE(), time, 2)
- self.seed_producer.GetOutputInformation(0).Set(SDSP.TIME_RANGE(), time, len(time_steps))
- time = info.Get(SDSP.UPDATE_TIME_STEP())
- velocity_field_producer.GetOutputInformation(0).Set(SDSP.UPDATE_TIME_STEP(), time)
- self.seed_producer.GetOutputInformation(0).Set(SDSP.UPDATE_TIME_STEP(), time)
- velocity_field_producer.GetOutputInformation(0).Set(vtk.vtkDataObject.DATA_TIME_STEP(), current_time)
- self.seed_producer.GetOutputInformation(0).Set(vtk.vtkDataObject.DATA_TIME_STEP(), time)
- # ------
- print("------")
- velocity_field_producer_output = velocity_field_producer.GetOutputDataObject(0)
- point_data = velocity_field_producer_output.GetPointData()
- velocity_array = point_data.GetArray("velocity")
- print(f"velocity_field_producer_output: {velocity_array}:")
- print("------")
- if self.seeds.GetInformation().Has(SDSP.TIME_STEPS()):
- time_steps = self.seeds.GetInformation().Get(SDSP.TIME_STEPS())
- print("TIME_STEPS in self.seeds:", time_steps)
- else:
- print("No TIME_STEPS in self.seeds")
- if self.seeds.GetInformation().Has(SDSP.TIME_RANGE()):
- time_steps = self.seeds.GetInformation().Get(SDSP.TIME_RANGE())
- print("TIME_RANGE in self.seeds:", time_steps)
- else:
- print("No TIME_RANGE in output_velocity_field")
- if self.seeds.GetInformation().Has(SDSP.UPDATE_TIME_STEP()):
- time_steps = self.seeds.GetInformation().Get(SDSP.UPDATE_TIME_STEP())
- print("UPDATE_TIME_STEP in self.seeds:", time_steps)
- else:
- print("No UPDATE_TIME_STEP in self.seeds")
- if self.seeds.GetInformation().Has(vtk.vtkDataObject.DATA_TIME_STEP()):
- time_steps = self.seeds.GetInformation().Get(vtk.vtkDataObject.DATA_TIME_STEP())
- print("DATA_TIME_STEP in self.seeds:", time_steps)
- else:
- print("No DATA_TIME_STEP in self.seeds")
- print("------")
- time_steps = 0
- if velocity_field_producer.GetOutputInformation(0).Has(SDSP.TIME_STEPS()):
- time_steps = velocity_field_producer.GetOutputInformation(0).Get(SDSP.TIME_STEPS())
- print("TIME_STEPS in velocity_field_producer: ", time_steps)
- else:
- print("No TIME_STEPS in velocity_field_producer")
- if velocity_field_producer.GetOutputInformation(0).Has(SDSP.TIME_RANGE()):
- time_steps = velocity_field_producer.GetOutputInformation(0).Get(SDSP.TIME_RANGE())
- print("TIME_RANGE in velocity_field_producer: ", time_steps)
- else:
- print("No TIME_RANGE in velocity_field_producer")
- if velocity_field_producer.GetOutputInformation(0).Has(SDSP.UPDATE_TIME_STEP()):
- time_steps = velocity_field_producer.GetOutputInformation(0).Get(SDSP.UPDATE_TIME_STEP())
- print("UPDATE_TIME_STEP in velocity_field_producer: ", time_steps)
- else:
- print("No UPDATE_TIME_STEP in velocity_field_producer")
- if velocity_field_producer.GetOutputInformation(0).Has(vtk.vtkDataObject.DATA_TIME_STEP()):
- time_steps = velocity_field_producer.GetOutputInformation(0).Get(vtk.vtkDataObject.DATA_TIME_STEP())
- print("DATA_TIME_STEP in velocity_field_producer: ", time_steps)
- else:
- print("No DATA_TIME_STEP in velocity_field_producer")
- print("------")
- time_steps = 0
- if self.seed_producer.GetOutputInformation(0).Has(SDSP.TIME_STEPS()):
- time_steps = self.seed_producer.GetOutputInformation(0).Get(SDSP.TIME_STEPS())
- print("TIME_STEPS in self.seed_producer: ", time_steps)
- else:
- print("No TIME_STEPS in self.seed_producer")
- if self.seed_producer.GetOutputInformation(0).Has(SDSP.TIME_RANGE()):
- time_steps = self.seed_producer.GetOutputInformation(0).Get(SDSP.TIME_RANGE())
- print("TIME_RANGE in self.seed_producer: ", time_steps)
- else:
- print("No TIME_RANGE in self.seed_producer")
- if self.seed_producer.GetOutputInformation(0).Has(SDSP.UPDATE_TIME_STEP()):
- time_steps = self.seed_producer.GetOutputInformation(0).Get(SDSP.UPDATE_TIME_STEP())
- print("UPDATE_TIME_STEP in self.seed_producer: ", time_steps)
- else:
- print("No UPDATE_TIME_STEP in self.seed_producer")
- if self.seed_producer.GetOutputInformation(0).Has(vtk.vtkDataObject.DATA_TIME_STEP()):
- time_steps = self.seed_producer.GetOutputInformation(0).Get(vtk.vtkDataObject.DATA_TIME_STEP())
- print("DATA_TIME_STEP in self.seed_producer: ", time_steps)
- else:
- print("No DATA_TIME_STEP in self.seed_producer")
- print("------")
- num_points = self.seeds.GetNumberOfPoints()
- print(f"Number of seed points: {num_points}")
- pd = output_velocity_field.VTKObject.GetPointData()
- velocity_array = pd.GetArray("velocity")
- print("------")
- if velocity_array:
- pd.SetVectors(velocity_array)
- print("Set 'velocity' as active vectors.")
- else:
- print("No array named 'velocity' found!")
- self.particle_tracer.SetInputConnection(0, velocity_field_producer.GetOutputPort())
- self.particle_tracer.SetInputConnection(1, self.seed_producer.GetOutputPort())
- velPortInfo = outInfo.GetInformationObject(3)
- tracerExec = self.particle_tracer.GetExecutive()
- inVelInfo = tracerExec.GetInputInformation(0, 0)
- input_info_tracer_1 = tracerExec.GetInputInformation(1, 0)
- if output_velocity_field.GetInformation().Has(SDSP.TIME_STEPS()):
- ts = output_velocity_field.GetInformation().Get(SDSP.TIME_STEPS())
- inVelInfo.Set(SDSP.TIME_STEPS(), ts, len(ts))
- input_info_tracer_1.Set(SDSP.TIME_STEPS(), ts, len(ts))
- if output_velocity_field.GetInformation().Has(SDSP.TIME_RANGE()):
- tr = output_velocity_field.GetInformation().Get(SDSP.TIME_RANGE())
- inVelInfo.Set(SDSP.TIME_RANGE(), tr, 2)
- input_info_tracer_1.Set(SDSP.TIME_RANGE(), tr, 2)
- if output_velocity_field.GetInformation().Has(SDSP.UPDATE_TIME_STEP()):
- ut = output_velocity_field.GetInformation().Get(SDSP.UPDATE_TIME_STEP())
- inVelInfo.Set(SDSP.UPDATE_TIME_STEP(), ut)
- inVelInfo.Set(vtk.vtkDataObject.DATA_TIME_STEP(), ut)
- input_info_tracer_1.Set(SDSP.UPDATE_TIME_STEP(), ut)
- input_info_tracer_1.Set(vtk.vtkDataObject.DATA_TIME_STEP(), ut)
- self.particle_tracer.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, "velocity")
- print("------")
- input_info_tracer_0 = self.particle_tracer.GetInputInformation(0, 0)
- input_info_tracer_1 = self.particle_tracer.GetInputInformation(1, 0)
- if input_info_tracer_0.Has(SDSP.TIME_STEPS()) and input_info_tracer_1.Has(SDSP.TIME_STEPS()):
- time_steps = input_info_tracer_0.Get(SDSP.TIME_STEPS())
- print("TIME_STEPS in input_info_tracer: ", time_steps)
- else:
- print("No TIME_STEP in input_info_tracer")
- if input_info_tracer_0.Has(SDSP.TIME_RANGE()) and input_info_tracer_1.Has(SDSP.TIME_RANGE()):
- time_steps = input_info_tracer_0.Get(SDSP.TIME_RANGE())
- print("TIME_RANGE in input_info_tracer: ", time_steps)
- else:
- print("No TIME_RANGE in input_info_tracer")
- if input_info_tracer_0.Has(SDSP.UPDATE_TIME_STEP()) and input_info_tracer_1.Has(SDSP.UPDATE_TIME_STEP()):
- time_steps = input_info_tracer_0.Get(SDSP.UPDATE_TIME_STEP())
- print("UPDATE_TIME_STEP in input_info_tracer: ", time_steps)
- else:
- print("No UPDATE_TIME_STEP in input_info_tracer")
- if input_info_tracer_0.Has(vtk.vtkDataObject.DATA_TIME_STEP()) and input_info_tracer_1.Has(vtk.vtkDataObject.DATA_TIME_STEP()):
- time_steps = input_info_tracer_0.Get(vtk.vtkDataObject.DATA_TIME_STEP())
- print("DATA_TIME_STEP in input_info_tracer: ", time_steps)
- else:
- print("No DATA_TIME_STEP in input_info_tracer")
- print("---")
- print("REQUEST_UPDATE_TIME: ", SDSP.REQUEST_UPDATE_TIME())
- # suppose 'alg' is any vtkAlgorithm (e.g. your reader or trivial producer)
- # info = tracer.GetExecutive().GetOutputInformation(0)
- # or if you have the producer: info = producer.GetOutputInformation(0)
- # request time T
- # info.Set(SDSP.UPDATE_TIME_STEP(), time_steps)
- # output_info = particle_tracer.GetInformation()
- # time_steps = input_info_tracer.Get(SDSP.TIME_STEPS())
- # output_info.Set(SDSP.TIME_STEPS(), time_steps, len(time_steps))
- # time_range = input_info_tracer.Get(SDSP.TIME_RANGE())
- # output_info.Set(SDSP.TIME_RANGE(), time_range, 2)
- # update_time_step = input_info_tracer.Get(SDSP.UPDATE_TIME_STEP())
- # output_info.Set(SDSP.UPDATE_TIME_STEP(), update_time_step)
- # data_time_step = input_info_tracer.Get(vtk.vtkDataObject.DATA_TIME_STEP())
- # output_info.Set(vtk.vtkDataObject.DATA_TIME_STEP(), data_time_step)
- # self.particle_tracer.SetInitialIntegrationStep(0.1) # Adjust as necessary
- # self.particle_tracer.SetMaximumNumberOfSteps(1000)
- # print("------")
- # input_data = self.particle_tracer.GetInputDataObject(0, 0)
- # if input_data is not None:
- # vectors = input_data.GetPointData().GetVectors()
- # if vectors:
- # print("Velocity field found:", vectors.GetName())
- # print("Number of vectors:", vectors.GetNumberOfTuples())
- # else:
- # print("No velocity vectors found in point data.")
- # else:
- # print("No input dataset connected.")
- # This works if you've connected your seeds to port 1 (seeds port)
- seeds = self.particle_tracer.GetInputDataObject(1, 0)
- if seeds is not None:
- print("Seeds present.")
- print("Number of seed points:", seeds.GetNumberOfPoints())
- else:
- print("No seed points connected.")
- print("------")
- print("BEFORE Update(): Num Particles:", self.particle_tracer.GetOutput().GetNumberOfPoints())
- print("BEFORE Update(): Current Time(Port-0):", self.particle_tracer.GetInputInformation(0, 0).Get(SDSP.UPDATE_TIME_STEP()))
- print("BEFORE Update(): Current Time(Port-1):", self.particle_tracer.GetInputInformation(1, 0).Get(SDSP.UPDATE_TIME_STEP()))
- print("BEFORE Update(): Current Time Object(Port-0):", self.particle_tracer.GetInputInformation(0, 0))
- print("BEFORE Update(): Current Time Object(Port-1):", self.particle_tracer.GetInputInformation(1, 0))
- rk4 = vtk.vtkRungeKutta4()
- #self.particle_tracer.SetIntegrator(rk4)
- #self.particle_tracer.SetIntegratorType(vtkParticleTracer.RUNGE_KUTTA2)
- self.particle_tracer.SetInterpolatorType(vtkParticleTracer.INTERPOLATOR_WITH_DATASET_POINT_LOCATOR)
- if not self.start_time_set:
- self.start_time_set = True
- self.particle_tracer.SetStartTime(self.particle_tracer.GetInputInformation(0, 0).Get(SDSP.UPDATE_TIME_STEP()))
- print("------")
- print("GetIntegrator: ", self.particle_tracer.GetIntegrator())
- print("GetIntegratorType: ", self.particle_tracer.GetIntegratorType())
- print("GetStartTime: ", self.particle_tracer.GetStartTime())
- print("GetIgnorePipelineTime: ", self.particle_tracer.GetIgnorePipelineTime())
- self.particle_tracer.InitializeObjectBase()
- self.particle_tracer.Modified()
- # Pass time to the tracer
- #tracer.SetCurrentTime(input_info_tracer_0.Has(SDSP.UPDATE_TIME_STEP()))
- self.particle_tracer.Update()
- # current_time_step = self.particle_tracer.GetInputInformation(0, 0).Get(SDSP.UPDATE_TIME_STEP())
- # current_time_step = current_time_step + 24
- # next_time_step = current_time_step # Or however you want to calculate the next time step
- # self.particle_tracer.GetInputInformation(0, 0).Set(SDSP.UPDATE_TIME_STEP(), next_time_step)
- print("------")
- print("AFTER Update(): Num Particles:", self.particle_tracer.GetOutput().GetNumberOfPoints())
- print("AFTER Update(): Current Time(Port-0):", self.particle_tracer.GetInputInformation(0, 0).Get(SDSP.UPDATE_TIME_STEP()))
- print("AFTER Update(): Current Time(Port-1):", self.particle_tracer.GetInputInformation(1, 0).Get(SDSP.UPDATE_TIME_STEP()))
- print("AFTER Update(): Current Time Object(Port-0):", self.particle_tracer.GetInputInformation(0, 0))
- print("AFTER Update(): Current Time Object(Port-1):", self.particle_tracer.GetInputInformation(1, 0))
- print("------")
- # Check number of particles
- print(f"Particles after update: {self.particle_tracer.GetOutput().GetNumberOfPoints()}")
- # Ensure ParticleAge is being correctly updated
- particle_age_array = self.particle_tracer.GetOutput().GetPointData().GetArray('ParticleAge')
- if particle_age_array:
- print("ParticleAge values after update:", particle_age_array)
- # for i in range(self.particle_tracer.GetOutput().GetNumberOfPoints()):
- # age = particle_age_array.GetValue(i)
- # print("age: ", age)
- print("------")
- output = self.particle_tracer.GetOutput()
- if output is not None and output.GetNumberOfPoints() > 0:
- print("Particles generated:", output.GetNumberOfPoints())
- else:
- print("No particles generated.")
- particle_tracer_object.ShallowCopy(self.particle_tracer.GetOutput())
- print("------")
- # After Copying Tracer's output
- if output_info.Has(SDSP.TIME_STEPS()):
- time_steps = output_info.Get(SDSP.TIME_STEPS())
- print("TIME_STEP in particle_tracer: ", time_steps)
- else:
- print("No TIME_STEP in particle_tracer")
- if output_info.Has(SDSP.TIME_RANGE()):
- time_steps = output_info.Get(SDSP.TIME_RANGE())
- print("TIME_RANGE in particle_tracer: ", time_steps)
- else:
- print("No TIME_RANGE in particle_tracer")
- if output_info.Has(SDSP.UPDATE_TIME_STEP()):
- time_steps = output_info.Get(SDSP.UPDATE_TIME_STEP())
- print("UPDATE_TIME_STEP in particle_tracer: ", time_steps)
- else:
- print("No UPDATE_TIME_STEP in particle_tracer")
- if output_info.Has(vtk.vtkDataObject.DATA_TIME_STEP()):
- time_steps = output_info.Get(vtk.vtkDataObject.DATA_TIME_STEP())
- print("DATA_TIME_STEP in particle_tracer: ", time_steps)
- else:
- print("No DATA_TIME_STEP in particle_tracer")
- # ------------------------------------------------------------------------------------------------------------------------------------------------------
- # ---------------------------------------------------- TemporalParticlesToPathlines --------------------------------------------------------------------
- pathline_filter = vtk.vtkTemporalPathLineFilter()
- #pathline_filter.SetInputConnection(self.particle_tracer.GetOutputPort())
- pathline_filter.SetInputData(particle_tracer_object)
- # pathline_filter.SetMaskPoints(1)
- # pathline_filter.SetMaxTrackLength(200)
- # pathline_filter.SetIdChannelArray("ParticleId")
- # 3. Set parameters (optional, depending on what you're tracking)
- #pathline_filter.SetMaskArray("Mask") # optional: if you want to mask/filter some particles
- pathline_filter.SetIdChannelArray("ParticleId") # required to track individual particles
- #pathline_filter.SetTimeChannelArray("IntegrationTime") # required to trace particle over time
- #pathline_filter.SetKeepFinalPoints(1) # optional: keeps the final points at the end of pathlines
- pathline_filter.SetMaxTrackLength(2500) # -1 means no limit
- pathline_filter.SetMaxStepDistance(0.0, 0.0, 0.0) # 0 means automatic
- pathline_filter.Update()
- print("Num Lines:", pathline_filter.GetOutput().GetNumberOfLines())
- # 4. Update the filter to produce the output
- # pathline_filter.Update()
- pathlines.ShallowCopy(pathline_filter.GetOutput())
- # Ensure connectivity is available
- # pathlines.GetCellData().AddArray(pathline_filter.GetOutput().GetCellData().GetArray("ParticleId"))
- # ------------------------------------------------------------------------------------------------------------------------------------------------------
- # 'Hide' Water
- empty = vtkUnstructuredGrid()
- output_data_water.ShallowCopy(empty)
- #output_velocity_field.ShallowCopy(empty)
- # 'Hide' Seeds
- #empty = vtkPolyData()
- #output_seeds.ShallowCopy(empty)
- return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement