vtk-m/vtkm/worklet/testing/UnitTestWorkletMapTopologyUniform.cxx

251 lines
9.1 KiB
C++
Raw Normal View History

2015-05-18 21:08:15 +00:00
//============================================================================
// 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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
2015-05-18 21:08:15 +00:00
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
2015-05-18 21:08:15 +00:00
//
// Under the terms of Contract DE-NA0003525 with NTESS,
2015-05-18 21:08:15 +00:00
// 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>
#include <vtkm/worklet/CellAverage.h>
#include <vtkm/worklet/PointAverage.h>
#include <vtkm/Math.h>
2015-05-18 21:08:15 +00:00
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/testing/Testing.h>
2015-05-18 21:08:15 +00:00
2017-05-18 14:29:41 +00:00
namespace test_uniform
{
2015-05-18 21:08:15 +00:00
class MaxPointOrCellValue : public vtkm::worklet::WorkletMapPointToCell
2015-05-18 21:08:15 +00:00
{
public:
typedef void ControlSignature(FieldInCell<Scalar> inCells,
FieldInPoint<Scalar> inPoints,
CellSetIn topology,
FieldOutCell<Scalar> outCells);
typedef void ExecutionSignature(_1, _4, _2, PointCount, CellShape, PointIndices);
2018-02-22 13:29:13 +00:00
using InputDomain = _3;
2015-05-18 21:08:15 +00:00
VTKM_CONT
2017-05-18 14:29:41 +00:00
MaxPointOrCellValue() {}
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,
2017-05-18 14:29:41 +00:00
const CellShapeTag& vtkmNotUsed(type),
const PointIndexType& vtkmNotUsed(pointIDs)) const
2015-05-18 21:08:15 +00:00
{
//simple functor that returns the max of cellValue and pointValue
maxValue = static_cast<OutCellType>(cellValue);
for (vtkm::IdComponent pointIndex = 0; pointIndex < numPoints; ++pointIndex)
{
2017-05-18 14:29:41 +00:00
maxValue = vtkm::Max(maxValue, static_cast<OutCellType>(pointValues[pointIndex]));
}
2015-05-18 21:08:15 +00:00
}
};
2017-05-18 14:29:41 +00:00
struct CheckStructuredUniformPointCoords : public vtkm::worklet::WorkletMapPointToCell
{
2017-05-18 14:29:41 +00:00
typedef void ControlSignature(CellSetIn topology, FieldInPoint<Vec3> pointCoords);
typedef void ExecutionSignature(_2);
VTKM_CONT
2017-05-18 14:29:41 +00:00
CheckStructuredUniformPointCoords() {}
2017-05-18 14:29:41 +00:00
template <vtkm::IdComponent NumDimensions>
VTKM_EXEC void operator()(
const vtkm::VecAxisAlignedPointCoordinates<NumDimensions>& vtkmNotUsed(coords)) const
{
// Success if here.
}
2017-05-18 14:29:41 +00:00
template <typename PointCoordsVecType>
VTKM_EXEC void operator()(const PointCoordsVecType& vtkmNotUsed(coords)) const
{
this->RaiseError("Got wrong point coordinates type.");
}
};
2015-05-18 21:08:15 +00:00
}
2017-05-18 14:29:41 +00:00
namespace
{
2015-05-18 21:08:15 +00:00
static void TestMaxPointOrCell();
static void TestAvgPointToCell();
static void TestAvgCellToPoint();
static void TestStructuredUniformPointCoords();
void TestWorkletMapTopologyUniform()
2015-05-18 21:08:15 +00:00
{
2018-02-22 13:29:13 +00:00
using DeviceAdapterTraits = vtkm::cont::DeviceAdapterTraits<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>;
2017-05-18 14:29:41 +00:00
std::cout << "Testing Topology Worklet ( Uniform ) on device adapter: "
<< DeviceAdapterTraits::GetName() << std::endl;
TestMaxPointOrCell();
TestAvgPointToCell();
TestAvgCellToPoint();
TestStructuredUniformPointCoords();
}
2017-05-18 14:29:41 +00:00
static void TestMaxPointOrCell()
{
2017-05-18 14:29:41 +00:00
std::cout << "Testing MaxPointOfCell worklet" << std::endl;
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0();
vtkm::cont::ArrayHandle<vtkm::Float32> result;
2017-05-18 14:29:41 +00:00
vtkm::worklet::DispatcherMapTopology<::test_uniform::MaxPointOrCellValue> dispatcher;
dispatcher.Invoke(
dataSet.GetField("cellvar"),
dataSet.GetField("pointvar"),
2017-05-18 14:29:41 +00:00
// We know that the cell set is a structured 2D grid and
// The worklet does not work with general types because
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet(0).ResetCellSetList(vtkm::cont::CellSetListTagStructured2D()),
result);
std::cout << "Make sure we got the right answer." << std::endl;
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f),
"Wrong result for MaxPointOrCell worklet");
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 200.1f),
"Wrong result for MaxPointOrCell worklet");
}
2017-05-18 14:29:41 +00:00
static void TestAvgPointToCell()
{
2017-05-18 14:29:41 +00:00
std::cout << "Testing AvgPointToCell worklet" << std::endl;
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0();
vtkm::cont::ArrayHandle<vtkm::Float32> result;
2017-05-18 14:29:41 +00:00
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(
2017-05-18 14:29:41 +00:00
// We know that the cell set is a structured 2D grid and
// The worklet does not work with general types because
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet(0).ResetCellSetList(vtkm::cont::CellSetListTagStructured2D()),
dataSet.GetField("pointvar"),
result);
std::cout << "Make sure we got the right answer." << std::endl;
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 30.1f),
"Wrong result for PointToCellAverage worklet");
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 40.1f),
"Wrong result for PointToCellAverage worklet");
2017-05-18 14:29:41 +00:00
std::cout << "Try to invoke with an input array of the wrong size." << std::endl;
bool exceptionThrown = false;
try
{
2017-05-18 14:29:41 +00:00
dispatcher.Invoke( // We know that the cell set is a structured 2D grid and
// The worklet does not work with general types because
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet().ResetCellSetList(vtkm::cont::CellSetListTagStructured2D()),
dataSet.GetField("cellvar"), // should be pointvar
result);
}
2017-05-18 14:29:41 +00:00
catch (vtkm::cont::ErrorBadValue& error)
{
2017-05-18 14:29:41 +00:00
std::cout << " Caught expected error: " << error.GetMessage() << std::endl;
exceptionThrown = true;
}
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(exceptionThrown, "Dispatcher did not throw expected exception.");
2015-05-18 21:08:15 +00:00
}
2017-05-18 14:29:41 +00:00
static void TestAvgCellToPoint()
{
2017-05-18 14:29:41 +00:00
std::cout << "Testing AvgCellToPoint worklet" << std::endl;
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0();
vtkm::cont::ArrayHandle<vtkm::Float32> result;
2017-05-18 14:29:41 +00:00
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::PointAverage> dispatcher;
dispatcher.Invoke(
2017-05-18 14:29:41 +00:00
// We know that the cell set is a structured 2D grid and
// The worklet does not work with general types because
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet(0).ResetCellSetList(vtkm::cont::CellSetListTagStructured2D()),
dataSet.GetField("cellvar"),
result);
std::cout << "Make sure we got the right answer." << std::endl;
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f),
"Wrong result for CellToPointAverage worklet");
VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 150.1f),
"Wrong result for CellToPointAverage worklet");
2017-05-18 14:29:41 +00:00
std::cout << "Try to invoke with an input array of the wrong size." << std::endl;
bool exceptionThrown = false;
try
{
2017-05-18 14:29:41 +00:00
dispatcher.Invoke( // We know that the cell set is a structured 2D grid and
// The worklet does not work with general types because
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet().ResetCellSetList(vtkm::cont::CellSetListTagStructured2D()),
dataSet.GetField("pointvar"), // should be cellvar
result);
}
2017-05-18 14:29:41 +00:00
catch (vtkm::cont::ErrorBadValue& error)
{
2017-05-18 14:29:41 +00:00
std::cout << " Caught expected error: " << error.GetMessage() << std::endl;
exceptionThrown = true;
}
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(exceptionThrown, "Dispatcher did not throw expected exception.");
}
2017-05-18 14:29:41 +00:00
static void TestStructuredUniformPointCoords()
{
2017-05-18 14:29:41 +00:00
std::cout << "Testing uniform point coordinates in structured grids" << std::endl;
vtkm::cont::testing::MakeTestDataSet testDataSet;
2017-05-18 14:29:41 +00:00
vtkm::worklet::DispatcherMapTopology<::test_uniform::CheckStructuredUniformPointCoords>
dispatcher;
vtkm::cont::DataSet dataSet3D = testDataSet.Make3DUniformDataSet0();
2017-05-18 14:29:41 +00:00
dispatcher.Invoke(dataSet3D.GetCellSet(), dataSet3D.GetCoordinateSystem());
vtkm::cont::DataSet dataSet2D = testDataSet.Make2DUniformDataSet0();
2017-05-18 14:29:41 +00:00
dispatcher.Invoke(dataSet2D.GetCellSet(), dataSet2D.GetCoordinateSystem());
}
2015-05-18 21:08:15 +00:00
} // anonymous namespace
2017-05-18 14:29:41 +00:00
int UnitTestWorkletMapTopologyUniform(int, char* [])
2015-05-18 21:08:15 +00:00
{
2017-05-18 14:29:41 +00:00
return vtkm::cont::testing::Testing::Run(TestWorkletMapTopologyUniform);
2015-05-18 21:08:15 +00:00
}