Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
iskakoff committed Feb 23, 2024
1 parent 3ac43bc commit 5d4da25
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "test/*"
4 changes: 2 additions & 2 deletions c++/green/grids/except.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace green::grids {
class grids_type_mismatch_error : public std::runtime_error {
public:
grids_type_mismatch_error(const std::string& string) : runtime_error(string) {}
explicit grids_type_mismatch_error(const std::string& string) : runtime_error(string) {}
};

class grids_file_not_found_error : public std::runtime_error {
public:
grids_file_not_found_error(const std::string& string) : runtime_error(string) {}
explicit grids_file_not_found_error(const std::string& string) : runtime_error(string) {}
};
} // namespace green::grids

Expand Down
50 changes: 25 additions & 25 deletions c++/green/grids/transformer_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace green::grids {
* Transformation between Fermionic and Bosonic grids.
* @param eta - [INPUT] starting statistics
*/
void fermi_boson_trans(const ztensor<4>& F_t_before, ztensor<4>& F_t_after, int eta);
void fermi_boson_trans(const ztensor<4>& F_t_before, ztensor<4>& F_t_after, int eta) const;

/**
* Transform the tensor represented in fermionic imaginary time into bosonic Matsubara frequency through the intermediate
Expand Down Expand Up @@ -227,7 +227,7 @@ namespace green::grids {
void tau_to_chebyshev(const ztensor<N>& F_t, ztensor<N>& F_c, int eta = 1) const {
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());
// size_t dim1 = F_t.shape()[1]*F_t.shape()[2]*F_t.shape()[3]*F_t.shape()[4];
// Chebyshev tensor as rectangular matrix
MMatrixXcd f_c(F_c.data(), F_c.shape()[0], dim1);
Expand All @@ -248,7 +248,7 @@ namespace green::grids {
assert(ic2 < ni);
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());
// size_t dim1 = F_t.shape()[1]*F_t.shape()[2]*F_t.shape()[3]*F_t.shape()[4];
// Chebyshev tensor as rectangular matrix
MMatrixXcd f_c(F_c.data(), F_c.shape()[0], dim1);
Expand All @@ -266,7 +266,7 @@ namespace green::grids {
assert(ic < ni);
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());
// size_t dim1 = F_t.shape()[1]*F_t.shape()[2]*F_t.shape()[3]*F_t.shape()[4];
// Chebyshev tensor as rectangular matrix
MMatrixXcd f_c(F_c.data(), F_c.shape()[0], dim1);
Expand All @@ -282,7 +282,7 @@ namespace green::grids {
void tau_to_omega(const ztensor<N>& F_t, ztensor<N>& F_w, int eta = 1) const {
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());
// size_t dim1 = F_t.shape()[1]*F_t.shape()[2]*F_t.shape()[3]*F_t.shape()[4];
// Chebyshev tensor as rectangular matrix
MMatrixXcd f_w(F_w.data(), F_w.shape()[0], dim1);
Expand All @@ -299,7 +299,7 @@ namespace green::grids {
void tau_to_omega_w(const ztensor<N>& F_t, ztensor<N>& F_w, size_t w, int eta = 1) const {
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());

size_t n = (eta == 1 ? _Tnt : _Tnt_B).rows();
size_t m = (eta == 1 ? _Tnt : _Tnt_B).cols();
Expand All @@ -316,8 +316,8 @@ namespace green::grids {
int eta = 1) const {
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim2 = std::accumulate(F_t.shape().begin() + 2, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());
size_t dim2 = std::accumulate(F_t.shape().begin() + 2, F_t.shape().end(), 1ul, std::multiplies<>());

size_t n = (eta == 1 ? _Tnt : _Tnt_B).rows();
size_t m = (eta == 1 ? _Tnt : _Tnt_B).cols();
Expand All @@ -334,9 +334,9 @@ namespace green::grids {
size_t k, int eta = 1) const {
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim2 = std::accumulate(F_t.shape().begin() + 2, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim3 = std::accumulate(F_t.shape().begin() + 3, F_t.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_t.shape().begin() + 1, F_t.shape().end(), 1ul, std::multiplies<>());
size_t dim2 = std::accumulate(F_t.shape().begin() + 2, F_t.shape().end(), 1ul, std::multiplies<>());
size_t dim3 = std::accumulate(F_t.shape().begin() + 3, F_t.shape().end(), 1ul, std::multiplies<>());

size_t n = (eta == 1 ? _Tnt : _Tnt_B).rows();
size_t m = (eta == 1 ? _Tnt : _Tnt_B).cols();
Expand All @@ -353,7 +353,7 @@ namespace green::grids {
void omega_to_tau(const ztensor<N>& F_w, ztensor<N>& F_t, int eta = 1) const {
// Calculate coefficients in Chebyshev nodes
// Dimension of the rest of arrays
size_t dim1 = std::accumulate(F_w.shape().begin() + 1, F_w.shape().end(), 1ul, std::multiplies<size_t>());
size_t dim1 = std::accumulate(F_w.shape().begin() + 1, F_w.shape().end(), 1ul, std::multiplies<>());
// size_t dim1 = F_t.shape()[1]*F_t.shape()[2]*F_t.shape()[3]*F_t.shape()[4];
// Chebyshev tensor as rectangular matrix
MMatrixXcd f_t(F_t.data(), F_t.shape()[0], dim1);
Expand Down Expand Up @@ -419,31 +419,31 @@ namespace green::grids {
* Getters
*/
public:
const MatrixXcd& Ttn_FB() const { return _Ttn_FB; }
[[nodiscard]] const MatrixXcd& Ttn_FB() const { return _Ttn_FB; }

const MatrixXcd& Tnt_BF() const { return _Tnt_BF; }
[[nodiscard]] const MatrixXcd& Tnt_BF() const { return _Tnt_BF; }

const MatrixXcd& Tnc() const { return _Tnc; }
[[nodiscard]] const MatrixXcd& Tnc() const { return _Tnc; }

const MatrixXcd& Tcn() const { return _Tcn; }
[[nodiscard]] const MatrixXcd& Tcn() const { return _Tcn; }

const MatrixXcd& Tnc_B() const { return _Tnc_B; }
[[nodiscard]] const MatrixXcd& Tnc_B() const { return _Tnc_B; }

const MatrixXd& Ttc() const { return _Ttc; }
[[nodiscard]] const MatrixXd& Ttc() const { return _Ttc; }

const MatrixXd& Ttc_B() const { return _Ttc_B; }
[[nodiscard]] const MatrixXd& Ttc_B() const { return _Ttc_B; }

const MatrixXd& Tct() const { return _Tct; }
[[nodiscard]] const MatrixXd& Tct() const { return _Tct; }

const MatrixXcd& Tnt() const { return _Tnt; }
[[nodiscard]] const MatrixXcd& Tnt() const { return _Tnt; }

const MatrixXcd& Ttn() const { return _Ttn; }
[[nodiscard]] const MatrixXcd& Ttn() const { return _Ttn; }

const MatrixXcd& Tnt_B() const { return _Tnt_B; }
[[nodiscard]] const MatrixXcd& Tnt_B() const { return _Tnt_B; }

const MatrixXcd& Ttn_B() const { return _Ttn_B; }
[[nodiscard]] const MatrixXcd& Ttn_B() const { return _Ttn_B; }

const sparse_data& sd() const { return _sd; }
[[nodiscard]] const sparse_data& sd() const { return _sd; }
};
} // namespace green::grids

