ALF  dev.
A QMC Code for fermionic models
Global_mod.F90
1 ! Copyright (C) 2016 - 2022 The ALF project
2 !
3 ! This file is part of the ALF project.
4 !
5 ! The ALF project is free software: you can redistribute it and/or modify
6 ! it under the terms of the GNU General Public License as published by
7 ! the Free Software Foundation, either version 3 of the License, or
8 ! (at your option) any later version.
9 !
10 ! The ALF project is distributed in the hope that it will be useful,
11 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ! GNU General Public License for more details.
14 !
15 ! You should have received a copy of the GNU General Public License
16 ! along with ALF. If not, see http://www.gnu.org/licenses/.
17 !
18 ! Under Section 7 of GPL version 3 we require you to fulfill the following additional terms:
19 !
20 ! - It is our hope that this program makes a contribution to the scientific community. Being
21 ! part of that community we feel that it is reasonable to require you to give an attribution
22 ! back to the original authors if you have benefitted from this program.
23 ! Guidelines for a proper citation can be found on the project's homepage
24 ! http://alf.physik.uni-wuerzburg.de .
25 !
26 ! - We require the preservation of the above copyright notice and this license in all original files.
27 !
28 ! - We prohibit the misrepresentation of the origin of the original source files. To obtain
29 ! the original source files please visit the homepage http://alf.physik.uni-wuerzburg.de .
30 !
31 ! - If you make substantial changes to the program we require you to either consider contributing
32 ! to the ALF project or to mark your material in a reasonable way as different from the original version.
33 
34 
35 
36 !--------------------------------------------------------------------
39 !
42 !
43 !--------------------------------------------------------------------
44 
45 Module global_mod
46 
47  Use runtime_error_mod
49  Use mymats
50  Use operator_mod
51  Use control
52  Use observables
53  Use fields_mod
54  Use random_wrap
55  use iso_fortran_env, only: output_unit, error_unit
56 
57  Implicit none
58 
59  type(obser_vec ), private :: tempering_acceptance
60 
61 
62  Contains
63 #if defined(TEMPERING)
64 !--------------------------------------------------------------------
67 !
107 !--------------------------------------------------------------------
108  Subroutine exchange_step(Phase,GR, udvr, udvl, Stab_nt, udvst, N_exchange_steps, Tempering_calc_det)
110  Use mpi
111  use wrapul_mod
112  use cgr1_mod
113  Implicit none
114 
115  ! Arguments
116  COMPLEX (Kind=Kind(0.d0)), INTENT(INOUT) :: Phase
117  CLASS(udv_state), intent(inout), allocatable, Dimension(:) :: udvl, udvr
118  COMPLEX (Kind=Kind(0.d0)), Dimension(:,:,:), INTENT(INOUT), allocatable :: GR
119  CLASS(udv_state), intent(inout), allocatable, Dimension(:, :) :: udvst
120  INTEGER, dimension(:), INTENT (IN), allocatable :: Stab_nt
121  INTEGER, INTENT(IN) :: N_exchange_steps
122  Logical, INTENT(IN) :: Tempering_calc_det
123 
124 
126  Integer :: NST, NSTM, NF, nf_eff, NT, NT1, NVAR,N, N1,N2, I, NC, I_Partner, n_step, N_count, N_part
127  type(fields) :: nsigma_old
128  Real (Kind=Kind(0.d0)) :: T0_Proposal_ratio, Weight, Weight1
129  Complex (Kind=Kind(0.d0)) :: Z_ONE = cmplx(1.d0, 0.d0, kind(0.d0)), z, ratiotot, ratiotot_p, phase_old, phase_new
130  Real (Kind=Kind(0.d0)), allocatable :: Det_vec_old(:,:), Det_vec_new(:,:)
131  Complex (Kind=Kind(0.d0)), allocatable :: Phase_Det_new(:), Phase_Det_old(:)
132  Complex (Kind=Kind(0.d0)) :: Ratio(2), Ratio_p(2),Phase_array(n_fl)
133  Logical :: TOGGLE, L_Test
134  Integer, allocatable :: List_partner(:), List_masters(:)
135 
136  ! Keep track of where the configuration originally came from
137  Integer :: nsigma_irank, nsigma_old_irank, nsigma_irank_temp
138  Integer :: n_GR
139 
140  !Integer, Dimension(:,:), allocatable :: nsigma_orig, nsigma_test
141  !Integer :: I1, I2
142 
143  Integer :: Isize, Irank, Ierr, irank_g, isize_g, igroup
144  Integer :: STATUS(mpi_status_size)
145 
146  Character (Len=64) :: storage
147 
148 
149  CALL mpi_comm_size(mpi_comm_world,isize,ierr)
150  CALL mpi_comm_rank(mpi_comm_world,irank,ierr)
151  call mpi_comm_rank(group_comm, irank_g, ierr)
152  call mpi_comm_size(group_comm, isize_g, ierr)
153  igroup = irank/isize_g
154  nsigma_irank = irank
155 
156  n1 = size(nsigma%f,1)
157  n2 = size(nsigma%f,2)
158  nstm = Size(udvst, 1)
159  call nsigma_old%make(n1, n2)
160  if (tempering_calc_det) then
161  Allocate ( det_vec_old(ndim,n_fl), det_vec_new(ndim,n_fl) )
162  Allocate ( phase_det_new(n_fl), phase_det_old(n_fl) )
163  endif
164  Allocate ( list_partner(0:isize-1), list_masters(isize/2) )
165 
166  ! Allocate ( nsigma_orig(n1,n2) )
167  ! nsigma_orig = nsigma
168 
169  ! Compute for each core the old weights.
170  l_test = .false.
171  if (tempering_calc_det) then
172  ! Set old weight.
173  storage = "Full"
174  Call compute_fermion_det(phase_det_old,det_vec_old, udvl, udvst, stab_nt, storage)
175  phase_array=1.0d0
176  Do nf_eff = 1,n_fl_eff
177  nf=calc_fl_map(nf_eff)
178  phase_array(nf) = phase_det_old(nf)
179  Call op_phase(phase_array(nf),op_v,nsigma,nf)
180  Enddo
181  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
182  phase_old=product(phase_array)
183  phase_old=phase_old**n_sun
184  endif
186  nsigma_old%f = nsigma%f
187  nsigma_old%t = nsigma%t
188  nsigma_old_irank = nsigma_irank
189  ! Setup the list of masters
190  nc = 0
191  Do i = 0,isize-1,2*isize_g
192  do n = 0,isize_g-1
193  nc = nc + 1
194  list_masters(nc) = i + n
195  enddo
196  Enddo
197  if (l_test) then
198  if (irank == 0) then
199  Write(6,*) 'List of masters: '
200  Do nc = 1,isize/2
201  Write(6,*) nc, list_masters(nc)
202  Enddo
203  endif
204  endif
205  DO n_count = 1,n_exchange_steps
206  ! Set the partner rank on each core
207  If (irank == 0 ) then
208  n_step = isize_g
209  if ( ranf_wrap() > 0.5d0 ) n_step = -isize_g
210  Do i = 0,isize-1,2*isize_g
211  do n = 0,isize_g-1
212  list_partner(npbc_tempering(i + n ,isize)) = npbc_tempering(i + n + n_step ,isize)
213  list_partner(npbc_tempering(i + n + n_step , isize)) = npbc_tempering(i + n ,isize)
214  enddo
215  enddo
216  endif
217 
218  CALL mpi_bcast(list_partner, isize ,mpi_integer, 0,mpi_comm_world,ierr)
219 
220  If (l_test) then
221  if (irank == 0 ) then
222  Write(6,*) 'Testing global : '
223  do i = 0,isize -1
224  Write(6,*) i, list_partner(i) !, Phase_old, Phase
225  Write(11,*) i, list_partner(i) !, Phase_old, Phase
226  enddo
227  Write(11,*)
228  endif
229  Endif
230 
231 
232  ! Exchange configurations
233  ! The types do not change --> no need to exchange them
234  n = size(nsigma_old%f,1)*size(nsigma_old%f,2)
235  CALL mpi_sendrecv(nsigma_old%f , n, mpi_complex16, list_partner(irank), 0, &
236  & nsigma%f , n, mpi_complex16, list_partner(irank), 0, mpi_comm_world,status,ierr)
237  CALL mpi_sendrecv(nsigma_old_irank, 1, mpi_integer, list_partner(irank), 0, &
238  & nsigma_irank , 1, mpi_integer, list_partner(irank), 0, mpi_comm_world,status,ierr)
239 
240  ! Each node now has a new configuration nsigma
241 
242 
243  if (tempering_calc_det) then
244  ! HERE
245  ! Compute ratio on weights one each rank
246  storage = "Empty"
247  Call compute_fermion_det(phase_det_new,det_vec_new, udvl, udvst, stab_nt, storage)
248 
249  phase_array = cmplx(1.d0,0.d0,kind=kind(0.d0))
250  Do nf_eff = 1,n_fl_eff
251  nf=calc_fl_map(nf_eff)
252  phase_array(nf) = phase_det_new(nf)
253  Call op_phase(phase_array(nf),op_v,nsigma,nf)
254  Enddo
255  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
256  phase_new=product(phase_array)
257  phase_new=phase_new**n_sun
258 
259  t0_proposal_ratio = 1.d0
260  ratiotot = compute_ratio_global(phase_det_old, phase_det_new, &
261  & det_vec_old, det_vec_new, nsigma_old, t0_proposal_ratio,ratio)
262 
263  If (l_test) Write(6,*) 'Ratio_global: Irank, Partner',irank,list_partner(irank), &
264  & ratiotot, ratio(1)*exp(ratio(2))
265  else
266  ratiotot = ham%Delta_S0_global(nsigma_old)
267  ratio(1) = ratiotot
268  ratio(2) = 0
269  endif
270 
271  ! Acceptace/rejection decision taken on master node after receiving information from slave
272  Do nc = 1,isize/2 ! Loop over masters
273  i = list_masters(nc)
274  If (irank == i ) Then
275  CALL mpi_send(ratio ,2, mpi_complex16 , list_partner(i), i+1024, mpi_comm_world,ierr)
276  else if (irank == list_partner(i) ) Then
277  CALL mpi_recv(ratio_p , 2, mpi_complex16, i, i+1024, mpi_comm_world,status,ierr)
278  !Weight = abs(Ratiotot_p*Ratiotot)
279  weight= abs(ratio(1) * ratio_p(1) * exp( ratio_p(2) + ratio(2) ) )
280  toggle = .false.
281  if ( weight > ranf_wrap() ) toggle =.true.
282  If (l_test) Write(6,*) 'Master : ', list_partner(i), i, weight, toggle
283  endif
284  enddo
285 
287  Do nc = 1,isize/2 ! Loop over masters
288  i = list_masters(nc)
289  If (irank == list_partner(i) ) Then
290  ! Write(6,*) 'Send from ', List_Partner(I), 'to, ', I, I + 512
291  CALL mpi_send(toggle, 1, mpi_logical, i, i+1024, mpi_comm_world,ierr)
292  else if (irank == i ) Then
293  CALL mpi_recv(toggle , 1, mpi_logical, list_partner(i), i+1024 ,mpi_comm_world,status,ierr)
294  If (l_test) Write(6,*) 'Slave : ', irank, toggle
295  endif
296  enddo
297 
298  If (l_test) Write(6,*) 'Final: ', irank, list_partner(irank), toggle
299  Call global_tempering_obser(toggle)
300  Call control_upgrade_temp (toggle)
301  If (toggle) then
302  ! Move has been accepted
303  if (tempering_calc_det) then
304  phase_old = phase_new
305  phase_det_old = phase_det_new
306  det_vec_old = det_vec_new
307  endif
308  nsigma_old%f = nsigma%f
309  nsigma_old%t = nsigma%t
310  nsigma_old_irank = nsigma_irank
311  else
312  nsigma%f = nsigma_old%f
313  nsigma%t = nsigma_old%t
314  nsigma_irank = nsigma_old_irank
315  endif
316  enddo
317 
318  ! Finalize
319  if (tempering_calc_det) then
320  ! If move has been accepted, no use to recomute storage
321  If (.not.toggle) then
322  DO nf_eff = 1,n_fl_eff
323  nf=calc_fl_map(nf_eff)
324  if (projector) then
325  CALL udvl(nf_eff)%reset('l',wf_l(nf)%P)
326  else
327  CALL udvl(nf_eff)%reset('l')
328  endif
329  ENDDO
330  DO nst = nstm-1,1,-1
331  nt1 = stab_nt(nst+1)
332  nt = stab_nt(nst )
333  !Write(6,*) NT1,NT, NST
334  CALL wrapul(nt1,nt, udvl)
335  Do nf_eff = 1,n_fl_eff
336  udvst(nst, nf_eff) = udvl(nf_eff)
337  ENDDO
338  ENDDO
339  nt1 = stab_nt(1)
340  CALL wrapul(nt1,0, udvl)
341  Endif
342  ! Compute the Green functions so as to provide correct starting point for the sequential updates.
343  nvar = 1
344  phase_array = cmplx(1.d0,0.d0,kind(0.d0))
345  do nf_eff = 1,n_fl_eff
346  nf=calc_fl_map(nf_eff)
347  CALL cgr(z, nvar, gr(:,:,nf), udvr(nf_eff), udvl(nf_eff))
348  call op_phase(z,op_v,nsigma,nf)
349  phase_array(nf)=z
350  Enddo
351  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
352  phase=product(phase_array)
353  phase=phase**n_sun
354  else
355  ! Send >> Phase, GR, udvr, udvl, udvst << to new node
356  ! First step: Each node sends to IRANK=0 its value nsigma_irank,
357  ! which is the node where its new Phase, GR, udvr, udvl, udvst is stored
358  ! This node then tells each node where to send its now old Phase, GR, udvr, udvl, udvst
359  ! Finally, the variables get submitted
360  If (irank == 0) then
361  Do i = 1,isize-1
362  CALL mpi_recv(nsigma_irank_temp , 1, mpi_integer, i, 0, mpi_comm_world,status,ierr)
363  If ( nsigma_irank_temp == 0) then
364  nsigma_old_irank = i
365  else
366  CALL mpi_send(i , 1, mpi_integer, nsigma_irank_temp, 0, mpi_comm_world,ierr)
367  endif
368  enddo
369  If ( nsigma_irank /= 0 ) then
370  CALL mpi_send(0 , 1, mpi_integer, nsigma_irank, 0, mpi_comm_world,ierr)
371  endif
372  else
373  CALL mpi_send(nsigma_irank , 1, mpi_integer, 0, 0, mpi_comm_world,ierr)
374  CALL mpi_recv(nsigma_old_irank , 1, mpi_integer, 0, 0, mpi_comm_world,status,ierr)
375  endif
376 
377  if ( nsigma_irank /= irank ) then
378  CALL mpi_sendrecv_replace(phase, 1, mpi_complex16, nsigma_old_irank, 0, &
379  & nsigma_irank, 0, mpi_comm_world, status, ierr)
380 
381  n_gr = size(gr,1)*size(gr,2)*size(gr,3)
382  CALL mpi_sendrecv_replace(gr, n_gr, mpi_complex16, nsigma_old_irank, 0, &
383  & nsigma_irank, 0, mpi_comm_world, status, ierr)
384 
385  do nf_eff = 1,n_fl_eff
386  CALL udvr(nf_eff)%MPI_Sendrecv(nsigma_old_irank, 0, nsigma_irank, 0, status, ierr)
387  enddo
388  do nf_eff = 1,n_fl_eff
389  CALL udvl(nf_eff)%MPI_Sendrecv(nsigma_old_irank, 0, nsigma_irank, 0, status, ierr)
390  enddo
391  do nst = 1, nstm
392  do nf_eff = 1,n_fl_eff
393  CALL udvst(nst, nf_eff)%MPI_Sendrecv(nsigma_old_irank, 0, nsigma_irank, 0, status, ierr)
394  enddo
395  enddo
396  endif
397  endif
398 
399 
400  call nsigma_old%clear
401 
402  if (tempering_calc_det) then
403  Deallocate ( det_vec_old, det_vec_new )
404  Deallocate ( phase_det_new, phase_det_old )
405  endif
406  Deallocate ( list_partner, list_masters )
407 
408  end Subroutine exchange_step
409 #endif
410 
411 
412 !--------------------------------------------------------------------
449 !--------------------------------------------------------------------
450  Subroutine global_updates(Phase,GR, udvr, udvl, Stab_nt, udvst,N_Global)
452  Use udv_state_mod
453  use wrapul_mod
454  use cgr1_mod
455  Implicit none
456 
457  ! Arguments
458  COMPLEX (Kind=Kind(0.d0)), INTENT(INOUT) :: Phase
459  class(udv_state), DIMENSION(:), ALLOCATABLE, INTENT(INOUT) :: udvl, udvr
460  COMPLEX (Kind=Kind(0.d0)), Dimension(:,:,:), allocatable,INTENT(INOUT) :: GR
461  CLASS(udv_state), Dimension(:,:), ALLOCATABLE, INTENT(INOUT) :: udvst
462  INTEGER, dimension(:), allocatable, INTENT(IN) :: Stab_nt
463  Integer, INTENT(IN) :: N_Global
464 
465 
466  ! Local variables.
467  Integer :: NST, NSTM, NF, NT, NT1, NVAR,N, N1,N2, I, NC, N_part,j, nf_eff
468  Real (Kind=Kind(0.d0)) :: T0_Proposal_ratio, Weight
469  Complex (Kind=Kind(0.d0)) :: Z_ONE = cmplx(1.d0, 0.d0, kind(0.d0)), z, ratiotot, phase_old, phase_new
470  Complex (Kind=Kind(0.d0)), allocatable :: Det_vec_test(:,:), Phase_Det_new(:), Phase_Det_old(:)
471  Real (Kind=Kind(0.d0)), allocatable :: Det_vec_old(:,:), Det_vec_new(:,:)
472  type(fields) :: nsigma_old
473 
474  Complex (Kind=Kind(0.d0)) :: Ratio(2), Phase_array(n_fl)
475  Logical :: TOGGLE, L_Test
476  Real (Kind=Kind(0.d0)) :: size_clust
477  Real (Kind=Kind(0.d0)) :: ratio_2_test
478  Character (Len=64) :: storage
479 
480 
481 
482  n1 = size(nsigma%f,1)
483  n2 = size(nsigma%f,2)
484  nstm = Size(udvst, 1)
485  call nsigma_old%make(n1, n2)
486 
487  Allocate ( det_vec_old(ndim,n_fl_eff), det_vec_new(ndim,n_fl_eff), det_vec_test(ndim,n_fl_eff) )
488  Allocate ( phase_det_new(n_fl_eff), phase_det_old(n_fl_eff) )
489 
490  l_test = .false.
491  ! Set old weight.
492  storage = "Full"
493  Call compute_fermion_det(phase_det_old,det_vec_old, udvl, udvst, stab_nt, storage)
494  phase_array = cmplx(1.d0,0.d0,kind=kind(0.d0))
495  Do nf_eff = 1,n_fl_eff
496  nf=calc_fl_map(nf_eff)
497  phase_array(nf) = phase_det_old(nf_eff)
498  Call op_phase(phase_array(nf),op_v,nsigma,nf)
499  Enddo
500  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
501  phase_old=product(phase_array)
502  phase_old=phase_old**n_sun
503 
504  If (l_test) then
505  ! Testing
506  Do nf_eff = 1,n_fl_eff
507  nf=calc_fl_map(nf_eff)
508  if (projector) then
509  CALL udvr(nf_eff)%reset('r',wf_r(nf)%P)
510  else
511  CALL udvr(nf_eff)%reset('r')
512  endif
513  Enddo
514  nvar = 1
515  phase_array = cmplx(1.d0,0.d0,kind(0.d0))
516  do nf_eff = 1,n_fl_eff
517  nf=calc_fl_map(nf_eff)
518  CALL cgr(z, nvar, gr(:,:,nf), udvr(nf_eff), udvl(nf_eff))
519  call op_phase(z,op_v,nsigma,nf)
520  phase_array(nf)=z
521  Enddo
522  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
523  phase=product(phase_array)
524  phase=phase**n_sun
525  Do nf_eff = 1,n_fl_eff
526  nf=calc_fl_map(nf_eff)
527  Call det_c_lu(gr(:,:,nf),det_vec_test(:,nf_eff),ndim)
528  !!!ATTENTION HOW DOES BELOW DEPEND ON NF BLOCK symm
529  z = phase_det_old(nf_eff)
530  ratio_2_test=0.d0
531  DO i = 1,ndim
532  z = z*det_vec_test(i,nf_eff)/abs(det_vec_test(i,nf_eff))
533  ratio_2_test=ratio_2_test+log(abs(det_vec_test(i,nf_eff)))+det_vec_old(i,nf_eff)
534  Enddo
535  z=z*cmplx(exp(ratio_2_test),0.d0,kind(0.d0))
536  Write(6,*) 'Testing weight: ', z
537  if(abs(ratio_2_test)>650) write(6,*) "Weight is about to reach double underflow!"
538  Enddo
539  Endif
540 
541  ! Store old configuration
542  nsigma_old%f = nsigma%f
543  nsigma_old%t = nsigma%t
544  ! Phase_old, Phase_det_old and Det_vec_old are all set.
545  nc = 0
546  Do n = 1,n_global
547  ! Draw a new spin configuration. This is provided by the user in the Hamiltonian module
548  ! Note that nsigma is a variable in the module Hamiltonian
549  Call ham%Global_move(t0_proposal_ratio,nsigma_old,size_clust)
550  If (t0_proposal_ratio > 0.d0) then
551  nc = nc + 1
552  ! Compute the new Green function
553  storage = "Empty"
554  Call compute_fermion_det(phase_det_new,det_vec_new, udvl, udvst, stab_nt, storage)
555 
556  phase_array = cmplx(1.d0,0.d0,kind=kind(0.d0))
557  Do nf_eff = 1,n_fl_eff
558  nf=calc_fl_map(nf_eff)
559  phase_array(nf) = phase_det_new(nf)
560  Call op_phase(phase_array(nf),op_v,nsigma,nf)
561  Enddo
562  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
563  phase_new=product(phase_array)
564  phase_new=phase_new**n_sun
565 
566  ratiotot = compute_ratio_global(phase_det_old, phase_det_new, &
567  & det_vec_old, det_vec_new, nsigma_old, t0_proposal_ratio, ratio)
568 
569  !Write(6,*) 'Ratio_global: ', Ratiotot
570 
571  weight = abs( real( phase_old * ratiotot, kind=kind(0.d0))/real(Phase_old,kind=Kind(0.d0)) )
572 
573  z = phase_old * ratiotot/abs(ratiotot)
574  Call control_precisionp_glob(z,phase_new)
575  !Write(6,*) Z, Phase_new
576 
577 
578  toggle = .false.
579  if ( weight > ranf_wrap() ) Then
580  toggle = .true.
581  phase_old = phase_new
582  phase_det_old = phase_det_new
583  nsigma_old%t = nsigma%t
584  nsigma_old%f = nsigma%f
585  det_vec_old = det_vec_new
586  else
587  nsigma%t = nsigma_old%t
588  nsigma%f = nsigma_old%f
589  endif
590  Call control_upgrade_glob(toggle,size_clust)
591  else
592  nsigma%t = nsigma_old%t
593  nsigma%f = nsigma_old%f
594  endif
595  Enddo
596 
597  If (nc > 0 ) then
598  If (.not.toggle) then
599  DO nf_eff = 1,n_fl_eff
600  nf=calc_fl_map(nf_eff)
601  if (projector) then
602  CALL udvl(nf_eff)%reset('l',wf_l(nf)%P)
603  else
604  CALL udvl(nf_eff)%reset('l')
605  endif
606  ENDDO
607  DO nst = nstm-1,1,-1
608  nt1 = stab_nt(nst+1)
609  nt = stab_nt(nst )
610  !Write(6,*) NT1,NT, NST
611  CALL wrapul(nt1,nt, udvl)
612  Do nf_eff = 1,n_fl_eff
613  udvst(nst, nf_eff) = udvl(nf_eff)
614  ENDDO
615  ENDDO
616  nt1 = stab_nt(1)
617  CALL wrapul(nt1,0, udvl)
618  Endif
619  !Compute the Green functions so as to provide correct starting point for the sequential updates.
620  nvar = 1
621  phase_array = cmplx(1.d0,0.d0,kind(0.d0))
622  do nf_eff = 1,n_fl_eff
623  nf=calc_fl_map(nf_eff)
624  CALL cgr(z, nvar, gr(:,:,nf), udvr(nf_eff), udvl(nf_eff))
625  call op_phase(z,op_v,nsigma,nf)
626  phase_array(nf)=z
627  Enddo
628  if (reconstruction_needed) call ham%weight_reconstruction(phase_array)
629  phase=product(phase_array)
630  phase=phase**n_sun
631  endif
632 
633 
634  call nsigma_old%clear
635  Deallocate ( det_vec_old , det_vec_new, det_vec_test )
636  Deallocate ( phase_det_new, phase_det_old )
637 
638 
639  End Subroutine global_updates
640 
641 
642 !--------------------------------------------------------------------
668 !--------------------------------------------------------------------
669  Complex (Kind=Kind(0.d0)) Function compute_ratio_global(Phase_Det_old, Phase_Det_new, &
670  & Det_vec_old, Det_vec_new, nsigma_old, T0_Proposal_ratio,Ratio)
672 
673  Implicit none
674 
675  ! Arguments
676  Complex (Kind=Kind(0.d0)), allocatable, INTENT(IN) :: Phase_Det_old(:), Phase_Det_new(:)
677  REAL (Kind=Kind(0.d0)), allocatable, INTENT(IN) :: Det_vec_old(:,:), Det_vec_new(:,:)
678  Real (Kind=Kind(0.d0)), INTENT(IN) :: T0_proposal_ratio
679  type(fields), INTENT(IN) :: nsigma_old
680  Complex (Kind=Kind(0.d0)), INTENT(out) :: Ratio(2)
681 
682  ! Local
683  Integer :: Nf, i, nt, nf_eff
684  Complex (Kind=Kind(0.d0)) :: Z, Z1, Ratio_1_array(n_fl), Ratio_2_array(n_fl), g_loc
685  Real (Kind=Kind(0.d0)) :: X, Ratio_2
686 
687  ratio = cmplx(0.d0,0.d0,kind(0.d0))
688  ratio_2_array = 0.d0
689  !X = 1.d0
690  Do nf_eff = 1,n_fl_eff
691  nf=calc_fl_map(nf_eff)
692  DO i = 1,ndim
693  !X= X*real(Det_vec_new(I,nf),kind(0.d0)) / Real(Det_vec_old(I,nf),kind(0.d0) )
694  ratio_2_array(nf) = ratio_2_array(nf) + det_vec_new(i,nf_eff) - det_vec_old(i,nf_eff)
695  enddo
696  !Z = Z**N_SUN
697  ratio_2_array(nf) = real(n_sun,kind(0.d0))*Ratio_2_array(nf)
698  enddo
699  !Z = cmplx(X,0.d0,kind(0.d0))
700  ratio_1_array = cmplx(1.d0,0.d0,kind(0.d0))
701  Do nf_eff = 1,n_fl_eff
702  nf=calc_fl_map(nf_eff)
703  !Z = Z*Phase_Det_new(nf)/Phase_Det_old(nf)
704  ratio_1_array(nf_eff) = ratio_1_array(nf) * phase_det_new(nf_eff)/phase_det_old(nf_eff)
705  !Z = Z**N_SUN
706  ratio_1_array(nf) = ratio_1_array(nf)**n_sun
707  enddo
708 
709  ratio(1)=1.d0
710  Do i = 1,Size(op_v,1)
711  x = 0.d0
712  Do nt = 1,ltrot
713  !Z = Z * cmplx( Gaml(nsigma(i,nt),2)/Gaml(nsigma_old(i,nt),2),0.d0,kind(0.d0) )
714  ratio(1) = ratio(1) * cmplx( nsigma%Gama(i,nt)/nsigma_old%Gama(i,nt),0.d0,kind(0.d0) ) !You could put this in Ratio_2
715  !X = X + nsigma%Phi(i,nt) - nsigma_old%Phi(i,nt)
716  Do nf_eff = 1,n_fl_eff
717  nf=calc_fl_map(nf_eff)
718  z = nsigma%Phi(i,nt) - nsigma_old%Phi(i,nt)
719  g_loc = op_v(i,nf)%g
720  if ( op_v(i,nf)%get_g_t_alloc()) g_loc = op_v(i,nf)%g_t(nt)
721  !Z = Z * exp(cmplx( X*Real(N_SUN,Kind(0.d0)), 0.d0,kind(0.d0)) * Op_V(i,nf)%g * Op_V(i,nf)%alpha )
722  ratio_1_array(nf) = ratio_1_array(nf) * exp(z*cmplx( Real(N_SUN,Kind(0.d0)), 0.d0,kind(0.d0)) * g_loc * op_v(i,nf)%alpha )
723  Enddo
724  Enddo
725  Enddo
726  if (reconstruction_needed) then
727  call ham%weight_reconstruction(ratio_1_array)
728  call ham%weight_reconstruction(ratio_2_array)
729  endif
730  ratio(1)=ratio(1)*product(ratio_1_array)
731  !Z = Z * cmplx( ham%Delta_S0_global(Nsigma_old),0.d0,kind(0.d0) )
732  !Z = Z * cmplx( T0_Proposal_ratio, 0.d0,kind(0.d0))
733  ratio(2) = sum(ratio_2_array)
734  ratio(2) = ratio(2) + log(ham%Delta_S0_global(nsigma_old)) + log(t0_proposal_ratio)
735 
736  compute_ratio_global = ratio(1)*exp(ratio(2))
737 
738 
739  end Function compute_ratio_global
740 
741 !--------------------------------------------------------------------
778 !--------------------------------------------------------------------
779  Subroutine compute_fermion_det(Phase_det,Det_Vec, udvl, udvst, Stab_nt, storage)
780 !--------------------------------------------------------------------
781 
782  Use udv_wrap_mod
783  Use udv_state_mod
784  use wrapul_mod
785 
786  Implicit none
787 
788  REAL (Kind=Kind(0.d0)), Dimension(:,:), Intent(OUT) :: Det_Vec
789  Complex (Kind=Kind(0.d0)), Dimension(:) , Intent(OUT) :: Phase_det
790  CLASS(udv_state), DIMENSION(:) , ALLOCATABLE, INTENT(INOUT) :: udvl
791  CLASS(udv_state), Dimension(:,:), ALLOCATABLE, INTENT(INOUT) :: udvst
792  INTEGER, dimension(:), allocatable, INTENT(IN) :: Stab_nt
793  Character (Len=64), Intent(IN) :: storage
794 
795 
797  Integer :: N_size, NCON, I, J, N_part, info, NSTM, N, nf, nst, nt, nt1, nf_eff
798  Integer, allocatable :: ipiv(:)
799  COMPLEX (Kind=Kind(0.d0)) :: alpha,beta, Z, Z1
800  TYPE(udv_state) :: udvlocal
801  COMPLEX (Kind=Kind(0.d0)), Dimension(:,:), Allocatable :: TP!, U, V
802  COMPLEX (Kind=Kind(0.d0)), Dimension(:), Allocatable :: D
803 
804  !! TODO adapt to flavor symmetry
805 
806  if(udvl(1)%side .ne. "L" .and. udvl(1)%side .ne. "l" ) then
807  write(error_unit,*) "Compute_Fermion_Det: calling wrong decompose"
808  CALL terminate_on_error(error_global_updates,__file__,__line__)
809  endif
810 
811  nstm = Size(udvst, 1)
812  If (str_to_upper(storage) == "EMPTY" ) then
813  DO nf_eff = 1,n_fl_eff
814  nf=calc_fl_map(nf_eff)
815  if (projector) then
816  CALL udvl(nf_eff)%reset('l',wf_l(nf)%P)
817  else
818  CALL udvl(nf_eff)%reset('l')
819  endif
820  ENDDO
821  DO nst = nstm-1,1,-1
822  nt1 = stab_nt(nst+1)
823  nt = stab_nt(nst )
824  !Write(6,*) 'Call wrapul', NT1,NT, udvl(1)%d(1), udvl(1)%N_part, udvl(1)%Ndim
825  CALL wrapul(nt1,nt, udvl)
826  Do nf_eff = 1,n_fl_eff
827  udvst(nst, nf_eff) = udvl(nf_eff)
828  ENDDO
829  ENDDO
830  nt1 = stab_nt(1)
831  CALL wrapul(nt1,0, udvl)
832  Endif
833 
834  if (projector) then
835  n_part=udvst(1,1)%N_part
836  n_size=udvl(1)%ndim
837  det_vec = 0.d0
838  Allocate (tp(n_part,n_part), ipiv(n_part))
839  do nf_eff=1,n_fl_eff
840  nf=calc_fl_map(nf_eff)
841  n_part=udvst(1,nf)%N_part
842  do i=1,nstm-1
843  do n=1,n_part
844 #if !defined(STABLOG)
845  det_vec(n,nf_eff)=det_vec(n,nf_eff)+log(dble(udvst(i,nf)%D(n)))
846 #else
847  det_vec(n,nf_eff)=det_vec(n,nf_eff)+udvst(i,nf)%L(n)
848 #endif
849  enddo
850  enddo
851  do n=1,n_part
852 #if !defined(STABLOG)
853  det_vec(n,nf_eff)=det_vec(n,nf_eff)+log(dble(udvl(nf_eff)%D(n)))
854 #else
855  det_vec(n,nf_eff)=det_vec(n,nf_eff)+udvl(nf_eff)%L(n)
856 #endif
857  enddo
858  alpha=1.d0
859  beta=0.d0
860  CALL zgemm('C','N',n_part,n_part,n_size,alpha,udvl(nf_eff)%U(1,1),n_size,wf_r(nf)%P(1,1),n_size,beta,tp(1,1),n_part)
861  ! ZGETRF computes an LU factorization of a general M-by-N matrix A
862  ! using partial pivoting with row interchanges.
863  call zgetrf(n_part, n_part, tp(1,1), n_part, ipiv, info)
864  z=1.d0
865  Do j=1,n_part
866  if (ipiv(j).ne.j) then
867  z = -z
868  endif
869  z = z * tp(j,j)
870  enddo
871  phase_det(nf_eff) = z/abs(z)
872  det_vec(1,nf) = det_vec(1,nf)+log(abs(z))
873  enddo
874  Deallocate(tp,ipiv)
875  return
876  endif
877 
878  ! N_size = SIZE(DL,1)
879  n_size = udvl(1)%ndim
880  ncon = 0
881  alpha = cmplx(1.d0,0.d0,kind(0.d0))
882  beta = cmplx(0.d0,0.d0,kind(0.d0))
883  Allocate (tp(n_size,n_size),d(n_size))
884  CALL udvlocal%alloc(n_size)
885  Do nf_eff = 1,n_fl_eff
886  nf=calc_fl_map(nf_eff)
887  tp = udvl(nf_eff)%U !udvl stores U^dag instead of U !CT(udvl%U)
888 #if !defined(STABLOG)
889 #if !defined(STAB3)
890  DO j = 1,n_size
891  tp(:,j) = tp(:,j) + udvl(nf_eff)%V(:,j)*udvl(nf_eff)%D(j)
892  ENDDO
893 #else
894  DO j = 1,n_size
895  if ( dble(udvl(nf_eff)%D(j)) <= 1.d0 ) then
896  tp(:,j) = tp(:,j) + udvl(nf_eff)%V(:,j)*udvl(nf_eff)%D(j)
897  else
898  tp(:,j) = tp(:,j)/udvl(nf_eff)%D(j) + udvl(nf_eff)%V(:,j)
899  endif
900  ENDDO
901 #endif
902 #else
903  DO j = 1,n_size
904  if ( udvl(nf_eff)%L(j) <= 0.d0 ) then
905  tp(:,j) = tp(:,j) + udvl(nf_eff)%V(:,j)*cmplx(exp(udvl(nf_eff)%L(j)),0.d0,kind(0.d0))
906  else
907  tp(:,j) = tp(:,j)*cmplx(exp(-udvl(nf_eff)%L(j)),0.d0,kind(0.d0)) + udvl(nf_eff)%V(:,j)
908  endif
909  ENDDO
910 #endif
911 
912  !Call UDV_WRAP_Pivot(TP,udvlocal%U, D, udvlocal%V, NCON,N_size,N_Size)
913  Call udv_wrap(tp,udvlocal%U, d, udvlocal%V, ncon )
914  z = det_c(udvlocal%V, n_size) ! Det destroys its argument
915  ! Call MMULT(TP, udvl%U, udvlocal%U)
916  CALL zgemm('C', 'N', n_size, n_size, n_size, alpha, udvl(nf_eff)%U(1,1), n_size, udvlocal%U(1,1), n_size, beta, tp, n_size)
917  z1 = det_c(tp, n_size)
918  phase_det(nf_eff) = z*z1/abs(z*z1)
919 #if !defined(STABLOG)
920 #if !defined(STAB3)
921  det_vec(:,nf_eff) = log(real(d(:)))
922  det_vec(1,nf_eff) = log(real(d(1))) + log(ABS(z*z1))
923 #else
924  det_vec(1,nf_eff) = log(real(d(1))*ABS(z*z1))
925  if (dble(udvl(nf_eff)%D(1)) > 1.d0) det_vec(1,nf_eff)=det_vec(1,nf_eff)+log(dble(udvl(nf_eff)%D(1)))
926  Do j=2,ndim
927  if (dble(udvl(nf_eff)%D(j))<=1.d0) then
928  det_vec(j,nf_eff) = log(real(d(j)))
929  else
930  det_vec(j,nf_eff) = log(real(d(j)))+log(dble(udvl(nf_eff)%d(j)))
931  endif
932  enddo
933 #endif
934 #else
935  det_vec(:,nf_eff) = log(real(d(:)))
936  det_vec(1,nf_eff) = log(real(d(1))*ABS(z*z1))
937  if (udvl(nf_eff)%L(1) > 0.d0) det_vec(1,nf_eff)=det_vec(1,nf_eff)+udvl(nf_eff)%L(1)
938  Do j=2,ndim
939  if (udvl(nf_eff)%L(j)<=0.d0) then
940  det_vec(j,nf_eff) = log(real(d(j)))
941  else
942  det_vec(j,nf_eff) = log(real(d(j)))+udvl(nf_eff)%l(J)
943  endif
944  enddo
945 #endif
946  Enddo
947  Deallocate (tp)
948  CALL udvlocal%dealloc
949 
950  end subroutine compute_fermion_det
951 
952 
953 !--------------------------------------------------------------------
959 !--------------------------------------------------------------------
960  Integer function npbc_tempering(n,Isize)
961  implicit none
962  Integer, INTENT(IN) :: Isize,n
963 
964  npbc_tempering = n
965  if ( npbc_tempering < 0 ) npbc_tempering = npbc_tempering + isize
966  if ( npbc_tempering > isize -1) npbc_tempering = npbc_tempering - isize
967 
968  end function npbc_tempering
969 
970 
971 !--------------------------------------------------------------------
977 !--------------------------------------------------------------------
978  Subroutine global_tempering_setup
979  Integer :: N
980  Character (len=64) :: Filename
981  n = 1
982  filename = "Acc_Temp"
983  Call obser_vec_make(tempering_acceptance,n,filename)
984  End Subroutine global_tempering_setup
985 
986 !--------------------------------------------------------------------
992 !--------------------------------------------------------------------
993 
994  Subroutine global_tempering_init_obs
995  Call obser_vec_init( tempering_acceptance )
996  end Subroutine global_tempering_init_obs
997 
998 !--------------------------------------------------------------------
1004 !--------------------------------------------------------------------
1005 
1006  Subroutine global_tempering_obser(toggle)
1007  Implicit none
1008 
1009  Logical, intent(in) :: toggle
1010  tempering_acceptance%N = tempering_acceptance%N + 1
1011  tempering_acceptance%Ave_sign = tempering_acceptance%Ave_sign + 1.d0
1012  if (toggle) tempering_acceptance%Obs_vec(1) = tempering_acceptance%Obs_vec(1) + cmplx(1.d0,0.d0,kind(0.d0))
1013 
1014  end Subroutine global_tempering_obser
1015 !--------------------------------------------------------------------
1021 !--------------------------------------------------------------------
1022  Subroutine global_tempering_pr
1023  Implicit none
1024  Call print_bin_vec(tempering_acceptance,group_comm)
1025  end Subroutine global_tempering_pr
1026 
1027 
1028 end Module global_mod
This module defines the Obser_Vec and Obser_Latt types and provides routine to initialize them and to...
Wrappers for linear algebra.
Definition: mymats_mod.F90:45
Handles global updates and parallel tempering.
Definition: Global_mod.F90:45
This module handles the calculation of the acceptance ratio. It also monitors the precision of the co...
Definition: control_mod.F90:46
Handles UDV decompositions.
Handles UDV decompositions.
This module contains two version of the stabilization. To switch between the two schemes you should d...
Handles Hubbard Stratonovitch fields.
Definition: Fields_mod.F90:58
This module defines the interface between the Hamiltonians (= model and observables definition) and t...