BELFEM 0.9.4
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
BELFEM I/O Usage Guide

Comprehensive guide to BELFEM's file input/output system.

Date: 2026-01-16 Purpose: Usage documentation for src/io module Revision History:

  • 2026-01-16: Initial documentation
  • 2026-01-16: Critical corrections (FileMode::NEW behavior, resource management, parallel I/O contracts)

Table of Contents

  1. Overview
  2. Common Pitfalls
  3. Core Classes
  4. File Utilities
  5. Common Patterns
  6. Performance Tips
  7. MPI Considerations
  8. Thread Safety
  9. Debugging Tips
  10. Enumeration Reference

Overview

BELFEM's I/O module provides unified interfaces for reading and writing data in multiple formats:

  • HDF5: Hierarchical data format for large-scale scientific data (requires BELFEM_HDF5)
  • ASCII: Line-based text file interface with buffering
  • CSV: Comma/delimiter-separated value files (numeric data → Matrix<real>)
  • InputFile: Hierarchical configuration file parser (key-value pairs, nested sections)
  • XML: XML document interface (requires BELFEM_XML)

Build Configuration

# Enable HDF5 support (default ON; highly recommended for large datasets).
# Requires USE_MPI=ON: configure aborts if HDF5 is on and MPI is off
cmake -DUSE_HDF5=ON ..
# Enable XML support via tinyxml2 (default OFF)
cmake -DUSE_TINYXML2=ON ..

(BELFEM_HDF5 is the compile definition those options generate, not a CMake cache entry — setting it on the command line does nothing.)

Design Philosophy

Following BELFEM's coding philosophy:

  • Manual resource management: Files use RAII (constructor opens, destructor closes)
  • Explicit control: FileMode explicitly controls read/write intent
  • Performance-first: Buffering in Ascii, direct memory copy in HDF5
  • Zero-overhead abstractions: Template functions for type-safe I/O
  • C-style error handling: a failed BELFEM_ERROR ends the run, by aborting in release builds or throwing in debug builds; BELFEM_ASSERT is checked only in debug builds (see Error Handling Philosophy)

Error Handling Philosophy

IMPORTANT: BELFEM uses C libraries (HDF5, BLAS, etc.) that report failures through return codes rather than C++ exceptions.

// The vector write path (hdf5_tools.hpp, save_vector_to_file)
T* tData = (T*) malloc(tLength * sizeof(T)); // 1. Allocate
std::memcpy(tData, aVector.data(), ...); // 2. Use (C function, never throws)
aStatus = H5Dwrite(..., tData); // 3. C API (returns error code, never throws)
free(tData); // 4. FREE ALWAYS EXECUTES
BELFEM_ERROR(aStatus >= 0, "..."); // 5. Every build: fails after cleanup
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264

