| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#ifndef calculate_centroid_hpp |
| 36 |
#define calculate_centroid_hpp |
| 37 |
|
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include "stk_mesh/base/NgpMesh.hpp" |
| 41 |
#include "stk_mesh/base/NgpField.hpp" |
| 42 |
#include "stk_mesh/base/GetNgpField.hpp" |
| 43 |
#include "stk_mesh/base/GetNgpMesh.hpp" |
| 44 |
#include "stk_mesh/base/GetEntities.hpp" |
| 45 |
#include "stk_mesh/base/ForEachEntity.hpp" |
| 46 |
|
| 47 |
namespace stk { |
| 48 |
namespace performance_tests { |
| 49 |
|
| 50 |
/** |
| 51 |
num_nodes: number of nodes connected to the element |
| 52 |
elem_node_coords: array of length num_nodes*3, containing the |
| 53 |
coordinates for an element's nodes |
| 54 |
elem_centroid: array of length 3 |
| 55 |
*/ |
| 56 |
template<class T> |
| 57 |
inline |
| 58 |
void |
| 59 |
calculate_centroid_3d( |
| 60 |
size_t num_nodes, |
| 61 |
const T * elem_node_coords, |
| 62 |
T * elem_centroid) |
| 63 |
{ |
| 64 |
//compute the element-centroid: |
| 65 |
for(size_t n = 0; n < num_nodes; ++n) { |
| 66 |
elem_centroid[0] += elem_node_coords[n*3 + 0]; |
| 67 |
elem_centroid[1] += elem_node_coords[n*3 + 1]; |
| 68 |
elem_centroid[2] += elem_node_coords[n*3 + 2]; |
| 69 |
} |
| 70 |
elem_centroid[0] /= num_nodes; |
| 71 |
elem_centroid[1] /= num_nodes; |
| 72 |
elem_centroid[2] /= num_nodes; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
num_elements: number of element |
| 77 |
num_nodes: number of nodes connected to the element |
| 78 |
elem_node_coords: array of length num_nodes*3, containing the |
| 79 |
coordinates for an element's nodes |
| 80 |
elem_centroid: array of length 3 |
| 81 |
*/ |
| 82 |
template<class T> |
| 83 |
inline |
| 84 |
void |
| 85 |
calculate_centroid_3d( |
| 86 |
size_t num_elements, |
| 87 |
size_t num_nodes, |
| 88 |
const T * elem_node_coords, |
| 89 |
T * elem_centroid) |
| 90 |
{ |
| 91 |
//compute the element-centroid: |
| 92 |
for (size_t i = 0; i < num_elements; ++i) { |
| 93 |
for(size_t n = 0; n < num_nodes; ++n) { |
| 94 |
elem_centroid[i*3 + 0] += elem_node_coords[i*num_nodes*3 + n*3 + 0]; |
| 95 |
elem_centroid[i*3 + 1] += elem_node_coords[i*num_nodes*3 + n*3 + 1]; |
| 96 |
elem_centroid[i*3 + 2] += elem_node_coords[i*num_nodes*3 + n*3 + 2]; |
| 97 |
} |
| 98 |
elem_centroid[i*3 + 0] /= num_nodes; |
| 99 |
elem_centroid[i*3 + 1] /= num_nodes; |
| 100 |
elem_centroid[i*3 + 2] /= num_nodes; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
template <typename CoordFieldType> |
| 105 |
void calculate_centroid(const stk::mesh::NgpMesh &ngpMesh, const CoordFieldType &ngpCoords, const stk::mesh::Selector &sel, stk::mesh::NgpField<double> &ngpCentroid) |
| 106 |
{ |
| 107 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, sel, |
| 108 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem) |
| 109 |
{ |
| 110 |
stk::mesh::NgpMesh::ConnectedNodes nodes = ngpMesh.get_nodes(stk::topology::ELEM_RANK, elem); |
| 111 |
const unsigned numComponents = ngpCentroid.get_num_components_per_entity(elem); |
| 112 |
if (numComponents == 0) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
stk::mesh::EntityFieldData<double> elemCentroid = ngpCentroid(elem); |
| 117 |
elemCentroid[0] = 0.0; |
| 118 |
elemCentroid[1] = 0.0; |
| 119 |
elemCentroid[2] = 0.0; |
| 120 |
|
| 121 |
for (size_t i = 0; i < nodes.size(); i++) { |
| 122 |
stk::mesh::FastMeshIndex nodeIndex = ngpMesh.fast_mesh_index(nodes[i]); |
| 123 |
const stk::mesh::EntityFieldData<double> nodeCoords = ngpCoords(nodeIndex); |
| 124 |
|
| 125 |
elemCentroid[0] += nodeCoords[0]; |
| 126 |
elemCentroid[1] += nodeCoords[1]; |
| 127 |
elemCentroid[2] += nodeCoords[2]; |
| 128 |
} |
| 129 |
|
| 130 |
elemCentroid[0] /= nodes.size(); |
| 131 |
elemCentroid[1] /= nodes.size(); |
| 132 |
elemCentroid[2] /= nodes.size(); |
| 133 |
}); |
| 134 |
ngpCentroid.modify_on_device(); |
| 135 |
} |
| 136 |
|
| 137 |
template <typename CoordFieldType> |
| 138 |
void calculate_centroid_using_coord_field(const stk::mesh::BulkData &bulk, const stk::mesh::Selector& selector, stk::mesh::FieldBase ¢roid) |
| 139 |
{ |
| 140 |
const stk::mesh::FieldBase& coords = *bulk.mesh_meta_data().coordinate_field(); |
| 141 |
stk::mesh::NgpField<double>& ngpCentroid = stk::mesh::get_updated_ngp_field<double>(centroid); |
| 142 |
stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk); |
| 143 |
|
| 144 |
stk::mesh::NgpField<double>& ngpCoords = stk::mesh::get_updated_ngp_field<double>(coords); |
| 145 |
calculate_centroid(ngpMesh, ngpCoords, selector, ngpCentroid); |
| 146 |
|
| 147 |
ngpCentroid.sync_to_host(); |
| 148 |
} |
| 149 |
|
| 150 |
template <typename CoordFieldType> |
| 151 |
void calculate_centroid_using_coord_field(const stk::mesh::BulkData &bulk, stk::mesh::FieldBase ¢roid) |
| 152 |
{ |
| 153 |
calculate_centroid_using_coord_field<CoordFieldType>(bulk, bulk.mesh_meta_data().locally_owned_part(), centroid); |
| 154 |
} |
| 155 |
|
| 156 |
inline |
| 157 |
void calc_centroid(const double** coordData, int numNodes, double* centroid) |
| 158 |
{ |
| 159 |
centroid[0] = 0.0; |
| 160 |
centroid[1] = 0.0; |
| 161 |
centroid[2] = 0.0; |
| 162 |
|
| 163 |
for(int i=0; i<numNodes; ++i) { |
| 164 |
centroid[0] += coordData[i][0]; |
| 165 |
centroid[1] += coordData[i][1]; |
| 166 |
centroid[2] += coordData[i][2]; |
| 167 |
} |
| 168 |
|
| 169 |
centroid[0] /= numNodes; |
| 170 |
centroid[1] /= numNodes; |
| 171 |
centroid[2] /= numNodes; |
| 172 |
} |
| 173 |
|
| 174 |
inline void calculate_centroid_using_host_coord_fields(const stk::mesh::BulkData& bulk, stk::mesh::FieldBase& centroid) |
| 175 |
{ |
| 176 |
stk::mesh::Selector selector(centroid); |
| 177 |
selector &= bulk.mesh_meta_data().locally_owned_part(); |
| 178 |
const stk::mesh::FieldBase& coords = *bulk.mesh_meta_data().coordinate_field(); |
| 179 |
|
| 180 |
constexpr size_t maxNumNodes = 8; |
| 181 |
const double*elemNodeCoords[maxNumNodes]; |
| 182 |
|
| 183 |
auto centroidCalculator = [&](stk::mesh::Entity elem, const stk::mesh::Entity* nodes, size_t numNodes) |
| 184 |
{ |
| 185 |
STK_ThrowAssertMsg(numNodes <= maxNumNodes, "numNodes("<<numNodes<<") must be <= maxNumNodes("<<maxNumNodes<<")"); |
| 186 |
|
| 187 |
for(size_t i = 0; i < numNodes; i++) { |
| 188 |
elemNodeCoords[i] = reinterpret_cast<double*>(stk::mesh::field_data(coords, nodes[i])); |
| 189 |
} |
| 190 |
|
| 191 |
double* centroidData = reinterpret_cast<double*>(stk::mesh::field_data(centroid, elem)); |
| 192 |
calc_centroid(elemNodeCoords, numNodes, centroidData); |
| 193 |
}; |
| 194 |
|
| 195 |
stk::mesh::for_each_entity_run_with_nodes(bulk, stk::topology::ELEM_RANK, selector, centroidCalculator); |
| 196 |
} |
| 197 |
|
| 198 |
inline |
| 199 |
std::vector<double> get_centroid_average_from_host(stk::mesh::BulkData &bulk, stk::mesh::Field<double> ¢roid, const stk::mesh::Selector& selector) |
| 200 |
{ |
| 201 |
std::vector<double> average = {0, 0, 0}; |
| 202 |
size_t numElems = 0; |
| 203 |
stk::mesh::Selector fieldSelector(centroid); |
| 204 |
fieldSelector &= selector; |
| 205 |
for (const stk::mesh::Bucket *bucket : bulk.get_buckets(stk::topology::ELEM_RANK, fieldSelector)) { |
| 206 |
for (stk::mesh::Entity elem : *bucket) { |
| 207 |
double *elemCentroid = stk::mesh::field_data(centroid, elem); |
| 208 |
for(size_t dim = 0; dim < 3; dim++) { |
| 209 |
average[dim] += elemCentroid[dim]; |
| 210 |
} |
| 211 |
numElems++; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if (numElems > 0) { |
| 216 |
for(size_t dim = 0; dim < 3; dim++) { |
| 217 |
average[dim] /= numElems; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return average; |
| 222 |
} |
| 223 |
|
| 224 |
inline |
| 225 |
std::vector<double> get_centroid_average_from_device(stk::mesh::BulkData &bulk, stk::mesh::Field<double> ¢roid, const stk::mesh::Selector& selector) |
| 226 |
{ |
| 227 |
stk::mesh::NgpField<double>& ngpField = stk::mesh::get_updated_ngp_field<double>(centroid); |
| 228 |
stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk); |
| 229 |
|
| 230 |
typedef Kokkos::View<double*, stk::ngp::MemSpace> DeviceAverageView; |
| 231 |
typedef typename DeviceAverageView::HostMirror HostAverageView; |
| 232 |
|
| 233 |
DeviceAverageView deviceAverageView("averageVew", 4); |
| 234 |
HostAverageView hostAverageView = Kokkos::create_mirror_view(deviceAverageView); |
| 235 |
|
| 236 |
Kokkos::deep_copy(deviceAverageView, 0.0); |
| 237 |
|
| 238 |
stk::mesh::Selector fieldSelector(centroid); |
| 239 |
fieldSelector &= selector; |
| 240 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, fieldSelector, |
| 241 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem) |
| 242 |
{ |
| 243 |
for(size_t dim = 0; dim < 3; dim++) { |
| 244 |
Kokkos::atomic_add(&deviceAverageView(dim), ngpField(elem, dim)); |
| 245 |
} |
| 246 |
Kokkos::atomic_inc(&deviceAverageView(3)); |
| 247 |
}); |
| 248 |
|
| 249 |
Kokkos::deep_copy(hostAverageView, deviceAverageView); |
| 250 |
|
| 251 |
std::vector<double> average = {0, 0, 0}; |
| 252 |
unsigned numElems = hostAverageView(3); |
| 253 |
|
| 254 |
for(size_t dim = 0; dim < 3; dim++) { |
| 255 |
average[dim] = numElems > 0 ? hostAverageView(dim) / numElems : 0.0; |
| 256 |
} |
| 257 |
|
| 258 |
return average; |
| 259 |
} |
| 260 |
|
| 261 |
} // namespace performance_tests |
| 262 |
} // namespace stk |
| 263 |
|
| 264 |
#endif |