1 //============================================================================
2 //  Copyright (c) Kitware, Inc.
3 //  All rights reserved.
4 //  See LICENSE.txt for details.
5 //  This software is distributed WITHOUT ANY WARRANTY; without even
6 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7 //  PURPOSE.  See the above copyright notice for more information.
8 //
9 //  Copyright 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 //  Copyright 2017 UT-Battelle, LLC.
11 //  Copyright 2017 Los Alamos National Security.
12 //
13 //  Under the terms of Contract DE-NA0003525 with NTESS,
14 //  the U.S. Government retains certain rights in this software.
15 //
16 //  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
17 //  Laboratory (LANL), the U.S. Government retains certain rights in
18 //  this software.
19 //============================================================================
20 #include <vtkm/cont/CellSetExplicit.h>
21 
22 #include <vtkm/cont/ArrayHandle.h>
23 #include <vtkm/cont/testing/Testing.h>
24 #include <vtkm/worklet/DispatcherMapTopology.h>
25 #include <vtkm/worklet/WorkletMapTopology.h>
26 
27 namespace
28 {
29 
30 using CellTag = vtkm::TopologyElementTagCell;
31 using PointTag = vtkm::TopologyElementTagPoint;
32 
33 const vtkm::Id numberOfPoints = 11;
34 
35 vtkm::UInt8 g_shapes[] = { static_cast<vtkm::UInt8>(vtkm::CELL_SHAPE_HEXAHEDRON),
36                            static_cast<vtkm::UInt8>(vtkm::CELL_SHAPE_PYRAMID),
37                            static_cast<vtkm::UInt8>(vtkm::CELL_SHAPE_TETRA),
38                            static_cast<vtkm::UInt8>(vtkm::CELL_SHAPE_WEDGE) };
39 
40 vtkm::IdComponent g_numIndices[] = { 8, 5, 4, 6 };
41 
42 vtkm::Id g_indexOffset[] = { 0, 8, 13, 17 };
43 
44 vtkm::Id g_connectivity[] = {
45   0, 1, 5, 4, 3, 2, 6, 7, 1, 5, 6, 2, 8, 5, 8, 10, 6, 4, 7, 9, 5, 6, 10
46 };
47 
48 template <typename T, std::size_t Length>
ArrayLength(const T (&)[Length])49 vtkm::Id ArrayLength(const T (&)[Length])
50 {
51   return static_cast<vtkm::Id>(Length);
52 }
53 
54 // all points are part of atleast 1 cell
MakeTestCellSet1()55 vtkm::cont::CellSetExplicit<> MakeTestCellSet1()
56 {
57   vtkm::cont::CellSetExplicit<> cs;
58   cs.Fill(numberOfPoints,
59           vtkm::cont::make_ArrayHandle(g_shapes, 4),
60           vtkm::cont::make_ArrayHandle(g_numIndices, 4),
61           vtkm::cont::make_ArrayHandle(g_connectivity, ArrayLength(g_connectivity)),
62           vtkm::cont::make_ArrayHandle(g_indexOffset, 4));
63   return cs;
64 }
65 
66 // some points are not part of any cell
MakeTestCellSet2()67 vtkm::cont::CellSetExplicit<> MakeTestCellSet2()
68 {
69   vtkm::cont::CellSetExplicit<> cs;
70   cs.Fill(numberOfPoints,
71           vtkm::cont::make_ArrayHandle(g_shapes + 1, 2),
72           vtkm::cont::make_ArrayHandle(g_numIndices + 1, 2),
73           vtkm::cont::make_ArrayHandle(g_connectivity + g_indexOffset[1],
74                                        g_indexOffset[3] - g_indexOffset[1]));
75   return cs;
76 }
77 
78 struct WorkletPointToCell : public vtkm::worklet::WorkletMapPointToCell
79 {
80   using ControlSignature = void(CellSetIn cellset, FieldOutCell<IdType> numPoints);
81   using ExecutionSignature = void(PointIndices, _2);
82   using InputDomain = _1;
83 
84   template <typename PointIndicesType>
operator ()__anon07436e7f0111::WorkletPointToCell85   VTKM_EXEC void operator()(const PointIndicesType& pointIndices, vtkm::Id& numPoints) const
86   {
87     numPoints = pointIndices.GetNumberOfComponents();
88   }
89 };
90 
91 struct WorkletCellToPoint : public vtkm::worklet::WorkletMapCellToPoint
92 {
93   using ControlSignature = void(CellSetIn cellset, FieldOutPoint<IdType> numCells);
94   using ExecutionSignature = void(CellIndices, _2);
95   using InputDomain = _1;
96 
97   template <typename CellIndicesType>
operator ()__anon07436e7f0111::WorkletCellToPoint98   VTKM_EXEC void operator()(const CellIndicesType& cellIndices, vtkm::Id& numCells) const
99   {
100     numCells = cellIndices.GetNumberOfComponents();
101   }
102 };
103 
TestCellSetExplicit()104 void TestCellSetExplicit()
105 {
106   vtkm::cont::CellSetExplicit<> cellset;
107   vtkm::cont::ArrayHandle<vtkm::Id> result;
108 
109   std::cout << "----------------------------------------------------\n";
110   std::cout << "Testing Case 1 (all points are part of atleast 1 cell): \n";
111   cellset = MakeTestCellSet1();
112 
113   std::cout << "\tTesting PointToCell\n";
114   vtkm::worklet::DispatcherMapTopology<WorkletPointToCell>().Invoke(cellset, result);
115 
116   VTKM_TEST_ASSERT(result.GetNumberOfValues() == cellset.GetNumberOfCells(),
117                    "result length not equal to number of cells");
118   for (vtkm::Id i = 0; i < result.GetNumberOfValues(); ++i)
119   {
120     VTKM_TEST_ASSERT(result.GetPortalConstControl().Get(i) == g_numIndices[i], "incorrect result");
121   }
122 
123   std::cout << "\tTesting CellToPoint\n";
124   vtkm::worklet::DispatcherMapTopology<WorkletCellToPoint>().Invoke(cellset, result);
125 
126   VTKM_TEST_ASSERT(result.GetNumberOfValues() == cellset.GetNumberOfPoints(),
127                    "result length not equal to number of points");
128 
129   vtkm::Id expected1[] = { 1, 2, 2, 1, 2, 4, 4, 2, 2, 1, 2 };
130   for (vtkm::Id i = 0; i < result.GetNumberOfValues(); ++i)
131   {
132     VTKM_TEST_ASSERT(result.GetPortalConstControl().Get(i) == expected1[i], "incorrect result");
133   }
134 
135   std::cout << "----------------------------------------------------\n";
136   std::cout << "Testing Case 2 (some points are not part of any cell): \n";
137   cellset = MakeTestCellSet2();
138 
139   std::cout << "\tTesting PointToCell\n";
140   vtkm::worklet::DispatcherMapTopology<WorkletPointToCell>().Invoke(cellset, result);
141 
142   VTKM_TEST_ASSERT(result.GetNumberOfValues() == cellset.GetNumberOfCells(),
143                    "result length not equal to number of cells");
144   VTKM_TEST_ASSERT(result.GetPortalConstControl().Get(0) == g_numIndices[1] &&
145                      result.GetPortalConstControl().Get(1) == g_numIndices[2],
146                    "incorrect result");
147 
148   std::cout << "\tTesting CellToPoint\n";
149   vtkm::worklet::DispatcherMapTopology<WorkletCellToPoint>().Invoke(cellset, result);
150 
151   VTKM_TEST_ASSERT(result.GetNumberOfValues() == cellset.GetNumberOfPoints(),
152                    "result length not equal to number of points");
153 
154   vtkm::Id expected2[] = { 0, 1, 1, 0, 0, 2, 2, 0, 2, 0, 1 };
155   for (vtkm::Id i = 0; i < result.GetNumberOfValues(); ++i)
156   {
157     VTKM_TEST_ASSERT(result.GetPortalConstControl().Get(i) == expected2[i], "incorrect result");
158   }
159 
160   std::cout << "----------------------------------------------------\n";
161   std::cout << "General Testing: \n";
162 
163   std::cout << "\tTesting resource releasing in CellSetExplicit\n";
164   cellset.ReleaseResourcesExecution();
165   VTKM_TEST_ASSERT(cellset.GetNumberOfCells() == ArrayLength(g_numIndices) / 2,
166                    "release execution resources should not change the number of cells");
167   VTKM_TEST_ASSERT(cellset.GetNumberOfPoints() == ArrayLength(expected2),
168                    "release execution resources should not change the number of points");
169 
170   std::cout << "\tTesting CellToPoint table caching\n";
171   cellset = MakeTestCellSet2();
172   VTKM_TEST_ASSERT(VTKM_PASS_COMMAS(cellset.HasConnectivity(PointTag{}, CellTag{})),
173                    "PointToCell table missing.");
174   VTKM_TEST_ASSERT(VTKM_PASS_COMMAS(!cellset.HasConnectivity(CellTag{}, PointTag{})),
175                    "CellToPoint table exists before PrepareForInput.");
176 
177   // Test a raw PrepareForInput call:
178   cellset.PrepareForInput(VTKM_DEFAULT_DEVICE_ADAPTER_TAG{}, CellTag{}, PointTag{});
179 
180   VTKM_TEST_ASSERT(VTKM_PASS_COMMAS(cellset.HasConnectivity(CellTag{}, PointTag{})),
181                    "CellToPoint table missing after PrepareForInput.");
182 
183   cellset.ResetConnectivity(CellTag{}, PointTag{});
184   VTKM_TEST_ASSERT(VTKM_PASS_COMMAS(!cellset.HasConnectivity(CellTag{}, PointTag{})),
185                    "CellToPoint table exists after resetting.");
186 
187   // Test a PrepareForInput wrapped inside a dispatch (See #268)
188   vtkm::worklet::DispatcherMapTopology<WorkletCellToPoint>().Invoke(cellset, result);
189   VTKM_TEST_ASSERT(VTKM_PASS_COMMAS(cellset.HasConnectivity(CellTag{}, PointTag{})),
190                    "CellToPoint table missing after CellToPoint worklet exec.");
191 }
192 
193 } // anonymous namespace
194 
UnitTestCellSetExplicit(int,char * [])195 int UnitTestCellSetExplicit(int, char* [])
196 {
197   return vtkm::cont::testing::Testing::Run(TestCellSetExplicit);
198 }
199