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

Public Member Functions

 LIBMESH_CPPUNIT_TEST_SUITE (WriteSidesetData)
 This test ensures you can write both vector and scalar variables.
 
 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_sideset_data.C.

Member Function Documentation

◆ CPPUNIT_TEST()

WriteSidesetData::CPPUNIT_TEST ( testWriteExodus  )

◆ CPPUNIT_TEST_SUITE_END()

WriteSidesetData::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

WriteSidesetData::LIBMESH_CPPUNIT_TEST_SUITE ( WriteSidesetData  )

This test ensures you can write both vector and scalar variables.

◆ testWriteExodus()

void WriteSidesetData::testWriteExodus ( )
inline

Definition at line 217 of file write_sideset_data.C.

218 {
219 LOG_UNIT_TEST;
220
221 testWriteImpl<ExodusII_IO>("write_sideset_data.e", /*write_vars=*/true);
222 testWriteImpl<ExodusII_IO>("write_sideset_data.e", /*write_vars=*/false);
223 }

◆ testWriteImpl()

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

Definition at line 39 of file write_sideset_data.C.

41 {
42 Mesh mesh(*TestCommWorld);
43
44 // We set our initial conditions based on build_square node ids
46
48 /*nx=*/5, /*ny=*/5,
49 -1., 1.,
50 -1., 1.,
51 QUAD4);
52
53 // Add an empty sideset
54 mesh.get_boundary_info().sideset_name(4) = "empty";
55
56 // Only used if write_vars == true
57 std::vector<std::string> var_names;
58 std::vector<std::set<boundary_id_type>> side_ids;
59 std::vector<std::map<BoundaryInfo::BCTuple, Real>> bc_vals;
60
61 if (write_vars)
62 {
63 // Get list of all (elem, side, id) tuples
64 std::vector<BoundaryInfo::BCTuple> all_bc_tuples =
66
67 // Data structures to be passed to ExodusII_IO::write_sideset_data
68 var_names = {"var1", "var2", "var3"};
69 side_ids =
70 {
71 {0, 2}, // var1 is defined on sidesets 0 and 2
72 {1, 3}, // var2 is defined on sidesets 1 and 3
73 {4} // var3 is only defined on the empty sideset 4
74 };
75
76 // Data structure mapping (elem, side, id) tuples to Real values that
77 // will be passed to Exodus.
78 bc_vals.resize(var_names.size());
79
80 // For each var_names[i], construct bc_vals[i]
81 for (unsigned int i=0; i<var_names.size(); ++i)
82 {
83 // const auto & var_name = var_names[i];
84 auto & vals = bc_vals[i];
85
86 for (const auto & t : all_bc_tuples)
87 {
88 // dof_id_type elem_id = std::get<0>(t);
89 // unsigned int side_id = std::get<1>(t);
90 boundary_id_type b_id = std::get<2>(t);
91
92 if (side_ids[i].count(b_id))
93 {
94 // Compute a value. This could in theory depend on
95 // var_name, elem_id, side_id, and/or b_id.
96 Real val = static_cast<Real>(b_id);
97
98 // Insert into the vals map.
99 vals.emplace(t, val);
100 }
101 }
102
103 // If we have a distributed mesh, write_sideset_data wants our
104 // ghost data too; we'll just serialize everything here.
105 if (!mesh.is_serial())
106 TestCommWorld->set_union(vals);
107
108 } // done constructing bc_vals
109
110 // We write the file in the ExodusII format.
111 {
112 IOClass writer(mesh);
113 writer.write(filename);
114 writer.write_sideset_data (/*timestep=*/1, var_names, side_ids, bc_vals);
115 }
116 } // if (write_vars)
117
118 // Make sure that the writing is done before the reading starts.
119 TestCommWorld->barrier();
120
121 // Now read it back in
122 Mesh read_mesh(*TestCommWorld);
123 IOClass reader(read_mesh);
124 reader.read(filename);
125
126 if (write_vars)
127 {
128 std::vector<std::string> read_in_var_names;
129 std::vector<std::set<boundary_id_type>> read_in_side_ids;
130 std::vector<std::map<BoundaryInfo::BCTuple, Real>> read_in_bc_vals;
131 reader.read_sideset_data
132 (/*timestep=*/1, read_in_var_names, read_in_side_ids, read_in_bc_vals);
133
134 // Assert that we got back out what we put in.
135 CPPUNIT_ASSERT(read_in_var_names == var_names);
136 CPPUNIT_ASSERT(read_in_side_ids == side_ids);
137 CPPUNIT_ASSERT(read_in_bc_vals == bc_vals);
138 } // if (write_vars)
139
140 // Also check that the flat indices match those in the file
141 std::map<BoundaryInfo::BCTuple, unsigned int> bc_array_indices;
142 reader.get_sideset_data_indices(bc_array_indices);
143
144 // Debugging
145 // for (const auto & [t, index] : bc_array_indices)
146 // {
147 // const auto & elem_id = std::get<0>(t);
148 // const auto & side_id = std::get<1>(t);
149 // const auto & boundary_id = std::get<2>(t);
150 // libMesh::out << "(elem, side, boundary_id) = "
151 // << "(" << elem_id << ", " << side_id << ", " << boundary_id << ") at index "
152 // << index << std::endl;
153 // }
154
155 // For this test case, the sideset arrays are ordered as follows:
156 // elem_ss1 = 1, 2, 3, 4, 5 ;
157 // side_ss1 = 1, 1, 1, 1, 1 ;
158 // elem_ss2 = 5, 10, 15, 20, 25 ;
159 // side_ss2 = 2, 2, 2, 2, 2 ;
160 // elem_ss3 = 21, 22, 23, 24, 25 ;
161 // side_ss3 = 3, 3, 3, 3, 3 ;
162 // elem_ss4 = 1, 6, 11, 16, 21 ;
163 // side_ss4 = 4, 4, 4, 4, 4 ;
164
165 // Check that the 0th side of the 0th element on boundary 0
166 // corresponds to array index 0.
167 // CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0),
168 // libmesh_map_find(bc_array_indices,
169 // std::make_tuple(/*elem id*/static_cast<dof_id_type>(0),
170 // /*side id*/static_cast<unsigned short int>(0),
171 // /*boundary id*/static_cast<boundary_id_type>(0))));
172
173 // CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0),
174 // libmesh_map_find(bc_array_indices, std::make_tuple(0,0,0)));
175 //
176 // CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1),
177 // libmesh_map_find(bc_array_indices, std::make_tuple(1,0,0)));
178
179 // Check the first five elements, which all have side 0 on
180 // boundary 0, and which happen to be in order in the BC array.
181 for (unsigned int i=0; i<5; ++i)
182 CPPUNIT_ASSERT_EQUAL
183 (static_cast<unsigned int>(i),
184 libmesh_map_find(bc_array_indices,
185 std::make_tuple(/*elem_id=*/cast_int<dof_id_type>(i),
186 /*side_id=*/0,
187 /*b_id=*/0)));
188
189 // Check side 1 of every fifth element starting with element 4, they are all in sideset 1.
190 for (unsigned int i=0; i<5; ++i)
191 CPPUNIT_ASSERT_EQUAL
192 (static_cast<unsigned int>(i),
193 libmesh_map_find(bc_array_indices,
194 std::make_tuple(/*elem_id=*/cast_int<dof_id_type>(5*i + 4),
195 /*side_id*/1,
196 /*b_id=*/1)));
197
198 // Check side 2 of the 5 consecutive elements starting with Elem 20. They are all in sideset 2.
199 for (unsigned int i=0; i<5; ++i)
200 CPPUNIT_ASSERT_EQUAL
201 (static_cast<unsigned int>(i),
202 libmesh_map_find(bc_array_indices,
203 std::make_tuple(/*elem_id=*/cast_int<dof_id_type>(20+i),
204 /*side_id*/2,
205 /*b_id=*/2)));
206
207 // Check side 3 of every fifth element, they are all in sideset 3
208 for (unsigned int i=0; i<5; ++i)
209 CPPUNIT_ASSERT_EQUAL
210 (static_cast<unsigned int>(i),
211 libmesh_map_find(bc_array_indices,
212 std::make_tuple(/*elem_id=*/cast_int<dof_id_type>(5*i),
213 /*side_id*/3,
214 /*b_id=*/3)));
215 }
std::string & sideset_name(boundary_id_type id)
std::vector< BCTuple > build_side_list(BCTupleSortBy sort_by=BCTupleSortBy::ELEM_ID) const
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_side_list(), libMesh::MeshTools::Generation::build_square(), libMesh::MeshBase::get_boundary_info(), libMesh::MeshBase::is_serial(), mesh, libMesh::QUAD4, libMesh::Real, libMesh::BoundaryInfo::sideset_name(), and TestCommWorld.

◆ testWriteNemesis()

void WriteSidesetData::testWriteNemesis ( )
inline

Definition at line 225 of file write_sideset_data.C.

226 {
227 // LOG_UNIT_TEST;
228
229 // FIXME: Not yet implemented
230 // testWriteImpl<Nemesis_IO>("write_sideset_data.n", /*write_vars=*/true);
231 }

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