| 1 |
// ####################### Start Clang Header Tool Managed Headers ######################## |
| 2 |
// clang-format off |
| 3 |
#include <gtest/gtest.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stk_mesh/base/BulkData.hpp> |
| 6 |
#include <stk_mesh/base/GetEntities.hpp> |
| 7 |
#include <stk_mesh/base/MetaData.hpp> |
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
#include "mpi.h" |
| 11 |
#include "stk_io/FillMesh.hpp" |
| 12 |
#include "stk_mesh/base/Entity.hpp" |
| 13 |
#include "stk_mesh/base/ForEachEntity.hpp" |
| 14 |
#include "stk_topology/topology.hpp" |
| 15 |
#include "stk_util/parallel/Parallel.hpp" |
| 16 |
#include "stk_unit_test_utils/BuildMesh.hpp" |
| 17 |
namespace stk { namespace mesh { struct MeshIndex; } } |
| 18 |
// clang-format on |
| 19 |
// ####################### End Clang Header Tool Managed Headers ######################## |
| 20 |
|
| 21 |
|
| 22 |
namespace |
| 23 |
{ |
| 24 |
using stk::unit_test_util::build_mesh; |
| 25 |
|
| 26 |
TEST(ForEachEntityRunNoThreads, test_counting_nodes) |
| 27 |
{ |
| 28 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 29 |
if(stk::parallel_machine_size(communicator) != 2) { GTEST_SKIP(); } |
| 30 |
|
| 31 |
const int spatialDim = 3; |
| 32 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator); |
| 33 |
stk::mesh::MetaData& metaData = bulkPtr->mesh_meta_data(); |
| 34 |
stk::mesh::BulkData& bulkData = *bulkPtr; |
| 35 |
|
| 36 |
std::string generatedMeshSpec = "generated:1x1x4"; |
| 37 |
stk::io::fill_mesh(generatedMeshSpec, bulkData); |
| 38 |
|
| 39 |
{//version that uses selector and functor that takes MeshIndex |
| 40 |
unsigned numNodes = 0; |
| 41 |
stk::mesh::for_each_entity_run_no_threads(bulkData, stk::topology::NODE_RANK, metaData.universal_part(), |
| 42 |
[&numNodes](const stk::mesh::BulkData & mesh, const stk::mesh::MeshIndex & meshIndex) |
| 43 |
{ |
| 44 |
stk::mesh::Entity entity = stk::mesh::impl::get_entity(meshIndex); |
| 45 |
if(mesh.is_valid(entity)) { |
| 46 |
numNodes++; |
| 47 |
} |
| 48 |
} |
| 49 |
); |
| 50 |
EXPECT_EQ(16u, numNodes); |
| 51 |
} |
| 52 |
{//version that uses selector and functor that takes Entity |
| 53 |
unsigned numNodes = 0; |
| 54 |
stk::mesh::for_each_entity_run_no_threads(bulkData, stk::topology::NODE_RANK, metaData.universal_part(), |
| 55 |
[&numNodes](const stk::mesh::BulkData & mesh, const stk::mesh::Entity entity) |
| 56 |
{ |
| 57 |
if(mesh.is_valid(entity)) { |
| 58 |
numNodes++; |
| 59 |
} |
| 60 |
} |
| 61 |
); |
| 62 |
EXPECT_EQ(16u, numNodes); |
| 63 |
} |
| 64 |
{//version with no selector and functor that takes MeshIndex |
| 65 |
unsigned numNodes = 0; |
| 66 |
stk::mesh::for_each_entity_run_no_threads(bulkData, stk::topology::NODE_RANK, |
| 67 |
[&numNodes](const stk::mesh::BulkData & mesh, const stk::mesh::MeshIndex & meshIndex) |
| 68 |
{ |
| 69 |
stk::mesh::Entity entity = stk::mesh::impl::get_entity(meshIndex); |
| 70 |
if(mesh.is_valid(entity)) { |
| 71 |
numNodes++; |
| 72 |
} |
| 73 |
} |
| 74 |
); |
| 75 |
EXPECT_EQ(16u, numNodes); |
| 76 |
} |
| 77 |
{//version with no selector and functor that takes Entity |
| 78 |
unsigned numNodes = 0; |
| 79 |
stk::mesh::for_each_entity_run_no_threads(bulkData, stk::topology::NODE_RANK, |
| 80 |
[&numNodes](const stk::mesh::BulkData & mesh, const stk::mesh::Entity entity) |
| 81 |
{ |
| 82 |
if(mesh.is_valid(entity)) { |
| 83 |
numNodes++; |
| 84 |
} |
| 85 |
} |
| 86 |
); |
| 87 |
EXPECT_EQ(16u, numNodes); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
TEST(SettingLocalIds, bucket_loop_iterates_in_same_order_as_for_each_entity_run) |
| 92 |
{ |
| 93 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 94 |
if(stk::parallel_machine_size(communicator) != 2) { GTEST_SKIP(); } |
| 95 |
|
| 96 |
const int spatialDim = 3; |
| 97 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = build_mesh(spatialDim, communicator); |
| 98 |
stk::mesh::MetaData& metaData = bulkPtr->mesh_meta_data(); |
| 99 |
stk::mesh::BulkData& bulkData = *bulkPtr; |
| 100 |
|
| 101 |
std::string generatedMeshSpec = "generated:1x1x4"; |
| 102 |
stk::io::fill_mesh(generatedMeshSpec, bulkData); |
| 103 |
|
| 104 |
const unsigned numNodes = stk::mesh::count_selected_entities(metaData.universal_part(), |
| 105 |
bulkData.buckets(stk::topology::NODE_RANK)); |
| 106 |
ASSERT_EQ(16u, numNodes); |
| 107 |
std::vector<unsigned> localIds(numNodes); |
| 108 |
unsigned index = 0; |
| 109 |
const stk::mesh::BucketVector& buckets = bulkData.get_buckets(stk::topology::NODE_RANK, metaData.universal_part()); |
| 110 |
for(const stk::mesh::Bucket* bptr : buckets) { |
| 111 |
for(stk::mesh::Entity node : *bptr) { |
| 112 |
if (bulkData.is_valid(node)) { |
| 113 |
bulkData.set_local_id(node, index); |
| 114 |
++index; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
stk::mesh::for_each_entity_run(bulkData, stk::topology::NODE_RANK, |
| 119 |
[&localIds](const stk::mesh::BulkData & mesh, const stk::mesh::MeshIndex & meshIndex) |
| 120 |
{ |
| 121 |
stk::mesh::Entity node = stk::mesh::impl::get_entity(meshIndex); |
| 122 |
if(mesh.is_valid(node)) |
| 123 |
{ |
| 124 |
unsigned localId = mesh.local_id(node); |
| 125 |
localIds[localId] = localId; |
| 126 |
} |
| 127 |
} |
| 128 |
); |
| 129 |
for(size_t i=0; i<numNodes; i++) |
| 130 |
{ |
| 131 |
EXPECT_EQ(i, localIds[i]); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
} |