From 7a52fa3940af5674d62d01b9eeab734999e497f8 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Thu, 10 Nov 2016 11:32:46 -0500 Subject: [PATCH] Add in Cell Gradient for scalar fields. --- vtkm/worklet/CMakeLists.txt | 1 + vtkm/worklet/Gradient.h | 108 ++++++++++++++++ vtkm/worklet/testing/CMakeLists.txt | 1 + vtkm/worklet/testing/UnitTestCellGradient.cxx | 120 ++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 vtkm/worklet/Gradient.h create mode 100644 vtkm/worklet/testing/UnitTestCellGradient.cxx diff --git a/vtkm/worklet/CMakeLists.txt b/vtkm/worklet/CMakeLists.txt index bc463457e..6a651510c 100644 --- a/vtkm/worklet/CMakeLists.txt +++ b/vtkm/worklet/CMakeLists.txt @@ -30,6 +30,7 @@ set(headers ExternalFaces.h FieldHistogram.h FieldStatistics.h + Gradient.h KernelSplatter.h Magnitude.h MarchingCubes.h diff --git a/vtkm/worklet/Gradient.h b/vtkm/worklet/Gradient.h new file mode 100644 index 000000000..ac931a029 --- /dev/null +++ b/vtkm/worklet/Gradient.h @@ -0,0 +1,108 @@ +//============================================================================ +// 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 Sandia Corporation. +// Copyright 2014 UT-Battelle, LLC. +// Copyright 2014 Los Alamos National Security. +// +// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, +// 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. +//============================================================================ + +#ifndef vtk_m_worklet_Gradient_h +#define vtk_m_worklet_Gradient_h + +#include +#include +#include +#include +#include +#include +#include + +namespace vtkm { +namespace worklet { + +struct GradientOutTypes + : vtkm::ListTagBase< + vtkm::Vec, + vtkm::Vec + > +{ }; +struct CellGradient : vtkm::worklet::WorkletMapPointToCell +{ + typedef void ControlSignature(CellSetIn, + FieldInPoint pointCoordinates, + FieldInPoint inputField, + FieldOutCell outputField); + + typedef void ExecutionSignature(CellShape, PointCount, _2, _3, _4); + typedef _1 InputDomain; + + template + VTKM_EXEC_EXPORT void operator()(CellTagType shape, + vtkm::IdComponent pointCount, const PointCoordVecType& pointCoordinates, + const FieldInVecType& inputField, FieldOutType& outputField) const + { + //To confirm that we have the proper input and output types we need + //to verify that input type matches the output vtkm::Vec 'T' type. + //For example: + // input is float => output is vtkm::Vec + // input is vtkm::Vec => output is vtkm::Vec< vtkm::Vec< float > > + + //Grab the dimension tag for the input + using InValueType = typename FieldInVecType::ComponentType; + using InDimensionTag = typename TypeTraits::DimensionalityTag; + + //grad the dimension tag for the output component type + using OutValueType = typename VecTraits::ComponentType; + using OutDimensionTag = typename TypeTraits::DimensionalityTag; + + //Verify that input and output dimension tags match + using Matches = typename std::is_same::type; + + this->Compute(shape, pointCount, pointCoordinates, inputField, outputField, + Matches()); + } + + template + VTKM_EXEC_EXPORT void Compute(CellShapeTag shape, + vtkm::IdComponent pointCount, const PointCoordVecType& wCoords, + const FieldInVecType& field, FieldOutType& outputField, + std::true_type) const + { + vtkm::Vec center = + vtkm::exec::ParametricCoordinatesCenter(pointCount, shape, *this); + outputField = vtkm::exec::CellDerivative(field, wCoords, center, shape, *this); + } + + template + VTKM_EXEC_EXPORT void Compute(CellShapeTag, + vtkm::IdComponent, + const PointCoordVecType&, + const FieldInVecType&, + FieldOutType&, + std::false_type) const + { + //this is invalid + } +}; + +} +} // namespace vtkm::worklet + +#endif \ No newline at end of file diff --git a/vtkm/worklet/testing/CMakeLists.txt b/vtkm/worklet/testing/CMakeLists.txt index 2807dbd77..ce699adc9 100644 --- a/vtkm/worklet/testing/CMakeLists.txt +++ b/vtkm/worklet/testing/CMakeLists.txt @@ -20,6 +20,7 @@ set(unit_tests UnitTestCellAverage.cxx + UnitTestCellGradient.cxx UnitTestClipping.cxx UnitTestExternalFaces.cxx UnitTestFieldHistogram.cxx diff --git a/vtkm/worklet/testing/UnitTestCellGradient.cxx b/vtkm/worklet/testing/UnitTestCellGradient.cxx new file mode 100644 index 000000000..5d27cfe86 --- /dev/null +++ b/vtkm/worklet/testing/UnitTestCellGradient.cxx @@ -0,0 +1,120 @@ +//============================================================================ +// 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 Sandia Corporation. +// Copyright 2014 UT-Battelle, LLC. +// Copyright 2014 Los Alamos National Security. +// +// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, +// 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 + +namespace { + + +void TestCellGradientUniform2D() +{ + std::cout << "Testing CellGradient Worklet on 2D structured data" << std::endl; + + vtkm::cont::testing::MakeTestDataSet testDataSet; + vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0(); + + vtkm::cont::ArrayHandle< vtkm::Vec > result; + + vtkm::worklet::DispatcherMapTopology dispatcher; + dispatcher.Invoke(dataSet.GetCellSet(), + dataSet.GetCoordinateSystem(), + dataSet.GetField("pointvar"), + result); + + vtkm::Vec expected[2] = { {10,30,0}, {10,30,0} }; + for (int i = 0; i < 2; ++i) + { + VTKM_TEST_ASSERT( + test_equal(result.GetPortalConstControl().Get(i), expected[i]), + "Wrong result for CellGradient worklet on 2D uniform data"); + } +} + + +void TestCellGradientUniform3D() +{ + std::cout << "Testing CellGradient Worklet on 3D strucutred data" << std::endl; + + vtkm::cont::testing::MakeTestDataSet testDataSet; + vtkm::cont::DataSet dataSet = testDataSet.Make3DUniformDataSet0(); + + vtkm::cont::ArrayHandle< vtkm::Vec > result; + + vtkm::worklet::DispatcherMapTopology dispatcher; + dispatcher.Invoke(dataSet.GetCellSet(), + dataSet.GetCoordinateSystem(), + dataSet.GetField("pointvar"), + result); + + vtkm::Vec expected[4] = { {10.025,30.075,60.125}, + {10.025,30.075,60.125}, + {10.025,30.075,60.175}, + {10.025,30.075,60.175}, + }; + for (int i = 0; i < 4; ++i) + { + VTKM_TEST_ASSERT( + test_equal(result.GetPortalConstControl().Get(i), expected[i]), + "Wrong result for CellGradient worklet on 3D uniform data"); + } +} + +void TestCellGradientExplicit() +{ + std::cout << "Testing CellGradient Worklet on Explicit data" << std::endl; + + vtkm::cont::testing::MakeTestDataSet testDataSet; + vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0(); + + vtkm::cont::ArrayHandle< vtkm::Vec > result; + + vtkm::worklet::DispatcherMapTopology dispatcher; + dispatcher.Invoke(dataSet.GetCellSet(), + dataSet.GetCoordinateSystem(), + dataSet.GetField("pointvar"), + result); + + vtkm::Vec expected[2] = { {10.f,10.1f,0.0f}, {10.f,10.1f,-0.0f} }; + for (int i = 0; i < 2; ++i) + { + VTKM_TEST_ASSERT( + test_equal(result.GetPortalConstControl().Get(i), expected[i]), + "Wrong result for CellGradient worklet on 3D explicit data"); + } +} + + +void TestCellGradient() +{ + TestCellGradientUniform2D(); + TestCellGradientUniform3D(); + TestCellGradientExplicit(); +} + +} + +int UnitTestCellGradient(int, char *[]) +{ + return vtkm::cont::testing::Testing::Run(TestCellGradient); +}