Why this matters:

  • HDF5 functions return error codes (herr_t), they never throw exceptions
  • malloc/free are C functions (don't throw in C++)
  • The two checks differ in when they are active (assert.hpp, BELFEM_ASSERTIONS_ACTIVE):
    • BELFEM_ERROR is always active. A failed check throws in a debug build and calls error_abort() in a release build.
    • BELFEM_ASSERT is active only when assertions are enabled (NDEBUG is not defined, or DEBUG is defined). When active, a failed check throws like BELFEM_ERROR. In a release build (-DNDEBUG) the macro expands to nothing: the condition is not evaluated, and nothing is reported.
  • After every H5Dread and H5Dwrite in hdf5_tools.cpp and hdf5_tools.hpp, the status is checked with a BELFEM_ERROR (aStatus >= 0; the string-list paths save_strings_to_file and load_strings_from_file test for 0), so a failed read or write ends the run in every build, debug and release. The array, vector and matrix save paths skip H5Dwrite for an empty object, so they also reject a failed H5Dcreate ("Could not create dataset") after closing the open dataspace and datatype. Duplicate datasets are rejected with a BELFEM_ERROR as well. BELFEM does not silence HDF5's default error handler, so the library's error stack is printed to stderr before the BELFEM message.
  • In a debug build the throw unwinds the stack, so a raw malloc that has not yet been freed when a check fails is leaked. In a release build a failed BELFEM_ERROR aborts without unwinding.

Resource cleanup guarantees:

  1. Normal path: Destructors run when objects go out of scope (RAII)
  2. Error path: Explicit cleanup (e.g., free()) runs before the status check
  3. Abort/crash: Process terminates → OS reclaims all memory, file descriptors, handles

Implication: in a release build, traditional C++ "exception safety" concerns (RAII wrappers, unique_ptr) do not drive the design of these error paths — a failed BELFEM_ERROR ends the process after the explicit cleanup rather than unwinding through it. Do not read that as "BELFEM never throws": debug builds deliberately throw at every rank so that a failure can be caught under a debugger. The pattern above does not leak in either build because free() precedes the check.

Note for contributors: If you add C++ library code that throws exceptions (e.g., std::vector::at()), wrap it in try/catch and convert to BELFEM_ERROR. Do NOT let exceptions propagate through C library boundaries.


Common Pitfalls

0. Modern C++ Tool Warnings (False Positives for BELFEM)

IMPORTANT: Static analyzers and modern C++ code reviewers may flag BELFEM code with warnings like:

  • "Use `std::unique_ptr` instead of raw pointers"
  • "Potential memory leak: `malloc` without exception-safe cleanup"
  • "Use RAII wrapper for file handles"
  • "Exception-unsafe resource management"

These warnings DO NOT apply to BELFEM because:

  1. BELFEM uses C libraries (HDF5, BLAS, etc.) that return error codes, not exceptions
  2. A failed check ends the run (BELFEM_ERROR: abort in release, throw in debug; BELFEM_ASSERT: throw in debug, compiled out in release)
  3. In release a failed BELFEM_ERROR does not unwind, so cleanup placed before the check always runs; in debug the throw unwinds, which is why the free() must precede the check
  4. Manual resource management is intentional for zero-overhead performance in HPC

Example of "false positive":

// Static analyzer warning: "Memory leak if H5Dwrite throws"
T* tData = (T*) malloc(tLength * sizeof(T));
aStatus = H5Dwrite(..., tData); // C API - NEVER throws
free(tData); // ALWAYS executes (before BELFEM_ERROR)
BELFEM_ERROR(aStatus >= 0, "..."); // Every build; checked AFTER cleanup

This does not leak because:

  • H5Dwrite() is a C function that returns an error code and never throws
  • free() executes before the status check
  • A failed check throws in a debug build and aborts in a release build; in both cases the buffer has already been freed (see Error Handling Philosophy)

When to ignore modern C++ warnings:

  • ✅ malloc/free with C library calls (HDF5, BLAS, etc.)
  • ✅ Raw pointers owned by RAII classes (deleted in destructor)
  • ✅ Manual cleanup before BELFEM_ERROR/BELFEM_ASSERT

When warnings ARE valid:

  • ❌ Calling C++ library functions that throw (e.g., std::vector::at()) between resource allocation and cleanup
  • ❌ Resources not cleaned up in EITHER destructor OR before error abort
  • ❌ Mixing exception-throwing C++ code with manual memory management

See "Error Handling Philosophy" section above for detailed explanation of BELFEM's C-style error handling model.

1. Forgetting to Enable HDF5/XML at Build Time

// WRONG: without BELFEM_HDF5 this compiles and runs, but silently writes nothing:
// the constructor opens no file, save_data() does nothing, group calls return -1,
// and no error is raised
HDF5 file("data.h5", FileMode::NEW);
// SOLUTION: Check CMake configuration
#ifdef BELFEM_HDF5
HDF5 file("data.h5", FileMode::NEW);
#else
BELFEM_ERROR(false, "BELFEM_HDF5 not enabled. Reconfigure with -DUSE_HDF5=ON");
#endif

2. Using Wrong FileMode (CRITICAL - Data Loss Hazard!)

// DANGER: FileMode::NEW silently truncates existing files!
HDF5 file("valuable_results.h5", FileMode::NEW); // DESTROYS existing data!
// SAFE: Check existence first for production workflows
if (file_exists("results.h5")) {
BELFEM_ERROR(false, "File already exists. Use OPEN_RDWR or choose new name.");
}
HDF5 file("results.h5", FileMode::NEW);
// CORRECT: Use OPEN_RDWR for modifying existing files
HDF5 file("existing_results.h5", FileMode::OPEN_RDWR);
// SAFE: Use versioned or timestamped names for checkpoints
string path = "checkpoint_" + std::to_string(timestep) + ".h5";
HDF5 file(path, FileMode::NEW);

FileMode options (cl_HDF5.cpp, HDF5::HDF5):

  • NEW: Create new file (TRUNCATES if exists — H5F_ACC_TRUNC — use with caution!)
  • OPEN_RDONLY: Read-only access (H5F_ACC_RDONLY, fails if doesn't exist)
  • OPEN_RDONLY_PARALLEL: not handled by HDF5 — the constructor has no case for it and falls through to "unknown filemode passed". Ascii implements it (rank 0 reads, then broadcasts)
  • OPEN_RDWR: Read-write access (H5F_ACC_RDWR, fails if doesn't exist)

Warning: FileMode::NEW will destroy existing results if used with automatically generated paths. For restarts, always use OPEN_RDWR. For checkpoints, use explicit versioning.

3. HDF5 Group Management Errors (Resource Leaks!)

// WRONG: Forgetting to close groups causes HDF5 handle leaks
HDF5 file("data.h5", FileMode::NEW);
file.create_group("Results");
file.save_data("GlobalMetadata", metadata); // Writes to /Results/GlobalMetadata!
// CORRECT: Close group before operating at parent level
HDF5 file("data.h5", FileMode::NEW);
file.create_group("Results");
file.save_data("field", vector); // Writes to /Results/field
file.close_active_group(); // Back to root /
file.save_data("GlobalMetadata", metadata); // Writes to /GlobalMetadata
// BEST: Use RAII scope guards (destructor closes automatically)
{
HDF5 file("data.h5", FileMode::NEW);
file.create_group("Results");
file.save_data("Temperature", T);
file.save_data("Pressure", P);
file.close_active_group();
} // File and the active group closed by destructor

HDF5 Group Navigation Invariant (cl_HDF5.cpp, create_group / select_group / close_active_group):

Critical Rule: HDF5 maintains exactly one active group at any time. All save_data() and load_data() calls operate relative to the currently active group.

  • create_group(name) opens the group (calls H5Gcreate2) and makes it active
  • select_group(name) opens an existing group (H5Gopen2) and makes it active
  • close_active_group() closes the current group (H5Gclose) and returns to parent
  • Destructor automatically calls close() → close_active_group() (RAII cleanup)

Resource Management:

  • Each opened group allocates an HDF5 handle (hid_t)
  • Handles are not automatically closed when opening a new group
  • Failure to close groups before program exit → handle leaks (finite limit!)
  • The destructor calls close(), which closes only the active group before H5Fclose; close_tree() exists but is not called automatically. Close every group you opened, or call close_tree() yourself before the object dies.

Recommendation: Always match create_group() / select_group() with close_active_group(). Rely on destructor for final cleanup, but explicitly close groups to maintain clarity.

4. InputFile Section Hierarchy Confusion

// Config file: section { subsection { key : value ; } }
// WRONG: Trying to access nested section directly
const input::Section* sec = input_file.section("subsection"); // BELFEM_ERROR: "Section ... does not exist"
// CORRECT: Navigate hierarchy
const input::Section* parent = input_file.section("Section");
const input::Section* child = parent->section("subsection");
real value = child->get_real("key");

5. CsvFile Assumes Numeric Data

// data.csv contains: "Name,Age,City"
CsvFile csv("data.csv"); // WRONG: Will fail parsing non-numeric data
// SOLUTION: CsvFile only handles numeric data → Matrix<real>
// For mixed types, use Ascii and parse manually
Ascii file("data.csv", FileMode::OPEN_RDONLY);
for (index_t i = 0; i < file.length(); ++i) {
string line = file.line(i);
// Custom parsing logic
}

Core Classes

HDF5 Class

Location: cl_HDF5.hpp

Hierarchical data format interface for saving/loading scalars, vectors, matrices, and structured data.

Constructor

HDF5(const string & aPath,
const enum FileMode aMode = FileMode::NEW,
const bool aParallelMode = false);

Parameters:

  • aPath: File path (.h5 or .hdf5 extension recommended)
  • aMode: File access mode (NEW, OPEN_RDONLY, OPEN_RDWR; OPEN_RDONLY_PARALLEL is not handled by HDF5 — see the FileMode table). The default is NEW, which truncates an existing file: HDF5 file("results.h5"); erases results.h5
  • aParallelMode: write one file per rank — the path is rewritten through make_path_parallel(). This is not PHDF5 and nothing about it is collective
  • With aParallelMode = false (the default) only rank 0 opens the file; on every other rank the object holds no open file. The single-file examples in this guide assume a serial run, or code executed on rank 0 only

Group Operations

// Create and navigate hierarchical structure
hid_t create_group(const string & aLabel); // returns the new active group
hid_t create_group(const string & aLabel,
const hid_t aParent); // aParent must be the active group (checked)
hid_t select_group(const string & aLabel);
void close_active_group();

create_group() raises a BELFEM_ERROR if a group of that name already exists under the active group; use select_group() to reopen it.

Example:

HDF5 file("simulation.h5", FileMode::NEW);
// Create nested structure: /TimeStep_001/Fields/Temperature
file.create_group("TimeStep_001");
file.create_group("Fields");
file.save_data("Temperature", temperature_field); // Saved at /TimeStep_001/Fields/Temperature
file.close_active_group(); // Back to /TimeStep_001
file.close_active_group(); // Back to root /

Save Operations

// Scalars
void save_data(const string & aLabel, const string & aValue);
void save_data(const string & aLabel, const sint & aValue);
void save_data(const string & aLabel, const uint & aValue);
void save_data(const string & aLabel, const luint & aValue);
void save_data(const string & aLabel, const lluint & aValue);
void save_data(const string & aLabel, const real & aValue);
void save_data(const string & aLabel, const bool & aValue);
// Vectors
void save_data(const string & aLabel, const Vector<sint> & aVector);
void save_data(const string & aLabel, const Vector<uint> & aVector);
void save_data(const string & aLabel, const Vector<luint> & aVector);
void save_data(const string & aLabel, const Vector<real> & aVector);
// Matrices (template on T; aTranspose writes the transpose)
void save_data(const string & aLabel, const Matrix<T> & aMatrix,
const bool aTranspose = false);
// Raw arrays (template on T)
void save_data(const string & aLabel, const T * aArray, const index_t aMemorySize);
// String arrays
void save_data(const string & aLabel, const Cell<string> & aStrings);

Load Operations

// Scalars
void load_data(const string & aLabel, string & aValue);
void load_data(const string & aLabel, sint & aValue);
void load_data(const string & aLabel, uint & aValue);
void load_data(const string & aLabel, luint & aValue);
void load_data(const string & aLabel, lluint & aValue);
void load_data(const string & aLabel, real & aValue);
void load_data(const string & aLabel, bool & aValue);
// Vectors (auto-resizes to file data size)
void load_data(const string & aLabel, Vector<sint> & aVector);
void load_data(const string & aLabel, Vector<uint> & aVector);
void load_data(const string & aLabel, Vector<luint> & aVector);
void load_data(const string & aLabel, Vector<real> & aVector);
// Matrices (template on T; auto-resizes to file data size;
// aTranspose reads the transpose)
void load_data(const string & aLabel, Matrix<T> & aMatrix,
const bool aTranspose = false);
// Raw arrays (template on T; aMemorySize must be known in advance)
void load_data(const string & aLabel, T * aArray, const index_t aMemorySize);
// String arrays
void load_data(const string & aLabel, Cell<string> & aStrings);

Best Practices

// GOOD: Explicit group management
{
HDF5 file("results.h5", FileMode::NEW);
// Organized structure
file.create_group("Mesh");
file.save_data("Nodes", node_coords);
file.save_data("Elements", element_connectivity);
file.close_active_group();
file.create_group("Solution");
file.save_data("Temperature", T);
file.save_data("Pressure", P);
file.close_active_group();
} // File automatically closed by destructor

Ascii Class

Location: cl_Ascii.hpp

Line-based ASCII file interface with an in-memory line buffer; changes must be written with save() explicitly.

Constructor

Ascii(const string & aPath, const enum FileMode & aMode);

Modes supported:

  • NEW: empty buffer, save() writes the file
  • OPEN_RDONLY: read existing file into the buffer
  • OPEN_RDONLY_PARALLEL: rank 0 reads, the buffer is broadcast
  • OPEN_RDWR: not supported by Ascii (constructor error)

Interface

// Access lines (0-indexed)
const string & line(index_t aLineNumber) const;
string & line(index_t aLineNumber); // Mutable access
// Number of lines
index_t length() const;
// Append line to buffer (cl_Ascii.cpp, Ascii::print)
void print(const string & aLine);
// Write buffer to file (cl_Ascii.cpp, Ascii::save); returns false
bool save();

save() always returns false: it clears the unsaved-changes flag and then returns that flag. A failure to open the file is reported through BELFEM_ERROR, not as a return value. Before opening the file, save() refuses both read-only modes, OPEN_RDONLY and OPEN_RDONLY_PARALLEL, with a BELFEM_ERROR ("opened in write protected mode"), so a buffer read in either mode cannot overwrite its source.

Only print() marks the buffer as changed. Editing a line through the mutable line(i) does not update that flag, so the destructor's unsaved-changes check does not catch such edits; call save() yourself.

Usage Patterns

Read text file:

Ascii file("input.txt", FileMode::OPEN_RDONLY);
for (index_t i = 0; i < file.length(); ++i) {
const string & line = file.line(i); // No copy
// Parse line
if (line.find("PARAMETER") != string::npos) {
// Extract parameter
}
}

Write text file (cl_Ascii.cpp, Ascii::print):

Ascii output("results.txt", FileMode::NEW);
// Append lines to buffer
output.print("# Simulation results");
output.print("Time, Temperature, Pressure");
for (index_t i = 0; i < n; ++i) {
output.print(std::to_string(time(i)) + ", " +
std::to_string(temp(i)) + ", " +
std::to_string(pres(i)));
}
// Write buffer to file
output.save();

Memory considerations:

  • Entire file loaded into memory (Cell<string> buffer)
  • Efficient for moderate-sized text files (< 10 MB)
  • For large files (> 100 MB), consider streaming with standard library
  • Destructor: raises a BELFEM_ERROR (always active) if print() added lines that were never saved (mChangedSinceLastSave); edits made through line(i) are not tracked

CsvFile Class

Location: cl_CsvFile.hpp

CSV reader that loads numeric data into Matrix<real>. Extends Ascii.

Constructor

CsvFile(const string & aPath, const char aDelimiter = ',');

Parameters:

  • aPath: Path to CSV file
  • aDelimiter: Column separator (default: ,)

Interface

const Matrix<real> & data() const; // Read-only access
Matrix<real> & data(); // Mutable access

Usage

// Load CSV with comma delimiter
CsvFile csv("data.csv");
const Matrix<real> & data = csv.data();
// data(row, col) contains numeric values
real value = data(0, 2); // First row, third column
// Tab-separated values
CsvFile tsv("data.tsv", '\t');

Assumptions:

  • All data is numeric (real type)
  • Rectangular data: the column count is taken from the first row only
  • A non-numeric token makes std::stod throw (std::invalid_argument)
  • A row shorter than the first row is not checked in a release build: the Cell bounds check is a BELFEM_ASSERT, so the read past the row's end is undefined behaviour. In a debug build the assertion fails and throws

InputFile and Input_Section Classes

Location: cl_InputFile.hpp, cl_Input_Section.hpp

Hierarchical configuration file parser with key-value pairs and nested sections.

File Format

// comments start with a double slash
// simple key-value pairs: key : value ;
title : my simulation ;
timesteps : 100 ;
dt : 0.001 s ;
// nested sections: the header is the line before its own-line brace
solver
{
type : mumps ;
tolerance : 1e-6 ;
max iterations : 1000 ;
// sub-sections
preconditioner
{
type : ilu ;
fill level : 2 ;
}
}
// named sections ( type : label )
material : steel
{
density : 7850 kg/m^3 ;
youngs modulus : 200e9 Pa ;
}
material : aluminum
{
density : 2700 kg/m^3 ;
youngs modulus : 69e9 Pa ;
}

Every key line ends with ;; a line without one is not a key. Keys, section types and labels are folded to lower case by the parser; values are stored as written. A key keeps its inner spaces: max iterations is looked up as "max iterations", never as "max_iterations".

Two more parser rules:

  • A ;-terminated line without : is a flag: verbose ; stores the string "true" under verbose.
  • Two sections with the same type and no label are both kept in file order (reachable through section(index)), but lookup by name returns only the later one.

The authoritative description of the syntax, and of every key BELFEM reads, is doc/input_file_reference.md.

InputFile Class

// Constructor
InputFile(const string & aPath);
// Access sections
const input::Section * section(const string & aSection) const; // By name
const input::Section * section(const index_t aIndex) const; // By index
// Query
bool section_exists(const string & aSection) const;
index_t num_sections() const;
// Debug
void print(); // Print the comment-stripped line buffer, numbered

Input_Section Class

// Hierarchy navigation
const string & type() const; // Section type (e.g., "Solver")
const string & label() const; // Section label (e.g., "Steel" in "Material:Steel")
const string & key() const; // Full key (type:label)
// Sub-sections
const Section * section(const string & aType) const; // Type only
const Section * section(const string & aType, const string & aLabel) const; // Type + label
const Section * section(const index_t aIndex) const; // By index
bool section_exists(const string & aType) const;
bool section_exists(const string & aType, const string & aLabel) const;
index_t num_sections() const;
// Key-value access
bool key_exists(const string & aKey) const;
bool key_is_real(const string & aKey) const;
const string & get_string(const string & aKey) const;
bool get_bool(const string & aKey) const;
real get_real(const string & aKey) const;
value get_value(const string & aKey, const string & aUnit) const; // With unit conversion
int get_int(const string & aKey) const; // Rounds; non-finite or out-of-int-range aborts
string get_units(const string & aKey) const;
// Array parsing
void get_ids(const string & aKey, Vector<id_t> & aIDs) const;
void get_reals(const string & aKey, Vector<real> & aReals) const;
void get_id_groups(const string & aKey, Cell<Cell<id_t>> & aIDs) const;
// Keys iteration
index_t num_keys() const;
const string & key(const index_t aIndex) const;
// Hierarchy
int level() const; // Nesting depth
const Section * parent() const;
string tree() const; // Path joined with "->" (e.g., "input.conf->solver->preconditioner");
// parents appear as type:label, the section itself by type only

Usage Example

// Load configuration
InputFile config("simulation.input");
// Access top-level key
if (config.section_exists("Solver")) {
const input::Section* solver = config.section("Solver");
// Get values with type conversion
string solver_type = solver->get_string("type"); // "mumps" (values keep their case)
real tol = solver->get_real("tolerance"); // 1e-6
int max_iter = solver->get_int("max iterations"); // 1000
// Navigate to sub-section
if (solver->section_exists("Preconditioner")) {
const input::Section* precond = solver->section("Preconditioner");
string precond_type = precond->get_string("type"); // "ilu"
}
}
// Access named sections
// the root stores labelled sections under "type:label"; the two-argument
// section("Material", "Steel") form exists on input::Section only
if (config.section_exists("material:steel")) {
const input::Section* steel = config.section("material:steel");
// Get value with unit conversion
value rho = steel->get_value("density", "kg/m^3"); // Converted to base units
value E = steel->get_value("youngs modulus", "Pa");
}
// Iterate over all materials
for (index_t i = 0; i < config.num_sections(); ++i) {
const input::Section* sec = config.section(i);
if (sec->type() == "material") {
string mat_name = sec->label(); // "steel", "aluminum" — the parser lower-cases types and labels
// Process material
}
}

Unit Handling:

The get_value() method automatically converts from file units to BELFEM base units.

Supported temperature conversions (cl_Input_Section.cpp, create_key):

  • K, °K — Kelvin (base unit, no conversion)
  • C, °C — Celsius → K: T_K = T_C + 273.15
  • °F — Fahrenheit → K: T_K = (T_F - 32) / 1.8 + 273.15 (a bare F is farad, not Fahrenheit)
  • R, °R — Rankine → K: T_K = T_R / 1.8

General units: Uses unit_to_si() from physics module for automatic SI conversion:

// Example conversions (from input file → BELFEM internal units)
density : 7.85 g/cm^3 ; → 7850 kg/m³
pressure : 100 MPa ; → 1e8 Pa
length : 25.4 mm ; → 0.0254 m
energy : 1 kJ ; → 1000 J

Usage:

// File contains: temperature : 300 C ;
value T = section->get_value("temperature", "C");
// T automatically converted to 573.15 K (BELFEM base unit)
// get_real() returns the converted value directly
real T_kelvin = section->get_real("temperature"); // 573.15 K
// get_units() returns the original unit string from file
string unit = section->get_units("temperature"); // "C"

Error Handling Contract:

  • Missing keys or sections are logic errors, not recoverable conditions.
  • InputFile uses fail-fast behavior via BELFEM_ERROR assertions.
  • Always check section_exists() and key_exists() before access in production code.

XML Class

Location: cl_XML.hpp

XML document interface using TinyXML2 backend.

Requires: BELFEM_XML enabled at compile time and TinyXML2 library

Constructor

XML(const string & aPath, const FileMode aMode = FileMode::OPEN_RDONLY);

The aMode parameter is accepted but ignored: every mode loads the existing file at aPath, so XML is a reader only and FileMode::NEW does not create a file. Without BELFEM_XML, the constructor raises BELFEM_ERROR ("XML support not compiled in").

Interface

// Path accessor
const string & path() const;
// Navigation
void select_first_child(const string & aLabel);
void select_parent();
bool next_sibling_of_same_name();
void select_subtree(const string & aTree); // Path like "root/section/subsection"
// Query
bool child_exists(const string & aLabel);
bool key_exists(const string & aKey);
uint number_of_children();
uint number_of_children(const string & aLabel);
// Child-element text access ( XML attributes are not read )
string get_string(const string & aKey);
int get_int(const string & aKey);
real get_real(const string & aKey);
bool get_bool(const string & aKey);

Usage Example

<!-- config.xml -->
<Configuration>
<Solver>
<type>MUMPS</type>
<threads>8</threads>
<Tolerance>1e-6</Tolerance>
<MaxIterations>1000</MaxIterations>
</Solver>
</Configuration>
#ifdef BELFEM_XML
XML xml("config.xml", FileMode::OPEN_RDONLY);
// Navigate to Solver element
xml.select_first_child("Configuration");
xml.select_first_child("Solver");
// Read the text of child elements ( get_* looks up a child element by
// name; it does not read XML attributes )
string solver_type = xml.get_string("type"); // "MUMPS"
int threads = xml.get_int("threads"); // 8
real tolerance = xml.get_real("Tolerance"); // 1e-6
xml.select_parent(); // Back to Configuration
#else
BELFEM_ERROR(false, "XML support not enabled. Reconfigure with -DUSE_TINYXML2=ON");
#endif

File Utilities

Location: filetools.hpp, filetools.cpp

FileMode Enum

enum class FileMode
{
NEW, // Create new file (TRUNCATES if exists!)
OPEN_RDONLY, // Read-only (error if doesn't exist)
OPEN_RDONLY_PARALLEL, // MPI parallel read-only
OPEN_RDWR // Read-write (error if doesn't exist)
};

Free Functions

// Check file existence (filetools.cpp, `file_exists`)
bool file_exists(const string & aPath);
// Extract file extension — lives in src/core/stringtools.hpp, not in filetools
string filetype(const string & aPath);
// Create MPI-safe file paths (make_path_parallel, filetools.cpp)
string make_path_parallel(const string & aPath);
// Resolve a data file named in an input file: the run directory first, then
// below aSubDirectory of the shared data directory (gBelfemDataPath);
// returns aFile unchanged if nothing is found (filetools.hpp)
string search_data_file(const string & aFile, const string & aSubDirectory);

Usage:

// Check before opening
if (file_exists("restart.h5")) {
HDF5 file("restart.h5", FileMode::OPEN_RDONLY);
// Load restart data
}
// Extract extension
string ext = filetype("data.exo"); // Returns "exo"
// MPI-parallel file naming (make_path_parallel, filetools.cpp)
string path = make_path_parallel("output.h5");
// Serial (comm_size == 1): "output.h5" (unchanged)
// Parallel with 4 ranks:
// Rank 0: "output_4.0.h5"
// Rank 1: "output_4.1.h5"
// Rank 2: "output_4.2.h5"
// Rank 3: "output_4.3.h5"
// Pattern: base_N.X.ext where N=comm_size(), X=comm_rank()
HDF5 file(path, FileMode::NEW);

Implementation details:

  • file_exists(): std::filesystem::exists()
  • filetype(): Returns substring after last '.'
  • make_path_parallel(): reads the global communicator gComm for the rank and size. BELFEM is not internally thread safe (doc/coding_philosophy.md); this is safe only in the sense that it does not mutate gComm.

Common Patterns

Pattern 1: Checkpoint/Restart with HDF5

// Save checkpoint
void save_checkpoint(const Vector<real> & aState, real aTime, int aTimestep) {
HDF5 file("checkpoint.h5", FileMode::NEW);
file.save_data("time", aTime);
file.save_data("timestep", aTimestep);
file.save_data("state", aState);
}
// Load checkpoint
void load_checkpoint(Vector<real> & aState, real & aTime, int & aTimestep) {
if (!file_exists("checkpoint.h5")) {
BELFEM_ERROR(false, "Checkpoint file not found");
}
HDF5 file("checkpoint.h5", FileMode::OPEN_RDONLY);
file.load_data("time", aTime);
file.load_data("timestep", aTimestep);
file.load_data("state", aState); // Auto-resizes aState
}

Pattern 2: Configuration-Driven Simulation

void solve_from_config(const string & aConfigPath) {
InputFile tConfig(aConfigPath);
// SolverParameters reads its own keys from the section
// (library, relative tolerance, max iterations, ...)
const input::Section * tSolverSection = tConfig.section("solver");
SolverParameters tParams(tSolverSection);
// a key the parameters do not read is queried directly
if (tSolverSection->key_exists("my extra key")) {
real tExtra = tSolverSection->get_real("my extra key");
// ...
}
// collective: every rank must construct the Solver together
Solver tSolver(tParams);
// ... assemble and solve with tSolver
}

The keys that SolverParameters( const input::Section * ) reads are listed in doc/input_file_reference.md.

Pattern 3: Time-Series Data Storage

void save_time_series(const Cell<real> & aTimes,
const Cell<Vector<real>> & aFields) {
HDF5 file("timeseries.h5", FileMode::NEW);
// Save time vector
Vector<real> times(aTimes.size());
for (index_t i = 0; i < aTimes.size(); ++i) {
times(i) = aTimes(i);
}
file.save_data("times", times);
// Save each timestep in separate group
for (index_t i = 0; i < aFields.size(); ++i) {
string group_name = "TimeStep_" + std::to_string(i);
file.create_group(group_name);
file.save_data("field", aFields(i));
file.close_active_group();
}
}

Pattern 4: Parallel I/O (One File Per Rank)

void save_distributed_data(const Vector<real> & aLocalData) {
// Each rank writes its own file. Pass aParallelMode rather than building
// the path by hand: the open is gated on ( aParallelMode || rank == 0 ),
// so a per-rank path alone leaves the workers with nothing open.
HDF5 file("distributed_output.h5", FileMode::NEW, true);
file.save_data("local_data", aLocalData);
file.save_data("rank", comm_rank());
file.save_data("size", comm_size());
}
void load_distributed_data(Vector<real> & aLocalData) {
// make_path_parallel() gives the same name the writer used, which is what
// the existence check needs; the open still takes aParallelMode.
string path = make_path_parallel("distributed_output.h5");
if (!file_exists(path)) {
BELFEM_ERROR(false, "Distributed file for rank %d not found", comm_rank());
}
HDF5 file("distributed_output.h5", FileMode::OPEN_RDONLY, true);
file.load_data("local_data", aLocalData);
}

Pattern 5: Reading Tabulated Data from CSV

// Load experimental data from CSV
void load_material_curve(const string & aPath,
Vector<real> & aStrain,
Vector<real> & aStress) {
CsvFile csv(aPath);
const Matrix<real> & data = csv.data();
index_t n = data.n_rows();
aStrain.set_size(n);
aStress.set_size(n);
// Column 0: strain, Column 1: stress
for (index_t i = 0; i < n; ++i) {
aStrain(i) = data(i, 0);
aStress(i) = data(i, 1);
}
}

Performance Tips

1. Minimize HDF5 Group Operations

// SLOW: Open/close file multiple times
for (index_t i = 0; i < n; ++i) {
HDF5 file("data.h5", FileMode::OPEN_RDWR); // File open overhead!
file.save_data("field_" + std::to_string(i), fields(i));
}
// FAST: Open once, write all data
HDF5 file("data.h5", FileMode::NEW);
for (index_t i = 0; i < n; ++i) {
file.save_data("field_" + std::to_string(i), fields(i));
}

2. Batch HDF5 Writes

// SLOW: Many small writes
HDF5 file("data.h5", FileMode::NEW);
for (index_t i = 0; i < 1000000; ++i) {
file.save_data("value_" + std::to_string(i), values(i)); // 1M datasets!
}
// FAST: Write as single vector
HDF5 file("data.h5", FileMode::NEW);
file.save_data("values", values); // Single write

3. Avoid Unnecessary String Copies in Ascii

// INEFFICIENT: Creates string copy each access
Ascii file("large.txt", FileMode::OPEN_RDONLY);
for (index_t i = 0; i < file.length(); ++i) {
string line_copy = file.line(i); // Copy!
// Process
}
// EFFICIENT: Use const reference
Ascii file("large.txt", FileMode::OPEN_RDONLY);
for (index_t i = 0; i < file.length(); ++i) {
const string & line_ref = file.line(i); // No copy
// Process
}

4. Pre-Allocate for Large CSV Files

// CsvFile loads entire file into Matrix<real>
// For very large CSVs (> 1 GB), consider streaming instead
// If CSV is too large:
Ascii file("huge_data.csv", FileMode::OPEN_RDONLY);
// Parse line-by-line, accumulate statistics, then allocate Matrix

5. One File Per Rank for Large MPI Jobs

BELFEM has no parallel-HDF5 support. BELFEM_PHDF5 is defined nowhere in the tree, and H5Pset_fapl_mpio is never called — every HDF5 file is opened with H5P_DEFAULT (cl_HDF5.cpp, HDF5::HDF5). The per-rank pattern is not a fallback; it is the only pattern.

// Each rank writes its own file: pass aParallelMode.
HDF5 file("output.h5", FileMode::NEW, true); // -> output_<size>.<rank>.h5

Do not hand-roll it with make_path_parallel() alone. The open is gated on ( aParallelMode || gComm.rank() == 0 ) (cl_HDF5.cpp, HDF5::HDF5), so passing a per-rank path without the flag still leaves every non-root rank with no open file at all.


MPI Considerations

MPI I/O Contract

The only pattern: one file per rank, via the aParallelMode constructor argument.

  • Each rank writes to its own file: base_N.X.ext
  • No communication overhead during I/O
  • Post-processing tools must gather files

There is no single-file pattern. aParallelMode = true does not open a shared file — it rewrites the path per rank and opens that. Nothing in the HDF5 layer is collective: no MPI access property list is set, and close() is a plain H5Fclose. A rank may open, write and close entirely on its own.

Consequences: there is no collective-close hazard to guard against, and post-processing always has to gather the per-rank files. If you genuinely need cooperative single-file output, it does not exist yet and would have to be added to the HDF5 wrapper.

File Naming Conventions

// Serial job (comm_size() == 1). Without aParallelMode only rank 0 opens
// the file, so in an MPI job this line serves rank 0 alone
HDF5 file("output.h5", FileMode::NEW);
// MPI job: each rank writes its own file.
// Pass aParallelMode -- do NOT hand-roll the path, because the open is gated
// on ( aParallelMode || rank == 0 ) and a per-rank path alone leaves every
// non-root rank with no open file ( cl_HDF5.cpp, HDF5::HDF5 ).
if (comm_size() > 1) {
HDF5 file("output.h5", FileMode::NEW, true);
// rank 0 -> "output_4.0.h5", rank 1 -> "output_4.1.h5", ...
}

Master-Rank I/O Pattern

// Every rank constructs the InputFile: it opens with FileMode::OPEN_RDONLY,
// so each rank reads and parses the file itself; no broadcast is involved
InputFile tConfig("solver.input");
const input::Section * tSolver = tConfig.section("solver");

InputFile does not use OPEN_RDONLY_PARALLEL, so the file must be readable from every rank. Constructing it on rank 0 alone leaves the other ranks without the configuration; they would then need it sent explicitly (comm module).

Per-rank HDF5 output (aParallelMode)

aParallelMode is one file per rank, not cooperative PHDF5. The third constructor argument rewrites the path through make_path_parallel() (cl_HDF5.cpp, HDF5::HDF5) and opens that per-rank file with H5P_DEFAULT — no MPI access property list is ever set (H5Fcreate / H5Fopen in the same constructor). Nothing is collective, so there is no cooperative write and no collective close to synchronize.

// Each rank opens its OWN file: global_solution_4.0.h5, _4.1.h5, ...
HDF5 file("global_solution.h5", FileMode::NEW, true); // aParallelMode = true
file.save_data("local_field", local_data);
// Nothing here is collective. A rank may open, write and close on its own.

Because the files are separate, the usual parallel-I/O hazards do not apply: a rank that opens without the others cannot deadlock, and post-processing has to stitch the per-rank files together itself.


Thread Safety

HDF5 Thread Safety

  • Not thread-safe: the SCLS HDF5 is built without the thread-safe option (H5_HAVE_THREADSAFE is undefined in its H5pubconf.h), and the HDF5 wrapper adds no lock
  • Two threads must not call such a library concurrently, even for separate files: one file per thread does not make concurrent calls safe
  • HDF5 output here is per-rank and process-level; there is no threaded or collective path
  • Solution: call HDF5 from one thread only, or serialize every HDF5 call, including the constructor and the destructor, in a single critical section
#pragma omp parallel
{
// ... thread-parallel compute ...
#pragma omp critical(belfem_hdf5)
{
// open, write and close entirely inside the critical section;
// without aParallelMode only rank 0 opens a file in an MPI job
int tThread = omp_get_thread_num();
HDF5 tFile("thread_" + std::to_string(tThread) + "_output.h5", FileMode::NEW);
tFile.save_data("data", tThreadLocalData);
}
}

Ascii/CsvFile Thread Safety

  • Read-only safe: Multiple threads can read same Ascii object
  • Write not safe: Modifications to buffer require synchronization
// SAFE: Concurrent reads
Ascii file("data.txt", FileMode::OPEN_RDONLY);
#pragma omp parallel for
for (index_t i = 0; i < file.length(); ++i) {
const string & line = file.line(i); // Safe: read-only
// Parse line
}

InputFile Thread Safety

  • Read-only after construction: Safe for concurrent queries
  • Typical usage: Parse once in serial, query in parallel
// Parse in serial
InputFile config("params.input");
const input::Section* solver = config.section("Solver");
// Query in parallel (read-only)
#pragma omp parallel
{
real tol = solver->get_real("tolerance"); // Safe
}

Memory Ownership and Lifetime

All I/O classes use RAII (Resource Acquisition Is Initialization) — resources are automatically released in destructors.

Error Handling Reminder: a failed BELFEM_ERROR ends the run (abort in release, throw in debug); BELFEM_ASSERT is checked in debug builds only. Resources are cleaned up explicitly before the check, or reclaimed by the OS on process termination. See "Error Handling Philosophy" section above for details.

Class Resource Owned Ownership Model Lifetime Rules
HDF5 File handle (hid_t mFile) RAII Destructor calls close() → H5Fclose()
Group handles (Cell<hid_t> mTree) RAII Destructor calls close() → close_active_group() → H5Gclose() on the active group only; close_tree() must be called explicitly to close the rest
Ascii Line buffer (Cell<string> mBuffer) Owned Destructor clears buffer; BELFEM_ERROR (always active) if unsaved changes
CsvFile Matrix data (Matrix<real> mData) Owned Destructor releases matrix memory (via Matrix RAII)
InputFile Section tree (input::Section* mData) Owned Destructor deletes root section (recursive delete)
Section pointers returned Non-owning Valid while InputFile lives; do not delete
input::Section Child sections (Cell<Section*> mData) Owned Destructor deletes all children recursively
Map entries (Map<string, Section*>) Non-owning views Point to children in mData; no separate cleanup
XML TinyXML2 document (tinyxml2::XMLDocument mFile) RAII Managed by tinyxml2 (auto-cleanup)
Element pointers returned Non-owning Valid while XML object lives; do not delete

Lifetime Rules

HDF5:

{
HDF5 file("data.h5", FileMode::NEW);
file.create_group("Results");
// ... operations ...
} // Destructor closes the active group ("Results") and the file;
// with deeper nesting, close the other groups or call close_tree() first

InputFile:

InputFile config("sim.input");
const input::Section* solver = config.section("Solver");
// solver pointer valid here (config still alive)
real tol = solver->get_real("tolerance");
// DO NOT: delete solver; // WRONG! Not the owner
// config destructor will clean up

Ascii buffer changes:

Ascii file("data.txt", FileMode::NEW);
file.print("Line 1");
file.print("Line 2");
file.save(); // Ascii has save(), not save_data() -- MUST call before
// the destructor if mChangedSinceLastSave
// Destructor raises BELFEM_ERROR (always active) if changes not saved

Debugging Tips

1. Enable HDF5 Error Reporting

// HDF5 errors printed to stderr automatically
// Check BELFEM_ERROR and BELFEM_ASSERT messages for details
// Example error (the label only, without the group path; the type name
// comes from datatype_string<T>()):
// "Dataset Temperature of type Matrix<double> does already exist."

2. Check File Existence Before Opening

if (!file_exists("restart.h5")) {
BELFEM_ERROR(false, "Restart file 'restart.h5' not found in %s",
std::filesystem::current_path().c_str());
}

3. Verify HDF5 Group State

// After complex group navigation, verify path
HDF5 file("data.h5", FileMode::NEW);
file.create_group("A");
file.create_group("B");
// Now at /A/B
// When in doubt, close all groups and start fresh
file.close_active_group(); // Back to /A
file.close_active_group(); // Back to /

4. Print InputFile Structure

InputFile config("complex.input");
config.print(); // Prints the numbered, comment-stripped line buffer to stdout

5. Validate Input File Sections

const input::Section* solver = config.section("Solver");
if (!solver->key_exists("tolerance")) {
BELFEM_ERROR(false, "Required key 'tolerance' not found in [Solver] section");
}
if (!solver->key_is_real("tolerance")) {
BELFEM_ERROR(false, "Key 'tolerance' must be numeric");
}

6. HDF5 File Inspection Tools

Use command-line tools to inspect HDF5 files:

# List contents
h5dump -n data.h5
# View dataset
h5dump -d /Results/Temperature data.h5
# Interactive browser (if available)
hdfview data.h5

7. Check CSV Parsing Errors

try {
CsvFile csv("data.csv");
} catch (...) {
BELFEM_ERROR(false, "Failed to parse CSV. Check for non-numeric data.");
}

This catches a non-numeric token (std::stod throws). It does not catch a short row in a release build: that read is unchecked undefined behaviour, not an exception (see CsvFile Assumptions). If the file may be ragged, check the row lengths yourself by reading it with Ascii.


Enumeration Reference

FileMode

Location: filetools.hpp

enum class FileMode
{
NEW, // Create new file (TRUNCATES if exists!)
OPEN_RDONLY, // Open existing file read-only (fails if doesn't exist)
OPEN_RDONLY_PARALLEL, // handled by Ascii; NOT implemented by HDF5
OPEN_RDWR // Open existing file read-write (fails if doesn't exist)
};

Usage Guidelines:

Mode Use Case File Must Exist Allows Write Behavior if Exists
NEW Creating output files No Yes TRUNCATES (data loss!)
OPEN_RDONLY Reading input files Yes No Opens existing
OPEN_RDONLY_PARALLEL Ascii only: rank 0 reads the file and broadcasts the buffer to the others (cl_Ascii.cpp, load_buffer(true)). Ascii::save() refuses this mode with a BELFEM_ERROR. HDF5 has no case for this mode and reaches its "unknown filemode passed" error (cl_HDF5.cpp, HDF5::HDF5) Yes No Opens existing
OPEN_RDWR Appending/modifying Yes Yes Opens existing

Critical: FileMode::NEW uses H5F_ACC_TRUNC — existing files are silently overwritten. Always check file_exists() first or use versioned filenames for production.


See Also