MFEM  v4.3.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-2021, 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 // fms: FMSDataCollection w/ protocol ascii
27 // fms_json: FMSDataCollection w/ protocol json
28 // fms_yaml: FMSDataCollection w/ protocol yaml
29 // fms_hdf5: FMSDataCollection w/ protocol hdf5
30 //
31 // Compile with: make convert-dc
32 //
33 // Serial sample runs:
34 // convert-dc -s ../../examples/Example5 -st visit -o Example5_Conduit -ot json
35 //
36 // Parallel sample runs:
37 // mpirun -np 4 convert-dc -s ../../examples/Example5-Parallel -st visit
38 // -o Example5-Parallel_Conduit -ot json
39 
40 #include "mfem.hpp"
41 
42 using namespace std;
43 using namespace mfem;
44 
45 DataCollection *create_data_collection(const std::string &dc_name,
46  const std::string &dc_type)
47 {
48  DataCollection *dc = NULL;
49 
50  if (dc_type == "visit")
51  {
52 #ifdef MFEM_USE_MPI
53  dc = new VisItDataCollection(MPI_COMM_WORLD, dc_name);
54 #else
55  dc = new VisItDataCollection(dc_name);
56 #endif
57  }
58  else if ( dc_type == "sidre" || dc_type == "sidre_hdf5")
59  {
60 #ifdef MFEM_USE_SIDRE
61  dc = new SidreDataCollection(dc_name);
62 #else
63  MFEM_ABORT("Must build with MFEM_USE_SIDRE=YES for sidre support.");
64 #endif
65  }
66  else if ( dc_type == "json" ||
67  dc_type == "conduit_json" ||
68  dc_type == "conduit_bin" ||
69  dc_type == "hdf5")
70  {
71 #ifdef MFEM_USE_CONDUIT
72 #ifdef MFEM_USE_MPI
73  ConduitDataCollection *conduit_dc = new ConduitDataCollection(MPI_COMM_WORLD,
74  dc_name);
75 #else
76  ConduitDataCollection *conduit_dc = new ConduitDataCollection(dc_name);
77 #endif
78  conduit_dc->SetProtocol(dc_type);
79  dc = conduit_dc;
80 #else
81  MFEM_ABORT("Must build with MFEM_USE_CONDUIT=YES for conduit support.");
82 #endif
83  }
84  else if ( dc_type.substr(0,3) == "fms")
85  {
86 #ifdef MFEM_USE_FMS
87  auto fms_dc = new FMSDataCollection(dc_name);
88  std::string::size_type pos = dc_type.find("_");
89  if (pos != std::string::npos)
90  {
91  std::string fms_protocol(dc_type.substr(pos+1, dc_type.size()-pos-1));
92  fms_dc->SetProtocol(fms_protocol);
93  }
94  dc = fms_dc;
95 #else
96  MFEM_ABORT("Must build with MFEM_USE_FMS=YES for FMS support.");
97 #endif
98  }
99  else
100  {
101  MFEM_ABORT("Unsupported Data Collection type:" << dc_type);
102  }
103 
104  return dc;
105 }
106 
107 int main(int argc, char *argv[])
108 {
109 #ifdef MFEM_USE_MPI
110  MPI_Session mpi;
111  if (!mpi.Root()) { mfem::out.Disable(); mfem::err.Disable(); }
112 #endif
113 
114  // Parse command-line options.
115  const char *src_coll_name = NULL;
116  const char *src_coll_type = "visit";
117  int src_cycle = 0;
118  const char *out_coll_name = NULL;
119  const char *out_coll_type = "visit";
120 
121  OptionsParser args(argc, argv);
122  args.AddOption(&src_coll_name, "-s", "--source-root-prefix",
123  "Set the source data collection root file prefix.", true);
124  args.AddOption(&out_coll_name, "-o", "--output-root-prefix",
125  "Set the source data collection root file prefix.", true);
126  args.AddOption(&src_cycle, "-c", "--cycle",
127  "Set the source cycle index to read.");
128  args.AddOption(&src_coll_type, "-st", "--source-type",
129  "Set the source data collection type. Options:\n"
130  "\t visit: VisItDataCollection (default)\n"
131  "\t sidre or sidre_hdf5: SidreDataCollection\n"
132  "\t json: ConduitDataCollection w/ protocol json\n"
133  "\t conduit_json: ConduitDataCollection w/ protocol conduit_json\n"
134  "\t conduit_bin: ConduitDataCollection w/ protocol conduit_bin\n"
135  "\t hdf5: ConduitDataCollection w/ protocol hdf5\n"
136  "\t fms: FMSDataCollection w/ protocol ascii\n"
137  "\t fms_json: FMSDataCollection w/ protocol json\n"
138  "\t fms_yaml: FMSDataCollection w/ protocol yaml\n"
139  "\t fms_hdf5: FMSDataCollection w/ protocol hdf5");
140  args.AddOption(&out_coll_type, "-ot", "--output-type",
141  "Set the output data collection type. Options:\n"
142  "\t visit: VisItDataCollection (default)\n"
143  "\t sidre or sidre_hdf5: SidreDataCollection\n"
144  "\t json: ConduitDataCollection w/ protocol json\n"
145  "\t conduit_json: ConduitDataCollection w/ protocol conduit_json\n"
146  "\t conduit_bin: ConduitDataCollection w/ protocol conduit_bin\n"
147  "\t hdf5: ConduitDataCollection w/ protocol hdf5\n"
148  "\t fms: FMSDataCollection w/ protocol ascii\n"
149  "\t fms_json: FMSDataCollection w/ protocol json\n"
150  "\t fms_yaml: FMSDataCollection w/ protocol yaml\n"
151  "\t fms_hdf5: FMSDataCollection w/ protocol hdf5");
152  args.Parse();
153  if (!args.Good())
154  {
155  args.PrintUsage(mfem::out);
156  return 1;
157  }
158  args.PrintOptions(mfem::out);
159 
160  DataCollection *src = create_data_collection(std::string(src_coll_name),
161  std::string(src_coll_type));
162 
163  DataCollection *out = create_data_collection(std::string(out_coll_name),
164  std::string(out_coll_type));
165 
166  src->Load(src_cycle);
167 
168  if (src->Error() != DataCollection::NO_ERROR)
169  {
170  mfem::out << "Error loading data collection: "
171  << src_coll_name
172  << " (type = "
173  << src_coll_type
174  << ")"
175  << endl;
176  return 1;
177  }
178 
179  out->SetOwnData(false);
180 
181  // add mesh from source dc to output dc
182 #ifdef MFEM_USE_MPI
183  out->SetMesh(MPI_COMM_WORLD,src->GetMesh());
184 #else
185  out->SetMesh(src->GetMesh());
186 #endif
187 
188  // propagate the basics
189  out->SetCycle(src->GetCycle());
190  out->SetTime(src->GetTime());
191  out->SetTimeStep(src->GetTimeStep());
192 
193  // loop over all fields in the source dc, and add them to the output dc
194  const DataCollection::FieldMapType &src_fields = src->GetFieldMap();
195 
196  for (DataCollection::FieldMapType::const_iterator it = src_fields.begin();
197  it != src_fields.end();
198  ++it)
199  {
200  out->RegisterField(it->first,it->second);
201  }
202 
203  out->Save();
204 
205  if (out->Error() != DataCollection::NO_ERROR)
206  {
207  mfem::out << "Error saving data collection: "
208  << out_coll_name
209  << " (type = "
210  << out_coll_type
211  << ")"
212  << endl;
213  return 1;
214  }
215 
216  // cleanup
217  delete src;
218  delete out;
219 
220  return 0;
221 }
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.
Data collection that uses FMS.
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.
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:150
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:45
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:457
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)
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 SetTimeStep(double ts)
Set the simulation time step (for time-dependent simulations)
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:327
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
int main()
void SetProtocol(const std::string &protocol)
Set the Conduit relay i/o protocol to use.
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:150