| 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 <stddef.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <gtest/gtest.h> |
| 38 |
#include <string> |
| 39 |
#include "stk_topology/topology.hpp" // for topology, etc |
| 40 |
#include <stk_mesh/base/Field.hpp> // for Field |
| 41 |
#include <stk_mesh/base/MetaData.hpp> // for MetaData, put_field |
| 42 |
#include <stk_mesh/base/BulkData.hpp> // for BulkData |
| 43 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 44 |
#include <stk_io/FillMesh.hpp> |
| 45 |
#include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker |
| 46 |
|
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
size_t open_results_file(const std::string& resultsFileName, |
| 51 |
stk::io::StkMeshIoBroker& stkIo, |
| 52 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr, |
| 53 |
stk::mesh::Field<double>& elemField) |
| 54 |
{ |
| 55 |
size_t resultsFileIndex = stkIo.create_output_mesh(resultsFileName, stk::io::WRITE_RESULTS); |
| 56 |
stkIo.set_bulk_data(bulkPtr); |
| 57 |
stkIo.add_field(resultsFileIndex, elemField); |
| 58 |
stkIo.write_output_mesh(resultsFileIndex); |
| 59 |
return resultsFileIndex; |
| 60 |
} |
| 61 |
|
| 62 |
void remove_entity_from_mesh(stk::mesh::BulkData& bulk, |
| 63 |
stk::mesh::EntityRank rank, |
| 64 |
stk::mesh::EntityId entityId) |
| 65 |
{ |
| 66 |
bulk.modification_begin(); |
| 67 |
stk::mesh::Entity entity = bulk.get_entity(rank, entityId); |
| 68 |
EXPECT_TRUE(bulk.is_valid(entity)); |
| 69 |
bulk.destroy_entity(entity); |
| 70 |
bulk.modification_end(); |
| 71 |
} |
| 72 |
|
| 73 |
void test_results_output(MPI_Comm comm, |
| 74 |
const std::string& fileName, |
| 75 |
int goldNumSteps) |
| 76 |
{ |
| 77 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(comm) |
| 78 |
.set_spatial_dimension(3).create(); |
| 79 |
stk::io::StkMeshIoBroker stkIo; |
| 80 |
stk::io::fill_mesh_preexisting(stkIo, fileName, *bulkPtr); |
| 81 |
EXPECT_EQ(goldNumSteps, stkIo.get_num_time_steps()); |
| 82 |
unlink(fileName.c_str()); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(StkIoResultsOutputMeshMod, writeResultsElemDelete) |
| 86 |
{ |
| 87 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 88 |
if (stk::parallel_machine_size(comm) > 1) { GTEST_SKIP(); } |
| 89 |
|
| 90 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(comm) |
| 91 |
.set_spatial_dimension(3).create(); |
| 92 |
stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data(); |
| 93 |
|
| 94 |
stk::mesh::Field<double>& elemField = meta.declare_field<double>(stk::topology::ELEM_RANK, "myElementField"); |
| 95 |
stk::mesh::put_field_on_mesh(elemField, meta.universal_part(), 1, nullptr); |
| 96 |
|
| 97 |
const std::string meshDesc("generated:1x1x2|sideset:x"); |
| 98 |
stk::io::fill_mesh(meshDesc, *bulkPtr); |
| 99 |
|
| 100 |
const std::string resultsFileName("results.e"); |
| 101 |
const std::string resultsFile2Name("results.e-s0002"); |
| 102 |
|
| 103 |
stk::io::StkMeshIoBroker stkIo(comm); |
| 104 |
size_t resultsFileIndex = open_results_file(resultsFileName, stkIo, bulkPtr, elemField); |
| 105 |
|
| 106 |
constexpr int nSteps = 5; |
| 107 |
for(int i = 1; i <= nSteps; ++i) { |
| 108 |
if (i == 3) { |
| 109 |
stk::mesh::EntityId elemId = 2; |
| 110 |
remove_entity_from_mesh(*bulkPtr, stk::topology::ELEM_RANK, elemId); |
| 111 |
|
| 112 |
stkIo.close_output_mesh(resultsFileIndex); |
| 113 |
resultsFileIndex = open_results_file(resultsFile2Name, stkIo, bulkPtr, elemField); |
| 114 |
} |
| 115 |
|
| 116 |
stkIo.process_output_request(resultsFileIndex, (double)(i-1)); |
| 117 |
} |
| 118 |
|
| 119 |
stkIo.close_output_mesh(resultsFileIndex); |
| 120 |
|
| 121 |
constexpr int nSteps_before_meshMod = 2; |
| 122 |
constexpr int nSteps_after_meshMod = 3; |
| 123 |
test_results_output(comm, resultsFileName, nSteps_before_meshMod); |
| 124 |
test_results_output(comm, resultsFile2Name, nSteps_after_meshMod); |
| 125 |
} |
| 126 |
|
| 127 |
void test_DTIO_results_output(MPI_Comm comm, |
| 128 |
const std::string& fileName, |
| 129 |
const std::vector<std::pair<int, int>> &goldGroupAndNumSteps) |
| 130 |
{ |
| 131 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(comm) |
| 132 |
.set_spatial_dimension(3).create(); |
| 133 |
stk::io::StkMeshIoBroker stkIo; |
| 134 |
stk::io::fill_mesh_preexisting(stkIo, fileName, *bulkPtr); |
| 135 |
|
| 136 |
int goldNumGroups = goldGroupAndNumSteps.size(); |
| 137 |
EXPECT_EQ(goldNumGroups, stkIo.num_mesh_groups()); |
| 138 |
|
| 139 |
for(const auto& entry : goldGroupAndNumSteps) { |
| 140 |
int meshGroup = entry.first; |
| 141 |
int numSteps = entry.second; |
| 142 |
|
| 143 |
EXPECT_TRUE(stkIo.load_mesh_group(meshGroup)); |
| 144 |
EXPECT_EQ(numSteps, stkIo.get_num_time_steps()); |
| 145 |
} |
| 146 |
unlink(fileName.c_str()); |
| 147 |
} |
| 148 |
|
| 149 |
TEST(StkIoResultsOutputMeshMod, writeResultsElemDeleteWithDTIO) |
| 150 |
{ |
| 151 |
MPI_Comm comm = MPI_COMM_WORLD; |
| 152 |
if (stk::parallel_machine_size(comm) > 1) { GTEST_SKIP(); } |
| 153 |
|
| 154 |
std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(comm) |
| 155 |
.set_spatial_dimension(3).create(); |
| 156 |
stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data(); |
| 157 |
|
| 158 |
stk::mesh::Field<double>& elemField = meta.declare_field<double>(stk::topology::ELEM_RANK, "myElementField"); |
| 159 |
stk::mesh::put_field_on_mesh(elemField, meta.universal_part(), 1, nullptr); |
| 160 |
|
| 161 |
const std::string meshDesc("generated:1x1x2|sideset:x"); |
| 162 |
stk::io::fill_mesh(meshDesc, *bulkPtr); |
| 163 |
|
| 164 |
const std::string resultsFileName("results.e"); |
| 165 |
|
| 166 |
stk::io::StkMeshIoBroker stkIo(comm); |
| 167 |
stkIo.enable_dynamic_topology(stk::io::FileOption::USE_DYNAMIC_TOPOLOGY_GROUP_FILE); |
| 168 |
size_t resultsFileIndex = open_results_file(resultsFileName, stkIo, bulkPtr, elemField); |
| 169 |
|
| 170 |
constexpr int nSteps = 5; |
| 171 |
for(int i = 1; i <= nSteps; ++i) { |
| 172 |
if (i == 3) { |
| 173 |
stk::mesh::EntityId elemId = 2; |
| 174 |
remove_entity_from_mesh(*bulkPtr, stk::topology::ELEM_RANK, elemId); |
| 175 |
stkIo.set_topology_modification(resultsFileIndex, Ioss::TOPOLOGY_CREATEELEM); |
| 176 |
} |
| 177 |
|
| 178 |
stkIo.process_output_request(resultsFileIndex, (double)(i-1)); |
| 179 |
} |
| 180 |
|
| 181 |
stkIo.close_output_mesh(resultsFileIndex); |
| 182 |
|
| 183 |
constexpr int nSteps_before_meshMod = 2; |
| 184 |
constexpr int nSteps_after_meshMod = 3; |
| 185 |
|
| 186 |
std::vector<std::pair<int,int>> goldGroupAndNumSteps{ {0, nSteps_before_meshMod}, {1, nSteps_after_meshMod} }; |
| 187 |
test_DTIO_results_output(comm, resultsFileName, goldGroupAndNumSteps); |
| 188 |
} |
| 189 |
|
| 190 |
} |
| 191 |
|