Debian Codegraph @

packages/stk/stk_integration_tests/stk_tools/createMesh.cpp

raw
1 #include <iostream>
2
3 #include <stk_unit_test_utils/MeshFixture.hpp>
4 #include <stk_unit_test_utils/getOption.h>
5
6 #include <stk_mesh/base/GetEntities.hpp>
7 #include <stk_mesh/base/FieldBase.hpp>
8 #include <stk_mesh/base/Field.hpp>
9 #include <stk_mesh/baseImpl/EquivalentEntityBlocks.hpp>
10
11 namespace
12 {
13
14 class StkToolsA : public stk::unit_test_util::MeshFixture
15 {
16
17 };
18
19 TEST_F(StkToolsA, WriteTextMeshDescFromExodusFile)
20 {
21 const std::string unNamed = "mesh not specified";
22 const std::string meshName = stk::unit_test_util::get_option("-i", unNamed);
23 STK_ThrowRequireMsg(meshName!=unNamed, "Please specify mesh with -i option.");
24 setup_mesh(meshName, stk::mesh::BulkData::NO_AUTO_AURA);
25
26 std::ostringstream os;
27 os.precision(16);
28
29 const stk::mesh::BucketVector &buckets = get_bulk().get_buckets(stk::topology::ELEM_RANK, get_meta().locally_owned_part());
30
31 os << "const std::string meshDesc = \"";
32
33 std::map<unsigned, int> localToGlobalId;
34 std::vector<stk::mesh::PartOrdinal> partOrdinals;
35
36 unsigned counter = 0;
37 for(size_t i=0;i<buckets.size();++i)
38 {
39 const stk::mesh::Bucket& bucket = *buckets[i];
40 for(size_t j=0;j<bucket.size();j++)
41 {
42 stk::mesh::Entity element = bucket[j];
43 stk::mesh::impl::get_element_block_part_ordinals(element, get_bulk(), partOrdinals);
44 unsigned partOrdinal = partOrdinals[0];
45 if(localToGlobalId.find(partOrdinal)==localToGlobalId.end())
46 {
47 localToGlobalId[partOrdinal]=counter;
48 counter++;
49 }
50 }
51 }
52
53 for(size_t i=0;i<buckets.size();++i)
54 {
55 const stk::mesh::Bucket& bucket = *buckets[i];
56 for(size_t j=0;j<bucket.size();j++)
57 {
58 stk::mesh::Entity element = bucket[j];
59 os << stk::parallel_machine_rank(get_comm()) << "," << get_bulk().identifier(element) << ",HEX_8";
60 unsigned num_nodes = get_bulk().num_nodes(element);
61 const stk::mesh::Entity *nodes = get_bulk().begin_nodes(element);
62 for(unsigned k=0;k<num_nodes;k++)
63 {
64 os << "," << get_bulk().identifier(nodes[k]);
65 }
66
67 stk::mesh::impl::get_element_block_part_ordinals(element, get_bulk(), partOrdinals);
68 unsigned part_id = partOrdinals[0];
69 unsigned local_id = localToGlobalId.at(part_id);
70 os << ",block_" << local_id+1 << "\\n\\\n";
71 }
72 }
73 os << "\";\n";
74
75 stk::mesh::Field<double>* coordField = get_meta().get_field<double>(stk::topology::NODE_RANK, "coordinates");
76 ASSERT_TRUE(coordField!=nullptr);
77
78 stk::mesh::Selector nodeSelector = get_meta().locally_owned_part() | get_meta().globally_shared_part();
79 stk::mesh::EntityVector nodes;
80 stk::mesh::get_entities(get_bulk(), stk::topology::NODE_RANK, nodes);
81
82 os << "std::vector<double> coordinates{\n";
83 for(stk::mesh::Entity node : nodes)
84 {
85 double *coord = stk::mesh::field_data(*coordField, node);
86 os << coord[0] << ", " << coord[1] << ", " << coord[2] << "," << std::endl;
87 }
88 os << "};\n";
89
90 std::ofstream outfile("test.out.txt");
91 outfile << os.str();
92 }
93 }