Biswajit Banerjee

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

   0.000000e+00   -1.000000e+00   0.000000e+00   100   1   50
6
1 0
1 -1 0 0 0 0 25
1 0
2 1 0 0 100 0 25
1 0
3 0 -1 0 50 -1 25
1 0
4 0 1 0 50 1 25
1 0
5 0 0 -1 50 0 0
1 0
6 0 0 1 50 0 50


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.

##### The problem

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.

#### Conclusion

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++.