TRIQS/triqs_cthyb 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
configuration.hpp
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#pragma once
22#include "./util.hpp"
23#include <triqs/hilbert_space/hilbert_space.hpp>
24#include <triqs/utility/time_pt.hpp>
25#include <triqs/atom_diag/atom_diag.hpp>
26#include <triqs/atom_diag/functions.hpp>
27#include <triqs/utility/macros.hpp>
28
29#include <h5/h5.hpp>
30
31#include <map>
32
33namespace triqs_cthyb {
34
35 using triqs::utility::time_pt;
36 using triqs::utility::time_segment;
37
39 struct op_desc {
42
45
47 bool dagger;
48
51
52 friend std::ostream &operator<<(std::ostream &out, op_desc const &op) {
53 out << (op.dagger ? "Cdag(" : "C(") << op.block_index << "," << op.inner_index << ")";
54 return out;
55 }
56
57 static std::string hdf5_format() { return "op_desc"; }
58
59 friend void h5_write(h5::group g, std::string const &name, op_desc const &op) {
60 auto gr = g.create_group(name);
61 h5::write_hdf5_format(gr, op); // NOLINT (slicing is intended)
62 h5::write(gr, "block", op.block_index);
63 h5::write(gr, "inner", op.inner_index);
64 h5::write(gr, "dagger", op.dagger);
65 h5::write(gr, "linear_index", op.linear_index);
66 }
67
68 friend void h5_read(h5::group g, std::string const &name, op_desc &op) {
69 h5::group gr = g.open_group(name);
70 h5::assert_hdf5_format(gr, op);
71 h5::read(g, "block", op.block_index);
72 h5::read(g, "inner", op.inner_index);
73 h5::read(g, "dagger", op.dagger);
74 h5::read(g, "linear_index", op.linear_index);
75 }
76
77 bool operator==(op_desc const &op) const = default;
78 };
79
81 struct configuration {
82
83 bool operator==(configuration const &config) const { return (beta_ == config.beta_ && oplist_ == config.oplist_); }
84
85 // a map associating an operator to an imaginary time
86 using oplist_t = std::map<time_pt, op_desc, std::greater<time_pt>>;
87
88#ifdef SAVE_CONFIGS
89 configuration(double beta, long id = 0, oplist_t oplist = {})
90 : beta_(beta), id_(id), oplist_(oplist), configs_hfile("configs.h5", exists("configs.h5") ? 'a' : 'w') {
91 if (NUM_CONFIGS_TO_SAVE > 0) h5_write(configs_hfile, "c_0", *this);
92 }
93 ~configuration() { configs_hfile.close(); }
94#else
95 configuration(double beta, long id = 0) : beta_(beta), id_(id) {}
96 C2PY_IGNORE configuration(double beta, long id, oplist_t oplist) : beta_(beta), id_(id), oplist_(oplist) {}
97#endif
98
100 C2PY_PROPERTY_GET(beta) double beta() const { return beta_; }
101 auto size() const { return oplist_.size(); }
102
109 void insert(time_pt tau, op_desc op) { oplist_.insert({tau, op}); }
110
117 void replace(time_pt tau, op_desc op) { oplist_[tau] = op; }
118
123 void erase(time_pt const &t) { oplist_.erase(t); }
124
126 void clear() { oplist_.clear(); }
127
128 oplist_t::iterator begin() { return oplist_.begin(); }
129 oplist_t::iterator end() { return oplist_.end(); }
130 oplist_t::const_iterator begin() const { return oplist_.begin(); }
131 oplist_t::const_iterator end() const { return oplist_.end(); }
132
133 friend std::ostream &operator<<(std::ostream &out, configuration const &c) {
134 for (auto const &op : c) out << "tau = " << op.first << " : " << op.second << std::endl;
135 return out;
136 }
137
139 static std::string hdf5_format() { return "CTHYB_Configuration"; }
140
142 friend void h5_write(h5::group g, std::string const &name, configuration const &c) {
143 h5::group gr = g.create_group(name);
144 h5::write_hdf5_format(gr, c); // NOLINT (slicing is intended)
145 h5::write(gr, "beta", c.beta_);
146 h5::write(gr, "id", c.id_);
147 h5::write(gr, "oplist", c.oplist_);
148 }
149
151 C2PY_IGNORE static configuration h5_read_construct(h5::group g, std::string const &name) {
152 h5::group gr = g.open_group(name);
153 h5::assert_hdf5_format<configuration>(gr);
154 auto beta = h5::read<double>(gr, "beta");
155 auto id = h5::read<long>(gr, "id");
156 auto oplist = h5::read<oplist_t>(gr, "oplist");
157 return configuration(beta, id, std::move(oplist));
158 }
159
161 long get_id() const { return id_; } // Get the id of the current configuration
162
164 void finalize() {
165 id_++;
166#ifdef SAVE_CONFIGS
167 if (id < NUM_CONFIGS_TO_SAVE) h5_write(configs_hfile, "c_" + std::to_string(id), *this);
168#endif
169 }
170
171 private:
172 double beta_;
173 long id_; // configuration id, for debug purposes
174 oplist_t oplist_;
175
176#ifdef SAVE_CONFIGS
177 // HDF5 file to save configurations
178 h5::file configs_hfile;
179#endif
180 };
181} // namespace triqs_cthyb
void finalize()
Finalize the configuration after a Monte Carlo move (increment the ID and save the configuration if n...
long get_id() const
Get the ID of the current configuration (for debug purposes).
static C2PY_IGNORE configuration h5_read_construct(h5::group g, std::string const &name)
Read a configuration from an hdf5 file.
void replace(time_pt tau, op_desc op)
Replace an existing operator at a given imaginary time with a new one.
friend void h5_write(h5::group g, std::string const &name, configuration const &c)
Write a configuration to an hdf5 file.
void erase(time_pt const &t)
Erase the operator at a given imaginary time.
C2PY_PROPERTY_GET(beta) double beta() const
Inverse temperature .
void insert(time_pt tau, op_desc op)
Insert a given operator at a given imaginary time.
void clear()
Clear the configuration (remove all operators).
static std::string hdf5_format()
HDF5 format string for configuration.
Description of a creation/annihilation operator.
long linear_index
Cumulative (linear) index.
bool dagger
Whether the operator is a dagger (creation operator).
int inner_index
Inner index within the block.
int block_index
Block index of the operator.