MFEM  v4.5.1
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  int pad_digits_cycle = 6;
45  int pad_digits_rank = 6;
46  bool visualization = true;
47 
48  OptionsParser args(argc, argv);
49  args.AddOption(&coll_name, "-r", "--root-file",
50  "Set the VisIt data collection root file prefix.", true);
51  args.AddOption(&cycle, "-c", "--cycle", "Set the cycle index to read.");
52  args.AddOption(&pad_digits_cycle, "-pdc", "--pad-digits-cycle",
53  "Number of digits in cycle.");
54  args.AddOption(&pad_digits_rank, "-pdr", "--pad-digits-rank",
55  "Number of digits in MPI rank.");
56  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
57  "--no-visualization",
58  "Enable or disable GLVis visualization.");
59  args.Parse();
60  if (!args.Good())
61  {
62  args.PrintUsage(mfem::out);
63  return 1;
64  }
65  args.PrintOptions(mfem::out);
66 
67 #ifdef MFEM_USE_MPI
68  VisItDataCollection dc(MPI_COMM_WORLD, coll_name);
69 #else
70  VisItDataCollection dc(coll_name);
71 #endif
72  dc.SetPadDigitsCycle(pad_digits_cycle);
73  dc.SetPadDigitsRank(pad_digits_rank);
74  dc.Load(cycle);
75 
76  if (dc.Error() != DataCollection::NO_ERROR)
77  {
78  mfem::out << "Error loading VisIt data collection: " << coll_name << endl;
79  return 1;
80  }
81 
82  typedef DataCollection::FieldMapType fields_t;
83  const fields_t &fields = dc.GetFieldMap();
84  // Print the names of all fields.
85  mfem::out << "fields: [ ";
86  for (fields_t::const_iterator it = fields.begin(); it != fields.end(); ++it)
87  {
88  if (it != fields.begin()) { mfem::out << ", "; }
89  mfem::out << it->first;
90  }
91  mfem::out << " ]" << endl;
92 
93  if (!visualization) { return 0; }
94 
95  char vishost[] = "localhost";
96  int visport = 19916;
97 
98  // Visualize all fields. If there are no fields, visualize the mesh.
99  for (fields_t::const_iterator it = fields.begin();
100  it != fields.end() || fields.begin() == fields.end(); ++it)
101  {
102  socketstream sol_sock(vishost, visport);
103  bool succeeded = sol_sock.good();
104 #ifdef MFEM_USE_MPI
105  bool all_succeeded;
106  MPI_Allreduce(&succeeded, &all_succeeded, 1,
107  MPI_C_BOOL, MPI_LAND, MPI_COMM_WORLD);
108  succeeded = all_succeeded;
109 #endif
110  if (!succeeded)
111  {
112  mfem::out << "Connection to " << vishost << ':' << visport
113  << " failed." << endl;
114  return 1;
115  }
116 #ifdef MFEM_USE_MPI
117  sol_sock << "parallel " << Mpi::WorldSize() << " "
118  << Mpi::WorldRank() << "\n";
119 #endif
120  if (fields.begin() == fields.end())
121  {
122  // no fields, just mesh:
123  sol_sock << "mesh\n" << *dc.GetMesh() << flush;
124  break;
125  }
126  sol_sock.precision(8);
127  sol_sock << "solution\n" << *dc.GetMesh() << *it->second
128  << "window_title '" << it->first << "'\n" << flush;
129  }
130 
131  return 0;
132 }
virtual void SetPadDigitsRank(int digits)
Set the number of digits used for the MPI rank in filenames.
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.
virtual void Load(int cycle_=0) override
Load the collection based on its VisIt data (described in its root file)
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:454
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
virtual void SetPadDigitsCycle(int digits)
Set the number of digits used for the cycle.
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