How to output errors into a file in OpenFOAM

How to open a file for writing, dump data into it, and close it.

Step-by-step guide

  1. Open an OFstream object (reference).
  2. Stream your class into the stream object.
    1. For this your data must provide an implementation of the Ostream& operator«(cost StreamedType& t, Ostream& os) either as a friend class (reference), or as a public function that relies on const data access from StreamedType.

You can also use the STL::fstream class. This makes it easy to manipulate output using ios:app for appending and setw and setfill for padding the output with other characters (often zeros).

OpenFOAM file output

// Open the error file for measurement output..
OFstream errorFile(dataFileName);
// Nt : number of cells in the tool mesh
// Nb : number of cells in the tool mesh
// Ev : volume conservation error:
// |tool mesh volume from volume fraction - tool mesh volume | / tool mesh volume.
// Ti : initialization time, loading the mesh and fields,
// Te : execution time of the CCI mesh intersection operation.
// Nx : total number of cells that are intersected: larger than the
//      number of interface cells, as bulk cells are intersected as well.
// Ni : number of interface cells,
// Nk : number of bulk cells.
errorFile << "Nt,Nb,Ev,Ti,Te,Nx,Ni,Nk\n";


 errorFile << toolMesh.nCells() << ","
		<< baseMesh.nCells() << ","
		<< Ev << "," 
		<< Ti << ","
		<< Te << ","
		<< meshIntersection.Nx() << ","
		<< Ni << "," << Nb << nl;

OpenFOAM uses a global object for the newline character: ’nl’, it’s easier to write and corresponds to the OF formatting syntax.

See also