MFEM  v4.4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
load-dc.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 //
12 // -------------------------------------------------------------------
13 // Load DC Miniapp: Visualize fields saved via DataCollection classes
14 // -------------------------------------------------------------------
15 //
16 // This miniapp loads and visualizes (in GLVis) previously saved data using
17 // DataCollection sub-classes, see e.g. Example 5/5p. Currently, only the
18 // VisItDataCollection class is supported.
19 //
20 // Compile with: make load-dc
21 //
22 // Serial sample runs:
23 // > load-dc -r ../../examples/Example5
24 //
25 // Parallel sample runs:
26 // > mpirun -np 4 load-dc -r ../../examples/Example5-Parallel
27 
28 #include "mfem.hpp"
29 
30 using namespace std;
31 using namespace mfem;
32 
33 int main(int argc, char *argv[])
34 {
35 #ifdef MFEM_USE_MPI
36  Mpi::Init();
37  if (!Mpi::Root()) { mfem::out.Disable(); mfem::err.Disable(); }
38  Hypre::Init();
39 #endif
40 
41  // Parse command-line options.
42  const char *coll_name = NULL;
43  int cycle = 0;
44  bool visualization = true;
45 
46  OptionsParser args(argc, argv);
47  args.AddOption(&coll_name, "-r", "--root-file",
48  "Set the VisIt data collection root file prefix.", true);
49  args.AddOption(&cycle, "-c", "--cycle", "Set the cycle index to read.");
50  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
51  "--no-visualization",
52  "Enable or disable GLVis visualization.");
53  args.Parse();
54  if (!args.Good())
55  {
56  args.PrintUsage(mfem::out);
57  return 1;
58  }
59  args.PrintOptions(mfem::out);
60 
61 #ifdef MFEM_USE_MPI
62  VisItDataCollection dc(MPI_COMM_WORLD, coll_name);
63 #else
64  VisItDataCollection dc(coll_name);
65 #endif
66  dc.Load(cycle);
67 
68  if (dc.Error() != DataCollection::NO_ERROR)
69  {
70  mfem::out << "Error loading VisIt data collection: " << coll_name << endl;
71  return 1;
72  }
73 
74  typedef DataCollection::FieldMapType fields_t;
75  const fields_t &fields = dc.GetFieldMap();
76  // Print the names of all fields.
77  mfem::out << "fields: [ ";
78  for (fields_t::const_iterator it = fields.begin(); it != fields.end(); ++it)
79  {
80  if (it != fields.begin()) { mfem::out << ", "; }
81  mfem::out << it->first;
82  }
83  mfem::out << " ]" << endl;
84 
85  if (!visualization) { return 0; }
86 
87  char vishost[] = "localhost";
88  int visport = 19916;
89 
90  // Visualize all fields. If there are no fields, visualize the mesh.
91  for (fields_t::const_iterator it = fields.begin();
92  it != fields.end() || fields.begin() == fields.end(); ++it)
93  {
94  socketstream sol_sock(vishost, visport);
95  bool succeeded = sol_sock.good();
96 #ifdef MFEM_USE_MPI
97  bool all_succeeded;
98  MPI_Allreduce(&succeeded, &all_succeeded, 1,
99  MPI_C_BOOL, MPI_LAND, MPI_COMM_WORLD);
100  succeeded = all_succeeded;
101 #endif
102  if (!succeeded)
103  {
104  mfem::out << "Connection to " << vishost << ':' << visport
105  << " failed." << endl;
106  return 1;
107  }
108 #ifdef MFEM_USE_MPI
109  sol_sock << "parallel " << Mpi::WorldSize() << " "
110  << Mpi::WorldRank() << "\n";
111 #endif
112  if (fields.begin() == fields.end())
113  {
114  // no fields, just mesh:
115  sol_sock << "mesh\n" << *dc.GetMesh() << flush;
116  break;
117  }
118  sol_sock.precision(8);
119  sol_sock << "solution\n" << *dc.GetMesh() << *it->second
120  << "window_title '" << it->first << "'\n" << flush;
121  }
122 
123  return 0;
124 }
int Error() const
Get the current error state.
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:151
constexpr char vishost[]
const FieldMapType & GetFieldMap() const
Get a const reference to the internal field map.
constexpr int visport
Data collection with VisIt I/O routines.
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:454
virtual void Load(int cycle_=0)
Load the collection based on its VisIt data (described in its root file)
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition: globals.hpp:71
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set &#39;var&#39; to receive the value. Enable/disable tags are used to set the bool...
Definition: optparser.hpp:82
void Disable()
Disable output.
Definition: globals.hpp:54
GFieldMap::MapType FieldMapType
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:324
Mesh * GetMesh()
Get a pointer to the mesh in the collection.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
int main()
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:150