diff --git a/vtkm/cont/testing/CMakeLists.txt b/vtkm/cont/testing/CMakeLists.txt index f39164d4a..47d0fe8a2 100644 --- a/vtkm/cont/testing/CMakeLists.txt +++ b/vtkm/cont/testing/CMakeLists.txt @@ -61,6 +61,7 @@ set(unit_tests UnitTestVariantArrayHandle.cxx UnitTestArrayPortalToIterators.cxx UnitTestCellLocator.cxx + UnitTestCellSet.cxx UnitTestCellSetExplicit.cxx UnitTestCellSetPermutation.cxx UnitTestContTesting.cxx diff --git a/vtkm/cont/testing/UnitTestCellSet.cxx b/vtkm/cont/testing/UnitTestCellSet.cxx new file mode 100644 index 000000000..4320cc231 --- /dev/null +++ b/vtkm/cont/testing/UnitTestCellSet.cxx @@ -0,0 +1,187 @@ +//============================================================================ +// 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. +// +// Copyright 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS). +// Copyright 2017 UT-Battelle, LLC. +// Copyright 2017 Los Alamos National Security. +// +// Under the terms of Contract DE-NA0003525 with NTESS, +// 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + +constexpr vtkm::Id xdim = 3, ydim = 5, zdim = 7; +constexpr vtkm::Id3 BaseLinePointDimensions{ xdim, ydim, zdim }; +constexpr vtkm::Id BaseLineNumberOfPoints = xdim * ydim * zdim; +constexpr vtkm::Id BaseLineNumberOfCells = (xdim - 1) * (ydim - 1) * (zdim - 1); + +vtkm::cont::CellSetStructured<3> BaseLine{ "BaseLine" }; + +void InitializeBaseLine() +{ + BaseLine.SetPointDimensions(BaseLinePointDimensions); +} + +class BaseLineConnectivityFunctor +{ +public: + explicit BaseLineConnectivityFunctor() + { + this->Structure.SetPointDimensions(BaseLinePointDimensions); + } + + VTKM_EXEC_CONT + vtkm::Id operator()(vtkm::Id idx) const + { + auto i = idx / this->Structure.NUM_POINTS_IN_CELL; + auto c = static_cast(idx % this->Structure.NUM_POINTS_IN_CELL); + return this->Structure.GetPointsOfCell(i)[c]; + } + +private: + vtkm::internal::ConnectivityStructuredInternals<3> Structure; +}; + +using BaseLineConnectivityType = vtkm::cont::ArrayHandleImplicit; +BaseLineConnectivityType BaseLineConnectivity(BaseLineConnectivityFunctor{}, + BaseLineNumberOfCells * 8); + +auto PermutationArray = vtkm::cont::ArrayHandleCounting(0, 2, BaseLineNumberOfCells / 2); + +//----------------------------------------------------------------------------- +vtkm::cont::CellSetExplicit<> MakeCellSetExplicit() +{ + vtkm::cont::ArrayHandle shapes; + vtkm::cont::ArrayCopy(vtkm::cont::ArrayHandleConstant{ vtkm::CELL_SHAPE_HEXAHEDRON, + BaseLineNumberOfCells }, + shapes); + + vtkm::cont::ArrayHandle numIndices; + vtkm::cont::ArrayCopy( + vtkm::cont::ArrayHandleConstant{ 8, BaseLineNumberOfCells }, numIndices); + + vtkm::cont::ArrayHandle connectivity; + vtkm::cont::ArrayCopy(BaseLineConnectivity, connectivity); + + vtkm::cont::CellSetExplicit<> cellset; + cellset.Fill(BaseLineNumberOfPoints, shapes, numIndices, connectivity); + return cellset; +} + +vtkm::cont::CellSetSingleType MakeCellSetSingleType() +{ + vtkm::cont::CellSetSingleType cellset; + cellset.Fill(BaseLineNumberOfPoints, vtkm::CELL_SHAPE_HEXAHEDRON, 8, BaseLineConnectivity); + return cellset; +} + +vtkm::cont::CellSetStructured<3> MakeCellSetStructured() +{ + vtkm::cont::CellSetStructured<3> cellset; + cellset.SetPointDimensions(BaseLinePointDimensions); + return cellset; +} + +//----------------------------------------------------------------------------- +enum class IsPermutationCellSet +{ + NO = 0, + YES = 1 +}; + +void TestAgainstBaseLine(const vtkm::cont::CellSet& cellset, + IsPermutationCellSet flag = IsPermutationCellSet::NO) +{ + vtkm::internal::ConnectivityStructuredInternals<3> baseLineStructure; + baseLineStructure.SetPointDimensions(BaseLinePointDimensions); + + VTKM_TEST_ASSERT(cellset.GetNumberOfPoints() == BaseLineNumberOfPoints, "Wrong number of points"); + + vtkm::Id numCells = cellset.GetNumberOfCells(); + vtkm::Id expectedNumCell = (flag == IsPermutationCellSet::NO) + ? BaseLineNumberOfCells + : PermutationArray.GetNumberOfValues(); + VTKM_TEST_ASSERT(numCells == expectedNumCell, "Wrong number of cells"); + + for (vtkm::Id i = 0; i < numCells; ++i) + { + VTKM_TEST_ASSERT(cellset.GetCellShape(i) == vtkm::CELL_SHAPE_HEXAHEDRON, "Wrong shape"); + VTKM_TEST_ASSERT(cellset.GetNumberOfPointsInCell(i) == 8, "Wrong number of points-of-cell"); + + vtkm::Id baseLineCellId = + (flag == IsPermutationCellSet::YES) ? PermutationArray.GetPortalConstControl().Get(i) : i; + auto baseLinePointIds = baseLineStructure.GetPointsOfCell(baseLineCellId); + + vtkm::Id pointIds[8]; + cellset.GetCellPointIds(i, pointIds); + for (int j = 0; j < 8; ++j) + { + VTKM_TEST_ASSERT(pointIds[j] == baseLinePointIds[j], "Wrong points-of-cell point id"); + } + } +} + +void RunTests(const vtkm::cont::CellSet& cellset, + IsPermutationCellSet flag = IsPermutationCellSet::NO) +{ + TestAgainstBaseLine(cellset, flag); + auto deepcopy = cellset.CreateNewInstance(); + deepcopy->DeepCopy(&cellset); + TestAgainstBaseLine(*deepcopy, flag); +} + +void TestCellSet() +{ + InitializeBaseLine(); + + std::cout << "Testing CellSetExplicit\n"; + auto csExplicit = MakeCellSetExplicit(); + RunTests(csExplicit); + std::cout << "Testing CellSetPermutation of CellSetExplicit\n"; + RunTests(vtkm::cont::make_CellSetPermutation(PermutationArray, csExplicit), + IsPermutationCellSet::YES); + + std::cout << "Testing CellSetSingleType\n"; + auto csSingle = MakeCellSetSingleType(); + RunTests(csSingle); + std::cout << "Testing CellSetPermutation of CellSetSingleType\n"; + RunTests(vtkm::cont::make_CellSetPermutation(PermutationArray, csSingle), + IsPermutationCellSet::YES); + + std::cout << "Testing CellSetStructured\n"; + auto csStructured = MakeCellSetStructured(); + RunTests(csStructured); + std::cout << "Testing CellSetPermutation of CellSetStructured\n"; + RunTests(vtkm::cont::make_CellSetPermutation(PermutationArray, csStructured), + IsPermutationCellSet::YES); +} + +} // anonymous namespace + +//----------------------------------------------------------------------------- +int UnitTestCellSet(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(TestCellSet, argc, argv); +}