TRIQS/triqs_tprf 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
lattice_utility.cpp
1/*******************************************************************************
2 *
3 * TRIQS: a Toolbox for Research in Interacting Quantum Systems
4 *
5 * Copyright (C) 2019, The Simons Foundation
6 * Authors: H. U.R. Strand
7 *
8 * TRIQS is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software
10 * Foundation, either version 3 of the License, or (at your option) any later
11 * version.
12 *
13 * TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * TRIQS. If not, see <http://www.gnu.org/licenses/>.
20 *
21 ******************************************************************************/
22
23#include <nda/nda.hpp>
24#include <nda/linalg/eigh.hpp>
25
26#include "gw.hpp"
27#include "common.hpp"
28#include "../mpi.hpp"
29
30namespace triqs_tprf {
31
32 template<typename g_out_t, typename g_in_t, typename mesh_t>
33 g_out_t dlr_on_immesh_template(g_in_t g_c, mesh_t mesh) {
34 g_out_t g_out(mesh, g_c.target_shape());
35 for (auto p : mesh) {
36 g_out[p] = g_c(p);
37 }
38 return g_out;
39 }
40
41 g_w_t dlr_on_imfreq(g_Dc_cvt g_c, mesh::imfreq wmesh) {
42 return dlr_on_immesh_template<g_w_t, g_Dc_cvt, mesh::imfreq>(g_c, wmesh);
43 }
44
45 chi_w_t dlr_on_imfreq(chi_Dc_cvt chi_c, mesh::imfreq wmesh) {
46 return dlr_on_immesh_template<chi_w_t, chi_Dc_cvt, mesh::imfreq>(chi_c, wmesh);
47 }
48
49 g_t_t dlr_on_imtime(g_Dc_cvt g_c, mesh::imtime tmesh) {
50 return dlr_on_immesh_template<g_t_t, g_Dc_cvt, mesh::imtime>(g_c, tmesh);
51 }
52
53 chi_t_t dlr_on_imtime(chi_Dc_cvt chi_c, mesh::imtime tmesh) {
54 return dlr_on_immesh_template<chi_t_t, chi_Dc_cvt, mesh::imtime>(chi_c, tmesh);
55 }
56
57 std::tuple<chi_wk_t, chi_k_t> split_into_dynamic_wk_and_constant_k(chi_wk_cvt chi_wk) {
58
59 auto _ = all_t{};
60 auto wmesh = std::get<0>(chi_wk.mesh());
61 auto kmesh = std::get<1>(chi_wk.mesh());
62
63 chi_wk_t chi_dyn_wk(chi_wk.mesh(), chi_wk.target_shape());
64 chi_dyn_wk() = 0.0;
65 chi_k_t chi_const_k(kmesh, chi_wk.target_shape());
66 chi_const_k() = 0.0;
67
68 auto arr = mpi_view(kmesh);
69
70#pragma omp parallel for
71 for (unsigned int idx = 0; idx < arr.size(); idx++) {
72 auto &k = arr[idx];
73
74 auto chi_w = chi_wk[_, k];
75 auto tail = std::get<0>(fit_tail(chi_w));
76
77 for (auto [a, b, c, d] : chi_wk.target_indices()) chi_const_k[k](a, b, c, d) = tail(0, a, b, c, d);
78
79 for (auto w : wmesh) chi_dyn_wk[w, k] = chi_wk[w, k] - chi_const_k[k];
80 }
81
82 chi_dyn_wk = mpi::all_reduce(chi_dyn_wk);
83 chi_const_k = mpi::all_reduce(chi_const_k);
84 return {chi_dyn_wk, chi_const_k};
85 }
86
87 template<typename g_out_t, typename g_dyn_t, typename g_stat_t>
88 g_out_t add_dynamic_and_static_template(g_dyn_t g_dyn_wk, g_stat_t g_stat_k) {
89
90 g_out_t g_wk(g_dyn_wk.mesh(), g_dyn_wk.target_shape());
91 g_wk() = 0.0;
92
93 auto arr = mpi_view(g_wk.mesh());
94#pragma omp parallel for
95 for (int idx = 0; idx < arr.size(); idx++) {
96 auto &[w, k] = arr[idx];
97
98 //for (const auto &[a, b] : g_wk.target_indices()) { g_wk[w, k](a, b) = g_dyn_wk[w, k](a, b) + g_stat_k[k](a, b); }
99 g_wk[w, k] = g_dyn_wk[w, k] + g_stat_k[k];
100 }
101 g_wk = mpi::all_reduce(g_wk);
102 return g_wk;
103 }
104
105 g_fk_t add_dynamic_and_static(g_fk_t g_dyn_fk, e_k_t g_stat_k) {
106 return add_dynamic_and_static_template<g_fk_t, g_fk_t, e_k_t>(g_dyn_fk, g_stat_k);
107 }
108
109 chi_fk_t add_dynamic_and_static(chi_fk_t chi_dyn_fk, chi_k_t chi_stat_k) {
110 return add_dynamic_and_static_template<chi_fk_t, chi_fk_t, chi_k_t>(chi_dyn_fk, chi_stat_k);
111 }
112
113 g_wk_t add_dynamic_and_static(g_wk_t g_dyn_wk, e_k_t g_stat_k) {
114 return add_dynamic_and_static_template<g_wk_t, g_wk_t, e_k_t>(g_dyn_wk, g_stat_k);
115 }
116
117 chi_wk_t add_dynamic_and_static(chi_wk_t chi_dyn_wk, chi_k_t chi_stat_k) {
118 return add_dynamic_and_static_template<chi_wk_t, chi_wk_t, chi_k_t>(chi_dyn_wk, chi_stat_k);
119 }
120
121 g_Dwk_t add_dynamic_and_static(g_Dwk_t g_dyn_wk, e_k_t g_stat_k) {
122 return add_dynamic_and_static_template<g_Dwk_t, g_Dwk_t, e_k_t>(g_dyn_wk, g_stat_k);
123 }
124
125 chi_Dwk_t add_dynamic_and_static(chi_Dwk_t chi_dyn_wk, chi_k_t chi_stat_k) {
126 return add_dynamic_and_static_template<chi_Dwk_t, chi_Dwk_t, chi_k_t>(chi_dyn_wk, chi_stat_k);
127 }
128
129// g_fk_t add_dynamic_fk_and_static_k(g_fk_t g_dyn_fk, e_k_t g_stat_k) {
130//
131// g_fk_t g_fk(g_dyn_fk.mesh(), g_dyn_fk.target_shape());
132// g_fk() = 0.0;
133//
134// auto arr = mpi_view(g_fk.mesh());
135//#pragma omp parallel for
136// for (int idx = 0; idx < arr.size(); idx++) {
137// auto &[f, k] = arr[idx];
138//
139// //for (const auto &[a, b] : g_fk.target_indices()) { g_fk[f, k](a, b) = g_dyn_fk[f, k](a, b) + g_stat_k[k](a, b); }
140// g_fk[f, k] = g_dyn_fk[f, k] + g_stat_k[k];
141// }
142// g_fk = mpi::all_reduce(g_fk);
143// return g_fk;
144// }
145
146 double fermi(double e) {
147 if( e < 0 ) {
148 return 1. / (exp(e) + 1.);
149 } else {
150 double exp_me = exp(-e);
151 return exp_me / (1 + exp_me);
152 }
153 }
154
155 double bose(double e) {
156 return 1. / (exp(e) - 1.);
157 }
158} // namespace triqs_tprf