TRIQS/triqs_modest 3.3.0
Modular Electronic Structure Toolkit
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
31 // decomposition of C used by Sigma
32 std::vector<long> sigma_embed_decomp;
33
34 // decomposition of each impurity
35 std::vector<std::vector<long>> imp_decomps;
36
37 // names of the sigma (and tau) indices
38 std::vector<std::string> _sigma_names;
39
40 // names of the alpha indices
41 std::vector<std::string> alpha_names;
42
43 // stream
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:
54 struct imp_block_t {
55
56 long imp_idx = 0;
57 long gamma = 0;
58 long tau = 0;
59
60 imp_block_t() = default;
61 imp_block_t(long n_imp, long gamma, long tau) : imp_idx(n_imp), gamma(gamma), tau(tau) {}
62 friend bool operator==(imp_block_t const &, imp_block_t const &) = default;
63
64 // FIXME : merge with format_as
65 friend std::ostream &operator<<(std::ostream &out, imp_block_t const &x) {
66 return out << fmt::format("(imp_idx = {}, γ = {}, τ = {})", x.imp_idx, x.gamma, x.tau);
67 }
68 };
69
72 private:
73 // For each imp, for all (γ, τ), a list of all (α, σ) such that ψ[α, σ] ==(n_imp, γ, τ)
74 nda::array<imp_block_t, 2> psi; // ψ [α, σ] --> (n_imp, γ, τ)
75
76 // reverse ψ [n_imp] -> (γ, τ) -> [(α, σ)]
77 std::vector<nda::array<std::vector<std::array<long, 2>>, 2>> reverse_psi;
78
79 public:
90 embedding(std::vector<long> sigma_embed_decomposition, std::vector<std::vector<long>> imp_decompositions, nda::array<imp_block_t, 2> psi,
91 std::vector<std::string> sigma_names);
92
96 embedding() = default; // for h5_read
97
102
107 imp_block_t operator[](long alpha, long sigma) const { return psi(alpha, sigma); }
108
109 // HL: get_psi -> psi to match other classes?
111 [[nodiscard]] nda::array<imp_block_t, 2> get_psi() const { return psi; }
112
114 [[nodiscard]] long n_alpha() const { return psi.extent(0); }
115
117 [[nodiscard]] long n_sigma() const { return psi.extent(1); }
118
120 [[nodiscard]] long n_gamma(long imp_idx) const { return long(imp_decomps[imp_idx].size()); }
121
123 [[nodiscard]] long n_impurities() const { return long(imp_decomps.size()); }
124
126 [[nodiscard]] std::vector<std::string> sigma_names() const { return _sigma_names; };
127
129 [[nodiscard]] std::vector<long> imp_decomposition(long imp_idx) const { return imp_decomps[imp_idx]; };
130
133
135 std::vector<gf_struct_t> imp_block_shape() const;
136
138 std::string description(bool verbosity = false) const;
140
141 // ****************** Operation methods to change the embedding **********************
142
149 // --------------------------------------------------------
150 bool operator==(embedding const &other) const = default;
151 // --------------------------------------------------------
152
162 embedding drop(long imp_idx) const;
163
174 embedding replace(long imp_idx_to_remove, long imp_idx_to_replace_with) const;
175
185 embedding flip_spin(long alpha) const;
186
196 embedding flip_spin(std::vector<long> alphas) const;
197
202 embedding split(long imp_idx, std::function<bool(long)> p) const;
203
214 embedding split(long imp_idx, std::vector<long> const &block_list) const;
215
216 embedding split(long imp_idx, std::initializer_list<const char *> x) = delete;
217
219
220 //------------------------------------------------------------------------------------
221
223 template <typename Mesh> std::vector<std::pair<block_gf<Mesh, matrix_valued>, block_matrix_t>> make_zero_imp_self_energies(Mesh const &mesh) {
224
225 auto make_block_matrix = [](auto const &gf_struct) {
226 return gf_struct | stdv::transform([](auto &x) {
227 auto bl_size = x.second;
228 return nda::zeros<dcomplex>(bl_size, bl_size);
229 })
230 | tl::to<std::vector<nda::matrix<dcomplex>>>();
231 };
232
233 auto make_self_energy = [&](auto const &gf_struct) {
234 auto Sigma_static = make_block_matrix(gf_struct);
235 auto Sigma_dynamic = block_gf<Mesh, matrix_valued>{mesh, gf_struct};
236 return std::make_pair(Sigma_dynamic, Sigma_static);
237 };
238
239 return imp_block_shape() | stdv::transform(make_self_energy) | tl::to<std::vector>();
240 }
241 //------------------------------------------------------------------------------------
242
243 // ****************************************************
244 // Embedding Extract/Embed methods
245 // ****************************************************
252 //--------------------- Embed -----------------------------------------
253
265 template <typename Mesh> block2_gf<Mesh, matrix_valued> embed(std::vector<block_gf<Mesh, matrix_valued>> const &Sigma_imp_vec) const {
266 // Check that all meshes are the same for Sigma_imp_vec
267 if (not all_equal(Sigma_imp_vec | stdv::transform([](auto &&x) -> decltype(auto) { return x[0].mesh(); })))
268 throw std::runtime_error{"[embedding_desc::embed]: meshes of solvers are not all equal"};
269
270 auto const &mesh = Sigma_imp_vec[0][0].mesh();
271
272 // build the result
273 auto Sigma_embed = make_block2_gf(mesh, this->sigma_embed_block_shape());
274 for (auto &&[S, m] : zip(Sigma_embed, psi)) {
275 if (m.imp_idx == -1) continue;
276 S() = Sigma_imp_vec[m.imp_idx][m.gamma + n_gamma(m.imp_idx) * m.tau];
277 }
278 return Sigma_embed;
279 }
280
291 template <typename Mesh>
292 std::pair<block2_gf<Mesh, matrix_valued>, block2_matrix_t> embed(std::vector<block_gf<Mesh, matrix_valued>> const &Sigma_imp_vec,
293 std::vector<block_matrix_t> const &Sigma_imp_static_vec) const {
294 if (Sigma_imp_vec.size() != Sigma_imp_static_vec.size()) {
295 throw std::runtime_error(fmt::format("The lists of self-energies are not equal {} != {}", Sigma_imp_vec.size(), Sigma_imp_static_vec.size()));
296 }
297 return {this->embed(Sigma_imp_vec), this->embed(Sigma_imp_static_vec)};
298 }
299
301 block2_matrix_t embed(std::vector<block_matrix_t> const &Sigma_imp_static_vec) const;
302
303 //--------------------- Extract ---------------------------------------
304
312 template <typename Mesh> std::vector<block_gf<Mesh, matrix_valued>> extract(block2_gf<Mesh, matrix_valued> const &g_loc) const {
313
314 if (auto decomp = get_struct(g_loc).dims(r_all, 0) | tl::to<std::vector>(); decomp != this->sigma_embed_decomp) {
315 if (decomp.size() != 1) throw std::runtime_error{"extract: g should have decomp = sigma_embedding_decomp or [1]"};
316 return extract(decomposition_view(g_loc, this->sigma_embed_block_shape()));
317 }
318
319 // FIXME : check all meshes are the same
320 auto imp_gf_stru_list = imp_block_shape();
321 auto extract_one_imp = [&](long n_imp) {
322 auto gimp = block_gf{g_loc(0, 0).mesh(), imp_gf_stru_list[n_imp]};
323 auto const &rpsi = reverse_psi[n_imp];
324 for (auto [gamma, tau] : rpsi.indices()) {
325 auto [alpha, sigma] = rpsi(gamma, tau)[0];
326 gimp[gamma + n_gamma(n_imp) * tau].data() = g_loc(alpha, sigma).data();
327 }
328 return gimp;
329 };
330 return range(n_impurities()) | stdv::transform(extract_one_imp) | tl::to<std::vector>();
331 }
332
334 std::vector<block_matrix_t> extract(block2_matrix_t const &matrix_C) const;
335
337
345 //--------------------- Embed -----------------------------------------
346
348 std::vector<nda::array<dcomplex, 3>> embed_1p(std::vector<std::vector<nda::array<dcomplex, 3>>> const &Sigma_imp_vec) const;
349
351 nda::array<dcomplex, 5> embed_2p(std::vector<nda::array<dcomplex, 5>> const &pi_imp_vec) const;
352
354 nda::array<dcomplex, 4> embed_tensor(std::vector<nda::array<dcomplex, 4>> const &U_tensor_vec) const;
355
356 //--------------------- Extract ---------------------------------------
357
359 std::vector<std::vector<nda::array<dcomplex, 3>>> extract_1p(nda::array<dcomplex, 4> const &g_loc) const;
360
362 std::vector<nda::array<dcomplex, 5>> extract_2p(nda::array<dcomplex, 5> const &Pi_loc) const;
363
365 std::vector<nda::array<dcomplex, 4>> extract_tensor(nda::array<dcomplex, 4> const &U_tensor) const;
366
368 };
369
370 // ***************************** Free functions/factories *******************************************
371
389 embedding embedding_builder(std::vector<std::string> const &spin_names, nda::array<std::vector<long>, 2> const &block_decomposition,
390 std::vector<long> const &atom_to_imp);
402 embedding embedding_builder(std::vector<std::string> const &spin_names, std::vector<std::vector<long>> const &block_decomposition,
403 std::vector<long> const &atom_to_imp);
404
419 embedding make_embedding(local_space const &C_space, bool use_atom_equivalences = true, bool use_atom_decomp = false);
420
421 // -----------------------------------------------------------------------
436 inline std::pair<one_body_elements_on_grid, embedding> make_embedding_with_clusters(one_body_elements_on_grid obe,
437 std::vector<std::vector<long>> const &atom_partition) {
438 auto new_obe = permute_local_space(atom_partition, obe);
439 auto E = make_embedding(new_obe.C_space, false);
440 return {new_obe, E};
441 }
442
445 // ------------------------------------------------------------------------------
446
447#define INSTANTIATE(Mesh) \
448 template block2_gf<Mesh, matrix_valued> embedding::embed(std::vector<block_gf<Mesh, matrix_valued>> const &) const; \
449 template std::pair<block2_gf<Mesh, matrix_valued>, block2_matrix_t> embedding::embed(std::vector<block_gf<Mesh, matrix_valued>> const &, \
450 std::vector<block_matrix_t> const &) const; \
451 template std::vector<block_gf<Mesh, matrix_valued>> embedding::extract(block2_gf<Mesh, matrix_valued> const &) const; \
452 template std::vector<std::pair<block_gf<Mesh, matrix_valued>, block_matrix_t>> embedding::make_zero_imp_self_energies(Mesh const &);
453 INSTANTIATE(imfreq);
454 INSTANTIATE(refreq);
455 INSTANTIATE(dlr_imfreq);
456#undef INSTANTIATE
457
458} // namespace triqs::modest
The embedding class.
Definition embedding.hpp:29
std::vector< nda::array< dcomplex, 3 > > embed_1p(std::vector< std::vector< nda::array< dcomplex, 3 > > > const &Sigma_imp_vec) const
Embed single-particle quantities (CoQui).
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 .
nda::array< dcomplex, 4 > embed_tensor(std::vector< nda::array< dcomplex, 4 > > const &U_tensor_vec) const
Embed tensors (CoQui).
long n_impurities() const
Number of impurities.
long n_gamma(long imp_idx) const
Number of blocks in for the [imp_idx].
long n_sigma() const
Number of blocks in for the .
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 .
nda::array< dcomplex, 5 > embed_2p(std::vector< nda::array< dcomplex, 5 > > const &pi_imp_vec) const
Embed two-particle quantities (CoQui).
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
Extract single-particle quantities (TRIQS/ModEST).
long n_alpha() const
Number of blocks in for the .
std::vector< long > imp_decomposition(long imp_idx) const
The impurity decomposition.
std::vector< nda::array< dcomplex, 5 > > extract_2p(nda::array< dcomplex, 5 > const &Pi_loc) const
Extract two-particle quantities (CoQui).
std::vector< std::vector< nda::array< dcomplex, 3 > > > extract_1p(nda::array< dcomplex, 4 > const &g_loc) const
Extract single-particle quantities (CoQui).
std::string description(bool verbosity=false) const
Summarize the embedding object.
embedding drop(long imp_idx) const
Remove an impurity from the embedding table .
friend std::ostream & operator<<(std::ostream &out, embedding const &E)
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)
Zero initialize impurity self-energies.
embedding split(long imp_idx, std::function< bool(long)> p) const
Split impurity imp_idx.
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: .
std::vector< nda::array< dcomplex, 4 > > extract_tensor(nda::array< dcomplex, 4 > const &U_tensor) const
Extract tensors (CoQui).
block2_gf< Mesh, matrix_valued > embed(std::vector< block_gf< Mesh, matrix_valued > > const &Sigma_imp_vec) const
Embed single-particle quantities (TRIQS/ModEST).
std::pair< block2_gf< Mesh, matrix_valued >, block2_matrix_t > embed(std::vector< block_gf< Mesh, matrix_valued > > const &Sigma_imp_vec, std::vector< block_matrix_t > const &Sigma_imp_static_vec) const
Embed single-particle quantities (TRIQS/ModEST).
#define C2PY_IGNORE
Definition defs.hpp:17
#define INSTANTIATE(Mesh)
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)
Make an embedding for clusters of atoms.
embedding make_embedding(local_space const &C_space, bool use_atom_equivalences, bool use_atom_decomp)
Make an embedding from the local space.
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::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 embedding_builder(std::vector< std::string > const &spin_names, nda::array< std::vector< long >, 2 > const &block_decomposition, std::vector< long > const &atom_to_imp)
Definition embedding.cpp:41
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.