Skip to content

Commit

Permalink
Merge pull request #32 from tyzhang1993/fix_expert_bug
Browse files Browse the repository at this point in the history
Fix bug in expert mode without matching contraction block.
  • Loading branch information
jturney authored Apr 19, 2019
2 parents af0b886 + 2114568 commit d8e1180
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/blocked_tensor/blocked_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ void LabeledBlockedTensor::contract(const LabeledBlockedTensorProduct &rhs,
}
if (not BT().is_block(result_key))
continue;
else if (zero_result) {
BT_.block(result_key).zero();
}
for (size_t n = 0; n < nterms; ++n)
{
const LabeledBlockedTensor &lbt = rhs[n];
Expand All @@ -1177,6 +1180,9 @@ void LabeledBlockedTensor::contract(const LabeledBlockedTensorProduct &rhs,
}
unique_indices_keys.push_back(uik);
}
if (unique_indices_keys.size() == 0) {
return;
}
if (full_contraction_size > unique_indices_keys.size()) {
full_contraction = false;
}
Expand Down Expand Up @@ -1334,6 +1340,9 @@ void LabeledBlockedTensor::contract_batched(const LabeledBlockedTensorBatchedPro
}
if (not BT().is_block(result_key))
continue;
else if (zero_result) {
BT_.block(result_key).zero();
}
bool do_contraction = true;
for (size_t n = 0; n < nterms; ++n)
{
Expand All @@ -1352,6 +1361,9 @@ void LabeledBlockedTensor::contract_batched(const LabeledBlockedTensorBatchedPro
unique_indices_keys.push_back(uik);
}
}
if (unique_indices_keys.size() == 0) {
return;
}
if (full_contraction_size > unique_indices_keys.size()) {
full_contraction = false;
}
Expand Down
59 changes: 59 additions & 0 deletions test/test_blocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,62 @@ double test_batched_with_factor()
return C2.norm(0);
}

double test_Oia_equal_Cbu_Guv_Tivab_expert()
{
BlockedTensor::set_expert_mode(true);
BlockedTensor::reset_mo_spaces();

const size_t nc = 3, na = 2, nv = 5;

// define space labels
std::string acore_label_ = "c";
std::string aactv_label_ = "a";
std::string avirt_label_ = "v";

std::vector<size_t> core_mos_(nc);
for (size_t i = 0; i < nc; ++i) {
core_mos_[i] = i;
}
std::vector<size_t> actv_mos_(na);
for (size_t i = 0; i < na; ++i) {
actv_mos_[i] = i;
}
std::vector<size_t> virt_mos_(nv);
for (size_t i = 0; i < nv; ++i) {
virt_mos_[i] = i;
}
// add Ambit index labels
BlockedTensor::add_mo_space(acore_label_, "mn", core_mos_, AlphaSpin);
BlockedTensor::add_mo_space(aactv_label_, "uv", actv_mos_, AlphaSpin);
BlockedTensor::add_mo_space(avirt_label_, "ef", virt_mos_, AlphaSpin);

// define composite spaces
BlockedTensor::add_composite_mo_space("h", "ijkl", {acore_label_, aactv_label_});
BlockedTensor::add_composite_mo_space("p", "abcd", {aactv_label_, avirt_label_});

BlockedTensor G = BlockedTensor::build(CoreTensor, "Gamma1", {"aa"});
BlockedTensor O = BlockedTensor::build(CoreTensor, "O1 PT3 1/3", {"pc", "va"});
BlockedTensor C = BlockedTensor::build(CoreTensor, "C1", {"cp", "av", "pc", "va"});
BlockedTensor T = BlockedTensor::build(CoreTensor, "T2 Amplitudes", {"hhpp"});

Tensor Oac_t = build_and_fill("Oac", {na, nc}, a2);
Tensor Ovc_t = build_and_fill("Ovc", {nv, nc}, b2);
Tensor Ova_t = build_and_fill("Ova", {nv, na}, c2);

O.block("ac")("ia") = Oac_t("ia");
O.block("vc")("ia") = Ovc_t("ia");
O.block("va")("ia") = Ova_t("ia");

O["ia"] = C["bu"] * G["uv"] * T["ivab"];

Tensor Oac = O.block("ac");
double diff_oo = difference(Oac, a2).second;

BlockedTensor::set_expert_mode(false);

return diff_oo;
}

int main(int argc, char *argv[])
{
printf(ANSI_COLOR_RESET);
Expand Down Expand Up @@ -2494,6 +2550,9 @@ int main(int argc, char *argv[])
std::make_tuple(
kPass, test_batched_with_factor,
"C2[\"ijrs\"] = batched(\"r\", 0.5 * A[\"abrs\"] * B[\"ijab\"])"),
std::make_tuple(
kPass, test_Oia_equal_Cbu_Guv_Tivab_expert,
"O[\"ia\"] = C[\"bu\"] * G[\"uv\"] * T[\"ivab\"]"),
};

std::vector<std::tuple<std::string, TestResult, double>> results;
Expand Down

0 comments on commit d8e1180

Please sign in to comment.