MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
convert-dc.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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 run (requires MFEM_USE_CONDUIT=YES):
34// > convert-dc -s ../../examples/Example5 -st visit -o Example5_Conduit -ot json
35//
36// Parallel sample run (requires MFEM_USE_CONDUIT=YES):
37// > mpirun -np 4 convert-dc -s ../../examples/Example5-Parallel -st visit -o Example5-Parallel_Conduit -ot json
38
39#include "mfem.hpp"
40
41using namespace std;
42using namespace mfem;
43
44DataCollection *create_data_collection(const std::string &dc_name,
45 const std::string &dc_type)
46{
47 DataCollection *dc = NULL;
48
49 if (dc_type == "visit")
50 {
51#ifdef MFEM_USE_MPI
52 dc = new VisItDataCollection(MPI_COMM_WORLD, dc_name);
53#else
54 dc = new VisItDataCollection(dc_name);
55#endif
56 }
57 else if ( dc_type == "sidre" || dc_type == "sidre_hdf5")
58 {
59#ifdef MFEM_USE_SIDRE
60 dc = new SidreDataCollection(dc_name);
61#else
62 MFEM_ABORT("Must build with MFEM_USE_SIDRE=YES for sidre support.");
63#endif
64 }
65 else if ( dc_type == "json" ||
66 dc_type == "conduit_json" ||
67 dc_type == "conduit_bin" ||
68 dc_type == "hdf5")
69 {
70#ifdef MFEM_USE_CONDUIT
71#ifdef MFEM_USE_MPI
72 ConduitDataCollection *conduit_dc = new ConduitDataCollection(MPI_COMM_WORLD,
73 dc_name);
74#else
75 ConduitDataCollection *conduit_dc = new ConduitDataCollection(dc_name);
76#endif
77 conduit_dc->SetProtocol(dc_type);
78 dc = conduit_dc;
79#else
80 MFEM_ABORT("Must build with MFEM_USE_CONDUIT=YES for conduit support.");
81#endif
82 }
83 else if ( dc_type.substr(0,3) == "fms")
84 {
85#ifdef MFEM_USE_FMS
86 auto fms_dc = new FMSDataCollection(dc_name);
87 std::string::size_type pos = dc_type.find("_");
88 if (pos != std::string::npos)
89 {
90 std::string fms_protocol(dc_type.substr(pos+1, dc_type.size()-pos-1));
91 fms_dc->SetProtocol(fms_protocol);
92 }
93 dc = fms_dc;
94#else
95 MFEM_ABORT("Must build with MFEM_USE_FMS=YES for FMS support.");
96#endif
97 }
98 else
99 {
100 MFEM_ABORT("Unsupported Data Collection type:" << dc_type);
101 }
102
103 return dc;
104}
105
106int main(int argc, char *argv[])
107{
108#ifdef MFEM_USE_MPI
109 Mpi::Init();
111 Hypre::Init();
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 int src_pad_digits_cycle = 6;
119 int src_pad_digits_rank = 6;
120 const char *out_coll_name = NULL;
121 const char *out_coll_type = "visit";
122 int out_pad_digits_cycle = -1;
123 int out_pad_digits_rank = -1;
124
125 OptionsParser args(argc, argv);
126 args.AddOption(&src_coll_name, "-s", "--source-root-prefix",
127 "Set the source data collection root file prefix.", true);
128 args.AddOption(&out_coll_name, "-o", "--output-root-prefix",
129 "Set the source data collection root file prefix.", true);
130 args.AddOption(&src_cycle, "-c", "--cycle",
131 "Set the source cycle index to read.");
132 args.AddOption(&src_pad_digits_cycle, "-pdc", "--pad-digits-cycle",
133 "Number of digits in source cycle.");
134 args.AddOption(&out_pad_digits_cycle, "-opdc", "--out-pad-digits-cycle",
135 "Number of digits in output cycle.");
136 args.AddOption(&src_pad_digits_rank, "-pdr", "--pad-digits-rank",
137 "Number of digits in source MPI rank.");
138 args.AddOption(&out_pad_digits_rank, "-opdr", "--out-pad-digits-rank",
139 "Number of digits in output MPI rank.");
140 args.AddOption(&src_coll_type, "-st", "--source-type",
141 "Set the source 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.AddOption(&out_coll_type, "-ot", "--output-type",
153 "Set the output data collection type. Options:\n"
154 "\t visit: VisItDataCollection (default)\n"
155 "\t sidre or sidre_hdf5: SidreDataCollection\n"
156 "\t json: ConduitDataCollection w/ protocol json\n"
157 "\t conduit_json: ConduitDataCollection w/ protocol conduit_json\n"
158 "\t conduit_bin: ConduitDataCollection w/ protocol conduit_bin\n"
159 "\t hdf5: ConduitDataCollection w/ protocol hdf5\n"
160 "\t fms: FMSDataCollection w/ protocol ascii\n"
161 "\t fms_json: FMSDataCollection w/ protocol json\n"
162 "\t fms_yaml: FMSDataCollection w/ protocol yaml\n"
163 "\t fms_hdf5: FMSDataCollection w/ protocol hdf5");
164 args.Parse();
165 if (!args.Good())
166 {
167 args.PrintUsage(mfem::out);
168 return 1;
169 }
170 if (out_pad_digits_cycle < 0)
171 {
172 out_pad_digits_cycle = src_pad_digits_cycle;
173 }
174 if (out_pad_digits_rank < 0)
175 {
176 out_pad_digits_rank = src_pad_digits_rank;
177 }
179
180 DataCollection *src_dc = create_data_collection(std::string(src_coll_name),
181 std::string(src_coll_type));
182
183 DataCollection *out_dc = create_data_collection(std::string(out_coll_name),
184 std::string(out_coll_type));
185
186 out_dc->SetPadDigitsCycle(out_pad_digits_cycle);
187 out_dc->SetPadDigitsRank(out_pad_digits_rank);
188 src_dc->SetPadDigitsCycle(src_pad_digits_cycle);
189 src_dc->SetPadDigitsRank(src_pad_digits_rank);
190 src_dc->Load(src_cycle);
191
192 if (src_dc->Error() != DataCollection::No_Error)
193 {
194 mfem::out << "Error loading data collection: "
195 << src_coll_name
196 << " (type = "
197 << src_coll_type
198 << ")"
199 << endl;
200 return 1;
201 }
202
203 out_dc->SetOwnData(false);
204
205 // add mesh from source dc to output dc
206#ifdef MFEM_USE_MPI
207 out_dc->SetMesh(MPI_COMM_WORLD,src_dc->GetMesh());
208#else
209 out_dc->SetMesh(src_dc->GetMesh());
210#endif
211
212 // propagate the basics
213 out_dc->SetCycle(src_dc->GetCycle());
214 out_dc->SetTime(src_dc->GetTime());
215 out_dc->SetTimeStep(src_dc->GetTimeStep());
216
217 // loop over all fields in the source dc, and add them to the output dc
218 const DataCollection::FieldMapType &src_fields = src_dc->GetFieldMap();
219
220 for (DataCollection::FieldMapType::const_iterator it = src_fields.begin();
221 it != src_fields.end();
222 ++it)
223 {
224 out_dc->RegisterField(it->first,it->second);
225 }
226
227 out_dc->Save();
228
229 if (out_dc->Error() != DataCollection::No_Error)
230 {
231 mfem::out << "Error saving data collection: "
232 << out_coll_name
233 << " (type = "
234 << out_coll_type
235 << ")"
236 << endl;
237 return 1;
238 }
239
240 // cleanup
241 delete src_dc;
242 delete out_dc;
243
244 return 0;
245}
Data collection that uses the Conduit Mesh Blueprint specification.
void SetProtocol(const std::string &protocol)
Set the Conduit relay i/o protocol to use.
virtual void SetMesh(Mesh *new_mesh)
Set/change the mesh associated with the collection.
int GetCycle() const
Get time cycle (for time-dependent simulations)
void SetTimeStep(real_t ts)
Set the simulation time step (for time-dependent simulations)
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
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.
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
void SetTime(real_t t)
Set physical time (for time-dependent simulations)
virtual void Load(int cycle_=0)
Load the collection. Not implemented in the base class DataCollection.
real_t GetTimeStep() const
Get the simulation time step (for time-dependent simulations)
const FieldMapType & GetFieldMap() const
Get a const reference to the internal field map.
Mesh * GetMesh()
Get a pointer to the mesh in the collection.
void SetOwnData(bool o)
Set the ownership of collection data.
virtual void Save()
Save the collection to disk.
GFieldMap::MapType FieldMapType
virtual void SetPadDigitsCycle(int digits)
Set the number of digits used for the cycle.
real_t GetTime() const
Get physical time (for time-dependent simulations)
Data collection that uses FMS.
static void Init()
Initialize hypre by calling HYPRE_Init() and set default options. After calling Hypre::Init(),...
Definition hypre.hpp:74
static bool Root()
Return true if the rank in MPI_COMM_WORLD is zero.
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 Sidre routines following the Conduit mesh blueprint specification.
Data collection with VisIt I/O routines.
DataCollection * create_data_collection(const std::string &dc_name, const std::string &dc_type)
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