UnitTestWorkletMapTopologyExplicit.cxx 6.74 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//  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.
//
9
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10
//  Copyright 2014 UT-Battelle, LLC.
11
//  Copyright 2014 Los Alamos National Security.
12
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19 20 21 22 23
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================

#include <vtkm/worklet/DispatcherMapTopology.h>
#include <vtkm/worklet/WorkletMapTopology.h>

24 25 26
#include <vtkm/worklet/CellAverage.h>
#include <vtkm/worklet/PointAverage.h>

27 28
#include <vtkm/Math.h>

29
#include <vtkm/cont/DataSet.h>
30
#include <vtkm/cont/internal/DeviceAdapterTag.h>
31

32
#include <vtkm/cont/testing/MakeTestDataSet.h>
33
#include <vtkm/cont/testing/Testing.h>
34

35 36
namespace test_explicit
{
37

38
class MaxPointOrCellValue : public vtkm::worklet::WorkletMapPointToCell
39 40
{
public:
41 42
  using ControlSignature = void(FieldInCell inCells,
                                FieldInPoint inPoints,
43
                                CellSetIn topology,
44
                                FieldOutCell outCells);
45
  using ExecutionSignature = void(_1, _4, _2, PointCount, CellShape, PointIndices);
46
  using InputDomain = _3;
47

48
  VTKM_CONT
49 50
  MaxPointOrCellValue() {}

51 52 53 54 55 56 57 58 59
  template <typename InCellType,
            typename OutCellType,
            typename InPointVecType,
            typename CellShapeTag,
            typename PointIndexType>
  VTKM_EXEC void operator()(const InCellType& cellValue,
                            OutCellType& maxValue,
                            const InPointVecType& pointValues,
                            const vtkm::IdComponent& numPoints,
60 61
                            const CellShapeTag& vtkmNotUsed(type),
                            const PointIndexType& vtkmNotUsed(pointIDs)) const
62
  {
63 64 65
    //simple functor that returns the max of cellValue and pointValue
    maxValue = static_cast<OutCellType>(cellValue);
    for (vtkm::IdComponent pointIndex = 0; pointIndex < numPoints; ++pointIndex)
66
    {
67
      maxValue = vtkm::Max(maxValue, static_cast<OutCellType>(pointValues[pointIndex]));
68
    }
69
  }
70 71 72
};
}

73 74
namespace
{
75

Kenneth Moreland's avatar
Kenneth Moreland committed
76 77
static void TestMaxPointOrCell();
static void TestAvgPointToCell();
78
static void TestAvgCellToPoint();
Dave Pugmire's avatar
Dave Pugmire committed
79

80
void TestWorkletMapTopologyExplicit(vtkm::cont::DeviceAdapterId id)
81
{
82 83
  std::cout << "Testing Topology Worklet ( Explicit ) on device adapter: " << id.GetName()
            << std::endl;
84

85 86 87
  TestMaxPointOrCell();
  TestAvgPointToCell();
  TestAvgCellToPoint();
Dave Pugmire's avatar
Dave Pugmire committed
88 89
}

90
static void TestMaxPointOrCell()
Dave Pugmire's avatar
Dave Pugmire committed
91
{
92
  std::cout << "Testing MaxPointOfCell worklet" << std::endl;
93
  vtkm::cont::testing::MakeTestDataSet testDataSet;
94
  vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0();
95
  auto cellset = dataSet.GetCellSet(0).Cast<vtkm::cont::CellSetExplicit<>>();
96

97
  vtkm::cont::ArrayHandle<vtkm::Float32> result;
98

99
  vtkm::worklet::DispatcherMapTopology<::test_explicit::MaxPointOrCellValue> dispatcher;
100 101 102 103 104
  dispatcher.Invoke(
    dataSet.GetField("cellvar").GetData().ResetTypes(vtkm::TypeListTagFieldScalar()),
    dataSet.GetField("pointvar").GetData().ResetTypes(vtkm::TypeListTagFieldScalar()),
    &cellset,
    result);
105

106
  std::cout << "Make sure we got the right answer." << std::endl;
107
  VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f),
Kenneth Moreland's avatar
Kenneth Moreland committed
108
                   "Wrong result for PointToCellMax worklet");
