diff --git a/data/sample.cosmotools b/data/sample.cosmotools new file mode 100644 index 000000000..dc48f003c --- /dev/null +++ b/data/sample.cosmotools @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9dc63465d864ec7a4546f1d006ca0153a5bb4c78fd4a42d16ad1177dabc70d75 +size 80263 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3129ad0f3..c4c30ad89 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -25,9 +25,10 @@ set(CMAKE_PREFIX_PATH ${VTKm_BINARY_DIR}/${VTKm_INSTALL_CONFIG_DIR}) add_subdirectory(clipping) add_subdirectory(contour_tree) +add_subdirectory(cosmotools) add_subdirectory(demo) add_subdirectory(dynamic_dispatcher) -add_subdirectory(game_of_life) +#add_subdirectory(game_of_life) add_subdirectory(hello_world) add_subdirectory(isosurface) add_subdirectory(multi_backend) diff --git a/examples/cosmotools/CMakeLists.txt b/examples/cosmotools/CMakeLists.txt new file mode 100644 index 000000000..5c6c7141c --- /dev/null +++ b/examples/cosmotools/CMakeLists.txt @@ -0,0 +1,62 @@ +##============================================================================= +## +## 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 2015 Sandia Corporation. +## Copyright 2015 UT-Battelle, LLC. +## Copyright 2015 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. +## +##============================================================================= +#Find the VTK-m package +find_package(VTKm REQUIRED QUIET + OPTIONAL_COMPONENTS Serial CUDA TBB + ) + +add_executable(CosmoCenterFinder_SERIAL CosmoCenterFinder.cxx) +target_include_directories(CosmoCenterFinder_SERIAL PRIVATE ${VTKm_INCLUDE_DIRS}) +target_link_libraries(CosmoCenterFinder_SERIAL PRIVATE ${VTKm_LIBRARIES}) +target_compile_options(CosmoCenterFinder_SERIAL PRIVATE ${VTKm_COMPILE_OPTIONS}) + +add_executable(CosmoHaloFinder_SERIAL CosmoHaloFinder.cxx) +target_include_directories(CosmoHaloFinder_SERIAL PRIVATE ${VTKm_INCLUDE_DIRS}) +target_link_libraries(CosmoHaloFinder_SERIAL PRIVATE ${VTKm_LIBRARIES}) +target_compile_options(CosmoHaloFinder_SERIAL PRIVATE ${VTKm_COMPILE_OPTIONS}) + +if(VTKm_CUDA_FOUND) + # Cuda compiles do not respect target_include_directories + cuda_include_directories(${VTKm_INCLUDE_DIRS}) + + cuda_add_executable(CosmoCenterFinder_CUDA CosmoCenterFinder.cu) + target_include_directories(CosmoCenterFinder_CUDA PRIVATE ${VTKm_INCLUDE_DIRS}) + target_link_libraries(CosmoCenterFinder_CUDA PRIVATE ${VTKm_LIBRARIES}) + target_compile_options(CosmoCenterFinder_CUDA PRIVATE ${VTKm_COMPILE_OPTIONS}) + + cuda_add_executable(CosmoHaloFinder_CUDA CosmoHaloFinder.cu) + target_include_directories(CosmoHaloFinder_CUDA PRIVATE ${VTKm_INCLUDE_DIRS}) + target_link_libraries(CosmoHaloFinder_CUDA PRIVATE ${VTKm_LIBRARIES}) + target_compile_options(CosmoHaloFinder_CUDA PRIVATE ${VTKm_COMPILE_OPTIONS}) +endif() + +if(VTKm_TBB_FOUND) + add_executable(CosmoCenterFinder_TBB CosmoCenterFinderTBB.cxx) + target_include_directories(CosmoCenterFinder_TBB PRIVATE ${VTKm_INCLUDE_DIRS}) + target_link_libraries(CosmoCenterFinder_TBB PRIVATE ${VTKm_LIBRARIES}) + target_compile_options(CosmoCenterFinder_TBB PRIVATE PRIVATE ${VTKm_COMPILE_OPTIONS}) + + add_executable(CosmoHaloFinder_TBB CosmoHaloFinderTBB.cxx) + target_include_directories(CosmoHaloFinder_TBB PRIVATE ${VTKm_INCLUDE_DIRS}) + target_link_libraries(CosmoHaloFinder_TBB PRIVATE ${VTKm_LIBRARIES}) + target_compile_options(CosmoHaloFinder_TBB PRIVATE PRIVATE ${VTKm_COMPILE_OPTIONS}) +endif() diff --git a/examples/cosmotools/CosmoCenterFinder.cu b/examples/cosmotools/CosmoCenterFinder.cu new file mode 100644 index 000000000..dce3d0147 --- /dev/null +++ b/examples/cosmotools/CosmoCenterFinder.cu @@ -0,0 +1,23 @@ +//============================================================================ +// 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. +//============================================================================ + +#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_CUDA + +#include "CosmoCenterFinder.cxx" diff --git a/examples/cosmotools/CosmoCenterFinder.cxx b/examples/cosmotools/CosmoCenterFinder.cxx new file mode 100644 index 000000000..97ee9d194 --- /dev/null +++ b/examples/cosmotools/CosmoCenterFinder.cxx @@ -0,0 +1,138 @@ +//============================================================================ +// 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 +#include +#include + +#include + +#include +#include +#include +#include +#include + +typedef vtkm::Vec FloatVec3; + +void TestCosmoCenterFinder(const char* fileName) +{ + std::cout << std::endl + << "Testing Cosmology MBP Center Finder Filter on one halo " << fileName << std::endl; + + // Open the file for reading + std::ifstream inFile(fileName); + if (inFile.fail()) + { + std::cout << "File does not exist " << fileName << std::endl; + return; + } + + // Read in number of particles and locations + int nParticles; + inFile >> nParticles; + + float* xLocation = new float[nParticles]; + float* yLocation = new float[nParticles]; + float* zLocation = new float[nParticles]; + std::cout << "Running MBP on " << nParticles << std::endl; + + for (vtkm::Id p = 0; p < nParticles; p++) + { + inFile >> xLocation[p] >> yLocation[p] >> zLocation[p]; + } + + vtkm::cont::ArrayHandle xLocArray = + vtkm::cont::make_ArrayHandle(xLocation, nParticles); + vtkm::cont::ArrayHandle yLocArray = + vtkm::cont::make_ArrayHandle(yLocation, nParticles); + vtkm::cont::ArrayHandle zLocArray = + vtkm::cont::make_ArrayHandle(zLocation, nParticles); + + // Output MBP particleId pairs array + vtkm::Pair nxnResult; + vtkm::Pair mxnResult; + + vtkm::cont::Timer total; + vtkm::cont::Timer timer; + + // Create the worklet and run it + vtkm::Float32 particleMass = 1.08413e+09f; + + vtkm::worklet::CosmoTools cosmoTools; + cosmoTools.RunMBPCenterFinderNxN( + xLocArray, yLocArray, zLocArray, nParticles, particleMass, nxnResult, DeviceAdapter()); + vtkm::Float64 nxnTime = timer.GetElapsedTime(); + + std::cout << "**** NxN MPB = " << nxnResult.first << " potential = " << nxnResult.second + << std::endl; + std::cout << "**** Time for NxN: " << nxnTime << std::endl; + + timer.Reset(); + cosmoTools.RunMBPCenterFinderMxN( + xLocArray, yLocArray, zLocArray, nParticles, particleMass, mxnResult, DeviceAdapter()); + vtkm::Float64 estTime = timer.GetElapsedTime(); + + std::cout << "**** MxN MPB = " << mxnResult.first << " potential = " << mxnResult.second + << std::endl; + std::cout << "**** Time for MxN: " << estTime << std::endl; + + if (nxnResult.first == mxnResult.first) + std::cout << "FOUND CORRECT PARTICLE " << mxnResult.first << " with potential " + << nxnResult.second << std::endl; + else + std::cout << "ERROR DID NOT FIND SAME PARTICLE" << std::endl; + + xLocArray.ReleaseResources(); + yLocArray.ReleaseResources(); + zLocArray.ReleaseResources(); + + delete[] xLocation; + delete[] yLocation; + delete[] zLocation; +} + +///////////////////////////////////////////////////////////////////// +// +// Form of the input file in ASCII +// Line 1: number of particles in the file +// Line 2+: (float) xLoc (float) yLoc (float) zLoc +// +// CosmoCenterFinder data.cosmotools +// +///////////////////////////////////////////////////////////////////// + +int main(int argc, char* argv[]) +{ + if (argc < 2) + { + std::cout << "Usage: " << std::endl << "$ " << argv[0] << " " << std::endl; + return 1; + } + + std::cout << "Device Adapter Name: " << vtkm::cont::DeviceAdapterTraits::GetName() + << std::endl; + + TestCosmoCenterFinder(argv[1]); + + return 0; +} diff --git a/examples/cosmotools/CosmoCenterFinderTBB.cxx b/examples/cosmotools/CosmoCenterFinderTBB.cxx new file mode 100644 index 000000000..bdd74ad4b --- /dev/null +++ b/examples/cosmotools/CosmoCenterFinderTBB.cxx @@ -0,0 +1,23 @@ +//============================================================================ +// 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. +//============================================================================ + +#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_TBB + +#include "CosmoCenterFinder.cxx" diff --git a/examples/cosmotools/CosmoHaloFinder.cu b/examples/cosmotools/CosmoHaloFinder.cu new file mode 100644 index 000000000..71ff87396 --- /dev/null +++ b/examples/cosmotools/CosmoHaloFinder.cu @@ -0,0 +1,23 @@ +//============================================================================ +// 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. +//============================================================================ + +#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_CUDA + +#include "CosmoHaloFinder.cxx" diff --git a/examples/cosmotools/CosmoHaloFinder.cxx b/examples/cosmotools/CosmoHaloFinder.cxx new file mode 100644 index 000000000..6b4b62ade --- /dev/null +++ b/examples/cosmotools/CosmoHaloFinder.cxx @@ -0,0 +1,132 @@ +//============================================================================ +// 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 +#include +#include + +#include + +#include +#include +#include +#include +#include + +typedef vtkm::Vec FloatVec3; + +void TestCosmoHaloFinder(const char* fileName) +{ + std::cout << std::endl + << "Testing Cosmology Halo Finder and MBP Center Finder " << fileName << std::endl; + + // Open the file for reading + std::ifstream inFile(fileName); + if (inFile.fail()) + { + std::cout << "File does not exist " << fileName << std::endl; + return; + } + + // Read in number of particles and locations + int nParticles; + inFile >> nParticles; + + float* xLocation = new float[nParticles]; + float* yLocation = new float[nParticles]; + float* zLocation = new float[nParticles]; + std::cout << "Running Halo Finder on " << nParticles << std::endl; + + for (vtkm::Id p = 0; p < nParticles; p++) + { + inFile >> xLocation[p] >> yLocation[p] >> zLocation[p]; + } + + vtkm::cont::ArrayHandle xLocArray = + vtkm::cont::make_ArrayHandle(xLocation, nParticles); + vtkm::cont::ArrayHandle yLocArray = + vtkm::cont::make_ArrayHandle(yLocation, nParticles); + vtkm::cont::ArrayHandle zLocArray = + vtkm::cont::make_ArrayHandle(zLocation, nParticles); + + // Output halo id, mbp id and min potential per particle + vtkm::cont::ArrayHandle resultHaloId; + vtkm::cont::ArrayHandle resultMBP; + vtkm::cont::ArrayHandle resultPot; + + vtkm::cont::Timer total; + vtkm::cont::Timer timer; + + // Create the worklet and run it + vtkm::Id minHaloSize = 20; + vtkm::Float32 linkingLength = 0.2f; + vtkm::Float32 particleMass = 1.08413e+09f; + + vtkm::worklet::CosmoTools cosmoTools; + cosmoTools.RunHaloFinder(xLocArray, + yLocArray, + zLocArray, + nParticles, + particleMass, + minHaloSize, + linkingLength, + resultHaloId, + resultMBP, + resultPot, + DeviceAdapter()); + vtkm::Float64 haloTime = timer.GetElapsedTime(); + std::cout << "**** Time for HaloFinder: " << haloTime << std::endl; + + xLocArray.ReleaseResources(); + yLocArray.ReleaseResources(); + zLocArray.ReleaseResources(); + + delete[] xLocation; + delete[] yLocation; + delete[] zLocation; +} + +///////////////////////////////////////////////////////////////////// +// +// Form of the input file in ASCII +// Line 1: number of particles in the file +// Line 2+: (float) xLoc (float) yLoc (float) zLoc +// +// CosmoHaloFinder data.cosmotools +// +///////////////////////////////////////////////////////////////////// + +int main(int argc, char* argv[]) +{ + if (argc < 2) + { + std::cout << "Usage: " << std::endl << "$ " << argv[0] << " " << std::endl; + return 1; + } + + std::cout << "Device Adapter Name: " << vtkm::cont::DeviceAdapterTraits::GetName() + << std::endl; + + TestCosmoHaloFinder(argv[1]); + + return 0; +} diff --git a/examples/cosmotools/CosmoHaloFinderTBB.cxx b/examples/cosmotools/CosmoHaloFinderTBB.cxx new file mode 100644 index 000000000..31f8b7360 --- /dev/null +++ b/examples/cosmotools/CosmoHaloFinderTBB.cxx @@ -0,0 +1,23 @@ +//============================================================================ +// 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. +//============================================================================ + +#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_TBB + +#include "CosmoHaloFinder.cxx" diff --git a/vtkm/worklet/CMakeLists.txt b/vtkm/worklet/CMakeLists.txt index da5611f8f..5e64f4b47 100644 --- a/vtkm/worklet/CMakeLists.txt +++ b/vtkm/worklet/CMakeLists.txt @@ -24,6 +24,7 @@ set(headers CellDeepCopy.h Clip.h ContourTreeUniform.h + CosmoTools.h DispatcherMapField.h DispatcherMapTopology.h DispatcherPointNeighborhood.h @@ -70,6 +71,7 @@ set(headers #----------------------------------------------------------------------------- add_subdirectory(internal) add_subdirectory(contourtree) +add_subdirectory(cosmotools) add_subdirectory(gradient) add_subdirectory(splatkernels) add_subdirectory(spatialstructure) diff --git a/vtkm/worklet/CosmoTools.h b/vtkm/worklet/CosmoTools.h new file mode 100644 index 000000000..a6f719f14 --- /dev/null +++ b/vtkm/worklet/CosmoTools.h @@ -0,0 +1,149 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtk_m_worklet_CosmoTools_h +#define vtk_m_worklet_CosmoTools_h + +#include +#include +#include + +#include +#include +#include + +namespace vtkm +{ +namespace worklet +{ + +class CosmoTools +{ +public: + // Run the halo finder and then the NxN MBP center finder + template + void RunHaloFinder(vtkm::cont::ArrayHandle& xLocation, + vtkm::cont::ArrayHandle& yLocation, + vtkm::cont::ArrayHandle& zLocation, + const vtkm::Id nParticles, + const FieldType particleMass, + const vtkm::Id minHaloSize, + const FieldType linkingLen, + vtkm::cont::ArrayHandle& resultHaloId, + vtkm::cont::ArrayHandle& resultMBP, + vtkm::cont::ArrayHandle& resultPot, + const DeviceAdapter&) + { + // Constructor gets particle locations, particle mass and min halo size + cosmotools::CosmoTools cosmo( + nParticles, particleMass, minHaloSize, linkingLen, xLocation, yLocation, zLocation); + + // Find the halos within the particles and the MBP center of each halo + cosmo.HaloFinder(resultHaloId, resultMBP, resultPot); + } + + // Run MBP on a single halo of particles using the N^2 algorithm + template + void RunMBPCenterFinderNxN(vtkm::cont::ArrayHandle xLocation, + vtkm::cont::ArrayHandle yLocation, + vtkm::cont::ArrayHandle zLocation, + const vtkm::Id nParticles, + const FieldType particleMass, + vtkm::Pair& nxnResult, + const DeviceAdapter&) + { + // Constructor gets particle locations and particle mass + cosmotools::CosmoTools cosmo( + nParticles, particleMass, xLocation, yLocation, zLocation); + + // Most Bound Particle N x N algorithm + FieldType nxnPotential; + vtkm::Id nxnMBP = cosmo.MBPCenterFinderNxN(&nxnPotential); + + nxnResult.first = nxnMBP; + nxnResult.second = nxnPotential; + } + + // Run MBP on a single halo of particles using MxN estimation algorithm + template + void RunMBPCenterFinderMxN(vtkm::cont::ArrayHandle xLocation, + vtkm::cont::ArrayHandle yLocation, + vtkm::cont::ArrayHandle zLocation, + const vtkm::Id nParticles, + const FieldType particleMass, + vtkm::Pair& mxnResult, + const DeviceAdapter&) + { + // Constructor gets particle locations and particle mass + cosmotools::CosmoTools cosmo( + nParticles, particleMass, xLocation, yLocation, zLocation); + + // Most Bound Particle M x N algorithm with binning estimates + FieldType mxnPotential; + vtkm::Id mxnMBP = cosmo.MBPCenterFinderMxN(&mxnPotential); + + mxnResult.first = mxnMBP; + mxnResult.second = mxnPotential; + } +}; +} +} // namespace vtkm::worklet + +#endif // vtk_m_worklet_CosmoTools_h diff --git a/vtkm/worklet/cosmotools/CMakeLists.txt b/vtkm/worklet/cosmotools/CMakeLists.txt new file mode 100644 index 000000000..e3f4ef4a5 --- /dev/null +++ b/vtkm/worklet/cosmotools/CMakeLists.txt @@ -0,0 +1,45 @@ +##============================================================================ +## 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 2016 Sandia Corporation. +## Copyright 2016 UT-Battelle, LLC. +## Copyright 2016 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. +##============================================================================ + +set(headers + CosmoTools.h + CosmoToolsCenterFinder.h + CosmoToolsHaloFinder.h + ComputeBins.h + ComputeBinIndices.h + ComputeBinRange.h + ComputeNeighborBins.h + ComputePotential.h + ComputePotentialBin.h + ComputePotentialNeighbors.h + ComputePotentialNxN.h + ComputePotentialMxN.h + ComputePotentialOnCandidates.h + EqualsMinimumPotential.h + GraftParticles.h + IsStar.h + MarkActiveNeighbors.h + PointerJump.h + SetCandidateParticles.h + ValidHalo.h + ) + +#----------------------------------------------------------------------------- +vtkm_declare_headers(${headers}) diff --git a/vtkm/worklet/cosmotools/ComputeBinIndices.h b/vtkm/worklet/cosmotools/ComputeBinIndices.h new file mode 100644 index 000000000..731c37c02 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputeBinIndices.h @@ -0,0 +1,109 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_bin_indices_h +#define vtkm_worklet_cosmotools_compute_bin_indices_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet to compute the x,y,z index for every bin +template +class ComputeBinIndices : public vtkm::worklet::WorkletMapField +{ +public: + typedef void ControlSignature(FieldIn binId, // (input) bin Id + FieldOut binX, // (output) bin indices + FieldOut binY, + FieldOut binZ); + typedef void ExecutionSignature(_1, _2, _3, _4); + typedef _1 InputDomain; + + vtkm::Id numBinsX, numBinsY, numBinsZ; // Number of bins each dimension + + // Constructor + VTKM_EXEC_CONT + ComputeBinIndices(vtkm::Id NX, vtkm::Id NY, vtkm::Id NZ) + : numBinsX(NX) + , numBinsY(NY) + , numBinsZ(NZ) + { + } + + VTKM_EXEC + void operator()(const vtkm::Id& bin, vtkm::Id& xbin, vtkm::Id& ybin, vtkm::Id& zbin) const + { + xbin = bin % numBinsX; + ybin = (bin / numBinsX) % numBinsY; + zbin = bin / (numBinsX * numBinsY); + } +}; // ComputeBinIndices +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputeBinRange.h b/vtkm/worklet/cosmotools/ComputeBinRange.h new file mode 100644 index 000000000..07a19c040 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputeBinRange.h @@ -0,0 +1,111 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_bin_range_h +#define vtkm_worklet_cosmotools_compute_bin_range_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the range of bins for any particle +// In 3D there will be 9 "left" neighbors which start 3 consecutive bins = 27 +class ComputeBinRange : public vtkm::worklet::WorkletMapField +{ +public: + typedef void ControlSignature(FieldIn leftNeighbor, // (input) left neighbor id + FieldOut rightNeighbor); // (output) right neighbor id + typedef _2 ExecutionSignature(_1); + typedef _1 InputDomain; + + vtkm::Id xNum; + + // Constructor + VTKM_EXEC_CONT + ComputeBinRange(vtkm::Id XNum) + : xNum(XNum) + { + } + + VTKM_EXEC + vtkm::Id operator()(const vtkm::Id& leftNeighbor) const + { + vtkm::Id rightNeighbor = leftNeighbor; + const vtkm::Id xbin = leftNeighbor % xNum; + if (xbin == xNum - 1) + ; + else if (xbin == xNum - 2) + rightNeighbor += 1; + else + rightNeighbor += 2; + return rightNeighbor; + } +}; // ComputeBinRange +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputeBins.h b/vtkm/worklet/cosmotools/ComputeBins.h new file mode 100644 index 000000000..7bd5abdf9 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputeBins.h @@ -0,0 +1,146 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_bins__h +#define vtkm_worklet_cosmotools_compute_bins__h + +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the bin id for every particle in domain +template +class ComputeBins : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn xLoc, // (input) x location in halo + FieldIn yLoc, // (input) y location in halo + FieldIn zLoc, // (input) z location in halo + FieldOut binId); // (output) bin Id + typedef _4 ExecutionSignature(_1, _2, _3); + typedef _1 InputDomain; + + T xMin, xMax, yMin, yMax, zMin, zMax; + vtkm::Id xNum, yNum, zNum; + + // Constructor + VTKM_EXEC_CONT + ComputeBins(T XMin, + T XMax, + T YMin, + T YMax, + T ZMin, + T ZMax, + vtkm::Id XNum, + vtkm::Id YNum, + vtkm::Id ZNum) + : xMin(XMin) + , xMax(XMax) + , yMin(YMin) + , yMax(YMax) + , zMin(ZMin) + , zMax(ZMax) + , xNum(XNum) + , yNum(YNum) + , zNum(ZNum) + { + } + + VTKM_EXEC + vtkm::Id operator()(const T& xLoc, const T& yLoc, const T& zLoc) const + { + vtkm::Id xbin = 0; + if (xNum > 1) + { + xbin = static_cast((static_cast(xNum) * (xLoc - xMin)) / (xMax - xMin)); + if (xbin >= xNum) + xbin = xNum - 1; + } + vtkm::Id ybin = 0; + if (yNum > 1) + { + ybin = static_cast((static_cast(yNum) * (yLoc - yMin)) / (yMax - yMin)); + if (ybin >= yNum) + ybin = yNum - 1; + } + vtkm::Id zbin = 0; + if (zNum > 1) + { + zbin = static_cast((static_cast(zNum) * (zLoc - zMin)) / (zMax - zMin)); + if (zbin >= zNum) + zbin = zNum - 1; + } + return (xbin + ybin * xNum + zbin * xNum * yNum); + } +}; // ComputeBins +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputeNeighborBins.h b/vtkm/worklet/cosmotools/ComputeNeighborBins.h new file mode 100644 index 000000000..0a45df7a5 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputeNeighborBins.h @@ -0,0 +1,132 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_neighbor_bins_h +#define vtkm_worklet_cosmotools_compute_neighbor_bins_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the left neighbor bin id for every particle in domain +// In 3D there will be 9 "left" neighbors which start 3 consecutive bins = 27 +class ComputeNeighborBins : public vtkm::worklet::WorkletMapField +{ +public: + typedef void ControlSignature(FieldIn partIndx, + FieldIn binId, // (input) bin Id + WholeArrayOut leftNeighbor); // (output) neighbor Id + typedef void ExecutionSignature(_1, _2, _3); + typedef _1 InputDomain; + + vtkm::Id xNum, yNum, zNum; + vtkm::Id NUM_NEIGHBORS; + + // Constructor + VTKM_EXEC_CONT + ComputeNeighborBins(vtkm::Id XNum, vtkm::Id YNum, vtkm::Id ZNum, vtkm::Id NumNeighbors) + : xNum(XNum) + , yNum(YNum) + , zNum(ZNum) + , NUM_NEIGHBORS(NumNeighbors) + { + } + + template + VTKM_EXEC void operator()(const vtkm::Id& i, + const vtkm::Id& binId, + OutFieldPortalType& leftNeighbor) const + { + const vtkm::Id xbin = binId % xNum; + const vtkm::Id ybin = (binId / xNum) % yNum; + const vtkm::Id zbin = binId / (xNum * yNum); + + vtkm::Id cnt = 0; + for (vtkm::Id z = zbin - 1; z <= zbin + 1; z++) + { + for (vtkm::Id y = ybin - 1; y <= ybin + 1; y++) + { + if ((y >= 0) && (y < yNum) && (z >= 0) && (z < zNum)) + { + if (xbin - 1 >= 0) + leftNeighbor.Set((NUM_NEIGHBORS * i + cnt), (xbin - 1) + y * xNum + z * xNum * yNum); + else + leftNeighbor.Set((NUM_NEIGHBORS * i + cnt), xbin + y * xNum + z * xNum * yNum); + } + else + { + leftNeighbor.Set((NUM_NEIGHBORS * i + cnt), -1); + } + cnt++; + } + } + } +}; // ComputeNeighborBins +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputePotential.h b/vtkm/worklet/cosmotools/ComputePotential.h new file mode 100644 index 000000000..f6fd31416 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputePotential.h @@ -0,0 +1,136 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_potential_h +#define vtkm_worklet_cosmotools_compute_potential_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the potential for a particle +template +class ComputePotential : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature( + FieldIn index, // (input) particle Id + WholeArrayIn partId, // (input) first particle in halo + WholeArrayIn xLoc, // (input) x location in domain + WholeArrayIn yLoc, // (input) y location in domain + WholeArrayIn zLoc, // (input) z location in domain + WholeArrayIn firstParticleId, // (input) first particle in halo + WholeArrayIn lastParticleId, // (input) last particle in halo + FieldOut potential); // (output) bin ID + typedef _8 ExecutionSignature(_1, _2, _3, _4, _5, _6, _7); + typedef _1 InputDomain; + + T mass; + + // Constructor + VTKM_EXEC_CONT + ComputePotential(T Mass) + : mass(Mass) + { + } + + template + VTKM_EXEC T operator()(const vtkm::Id& i, + const InIdPortalType& partId, + const InFieldPortalType& xLoc, + const InFieldPortalType& yLoc, + const InFieldPortalType& zLoc, + const InIdPortalType& firstParticleId, + const PermutePortalType& lastParticleId) const + { + // Worklet index is into array of particle ids sorted by bin + // Current particle and other particles retrieved from partId array + vtkm::Id iId = partId.Get(i); + T potential = 0.0f; + + // first and last particle are arranged by sorted bin id so they match index and not the partId + for (vtkm::Id j = firstParticleId.Get(i); j <= lastParticleId.Get(i); j++) + { + vtkm::Id jId = partId.Get(j); + T xDist = xLoc.Get(iId) - xLoc.Get(jId); + T yDist = yLoc.Get(iId) - yLoc.Get(jId); + T zDist = zLoc.Get(iId) - zLoc.Get(jId); + T r = vtkm::Sqrt((xDist * xDist) + (yDist * yDist) + (zDist * zDist)); + if ((i != j) && (fabs(r) > 0.00000000001f)) + potential -= mass / r; + } + return potential; + } +}; // ComputePotential +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputePotentialBin.h b/vtkm/worklet/cosmotools/ComputePotentialBin.h new file mode 100644 index 000000000..e3662d8f3 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputePotentialBin.h @@ -0,0 +1,154 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_potential_bin_h +#define vtkm_worklet_cosmotools_compute_potential_bin_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the potential for a bin in one halo +template +class ComputePotentialBin : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn binId, // (input) bin Id + WholeArrayIn binCount, // (input) particles per bin + WholeArrayIn binX, // (input) x index in bin + WholeArrayIn binY, // (input) y index in bin + WholeArrayIn binZ, // (input) z index in bin + FieldInOut bestPot, // (output) best potential estimate + FieldInOut worstPot); // (output) worst potential estimate + typedef void ExecutionSignature(_1, _2, _3, _4, _5, _6, _7); + typedef _1 InputDomain; + + vtkm::Id nBins; // Number of bins + T mass; // Particle mass + T linkLen; // Linking length is side of bin + + // Constructor + VTKM_EXEC_CONT + ComputePotentialBin(vtkm::Id N, T Mass, T LinkLen) + : nBins(N) + , mass(Mass) + , linkLen(LinkLen) + { + } + + template + VTKM_EXEC void operator()(const vtkm::Id& i, + const InIdPortalType& count, + const InIdPortalType& binX, + const InIdPortalType& binY, + const InIdPortalType& binZ, + T& bestPotential, + T& worstPotential) const + { + vtkm::Id ibinX = binX.Get(i); + vtkm::Id ibinY = binY.Get(i); + vtkm::Id ibinZ = binZ.Get(i); + + for (vtkm::Id j = 0; j < nBins; j++) + { + vtkm::Id xDelta = vtkm::Abs(ibinX - binX.Get(j)); + vtkm::Id yDelta = vtkm::Abs(ibinY - binY.Get(j)); + vtkm::Id zDelta = vtkm::Abs(ibinZ - binZ.Get(j)); + + if ((count.Get(j) != 0) && (xDelta > 1) && (yDelta > 1) && (zDelta > 1)) + { + T xDistNear = static_cast((xDelta - 1)) * linkLen; + T yDistNear = static_cast((yDelta - 1)) * linkLen; + T zDistNear = static_cast((zDelta - 1)) * linkLen; + T xDistFar = static_cast((xDelta + 1)) * linkLen; + T yDistFar = static_cast((yDelta + 1)) * linkLen; + T zDistFar = static_cast((zDelta + 1)) * linkLen; + + T rNear = + vtkm::Sqrt((xDistNear * xDistNear) + (yDistNear * yDistNear) + (zDistNear * zDistNear)); + T rFar = vtkm::Sqrt((xDistFar * xDistFar) + (yDistFar * yDistFar) + (zDistFar * zDistFar)); + + if (rFar > 0.00000000001f) + { + worstPotential -= (static_cast(count.Get(j)) * mass) / rFar; + } + if (rNear > 0.00000000001f) + { + bestPotential -= (static_cast(count.Get(j)) * mass) / rNear; + } + } + } + } +}; // ComputePotentialBin +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputePotentialMxN.h b/vtkm/worklet/cosmotools/ComputePotentialMxN.h new file mode 100644 index 000000000..274d3038d --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputePotentialMxN.h @@ -0,0 +1,130 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_potential_mxn_h +#define vtkm_worklet_cosmotools_compute_potential_mxn_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the exact potential for all particles in range vs all particles in system +template +class ComputePotentialMxN : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn index, // (input) index into particles for one bin + WholeArrayIn partId, // (input) original particle id + WholeArrayIn xLoc, // (input) x location in domain + WholeArrayIn yLoc, // (input) y location in domain + WholeArrayIn zLoc, // (input) z location in domain + FieldOut potential); // (output) bin ID + typedef _6 ExecutionSignature(_1, _2, _3, _4, _5); + typedef _1 InputDomain; + + vtkm::Id nParticles; // Number of particles in halo + T mass; // Particle mass + + // Constructor + VTKM_EXEC_CONT + ComputePotentialMxN(vtkm::Id N, T Mass) + : nParticles(N) + , mass(Mass) + { + } + + template + VTKM_EXEC T operator()(const vtkm::Id& i, + const InIdPortalType& partId, + const InFieldPortalType& xLoc, + const InFieldPortalType& yLoc, + const InFieldPortalType& zLoc) const + { + T potential = 0.0f; + vtkm::Id iPartId = partId.Get(i); + for (vtkm::Id j = 0; j < nParticles; j++) + { + T xDist = xLoc.Get(iPartId) - xLoc.Get(j); + T yDist = yLoc.Get(iPartId) - yLoc.Get(j); + T zDist = zLoc.Get(iPartId) - zLoc.Get(j); + T r = sqrt((xDist * xDist) + (yDist * yDist) + (zDist * zDist)); + if (fabs(r) > 0.00000000001f) + { + potential -= mass / r; + } + } + return potential; + } +}; // ComputePotentialMxN +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputePotentialNeighbors.h b/vtkm/worklet/cosmotools/ComputePotentialNeighbors.h new file mode 100644 index 000000000..960c9d71c --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputePotentialNeighbors.h @@ -0,0 +1,162 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_potential_neighbors_h +#define vtkm_worklet_cosmotools_compute_potential_neighbors_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the potential for a particle using 27 neighbor bins +template +class ComputePotentialNeighbors : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature( + FieldIn index, // (input) particle Id + WholeArrayIn binId, // (input) bin id for this particle + WholeArrayIn partId, // (input) first particle in halo + WholeArrayIn xLoc, // (input) x location in domain + WholeArrayIn yLoc, // (input) y location in domain + WholeArrayIn zLoc, // (input) z location in domain + WholeArrayIn firstParticleId, // (input) first particle in halo + WholeArrayIn lastParticleId, // (input) last particle in halo + FieldOut potential); // (output) bin ID + typedef _9 ExecutionSignature(_1, _2, _3, _4, _5, _6, _7, _8); + typedef _1 InputDomain; + + vtkm::Id xNum, yNum, zNum; + vtkm::Id NUM_NEIGHBORS; + T mass; + + // Constructor + VTKM_EXEC_CONT + ComputePotentialNeighbors(const vtkm::Id XNum, + const vtkm::Id YNum, + const vtkm::Id ZNum, + const vtkm::Id NumNeighbors, + T Mass) + : xNum(XNum) + , yNum(YNum) + , zNum(ZNum) + , NUM_NEIGHBORS(NumNeighbors) + , mass(Mass) + { + } + + template + VTKM_EXEC T operator()(const vtkm::Id& i, + const InIdPortalType& binId, + const InIdPortalType& partId, + const InFieldPortalType& xLoc, + const InFieldPortalType& yLoc, + const InFieldPortalType& zLoc, + const InVectorPortalType& firstParticleId, + const InVectorPortalType& lastParticleId) const + { + vtkm::Id iId = partId.Get(i); + vtkm::Id ibin = binId.Get(i); + + const vtkm::Id yVal = (ibin / xNum) % yNum; + const vtkm::Id zVal = ibin / (xNum * yNum); + vtkm::Id cnt = 0; + T potential = 0.0f; + + // Iterate on the 9 sets of neighbor particles + for (vtkm::Id z = zVal - 1; z <= zVal + 1; z++) + { + for (vtkm::Id y = yVal - 1; y <= yVal + 1; y++) + { + vtkm::Id firstBinId = NUM_NEIGHBORS * i + cnt; + vtkm::Id startParticle = firstParticleId.Get(firstBinId); + vtkm::Id endParticle = lastParticleId.Get(firstBinId); + + for (vtkm::Id j = startParticle; j < endParticle; j++) + { + vtkm::Id jId = partId.Get(j); + T xDist = xLoc.Get(iId) - xLoc.Get(jId); + T yDist = yLoc.Get(iId) - yLoc.Get(jId); + T zDist = zLoc.Get(iId) - zLoc.Get(jId); + T r = vtkm::Sqrt((xDist * xDist) + (yDist * yDist) + (zDist * zDist)); + if ((i != j) && (fabs(r) > 0.00000000001f)) + potential -= mass / r; + } + cnt++; + } + } + return potential; + } +}; // ComputePotentialNeighbors +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputePotentialNxN.h b/vtkm/worklet/cosmotools/ComputePotentialNxN.h new file mode 100644 index 000000000..479f2dcfc --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputePotentialNxN.h @@ -0,0 +1,127 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_potential_nxn_h +#define vtkm_worklet_cosmotools_compute_potential_nxn_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the potential for a particle in one halo +template +class ComputePotentialNxN : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn partId, // (input) particle Id + WholeArrayIn xLoc, // (input) x location in domain + WholeArrayIn yLoc, // (input) y location in domain + WholeArrayIn zLoc, // (input) z location in domain + FieldOut potential); // (output) bin ID + typedef _5 ExecutionSignature(_1, _2, _3, _4); + typedef _1 InputDomain; + + vtkm::Id nParticles; // Number of particles in halo + T mass; // Particle mass + + // Constructor + VTKM_EXEC_CONT + ComputePotentialNxN(vtkm::Id N, T Mass) + : nParticles(N) + , mass(Mass) + { + } + + template + VTKM_EXEC T operator()(const vtkm::Id& i, + const InFieldPortalType& xLoc, + const InFieldPortalType& yLoc, + const InFieldPortalType& zLoc) const + { + T potential = 0.0f; + for (vtkm::Id j = 0; j < nParticles; j++) + { + T xDist = xLoc.Get(i) - xLoc.Get(j); + T yDist = yLoc.Get(i) - yLoc.Get(j); + T zDist = zLoc.Get(i) - zLoc.Get(j); + T r = vtkm::Sqrt((xDist * xDist) + (yDist * yDist) + (zDist * zDist)); + if ((i != j) && (fabs(r) > 0.00000000001f)) + { + potential -= mass / r; + } + } + return potential; + } +}; // ComputePotentialNxN +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ComputePotentialOnCandidates.h b/vtkm/worklet/cosmotools/ComputePotentialOnCandidates.h new file mode 100644 index 000000000..8ffd96eb2 --- /dev/null +++ b/vtkm/worklet/cosmotools/ComputePotentialOnCandidates.h @@ -0,0 +1,127 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_compute_potential_on_candidates_h +#define vtkm_worklet_cosmotools_compute_potential_on_candidates_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the potential for a candidate particle +template +class ComputePotentialOnCandidates : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn partId, // (input) particle is part of M + WholeArrayIn xLoc, // (input) x location in domain + WholeArrayIn yLoc, // (input) y location in domain + WholeArrayIn zLoc, // (input) z location in domain + FieldOut potential); // (output) bin ID + typedef _5 ExecutionSignature(_1, _2, _3, _4); + typedef _1 InputDomain; + + vtkm::Id nParticles; // Number of particles in halo + T mass; // Particle mass + + // Constructor + VTKM_EXEC_CONT + ComputePotentialOnCandidates(vtkm::Id N, T Mass) + : nParticles(N) + , mass(Mass) + { + } + + template + VTKM_EXEC T operator()(const vtkm::Id& i, + const InFieldPortalType& xLoc, + const InFieldPortalType& yLoc, + const InFieldPortalType& zLoc) const + { + T potential = 0.0f; + for (vtkm::Id j = 0; j < nParticles; j++) + { + T xDist = xLoc.Get(i) - xLoc.Get(j); + T yDist = yLoc.Get(i) - yLoc.Get(j); + T zDist = zLoc.Get(i) - zLoc.Get(j); + T r = vtkm::Sqrt((xDist * xDist) + (yDist * yDist) + (zDist * zDist)); + if ((i != j) && (fabs(r) > 0.00000000001f)) + { + potential -= mass / r; + } + } + return potential; + } +}; // ComputePotentialOnCandidates +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/CosmoTools.h b/vtkm/worklet/cosmotools/CosmoTools.h new file mode 100644 index 000000000..c15602af8 --- /dev/null +++ b/vtkm/worklet/cosmotools/CosmoTools.h @@ -0,0 +1,308 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_cosmotools_h +#define vtkm_worklet_cosmotools_cosmotools_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +//#define DEBUG_PRINT 1 + +namespace +{ + +/////////////////////////////////////////////////////////////////////////////// +// +// Debug prints +// +/////////////////////////////////////////////////////////////////////////////// +template +void DebugPrint(const char* msg, vtkm::cont::ArrayHandle& array) +{ + vtkm::Id count = 20; + count = std::min(count, array.GetNumberOfValues()); + std::cout << std::setw(15) << msg << ": "; + for (vtkm::Id i = 0; i < count; i++) + std::cout << std::setprecision(3) << std::setw(5) << array.GetPortalConstControl().Get(i) + << " "; + std::cout << std::endl; +} + +template +void DebugPrint(const char* msg, vtkm::cont::ArrayHandleReverse>& array) +{ + vtkm::Id count = 20; + count = std::min(count, array.GetNumberOfValues()); + std::cout << std::setw(15) << msg << ": "; + for (vtkm::Id i = 0; i < count; i++) + std::cout << std::setw(5) << array.GetPortalConstControl().Get(i) << " "; + std::cout << std::endl; +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Scatter the result of a reduced array +// +/////////////////////////////////////////////////////////////////////////////// +template +struct ScatterWorklet : public vtkm::worklet::WorkletMapField +{ + typedef void ControlSignature(FieldIn<> inIndices, FieldOut<> outIndices); + typedef void ExecutionSignature(_1, _2); + using ScatterType = vtkm::worklet::ScatterCounting; + + VTKM_CONT + ScatterType GetScatter() const { return this->Scatter; } + + VTKM_CONT + ScatterWorklet(const vtkm::worklet::ScatterCounting& scatter) + : Scatter(scatter) + { + } + + VTKM_EXEC + void operator()(T inputIndex, T& outputIndex) const { outputIndex = inputIndex; } +private: + ScatterType Scatter; +}; + +/////////////////////////////////////////////////////////////////////////////// +// +// Scale or offset values of an array +// +/////////////////////////////////////////////////////////////////////////////// +template +struct ScaleBiasFunctor +{ + T Scale; + T Bias; + + VTKM_CONT + ScaleBiasFunctor(T scale = T(1), T bias = T(0)) + : Scale(scale) + , Bias(bias) + { + } + + VTKM_EXEC_CONT + T operator()(T value) const { return (Scale * value + Bias); } +}; +} + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +template +class CosmoTools +{ +public: + using DeviceAlgorithm = vtkm::cont::DeviceAdapterAlgorithm; + const vtkm::Id NUM_NEIGHBORS = 9; + + // geometry of domain + const vtkm::Id nParticles; + const T particleMass; + const vtkm::Id minPartPerHalo; + const T linkLen; + vtkm::Id numBinsX; + vtkm::Id numBinsY; + vtkm::Id numBinsZ; + + // particle locations within domain + using LocationType = typename vtkm::cont::ArrayHandle; + LocationType& xLoc; + LocationType& yLoc; + LocationType& zLoc; + + // cosmo tools constructor for all particles + CosmoTools(const vtkm::Id NParticles, // Number of particles + const T mass, // Particle mass for potential + const vtkm::Id pmin, // Minimum particles per halo + const T bb, // Linking length between particles + vtkm::cont::ArrayHandle& X, // Physical location of each particle + vtkm::cont::ArrayHandle& Y, + vtkm::cont::ArrayHandle& Z); + + // cosmo tools constructor for particles in one halo + CosmoTools(const vtkm::Id NParticles, // Number of particles + const T mass, // Particle mass for potential + vtkm::cont::ArrayHandle& X, // Physical location of each particle + vtkm::cont::ArrayHandle& Y, + vtkm::cont::ArrayHandle& Z); + ~CosmoTools() {} + + // Halo finding and center finding on halos + void HaloFinder(vtkm::cont::ArrayHandle& resultHaloId, + vtkm::cont::ArrayHandle& resultMBP, + vtkm::cont::ArrayHandle& resultPot); + void BinParticlesAll(vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& binId, + vtkm::cont::ArrayHandle& leftNeighbor, + vtkm::cont::ArrayHandle& rightNeighbor); + void MBPCenterFindingByHalo(vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& haloId, + vtkm::cont::ArrayHandle& mbpId, + vtkm::cont::ArrayHandle& minPotential); + + // MBP Center finding on single halo using NxN algorithm + vtkm::Id MBPCenterFinderNxN(T* nxnPotential); + + // MBP Center finding on single halo using MxN estimation + vtkm::Id MBPCenterFinderMxN(T* mxnPotential); + + void BinParticlesHalo(vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& binId, + vtkm::cont::ArrayHandle& uniqueBins, + vtkm::cont::ArrayHandle& partPerBin, + vtkm::cont::ArrayHandle& particleOffset, + vtkm::cont::ArrayHandle& binX, + vtkm::cont::ArrayHandle& binY, + vtkm::cont::ArrayHandle& binZ); + void MBPCenterFindingByKey(vtkm::cont::ArrayHandle& keyId, + vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& minPotential); +}; + +/////////////////////////////////////////////////////////////////////////////// +// +// Constructor for all particles in the system +// +/////////////////////////////////////////////////////////////////////////////// +template +CosmoTools::CosmoTools(const vtkm::Id NParticles, + const T mass, + const vtkm::Id pmin, + const T bb, + vtkm::cont::ArrayHandle& X, + vtkm::cont::ArrayHandle& Y, + vtkm::cont::ArrayHandle& Z) + : nParticles(NParticles) + , particleMass(mass) + , minPartPerHalo(pmin) + , linkLen(bb) + , xLoc(X) + , yLoc(Y) + , zLoc(Z) +{ +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Constructor for particles in a single halo +// +/////////////////////////////////////////////////////////////////////////////// +template +CosmoTools::CosmoTools(const vtkm::Id NParticles, + const T mass, + vtkm::cont::ArrayHandle& X, + vtkm::cont::ArrayHandle& Y, + vtkm::cont::ArrayHandle& Z) + : nParticles(NParticles) + , particleMass(mass) + , minPartPerHalo(10) + , linkLen(0.2f) + , xLoc(X) + , yLoc(Y) + , zLoc(Z) +{ +} +} +} +} +#endif diff --git a/vtkm/worklet/cosmotools/CosmoToolsCenterFinder.h b/vtkm/worklet/cosmotools/CosmoToolsCenterFinder.h new file mode 100644 index 000000000..5a718117e --- /dev/null +++ b/vtkm/worklet/cosmotools/CosmoToolsCenterFinder.h @@ -0,0 +1,435 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_cosmotools_centerfinder_h +#define vtkm_worklet_cosmotools_cosmotools_centerfinder_h + +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +/////////////////////////////////////////////////////////////////////////////// +// +// Center finder for particles in FOF halo using estimations but with exact final answer +// MBP (Most Bound Particle) is particle with the minimum potential energy +// +/////////////////////////////////////////////////////////////////////////////// + +template +vtkm::Id CosmoTools::MBPCenterFinderMxN(T* mxnPotential) +{ + vtkm::cont::ArrayHandle partId; + vtkm::cont::ArrayHandle binId; + + vtkm::cont::ArrayHandle uniqueBins; + vtkm::cont::ArrayHandle partPerBin; + vtkm::cont::ArrayHandle particleOffset; + + vtkm::cont::ArrayHandle binX; + vtkm::cont::ArrayHandle binY; + vtkm::cont::ArrayHandle binZ; + + // Bin all particles in the halo into bins of size linking length + BinParticlesHalo(partId, binId, uniqueBins, partPerBin, particleOffset, binX, binY, binZ); +#ifdef DEBUG_PRINT + DebugPrint("uniqueBins", uniqueBins); + DebugPrint("partPerBin", partPerBin); +#endif + + // Compute the estimated potential per bin using 27 contiguous bins + vtkm::cont::ArrayHandle partPotential; + MBPCenterFindingByKey(binId, partId, partPotential); + + // Reduce by key to get the estimated minimum potential per bin within 27 neighbors + vtkm::cont::ArrayHandle tempId; + vtkm::cont::ArrayHandle minPotential; + DeviceAlgorithm::ReduceByKey(binId, partPotential, tempId, minPotential, vtkm::Minimum()); + + // Reduce by key to get the estimated maximum potential per bin within 27 neighbors + vtkm::cont::ArrayHandle maxPotential; + DeviceAlgorithm::ReduceByKey(binId, partPotential, tempId, maxPotential, vtkm::Maximum()); +#ifdef DEBUG_PRINT + DebugPrint("minPotential", minPotential); + DebugPrint("maxPotential", maxPotential); +#endif + + // Compute potentials estimate for a bin using all other bins + // Particles in the other bins are located at the closest point to this bin + vtkm::cont::ArrayHandleIndex uniqueIndex(uniqueBins.GetNumberOfValues()); + vtkm::cont::ArrayHandle bestEstPotential; + vtkm::cont::ArrayHandle worstEstPotential; + + // Initialize each bin potential with the nxn for that bin + DeviceAlgorithm::Copy(minPotential, bestEstPotential); + DeviceAlgorithm::Copy(maxPotential, worstEstPotential); + + // Estimate only across the uniqueBins that contain particles + ComputePotentialBin computePotentialBin(uniqueBins.GetNumberOfValues(), particleMass, linkLen); + vtkm::worklet::DispatcherMapField> computePotentialBinDispatcher( + computePotentialBin); + + computePotentialBinDispatcher.Invoke(uniqueIndex, // input + partPerBin, // input (whole array) + binX, // input (whole array) + binY, // input (whole array) + binZ, // input (whole array) + bestEstPotential, // input/output + worstEstPotential); // input/output +#ifdef DEBUG_PRINT + DebugPrint("bestEstPotential", bestEstPotential); + DebugPrint("worstEstPotential", worstEstPotential); + std::cout << "Number of bestEstPotential " << bestEstPotential.GetNumberOfValues() << std::endl; + std::cout << "Number of worstEstPotential " << worstEstPotential.GetNumberOfValues() << std::endl; +#endif + + // Sort everything by the best estimated potential per bin + vtkm::cont::ArrayHandle tempBest; + DeviceAlgorithm::Copy(bestEstPotential, tempBest); + DeviceAlgorithm::SortByKey(tempBest, worstEstPotential); + + // Use the worst estimate for the first selected bin to compare to best of all others + // Any bin that passes is a candidate for having the MBP + T cutoffPotential = worstEstPotential.GetPortalControl().Get(0); + + vtkm::cont::ArrayHandle candidate; + DeviceAlgorithm::Copy(vtkm::cont::ArrayHandleConstant(0, nParticles), candidate); + + SetCandidateParticles setCandidateParticles(cutoffPotential); + vtkm::worklet::DispatcherMapField> setCandidateParticlesDispatcher( + setCandidateParticles); + setCandidateParticlesDispatcher.Invoke(bestEstPotential, // input + particleOffset, // input + partPerBin, // input + candidate); // output (whole array) + + // Copy the M candidate particles to a new array + vtkm::cont::ArrayHandle mparticles; + DeviceAlgorithm::CopyIf(partId, candidate, mparticles); + + // Compute potentials only on the candidate particles + vtkm::cont::ArrayHandle mpotential; + ComputePotentialOnCandidates computePotentialOnCandidates(nParticles, particleMass); + vtkm::worklet::DispatcherMapField> + computePotentialOnCandidatesDispatcher(computePotentialOnCandidates); + + computePotentialOnCandidatesDispatcher.Invoke(mparticles, + xLoc, // input (whole array) + yLoc, // input (whole array) + zLoc, // input (whole array) + mpotential); // output + + // Of the M candidate particles which has the minimum potential + DeviceAlgorithm::SortByKey(mpotential, mparticles); +#ifdef DEBUG_PRINT + DebugPrint("mparticles", mparticles); + DebugPrint("mpotential", mpotential); +#endif + + // Return the found MBP particle and its potential + vtkm::Id mxnMBP = mparticles.GetPortalControl().Get(0); + *mxnPotential = mpotential.GetPortalControl().Get(0); + + return mxnMBP; +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Bin particles in one halo for quick MBP finding +// +/////////////////////////////////////////////////////////////////////////////// +template +void CosmoTools::BinParticlesHalo( + vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& binId, + vtkm::cont::ArrayHandle& uniqueBins, + vtkm::cont::ArrayHandle& partPerBin, + vtkm::cont::ArrayHandle& particleOffset, + vtkm::cont::ArrayHandle& binX, + vtkm::cont::ArrayHandle& binY, + vtkm::cont::ArrayHandle& binZ) +{ + // Compute number of bins and ranges for each bin + vtkm::Vec xRange(xLoc.GetPortalConstControl().Get(0)); + vtkm::Vec yRange(yLoc.GetPortalConstControl().Get(0)); + vtkm::Vec zRange(zLoc.GetPortalConstControl().Get(0)); + xRange = DeviceAlgorithm::Reduce(xLoc, xRange, vtkm::MinAndMax()); + T minX = xRange[0]; + T maxX = xRange[1]; + yRange = DeviceAlgorithm::Reduce(yLoc, yRange, vtkm::MinAndMax()); + T minY = yRange[0]; + T maxY = yRange[1]; + zRange = DeviceAlgorithm::Reduce(zLoc, zRange, vtkm::MinAndMax()); + T minZ = zRange[0]; + T maxZ = zRange[1]; + + numBinsX = static_cast(vtkm::Floor((maxX - minX) / linkLen)); + numBinsY = static_cast(vtkm::Floor((maxY - minY) / linkLen)); + numBinsZ = static_cast(vtkm::Floor((maxZ - minZ) / linkLen)); + + vtkm::Id maxBins = 1048576; + numBinsX = std::min(maxBins, numBinsX); + numBinsY = std::min(maxBins, numBinsY); + numBinsZ = std::min(maxBins, numBinsZ); + + vtkm::Id minBins = 1; + numBinsX = std::max(minBins, numBinsX); + numBinsY = std::max(minBins, numBinsY); + numBinsZ = std::max(minBins, numBinsZ); + +#ifdef DEBUG_PRINT + std::cout << std::endl + << "** BinParticlesHalo (" << numBinsX << ", " << numBinsY << ", " << numBinsZ << ") (" + << minX << ", " << minY << ", " << minZ << ") (" << maxX << ", " << maxY << ", " << maxZ + << ")" << std::endl; +#endif + + // Compute which bin each particle is in + ComputeBins computeBins(minX, + maxX, // Physical range on domain + minY, + maxY, + minZ, + maxZ, + numBinsX, + numBinsY, + numBinsZ); // Size of superimposed mesh + vtkm::worklet::DispatcherMapField> computeBinsDispatcher(computeBins); + computeBinsDispatcher.Invoke(xLoc, // input + yLoc, // input + zLoc, // input + binId); // output + + vtkm::cont::ArrayHandleIndex indexArray(nParticles); + DeviceAlgorithm::Copy(indexArray, partId); + +#ifdef DEBUG_PRINT + DebugPrint("xLoc", xLoc); + DebugPrint("yLoc", yLoc); + DebugPrint("zLoc", zLoc); + DebugPrint("partId", partId); + DebugPrint("binId", binId); +#endif + + // Sort the particles by bin + DeviceAlgorithm::SortByKey(binId, partId); + + // Count the number of particles per bin + vtkm::cont::ArrayHandleConstant constArray(1, nParticles); + DeviceAlgorithm::ReduceByKey(binId, constArray, uniqueBins, partPerBin, vtkm::Add()); +#ifdef DEBUG_PRINT + DebugPrint("sorted binId", binId); + DebugPrint("sorted partId", partId); + DebugPrint("uniqueBins", uniqueBins); + DebugPrint("partPerBin", partPerBin); +#endif + + // Calculate the bin indices + vtkm::cont::ArrayHandleIndex uniqueIndex(uniqueBins.GetNumberOfValues()); + ComputeBinIndices computeBinIndices(numBinsX, numBinsY, numBinsZ); + vtkm::worklet::DispatcherMapField> computeBinIndicesDispatcher( + computeBinIndices); + + computeBinIndicesDispatcher.Invoke(uniqueBins, // input + binX, // input + binY, // input + binZ); // input + + DeviceAlgorithm::ScanExclusive(partPerBin, particleOffset); +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Center finder for all particles given location, particle id and key id +// Assumed that key and particles are already sorted +// MBP (Most Bound Particle) is particle with the minimum potential energy +// Method uses ScanInclusiveByKey() and ArrayHandleReverse +// +/////////////////////////////////////////////////////////////////////////////// +template +void CosmoTools::MBPCenterFindingByKey( + vtkm::cont::ArrayHandle& keyId, + vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& minPotential) +{ + // Compute starting and ending indices of each key (bin or halo) + vtkm::cont::ArrayHandleIndex indexArray(nParticles); + vtkm::cont::ArrayHandle potential; + + vtkm::cont::ArrayHandleReverse> keyReverse(keyId); + vtkm::cont::ArrayHandleReverse> minPotReverse(minPotential); + + // Compute indices of all left neighbor bins per bin not per particle + vtkm::cont::ArrayHandle leftNeighbor; + vtkm::cont::ArrayHandle rightNeighbor; + leftNeighbor.Allocate(NUM_NEIGHBORS * nParticles); + rightNeighbor.Allocate(NUM_NEIGHBORS * nParticles); + + vtkm::cont::ArrayHandleIndex countArray(nParticles); + ComputeNeighborBins computeNeighborBins(numBinsX, numBinsY, numBinsZ, NUM_NEIGHBORS); + vtkm::worklet::DispatcherMapField computeNeighborBinsDispatcher( + computeNeighborBins); + computeNeighborBinsDispatcher.Invoke(countArray, keyId, leftNeighbor); + + // Compute indices of all right neighbor bins + ComputeBinRange computeBinRange(numBinsX); + vtkm::worklet::DispatcherMapField computeBinRangeDispatcher(computeBinRange); + computeBinRangeDispatcher.Invoke(leftNeighbor, rightNeighbor); + + // Convert bin range to particle range within the bins + DeviceAlgorithm::LowerBounds(keyId, leftNeighbor, leftNeighbor); + DeviceAlgorithm::UpperBounds(keyId, rightNeighbor, rightNeighbor); +#ifdef DEBUG_PRINT + DebugPrint("leftNeighbor", leftNeighbor); + DebugPrint("rightNeighbor", rightNeighbor); +#endif + + // Initialize halo id of each particle to itself + // Compute potentials on particles in 27 neighbors to find minimum + ComputePotentialNeighbors computePotentialNeighbors( + numBinsX, numBinsY, numBinsZ, NUM_NEIGHBORS, particleMass); + vtkm::worklet::DispatcherMapField> + computePotentialNeighborsDispatcher(computePotentialNeighbors); + + computePotentialNeighborsDispatcher.Invoke(indexArray, + keyId, // input (whole array) + partId, // input (whole array) + xLoc, // input (whole array) + yLoc, // input (whole array) + zLoc, // input (whole array) + leftNeighbor, // input (whole array) + rightNeighbor, // input (whole array) + potential); // output + + // Find minimum potential for all particles in a halo + DeviceAlgorithm::ScanInclusiveByKey(keyId, potential, minPotential, vtkm::Minimum()); + DeviceAlgorithm::ScanInclusiveByKey(keyReverse, minPotReverse, minPotReverse, vtkm::Minimum()); +#ifdef DEBUG_PRINT + DebugPrint("potential", potential); + DebugPrint("minPotential", minPotential); +#endif + + // Find the particle id matching the minimum potential + vtkm::cont::ArrayHandle centerId; + EqualsMinimumPotential equalsMinimumPotential; + vtkm::worklet::DispatcherMapField> equalsMinimumPotentialDispatcher( + equalsMinimumPotential); + + equalsMinimumPotentialDispatcher.Invoke(partId, potential, minPotential, centerId); +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Center finder for particles in a single halo given location and particle id +// MBP (Most Bound Particle) is particle with the minimum potential energy +// Method uses ScanInclusiveByKey() and ArrayHandleReverse +// +/////////////////////////////////////////////////////////////////////////////// +template +vtkm::Id CosmoTools::MBPCenterFinderNxN(T* nxnPotential) +{ + vtkm::cont::ArrayHandle potential; + vtkm::cont::ArrayHandle minPotential; + + vtkm::cont::ArrayHandleReverse> minPotReverse(minPotential); + + vtkm::cont::ArrayHandleIndex particleIndex(nParticles); + + // Compute potentials (Worklet) + ComputePotentialNxN computePotentialHalo(nParticles, particleMass); + vtkm::worklet::DispatcherMapField> computePotentialHaloDispatcher( + computePotentialHalo); + + computePotentialHaloDispatcher.Invoke(particleIndex, // input + xLoc, // input (whole array) + yLoc, // input (whole array) + zLoc, // input (whole array) + potential); // output + + // Find minimum potential for all particles in a halo + DeviceAlgorithm::ScanInclusive(potential, minPotential, vtkm::Minimum()); + DeviceAlgorithm::ScanInclusive(minPotReverse, minPotReverse, vtkm::Minimum()); + + // Find the particle id matching the minimum potential + vtkm::cont::ArrayHandle centerId; + EqualsMinimumPotential equalsMinimumPotential; + vtkm::worklet::DispatcherMapField> equalsMinimumPotentialDispatcher( + equalsMinimumPotential); + + equalsMinimumPotentialDispatcher.Invoke(particleIndex, potential, minPotential, centerId); + + // Fill out entire array with center index + vtkm::cont::ArrayHandleReverse> centerIdReverse(centerId); + DeviceAlgorithm::ScanInclusive(centerId, centerId, vtkm::Maximum()); + DeviceAlgorithm::ScanInclusive(centerIdReverse, centerIdReverse, vtkm::Maximum()); + + vtkm::Id nxnMBP = centerId.GetPortalConstControl().Get(0); + *nxnPotential = potential.GetPortalConstControl().Get(nxnMBP); + + return nxnMBP; +} +} +} +} +#endif diff --git a/vtkm/worklet/cosmotools/CosmoToolsHaloFinder.h b/vtkm/worklet/cosmotools/CosmoToolsHaloFinder.h new file mode 100644 index 000000000..5f45b0c4a --- /dev/null +++ b/vtkm/worklet/cosmotools/CosmoToolsHaloFinder.h @@ -0,0 +1,417 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_cosmotools_halofinder_h +#define vtkm_worklet_cosmotools_cosmotools_halofinder_h + +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +/////////////////////////////////////////////////////////////////////////////////////////// +// +// Halo finder for all particles in domain +// +/////////////////////////////////////////////////////////////////////////////////////////// + +template +void CosmoTools::HaloFinder( + vtkm::cont::ArrayHandle& resultHaloId, + vtkm::cont::ArrayHandle& resultMBP, + vtkm::cont::ArrayHandle& resultPot) +{ + // Package locations for worklets + using CompositeLocationType = + typename vtkm::cont::ArrayHandleCompositeVectorType::type; + CompositeLocationType location; + location = make_ArrayHandleCompositeVector(xLoc, 0, yLoc, 0, zLoc, 0); + + vtkm::cont::ArrayHandle leftNeighbor; // lower particle id to check for linking length + vtkm::cont::ArrayHandle rightNeighbor; // upper particle id to check for linking length + vtkm::cont::ArrayHandle + activeMask; // mask per particle indicating active neighbor bins + vtkm::cont::ArrayHandle partId; // index into all particles + vtkm::cont::ArrayHandle binId; // bin id for each particle in each FOF halo + + leftNeighbor.Allocate(NUM_NEIGHBORS * nParticles); + rightNeighbor.Allocate(NUM_NEIGHBORS * nParticles); + + vtkm::cont::ArrayHandleConstant trueArray(true, nParticles); + vtkm::cont::ArrayHandleIndex indexArray(nParticles); + + // Bin all particles in domain into bins of size linking length + BinParticlesAll(partId, binId, leftNeighbor, rightNeighbor); + + // Mark active neighbor bins, meaning at least one particle in the bin + // is within linking length of the given particle indicated by mask + MarkActiveNeighbors markActiveNeighbors(numBinsX, numBinsY, numBinsZ, NUM_NEIGHBORS, linkLen); + vtkm::worklet::DispatcherMapField> markActiveNeighborsDispatcher( + markActiveNeighbors); + markActiveNeighborsDispatcher.Invoke( + indexArray, // (input) index into all particles + partId, // (input) particle id sorted by bin + binId, // (input) bin id sorted + partId, // (input) particle id (whole array) + location, // (input) location on original particle order + leftNeighbor, // (input) first partId for neighbor vector + rightNeighbor, // (input) last partId for neighbor vector + activeMask); // (output) mask per particle indicating valid neighbors + + // Initialize halo id of each particle to itself + vtkm::cont::ArrayHandle haloIdCurrent; + vtkm::cont::ArrayHandle haloIdLast; + DeviceAlgorithm::Copy(indexArray, haloIdCurrent); + DeviceAlgorithm::Copy(indexArray, haloIdLast); + + // rooted star is nchecked each iteration for all particles being rooted in a halo + vtkm::cont::ArrayHandle rootedStar; + + // Iterate over particles graft together to form halos + while (true) + { + // Connect each particle to another close particle to build halos + GraftParticles graftParticles(numBinsX, numBinsY, numBinsZ, NUM_NEIGHBORS, linkLen); + vtkm::worklet::DispatcherMapField> graftParticlesDispatcher(graftParticles); + + graftParticlesDispatcher.Invoke(indexArray, // (input) index into particles + partId, // (input) particle id sorted by bin + binId, // (input) bin id sorted by bin + activeMask, // (input) flag indicates if neighor range is used + partId, // (input) particle id (whole array) + location, // (input) location on original particle order + leftNeighbor, // (input) first partId for neighbor + rightNeighbor, // (input) last partId for neighbor + haloIdCurrent); // (output) +#ifdef DEBUG_PRINT + DebugPrint("haloIdCurrent", haloIdCurrent); +#endif + + // Reininitialize rootedStar for each pass + DeviceAlgorithm::Copy(trueArray, rootedStar); + + // By comparing the haloIds from the last pass and this one + // determine if any particles are still migrating to halos + IsStar isStar; + vtkm::worklet::DispatcherMapField isStarDispatcher(isStar); + isStarDispatcher.Invoke(indexArray, + haloIdCurrent, // input (whole array) + haloIdLast, // input (whole array) + rootedStar); // output (whole array) + + // If all vertices are in rooted stars, algorithm is complete + bool allStars = DeviceAlgorithm::Reduce(rootedStar, true, vtkm::BitwiseAnd()); + if (allStars) + { + break; + } + else + // Otherwise copy current halo ids to last pass halo ids + { + PointerJump pointerJump; + vtkm::worklet::DispatcherMapField pointerJumpDispatcher(pointerJump); + pointerJumpDispatcher.Invoke(indexArray, haloIdCurrent); // input (whole array) + DeviceAlgorithm::Copy(haloIdCurrent, haloIdLast); + } + } + + // Index into final halo id is the original particle ordering + // not the particles sorted by bin + DeviceAlgorithm::Copy(indexArray, partId); +#ifdef DEBUG_PRINT + DebugPrint("FINAL haloId", haloIdCurrent); + DebugPrint("FINAL partId", partId); +#endif + + // Call center finding on all halos using method with ReduceByKey and Scatter + DeviceAlgorithm::Copy(haloIdCurrent, resultHaloId); + MBPCenterFindingByHalo(partId, resultHaloId, resultMBP, resultPot); +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Bin all particles in the system for halo finding +// +/////////////////////////////////////////////////////////////////////////////// +template +void CosmoTools::BinParticlesAll( + vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& binId, + vtkm::cont::ArrayHandle& leftNeighbor, + vtkm::cont::ArrayHandle& rightNeighbor) +{ + // Compute number of bins and ranges for each bin + vtkm::Vec result; + vtkm::Vec xInit(xLoc.GetPortalConstControl().Get(0)); + vtkm::Vec yInit(yLoc.GetPortalConstControl().Get(0)); + vtkm::Vec zInit(zLoc.GetPortalConstControl().Get(0)); + result = DeviceAlgorithm::Reduce(xLoc, xInit, vtkm::MinAndMax()); + T minX = result[0]; + T maxX = result[1]; + result = DeviceAlgorithm::Reduce(yLoc, yInit, vtkm::MinAndMax()); + T minY = result[0]; + T maxY = result[1]; + result = DeviceAlgorithm::Reduce(zLoc, zInit, vtkm::MinAndMax()); + T minZ = result[0]; + T maxZ = result[1]; + + vtkm::Id maxBins = 1048576; + vtkm::Id minBins = 1; + + numBinsX = static_cast(vtkm::Floor((maxX - minX) / linkLen)); + numBinsY = static_cast(vtkm::Floor((maxY - minY) / linkLen)); + numBinsZ = static_cast(vtkm::Floor((maxZ - minZ) / linkLen)); + + numBinsX = std::min(maxBins, numBinsX); + numBinsY = std::min(maxBins, numBinsY); + numBinsZ = std::min(maxBins, numBinsZ); + + numBinsX = std::max(minBins, numBinsX); + numBinsY = std::max(minBins, numBinsY); + numBinsZ = std::max(minBins, numBinsZ); + + // Compute which bin each particle is in + ComputeBins computeBins(minX, + maxX, // Physical range on domain + minY, + maxY, + minZ, + maxZ, + numBinsX, + numBinsY, + numBinsZ); // Size of superimposed mesh + vtkm::worklet::DispatcherMapField> computeBinsDispatcher(computeBins); + computeBinsDispatcher.Invoke(xLoc, // input + yLoc, // input + zLoc, // input + binId); // output + + vtkm::cont::ArrayHandleIndex indexArray(nParticles); + DeviceAlgorithm::Copy(indexArray, partId); + +#ifdef DEBUG_PRINT + std::cout << std::endl + << "** BinParticlesAll (" << numBinsX << ", " << numBinsY << ", " << numBinsZ << ")" + << std::endl; + DebugPrint("xLoc", xLoc); + DebugPrint("yLoc", yLoc); + DebugPrint("zLoc", zLoc); + DebugPrint("partId", partId); + DebugPrint("binId", binId); + std::cout << std::endl; +#endif + + // Sort the particles by bin (remember that xLoc and yLoc are not sorted) + DeviceAlgorithm::SortByKey(binId, partId); +#ifdef DEBUG_PRINT + DebugPrint("partId", partId); + DebugPrint("binId", binId); +#endif + + // Compute indices of all left neighbor bins + vtkm::cont::ArrayHandleIndex countArray(nParticles); + ComputeNeighborBins computeNeighborBins(numBinsX, numBinsY, numBinsZ, NUM_NEIGHBORS); + vtkm::worklet::DispatcherMapField computeNeighborBinsDispatcher( + computeNeighborBins); + computeNeighborBinsDispatcher.Invoke(countArray, binId, leftNeighbor); + + // Compute indices of all right neighbor bins + ComputeBinRange computeBinRange(numBinsX); + vtkm::worklet::DispatcherMapField computeBinRangeDispatcher(computeBinRange); + computeBinRangeDispatcher.Invoke(leftNeighbor, rightNeighbor); + + // Convert bin range to particle range within the bins + DeviceAlgorithm::LowerBounds(binId, leftNeighbor, leftNeighbor); + DeviceAlgorithm::UpperBounds(binId, rightNeighbor, rightNeighbor); +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Center finder for all particles given location, particle id and halo id +// MBP (Most Bound Particle) is particle with the minimum potential energy +// Method uses ReduceByKey() and Scatter() +// +/////////////////////////////////////////////////////////////////////////////// +template +void CosmoTools::MBPCenterFindingByHalo( + vtkm::cont::ArrayHandle& partId, + vtkm::cont::ArrayHandle& haloId, + vtkm::cont::ArrayHandle& mbpId, + vtkm::cont::ArrayHandle& minPotential) +{ + // Sort particles into groups according to halo id using an index into WholeArrays + DeviceAlgorithm::SortByKey(haloId, partId); +#ifdef DEBUG_PRINT + DebugPrint("Sorted haloId", haloId); + DebugPrint("Sorted partId", partId); +#endif + + // Find the particle in each halo with the lowest potential + // Compute starting and ending indices of each halo + vtkm::cont::ArrayHandleConstant constArray(1, nParticles); + vtkm::cont::ArrayHandleIndex indexArray(nParticles); + vtkm::cont::ArrayHandle uniqueHaloIds; + vtkm::cont::ArrayHandle particlesPerHalo; + vtkm::cont::ArrayHandle minParticle; + vtkm::cont::ArrayHandle maxParticle; + vtkm::cont::ArrayHandle potential; + vtkm::cont::ArrayHandle tempI; + vtkm::cont::ArrayHandle tempT; + + // Halo ids have been sorted, reduce to find the number of particles per halo + DeviceAlgorithm::ReduceByKey(haloId, constArray, uniqueHaloIds, particlesPerHalo, vtkm::Add()); +#ifdef DEBUG_PRINT + DebugPrint("uniqueHaloId", uniqueHaloIds); + DebugPrint("partPerHalo", particlesPerHalo); + std::cout << std::endl; +#endif + + // Setup the ScatterCounting worklets needed to expand the ReduceByKeyResults + vtkm::worklet::ScatterCounting scatter(particlesPerHalo, DeviceAdapter()); + ScatterWorklet scatterWorkletId(scatter); + ScatterWorklet scatterWorklet(scatter); + vtkm::worklet::DispatcherMapField> scatterWorkletIdDispatcher( + scatterWorkletId); + vtkm::worklet::DispatcherMapField> scatterWorkletDispatcher(scatterWorklet); + + // Calculate the minimum particle index per halo id and scatter + DeviceAlgorithm::ScanExclusive(particlesPerHalo, tempI); + scatterWorkletIdDispatcher.Invoke(tempI, minParticle); + + // Calculate the maximum particle index per halo id and scatter + DeviceAlgorithm::ScanInclusive(particlesPerHalo, tempI); + scatterWorkletIdDispatcher.Invoke(tempI, maxParticle); + + using IdArrayType = vtkm::cont::ArrayHandle; + vtkm::cont::ArrayHandleTransform> scaleBias = + vtkm::cont::make_ArrayHandleTransform(maxParticle, + ScaleBiasFunctor(1, -1)); + + DeviceAlgorithm::Copy(scaleBias, maxParticle); +#ifdef DEBUG_PRINT + DebugPrint("minParticle", minParticle); + DebugPrint("maxParticle", maxParticle); +#endif + + // Compute potentials + ComputePotential computePotential(particleMass); + vtkm::worklet::DispatcherMapField> computePotentialDispatcher( + computePotential); + + computePotentialDispatcher.Invoke(indexArray, + partId, // input (whole array) + xLoc, // input (whole array) + yLoc, // input (whole array) + zLoc, // input (whole array) + minParticle, // input (whole array) + maxParticle, // input (whole array) + potential); // output + + // Find minimum potential for all particles in a halo and scatter + DeviceAlgorithm::ReduceByKey(haloId, potential, uniqueHaloIds, tempT, vtkm::Minimum()); + scatterWorkletDispatcher.Invoke(tempT, minPotential); +#ifdef DEBUG_PRINT + DebugPrint("potential", potential); + DebugPrint("minPotential", minPotential); +#endif + + // Find the particle id matching the minimum potential (Worklet) + EqualsMinimumPotential equalsMinimumPotential; + vtkm::worklet::DispatcherMapField> equalsMinimumPotentialDispatcher( + equalsMinimumPotential); + + equalsMinimumPotentialDispatcher.Invoke(partId, potential, minPotential, mbpId); + + // Fill out entire array with center index, another reduce and scatter + vtkm::cont::ArrayHandle minIndx; + minIndx.Allocate(nParticles); + DeviceAlgorithm::ReduceByKey(haloId, mbpId, uniqueHaloIds, minIndx, vtkm::Maximum()); + scatterWorkletIdDispatcher.Invoke(minIndx, mbpId); + + // Resort particle ids and mbpId to starting order + vtkm::cont::ArrayHandle savePartId; + DeviceAlgorithm::Copy(partId, savePartId); + + DeviceAlgorithm::SortByKey(partId, haloId); + DeviceAlgorithm::Copy(savePartId, partId); + DeviceAlgorithm::SortByKey(partId, mbpId); + DeviceAlgorithm::Copy(savePartId, partId); + DeviceAlgorithm::SortByKey(partId, minPotential); + +#ifdef DEBUG_PRINT + std::cout << std::endl; + DebugPrint("partId", partId); + DebugPrint("xLoc", xLoc); + DebugPrint("yLoc", yLoc); + DebugPrint("haloId", haloId); + DebugPrint("mbpId", mbpId); + DebugPrint("minPotential", minPotential); +#endif +} +} +} +} +#endif diff --git a/vtkm/worklet/cosmotools/EqualsMinimumPotential.h b/vtkm/worklet/cosmotools/EqualsMinimumPotential.h new file mode 100644 index 000000000..98416de1e --- /dev/null +++ b/vtkm/worklet/cosmotools/EqualsMinimumPotential.h @@ -0,0 +1,109 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_equals_minimum_potential_h +#define vtkm_worklet_cosmotools_equals_minimum_potential_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for computing the potential for a particle +template +class EqualsMinimumPotential : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn partId, // (input) particle Id + FieldIn potential1, // (input) particle potential + FieldIn potential2, // (input) minimum potential + FieldOut minParticleIndx); // (output) particle Id of min + typedef _4 ExecutionSignature(_1, _2, _3); + typedef _1 InputDomain; + + // Constructor + VTKM_EXEC_CONT + EqualsMinimumPotential() {} + + VTKM_EXEC + vtkm::Id operator()(const vtkm::Id& partId, const T& potential1, const T& potential2) const + { + vtkm::Id minParticleIndx = 0; + if (fabs(potential1 - potential2) < 0.000000000001) + { + minParticleIndx = partId; + } + return minParticleIndx; + } +}; // EqualsMinimumPotential +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/GraftParticles.h b/vtkm/worklet/cosmotools/GraftParticles.h new file mode 100644 index 000000000..d70469f3e --- /dev/null +++ b/vtkm/worklet/cosmotools/GraftParticles.h @@ -0,0 +1,173 @@ + +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_graft_particle_h +#define vtkm_worklet_cosmotools_graft_particle_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet to graft particles together to form halos +template +class GraftParticles : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature( + FieldIn index, // (input) index into particles + FieldIn partId, // (input) particle id sorted by bin + FieldIn binId, // (input) bin id sorted by bin + FieldIn activeFlag, // (input) flag indicates which of neighbor ranges are used + WholeArrayIn partIdArray, // (input) particle id sorted by bin entire array + WholeArrayIn location, // (input) location of particles + WholeArrayIn firstParticleId, // (input) first particle index vector + WholeArrayIn lastParticleId, // (input) last particle index vector + WholeArrayOut haloId); + typedef void ExecutionSignature(_1, _2, _3, _4, _5, _6, _7, _8, _9); + typedef _1 InputDomain; + + vtkm::Id xNum, yNum, zNum; + vtkm::Id NUM_NEIGHBORS; + T linkLenSq; + + // Constructor + VTKM_EXEC_CONT + GraftParticles(const vtkm::Id XNum, + const vtkm::Id YNum, + const vtkm::Id ZNum, + const vtkm::Id NumNeighbors, + const T LinkLen) + : xNum(XNum) + , yNum(YNum) + , zNum(ZNum) + , NUM_NEIGHBORS(NumNeighbors) + , linkLenSq(LinkLen * LinkLen) + { + } + + template + VTKM_EXEC void operator()(const vtkm::Id& i, + const vtkm::Id& iPartId, + const vtkm::Id& iBinId, + const vtkm::UInt32& activeFlag, + const InIdPortalType& partIdArray, + const InFieldPortalType& location, + const InVectorPortalType& firstParticleId, + const InVectorPortalType& lastParticleId, + OutPortalType& haloId) const + { + const vtkm::Id yVal = (iBinId / xNum) % yNum; + const vtkm::Id zVal = iBinId / (xNum * yNum); + vtkm::UInt32 flag = activeFlag; + vtkm::Id cnt = 0; + + // Iterate on both sides of the bin this particle is in + for (vtkm::Id z = zVal - 1; z <= zVal + 1; z++) + { + for (vtkm::Id y = yVal - 1; y <= yVal + 1; y++) + { + if (flag & 0x1) + { + vtkm::Id firstBinId = NUM_NEIGHBORS * i + cnt; + vtkm::Id startParticle = firstParticleId.Get(firstBinId); + vtkm::Id endParticle = lastParticleId.Get(firstBinId); + + for (vtkm::Id j = startParticle; j < endParticle; j++) + { + vtkm::Id jPartId = partIdArray.Get(j); + vtkm::Vec iloc = location.Get(iPartId); + vtkm::Vec jloc = location.Get(jPartId); + T xDist = iloc[0] - jloc[0]; + T yDist = iloc[1] - jloc[1]; + T zDist = iloc[2] - jloc[2]; + if ((xDist * xDist + yDist * yDist + zDist * zDist) <= linkLenSq) + { + if ((haloId.Get(iPartId) == haloId.Get(haloId.Get(iPartId))) && + (haloId.Get(jPartId) < haloId.Get(iPartId))) + { + haloId.Set(haloId.Get(iPartId), haloId.Get(jPartId)); + } + } + } + } + flag = flag >> 1; + cnt++; + } + } + } +}; // GraftParticles +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/IsStar.h b/vtkm/worklet/cosmotools/IsStar.h new file mode 100644 index 000000000..a7c20ad74 --- /dev/null +++ b/vtkm/worklet/cosmotools/IsStar.h @@ -0,0 +1,109 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_is_star_h +#define vtkm_worklet_cosmotools_is_star_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Examine halo ids from current pass and last pass to see if particles +// are rooted in an existing halo +class IsStar : public vtkm::worklet::WorkletMapField +{ +public: + typedef void ControlSignature(FieldIn index, + WholeArrayInOut haloIdCurrent, + WholeArrayInOut haloIdLast, + WholeArrayInOut rootedStar); + typedef void ExecutionSignature(_1, _2, _3, _4); + typedef _1 InputDomain; + + // Constructor + VTKM_EXEC_CONT + IsStar() {} + + template + VTKM_EXEC void operator()(const vtkm::Id& i, + const InPortalType& haloIdCurrent, + const InPortalType& haloIdLast, + InPortalType2& rootedStar) const + { + vtkm::Id dValue = haloIdLast.Get(i); + vtkm::Id ddValue = haloIdCurrent.Get(dValue); + if (dValue != ddValue) + { + rootedStar.Set(dValue, false); + rootedStar.Set(ddValue, false); + } + } +}; // IsStar +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/MarkActiveNeighbors.h b/vtkm/worklet/cosmotools/MarkActiveNeighbors.h new file mode 100644 index 000000000..8126d4e2e --- /dev/null +++ b/vtkm/worklet/cosmotools/MarkActiveNeighbors.h @@ -0,0 +1,171 @@ + +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_mark_active_neighbors_h +#define vtkm_worklet_cosmotools_mark_active_neighbors_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet for particles to indicate which neighbors are active +// because at least one particle in that bin is within linking length +template +class MarkActiveNeighbors : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature( + FieldIn index, // (input) particle index + FieldIn partId, // (input) particle id sorted + FieldIn binId, // (input) bin Id per particle + WholeArrayIn partIdArray, // (input) sequence imposed on sorted particle Ids + WholeArrayIn location, // (input) location of particles + WholeArrayIn firstPartId, // (input) vector of first particle indices + WholeArrayIn lastPartId, // (input) vector of last particle indices + FieldOut flag); // (output) active bin neighbors mask + typedef _8 ExecutionSignature(_1, _2, _3, _4, _5, _6, _7); + typedef _1 InputDomain; + + vtkm::Id xNum, yNum, zNum; + vtkm::Id NUM_NEIGHBORS; + T linkLenSq; + + // Constructor + VTKM_EXEC_CONT + MarkActiveNeighbors(const vtkm::Id XNum, + const vtkm::Id YNum, + const vtkm::Id ZNum, + const vtkm::Id NumNeighbors, + const T LinkLen) + : xNum(XNum) + , yNum(YNum) + , zNum(ZNum) + , NUM_NEIGHBORS(NumNeighbors) + , linkLenSq(LinkLen * LinkLen) + { + } + + template + VTKM_EXEC vtkm::UInt32 operator()(const vtkm::Id& i, + const vtkm::Id& iPartId, + const vtkm::Id& iBinId, + const InIdPortalType& partIdArray, + const InFieldPortalType& location, + const InVectorPortalType& firstPartId, + const InVectorPortalType& lastPartId) const + { + const vtkm::Id ybin = (iBinId / xNum) % yNum; + const vtkm::Id zbin = iBinId / (xNum * yNum); + vtkm::UInt32 activeFlag = 0; + vtkm::UInt32 bcnt = 1; + vtkm::Id cnt = 0; + + // Examine all neighbor bins surrounding this particle + for (vtkm::Id z = zbin - 1; z <= zbin + 1; z++) + { + for (vtkm::Id y = ybin - 1; y <= ybin + 1; y++) + { + if ((y >= 0) && (y < yNum) && (z >= 0) && (z < zNum)) + { + vtkm::Id pos = NUM_NEIGHBORS * i + cnt; + vtkm::Id startParticle = firstPartId.Get(pos); + vtkm::Id endParticle = lastPartId.Get(pos); + + // If the bin has any particles, check to see if any of those + // are within the linking length from this particle + for (vtkm::Id j = startParticle; j < endParticle; j++) + { + vtkm::Id jPartId = partIdArray.Get(j); + vtkm::Vec iloc = location.Get(iPartId); + vtkm::Vec jloc = location.Get(jPartId); + T xDist = iloc[0] - jloc[0]; + T yDist = iloc[1] - jloc[1]; + T zDist = iloc[2] - jloc[2]; + + // Found a particle within linking length so this bin is active + if ((xDist * xDist + yDist * yDist + zDist * zDist) <= linkLenSq) + { + activeFlag = activeFlag | bcnt; + break; + } + } + } + bcnt = bcnt << 1; + cnt++; + } + } + return activeFlag; + } +}; // MarkActiveNeighbors +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/PointerJump.h b/vtkm/worklet/cosmotools/PointerJump.h new file mode 100644 index 000000000..bf198e2f4 --- /dev/null +++ b/vtkm/worklet/cosmotools/PointerJump.h @@ -0,0 +1,97 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_pointer_jump_h +#define vtkm_worklet_cosmotools_pointer_jump_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Jump to next pointer in array +class PointerJump : public vtkm::worklet::WorkletMapField +{ +public: + typedef void ControlSignature(FieldIn index, WholeArrayInOut D); + typedef void ExecutionSignature(_1, _2); + typedef _1 InputDomain; + + // Constructor + VTKM_EXEC_CONT + PointerJump() {} + + template + VTKM_EXEC void operator()(const vtkm::Id& index, InPortalType& D) const + { + vtkm::Id dValue = D.Get(index); + D.Set(index, D.Get(dValue)); + } +}; // PointerJump +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/SetCandidateParticles.h b/vtkm/worklet/cosmotools/SetCandidateParticles.h new file mode 100644 index 000000000..f406e46dc --- /dev/null +++ b/vtkm/worklet/cosmotools/SetCandidateParticles.h @@ -0,0 +1,116 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_set_candidate_particles_h +#define vtkm_worklet_cosmotools_set_candidate_particles_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Worklet to examine each bin against a cutoff and mark its particles as candidates +template +class SetCandidateParticles : public vtkm::worklet::WorkletMapField +{ +public: + struct TagType : vtkm::ListTagBase + { + }; + + typedef void ControlSignature(FieldIn bestPot, // (input) bin's best potential + FieldIn partOffset, // (input) offset to first particle + FieldIn partCount, // (input) count of particles in bin + WholeArrayOut candidate); // (input) z location in bin + typedef void ExecutionSignature(_1, _2, _3, _4); + typedef _1 InputDomain; + + T cutoffPotential; // Lowest potential for a candidate bin + + // Constructor + VTKM_EXEC_CONT + SetCandidateParticles(T Cutoff) + : cutoffPotential(Cutoff) + { + } + + template + VTKM_EXEC void operator()(const T& bestPot, + const vtkm::Id& offset, + const vtkm::Id& count, + const OutPortalType& candidate) const + { + if (bestPot <= cutoffPotential) + { + for (vtkm::Id part = offset; part < offset + count; part++) + candidate.Set(part, 1); + } + } +}; // SetCandidateParticles +} +} +} + +#endif diff --git a/vtkm/worklet/cosmotools/ValidHalo.h b/vtkm/worklet/cosmotools/ValidHalo.h new file mode 100644 index 000000000..6b50fb973 --- /dev/null +++ b/vtkm/worklet/cosmotools/ValidHalo.h @@ -0,0 +1,112 @@ +//============================================================================ +// 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. +//============================================================================ +// Copyright (c) 2016, Los Alamos National Security, LLC +// All rights reserved. +// +// Copyright 2016. Los Alamos National Security, LLC. +// This software was produced under U.S. Government contract DE-AC52-06NA25396 +// for Los Alamos National Laboratory (LANL), which is operated by +// Los Alamos National Security, LLC for the U.S. Department of Energy. +// The U.S. Government has rights to use, reproduce, and distribute this +// software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC +// MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE +// USE OF THIS SOFTWARE. If software is modified to produce derivative works, +// such modified software should be clearly marked, so as not to confuse it +// with the version available from LANL. +// +// Additionally, redistribution and use in source and binary forms, with or +// without modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// 3. Neither the name of Los Alamos National Security, LLC, Los Alamos +// National Laboratory, LANL, the U.S. Government, nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND +// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS +// NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//============================================================================ + +#ifndef vtkm_worklet_cosmotools_valid_halo_h +#define vtkm_worklet_cosmotools_valid_halo_h + +#include +#include + +namespace vtkm +{ +namespace worklet +{ +namespace cosmotools +{ + +// Return 1 for every unique halo meeting minimum size +class ValidHalo : public vtkm::worklet::WorkletMapField +{ +public: + typedef void ControlSignature(FieldIn index, + FieldIn partPerHalo, + WholeArrayIn haloId, + FieldOut isValidHalo); + typedef _4 ExecutionSignature(_1, _2, _3); + typedef _1 InputDomain; + + vtkm::Id pmin; + + // Constructor + VTKM_EXEC_CONT + ValidHalo(vtkm::Id minimumHalo) + : pmin(minimumHalo) + { + } + + template + VTKM_EXEC vtkm::Id operator()(const vtkm::Id& i, + const vtkm::Id& partPerHalo, + const InPortalType& haloId) const + { + // Boundary condition + if ((i == 0) && (partPerHalo >= pmin)) + return 1; + // Return valid if it is the first particle in halo of correct size + if ((haloId.Get(i) != haloId.Get(i - 1)) && (partPerHalo >= pmin)) + return 1; + return 0; + } +}; // ValidHalo +} +} +} + +#endif diff --git a/vtkm/worklet/testing/CMakeLists.txt b/vtkm/worklet/testing/CMakeLists.txt index 9f2d9f1c6..53c5754b8 100644 --- a/vtkm/worklet/testing/CMakeLists.txt +++ b/vtkm/worklet/testing/CMakeLists.txt @@ -25,6 +25,7 @@ set(unit_tests UnitTestCellGradient.cxx UnitTestClipping.cxx UnitTestContourTreeUniform.cxx + UnitTestCosmoTools.cxx UnitTestExternalFaces.cxx UnitTestExtractGeometry.cxx UnitTestExtractPoints.cxx diff --git a/vtkm/worklet/testing/UnitTestCosmoTools.cxx b/vtkm/worklet/testing/UnitTestCosmoTools.cxx new file mode 100644 index 000000000..96d5ce9f0 --- /dev/null +++ b/vtkm/worklet/testing/UnitTestCosmoTools.cxx @@ -0,0 +1,356 @@ +//============================================================================ +// 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 +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace +{ + +template +bool TestArrayHandle(const vtkm::cont::ArrayHandle& ah, + const vtkm::cont::ArrayHandle& expected, + vtkm::Id size) +{ + if (size != ah.GetNumberOfValues()) + { + return false; + } + + for (vtkm::Id i = 0; i < size; ++i) + { + if (ah.GetPortalConstControl().Get(i) != expected.GetPortalConstControl().Get(i)) + { + return false; + } + } + return true; +} + +// +// Test 2D explicit dataset of particles +// +vtkm::cont::DataSet MakeCosmo_2DDataSet_0() +{ + vtkm::cont::DataSet dataSet; + vtkm::cont::DataSetBuilderExplicit dsb; + vtkm::cont::DataSetFieldAdd dsf; + + // Coordinates + const int nVerts = 17; + const int nCells = 17; + using CoordType = vtkm::Vec; + std::vector coords(nVerts); + + coords[0] = CoordType(1, 1, 0); + coords[1] = CoordType(1, 2, 0); + coords[2] = CoordType(2, 6, 0); + coords[3] = CoordType(1, 3, 0); + coords[4] = CoordType(3, 5, 0); + coords[5] = CoordType(1, 4, 0); + coords[6] = CoordType(1, 5, 0); + coords[7] = CoordType(3, 6, 0); + coords[8] = CoordType(2, 3, 0); + coords[9] = CoordType(3, 3, 0); + coords[10] = CoordType(4, 3, 0); + coords[11] = CoordType(3, 7, 0); + coords[12] = CoordType(5, 2, 0); + coords[13] = CoordType(5, 3, 0); + coords[14] = CoordType(4, 6, 0); + coords[15] = CoordType(5, 4, 0); + coords[16] = CoordType(6, 3, 0); + + // Connectivity + std::vector shapes; + std::vector numindices; + std::vector conn; + + for (vtkm::Id pt = 0; pt < nCells; pt++) + { + shapes.push_back(vtkm::CELL_SHAPE_VERTEX); + numindices.push_back(1); + conn.push_back(pt); + } + dataSet = dsb.Create(coords, shapes, numindices, conn, "coordinates", "cells"); + + // Field data + vtkm::Float32 xLocation[nCells] = { 1, 1, 2, 1, 3, 1, 1, 3, 2, 3, 4, 3, 5, 5, 4, 5, 6 }; + vtkm::Float32 yLocation[nCells] = { 1, 2, 6, 3, 5, 4, 5, 6, 3, 3, 3, 7, 2, 3, 6, 4, 3 }; + vtkm::Float32 zLocation[nCells] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + vtkm::Id haloId[nCells] = { 0, 0, 2, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 0 }; + vtkm::Id mbp[nCells] = { 8, 8, 7, 8, 7, 8, 8, 7, 8, 8, 8, 7, 8, 8, 7, 8, 8 }; + + dsf.AddCellField(dataSet, "xLocation", xLocation, nCells); + dsf.AddCellField(dataSet, "yLocation", yLocation, nCells); + dsf.AddCellField(dataSet, "zLocation", zLocation, nCells); + dsf.AddCellField(dataSet, "haloId", haloId, nCells); + dsf.AddCellField(dataSet, "mbp", mbp, nCells); + return dataSet; +} + +// +// Test 3D explicit dataset of particles +// +vtkm::cont::DataSet MakeCosmo_3DDataSet_0() +{ + vtkm::cont::DataSet dataSet; + vtkm::cont::DataSetBuilderExplicit dsb; + vtkm::cont::DataSetFieldAdd dsf; + + // Coordinates + const int nVerts = 14; + const int nCells = 14; + typedef vtkm::Vec CoordType; + std::vector coords(nVerts); + + coords[0] = CoordType(20.8125f, 10.8864f, 0.309784f); + coords[1] = CoordType(29.6871f, 15.4445f, 1.55953f); + coords[2] = CoordType(29.724f, 15.4766f, 1.51077f); + coords[3] = CoordType(29.6783f, 15.4766f, 1.5324f); + coords[4] = CoordType(29.7051f, 15.5052f, 1.52008f); + coords[5] = CoordType(20.8172f, 10.8534f, 0.23461f); + coords[6] = CoordType(20.8665f, 10.8679f, 0.254398f); + coords[7] = CoordType(20.8271f, 10.8677f, 0.234255f); + coords[8] = CoordType(20.8592f, 10.9505f, 0.248716f); + coords[9] = CoordType(20.819f, 10.8949f, 0.304834f); + coords[10] = CoordType(29.708f, 15.4251f, 1.53951f); + coords[11] = CoordType(20.8829f, 10.9144f, 0.261517f); + coords[12] = CoordType(20.8379f, 10.877f, 0.27677f); + coords[13] = CoordType(29.7278f, 15.5267f, 1.50798f); + + // Connectivity + std::vector shapes; + std::vector numindices; + std::vector conn; + + for (vtkm::Id pt = 0; pt < nCells; pt++) + { + shapes.push_back(vtkm::CELL_SHAPE_VERTEX); + numindices.push_back(1); + conn.push_back(pt); + } + dataSet = dsb.Create(coords, shapes, numindices, conn, "coordinates", "cells"); + + // Field data + vtkm::Float32 xLocation[nCells] = { 20.8125f, 29.6871f, 29.724f, 29.6783f, 29.7051f, + 20.8172f, 20.8665f, 20.8271f, 20.8592f, 20.819f, + 29.708f, 20.8829f, 20.8379f, 29.7278f }; + vtkm::Float32 yLocation[nCells] = { 10.8864f, 15.4445f, 15.4766f, 15.4766f, 15.5052f, + 10.8534f, 10.8679f, 10.8677f, 10.9505f, 10.8949f, + 15.4251f, 10.9144f, 10.877f, 15.5267f }; + vtkm::Float32 zLocation[nCells] = { 0.309784f, 1.55953f, 1.51077f, 1.5324f, 1.52008f, + 0.23461f, 0.254398f, 0.234255f, 0.248716f, 0.304834f, + 1.53951f, 0.261517f, 0.27677f, 1.50798f }; + + vtkm::Id haloId[nCells] = { 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1 }; + vtkm::Id mbp[nCells] = { 9, 4, 4, 4, 4, 9, 9, 9, 9, 9, 4, 9, 9, 4 }; + + dsf.AddCellField(dataSet, "xLocation", xLocation, nCells); + dsf.AddCellField(dataSet, "yLocation", yLocation, nCells); + dsf.AddCellField(dataSet, "zLocation", zLocation, nCells); + dsf.AddCellField(dataSet, "haloId", haloId, nCells); + dsf.AddCellField(dataSet, "mbp", mbp, nCells); + return dataSet; +} + +} // namespace + + +//////////////////////////////////////////////////////////////////////////////////// +// +// Create an explicit 2D particle set and find halos and minimum potential particle of each halo +// +//////////////////////////////////////////////////////////////////////////////////// + +void TestCosmo_2DHaloFind() +{ + std::cout << "Testing Halo Finder 2D" << std::endl; + + // Create the input 2D particle dataset + vtkm::cont::DataSet dataSet = MakeCosmo_2DDataSet_0(); + vtkm::Id nCells = dataSet.GetCellSet(0).GetNumberOfCells(); + + vtkm::cont::ArrayHandle xLocArray; + vtkm::cont::ArrayHandle yLocArray; + vtkm::cont::ArrayHandle zLocArray; + vtkm::cont::ArrayHandle haloIdArray; + vtkm::cont::ArrayHandle mbpArray; + + dataSet.GetField("xLocation").GetData().CopyTo(xLocArray); + dataSet.GetField("yLocation").GetData().CopyTo(yLocArray); + dataSet.GetField("zLocation").GetData().CopyTo(zLocArray); + dataSet.GetField("haloId").GetData().CopyTo(haloIdArray); + dataSet.GetField("mbp").GetData().CopyTo(mbpArray); + + // Output haloId, MBP, potential per particle + vtkm::cont::ArrayHandle resultHaloId; + vtkm::cont::ArrayHandle resultMBP; + vtkm::cont::ArrayHandle resultPot; + + // Create the worklet and run it + vtkm::Id minHaloSize = 3; + vtkm::Float32 linkingLength = 1.0f; + vtkm::Float32 particleMass = 1.0f; + + vtkm::worklet::CosmoTools cosmoTools; + cosmoTools.RunHaloFinder(xLocArray, + yLocArray, + zLocArray, + nCells, + particleMass, + minHaloSize, + linkingLength, + resultHaloId, + resultMBP, + resultPot, + VTKM_DEFAULT_DEVICE_ADAPTER_TAG()); + + VTKM_TEST_ASSERT(TestArrayHandle(haloIdArray, resultHaloId, nCells), "Incorrect Halo Ids"); + VTKM_TEST_ASSERT(TestArrayHandle(mbpArray, resultMBP, nCells), "Incorrect MBP Ids"); +} + +//////////////////////////////////////////////////////////////////////////////////// +// +// Create an explicit 3D particle set and find halos and minimum potential particle of each halo +// +//////////////////////////////////////////////////////////////////////////////////// + +void TestCosmo_3DHaloFind() +{ + std::cout << "Testing Halo Finder 3D" << std::endl; + + // Create the input 3D particle dataset + vtkm::cont::DataSet dataSet = MakeCosmo_3DDataSet_0(); + vtkm::Id nCells = dataSet.GetCellSet(0).GetNumberOfCells(); + + vtkm::cont::ArrayHandle xLocArray; + vtkm::cont::ArrayHandle yLocArray; + vtkm::cont::ArrayHandle zLocArray; + vtkm::cont::ArrayHandle haloIdArray; + vtkm::cont::ArrayHandle mbpArray; + + dataSet.GetField("xLocation").GetData().CopyTo(xLocArray); + dataSet.GetField("yLocation").GetData().CopyTo(yLocArray); + dataSet.GetField("zLocation").GetData().CopyTo(zLocArray); + dataSet.GetField("haloId").GetData().CopyTo(haloIdArray); + dataSet.GetField("mbp").GetData().CopyTo(mbpArray); + + // Output haloId, MBP, potential per particle + vtkm::cont::ArrayHandle resultHaloId; + vtkm::cont::ArrayHandle resultMBP; + vtkm::cont::ArrayHandle resultPot; + + // Create the worklet and run it + vtkm::Id minHaloSize = 3; + vtkm::Float32 linkingLength = 0.2f; + vtkm::Float32 particleMass = 1.0f; + + vtkm::worklet::CosmoTools cosmoTools; + cosmoTools.RunHaloFinder(xLocArray, + yLocArray, + zLocArray, + nCells, + particleMass, + minHaloSize, + linkingLength, + resultHaloId, + resultMBP, + resultPot, + VTKM_DEFAULT_DEVICE_ADAPTER_TAG()); + + VTKM_TEST_ASSERT(TestArrayHandle(haloIdArray, resultHaloId, nCells), "Incorrect Halo Ids"); + VTKM_TEST_ASSERT(TestArrayHandle(mbpArray, resultMBP, nCells), "Incorrect MBP Ids"); +} + +//////////////////////////////////////////////////////////////////////////////////// +// +// Create an explicit 3D particle set and find halos and minimum potential particle of each halo +// +//////////////////////////////////////////////////////////////////////////////////// + +void TestCosmo_3DCenterFind() +{ + std::cout << "Testing Center Finder 3D" << std::endl; + + // Create the input 3D particle dataset + vtkm::cont::DataSet dataSet = MakeCosmo_3DDataSet_0(); + vtkm::Id nCells = dataSet.GetCellSet(0).GetNumberOfCells(); + + vtkm::cont::ArrayHandle xLocArray; + vtkm::cont::ArrayHandle yLocArray; + vtkm::cont::ArrayHandle zLocArray; + vtkm::cont::ArrayHandle haloIdArray; + dataSet.GetField("xLocation").GetData().CopyTo(xLocArray); + dataSet.GetField("yLocation").GetData().CopyTo(yLocArray); + dataSet.GetField("zLocation").GetData().CopyTo(zLocArray); + + // Output haloId MBP particleId pairs array + vtkm::Pair nxnResult; + vtkm::Pair mxnResult; + + // Create the worklet and run it + vtkm::Float32 particleMass = 1.0f; + + vtkm::worklet::CosmoTools cosmoTools; + cosmoTools.RunMBPCenterFinderNxN(xLocArray, + yLocArray, + zLocArray, + nCells, + particleMass, + nxnResult, + VTKM_DEFAULT_DEVICE_ADAPTER_TAG()); + + cosmoTools.RunMBPCenterFinderMxN(xLocArray, + yLocArray, + zLocArray, + nCells, + particleMass, + mxnResult, + VTKM_DEFAULT_DEVICE_ADAPTER_TAG()); + + VTKM_TEST_ASSERT(test_equal(nxnResult.first, mxnResult.first), + "NxN and MxN got different results"); +} + +void TestCosmoTools() +{ + TestCosmo_2DHaloFind(); + TestCosmo_3DHaloFind(); + + TestCosmo_3DCenterFind(); +} + +int UnitTestCosmoTools(int, char* []) +{ + return vtkm::cont::testing::Testing::Run(TestCosmoTools); +}