TRIQS/triqs_ctseg 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
tau_t.hpp
1// Copyright (c) 2022--present, The Simons Foundation
2// Copyright (c) 2022--present, Max Planck Institute for Polymer Research, Mainz, Germany
3// This file is part of TRIQS/ctseg and is licensed under the terms of GPLv3 or later.
4// SPDX-License-Identifier: GPL-3.0-or-later
5// See LICENSE in the root of this distribution for details.
6
7#pragma once
8#include <h5/h5.hpp>
9#include <limits>
10#include <iostream>
11#include <cmath>
12#include <assert.h>
13
14#include <fmt/ostream.h>
15
16namespace triqs_ctseg {
17
28
29 class tau_t {
30
32 inline static double _beta;
33
35 uint64_t n = 0;
36
37 public:
39 static constexpr uint64_t n_max = std::numeric_limits<uint64_t>::max();
40
42 static void set_beta(double beta) { _beta = beta; }
43
45 tau_t() = default;
46
48 tau_t(uint64_t n_) : n(n_) {}
50 tau_t(double x) {
51 if ((x > _beta || x < 0)) {
52 throw std::invalid_argument("Time tau must be in the range [0, beta]");
53 } else if (x == _beta)
54 n = n_max;
55 else
56 n = uint64_t((x / _beta) * double(n_max));
57 }
58
60 auto operator<=>(tau_t const &tau) const { return n <=> tau.n; }
61 bool operator==(tau_t const &tau) const { return n == tau.n; }
62
64 explicit operator double() const { return _beta * (double(n) / double(n_max)); }
65
67 static tau_t beta() { return {uint64_t{n_max}}; }
68
70 static tau_t zero() { return {uint64_t{0}}; }
71
73 static tau_t epsilon() { return {uint64_t{1}}; }
74
75 // Get a random point in $]tau1, tau2[$
76 static tau_t random(auto &rng, tau_t const &tau1, tau_t const &tau2) {
77 auto n1 = tau1.n + 1;
78 return {rng(tau2.n - n1) + n1};
79 }
80
82 static tau_t random(auto &rng, tau_t const &tau) { return {rng(tau.n - 1) + 1}; }
83
84 // ----- Some basic op, with cyclicity --------
85
86 friend tau_t operator+(tau_t const &a, tau_t const &b) { return {a.n + b.n}; }
87
88 friend tau_t operator-(tau_t const &a, tau_t const &b) { return {a.n - b.n}; }
89
90 friend tau_t operator-(tau_t const &a) { return {tau_t::n_max - a.n}; }
91
92 // ----- IO --------
93
95 friend std::ostream &operator<<(std::ostream &out, tau_t const &p) {
96 return out << double(p) << " [tau_t : n = " << p.n << "]";
97 }
98 };
99
100 // ----- arithmetic operations --------
101
103 inline double operator*(tau_t const &a, tau_t const &b) { return double(a) * double(b); }
104 inline double operator/(tau_t const &a, tau_t const &b) { return double(a) / double(b); }
105
106 inline double operator+(tau_t const &x, double y) { return double(x) + y; }
107 inline double operator+(double y, tau_t const &x) { return y + double(x); }
108
109 inline double operator-(tau_t const &x, double y) { return double(x) - y; }
110 inline double operator-(double y, tau_t const &x) { return y - double(x); }
111
112 inline double operator*(tau_t const &x, double y) { return double(x) * y; }
113 inline double operator*(double y, tau_t const &x) { return y * double(x); }
114
115 inline double operator/(tau_t const &x, double y) { return double(x) / y; }
116 inline double operator/(double y, tau_t const &x) { return y / double(x); }
117
118} // namespace triqs_ctseg
119
120template <> struct fmt::formatter<triqs_ctseg::tau_t> : ostream_formatter {};
friend std::ostream & operator<<(std::ostream &out, tau_t const &p)
Stream insertion.
Definition tau_t.hpp:95
static constexpr uint64_t n_max
Maximum value that can be stored inside a uint64_t.
Definition tau_t.hpp:39
static void set_beta(double beta)
Set the temperature.
Definition tau_t.hpp:42
tau_t(uint64_t n_)
Not for users. Use the factories.
Definition tau_t.hpp:48
tau_t(double x)
For test only, not for users. Use the factories.
Definition tau_t.hpp:50
static tau_t epsilon()
Get epsilon, defined as $\epsilon = \beta /N_max$.
Definition tau_t.hpp:73
static tau_t beta()
tau_t at tau = beta
Definition tau_t.hpp:67
static tau_t random(auto &rng, tau_t const &tau)
Get a random point in $]0, tau[$.
Definition tau_t.hpp:82
auto operator<=>(tau_t const &tau) const
Comparisons (using integer, so it is safe).
Definition tau_t.hpp:60
static tau_t zero()
$\tau = 0$
Definition tau_t.hpp:70