MFEM  v4.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, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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  if (!sol_sock)
95  {
96  mfem::out << "Connection to " << vishost << ':' << visport
97  << " failed." << endl;
98  return 1;
99  }
100 #ifdef MFEM_USE_MPI
101  sol_sock << "parallel " << mpi.WorldSize() << " " << mpi.WorldRank()
102  << "\n";
103 #endif
104  if (fields.begin() == fields.end())
105  {
106  // no fields, just mesh:
107  sol_sock << "mesh\n" << *dc.GetMesh() << flush;
108  break;
109  }
110  sol_sock.precision(8);
111  sol_sock << "solution\n" << *dc.GetMesh() << *it->second
112  << "window_title '" << it->first << "'\n" << flush;
113  }
114 
115  return 0;
116 }
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:58
A simple convenience class that calls MPI_Init() at construction and MPI_Finalize() at destruction...
const FieldMapType & GetFieldMap() const
Get a const reference to the internal field map.
Data collection with VisIt I/O routines.
bool Root() const
Return true if WorldRank() == 0.
void PrintUsage(std::ostream &out) const
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:69
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)
Definition: optparser.hpp:76
void Disable()
Disable output.
Definition: globals.hpp:52
GFieldMap::MapType FieldMapType
void PrintOptions(std::ostream &out) const
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:64
bool Good() const
Definition: optparser.hpp:122