22use omp_lib, 
only : omp_get_wtime
 
   45    real(dp), 
allocatable :: vscales(:)
 
   48    real(dp), 
allocatable :: v4pi2lp1(:)
 
   53    real(dp), 
allocatable :: vscales_rel(:)
 
   59    real(dp), 
allocatable :: vfact(:)
 
   63    real(dp), 
allocatable :: vcnk(:)
 
   67    real(dp), 
allocatable :: m2l_ztranslate_coef(:, :, :)
 
   71    real(dp), 
allocatable :: m2l_ztranslate_adj_coef(:, :, :)
 
   73    real(dp), 
allocatable :: cgrid(:, :)
 
   75    real(dp), 
allocatable :: wgrid(:)
 
   83    integer :: vgrid_nbasis
 
   86    real(dp), 
allocatable :: vgrid(:, :)
 
   90    real(dp), 
allocatable :: vwgrid(:, :)
 
   92    real(dp), 
allocatable :: vgrid2(:, :)
 
   99    real(dp), 
allocatable :: pchi(:, :, :)
 
  102    real(dp), 
allocatable :: c_ik(:, :)
 
  104    real(dp), 
allocatable :: si_ri(:, :)
 
  107    real(dp), 
allocatable :: di_ri(:, :)
 
  109    real(dp), 
allocatable :: sk_ri(:, :)
 
  112    real(dp), 
allocatable :: dk_ri(:, :)
 
  114    real(dp), 
allocatable :: termimat(:, :)
 
  116    real(dp), 
allocatable :: b(:,:,:)
 
  118    real(dp), 
allocatable :: l(:,:,:)
 
  123    integer, 
allocatable :: inl(:)
 
  126    integer, 
allocatable :: nl(:)
 
  128    integer, 
allocatable :: itrnl(:)
 
  131    real(dp), 
allocatable :: fi(:, :)
 
  134    real(dp), 
allocatable :: ui(:, :)
 
  137    real(dp), 
allocatable :: ui_cav(:)
 
  140    real(dp), 
allocatable :: zi(:, :, :)
 
  144    integer, 
allocatable :: ncav_sph(:)
 
  146    real(dp), 
allocatable :: ccav(:, :)
 
  148    integer, 
allocatable :: icav_ia(:)
 
  150    integer, 
allocatable :: icav_ja(:)
 
  153    real(dp), 
allocatable :: rx_prc(:, :, :)
 
  157    integer, 
allocatable :: order(:)
 
  162    integer, 
allocatable :: cluster(:, :)
 
  165    integer, 
allocatable :: children(:, :)
 
  168    integer, 
allocatable :: parent(:)
 
  171    real(dp), 
allocatable :: cnode(:, :)
 
  174    real(dp), 
allocatable :: rnode(:)
 
  177    integer, 
allocatable :: snode(:)
 
  180    real(dp), 
allocatable :: sk_rnode(:, :)
 
  183    real(dp), 
allocatable :: si_rnode(:, :)
 
  190    integer, 
allocatable :: nfar(:)
 
  193    integer, 
allocatable :: nnear(:)
 
  196    integer, 
allocatable :: far(:)
 
  199    integer, 
allocatable :: near(:)
 
  203    integer, 
allocatable :: sfar(:)
 
  207    integer, 
allocatable :: snear(:)
 
  216    integer :: m2p_nbasis
 
  220    integer :: grad_nbasis
 
  222    real(dp) :: inner_tol
 
  242    type(ddx_error_type), 
intent(inout) :: ddx_error
 
  244    integer :: i, alloc_size, l, indl, igrid, isph, l0, &
 
  245        & NZ, ierr, info, tmp_pmax
 
  246    real(dp) :: rho, ctheta, stheta, cphi, sphi, termi, termk, &
 
  248    real(dp), 
allocatable :: vplm(:), vcos(:), vsin(:), vylm(:), SK_rijn(:), &
 
  250    complex(dp), 
allocatable :: bessel_work(:)
 
  254    if (ddx_error % flag .ne. 0) 
