TRIQS/triqs_modest 3.3.0
Brillouin zone summation
Loading...
Searching...
No Matches
embedding.hpp
Go to the documentation of this file.
1// Copyright (c) 2025--present, The Simons Foundation
2// This file is part of TRIQS/modest and is licensed under the terms of GPLv3 or later.
3// SPDX-License-Identifier: GPL-3.0-or-later
4// See LICENSE in the root of this distribution for details.
5
6#pragma once
7#include "./downfolding.hpp"
8#include "utils/defs.hpp"
9#include "utils/gf_supp.hpp"
10#include "utils/range_supp.hpp"
11#include <fmt/ranges.h>
12
13namespace triqs {
14 using block_matrix_t = std::vector<nda::matrix<dcomplex>>;
15 using block2_matrix_t = nda::array<nda::matrix<dcomplex>, 2>;
16} // namespace triqs
17
18namespace triqs::modest {
19
29 class embedding {
30
32 std::vector<long> sigma_embed_decomp;
33
35 std::vector<std::vector<long>> imp_decomps;
36
38 std::vector<std::string> _sigma_names;
39
41 std::vector<std::string> alpha_names;
42
44 friend std::ostream &operator<<(std::ostream &out, embedding const &E);
45
47 friend void h5_write(h5::group g, std::string const &name, embedding const &x);
48 friend void h5_read(h5::group g, std::string const &name, embedding &x);
49
50 public:
53 struct imp_block_t {
54
55 long imp_idx = 0; // index of the impurity
56 long gamma = 0; // first index γ of the block in the Σ_imp
57 long tau = 0; // second index τ of the block in the Σ_imp
58
59 imp_block_t() = default;
60 imp_block_t(long n_imp, long gamma, long tau) : imp_idx(n_imp), gamma(gamma), tau(tau) {}
61 friend bool operator==(imp_block_t const &, imp_block_t const &) = default;
62
63 // FIXME : merge with format_as
64 friend std::ostream &operator<<(std::ostream &out, imp_block_t const &x) {
65 return out << fmt::format("(imp_idx = {}, γ = {}, τ = {})", x.imp_idx, x.gamma, x.tau);
66 }
67 };
70 private:
71 // For each imp, for all (γ, τ), a list of all (α, σ) such that ψ[α, σ] ==(n_imp, γ, τ)
72 nda::array<imp_block_t, 2> psi; // ψ [α, σ] --> (n_imp, γ, τ)
73
74 // reverse ψ [n_imp] -> (γ, τ) -> [(α, σ)]
75 std::vector<nda::array<std::vector<std::array<long, 2>>, 2>> reverse_psi;
76
77 public:
79
88 embedding(std::vector<long> sigma_embed_decomposition, std::vector<std::vector<long>> imp_decompositions, nda::array<imp_block_t, 2> psi,
89 std::vector<std::string> sigma_names);
90
92 embedding() = default; // for h5_read
96
97
99 imp_block_t operator[](long alpha, long sigma) const { return psi(alpha, sigma); }
100
101 // HL: get_psi -> psi to match other classes?
103 [[nodiscard]] nda::array<imp_block_t, 2> get_psi() const { return psi; }
104
106 [[nodiscard]] long n_alpha() const { return psi.extent(0); }
107
109 [[nodiscard]] long n_sigma() const { return psi.extent(1); }
110
112 [[nodiscard]] long n_gamma(long imp_idx) const { return long(imp_decomps[imp_idx].size()); }
113
115 [[nodiscard]] long n_impurities() const { return long(imp_decomps.size()); }
116
118 [[nodiscard]] std::vector<std::string> sigma_names() const { return _sigma_names; };
119
121 [[nodiscard]] std::vector<long> imp_decomposition(long imp_idx) const { return imp_decomps[imp_idx]; };
122
125
127 std::vector<gf_struct_t> imp_block_shape() const;
128
130 std::string description(bool verbosity = false) const;
132
133 // ****************** Operation methods to change the embedding **********************
134
139
140 // --------------------------------------------------------
141 bool operator==(embedding const &other) const = default;
142 // --------------------------------------------------------
143
153 embedding drop(long imp_idx) const;
154
165 embedding replace(long imp_idx_to_remove, long imp_idx_to_replace_with) const;
166
175 embedding flip_spin(long alpha) const;
176
185 embedding flip_spin(std::vector<long> alphas) const;
186
188 embedding split(long imp_idx, std::function<bool(long)> p) const;
189
200 embedding split(long imp_idx, std::vector<long> const &block_list) const;
201
202 embedding split(long imp_idx, std::initializer_list<const char *> x) = delete;
204
205 //------------------------------------------------------------------------------------
206 template <typename Mesh> std::vector<std::pair<block_gf<Mesh, matrix_valued>, block_matrix_t>> make_zero_imp_self_energies(Mesh const &mesh) {
207 auto make_block_matrix = [](auto const &gf_struct) {
208 return gf_struct | stdv::transform([](auto &x) {
209 auto bl_size = x.second;
210 return nda::zeros<dcomplex>(bl_size, bl_size);
211 })
212 | tl::to<std::vector<nda::matrix<dcomplex>>>();
213 };
214 return imp_block_shape() | stdv::transform([&](auto const &gf_struct) {
215 auto Sigma_static = make_block_matrix(gf_struct);
216 auto Sigma_dynamic = block_gf<Mesh, matrix_valued>{mesh, gf_struct};
217 return std::make_pair(Sigma_dynamic, Sigma_static);
218 })
219 | tl::to<std::vector>();
220 }
221 //------------------------------------------------------------------------------------
222
223 // ****************************************************
224 // Embedding Extract/Embed methods
225 // ****************************************************
226 //--------------------- Embed -----------------------------------------
230
231
233 template <typename Mesh> block2_gf<Mesh, matrix_valued> embed(std::vector<block_gf<Mesh, matrix_valued>> const &Sigma_imp_vec) const;
234
236 template <typename Mesh>
237 std::pair<block2_gf<Mesh, matrix_valued>, block2_matrix_t> embed(std::vector<block_gf<Mesh, matrix_valued>> const &Sigma_imp_vec,
238 std::vector<block_matrix_t> const &Sigma_imp_static_vec) const;
239
241 block2_matrix_t embed(std::vector<block_matrix_t> const &Sigma_imp_static_vec) const;
242
244 nda::array<nda::array<dcomplex, 3>, 2> embed(std::vector<std::vector<nda::array<dcomplex, 3>>> const &Sigma_imp_vec) const;
245
247 std::vector<nda::array<dcomplex, 5>> embed(std::vector<nda::array<dcomplex, 5>> const &pi_imp_vec) const;
248
250 // nda::array<dcomplex, 4> embed(std::vector<nda::array<dcomplex, 4>> const &U_tensor_vec) const;
251
252 //--------------------- Extract ---------------------------------------
253
255 template <typename Mesh> std::vector<block_gf<Mesh, matrix_valued>> extract(block2_gf<Mesh, matrix_valued> const &g_loc) const;
256
258 std::vector<block_matrix_t> extract(block2_matrix_t const &matrix_C) const;
259
261 std::vector<std::vector<nda::array<dcomplex, 3>>> extract(nda::array<dcomplex, 4> const &g_loc) const;
262
264 std::vector<nda::array<dcomplex, 5>> extract(nda::array<dcomplex, 5> const &Pi_loc) const;
265
267 // std::vector<nda::array<dcomplex, 4>> extract(nda::array<dcomplex, 4> const &U_tensor) const;
269 };
270
271 // ***************************** Free functions/factories *******************************************
272
289 embedding make_embedding_impl(local_space const &C_space, nda::array<std::vector<long>, 2> const &block_decomposition,
290 std::optional<std::vector<long>> const &atom_to_imp = std::nullopt);
301 embedding make_embedding(local_space const &C_space, bool use_atom_equivalences = true);
302
305 // ***************************** Method implementation ***************************************
306
315 template <typename Mesh> block2_gf<Mesh, matrix_valued> embedding::embed(std::vector<block_gf<Mesh, matrix_valued>> const &Sigma_imp_vec) const {
316 // Check that all meshes are the same for Sigma_imp_vec
317 if (not all_equal(Sigma_imp_vec | stdv::transform([](auto &&x) -> decltype(auto) { return x[0].mesh(); })))
318 throw std::runtime_error{"[embedding_desc::embed]: meshes of solvers are not all equal"};
319
320 auto const &mesh = Sigma_imp_vec[0][0].mesh();
321
322 // build the result
323 auto Sigma_embed = make_block2_gf(mesh, this->sigma_embed_block_shape());
324 for (auto &&[S, m] : zip(Sigma_embed, psi)) {
325 if (m.imp_idx == -1) continue;
326 S() = Sigma_imp_vec[m.imp_idx][m.gamma + n_gamma(m.imp_idx) * m.tau];
327 }
328 return Sigma_embed;
329 }
330
339 template <typename Mesh>
340 std::pair<block2_gf<Mesh, matrix_valued>, block2_matrix_t> embedding::embed(std::vector<block_gf<Mesh, matrix_valued>> const &Sigma_imp_vec,
341 std::vector<block_matrix_t> const &Sigma_imp_static_vec) const {
342 if (Sigma_imp_vec.size() != Sigma_imp_static_vec.size()) {
343 throw std::runtime_error(fmt::format("The lists of self-energies are not equal {} != {}", Sigma_imp_vec.size(), Sigma_imp_static_vec.size()));
344 }
345 return {this->embed(Sigma_imp_vec), this->embed(Sigma_imp_static_vec)};
346 }
347
348 // ---------------------------------------------------------------
349
357 template <typename Mesh> std::vector<block_gf<Mesh, matrix_valued>> embedding::extract(block2_gf<Mesh, matrix_valued> const &g) const {
358
359 if (auto decomp = get_struct(g).dims(r_all, 0) | tl::to<std::vector>(); decomp != this->sigma_embed_decomp) {
360 if (decomp.size() != 1) throw std::runtime_error{"extract: g should have decomp = sigma_embedding_decomp or [1]"};
362 }
363
364 // FIXME : check all meshes are the same
365 auto imp_gf_stru_list = imp_block_shape();
366 auto extract_one_imp = [&](long n_imp) {
367 auto gimp = block_gf{g(0, 0).mesh(), imp_gf_stru_list[n_imp]};
368 auto const &rpsi = reverse_psi[n_imp];
369 for (auto [gamma, tau] : rpsi.indices()) {
370 auto [alpha, sigma] = rpsi(gamma, tau)[0];
371 gimp[gamma + n_gamma(n_imp) * tau].data() = g(alpha, sigma).data();
372 }
373 return gimp;
374 };
375 return range(n_impurities()) | stdv::transform(extract_one_imp) | tl::to<std::vector>();
376 }
377
378 // -----------------------------------------------------------------------
379 inline std::pair<one_body_elements_on_grid, embedding> make_embedding_with_clusters(one_body_elements_on_grid obe,
380 std::vector<std::vector<long>> const &atom_partition) {
381 auto new_obe = permute_local_space(atom_partition, obe);
382 auto E = make_embedding(new_obe.C_space, false);
383 return {new_obe, E};
384 }
385
386 // ------------------------------------------------------------------------------
387
388#define INSTANTIATE(Mesh) \
389 template block2_gf<Mesh, matrix_valued> embedding::embed(std::vector<block_gf<Mesh, matrix_valued>> const &) const; \
390 template std::pair<block2_gf<Mesh, matrix_valued>, block2_matrix_t> embedding::embed(std::vector<block_gf<Mesh, matrix_valued>> const &, \
391 std::vector<block_matrix_t> const &) const; \
392 template std::vector<block_gf<Mesh, matrix_valued>> embedding::extract(block2_gf<Mesh, matrix_valued> const &) const; \
393 template std::vector<std::pair<block_gf<Mesh, matrix_valued>, block_matrix_t>> embedding::make_zero_imp_self_energies(Mesh const &);
394 INSTANTIATE(imfreq);
395 INSTANTIATE(refreq);
396 INSTANTIATE(dlr_imfreq);
397#undef INSTANTIATE
398
399} // namespace triqs::modest
The embedding class.
Definition embedding.hpp:29
friend void h5_write(h5::group g, std::string const &name, embedding const &x)
h5 read/write
Definition h5.cpp:104
C2PY_IGNORE gf_struct2_t sigma_embed_block_shape() const
Gf block structure for Σ_embed.
long n_impurities() const
Number of impurities.
long n_gamma(long imp_idx) const
Number of blocks in γ for the Σ_imp[imp_idx].
long n_sigma() const
Number of blocks in σ for the Σ_embed.
std::vector< gf_struct_t > imp_block_shape() const
Gf block structure for the impurity solvers.
embedding flip_spin(long alpha) const
Flip the spins (σ) for block α.
bool operator==(embedding const &other) const =default
embedding split(long imp_idx, std::initializer_list< const char * > x)=delete
nda::array< imp_block_t, 2 > get_psi() const
the mapping table ψ
std::vector< std::string > sigma_names() const
The names of the sigma indices.
std::vector< block_gf< Mesh, matrix_valued > > extract(block2_gf< Mesh, matrix_valued > const &g_loc) const
embed tensors
long n_alpha() const
Number of blocks in α for the Σ_embed.
std::vector< long > imp_decomposition(long imp_idx) const
The impurity decomposition.
std::string description(bool verbosity=false) const
Summarize the embedding object.
Definition embedding.cpp:98
embedding drop(long imp_idx) const
Remove an impurity from the embedding table ψ
friend std::ostream & operator<<(std::ostream &out, embedding const &E)
stream
friend void h5_read(h5::group g, std::string const &name, embedding &x)
Definition h5.cpp:96
std::vector< std::pair< block_gf< Mesh, matrix_valued >, block_matrix_t > > make_zero_imp_self_energies(Mesh const &mesh)
embedding split(long imp_idx, std::function< bool(long)> p) const
Predicate p (long block_idx) -> 0 or 1.
embedding replace(long imp_idx_to_remove, long imp_idx_to_replace_with) const
Replaces one impurity in the embedding table ψ.
imp_block_t operator[](long alpha, long sigma) const
bracket accessor: [α, σ] -> the corresponding impurity block (n_imp, γ, τ)
Definition embedding.hpp:99
block2_gf< Mesh, matrix_valued > embed(std::vector< block_gf< Mesh, matrix_valued > > const &Sigma_imp_vec) const
embed single-particle quantities
#define C2PY_IGNORE
Definition defs.hpp:17
#define INSTANTIATE(Mesh)
embedding make_embedding(local_space const &C_space, bool use_atom_equivalences)
Make an embedding from the local space.
Definition embedding.cpp:85
block2_gf< Mesh, matrix_valued > make_block2_gf(Mesh const &mesh, gf_struct2_t const &gf_s)
Definition gf_supp.hpp:41
block2_gf< Mesh > decomposition_view(block2_gf< Mesh > const &g, gf_struct2_t const &stru)
Definition gf_supp.hpp:64
gf_struct_t get_struct(block_gf< Mesh > const &g)
Definition gf_supp.hpp:51
std::pair< one_body_elements_on_grid, embedding > make_embedding_with_clusters(one_body_elements_on_grid obe, std::vector< std::vector< long > > const &atom_partition)
std::ostream & operator<<(std::ostream &out, one_body_elements_on_grid const &)
Definition printing.cpp:73
one_body_elements_on_grid permute_local_space(std::vector< std::vector< long > > const &atom_partition, one_body_elements_on_grid const &obe)
embedding make_embedding_impl(local_space const &C_space, nda::array< std::vector< long >, 2 > const &block_decomposition, std::optional< std::vector< long > > const &atom_to_imp)
Definition embedding.cpp:39
nda::array< nda::matrix< dcomplex >, 2 > block2_matrix_t
Definition embedding.hpp:15
static constexpr auto r_all
Definition defs.hpp:40
bool all_equal(R const &r)
Determines if all elements in the given range are equal.
std::vector< nda::matrix< dcomplex > > block_matrix_t
Definition embedding.hpp:14
A one-body elements struct where all of the underlying data exists on a fixed momentum grid.