int main(int argc, char **argv) { try { // write std::map<std::string, int> m = { {"a",1}, {"b",2} }; std::map<std::string, std::vector<double>> mv = { {"a",{1.0, 2.0}}, {"b",{2.0, 3.0, 4.0}} }; { h5::file file1("test_map.h5", H5F_ACC_TRUNC); h5::group top1(file1); h5_write(top1, "map_int", m); h5_write(top1, "map_vec", mv); } // read std::map<std::string, int> mm = { {"c",1} }; std::map<std::string, std::vector<double>> mmv = { {"c",{1.0}} }; h5::file file2("test_map.h5", H5F_ACC_RDONLY); h5::group top2(file2); h5_read(top2, "map_int", mm); h5_read(top2, "map_vec", mmv); for (auto const & val: mm) std::cout << val.first << " " << val.second << std::endl; for (auto const & val: mmv) { std::cout << val.first << std::endl; for (auto const & x: val.second) std::cout << x << std::endl; } } TRIQS_CATCH_AND_ABORT; }
// -------------- HDF5 -------------------------- /// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, linear_mesh const &m) { h5::group gr = fg.create_group(subgroup_name); h5_write(gr, "domain", m.domain()); h5_write(gr, "min", m.xmin); h5_write(gr, "max", m.xmax); h5_write(gr, "size", long(m.size())); }
static void write(h5::group gr, gf_const_view<M, T, S, E> g) { gf_h5_write_data<M, T, S, E>::invoke(gr, g); // h5_write(gr, "data", g._data); h5_write(gr, "singularity", g._singularity); h5_write(gr, "mesh", g._mesh); h5_write(gr, "symmetry", g._symmetry); h5_write(gr, "indices", g._indices); }
static void write(h5::group gr, gf_const_view<block_index2, Target> g) { auto const &m0 = std::get<0>(g.mesh()); auto const &m1 = std::get<1>(g.mesh()); for (int i = 0; i < m0.size(); ++i) for (int j = 0; j < m1.size(); ++j) h5_write(gr, m0.domain().names()[i] + "_" + m1.domain().names()[j], g._data[i][j]); h5_write(gr, "block_names", m0.domain().names()); }
std::c14::enable_if_t<is_amv_value_or_view_class<ArrayType>::value && !has_scalar_or_string_value_type<ArrayType>::value> h5_write(h5::group gr, std::string name, ArrayType const& a) { if (a.is_empty()) TRIQS_RUNTIME_ERROR << " Cannot save an empty array into hdf5"; auto gr2 = gr.create_group(name); gr2.write_triqs_hdf5_data_scheme(a); // save the shape array<int, 1> sha(ArrayType::rank); for (int u = 0; u < ArrayType::rank; ++u) sha(u) = a.shape()[u]; h5_write(gr2, "shape", sha); #ifndef __cpp_generic_lambdas foreach(a, h5_impl::_save_lambda<ArrayType>{a, gr2}); #else foreach(a, [&](auto... is) { h5_write(gr2, h5_impl::_h5_name(is...), a(is...)); }); #endif }
/// HDF5 interface friend void h5_write (h5::group g, std::string const & name, mc_generic const & mc){ auto gr = g.create_group(name); h5_write(gr,"moves", mc.AllMoves); h5_write(gr,"measures", mc.AllMeasures); h5_write(gr,"length_monte_carlo_cycle", mc.Length_MC_Cycle); h5_write(gr,"number_cycle_requested", mc.NCycles); h5_write(gr,"number_warming_cycle_requested", mc.NWarmIterations); h5_write(gr,"number_cycle_done", mc.NC); h5_write(gr,"number_measure_done", mc.nmeasures); h5_write(gr,"sign", mc.sign); h5_write(gr,"sum_sign", mc.sum_sign); }
//------------------------------------------------------------ // MAIN //------------------------------------------------------------ int main(int argc, char* argv[]) { // initialize mpi boost::mpi::environment env(argc, argv); boost::mpi::communicator world; // greeting if (world.rank() == 0) std::cout << "Random walk calculation" << std::endl; // prepare the MC parameters int N_Cycles = 100000; int Length_Cycle = 100; int N_Warmup_Cycles = 0; std::string Random_Name = ""; int Random_Seed = 374982 + world.rank() * 273894; int Verbosity = (world.rank() == 0 ? 3 : 0); int xmax=floor(4*sqrt(Length_Cycle) ); //max of the position registered in the histogram double pl=1.5, pr=1; //non normalized probabilities for proposing a left or right move h5::file file("params.h5",H5F_ACC_TRUNC); h5_write(file,"pr",make_array(pr)); h5_write(file,"pl",make_array(pl)); h5_write(file,"xmax",make_array(xmax)); h5_write(file,"N_Cycles",make_array(N_Cycles)); h5_write(file,"Length_Cycle",make_array(Length_Cycle)); // construct a Monte Carlo loop triqs::mc_tools::mc_generic<double> IntMC(N_Cycles, Length_Cycle, N_Warmup_Cycles, Random_Name, Random_Seed, Verbosity); // construct configuration configuration config; triqs::arrays::array<double,1> H(2*xmax); H()=0; // add moves and measures IntMC.add_move(move_left (config, pl, pr, IntMC.rng() ), "left move", pl); IntMC.add_move(move_right(config, pl, pr, IntMC.rng() ), "right move", pr); IntMC.add_measure(compute_histo(config, H, xmax), "histogramn measure"); // Run and collect results IntMC.start(1.0, triqs::utility::clock_callback(600)); IntMC.collect_results(world); return 0; }
/// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, matsubara_freq_mesh const &m) { h5::group gr = fg.create_group(subgroup_name); h5_write(gr, "domain", m.domain()); h5_write(gr, "size", m.size()); if (m._positive_only) { // kept ONLY for backward compatibility of archives auto beta = m.domain().beta; h5_write(gr, "min", Fermion ? M_PI / beta : 0); h5_write(gr, "max", ((Fermion ? 1 : 0) + 2 * m.size()) * M_PI / beta); h5_write(gr, "kind", 2); } else { // A strange way : to preserve backward compatibility for old archive. h5_write(gr, "start_at_0", m._positive_only); } }
TEST(GfCartesian, H5_RW_EvaluatorM) { double beta = 1; auto g = gf<cartesian_product<imfreq, imfreq>, matrix_valued>{{{beta, Fermion, 5}, {beta, Boson, 5}}, {1, 1}}; g() = 2; h5::file file("g_nu_nuph5", H5F_ACC_TRUNC); h5_write(file, "g", g); gf<cartesian_product<imfreq, imfreq>, matrix_valued> g2{}; h5_read(file, "g", g2); EXPECT_ARRAY_NEAR(g.data(), g2.data()); //EXPECT_GF_NEAR(g, g2); auto w0 = matsubara_freq(0, beta, Fermion); auto W10 = matsubara_freq(10, beta, Boson); auto W0 = matsubara_freq(0, beta, Boson); EXPECT_ARRAY_NEAR(g(w0, W0), g2(w0, W0)); EXPECT_ARRAY_NEAR(g(w0, W10), g2(w0, W10)); }
void write_array(h5::group g, std::string const& name, array_const_view<std::string, 1> V) { std::vector<std::string> tmp(first_dim(V)); std::copy(begin(V), end(V), begin(tmp)); h5_write(g, name, tmp); }
/// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, gf_mesh const &m) { h5::group gr = fg.create_group(subgroup_name); auto l = [gr](int N, auto const &m) { h5_write(gr, "MeshComponent" + std::to_string(N), m); }; triqs::tuple::for_each_enumerate(m.components(), l); }
static void write(h5::group gr, gf_const_view<block_index, Target> g) { for (size_t i = 0; i < g.mesh().size(); ++i) h5_write(gr, g.mesh().domain().names()[i], g._data[i]); h5_write(gr, "block_names", g.mesh().domain().names()); }
// overload : special treatment for arrays of strings (one dimension only). void write_array(h5::group g, std::string const& name, vector_const_view<std::string> V) { std::vector<std::string> tmp(V.size()); std::copy(begin(V), end(V), begin(tmp)); h5_write(g, name, tmp); }
// given a pointer to an object, synthesize h5_read/write function for this object template<typename T> h5_rw_lambda_t make_h5_write_impl(T * p, std::true_type) { return [p](h5::group F, std::string const &Name) {h5_write(F,Name, *p);}; }
template <typename... Is> void operator()(Is const&... is) const { h5_write(g, _h5_name(is...), a(is...)); }
/// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, matsubara_domain const &d) { h5::group gr = fg.create_group(subgroup_name); h5_write(gr, "beta", d.beta); h5_write(gr, "statistic", (d.statistic == Fermion ? "F" : "B")); }
static void write(h5::group gr, gf_const_view<block_index, Target, Opt> g) { for (size_t i = 0; i < g.mesh().size(); ++i) h5_write(gr, g.mesh().domain().names()[i], g._data[i]); // h5_write(gr,"symmetry",g._symmetry); }
/// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, gf_mesh const &m) { h5::group gr = fg.create_group(subgroup_name); h5_write(gr, "domain", m.domain()); h5_write(gr, "size", long(m.size())); h5_write(gr, "positive_freq_only", (m._positive_only?1:0)); }
template <typename... T> void h5_write(h5::group gr, std::string const &name, std::variant<T...> const &v) { visit([&](auto const &x) { h5_write(gr, name, x); }, v); }
void h5_write (triqs::h5::group fg, std::string subgroup_name, block_matrix<T> const & c) { triqs::h5::group gr = fg.create_group(subgroup_name); h5_write(gr,"block_names",c.block_names); h5_write(gr,"matrix_vec",c.matrix_vec); h5_write_attribute(gr, "TRIQS_HDF5_data_scheme", get_triqs_hdf5_data_scheme(c)); }
template <typename G> static void invoke(h5::group gr, G const &g) { if (!is_gf_real(g)) h5_write(gr, "data", g.data()); else h5_write(gr, "data", array<double, 3>(real(g.data()))); }
void collect_results(boost::mpi::communicator const &c) { H/=tot; h5::file file("histo.h5",H5F_ACC_TRUNC); h5_write(file,"H",H); }
// HDF5 interface friend void h5_write (h5::group g, std::string const & name, measure_set const & ms) { auto gr = g.create_group(name); for (auto & p : ms.m_map) h5_write(gr,p.first, p.second); }
/// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, discrete_mesh const &m) { h5::group gr = fg.create_group(subgroup_name); h5_write(gr, "domain", m.domain()); }
/// Write into HDF5 friend void h5_write (tqa::h5::group_or_file fg, std::string subgroup_name, discrete_mesh const & m) { tqa::h5::group_or_file gr = fg.create_group(subgroup_name); h5_write(gr,"domain",m.domain()); }
template <typename G> static void invoke(h5::group gr, G const &g) { h5_write(gr, "data", g.data()); }
/// Write into HDF5 friend void h5_write(h5::group fg, std::string subgroup_name, gf_mesh const& m) { h5::group gr = fg.create_group(subgroup_name); h5_write(gr, "dims", m.dims.to_vector()); }
friend void h5_write(h5::group fg, std::string subgroup_name, tail_impl const &t) { auto gr = fg.create_group(subgroup_name); h5_write(gr, "omin", t.omin); h5_write(gr, "mask", t._mask); h5_write(gr, "data", t._data); }