VTK-m  2.0
RK4Integrator.h
Go to the documentation of this file.
1 //=============================================================================
2 //
3 // Copyright (c) Kitware, Inc.
4 // All rights reserved.
5 // See LICENSE.txt for details.
6 //
7 // This software is distributed WITHOUT ANY WARRANTY; without even
8 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 // PURPOSE. See the above copyright notice for more information.
10 //
11 //=============================================================================
12 
13 #ifndef vtk_m_filter_flow_worklet_RK4Integrator_h
14 #define vtk_m_filter_flow_worklet_RK4Integrator_h
15 
18 
19 namespace vtkm
20 {
21 namespace worklet
22 {
23 namespace flow
24 {
25 
26 template <typename ExecEvaluatorType>
28 {
29 public:
31  ExecRK4Integrator(const ExecEvaluatorType& evaluator)
32  : Evaluator(evaluator)
33  {
34  }
35 
36  template <typename Particle>
38  vtkm::FloatDefault stepLength,
39  vtkm::Vec3f& velocity) const
40  {
41  auto time = particle.GetTime();
42  auto inpos = particle.GetEvaluationPosition(stepLength);
43  vtkm::FloatDefault boundary = this->Evaluator.GetTemporalBoundary(static_cast<vtkm::Id>(1));
44  if ((time + stepLength + vtkm::Epsilon<vtkm::FloatDefault>() - boundary) > 0.0)
45  stepLength = boundary - time;
46 
47  //k1 = F(p,t)
48  //k2 = F(p+hk1/2, t+h/2
49  //k3 = F(p+hk2/2, t+h/2
50  //k4 = F(p+hk3, t+h)
51  //Yn+1 = Yn + 1/6 h (k1+2k2+2k3+k4)
52 
53  vtkm::FloatDefault var1 = (stepLength / static_cast<vtkm::FloatDefault>(2));
54  vtkm::FloatDefault var2 = time + var1;
55  vtkm::FloatDefault var3 = time + stepLength;
56 
58  vtkm::Vec3f v2 = v1, v3 = v1, v4 = v1;
59  vtkm::VecVariable<vtkm::Vec3f, 2> k1, k2, k3, k4;
60 
61  GridEvaluatorStatus evalStatus;
62 
63  evalStatus = this->Evaluator.Evaluate(inpos, time, k1);
64  if (evalStatus.CheckFail())
65  return IntegratorStatus(evalStatus, false);
66  v1 = particle.Velocity(k1, stepLength);
67 
68  evalStatus = this->Evaluator.Evaluate(inpos + var1 * v1, var2, k2);
69  if (evalStatus.CheckFail())
70  return IntegratorStatus(evalStatus, false);
71  v2 = particle.Velocity(k2, stepLength);
72 
73  evalStatus = this->Evaluator.Evaluate(inpos + var1 * v2, var2, k3);
74  if (evalStatus.CheckFail())
75  return IntegratorStatus(evalStatus, false);
76  v3 = particle.Velocity(k3, stepLength);
77 
78  evalStatus = this->Evaluator.Evaluate(inpos + stepLength * v3, var3, k4);
79  if (evalStatus.CheckFail())
80  return IntegratorStatus(evalStatus, false);
81  v4 = particle.Velocity(k4, stepLength);
82 
83  velocity = (v1 + 2 * v2 + 2 * v3 + v4) / static_cast<vtkm::FloatDefault>(6);
84 
85  return IntegratorStatus(
86  evalStatus, vtkm::MagnitudeSquared(velocity) <= vtkm::Epsilon<vtkm::FloatDefault>());
87  }
88 
89 private:
90  ExecEvaluatorType Evaluator;
91 };
92 
93 template <typename EvaluatorType>
95 {
96 private:
97  EvaluatorType Evaluator;
98 
99 public:
100  VTKM_CONT
101  RK4Integrator() = default;
102 
103  VTKM_CONT
104  RK4Integrator(const EvaluatorType& evaluator)
105  : Evaluator(evaluator)
106  {
107  }
108 
110  vtkm::cont::Token& token) const
111  -> ExecRK4Integrator<decltype(this->Evaluator.PrepareForExecution(device, token))>
112  {
113  auto evaluator = this->Evaluator.PrepareForExecution(device, token);
114  using ExecEvaluatorType = decltype(evaluator);
115  return ExecRK4Integrator<ExecEvaluatorType>(evaluator);
116  }
117 };
118 
119 }
120 }
121 } //vtkm::worklet::flow
122 
123 #endif // vtk_m_filter_flow_worklet_RK4Integrator_h
vtkm::MagnitudeSquared
VTKM_EXEC_CONT detail::FloatingPointReturnType< T >::Type MagnitudeSquared(const T &x)
Returns the square of the magnitude of a vector.
Definition: VectorAnalysis.h:64
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::flow::RK4Integrator
Definition: RK4Integrator.h:94
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Particle
Definition: Particle.h:90
vtkm::Particle::GetEvaluationPosition
VTKM_EXEC_CONT vtkm::Vec3f GetEvaluationPosition(const vtkm::FloatDefault &deltaT) const
Definition: Particle.h:155
GridEvaluatorStatus.h
vtkm::worklet::flow::RK4Integrator::RK4Integrator
VTKM_CONT RK4Integrator()=default
vtkm::worklet::flow::GridEvaluatorStatus::CheckFail
VTKM_EXEC_CONT bool CheckFail() const
Definition: GridEvaluatorStatus.h:39
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::VecVariable
A short variable-length array with maximum length.
Definition: VecVariable.h:30
vtkm::worklet::flow::RK4Integrator::Evaluator
EvaluatorType Evaluator
Definition: RK4Integrator.h:97
vtkm::worklet::flow::IntegratorStatus
Definition: IntegratorStatus.h:33
vtkm::Particle::GetTime
VTKM_EXEC_CONT vtkm::FloatDefault GetTime() const
Definition: Particle.h:141
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::flow::ExecRK4Integrator::ExecRK4Integrator
VTKM_EXEC_CONT ExecRK4Integrator(const ExecEvaluatorType &evaluator)
Definition: RK4Integrator.h:31
vtkm::Particle::Velocity
VTKM_EXEC_CONT vtkm::Vec3f Velocity(const vtkm::VecVariable< vtkm::Vec3f, 2 > &vectors, const vtkm::FloatDefault &vtkmNotUsed(length))
Definition: Particle.h:145
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::Vec< vtkm::FloatDefault, 3 >
vtkm::FloatDefault
vtkm::Float32 FloatDefault
The floating point type to use when no other precision is specified.
Definition: Types.h:198
vtkm::worklet::flow::ExecRK4Integrator::CheckStep
VTKM_EXEC IntegratorStatus CheckStep(Particle &particle, vtkm::FloatDefault stepLength, vtkm::Vec3f &velocity) const
Definition: RK4Integrator.h:37
vtkm::worklet::flow::ExecRK4Integrator
Definition: RK4Integrator.h:27
vtkm::worklet::flow::RK4Integrator::RK4Integrator
VTKM_CONT RK4Integrator(const EvaluatorType &evaluator)
Definition: RK4Integrator.h:104
vtkm::worklet::flow::RK4Integrator::PrepareForExecution
VTKM_CONT auto PrepareForExecution(vtkm::cont::DeviceAdapterId device, vtkm::cont::Token &token) const -> ExecRK4Integrator< decltype(this->Evaluator.PrepareForExecution(device, token))>
Definition: RK4Integrator.h:109
IntegratorStatus.h
vtkm::TypeTraits::ZeroInitialization
static VTKM_EXEC_CONT T ZeroInitialization()
Definition: TypeTraits.h:75
vtkm::worklet::flow::ExecRK4Integrator::Evaluator
ExecEvaluatorType Evaluator
Definition: RK4Integrator.h:90
vtkm::worklet::flow::GridEvaluatorStatus
Definition: GridEvaluatorStatus.h:23