TRIQS/triqs_tprf 4.0.0
A TRIQS application
Loading...
Searching...
No Matches
dynamical_screened_interaction.cpp
1/*******************************************************************************
2 *
3 * TRIQS: a Toolbox for Research in Interacting Quantum Systems
4 *
5 * Copyright (C) 2022, The Simons Foundation
6 * Authors: H. U.R. Strand, Y. in 't Veld, N. Wentzell
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 "dynamical_screened_interaction.hpp"
27#include "common.hpp"
28#include "../mpi.hpp"
29
30namespace triqs_tprf {
31
32 enum SusceptibilityType { bubble, generalized };
33
34 template <SusceptibilityType susType, typename chi_t, typename v_t> auto screened_interaction_from_generic_susceptibility(chi_t &chi, v_t &V) {
35
36 auto const &[freqmesh, kmesh] = chi.mesh();
37
38 auto const &vkmesh = [&V]() -> auto & {
39 if constexpr (v_t::arity == 1)
40 return V.mesh();
41 else
42 return std::get<1>(V.mesh());
43 }();
44
45 if (kmesh != vkmesh) TRIQS_RUNTIME_ERROR << "dynamical_screened_interaction_W: k-space meshes are not the same\n";
46
47 auto W = make_gf(chi);
48 W() = 0.;
49 size_t nb = chi.target_shape()[0];
50
51 using scalar_t = typename chi_t::scalar_t;
52 auto I = nda::eye<scalar_t>(nb * nb);
53
54 // MPI and openMP parallell loop
55 auto arr = mpi_view(W.mesh());
56#pragma omp parallel for
57 for (unsigned int idx = 0; idx < arr.size(); idx++) {
58 auto &[w, k] = arr[idx];
59
60 array<scalar_t, 4> V_arr;
61 if constexpr (v_t::arity == 1)
62 V_arr = V[k];
63 else
64 V_arr = V[w, k];
65
66 array<scalar_t, 4> chi_arr{chi[w, k]};
67 array<scalar_t, 4> W_arr{nb, nb, nb, nb};
68
69 auto V_mat = make_matrix_view(group_indices_view(V_arr, idx_group<0, 1>, idx_group<3, 2>));
70 auto chi_mat = make_matrix_view(group_indices_view(chi_arr, idx_group<0, 1>, idx_group<3, 2>));
71 auto W_mat = make_matrix_view(group_indices_view(W_arr, idx_group<0, 1>, idx_group<3, 2>));
72
73 if constexpr (susType == false)
74 W_mat = V_mat * nda::linalg::inv(I - chi_mat * V_mat);
75 else
76 W_mat = V_mat * chi_mat * V_mat + V_mat;
77
78 W[w, k] = W_arr;
79 }
80
81 W = mpi::all_reduce(W);
82 return W;
83 }
84
85 template <typename chi_t>
86 auto dynamical_screened_interaction_W_opt_bubble(chi_t chi_wk, chi_k_cvt V_k) {
87
88 auto const &[wmesh, kmesh] = chi_wk.mesh();
89 auto const &v_kmesh = V_k.mesh();
90
91 if (kmesh != v_kmesh) TRIQS_RUNTIME_ERROR << "dynamical_screened_interaction_W: k-space meshes are not the same\n";
92
93 auto W_wk = make_gf(chi_wk);
94 W_wk() = 0.;
95 size_t nb = chi_wk.target_shape()[0];
96
97 using scalar_t = typename chi_wk_cvt::scalar_t;
98 auto I = nda::eye<scalar_t>(nb * nb);
99
100 // MPI and openMP parallell loop
101 auto arr = mpi_view(W_wk.mesh());
102
103#pragma omp parallel for
104 for (unsigned int idx = 0; idx < arr.size(); idx++) {
105 auto &[w, k] = arr[idx];
106
107 array<scalar_t, 4> denom{nb, nb, nb, nb};
108 array<scalar_t, 4> inv_denom{nb, nb, nb, nb};
109
110 auto denom_mat = make_matrix_view(group_indices_view(denom, idx_group<0, 1>, idx_group<3, 2>));
111 auto inv_denom_mat = make_matrix_view(group_indices_view(inv_denom, idx_group<0, 1>, idx_group<3, 2>));
112
113 denom_mat = I;
114
115 for (auto const &[a, b, c, d] : V_k.target_indices())
116 for( auto e : range(nb) )
117 for( auto f : range(nb) )
118 denom(a,b,c,d) -= chi_wk[w,k](a,b,e,f)*V_k[k](e,f,c,d);
119
120 inv_denom_mat = nda::linalg::inv(denom_mat);
121
122 for (auto const &[a, b, c, d] : V_k.target_indices())
123 for( auto e : range(nb) )
124 for( auto f : range(nb) )
125 W_wk[w, k](a,b,c,d) += V_k[k](a,b,e,f) * inv_denom(e,f,c,d);
126 }
127
128 W_wk = mpi::all_reduce(W_wk);
129 return W_wk;
130 }
131
132 chi_wk_t dynamical_screened_interaction_W(chi_wk_cvt PI_wk, chi_k_cvt V_k) {
133 return dynamical_screened_interaction_W_opt_bubble<chi_wk_cvt>(PI_wk, V_k);
134 }
135
136 chi_Dwk_t dynamical_screened_interaction_W(chi_Dwk_cvt PI_wk, chi_k_cvt V_k) {
137 return dynamical_screened_interaction_W_opt_bubble<chi_Dwk_cvt>(PI_wk, V_k);
138 }
139
140 chi_fk_t dynamical_screened_interaction_W(chi_fk_cvt PI_fk, chi_k_cvt V_k) {
141 return screened_interaction_from_generic_susceptibility<bubble>(PI_fk, V_k);
142 }
143
144 chi_wk_t dynamical_screened_interaction_W(chi_wk_cvt PI_wk, chi_wk_cvt V_wk) {
145 return screened_interaction_from_generic_susceptibility<bubble>(PI_wk, V_wk);
146 }
147
148 chi_Dwk_t dynamical_screened_interaction_W(chi_Dwk_cvt PI_wk, chi_Dwk_cvt V_wk) {
149 return screened_interaction_from_generic_susceptibility<bubble>(PI_wk, V_wk);
150 }
151
152 chi_fk_t dynamical_screened_interaction_W(chi_fk_cvt PI_fk, chi_fk_cvt V_fk) {
153 return screened_interaction_from_generic_susceptibility<bubble>(PI_fk, V_fk);
154 }
155
156 chi_wk_t dynamical_screened_interaction_W_from_generalized_susceptibility(chi_wk_cvt chi_wk, chi_k_cvt V_k) {
157 return screened_interaction_from_generic_susceptibility<generalized>(chi_wk, V_k);
158 }
159
160 chi_Dwk_t dynamical_screened_interaction_W_from_generalized_susceptibility(chi_Dwk_cvt chi_wk, chi_k_cvt V_k) {
161 return screened_interaction_from_generic_susceptibility<generalized>(chi_wk, V_k);
162 }
163
164 chi_fk_t dynamical_screened_interaction_W_from_generalized_susceptibility(chi_fk_cvt chi_fk, chi_k_cvt V_k) {
165 return screened_interaction_from_generic_susceptibility<generalized>(chi_fk, V_k);
166 }
167
168 chi_wk_t dynamical_screened_interaction_W_from_generalized_susceptibility(chi_wk_cvt chi_wk, chi_wk_cvt V_wk) {
169 return screened_interaction_from_generic_susceptibility<generalized>(chi_wk, V_wk);
170 }
171
172 chi_Dwk_t dynamical_screened_interaction_W_from_generalized_susceptibility(chi_Dwk_cvt chi_wk, chi_Dwk_cvt V_wk) {
173 return screened_interaction_from_generic_susceptibility<generalized>(chi_wk, V_wk);
174 }
175
176 chi_fk_t dynamical_screened_interaction_W_from_generalized_susceptibility(chi_fk_cvt chi_fk, chi_fk_cvt V_fk) {
177 return screened_interaction_from_generic_susceptibility<generalized>(chi_fk, V_fk);
178 }
179
180} // namespace triqs_tprf