TRIQS/triqs_modest 3.3.0
Brillouin zone summation
Loading...
Searching...
No Matches
gf_supp.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 "defs.hpp"
9#include <triqs/mesh/dlr_imfreq.hpp>
10#include <triqs/mesh/utils.hpp>
11
12#include <fmt/ostream.h>
13
14// Detect if a formatter already exists for T
15// template <typename T, typename Char = char>
16// concept HasFmtFormatter = requires { fmt::formatter<T, Char>{}; };
17template <typename T>
18concept OstreamPrintable = requires(std::ostream &os, T const &t) {
19 { os << t } -> std::same_as<std::ostream &>;
20};
21template <typename M>
22 requires(triqs::gfs::Mesh<M> && OstreamPrintable<M>)
23struct fmt::formatter<M> : fmt::ostream_formatter {};
24
25namespace triqs::gfs {
26
27 // We need formatters for meshes
28
29 // Mainly notes for myself here.
30 // - use array<gf, 2> of indices alpha, sigma
31 // - have a new gf_struct_t --> gf_struct = array<pair<string, dim>, 2>
32 // then make_array_gf(mesh m, array<pair<string, dim>, 2> S)
33 // = map( [&m](auto && p){ return g(m, p); })(S);
34
35 struct gf_struct2_t {
36 std::vector<std::vector<std::string>> names;
37 nda::array<long, 2> dims;
38 };
39
40 // FIXME : to be simplified when block2_gf gets simplified ...
41 template <typename Mesh> block2_gf<Mesh, matrix_valued> make_block2_gf(Mesh const &mesh, gf_struct2_t const &gf_s) {
42 using g_t = gf<Mesh, matrix_valued>;
43 auto g = std::vector<std::vector<g_t>>(gf_s.names[0].size(), std::vector<g_t>(gf_s.names[1].size()));
44 for (auto [alpha, sigma] : gf_s.dims().indices()) {
45 auto dim = gf_s.dims()(alpha, sigma);
46 g[alpha][sigma] = g_t{mesh, {dim, dim}};
47 }
48 return {gf_s.names, std::move(g)};
49 }
50
51 template <typename Mesh> gf_struct_t get_struct(block_gf<Mesh> const &g) {
52 gf_struct_t result;
53 for (auto [name, size] : zip(g.block_names(), g.block_sizes())) result.emplace_back(name, size);
54 return result;
55 }
56
57 //
58 template <typename Mesh> gf_struct2_t get_struct(block2_gf<Mesh, matrix_valued> const &g) {
59 auto res = nda::zeros<long>(g.size1(), g.size2());
60 for (auto [alpha, sigma] : res.indices()) res(alpha, sigma) = g(alpha, sigma).target_shape()[0];
61 return {g.block_names(), std::move(res)};
62 }
63
64 template <typename Mesh> block2_gf<Mesh> decomposition_view(block2_gf<Mesh> const &g, gf_struct2_t const &stru) {
65 if (g.size1() != 1) throw std::runtime_error{"decomposition_view: not implemented if g.size1 !=1"};
66 auto Gout = make_block2_gf(g(0, 0).mesh(), stru);
67 auto decomp = stru.dims(r_all, 0);
68 auto n_sigma = g.size2();
69 for (auto [alpha, Ralpha] : enumerated_sub_slices(decomp)) {
70 for (auto sigma : range(n_sigma)) { Gout(alpha, sigma).data() = g(0, sigma).data()(r_all, Ralpha, Ralpha); }
71 }
72 return Gout;
73 }
74
75 template <typename Mesh> block2_gf<Mesh> decomposition_view(block_gf<Mesh> const &g, gf_struct2_t const &stru) {
76 auto Gout = make_block2_gf(g[0].mesh(), stru);
77 auto decomp = stru.dims(r_all, 0);
78 auto n_sigma = g.size();
79 for (auto [alpha, Ralpha] : enumerated_sub_slices(decomp)) {
80 for (auto sigma : range(n_sigma)) { Gout(alpha, sigma).data() = g(0, sigma).data()(r_all, Ralpha, Ralpha); }
81 }
82 return Gout;
83 }
84
85 inline block2_gf<mesh::dlr_imfreq, matrix_valued> make_block2_dlr_gf(block2_gf<mesh::imfreq, matrix_valued> const &g, double w_max, double eps) {
86 auto &in_mesh = g(0, 0).mesh();
87 auto beta = in_mesh.beta();
88 auto dlr_mesh = mesh::dlr_imfreq(beta, mesh::statistic_enum::Fermion, w_max, eps);
89 auto gdlr = make_block2_gf(dlr_mesh, get_struct(g));
90 auto n_alpha = g.size1();
91 auto n_sigma = g.size2();
92 for (auto a : range(n_alpha))
93 for (auto s : range(n_sigma))
94 for (auto [iw, wdlr] : enumerate(dlr_mesh)) {
95 if (wdlr.n > g(0, 0).mesh().last_index())
96 throw std::runtime_error{
97 fmt::format("make_block2_dlr_gf: the imfreq mesh \n{}\n is too short to extract the dlr_mesh \n{}\n", in_mesh, dlr_mesh)};
98 // FIXME : fmt
99 gdlr(a, s).data()(iw, r_all, r_all) = g(a, s)(wdlr);
100 }
101 return gdlr;
102 }
103
104} // namespace triqs::gfs
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
block2_gf< mesh::dlr_imfreq, matrix_valued > make_block2_dlr_gf(block2_gf< mesh::imfreq, matrix_valued > const &g, double w_max, double eps)
Definition gf_supp.hpp:85
static constexpr auto r_all
Definition defs.hpp:40
generator< std::pair< long, nda::range > > enumerated_sub_slices(auto sub_div)
nda::array< long, 2 > dims
Definition gf_supp.hpp:37
std::vector< std::vector< std::string > > names
Definition gf_supp.hpp:36