VTK-m  2.0
PointGradient.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 
11 #ifndef vtk_m_worklet_gradient_PointGradient_h
12 #define vtk_m_worklet_gradient_PointGradient_h
13 
17 
18 #include <utility>
20 
21 
22 namespace vtkm
23 {
24 namespace worklet
25 {
26 namespace gradient
27 {
28 
30 {
31  using ControlSignature = void(CellSetIn,
32  WholeCellSetIn<Cell, Point>,
33  WholeArrayIn pointCoordinates,
34  WholeArrayIn inputField,
35  GradientOutputs outputFields);
36 
37  using ExecutionSignature = void(CellCount, CellIndices, WorkIndex, _2, _3, _4, _5);
38  using InputDomain = _1;
39 
40  template <typename FromIndexType,
41  typename CellSetInType,
42  typename WholeCoordinatesIn,
43  typename WholeFieldIn,
44  typename GradientOutType>
45  VTKM_EXEC void operator()(const vtkm::IdComponent& numCells,
46  const FromIndexType& cellIds,
47  const vtkm::Id& pointId,
48  const CellSetInType& geometry,
49  const WholeCoordinatesIn& pointCoordinates,
50  const WholeFieldIn& inputField,
51  GradientOutType& outputGradient) const
52  {
53  // Use optimized ThreadIndicesTopologyMap
54  using CellThreadIndices =
57 
58  using ValueType = typename WholeFieldIn::ValueType;
59  using CellShapeTag = typename CellSetInType::CellShapeTag;
60 
61  vtkm::Vec<ValueType, 3> gradient(ValueType(0.0));
62  for (vtkm::IdComponent i = 0; i < numCells; ++i)
63  {
64  const vtkm::Id cellId = cellIds[i];
65  CellThreadIndices cellIndices(cellId, cellId, 0, cellId, geometry);
66 
67  const CellShapeTag cellShape = cellIndices.GetCellShape();
68 
69  // compute the parametric coordinates for the current point
70  const auto wCoords = this->GetValues(cellIndices, pointCoordinates);
71  const auto field = this->GetValues(cellIndices, inputField);
72 
73  const vtkm::IdComponent pointIndexForCell = this->GetPointIndexForCell(cellIndices, pointId);
74 
75  this->ComputeGradient(cellShape, pointIndexForCell, wCoords, field, gradient);
76  }
77 
78  if (numCells != 0)
79  {
80  using BaseGradientType = typename vtkm::VecTraits<ValueType>::BaseComponentType;
81  const BaseGradientType invNumCells =
82  static_cast<BaseGradientType>(1.) / static_cast<BaseGradientType>(numCells);
83 
84  gradient[0] = gradient[0] * invNumCells;
85  gradient[1] = gradient[1] * invNumCells;
86  gradient[2] = gradient[2] * invNumCells;
87  }
88  outputGradient = gradient;
89  }
90 
91 private:
92  template <typename CellShapeTag,
93  typename PointCoordVecType,
94  typename FieldInVecType,
95  typename OutValueType>
96  inline VTKM_EXEC void ComputeGradient(CellShapeTag cellShape,
97  const vtkm::IdComponent& pointIndexForCell,
98  const PointCoordVecType& wCoords,
99  const FieldInVecType& field,
100  vtkm::Vec<OutValueType, 3>& gradient) const
101  {
102  vtkm::Vec3f pCoords;
103  vtkm::exec::ParametricCoordinatesPoint(
104  wCoords.GetNumberOfComponents(), pointIndexForCell, cellShape, pCoords);
105 
106  //we need to add this to a return value
107  vtkm::Vec<OutValueType, 3> pointGradient;
108  auto status = vtkm::exec::CellDerivative(field, wCoords, pCoords, cellShape, pointGradient);
109  if (status == vtkm::ErrorCode::Success)
110  {
111  gradient += pointGradient;
112  }
113  }
114 
115  template <typename ThreadIndicesType>
116  VTKM_EXEC vtkm::IdComponent GetPointIndexForCell(const ThreadIndicesType& indices,
117  vtkm::Id pointId) const
118  {
119  vtkm::IdComponent result = 0;
120  const auto& topo = indices.GetIndicesIncident();
121  for (vtkm::IdComponent i = 0; i < topo.GetNumberOfComponents(); ++i)
122  {
123  if (topo[i] == pointId)
124  {
125  result = i;
126  }
127  }
128  return result;
129  }
130 
131  template <typename ThreadIndicesType, typename WholeFieldIn>
132  VTKM_EXEC auto GetValues(const ThreadIndicesType& indices, const WholeFieldIn& in) const
133  {
134  //the current problem is that when the topology is structured
135  //we are passing in an vtkm::Id when it wants a Id2 or an Id3 that
136  //represents the flat index of the topology
139  WholeFieldIn>;
140  Fetch fetch;
141  return fetch.Load(indices, in);
142  }
143 };
144 }
145 }
146 }
147 
148 #endif
vtkm::worklet::gradient::PointGradient::GetValues
VTKM_EXEC auto GetValues(const ThreadIndicesType &indices, const WholeFieldIn &in) const
Definition: PointGradient.h:132
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::gradient::PointGradient::operator()
VTKM_EXEC void operator()(const vtkm::IdComponent &numCells, const FromIndexType &cellIds, const vtkm::Id &pointId, const CellSetInType &geometry, const WholeCoordinatesIn &pointCoordinates, const WholeFieldIn &inputField, GradientOutType &outputGradient) const
Definition: PointGradient.h:45
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::exec::arg::FetchTagArrayTopologyMapIn
Fetch tag for getting array values determined by topology connections.
Definition: FetchTagArrayTopologyMapIn.h:40
vtkm::ErrorCode::Success
@ Success
CellDerivative.h
vtkm::exec::arg::Fetch
Class for loading and storing values in thread instance.
Definition: Fetch.h:49
vtkm::VecTraits::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:80
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::worklet::gradient::PointGradient
Definition: PointGradient.h:29
GradientOutput.h
vtkm::worklet::gradient::PointGradient::InputDomain
_1 InputDomain
Definition: PointGradient.h:38
vtkm::worklet::WorkletVisitPointsWithCells::CellCount
IncidentElementCount CellCount
Definition: WorkletMapTopology.h:286
vtkm::worklet::WorkletVisitPointsWithCells
Base class for worklets that map from Cells to Points.
Definition: WorkletMapTopology.h:274
vtkm::exec::CellDerivative
VTKM_EXEC vtkm::ErrorCode CellDerivative(const FieldVecType &field, const WorldCoordType &wCoords, const vtkm::Vec< ParametricCoordType, 3 > &pcoords, CellShapeTag shape, vtkm::Vec< typename FieldVecType::ComponentType, 3 > &result)
Definition: CellDerivative.h:69
vtkm::worklet::WorkletVisitPointsWithCells::CellIndices
IncidentElementIndices CellIndices
Definition: WorkletMapTopology.h:288
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::worklet::gradient::PointGradient::ControlSignature
void(CellSetIn, WholeCellSetIn< Cell, Point >, WholeArrayIn pointCoordinates, WholeArrayIn inputField, GradientOutputs outputFields) ControlSignature
Definition: PointGradient.h:35
vtkm::exec::arg::AspectTagDefault
Aspect tag to use for default load/store of data.
Definition: AspectTagDefault.h:22
vtkm::worklet::gradient::PointGradient::GetPointIndexForCell
VTKM_EXEC vtkm::IdComponent GetPointIndexForCell(const ThreadIndicesType &indices, vtkm::Id pointId) const
Definition: PointGradient.h:116
vtkm::worklet::gradient::PointGradient::ExecutionSignature
void(CellCount, CellIndices, WorkIndex, _2, _3, _4, _5) ExecutionSignature
Definition: PointGradient.h:37
WorkletMapTopology.h
vtkm::worklet::gradient::PointGradient::ComputeGradient
VTKM_EXEC void ComputeGradient(CellShapeTag cellShape, const vtkm::IdComponent &pointIndexForCell, const PointCoordVecType &wCoords, const FieldInVecType &field, vtkm::Vec< OutValueType, 3 > &gradient) const
Definition: PointGradient.h:96
vtkm::exec::arg::ThreadIndicesTopologyMap
Container for thread indices in a topology map.
Definition: ThreadIndicesTopologyMap.h:95
ParametricCoordinates.h
vtkm::exec::arg::DefaultScatterAndMaskTag
Uses spaces optimizations when using MaskNone and ScatterIdentity.
Definition: ThreadIndicesTopologyMap.h:74
vtkm::exec::arg::WorkIndex
The ExecutionSignature tag to use to get the work index.
Definition: WorkIndex.h:39
vtkm::worklet::gradient::GradientOutputs
Definition: GradientOutput.h:305