109
  VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 100.2f),
Kenneth Moreland's avatar
Kenneth Moreland committed
110
                   "Wrong result for PointToCellMax worklet");
111 112
}

113
static void TestAvgPointToCell()
Dave Pugmire's avatar
Dave Pugmire committed
114
{
115
  std::cout << "Testing AvgPointToCell worklet" << std::endl;
116

117
  vtkm::cont::testing::MakeTestDataSet testDataSet;
118
  vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0();
119
  auto cellset = dataSet.GetCellSet();
Dave Pugmire's avatar
Dave Pugmire committed
120

121
  vtkm::cont::ArrayHandle<vtkm::Float32> result;
Dave Pugmire's avatar
Dave Pugmire committed
122

123
  vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
124
  dispatcher.Invoke(&cellset, dataSet.GetField("pointvar"), &result);
125

126
  std::cout << "Make sure we got the right answer." << std::endl;
127
  VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 20.1333f),
Kenneth Moreland's avatar
Kenneth Moreland committed
128
                   "Wrong result for PointToCellAverage worklet");
129
  VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 35.2f),
Kenneth Moreland's avatar
Kenneth Moreland committed
130
                   "Wrong result for PointToCellAverage worklet");
131

132
  std::cout << "Try to invoke with an input array of the wrong size." << std::endl;
133 134 135 136 137 138 139
  bool exceptionThrown = false;
  try
  {
    dispatcher.Invoke(dataSet.GetCellSet(),
                      dataSet.GetField("cellvar"), // should be pointvar
                      result);
  }
140
  catch (vtkm::cont::ErrorBadValue& error)
141
  {
142
    std::cout << "  Caught expected error: " << error.GetMessage() << std::endl;
143 144
    exceptionThrown = true;
  }
145
  VTKM_TEST_ASSERT(exceptionThrown, "Dispatcher did not throw expected exception.");
Dave Pugmire's avatar
Dave Pugmire committed
146 147
}

148
static void TestAvgCellToPoint()
149
{
150
  std::cout << "Testing AvgCellToPoint worklet" << std::endl;
151 152 153

  vtkm::cont::testing::MakeTestDataSet testDataSet;
  vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet1();
154
  auto field = dataSet.GetField("cellvar");
155

156
  vtkm::cont::ArrayHandle<vtkm::Float32> result;
157

158
  vtkm::worklet::DispatcherMapTopology<vtkm::worklet::PointAverage> dispatcher;
159
  dispatcher.Invoke(dataSet.GetCellSet(), &field, result);
160

161
  std::cout << "Make sure we got the right answer." << std::endl;
162
  VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f),
163
                   "Wrong result for CellToPointAverage worklet");
164
  VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 100.15f),
165
                   "Wrong result for CellToPointAverage worklet");
166

167
  std::cout << "Try to invoke with an input array of the wrong size." << std::endl;
168 169 170 171 172 173 174
  bool exceptionThrown = false;
  try
  {
    dispatcher.Invoke(dataSet.GetCellSet(),
                      dataSet.GetField("pointvar"), // should be cellvar
                      result);
  }
175
  catch (vtkm::cont::ErrorBadValue& error)
176
  {
177
    std::cout << "  Caught expected error: " << error.GetMessage() << std::endl;
178 179
    exceptionThrown = true;
  }
180
  VTKM_TEST_ASSERT(exceptionThrown, "Dispatcher did not throw expected exception.");
181 182
}

183 184
} // anonymous namespace

185
int UnitTestWorkletMapTopologyExplicit(int argc, char* argv[])
186
{
187
  return vtkm::cont::testing::Testing::RunOnDevice(TestWorkletMapTopologyExplicit, argc, argv);
188
}