DIIS method

subroutine, use cpp and extend_column options to compile. If you use ifort (intel fortran compiler), the option is '-cpp -132'.
explanation of DIIS (pdf)

how to use it

call it as

subroutine mixing_diis3_X(ite,n,alpha, fin, pout, Fwork,nstepFwork)


where X=c(complex(8)) or r(real(8)).

integer,intent(in)::ite,n,nstepFwork
complex(8),intent(in):: Fin(n) ! input
complex(8),intent(out):: pout(n) ! optimized output
complex(8),intent(out):: Fwork(nstepFwork,3) ! workarea(>=n), 3=2*level-1, level=1 now,
real(8),intent(in) :: alpha !mixing

Fwork is the workarea,
Fwork(n,1) : F(i-1)
Fwork(n,2) : p(i-1)
Fwork(n,3) : p(i)


When you set an initial value of fin(1), first call it as

ite=1
call mixing_diis3_c(ite,n,alpha, fin, pout, Fwork,nstepFwork)

pout = fin in this call.This call simply sets Fwork.

Calculate the next output. The next call for fin(2), which is the calculated result, is

ite=2 !=ite+1
call mixing_diis3_c(ite,n,alpha, fin, pout, Fwork,nstepFwork)

pout, which is the next optimized input, is the simple mixing of fin(1) and fin(2).

Again calculate the next output. For ite>=3,

ite=ite+1 !>=3
call mixing_diis3_c(ite,n,alpha, fin, pout, Fwork,nstepFwork)

pout is the optimized output in the DIIS mixing. pout is the next input.

The loop is like this.

ite=1
call mixing_diis3_c(ite,...) ! simply set the initial value
loop: do
ite=ite+1
! pout -> execute some calculation -> fin
call mixing_diis3_c(ite,...) !pout is the optimized next input
if (meet_the_criterion) exit
enddo loop

Also see the sample program.

You can reset, or restart, the DIIS mixing by ite=1 again.

Note

Use the DIIS method, when the error becomes smaller and the system begins to be converged, but the speed of the convergence is too slow to reach the criterion, because you must use quite small mixing parameter,~0.001 or so.
Then you can switch to the DIIS method. You can use 0.1~0.2 or so as the mixing parameter of DIIS.