data2pvd.cpp File Reference
#include <iostream>
#include <fstream>
#include <Logger.h>
#include "MercuryData.h"
#include "VTKData.h"

Functions

template<std::size_t NDIMS>
int transformMercuryToVTK (MercuryDataFile &infile, std::string prefix)
 Templated version to automatically generate VTK output files. More...
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int argc  ,
char **  argv 
)
30 {
31  //Check to see if we actually received two arguments
32  if (argc != 3)
33  {
34  //We didn't. Print a usage and exit the program.
35  logger(FATAL, "Usage: % [infile] [outfilePrefix]\n"
36  " This program converts MercuryDPM .data files to ParaView .pvd data files.\n"
37  " which can then be used directly into ParaView, to visualize your particles.\n"
38  "\n"
39  " infile: MercuryDPM .data file\n"
40  "\n"
41  " outfilePrefix: Prefix to prepend to the output files generated.\n"
42  " The following files will be generated:\n"
43  " - prefix.pvd\n"
44  " - prefix_0.vtu\n"
45  " - prefix_1.vtu\n"
46  " ( ... )\n"
47  " - prefix_987654321.vtu\n"
48  " depending on the amount of time steps.", argv[0]);
49  }
50 
51  // Open our Mercury 3D data file.
52  MercuryDataFile infile(argv[1]);
53 
54  // Was it readable?
55  if (!infile)
56  {
57  logger(FATAL, "Could not open ''%' for input.\n"
58  "Please make sure the file exists and you have the appropriate rights.", argv[1]);
59  }
60  //make sure we don't end in a slash, as this both breaks the creation of a relative path right now,
61  //and it's really not what you want to end up with...
62  if (std::string(argv[2]).back() == '/')
63  {
64  logger(ERROR, "The output prefix ends in a slash. This is not allowed.");
65  }
66 
67  //And were we using a 3D file format?
68  if (infile.isMercuryDataFile<3>())
69  {
70  logger(VERBOSE, "Assuming 3D data format.");
71  return transformMercuryToVTK<3>(infile, argv[2]);
72  }//Or a 2d format?
73  else if (infile.isMercuryDataFile<2>())
74  {
75  logger(VERBOSE, "Assuming 2D data format.");
76  return transformMercuryToVTK<2>(infile, argv[2]);
77  }//help...
78  else
79  {
80  logger(ERROR, "The file '%' does not seem to be a Mercury .data file.\n"
81  "Please make sure you are reading the correct file.", argv[1]);
82  return 4;
83  }
84 }
LL< Log::VERBOSE > VERBOSE
Verbose information.
Definition: Logger.cc:36
LL< Log::FATAL > FATAL
Definition of the different loglevels by its wrapper class LL. These are used as tags in template met...
Definition: Logger.cc:31
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:32
Definition: MercuryData.h:420
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286

References ERROR, FATAL, MercuryDataFile::isMercuryDataFile(), logger, oomph::Global_string_for_annotation::string(), and VERBOSE.

◆ transformMercuryToVTK()

template<std::size_t NDIMS>
int transformMercuryToVTK ( MercuryDataFile infile,
std::string  prefix 
)

Templated version to automatically generate VTK output files.

this function reads the NDIMS-dimensional Mercury .data file, and writes all the corresponding VTK output files.

91 {
92  //We really want to describe our exit code.
93  int exitCode = 0;
94 
95  //We'll set up a descriptor.
96  //Let the compiler figure out what the types / dimensions are, that's too
97  //much work anyway. Oh and make sure position is used as both a datafield
98  //and the actual position of the object.
100  descriptor
101  .addProperty( "Position", & MercuryParticle< NDIMS >::position , true)
102  .addProperty( "Velocity", & MercuryParticle< NDIMS >::velocity )
103  .addProperty( "Rotation", & MercuryParticle< NDIMS >::rotation )
104  .addProperty( "AngVelocity", & MercuryParticle< NDIMS >::angularV )
105  .addProperty( "Radius", & MercuryParticle< NDIMS >::radius )
106  .addProperty( "Species", & MercuryParticle< NDIMS >::speciesID );
107 
108  // We want a Collection file which lists all individual files
109  VTKCollection collection( prefix + ".pvd" );
110  // First, make sure it is sane.
111  if (!collection)
112  logger( FATAL, "Could not open '%.pvd' for output.\n"
113  "Please make sure you have the appropriate permissions and try again.", prefix);
114 
115  std::size_t timeStepCount = 0;
116 
117  //Now, read all the time steps as if they were NDIMS long.
118  for (const MercuryTimeStep<NDIMS> & ts : infile.as<NDIMS>())
119  {
120  //Generate the filename for the individual data files.
121  std::ostringstream filename;
122  filename << prefix << '_' << ts.getTimeStepID() << ".vtu";
123 
124  //We'll set up a datafile containing the individual time step.
125  VTKUnstructuredGrid< MercuryParticle<NDIMS> > timeStepFile(filename.str(), &descriptor);
126  if (!timeStepFile) //but not after we've done some sanity checking!
127  {
128  logger(WARN, "Could not open '%' for output.\n"
129  "Please make sure you have the appropriate permissions and try again.", filename.str());
130  exitCode = 6;
131  break;
132  }
133 
134  if (!infile) //Some sanity checking? More sanity checking! ALL the sanity checking!!!
135  {
136  logger(WARN, "An IOError occurred during the reading of the input file.\n"
137  "Please make sure you are feeding this tool mercury data files.");
138  exitCode = 5;
139  break;
140  }
141  //Okay. we done.
142  //Let's write.
143  timeStepFile.write(ts);
144 
145  //So, a user may give an output path which is in a different directory.
146  // However, since the index files resides in the same output directory
147  // as the time step files, we need to specify relative paths.
148  std::string strippedPath = filename.str();
149  //so, we try to find the last / - because to hell with everybody with other
150  //path separators...
151  std::string::size_type slashPosition = strippedPath.rfind('/');
152  //and take only the last part. It's not the last character, trust me.
153  //we checked for that in main().
154  if (slashPosition != std::string::npos)
155  strippedPath = strippedPath.substr(slashPosition + 1);
156  //And now put the relative path in the listing file
157  collection.append(strippedPath);
158 
159  timeStepCount++;
160  logger(INFO, "Written %", filename.str());
161  }
162 
163  logger(INFO, "Written % time steps in a %D system.", timeStepCount, NDIMS);
164 
165  return exitCode;
166 }
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:33
IteratorProxy< NDIMS > as()
Definition: MercuryData.h:510
Definition: MercuryData.h:157
Definition: Tools/VTKData.h:227
Definition: Tools/VTKData.h:164
std::enable_if< std::is_array< DATATYPE >::value, VTKPointDescriptor & >::type addProperty(std::string name, DATATYPE T::*m, bool isPrimary=false)
Definition: Tools/VTKData.h:180
Definition: Tools/VTKData.h:287
#define INFO(i)
Definition: mumps_solver.h:54
string filename
Definition: MergeRestartFiles.py:39
Definition: MercuryData.h:21

References VTKPointDescriptor< T >::addProperty(), VTKCollection::append(), MercuryDataFile::as(), FATAL, MergeRestartFiles::filename, INFO, logger, oomph::Global_string_for_annotation::string(), WARN, and VTKUnstructuredGrid< T >::write().