Merge topic 'field-conversion-interp-any-field'

d26d76dea Fix construction issue for XGC arrays in topology map fetch
634847ce2 Change PointAverage to work on fields of any type
0c13917c1 Change CellAverage to work on fields of any type

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !2979
This commit is contained in:
Kenneth Moreland 2023-02-04 14:19:07 +00:00 committed by Kitware Robot
commit 763389bd4e
9 changed files with 87 additions and 107 deletions

@ -13,7 +13,9 @@ to get data from any type of array. The following filters have been
updated.
* `CleanGrid`
* `CellAverage`
* `ClipWithField`
* `ClipWithImplicitFunction`
* `Contour`
* `MIRFilter`
* `PointAverage`

@ -105,7 +105,10 @@ void TestDataSet_Explicit()
//run a basic for-each topology algorithm on this
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(subset, dataSet.GetField("pointvar"), result);
dispatcher.Invoke(
subset,
dataSet.GetField("pointvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(),
result);
//iterate same cell 4 times
vtkm::Float32 expected[4] = { 30.1667f, 30.1667f, 30.1667f, 30.1667f };
@ -139,7 +142,10 @@ void TestDataSet_Structured2D()
//run a basic for-each topology algorithm on this
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(subset, dataSet.GetField("pointvar"), result);
dispatcher.Invoke(
subset,
dataSet.GetField("pointvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(),
result);
vtkm::Float32 expected[4] = { 40.1f, 40.1f, 40.1f, 40.1f };
auto resultPortal = result.ReadPortal();
@ -172,7 +178,10 @@ void TestDataSet_Structured3D()
//run a basic for-each topology algorithm on this
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(subset, dataSet.GetField("pointvar"), result);
dispatcher.Invoke(
subset,
dataSet.GetField("pointvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(),
result);
vtkm::Float32 expected[4] = { 70.2125f, 70.2125f, 70.2125f, 70.2125f };
auto resultPortal = result.ReadPortal();

@ -227,15 +227,13 @@ struct Fetch<vtkm::exec::arg::FetchTagArrayTopologyMapIn,
const vtkm::Id offset2 = (xgcidx.Planes[1] * xgcidx.NumberOfPointsPerPlane);
using ValueType = vtkm::Vec<typename ExecObjectType::ValueType, 6>;
ValueType result;
result[0] = portal.Get(offset1 + xgcidx.PointIds[0][0]);
result[1] = portal.Get(offset1 + xgcidx.PointIds[0][1]);
result[2] = portal.Get(offset1 + xgcidx.PointIds[0][2]);
result[3] = portal.Get(offset2 + xgcidx.PointIds[1][0]);
result[4] = portal.Get(offset2 + xgcidx.PointIds[1][1]);
result[5] = portal.Get(offset2 + xgcidx.PointIds[1][2]);
return result;
return ValueType(portal.Get(offset1 + xgcidx.PointIds[0][0]),
portal.Get(offset1 + xgcidx.PointIds[0][1]),
portal.Get(offset1 + xgcidx.PointIds[0][2]),
portal.Get(offset2 + xgcidx.PointIds[1][0]),
portal.Get(offset2 + xgcidx.PointIds[1][1]),
portal.Get(offset2 + xgcidx.PointIds[1][2]));
}

@ -28,17 +28,15 @@ vtkm::cont::DataSet CellAverage::DoExecute(const vtkm::cont::DataSet& input)
}
vtkm::cont::UnknownCellSet inputCellSet = input.GetCellSet();
vtkm::cont::UnknownArrayHandle outArray;
vtkm::cont::UnknownArrayHandle inArray = field.GetData();
vtkm::cont::UnknownArrayHandle outArray = inArray.NewInstanceBasic();
auto resolveType = [&](const auto& concrete) {
using T = typename std::decay_t<decltype(concrete)>::ValueType;
vtkm::cont::ArrayHandle<T> result;
using T = typename std::decay_t<decltype(concrete)>::ValueType::ComponentType;
auto result = outArray.ExtractArrayFromComponents<T>();
this->Invoke(vtkm::worklet::CellAverage{}, inputCellSet, concrete, result);
outArray = result;
};
field.GetData()
.CastAndCallForTypesWithFloatFallback<vtkm::TypeListField, VTKM_DEFAULT_STORAGE_LIST>(
resolveType);
inArray.CastAndCallWithExtractedArray(resolveType);
std::string outputName = this->GetOutputFieldName();
if (outputName.empty())

@ -29,25 +29,23 @@ vtkm::cont::DataSet PointAverage::DoExecute(const vtkm::cont::DataSet& input)
}
vtkm::cont::UnknownCellSet cellSet = input.GetCellSet();
vtkm::cont::UnknownArrayHandle outArray;
vtkm::cont::UnknownArrayHandle inArray = field.GetData();
vtkm::cont::UnknownArrayHandle outArray = inArray.NewInstanceBasic();
auto resolveType = [&](const auto& concrete) {
using T = typename std::decay_t<decltype(concrete)>::ValueType;
using T = typename std::decay_t<decltype(concrete)>::ValueType::ComponentType;
auto result = outArray.ExtractArrayFromComponents<T>();
using SupportedCellSets =
vtkm::ListAppend<vtkm::List<vtkm::cont::CellSetExtrude>, VTKM_DEFAULT_CELL_SET_LIST>;
vtkm::cont::ArrayHandle<T> result;
this->Invoke(vtkm::worklet::PointAverage{},
cellSet.ResetCellSetList<SupportedCellSets>(),
concrete,
result);
outArray = result;
};
// TODO: Do we need to deal with XCG storage type explicitly?
// using AdditionalFieldStorage = vtkm::List<vtkm::cont::StorageTagXGCCoordinates>;
field.GetData()
.CastAndCallForTypesWithFloatFallback<vtkm::TypeListField, VTKM_DEFAULT_STORAGE_LIST>(
resolveType);
// TODO: Do we need to deal with XCG storage type (vtkm::cont::ArrayHandleXGCCoordinates)
// explicitly? Extracting from that is slow.
inArray.CastAndCallWithExtractedArray(resolveType);
std::string outputName = this->GetOutputFieldName();
if (outputName.empty())

