forked from MHarland/ClusterDMFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdmft.py
355 lines (317 loc) · 15.8 KB
/
cdmft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from itertools import izip
from matplotlib import pyplot as plt, cm
from matplotlib.backends.backend_pdf import PdfPages
from numpy import ndarray, identity, array, zeros
from pytriqs.utility import mpi
from pytriqs.gf.local import BlockGf, inverse, GfImFreq, GfImTime, LegendreToMatsubara, TailGf
from pytriqs.version import version
from pytriqs.archive import HDFArchive
from pytriqs.plot.mpl_interface import oplot
from pytriqs.applications.impurity_solvers.cthyb import SolverCore as Solver
from pytriqs.random_generator import random_generator_names_list
from time import time
from .archive import ArchiveConnected
from .dmft_struct import DMFTObjects
from .schemes import Scheme
from .periodization.periodization import ClusterPeriodization
from .plot import plot_from_archive, plot_of_loops_from_archive, checksym_plot, checktransf_plot, plot_ln_abs #move to dmftobjects?
from .loop_parameters import CleanLoopParameters
from .process_g import addExtField
from .transformation.sites import ClustersiteTransformation
from .transformation.nambu import NambuTransformation
from .utility import get_site_nrs, get_dim, get_n_sites, Reporter
class CDmft(ArchiveConnected):
"""
TODO
Transformation has to be unitary(?)
"""
_version = '1.10'
def __init__(self, **kwargs):
self.parameters = dict()
for i in kwargs:
self.parameters[i] = kwargs[i]
super(CDmft, self).__init__(**self.parameters)
if mpi.is_master_node():
archive = HDFArchive(self.parameters['archive'], 'a')
archive['CDmft_version'] = CDmft._version
del archive
def set_parameters(self, parameters):
"""parameters: dict"""
for key, val in parameters.items():
self.parameters[key] = val
def get_parameter(self, key):
return self.parameters[key]
def get_parameters(self):
return self.parameters
def print_parameters(self):
if mpi.is_master_node():
print 'Parameters:'
print self.parameters
print
def run_dmft_loops(self, n_dmft_loops = 1):
"""runs the DMFT calculation"""
clp = p = CleanLoopParameters(self.get_parameters())
report = Reporter(**clp)
report('Parameters:', clp)
scheme = Scheme(**clp)
dmft = DMFTObjects(**clp)
raw_dmft = DMFTObjects(**clp)
g_0_c_iw, g_c_iw, sigma_c_iw, dmu = dmft.get_dmft_objs() # ref, ref, ref, value
report('Initializing...')
if clp['nambu']:
transf = NambuTransformation(**clp)
scheme.set_pretransf(transf.pretransformation(), transf.pretransformation_inverse())
raw_transf = NambuTransformation(**clp)
else:
transf = ClustersiteTransformation(g_loc = scheme.g_local(sigma_c_iw, dmu), **clp)
clp.update({'g_transf_struct': transf.get_g_struct()})
raw_transf = ClustersiteTransformation(**clp)
transf.set_hamiltonian(**clp)
report('Transformation ready')
report('New basis:', transf.get_g_struct())
impurity = Solver(beta = clp['beta'], gf_struct = dict(transf.get_g_struct()),
n_tau = clp['n_tau'], n_iw = clp['n_iw'], n_l = clp['n_legendre'])
impurity.Delta_tau.name = '$\\tilde{\\Delta}_c$'
rnames = random_generator_names_list()
report('H = ', transf.hamiltonian)
report('Impurity solver ready')
report('')
for loop_nr in range(self.next_loop(), self.next_loop() + n_dmft_loops):
report('DMFT-loop nr. %s'%loop_nr)
if mpi.is_master_node(): duration = time()
report('Calculating dmu...')
dmft.find_dmu(scheme, **clp)
g_0_c_iw, g_c_iw, sigma_c_iw, dmu = dmft.get_dmft_objs()
report('dmu = %s'%dmu)
report('Calculating local Greenfunction...')
g_c_iw << scheme.g_local(sigma_c_iw, dmu)
g_c_iw << addExtField(g_c_iw, p['ext_field'])
if mpi.is_master_node() and p['verbosity'] > 1: checksym_plot(g_c_iw, p['archive'][0:-3] + 'Gchecksym' + str(loop_nr) + '.pdf')
report('Calculating Weiss-field...')
g_0_c_iw << inverse(inverse(g_c_iw) + sigma_c_iw)
dmft.make_g_0_iw_with_delta_tau_real()
report('Changing basis...')
transf.set_dmft_objs(*dmft.get_dmft_objs())
if mpi.is_master_node() and p['verbosity'] > 1:
checktransf_plot(transf.get_g_iw(), p['archive'][0:-3] + 'Gchecktransf' + str(loop_nr) + '.pdf')
checktransf_plot(g_0_c_iw, p['archive'][0:-3] + 'Gweisscheck' + str(loop_nr) + '.pdf')
checksym_plot(inverse(transf.g_0_iw), p['archive'][0:-3] + 'invGweisscheckconst' + str(loop_nr) + '.pdf')
#checksym_plot(inverse(transf.get_g_iw()), p['archive'][0:-3] + 'invGsymcheckconst' + str(loop_nr) + '.pdf')
if not clp['random_name']: clp.update({'random_name': rnames[int((loop_nr + mpi.rank) % len(rnames))]}) # TODO move
if not clp['random_seed']: clp.update({'random_seed': 862379 * mpi.rank + 12563 * self.next_loop()})
impurity.G0_iw << transf.get_g_0_iw()
report('Solving impurity problem...')
mpi.barrier()
impurity.solve(h_int = transf.get_hamiltonian(), **clp.get_cthyb_parameters())
if mpi.is_master_node() and p['verbosity'] > 1:
checksym_plot(inverse(impurity.G0_iw), p['archive'][0:-3] + 'invGweisscheckconstsolver' + str(loop_nr) + '.pdf')
report('Postprocessing measurements...')
if clp['measure_g_l']:
for ind, g in transf.get_g_iw(): g << LegendreToMatsubara(impurity.G_l[ind])
else:
for ind, g in transf.get_g_iw(): g.set_from_fourier(impurity.G_tau[ind])
raw_transf.set_dmft_objs(transf.get_g_0_iw(),
transf.get_g_iw(),
inverse(transf.get_g_0_iw()) - inverse(transf.get_g_iw()))
if clp['measure_g_tau'] and clp['fit_tail']:
for ind, g in transf.get_g_iw():
for tind in transf.get_g_struct():
if tind[0] == ind: block_inds = tind[1]
fixed_moments = TailGf(len(block_inds), len(block_inds), 1, 1)
fixed_moments[1] = identity(len(block_inds))
g.fit_tail(fixed_moments, 3, clp['tail_start'], clp['n_iw'] - 1)
if mpi.is_master_node() and p['verbosity'] > 1: checksym_plot(inverse(transf.get_g_iw()), p['archive'][0:-3] + 'invGsymcheckconstsolver' + str(loop_nr) + '.pdf')
report('Backtransforming...')
transf.set_sigma_iw(inverse(transf.get_g_0_iw()) - inverse(transf.get_g_iw()))
dmft.set_dmft_objs(*transf.get_backtransformed_dmft_objs())
dmft.set_dmu(dmu)
raw_dmft.set_dmft_objs(*raw_transf.get_backtransformed_dmft_objs())
raw_dmft.set_dmu(dmu)
if clp['mix']: dmft.mix()
if clp['impose_paramagnetism']: dmft.paramagnetic()
if clp['impose_afm']: dmft.afm()
if clp['site_symmetries']: dmft.site_symmetric(clp['site_symmetries'])
density = scheme.apply_pretransf_inv(dmft.get_g_iw(), True).total_density()
report('Saving results...')
if mpi.is_master_node():
a = HDFArchive(p['archive'], 'a')
if not a.is_group('results'):
a.create_group('results')
a_r = a['results']
a_r.create_group(str(loop_nr))
a_l = a_r[str(loop_nr)]
a_l['g_c_iw'] = dmft.get_g_iw()
a_l['g_c_iw_raw'] = raw_dmft.get_g_iw()
a_l['g_transf_iw'] = transf.get_g_iw()
a_l['g_transf_iw_raw'] = raw_transf.get_g_iw()
a_l['sigma_c_iw'] = dmft.get_sigma_iw()
a_l['sigma_c_iw_raw'] = raw_dmft.get_sigma_iw()
a_l['sigma_transf_iw'] = transf.get_sigma_iw()
a_l['sigma_transf_iw_raw'] = raw_transf.get_sigma_iw()
a_l['g_0_c_iw'] = dmft.get_g_0_iw()
a_l['g_0_c_iw_raw'] = raw_dmft.get_g_0_iw()
a_l['g_0_transf_iw'] = transf.get_g_0_iw()
a_l['g_0_transf_iw_raw'] = raw_transf.get_g_0_iw()
a_l['dmu'] = dmft.get_dmu()
a_l['density'] = density
a_l['loop_time'] = {'seconds': time() - duration,
'hours': (time() - duration)/3600.,
'days': (time() - duration)/3600./24.}
a_l['n_cpu'] = mpi.size
a_l['cdmft_code_version'] = CDmft._version
clp_dict = dict()
clp_dict.update(clp)
a_l['parameters'] = clp_dict
a_l['triqs_code_version'] = version
a_l['delta_transf_tau'] = impurity.Delta_tau
if clp['measure_g_l']: a_l['g_transf_l'] = impurity.G_l
if clp['measure_g_tau']: a_l['g_transf_tau'] = impurity.G_tau
a_l['sign'] = impurity.average_sign
if clp['measure_density_matrix']: a_l['density_matrix'] = impurity.density_matrix
a_l['g_atomic_tau'] = impurity.atomic_gf
a_l['h_loc_diagonalization'] = impurity.h_loc_diagonalization
if a_r.is_data('n_dmft_loops'):
a_r['n_dmft_loops'] += 1
else:
a_r['n_dmft_loops'] = 1
del a_l, a_r, a
report('Loop done')
report('')
mpi.barrier()
def export_results(self, filename = False, prange = (0, 60)):
"""
routine to export easily a selection of results into a pdf file
"""
if mpi.is_master_node(): self._export_results(filename = filename, prange = prange)
def _export_results(self, filename = False, prange = (0, 60)):
p = self.parameters = self.load('parameters')
p['archive'] = self.archive
n_sites = len(p['blockstates'])
sites = p['blockstates']
blocks = p['blocks']
transf_blocks = p['g_transf_struct']
if not filename: filename = p['archive'][0:-3] + '.pdf'
pp = PdfPages(filename)
functions = ['g_c_iw', 'sigma_c_iw']
for f in functions:
for i in sites:
plot_from_archive(p['archive'], f, [-1], indices = [(0, i)], x_window = prange, marker = '+', blocks = [blocks[0]])
pp.savefig()
plt.close()
markers = ['o', '+', 'x', '^', '>', 'v', '<', '.', 'd', 'h']
for i in sites:
m = markers[i % len(markers)]
plot_from_archive(p['archive'], 'g_c_iw', [-1], indices = [(i, i)], x_window = prange, marker = m, blocks = [blocks[0]])
pp.savefig()
plt.close()
for m in ['R', 'I']:
for b, marker in zip(blocks, ['x','o']):
plot_from_archive(p['archive'], 'g_c_iw', range(-min(self.next_loop(), 3), 0), blocks = [blocks[0]], RI = m, x_window = prange, marker = marker)
pp.savefig()
plt.close()
a = HDFArchive(p['archive'], 'r')
if a['results'][str(self.last_loop())].is_group('g_transf_l'):
plot_ln_abs(self.load('g_transf_l'))
pp.savefig()
plt.close()
del a
plot_from_archive(p['archive'], 'delta_transf_tau', range(-min(self.next_loop(), 5), 0), blocks = [transf_blocks[0][0]])
pp.savefig()
plt.close()
plot_from_archive(p['archive'], 'delta_transf_tau', blocks = dict(p['g_transf_struct']).keys())
pp.savefig()
plt.close()
inds = dict(p['g_transf_struct'])
a = HDFArchive(self.parameters['archive'], 'r')
n_graphs = 0
for spin, orb_list in inds.items():
n_graphs += len(orb_list)
for f in ['g_transf_iw', 'sigma_transf_iw']:
for m in ['R', 'I']:
c = 0
for ind in inds:
for orb in inds[ind]:
plot_from_archive(p['archive'], f+'_raw', indices = [(orb, orb)], blocks = [str(ind)], RI = m, x_window = prange, marker = 'x', color = cm.jet(c/float(n_graphs-1)))
plot_from_archive(p['archive'], f, indices = [(orb, orb)], blocks = [str(ind)], RI = m, x_window = prange, marker = '+', color = cm.jet(c/float(n_graphs-1)))
c += 1
pp.savefig()
plt.close()
del a
for f in ['g_c_iw_raw', 'sigma_c_iw_raw']:
for m in ['R', 'I']:
c = 0
for i in range(n_sites):
for j in range(n_sites):
for k, b in enumerate(blocks):
mark = ['x', '+'][k%2]
plot_from_archive(p['archive'], f, indices = [(i, j)], blocks = [b], RI = m, x_window = prange, marker = mark, color = cm.jet(int(c)/float(-1+n_sites**len(blocks))))
c += .5
pp.savefig()
plt.close()
for m in ['R', 'I']:
plot_from_archive(p['archive'], 'g_c_iw', range(-min(self.next_loop(), 5), 0), RI = m, x_window = prange, marker = '+', blocks = [blocks[0]])
pp.savefig()
plt.close()
for m in ['R', 'I']:
plot_from_archive(p['archive'], 'sigma_c_iw', range(-min(self.next_loop(), 5), 0), RI = m, x_window = prange, marker = '+', blocks = [blocks[0]])
pp.savefig()
plt.close()
functions = ['g_c_iw', 'sigma_c_iw']
for f in functions:
plot_of_loops_from_archive(p['archive'], f, indices = [(0, i) for i in sites], marker = '+', blocks = [blocks[0]])
pp.savefig()
plt.close()
functions = ['density', 'sign', 'dmu']
for f in functions:
plot_of_loops_from_archive(p['archive'], f, marker = '+', blocks = [blocks[0]])
pp.savefig()
plt.close()
"""
arch_text = self.archive_content(group = ['results', str(self.last_loop())], dont_exp = ['bz_grid', 'bz_weights', 'eps', 'rbz_grid'])
line = 1
page_text = str()
for nr, char in enumerate(arch_text, 2):
page_text += char
if '\n' in arch_text[(nr - 2):nr]:
line += 1
if line % 80 == 0 or nr == len(arch_text) - 1:
plt.figtext(0.05, 0.95, page_text, fontsize = 8, verticalalignment = 'top')
pp.savefig()
plt.close()
page_text = str()
line = 1
"""
pp.close()
print filename, 'ready'
def plot(self, function, *args, **kwargs):
"""
plot several loops and/or indices in one figure
possible options for function are:
'delta_transf_tau', 'g_c_iw', 'g_c_tau', 'sigma_c_iw', 'g_transf_l'
further keywordarguments go into oplot
"""
if 'filename' in kwargs.keys():
filename = kwargs['filename']
else:
filename = function + '.pdf'
plot_from_archive(self.parameters['archive'], function, *args, **kwargs)
plt.savefig(filename)
plt.close()
print filename, ' ready'
def plot_of_loops(self, function, *args, **kwargs):
"""
Options for function are: all functions of iw, mu and density.
matsubara_freqs: list of int
blocks: list of str
indices: list of 2-tuple
RI: 'R', 'I'
further keywordarguments go to plot of matplotlib.pyplot
"""
if 'filename' in kwargs.keys():
filename = kwargs['filename']
else:
filename = function + '.pdf'
plot_of_loops_from_archive(self.parameters['archive'], function, *args, **kwargs)
plt.savefig(filename)
plt.close()
print filename, ' ready'