Mechanics research codes are typically written by graduate students who aim to get their work done as quickly as possible. These codes are not meant to last beyond the publication of a few related papers. As a result, typical input files have the form
These files have the advantage that they can be read in quickly using an input file stream and the code for doing that can be written in minutes.
But sometimes these code last beyond the tenure of the student, and some other person (or the student her/himself) has to try to understand the format of the input file. That process can take hours and sometimes days, especially if the format has not been documented and the next person has to dig into the code to understand what each of the numbers means.
The XML Approach
One potential approach is to use XML to mark-up the input file which can be transformed into:
Though this file is more verbose that the original, the user does not have to read the source code to understand the format of the file and what the various numbers mean. The challenge for mechanics researchers is parsing and reading that sort of format. Below I’ll show you how that’s done quickly and easily with modern C++.
For production codes such as Vaango we use full featured libraries such as Xerces or LibXml2. However, for research codes, there are a number of simpler alternatives. I prefer the header-only style provided by ZenXml.
The reader code is quite straightforward. Let’s encapsulate it in the class BoundaryReader.
First, the header BoundaryReader.h
and then the implementation BoundaryReader.cc
Of course, if you want to use the data that you have read, you will have to save them. That’s left as an exercise for the reader. As you can see, reading a XML file can be quite straightforward and you have checks on the validity of your file along each step of the reading process.
I recommend shifting the XML or some other similar structured format for the input files in your research codes. However, you may think that the XML format is too verbose. In that case I’d suggest using JSON and in a future post I’ll show you how to read JSON in C++.
If you have questions/comments/corrections, please contact banerjee at parresianz dot com dot zen (without the dot zen).