TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
time_pt.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
4// Copyright (c) 2016 Igor Krivenko
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Michel Ferrero, Igor Krivenko, Olivier Parcollet, Priyanka Seth, Hugo U. R. Strand, Nils Wentzell
20
25
26#pragma once
27
28#include "./first_include.hpp"
29#include "./macros.hpp"
30
31#include <h5/h5.hpp>
32
33#include <algorithm>
34#include <cstdint>
35#include <iostream>
36#include <limits>
37#include <string>
38
39namespace triqs::utility {
40
41 // Forward declaration.
42 struct time_segment;
43
48
60 struct time_pt {
61 // Friend declaration.
62 friend struct time_segment;
63
67 time_pt() = default;
68
75 time_pt(uint64_t n_, double beta_) : n(n_), beta(beta_), val(beta_ * (static_cast<double>(n_) / static_cast<double>(Nmax))) {}
76
78 time_pt &operator=(double v) = delete;
79
86 auto operator<=>(time_pt const &tp) const { return n <=> tp.n; }
87
94 bool operator==(time_pt const &tp) const { return n == tp.n; }
95
103 inline friend time_pt operator+(time_pt const &tp1, time_pt const &tp2) {
104 bool wrapped = ((Nmax - std::max(tp1.n, tp2.n)) < std::min(tp1.n, tp2.n));
105 if (!wrapped)
106 return {(tp1.n + tp2.n) % Nmax, tp1.beta};
107 else
108 return {((tp1.n + tp2.n) + 1) % Nmax, tp1.beta};
109 }
110
118 inline friend time_pt operator-(time_pt const &tp1, time_pt const &tp2) {
119 uint64_t nres = (tp1.n >= tp2.n ? (tp1.n - tp2.n) % Nmax : Nmax - (tp2.n - tp1.n));
120 return {nres, tp1.beta};
121 }
122
129 friend time_pt operator-(time_pt const &tp) { return {Nmax - tp.n, tp.beta}; }
130
138 friend time_pt div_by_int(time_pt const &tp, size_t a) { return {tp.n / a, tp.beta}; }
139
147 friend time_pt mult_by_int(time_pt const &tp, size_t a) { return {tp.n * a, tp.beta}; }
148
156 friend size_t floor_div(time_pt const &tp1, time_pt const &tp2) { return tp1.n / tp2.n; }
157
162 explicit operator double() const { return val; }
163
171 friend std::ostream &operator<<(std::ostream &out, time_pt const &p) {
172 return out << p.val << " [time_pt : beta = " << p.beta << " n = " << p.n << "]";
173 }
174
176 static constexpr uint64_t Nmax = std::numeric_limits<uint64_t>::max();
177
179 [[nodiscard]] static std::string hdf5_format() { return "time_pt"; }
180
188 friend void h5_write(h5::group g, std::string const &name, time_pt const &tp) {
189 auto gr = g.create_group(name);
190 write_hdf5_format(gr, tp); // NOLINT
191 h5_write(gr, "beta", tp.beta);
192 h5_write(gr, "val", tp.val);
193 h5_write(gr, "n", tp.n);
194 }
195
203 friend void h5_read(h5::group g, std::string const &name, time_pt &tp) {
204 auto gr = g.open_group(name);
205 h5_read(gr, "beta", tp.beta);
206 h5_read(gr, "val", tp.val);
207 h5_read(gr, "n", tp.n);
208 }
209
210 private:
211 uint64_t n = 0;
212 double beta = 0;
213 double val = 0;
214 };
215
224 inline double operator*(time_pt const &tp1, time_pt const &tp2) { return double(tp1) * double(tp2); }
225
234 inline double operator/(time_pt const &tp1, time_pt const &tp2) { return double(tp1) / double(tp2); }
235
236 // Arithmetic operations between time_pt and double values.
237#define IMPL_OP(OP) \
238 inline double operator OP(time_pt const &x, double y) { return static_cast<double>(x) OP y; } \
239 inline double operator OP(double y, time_pt const &x) { return y OP static_cast<double>(x); }
240 IMPL_OP(+);
241 IMPL_OP(-);
242 IMPL_OP(*);
243 IMPL_OP(/);
244#undef IMPL_OP
245
251 double beta;
252
257 time_segment(double beta_) : beta(beta_) {}
258
267 template <typename RNG> [[nodiscard]] time_pt get_random_pt(RNG &rng, time_pt tp) const { return {rng(tp.n), beta}; }
268
276 template <typename RNG> [[nodiscard]] time_pt get_random_pt(RNG &rng) const { return {rng(time_pt::Nmax), beta}; }
277
287 template <typename RNG> [[nodiscard]] time_pt get_random_pt(RNG &rng, time_pt tp1, time_pt tp2) const {
288 return {rng(tp2.n - tp1.n) + tp1.n, beta};
289 }
290
295 [[nodiscard]] time_pt get_upper_pt() const { return {time_pt::Nmax, beta}; }
296
301 [[nodiscard]] time_pt get_lower_pt() const { return {0, beta}; }
302
307 [[nodiscard]] time_pt get_epsilon() const { return {1, beta}; }
308
315 [[nodiscard]] time_pt make_time_pt(double x) const {
316 EXPECTS(0 <= x && x <= beta);
317 uint64_t n = static_cast<uint64_t>(static_cast<double>(time_pt::Nmax) * std::min(1.0, (x / beta)));
318 return {n, beta};
319 }
320 };
321
323
324} // namespace triqs::utility
Compiler / platform glue and the dcomplex alias (must be included before any Boost header).
double operator*(time_pt const &tp1, time_pt const &tp2)
Multiplication between two time points.
Definition time_pt.hpp:224
double operator/(time_pt const &tp1, time_pt const &tp2)
Division between two time points.
Definition time_pt.hpp:234
Common macros used in TRIQS.
A point in imaginary time, , stored on a very fine integer grid.
Definition time_pt.hpp:60
bool operator==(time_pt const &tp) const
Equal-to operator compares the integer grid positions of two time points.
Definition time_pt.hpp:94
static constexpr uint64_t Nmax
Largest possible grid position.
Definition time_pt.hpp:176
friend void h5_write(h5::group g, std::string const &name, time_pt const &tp)
Write a time_pt to HDF5.
Definition time_pt.hpp:188
time_pt()=default
Default constructor creates the point for .
friend time_pt operator-(time_pt const &tp1, time_pt const &tp2)
Subtraction operator performs cyclic subtraction on .
Definition time_pt.hpp:118
friend size_t floor_div(time_pt const &tp1, time_pt const &tp2)
Perform floor division between two time points.
Definition time_pt.hpp:156
static std::string hdf5_format()
Get the HDF5 format tag.
Definition time_pt.hpp:179
friend void h5_read(h5::group g, std::string const &name, time_pt &tp)
Read a time_pt from HDF5.
Definition time_pt.hpp:203
friend time_pt mult_by_int(time_pt const &tp, size_t a)
Multiply a time point by an integer.
Definition time_pt.hpp:147
friend time_pt div_by_int(time_pt const &tp, size_t a)
Divide a time point by an integer.
Definition time_pt.hpp:138
friend time_pt operator+(time_pt const &tp1, time_pt const &tp2)
Addiditon operator performs cyclic addition on .
Definition time_pt.hpp:103
friend time_pt operator-(time_pt const &tp)
Unary minus operator performs cyclic negation .
Definition time_pt.hpp:129
time_pt & operator=(double v)=delete
Assigning a double is deleted to avoid accidental loss of precision.
time_pt(uint64_t n_, double beta_)
Construct a point from an integer grid position and the inverse temperature.
Definition time_pt.hpp:75
friend std::ostream & operator<<(std::ostream &out, time_pt const &p)
Write a time_pt to an output stream.
Definition time_pt.hpp:171
auto operator<=>(time_pt const &tp) const
Three-way comparison operator compares the integer grid positions of two time points.
Definition time_pt.hpp:86
Represents the imaginary-time segment .
Definition time_pt.hpp:249
time_pt get_lower_pt() const
Get the lower end of the segment.
Definition time_pt.hpp:301
time_pt get_random_pt(RNG &rng, time_pt tp1, time_pt tp2) const
Draw a uniform random point in .
Definition time_pt.hpp:287
time_pt get_upper_pt() const
Get the upper end of the segment.
Definition time_pt.hpp:295
time_pt get_epsilon() const
Get a time point representing one grid step .
Definition time_pt.hpp:307
time_pt get_random_pt(RNG &rng) const
Draw a uniform random point in .
Definition time_pt.hpp:276
time_pt make_time_pt(double x) const
Convert a double value to the nearest point on the grid.
Definition time_pt.hpp:315
time_pt get_random_pt(RNG &rng, time_pt tp) const
Draw a uniform random point in .
Definition time_pt.hpp:267
time_segment(double beta_)
Construct a segment from an inverse temperature.
Definition time_pt.hpp:257
double beta
Inverse temperature .
Definition time_pt.hpp:251