In-line creation of Damaris XML configuration file

Enabling in-line creation of Damaris XML file in C++

This page describes the use of a C++ helper class named ModifyModel. This is currently only available in via C++, via which the class enables the simple creation of a Damaris XML configuration file that can be edited programmatically via inputs from a simulation command line, from computed values, or other input such as from a configuration file that may already be implemented by a code.

The ModifyModel class requires a std::string representation of a Damaris XML configuration file. The string may contain substrings that can be replaced using the C++11 regex library functions. The substrings and their replacements are defined in a std::map<std::string, std::sting> data structure. An example of a Damaris XML file in string representation is shown in the following code block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    std::string damaris_config_xml =  R"V0G0N(<?xml version="1.0"?>
<simulation name="opm-flow" language="c" 
             xmlns="http://damaris.gforge.inria.fr/damaris/model">
 
        <architecture>
            <domains count="1"/>
            <dedicated cores="_DC_REGEX_" nodes="_DN_REGEX_"/>
            <buffer name="buffer" size="_SHMEM_BUFFER_BYTES_REGEX_" />
            <placement />
            <queue  name="queue" size="100" />
        </architecture>
 
        <data>
            <parameter name="n_elements_total"     type="int" value="1" /> 
            <parameter name="n_elements_local"     type="int" value="1" />
            <parameter name="n"     type="int" value="1" />
 
            <layout   name="zonal_layout_usmesh"             type="double" dimensions="n_elements_local" 
                                   global="n_elements_total"   comment="For the field data e.g. Pressure"  />
 
            <variable name="PRESSURE"    layout="zonal_layout_usmesh"     type="scalar"  visualizable="false"  
                                            unit="Pa"   centering="zonal"  store="_MYSTORE_OR_EMPTY_REGEX_" /> 
	</data>
 
        <storage>
            <store name="MyStore" type="HDF5">
                <option key="FileMode">Collective</option>
                <option key="XDMFMode">NoIteration</option>
                <option key="FilesPath">_PATH_OUTPUT_REGEX_ </option>
            </store>
        </storage>
 
    <log FileName="_PATH_REGEX_/logfile" RotationSize="5" 
                              LogFormat="[%TimeStamp%]: %Message%" Flush="True"  LogLevel="debug" />
 
</simulation>
)V0G0N";

What can be seen in the string definition above are a set of sub-strings that are to be replaced. The are as follows:

    • _SHMEM_BUFFER_BYTES_REGEX_ – The size of the shared memory buffer (in bytes)
    • _DC_REGEX_ – The number of dedicated cores per nodes (assumes dedicated nodes == 0)
    • _DN_REGEX_ – The number of dedicated nodes (assumes dedicated cores == 0)
    • _PATH_LOGS_REGEX_ – The path to where log files.
    • _PATH_OUTPUT_REGEX_ – The path to where HDF5 store will be saved – The path requires a slash at the end to denote a directory.
    • _MYSTORE_OR_EMPTY_REGEX_ – Gives the ability to turn on/off the HDF5 output for particular variables

These strings are placed in a std::map/ and passed to the myMod.ReplaceWithRegex() method, which does the substitutions for us. The replaced string is then saved to disk (by global rank 0) and then the resulting file can be used by damaris_initialize(file, MPI_Comm) as input file with which to initialize Damaris.

Please note that the order of replacement can be important, and the replacement strings can themselves contain strings to replace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
      // The output directory for HDF5 files.
      // It requires the '/' at the end to denote a directory
      // N.B. The directory must exist by the time the first output is written
      std::string output_path_str("outputdir/") ;  
      // The output directory for log files
      // N.B. The directory path is created
      std::string log_path_str = "outputdir/damaris_logs/" ; 
 
      // Use the xml string in the ModifyModel constructor:
      damaris::model::ModifyModel myMod = damaris::model::ModifyModel(damaris_config_xml);  
      // The map file find all occurrences of the string in position 1 and 
      // replace it/them with string in position 2
      std::map&lt;std::string,std::string&gt; find_replace_map = {
                {"_SHMEM_BUFFER_BYTES_REGEX_","67108864"},
                {"_DC_REGEX_","1"},
                {"_DN_REGEX_","0"},
                {"_PATH_LOGS_REGEX_", log_path_str },
                {"_PATH_OUTPUT_REGEX_ ", output_path_str},
                {"_MYSTORE_OR_EMPTY_REGEX_","MyStore"},
                {"_MORE_VARIABLES_REGEX_",""},
       };
       myMod.RepalceWithRegEx(find_replace_map);
       std::string damaris_xml_filename_str = "/damaris_config.xml" ;
 
      if (mpiRank == 0) {            
           myMod.SaveXMLStringToFile(damaris_xml_filename_str) ;
      }

Comments are closed.