File.h

Note

The documentation on this page was automatically extracted from the DOLFIN C++ code and may need to be edited or expanded.

class File

A File represents a data file for reading and writing objects. Unless specified explicitly, the format is determined by the file name suffix. A list of objects that can be read/written to file can be found in GenericFile.h. Compatible file formats include:

  • Binary (.bin)
  • RAW (.raw)
  • SVG (.svg)
  • XD3 (.xd3)
  • XML (.xml)
  • XYZ (.xyz)
  • VTK (.pvd)
enum class Type

File formats

File(const std::string filename, std::string encoding = "ascii")

Create a file with given name

Arguments
filename (std::string)
Name of file.
encoding (std::string)
Optional argument specifying encoding, ASCII is default.
Example
// Save solution to file
File file("solution.pvd");
file << u;

// Read mesh data from file
File mesh_file("mesh.xml");
mesh_file >> mesh;

// Using compressed binary format
File comp_file("solution.pvd", "compressed");
File(MPI_Comm comm, const std::string filename, std::string encoding = "ascii")

Create a file with given name with MPI communicator

Arguments
communicator (MPI_Comm)
The MPI communicator.
filename (std::string)
Name of file.
encoding (std::string)
Optional argument specifying encoding, ascii is default.
Example
// Save solution to file
File file(u.mesh()->mpi_comm(), "solution.pvd");
file << u;

// Read mesh data from file
File mesh_file(MPI_COMM_WORLD, "mesh.xml");
mesh_file >> mesh;

// Using compressed binary format
File comp_file(u.mesh()->mpi_comm(), "solution.pvd",
               "compressed");
File(const std::string filename, Type type, std::string encoding = "ascii")

Create a file with given name and type (format)

Arguments
filename (std::string)
Name of file.
type (Type)
File format.
encoding (std::string)
Optional argument specifying encoding, ascii is default.
Example
File file("solution", vtk);
File(MPI_Comm comm, const std::string filename, Type type, std::string encoding = "ascii")

Create a file with given name and type (format) with MPI communicator

Arguments
communicator (MPI_Comm)
The MPI communicator.
filename (std::string)
Name of file.
type (Type)
File format.
encoding (std::string)
Optional argument specifying encoding, ascii is default.
Example
File file(MPI_COMM_WORLD, "solution", vtk);
File(std::ostream &outstream)

Create an outfile object writing to stream

Arguments
outstream (std::ostream)
The stream.
void operator>>(T &t)

Read from file

void operator<<(const std::pair<const Mesh *, double> mesh)

Write Mesh to file with time

Example
File file("mesh.pvd", "compressed");
file << std::make_pair<const Mesh*, double>(&mesh, t);
void operator<<(const std::pair<const MeshFunction<int> *, double> f)

Write MeshFunction to file with time

Example
File file("markers.pvd", "compressed");
file << std::make_pair<const MeshFunction<int>*, double>(&f, t);
void operator<<(const std::pair<const MeshFunction<std::size_t> *, double> f)

Write MeshFunction to file with time

Example
File file("markers.pvd", "compressed");
file << std::make_pair<const MeshFunction<std::size_t>*, double>(&f, t);
void operator<<(const std::pair<const MeshFunction<double> *, double> f)

Write MeshFunction to file with time

Example
File file("markers.pvd", "compressed");
file << std::make_pair<const MeshFunction<double>*, double>(&f, t);
void operator<<(const std::pair<const MeshFunction<bool> *, double> f)

Write MeshFunction to file with time

Example
File file("markers.pvd", "compressed");
file << std::make_pair<const MeshFunction<bool>*, double>(&f, t);
void operator<<(const std::pair<const Function *, double> u)

Write Function to file with time

Example
File file("solution.pvd", "compressed");
file << std::make_pair<const Function*, double>(&u, t);
void operator<<(const T &t)

Write object to file

static bool exists(std::string filename)

Check if file exists

Arguments
filename (std::string)
Name of file.
Returns
bool
True if the file exists.
static void create_parent_path(std::string filename)
Arguments
filename (std::string)
Name of file / path.