libMesh
write_sideset_data.C
Go to the documentation of this file.
1 // Basic include files
2 #include "libmesh/equation_systems.h"
3 #include "libmesh/exodusII_io.h"
4 #include "libmesh/mesh.h"
5 #include "libmesh/mesh_generation.h"
6 #include "libmesh/parallel.h" // set_union
7 #include "libmesh/string_to_enum.h"
8 #include "libmesh/boundary_info.h"
9 
10 #include "test_comm.h"
11 #include "libmesh_cppunit.h"
12 
13 
14 // Bring in everything from the libMesh namespace
15 using namespace libMesh;
16 
17 class WriteSidesetData : public CppUnit::TestCase
18 {
22 public:
23  CPPUNIT_TEST_SUITE(WriteSidesetData);
24 
25 #if LIBMESH_DIM > 1
26  CPPUNIT_TEST(testWrite);
27 #endif
28 
29  CPPUNIT_TEST_SUITE_END();
30 
31  void testWrite()
32  {
34 
35  // We set our initial conditions based on build_square node ids
36  mesh.allow_renumbering(false);
37 
39  /*nx=*/5, /*ny=*/5,
40  -1., 1.,
41  -1., 1.,
42  QUAD4);
43 
44  // Get list of all (elem, side, id) tuples
45  std::vector<BoundaryInfo::BCTuple> all_bc_tuples =
47 
48  // Data structures to be passed to ExodusII_IO::write_sideset_data
49  std::vector<std::string> var_names = {"var1", "var2"};
50  std::vector<std::set<boundary_id_type>> side_ids =
51  {
52  {0, 2}, // var1 is defined on sidesets 0 and 2
53  {1, 3} // var2 is defined on sidesets 1 and 3
54  };
55 
56  // Data structure mapping (elem, side, id) tuples to Real values that
57  // will be passed to Exodus.
58  std::vector<std::map<BoundaryInfo::BCTuple, Real>> bc_vals(var_names.size());
59 
60  // For each var_names[i], construct bc_vals[i]
61  for (unsigned int i=0; i<var_names.size(); ++i)
62  {
63  // const auto & var_name = var_names[i];
64  auto & vals = bc_vals[i];
65 
66  for (const auto & t : all_bc_tuples)
67  {
68  // dof_id_type elem_id = std::get<0>(t);
69  // unsigned int side_id = std::get<1>(t);
70  boundary_id_type b_id = std::get<2>(t);
71 
72  if (side_ids[i].count(b_id))
73  {
74  // Compute a value. This could in theory depend on
75  // var_name, elem_id, side_id, and/or b_id.
76  Real val = static_cast<Real>(b_id);
77 
78  // Insert into the vals map.
79  vals.insert(std::make_pair(t, val));
80  }
81  }
82 
83  // If we have a distributed mesh, write_sideset_data wants our
84  // ghost data too; we'll just serialize everything here.
85  if (!mesh.is_serial())
86  TestCommWorld->set_union(vals);
87 
88  } // done constructing bc_vals
89 
90 #ifdef LIBMESH_HAVE_EXODUS_API
91 
92  // We write the file in the ExodusII format.
93  {
94  ExodusII_IO writer(mesh);
95  writer.write("write_sideset_data.e");
96  writer.write_sideset_data (/*timestep=*/1, var_names, side_ids, bc_vals);
97  }
98 
99  // Make sure that the writing is done before the reading starts.
100  TestCommWorld->barrier();
101 
102  // Now read it back in
103  Mesh read_mesh(*TestCommWorld);
104  ExodusII_IO reader(read_mesh);
105  reader.read("write_sideset_data.e");
106 
107  std::vector<std::string> read_in_var_names;
108  std::vector<std::set<boundary_id_type>> read_in_side_ids;
109  std::vector<std::map<BoundaryInfo::BCTuple, Real>> read_in_bc_vals;
110  reader.read_sideset_data
111  (/*timestep=*/1, read_in_var_names, read_in_side_ids, read_in_bc_vals);
112 
113  // Assert that we got back out what we put in.
114  CPPUNIT_ASSERT(read_in_var_names == var_names);
115  CPPUNIT_ASSERT(read_in_side_ids == side_ids);
116  CPPUNIT_ASSERT(read_in_bc_vals == bc_vals);
117 
118 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
119  }
120 };
121 
libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
libMesh::MeshBase::get_boundary_info
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition: mesh_base.h:132
libMesh::MeshBase::is_serial
virtual bool is_serial() const
Definition: mesh_base.h:159
WriteSidesetData::testWrite
void testWrite()
Definition: write_sideset_data.C:31
libMesh::ExodusII_IO::write_sideset_data
void write_sideset_data(int timestep, const std::vector< std::string > &var_names, const std::vector< std::set< boundary_id_type >> &side_ids, const std::vector< std::map< BoundaryInfo::BCTuple, Real >> &bc_vals)
The Exodus format can also store values on sidesets.
Definition: exodusII_io.C:1305
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(WriteSidesetData)
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::ExodusII_IO
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:51
WriteSidesetData
Definition: write_sideset_data.C:17
libMesh::boundary_id_type
int8_t boundary_id_type
Definition: id_types.h:51
libMesh::BoundaryInfo::build_side_list
void build_side_list(std::vector< dof_id_type > &element_id_list, std::vector< unsigned short int > &side_list, std::vector< boundary_id_type > &bc_id_list) const
Creates a list of element numbers, sides, and ids for those sides.
Definition: boundary_info.C:1976
libMesh::MeshTools::Generation::build_square
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.
Definition: mesh_generation.C:1501
libMesh::ExodusII_IO::read_sideset_data
void read_sideset_data(int timestep, std::vector< std::string > &var_names, std::vector< std::set< boundary_id_type >> &side_ids, std::vector< std::map< BoundaryInfo::BCTuple, Real >> &bc_vals)
Similar to write_sideset_data(), this function is used to read the data at a particular timestep.
Definition: exodusII_io.C:1322
libMesh::QUAD4
Definition: enum_elem_type.h:41
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libmesh_cppunit.h
libMesh::ExodusII_IO::write
virtual void write(const std::string &fname) override
This method implements writing a mesh to a specified file.
Definition: exodusII_io.C:1338
libMesh::MeshBase::allow_renumbering
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:1025
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
test_comm.h
libMesh::ExodusII_IO::read
virtual void read(const std::string &name) override
This method implements reading a mesh from a specified file.
Definition: exodusII_io.C:143