MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
convert-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 // Convert DC: Convert between different types of data collections
14 // ---------------------------------------------------------------
15 //
16 // This tool demonstrates how to convert between MFEM's different concrete
17 // DataCollection options.
18 //
19 // Currently supported data collection type options:
20 // visit: VisItDataCollection (default)
21 // sidre or sidre_hdf5: SidreDataCollection
22 // json: ConduitDataCollection w/ protocol json
23 // conduit_json: ConduitDataCollection w/ protocol conduit_json
24 // conduit_bin: ConduitDataCollection w/ protocol conduit_bin
25 // hdf5: ConduitDataCollection w/ protocol hdf5
26 //
27 // Compile with: make convert-dc
28 //
29 // Serial sample runs:
30 // convert-dc -s ../../examples/Example5 -st visit -o Example5_Conduit -ot json
31 //
32 // Parallel sample runs:
33 // mpirun -np 4 convert-dc -s ../../examples/Example5-Parallel -st visit
34 // -o Example5-Parallel_Conduit -ot json
35 
36 #include "mfem.hpp"
37 
38 using namespace std;
39 using namespace mfem;
40 
41 DataCollection *create_data_collection(const std::string &dc_name,
42  const std::string &dc_type)
43 {
44  DataCollection *dc = NULL;
45 
46  if (dc_type == "visit")
47  {
48 #ifdef MFEM_USE_MPI
49  dc = new VisItDataCollection(MPI_COMM_WORLD, dc_name);
50 #else
51  dc = new VisItDataCollection(dc_name);
52 #endif
53  }
54  else if ( dc_type == "sidre" || dc_type == "sidre_hdf5")
55  {
56 #ifdef MFEM_USE_SIDRE
57  dc = new SidreDataCollection(dc_name);
58 #else
59  MFEM_ABORT("Must build with MFEM_USE_SIDRE=YES for sidre support.");
60 #endif
61  }
62  else if ( dc_type == "json" ||
63  dc_type == "conduit_json" ||
64  dc_type == "conduit_bin" ||
65  dc_type == "hdf5")
66  {
67 #ifdef MFEM_USE_CONDUIT
68 #ifdef MFEM_USE_MPI
69  ConduitDataCollection *conduit_dc = new ConduitDataCollection(MPI_COMM_WORLD,
70  dc_name);
71 #else
72  ConduitDataCollection *conduit_dc = new ConduitDataCollection(dc_name);
73 #endif
74  conduit_dc->SetProtocol(dc_type);
75  dc = conduit_dc;
76 #else
77  MFEM_ABORT("Must build with MFEM_USE_CONDUIT=YES for conduit support.");
78 #endif
79  }
80  else
81  {
82  MFEM_ABORT("Unsupported Data Collection type:" << dc_type);
83  }
84 
85  return dc;
86 }
87 
88 int main(int argc, char *argv[])
89 {
90 #ifdef MFEM_USE_MPI
91  MPI_Session mpi;
92  if (!mpi.Root()) { mfem::out.Disable(); mfem::err.Disable(); }
93 #endif
94 
95  // Parse command-line options.
96  const char *src_coll_name = NULL;
97  const char *src_coll_type = "visit";
98  int src_cycle = 0;
99  const char *out_coll_name = NULL;
100  const char *out_coll_type = "visit";
101 
102  OptionsParser args(argc, argv);
103  args.AddOption(&src_coll_name, "-s", "--source-root-prefix",
104  "Set the source data collection root file prefix.", true);
105  args.AddOption(&out_coll_name, "-o", "--output-root-prefix",
106  "Set the source data collection root file prefix.", true);
107  args.AddOption(&src_cycle, "-c", "--cycle",
108  "Set the source cycle index to read.");
109  args.AddOption(&src_coll_type, "-st", "--source-type",
110  "Set the source data collection type. Options:\n"
111  "\t visit: VisItDataCollection (default)\n"
112  "\t sidre or sidre_hdf5: SidreDataCollection\n"
113  "\t json: ConduitDataCollection w/ protocol json\n"
114  "\t conduit_json: ConduitDataCollection w/ protocol conduit_json\n"
115  "\t conduit_bin: ConduitDataCollection w/ protocol conduit_bin\n"
116  "\t hdf5: ConduitDataCollection w/ protocol hdf5");
117  args.AddOption(&out_coll_type, "-ot", "--output-type",
118  "Set the output data collection type. Options:\n"
119  "\t visit: VisItDataCollection (default)\n"
120  "\t sidre or sidre_hdf5: SidreDataCollection\n"
121  "\t json: ConduitDataCollection w/ protocol json\n"
122  "\t conduit_json: ConduitDataCollection w/ protocol conduit_json\n"
123  "\t conduit_bin: ConduitDataCollection w/ protocol conduit_bin\n"
124  "\t hdf5: ConduitDataCollection w/ protocol hdf5");
125  args.Parse();
126  if (!args.Good())
127  {
128  args.PrintUsage(mfem::out);
129  return 1;
130  }
131  args.PrintOptions(mfem::out);
132 
133  DataCollection *src = create_data_collection(std::string(src_coll_name),
134  std::string(src_coll_type));
135 
136  DataCollection *out = create_data_collection(std::string(out_coll_name),
137  std::string(out_coll_type));
138 
139  src->Load(src_cycle);
140 
141  if (src->Error() != DataCollection::NO_ERROR)
142  {
143  mfem::out << "Error loading data collection: "
144  << src_coll_name
145  << " (type = "
146  << src_coll_type
147  << ")"
148  << endl;
149  return 1;
150  }
151 
152  out->SetOwnData(false);
153 
154  // add mesh from source dc to output dc
155 #ifdef MFEM_USE_MPI
156  out->SetMesh(MPI_COMM_WORLD,src->GetMesh());
157 #else
158  out->SetMesh(src->GetMesh());
159 #endif
160 
161  // propagate the basics
162  out->SetCycle(src->GetCycle());
163  out->SetTime(src->GetTime());
164  out->SetTimeStep(src->GetTimeStep());
165 
166  // loop over all fields in the source dc, and add them to the output dc
167  const DataCollection::FieldMapType &src_fields = src->GetFieldMap();
168 
169  for (DataCollection::FieldMapType::const_iterator it = src_fields.begin();
170  it != src_fields.end();
171  ++it)
172  {
173  out->RegisterField(it->first,it->second);
174  }
175 
176  out->Save();
177 
178  if (out->Error() != DataCollection::NO_ERROR)
179  {
180  mfem::out << "Error saving data collection: "
181  << out_coll_name
182  << " (type = "
183  << out_coll_type
184  << ")"
185  << endl;
186  return 1;
187  }
188 
189  // cleanup
190  delete src;
191  delete out;
192 
193  return 0;
194 }
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
double GetTime() const
Get physical time (for time-dependent simulations)
Data collection that uses the Conduit Mesh Blueprint specification.
int Error() const
Get the current error state.
int main(int argc, char *argv[])
Definition: ex1.cpp:62
double GetTimeStep() const
Get the simulation time step (for time-dependent simulations)
Data collection with Sidre routines following the Conduit mesh blueprint specification.
A simple convenience class that calls MPI_Init() at construction and MPI_Finalize() at destruction...
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
virtual void Save()
Save the collection to disk.
const FieldMapType & GetFieldMap() const
Get a const reference to the internal field map.
int GetCycle() const
Get time cycle (for time-dependent simulations)
Data collection with VisIt I/O routines.
bool Root() const
Return true if WorldRank() == 0.
DataCollection * create_data_collection(const std::string &dc_name, const std::string &dc_type)
Definition: convert-dc.cpp:41
void PrintUsage(std::ostream &out) const
Definition: optparser.cpp:434
void SetTime(double t)
Set physical time (for time-dependent simulations)
void SetOwnData(bool o)
Set the ownership of collection data.
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)
Definition: optparser.hpp:76
void Disable()
Disable output.
Definition: globals.hpp:54
GFieldMap::MapType FieldMapType
void SetTimeStep(double ts)
Set the simulation time step (for time-dependent simulations)
void PrintOptions(std::ostream &out) const
Definition: optparser.cpp:304
virtual void Load(int cycle_=0)
Load the collection. Not implemented in the base class DataCollection.
Mesh * GetMesh()
Get a pointer to the mesh in the collection.
virtual void SetMesh(Mesh *new_mesh)
Set/change the mesh associated with 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
void SetProtocol(const std::string &protocol)
Set the Conduit relay i/o protocol to use.
bool Good() const
Definition: optparser.hpp:125