Skip to content

Commit

Permalink
[lattice] Make tight_binding object h5 read and writeable + test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Aug 30, 2018
1 parent f287211 commit c6759f4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 28 deletions.
6 changes: 2 additions & 4 deletions pytriqs/gf/gf_fnt_desc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@

# FIXME
# New tail
m.add_function("std::pair<array<dcomplex,3>, double> fit_tail(gf_view<imfreq, matrix_valued> g)", doc = "tail")
m.add_function("std::pair<array<dcomplex,3>, double> fit_tail(gf_view<imfreq, matrix_valued> g, array_view<dcomplex,3> known_moments)", doc = "tail")
m.add_function("std::pair<array<dcomplex,1>, double> fit_tail(gf_view<imfreq, scalar_valued> g)", doc = "tail")
m.add_function("std::pair<array<dcomplex,1>, double> fit_tail(gf_view<imfreq, scalar_valued> g, array_view<dcomplex,1> known_moments)", doc = "tail")
m.add_function("std::pair<array<dcomplex,3>, double> fit_tail(gf_view<imfreq, matrix_valued> g, array_view<dcomplex,3> known_moments = {})", doc = "tail")
m.add_function("std::pair<array<dcomplex,1>, double> fit_tail(gf_view<imfreq, scalar_valued> g, array_view<dcomplex,1> known_moments = {})", doc = "tail")

# density
m.add_function("matrix<dcomplex> density(gf_view<imfreq, matrix_valued> g, array_view<dcomplex, 3> known_moments = {})", doc = "Density, as a matrix, computed from a Matsubara sum")
Expand Down
2 changes: 1 addition & 1 deletion pytriqs/lattice/lattice_tools_desc.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
tb = class_(py_type = "TightBinding",
c_type = "tight_binding",
c_type_absolute = "triqs::lattice::tight_binding",
#serializable= "tuple",
hdf5 = True
)

