| 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 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 35 |
#include <stk_unit_test_utils/unittestMeshUtils.hpp> |
| 36 |
#include <stk_util/parallel/ParallelReduce.hpp> |
| 37 |
#include <stk_util/environment/EnvData.hpp> |
| 38 |
#include <vector> |
| 39 |
#include <string> |
| 40 |
|
| 41 |
namespace { |
| 42 |
|
| 43 |
class DistributionFactor : public stk::unit_test_util::MeshFixture |
| 44 |
{ |
| 45 |
public: |
| 46 |
DistributionFactor() |
| 47 |
{ |
| 48 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 49 |
} |
| 50 |
|
| 51 |
void test_df_values(const stk::mesh::BulkData& bulk, const std::vector<std::pair<std::string, std::string>> & dfFieldMapping) |
| 52 |
{ |
| 53 |
const stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 54 |
|
| 55 |
for (const auto & dfFieldInfo : dfFieldMapping) { |
| 56 |
const stk::mesh::Field<double> & dfField = *meta.get_field<double>(stk::topology::FACE_RANK, dfFieldInfo.first); |
| 57 |
const stk::mesh::Part & sidesetPart = *meta.get_part(dfFieldInfo.second); |
| 58 |
|
| 59 |
const stk::mesh::EntityVector faces = stk::mesh::get_entities(bulk, stk::topology::FACE_RANK, sidesetPart); |
| 60 |
for (stk::mesh::Entity face : faces) { |
| 61 |
double * df = stk::mesh::field_data(dfField, face); |
| 62 |
unsigned fieldLength = stk::mesh::field_scalars_per_entity(dfField, face); |
| 63 |
for (unsigned i = 0; i < fieldLength; ++i) { |
| 64 |
EXPECT_EQ(2.0, df[i]); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void write_serial_df_mesh(const std::string & meshSpec, const std::string & fileName, |
| 71 |
const std::vector<std::pair<std::string, std::string>> & dfFieldMapping) |
| 72 |
{ |
| 73 |
if (stk::parallel_machine_rank(MPI_COMM_WORLD) == 0) { |
| 74 |
stk::mesh::MeshBuilder builder(MPI_COMM_SELF); |
| 75 |
builder.set_spatial_dimension(3); |
| 76 |
builder.set_aura_option(stk::mesh::BulkData::NO_AUTO_AURA); |
| 77 |
auto bulk = builder.create(); |
| 78 |
stk::mesh::MetaData & meta = bulk->mesh_meta_data(); |
| 79 |
|
| 80 |
for (const auto & dfFieldInfo : dfFieldMapping) { |
| 81 |
stk::mesh::FieldBase & dfField = meta.declare_field<double>(stk::topology::FACE_RANK, dfFieldInfo.first); |
| 82 |
stk::mesh::Part & sidesetPart = meta.declare_part_with_topology(dfFieldInfo.second, stk::topology::QUAD_4); |
| 83 |
|
| 84 |
const unsigned numNodes = sidesetPart.topology().num_nodes(); |
| 85 |
stk::mesh::put_field_on_mesh(dfField, sidesetPart, numNodes, nullptr); |
| 86 |
|
| 87 |
stk::io::set_field_role(dfField, Ioss::Field::MESH); |
| 88 |
stk::io::set_distribution_factor_field(sidesetPart, dfField); |
| 89 |
} |
| 90 |
|
| 91 |
stk::io::fill_mesh(meshSpec, *bulk); |
| 92 |
|
| 93 |
for (const auto & dfFieldInfo : dfFieldMapping) { |
| 94 |
const stk::mesh::Field<double> & dfField = *meta.get_field<double>(stk::topology::FACE_RANK, dfFieldInfo.first); |
| 95 |
const stk::mesh::Part & sidesetPart = *meta.get_part(dfFieldInfo.second); |
| 96 |
|
| 97 |
const stk::mesh::EntityVector faces = stk::mesh::get_entities(*bulk, stk::topology::FACE_RANK, sidesetPart); |
| 98 |
for (stk::mesh::Entity face : faces) { |
| 99 |
double * df = stk::mesh::field_data(dfField, face); |
| 100 |
unsigned fieldLength = stk::mesh::field_scalars_per_entity(dfField, face); |
| 101 |
for (unsigned i = 0; i < fieldLength; ++i) { |
| 102 |
df[i] = 2.0; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
stk::io::write_mesh(fileName, *bulk); |
| 108 |
} |
| 109 |
MPI_Barrier(MPI_COMM_WORLD); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
std::string get_text_mesh_spec() |
| 114 |
{ |
| 115 |
const std::string meshSpec = "textmesh:0,1,HEX_8,1,2,5,4,10,11,14,13,block_1\n" |
| 116 |
"0,2,HEX_8,2,3,6,5,11,12,15,14,block_2\n" |
| 117 |
"0,3,HEX_8,4,5,8,7,13,14,17,16,block_3\n" |
| 118 |
"0,4,HEX_8,5,6,9,8,14,15,18,17,block_4\n" |
| 119 |
"|coordinates: 0,0,0, 1,0,0, 2,0,0, 0,1,0, 1,1,0, 2,1,0, 0,2,0, 1,2,0, 2,2,0, 0,0,1, 1,0,1, 2,0,1, 0,1,1, 1,1,1, 2,1,1, 0,2,1, 1,2,1, 2,2,1" |
| 120 |
"|sideset:name=internal_surface; data=1,3, 1,2, 2,3, 3,2; split=block"; |
| 121 |
|
| 122 |
return meshSpec; |
| 123 |
} |
| 124 |
|
| 125 |
std::string get_text_mesh_spec_2elem() |
| 126 |
{ |
| 127 |
const std::string meshSpec = "textmesh:0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n" |
| 128 |
"0,2,HEX_8,5,6,7,8,9,10,11,12,block_2\n" |
| 129 |
"|coordinates: 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, 0,0,2, 1,0,2, 1,1,2, 0,1,2" |
| 130 |
"|sideset:name=internal_surface; data=1,6; split=block"; |
| 131 |
|
| 132 |
return meshSpec; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
TEST_F(DistributionFactor, load_mesh_then_check_DF) |
| 137 |
{ |
| 138 |
const std::string fileName = "df_mesh.g"; |
| 139 |
const std::string meshDesc = get_text_mesh_spec_2elem(); |
| 140 |
|
| 141 |
const std::vector<std::pair<std::string, std::string>> dfFieldMapping = {{"internal_surface_df", "internal_surface"}}; |
| 142 |
write_serial_df_mesh(meshDesc, fileName, dfFieldMapping); |
| 143 |
MPI_Barrier(stk::parallel_machine_world()); |
| 144 |
|
| 145 |
stk::io::StkMeshIoBroker stkIo(stk::parallel_machine_world()); |
| 146 |
stkIo.property_add(Ioss::Property("DECOMPOSITION_METHOD", "RCB")); |
| 147 |
stkIo.set_bulk_data(bulkData); |
| 148 |
stkIo.add_mesh_database(fileName, stk::io::READ_MESH); |
| 149 |
stkIo.create_input_mesh(); |
| 150 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 151 |
stkIo.populate_bulk_data(); |
| 152 |
|
| 153 |
stk::mesh::BulkData& bulkDataExtracted = stkIo.bulk_data(); |
| 154 |
|
| 155 |
test_df_values(bulkDataExtracted, dfFieldMapping); |
| 156 |
} |
| 157 |
|
| 158 |
TEST_F(DistributionFactor, load_mesh_then_check_DF_4elem) |
| 159 |
{ |
| 160 |
const std::string fileName = "df_mesh.g"; |
| 161 |
const std::string meshDesc = get_text_mesh_spec(); |
| 162 |
|
| 163 |
const std::vector<std::pair<std::string, std::string>> dfFieldMapping = {{"internal_surface_df", "internal_surface"}}; |
| 164 |
write_serial_df_mesh(meshDesc, fileName, dfFieldMapping); |
| 165 |
MPI_Barrier(stk::parallel_machine_world()); |
| 166 |
|
| 167 |
stk::io::StkMeshIoBroker stkIo(stk::parallel_machine_world()); |
| 168 |
stkIo.property_add(Ioss::Property("DECOMPOSITION_METHOD", "RIB")); |
| 169 |
stkIo.set_bulk_data(bulkData); |
| 170 |
stkIo.add_mesh_database(fileName, stk::io::READ_MESH); |
| 171 |
stkIo.create_input_mesh(); |
| 172 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 173 |
stkIo.populate_bulk_data(); |
| 174 |
|
| 175 |
stk::mesh::BulkData& bulkDataExtracted = stkIo.bulk_data(); |
| 176 |
test_df_values(bulkDataExtracted, dfFieldMapping); |
| 177 |
} |
| 178 |
|
| 179 |
TEST_F(DistributionFactor, change_entity_owner_then_check_DF) |
| 180 |
{ |
| 181 |
if (stk::parallel_machine_size(stk::parallel_machine_world()) < 3) |
| 182 |
{ |
| 183 |
GTEST_SKIP(); |
| 184 |
} |
| 185 |
const std::string fileName = "df_mesh.g"; |
| 186 |
const std::string meshDesc = get_text_mesh_spec_2elem(); |
| 187 |
|
| 188 |
const std::vector<std::pair<std::string, std::string>> dfFieldMapping = {{"internal_surface_df", "internal_surface"}}; |
| 189 |
write_serial_df_mesh(meshDesc, fileName, dfFieldMapping); |
| 190 |
MPI_Barrier(stk::parallel_machine_world()); |
| 191 |
|
| 192 |
stk::io::StkMeshIoBroker stkIo(stk::parallel_machine_world()); |
| 193 |
stkIo.property_add(Ioss::Property("DECOMPOSITION_METHOD", "LINEAR")); |
| 194 |
stkIo.set_bulk_data(bulkData); |
| 195 |
stkIo.add_mesh_database(fileName, stk::io::READ_MESH); |
| 196 |
stkIo.create_input_mesh(); |
| 197 |
stkIo.add_all_mesh_fields_as_input_fields(); |
| 198 |
stkIo.populate_bulk_data(); |
| 199 |
|
| 200 |
stk::mesh::BulkData& bulkDataExtracted = stkIo.bulk_data(); |
| 201 |
test_df_values(bulkDataExtracted, dfFieldMapping); |
| 202 |
|
| 203 |
stk::mesh::EntityProcVec entitiesToChange; |
| 204 |
if (stk::parallel_machine_rank(stk::parallel_machine_world()) == 1) |
| 205 |
{ |
| 206 |
stk::mesh::Entity element = bulkDataExtracted.get_entity(stk::topology::ELEMENT_RANK, 2); |
| 207 |
int newOwnerProc = 2; |
| 208 |
entitiesToChange.push_back(std::make_pair(element, newOwnerProc)); |
| 209 |
for (int i=9; i <= 12; ++i) |
| 210 |
{ |
| 211 |
stk::mesh::Entity node = bulkDataExtracted.get_entity(stk::topology::NODE_RANK, i); |
| 212 |
entitiesToChange.push_back(std::make_pair(node, newOwnerProc)); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
bulkDataExtracted.change_entity_owner(entitiesToChange); |
| 217 |
test_df_values(bulkDataExtracted, dfFieldMapping); |
| 218 |
} |
| 219 |
|
| 220 |
} |