# Create a lot of hyperstreamlines and write to disk.
#
# The purpose of this script is to show how much memory is used.
#
# There are two lines you can uncomment below to have the streamlines
# each update two times, and by using top you can see this 
# is more memory than when they execute once.
# 

package require vtk

# generate tensors (from Hyper.tcl)
vtkPointLoad ptLoad
ptLoad SetLoadValue 100.0
ptLoad SetSampleDimensions 20 20 20
ptLoad ComputeEffectiveStressOn
ptLoad SetModelBounds -10 10 -10 10 -10 10
set input [ptLoad GetOutput]

# make tensors
ptLoad Update

# for writing all hyperstreamlines to disk
vtkAppendPolyData app


# make many hyperstreamlines, each somewhere 
# within the dataset
set count 1
set numberOfHyperStreamlines 1000

while { $count < $numberOfHyperStreamlines } {

    vtkHyperStreamline s$count
    #s$count SetStartPosition  -9 -9 -9
    s$count SetStartPosition  -9 -9 [expr -9 + 19/$count]
    s$count SetInput $input

    s$count Update

    # Uncomment these lines in order to see the memory leak
    # This will use more memory than when the update happens once.
    #s$count Modified
    #s$count Update

    # Uncomment this to see some memory is released, but not
    # all if the objects update twice.
    #s$count Delete

    # add this one to the appender
    app AddInput [s$count GetOutput]

    incr count
}

puts "Created $count hyperstreamlines."
puts "Now check memory use via top or similar program."

# Save the appended hyperstreamlines
vtkPolyDataWriter w
w SetInput [app GetOutput]
set fileName "testHyperStreamlines.vtk"
w SetFileName $fileName
w Write

puts "Wrote hyperstreamlines in file $fileName."