then 
  255        call update_error(ddx_error, 
"constants_init received input in error " // &
 
  261    constants % dodiag = .false.
 
  263    constants % nbasis = (params % lmax+1) ** 2
 
  265    constants % n = params % nsph * constants % nbasis
 
  267    if (params % fmm .eq. 0) 
then 
  268        if (params % force .eq. 1) 
then 
  269            constants % dmax = params % lmax + 1
 
  270            constants % vgrid_dmax = params % lmax + 1
 
  272            constants % dmax = params % lmax
 
  273            constants % vgrid_dmax = params % lmax
 
  276        constants % m2p_lmax = -1
 
  277        constants % m2p_nbasis = -1
 
  278        constants % grad_nbasis = -1
 
  282        if (params % force .eq. 1) 
then 
  283            constants % dmax = max(params % pm+params % pl+1, &
 
  285            constants % m2p_lmax = params % lmax + 1
 
  286            constants % grad_nbasis = (params % lmax+2) ** 2
 
  287            constants % vgrid_dmax = max(params % pl, params % lmax) + 1
 
  289            constants % dmax = max(params % pm+params % pl, &
 
  291            constants % m2p_lmax = params % lmax
 
  292            constants % grad_nbasis = -1
 
  293            constants % vgrid_dmax = max(params % pl, params % lmax)
 
  295        constants % m2p_nbasis = (constants % m2p_lmax+1) ** 2
 
  298    constants % vgrid_nbasis = (constants % vgrid_dmax+1) ** 2
 
  299    constants % nfact = 2*constants % dmax+1
 
  300    constants % nscales = (constants % dmax+1) ** 2
 
  302    allocate(constants % vscales(constants % nscales), stat=info)
 
  303    if (info .ne. 0) 
then 
  304        call update_error(ddx_error, 
"constants_init: `vscales` allocation " &
 
  308    allocate(constants % v4pi2lp1(constants % dmax+1), stat=info)
 
  309    if (info .ne. 0) 
then 
  310        call update_error(ddx_error, 
"constants_init: `v4pi2lp1` allocation " &
 
  314    allocate(constants % vscales_rel(constants % nscales), stat=info)
 
  315    if (info .ne. 0) 
then 
  316        call update_error(ddx_error, 
"constants_init: `vscales_rel` " // &
 
  317            & 
"allocation failed")
 
  321    call ylmscale(constants % dmax, constants % vscales, &
 
  322        & constants % v4pi2lp1, constants % vscales_rel)
 
  324    allocate(constants % vfact(constants % nfact), stat=info)
 
  325    if (info .ne. 0) 
then 
  326        call update_error(ddx_error, 
"constants_init: `vfact` allocation failed")
 
  330    constants % vfact(1) = 1
 
  331    do i = 2, constants % nfact
 
  332        constants % vfact(i) = constants % vfact(i-1) * sqrt(dble(i-1))
 
  336    if (params % fmm .eq. 1 .or. &
 
  337        & (params % model .eq. 3 .and. params % force .eq. 1)) 
then 
  338        alloc_size = 2*constants % dmax + 1
 
  339        alloc_size = alloc_size * (constants % dmax+1)
 
  341        allocate(constants % vcnk(alloc_size), stat=info)
 
  342        if (info .ne. 0) 
then 
  343            call update_error(ddx_error, 
"constants_init: `vcnk` allocation " &
 
  348        allocate(constants % m2l_ztranslate_coef(&
 
  349            & (params % pm+1), (params % pl+1), (params % pl+1)), &
 
  351        if (info .ne. 0) 
then 
  352            call update_error(ddx_error, 
"constants_init: " // &
 
  353                & 
"`m2l_ztranslate_coef` allocation failed")
 
  357        allocate(constants % m2l_ztranslate_adj_coef(&
 
  358            & (params % pl+1), (params % pl+1), (params % pm+1)), &
 
  360        if (info .ne. 0) 
then 
  361            call update_error(ddx_error, 
"constants_init: " // &
 
  362                & 
"`m2l_ztranslate_adj_coef` allocation failed")
 
  367            & params % pl, constants % vcnk, &
 
  368            & constants % m2l_ztranslate_coef, &
 
  369            & constants % m2l_ztranslate_adj_coef)
 
  372    allocate(constants % cgrid(3, params % ngrid), &
 
  373        & constants % wgrid(params % ngrid), stat=info)
 
  374    if (info .ne. 0) 
then 
  375        call update_error(ddx_error, 
"constants_init: `cgrid` and `wgrid` " // &
 
  376            & 
"allocations failed")
 
  380    call llgrid(params % ngrid, constants % wgrid, constants % cgrid)
 
  383    allocate(constants % vgrid(constants % vgrid_nbasis, params % ngrid), &
 
  384        & constants % vwgrid(constants % vgrid_nbasis, params % ngrid), &
 
  386    if (info .ne. 0) 
then 
  387        call update_error(ddx_error, 
"constants_init: `vgrid`, `wgrid` and" // &
 
  388            & 
" allocations failed")
 
  391    allocate(vplm(constants % vgrid_nbasis), vcos(constants % vgrid_dmax+1), &
 
  392        & vsin(constants % vgrid_dmax+1), stat=info)
 
  393    if (info .ne. 0) 
then 
  394        call update_error(ddx_error, 
"constants_init: `vplm`, `vcos` and " // &
 
  395            & 
"`vsin` allocations failed")
 
  400    do igrid = 1, params % ngrid
 
  401        call ylmbas(constants % cgrid(:, igrid), rho, ctheta, stheta, cphi, &
 
  402            & sphi, constants % vgrid_dmax, constants % vscales, &
 
  403            & constants % vgrid(:, igrid), vplm, vcos, vsin)
 
  404        constants % vwgrid(:, igrid) = constants % wgrid(igrid) * &
 
  405            & constants % vgrid(:, igrid)
 
  407    if (params % fmm .eq. 1) 
then 
  408        allocate(constants % vgrid2( &
 
  409            & constants % vgrid_nbasis, params % ngrid), &
 
  411        if (info .ne. 0) 
then 
  412            call update_error(ddx_error, 
"constants_init: `vgrid2` " // &
 
  413                & 
"allocation failed")
 
  416        do igrid = 1, params % ngrid
 
  417            do l = 0, constants % vgrid_dmax
 
  419                constants % vgrid2(indl-l:indl+l, igrid) = &
 
  420                    & constants % vgrid(indl-l:indl+l, igrid) / &
 
  421                    & constants % vscales(indl)**2
 
  428    if (ddx_error % flag .ne. 0) 
then 
  429        call update_error(ddx_error, 
"constants_geometry_init returned an " // &
 
  435    if (params % model .eq. 3) 
then 
  436        constants % lmax0 = params % lmax
 
  437        constants % nbasis0 = constants % nbasis
 
  438        allocate(vylm(constants % vgrid_nbasis), &
 
  439            & sk_rijn(0:constants % lmax0), dk_rijn(0:constants % lmax0), &
 
  440            & bessel_work(constants % dmax+2), stat=info)
 
  441        if (info .ne. 0) 
then 
  442            call update_error(ddx_error, 
"constants_init: `vylm`, `SK_rijn` " // &
 
  443                & 
"and `DK_rijn` allocations failed")
 
  446        allocate(constants % SI_ri(0:constants % dmax+1, params % nsph), &
 
  447            & constants % DI_ri(0:constants % dmax+1, params % nsph), &
 
  448            & constants % SK_ri(0:params % lmax+1, params % nsph), &
 
  449            & constants % DK_ri(0:params % lmax+1, params % nsph), &
 
  450            & constants % Pchi(constants % nbasis, constants % nbasis0, params % nsph), &
 
  451            & constants % C_ik(0:params % lmax, params % nsph), &
 
  452            & constants % termimat(0:params % lmax, params % nsph), &
 
  454        if (info .ne. 0) 
then 
  455            call update_error(ddx_error, 
"constants_init: allocation failed")
 
  460        do isph = 1, params % nsph
 
  463            call modified_spherical_bessel_first_kind(constants % dmax+1, &
 
  464                & params % rsph(isph)*params % kappa,&
 
  465                & constants % SI_ri(:, isph), constants % DI_ri(:, isph), &
 
  467            call modified_spherical_bessel_second_kind(params % lmax+1, &
 
  468                & params % rsph(isph)*params % kappa, &
 
  469                & constants % SK_ri(:, isph), constants % DK_ri(:, isph), &
 
  474            call mkpmat(params, constants, isph, constants % Pchi(:, :, isph))
 
  476            do l = 0, params % lmax
 
  477                constants % termimat(l, isph) = constants % DI_ri(l, isph) / &
 
  478                    & constants % SI_ri(l, isph) * params % kappa
 
  481            do l0 = 0, constants % lmax0
 
  482                termi = constants % DI_ri(l0, isph) / &
 
  483                    & constants % SI_ri(l0, isph) * params % kappa
 
  484                termk = constants % DK_ri(l0, isph)/ &
 
  485                    & constants % SK_ri(l0, isph) * params % kappa
 
  486                constants % C_ik(l0, isph) = one / (termi-termk)
 
  489        if (params % fmm .eq. 1) 
then 
  490            tmp_pmax = max(params % pm, params % pl)
 
  491            allocate(constants % SI_rnode(tmp_pmax+1, constants % nclusters), &
 
  492                & constants % SK_rnode(tmp_pmax+1, constants % nclusters), &
 
  495                call update_error(ddx_error, 
"constants_init: `si_rnode, " // &
 
  496                    & 
"sk_rnode allocation failed")
 
  499            do i = 1, constants % nclusters
 
  500                z = constants % rnode(i) * params % kappa
 
  501                s1 = sqrt(two/(pi*real(z)))
 
  502                s2 = sqrt(pi/(two*real(z)))
 
  503                call cbesk(z, pt5, 1, 1, bessel_work(1), nz, ierr)
 
  504                constants % SK_rnode(1, i) = s1 * real(bessel_work(1))
 
  505                call cbesi(z, pt5, 1, 1, bessel_work(1), nz, ierr)
 
  506                constants % SI_rnode(1, i) = s2 * real(bessel_work(1))
 
  507                if (tmp_pmax .gt. 0) 
then 
  508                    call cbesk(z, 1.5d0, 1, tmp_pmax, bessel_work(2:), nz, ierr)
 
  509                    constants % SK_rnode(2:, i) = s1 * real(bessel_work(2:tmp_pmax+1))
 
  510                    call cbesi(z, 1.5d0, 1, tmp_pmax, bessel_work(2:), nz, ierr)
 
  511                    constants % SI_rnode(2:, i) = s2 * real(bessel_work(2:tmp_pmax+1))
 
  515        deallocate(vylm, sk_rijn, dk_rijn, bessel_work, stat=info)
 
  516        if (info .ne. 0) 
then 
  517            call update_error(ddx_error, 
"constants_init: `vylm`, `SK_rijn` " // &
 
  518                & 
"and `DK_rijn` deallocations failed")
 
  522        if (params % matvecmem .eq. 1) 
then 
  523            call build_b(constants, params, ddx_error)
 
  524            if (ddx_error % flag .ne. 0) 
then 
  525                call update_error(ddx_error, 
"build_b returned an " // &
 
  531    deallocate(vplm, vcos, vsin, stat=info)
 
  532    if (info .ne. 0) 
then 
  533        call update_error(ddx_error, 
"constants_init: `vplm`, `vcos` and " // &
 
  534            & 
"`vsin` deallocations failed")
 
  538    if (params % matvecmem .eq. 1) 
then 
  540        if (ddx_error % flag .ne. 0) 
then 
  541            call update_error(ddx_error, 
"build_itrnl returned an " // &
 
  545        call build_l(constants, params, ddx_error)
 
  546        if (ddx_error % flag .ne. 0) 
then 
  547            call update_error(ddx_error, 
"build_l returned an " // &
 
  559    type(ddx_error_type), 
intent(inout) :: ddx_error
 
  560    integer :: isph, ij, jsph, ji, istat
 
  562    allocate(constants % itrnl(constants % inl(params % nsph + 1)), stat=istat)
 
  564        call update_error(ddx_error, 
'Allocation failed in build_itrnl')
 
  568    do isph = 1, params % nsph
 
  569        do ij = constants % inl(isph), constants % inl(isph + 1) - 1
 
  570            jsph = constants % nl(ij)
 
  571            do ji = constants % inl(jsph), constants % inl(jsph + 1) - 1
 
  572                if (constants % nl(ji) .eq. isph) 
exit 
  574            constants % itrnl(ij) = ji
 
  580subroutine build_l(constants, params, ddx_error)
 
  584    type(ddx_error_type), 
intent(inout) :: ddx_error
 
  585    integer :: isph, ij, jsph, igrid, l, m, ind, info
 
  586    real(dp), 
dimension(3) :: vij, sij
 
  587    real(dp) :: vvij, tij, xij, oij, rho, ctheta, stheta, cphi, sphi, &
 
  589    real(dp), 
dimension(constants % nbasis) :: vylm, vplm
 
  590    real(dp), 
dimension(params % lmax + 1) :: vcos, vsin
 
  591    real(dp), 
dimension(constants % nbasis, params % ngrid) :: scratch
 
  594    allocate(constants % l(constants % nbasis, constants % nbasis, &
 
  595        & constants % inl(params % nsph + 1)), stat=info)
 
  596    if (info .ne. 0) 
then 
  597        call update_error(ddx_error, 
'Allocation failed in build_l')
 
  601    thigh = one + pt5*(params % se + one)*params % eta
 
  607    do isph = 1, params % nsph
 
  608        do ij = constants % inl(isph), constants % inl(isph + 1) - 1
 
  609            jsph = constants % nl(ij)
 
  611            do igrid = 1, params % ngrid
 
  612                if (constants % ui(igrid, isph).eq.one) cycle
 
  613                vij = params % csph(:, isph) &
 
  614                    & + params % rsph(isph)*constants % cgrid(:,igrid) &
 
  615                    & - params % csph(:, jsph)
 
  616                vvij = sqrt(dot_product(vij, vij))
 
  617                tij = vvij/params % rsph(jsph)
 
  618                if (tij.lt.thigh) 
then 
  619                    if (tij.ne.zero) 
then 
  624                    xij = 
fsw(tij, params % se, params % eta)
 
  625                    if (constants % fi(igrid, isph).gt.one) 
then 
  626                        oij = xij/constants % fi(igrid, isph)
 
  630                    call ylmbas(sij, rho, ctheta, stheta, cphi, sphi, &
 
  631                        & params % lmax, constants % vscales, vylm, vplm, &
 
  634                    do l = 0, params % lmax
 
  636                        fac = - tt/(constants % vscales(ind)**2)
 
  638                            scratch(ind + m, igrid) = fac*vylm(ind + m)
 
  644            call dgemm(
'n', 
't', constants % nbasis, constants % nbasis, params % ngrid, &
 
  645                & one, constants % vwgrid, constants % vgrid_nbasis, scratch, &
 
  646                & constants % nbasis, zero, constants % l(:,:,ij), constants % nbasis)
 
  652subroutine build_b(constants, params, ddx_error)
 
  656    type(ddx_error_type), 
intent(inout) :: ddx_error
 
  657    integer :: isph, ij, jsph, igrid, l, m, ind
 
  658    real(dp), 
dimension(3) :: vij, sij
 
  659    real(dp) :: vvij, tij, xij, oij, rho, ctheta, stheta, cphi, sphi, &
 
  661    real(dp), 
dimension(constants % nbasis) :: vylm, vplm
 
  662    real(dp), 
dimension(params % lmax + 1) :: vcos, vsin
 
  663    complex(dp), 
dimension(max(2, params % lmax + 1)) :: bessel_work
 
  664    real(dp), 
dimension(0:params % lmax) :: SI_rijn, DI_rijn
 
  665    real(dp), 
dimension(constants % nbasis, params % ngrid) :: scratch
 
  669    allocate(constants % b(constants % nbasis, constants % nbasis, &
 
  670        & constants % inl(params % nsph + 1)), stat=info)
 
  672        call update_error(ddx_error, 
"Allocation failed in build_b")
 
  676    thigh = one + pt5*(params % se + one)*params % eta
 
  683    do isph = 1, params % nsph
 
  684        do ij = constants % inl(isph), constants % inl(isph + 1) - 1
 
  685            jsph = constants % nl(ij)
 
  687            do igrid = 1, params % ngrid
 
  688                if (constants % ui(igrid, isph).eq.one) cycle
 
  689                vij = params % csph(:, isph) &
 
  690                    & + params % rsph(isph)*constants % cgrid(:,igrid) &
 
  691                    & - params % csph(:, jsph)
 
  692                vvij = sqrt(dot_product(vij, vij))
 
  693                tij = vvij/params % rsph(jsph)
 
  694                if (tij.lt.thigh) 
then 
  695                    if (tij.ne.zero) 
then 
  700                    xij = 
fsw(tij, params % se, params % eta)
 
  701                    if (constants % fi(igrid, isph).gt.one) 
then 
  702                        oij = xij/constants % fi(igrid, isph)
 
  706                    call ylmbas(sij, rho, ctheta, stheta, cphi, sphi, &
 
  707                        & params % lmax, constants % vscales, vylm, vplm, &
 
  711                    vvtij = vvij*params % kappa
 
  712                    call modified_spherical_bessel_first_kind(params % lmax, &
 
  713                        & vvtij, si_rijn, di_rijn, bessel_work)
 
  714                    do l = 0, params % lmax
 
  715                        fac = - oij*si_rijn(l)/constants % SI_ri(l, jsph)
 
  718                            scratch(ind + m, igrid) = fac*vylm(ind + m)
 
  723            call dgemm(
'n', 
't', constants % nbasis, constants % nbasis, params % ngrid, &
 
  724                & one, constants % vwgrid, constants % vgrid_nbasis, scratch, &
 
  725                & constants % nbasis, zero, constants % b(:,:,ij), constants % nbasis)
 
  731subroutine mkpmat(params, constants, isph, pmat)
 
  734    integer,  
intent(in) :: isph
 
  735    real(dp), 
dimension(constants % nbasis, constants % nbasis0), 
intent(inout) :: pmat
 
  736    integer :: l, m, ind, l0, m0, ind0, its
 
  739    do its = 1, params % ngrid
 
  740        if (constants % ui(its,isph).ne.0) 
then 
  741            do l = 0, params % lmax
 
  744                    f = constants % wgrid(its) * constants % vgrid(ind+m,its) * constants % ui(its,isph)
 
  745                    do l0 = 0, constants % lmax0
 
  746                        ind0 = l0*l0 + l0 + 1
 
  748                            f0 = constants % vgrid(ind0+m0,its)
 
  749                            pmat(ind+m,ind0+m0) = pmat(ind+m,ind0+m0) + f * f0
 
  772    type(ddx_error_type), 
intent(inout) :: ddx_error
 
  774    real(dp) :: swthr, v(3), maxv, ssqv, vv, t
 
  775    integer :: i, isph, jsph, inear, igrid, iwork, jwork, lwork, &
 
  776        & old_lwork, icav, info
 
  777    integer, 
allocatable :: work(:, :), tmp_work(:, :)
 
  778    real(dp) :: start_time
 
  781    start_time = omp_get_wtime()
 
  782    if (params % fmm .eq. 1) 
then 
  784        allocate(constants % order(params % nsph), stat=info)
 
  785        if (info .ne. 0) 
then 
  786            call update_error(ddx_error, 
"constants_geometry_init: `order` " &
 
  787                & // 
"allocation failed")
 
  790        constants % nclusters = 2*params % nsph - 1
 
  791        allocate(constants % cluster(2, constants % nclusters), stat=info)
 
  792        if (info .ne. 0) 
then 
  793            call update_error(ddx_error, 
"constants_geometry_init: `cluster` " &
 
  794                & // 
"allocation failed")
 
  797        allocate(constants % children(2, constants % nclusters), stat=info)
 
  798        if (info .ne. 0) 
then 
  799            call update_error(ddx_error, 
"constants_geometry_init: " // &
 
  800                & 
"`children` allocation failed")
 
  803        allocate(constants % parent(constants % nclusters), stat=info)
 
  804        if (info .ne. 0) 
then 
  805            call update_error(ddx_error, 
"constants_geometry_init: `parent` " &
 
  806                & // 
"allocation failed")
 
  809        allocate(constants % cnode(3, constants % nclusters), stat=info)
 
  810        if (info .ne. 0) 
then 
  811            call update_error(ddx_error, 
"constants_geometry_init: `cnode` " &
 
  812                & // 
"allocation failed")
 
  815        allocate(constants % rnode(constants % nclusters), stat=info)
 
  816        if (info .ne. 0) 
then 
  817            call update_error(ddx_error, 
"constants_geometry_init: `rnode` " &
 
  818                & // 
"allocation failed")
 
  821        allocate(constants % snode(params % nsph), stat=info)
 
  822        if (info .ne. 0) 
then 
  823            call update_error(ddx_error, 
"constants_geometry_init: `snode` " &
 
  824                & // 
"allocation failed")
 
  827        allocate(constants % nfar(constants % nclusters), stat=info)
 
  828        if (info .ne. 0) 
then 
  829            call update_error(ddx_error, 
"constants_geometry_init: `nfar` " &
 
  830                & // 
"allocation failed")
 
  833        allocate(constants % nnear(constants % nclusters), stat=info)
 
  834        if (info .ne. 0) 
then 
  835            call update_error(ddx_error, 
"constants_geometry_init: `nnear` " &
 
  836                & // 
"allocation failed")
 
  840        call tree_rib_build(params % nsph, params % csph, params % rsph, &
 
  841            & constants % order, constants % cluster, constants % children, &
 
  842            & constants % parent, constants % cnode, constants % rnode, &
 
  843            & constants % snode, ddx_error)
 
  844        if (ddx_error % flag .ne. 0) 
then 
  845            call update_error(ddx_error, 
"tree_rib_build returned an " // &
 
  846                & 
"ddx_error, exiting")
 
  853        allocate(work(3, lwork), stat=info)
 
  854        if (info .ne. 0) 
then 
  855            call update_error(ddx_error, 
"constants_geometry_init: `work` " &
 
  856                & // 
"allocation failed")
 
  859        do while (iwork .le. jwork)
 
  860            allocate(tmp_work(3, lwork), stat=info)
 
  861            if (info .ne. 0) 
then 
  862            call update_error(ddx_error, 
"constants_geometry_init: " // &
 
  863                    & 
"`tmp_work` allocation failed")
 
  867            deallocate(work, stat=info)
 
  868            if (info .ne. 0) 
then 
  869            call update_error(ddx_error, 
"constants_geometry_init: " // &
 
  870                    & 
"`work` deallocation failed")
 
  874            lwork = old_lwork + 1000*params % nsph
 
  875            allocate(work(3, lwork), stat=info)
 
  876            if (info .ne. 0) 
then 
  877            call update_error(ddx_error, 
"constants_geometry_init: " // &
 
  878                    & 
"`work` allocation failed")
 
  881            work(:, 1:old_lwork) = tmp_work
 
  882            deallocate(tmp_work, stat=info)
 
  883            if (info .ne. 0) 
then 
  884            call update_error(ddx_error, 
"constants_geometry_init: " // &
 
  885                    & 
"`tmp_work` deallocation failed")
 
  889                & constants % children, constants % cnode, &
 
  890                & constants % rnode, lwork, iwork, jwork, work, &
 
  891                & constants % nnfar, constants % nfar, constants % nnnear, &
 
  894        allocate(constants % sfar(constants % nclusters+1), &
 
  895            & constants % snear(constants % nclusters+1), stat=info)
 
  896        if (info .ne. 0) 
then 
  897            call update_error(ddx_error, 
"constants_geometry_init: `sfar` " // &
 
  898                & 
"and `snear` allocations failed")
 
  901        allocate(constants % far(constants % nnfar), stat=info)
 
  902        if (info .ne. 0) 
then 
  903            call update_error(ddx_error, 
"constants_geometry_init: `far` " // &
 
  904                & 
"allocation failed")
 
  907        allocate(constants % near(constants % nnnear), stat=info)
 
  908        if (info .ne. 0) 
then 
  909            call update_error(ddx_error, 
"constants_geometry_init: `near` " // &
 
  910                & 
"allocation failed")
 
  914            & constants % nnfar, constants % nfar, constants % sfar, &
 
  915            & constants % far, constants % nnnear, constants % nnear, &
 
  916            & constants % snear, constants % near)
 
  917        deallocate(work, stat=info)
 
  918        if (info .ne. 0) 
then 
  919            call update_error(ddx_error, 
"constants_geometry_init: `work` " // &
 
  920                & 
"deallocation failed")
 
  925    swthr = one + (params % se+one)*params % eta/two
 
  927    if (params % fmm .eq. 1) 
then 
  932    if (ddx_error % flag .ne. 0) 
then 
  933        call update_error(ddx_error, 
"Neighbor list construction failed")
 
  937    allocate(constants % fi(params % ngrid, params % nsph), &
 
  938        & constants % ui(params % ngrid, params % nsph), stat=info)
 
  939    if (info .ne. 0) 
then 
  940        call update_error(ddx_error, 
"`fi` and `ui` allocations failed")
 
  943    constants % fi = zero
 
  944    constants % ui = zero
 
  946    if (params % force .eq. 1) 
then 
  947        allocate(constants % zi(3, params % ngrid, params % nsph), stat=info)
 
  948        if (info .ne. 0) 
then 
  949            call update_error(ddx_error, 
"`zi` allocation failed")
 
  952        constants % zi = zero
 
  957    do isph = 1, params % nsph
 
  958        do igrid = 1, params % ngrid
 
  960            do inear = constants % inl(isph), constants % inl(isph+1)-1
 
  962                jsph = constants % nl(inear)
 
  964                v = params % csph(:, isph) - params % csph(:, jsph) + &
 
  965                    & params % rsph(isph)*constants % cgrid(:, igrid)
 
  966                maxv = max(abs(v(1)), abs(v(2)), abs(v(3)))
 
  967                if (maxv .gt. zero) 
then 
  968                    ssqv = (v(1)/maxv)**2 + (v(2)/maxv)**2 + (v(3)/maxv)**2
 
  972                vv = maxv * sqrt(ssqv)
 
  973                t = vv / params % rsph(jsph)
 
  975                constants % fi(igrid, isph) = constants % fi(igrid, isph) + &
 
  976                    & 
fsw(t, params % se, params % eta)
 
  978                if (params % force .eq. 1) 
then 
  980                    if ((t .lt. swthr) .and. (t .gt. swthr-params % eta)) 
then 
  982                        vv = 
dfsw(t, params % se, params % eta) / &
 
  983                            & params % rsph(jsph) / vv
 
  984                        constants % zi(:, igrid, isph) = &
 
  985                            & constants % zi(:, igrid, isph) + vv*v
 
  990            if (constants % fi(igrid, isph) .le. one) 
then 
  991                constants % ui(igrid, isph) = one - constants % fi(igrid, isph)
 
  996    allocate(constants % ncav_sph(params % nsph), stat=info)
 
  997    if (info .ne. 0) 
then 
  998        call update_error(ddx_error, 
"`ncav_sph` allocation failed")
 
 1003    do isph = 1, params % nsph
 
 1004        constants % ncav_sph(isph) = 0
 
 1006        do i = 1, params % ngrid
 
 1008            if (constants % ui(i, isph) .gt. zero) 
then 
 1009                constants % ncav_sph(isph) = constants % ncav_sph(isph) + 1
 
 1013    constants % ncav = sum(constants % ncav_sph)
 
 1015    allocate(constants % ccav(3, constants % ncav), &
 
 1016        & constants % icav_ia(params % nsph+1), &
 
 1017        & constants % icav_ja(constants % ncav), stat=info)
 
 1018    if (info .ne. 0) 
then 
 1019        call update_error(ddx_error, 
"`ccav`, `icav_ia` and " // &
 
 1020            & 
"`icav_ja` allocations failed")
 
 1024    allocate(constants % ui_cav(constants % ncav), stat=info)
 
 1025    if (info .ne. 0) 
then 
 1026        call update_error(ddx_error, 
"`ui_cav` allocations failed")
 
 1031    constants % icav_ia(1) = 1
 
 1033    do isph = 1, params % nsph
 
 1034        constants % icav_ia(isph+1) = constants % icav_ia(isph) + &
 
 1035            & constants % ncav_sph(isph)
 
 1037        do igrid = 1, params % ngrid
 
 1039            if (constants % ui(igrid, isph) .eq. zero) cycle
 
 1041            constants % ccav(:, icav) = params % csph(:, isph) + &
 
 1042                & params % rsph(isph)* &
 
 1043                & constants % cgrid(:, igrid)
 
 1045            constants % icav_ja(icav) = igrid
 
 1047            constants % ui_cav(icav) = constants % ui(igrid, isph)
 
 1053    if (params % model .eq. 2) 
then 
 1054        allocate(constants % rx_prc( &
 
 1055            & constants % nbasis, constants % nbasis, params % nsph), &
 
 1057        if (info .ne. 0) 
then 
 1058            call update_error(ddx_error, 
"constants_geometry_init: `rx_prc` " &
 
 1059                & // 
"allocation failed")
 
 1062        call mkprec(params % lmax, constants % nbasis, params % nsph, &
 
 1063            & params % ngrid, params % eps, constants % ui, &
 
 1064            & constants % wgrid, constants % vgrid, constants % vgrid_nbasis, &
 
 1065            & constants % rx_prc, ddx_error)
 
 1066        if (info .ne. 0) 
then 
 1067            call update_error(ddx_error, 
"mkprec returned an ddx_error, exiting")
 
 1077    type(ddx_error_type), 
intent(inout) :: ddx_error
 
 1078    real(dp) :: swthr, v(3), maxv, ssqv, vv, r
 
 1079    integer :: nngmax, i, lnl, isph, jsph, info
 
 1080    integer, 
allocatable :: tmp_nl(:)
 
 1082    swthr = (params % se+one)*params % eta/two
 
 1085    allocate(constants % inl(params % nsph+1), &
 
 1086        & constants % nl(params % nsph*nngmax), stat=info)
 
 1087    if (info .ne. 0) 
then 
 1088        call update_error(ddx_error, 
"`inl` and `nl` allocations failed")
 
 1093    do isph = 1, params % nsph
 
 1094        constants % inl(isph) = lnl + 1
 
 1095        do jsph = 1, params % nsph
 
 1096            if (isph .ne. jsph) 
then 
 1097                v = params % csph(:, isph)-params % csph(:, jsph)
 
 1098                maxv = max(abs(v(1)), abs(v(2)), abs(v(3)))
 
 1099                if (maxv .gt. zero) 
then 
 1100                    ssqv = (v(1)/maxv)**2 + (v(2)/maxv)**2 + (v(3)/maxv)**2
 
 1104                vv = maxv * sqrt(ssqv)
 
 1110                r = params % rsph(isph) + params % rsph(jsph) &
 
 1111                    & + swthr * max(params % rsph(isph), params % rsph(jsph))
 
 1113                    constants % nl(i) = jsph
 
 1117                    if (i .gt. params % nsph*nngmax) 
then 
 1118                        allocate(tmp_nl(params % nsph*nngmax), stat=info)
 
 1119                        if (info .ne. 0) 
then 
 1120                            call update_error(ddx_error, 
"`tmp_nl` " // &
 
 1121                                & 
"allocation failed")
 
 1124                        tmp_nl(1:params % nsph*nngmax) = &
 
 1125                            & constants % nl(1:params % nsph*nngmax)
 
 1126                        deallocate(constants % nl, stat=info)
 
 1127                        if (info .ne. 0) 
then 
 1128                            call update_error(ddx_error, 
"`nl` " // &
 
 1129                                & 
"deallocation failed")
 
 1132                        nngmax = nngmax + 10
 
 1133                        allocate(constants % nl(params % nsph*nngmax), &
 
 1135                        if (info .ne. 0) 
then 
 1136                            call update_error(ddx_error, 
"`nl` " // &
 
 1137                                & 
"allocation failed")
 
 1140                        constants % nl(1:params % nsph*(nngmax-10)) = &
 
 1141                            & tmp_nl(1:params % nsph*(nngmax-10))
 
 1142                        deallocate(tmp_nl, stat=info)
 
 1143                        if (info .ne. 0) 
then 
 1144                            call update_error(ddx_error, 
"`tmp_nl` " // &
 
 1145                                & 
"deallocation failed")
 
 1153    constants % inl(params % nsph+1) = lnl+1
 
 1154    constants % nngmax = nngmax
 
 1162    type(ddx_error_type), 
intent(inout) :: ddx_error
 
 1163    real(dp) :: swthr, v(3), maxv, ssqv, vv, r
 
 1164    integer :: nngmax, i, lnl, isph, jsph, inode, jnode, j, k, info
 
 1165    integer, 
allocatable :: tmp_nl(:)
 
 1167    swthr = (params % se+one)*params % eta/two
 
 1170    allocate(constants % inl(params % nsph+1), &
 
 1171        & constants % nl(params % nsph*nngmax), stat=info)
 
 1172    if (info .ne. 0) 
then 
 1173        call update_error(ddx_error, 
"`inl` and `nl` allocations failed")
 
 1179    do isph = 1, params % nsph
 
 1180        constants % inl(isph) = lnl + 1
 
 1181        inode = constants % snode(isph)
 
 1183        do j = constants % snear(inode), constants % snear(inode+1) - 1
 
 1184            jnode = constants % near(j)
 
 1185            do k = constants % cluster(1, jnode), constants % cluster(2, jnode)
 
 1186                jsph = constants % order(k)
 
 1187                if (isph .ne. jsph) 
then 
 1188                    v = params % csph(:, isph)-params % csph(:, jsph)
 
 1189                    maxv = max(abs(v(1)), abs(v(2)), abs(v(3)))
 
 1190                    if (maxv .gt. zero) 
then 
 1191                        ssqv = (v(1)/maxv)**2 + (v(2)/maxv)**2 + (v(3)/maxv)**2
 
 1195                    vv = maxv * sqrt(ssqv)
 
 1201                    r = params % rsph(isph) + params % rsph(jsph) &
 
 1202                        & + swthr * max(params % rsph(isph), params % rsph(jsph))
 
 1204                        constants % nl(i) = jsph
 
 1208                        if (i .gt. params % nsph*nngmax) 
then 
 1209                            allocate(tmp_nl(params % nsph*nngmax), stat=info)
 
 1210                            if (info .ne. 0) 
then 
 1211                                call update_error(ddx_error, 
"`tmp_nl` " // &
 
 1212                                    & 
"allocation failed")
 
 1215                            tmp_nl(1:params % nsph*nngmax) = &
 
 1216                                & constants % nl(1:params % nsph*nngmax)
 
 1217                            deallocate(constants % nl, stat=info)
 
 1218                            if (info .ne. 0) 
then 
 1219                                call update_error(ddx_error, 
"`nl` " // &
 
 1220                                    & 
"deallocation failed")
 
 1223                            nngmax = nngmax + 10
 
 1224                            allocate(constants % nl(params % nsph*nngmax), &
 
 1226                            if (info .ne. 0) 
then 
 1227                                call update_error(ddx_error, 
"`nl` " // &
 
 1228                                    & 
"allocation failed")
 
 1231                            constants % nl(1:params % nsph*(nngmax-10)) = &
 
 1232                                & tmp_nl(1:params % nsph*(nngmax-10))
 
 1233                            deallocate(tmp_nl, stat=info)
 
 1234                            if (info .ne. 0) 
then 
 1235                                call update_error(ddx_error, 
"`tmp_nl` " // &
 
 1236                                    & 
"deallocation failed")
 
 1245    constants % inl(params % nsph+1) = lnl+1
 
 1246    constants % nngmax = nngmax
 
 1276real(dp) function fsw(t, se, eta)
 
 1278    real(dp), 
intent(in) :: t, se, eta
 
 1280    real(dp) :: a, b, c, x
 
 1285    x = t - (se*pt5 + pt5)*eta
 
 1290    if (a .le. zero) 
then 
 1293    else if (a .ge. eta) 
then 
 1333    real(dp), 
intent(in) :: t, se, eta
 
 1336    real(dp), 
parameter :: f30=30.0d0
 
 1341    x = t - (se + 1.d0)*eta / 2.d0
 
 1345    if (x .ge. one) 
then 
 1347    else if (x .le. flow) 
then 
 1350        dfsw = -f30 * (( (one-x)*(x-one+eta) )**2) / (eta**5)
 
 1357subroutine mkprec(lmax, nbasis, nsph, ngrid, eps, ui, wgrid, vgrid, &
 
 1358        & vgrid_nbasis, rx_prc, ddx_error)
 
 1360    integer, 
intent(in) :: lmax, nbasis, nsph, ngrid, vgrid_nbasis
 
 1361    real(dp), 
intent(in) :: eps, ui(ngrid, nsph), wgrid(ngrid), &
 
 1362        & vgrid(vgrid_nbasis, ngrid)
 
 1364    real(dp), 
intent(out) :: rx_prc(nbasis, nbasis, nsph)
 
 1365    type(ddx_error_type), 
intent(inout) :: ddx_error
 
 1367    integer :: info, isph, lm, l1, m1, ind1, igrid
 
 1369    integer, 
allocatable :: ipiv(:)
 
 1370    real(dp),  
allocatable :: work(:)
 
 1371    external :: dgetrf, dgetri
 
 1373    allocate(ipiv(nbasis), work(nbasis**2), stat=info)
 
 1374    if (info .ne. 0) 
then 
 1375        call update_error(ddx_error, 
"mkprec: `ipiv` and `work` allocation failed")
 
 1383            f = twopi * ui(igrid, isph) * wgrid(igrid)
 
 1385                ind1 = l1*l1 + l1 + 1
 
 1387                    f1 = f * vgrid(ind1+m1, igrid) / (two*dble(l1) + one)
 
 1389                        rx_prc(lm, ind1+m1, isph) = &
 
 1390                            & rx_prc(lm, ind1+m1, isph) + f1*vgrid(lm, igrid)
 
 1397    f = twopi * (eps+one) / (eps-one)
 
 1400            rx_prc(lm, lm, isph) = rx_prc(lm, lm, isph) + f
 
 1405        call dgetrf(nbasis, nbasis, rx_prc(:, :, isph), nbasis, ipiv, info)
 
 1406        if (info .ne. 0) 
then 
 1407            call update_error(ddx_error, 
"mkprec: dgetrf failed")
 
 1410        call dgetri(nbasis, rx_prc(:, :, isph), nbasis, ipiv, work, &
 
 1412        if (info .ne. 0) 
then  
 1413            call update_error(ddx_error, 
"mkprec: dgetri failed")
 
 1418    deallocate(work, ipiv, stat=info)
 
 1419    if (info .ne. 0) 
then 
 1420        call update_error(ddx_error, 
"mkprec: `ipiv` and `work` " // &
 
 1421            & 
"deallocation failed")
 
 1449        & cnode, rnode, snode, ddx_error)
 
 1451    integer, 
intent(in) :: nsph
 
 1452    real(dp), 
intent(in) :: csph(3, nsph), rsph(nsph)
 
 1454    integer, 
intent(out) :: order(nsph), cluster(2, 2*nsph-1), &
 
 1455        & children(2, 2*nsph-1), parent(2*nsph-1), snode(nsph)
 
 1456    real(dp), 
intent(out) :: cnode(3, 2*nsph-1), rnode(2*nsph-1)
 
 1457    type(ddx_error_type), 
intent(inout) :: ddx_error
 
 1459    integer :: nclusters, i, j, n, s, e, div
 
 1460    real(dp) :: r, r1, r2, c(3), c1(3), c2(3), d, maxc
 
 1462    nclusters = 2*nsph - 1
 
 1465    cluster(2, 1) = nsph
 
 1486            if (ddx_error % flag .ne. 0) 
then 
 1487                call update_error(ddx_error, 
"tree_rib_node_bisect returned " // &
 
 1488                    & 
"an ddx_error, exiting")
 
 1493            cluster(2, j) = s + div - 1
 
 1495            cluster(1, j+1) = s + div
 
 1499            children(2, i) = j + 1
 
 1515    do i = nclusters, 1, -1
 
 1518        if (children(1, i) .eq. 0) 
then 
 1520            j = order(cluster(1, i))
 
 1522            cnode(:, i) = csph(:, j)
 
 1538            maxc = max(abs(c(1)), abs(c(2)), abs(c(3)))
 
 1539            if (maxc .eq. zero) 
then 
 1542                d = (c(1)/maxc)**2 + (c(2)/maxc)**2 + (c(3)/maxc)**2
 
 1546            if (r1 .ge. (r2+d)) 
then 
 1550            else if(r2 .ge. (r1+d)) 
then 
 1577    integer, 
intent(in) :: nsph, n
 
 1578    real(dp), 
intent(in) :: csph(3, nsph)
 
 1580    integer, 
intent(inout) :: order(n)
 
 1581    integer, 
intent(out) :: div
 
 1582    type(ddx_error_type), 
intent(inout) :: ddx_error
 
 1584    real(dp) :: c(3),  s(3)
 
 1585    real(dp), 
allocatable :: tmp_csph(:, :), work(:)
 
 1587    integer :: i, l, r, lwork, info, istat
 
 1588    integer, 
allocatable :: tmp_order(:)
 
 1590    allocate(tmp_csph(3, n), tmp_order(n), stat=istat)
 
 1591    if (istat.ne.0) 
then 
 1592        call update_error(ddx_error, 
'Allocation failed in tree_node_bisect')
 
 1599        c = c + csph(:, order(i))
 
 1604        tmp_csph(:, i) = csph(:, order(i)) - c
 
 1609    call dgesvd(
'N', 
'O', 3, n, tmp_csph, 3, s, tmp_csph, 3, tmp_csph, 3, &
 
 1612    allocate(work(lwork), stat=istat)
 
 1613    if (istat.ne.0) 
then 
 1614        call update_error(ddx_error, 
'Allocation failed in tree_node_bisect')
 
 1618    call dgesvd(
'N', 
'O', 3, n, tmp_csph, 3, s, tmp_csph, 3, tmp_csph, 3, &
 
 1619        & work, lwork, info)
 
 1621        call update_error(ddx_error, 
'DGESVD failed in tree_node_bisect')
 
 1624    deallocate(work, stat=istat)
 
 1625    if (istat.ne.0) 
then 
 1626        call update_error(ddx_error, 
'Deallocation failed in tree_node_bisect')
 
 1640        if (tmp_csph(1, i) .ge. zero) 
then 
 1641            tmp_order(l) = order(i)
 
 1645            tmp_order(r) = order(i)
 
 1652    deallocate(tmp_csph, tmp_order, stat=istat)
 
 1653    if (istat.ne.0) 
then 
 1654        call update_error(ddx_error, 
'Deallocation failed in tree_node_bisect')
 
 1684        & jwork, work, nnfar, nfar, nnnear, nnear)
 
 1686    integer, 
intent(in) :: n, children(2, n), lwork
 
 1687    real(dp), 
intent(in) :: cnode(3, n), rnode(n)
 
 1689    integer, 
intent(inout) :: iwork, jwork, work(3, lwork)
 
 1690    integer, 
intent(out) :: nnfar, nfar(n), nnnear, nnear(n)
 
 1692    integer :: j(2), npairs, k1, k2
 
 1693    real(dp) :: c(3), r, d, dmax, dssq
 
 1695    if (iwork .eq. 0) 
then 
 1702    do while (iwork .le. jwork)
 
 1703        j = work(1:2, iwork)
 
 1704        c = cnode(:, j(1)) - cnode(:, j(2))
 
 1705        r = rnode(j(1)) + rnode(j(2)) + max(rnode(j(1)), rnode(j(2)))
 
 1707        dmax = max(abs(c(1)), abs(c(2)), abs(c(3)))
 
 1708        if (dmax .gt. zero) 
then 
 1709            dssq = (c(1)/dmax)**2 + (c(2)/dmax)**2 + (c(3)/dmax)**2
 
 1713        d = dmax * sqrt(dssq)
 
 1718        npairs = max(1, children(2, j(1))-children(1, j(1))+1) * &
 
 1719            & max(1, children(2, j(2))-children(1, j(2))+1)
 
 1723        else if (npairs .eq. 1) 
then 
 1726        else if (jwork+npairs .gt. lwork) 
then 
 1734            if (children(1, j(1)) .eq. 0) 
then 
 1736                do k2 = children(1, j(2)), children(2, j(2))
 
 1741            else if(children(1, j(2)) .eq. 0) 
then 
 1743                do k1 = children(1, j(1)), children(2, j(1))
 
 1749                do k1 = children(1, j(1)), children(2, j(1))
 
 1750                    do k2 = children(1, j(2)), children(2, j(2))
 
 1763        if (work(3, iwork) .eq. 1) 
then 
 1764            nfar(work(1, iwork)) = nfar(work(1, iwork)) + 1
 
 1765        else if (work(3, iwork) .eq. 2) 
then 
 1766            nnear(work(1, iwork)) = nnear(work(1, iwork)) + 1
 
 1777        & nnnear, nnear, snear, near)
 
 1791    integer, 
intent(in) :: jwork, lwork, work(3, lwork), n, nnfar, nnnear
 
 1792    integer, 
intent(in) :: nfar(n), nnear(n)
 
 1793    integer, 
intent(out) :: sfar(n+1), far(nnfar), snear(n+1), near(nnnear)
 
 1795    integer, 
allocatable :: cfar(:), cnear(:)
 
 1797    allocate(cfar(n+1), cnear(n+1))
 
 1801        sfar(i) = sfar(i-1) + nfar(i-1)
 
 1802        snear(i) = snear(i-1) + nnear(i-1)
 
 1807        if (work(3, i) .eq. 1) 
then 
 1810            if ((j .gt. n) .or. (j .le. 0)) 
then 
 1811                write(*,*) 
"ALARM", j
 
 1813            far(cfar(j)) = work(2, i)
 
 1814            cfar(j) = cfar(j) + 1
 
 1815        else if (work(3, i) .eq. 2) 
then 
 1818            if ((j .gt. n) .or. (j .le. 0)) 
then 
 1819                write(*,*) 
"ALARM", j
 
 1821            near(cnear(j)) = work(2, i)
 
 1822            cnear(j) = cnear(j) + 1
 
 1826    deallocate(cfar, cnear)
 
 1838    type(ddx_error_type), 
intent(inout) :: ddx_error
 
 1843    if (
allocated(constants % vscales)) 
then 
 1844        deallocate(constants % vscales, stat=istat)
 
 1845        if (istat .ne. 0) 
then 
 1846            call update_error(ddx_error, 
"`vscales` deallocation failed!")
 
 1849    if (
allocated(constants % v4pi2lp1)) 
then 
 1850        deallocate(constants % v4pi2lp1, stat=istat)
 
 1851        if (istat .ne. 0) 
then 
 1852            call update_error(ddx_error, 
"`v4pi2lp1` deallocation failed!")
 
 1855    if (
allocated(constants % vscales_rel)) 
then 
 1856        deallocate(constants % vscales_rel, stat=istat)
 
 1857        if (istat .ne. 0) 
then 
 1858            call update_error(ddx_error, 
"`vscales_rel` deallocation failed!")
 
 1861    if (
allocated(constants % vfact)) 
then 
 1862        deallocate(constants % vfact, stat=istat)
 
 1863        if (istat .ne. 0) 
then 
 1864            call update_error(ddx_error, 
"`vfact` deallocation failed!")
 
 1867    if (
allocated(constants % vcnk)) 
then 
 1868        deallocate(constants % vcnk, stat=istat)
 
 1869        if (istat .ne. 0) 
then 
 1870            call update_error(ddx_error, 
"`vcnk` deallocation failed!")
 
 1873    if (
allocated(constants % m2l_ztranslate_coef)) 
then 
 1874        deallocate(constants % m2l_ztranslate_coef, stat=istat)
 
 1875        if (istat .ne. 0) 
then 
 1876            call update_error(ddx_error, &
 
 1877                & 
"`m2l_ztranslate_coef` deallocation failed!")
 
 1880    if (
allocated(constants % m2l_ztranslate_adj_coef)) 
then 
 1881        deallocate(constants % m2l_ztranslate_adj_coef, stat=istat)
 
 1882        if (istat .ne. 0) 
then 
 1883            call update_error(ddx_error, &
 
 1884                & 
"`m2l_ztranslate_adj_coef` deallocation failed!")
 
 1887    if (
allocated(constants % cgrid)) 
then 
 1888        deallocate(constants % cgrid, stat=istat)
 
 1889        if (istat .ne. 0) 
then 
 1890            call update_error(ddx_error, 
"`cgrid` deallocation failed!")
 
 1893    if (
allocated(constants % wgrid)) 
then 
 1894        deallocate(constants % wgrid, stat=istat)
 
 1895        if (istat .ne. 0) 
then 
 1896            call update_error(ddx_error, 
"`wgrid` deallocation failed!")
 
 1899    if (
allocated(constants % vgrid)) 
then 
 1900        deallocate(constants % vgrid, stat=istat)
 
 1901        if (istat .ne. 0) 
then 
 1902            call update_error(ddx_error, 
"`vgrid` deallocation failed!")
 
 1905    if (
allocated(constants % vwgrid)) 
then 
 1906        deallocate(constants % vwgrid, stat=istat)
 
 1907        if (istat .ne. 0) 
then 
 1908            call update_error(ddx_error, 
"`vwgrid` deallocation failed!")
 
 1911    if (
allocated(constants % vgrid2)) 
then 
 1912        deallocate(constants % vgrid2, stat=istat)
 
 1913        if (istat .ne. 0) 
then 
 1914            call update_error(ddx_error, 
"`vgrid2` deallocation failed!")
 
 1917    if (
allocated(constants % pchi)) 
then 
 1918        deallocate(constants % pchi, stat=istat)
 
 1919        if (istat .ne. 0) 
then 
 1920            call update_error(ddx_error, 
"`pchi` deallocation failed!")
 
 1923    if (
allocated(constants % c_ik)) 
then 
 1924        deallocate(constants % c_ik, stat=istat)
 
 1925        if (istat .ne. 0) 
then 
 1926            call update_error(ddx_error, 
"`c_ik` deallocation failed!")
 
 1929    if (
allocated(constants % si_ri)) 
then 
 1930        deallocate(constants % si_ri, stat=istat)
 
 1931        if (istat .ne. 0) 
then 
 1932            call update_error(ddx_error, 
"`si_ri` deallocation failed!")
 
 1935    if (
allocated(constants % di_ri)) 
then 
 1936        deallocate(constants % di_ri, stat=istat)
 
 1937        if (istat .ne. 0) 
then 
 1938            call update_error(ddx_error, 
"`di_ri` deallocation failed!")
 
 1941    if (
allocated(constants % sk_ri)) 
then 
 1942        deallocate(constants % sk_ri, stat=istat)
 
 1943        if (istat .ne. 0) 
then 
 1944            call update_error(ddx_error, 
"`sk_ri` deallocation failed!")
 
 1947    if (
allocated(constants % dk_ri)) 
then 
 1948        deallocate(constants % dk_ri, stat=istat)
 
 1949        if (istat .ne. 0) 
then 
 1950            call update_error(ddx_error, 
"`dk_ri` deallocation failed!")
 
 1953    if (
allocated(constants % termimat)) 
then 
 1954        deallocate(constants % termimat, stat=istat)
 
 1955        if (istat .ne. 0) 
then 
 1956            call update_error(ddx_error, 
"`termimat` deallocation failed!")
 
 1959    if (
allocated(constants % b)) 
then 
 1960        deallocate(constants % b, stat=istat)
 
 1961        if (istat .ne. 0) 
then 
 1962            call update_error(ddx_error, 
"`b` deallocation failed!")
 
 1965    if (
allocated(constants % l)) 
then 
 1966        deallocate(constants % l, stat=istat)
 
 1967        if (istat .ne. 0) 
then 
 1968            call update_error(ddx_error, 
"`l` deallocation failed!")
 
 1971    if (
allocated(constants % inl)) 
then 
 1972        deallocate(constants % inl, stat=istat)
 
 1973        if (istat .ne. 0) 
then 
 1974            call update_error(ddx_error, 
"`inl` deallocation failed!")
 
 1977    if (
allocated(constants % nl)) 
then 
 1978        deallocate(constants % nl, stat=istat)
 
 1979        if (istat .ne. 0) 
then 
 1980            call update_error(ddx_error, 
"`nl` deallocation failed!")
 
 1983    if (
allocated(constants % itrnl)) 
then 
 1984        deallocate(constants % itrnl, stat=istat)
 
 1985        if (istat .ne. 0) 
then 
 1986            call update_error(ddx_error, 
"`itrnl` deallocation failed!")
 
 1989    if (
allocated(constants % fi)) 
then 
 1990        deallocate(constants % fi, stat=istat)
 
 1991        if (istat .ne. 0) 
then 
 1992            call update_error(ddx_error, 
"`fi` deallocation failed!")
 
 1995    if (
allocated(constants % ui)) 
then 
 1996        deallocate(constants % ui, stat=istat)
 
 1997        if (istat .ne. 0) 
then 
 1998            call update_error(ddx_error, 
"`ui` deallocation failed!")
 
 2001    if (
allocated(constants % ui_cav)) 
then 
 2002        deallocate(constants % ui_cav, stat=istat)
 
 2003        if (istat .ne. 0) 
then 
 2004            call update_error(ddx_error, 
"`ui_cav` deallocation failed!")
 
 2007    if (
allocated(constants % zi)) 
then 
 2008        deallocate(constants % zi, stat=istat)
 
 2009        if (istat .ne. 0) 
then 
 2010            call update_error(ddx_error, 
"`zi` deallocation failed!")
 
 2013    if (
allocated(constants % ncav_sph)) 
then 
 2014        deallocate(constants % ncav_sph, stat=istat)
 
 2015        if (istat .ne. 0) 
then 
 2016            call update_error(ddx_error, 
"`ncav_sph` deallocation failed!")
 
 2019    if (
allocated(constants % ccav)) 
then 
 2020        deallocate(constants % ccav, stat=istat)
 
 2021        if (istat .ne. 0) 
then 
 2022            call update_error(ddx_error, 
"`ccav` deallocation failed!")
 
 2025    if (
allocated(constants % icav_ia)) 
then 
 2026        deallocate(constants % icav_ia, stat=istat)
 
 2027        if (istat .ne. 0) 
then 
 2028            call update_error(ddx_error, 
"`icav_ia` deallocation failed!")
 
 2031    if (
allocated(constants % icav_ja)) 
then 
 2032        deallocate(constants % icav_ja, stat=istat)
 
 2033        if (istat .ne. 0) 
then 
 2034            call update_error(ddx_error, 
"`icav_ja` deallocation failed!")
 
 2037    if (
allocated(constants % rx_prc)) 
then 
 2038        deallocate(constants % rx_prc, stat=istat)
 
 2039        if (istat .ne. 0) 
then 
 2040            call update_error(ddx_error, 
"`rx_prc` deallocation failed!")
 
 2043    if (
allocated(constants % order)) 
then 
 2044        deallocate(constants % order, stat=istat)
 
 2045        if (istat .ne. 0) 
then 
 2046            call update_error(ddx_error, 
"`order` deallocation failed!")
 
 2049    if (
allocated(constants % cluster)) 
then 
 2050        deallocate(constants % cluster, stat=istat)
 
 2051        if (istat .ne. 0) 
then 
 2052            call update_error(ddx_error, 
"`cluster` deallocation failed!")
 
 2055    if (
allocated(constants % children)) 
then 
 2056        deallocate(constants % children, stat=istat)
 
 2057        if (istat .ne. 0) 
then 
 2058            call update_error(ddx_error, 
"`children` deallocation failed!")
 
 2061    if (
allocated(constants % parent)) 
then 
 2062        deallocate(constants % parent, stat=istat)
 
 2063        if (istat .ne. 0) 
then 
 2064            call update_error(ddx_error, 
"`parent` deallocation failed!")
 
 2067    if (
allocated(constants % cnode)) 
then 
 2068        deallocate(constants % cnode, stat=istat)
 
 2069        if (istat .ne. 0) 
then 
 2070            call update_error(ddx_error, 
"`cnode` deallocation failed!")
 
 2073    if (
allocated(constants % rnode)) 
then 
 2074        deallocate(constants % rnode, stat=istat)
 
 2075        if (istat .ne. 0) 
then 
 2076            call update_error(ddx_error, 
"`rnode` deallocation failed!")
 
 2079    if (
allocated(constants % snode)) 
then 
 2080        deallocate(constants % snode, stat=istat)
 
 2081        if (istat .ne. 0) 
then 
 2082            call update_error(ddx_error, 
"`snode` deallocation failed!")
 
 2085    if (
allocated(constants % sk_rnode)) 
then 
 2086        deallocate(constants % sk_rnode, stat=istat)
 
 2087        if (istat .ne. 0) 
then 
 2088            call update_error(ddx_error, 
"`sk_rnode` deallocation failed!")
 
 2091    if (
allocated(constants % si_rnode)) 
then 
 2092        deallocate(constants % si_rnode, stat=istat)
 
 2093        if (istat .ne. 0) 
then 
 2094            call update_error(ddx_error, 
"`si_rnode` deallocation failed!")
 
 2097    if (
allocated(constants % nfar)) 
then 
 2098        deallocate(constants % nfar, stat=istat)
 
 2099        if (istat .ne. 0) 
then 
 2100            call update_error(ddx_error, 
"`nfar` deallocation failed!")
 
 2103    if (
allocated(constants % nnear)) 
then 
 2104        deallocate(constants % nnear, stat=istat)
 
 2105        if (istat .ne. 0) 
then 
 2106            call update_error(ddx_error, 
"`nnear` deallocation failed!")
 
 2109    if (
allocated(constants % far)) 
then 
 2110        deallocate(constants % far, stat=istat)
 
 2111        if (istat .ne. 0) 
then 
 2112            call update_error(ddx_error, 
"`far` deallocation failed!")
 
 2115    if (
allocated(constants % near)) 
then 
 2116        deallocate(constants % near, stat=istat)
 
 2117        if (istat .ne. 0) 
then 
 2118            call update_error(ddx_error, 
"`near` deallocation failed!")
 
 2121    if (
allocated(constants % sfar)) 
then 
 2122        deallocate(constants % sfar, stat=istat)
 
 2123        if (istat .ne. 0) 
then 
 2124            call update_error(ddx_error, 
"`sfar` deallocation failed!")
 
 2127    if (
allocated(constants % snear)) 
then 
 2128        deallocate(constants % snear, stat=istat)
 
 2129        if (istat .ne. 0) 
then 
 2130            call update_error(ddx_error, 
"`snear` deallocation failed!")
 
 2133    if (
allocated(constants % m2l_ztranslate_coef)) 
then 
 2134        deallocate(constants % m2l_ztranslate_coef, stat=istat)
 
 2135        if (istat .ne. 0) 
then 
 2136            call update_error(ddx_error, 
"`m2l_ztranslate_coef` " // &
 
 2137                & 
"deallocation failed!")
 
 2140    if (
allocated(constants % m2l_ztranslate_adj_coef)) 
then 
 2141        deallocate(constants % m2l_ztranslate_adj_coef, stat=istat)
 
 2142        if (istat .ne. 0) 
then 
 2143            call update_error(ddx_error, 
"`m2l_ztranslate_adj_coef` " // &
 
 2144                & 
"deallocation failed!")
 
subroutine build_itrnl(constants, params, ddx_error)
Build the transposed neighbor list.
 
subroutine build_b(constants, params, ddx_error)
Allocate and build the HSP sparse matrix, only if incore is set.
 
subroutine tree_get_farnear_work(n, children, cnode, rnode, lwork, iwork, jwork, work, nnfar, nfar, nnnear, nnear)
Find near and far admissible pairs of tree nodes and store it in work array.
 
subroutine neighbor_list_init(params, constants, ddx_error)
Build the neighbor list using a N^2 code.
 
subroutine mkprec(lmax, nbasis, nsph, ngrid, eps, ui, wgrid, vgrid, vgrid_nbasis, rx_prc, ddx_error)
Compute preconditioner assemble the diagonal blocks of the reps matrix then invert them to build the ...
 
subroutine build_l(constants, params, ddx_error)
Allocate and build the ddCOSMO sparse matrix, only if incore is set.
 
real(dp) function dfsw(t, se, eta)
Derivative of a switching function.
 
subroutine constants_init(params, constants, ddx_error)
Compute all necessary constants.
 
subroutine neighbor_list_init_fmm(params, constants, ddx_error)
Build the neighbor list using a linear scaling code (only if FMMs are enabled)
 
subroutine constants_geometry_init(params, constants, ddx_error)
Initialize geometry-related constants like list of neighbouring spheres.
 
subroutine constants_free(constants, ddx_error)
Deallocate the constants.
 
subroutine tree_rib_build(nsph, csph, rsph, order, cluster, children, parent, cnode, rnode, snode, ddx_error)
Build a recursive inertial binary tree.
 
real(dp) function fsw(t, se, eta)
Switching function.
 
subroutine tree_rib_node_bisect(nsph, csph, n, order, div, ddx_error)
Divide given cluster of spheres into two subclusters by inertial bisection.
 
subroutine mkpmat(params, constants, isph, pmat)
Computation of P_chi.
 
subroutine tree_get_farnear(jwork, lwork, work, n, nnfar, nfar, sfar, far, nnnear, nnear, snear, near)
Get near and far admissible pairs from work array of tree_get_farnear_work Works only for binary tree...
 
Harmonics-related core routines.
 
subroutine ylmscale(p, vscales, v4pi2lp1, vscales_rel)
Compute scaling factors of real normalized spherical harmonics.
 
subroutine ylmbas(x, rho, ctheta, stheta, cphi, sphi, p, vscales, vylm, vplm, vcos, vsin)
Compute all spherical harmonics up to a given degree at a given point.
 
subroutine fmm_constants(dmax, pm, pl, vcnk, m2l_ztranslate_coef, m2l_ztranslate_adj_coef)
Compute FMM-related constants.
 
Module to treat properly user input parameters.
 
Container for precomputed constants.
 
Type to check and store user input parameters.