Loading...
Searching...
No Matches
HDF5File.h
// Copyright (C) 2012 Chris N. Richardson
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Garth N. Wells, 2012
#ifndef __DOLFIN_HDF5FILE_H
#define __DOLFIN_HDF5FILE_H
#ifdef HAS_HDF5
#include <string>
#include <utility>
#include <vector>
#include <dolfin/common/MPI.h>
#include <dolfin/common/Variable.h>
#include <dolfin/geometry/Point.h>
#include "HDF5Attribute.h"
#include "HDF5Interface.h"
namespace dolfin
{
class CellType;
class Function;
class GenericVector;
class LocalMeshData;
class Mesh;
template<typename T> class MeshFunction;
template<typename T> class MeshValueCollection;
class HDF5Attribute;
class HDF5File : public Variable
{
public:
HDF5File(MPI_Comm comm, const std::string filename,
const std::string file_mode);
~HDF5File();
void close();
void flush();
void write(const std::vector<Point>& points, const std::string name);
void write(const std::vector<double>& values, const std::string name);
void write(const GenericVector& x, const std::string name);
void read(GenericVector& x, const std::string dataset_name,
const bool use_partition_from_file) const;
void write(const Mesh& mesh, const std::string name);
void write(const Mesh& mesh, const std::size_t cell_dim,
const std::string name);
void write(const Function& u, const std::string name);
void write(const Function& u, const std::string name, double timestamp);
void read(Function& u, const std::string name);
void read(Mesh& mesh, const std::string data_path,
bool use_partition_from_file) const;
void read(Mesh& input_mesh,
const std::string topology_path,
const std::string geometry_path,
const int gdim , const CellType& cell_type,
const std::int64_t expected_num_global_cells,
const std::int64_t expected_num_global_points,
bool use_partition_from_file) const;
void write(const MeshFunction<std::size_t>& meshfunction,
const std::string name);
void write(const MeshFunction<int>& meshfunction, const std::string name);
void write(const MeshFunction<double>& meshfunction,
const std::string name);
void write(const MeshFunction<bool>& meshfunction, const std::string name);
void read(MeshFunction<std::size_t>& meshfunction,
const std::string name) const;
void read(MeshFunction<int>& meshfunction, const std::string name) const;
void read(MeshFunction<double>& meshfunction,
const std::string name) const;
void read(MeshFunction<bool>& meshfunction, const std::string name) const;
void write(const MeshValueCollection<std::size_t>& mesh_values,
const std::string name);
void write(const MeshValueCollection<double>& mesh_values,
const std::string name);
void write(const MeshValueCollection<bool>& mesh_values,
const std::string name);
void read(MeshValueCollection<std::size_t>& mesh_values,
const std::string name) const;
void read(MeshValueCollection<double>& mesh_values,
const std::string name) const;
void read(MeshValueCollection<bool>& mesh_values,
const std::string name) const;
bool has_dataset(const std::string dataset_name) const;
// Get/set attributes of an existing dataset
HDF5Attribute attributes(const std::string dataset_name);
void set_mpi_atomicity(bool atomic);
bool get_mpi_atomicity() const;
hid_t h5_id() const
{ return _hdf5_file_id; }
private:
// Friend
friend class XDMFFile;
friend class TimeSeries;
// Write a MeshFunction to file
template <typename T>
void write_mesh_function(const MeshFunction<T>& meshfunction,
const std::string name);
// Read a MeshFunction from file
template <typename T>
void read_mesh_function(MeshFunction<T>& meshfunction,
const std::string name) const;
// Write a MeshValueCollection to file (old format)
template <typename T>
void write_mesh_value_collection_old(
const MeshValueCollection<T>& mesh_values,
const std::string name);
// Write a MeshValueCollection to file (new version using vertex
// indices)
template <typename T>
void write_mesh_value_collection(const MeshValueCollection<T>& mesh_values,
const std::string name);
// Read a MeshValueCollection from file
template <typename T>
void read_mesh_value_collection(MeshValueCollection<T>& mesh_values,
const std::string name) const;
// Read a MeshValueCollection (old format)
template <typename T>
void read_mesh_value_collection_old(MeshValueCollection<T>& mesh_values,
const std::string name) const;
// Write contiguous data to HDF5 data set. Data is flattened into
// a 1D array, e.g. [x0, y0, z0, x1, y1, z1] for a vector in 3D
template <typename T>
void write_data(const std::string dataset_name,
const std::vector<T>& data,
const std::vector<std::int64_t> global_size,
bool use_mpi_io);
// HDF5 file descriptor/handle
hid_t _hdf5_file_id;
// MPI communicator
dolfin::MPI::Comm _mpi_comm;
};
//---------------------------------------------------------------------------
// Needs to go here, because of use in XDMFFile.cpp
template <typename T>
void HDF5File::write_data(const std::string dataset_name,
const std::vector<T>& data,
const std::vector<std::int64_t> global_size,
bool use_mpi_io)
{
dolfin_assert(_hdf5_file_id > 0);
dolfin_assert(global_size.size() > 0);
// Get number of 'items'
std::size_t num_local_items = 1;
for (std::size_t i = 1; i < global_size.size(); ++i)
num_local_items *= global_size[i];
num_local_items = data.size()/num_local_items;
// Compute offset
const std::size_t offset = MPI::global_offset(_mpi_comm.comm(), num_local_items,
true);
std::pair<std::size_t, std::size_t> range(offset,
offset + num_local_items);
// Write data to HDF5 file
const bool chunking = parameters["chunking"];
// Ensure dataset starts with '/'
std::string dset_name(dataset_name);
if (dset_name[0] != '/')
dset_name = "/" + dataset_name;
HDF5Interface::write_dataset(_hdf5_file_id, dset_name, data,
range, global_size, use_mpi_io, chunking);
}
//---------------------------------------------------------------------------
}
#endif
#endif
dolfin::CellType
Definition CellType.h:47
dolfin::Function
Definition Function.h:66
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::HDF5Attribute
Definition HDF5Attribute.h:39
dolfin::HDF5File::HDF5File
HDF5File(MPI_Comm comm, const std::string filename, const std::string file_mode)
Definition HDF5File.cpp:59
dolfin::HDF5File::read
void read(GenericVector &x, const std::string dataset_name, const bool use_partition_from_file) const
Definition HDF5File.cpp:186
dolfin::HDF5File::flush
void flush()
Flush buffered I/O to disk.
Definition HDF5File.cpp:118
dolfin::HDF5File::close
void close()
Close file.
Definition HDF5File.cpp:110
dolfin::HDF5File::~HDF5File
~HDF5File()
Destructor.
Definition HDF5File.cpp:105
dolfin::HDF5File::get_mpi_atomicity
bool get_mpi_atomicity() const
Get the MPI atomicity.
Definition HDF5File.cpp:1975
dolfin::HDF5File::set_mpi_atomicity
void set_mpi_atomicity(bool atomic)
Set the MPI atomicity.
Definition HDF5File.cpp:1969
dolfin::HDF5File::has_dataset
bool has_dataset(const std::string dataset_name) const
Check if dataset exists in HDF5 file.
Definition HDF5File.cpp:1950
dolfin::HDF5File::write
void write(const std::vector< Point > &points, const std::string name)
Write points to file.
Definition HDF5File.cpp:124
dolfin::HDF5Interface::write_dataset
static void write_dataset(const hid_t file_handle, const std::string dataset_path, const std::vector< T > &data, const std::pair< std::int64_t, std::int64_t > range, const std::vector< std::int64_t > global_size, bool use_mpio, bool use_chunking)
dolfin::LocalMeshData
This class stores mesh data on a local processor corresponding to a portion of a (larger) global mesh...
Definition LocalMeshData.h:59
dolfin::MPI::Comm
Definition MPI.h:77
dolfin::MPI::Comm::comm
MPI_Comm comm() const
Return the underlying MPI_Comm object.
Definition MPI.cpp:117
dolfin::MPI::global_offset
static std::size_t global_offset(MPI_Comm comm, std::size_t range, bool exclusive)
Definition MPI.cpp:185
dolfin::MeshFunction
Definition MeshFunction.h:58
dolfin::MeshValueCollection
Definition MeshValueCollection.h:51
dolfin::Mesh
Definition Mesh.h:84
dolfin::Variable::name
std::string name() const
Return name.
Definition Variable.cpp:71
dolfin::Variable::Variable
Variable()
Create unnamed variable.
Definition Variable.cpp:31
dolfin
Definition adapt.h:30
dolfin::parameters
GlobalParameters parameters
The global parameter database.
Definition GlobalParameters.cpp:32