9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-06-22 12:32:04 +02:00
qp2/src/cipsi/pt2_stoch_routines.irp.f

867 lines
26 KiB
Fortran
Raw Normal View History

2019-01-25 11:39:31 +01:00
BEGIN_PROVIDER [ integer, pt2_stoch_istate ]
implicit none
BEGIN_DOC
! State for stochatsic PT2
END_DOC
pt2_stoch_istate = 1
END_PROVIDER
BEGIN_PROVIDER [ integer, pt2_F, (N_det_generators) ]
&BEGIN_PROVIDER [ integer, pt2_n_tasks_max ]
implicit none
logical, external :: testTeethBuilding
2019-02-03 17:30:28 +01:00
integer :: i,j
2020-04-06 00:03:59 +02:00
pt2_n_tasks_max = elec_alpha_num*elec_alpha_num + elec_alpha_num*elec_beta_num - n_core_orb*2
2019-02-03 17:30:28 +01:00
pt2_n_tasks_max = min(pt2_n_tasks_max,1+N_det_generators/10000)
call write_int(6,pt2_n_tasks_max,'pt2_n_tasks_max')
2021-04-27 00:35:18 +02:00
pt2_F(:) = max(int(sqrt(float(pt2_n_tasks_max))),1)
2019-02-04 13:20:24 +01:00
do i=1,pt2_n_0(1+pt2_N_teeth/4)
2020-11-08 16:52:39 +01:00
pt2_F(i) = pt2_n_tasks_max*pt2_min_parallel_tasks
2019-02-03 17:30:28 +01:00
enddo
2020-11-08 16:52:39 +01:00
do i=1+pt2_n_0(pt2_N_teeth-pt2_N_teeth/4), pt2_n_0(pt2_N_teeth-pt2_N_teeth/10)
pt2_F(i) = pt2_min_parallel_tasks
enddo
do i=1+pt2_n_0(pt2_N_teeth-pt2_N_teeth/10), N_det_generators
2019-02-03 17:30:28 +01:00
pt2_F(i) = 1
2019-01-25 11:39:31 +01:00
enddo
2019-02-03 17:30:28 +01:00
2019-01-25 11:39:31 +01:00
END_PROVIDER
BEGIN_PROVIDER [ integer, pt2_N_teeth ]
&BEGIN_PROVIDER [ integer, pt2_minDetInFirstTeeth ]
implicit none
logical, external :: testTeethBuilding
if(N_det_generators < 1024) then
pt2_minDetInFirstTeeth = 1
pt2_N_teeth = 1
else
pt2_minDetInFirstTeeth = min(5, N_det_generators)
2019-02-04 14:43:30 +01:00
do pt2_N_teeth=100,2,-1
2019-01-25 11:39:31 +01:00
if(testTeethBuilding(pt2_minDetInFirstTeeth, pt2_N_teeth)) exit
end do
end if
call write_int(6,pt2_N_teeth,'Number of comb teeth')
END_PROVIDER
logical function testTeethBuilding(minF, N)
implicit none
integer, intent(in) :: minF, N
integer :: n0, i
double precision :: u0, Wt, r
double precision, allocatable :: tilde_w(:), tilde_cW(:)
integer, external :: dress_find_sample
double precision :: rss
double precision, external :: memory_of_double, memory_of_int
rss = memory_of_double(2*N_det_generators+1)
call check_mem(rss,irp_here)
allocate(tilde_w(N_det_generators), tilde_cW(0:N_det_generators))
2020-08-23 02:07:20 +02:00
double precision :: norm2
norm2 = 0.d0
2019-01-25 11:39:31 +01:00
do i=N_det_generators,1,-1
2019-02-04 14:43:30 +01:00
tilde_w(i) = psi_coef_sorted_gen(i,pt2_stoch_istate) * &
psi_coef_sorted_gen(i,pt2_stoch_istate)
2020-08-23 02:07:20 +02:00
norm2 = norm2 + tilde_w(i)
2019-01-25 11:39:31 +01:00
enddo
2020-08-23 02:07:20 +02:00
f = 1.d0/norm2
2019-02-04 14:43:30 +01:00
tilde_w(:) = tilde_w(:) * f
2019-01-25 11:39:31 +01:00
tilde_cW(0) = -1.d0
do i=1,N_det_generators
tilde_cW(i) = tilde_cW(i-1) + tilde_w(i)
enddo
tilde_cW(:) = tilde_cW(:) + 1.d0
2019-10-30 15:28:46 +01:00
deallocate(tilde_w)
2019-01-25 11:39:31 +01:00
n0 = 0
testTeethBuilding = .false.
2019-02-03 17:30:28 +01:00
double precision :: f
integer :: minFN
minFN = N_det_generators - minF * N
f = 1.d0/dble(N)
2019-01-25 11:39:31 +01:00
do
u0 = tilde_cW(n0)
r = tilde_cW(n0 + minF)
2020-04-06 00:03:59 +02:00
Wt = (1d0 - u0) * f
2019-01-25 11:39:31 +01:00
if (dabs(Wt) <= 1.d-3) then
2019-10-30 15:28:46 +01:00
exit
2019-01-25 11:39:31 +01:00
endif
if(Wt >= r - u0) then
testTeethBuilding = .true.
2019-10-30 15:28:46 +01:00
exit
2019-01-25 11:39:31 +01:00
end if
n0 += 1
2019-02-03 17:30:28 +01:00
if(n0 > minFN) then
2019-10-30 15:28:46 +01:00
exit
2019-01-25 11:39:31 +01:00
end if
end do
2019-10-30 15:28:46 +01:00
deallocate(tilde_cW)
2019-01-25 11:39:31 +01:00
end function
2020-08-31 01:45:36 +02:00
subroutine ZMQ_pt2(E, pt2_data, pt2_data_err, relative_error, N_in)
2019-01-25 11:39:31 +01:00
use f77_zmq
use selection_types
implicit none
integer(ZMQ_PTR) :: zmq_to_qp_run_socket, zmq_socket_pull
integer, intent(in) :: N_in
2020-04-06 00:03:59 +02:00
! integer, intent(inout) :: N_in
2019-01-25 11:39:31 +01:00
double precision, intent(in) :: relative_error, E(N_states)
2020-08-31 01:45:36 +02:00
type(pt2_type), intent(inout) :: pt2_data, pt2_data_err
2020-08-28 00:10:46 +02:00
!
2019-01-25 11:39:31 +01:00
integer :: i, N
double precision :: state_average_weight_save(N_states), w(N_states,4)
integer(ZMQ_PTR), external :: new_zmq_to_qp_run_socket
type(selection_buffer) :: b
PROVIDE psi_bilinear_matrix_columns_loc psi_det_alpha_unique psi_det_beta_unique
PROVIDE psi_bilinear_matrix_rows psi_det_sorted_order psi_bilinear_matrix_order
PROVIDE psi_bilinear_matrix_transp_rows_loc psi_bilinear_matrix_transp_columns
PROVIDE psi_bilinear_matrix_transp_order psi_selectors_coef_transp psi_det_sorted
PROVIDE psi_det_hii selection_weight pseudo_sym
2021-06-25 00:11:57 +02:00
PROVIDE n_act_orb n_inact_orb n_core_orb n_virt_orb n_del_orb seniority_max
PROVIDE pert_2rdm excitation_beta_max excitation_alpha_max excitation_max
2019-01-25 11:39:31 +01:00
if (h0_type == 'CFG') then
PROVIDE psi_configuration_hii det_to_configuration
2019-01-28 11:51:38 +01:00
endif
2019-01-25 11:39:31 +01:00
2020-04-06 00:03:59 +02:00
if (N_det <= max(4,N_states) .or. pt2_N_teeth < 2) then
2020-08-28 15:39:01 +02:00
call ZMQ_selection(N_in, pt2_data)
2019-01-25 11:39:31 +01:00
else
N = max(N_in,1) * N_states
state_average_weight_save(:) = state_average_weight(:)
2019-02-04 13:20:24 +01:00
if (int(N,8)*2_8 > huge(1)) then
print *, irp_here, ': integer too large'
stop -1
endif
2019-01-25 11:39:31 +01:00
call create_selection_buffer(N, N*2, b)
ASSERT (associated(b%det))
ASSERT (associated(b%val))
do pt2_stoch_istate=1,N_states
state_average_weight(:) = 0.d0
state_average_weight(pt2_stoch_istate) = 1.d0
2019-10-24 13:55:38 +02:00
TOUCH state_average_weight pt2_stoch_istate selection_weight
2019-01-25 11:39:31 +01:00
PROVIDE nproc pt2_F mo_two_e_integrals_in_map mo_one_e_integrals pt2_w
PROVIDE psi_selectors pt2_u pt2_J pt2_R
call new_parallel_job(zmq_to_qp_run_socket, zmq_socket_pull, 'pt2')
integer, external :: zmq_put_psi
integer, external :: zmq_put_N_det_generators
integer, external :: zmq_put_N_det_selectors
integer, external :: zmq_put_dvector
integer, external :: zmq_put_ivector
if (zmq_put_psi(zmq_to_qp_run_socket,1) == -1) then
stop 'Unable to put psi on ZMQ server'
endif
if (zmq_put_N_det_generators(zmq_to_qp_run_socket, 1) == -1) then
stop 'Unable to put N_det_generators on ZMQ server'
endif
if (zmq_put_N_det_selectors(zmq_to_qp_run_socket, 1) == -1) then
stop 'Unable to put N_det_selectors on ZMQ server'
endif
if (zmq_put_dvector(zmq_to_qp_run_socket,1,'energy',pt2_e0_denominator,size(pt2_e0_denominator)) == -1) then
stop 'Unable to put energy on ZMQ server'
endif
if (zmq_put_dvector(zmq_to_qp_run_socket,1,'state_average_weight',state_average_weight,N_states) == -1) then
stop 'Unable to put state_average_weight on ZMQ server'
endif
2019-06-05 17:34:36 +02:00
if (zmq_put_dvector(zmq_to_qp_run_socket,1,'selection_weight',selection_weight,N_states) == -1) then
stop 'Unable to put selection_weight on ZMQ server'
endif
2019-01-25 11:39:31 +01:00
if (zmq_put_ivector(zmq_to_qp_run_socket,1,'pt2_stoch_istate',pt2_stoch_istate,1) == -1) then
stop 'Unable to put pt2_stoch_istate on ZMQ server'
endif
2021-03-12 11:41:58 +01:00
if (zmq_put_dvector(zmq_to_qp_run_socket,1,'threshold_generators',(/threshold_generators/),1) == -1) then
2019-01-25 11:39:31 +01:00
stop 'Unable to put threshold_generators on ZMQ server'
endif
integer, external :: add_task_to_taskserver
character(300000) :: task
integer :: j,k,ipos,ifirst
ifirst=0
ipos=0
do i=1,N_det_generators
if (pt2_F(i) > 1) then
ipos += 1
endif
enddo
call write_int(6,sum(pt2_F),'Number of tasks')
call write_int(6,ipos,'Number of fragmented tasks')
ipos=1
do i= 1, N_det_generators
do j=1,pt2_F(pt2_J(i))
write(task(ipos:ipos+30),'(I9,1X,I9,1X,I9,''|'')') j, pt2_J(i), N_in
ipos += 30
if (ipos > 300000-30) then
if (add_task_to_taskserver(zmq_to_qp_run_socket,trim(task(1:ipos))) == -1) then
stop 'Unable to add task to task server'
endif
ipos=1
if (ifirst == 0) then
ifirst=1
if (zmq_set_running(zmq_to_qp_run_socket) == -1) then
print *, irp_here, ': Failed in zmq_set_running'
endif
endif
endif
end do
enddo
if (ipos > 1) then
if (add_task_to_taskserver(zmq_to_qp_run_socket,trim(task(1:ipos))) == -1) then
stop 'Unable to add task to task server'
endif
endif
integer, external :: zmq_set_running
if (zmq_set_running(zmq_to_qp_run_socket) == -1) then
print *, irp_here, ': Failed in zmq_set_running'
endif
double precision :: mem_collector, mem, rss
call resident_memory(rss)
mem_collector = 8.d0 * & ! bytes
( 1.d0*pt2_n_tasks_max & ! task_id, index
+ 0.635d0*N_det_generators & ! f,d
2020-08-31 22:39:40 +02:00
+ pt2_n_tasks_max*pt2_type_size(N_states) & ! pt2_data_task
+ N_det_generators*pt2_type_size(N_states) & ! pt2_data_I
2019-01-25 11:39:31 +01:00
+ 4.d0*(pt2_N_teeth+1) & ! S, S2, T2, T3
+ 1.d0*(N_int*2.d0*N + N) & ! selection buffer
+ 1.d0*(N_int*2.d0*N + N) & ! sort selection buffer
) / 1024.d0**3
integer :: nproc_target, ii
nproc_target = nthreads_pt2
ii = min(N_det, (elec_alpha_num*(mo_num-elec_alpha_num))**2)
do
mem = mem_collector + & !
nproc_target * 8.d0 * & ! bytes
( 0.5d0*pt2_n_tasks_max & ! task_id
+ 64.d0*pt2_n_tasks_max & ! task
2020-08-31 22:39:40 +02:00
+ pt2_type_size(N_states)*pt2_n_tasks_max*N_states & ! pt2, variance, overlap
2019-01-25 11:39:31 +01:00
+ 1.d0*pt2_n_tasks_max & ! i_generator, subset
2019-01-31 11:26:13 +01:00
+ 1.d0*(N_int*2.d0*ii+ ii) & ! selection buffer
+ 1.d0*(N_int*2.d0*ii+ ii) & ! sort selection buffer
2019-01-25 11:39:31 +01:00
+ 2.0d0*(ii) & ! preinteresting, interesting,
! prefullinteresting, fullinteresting
+ 2.0d0*(N_int*2*ii) & ! minilist, fullminilist
+ 1.0d0*(N_states*mo_num*mo_num) & ! mat
) / 1024.d0**3
if (nproc_target == 0) then
call check_mem(mem,irp_here)
nproc_target = 1
exit
endif
if (mem+rss < qp_max_mem) then
exit
endif
nproc_target = nproc_target - 1
enddo
call write_int(6,nproc_target,'Number of threads for PT2')
call write_double(6,mem,'Memory (Gb)')
2021-11-29 10:39:34 +01:00
call set_multiple_levels_omp(.False.)
2019-01-25 11:39:31 +01:00
2020-09-03 23:14:08 +02:00
print '(A)', '========== ======================= ===================== ===================== ==========='
2020-09-03 16:16:16 +02:00
print '(A)', ' Samples Energy Variance Norm^2 Seconds'
2020-09-03 23:14:08 +02:00
print '(A)', '========== ======================= ===================== ===================== ==========='
2019-01-25 11:39:31 +01:00
2020-04-06 00:03:59 +02:00
PROVIDE global_selection_buffer
2020-08-28 16:05:53 +02:00
2019-01-25 11:39:31 +01:00
!$OMP PARALLEL DEFAULT(shared) NUM_THREADS(nproc_target+1) &
!$OMP PRIVATE(i)
i = omp_get_thread_num()
if (i==0) then
2020-08-31 01:45:36 +02:00
call pt2_collector(zmq_socket_pull, E(pt2_stoch_istate),relative_error, pt2_data, pt2_data_err, b, N)
2020-08-28 16:05:53 +02:00
pt2_data % rpt2(pt2_stoch_istate) = &
2020-08-31 22:39:40 +02:00
pt2_data % pt2(pt2_stoch_istate)/(1.d0+pt2_data % overlap(pt2_stoch_istate,pt2_stoch_istate))
2020-08-28 16:05:53 +02:00
!TODO : We should use here the correct formula for the error of X/Y
2020-08-31 01:45:36 +02:00
pt2_data_err % rpt2(pt2_stoch_istate) = &
2020-08-31 22:39:40 +02:00
pt2_data_err % pt2(pt2_stoch_istate)/(1.d0 + pt2_data % overlap(pt2_stoch_istate,pt2_stoch_istate))
2019-01-25 11:39:31 +01:00
else
call pt2_slave_inproc(i)
endif
!$OMP END PARALLEL
call end_parallel_job(zmq_to_qp_run_socket, zmq_socket_pull, 'pt2')
2021-11-29 10:39:34 +01:00
call set_multiple_levels_omp(.True.)
2019-01-25 11:39:31 +01:00
2020-09-03 23:14:08 +02:00
print '(A)', '========== ======================= ===================== ===================== ==========='
2019-01-25 11:39:31 +01:00
2020-09-02 10:47:29 +02:00
do k=1,N_states
pt2_overlap(pt2_stoch_istate,k) = pt2_data % overlap(k,pt2_stoch_istate)
enddo
2020-08-31 23:04:34 +02:00
SOFT_TOUCH pt2_overlap
2019-01-25 11:39:31 +01:00
enddo
FREE pt2_stoch_istate
2020-09-03 18:12:58 +02:00
! Symmetrize overlap
do j=2,N_states
do i=1,j-1
pt2_overlap(i,j) = 0.5d0 * (pt2_overlap(i,j) + pt2_overlap(j,i))
pt2_overlap(j,i) = pt2_overlap(i,j)
enddo
enddo
print *, 'Overlap of perturbed states:'
do k=1,N_states
print *, pt2_overlap(k,:)
enddo
print *, '-------'
2019-01-25 11:39:31 +01:00
if (N_in > 0) then
b%cur = min(N_in,b%cur)
if (s2_eig) then
call make_selection_buffer_s2(b)
else
call remove_duplicates_in_selection_buffer(b)
endif
call fill_H_apply_buffer_no_selection(b%cur,b%det,N_int,0)
endif
call delete_selection_buffer(b)
state_average_weight(:) = state_average_weight_save(:)
TOUCH state_average_weight
2021-02-11 01:03:24 +01:00
call update_pt2_and_variance_weights(pt2_data, N_states)
2019-01-25 11:39:31 +01:00
endif
2019-05-15 12:29:39 +02:00
2019-01-25 11:39:31 +01:00
end subroutine
subroutine pt2_slave_inproc(i)
implicit none
integer, intent(in) :: i
2020-04-06 00:03:59 +02:00
PROVIDE global_selection_buffer
2019-01-25 11:39:31 +01:00
call run_pt2_slave(1,i,pt2_e0_denominator)
end
2020-08-31 01:45:36 +02:00
subroutine pt2_collector(zmq_socket_pull, E, relative_error, pt2_data, pt2_data_err, b, N_)
2019-01-25 11:39:31 +01:00
use f77_zmq
use selection_types
use bitmasks
implicit none
integer(ZMQ_PTR), intent(in) :: zmq_socket_pull
double precision, intent(in) :: relative_error, E
2020-08-31 01:45:36 +02:00
type(pt2_type), intent(inout) :: pt2_data, pt2_data_err
2019-01-25 11:39:31 +01:00
type(selection_buffer), intent(inout) :: b
integer, intent(in) :: N_
2020-08-29 01:15:48 +02:00
type(pt2_type), allocatable :: pt2_data_task(:)
2020-08-29 11:28:59 +02:00
type(pt2_type), allocatable :: pt2_data_I(:)
type(pt2_type), allocatable :: pt2_data_S(:)
type(pt2_type), allocatable :: pt2_data_S2(:)
type(pt2_type) :: pt2_data_teeth
2019-01-25 11:39:31 +01:00
integer(ZMQ_PTR),external :: new_zmq_to_qp_run_socket
integer(ZMQ_PTR) :: zmq_to_qp_run_socket
2019-01-31 17:23:47 +01:00
integer, external :: zmq_delete_tasks_async_send
integer, external :: zmq_delete_tasks_async_recv
2019-01-25 11:39:31 +01:00
integer, external :: zmq_abort
integer, external :: pt2_find_sample_lr
2020-08-29 11:28:59 +02:00
PROVIDE pt2_stoch_istate
2019-01-25 11:39:31 +01:00
integer :: more, n, i, p, c, t, n_tasks, U
integer, allocatable :: task_id(:)
integer, allocatable :: index(:)
2020-08-31 22:39:40 +02:00
double precision :: v, x, x2, x3, avg, avg2, avg3(N_states), eqt, E0, v0, n0(N_states)
double precision :: eqta(N_states)
2019-01-25 11:39:31 +01:00
double precision :: time, time1, time0
integer, allocatable :: f(:)
logical, allocatable :: d(:)
2019-01-31 17:23:47 +01:00
logical :: do_exit, stop_now, sending
2019-01-25 11:39:31 +01:00
logical, external :: qp_stop
type(selection_buffer) :: b2
double precision :: rss
double precision, external :: memory_of_double, memory_of_int
2019-01-31 17:23:47 +01:00
sending =.False.
2019-01-25 11:39:31 +01:00
rss = memory_of_int(pt2_n_tasks_max*2+N_det_generators*2)
rss += memory_of_double(N_states*N_det_generators)*3.d0
rss += memory_of_double(N_states*pt2_n_tasks_max)*3.d0
rss += memory_of_double(pt2_N_teeth+1)*4.d0
call check_mem(rss,irp_here)
! If an allocation is added here, the estimate of the memory should also be
! updated in ZMQ_pt2
allocate(task_id(pt2_n_tasks_max), index(pt2_n_tasks_max), f(N_det_generators))
allocate(d(N_det_generators+1))
2020-08-29 01:15:48 +02:00
allocate(pt2_data_task(pt2_n_tasks_max))
2020-08-29 11:28:59 +02:00
allocate(pt2_data_I(N_det_generators))
allocate(pt2_data_S(pt2_N_teeth+1))
allocate(pt2_data_S2(pt2_N_teeth+1))
2019-01-25 11:39:31 +01:00
zmq_to_qp_run_socket = new_zmq_to_qp_run_socket()
call create_selection_buffer(N_, N_*2, b2)
2020-08-28 16:05:53 +02:00
pt2_data % pt2(pt2_stoch_istate) = -huge(1.)
2020-08-31 01:45:36 +02:00
pt2_data_err % pt2(pt2_stoch_istate) = huge(1.)
2020-08-28 16:05:53 +02:00
pt2_data % variance(pt2_stoch_istate) = huge(1.)
2020-08-31 01:45:36 +02:00
pt2_data_err % variance(pt2_stoch_istate) = huge(1.)
2020-08-31 22:39:40 +02:00
pt2_data % overlap(:,pt2_stoch_istate) = 0.d0
pt2_data_err % overlap(:,pt2_stoch_istate) = huge(1.)
2019-01-25 11:39:31 +01:00
n = 1
t = 0
U = 0
2020-08-29 11:28:59 +02:00
do i=1,pt2_n_tasks_max
call pt2_alloc(pt2_data_task(i),N_states)
enddo
do i=1,pt2_N_teeth+1
call pt2_alloc(pt2_data_S(i),N_states)
call pt2_alloc(pt2_data_S2(i),N_states)
enddo
do i=1,N_det_generators
call pt2_alloc(pt2_data_I(i),N_states)
enddo
2019-01-25 11:39:31 +01:00
f(:) = pt2_F(:)
d(:) = .false.
n_tasks = 0
E0 = E
v0 = 0.d0
2020-08-31 22:39:40 +02:00
n0(:) = 0.d0
2019-01-25 11:39:31 +01:00
more = 1
call wall_time(time0)
time1 = time0
do_exit = .false.
stop_now = .false.
do while (n <= N_det_generators)
if(f(pt2_J(n)) == 0) then
d(pt2_J(n)) = .true.
do while(d(U+1))
U += 1
end do
! Deterministic part
do while(t <= pt2_N_teeth)
if(U >= pt2_n_0(t+1)) then
t=t+1
E0 = 0.d0
v0 = 0.d0
2020-08-31 22:39:40 +02:00
n0(:) = 0.d0
2019-01-25 11:39:31 +01:00
do i=pt2_n_0(t),1,-1
2020-08-29 11:28:59 +02:00
E0 += pt2_data_I(i) % pt2(pt2_stoch_istate)
v0 += pt2_data_I(i) % variance(pt2_stoch_istate)
2020-08-31 22:39:40 +02:00
n0(:) += pt2_data_I(i) % overlap(:,pt2_stoch_istate)
2019-01-25 11:39:31 +01:00
end do
else
exit
end if
end do
! Add Stochastic part
c = pt2_R(n)
if(c > 0) then
2020-08-29 11:28:59 +02:00
call pt2_alloc(pt2_data_teeth,N_states)
2019-01-25 11:39:31 +01:00
do p=pt2_N_teeth, 1, -1
v = pt2_u_0 + pt2_W_T * (pt2_u(c) + dble(p-1))
i = pt2_find_sample_lr(v, pt2_cW,pt2_n_0(p),pt2_n_0(p+1))
2020-08-30 22:16:39 +02:00
v = pt2_W_T / pt2_w(i)
call pt2_add ( pt2_data_teeth, v, pt2_data_I(i) )
2020-08-29 11:28:59 +02:00
call pt2_add ( pt2_data_S(p), 1.d0, pt2_data_teeth )
call pt2_add2( pt2_data_S2(p), 1.d0, pt2_data_teeth )
2020-08-30 22:16:39 +02:00
enddo
2020-08-29 11:28:59 +02:00
call pt2_dealloc(pt2_data_teeth)
2020-08-31 01:45:36 +02:00
2020-08-29 11:28:59 +02:00
avg = E0 + pt2_data_S(t) % pt2(pt2_stoch_istate) / dble(c)
avg2 = v0 + pt2_data_S(t) % variance(pt2_stoch_istate) / dble(c)
2020-08-31 22:39:40 +02:00
avg3(:) = n0(:) + pt2_data_S(t) % overlap(:,pt2_stoch_istate) / dble(c)
2019-01-25 11:39:31 +01:00
if ((avg /= 0.d0) .or. (n == N_det_generators) ) then
do_exit = .true.
endif
if (qp_stop()) then
stop_now = .True.
endif
2020-08-28 16:05:53 +02:00
pt2_data % pt2(pt2_stoch_istate) = avg
pt2_data % variance(pt2_stoch_istate) = avg2
2020-08-31 22:39:40 +02:00
pt2_data % overlap(:,pt2_stoch_istate) = avg3(:)
2019-02-04 13:20:24 +01:00
call wall_time(time)
2019-01-25 11:39:31 +01:00
! 1/(N-1.5) : see Brugger, The American Statistician (23) 4 p. 32 (1969)
if(c > 2) then
2020-08-29 11:28:59 +02:00
eqt = dabs((pt2_data_S2(t) % pt2(pt2_stoch_istate) / c) - (pt2_data_S(t) % pt2(pt2_stoch_istate)/c)**2) ! dabs for numerical stability
2019-01-25 11:39:31 +01:00
eqt = sqrt(eqt / (dble(c) - 1.5d0))
2020-08-31 01:45:36 +02:00
pt2_data_err % pt2(pt2_stoch_istate) = eqt
2020-08-29 11:28:59 +02:00
eqt = dabs((pt2_data_S2(t) % variance(pt2_stoch_istate) / c) - (pt2_data_S(t) % variance(pt2_stoch_istate)/c)**2) ! dabs for numerical stability
eqt = sqrt(eqt / (dble(c) - 1.5d0))
2020-08-31 01:45:36 +02:00
pt2_data_err % variance(pt2_stoch_istate) = eqt
2020-08-31 22:39:40 +02:00
eqta(:) = dabs((pt2_data_S2(t) % overlap(:,pt2_stoch_istate) / c) - (pt2_data_S(t) % overlap(:,pt2_stoch_istate)/c)**2) ! dabs for numerical stability
eqta(:) = sqrt(eqta(:) / (dble(c) - 1.5d0))
pt2_data_err % overlap(:,pt2_stoch_istate) = eqta(:)
2020-08-29 11:28:59 +02:00
2019-01-25 11:39:31 +01:00
if ((time - time1 > 1.d0) .or. (n==N_det_generators)) then
time1 = time
2020-09-03 23:14:08 +02:00
print '(I10, X, F12.6, X, G10.3, X, F10.6, X, G10.3, X, F10.6, X, G10.3, X, F10.4)', c, &
2020-09-03 16:16:16 +02:00
pt2_data % pt2(pt2_stoch_istate) +E, &
pt2_data_err % pt2(pt2_stoch_istate), &
pt2_data % variance(pt2_stoch_istate), &
pt2_data_err % variance(pt2_stoch_istate), &
pt2_data % overlap(pt2_stoch_istate,pt2_stoch_istate), &
pt2_data_err % overlap(pt2_stoch_istate,pt2_stoch_istate), &
time-time0
2019-01-25 11:39:31 +01:00
if (stop_now .or. ( &
2020-08-31 01:45:36 +02:00
(do_exit .and. (dabs(pt2_data_err % pt2(pt2_stoch_istate)) / &
2020-08-28 16:05:53 +02:00
(1.d-20 + dabs(pt2_data % pt2(pt2_stoch_istate)) ) <= relative_error))) ) then
2019-01-25 11:39:31 +01:00
if (zmq_abort(zmq_to_qp_run_socket) == -1) then
call sleep(10)
if (zmq_abort(zmq_to_qp_run_socket) == -1) then
print *, irp_here, ': Error in sending abort signal (2)'
endif
endif
endif
endif
endif
end if
n += 1
else if(more == 0) then
exit
else
2020-08-29 01:15:48 +02:00
call pull_pt2_results(zmq_socket_pull, index, pt2_data_task, task_id, n_tasks, b2)
2019-10-24 13:44:40 +02:00
if(n_tasks > pt2_n_tasks_max)then
print*,'PB !!!'
print*,'If you see this, send a bug report with the following content'
2019-10-24 13:44:40 +02:00
print*,irp_here
2020-04-06 00:03:59 +02:00
print*,'n_tasks,pt2_n_tasks_max = ',n_tasks,pt2_n_tasks_max
stop -1
2019-10-24 13:44:40 +02:00
endif
2019-01-31 17:23:47 +01:00
if (zmq_delete_tasks_async_send(zmq_to_qp_run_socket,task_id,n_tasks,sending) == -1) then
2019-02-05 18:44:03 +01:00
stop 'PT2: Unable to delete tasks (send)'
2019-01-25 11:39:31 +01:00
endif
do i=1,n_tasks
2020-08-29 11:28:59 +02:00
if(index(i).gt.size(pt2_data_I,1).or.index(i).lt.1)then
2019-10-24 13:44:40 +02:00
print*,'PB !!!'
2020-08-29 11:28:59 +02:00
print*,'If you see this, send a bug report with the following content'
2019-10-24 13:44:40 +02:00
print*,irp_here
2020-08-29 11:28:59 +02:00
print*,'i,index(i),size(pt2_data_I,1) = ',i,index(i),size(pt2_data_I,1)
2020-04-06 00:03:59 +02:00
stop -1
2019-10-24 13:44:40 +02:00
endif
2020-08-29 11:28:59 +02:00
call pt2_add(pt2_data_I(index(i)),1.d0,pt2_data_task(i))
2019-01-25 11:39:31 +01:00
f(index(i)) -= 1
end do
do i=1, b2%cur
2019-01-31 11:57:46 +01:00
! We assume the pulled buffer is sorted
2019-01-25 11:39:31 +01:00
if (b2%val(i) > b%mini) exit
2019-02-04 13:20:24 +01:00
call add_to_selection_buffer(b, b2%det(1,1,i), b2%val(i))
2019-01-25 11:39:31 +01:00
end do
2019-01-31 17:23:47 +01:00
if (zmq_delete_tasks_async_recv(zmq_to_qp_run_socket,more,sending) == -1) then
2019-02-05 18:44:03 +01:00
stop 'PT2: Unable to delete tasks (recv)'
2019-01-31 17:23:47 +01:00
endif
2019-01-25 11:39:31 +01:00
end if
end do
2020-08-29 11:28:59 +02:00
do i=1,N_det_generators
call pt2_dealloc(pt2_data_I(i))
enddo
do i=1,pt2_N_teeth+1
call pt2_dealloc(pt2_data_S(i))
call pt2_dealloc(pt2_data_S2(i))
enddo
do i=1,pt2_n_tasks_max
call pt2_dealloc(pt2_data_task(i))
enddo
2019-01-31 11:26:13 +01:00
!print *, 'deleting b2'
2019-01-25 11:39:31 +01:00
call delete_selection_buffer(b2)
2019-01-31 11:26:13 +01:00
!print *, 'sorting b'
2019-01-25 11:39:31 +01:00
call sort_selection_buffer(b)
2019-01-31 11:26:13 +01:00
!print *, 'done'
2019-01-25 11:39:31 +01:00
call end_zmq_to_qp_run_socket(zmq_to_qp_run_socket)
end subroutine
integer function pt2_find_sample(v, w)
implicit none
double precision, intent(in) :: v, w(0:N_det_generators)
integer, external :: pt2_find_sample_lr
pt2_find_sample = pt2_find_sample_lr(v, w, 0, N_det_generators)
end function
integer function pt2_find_sample_lr(v, w, l_in, r_in)
implicit none
double precision, intent(in) :: v, w(0:N_det_generators)
integer, intent(in) :: l_in,r_in
integer :: i,l,r
l=l_in
r=r_in
do while(r-l > 1)
i = shiftr(r+l,1)
if(w(i) < v) then
l = i
else
r = i
end if
end do
i = r
do r=i+1,N_det_generators
if (w(r) /= w(i)) then
exit
endif
enddo
pt2_find_sample_lr = r-1
end function
BEGIN_PROVIDER [ integer, pt2_n_tasks ]
implicit none
BEGIN_DOC
! Number of parallel tasks for the Monte Carlo
END_DOC
pt2_n_tasks = N_det_generators
END_PROVIDER
BEGIN_PROVIDER[ double precision, pt2_u, (N_det_generators)]
implicit none
integer, allocatable :: seed(:)
integer :: m,i
call random_seed(size=m)
allocate(seed(m))
do i=1,m
seed(i) = i
enddo
call random_seed(put=seed)
deallocate(seed)
call RANDOM_NUMBER(pt2_u)
END_PROVIDER
BEGIN_PROVIDER[ integer, pt2_J, (N_det_generators)]
&BEGIN_PROVIDER[ integer, pt2_R, (N_det_generators)]
implicit none
BEGIN_DOC
! pt2_J contains the list of generators after ordering them according to the
! Monte Carlo sampling.
!
! pt2_R(i) is the number of combs drawn when determinant i is computed.
END_DOC
2019-01-25 11:39:31 +01:00
integer :: N_c, N_j
integer :: U, t, i
double precision :: v
integer, external :: pt2_find_sample_lr
logical, allocatable :: pt2_d(:)
integer :: m,l,r,k
integer :: ncache
integer, allocatable :: ii(:,:)
double precision :: dt
ncache = min(N_det_generators,10000)
double precision :: rss
double precision, external :: memory_of_double, memory_of_int
rss = memory_of_int(ncache)*dble(pt2_N_teeth) + memory_of_int(N_det_generators)
call check_mem(rss,irp_here)
allocate(ii(pt2_N_teeth,ncache),pt2_d(N_det_generators))
pt2_R(:) = 0
pt2_d(:) = .false.
N_c = 0
N_j = pt2_n_0(1)
do i=1,N_j
pt2_d(i) = .true.
pt2_J(i) = i
end do
U = 0
do while(N_j < pt2_n_tasks)
if (N_c+ncache > N_det_generators) then
ncache = N_det_generators - N_c
endif
!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(dt,v,t,k)
do k=1, ncache
dt = pt2_u_0
do t=1, pt2_N_teeth
v = dt + pt2_W_T *pt2_u(N_c+k)
dt = dt + pt2_W_T
ii(t,k) = pt2_find_sample_lr(v, pt2_cW,pt2_n_0(t),pt2_n_0(t+1))
end do
enddo
!$OMP END PARALLEL DO
do k=1,ncache
!ADD_COMB
N_c = N_c+1
do t=1, pt2_N_teeth
i = ii(t,k)
if(.not. pt2_d(i)) then
N_j += 1
pt2_J(N_j) = i
pt2_d(i) = .true.
end if
end do
pt2_R(N_j) = N_c
!FILL_TOOTH
do while(U < N_det_generators)
U += 1
if(.not. pt2_d(U)) then
N_j += 1
pt2_J(N_j) = U
pt2_d(U) = .true.
exit
end if
end do
if (N_j >= pt2_n_tasks) exit
end do
enddo
if(N_det_generators > 1) then
pt2_R(N_det_generators-1) = 0
pt2_R(N_det_generators) = N_c
end if
deallocate(ii,pt2_d)
END_PROVIDER
2019-10-24 13:55:38 +02:00
BEGIN_PROVIDER [ double precision, pt2_w, (N_det_generators) ]
&BEGIN_PROVIDER [ double precision, pt2_cW, (0:N_det_generators) ]
&BEGIN_PROVIDER [ double precision, pt2_W_T ]
&BEGIN_PROVIDER [ double precision, pt2_u_0 ]
&BEGIN_PROVIDER [ integer, pt2_n_0, (pt2_N_teeth+1) ]
implicit none
integer :: i, t
double precision, allocatable :: tilde_w(:), tilde_cW(:)
double precision :: r, tooth_width
integer, external :: pt2_find_sample
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
double precision :: rss
double precision, external :: memory_of_double, memory_of_int
rss = memory_of_double(2*N_det_generators+1)
call check_mem(rss,irp_here)
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
if (N_det_generators == 1) then
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
pt2_w(1) = 1.d0
pt2_cw(1) = 1.d0
pt2_u_0 = 1.d0
pt2_W_T = 0.d0
pt2_n_0(1) = 0
pt2_n_0(2) = 1
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
else
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
allocate(tilde_w(N_det_generators), tilde_cW(0:N_det_generators))
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
tilde_cW(0) = 0d0
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
do i=1,N_det_generators
tilde_w(i) = psi_coef_sorted_gen(i,pt2_stoch_istate)**2 !+ 1.d-20
enddo
2020-04-06 00:03:59 +02:00
2020-08-23 02:07:20 +02:00
double precision :: norm2
norm2 = 0.d0
2019-10-24 13:55:38 +02:00
do i=N_det_generators,1,-1
2020-08-23 02:07:20 +02:00
norm2 += tilde_w(i)
2019-10-24 13:55:38 +02:00
enddo
2020-04-06 00:03:59 +02:00
2020-08-23 02:07:20 +02:00
tilde_w(:) = tilde_w(:) / norm2
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
tilde_cW(0) = -1.d0
do i=1,N_det_generators
tilde_cW(i) = tilde_cW(i-1) + tilde_w(i)
enddo
tilde_cW(:) = tilde_cW(:) + 1.d0
2019-10-30 15:28:46 +01:00
2019-10-24 13:55:38 +02:00
pt2_n_0(1) = 0
do
pt2_u_0 = tilde_cW(pt2_n_0(1))
r = tilde_cW(pt2_n_0(1) + pt2_minDetInFirstTeeth)
pt2_W_T = (1d0 - pt2_u_0) / dble(pt2_N_teeth)
if(pt2_W_T >= r - pt2_u_0) then
exit
end if
pt2_n_0(1) += 1
if(N_det_generators - pt2_n_0(1) < pt2_minDetInFirstTeeth * pt2_N_teeth) then
print *, "teeth building failed"
stop -1
end if
end do
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
do t=2, pt2_N_teeth
r = pt2_u_0 + pt2_W_T * dble(t-1)
pt2_n_0(t) = pt2_find_sample(r, tilde_cW)
end do
pt2_n_0(pt2_N_teeth+1) = N_det_generators
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
pt2_w(:pt2_n_0(1)) = tilde_w(:pt2_n_0(1))
do t=1, pt2_N_teeth
tooth_width = tilde_cW(pt2_n_0(t+1)) - tilde_cW(pt2_n_0(t))
if (tooth_width == 0.d0) then
tooth_width = sum(tilde_w(pt2_n_0(t):pt2_n_0(t+1)))
endif
ASSERT(tooth_width > 0.d0)
do i=pt2_n_0(t)+1, pt2_n_0(t+1)
pt2_w(i) = tilde_w(i) * pt2_W_T / tooth_width
end do
end do
2020-04-06 00:03:59 +02:00
2019-10-24 13:55:38 +02:00
pt2_cW(0) = 0d0
do i=1,N_det_generators
pt2_cW(i) = pt2_cW(i-1) + pt2_w(i)
end do
pt2_n_0(pt2_N_teeth+1) = N_det_generators
endif
2019-01-25 11:39:31 +01:00
END_PROVIDER