mirror of
https://github.com/triqs/dft_tools
synced 2025-01-12 14:08:24 +01:00
arrays : rm old array_proxy
- useless old draft
This commit is contained in:
parent
172ecaa29e
commit
c151e63de2
@ -1,7 +1,6 @@
|
|||||||
# Doxygen sources
|
# Doxygen sources
|
||||||
set_property(GLOBAL APPEND PROPERTY DOXYGEN_SOURCES
|
set_property(GLOBAL APPEND PROPERTY DOXYGEN_SOURCES
|
||||||
${TRIQS_SOURCE_DIR}/triqs/arrays/h5/simple_read_write.hpp
|
${TRIQS_SOURCE_DIR}/triqs/arrays/h5/simple_read_write.hpp
|
||||||
${TRIQS_SOURCE_DIR}/triqs/arrays/h5/array_proxy.hpp
|
|
||||||
${TRIQS_SOURCE_DIR}/triqs/arrays/h5/array_stack.hpp
|
${TRIQS_SOURCE_DIR}/triqs/arrays/h5/array_stack.hpp
|
||||||
${TRIQS_SOURCE_DIR}/triqs/arrays/array.hpp
|
${TRIQS_SOURCE_DIR}/triqs/arrays/array.hpp
|
||||||
${TRIQS_SOURCE_DIR}/triqs/arrays/matrix.hpp
|
${TRIQS_SOURCE_DIR}/triqs/arrays/matrix.hpp
|
||||||
|
@ -1,127 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
*
|
|
||||||
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
||||||
*
|
|
||||||
* Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
******************************************************************************/
|
|
||||||
#include "./python_stuff.hpp"
|
|
||||||
|
|
||||||
#include "./src/h5/array_proxy.hpp"
|
|
||||||
#include "./src/h5/simple_read_write.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
#include "./src/asserts.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
using namespace triqs::arrays;
|
|
||||||
|
|
||||||
template < class T>
|
|
||||||
void test(std::string filename, T init) {
|
|
||||||
|
|
||||||
h5::H5File file( filename.c_str(), H5F_ACC_TRUNC );
|
|
||||||
|
|
||||||
const size_t N = 12, d= 2;
|
|
||||||
|
|
||||||
array<T,2> A(d,d+1), A2(d,d+1);
|
|
||||||
array<T,3> A_stack_keep(N,d,d+1), A_stack_compare(N,d,d+1);
|
|
||||||
|
|
||||||
array<T,1> B(d), B2(d);
|
|
||||||
array<T,2> B_stack_keep(N,d), B_stack_compare(N,d);
|
|
||||||
|
|
||||||
|
|
||||||
for (size_t u = 0; u<N; ++u) {
|
|
||||||
A() = double(u+1)* init;
|
|
||||||
B() = double(u+1)* init;
|
|
||||||
A(0,0) *=2;
|
|
||||||
B(0) *=2;
|
|
||||||
A_stack_keep(u,range(),range()) = A;
|
|
||||||
B_stack_keep(u,range()) = B;
|
|
||||||
}
|
|
||||||
|
|
||||||
array<T,2> M(6,6);
|
|
||||||
for (int u = 0; u<6; ++u)
|
|
||||||
for (int v = 0; v<6; ++v)
|
|
||||||
M(u,v) = 10*u + v;
|
|
||||||
|
|
||||||
h5_write( file, "A", A_stack_keep);
|
|
||||||
h5_write( file, "A2", A_stack_keep);
|
|
||||||
h5_write( file, "B", B_stack_keep);
|
|
||||||
h5_write( file, "M", M);
|
|
||||||
|
|
||||||
h5::array_proxy<T,3,3> P( file, "A2");
|
|
||||||
P (0,range(), range()) = T(2) *A;
|
|
||||||
|
|
||||||
h5::array_proxy<T,3,3> Pnew( file, "Z", make_shape(2,2,3) );
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
// READING
|
|
||||||
//
|
|
||||||
h5::H5File file2 ( filename.c_str(), H5F_ACC_RDONLY );
|
|
||||||
|
|
||||||
h5::array_proxy<T,3,3> AA( file2, "A");
|
|
||||||
A_stack_compare = AA;
|
|
||||||
assert_all_close (A_stack_keep, A_stack_compare, 1.e-13);
|
|
||||||
std::cerr << "A_stack_compare = "<< A_stack_compare << std::endl;
|
|
||||||
|
|
||||||
array<T,2> A3(1,1);
|
|
||||||
for (size_t u = 0; u<N; ++u) {
|
|
||||||
A() = double(u+1)* init;
|
|
||||||
B() = double(u+1)* init;
|
|
||||||
A(0,0) *=2;
|
|
||||||
B(0) *=2;
|
|
||||||
|
|
||||||
A2 = AA (u,range(), range()) ;
|
|
||||||
std::cerr << "A from file = "<< A2 << std::endl;
|
|
||||||
std::cerr << " ... should be = "<< A << std::endl;
|
|
||||||
assert_all_close (A, A2, 1.e-15);
|
|
||||||
|
|
||||||
A3 = AA (u,range(), range()) ;
|
|
||||||
std::cerr << "A from file with resize = "<< A3 << std::endl;
|
|
||||||
std::cerr << " ... should be = "<< A << std::endl;
|
|
||||||
assert_all_close (A, A3, 1.e-15);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cerr << " ... done ... now matrix "<< std::endl;
|
|
||||||
|
|
||||||
h5::array_proxy<T,2> Pm( file2, "M");
|
|
||||||
A3 = Pm;
|
|
||||||
std::cerr << "M from file with resize = "<< A3 << std::endl;
|
|
||||||
std::cerr << " ... should be = "<< M << std::endl;
|
|
||||||
assert_all_close (M, A3, 1.e-15);
|
|
||||||
|
|
||||||
A3 = Pm( range(1,6,2), range (1,6,2));
|
|
||||||
array_view<T,2> V = M( range(1,6,2), range (1,6,2));
|
|
||||||
std::cerr << "M from file with resize = "<< A3 << std::endl;
|
|
||||||
std::cerr << " ... should be = "<< V << std::endl;
|
|
||||||
assert_all_close (V, A3, 1.e-15);
|
|
||||||
|
|
||||||
std::cerr << " ... done "<< std::endl;
|
|
||||||
|
|
||||||
file2.close();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
|
||||||
|
|
||||||
init_python_stuff(argc,argv);
|
|
||||||
|
|
||||||
test("proxy_d.h5", 1.0 );
|
|
||||||
test("proxy_c.h5", std::complex<double>(1,2) );
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,130 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
*
|
|
||||||
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
||||||
*
|
|
||||||
* Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
******************************************************************************/
|
|
||||||
#ifndef TRIQS_ARRAYS_HDF5_ARRAY_PROXY_H
|
|
||||||
#define TRIQS_ARRAYS_HDF5_ARRAY_PROXY_H
|
|
||||||
#include "./array_proxy_impl.hpp"
|
|
||||||
|
|
||||||
namespace triqs { namespace arrays {
|
|
||||||
namespace h5 {
|
|
||||||
|
|
||||||
/// The array proxy
|
|
||||||
template<typename ValueType, int Rank, int Rank_f = Rank >
|
|
||||||
class array_proxy : TRIQS_CONCEPT_TAG_NAME(ImmutableArray), // WRONG ! IT does not yet implement [ ]
|
|
||||||
public sliceable_object < ValueType,
|
|
||||||
h5::index_system<Rank,Rank_f>,
|
|
||||||
array_proxy_option<Rank_f>,
|
|
||||||
Tag::h5_array_proxy,
|
|
||||||
ViewFactory,
|
|
||||||
indexmaps::slicer,
|
|
||||||
array_proxy<ValueType,Rank, Rank_f> >
|
|
||||||
{
|
|
||||||
public :
|
|
||||||
typedef ValueType value_type;
|
|
||||||
typedef std::pair< boost::shared_ptr<H5::CommonFG> ,std::string> storage_type;
|
|
||||||
static const bool T_is_complex = boost::is_complex<ValueType>::value;
|
|
||||||
typedef index_system< Rank, Rank_f> indexmap_type;
|
|
||||||
static const unsigned int rank = Rank;
|
|
||||||
|
|
||||||
/// Opens a proxy on an existing array. The dataset must exists
|
|
||||||
template< class FileGroupType >
|
|
||||||
array_proxy ( FileGroupType file_group, std::string const & name) :
|
|
||||||
indexmap_ ( indexmap_type(file_group.openDataSet( name.c_str() ).getSpace(),T_is_complex) ) {
|
|
||||||
if (!h5::exists(file_group, name)) TRIQS_RUNTIME_ERROR<< " h5 : no dataset"<< name << " in file ";
|
|
||||||
storage_ = std::make_pair( boost::make_shared<FileGroupType>(file_group),name);
|
|
||||||
DataSet dataset = file_group.openDataSet( name.c_str() );
|
|
||||||
try { if (T_is_complex) write_string_attribute(&dataset,"__complex__","1"); }
|
|
||||||
catch (...) {} // catch if the attribute already exists...
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Constructs a proxy on a new data set of the dimension of the domain D. The data must not exist.
|
|
||||||
template< class FileGroupType, class LengthType >
|
|
||||||
array_proxy ( FileGroupType file_group, std::string const & name_, LengthType L, bool overwrite = false) :
|
|
||||||
indexmap_ ( indexmap_type (L) )
|
|
||||||
{
|
|
||||||
if (h5::exists(file_group, name_)) {
|
|
||||||
if (overwrite) file_group.unlink(name_.c_str());
|
|
||||||
else TRIQS_RUNTIME_ERROR<< " h5 : dataset"<< name_ << " already exists in the file ";
|
|
||||||
}
|
|
||||||
storage_ = std::make_pair( boost::make_shared<FileGroupType>(file_group),name_);
|
|
||||||
DataSpace ds = indexmap_.template dataspace<T_is_complex>(); //(indexmap_type::rank_full, &indexmap_.lengths()[0], &indexmap_.strides()[0] );
|
|
||||||
DataSet dataset = file_group.createDataSet( name_.c_str(), data_type_file(ValueType()), ds);
|
|
||||||
if (T_is_complex) write_string_attribute(&dataset,"__complex__","1");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shallow copy
|
|
||||||
array_proxy(const array_proxy & X):indexmap_(X.indexmap()),storage_(X.storage_){}
|
|
||||||
|
|
||||||
/// for slice construction
|
|
||||||
array_proxy (const indexmap_type & IM, const storage_type & ST): indexmap_(IM),storage_(ST){ }
|
|
||||||
|
|
||||||
public:
|
|
||||||
indexmap_type const & indexmap() const {return indexmap_;}
|
|
||||||
storage_type const & storage() const {return storage_;}
|
|
||||||
storage_type & storage() {return storage_;}
|
|
||||||
const H5::CommonFG * file_group() const { return storage_.first.get();}
|
|
||||||
std::string const & name() const { return storage_.second;}
|
|
||||||
|
|
||||||
typedef typename indexmap_type::domain_type domain_type;
|
|
||||||
domain_type const & domain() const { return indexmap_.domain();}
|
|
||||||
|
|
||||||
typedef typename domain_type::index_value_type shape_type;
|
|
||||||
shape_type const & shape() const { return domain().lengths();}
|
|
||||||
|
|
||||||
size_t num_elements() const { return domain().number_of_elements();}
|
|
||||||
bool is_empty() const { return this->num_elements()==0;}
|
|
||||||
|
|
||||||
template< typename ISP>// put in the file...
|
|
||||||
void operator=(ISP const & X) {
|
|
||||||
try {
|
|
||||||
BOOST_AUTO(C, make_const_cache(X, Option::C()));
|
|
||||||
//typename result_of::cache<false,Tag::C, ISP >::type C(X);
|
|
||||||
DataSet dataset = file_group()->openDataSet( name().c_str() );
|
|
||||||
dataset.write( h5::data(C.view()), h5::data_type_mem(C.view()),h5::data_space(C.view()),indexmap().template dataspace<T_is_complex>());
|
|
||||||
}
|
|
||||||
TRIQS_ARRAYS_H5_CATCH_EXCEPTION;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename LHS> // from the file to the array or the array_view...
|
|
||||||
friend void triqs_arrays_assign_delegation (LHS & lhs, array_proxy const & rhs) {
|
|
||||||
static_assert((is_amv_value_or_view_class<LHS>::value), "LHS is not a value or a view class ");
|
|
||||||
h5::resize_or_check(lhs, rhs.indexmap().domain().lengths());
|
|
||||||
try {
|
|
||||||
DataSet dataset = rhs.file_group()->openDataSet( rhs.name().c_str() );
|
|
||||||
BOOST_AUTO(C, make_cache(lhs, Option::C() ));
|
|
||||||
//typename result_of::cache<true,Tag::C, LHS >::type C(lhs);
|
|
||||||
dataset.read( h5::data(C.view()), h5::data_type_mem(C.view()), h5::data_space(C.view()),
|
|
||||||
rhs.indexmap().template dataspace<T_is_complex>() );
|
|
||||||
}
|
|
||||||
TRIQS_ARRAYS_H5_CATCH_EXCEPTION;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
indexmap_type indexmap_;
|
|
||||||
storage_type storage_;
|
|
||||||
|
|
||||||
};
|
|
||||||
}// namesapce h5
|
|
||||||
|
|
||||||
template < class V, int R, int Rf >
|
|
||||||
struct ViewFactory< V, R, h5::array_proxy_option<Rf>, Tag::h5_array_proxy > { typedef h5::array_proxy <V,R,Rf> type; };
|
|
||||||
}}
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,160 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
*
|
|
||||||
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
||||||
*
|
|
||||||
* Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
******************************************************************************/
|
|
||||||
#ifndef TRIQS_ARRAYS_HDF5_ARRAY_PROXY_IMPL_H
|
|
||||||
#define TRIQS_ARRAYS_HDF5_ARRAY_PROXY_IMPL_H
|
|
||||||
//#define TRIQS_ARRAYS_DEBUG_H5_SLICE
|
|
||||||
|
|
||||||
#include "../indexmaps/cuboid/domain.hpp"
|
|
||||||
#include "./group_or_file.hpp"
|
|
||||||
#include "../impl/sliceable_object.hpp"
|
|
||||||
#include "../impl/tuple_tools.hpp"
|
|
||||||
#include <boost/shared_ptr.hpp>
|
|
||||||
#include <boost/make_shared.hpp>
|
|
||||||
|
|
||||||
namespace triqs { namespace arrays {
|
|
||||||
|
|
||||||
namespace h5 { template<int Rf> struct array_proxy_option {}; }
|
|
||||||
|
|
||||||
namespace Option {
|
|
||||||
template<class IM2, int Rf> struct Im_Opt_2_Opt<IM2, h5::array_proxy_option<Rf> > { typedef h5::array_proxy_option<Rf> type;};
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace h5 {
|
|
||||||
|
|
||||||
template<int Rank, int Rank_full>
|
|
||||||
class index_system {
|
|
||||||
|
|
||||||
public :
|
|
||||||
static const unsigned int rank= Rank, rank_full = Rank_full;
|
|
||||||
typedef mini_vector<size_t, Rank> v_type;
|
|
||||||
typedef mini_vector<size_t, Rank_full> v_type_full;
|
|
||||||
typedef indexmaps::cuboid::domain<Rank> domain_type;
|
|
||||||
domain_type const & domain() const { return mydomain;}
|
|
||||||
|
|
||||||
index_system (v_type_full const & total_lengths_) {
|
|
||||||
total_lens_ = total_lengths_; lens_= total_lengths_;
|
|
||||||
for (size_t i =0; i<rank_full; ++i) stri_[i] = 1;
|
|
||||||
for (size_t i =0; i<rank; ++i) dims[i] = lens_[i];
|
|
||||||
mydomain = domain_type (dims);
|
|
||||||
}
|
|
||||||
|
|
||||||
index_system (v_type const & dims_, v_type_full const & total_lengths_, v_type_full const & lengths_,
|
|
||||||
v_type_full const & strides_, v_type_full const & offset_ ) {
|
|
||||||
dims = dims_;total_lens_ = total_lengths_; lens_= lengths_; stri_ = strides_; off_=offset_;
|
|
||||||
mydomain = domain_type (dims);
|
|
||||||
}
|
|
||||||
|
|
||||||
index_system (DataSpace const & ds, bool is_complex) {
|
|
||||||
int rf = ds.getSimpleExtentNdims();
|
|
||||||
if ( rf != rank_full + (is_complex ? 1 : 0) ) TRIQS_RUNTIME_ERROR << "H5 : dimension error";
|
|
||||||
//int ndims = ds.getSimpleExtentDims( &lens_[0], NULL);
|
|
||||||
ds.getSimpleExtentDims( &lens_[0], NULL);
|
|
||||||
for (size_t i =0; i<rank; ++i) { dims[i] = lens_[i]; stri_[i] = 1; off_[i]= 0; }
|
|
||||||
total_lens_=dims;
|
|
||||||
mydomain = domain_type (dims);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<bool C> DataSpace dataspace() const { return h5::dataspace_from_LS<rank_full,C> (total_lens_, lens_,stri_,off_); }
|
|
||||||
size_t size() const { size_t _size = 1; for (size_t i =0; i<rank; ++i) _size *= lens_[i]; return _size;}
|
|
||||||
mini_vector<size_t, rank_full> total_lengths() const { return this->total_lens_;}
|
|
||||||
mini_vector<size_t, rank_full> lengths() const { return this->lens_;}
|
|
||||||
mini_vector<size_t, rank_full> strides() const { return this->stri_;}
|
|
||||||
|
|
||||||
private:
|
|
||||||
mini_vector<hsize_t,rank_full> lens_, off_, stri_;
|
|
||||||
v_type dims;
|
|
||||||
v_type_full total_lens_;
|
|
||||||
domain_type mydomain;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace slicer_impl {
|
|
||||||
|
|
||||||
template <bool BC> inline void _check_BC ( int N, int ind, size_t B) { }
|
|
||||||
template <> inline void _check_BC<true> (int N, int ind, size_t B) {
|
|
||||||
bool cond = (ind >= 0) && (ind < int(B)); // fix this int ...
|
|
||||||
if (!cond) TRIQS_ARRAYS_KEY_ERROR << " index "<<N<<" is out of domain: \n " << ind <<" is not within [0,"<< B <<"[\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
template<int Rank_in, int Rank_out, int N,int P, int c, bool BoundCheck> struct slice_calc {
|
|
||||||
|
|
||||||
typedef mini_vector<size_t,Rank_in> const & i_type;
|
|
||||||
typedef mini_vector<size_t,Rank_in > & o_type;
|
|
||||||
typedef mini_vector<size_t,Rank_out > & os_type;
|
|
||||||
|
|
||||||
template< typename ArgsTuple>
|
|
||||||
static void invoke(i_type li, i_type si, os_type lo_c, o_type lo, o_type so, o_type offset, ArgsTuple const & args ) {
|
|
||||||
const int dP = boost::is_base_of<typename boost::tuples::element<N,ArgsTuple>::type, range >::type::value ;
|
|
||||||
one_step(li[N], si[N],lo[N],so[N], offset[N] ,boost::tuples::get<N>(args));
|
|
||||||
lo_c[P] = lo[N];
|
|
||||||
slice_calc<Rank_in,Rank_out,N+1,P+dP,c-1, BoundCheck>::invoke(li,si,lo_c,lo,so, offset, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void one_step(size_t li, size_t si, size_t & lo, size_t & so, size_t & offset, size_t R){
|
|
||||||
_check_BC <BoundCheck> (N, R, li);
|
|
||||||
offset = R; lo =1; so= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void one_step(size_t li, size_t si, size_t & lo, size_t & so, size_t & offset, range R){
|
|
||||||
_check_BC <BoundCheck> (N, R.first(),li);
|
|
||||||
_check_BC <BoundCheck> (N, (R.last()==-1 ? li : R.last()) -1 ,li);
|
|
||||||
lo = ((R.last()==-1 ? li : R.last()) - R.first() + R.step()-1 )/R.step() ; // python behaviour
|
|
||||||
so = R.step(); offset = R.first() ;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// stop the recursion
|
|
||||||
template<int Ri, int Ro, int N, int P, bool BC> struct slice_calc <Ri,Ro,N,P,0,BC> : slice_calc<Ri,Ro,N,P,1,BC> {
|
|
||||||
template<class T1,class T2,class T3,class T4,class T5,class T6, class T7> static void invoke(T1,T2,T3,T4,T5,T6,T7 ) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
}//namespace slicer_impl
|
|
||||||
} // h5
|
|
||||||
|
|
||||||
namespace indexmaps {
|
|
||||||
|
|
||||||
template<int R, int Rf, typename ArgsTuple>
|
|
||||||
struct slicer < h5::index_system<R,Rf>, ArgsTuple> {
|
|
||||||
|
|
||||||
static const unsigned int len = boost::tuples::length<ArgsTuple>::value;
|
|
||||||
static_assert(len>=R, "Too few arguments in slice");
|
|
||||||
static_assert(len<=R, "Too many arguments in slice");
|
|
||||||
static_assert( (R==Rf), "Can not slice array_proxy twice (not implemented)");
|
|
||||||
|
|
||||||
static const unsigned int R2 = R - TupleTools::CountHowManyInt<ArgsTuple>::value;
|
|
||||||
typedef h5::index_system< R2, Rf> return_type;
|
|
||||||
|
|
||||||
static return_type invoke ( h5::index_system<R,R> const & X, ArgsTuple args) {
|
|
||||||
mini_vector<size_t, R2> newdims;
|
|
||||||
mini_vector<size_t,R> newoffset, newlengths, newstrides;
|
|
||||||
h5::slicer_impl::slice_calc<R,R2,0,0,R,true>::invoke(X.lengths(),X.strides(),newdims, newlengths,newstrides, newoffset, args);
|
|
||||||
#ifdef TRIQS_ARRAYS_DEBUG_H5_SLICE
|
|
||||||
std::cerr<<"-----------------------------------------------"<<std::endl;
|
|
||||||
std::cerr<<"H5 Slicing "<< X.lengths().to_string()<<X.strides().to_string()<<newlengths.to_string()<<newstrides.to_string()<< newoffset.to_string() << args<<std::endl;
|
|
||||||
#endif
|
|
||||||
return return_type(newdims,X.total_lengths(), newlengths,newstrides,newoffset);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
template<int R, int Rf> struct slicer < h5::index_system<R,Rf>, boost::tuple<> > { typedef h5::index_system< R,Rf> return_type;};
|
|
||||||
}
|
|
||||||
|
|
||||||
}}
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,230 +0,0 @@
|
|||||||
/*******************************************************************************
|
|
||||||
*
|
|
||||||
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
||||||
*
|
|
||||||
* Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
******************************************************************************/
|
|
||||||
#ifndef TRIQS_ARRAYS_H5_COMMON_H
|
|
||||||
#define TRIQS_ARRAYS_H5_COMMON_H
|
|
||||||
|
|
||||||
#include <H5Cpp.h>
|
|
||||||
#include "hdf5_hl.h"
|
|
||||||
#include "../cache.hpp"
|
|
||||||
#include <boost/type_traits/is_complex.hpp>
|
|
||||||
#include <boost/utility/enable_if.hpp>
|
|
||||||
|
|
||||||
template<typename T> std::string get_triqs_hdf5_data_scheme(T const & ) { return "";}
|
|
||||||
|
|
||||||
namespace triqs { namespace arrays { namespace h5 {
|
|
||||||
using namespace H5;
|
|
||||||
// conversion of C type to HDF5 native
|
|
||||||
inline PredType native_type_from_C(char) { return PredType::NATIVE_CHAR; }
|
|
||||||
inline PredType native_type_from_C(signed char) { return PredType::NATIVE_SCHAR; }
|
|
||||||
inline PredType native_type_from_C(unsigned char) { return PredType::NATIVE_UCHAR; }
|
|
||||||
inline PredType native_type_from_C(short) { return PredType::NATIVE_SHORT; }
|
|
||||||
inline PredType native_type_from_C(unsigned short) { return PredType::NATIVE_USHORT; }
|
|
||||||
inline PredType native_type_from_C(int) { return PredType::NATIVE_INT; }
|
|
||||||
inline PredType native_type_from_C(unsigned) { return PredType::NATIVE_UINT; }
|
|
||||||
inline PredType native_type_from_C(long) { return PredType::NATIVE_LONG; }
|
|
||||||
inline PredType native_type_from_C(unsigned long) { return PredType::NATIVE_ULONG; }
|
|
||||||
inline PredType native_type_from_C(long long) { return PredType::NATIVE_LLONG; }
|
|
||||||
inline PredType native_type_from_C(unsigned long long) { return PredType::NATIVE_ULLONG; }
|
|
||||||
inline PredType native_type_from_C(float) { return PredType::NATIVE_FLOAT; }
|
|
||||||
inline PredType native_type_from_C(double) { return PredType::NATIVE_DOUBLE; }
|
|
||||||
inline PredType native_type_from_C(long double) { return PredType::NATIVE_LDOUBLE; }
|
|
||||||
inline PredType native_type_from_C(bool) { return PredType::NATIVE_SCHAR; }
|
|
||||||
inline PredType native_type_from_C(std::string) { return 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 PredType h5_type_from_C(char) { return PredType::NATIVE_CHAR; }
|
|
||||||
inline PredType h5_type_from_C(signed char) { return PredType::NATIVE_SCHAR; }
|
|
||||||
inline PredType h5_type_from_C(unsigned char) { return PredType::NATIVE_UCHAR; }
|
|
||||||
inline PredType h5_type_from_C(short) { return PredType::NATIVE_SHORT; }
|
|
||||||
inline PredType h5_type_from_C(unsigned short) { return PredType::NATIVE_USHORT; }
|
|
||||||
inline PredType h5_type_from_C(int) { return PredType::NATIVE_INT; }
|
|
||||||
inline PredType h5_type_from_C(unsigned) { return PredType::NATIVE_UINT; }
|
|
||||||
inline PredType h5_type_from_C(long) { return PredType::NATIVE_LONG; }
|
|
||||||
inline PredType h5_type_from_C(unsigned long) { return PredType::NATIVE_ULONG; }
|
|
||||||
inline PredType h5_type_from_C(long long) { return PredType::NATIVE_LLONG; }
|
|
||||||
inline PredType h5_type_from_C(unsigned long long) { return PredType::NATIVE_ULLONG; }
|
|
||||||
inline PredType h5_type_from_C(float) { return PredType::NATIVE_FLOAT; }
|
|
||||||
inline PredType h5_type_from_C(double) { return PredType::NATIVE_DOUBLE; }
|
|
||||||
inline PredType h5_type_from_C(long double) { return PredType::NATIVE_LDOUBLE; }
|
|
||||||
inline PredType h5_type_from_C(bool) { return PredType::NATIVE_SCHAR; }
|
|
||||||
inline PredType h5_type_from_C(std::string) { return PredType::C_S1; }
|
|
||||||
|
|
||||||
// If it is complex<T> return T else T
|
|
||||||
template<typename T> struct remove_complex { typedef T type;};
|
|
||||||
template<typename T> struct remove_complex<std::complex<T> > { typedef T type;};
|
|
||||||
|
|
||||||
template <typename ArrayType >
|
|
||||||
PredType data_type_mem ( ArrayType const & A) {
|
|
||||||
return native_type_from_C(typename remove_complex<typename ArrayType::value_type>::type());
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename S >
|
|
||||||
PredType data_type_mem_scalar ( S const & A) {
|
|
||||||
return native_type_from_C(typename remove_complex<S>::type());
|
|
||||||
}
|
|
||||||
|
|
||||||
// the type of data to put in the file_or_group
|
|
||||||
template <typename V >
|
|
||||||
PredType data_type_file ( V const & ) { return h5_type_from_C(typename remove_complex<V>::type());}
|
|
||||||
|
|
||||||
// the pointer on the start of data
|
|
||||||
template <typename ArrayType >
|
|
||||||
TYPE_DISABLE_IF ( void *, boost::is_complex<typename ArrayType::value_type> )
|
|
||||||
data ( ArrayType const & A) { return &(A.storage()[0]);}
|
|
||||||
|
|
||||||
template <typename ArrayType >
|
|
||||||
TYPE_ENABLE_IF ( void *, boost::is_complex<typename ArrayType::value_type> )
|
|
||||||
data ( ArrayType const & A) {
|
|
||||||
typedef typename ArrayType::value_type::value_type T;
|
|
||||||
std::complex<T> * p = &(A.storage()[0]);
|
|
||||||
return reinterpret_cast<T*>(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// dataspace from lengths and strides. Correct for the complex. strides must be >0
|
|
||||||
template<int R, bool IsComplex>
|
|
||||||
H5::DataSpace dataspace_from_LS (
|
|
||||||
mini_vector<hsize_t, R> const & Ltot,
|
|
||||||
mini_vector<hsize_t, R> const & L,
|
|
||||||
mini_vector<hsize_t, R> const & S,
|
|
||||||
mini_vector<hsize_t, R> const & offset = mini_vector<hsize_t,R>() ) {
|
|
||||||
#ifdef TRIQS_ARRAYS_DEBUG_H5_SLICE
|
|
||||||
std::cerr << "total lens in dataspace_from_LS"<< Ltot.to_string() << std::endl ;
|
|
||||||
std::cerr << "lens in dataspace_from_LS"<< L.to_string() << std::endl ;
|
|
||||||
std::cerr << "Strides in dataspace_from_LS "<< S.to_string() << std::endl ;
|
|
||||||
std::cerr << "offset in dataspace_from_LS"<< offset.to_string() << std::endl ;
|
|
||||||
#endif
|
|
||||||
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; u<R ; ++u) { offsetf[u] = offset[u]; dimsf[u] = L[u]; totdimsf[u] = Ltot[u]; stridesf[u] = S[u]; }
|
|
||||||
if (IsComplex) { offsetf[rank-1]=0; dimsf[rank-1]=2; totdimsf[rank-1] = 2; stridesf[rank-1]=1; }
|
|
||||||
DataSpace ds ( rank, totdimsf );
|
|
||||||
ds.selectHyperslab (H5S_SELECT_SET , dimsf, offsetf, stridesf);
|
|
||||||
return ds;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the dataspace corresponding to the array. Contiguous data only...
|
|
||||||
template <typename ArrayType >
|
|
||||||
H5::DataSpace data_space ( ArrayType const & A) {
|
|
||||||
static const unsigned int R = ArrayType::rank;
|
|
||||||
mini_vector<hsize_t,R> S;
|
|
||||||
mini_vector<std::ptrdiff_t,R> const & S1 ( A.indexmap().strides() );
|
|
||||||
for (size_t u=0; u<R ; ++u) {
|
|
||||||
if (S1[u]<=0) TRIQS_RUNTIME_ERROR<<" negative strides not permitted in h5";
|
|
||||||
S[u] =1;
|
|
||||||
}
|
|
||||||
if (!A.indexmap().is_contiguous()) TRIQS_RUNTIME_ERROR<<" h5 : internal error : array not contiguous";
|
|
||||||
static const bool is_complex = boost::is_complex<typename ArrayType::value_type>::value;
|
|
||||||
return dataspace_from_LS<R,is_complex > ( A.indexmap().domain().lengths(),A.indexmap().domain().lengths(), S);
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************** resize or check the size ****************************************************/
|
|
||||||
|
|
||||||
template <typename A> ENABLE_IF(is_amv_value_class<A>)
|
|
||||||
resize_or_check ( A & a, mini_vector<size_t,A::rank> const & dimsf ) { a.resize( indexmaps::cuboid::domain_t<A::rank>( dimsf)); }
|
|
||||||
|
|
||||||
template <typename A> ENABLE_IF(is_amv_view_class<A>)
|
|
||||||
resize_or_check ( A const & a, mini_vector<size_t,A::rank> 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 : "<<a.indexmap().domain().lengths().to_string() ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define TRIQS_ARRAYS_H5_CATCH_EXCEPTION \
|
|
||||||
catch( triqs::runtime_error error) { throw triqs::runtime_error() << error.what();}\
|
|
||||||
catch( H5::FileIException error ) { error.printError(); TRIQS_RUNTIME_ERROR<<"H5 File error"; }\
|
|
||||||
catch( H5::DataSetIException error ) { error.printError(); TRIQS_RUNTIME_ERROR<<"H5 DataSet error"; }\
|
|
||||||
catch( H5::DataSpaceIException error ) { error.printError(); TRIQS_RUNTIME_ERROR<<"H5 DataSpace error"; }\
|
|
||||||
catch( H5::DataTypeIException error ) { error.printError(); TRIQS_RUNTIME_ERROR<<"H5 DataType error"; }\
|
|
||||||
catch( H5::AttributeIException error ) { error.printError(); TRIQS_RUNTIME_ERROR<<"H5 Attribute error"; }\
|
|
||||||
catch(...) { TRIQS_RUNTIME_ERROR<<"H5 unknown error";}
|
|
||||||
|
|
||||||
/****************** WRITE attribute *********************************************/
|
|
||||||
|
|
||||||
/*inline void write_attribute2 ( H5::H5Object const & grp, std::string obj_name, std::string attr_name, std::string value ) {
|
|
||||||
herr_t err = H5LTset_attribute_string(grp.getId(),obj_name.c_str(),attr_name.c_str(), , value.c_str() ) ;
|
|
||||||
if (err<0) TRIQS_RUNTIME_ERROR << "Error in setting attribute "<< name << " to " << value;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
/****************** Write string attribute *********************************************/
|
|
||||||
|
|
||||||
inline void write_string_attribute ( H5::H5Object const * obj, std::string name, std::string value ) {
|
|
||||||
DataSpace attr_dataspace = DataSpace(H5S_SCALAR);
|
|
||||||
// Create new string datatype for attribute
|
|
||||||
StrType strdatatype(PredType::C_S1, value.size());
|
|
||||||
// Set up write buffer for attribute
|
|
||||||
//const H5std_string strwritebuf (value);
|
|
||||||
// Create attribute and write to it
|
|
||||||
Attribute myatt_in = obj->createAttribute(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 ="";
|
|
||||||
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()) ) ;
|
|
||||||
//if (err2 < 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0";
|
|
||||||
//value.append( &(buf.front()) );
|
|
||||||
try { attr= obj->openAttribute(name.c_str());}
|
|
||||||
catch (H5::AttributeIException) { return value;}
|
|
||||||
try {
|
|
||||||
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();
|
|
||||||
StrType strdatatype(PredType::C_S1, size);
|
|
||||||
std::vector<char> buf(size+1, 0x00);
|
|
||||||
attr.read(strdatatype, (void *)(&buf[0]));
|
|
||||||
value.append( &(buf.front()) );
|
|
||||||
}
|
|
||||||
TRIQS_ARRAYS_H5_CATCH_EXCEPTION;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************** Read/Write the special TRIQS_HDF5_data_scheme attribute *********************************************/
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
inline void write_triqs_hdf5_data_scheme( H5::Group g, T const & obj) {
|
|
||||||
write_string_attribute( &g, "TRIQS_HDF5_data_scheme" , get_triqs_hdf5_data_scheme(obj).c_str() ) ;
|
|
||||||
// herr_t err = H5LTset_attribute_string(F.getId(), subgroup_name.c_str(), "TRIQS_HDF5_data_scheme" , get_triqs_hdf5_data_scheme(p).c_str() ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::string read_triqs_hdf5_data_scheme( H5::Group g) { return read_string_attribute( &g, "TRIQS_HDF5_data_scheme") ; }
|
|
||||||
|
|
||||||
|
|
||||||
/****************** WRITE attribute *********************************************/
|
|
||||||
|
|
||||||
//inline void write_triqs_data_scheme ( H5::H5Object const & grp, std::string obj_name, std::string attr_name, std::string triqs_data_scheme ) {
|
|
||||||
// herr_t err = H5LTset_attribute_string(grp.getId(),obj_name.c_str(),attr_name.c_str(), , value.c_str() ) ;
|
|
||||||
// if (err<0) TRIQS_RUNTIME_ERROR << "Error in setting attribute "<< name << " to " << value;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}}}
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue
Block a user