MFEM  v4.2.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-2020, 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_Session mpi;
37  if (!mpi.Root()) { mfem::out.Disable(); mfem::err.Disable(); }
38 #endif
39 
40  // Parse command-line options.
41  const char *coll_name = NULL;
42  int cycle = 0;
43  bool visualization = true;
44 
45  OptionsParser args(argc, argv);
46  args.AddOption(&coll_name, "-r", "--root-file",
47  "Set the VisIt data collection root file prefix.", true);
48  args.AddOption(&cycle, "-c", "--cycle", "Set the cycle index to read.");
49  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
50  "--no-visualization",
51  "Enable or disable GLVis visualization.");
52  args.Parse();
53  if (!args.Good())
54  {
55  args.PrintUsage(mfem::out);
56  return 1;
57  }
58  args.PrintOptions(mfem::out);
59 
60 #ifdef MFEM_USE_MPI
61  VisItDataCollection dc(MPI_COMM_WORLD, coll_name);
62 #else
63  VisItDataCollection dc(coll_name);
64 #endif
65  dc.Load(cycle);
66 
67  if (dc.Error() != DataCollection::NO_ERROR)
68  {
69  mfem::out << "Error loading VisIt data collection: " << coll_name << endl;
70  return 1;
71  }
72 
73  typedef DataCollection::FieldMapType fields_t;
74  const fields_t &fields = dc.GetFieldMap();
75  // Print the names of all fields.
76  mfem::out << "fields: [ ";
77  for (fields_t::const_iterator it = fields.begin(); it != fields.end(); ++it)
78  {
79  if (it != fields.begin()) { mfem::out << ", "; }
80  mfem::out << it->first;
81  }
82  mfem::out << " ]" << endl;
83 
84  if (!visualization) { return 0; }
85 
86  char vishost[] = "localhost";
87  int visport = 19916;
88 
89  // Visualize all fields. If there are no fields, visualize the mesh.
90  for (fields_t::const_iterator it = fields.begin();
91  it != fields.end() || fields.begin() == fields.end(); ++it)
92  {
93  socketstream sol_sock(vishost, visport);
94  bool succeeded = sol_sock.good();
95 #ifdef MFEM_USE_MPI
96  bool all_succeeded;
97  MPI_Allreduce(&succeeded, &all_succeeded, 1,
98  MPI_C_BOOL, MPI_LAND, MPI_COMM_WORLD);
99  succeeded = all_succeeded;
100 #endif
101  if (!succeeded)
102  {
103  mfem::out << "Connection to " << vishost << ':' << visport
104  << " failed." << endl;
105  return 1;
106  }
107 #ifdef MFEM_USE_MPI
108  sol_sock << "parallel " << mpi.WorldSize() << " " << mpi.WorldRank()
109  << "\n";
110 #endif
111  if (fields.begin() == fields.end())
112  {
113  // no fields, just mesh:
114  sol_sock << "mesh\n" << *dc.GetMesh() << flush;
115  break;
116  }
117  sol_sock.precision(8);
118  sol_sock << "solution\n" << *dc.GetMesh() << *it->second
119  << "window_title '" << it->first << "'\n" << flush;
120  }
121 
122  return 0;
123 }
int WorldSize() const
Return MPI_COMM_WORLD&#39;s size.
int Error() const
Get the current error state.
int main(int argc, char *argv[])
Definition: ex1.cpp:66
A simple convenience class that calls MPI_Init() at construction and MPI_Finalize() at destruction...
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:150
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.
bool Root() const
Return true if WorldRank() == 0.
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:434
virtual void Load(int cycle_=0)
Load the collection based on its VisIt data (described in its root file)
int WorldRank() const
Return MPI_COMM_WORLD&#39;s rank.
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:304
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
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:145