TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
grid_generator.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//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include <itertools/itertools.hpp>
28#include <nda/nda.hpp>
29
30#include <iterator>
31
32namespace triqs::lattice {
33
38
40 using v_t = nda::vector<double>;
41
66 class grid_generator : public itertools::iterator_facade<grid_generator, v_t const, std::forward_iterator_tag> {
67 friend struct itertools::iterator_facade<grid_generator, v_t const, std::forward_iterator_tag>;
68 int dim{3}, nkpts{0}, nx{0}, ny{0}, nz{0}, N_X{0}, N_Y{0}, N_Z{0}, index_{0};
69 double step_x{0.0}, step_y{0.0}, step_z{0.0};
70 bool at_end{false};
71 v_t pt;
72 void init() {
73 N_X = nkpts;
74 N_Y = (dim > 1 ? nkpts : 1);
75 N_Z = (dim > 2 ? nkpts : 1);
76 step_x = 1.0 / double(N_X);
77 step_y = 1.0 / double(N_Y);
78 step_z = 1.0 / double(N_Z);
79 pt(0) = step_x / 2;
80 pt(1) = step_y / 2;
81 pt(2) = step_z / 2;
82 }
83
84 void increment() {
85 if (nx < N_X - 1) {
86 ++nx;
87 pt(0) += step_x;
88 ++index_;
89 return;
90 }
91 pt(0) = step_x / 2;
92 nx = 0;
93 if (ny < N_Y - 1) {
94 ++ny;
95 pt(1) += step_y;
96 ++index_;
97 return;
98 }
99 pt(1) = step_y / 2;
100 ny = 0;
101 if (nz < N_Z - 1) {
102 ++nz;
103 pt(2) += step_z;
104 ++index_;
105 return;
106 }
107 at_end = true;
108 }
109
110 value_type dereference() const { return pt; }
111
112 public:
119 grid_generator(int dim, int nkpts) : dim(dim), nkpts(nkpts), pt(3) { init(); }
120
122 grid_generator() : pt(3) { init(); }
123
125 bool operator==(grid_generator const &other) const { return ((other.dim == dim) && (other.index_ == index_) && (other.nkpts == nkpts)); }
126
128 int size() const { return (N_X * N_Y * N_Z); }
129
131 int index() const { return index_; }
132
134 operator bool() const { return !(at_end); }
135 };
136
138
139} // namespace triqs::lattice
bool operator==(grid_generator const &other) const
Equal-to comparison: two generators are equal if they share the same dimension, number of points and ...
grid_generator(int dim, int nkpts)
Construct a grid generator with the given dimension and number of points per dimension.
int size() const
Total number of grid points, equal to (trailing factors are 1 for dim < 3).
grid_generator()
Default constructor: produces a 3-dimensional generator with zero points (an empty/end iterator).
int index() const
Linear index of the current grid point in the iteration order (x fastest, then y, then z).
nda::vector< double > v_t
Point type used by grid_generator: a real-valued vector (nda::vector<double>).