vtk-m2/vtkm/worklet/particleadvection/ParticleAdvectionWorklets.h

243 lines
8.5 KiB
C
Raw Normal View History

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#ifndef vtk_m_worklet_particleadvection_ParticleAdvectionWorklets_h
#define vtk_m_worklet_particleadvection_ParticleAdvectionWorklets_h
#include <vtkm/Types.h>
#include <vtkm/cont/Algorithm.h>
#include <vtkm/cont/ArrayCopy.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleCast.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/ConvertNumComponentsToOffsets.h>
#include <vtkm/cont/ExecutionObjectBase.h>
#include <vtkm/Particle.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/worklet/particleadvection/Particles.h>
#include <vtkm/worklet/particleadvection/Stepper.h>
2020-06-16 12:54:01 +00:00
#ifdef VTKM_CUDA
#include <vtkm/cont/cuda/internal/ScopedCudaStackSize.h>
#endif
namespace vtkm
{
namespace worklet
{
namespace particleadvection
{
class ParticleAdvectWorklet : public vtkm::worklet::WorkletMapField
{
public:
2019-10-01 17:33:12 +00:00
using ControlSignature = void(FieldIn idx,
ExecObject integrator,
ExecObject integralCurve,
2019-10-01 17:33:12 +00:00
FieldIn maxSteps);
using ExecutionSignature = void(_1 idx, _2 integrator, _3 integralCurve, _4 maxSteps);
2019-10-01 17:33:12 +00:00
using InputDomain = _1;
template <typename IntegratorType, typename IntegralCurveType>
2019-10-01 17:33:12 +00:00
VTKM_EXEC void operator()(const vtkm::Id& idx,
const IntegratorType& integrator,
IntegralCurveType& integralCurve,
const vtkm::Id& maxSteps) const
{
auto particle = integralCurve.GetParticle(idx);
vtkm::FloatDefault time = particle.Time;
bool tookAnySteps = false;
2019-10-01 17:33:12 +00:00
//the integrator status needs to be more robust:
// 1. you could have success AND at temporal boundary.
// 2. could you have success AND at spatial?
// 3. all three?
integralCurve.PreStepUpdate(idx);
2019-10-01 17:33:12 +00:00
do
{
particle = integralCurve.GetParticle(idx);
vtkm::Vec3f outpos;
auto status = integrator.Step(particle, time, outpos);
2019-10-01 17:33:12 +00:00
if (status.CheckOk())
{
integralCurve.StepUpdate(idx, particle, time, outpos);
tookAnySteps = true;
}
2019-10-01 17:33:12 +00:00
//We can't take a step inside spatial boundary.
//Try and take a step just past the boundary.
else if (status.CheckSpatialBounds())
{
status = integrator.SmallStep(particle, time, outpos);
if (status.CheckOk())
{
integralCurve.StepUpdate(idx, particle, time, outpos);
2019-10-01 17:33:12 +00:00
tookAnySteps = true;
}
}
integralCurve.StatusUpdate(idx, status, maxSteps);
} while (integralCurve.CanContinue(idx));
2019-10-01 17:33:12 +00:00
//Mark if any steps taken
integralCurve.UpdateTookSteps(idx, tookAnySteps);
}
};
template <typename IntegratorType, typename ParticleType>
class ParticleAdvectionWorklet
{
public:
VTKM_EXEC_CONT ParticleAdvectionWorklet() {}
~ParticleAdvectionWorklet() {}
void Run(const IntegratorType& integrator,
vtkm::cont::ArrayHandle<ParticleType>& particles,
vtkm::Id& MaxSteps)
{
using ParticleAdvectWorkletType = vtkm::worklet::particleadvection::ParticleAdvectWorklet;
using ParticleWorkletDispatchType =
typename vtkm::worklet::DispatcherMapField<ParticleAdvectWorkletType>;
using ParticleArrayType = vtkm::worklet::particleadvection::Particles<ParticleType>;
2017-05-25 21:16:09 +00:00
vtkm::Id numSeeds = static_cast<vtkm::Id>(particles.GetNumberOfValues());
//Create and invoke the particle advection.
vtkm::cont::ArrayHandleConstant<vtkm::Id> maxSteps(MaxSteps, numSeeds);
2019-10-01 17:33:12 +00:00
vtkm::cont::ArrayHandleIndex idxArray(numSeeds);
// TODO: The particle advection sometimes behaves incorrectly on CUDA if the stack size
// is not changed thusly. This is concerning as the compiler should be able to determine
// staticly the required stack depth. What is even more concerning is that the runtime
// does not report a stack overflow. Rather, the worklet just silently reports the wrong
// value. Until we determine the root cause, other problems may pop up.
#ifdef VTKM_CUDA
// This worklet needs some extra space on CUDA.
2020-06-16 12:54:01 +00:00
vtkm::cont::cuda::internal::ScopedCudaStackSize stack(16 * 1024);
(void)stack;
#endif // VTKM_CUDA
ParticleArrayType particlesObj(particles, MaxSteps);
//Invoke particle advection worklet
ParticleWorkletDispatchType particleWorkletDispatch;
particleWorkletDispatch.Invoke(idxArray, integrator, particlesObj, maxSteps);
}
};
namespace detail
{
2019-12-05 20:46:41 +00:00
class GetSteps : public vtkm::worklet::WorkletMapField
{
public:
VTKM_CONT
2019-12-05 20:46:41 +00:00
GetSteps() {}
using ControlSignature = void(FieldIn, FieldOut);
using ExecutionSignature = void(_1, _2);
VTKM_EXEC void operator()(const vtkm::Particle& p, vtkm::Id& numSteps) const
{
numSteps = p.NumSteps;
}
};
class ComputeNumPoints : public vtkm::worklet::WorkletMapField
{
public:
VTKM_CONT
ComputeNumPoints() {}
using ControlSignature = void(FieldIn, FieldIn, FieldOut);
using ExecutionSignature = void(_1, _2, _3);
2019-12-05 20:46:41 +00:00
// Offset is number of points in streamline.
// 1 (inital point) + number of steps taken (p.NumSteps - initalNumSteps)
VTKM_EXEC void operator()(const vtkm::Particle& p,
const vtkm::Id& initialNumSteps,
vtkm::Id& diff) const
{
2019-12-05 20:46:41 +00:00
diff = 1 + p.NumSteps - initialNumSteps;
}
};
} // namespace detail
2019-10-10 13:46:29 +00:00
template <typename IntegratorType, typename ParticleType>
class StreamlineWorklet
{
public:
template <typename PointStorage, typename PointStorage2>
void Run(const IntegratorType& it,
vtkm::cont::ArrayHandle<ParticleType, PointStorage>& particles,
vtkm::Id& MaxSteps,
vtkm::cont::ArrayHandle<vtkm::Vec3f, PointStorage2>& positions,
vtkm::cont::CellSetExplicit<>& polyLines)
2019-10-10 13:46:29 +00:00
{
using ParticleWorkletDispatchType = typename vtkm::worklet::DispatcherMapField<
vtkm::worklet::particleadvection::ParticleAdvectWorklet>;
using StreamlineArrayType =
vtkm::worklet::particleadvection::StateRecordingParticles<ParticleType>;
2019-12-05 20:46:41 +00:00
vtkm::cont::ArrayHandle<vtkm::Id> initialStepsTaken;
vtkm::Id numSeeds = static_cast<vtkm::Id>(particles.GetNumberOfValues());
vtkm::cont::ArrayHandleIndex idxArray(numSeeds);
2019-12-05 20:46:41 +00:00
vtkm::worklet::DispatcherMapField<detail::GetSteps> getStepDispatcher{ (detail::GetSteps{}) };
getStepDispatcher.Invoke(particles, initialStepsTaken);
// This method uses the same workklet as ParticleAdvectionWorklet::Run (and more). Yet for
// some reason ParticleAdvectionWorklet::Run needs this adjustment while this method does
// not.
2019-12-05 20:46:41 +00:00
#ifdef VTKM_CUDA
// // This worklet needs some extra space on CUDA.
// vtkm::cont::cuda::internal::ScopedCudaStackSize stack(16 * 1024);
// (void)stack;
2019-12-05 20:46:41 +00:00
#endif // VTKM_CUDA
//Run streamline worklet
StreamlineArrayType streamlines(particles, MaxSteps);
ParticleWorkletDispatchType particleWorkletDispatch;
vtkm::cont::ArrayHandleConstant<vtkm::Id> maxSteps(MaxSteps, numSeeds);
particleWorkletDispatch.Invoke(idxArray, it, streamlines, maxSteps);
2017-08-28 20:58:44 +00:00
//Get the positions
streamlines.GetCompactedHistory(positions);
//Create the cells
2019-12-05 20:46:41 +00:00
vtkm::cont::ArrayHandle<vtkm::Id> numPoints;
vtkm::worklet::DispatcherMapField<detail::ComputeNumPoints> computeNumPointsDispatcher{ (
detail::ComputeNumPoints{}) };
computeNumPointsDispatcher.Invoke(particles, initialStepsTaken, numPoints);
vtkm::cont::ArrayHandle<vtkm::Id> cellIndex;
2019-12-05 20:46:41 +00:00
vtkm::Id connectivityLen = vtkm::cont::Algorithm::ScanExclusive(numPoints, cellIndex);
vtkm::cont::ArrayHandleCounting<vtkm::Id> connCount(0, 1, connectivityLen);
vtkm::cont::ArrayHandle<vtkm::Id> connectivity;
vtkm::cont::ArrayCopy(connCount, connectivity);
vtkm::cont::ArrayHandle<vtkm::UInt8> cellTypes;
auto polyLineShape =
vtkm::cont::make_ArrayHandleConstant<vtkm::UInt8>(vtkm::CELL_SHAPE_POLY_LINE, numSeeds);
vtkm::cont::ArrayCopy(polyLineShape, cellTypes);
auto offsets = vtkm::cont::ConvertNumComponentsToOffsets(numPoints);
polyLines.Fill(positions.GetNumberOfValues(), cellTypes, connectivity, offsets);
}
};
}
}
} // namespace vtkm::worklet::particleadvection
#endif // vtk_m_worklet_particleadvection_ParticleAdvectionWorklets_h