@ -35,43 +35,25 @@ public:
{
using PointValueType = typename PointValueVecType::ComponentType;
using InVecSize =
std::integral_constant<vtkm::IdComponent, vtkm::VecTraits<PointValueType>::NUM_COMPONENTS>;
using OutVecSize =
std::integral_constant<vtkm::IdComponent, vtkm::VecTraits<OutType>::NUM_COMPONENTS>;
using SameLengthVectors = typename std::is_same<InVecSize, OutVecSize>::type;
VTKM_ASSERT(vtkm::VecTraits<PointValueType>::GetNumberOfComponents(pointValues[0]) ==
vtkm::VecTraits<OutType>::GetNumberOfComponents(average));
this->DoAverage(numPoints, pointValues, average, SameLengthVectors());
}
private:
template <typename PointValueVecType, typename OutType>
VTKM_EXEC void DoAverage(const vtkm::IdComponent& numPoints,
const PointValueVecType& pointValues,
OutType& average,
std::true_type) const
{
using OutComponentType = typename vtkm::VecTraits<OutType>::ComponentType;
OutType sum = OutType(pointValues[0]);
average = pointValues[0];
for (vtkm::IdComponent pointIndex = 1; pointIndex < numPoints; ++pointIndex)
{
// OutType constructor is for when OutType is a Vec.
// static_cast is for when OutType is a small int that gets promoted to int32.
sum = static_cast<OutType>(sum + OutType(pointValues[pointIndex]));
average += pointValues[pointIndex];
}
// OutType constructor is for when OutType is a Vec.
// static_cast is for when OutType is a small int that gets promoted to int32.
average = static_cast<OutType>(sum / OutType(static_cast<OutComponentType>(numPoints)));
}
template <typename PointValueVecType, typename OutType>
VTKM_EXEC void DoAverage(const vtkm::IdComponent& vtkmNotUsed(numPoints),
const PointValueVecType& vtkmNotUsed(pointValues),
OutType& vtkmNotUsed(average),
std::false_type) const
{
this->RaiseError("CellAverage called with mismatched Vec sizes for CellAverage.");
using VTraits = vtkm::VecTraits<OutType>;
using OutComponentType = typename VTraits::ComponentType;
const vtkm::IdComponent numComponents = VTraits::GetNumberOfComponents(average);
for (vtkm::IdComponent cIndex = 0; cIndex < numComponents; ++cIndex)
{
VTraits::SetComponent(
average,
cIndex,
static_cast<OutComponentType>(VTraits::GetComponent(average, cIndex) / numPoints));
}
}
};
}

