libMesh
Loading...
Searching...
No Matches
Public Member Functions | List of all members
WriteNodesetData Class Reference
Inheritance diagram for WriteNodesetData:
[legend]

Public Member Functions

 LIBMESH_CPPUNIT_TEST_SUITE (WriteNodesetData)
 
 CPPUNIT_TEST (testWriteExodus)
 
 CPPUNIT_TEST_SUITE_END ()
 
template<typename IOClass >
void testWriteImpl (const std::string &filename, bool write_vars)
 
void testWriteExodus ()
 
void testWriteNemesis ()
 

Detailed Description

Definition at line 19 of file write_nodeset_data.C.

Member Function Documentation

◆ CPPUNIT_TEST()

WriteNodesetData::CPPUNIT_TEST ( testWriteExodus  )

◆ CPPUNIT_TEST_SUITE_END()

WriteNodesetData::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

WriteNodesetData::LIBMESH_CPPUNIT_TEST_SUITE ( WriteNodesetData  )

◆ testWriteExodus()

void WriteNodesetData::testWriteExodus ( )
inline

Definition at line 195 of file write_nodeset_data.C.

196 {
197 LOG_UNIT_TEST;
198
199 testWriteImpl<ExodusII_IO>("write_nodeset_data.e", /*write_vars=*/true);
200 testWriteImpl<ExodusII_IO>("write_nodeset_data.e", /*write_vars=*/false);
201 }

◆ testWriteImpl()

template<typename IOClass >
void WriteNodesetData::testWriteImpl ( const std::string &  filename,
bool  write_vars 
)
inline

Definition at line 36 of file write_nodeset_data.C.

