| 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> |
| 36 |
#include <stk_util/stk_config.h> |
| 37 |
#include <stk_unit_test_utils/getOption.h> |
| 38 |
#include <stk_util/parallel/ParallelReduce.hpp> |
| 39 |
#include <stk_mesh/base/MetaData.hpp> |
| 40 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 41 |
#include <stk_mesh/base/BulkData.hpp> |
| 42 |
#include <stk_mesh/base/Bucket.hpp> |
| 43 |
#include <stk_mesh/base/Field.hpp> |
| 44 |
#include <stk_mesh/base/FieldBase.hpp> |
| 45 |
#include <stk_mesh/base/Entity.hpp> |
| 46 |
#include <stk_io/FillMesh.hpp> |
| 47 |
#include <stk_io/StkMeshIoBroker.hpp> |
| 48 |
#include <stk_unit_test_utils/timer.hpp> |
| 49 |
|
| 50 |
#include <unistd.h> |
| 51 |
|
| 52 |
namespace stk_perf_io_mesh_field_write |
| 53 |
{ |
| 54 |
|
| 55 |
void create_nodal_vector_field(stk::mesh::MetaData& meshMetaData, |
| 56 |
const std::string &fieldName, |
| 57 |
const std::string &partName) |
| 58 |
{ |
| 59 |
double initial_value[3] = {0, 0, 0}; |
| 60 |
stk::mesh::Field<double> &field = meshMetaData.declare_field<double>(stk::topology::NODE_RANK, fieldName); |
| 61 |
stk::io::set_field_role(field, Ioss::Field::TRANSIENT); |
| 62 |
stk::mesh::Part* part = meshMetaData.get_part(partName); |
| 63 |
ASSERT_TRUE(part != nullptr)<<partName<<" not found"; |
| 64 |
stk::mesh::put_field_on_mesh(field, *part, 3, initial_value); |
| 65 |
} |
| 66 |
|
| 67 |
void create_nodal_vector_fields(stk::mesh::MetaData& meshMetaData, |
| 68 |
const std::string& fieldBaseName, |
| 69 |
unsigned numFields, |
| 70 |
const std::string& partName) |
| 71 |
{ |
| 72 |
for(unsigned i=0; i<numFields; ++i) { |
| 73 |
std::string fieldName = fieldBaseName+std::to_string(i); |
| 74 |
create_nodal_vector_field(meshMetaData, fieldName, partName); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
size_t set_field_data(stk::mesh::BulkData& mesh, const stk::mesh::Selector& select, int outputStep) |
| 79 |
{ |
| 80 |
const stk::mesh::MetaData& meta = mesh.mesh_meta_data(); |
| 81 |
const stk::mesh::FieldVector& fields = meta.get_fields(stk::topology::NODE_RANK); |
| 82 |
const stk::mesh::BucketVector& node_buckets = mesh.get_buckets(stk::topology::NODE_RANK, select); |
| 83 |
size_t num_nodes = 0; |
| 84 |
for(size_t i=0; i<node_buckets.size(); ++i) { |
| 85 |
stk::mesh::Bucket& bucket = *node_buckets[i]; |
| 86 |
num_nodes += bucket.size(); |
| 87 |
for(size_t f=0; f<fields.size(); ++f) { |
| 88 |
double* data = static_cast<double*>(stk::mesh::field_data(*fields[f], bucket)); |
| 89 |
unsigned flen = stk::mesh::field_scalars_per_entity(*fields[f], bucket); |
| 90 |
for(size_t n=0; n<flen*bucket.size(); ++n) { |
| 91 |
data[n] = static_cast<double>(f + 10*outputStep); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return num_nodes; |
| 97 |
} |
| 98 |
|
| 99 |
void setup_mesh(const std::string& meshSpec, stk::mesh::BulkData& bulkData, unsigned numFields) |
| 100 |
{ |
| 101 |
stk::ParallelMachine communicator = bulkData.parallel(); |
| 102 |
stk::io::StkMeshIoBroker exodusFileReader(communicator); |
| 103 |
|
| 104 |
exodusFileReader.set_bulk_data(bulkData); |
| 105 |
exodusFileReader.add_mesh_database(meshSpec, stk::io::READ_MESH); |
| 106 |
|
| 107 |
int myProc = bulkData.parallel_rank(); |
| 108 |
|
| 109 |
if (myProc==0) { |
| 110 |
std::cerr << "Starting To Read Mesh: " << std::endl; |
| 111 |
} |
| 112 |
|
| 113 |
exodusFileReader.create_input_mesh(); |
| 114 |
std::shared_ptr<stk::mesh::MetaData> metaData = exodusFileReader.meta_data_ptr(); |
| 115 |
|
| 116 |
create_nodal_vector_fields(*metaData, "nodalField", numFields, "block_1"); |
| 117 |
|
| 118 |
metaData->commit(); |
| 119 |
|
| 120 |
bool delay_field_data_allocation = true; |
| 121 |
exodusFileReader.populate_mesh(delay_field_data_allocation); |
| 122 |
exodusFileReader.populate_field_data(); |
| 123 |
} |
| 124 |
|
| 125 |
void add_output_fields(stk::io::StkMeshIoBroker& outputBroker, size_t outputFileIndex, stk::mesh::BulkData& bulkData) |
| 126 |
{ |
| 127 |
const stk::mesh::FieldVector fields = bulkData.mesh_meta_data().get_fields(); |
| 128 |
for(stk::mesh::FieldBase* field : fields) |
| 129 |
{ |
| 130 |
const Ioss::Field::RoleType* fieldRole = stk::io::get_field_role(*field); |
| 131 |
if(*fieldRole == Ioss::Field::TRANSIENT) |
| 132 |
outputBroker.add_field(outputFileIndex, *field); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void do_output(stk::io::StkMeshIoBroker& outputBroker, size_t outputFileIndex, |
| 137 |
stk::mesh::BulkData& bulkData, unsigned outputStep, |
| 138 |
bool DO_FLUSH = false) |
| 139 |
{ |
| 140 |
set_field_data(bulkData, bulkData.mesh_meta_data().universal_part(), outputStep); |
| 141 |
|
| 142 |
double outputTime = static_cast<double>(outputStep); |
| 143 |
outputBroker.begin_output_step(outputFileIndex, outputTime); |
| 144 |
outputBroker.write_defined_output_fields(outputFileIndex); |
| 145 |
outputBroker.end_output_step(outputFileIndex); |
| 146 |
|
| 147 |
if(DO_FLUSH) { |
| 148 |
outputBroker.flush_output(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
TEST(StkIo, meshFieldWrite_hex_noAura) |
| 153 |
{ |
| 154 |
stk::ParallelMachine communicator = MPI_COMM_WORLD; |
| 155 |
|
| 156 |
int nProc = stk::parallel_machine_size(communicator); |
| 157 |
int myProc = stk::parallel_machine_rank(communicator); |
| 158 |
|
| 159 |
if (nProc > 16) { GTEST_SKIP(); } |
| 160 |
|
| 161 |
const unsigned NUM_RUNS = 1; |
| 162 |
const unsigned NUM_ITERS = 500; |
| 163 |
const unsigned NUM_FIELDS = 15; |
| 164 |
|
| 165 |
int ELEMS_PER_DIM = stk::unit_test_util::get_command_line_option("--ne", 80); |
| 166 |
bool DO_FLUSH = stk::unit_test_util::has_option("--flush"); |
| 167 |
|
| 168 |
std::string elems = std::to_string(ELEMS_PER_DIM); |
| 169 |
std::string meshSpec = "generated:"+elems+"x"+elems+"x"+elems; |
| 170 |
std::string outputFileName = "meshFieldWrite.e"; |
| 171 |
std::string perProcOutputFileName = stk::io::construct_filename_for_serial_or_parallel(outputFileName, nProc, myProc); |
| 172 |
|
| 173 |
if (myProc == 0) { |
| 174 |
std::cout << "Using mesh-spec: " << meshSpec << std::endl; |
| 175 |
} |
| 176 |
|
| 177 |
std::unique_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(communicator) |
| 178 |
.set_aura_option(stk::mesh::BulkData::NO_AUTO_AURA) |
| 179 |
.create(); |
| 180 |
setup_mesh(meshSpec, *bulkPtr, NUM_FIELDS); |
| 181 |
|
| 182 |
stk::unit_test_util::BatchTimer batchTimer(communicator); |
| 183 |
|
| 184 |
batchTimer.initialize_batch_timer(); |
| 185 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 186 |
unlink(perProcOutputFileName.c_str()); |
| 187 |
|
| 188 |
stk::parallel_machine_barrier(communicator); |
| 189 |
|
| 190 |
if (myProc == 0) { |
| 191 |
std::cout << "Starting run: " << j << std::endl; |
| 192 |
} |
| 193 |
|
| 194 |
stk::io::StkMeshIoBroker outputBroker(communicator); |
| 195 |
outputBroker.set_bulk_data(*bulkPtr); |
| 196 |
size_t outputFileIndex = outputBroker.create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 197 |
|
| 198 |
batchTimer.start_batch_timer(); |
| 199 |
|
| 200 |
add_output_fields(outputBroker, outputFileIndex, *bulkPtr); |
| 201 |
|
| 202 |
outputBroker.write_output_mesh(outputFileIndex); |
| 203 |
|
| 204 |
for(unsigned i=0; i<NUM_ITERS; ++i) { |
| 205 |
do_output(outputBroker, outputFileIndex, *bulkPtr, i,DO_FLUSH); |
| 206 |
} |
| 207 |
|
| 208 |
batchTimer.stop_batch_timer(); |
| 209 |
|
| 210 |
unlink(perProcOutputFileName.c_str()); |
| 211 |
} |
| 212 |
batchTimer.print_batch_timing(NUM_ITERS); |
| 213 |
} |
| 214 |
|
| 215 |
} |