| 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 |
#include "stk_performance_tests/stk_mesh/multi_block.hpp" |
| 36 |
#include "stk_mesh/base/GetEntities.hpp" |
| 37 |
#include "stk_mesh/base/SkinBoundary.hpp" |
| 38 |
#include "stk_mesh/base/ExodusTranslator.hpp" |
| 39 |
#include "stk_io/IossBridge.hpp" |
| 40 |
|
| 41 |
namespace stk { |
| 42 |
namespace performance_tests { |
| 43 |
|
| 44 |
void setup_multiple_blocks(stk::mesh::MetaData& meta, unsigned numBlocks) |
| 45 |
{ |
| 46 |
std::string block1Name("block_1"); |
| 47 |
stk::mesh::Part& block1Part = meta.declare_part_with_topology(block1Name, stk::topology::HEX_8); |
| 48 |
meta.set_part_id(block1Part, 1); |
| 49 |
stk::io::put_io_part_attribute(block1Part); |
| 50 |
|
| 51 |
unsigned partId = 10; |
| 52 |
for (unsigned i = 1; i < numBlocks; i++) { |
| 53 |
std::string blockName = "block_" + std::to_string(partId); |
| 54 |
stk::mesh::Part& part = meta.declare_part_with_topology(blockName, stk::topology::HEX_8); |
| 55 |
meta.set_part_id(part, partId); |
| 56 |
stk::io::put_io_part_attribute(part); |
| 57 |
++partId; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void setup_elem_fields_on_blocks(stk::mesh::MetaData& meta, unsigned numFields, bool allFieldsSameSize) |
| 62 |
{ |
| 63 |
{ |
| 64 |
std::vector<stk::mesh::FieldBase*> fields(numFields); |
| 65 |
for(unsigned f=0; f<numFields; ++f) { |
| 66 |
fields[f] = &meta.declare_field<double>(stk::topology::ELEM_RANK, std::string("elemField"+std::to_string(f))); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
unsigned scalarsPerEntity = 1; |
| 71 |
stk::mesh::PartVector elemBlocks; |
| 72 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlocks); |
| 73 |
for(stk::mesh::Part* elemBlock : elemBlocks) { |
| 74 |
for(stk::mesh::FieldBase* field : meta.get_fields(stk::topology::ELEM_RANK)) { |
| 75 |
stk::mesh::put_field_on_mesh(*field, *elemBlock, scalarsPerEntity, nullptr); |
| 76 |
} |
| 77 |
if(!allFieldsSameSize) { |
| 78 |
++scalarsPerEntity; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
std::string sideset_name_between_blocks(unsigned leftBlock, unsigned rightBlock) |
| 84 |
{ |
| 85 |
return "surfaceBetween_" + std::to_string(leftBlock) + "_and_" + std::to_string(rightBlock); |
| 86 |
} |
| 87 |
|
| 88 |
void setup_sidesets_between_blocks(stk::mesh::MetaData& meta) |
| 89 |
{ |
| 90 |
stk::mesh::PartVector elemBlocks; |
| 91 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlocks); |
| 92 |
const unsigned numBlocks = elemBlocks.size(); |
| 93 |
unsigned partId = 10; |
| 94 |
for(unsigned i = 1; i < numBlocks; i++) { |
| 95 |
unsigned leftBlockId = elemBlocks[i-1]->id(); |
| 96 |
unsigned rightBlockId = elemBlocks[i]->id(); |
| 97 |
std::string sidesetName = sideset_name_between_blocks(leftBlockId, rightBlockId)+"_ss1"; |
| 98 |
stk::mesh::Part& part = meta.declare_part(sidesetName, meta.side_rank()); |
| 99 |
meta.set_part_id(part, partId++); |
| 100 |
|
| 101 |
std::string sidesetName2 = sideset_name_between_blocks(leftBlockId, rightBlockId)+"_ss2"; |
| 102 |
stk::mesh::Part& part2 = meta.declare_part(sidesetName2, meta.side_rank()); |
| 103 |
meta.set_part_id(part2, partId++); |
| 104 |
|
| 105 |
stk::mesh::Part& leftBlockPart = *elemBlocks[i-1]; |
| 106 |
stk::mesh::Part& rightBlockPart = *elemBlocks[i]; |
| 107 |
|
| 108 |
meta.set_surface_to_block_mapping(&part, {&leftBlockPart, &rightBlockPart}); |
| 109 |
meta.set_surface_to_block_mapping(&part2, {&leftBlockPart, &rightBlockPart}); |
| 110 |
|
| 111 |
stk::io::put_io_part_attribute(part); |
| 112 |
stk::io::put_io_part_attribute(part2); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void setup_sidesets_for_blocks(stk::mesh::MetaData& meta) |
| 117 |
{ |
| 118 |
stk::mesh::PartVector elemBlocks; |
| 119 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlocks); |
| 120 |
const unsigned numBlocks = elemBlocks.size(); |
| 121 |
for(unsigned i = 0; i < numBlocks; i++) { |
| 122 |
stk::mesh::Part& blockPart = *elemBlocks[i]; |
| 123 |
unsigned partId = blockPart.id(); |
| 124 |
|
| 125 |
std::string sidesetName = "surface_" + std::to_string(partId); |
| 126 |
stk::mesh::Part& part = meta.declare_part(sidesetName, meta.side_rank()); |
| 127 |
meta.set_part_id(part, partId); |
| 128 |
|
| 129 |
meta.set_surface_to_block_mapping(&part, {&blockPart}); |
| 130 |
|
| 131 |
stk::io::put_io_part_attribute(part); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
void move_elements_to_other_blocks(stk::mesh::BulkData& bulk, unsigned numElemsInDimX) |
| 136 |
{ |
| 137 |
stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 138 |
stk::mesh::PartVector elemBlocks; |
| 139 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlocks); |
| 140 |
const unsigned numBlocks = elemBlocks.size(); |
| 141 |
|
| 142 |
STK_ThrowRequireMsg(((numElemsInDimX % numBlocks) == 0), |
| 143 |
"Number of blocks (" << numBlocks << ") must divide evenly into numElemsInDimX (" << numElemsInDimX << ")"); |
| 144 |
|
| 145 |
stk::mesh::EntityVector elems; |
| 146 |
stk::mesh::Selector ownedHexes = meta.get_topology_root_part(stk::topology::HEX_8) & |
| 147 |
meta.locally_owned_part(); |
| 148 |
stk::mesh::get_selected_entities(ownedHexes, bulk.buckets(stk::topology::ELEMENT_RANK), elems); |
| 149 |
const stk::mesh::Part* block1Part = elemBlocks[0]; |
| 150 |
|
| 151 |
std::vector<stk::mesh::EntityVector> elemsToMove(elemBlocks.size()); |
| 152 |
|
| 153 |
unsigned blockIdx = 0; |
| 154 |
for(stk::mesh::Entity elem : elems) { |
| 155 |
elemsToMove[blockIdx].push_back(elem); |
| 156 |
++blockIdx; |
| 157 |
if (blockIdx == numBlocks) { |
| 158 |
blockIdx = 0; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
bulk.modification_begin(); |
| 163 |
|
| 164 |
for(unsigned i = 1; i < numBlocks; i++) { |
| 165 |
stk::mesh::Part* newBlock = elemBlocks[i]; |
| 166 |
|
| 167 |
bulk.change_entity_parts(elemsToMove[i], stk::mesh::ConstPartVector{newBlock}, stk::mesh::ConstPartVector{block1Part}); |
| 168 |
} |
| 169 |
|
| 170 |
bulk.modification_end(); |
| 171 |
} |
| 172 |
|
| 173 |
void move_elements_to_other_contiguous_blocks(stk::mesh::BulkData& bulk, unsigned numElemsInDimX) |
| 174 |
{ |
| 175 |
stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 176 |
stk::mesh::PartVector elemBlocks; |
| 177 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlocks); |
| 178 |
const unsigned numBlocks = elemBlocks.size(); |
| 179 |
|
| 180 |
STK_ThrowRequireMsg(((numElemsInDimX % numBlocks) == 0), |
| 181 |
"Number of blocks (" << numBlocks << ") must divide evenly into numElemsInDimX (" << numElemsInDimX << ")"); |
| 182 |
|
| 183 |
stk::mesh::EntityVector elems; |
| 184 |
stk::mesh::Selector ownedHexes = meta.get_topology_root_part(stk::topology::HEX_8) & |
| 185 |
meta.locally_owned_part(); |
| 186 |
stk::mesh::get_selected_entities(ownedHexes, bulk.buckets(stk::topology::ELEMENT_RANK), elems); |
| 187 |
const stk::mesh::Part* block1Part = elemBlocks[0]; |
| 188 |
|
| 189 |
std::vector<stk::mesh::EntityVector> elemsToMove(elemBlocks.size()); |
| 190 |
unsigned numElemsInDimXPerBlock = numElemsInDimX / numBlocks; |
| 191 |
|
| 192 |
for(stk::mesh::Entity elem : elems) { |
| 193 |
stk::mesh::EntityId id = bulk.identifier(elem); |
| 194 |
unsigned blockIdx = ((id - 1)%numElemsInDimX)/numElemsInDimXPerBlock; |
| 195 |
|
| 196 |
STK_ThrowRequire(blockIdx < numBlocks); |
| 197 |
elemsToMove[blockIdx].push_back(elem); |
| 198 |
} |
| 199 |
|
| 200 |
bulk.modification_begin(); |
| 201 |
|
| 202 |
for(unsigned i = 1; i < numBlocks; i++) { |
| 203 |
stk::mesh::Part* newBlock = elemBlocks[i]; |
| 204 |
|
| 205 |
bulk.change_entity_parts(elemsToMove[i], stk::mesh::ConstPartVector{newBlock}, stk::mesh::ConstPartVector{block1Part}); |
| 206 |
} |
| 207 |
|
| 208 |
bulk.modification_end(); |
| 209 |
} |
| 210 |
|
| 211 |
void fill_sideset(stk::mesh::BulkData& bulk, |
| 212 |
stk::mesh::Part& partToPutSidesInto, |
| 213 |
const stk::mesh::Selector& blockSelector) |
| 214 |
{ |
| 215 |
stk::mesh::create_interior_block_boundary_sides(bulk, blockSelector, |
| 216 |
stk::mesh::PartVector{&partToPutSidesInto}); |
| 217 |
stk::mesh::SideSet* sideset = nullptr; |
| 218 |
if (bulk.does_sideset_exist(partToPutSidesInto)) { |
| 219 |
sideset = &bulk.get_sideset(partToPutSidesInto); |
| 220 |
} |
| 221 |
else { |
| 222 |
sideset = &bulk.create_sideset(partToPutSidesInto); |
| 223 |
} |
| 224 |
|
| 225 |
stk::mesh::EntityVector sides; |
| 226 |
stk::mesh::get_selected_entities(partToPutSidesInto, bulk.buckets(stk::topology::FACE_RANK), sides); |
| 227 |
|
| 228 |
for(stk::mesh::Entity side : sides) { |
| 229 |
const unsigned numElems = bulk.num_elements(side); |
| 230 |
const stk::mesh::Entity* elems = bulk.begin_elements(side); |
| 231 |
const stk::mesh::ConnectivityOrdinal* ords = bulk.begin_element_ordinals(side); |
| 232 |
for(unsigned i=0; i<numElems; ++i) { |
| 233 |
sideset->add({elems[i], ords[i]}); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
void fill_sidesets_between_blocks(stk::mesh::BulkData& bulk) |
| 239 |
{ |
| 240 |
const stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 241 |
stk::mesh::PartVector elemBlocks; |
| 242 |
stk::mesh::fill_element_block_parts(meta, stk::topology::HEX_8, elemBlocks); |
| 243 |
const unsigned numBlocks = elemBlocks.size(); |
| 244 |
for(unsigned i = 1; i < numBlocks; i++) { |
| 245 |
stk::mesh::Part& leftBlockPart = *elemBlocks[i-1]; |
| 246 |
stk::mesh::Part& rightBlockPart = *elemBlocks[i]; |
| 247 |
std::string sidesetName = sideset_name_between_blocks(leftBlockPart.id(), rightBlockPart.id())+"_ss1"; |
| 248 |
stk::mesh::Part& sidesetPart = *meta.get_part(sidesetName); |
| 249 |
|
| 250 |
fill_sideset(bulk, sidesetPart, stk::mesh::Selector(leftBlockPart|rightBlockPart)); |
| 251 |
|
| 252 |
std::string sidesetName2 = sideset_name_between_blocks(leftBlockPart.id(), rightBlockPart.id())+"_ss2"; |
| 253 |
stk::mesh::Part& sidesetPart2 = *meta.get_part(sidesetName2); |
| 254 |
|
| 255 |
fill_sideset(bulk, sidesetPart2, stk::mesh::Selector(leftBlockPart|rightBlockPart)); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
}} |