forked from QcmPlab/CDMFT-LANC-ED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathED_FIT_CHI2.f90
646 lines (618 loc) · 24 KB
/
ED_FIT_CHI2.f90
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
MODULE ED_FIT_CHI2
USE SF_CONSTANTS
USE SF_OPTIMIZE, only:fmin_cg,fmin_cgminimize
USE SF_LINALG, only:eye,zeye,inv,inv_her,operator(.x.)
USE SF_IOTOOLS, only:reg,free_unit,txtfy
USE SF_ARRAYS, only:arange
USE SF_MISC, only:assert_shape
USE ED_INPUT_VARS
USE ED_VARS_GLOBAL
USE ED_AUX_FUNX
USE ED_BATH
USE ED_BATH_FUNCTIONS
implicit none
private
interface ed_chi2_fitgf
module procedure chi2_fitgf_generic_normal
end interface ed_chi2_fitgf
public :: ed_chi2_fitgf
integer :: Ldelta
complex(8),dimension(:,:),allocatable :: Gdelta
complex(8),dimension(:,:),allocatable :: Fdelta
real(8),dimension(:),allocatable :: Xdelta,Wdelta
integer :: totNorb,totNspin,totNlso
integer,dimension(:),allocatable :: getIorb,getJorb,getIspin,getJspin,getIlat,getJlat
integer :: Orb_indx,Spin_indx,Spin_mask
integer,dimension(:),allocatable :: Nlambdas
!location of the maximum of the chisquare over Nlso.
integer :: maxchi_loc
!
type nsymm_vector
real(8),dimension(:),allocatable :: element
end type nsymm_vector
!
contains
!##################################################################
! THE CALCULATION OF THE \chi^2 FUNCTIONS USE PROCEDURES FURTHER
! BELOW TO EVALUATE INDEPENDENTLY THE ANDERSON MODEL:
! - DELTA,
! -\GRAD DELTA
! - G0
! THE LATTER ARE ADAPTED FROM THE PROCEDURES:
! DELTA_BATH_MATS
! GRAD_DELTA_BATH_MATS
! G0 BATH_MATS
! FOR, YOU NEED TO DECOMPOSE THE a INPUT ARRAY INTO ELEMENTS.
!##################################################################
!+----------------------------------------------------------------------+
!PURPOSE : Chi^2 fit of the G0/Delta
!+----------------------------------------------------------------------+
subroutine chi2_fitgf_generic_normal(fg,bath)
complex(8),dimension(:,:,:,:,:,:,:) :: fg ![Nlat][Nlat][Nspin][Nspin][Norb][Norb][Niw]
real(8),dimension(:) :: bath
!integer,optional :: ispin,iorb
!integer :: ispin_
!
!ispin_=1;if(present(ispin))ispin_=ispin
!
call assert_shape(fg,[Nlat,Nlat,Nspin,Nspin,Norb,Norb,size(fg,7)],"chi2_fitgf_generic_normal","fg")
allocate(Nlambdas(Nbath))
!
select case(cg_method)
case default
stop "ED Error: cg_method > 1"
case (0)
if(ed_verbose>2)write(LOGfile,"(A,I1,A,A)")"\Chi2 fit with CG-nr and CG-weight: ",cg_weight," on: ",cg_scheme
case (1)
if(ed_verbose>2)write(LOGfile,"(A,I1,A,A)")"\Chi2 fit with CG-minimize and CG-weight: ",cg_weight," on: ",cg_scheme
end select
!
call chi2_fitgf_replica(fg,bath)
!
!set trim_state_list to true after the first fit has been done: this
!marks the ends of the cycle of the 1st DMFT loop.
trim_state_list=.true.
deallocate(Nlambdas)
end subroutine chi2_fitgf_generic_normal
!+-------------------------------------------------------------+
!PURPOSE : Chi^2 interface for
!+-------------------------------------------------------------+
subroutine chi2_fitgf_replica(fg,bath_)
complex(8),dimension(:,:,:,:,:,:,:) :: fg ![Nlat][Nlat][Nspin][Nspin][Norb][Norb][Lmats]
logical(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb) :: Hmask
real(8),dimension(:),intent(inout) :: bath_
real(8),dimension(:),allocatable :: array_bath
integer :: i,j,ilat,jlat,iorb,jorb,ispin,jspin,ibath,io,jo
integer :: iter,stride,counter,Asize
real(8) :: chi
logical :: check
character(len=256) :: suffix
integer :: unit
!
check= check_bath_dimension(bath_)
if(.not.check)stop "chi2_fitgf_replica error: wrong bath dimensions"
!
call allocate_dmft_bath()
call set_dmft_bath(bath_)
allocate(array_bath(size(bath_)-Nbath))
counter=0
do ibath=1,Nbath
counter=counter+1
Nlambdas(ibath)=bath_(counter)
enddo
array_bath=bath_(Nbath+1:size(bath_))
!
Ldelta = Lfit ; if(Ldelta>size(fg,7))Ldelta=size(fg,7)
!
Hmask=mask_hloc(impHloc,wdiag=.true.,uplo=.true.)
totNlso=count(Hmask)
!
allocate(getIlat(totNlso) ,getJlat(totNlso))
allocate(getIspin(totNlso),getJspin(totNlso))
allocate(getIorb(totNlso) ,getJorb(totNlso))
counter=0
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
if (Hmask(ilat,jlat,ispin,jspin,iorb,jorb))then
counter=counter+1
getIlat(counter) = ilat
getIspin(counter) = ispin
getIorb(counter) = iorb
getJlat(counter) = jlat
getJspin(counter) = jspin
getJorb(counter) = jorb
endif
enddo
enddo
enddo
enddo
enddo
enddo
!
allocate(Gdelta(totNlso,Ldelta))
allocate(Xdelta(Ldelta))
allocate(Wdelta(Ldelta))
!
Xdelta = pi/beta*(2*arange(1,Ldelta)-1)
!
select case(cg_weight)
case default
Wdelta=1d0
case(2)
Wdelta=1d0*arange(1,Ldelta)
case(3)
Wdelta=Xdelta
end select
!
write(LOGfile,*)" fitted functions",totNlso
do i=1,totNlso
Gdelta(i,1:Ldelta) = fg(getIlat(i),getJlat(i),getIspin(i),getJspin(i),getIorb(i),getJorb(i),1:Ldelta)
enddo
!
select case(cg_method) !0=NR-CG[default]; 1=CG-MINIMIZE
case default
if(cg_grad==0)then
#if __GNUC__ >= 8
write(LOGfile,*)" Using analytic gradient"
select case (cg_scheme)
case ("weiss")
call fmin_cg(array_bath,&
chi2_weiss_replica,&
grad_chi2_weiss_replica,&
iter,&
chi, &
itmax=cg_niter,&
ftol=cg_Ftol, &
istop=cg_stop, &
iverbose=(ed_verbose>3))
case ("delta")
call fmin_cg(array_bath,&
chi2_delta_replica,&
grad_chi2_delta_replica,&
iter,&
chi, &
itmax=cg_niter,&
ftol=cg_Ftol, &
istop=cg_stop, &
iverbose=(ed_verbose>3))
case default
stop "chi2_fitgf_replica error: cg_scheme != [weiss,delta]"
end select
#else
STOP "analytic gradient not supported for gfortran < 8"
#endif
else
write(LOGfile,*)" Using numerical gradient"
select case (cg_scheme)
case ("weiss")
call fmin_cg(array_bath,chi2_weiss_replica,&
iter,&
chi, &
itmax=cg_niter,&
ftol=cg_Ftol, &
istop=cg_stop, &
iverbose=(ed_verbose>3))
case ("delta")
call fmin_cg(array_bath,chi2_delta_replica,&
iter,&
chi, &
itmax=cg_niter,&
ftol=cg_Ftol, &
istop=cg_stop, &
iverbose=(ed_verbose>3))
case default
stop "chi2_fitgf_replica error: cg_scheme != [weiss,delta]"
end select
endif
!
case (1)
select case (cg_scheme)
case ("weiss")
call fmin_cgminimize(array_bath,chi2_weiss_replica,&
iter,&
chi, &
itmax=cg_niter,&
ftol=cg_Ftol, &
new_version=cg_minimize_ver,&
hh_par=cg_minimize_hh, &
iverbose=(ed_verbose>3))
case ("delta")
call fmin_cgminimize(array_bath,chi2_delta_replica,&
iter,&
chi, &
itmax=cg_niter,&
ftol=cg_Ftol, &
new_version=cg_minimize_ver,&
hh_par=cg_minimize_hh, &
iverbose=(ed_verbose>3))
case default
stop "chi2_fitgf_replica error: cg_scheme != [weiss,delta]"
end select
!
end select
!
write(LOGfile,"(A,ES18.9,A,I5,A)")"chi^2|iter"//reg(ed_file_suffix)//'= ',chi," | ",iter," <-- All Orbs, All Spins"
!
suffix="_ALLorb_ALLspins"//reg(ed_file_suffix)
unit=free_unit()
open(unit,file="chi2fit_results"//reg(suffix)//".ed",position="append")
write(unit,"(ES18.9,1x,I5)") chi,iter
close(unit)
!
bath_(Nbath+1:size(bath_))=array_bath
call set_dmft_bath(bath_) ! *** bath_ --> dmft_bath *** (per write fit result)
call write_dmft_bath(LOGfile)
call save_dmft_bath()
!
call write_fit_result()
!
call get_dmft_bath(bath_) ! *** dmft_bath --> bath_ *** (bath in output)
call deallocate_dmft_bath()
deallocate(Gdelta,Xdelta,Wdelta)
deallocate(getIlat,getJlat)
deallocate(getIspin,getJspin)
deallocate(getIorb,getJorb)
deallocate(array_bath)
!
contains
!
subroutine write_fit_result()
complex(8) :: fgand(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta)
integer :: i,j,s,l,ilat,jlat,iorb,jorb,ispin,jspin
real(8) :: w
if(cg_scheme=='weiss')then
fgand(:,:,:,:,:,:,:) = g0and_bath(xi*Xdelta(:))
else
fgand(:,:,:,:,:,:,:) = delta_bath(xi*Xdelta(:))
endif
!
do l=1,totNlso
ilat = getIlat(l)
jlat = getJlat(l)
iorb = getIorb(l)
jorb = getJorb(l)
ispin = getIspin(l)
jspin = getJspin(l)
suffix="_i"//reg(txtfy(ilat))//&
"_j"//reg(txtfy(jlat))//&
"_l"//reg(txtfy(iorb))//&
"_m"//reg(txtfy(jorb))//&
"_s"//reg(txtfy(ispin))//&
"_r"//reg(txtfy(jspin))//reg(ed_file_suffix)
unit=free_unit()
if(cg_scheme=='weiss')then
open(unit,file="fit_weiss"//reg(suffix)//".ed")
else
open(unit,file="fit_delta"//reg(suffix)//".ed")
endif
do i=1,Ldelta
w = Xdelta(i)
write(unit,"(5F24.15)")Xdelta(i),&
dimag(fg(ilat,jlat,ispin,jspin,iorb,jorb,i)),dimag(fgand(ilat,jlat,ispin,jspin,iorb,jorb,i)),&
dreal(fg(ilat,jlat,ispin,jspin,iorb,jorb,i)),dreal(fgand(ilat,jlat,ispin,jspin,iorb,jorb,i))
enddo
close(unit)
enddo
end subroutine write_fit_result
!
end subroutine chi2_fitgf_replica
!##################################################################
! THESE PROCEDURES EVALUATES THE \chi^2 FUNCTIONS TO MINIMIZE.
!##################################################################
!+-------------------------------------------------------------+
!PURPOSE: Evaluate the \chi^2 distance of \Delta_Anderson function.
!+-------------------------------------------------------------+
function chi2_delta_replica(a) result(chi2)
real(8),dimension(:) :: a
real(8) :: chi2
real(8),dimension(totNlso) :: chi2_lso
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: Delta
real(8),dimension(Ldelta) :: Ctmp
integer :: i,l,ilat,jlat,iorb,jorb,ispin,jspin
!
Delta = delta_replica(a)
!
do l=1,totNlso
ilat = getIlat(l)
jlat = getJlat(l)
iorb = getIorb(l)
jorb = getJorb(l)
ispin = getIspin(l)
jspin = getJspin(l)
!
Ctmp = abs(Gdelta(l,:)-Delta(ilat,jlat,ispin,jspin,iorb,jorb,:))
chi2_lso(l) = sum( Ctmp**cg_pow/Wdelta )
enddo
!
chi2=sum(chi2_lso)
chi2=chi2/Ldelta
!
end function chi2_delta_replica
#if __GNUC__ >= 8
!+-------------------------------------------------------------+
!PURPOSE: Evaluate the gradient \Grad\chi^2 of
! \Delta_Anderson function.
!+-------------------------------------------------------------+
function grad_chi2_delta_replica(a) result(dchi2)
real(8),dimension(:) :: a
real(8),dimension(size(a)) :: dchi2
real(8),dimension(totNlso,size(a)) :: df
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: Delta
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta,size(a)) :: dDelta
complex(8),dimension(Ldelta) :: Ftmp
real(8),dimension(Ldelta) :: Ctmp
integer :: i,j,l,ilat,jlat,iorb,jorb,ispin,jspin
!
Delta = delta_replica(a)
dDelta = grad_delta_replica(a)
!
do l=1,totNlso
ilat = getIlat(l)
jlat = getJlat(l)
iorb = getIorb(l)
jorb = getJorb(l)
ispin = getIspin(l)
jspin = getJspin(l)
!
Ftmp = Gdelta(l,:)-Delta(ilat,jlat,ispin,jspin,iorb,jorb,:)
Ctmp = abs(Ftmp)**(cg_pow-2)
do j=1,size(a)
df(l,j)=&
sum( dreal(Ftmp)*dreal(dDelta(ilat,jlat,ispin,jspin,iorb,jorb,:,j))*Ctmp/Wdelta ) + &
sum( dimag(Ftmp)*dimag(dDelta(ilat,jlat,ispin,jspin,iorb,jorb,:,j))*Ctmp/Wdelta )
enddo
enddo
!
dchi2 = -cg_pow*sum(df,1)/Ldelta !sum over all orbital indices
print*,dchi2
!
end function grad_chi2_delta_replica
#endif
!+-------------------------------------------------------------+
!PURPOSE: Evaluate the \chi^2 distance of G_0_Anderson function
! The Gradient is not evaluated, so the minimization requires
! a numerical estimate of the gradient.
!+-------------------------------------------------------------+
function chi2_weiss_replica(a) result(chi2)
real(8),dimension(:) :: a
real(8) :: chi2
real(8),dimension(totNlso) :: chi2_lso
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: g0and
real(8),dimension(Ldelta) :: Ctmp
integer :: i,l,ilat,jlat,iorb,jorb,ispin,jspin
!
g0and = g0and_replica(a)
!
do l=1,totNlso
ilat = getIlat(l)
jlat = getJlat(l)
iorb = getIorb(l)
jorb = getJorb(l)
ispin = getIspin(l)
jspin = getJspin(l)
!
Ctmp = abs(Gdelta(l,:)-g0and(ilat,jlat,ispin,jspin,iorb,jorb,:))
chi2_lso(l) = sum( Ctmp**cg_pow/Wdelta )
enddo
!
!FIXME:THIS NEEDS A THOROUGH DISCUSSION
!
!chi2=sum(chi2_lso)
chi2=maxval(chi2_lso)
maxchi_loc=maxloc(chi2_lso,1)
chi2=chi2/Ldelta
!
end function chi2_weiss_replica
#if __GNUC__ >= 8
!+-------------------------------------------------------------+
!PURPOSE: Evaluate the gradient \Grad\chi^2 of
! \Delta_Anderson function.
!+-------------------------------------------------------------+
function grad_chi2_weiss_replica(a) result(dchi2)
real(8),dimension(:) :: a
real(8),dimension(size(a)) :: dchi2
real(8),dimension(totNlso,size(a)) :: df
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: g0and
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta,size(a)) :: dg0and
complex(8),dimension(Ldelta) :: Ftmp
real(8),dimension(Ldelta) :: Ctmp
integer :: i,j,l,ilat,jlat,iorb,jorb,ispin,jspin
!
g0and = g0and_replica(a)
dg0and = grad_g0and_replica(a)
!
do l=1,totNlso
ilat = getIlat(l)
jlat = getJlat(l)
iorb = getIorb(l)
jorb = getJorb(l)
ispin = getIspin(l)
jspin = getJspin(l)
!
Ftmp = Gdelta(l,:)-g0and(ilat,jlat,ispin,jspin,iorb,jorb,:)
Ctmp = abs(Ftmp)**(cg_pow-2)
do j=1,size(a)
df(l,j)=&
sum( dreal(Ftmp)*dreal(dg0and(ilat,jlat,ispin,jspin,iorb,jorb,:,j))*Ctmp/Wdelta ) + &
sum( dimag(Ftmp)*dimag(dg0and(ilat,jlat,ispin,jspin,iorb,jorb,:,j))*Ctmp/Wdelta )
enddo
enddo
!
!dchi2 = -cg_pow*sum(df,1) !sum over all orbital indices
dchi2 = -cg_pow*df(maxchi_loc,:)
dchi2 = dchi2/Ldelta
!
end function grad_chi2_weiss_replica
#endif
!##################################################################
! THESE PROCEDURES EVALUATE THE
! - \delta
! - g0
! FUNCTIONS.
!##################################################################
function delta_replica(a) result(Delta)
real(8),dimension(:) :: a
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: Delta
integer :: ilat,jlat,ispin,jspin,iorb,jorb,ibath,isym
integer :: i,io,jo,ndx,stride
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: V_k
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: Haux
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb) :: invH_knnn
real(8),dimension(Nbath) :: dummy_Vbath
type(nsymm_vector),dimension(Nbath) :: dummy_lambda
complex(8) :: iw
!
!ACHTUNG! here the bath was a temporary one, since we removed the possibility to act on other baths we need to replicate the
!function behaviour. Rather ugly...
!Get Hs
stride = 0
do ibath=1,Nbath
allocate(dummy_lambda(ibath)%element(Nlambdas(ibath)))
!Get Vs
stride = stride + 1
dummy_vbath(ibath) = a(stride)
!get Lambdas
dummy_lambda(ibath)%element=a(stride+1:stride+Nlambdas(ibath))
stride=stride+Nlambdas(ibath)
enddo
!
!
Delta=zero
do i=1,Ldelta
iw = xi*Xdelta(i)+xmu
do ibath=1,Nbath
invH_knnn = bath_from_sym(dummy_lambda(ibath)%element)
Haux = zeye(Nlat*Nspin*Norb)*iw - nnn2lso_reshape(invH_knnn,Nlat,Nspin,Norb)
call inv(Haux)
invH_knnn = lso2nnn_reshape(Haux,Nlat,Nspin,Norb)
Delta(:,:,:,:,:,:,i)=Delta(:,:,:,:,:,:,i)+ dummy_Vbath(ibath)*invH_knnn(:,:,:,:,:,:)*dummy_Vbath(ibath)
enddo
enddo
!
do ibath=1,Nbath
deallocate(dummy_lambda(ibath)%element)
enddo
!
end function delta_replica
function g0and_replica(a) result(G0and)
real(8),dimension(:) :: a
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: G0and,Delta
complex(8),dimension(Nlat*Norb*Nspin,Nlat*Norb*Nspin) :: zeta,fgorb
integer :: i,Nlso
integer :: ilat,jlat,iorb,jorb,ispin,jspin,io,jo
!
Nlso = Nlat*Norb*Nspin
!
Delta = delta_replica(a)
!
do i=1,Ldelta
zeta = (xi*Xdelta(i)+xmu)*zeye(Nlso)
FGorb = zeta - nnn2lso_reshape(impHloc + Delta(:,:,:,:,:,:,i), Nlat,Nspin,Norb)
call inv(FGorb)
G0and(:,:,:,:,:,:,i) = lso2nnn_reshape(FGorb,Nlat,Nspin,Norb)
enddo
!
end function g0and_replica
#if __GNUC__ >= 8
!##################################################################
! THESE PROCEDURES EVALUATE GRADIENT OF THE
! - \delta
! - g0
! FUNCTIONS.
!##################################################################
function grad_delta_replica(a) result(dDelta)
real(8),dimension(:) :: a
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta,size(a)) :: dDelta
integer :: ilat,jlat,ispin,iorb,jorb,ibath
integer :: k,l,io,counter
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: H_reconstructed, Htmp,Hbasis_lso
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb,Ldelta) :: Haux
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: invH_knn
real(8),dimension(1,Nbath) :: dummy_Vbath !FIXME: TO EXTEND: 1->NSPIN
type(nsymm_vector),dimension(Nbath) :: dummy_lambda
!
!
!Get Hs
counter = 0
do ibath=1,Nbath
allocate(dummy_lambda(ibath)%element(Nlambdas(ibath)))
!
!FIXME: to extend uncomment Nspin and 1->NSPIN
!do ispin=1,Nspin
counter = counter + 1
dummy_vbath(1,ibath) = a(counter)
!enddo
!
dummy_lambda(ibath)%element=a(counter+1:counter+Nlambdas(ibath))
counter=counter+Nlambdas(ibath)
enddo
!
dDelta=zero
counter=0
!
do ibath=1,Nbath
H_reconstructed = nnn2lso_reshape(bath_from_sym(dummy_lambda(ibath)%element),Nlat,Nspin,Norb)
do l=1,Ldelta
Haux(:,:,l) = zeye(Nlat*Nspin*Norb)*(xi*Xdelta(l)+xmu) - H_reconstructed
call inv(Haux(:,:,l))
invH_knn(:,:,:,:,:,:,l) = lso2nnn_reshape(Haux(:,:,l),Nlat,Nspin,Norb)
enddo
!Derivate_Vp
do ispin=1,Nspin
counter = counter + 1
do ilat=1,Nlat
do jlat=1,Nlat
do iorb=1,Norb
do jorb=1,Norb
!FIXME: to extend, 1->ISPIN
dDelta(ilat,jlat,ispin,ispin,iorb,jorb,:,counter)=2d0*dummy_Vbath(1,ibath)*invH_knn(ilat,jlat,ispin,ispin,iorb,jorb,:)
enddo
enddo
enddo
enddo
enddo
!Derivate_lambda_p
do k=1,size(dummy_lambda(ibath)%element)
counter = counter + 1
Hbasis_lso=nnn2lso_reshape(H_basis(k)%O,Nlat,Nspin,Norb)
do l=1,Ldelta
! Hbasis_lso=nnn2lso_reshape(H_basis(k)%O,Nlat,Nspin,Norb)
! Htmp=matmul(Haux(:,:,l),Hbasis_lso)
! Htmp=matmul(Htmp,Haux(:,:,l))
Htmp = ((Haux(:,:,l) .x. Hbasis_lso)) .x. Haux(:,:,l)
dDelta(:,:,:,:,:,:,l,counter)=lso2nnn_reshape((dummy_Vbath(1,ibath)**2)*Htmp,Nlat,Nspin,Norb)
enddo
enddo
enddo
end function grad_delta_replica
function grad_g0and_replica(a) result(dG0and)
real(8),dimension(:) :: a
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta,size(a)) :: dG0and,dDelta
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb,Ldelta) :: G0and
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: dDelta_lso,dG0and_lso,G0and_lso
integer :: ilat,jlat,ispin,iorb,jorb
integer :: ik,l
!
G0and = g0and_replica(a)
dDelta = grad_delta_replica(a)
!
dG0and = zero
!
do l=1,Ldelta
G0and_lso=nnn2lso_reshape(g0and(:,:,:,:,:,:,l),Nlat,Nspin,Norb)
do ik=1,size(a)
dDelta_lso=nnn2lso_reshape(dDelta(:,:,:,:,:,:,l,ik),Nlat,Nspin,Norb)
! dG0and_lso=matmul(-G0and_lso,dDelta_lso)
! dG0and_lso=matmul(dG0and_lso,G0and_lso)
dG0and_lso = (G0and_lso .x. dDelta_lso) .x. G0and_lso
dG0and(:,:,:,:,:,:,l,ik)=-lso2nnn_reshape(dG0and_lso,Nlat,Nspin,Norb)
enddo
enddo
!
end function grad_g0and_replica
#endif
end MODULE ED_FIT_CHI2