| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <unistd.h> |
| 3 |
#include <stk_io/WriteMesh.hpp> |
| 4 |
#include <stk_io/StkMeshIoBroker.hpp> |
| 5 |
#include <stk_mesh/base/MetaData.hpp> |
| 6 |
#include <stk_mesh/base/BulkData.hpp> |
| 7 |
#include <stk_mesh/base/Field.hpp> |
| 8 |
#include "stk_mesh/base/FieldBase.hpp" |
| 9 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 10 |
#include "stk_io/MeshField.hpp" |
| 11 |
|
| 12 |
namespace |
| 13 |
{ |
| 14 |
|
| 15 |
class InputNodesetDataCalledDispX : public stk::unit_test_util::MeshFixture |
| 16 |
{ |
| 17 |
protected: |
| 18 |
~InputNodesetDataCalledDispX() |
| 19 |
{ |
| 20 |
delete stkIo; |
| 21 |
} |
| 22 |
|
| 23 |
void read_meta(const std::string &filename, double initVal) |
| 24 |
{ |
| 25 |
delete stkIo; |
| 26 |
stkIo = new stk::io::StkMeshIoBroker; |
| 27 |
setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA); |
| 28 |
stkIo->set_bulk_data(get_bulk()); |
| 29 |
stkIo->add_mesh_database(filename, stk::io::READ_MESH); |
| 30 |
stkIo->create_input_mesh(); |
| 31 |
create_both_fields(initVal); |
| 32 |
} |
| 33 |
|
| 34 |
void create_both_fields(double initVal) |
| 35 |
{ |
| 36 |
nodalDisp = create_field("dispx", get_meta().universal_part(), initVal); |
| 37 |
|
| 38 |
stk::mesh::Part *nodesetPart = get_meta().get_part("nodelist_1"); |
| 39 |
ASSERT_TRUE(nodesetPart != nullptr); |
| 40 |
nsDisp = create_field("input_dispx", *nodesetPart, initVal); |
| 41 |
} |
| 42 |
|
| 43 |
stk::mesh::FieldBase *create_field(const std::string &name, stk::mesh::Part &part, double initVal) |
| 44 |
{ |
| 45 |
stk::mesh::FieldBase *field = &get_meta().declare_field<double>(stk::topology::NODE_RANK, name); |
| 46 |
stk::mesh::put_field_on_mesh(*field, part, &initVal); |
| 47 |
return field; |
| 48 |
} |
| 49 |
|
| 50 |
void write_output() |
| 51 |
{ |
| 52 |
size_t outputFileIndex = stkIo->create_output_mesh(outputFileName, stk::io::WRITE_RESULTS); |
| 53 |
stkIo->add_field(outputFileIndex, *nodalDisp); |
| 54 |
stkIo->add_field(outputFileIndex, *nsDisp, nameOfNodesetVarInFile); |
| 55 |
stkIo->write_output_mesh(outputFileIndex); |
| 56 |
stkIo->begin_output_step(outputFileIndex, 1.0); |
| 57 |
stkIo->write_defined_output_fields(outputFileIndex); |
| 58 |
stkIo->end_output_step(outputFileIndex); |
| 59 |
} |
| 60 |
|
| 61 |
void read_and_check_field_data_at_step(int step) |
| 62 |
{ |
| 63 |
stkIo->populate_bulk_data(); |
| 64 |
stkIo->read_defined_input_fields(step); |
| 65 |
expect_both_fields_data(); |
| 66 |
} |
| 67 |
|
| 68 |
void expect_both_fields_data() |
| 69 |
{ |
| 70 |
expect_field_data(nodalDisp, initialValue); |
| 71 |
expect_field_data(nsDisp, 6.38e-4); |
| 72 |
} |
| 73 |
|
| 74 |
void expect_field_data(stk::mesh::FieldBase *field, double expected) |
| 75 |
{ |
| 76 |
stk::mesh::Entity node11 = get_bulk().get_entity(stk::topology::NODE_RANK, 11); |
| 77 |
double *data = static_cast<double *>(stk::mesh::field_data(*field, node11)); |
| 78 |
ASSERT_TRUE(data != nullptr); |
| 79 |
const double epsilon = 1e-6; |
| 80 |
EXPECT_NEAR(expected, *data, epsilon); |
| 81 |
} |
| 82 |
|
| 83 |
const std::string inputFileName = "nodesetData.exo"; |
| 84 |
const std::string outputFileName = "output.exo"; |
| 85 |
double initialValue = 13.0; |
| 86 |
stk::io::StkMeshIoBroker *stkIo = nullptr; |
| 87 |
|
| 88 |
const std::string nameOfNodesetVarInFile = "dispx"; |
| 89 |
stk::mesh::FieldBase *nodalDisp = nullptr; |
| 90 |
stk::mesh::FieldBase *nsDisp = nullptr; |
| 91 |
}; |
| 92 |
|
| 93 |
TEST_F(InputNodesetDataCalledDispX, writingDispDataToFile_nodalDataDispXAndNodesetDataDispX_externalFile) |
| 94 |
{ |
| 95 |
if(stk::parallel_machine_size(get_comm()) == 1) |
| 96 |
{ |
| 97 |
read_meta(inputFileName, initialValue); |
| 98 |
stkIo->add_input_field(stk::io::MeshField(nsDisp, nameOfNodesetVarInFile)); |
| 99 |
read_and_check_field_data_at_step(100); |
| 100 |
|
| 101 |
write_output(); |
| 102 |
|
| 103 |
reset_mesh(); |
| 104 |
|
| 105 |
read_meta(outputFileName, 0.0); |
| 106 |
stkIo->add_input_field(stk::io::MeshField(nodalDisp, nodalDisp->name())); |
| 107 |
stkIo->add_input_field(stk::io::MeshField(nsDisp, nameOfNodesetVarInFile)); |
| 108 |
read_and_check_field_data_at_step(1); |
| 109 |
|
| 110 |
unlink(outputFileName.c_str()); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
} |