Expand Down
2 changes: 1 addition & 1 deletion c++/transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace green::grids {
_Ttn_FB = _Ttc_B_other * _Tcn_B;
}

void transformer_t::fermi_boson_trans(const ztensor<4>& F_t_before, ztensor<4>& F_t_after, int eta) {
void transformer_t::fermi_boson_trans(const ztensor<4>& F_t_before, ztensor<4>& F_t_after, int eta) const {
size_t dim_t = F_t_before.shape()[1] * F_t_before.shape()[2] * F_t_before.shape()[3];
size_t dim_c = F_t_after.shape()[1] * F_t_after.shape()[2] * F_t_after.shape()[3];
assert(dim_t == dim_c);
Expand Down
71 changes: 70 additions & 1 deletion test/grids_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,65 @@ void check_transformer(green::grids::transformer_t& tr) {
REQUIRE_THAT(X4w, IsCloseTo(X1w(iw, is)));
}
}

SECTION("FERMI-BOSON") {
std::transform(epsk.begin(), epsk.end(), epsk.begin(), [&i, nk](double x) { return std::cos((2.0 * M_PI * i++) / nk); });
green::grids::ztensor<4> X5w_f;
{
green::grids::ztensor<2> tmp(tr.sd().repn_fermi().nw(), nk);
for (size_t iw = 0; iw < tmp.shape()[0]; ++iw) {
tmp(iw) += (1. / (tr.sd().repn_fermi().wsample()(iw) * 1.i - epsk));
}
X5w_f = tmp.reshape(tr.sd().repn_fermi().nw(), nk, 1, 1);
}
green::grids::ztensor<4> X5t_f(tr.sd().repn_fermi().nts(), nk, 1, 1);
tr.omega_to_tau(X5w_f, X5t_f);
green::grids::ztensor<4> X5t_b(tr.sd().repn_bose().nts(), nk, 1, 1);
green::grids::ztensor<4> X6t_f(X5t_f.shape());
tr.fermi_boson_trans(X5t_f, X5t_b, 1);
tr.fermi_boson_trans(X5t_b, X6t_f, 0);
REQUIRE_THAT(X5t_f, IsCloseTo(X6t_f));
}
SECTION("Fixed Frequency Transform") {
green::grids::ztensor<5> X1w_partial(2, ns, nk, 1, 1);
tr.tau_f_to_w_b(X1t, X1w_b);
tr.tau_f_to_w_b(X1t, X1w_partial, 2, 2);
REQUIRE_THAT(X1w_b(2), IsCloseTo(X1w_partial(0)));
REQUIRE_THAT(X1w_b(3), IsCloseTo(X1w_partial(1)));
}
SECTION("To And From Basis") {
green::grids::ztensor<5> X1c(tr.sd().repn_fermi().ni(), ns, nk, 1, 1);
green::grids::ztensor<5> X1w_2(X1w.shape());
green::grids::ztensor<5> X1t_2(X1t.shape());
tr.matsubara_to_chebyshev(X1w, X1c, 1);
tr.chebyshev_to_matsubara(X1c, X1w_2, 1);
REQUIRE_THAT(X1w, IsCloseTo(X1w_2));
tr.tau_to_chebyshev(X1t, X1c, 1);
tr.chebyshev_to_tau(X1c, X1t_2, 1);
REQUIRE_THAT(X1t, IsCloseTo(X1t_2));
}
SECTION("Bose") {
green::grids::ztensor<4> W1w_b;
{
green::grids::ztensor<2> tmp(tr.sd().repn_bose().nw(), nk);
for (size_t iw = 0; iw < tmp.shape()[0]; ++iw) {
tmp(iw) += (1. / (tr.sd().repn_bose().wsample()(iw) * 1.i - epsk));
}
W1w_b = tmp.reshape(tr.sd().repn_bose().nw(), nk, 1, 1);
}
green::grids::ztensor<4> W1t_b(tr.sd().repn_bose().nts(), nk, 1, 1);
tr.omega_to_tau(W1w_b, W1t_b, 0);
green::grids::ztensor<4> W2w_b(tr.sd().repn_bose().nw(), nk, 1, 1);
green::grids::ztensor<4> W3w_b(1, nk, 1, 1);
tr.tau_to_omega(W1t_b, W2w_b, 0);
tr.tau_to_omega_w(W1t_b, W3w_b, 5, 0);
REQUIRE_THAT(W1w_b, IsCloseTo(W2w_b));
REQUIRE_THAT(W1w_b(5), IsCloseTo(W3w_b(0)));
}
SECTION("Check Leakage") {
double leakage = tr.check_chebyshev(X1t, 1);
REQUIRE(leakage < 1e-10);
}
}

TEST_CASE("Grids") {
Expand Down Expand Up @@ -104,7 +163,7 @@ TEST_CASE("Grids") {
check_transformer(tr);
}

SECTION("Wrong File") {
SECTION("File Does Not Exist") {
auto p = green::params::params("DESCR");
green::grids::define_parameters(p);
std::string input_file = "XXX"s;
Expand All @@ -114,6 +173,16 @@ TEST_CASE("Grids") {
REQUIRE_THROWS_AS(green::grids::transformer_t(p), green::grids::grids_file_not_found_error);
}

SECTION("Wrong File") {
auto p = green::params::params("DESCR");
green::grids::define_parameters(p);
std::string input_file = TEST_PATH + "/wrong_grid.h5"s;
std::string args = "test --BETA 10 --grid_file " + input_file;
auto [argc, argv] = get_argc_argv(args);
p.parse(argc, argv);
REQUIRE_THROWS_AS(green::grids::transformer_t(p), green::grids::grids_type_mismatch_error);
}

SECTION("Read Internally Stored Grids") {
auto p = green::params::params("DESCR");
green::grids::define_parameters(p);
Expand Down

0 comments on commit 5d4da25

Please sign in to comment.