TRIQS/triqs_cthyb 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
impurity_trace.checks.cpp
1/*******************************************************************************
2 *
3 * TRIQS: a Toolbox for Research in Interacting Quantum Systems
4 *
5 * Copyright (C) 2014, P. Seth, I. Krivenko, M. Ferrero and O. Parcollet
6 *
7 * TRIQS is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later
10 * version.
11 *
12 * TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * TRIQS. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ******************************************************************************/
21
22#include "impurity_trace.hpp"
23using namespace triqs_cthyb;
24
25//-------------------- Cache integrity check --------------------------------
26
27void impurity_trace::check_cache_integrity([[maybe_unused]] bool print) {
28#ifdef TRACE_CHECK_CACHE
29 static int check_counter = 0;
30 ++check_counter;
31 if (check_counter % 10 == 0) {
32 if (print) std::cout << " ---- Cache integrity check ---- " << std::endl;
33 if (print) std::cout << " check_counter = config number = " << check_counter << std::endl;
34 if (print) tree.graphviz(std::ofstream("tree_cache_check"));
35 foreach_subtree_first(tree, [&](node y) { this->check_cache_integrity_one_node(y, print); });
36 if (print) std::cout << " ---- Cache integrity completed ---- " << std::endl;
37 }
38#endif
39}
40
41//--------------------- Compute block table for one subtree, using an ordered traversal of the subtree -------------------
42
43int impurity_trace::check_one_block_table_linear(node n, int b, bool print) {
44
45 int B = b;
46 foreach_reverse(tree, n, [&](node y) {
47 if (B == -1) return;
48 auto BB = B;
49 B = (y->delete_flag ? B : this->get_op_block_map(y, B));
50 if (print)
51 std::cout << "linear computation : " << y->key << " " << y->op.dagger << " " << y->op.linear_index << " | " << BB << " -> " << B << std::endl;
52 });
53 return B;
54}
55
56//--------------------- Compute block table for one subtree, using an ordered traversal of the subtree -------------------
57
58matrix_t impurity_trace::check_one_block_matrix_linear(node top, int b) {
59
60 node p = tree.max(top);
61 matrix_t M = nda::eye<h_scalar_t>(get_block_dim(b));
62 auto _ = arrays::range::all;
63
64 foreach_reverse(tree, top, [&](node n) {
65 // multiply by the exponential unless it is the first call, i.e. first operator n==p
66 if (n != p) {
67 auto dtau = double(n->key - p->key);
68 // M <- exp * M
69 auto dim = M.shape()[0];
70 for (int i = 0; i < dim; ++i) M(i, _) *= std::exp(-dtau * get_block_eigenval(b, i));
71 // M <- Op * M
72 }
73 // multiply by operator matrix unless it is delete_flag
74 if (!n->delete_flag) {
75 int bp = this->get_op_block_map(n, b);
76 if (bp == -1) TRIQS_RUNTIME_ERROR << " Nasty error ";
77 M = get_op_block_matrix(n, b) * M;
78 b = bp;
79 }
80 p = n;
81 });
82
83 return M;
84}
85//-------------------- Cache integrity check for one node --------------------------------
86
87void impurity_trace::check_cache_integrity_one_node(node n, bool print) {
88 if (n == nullptr) return;
89 if (print) std::cout << " ... checking cache integrity for node " << n->key << std::endl;
90
91 // debug check : redo the linear calculation
92 auto &ca = n->cache;
93 for (int b = 0; b < n_blocks; ++b) {
94 auto check = check_one_block_table_linear(n, b, false);
95 if (ca.block_table[b] != check) {
96 std::cout << " Inconsistent block table for block " << b << " : cache = " << ca.block_table[b] << " while it should be " << check
97 << std::endl;
98 check_one_block_table_linear(n, b, true);
99 TRIQS_RUNTIME_ERROR << " FATAL ";
100 }
101 }
102}