/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2011-2013 by O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see .
*
******************************************************************************/
#ifndef TRIQS_ARRAYS_H5_LOWLEVEL_H
#define TRIQS_ARRAYS_H5_LOWLEVEL_H
#include
#include
#include
#include "../cache.hpp"
namespace triqs { namespace arrays {
namespace h5_impl {
template const void * get_array_data_cptr ( array_const_view const & A) { return h5::get_data_ptr(&(A.storage()[0]));}
template const void * get_array_data_cptr ( array_view const & A) { return h5::get_data_ptr(&(A.storage()[0]));}
template const void * get_array_data_cptr ( array const & A) { return h5::get_data_ptr(&(A.storage()[0]));}
//template void * get_array_data_ptr ( array_view & A) { return h5::get_data_ptr(&(A.storage()[0]));}
//template void * get_array_data_ptr ( array & A) { return h5::get_data_ptr(&(A.storage()[0]));}
template ENABLE_IF(is_amv_value_or_view_class) *
get_array_data_ptr (A & x) { return h5::get_data_ptr(&(x.storage()[0]));}
// the dataspace corresponding to the array. Contiguous data only...
template
H5::DataSpace data_space ( ArrayType const & A) {
static const unsigned int R = ArrayType::rank;
mini_vector S;
mini_vector const & S1 ( A.indexmap().strides() );
for (size_t u=0; u::value;
return h5::dataspace_from_LS ( A.indexmap().domain().lengths(),A.indexmap().domain().lengths(), S);
}
/******************** resize or check the size ****************************************************/
template ENABLE_IF(is_amv_value_class)
resize_or_check ( A & a, mini_vector const & dimsf ) { a.resize( indexmaps::cuboid::domain_t( dimsf)); }
template ENABLE_IF(is_amv_view_class)
resize_or_check ( A const & a, mini_vector const & dimsf ) {
if (a.indexmap().domain().lengths() != dimsf) TRIQS_RUNTIME_ERROR<<"Dimension error : the view can not be resized : "
<< "\n in file : "<< dimsf.to_string()
<< "\n in view : "<
void write_array (h5::group g, std::string const & name, array_const_view const & A, bool C_reorder = true) {
static_assert( !std::is_base_of::value, " Not implemented");// 1d is below
if (C_reorder) { write_array(g,name, make_const_cache(A).view(),false); return; }
try {
H5::DataSet ds = g.create_dataset(name, h5::data_type_file(), data_space(A) );
ds.write( get_array_data_cptr(A), h5::data_type_memory(), data_space(A) );
// if complex, to be python compatible, we add the __complex__ attribute
if (boost::is_complex::value) h5::write_string_attribute(&ds,"__complex__","1");
}
TRIQS_ARRAYS_H5_CATCH_EXCEPTION;
}
template
void write_array (h5::group g, std::string const & name, array const & A, bool C_reorder = true) { write_array(g,name,A(),C_reorder);}
template
void write_array (h5::group g, std::string const & name, array_view const & A, bool C_reorder = true) { write_array(g,name,A(),C_reorder);}
/*********************************** READ array ****************************************************************/
/**
* \brief Read an array or a view from an hdf5 file
* \tparam ArrayType The type of the array/matrix/vector, etc..
* \param f The h5 file or group of type H5::H5File or H5::Group
* \param name The name of the hdf5 array in the file/group where the stack will be stored
* \param A The array to be stored
* \param C_reorder bool If true [default] the data will be stored in C order in the hdf5, hence making a temporary
* cache of the data to reorder them in memory. If false, the array is stored as it [if you know what you are doing]
* \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc).
*/
template
void read_array (h5::group g, std::string const & name, ArrayType1 && A, bool C_reorder = true) {
typedef typename std::remove_reference::type ArrayType;
static_assert( !std::is_base_of::value, " Not implemented");// 1d is below
typedef typename ArrayType::value_type V;
try {
H5::DataSet ds = g.open_dataset(name);
H5::DataSpace dataspace = ds.getSpace();
static const unsigned int Rank = ArrayType::rank + (boost::is_complex::value ? 1 : 0);
int rank = dataspace.getSimpleExtentNdims();
if (rank != Rank) TRIQS_RUNTIME_ERROR << "triqs::array::h5::read. Rank mismatch : the array has rank = "
<";
return fs.str();
}
/**
* \brief Read an array or a view from an hdf5 file
* \tparam ArrayType The type of the array/matrix/vector, etc..
* \param fg The h5 file or group of type H5::H5File or H5::Group
* \param name The name of the hdf5 array in the file/group where the stack will be stored
* \param A The array to be stored
* \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc).
*/
template
//ENABLE_IF(is_amv_value_or_view_class)
ENABLE_IFC(is_amv_value_or_view_class::value && has_scalar_or_string_value_type::value)
h5_read (h5::group fg, std::string const & name, ArrayType & A) { h5_impl::read_array(fg,name, A);}
/**
* \brief Write an array or a view into an hdf5 file
* \tparam ArrayType The type of the array/matrix/vector, etc..
* \param fg The h5 file or group of type H5::H5File or H5::Group
* \param name The name of the hdf5 array in the file/group where the stack will be stored
* \param A The array to be stored
* \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc).
*/
template
//ENABLE_IF(is_amv_value_or_view_class)
ENABLE_IFC(is_amv_value_or_view_class::value && has_scalar_or_string_value_type::value)
h5_write (h5::group fg, std::string const & name, ArrayType const & A) { h5_impl::write_array(fg,name, array_const_view(A));}
}}
#endif