VMPS++
Loading...
Searching...
No Matches
EntropyObserver.h
Go to the documentation of this file.
1#ifndef ENTROPY_OBSERVER
2#define ENTROPY_OBSERVER
3
4#include "EigenFiles.h" // from TOOLS
5
6template<typename MpsType>
8{
9public:
10
12 EntropyObserver (size_t L_input, size_t tpoints_input, DMRG::VERBOSITY::OPTION VERBOSITY=DMRG::VERBOSITY::SILENT, double DeltaS_input=1e-2)
13 :L(L_input), tpoints(tpoints_input), DeltaS(DeltaS_input), CHOSEN_VERBOSITY(VERBOSITY)
14 {
15 data.resize(tpoints,L-1);
16 DeltaSb.resize(tpoints,L-1);
17 }
18
19 vector<bool> TWO_SITE (int it, const MpsType &Psi, double r=1., vector<size_t> true_overrides={}, vector<size_t> false_overrides={});
20
21 void elongate (size_t Lleft=0, size_t Lright=0);
22
23 void save (string filename) const;
24 void save (int it, string filename) const;
25
26 MatrixXd get_DeltaSb() const {return DeltaSb;};
27
28private:
29
31 size_t L;
32 size_t tpoints;
33 double DeltaS;
34 MatrixXd data;
35 MatrixXd DeltaSb;
36};
37
38template<typename MpsType>
40TWO_SITE (int it, const MpsType &Psi, double r, vector<size_t> true_overrides, vector<size_t> false_overrides)
41{
42 vector<bool> res(L-1);
43
44 for (int b=0; b<L-1; ++b)
45 {
46 data(it,b) = Psi.entropy()(b);
47
48 DeltaSb(it,b) = std::nan("0");
49
50 if (it == 1)
51 {
52 // backward derivative using 1 point
53 DeltaSb(it,b) = (data(it,b)-data(it-1,b))/data(it-1,b);
54 }
55 else if (it > 1)
56 {
57 // backward derivative using 2 points
58 // 0.5*(1.+r): heuristic correction factor for arbitrary timestep
59 // r=dt(-2)/dt(-1)
60 DeltaSb(it,b) = 0.5*(3.*data(it,b)-4.*data(it-1,b)+data(it-2,b)) * 0.5*(1.+r) /data(it-2,b);
61 }
62
63 if (it == 0)
64 {
65 res[b] = true;
66 }
67 else
68 {
69 res[b] = (abs(DeltaSb(it,b)) > DeltaS)? true:false;
70 }
71
72 #ifndef TIME_PROP_USE_TERMPLOT
73 if (CHOSEN_VERBOSITY >= DMRG::VERBOSITY::STEPWISE)
74 {
75 std::streamsize ss = std::cout.precision();
76 lout << setprecision(4) << "b=" << b << ", δS(b)/S=" << DeltaSb(it,b) << ", S=" << data(it,b) << setprecision(ss) << endl;
77 }
78 #endif
79 }
80
81 #ifdef TIME_PROP_USE_TERMPLOT
82 if (CHOSEN_VERBOSITY >= DMRG::VERBOSITY::STEPWISE and it>0)
83 {
84 PlotParams p;
85 p.label = "δS(b)/S";
86 ArrayXd plotrow = DeltaSb.row(it);
87 if (!isnan(plotrow(0))) TerminalPlot::plot(plotrow,p);
88 }
89 #endif
90
91// if (res[0] == true and it>0 and CHOSEN_VERBOSITY > DMRG::VERBOSITY::SILENT)
92// {
93// lout << termcolor::yellow << "Entropy increase at the left edge!" << termcolor::reset << endl;
94// }
95// if (res[L-2] == true and it>0 and CHOSEN_VERBOSITY > DMRG::VERBOSITY::SILENT)
96// {
97// lout << termcolor::yellow << "Entropy increase at the right edge!" << termcolor::reset << endl;
98// }
99
100 for (int ib=0; ib<true_overrides.size(); ++ib)
101 {
102 res[true_overrides[ib]] = true;
103 }
104 for (int ib=0; ib<false_overrides.size(); ++ib)
105 {
106 res[false_overrides[ib]] = false;
107 }
108
109 if (CHOSEN_VERBOSITY >= DMRG::VERBOSITY::HALFSWEEPWISE)
110 {
111 lout << "EntropyObserver: ";
112 for (int b=0; b<L-1; ++b)
113 {
114 if (res[b])
115 {
116 cout << termcolor::red;
117 lout << 2;
118 }
119 else
120 {
121 cout << termcolor::blue;
122 lout << 1;
123 }
124 }
125 cout << termcolor::reset;
126 int trues = std::count(res.begin(), res.end(), true);
127 lout << " N_steps(2-site)=" << trues << " (" << round(trues*100./(L-1),1) << "%)" << endl;
128 }
129
130 return res;
131}
132
133template<typename MpsType>
135elongate (size_t Lleft, size_t Lright)
136{
137 if (Lleft>0 or Lright>0)
138 {
139 int Lold = L;
140 L += Lleft + Lright;
141 MatrixXd data_new(tpoints,L-1+Lleft+Lright);
142 data_new.setZero();
143 data_new.block(Lleft,0,data.rows(),data.cols()) = data;
144 data = data_new;
145 }
146}
147
148template<typename MpsType>
150save (string filename) const
151{
152 saveMatrix<double>(data, filename);
153}
154
155template<typename MpsType>
157save (int it, string filename) const
158{
159 saveMatrix<double>(data.topRows(it+1), filename);
160}
161
162#endif
vector< bool > TWO_SITE(int it, const MpsType &Psi, double r=1., vector< size_t > true_overrides={}, vector< size_t > false_overrides={})
void save(string filename) const
MatrixXd get_DeltaSb() const
void elongate(size_t Lleft=0, size_t Lright=0)
DMRG::VERBOSITY::OPTION CHOSEN_VERBOSITY
EntropyObserver(size_t L_input, size_t tpoints_input, DMRG::VERBOSITY::OPTION VERBOSITY=DMRG::VERBOSITY::SILENT, double DeltaS_input=1e-2)