TRIQS/triqs_modest 3.3.0
Modular Electronic Structure Toolkit
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 //
52 template <typename Mesh> gf_struct2_t get_struct(block2_gf<Mesh, matrix_valued> const &g) {
53 auto res = nda::zeros<long>(g.size1(), g.size2());
54 for (auto [alpha, sigma] : res.indices()) res(alpha, sigma) = g(alpha, sigma).target_shape()[0];
55 return {g.block_names(), std::move(res)};
56 }
57
58 template <typename Mesh> block2_gf<Mesh> decomposition_view(block2_gf<Mesh> const &g, gf_struct2_t const &stru) {
59 if (g.size1() != 1) throw std::runtime_error{"decomposition_view: not implemented if g.size1 !=1"};
60 auto Gout = make_block2_gf(g(0, 0).mesh(), stru);
61 auto decomp = stru.dims(r_all, 0);
62 auto n_sigma = g.size2();
63 for (auto [alpha, Ralpha] : enumerated_sub_slices(decomp)) {
64 for (auto sigma : range(n_sigma)) { Gout(alpha, sigma).data() = g(0, sigma).data()(r_all, Ralpha, Ralpha); }
65 }
66 return Gout;
67 }
68
69 template <typename Mesh> block2_gf<Mesh> decomposition_view(block_gf<Mesh> const &g, gf_struct2_t const &stru) {
70 auto Gout = make_block2_gf(g[0].mesh(), stru);
71 auto decomp = stru.dims(r_all, 0);
72 auto n_sigma = g.size();
73 for (auto [alpha, Ralpha] : enumerated_sub_slices(decomp)) {
74 for (auto sigma : range(n_sigma)) { Gout(alpha, sigma).data() = g(0, sigma).data()(r_all, Ralpha, Ralpha); }
75 }
76 return Gout;
77 }
78
79 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) {
80 auto &in_mesh = g(0, 0).mesh();
81 auto beta = in_mesh.beta();
82 auto dlr_mesh = mesh::dlr_imfreq(beta, mesh::statistic_enum::Fermion, w_max, eps);
83 auto gdlr = make_block2_gf(dlr_mesh, get_struct(g));
84 auto n_alpha = g.size1();
85 auto n_sigma = g.size2();
86 for (auto a : range(n_alpha))
87 for (auto s : range(n_sigma))
88 for (auto [iw, wdlr] : enumerate(dlr_mesh)) {
89 if (wdlr.n > g(0, 0).mesh().last_index())
90 throw std::runtime_error{
91 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)};
92 // FIXME : fmt
93 gdlr(a, s).data()(iw, r_all, r_all) = g(a, s)(wdlr);
94 }
95 return gdlr;
96 }
97
98} // namespace triqs::gfs
gf_struct2_t get_struct(block2_gf< Mesh, matrix_valued > const &g)
Definition gf_supp.hpp:52
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:58
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:79
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