subroutine write_spin_diff_stats(initial)
! routine to print out the instant spin-difference to a file
! routine to print out the instant spin-difference to a file
logical, intent(in), optional :: initial
character(*), parameter :: this_routine = "write_spin_diff_stats"
type(write_state_t), save :: state
logical, save :: inited = .false.
integer :: i
character(12) :: num
def_default(state%init, initial, .false.)
if (iProcIndex == root .and. .not. inited) then
state%funit = get_free_unit()
call init_spin_diff_output(state%funit)
inited = .true.
end if
if (iProcIndex == root) then
if (state%init .or. state%prepend) then
write(state%funit, '("#")', advance='no')
state%prepend = state%init
else if (.not. state%prepend) then
write(state%funit, '(" ")', advance='no')
end if
state%cols = 0
state%cols_mc = 0
state%mc_out = tMCOutput
call stats_out(state, .false., iter + PreviousCycles, 'Iter.')
do i = 1, nBasis / 2
write(num, '(i12)') i
call stats_out(state, .false., all_inst_spin_diff(i) / &
(sum(all_norm_psi_squared) / real(inum_runs, dp) * real(StepsSft, dp)), &
'orbital '//trim(adjustl(num)))
end do
write(state%funit, *)
call neci_flush(state%funit)
end if
end subroutine write_spin_diff_stats