| 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 <gtest/gtest.h> // for AssertHelper, EXPECT_NE, etc |
| 36 |
#include <stddef.h> // for size_t, NULL |
| 37 |
#include <unistd.h> // for unlink |
| 38 |
#include <stk_io/FillMesh.hpp> |
| 39 |
#include <stk_io/WriteMesh.hpp> |
| 40 |
#include <stk_io/StkMeshIoBroker.hpp> |
| 41 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 42 |
#include <stk_mesh/base/MetaData.hpp> |
| 43 |
#include <stk_mesh/base/BulkData.hpp> |
| 44 |
#include <stk_mesh/base/GetEntities.hpp> |
| 45 |
#include <string> // for string |
| 46 |
#include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc |
| 47 |
#include "stk_util/parallel/Parallel.hpp" |
| 48 |
namespace stk { namespace mesh { class Part; } } |
| 49 |
namespace { |
| 50 |
|
| 51 |
TEST(StkMeshIoBrokerHowTo, readMesh) |
| 52 |
{ |
| 53 |
std::string mesh_file_name = "input_mesh_example.e"; |
| 54 |
MPI_Comm communicator = MPI_COMM_WORLD; |
| 55 |
int numProcs = stk::parallel_machine_size(communicator); |
| 56 |
if (numProcs != 1) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
{ |
| 61 |
// ============================================================ |
| 62 |
//BeginBasicReadWrite |
| 63 |
std::shared_ptr<stk::mesh::BulkData> stkMesh = stk::mesh::MeshBuilder(communicator).create(); |
| 64 |
|
| 65 |
//+ Create a basic mesh with a hex block, 3 shell blocks, 3 nodesets, and 3 sidesets. |
| 66 |
const std::string generatedFileName = "generated:8x8x8|shell:xyz|nodeset:xyz|sideset:XYZ"; |
| 67 |
stk::io::fill_mesh(generatedFileName, *stkMesh); |
| 68 |
stk::io::write_mesh(mesh_file_name, *stkMesh); |
| 69 |
//EndBasicReadWrite |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
//-BEGIN |
| 74 |
// ============================================================ |
| 75 |
//+ EXAMPLE: |
| 76 |
//+ Read mesh data from the specified file. |
| 77 |
stk::io::StkMeshIoBroker stkIo(communicator); |
| 78 |
stkIo.add_mesh_database(mesh_file_name, stk::io::READ_MESH); |
| 79 |
|
| 80 |
//+ Creates meta data; creates parts |
| 81 |
stkIo.create_input_mesh(); |
| 82 |
|
| 83 |
//+ Modifications to the meta data (such as creating extra parts and fields) |
| 84 |
//+ is generally be done here. |
| 85 |
|
| 86 |
//+ Commit the meta data and create the bulk data. |
| 87 |
//+ Populate the bulk data with data from the mesh file. |
| 88 |
stkIo.populate_bulk_data(); |
| 89 |
|
| 90 |
// ============================================================ |
| 91 |
//+ VERIFICATION |
| 92 |
//+ In this case we know that the mesh (specified above) contains |
| 93 |
//+ 4 element blocks, 3 nodesets, and 3 sidesets |
| 94 |
//+ There should be a STK Mesh Part for each of those. |
| 95 |
std::shared_ptr<const stk::mesh::MetaData> meta = stkIo.meta_data_ptr(); |
| 96 |
EXPECT_NE(nullptr, meta->get_part("block_1")); |
| 97 |
EXPECT_NE(nullptr, meta->get_part("block_2")); |
| 98 |
EXPECT_NE(nullptr, meta->get_part("block_3")); |
| 99 |
EXPECT_NE(nullptr, meta->get_part("block_4")); |
| 100 |
|
| 101 |
EXPECT_NE(nullptr, meta->get_part("nodelist_1")); |
| 102 |
EXPECT_NE(nullptr, meta->get_part("nodelist_2")); |
| 103 |
EXPECT_NE(nullptr, meta->get_part("nodelist_3")); |
| 104 |
|
| 105 |
EXPECT_NE(nullptr, meta->get_part("surface_1")); |
| 106 |
EXPECT_NE(nullptr, meta->get_part("surface_2")); |
| 107 |
EXPECT_NE(nullptr, meta->get_part("surface_3")); |
| 108 |
|
| 109 |
std::shared_ptr<const stk::mesh::BulkData> bulk = stkIo.bulk_data_ptr(); |
| 110 |
stk::mesh::EntityVector shellElems; |
| 111 |
stk::mesh::get_entities(*bulk, stk::topology::ELEM_RANK, *meta->get_part("block_2"), shellElems); |
| 112 |
EXPECT_EQ(64u, shellElems.size()); |
| 113 |
//-END |
| 114 |
} |
| 115 |
// ============================================================ |
| 116 |
// Cleanup |
| 117 |
unlink(mesh_file_name.c_str()); |
| 118 |
} |
| 119 |
} |