tb.add_constructor(signature = "(bravais_lattice latt, PyObject* hopping)",
Expand Down
58 changes: 58 additions & 0 deletions test/triqs/lattice/tight_binding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2018 by N. Wentzell
*
* 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 <triqs/test_tools/arrays.hpp>
#include <triqs/test_tools/gfs.hpp>

#include <triqs/lattice/tight_binding.hpp>

#include <vector>

using namespace triqs::gfs;
using namespace triqs::lattice;
using namespace triqs::arrays;

TEST(tight_binding, h5_read_write) {
double t = 1.0;
auto bl = bravais_lattice{{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}};
auto displ_vec = std::vector<std::vector<long>>{{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
auto overlap_mat_vec = std::vector<matrix<dcomplex>>{{{t}}, {{t}}, {{t}}, {{t}}, {{ t}}, { { t }} };

auto tb = tight_binding{bl, displ_vec, overlap_mat_vec};

// write
{
auto file = triqs::h5::file{"test_tb.h5", H5F_ACC_TRUNC};
auto grp = triqs::h5::group{file};
h5_write(grp, "cubic_lattice_nn_hop", tb);
}

// read
{
auto file = triqs::h5::file{"test_tb.h5", H5F_ACC_RDONLY};
auto grp = triqs::h5::group{file};
auto tb_in = triqs::h5::h5_read<tight_binding>(grp, "cubic_lattice_nn_hop");
//EXPECT_EQ(tb, tb_in);
}
}

MAKE_MAIN;
4 changes: 2 additions & 2 deletions triqs/h5/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace triqs {
}

group group::open_group(std::string const &key) const {
if (key.empty()) return group{h5_object::from_borrowed(id)};
if (key.empty()) return *this;
if (!has_key(key)) TRIQS_RUNTIME_ERROR << "no subgroup " << key << " in the group";
hid_t sg = H5Gopen2(id, key.c_str(), H5P_DEFAULT);
if (sg < 0) TRIQS_RUNTIME_ERROR << "Error in opening the subgroup " << key;
Expand All @@ -68,7 +68,7 @@ namespace triqs {
}

group group::create_group(std::string const &key, bool delete_if_exists) const {
if (key.empty()) return group{h5_object::from_borrowed(id)};
if (key.empty()) return *this;
unlink_key_if_exists(key);
hid_t id_g = H5Gcreate2(id, key.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if (id_g < 0) TRIQS_RUNTIME_ERROR << "Cannot create the subgroup " << key << " of the group" << name();
Expand Down
11 changes: 6 additions & 5 deletions triqs/lattice/gf_mesh_brillouin_zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,19 @@ namespace triqs {
friend void h5_write(h5::group fg, std::string const &subgroup_name, gf_mesh const &m) {
h5_write_impl(fg, subgroup_name, m, "MeshBrillouinZone");
h5::group gr = fg.open_group(subgroup_name);
h5_write(gr, "bz", m.bz);
h5_write(gr, "brillouin_zone", m.bz);
}

friend void h5_read(h5::group fg, std::string const &subgroup_name, gf_mesh &m) {
h5_read_impl(fg, subgroup_name, m, "MeshBrillouinZone");
h5::group gr = fg.open_group(subgroup_name);
try{ // Care for Backward Compatibility
try { // Care for Backward Compatibility
h5_read(gr, "bz", m.bz);
} catch(triqs::runtime_error const & re){}
try{
return;
} catch (triqs::runtime_error const & re){}
try {
h5_read(gr, "brillouin_zone", m.bz);
} catch(triqs::runtime_error const & re){}
} catch (triqs::runtime_error const & re){}
}
};
} // namespace gfs
Expand Down
2 changes: 1 addition & 1 deletion triqs/lattice/gf_mesh_cyclic_lattice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace triqs {
friend void h5_write(h5::group fg, std::string const &subgroup_name, gf_mesh const &m) {
h5_write_impl(fg, subgroup_name, m, "MeshCyclicLattice");
h5::group gr = fg.open_group(subgroup_name);
h5_write(gr, "bl", m.bl);
h5_write(gr, "bravais_lattice", m.bl);
}

friend void h5_read(h5::group fg, std::string const &subgroup_name, gf_mesh &m) {
Expand Down
20 changes: 10 additions & 10 deletions triqs/lattice/tight_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ namespace triqs {

using namespace arrays;

tight_binding::tight_binding(bravais_lattice const &bl, std::vector<std::vector<long>> all_disp, std::vector<matrix<dcomplex>> all_matrices)
: bl_(bl), all_disp(std::move(all_disp)), all_matrices(std::move(all_matrices)) {
tight_binding::tight_binding(bravais_lattice const &bl, std::vector<std::vector<long>> displ_vec, std::vector<matrix<dcomplex>> overlap_mat_vec)
: bl_(bl), displ_vec_(std::move(displ_vec)), overlap_mat_vec_(std::move(overlap_mat_vec)) {

// checking inputs
if (all_disp.size() != all_matrices.size()) TRIQS_RUNTIME_ERROR << " Number of displacements != Number of matrices";
for (int i = 0; i < all_disp.size(); ++i) {
if (all_disp[i].size() != bl_.dim())
TRIQS_RUNTIME_ERROR << "displacement of incorrect size : got " << all_disp[i].size() << "instead of " << bl_.dim();
if (first_dim(all_matrices[i]) != n_bands())
TRIQS_RUNTIME_ERROR << "the first dim matrix is of size " << first_dim(all_matrices[i]) << " instead of " << n_bands();
if (second_dim(all_matrices[i]) != n_bands())
TRIQS_RUNTIME_ERROR << "the second dim matrix is of size " << second_dim(all_matrices[i]) << " instead of " << n_bands();
if (displ_vec_.size() != overlap_mat_vec_.size()) TRIQS_RUNTIME_ERROR << " Number of displacements != Number of matrices";
for (int i = 0; i < displ_vec_.size(); ++i) {
if (displ_vec_[i].size() != bl_.dim())
TRIQS_RUNTIME_ERROR << "displacement of incorrect size : got " << displ_vec_[i].size() << "instead of " << bl_.dim();
if (first_dim(overlap_mat_vec_[i]) != n_bands())
TRIQS_RUNTIME_ERROR << "the first dim matrix is of size " << first_dim(overlap_mat_vec_[i]) << " instead of " << n_bands();
if (second_dim(overlap_mat_vec_[i]) != n_bands())
TRIQS_RUNTIME_ERROR << "the second dim matrix is of size " << second_dim(overlap_mat_vec_[i]) << " instead of " << n_bands();
}
}

Expand Down
33 changes: 28 additions & 5 deletions triqs/lattice/tight_binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once
#include "brillouin_zone.hpp"
#include <triqs/utility/complex_ops.hpp>
#include <triqs/h5.hpp>

namespace triqs {
namespace lattice {
Expand All @@ -32,12 +33,12 @@ namespace triqs {
class tight_binding {

bravais_lattice bl_;
std::vector<std::vector<long>> all_disp;
std::vector<matrix<dcomplex>> all_matrices;
std::vector<std::vector<long>> displ_vec_;
std::vector<matrix<dcomplex>> overlap_mat_vec_;

public:
///
tight_binding(bravais_lattice const &bl, std::vector<std::vector<long>> all_disp, std::vector<matrix<dcomplex>> all_matrices);
tight_binding(bravais_lattice const &bl, std::vector<std::vector<long>> displ_vec, std::vector<matrix<dcomplex>> overlap_mat_vec);

/// Underlying lattice
bravais_lattice const &lattice() const { return bl_; }
Expand All @@ -47,8 +48,8 @@ namespace triqs {

// calls F(R, t(R)) for all R
template <typename F> friend void foreach (tight_binding const &tb, F f) {
int n = tb.all_disp.size();
for (int i = 0; i < n; ++i) f(tb.all_disp[i], tb.all_matrices[i]);
int n = tb.displ_vec_.size();
for (int i = 0; i < n; ++i) f(tb.displ_vec_[i], tb.overlap_mat_vec_[i]);
}

// a simple function on the domain brillouin_zone
Expand Down Expand Up @@ -78,6 +79,28 @@ namespace triqs {

fourier_impl friend fourier(tight_binding const &tb) { return {tb, tb.n_bands()}; }

// HDF5 Read / Write
static std::string hdf5_scheme() { return "tight_binding"; }

// Function that writes the solver_core to hdf5 file
friend void h5_write(triqs::h5::group g, std::string subgroup_name, tight_binding const &tb) {
auto grp = g.create_group(subgroup_name);
h5_write_attribute(grp, "TRIQS_HDF5_data_scheme", tight_binding::hdf5_scheme());
h5_write(grp, "bravais_lattice", tb.bl_);
h5_write(grp, "displ_vec", tb.displ_vec_);
h5_write(grp, "overlap_mat_vec", tb.overlap_mat_vec_);
}

// Function to read tight_binding object from hdf5 file
CPP2PY_IGNORE
static tight_binding h5_read_construct(triqs::h5::group g, std::string subgroup_name) {
auto grp = g.open_group(subgroup_name);
auto bl = triqs::h5::h5_read<bravais_lattice>(grp, "bravais_lattice");
auto displ_vec = triqs::h5::h5_read<std::vector<std::vector<long>>>(grp, "displ_vec");
auto overlap_mat_vec = triqs::h5::h5_read<std::vector<matrix<dcomplex>>>(grp, "overlap_mat_vec");
return tight_binding(bl, displ_vec, overlap_mat_vec);
}

}; // tight_binding

/**
Expand Down

0 comments on commit c6759f4

Please sign in to comment.