@ -37,48 +37,26 @@ public:
OutType& average) const
{
using CellValueType = typename CellValueVecType::ComponentType;
using InVecSize =
std::integral_constant<vtkm::IdComponent, vtkm::VecTraits<CellValueType>::NUM_COMPONENTS>;
using OutVecSize =
std::integral_constant<vtkm::IdComponent, vtkm::VecTraits<OutType>::NUM_COMPONENTS>;
using SameLengthVectors = typename std::is_same<InVecSize, OutVecSize>::type;
VTKM_ASSERT(vtkm::VecTraits<CellValueType>::GetNumberOfComponents(cellValues[0]) ==
vtkm::VecTraits<OutType>::GetNumberOfComponents(average));
average = vtkm::TypeTraits<OutType>::ZeroInitialization();
if (numCells != 0)
{
this->DoAverage(numCells, cellValues, average, SameLengthVectors());
}
}
private:
template <typename CellValueVecType, typename OutType>
VTKM_EXEC void DoAverage(const vtkm::IdComponent& numCells,
const CellValueVecType& cellValues,
OutType& average,
std::true_type) const
{
using OutComponentType = typename vtkm::VecTraits<OutType>::ComponentType;
OutType sum = OutType(cellValues[0]);
average = cellValues[0];
for (vtkm::IdComponent cellIndex = 1; cellIndex < numCells; ++cellIndex)
{
// OutType constructor is for when OutType is a Vec.
// static_cast is for when OutType is a small int that gets promoted to int32.
sum = static_cast<OutType>(sum + OutType(cellValues[cellIndex]));
average += cellValues[cellIndex];
}
// OutType constructor is for when OutType is a Vec.
// static_cast is for when OutType is a small int that gets promoted to int32.
average = static_cast<OutType>(sum / OutType(static_cast<OutComponentType>(numCells)));
}
template <typename CellValueVecType, typename OutType>
VTKM_EXEC void DoAverage(const vtkm::IdComponent& vtkmNotUsed(numCells),
const CellValueVecType& vtkmNotUsed(cellValues),
OutType& vtkmNotUsed(average),
std::false_type) const
{
this->RaiseError("PointAverage called with mismatched Vec sizes for PointAverage.");
using VTraits = vtkm::VecTraits<OutType>;
using OutComponentType = typename vtkm::VecTraits<OutType>::ComponentType;
const vtkm::IdComponent numComponents = VTraits::GetNumberOfComponents(average);
for (vtkm::IdComponent compIndex = 0; compIndex < numComponents; ++compIndex)
{
VTraits::SetComponent(
average,
compIndex,
static_cast<OutComponentType>(VTraits::GetComponent(average, compIndex) / numCells));
}
}
};
}

@ -114,7 +114,10 @@ static void TestAvgPointToCell()
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::CellAverage> dispatcher;
dispatcher.Invoke(&cellset, dataSet.GetField("pointvar"), &result);
dispatcher.Invoke(
&cellset,
dataSet.GetField("pointvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(),
&result);
std::cout << "Make sure we got the right answer." << std::endl;
VTKM_TEST_ASSERT(test_equal(result.ReadPortal().Get(0), 20.1333f),
@ -126,9 +129,12 @@ static void TestAvgPointToCell()
bool exceptionThrown = false;
try
{
dispatcher.Invoke(dataSet.GetCellSet(),
dataSet.GetField("cellvar"), // should be pointvar
result);
dispatcher.Invoke(
dataSet.GetCellSet(),
dataSet.GetField("cellvar")
.GetData()
.AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(), // should be pointvar
result);
}
catch (vtkm::cont::ErrorBadValue& error)
{
@ -145,11 +151,13 @@ static void TestAvgCellToPoint()
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet1();
auto field = dataSet.GetField("cellvar");
vtkm::cont::ArrayHandle<vtkm::Float32> inArray;
field.GetData().AsArrayHandle(inArray);
vtkm::cont::ArrayHandle<vtkm::Float32> result;
vtkm::worklet::DispatcherMapTopology<vtkm::worklet::PointAverage> dispatcher;
dispatcher.Invoke(dataSet.GetCellSet(), &field, result);
dispatcher.Invoke(dataSet.GetCellSet(), &inArray, result);
std::cout << "Make sure we got the right answer." << std::endl;
VTKM_TEST_ASSERT(test_equal(result.ReadPortal().Get(0), 100.1f),
@ -161,9 +169,12 @@ static void TestAvgCellToPoint()
bool exceptionThrown = false;
try
{
dispatcher.Invoke(dataSet.GetCellSet(),
dataSet.GetField("pointvar"), // should be cellvar
result);
dispatcher.Invoke(
dataSet.GetCellSet(),
dataSet.GetField("pointvar")
.GetData()
.AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(), // should be cellvar
result);
}
catch (vtkm::cont::ErrorBadValue& error)
{

@ -147,7 +147,7 @@ static void TestAvgPointToCell()
// of the way we get cell indices. We need to make that
// part more flexible.
&cellset,
dataSet.GetField("pointvar"),
dataSet.GetField("pointvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(),
result);
std::cout << "Make sure we got the right answer." << std::endl;
@ -165,7 +165,9 @@ static void TestAvgPointToCell()
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet().ResetCellSetList(vtkm::cont::CellSetListStructured2D()),
dataSet.GetField("cellvar"), // should be pointvar
dataSet.GetField("cellvar")
.GetData()
.AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(), // should be pointvar
result);
}
catch (vtkm::cont::ErrorBadValue& error)
@ -192,7 +194,7 @@ static void TestAvgCellToPoint()
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet().ResetCellSetList(vtkm::cont::CellSetListStructured2D()),
dataSet.GetField("cellvar"),
dataSet.GetField("cellvar").GetData().AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(),
result);
std::cout << "Make sure we got the right answer." << std::endl;
@ -210,7 +212,9 @@ static void TestAvgCellToPoint()
// of the way we get cell indices. We need to make that
// part more flexible.
dataSet.GetCellSet().ResetCellSetList(vtkm::cont::CellSetListStructured2D()),
dataSet.GetField("pointvar"), // should be cellvar
dataSet.GetField("pointvar")
.GetData()
.AsArrayHandle<vtkm::cont::ArrayHandle<vtkm::Float32>>(), // should be cellvar
result);
}
catch (vtkm::cont::ErrorBadValue& error)