libMesh
write_vec_and_scalar.C
Go to the documentation of this file.
1 // Basic include files
2 #include "libmesh/distributed_mesh.h"
3 #include "libmesh/equation_systems.h"
4 #include "libmesh/exodusII_io.h"
5 #include "libmesh/explicit_system.h"
6 #include "libmesh/mesh_generation.h"
7 #include "libmesh/nemesis_io.h"
8 #include "libmesh/node.h"
9 #include "libmesh/numeric_vector.h"
10 #include "libmesh/replicated_mesh.h"
11 #include "libmesh/string_to_enum.h"
12 
13 #include "test_comm.h"
14 #include "libmesh_cppunit.h"
15 
16 
17 // Bring in everything from the libMesh namespace
18 using namespace libMesh;
19 
20 class WriteVecAndScalar : public CppUnit::TestCase
21 {
25 public:
26  LIBMESH_CPPUNIT_TEST_SUITE(WriteVecAndScalar);
27 
28 #if LIBMESH_DIM > 1
29 #if defined(LIBMESH_HAVE_EXODUS_API) && defined(LIBMESH_HAVE_NEMESIS_API)
30  CPPUNIT_TEST(testWriteExodusDistributed);
31  CPPUNIT_TEST(testWriteExodusReplicated);
32  CPPUNIT_TEST(testWriteNemesisDistributed);
33  CPPUNIT_TEST(testWriteNemesisReplicated);
34 #endif
35 #endif
36 
37  CPPUNIT_TEST_SUITE_END();
38 
39  void setupTests(UnstructuredMesh & mesh, EquationSystems & equation_systems)
40  {
41  // We set our initial conditions based on build_square node ids
42  mesh.allow_renumbering(false);
43 
45  1, 1,
46  -1., 1.,
47  -1., 1.,
48  Utility::string_to_enum<ElemType>("TRI6"));
49 
50  ExplicitSystem & system = equation_systems.add_system<ExplicitSystem>("Tester");
51  system.add_variable("u", FIRST, NEDELEC_ONE);
52  system.add_variable("v", FIRST, LAGRANGE);
53 
54  // Initialize the system
55  equation_systems.init();
56 
57  // Mimic the initial conditions of u(i)=2*i in the serial case,
58  // but indexed by node id rather than dof id.
59  std::vector<Real> initial_vector({10, 0, 12, 8, 4, 2, 16, 6, 14});
60 
61  NumericVector<Number> & sys_solution = *(system.solution);
62  for (const auto & node : mesh.node_ptr_range())
63  {
64  dof_id_type dof_id;
65  if (node->n_comp(0,0))
66  {
67  dof_id = node->dof_number(0,0,0);
68  }
69  else
70  {
71  CPPUNIT_ASSERT(node->n_comp(0,1));
72  dof_id = node->dof_number(0,1,0);
73  }
74  if (dof_id >= sys_solution.first_local_index() &&
75  dof_id < sys_solution.last_local_index())
76  sys_solution.set(dof_id, initial_vector[node->id()]);
77  if (mesh.n_processors() == 1)
78  CPPUNIT_ASSERT_EQUAL(initial_vector[node->id()], dof_id*Real(2));
79  }
80  sys_solution.close();
81  }
82 
83 
84  void testSolution (const MeshBase & read_mesh, const System & sys2)
85  {
86  // VariableGroup optimization means that DoFs iterate over each of
87  // the 3 variables on each node before proceeding to the next
88  // node.
89  // u_x, u_y, v
90  const std::vector<Real> gold_vector = {-1, 3, 10,
91  0, 1, 12,
92  2, 0, 14,
93  0, 1.5, 11,
94  0.5, 1, 13,
95  0.5, 1.5, 12,
96  3, 4, 16,
97  3, 1.5, 15,
98  0.5, 4, 13};
99 
100  // Translation from node id to dof indexing order as gets done in
101  // serial
102  const std::vector<dof_id_type>
103  node_reordering({0, 3, 1, 8, 5, 4, 6, 7, 2});
104 
105  Real tol = 1e-12;
106  NumericVector<Number> & sys2_soln(*sys2.solution);
107  for (const auto & node : read_mesh.node_ptr_range())
108  {
109  if (node->processor_id() != read_mesh.processor_id())
110  continue;
111 
112  // We write out real, imaginary, and magnitude for complex
113  // numbers
114 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
115  const unsigned int v2 = 3;
116 #else
117  const unsigned int v2 = 1;
118 #endif
119  CPPUNIT_ASSERT_EQUAL(node->n_vars(0), 3*v2);
120 
121  CPPUNIT_ASSERT_LESS(node_reordering.size(), std::size_t(node->id()));
122  const dof_id_type gold_i_ux = node_reordering[node->id()] * 3;
123  const dof_id_type gold_i_uy = gold_i_ux + 1;
124  const dof_id_type gold_i_v = gold_i_uy + 1;
125  CPPUNIT_ASSERT_LESS(gold_vector.size(), std::size_t(gold_i_v));
126 
127  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,0,0)),
128  gold_vector[gold_i_ux], tol);
129  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,v2,0)),
130  gold_vector[gold_i_uy], tol);
131  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,2*v2,0)),
132  gold_vector[gold_i_v], tol);
133 
134  // Let's check imaginary parts and magnitude for good measure
135 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
136  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,1,0)),
137  0.0, tol);
138  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,2,0)),
139  std::abs(gold_vector[gold_i_ux]), tol);
140  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,4,0)),
141  0.0, tol);
142  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,5,0)),
143  std::abs(gold_vector[gold_i_uy]), tol);
144  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,7,0)),
145  0.0, tol);
146  LIBMESH_ASSERT_NUMBERS_EQUAL(sys2_soln(node->dof_number(0,8,0)),
147  std::abs(gold_vector[gold_i_v]), tol);
148 #endif
149  }
150  }
151 
152 
153  template <typename MeshType>
154  void testWriteExodus(const std::string & filename)
155  {
156  MeshType mesh(*TestCommWorld);
157  EquationSystems equation_systems(mesh);
158 
159  setupTests(mesh, equation_systems);
160 
161 #ifdef LIBMESH_HAVE_EXODUS_API
162  // Make sure any previous reads are done before the writing starts
164 
165  // We write the file in the ExodusII format.
166  ExodusII_IO(mesh).write_equation_systems(filename, equation_systems);
167 
168  // Make sure that the writing is done before the reading starts.
170 
171  // Now read it back in
172  MeshType read_mesh(*TestCommWorld);
173  ExodusII_IO exio(read_mesh);
174  exio.read(filename);
175 
176  read_mesh.prepare_for_use();
177  EquationSystems es2(read_mesh);
178  ExplicitSystem & sys2 = es2.add_system<ExplicitSystem>("Test");
179 
180  const std::vector<std::string> & nodal_vars(exio.get_nodal_var_names());
181 
182  for (const auto & var_name : nodal_vars)
183  sys2.add_variable(var_name, SECOND, LAGRANGE);
184 
185  es2.init();
186 
187  for (const auto & var_name : nodal_vars)
188  exio.copy_nodal_solution(sys2, var_name, var_name, 1);
189 
190  testSolution(read_mesh, sys2);
191 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
192  }
193 
194 
195  void testWriteExodusReplicated() { LOG_UNIT_TEST; testWriteExodus<ReplicatedMesh>("rep.e"); }
196  void testWriteExodusDistributed() { LOG_UNIT_TEST; testWriteExodus<DistributedMesh>("dist.e"); }
197 
198 
199  template <typename MeshType>
200  void testWriteNemesis(const std::string & filename)
201  {
202  MeshType mesh(*TestCommWorld);
203  EquationSystems equation_systems(mesh);
204 
205  setupTests(mesh, equation_systems);
206 
207 #ifdef LIBMESH_HAVE_NEMESIS_API
208  // Make sure any previous reads are done before the writing starts
210 
211  // We write the file in the Nemesis format.
212  Nemesis_IO(mesh).write_equation_systems(filename, equation_systems);
213 
214  // Make sure that the writing is done before the reading starts.
216 
217  // Now read it back in
218  MeshType read_mesh(*TestCommWorld);
219  Nemesis_IO nemio(read_mesh);
220  nemio.read(filename);
221 
222  read_mesh.prepare_for_use();
223  EquationSystems es2(read_mesh);
224  ExplicitSystem & sys2 = es2.add_system<ExplicitSystem>("Test");
225 
226  const std::vector<std::string> & nodal_vars(nemio.get_nodal_var_names());
227 
228  for (const auto & var_name : nodal_vars)
229  sys2.add_variable(var_name, SECOND, LAGRANGE);
230 
231  es2.init();
232 
233  for (const auto & var_name : nodal_vars)
234  nemio.copy_nodal_solution(sys2, var_name, var_name, 1);
235 
236  // FIXME - Nemesis still needs work for vector-valued variables
237  // testSolution(read_mesh, sys2);
238 #else
239  libmesh_ignore(filename);
240 #endif // #ifdef LIBMESH_HAVE_NEMESIS_API
241  }
242 
243  void testWriteNemesisReplicated() { LOG_UNIT_TEST; testWriteNemesis<ReplicatedMesh>("rep.nem"); }
244  void testWriteNemesisDistributed() { LOG_UNIT_TEST; testWriteNemesis<DistributedMesh>("dist.nem"); }
245 
246 };
247 
This is the EquationSystems class.
virtual void read(const std::string &base_filename) override
Implements reading the mesh from several different files.
Definition: nemesis_io.C:214
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:1196
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Definition: mesh_output.C:31
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void copy_nodal_solution(System &system, std::string system_var_name, std::string exodus_var_name, unsigned int timestep=1)
If we read in a nodal solution while reading in a mesh, we can attempt to copy that nodal solution in...
Definition: nemesis_io.C:1686
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
void barrier() const
void testWriteNemesis(const std::string &filename)
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.
The libMesh namespace provides an interface to certain functionality in the library.
This is the MeshBase class.
Definition: mesh_base.h:75
CPPUNIT_TEST_SUITE_REGISTRATION(WriteVecAndScalar)
processor_id_type n_processors() const
void libmesh_ignore(const Args &...)
The Nemesis_IO class implements reading parallel meshes in the Nemesis file format from Sandia Nation...
Definition: nemesis_io.h:51
The UnstructuredMesh class is derived from the MeshBase class.
virtual void write_equation_systems(const std::string &fname, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
Writes out the solution for no specific time or timestep.
Definition: exodusII_io.C:2033
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
void testWriteExodus(const std::string &filename)
void copy_nodal_solution(System &system, std::string system_var_name, std::string exodus_var_name, unsigned int timestep=1)
If we read in a nodal solution while reading in a mesh, we can attempt to copy that nodal solution in...
Definition: exodusII_io.C:1043
void testSolution(const MeshBase &read_mesh, const System &sys2)
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1593
void setupTests(UnstructuredMesh &mesh, EquationSystems &equation_systems)
const std::vector< std::string > & get_nodal_var_names()
Return list of the nodal variable names.
Definition: nemesis_io.C:1678
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1357
virtual void read(const std::string &name) override
This method implements reading a mesh from a specified file.
Definition: exodusII_io.C:244
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void set(const numeric_index_type i, const T value)=0
Sets v(i) = value.
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
processor_id_type processor_id() const
const std::vector< std::string > & get_nodal_var_names()
Return list of the nodal variable names.
Definition: exodusII_io.C:2363
Manages consistently variables, degrees of freedom, and coefficient vectors for explicit systems...
uint8_t dof_id_type
Definition: id_types.h:67