| 1 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 2 |
#include <stk_unit_test_utils/getOption.h> |
| 3 |
#include <stk_mesh/base/Comm.hpp> |
| 4 |
#include <stk_mesh/base/DestroyElements.hpp> |
| 5 |
#include <stk_mesh/base/GetEntities.hpp> |
| 6 |
#include <stk_util/util/string_utils.hpp> |
| 7 |
|
| 8 |
namespace |
| 9 |
{ |
| 10 |
|
| 11 |
class StkToolsC : public stk::unit_test_util::MeshFixture |
| 12 |
{}; |
| 13 |
|
| 14 |
TEST_F(StkToolsC, DeleteMeshExceptSpecifiedElems) |
| 15 |
{ |
| 16 |
const std::string unNamed = "mesh not specified"; |
| 17 |
const std::string meshName = stk::unit_test_util::get_option("-i", unNamed); |
| 18 |
STK_ThrowRequireMsg(meshName!=unNamed, "Please specify mesh with -i option."); |
| 19 |
setup_mesh(meshName, stk::mesh::BulkData::NO_AUTO_AURA); |
| 20 |
|
| 21 |
std::string invalidElemId = "-1"; |
| 22 |
std::string inputElemIds = stk::unit_test_util::get_command_line_option("-e", invalidElemId); |
| 23 |
STK_ThrowRequireMsg(inputElemIds != invalidElemId, "Please specify element list with -e."); |
| 24 |
|
| 25 |
std::set<stk::mesh::EntityId> elemIdsToKeep; |
| 26 |
std::vector<std::string> elemIdSegments = stk::split_csv_string(inputElemIds); |
| 27 |
for (const std::string & elemIdSegment : elemIdSegments) { |
| 28 |
const std::string trimmedElemId = stk::trim_string(elemIdSegment); |
| 29 |
const int elemId = std::stoi(trimmedElemId); |
| 30 |
elemIdsToKeep.insert(elemId); |
| 31 |
} |
| 32 |
|
| 33 |
const stk::mesh::BucketVector &buckets = get_bulk().get_buckets(stk::topology::ELEM_RANK, get_meta().locally_owned_part()); |
| 34 |
|
| 35 |
std::vector<size_t> entityCounts; |
| 36 |
stk::mesh::comm_mesh_counts(get_bulk(), entityCounts); |
| 37 |
|
| 38 |
std::cout << "num entities = " << entityCounts[stk::topology::ELEMENT_RANK] << std::endl; |
| 39 |
|
| 40 |
stk::mesh::EntityVector elementsToDestroy; |
| 41 |
for(size_t i=0;i<buckets.size();++i) |
| 42 |
{ |
| 43 |
const stk::mesh::Bucket& bucket = *buckets[i]; |
| 44 |
for(size_t j=0;j<bucket.size();j++) |
| 45 |
{ |
| 46 |
stk::mesh::Entity element = bucket[j]; |
| 47 |
stk::mesh::EntityId elemId = get_bulk().identifier(element); |
| 48 |
if (elemIdsToKeep.find(elemId) == elemIdsToKeep.end()) |
| 49 |
{ |
| 50 |
elementsToDestroy.push_back(element); |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
std::cout << "keeping element ID: " << elemId << std::endl; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
destroy_elements(get_bulk(), elementsToDestroy); |
| 59 |
stk::io::write_mesh("modified.e", get_bulk()); |
| 60 |
} |
| 61 |
|
| 62 |
void stk_determine_centroid(const unsigned spatial_dim, stk::mesh::Entity element, |
| 63 |
const stk::mesh::FieldBase& nodal_coord, std::vector<double> & centroid) |
| 64 |
{ |
| 65 |
centroid.resize(spatial_dim); |
| 66 |
std::fill(centroid.begin(), centroid.end(), 0.0); |
| 67 |
|
| 68 |
const stk::mesh::BulkData& bulk_data = nodal_coord.get_mesh(); |
| 69 |
const stk::mesh::Entity* const node_vec = bulk_data.begin_nodes(element); |
| 70 |
const unsigned num_nodes = bulk_data.num_nodes(element); |
| 71 |
|
| 72 |
for (unsigned iNode = 0; iNode < num_nodes; ++iNode) { |
| 73 |
stk::mesh::Entity node = node_vec[iNode]; |
| 74 |
double* coor = static_cast<double*>(stk::mesh::field_data(nodal_coord, node)); |
| 75 |
STK_ThrowRequire(coor); |
| 76 |
for (unsigned i = 0; i < spatial_dim; ++i) { |
| 77 |
centroid[i] += coor[i]; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
for(unsigned i = 0; i < spatial_dim; ++i) { |
| 82 |
centroid[i] /= num_nodes; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
TEST_F(StkToolsC, DeleteMeshExceptWithinBoundingBox) |
| 87 |
{ |
| 88 |
const std::string unNamed = "mesh not specified"; |
| 89 |
const std::string inputMeshName = stk::unit_test_util::get_option("-i", unNamed); |
| 90 |
STK_ThrowRequireMsg(inputMeshName!=unNamed, "Please specify mesh with -i option."); |
| 91 |
setup_mesh(inputMeshName, stk::mesh::BulkData::NO_AUTO_AURA); |
| 92 |
|
| 93 |
const std::string outputMeshName = stk::unit_test_util::get_option("-o", "modified.g"); |
| 94 |
|
| 95 |
double xLo = stk::unit_test_util::get_command_line_option("-x", std::numeric_limits<double>::lowest()); |
| 96 |
double xHi = stk::unit_test_util::get_command_line_option("-X", std::numeric_limits<double>::max()); |
| 97 |
double yLo = stk::unit_test_util::get_command_line_option("-y", std::numeric_limits<double>::lowest()); |
| 98 |
double yHi = stk::unit_test_util::get_command_line_option("-Y", std::numeric_limits<double>::max()); |
| 99 |
double zLo = stk::unit_test_util::get_command_line_option("-z", std::numeric_limits<double>::lowest()); |
| 100 |
double zHi = stk::unit_test_util::get_command_line_option("-Z", std::numeric_limits<double>::max()); |
| 101 |
|
| 102 |
const stk::mesh::BucketVector &buckets = get_bulk().get_buckets(stk::topology::ELEM_RANK, get_meta().locally_owned_part()); |
| 103 |
const stk::mesh::FieldBase * coordinates = get_meta().coordinate_field(); |
| 104 |
|
| 105 |
stk::mesh::EntityVector elementsToDestroy; |
| 106 |
std::vector<double> centroid(3); |
| 107 |
|
| 108 |
for (size_t i = 0; i < buckets.size(); ++i) { |
| 109 |
const stk::mesh::Bucket& bucket = *buckets[i]; |
| 110 |
for (size_t j = 0; j < bucket.size(); ++j) { |
| 111 |
stk::mesh::Entity element = bucket[j]; |
| 112 |
stk_determine_centroid(3, element, *coordinates, centroid); |
| 113 |
|
| 114 |
if ((centroid[0] < xLo) || (centroid[0] > xHi) || |
| 115 |
(centroid[1] < yLo) || (centroid[1] > yHi) || |
| 116 |
(centroid[2] < zLo) || (centroid[2] > zHi)) { |
| 117 |
elementsToDestroy.push_back(element); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
destroy_elements(get_bulk(), elementsToDestroy); |
| 123 |
stk::io::write_mesh(outputMeshName, get_bulk()); |
| 124 |
} |
| 125 |
|
| 126 |
TEST_F(StkToolsC, FlipElementConnectivity) |
| 127 |
{ |
| 128 |
const std::string unNamed = "mesh not specified"; |
| 129 |
const std::string meshName = stk::unit_test_util::get_option("-i", unNamed); |
| 130 |
STK_ThrowRequireMsg(meshName!=unNamed, "Please specify mesh with -i option."); |
| 131 |
setup_mesh(meshName, stk::mesh::BulkData::NO_AUTO_AURA); |
| 132 |
|
| 133 |
int invalidBlockId = -1; |
| 134 |
int inputBlockId = stk::unit_test_util::get_command_line_option("-b", invalidBlockId); |
| 135 |
STK_ThrowRequireMsg(inputBlockId!=invalidBlockId, "Please specify block with -b."); |
| 136 |
|
| 137 |
std::ostringstream os; |
| 138 |
os << "block_" << inputBlockId; |
| 139 |
stk::mesh::Part* elemBlock = get_meta().get_part(os.str()); |
| 140 |
|
| 141 |
stk::mesh::EntityVector elems; |
| 142 |
stk::mesh::BucketVector buckets = get_bulk().buckets(stk::topology::ELEM_RANK); |
| 143 |
stk::mesh::get_selected_entities(stk::mesh::Selector(*elemBlock), buckets, elems); |
| 144 |
|
| 145 |
get_bulk().modification_begin(); |
| 146 |
for (auto elem : elems) |
| 147 |
{ |
| 148 |
stk::topology topology = get_bulk().bucket(elem).topology(); |
| 149 |
STK_ThrowRequireMsg(topology == stk::topology::HEX_8, "Input block must have HEX_8 topology but found topology " << topology); |
| 150 |
|
| 151 |
stk::mesh::EntityVector storedNodes; |
| 152 |
const stk::mesh::Entity* node = get_bulk().begin_nodes(elem); |
| 153 |
unsigned numNodes = get_bulk().num_nodes(elem); |
| 154 |
for (unsigned i = 0; i < numNodes; ++i) |
| 155 |
{ |
| 156 |
storedNodes.push_back(node[i]); |
| 157 |
} |
| 158 |
for (unsigned i = 0; i < numNodes; ++i) |
| 159 |
{ |
| 160 |
get_bulk().destroy_relation(elem, storedNodes[i], i); |
| 161 |
} |
| 162 |
for (unsigned i = 0; i < numNodes; ++i) |
| 163 |
{ |
| 164 |
unsigned correctNodeId = (i+4)%8; |
| 165 |
get_bulk().declare_relation(elem, storedNodes[correctNodeId], i); |
| 166 |
} |
| 167 |
} |
| 168 |
get_bulk().modification_end(); |
| 169 |
stk::io::write_mesh("flipped.e", get_bulk()); |
| 170 |
} |
| 171 |
} |