MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
load-dc.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, 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
30using namespace std;
31using namespace mfem;
32
33int main(int argc, char *argv[])
34{
35#ifdef MFEM_USE_MPI
36 Mpi::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 int visport = 19916;
47 bool visualization = true;
48
49 OptionsParser args(argc, argv);
50 args.AddOption(&coll_name, "-r", "--root-file",
51 "Set the VisIt data collection root file prefix.", true);
52 args.AddOption(&cycle, "-c", "--cycle", "Set the cycle index to read.");
53 args.AddOption(&pad_digits_cycle, "-pdc", "--pad-digits-cycle",
54 "Number of digits in cycle.");
55 args.AddOption(&pad_digits_rank, "-pdr", "--pad-digits-rank",
56 "Number of digits in MPI rank.");
57 args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
58 "--no-visualization",
59 "Enable or disable GLVis visualization.");
60 args.AddOption(&visport, "-p", "--send-port", "Socket for GLVis.");
61 args.Parse();
62 if (!args.Good())
63 {
65 return 1;
66 }
68
69#ifdef MFEM_USE_MPI
70 VisItDataCollection dc(MPI_COMM_WORLD, coll_name);
71#else
72 VisItDataCollection dc(coll_name);
73#endif
74 dc.SetPadDigitsCycle(pad_digits_cycle);
75 dc.SetPadDigitsRank(pad_digits_rank);
76 dc.Load(cycle);
77
79 {
80 mfem::out << "Error loading VisIt data collection: " << coll_name << endl;
81 return 1;
82 }
83
84 typedef DataCollection::FieldMapType fields_t;
85 const fields_t &fields = dc.GetFieldMap();
86 // Print the names of all fields.
87 mfem::out << "fields: [ ";
88 for (fields_t::const_iterator it = fields.begin(); it != fields.end(); ++it)
89 {
90 if (it != fields.begin()) { mfem::out << ", "; }
91 mfem::out << it->first;
92 }
93 mfem::out << " ]" << endl;
94
95 if (!visualization) { return 0; }
96
97 char vishost[] = "localhost";
98
99 // Visualize all fields. If there are no fields, visualize the mesh.
100 for (fields_t::const_iterator it = fields.begin();
101 it != fields.end() || fields.begin() == fields.end(); ++it)
102 {
103 socketstream sol_sock(vishost, visport);
104 bool succeeded = sol_sock.good();
105#ifdef MFEM_USE_MPI
106 bool all_succeeded;
107 MPI_Allreduce(&succeeded, &all_succeeded, 1,
108 MFEM_MPI_CXX_BOOL, MPI_LAND, MPI_COMM_WORLD);
109 succeeded = all_succeeded;
110#endif
111 if (!succeeded)
112 {
113 mfem::out << "Connection to " << vishost << ':' << visport
114 << " failed." << endl;
115 return 1;
116 }
117#ifdef MFEM_USE_MPI
118 sol_sock << "parallel " << Mpi::WorldSize() << " "
119 << Mpi::WorldRank() << "\n";
120#endif
121 if (fields.begin() == fields.end())
122 {
123 // no fields, just mesh:
124 sol_sock << "mesh\n" << *dc.GetMesh() << flush;
125 break;
126 }
127 sol_sock.precision(8);
128 sol_sock << "solution\n" << *dc.GetMesh() << *it->second
129 << "window_title '" << it->first << "'\n" << flush;
130 }
131
132 return 0;
133}
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.
const FieldMapType & GetFieldMap() const
Get a const reference to the internal field map.
Mesh * GetMesh()
Get a pointer to the mesh in the collection.
GFieldMap::MapType FieldMapType
virtual void SetPadDigitsCycle(int digits)
Set the number of digits used for the cycle.
static void Init()
Initialize hypre by calling HYPRE_Init() and set default options. After calling Hypre::Init(),...
Definition hypre.cpp:33
static bool Root()
Return true if the rank in MPI_COMM_WORLD is zero.
static int WorldRank()
Return the MPI rank in MPI_COMM_WORLD.
static int WorldSize()
Return the size of MPI_COMM_WORLD.
static void Init(int &argc, char **&argv, int required=default_thread_required, int *provided=nullptr)
Singleton creation with Mpi::Init(argc, argv).
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
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 'var' to receive the value. Enable/disable tags are used to set the bool...
Definition optparser.hpp:82
bool Good() const
Return true if the command line options were parsed successfully.
void Disable()
Disable output.
Definition globals.hpp:54
Data collection with VisIt I/O routines.
void Load(int cycle_=0) override
Load the collection based on its VisIt data (described in its root file)
int main()
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
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
const char vishost[]