38 {
39 Mesh mesh(*TestCommWorld);
42 /*nx=*/5, /*ny=*/5,
43 -1., 1.,
44 -1., 1.,
45 QUAD4);
46
47 // Add an empty nodeset
48 mesh.get_boundary_info().nodeset_name(4) = "empty";
49
50 // Only used if write_vars == true
51 std::vector<std::string> var_names;
52 std::vector<std::set<boundary_id_type>> node_boundary_ids;
53 std::vector<std::map<BoundaryInfo::NodeBCTuple, Real>> bc_vals;
54
55 if (write_vars)
56 {
58
59 // Meshes created via build_square() don't have any nodesets
60 // defined on them by default, so let's create some now. Note that
61 // this function handles the synchronization of ghost element and
62 // node ids in parallel internally.
64
65 // Get list of all (node, id) tuples
66 std::vector<BoundaryInfo::NodeBCTuple> all_bc_tuples = bi.build_node_list();
67
68 // Data structures to be passed to ExodusII_IO::write_nodeset_data()
69 var_names = {"var1", "var2", "var3"};
70 node_boundary_ids =
71 {
72 {0, 2}, // var1 is defined on nodesets 0 and 2
73 {1, 3}, // var2 is defined on nodesets 1 and 3
74 {4} // var3 is only defined on the empty nodeset 4
75 };
76
77 // Data structure mapping (node, id) tuples to Real values that
78 // will be passed to Exodus.
79 bc_vals.resize(var_names.size());
80
81 // For each var_names[i], construct bc_vals[i]
82 for (unsigned int i=0; i<var_names.size(); ++i)
83 {
84 // const auto & var_name = var_names[i];
85 auto & vals = bc_vals[i];
86
87 for (const auto & t : all_bc_tuples)
88 {
89 boundary_id_type b_id = std::get<1>(t);
90
91 if (node_boundary_ids[i].count(b_id))
92 {
93 // Compute a value. This could in theory depend on
94 // var_name, node_id, and/or b_id.
95 Real val = static_cast<Real>(b_id);
96
97 // Insert into the vals map.
98 vals.emplace(t, val);
99 }
100 }
101
102 // If we have a distributed mesh, the ExodusII writer
103 // serializes it before writing, so we need to serialize our
104 // nodeset data as well so that it can all be written from
105 // processor 0.
106 if (!mesh.is_serial())
107 TestCommWorld->set_union(vals);
108
109 } // done constructing bc_vals
110
111 // We write the file in the ExodusII format.
112 {
113 IOClass writer(mesh);
114 writer.write(filename);
115 writer.write_nodeset_data (/*timestep=*/1, var_names, node_boundary_ids, bc_vals);
116 }
117 }
118
119 // Make sure that the writing is done before the reading starts.
120 TestCommWorld->barrier();
121
122 // Now read it back in
123 Mesh read_mesh(*TestCommWorld);
124 IOClass reader(read_mesh);
125 reader.read(filename);
126
127 if (write_vars)
128 {
129 std::vector<std::string> read_in_var_names;
130 std::vector<std::set<boundary_id_type>> read_in_node_boundary_ids;
131 std::vector<std::map<BoundaryInfo::NodeBCTuple, Real>> read_in_bc_vals;
132 reader.read_nodeset_data
133 (/*timestep=*/1, read_in_var_names, read_in_node_boundary_ids, read_in_bc_vals);
134
135 // Assert that we got back out what we put in.
136 CPPUNIT_ASSERT(read_in_var_names == var_names);
137 CPPUNIT_ASSERT(read_in_node_boundary_ids == node_boundary_ids);
138 CPPUNIT_ASSERT(read_in_bc_vals == bc_vals);
139 } // if (write_vars)
140
141 // Also check that the flat indices match those in the file
142 std::map<BoundaryInfo::NodeBCTuple, unsigned int> bc_array_indices;
143 reader.get_nodeset_data_indices(bc_array_indices);
144
145 // Debugging
146 // for (const auto & [t, index] : bc_array_indices)
147 // {
148 // const auto & node_id = std::get<0>(t);
149 // const auto & boundary_id = std::get<1>(t);
150 // libMesh::out << "(node, boundary_id) = "
151 // << "(" << node_id << ", " << boundary_id << ")"
152 // << " is at array index " << index
153 // << std::endl;
154 // }
155
156 // For this test case, the nodeset arrays are ordered as follows:
157 // ns_prop1 = 0, 1, 2, 3, 4 ;
158 // ns_names = "bottom", "right", "top", "left", "empty" ;
159 //
160 // Exodus (1-based) node ids
161 // node_ns1 = 1, 2, 3, 4, 5, 6 ;
162 // node_ns2 = 6, 12, 18, 24, 30, 36 ;
163 // node_ns3 = 31, 32, 33, 34, 35, 36 ;
164 // node_ns4 = 1, 7, 13, 19, 25, 31 ;
165
166 // Check the node ids in the "bottom" nodeset, which contains the
167 // first six nodes in order.
168 for (unsigned int i=0; i<6; ++i)
169 CPPUNIT_ASSERT_EQUAL
170 (static_cast<unsigned int>(i),
171 libmesh_map_find(bc_array_indices, std::make_tuple(/*node_id=*/cast_int<dof_id_type>(i), /*b_id=*/0)));
172
173 // Check the node ids in the "right" sideset, which contains every
174 // 6th node starting from 5, i.e. 5, 11, 17
175 for (unsigned int i=0; i<6; ++i)
176 CPPUNIT_ASSERT_EQUAL
177 (static_cast<unsigned int>(i),
178 libmesh_map_find(bc_array_indices, std::make_tuple(/*node_id=*/cast_int<dof_id_type>(6*i + 5), /*b_id=*/1)));
179
180 // Check the node ids in the "top" sideset, which contains the last
181 // six nodes in order.
182 for (unsigned int i=0; i<6; ++i)
183 CPPUNIT_ASSERT_EQUAL
184 (static_cast<unsigned int>(i),
185 libmesh_map_find(bc_array_indices, std::make_tuple(/*node_id=*/cast_int<dof_id_type>(30 + i), /*b_id=*/2)));
186
187 // Check the node ids in the "left" sideset, which contains every
188 // 6th node starting from 0, i.e. 0, 6, 12
189 for (unsigned int i=0; i<6; ++i)
190 CPPUNIT_ASSERT_EQUAL
191 (static_cast<unsigned int>(i),
192 libmesh_map_find(bc_array_indices, std::make_tuple(/*node_id=*/cast_int<dof_id_type>(6*i), /*b_id=*/3)));
193 }
The BoundaryInfo class contains information relevant to boundary conditions including storing faces,...
std::vector< NodeBCTuple > build_node_list(NodeBCTupleSortBy sort_by=NodeBCTupleSortBy::NODE_ID) const
std::string & nodeset_name(boundary_id_type id)
void build_node_list_from_side_list(const std::set< boundary_id_type > &sideset_list={})
Adds nodes with boundary ids based on the side's boundary ids they are connected to.
virtual bool is_serial() const
Definition mesh_base.h:357
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition mesh_base.h:170
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
Definition mesh_base.h:1355
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
Communicator * TestCommWorld
MeshBase & mesh
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
int8_t boundary_id_type
Definition id_types.h:51
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References libMesh::MeshBase::allow_renumbering(), libMesh::BoundaryInfo::build_node_list(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::MeshTools::Generation::build_square(), libMesh::MeshBase::get_boundary_info(), libMesh::MeshBase::is_serial(), mesh, libMesh::BoundaryInfo::nodeset_name(), libMesh::QUAD4, libMesh::Real, and TestCommWorld.

◆ testWriteNemesis()

void WriteNodesetData::testWriteNemesis ( )
inline

Definition at line 203 of file write_nodeset_data.C.

204 {
205 LOG_UNIT_TEST;
206
207 // FIXME: Not yet implemented
208 // testWriteImpl<Nemesis_IO>("write_nodeset_data.n");
209 }

The documentation for this class was generated from the following file: