1 //============================================================================
2 //  Copyright (c) Kitware, Inc.
3 //  All rights reserved.
4 //  See LICENSE.txt for details.
5 //  This software is distributed WITHOUT ANY WARRANTY; without even
6 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7 //  PURPOSE.  See the above copyright notice for more information.
8 //
9 //  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 //  Copyright 2014 UT-Battelle, LLC.
11 //  Copyright 2014 Los Alamos National Security.
12 //
13 //  Under the terms of Contract DE-NA0003525 with NTESS,
14 //  the U.S. Government retains certain rights in this software.
15 //
16 //  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
17 //  Laboratory (LANL), the U.S. Government retains certain rights in
18 //  this software.
19 //============================================================================
20 //  Copyright (c) 2016, Los Alamos National Security, LLC
21 //  All rights reserved.
22 //
23 //  Copyright 2016. Los Alamos National Security, LLC.
24 //  This software was produced under U.S. Government contract DE-AC52-06NA25396
25 //  for Los Alamos National Laboratory (LANL), which is operated by
26 //  Los Alamos National Security, LLC for the U.S. Department of Energy.
27 //  The U.S. Government has rights to use, reproduce, and distribute this
28 //  software.  NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC
29 //  MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE
30 //  USE OF THIS SOFTWARE.  If software is modified to produce derivative works,
31 //  such modified software should be clearly marked, so as not to confuse it
32 //  with the version available from LANL.
33 //
34 //  Additionally, redistribution and use in source and binary forms, with or
35 //  without modification, are permitted provided that the following conditions
36 //  are met:
37 //
38 //  1. Redistributions of source code must retain the above copyright notice,
39 //     this list of conditions and the following disclaimer.
40 //  2. Redistributions in binary form must reproduce the above copyright notice,
41 //     this list of conditions and the following disclaimer in the documentation
42 //     and/or other materials provided with the distribution.
43 //  3. Neither the name of Los Alamos National Security, LLC, Los Alamos
44 //     National Laboratory, LANL, the U.S. Government, nor the names of its
45 //     contributors may be used to endorse or promote products derived from
46 //     this software without specific prior written permission.
47 //
48 //  THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND
49 //  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
50 //  BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 //  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS
52 //  NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 //  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
55 //  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 //  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 //  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 //  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 //============================================================================
60 
61 #ifndef vtkm_worklet_cosmotools_compute_bins__h
62 #define vtkm_worklet_cosmotools_compute_bins__h
63 
64 #include <vtkm/worklet/WorkletMapField.h>
65 
66 namespace vtkm
67 {
68 namespace worklet
69 {
70 namespace cosmotools
71 {
72 
73 // Worklet for computing the bin id for every particle in domain
74 template <typename T>
75 class ComputeBins : public vtkm::worklet::WorkletMapField
76 {
77 public:
78   struct TagType : vtkm::ListTagBase<T>
79   {
80   };
81 
82   using ControlSignature = void(FieldIn<TagType> xLoc,   // (input) x location in halo
83                                 FieldIn<TagType> yLoc,   // (input) y location in halo
84                                 FieldIn<TagType> zLoc,   // (input) z location in halo
85                                 FieldOut<IdType> binId); // (output) bin Id
86   using ExecutionSignature = _4(_1, _2, _3);
87   using InputDomain = _1;
88 
89   T xMin, xMax, yMin, yMax, zMin, zMax;
90   vtkm::Id xNum, yNum, zNum;
91 
92   // Constructor
93   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)94   ComputeBins(T XMin,
95               T XMax,
96               T YMin,
97               T YMax,
98               T ZMin,
99               T ZMax,
100               vtkm::Id XNum,
101               vtkm::Id YNum,
102               vtkm::Id ZNum)
103     : xMin(XMin)
104     , xMax(XMax)
105     , yMin(YMin)
106     , yMax(YMax)
107     , zMin(ZMin)
108     , zMax(ZMax)
109     , xNum(XNum)
110     , yNum(YNum)
111     , zNum(ZNum)
112   {
113   }
114 
115   VTKM_EXEC
operator()116   vtkm::Id operator()(const T& xLoc, const T& yLoc, const T& zLoc) const
117   {
118     vtkm::Id xbin = 0;
119     if (xNum > 1)
120     {
121       xbin = static_cast<vtkm::Id>((static_cast<T>(xNum) * (xLoc - xMin)) / (xMax - xMin));
122       if (xbin >= xNum)
123         xbin = xNum - 1;
124     }
125     vtkm::Id ybin = 0;
126     if (yNum > 1)
127     {
128       ybin = static_cast<vtkm::Id>((static_cast<T>(yNum) * (yLoc - yMin)) / (yMax - yMin));
129       if (ybin >= yNum)
130         ybin = yNum - 1;
131     }
132     vtkm::Id zbin = 0;
133     if (zNum > 1)
134     {
135       zbin = static_cast<vtkm::Id>((static_cast<T>(zNum) * (zLoc - zMin)) / (zMax - zMin));
136       if (zbin >= zNum)
137         zbin = zNum - 1;
138     }
139     return (xbin + ybin * xNum + zbin * xNum * yNum);
140   }
141 }; // ComputeBins
142 }
143 }
144 }
145 
146 #endif
147