/*******************************************************************************
*
* 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_H5_BASE_H
#define TRIQS_H5_BASE_H
#include
#include
#include
#include "hdf5_hl.h"
#include
namespace triqs {
inline std::string get_triqs_hdf5_data_scheme(bool) { return "bool";}
inline std::string get_triqs_hdf5_data_scheme(int) { return "int";}
inline std::string get_triqs_hdf5_data_scheme(long) { return "long";}
inline std::string get_triqs_hdf5_data_scheme(long long) { return "long long";}
inline std::string get_triqs_hdf5_data_scheme(unsigned int) { return "int";}
inline std::string get_triqs_hdf5_data_scheme(unsigned long) { return "unsigned long";}
inline std::string get_triqs_hdf5_data_scheme(unsigned long long) { return "unsigned long long";}
inline std::string get_triqs_hdf5_data_scheme(float) { return "float";}
inline std::string get_triqs_hdf5_data_scheme(double) { return "double";}
inline std::string get_triqs_hdf5_data_scheme(long double) { return "long double";}
inline std::string get_triqs_hdf5_data_scheme(std::complex) { return "complex";}
namespace h5 {
using utility::mini_vector;
// conversion of C type to HDF5 native
inline H5::PredType native_type_from_C(char) { return H5::PredType::NATIVE_CHAR; }
inline H5::PredType native_type_from_C(signed char) { return H5::PredType::NATIVE_SCHAR; }
inline H5::PredType native_type_from_C(unsigned char) { return H5::PredType::NATIVE_UCHAR; }
inline H5::PredType native_type_from_C(short) { return H5::PredType::NATIVE_SHORT; }
inline H5::PredType native_type_from_C(unsigned short) { return H5::PredType::NATIVE_USHORT; }
inline H5::PredType native_type_from_C(int) { return H5::PredType::NATIVE_INT; }
inline H5::PredType native_type_from_C(unsigned) { return H5::PredType::NATIVE_UINT; }
inline H5::PredType native_type_from_C(long) { return H5::PredType::NATIVE_LONG; }
inline H5::PredType native_type_from_C(unsigned long) { return H5::PredType::NATIVE_ULONG; }
inline H5::PredType native_type_from_C(long long) { return H5::PredType::NATIVE_LLONG; }
inline H5::PredType native_type_from_C(unsigned long long) { return H5::PredType::NATIVE_ULLONG; }
inline H5::PredType native_type_from_C(float) { return H5::PredType::NATIVE_FLOAT; }
inline H5::PredType native_type_from_C(double) { return H5::PredType::NATIVE_DOUBLE; }
inline H5::PredType native_type_from_C(long double) { return H5::PredType::NATIVE_LDOUBLE; }
inline H5::PredType native_type_from_C(bool) { return H5::PredType::NATIVE_SCHAR; }
inline H5::PredType native_type_from_C(std::string) { return H5::PredType::C_S1; }
// conversion of C type to HDF5 native
// NEED TO CHANGE THIS ? It is not standard... We should fix a standard or have a trait
inline H5::PredType h5_type_from_C(char) { return H5::PredType::NATIVE_CHAR; }
inline H5::PredType h5_type_from_C(signed char) { return H5::PredType::NATIVE_SCHAR; }
inline H5::PredType h5_type_from_C(unsigned char) { return H5::PredType::NATIVE_UCHAR; }
inline H5::PredType h5_type_from_C(short) { return H5::PredType::NATIVE_SHORT; }
inline H5::PredType h5_type_from_C(unsigned short) { return H5::PredType::NATIVE_USHORT; }
inline H5::PredType h5_type_from_C(int) { return H5::PredType::NATIVE_INT; }
inline H5::PredType h5_type_from_C(unsigned) { return H5::PredType::NATIVE_UINT; }
inline H5::PredType h5_type_from_C(long) { return H5::PredType::NATIVE_LONG; }
inline H5::PredType h5_type_from_C(unsigned long) { return H5::PredType::NATIVE_ULONG; }
inline H5::PredType h5_type_from_C(long long) { return H5::PredType::NATIVE_LLONG; }
inline H5::PredType h5_type_from_C(unsigned long long) { return H5::PredType::NATIVE_ULLONG; }
inline H5::PredType h5_type_from_C(float) { return H5::PredType::NATIVE_FLOAT; }
inline H5::PredType h5_type_from_C(double) { return H5::PredType::NATIVE_DOUBLE; }
inline H5::PredType h5_type_from_C(long double) { return H5::PredType::NATIVE_LDOUBLE; }
inline H5::PredType h5_type_from_C(bool) { return H5::PredType::NATIVE_SCHAR; }
inline H5::PredType h5_type_from_C(std::string) { return H5::PredType::C_S1; }
// If it is complex return T else T
template struct remove_complex { typedef T type;};
template struct remove_complex > { typedef T type;};
template struct remove_complex > { typedef T type;};
template struct remove_complex: remove_complex{};
//------------- compute the data type (removing complex) ----------------------------------
// in memory
template H5::PredType data_type_memory () { return native_type_from_C(typename remove_complex::type()); }
// the type of data to put in the file_or_group
template H5::PredType data_type_file () { return h5_type_from_C(typename remove_complex::type());}
//------------- compute void * pointer to the data ----------------------------------
template
typename std::enable_if< boost::is_complex::value, typename std::conditional::value, const void *, void *>::type >::type
get_data_ptr (S * p) {
typedef typename std::conditional::value, const typename S::value_type, typename S::value_type>::type T;
return reinterpret_cast(p);
}
template
typename std::enable_if< !boost::is_complex::value, typename std::conditional::value, const void *, void *>::type >::type
get_data_ptr (S * p) {return p;}
// dataspace from lengths and strides. Correct for the complex. strides must be >0
template
H5::DataSpace dataspace_from_LS (
mini_vector const & Ltot,
mini_vector const & L,
mini_vector const & S,
mini_vector const & offset = mini_vector() )
{
static const unsigned int rank = R + (IsComplex ? 1 : 0);
hsize_t totdimsf[rank], dimsf [rank], stridesf[rank], offsetf[rank]; // dataset dimensions
for (size_t u=0; ucreateAttribute(name.c_str(), strdatatype, attr_dataspace);
//myatt_in.write(strdatatype, strwritebuf);
myatt_in.write(strdatatype, (void *)(value.c_str()));
}
/****************** Read string attribute *********************************************/
/// Return the attribute name of obj, and "" if the attribute does not exist.
inline std::string read_string_attribute (H5::H5Object const * obj, std::string name ) {
std::string value ="";
H5::Attribute attr;
if (H5LTfind_attribute(obj -> getId(), name.c_str() )==0) return value;// not present
// can not find how to get the size with hl. Using full interface
//herr_t err2 = H5LTget_attribute_string(gr.getId(), x.c_str(), name.c_str() , &(buf.front()) ) ;
//value.append( &(buf.front()) );
try { attr= obj->openAttribute(name.c_str());}
catch (H5::AttributeIException) { return value;}
try {
H5::DataSpace dataspace = attr.getSpace();
int rank = dataspace.getSimpleExtentNdims();
if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0";
size_t size = attr.getStorageSize();
H5::StrType strdatatype(H5::PredType::C_S1, size+1);
std::vector buf(size+1, 0x00);
attr.read(strdatatype, (void *)(&buf[0]));
value.append( &(buf.front()) );
}
TRIQS_ARRAYS_H5_CATCH_EXCEPTION;
return value;
}
}}
#endif