diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e5dc5d72b..742c83219 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,7 +45,6 @@ list(APPEND srcs "${dir}/aoparam.f90" "${dir}/approxrab.f90" "${dir}/axis_trafo.f90" - "${dir}/basic_geo.f90" "${dir}/bfgs.f90" "${dir}/bias_path.f90" "${dir}/blowsy.f90" diff --git a/src/basic_geo.f90 b/src/basic_geo.f90 deleted file mode 100644 index 9813e9858..000000000 --- a/src/basic_geo.f90 +++ /dev/null @@ -1,521 +0,0 @@ -! This file is part of xtb. -! -! Copyright (C) 2017-2020 Stefan Grimme -! -! xtb is free software: you can redistribute it and/or modify it under -! the terms of the GNU Lesser General Public License as published by -! the Free Software Foundation, either version 3 of the License, or -! (at your option) any later version. -! -! xtb is distributed in the hope that it will be useful, -! but WITHOUT ANY WARRANTY; without even the implied warranty of -! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -! GNU Lesser General Public License for more details. -! -! You should have received a copy of the GNU Lesser General Public License -! along with xtb. If not, see . - -module xtb_basic_geo -contains -! ..................................................................... - - Subroutine dthetadr(nat,xyz,j,k,i,theta,kijk,dei,dej,dek) - use xtb_mctc_accuracy, only : wp - -! ..................................................................... -! -! Input -! ----- -! __ __ -! ra,rb : distance ij and distnace ik -! -! sinth : sinus of angle j-i-k -! -! costh : cosinus of angle j-i-k -! -! -! Output -! ------ -! -! dei : derivatives of the atom i -! -! dej : derivatives of the atom j -! -! dek : derivatives of the atom k -! -! ..................................................................... - - implicit none - - external vecnorm - - integer ic,nat,j,k,i - - real(wp) ran,rbn,theta,xyz(3,nat) - real(wp) ra(3),rb(3),sinth,costh - real(wp) dei(3),dej(3),dek(3),kijk - real(wp) fac,vecnorm,eps - - parameter (eps=1.d-14) - -! ... dtheta/dri, dtheta/drj and dtheta/drk - - sinth=sin(theta) - costh=cos(theta) - - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,i) - end do - - ran=vecnorm(ra,3,0) - rbn=vecnorm(rb,3,0) - if (abs(sinth).le.eps) then - fac=sign(kijk,costh)/(ran*rbn) - else - fac=1.d0/(sinth*ran*rbn) - end if - - do ic=1,3 -! ... dedri - dei(ic)=fac*( ra(ic)+rb(ic)-(rbn*rbn*ra(ic)+ran*ran*rb(ic))* & - costh/(ran*rbn)) -! ... dedrj - dej(ic)=fac*(ra(ic)*costh*rbn/ran-rb(ic)) -! ... dedrk - dek(ic)=fac*(rb(ic)*costh*ran/rbn-ra(ic)) - end do - - End Subroutine dthetadr - - Subroutine dphidr(nat,xyz,i,j,k,l,phi, & - dphidri,dphidrj,dphidrk,dphidrl) - use xtb_mctc_accuracy, only : wp -! the torsion derivatives - - implicit none - - external vecnorm - - integer ic,i,j,k,l,nat - - real(wp) sinphi,cosphi,onenner,thab,thbc - real(wp) ra(3),rb(3),rc(3),rab(3),rac(3),rbc(3),rbb(3) - real(wp) raa(3),rba(3),rapba(3),rapbb(3),rbpca(3),rbpcb(3) - real(wp) rapb(3),rbpc(3),na(3),nb(3),nan,nbn - real(wp) dphidri(3),dphidrj(3),dphidrk(3),dphidrl(3) - real(wp) xyz(3,nat),phi,vecnorm,nenner,eps,vz - - parameter (eps=1.d-14) - - cosphi=cos(phi) - sinphi=sin(phi) - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,j) - rc(ic)=xyz(ic,l)-xyz(ic,k) - - rapb(ic)=ra(ic)+rb(ic) - rbpc(ic)=rb(ic)+rc(ic) - end do - - call crossprod(ra,rb,na) - call crossprod(rb,rc,nb) - nan=vecnorm(na,3,0) - nbn=vecnorm(nb,3,0) - - nenner=nan*nbn*sinphi - if (abs(nenner).lt.eps) then - dphidri=0 - dphidrj=0 - dphidrk=0 - dphidrl=0 - onenner=1.0d0/(nan*nbn) - else - onenner=1.d0/nenner - endif - - call crossprod(na,rb,rab) - call crossprod(nb,ra,rba) - call crossprod(na,rc,rac) - call crossprod(nb,rb,rbb) - call crossprod(nb,rc,rbc) - call crossprod(na,ra,raa) - - call crossprod(rapb,na,rapba) - call crossprod(rapb,nb,rapbb) - call crossprod(rbpc,na,rbpca) - call crossprod(rbpc,nb,rbpcb) - -! ... dphidri - do ic=1,3 - dphidri(ic)=onenner*(cosphi*nbn/nan*rab(ic)-rbb(ic)) - -! ... dphidrj - dphidrj(ic)=onenner*(cosphi*(nbn/nan*rapba(ic) & - +nan/nbn*rbc(ic)) & - -(rac(ic)+rapbb(ic))) -! ... dphidrk - dphidrk(ic)=onenner*(cosphi*(nbn/nan*raa(ic) & - +nan/nbn*rbpcb(ic)) & - -(rba(ic)+rbpca(ic))) -! ... dphidrl - dphidrl(ic)=onenner*(cosphi*nan/nbn*rbb(ic)-rab(ic)) - end do - - End Subroutine dphidr - -! ..................................................................... - - Subroutine domegadr & - (nat,xyz, & - i,j,k,l,omega, & - domegadri,domegadrj,domegadrk,domegadrl) - use xtb_mctc_accuracy, only : wp - -! inversion derivatives -! ..................................................................... - - implicit none - - external vecnorm - - integer ic,i,j,k,l,nat - - real(wp) omega,sinomega - real(wp) xyz(3,nat),onenner,vecnorm,rnn,rvn - real(wp) rn(3),rv(3),rd(3),re(3),rdme(3),rve(3) - real(wp) rne(3),rdv(3),rdn(3) - real(wp) rvdme(3),rndme(3),nenner - real(wp) domegadri(3),domegadrj(3),domegadrk(3),domegadrl(3),eps - - parameter (eps=1.d-14) - - sinomega=sin(omega) - - do ic=1,3 - rv(ic)=xyz(ic,l)-xyz(ic,i) - rd(ic)=xyz(ic,k)-xyz(ic,j) - re(ic)=xyz(ic,i)-xyz(ic,j) - - rdme(ic)=rd(ic)-re(ic) - end do - - call crossprod(re,rd,rn) - rvn=vecnorm(rv,3,0) - rnn=vecnorm(rn,3,0) - - call crossprod(rv,re,rve) - call crossprod(rn,re,rne) - call crossprod(rd,rv,rdv) - call crossprod(rd,rn,rdn) - call crossprod(rv,rdme,rvdme) - call crossprod(rn,rdme,rndme) - - nenner=rnn*rvn*cos(omega) - if (abs(nenner).gt.eps) then - onenner=1.d0/nenner - do ic=1,3 -! ... domega/dri - domegadri(ic)=onenner*(rdv(ic)-rn(ic)- & - sinomega*(rvn/rnn*rdn(ic)-rnn/rvn*rv(ic))) - -! ... domega/drj - domegadrj(ic)=onenner*(rvdme(ic)-sinomega*rvn/rnn*rndme(ic)) - -! ... domega/drk - domegadrk(ic)=onenner*(rve(ic)-sinomega*rvn/rnn*rne(ic)) - -! ... domega/drl - domegadrl(ic)=onenner*(rn(ic)-sinomega*rnn/rvn*rv(ic)) - end do - else - do ic=1,3 - domegadri(ic)=0.d0 - domegadrj(ic)=0.d0 - domegadrk(ic)=0.d0 - domegadrl(ic)=0.d0 - end do - end if - - End Subroutine domegadr - -! ..................................................................... - - real(wp) Function valijkl(nat,xyz,i,j,k,l) - use xtb_mctc_accuracy, only : wp - use xtb_mctc_constants, only: pi - -! ..................................................................... - - implicit none - - external vecnorm,valijk - - integer ic,i,j,k,l,nat - - real(wp) xyz(3,nat) - real(wp) eps,ra(3),rb(3),rc(3),na(3),nb(3) - real(wp) rab,rbc,thab,thbc,valijk - real(wp) vecnorm,nan,nbn,rcn,snanb,deter,test - real(wp) raabs,rbabs,rcabs - - parameter (eps=1.0d-14) - - raabs=0.0d0 - rbabs=0.0d0 - rcabs=0.0d0 -! ... get torsion coordinate - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,j) - rc(ic)=xyz(ic,l)-xyz(ic,k) - raabs=raabs+ra(ic)*ra(ic) - rbabs=rbabs+rb(ic)*rb(ic) - rcabs=rcabs+rc(ic)*rc(ic) - end do - raabs=sqrt(raabs) - rbabs=sqrt(rbabs) - rcabs=sqrt(rcabs) -! ... normalize vectors - do ic=1,3 - ra(ic)=ra(ic)/raabs - rb(ic)=rb(ic)/rbabs - rc(ic)=rc(ic)/rcabs - end do -! ... determinante of rb,ra,rc - deter= ra(1)*(rb(2)*rc(3)-rb(3)*rc(2)) & - -ra(2)*(rb(1)*rc(3)-rb(3)*rc(1)) & - +ra(3)*(rb(1)*rc(2)-rb(2)*rc(1)) - - call crossprod(-ra,rc,na) - test=0.0d0 - do ic=1,3 - test=test+na(ic)*rb(ic) - end do - - thab=valijk(nat,xyz,i,k,j) - thbc=valijk(nat,xyz,j,l,k) - call crossprod(ra,rb,na) - call crossprod(rb,rc,nb) - nan=vecnorm(na,3,1) - nbn=vecnorm(nb,3,1) - - snanb=0.0d0 - do ic=1,3 - snanb=snanb+na(ic)*nb(ic) - end do - if (abs(abs(snanb)-1.d0).lt.eps) then - snanb=sign(1.d0,snanb) - end if - - valijkl=acos(snanb) - if(test.lt.0.0d0) valijkl=-acos(snanb) - -! the gradient dphir is only compatible with this subroutine -! if the statement below is commented out. If not, opt. and -! Hessian show large errors and imags. I don't understand -! this entirely but thats how it is. -! SG, Sat May 24 11:41:42 CEST 2014 - -! if (deter.lt.0) then -! valijkl=2.d0*pi-valijkl -! end if - - End Function valijkl - -! ..................................................................... - - real(wp) Function valijk(nat,xyz,j,k,i) - use xtb_mctc_accuracy, only : wp - -! ..................................................................... - - implicit none - - external vecnorm - - integer nat,j,k,i,ic - - real(wp) ra(3),rb(3),rab,eps - real(wp) xyz(3,nat),vecnorm,ran,rbn - - parameter (eps=1.d-14) - - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,i) - end do - - ran=vecnorm(ra,3,1) - rbn=vecnorm(rb,3,1) - rab=0.d0 - do ic=1,3 - rab=rab+ra(ic)*rb(ic) - end do - - if (abs(abs(rab)-1.d0).lt.eps) then - rab=sign(1.d0,rab) - end if - valijk=acos(rab) - - End Function valijk - -! ..................................................................... - - real(wp) Function omega (nat,xyz,i,j,k,l) - use xtb_mctc_accuracy, only : wp - -! Calculates the inversion angle -! ..................................................................... - - implicit none - - external vecnorm - - integer ic,nat,i,j,k,l - - real(wp) xyz(3,nat) - real(wp) rd(3),re(3),rn(3),rv(3),rnv - real(wp) vecnorm,rkjn,rljn,rnn,rvn - - do ic=1,3 - re(ic)=xyz(ic,i)-xyz(ic,j) - rd(ic)=xyz(ic,k)-xyz(ic,j) - rv(ic)=xyz(ic,l)-xyz(ic,i) - end do - call crossprod(re,rd,rn) - rnn=vecnorm(rn,3,1) - rvn=vecnorm(rv,3,1) - - rnv=rn(1)*rv(1)+rn(2)*rv(2)+rn(3)*rv(3) - omega=asin( rnv ) - - End Function omega - -! ..................................................................... - - Subroutine crossprod(ra,rb,rab) - use xtb_mctc_accuracy, only : wp - - implicit none - - real(wp) ra(3),rb(3),rab(3) - - rab(1)=ra(2)*rb(3)-ra(3)*rb(2) - rab(2)=ra(3)*rb(1)-ra(1)*rb(3) - rab(3)=ra(1)*rb(2)-ra(2)*rb(1) - - End Subroutine crossprod - -! ..................................................................... - - real(wp) Function vecnorm (r,n,inorm) - use xtb_mctc_accuracy, only : wp - - implicit none - - integer i,n,inorm - - real(wp) r(n),or,sp,rn - - sp=0. - do i=1,n - sp=sp+r(i)*r(i) - end do - rn=sqrt(sp) - if (inorm.gt.0) then - if (abs(rn).gt.1.d-14) then - or=1.0d0/rn - do i=1,n - r(i)=or*r(i) - end do - end if - end if - vecnorm=rn - - End Function vecnorm - -! ..................................................................... - - subroutine crprod(a,b,c) - use xtb_mctc_accuracy, only : wp - implicit double precision (a-h,o-z) - dimension a(3),b(3),c(3) - - c(1)=a(2)*b(3)-a(3)*b(2) - c(2)=a(3)*b(1)-a(1)*b(3) - c(3)=a(1)*b(2)-a(2)*b(1) - - return - end subroutine crprod - -! ..................................................................... - - subroutine vsc1(a,scale,tol) - use xtb_mctc_accuracy, only : wp - implicit integer (i-n) - implicit double precision (a-h,o-z) - dimension a(3) - - rlen = norm2(a) - - if (rlen.gt.tol) then - do i=1,3 - a(i) = a(i)*scale/rlen - end do - endif - - return - end subroutine vsc1 - -! ..................................................................... - - pure subroutine impsc(a,b,c) - use xtb_mctc_accuracy, only : wp - implicit none - real(wp),intent(in) :: a(3),b(3) - real(wp),intent(out) :: c - integer :: i - real(wp) :: rimp,al,bl - - rimp = 0.0d0 - - do i=1,3 - rimp = rimp + a(i)*b(i) - end do - - al = norm2(a) - bl = norm2(b) - - if (al.gt.0.0d0.and.bl.gt.0.0d0) then - c = rimp/(al*bl) - else - c = 0.0d0 - endif - - return - end subroutine impsc - -! ..................................................................... - - pure subroutine bangl(xyz,i,j,k,angle) - use xtb_mctc_accuracy, only : wp - implicit none - real(wp),intent(in) :: xyz(3,*) - real(wp),intent(out) :: angle - integer, intent(in) :: i,j,k - - real(wp) d2ij,d2jk,d2ik,xy,temp - - d2ij = sum((xyz(:,i)-xyz(:,j))**2) - d2jk = sum((xyz(:,j)-xyz(:,k))**2) - d2ik = sum((xyz(:,i)-xyz(:,k))**2) - xy = sqrt(d2ij*d2jk) - temp = 0.5d0 * (d2ij+d2jk-d2ik) / xy - if (temp .gt. 1.0d0) temp= 1.0d0 - if (temp .lt. -1.0d0) temp=-1.0d0 - angle = acos( temp ) - - end subroutine bangl -end module xtb_basic_geo diff --git a/src/bmatrix.f90 b/src/bmatrix.f90 index 5af4cafaf..31b7b406d 100644 --- a/src/bmatrix.f90 +++ b/src/bmatrix.f90 @@ -22,7 +22,7 @@ module xtb_bmatrix use xtb_mctc_constants, only : pi use xtb_mctc_math, only : crossProd use xtb_internals_type, only : internal_coords_set_type, coord_bond, & - & coord_angle, coord_dihedral, coord_linbend + & coord_angle, coord_dihedral, coord_linbend, angle_value implicit none private @@ -37,22 +37,11 @@ module xtb_bmatrix real(wp), parameter :: tol_collinear = 1.0e-13_wp !> Tolerance for cross-product magnitude (collinearity guard) real(wp), parameter :: tol_cross = 1.0e-10_wp - !> Tolerance for near-linear bend detection - real(wp), parameter :: tol_bend = 1.0e-6_wp !> Avoid zero division in angle derivatives (1e-24 is ~1e-12 in energy) real(wp), parameter :: eps_sqrt = 1.0e-24_wp contains -!> Clamp a cosine to [-1, 1]; rounding can push it outside and make acos NaN. -elemental function clamp(co) result(c) - !> Cosine to clamp. - real(wp), intent(in) :: co - - real(wp) :: c - c = min(1.0_wp, max(-1.0_wp, co)) -end function clamp - !> Wilson B row for a bond length (2 atoms). !> !> Row layout is [atom1(1:3), atom2(4:6)] for vec = xyz(atom1) - xyz(atom2). @@ -79,44 +68,19 @@ pure function bmat_angle(vec1, vec2) result(bmat) real(wp), intent(in) :: vec2(3) real(wp) :: bmat(9) - real(wp) :: l1, l2, nvec1(3), nvec2(3) - real(wp) :: dl(2, 6), dnvec(2, 3, 6), dinprod(9) - real(wp) :: sinphi - integer :: ii + real(wp) :: l1, l2, nvec1(3), nvec2(3), co, sinphi, g1(3), g2(3) l1 = norm2(vec1) l2 = norm2(vec2) nvec1 = vec1 / l1 nvec2 = vec2 / l2 - - dl = 0.0_wp - dl(1, 1:3) = nvec1 - dl(1, 4:6) = -nvec1 - dl(2, 1:3) = nvec2 - dl(2, 4:6) = -nvec2 - - dnvec = 0.0_wp - do ii = 1, 6 - dnvec(1, 1:3, ii) = -nvec1 * dl(1, ii) / l1 - dnvec(2, 1:3, ii) = -nvec2 * dl(2, ii) / l2 - end do - do ii = 1, 3 - dnvec(1, ii, ii) = dnvec(1, ii, ii) + 1.0_wp / l1 - dnvec(2, ii, ii) = dnvec(2, ii, ii) + 1.0_wp / l2 - dnvec(1, ii, ii+3) = dnvec(1, ii, ii+3) - 1.0_wp / l1 - dnvec(2, ii, ii+3) = dnvec(2, ii, ii+3) - 1.0_wp / l2 - end do - - dinprod = 0.0_wp - do ii = 1, 3 - dinprod(ii) = dot_product(dnvec(1, :, ii), nvec2) - dinprod(ii+3) = dot_product(dnvec(1, :, ii+3), nvec2) & - & + dot_product(dnvec(2, :, ii+3), nvec1) - dinprod(ii+6) = dot_product(dnvec(2, :, ii), nvec1) - end do - - sinphi = norm2(crossProd(nvec1, nvec2)) - bmat = -dinprod / max(sqrt(eps_sqrt), sinphi) + co = dot_product(nvec1, nvec2) + sinphi = max(sqrt(eps_sqrt), norm2(crossProd(nvec1, nvec2))) + g1 = (co * nvec1 - nvec2) / (l1 * sinphi) + g2 = (co * nvec2 - nvec1) / (l2 * sinphi) + bmat(1:3) = g1 + bmat(4:6) = -(g1 + g2) + bmat(7:9) = g2 end function bmat_angle !> Orthonormal pair of directions perpendicular to a unit axis, the fixed frame @@ -182,14 +146,14 @@ pure function bmat_torsion(xyz) result(bt) rij1 = norm2(xyz(:, 2) - xyz(:, 1)) rjk1 = norm2(xyz(:, 3) - xyz(:, 2)) rkl1 = norm2(xyz(:, 4) - xyz(:, 3)) - brij = bmat_strtch(xyz(:, 1:2)) - brjk = bmat_strtch(xyz(:, 2:3)) - brkl = bmat_strtch(xyz(:, 3:4)) + brij = reshape(bmat_bond(xyz(:, 1) - xyz(:, 2)), [3, 2]) + brjk = reshape(bmat_bond(xyz(:, 2) - xyz(:, 3)), [3, 2]) + brkl = reshape(bmat_bond(xyz(:, 3) - xyz(:, 4)), [3, 2]) - fi2 = bend_angle(xyz(:, 1:3)) + fi2 = angle_value(xyz, 1, 2, 3) sinfi2 = sin(fi2) cosfi2 = cos(fi2) - fi3 = bend_angle(xyz(:, 2:4)) + fi3 = angle_value(xyz, 2, 3, 4) sinfi3 = sin(fi3) cosfi3 = cos(fi3) @@ -206,6 +170,7 @@ pure function bmat_torsion(xyz) result(bt) & + rkl1 * cosfi3 * bt(ix, 4)) / rjk1 bt(ix, 3) = -(bt(ix, 1) + bt(ix, 2) + bt(ix, 4)) end do + bt = -bt end function bmat_torsion !> Wilson B rows for an out-of-plane angle (4 atoms, atom 4 central). @@ -216,25 +181,12 @@ pure function bmat_outofplane(xyz) result(bt) real(wp), intent(in) :: xyz(3, 4) real(wp) :: bt(3, 4) - real(wp) :: r2(3), r3(3) - real(wp) :: q42, q43, e42(3), e43(3) - real(wp) :: cosfi1, fi1 + real(wp) :: fi1 real(wp) :: c14(3, 3), br14(3, 3) real(wp) :: r42(3), r43(3) integer :: ix, iy, iz - ! 4 -> 2 (bond in plane) - r2 = xyz(:, 2) - xyz(:, 4) - q42 = norm2(r2) - e42 = r2 / q42 - ! 4 -> 3 (bond in plane) - r3 = xyz(:, 3) - xyz(:, 4) - q43 = norm2(r3) - e43 = r3 / q43 - - ! angle between e43 and e42 - cosfi1 = clamp(dot_product(e43, e42)) - fi1 = acos(cosfi1) + fi1 = angle_value(xyz, 2, 4, 3) ! dirty exit when 2-3-4 collinear if (abs(fi1 - pi) < tol_collinear) then @@ -258,7 +210,7 @@ pure function bmat_outofplane(xyz) result(bt) end if c14(:, 3) = c14(:, 3) + xyz(:, 4) - br14 = bmat_bend(c14) + br14 = reshape(bmat_angle(c14(:, 1) - c14(:, 2), c14(:, 3) - c14(:, 2)), [3, 3]) ! compute the WDC matrix do ix = 1, 3 @@ -283,19 +235,10 @@ pure function oop_angle(xyz) result(theta) real(wp), intent(in) :: xyz(3, 4) real(wp) :: theta - real(wp) :: r2(3), r3(3), q42, q43, e42(3), e43(3) - real(wp) :: cosfi1, fi1 + real(wp) :: fi1 real(wp) :: r42(3), r43(3), c14(3, 3) - r2 = xyz(:, 2) - xyz(:, 4) - q42 = norm2(r2) - e42 = r2 / q42 - r3 = xyz(:, 3) - xyz(:, 4) - q43 = norm2(r3) - e43 = r3 / q43 - - cosfi1 = clamp(dot_product(e43, e42)) - fi1 = acos(cosfi1) + fi1 = angle_value(xyz, 2, 4, 3) if (abs(fi1 - pi) < tol_collinear) then theta = 0.0_wp @@ -314,7 +257,7 @@ pure function oop_angle(xyz) result(theta) end if c14(:, 3) = c14(:, 3) + xyz(:, 4) - theta = bend_angle(c14) - 0.5_wp * pi + theta = angle_value(c14, 1, 2, 3) - 0.5_wp * pi end function oop_angle !> Assemble the Wilson B matrix for an internal-coordinate set. @@ -361,92 +304,6 @@ pure subroutine get_bmatrix(internals, xyz, bmat) end do end subroutine get_bmatrix -!> Stretch B row for a 2-atom fragment (private helper). -!> -!> Row layout is [atom1(1:3), atom2(4:6)]. -pure function bmat_strtch(xyz) result(b) - !> Cartesian coordinates of atoms 1 and 2. - real(wp), intent(in) :: xyz(3, 2) - - real(wp) :: b(3, 2) - real(wp) :: r(3), rr - - r = xyz(:, 2) - xyz(:, 1) - rr = norm2(r) - b(:, 1) = -r / rr - b(:, 2) = -b(:, 1) -end function bmat_strtch - -!> Bend angle for a 3-atom fragment in radians (private helper). -pure function bend_angle(xyz) result(fir) - !> Cartesian coordinates of atoms 1-2-3; atom 2 is the angle centre. - real(wp), intent(in) :: xyz(3, 3) - - real(wp) :: fir - real(wp) :: brij(3, 2), brjk(3, 2) - real(wp) :: co, crap - integer :: i - - brij = bmat_strtch(xyz(:, 1:2)) - brjk = bmat_strtch(xyz(:, 2:3)) - co = 0.0_wp - crap = 0.0_wp - do i = 1, 3 - co = co + brij(i, 1) * brjk(i, 2) - crap = crap + (brjk(i, 2) + brij(i, 1))**2 - end do - co = clamp(co) - if (sqrt(crap) < tol_bend) then - fir = pi - asin(sqrt(crap)) - else - fir = acos(co) - end if - if (abs(fir - pi) < tol_collinear) then - fir = pi - end if -end function bend_angle - -!> Bend B rows for a 3-atom fragment (private helper). -!> -!> bf(:, a) is the 3-vector of angle derivatives with respect to atom a. -pure function bmat_bend(xyz) result(bf) - !> Cartesian coordinates of atoms 1-2-3; atom 2 is the angle centre. - real(wp), intent(in) :: xyz(3, 3) - - real(wp) :: bf(3, 3) - real(wp) :: brij(3, 2), brjk(3, 2) - real(wp) :: co, crap, fir, si - real(wp) :: rij1, rjk1 - integer :: i - - brij = bmat_strtch(xyz(:, 1:2)) - brjk = bmat_strtch(xyz(:, 2:3)) - rij1 = norm2(xyz(:, 2) - xyz(:, 1)) - rjk1 = norm2(xyz(:, 3) - xyz(:, 2)) - co = 0.0_wp - crap = 0.0_wp - do i = 1, 3 - co = co + brij(i, 1) * brjk(i, 2) - crap = crap + (brjk(i, 2) + brij(i, 1))**2 - end do - co = clamp(co) - if (sqrt(crap) < tol_bend) then - fir = pi - asin(sqrt(crap)) - si = sqrt(crap) - else - fir = acos(co) - si = sqrt(1.0_wp - co**2) - end if - bf = 0.0_wp - if (abs(fir - pi) < tol_collinear) then - return - end if - do i = 1, 3 - bf(i, 1) = (co*brij(i, 1) - brjk(i, 2)) / (si*rij1) - bf(i, 3) = (co*brjk(i, 2) - brij(i, 1)) / (si*rjk1) - bf(i, 2) = -(bf(i, 1) + bf(i, 3)) - end do -end function bmat_bend !> Accumulate scale * B^T B into a packed lower-triangular Cartesian Hessian. !> diff --git a/src/constr.f90 b/src/constr.f90 index 2c3223236..583fe9fd8 100644 --- a/src/constr.f90 +++ b/src/constr.f90 @@ -19,13 +19,13 @@ subroutine constralltors(n,at,xyz) use xtb_mctc_accuracy, only : wp use xtb_mctc_constants use xtb_scanparam - use xtb_intmodes + use xtb_internals_type, only : angle_value, dihedral_value implicit none integer, intent(in) :: n integer, intent(in) :: at(n) real(wp),intent(in) :: xyz(3,n) - real(wp) :: phi,valijkl,a1,a2 + real(wp) :: phi,a1,a2 integer :: i,j,k,ii,jj,kk,ll,irm integer :: nb(0:20,n),list(n,n) @@ -51,8 +51,8 @@ subroutine constralltors(n,at,xyz) ll=nb(jj,j) if(kk.eq.j) cycle if(ll.eq.i) cycle - call BANGLE(XYZ,I,J,ll,A1) ! avoid linear cas - call BANGLE(XYZ,J,I,KK,A2) + a1 = angle_value(xyz, i, j, ll) + a2 = angle_value(xyz, j, i, kk) if(abs(pi-a1).lt.0.2.or.abs(pi-a2).lt.0.2)then irm=irm+1 cycle @@ -62,7 +62,7 @@ subroutine constralltors(n,at,xyz) atconstr(2,k)=i atconstr(3,k)=j atconstr(4,k)=ll - phi=valijkl(n,xyz,kk,i,j,ll) + phi = dihedral_value(xyz, kk, i, j, ll) valconstr(k)=phi ! write(*,*) atconstr(1:4,k),phi*180./3.14159 enddo @@ -80,11 +80,12 @@ subroutine constrallangles(n,at,xyz) use xtb_mctc_accuracy, only : wp use xtb_mctc_constants use xtb_scanparam + use xtb_internals_type, only : angle_value implicit none integer n, at(n) real(wp) xyz(3,n) - real(wp) phi,valijkl,a1,a2 + real(wp) phi,a1,a2 integer i,j,k,ii,jj,kk,ll,irm,nb(0:20,n),list(n,n) call neighbor(n,xyz,at,nb) @@ -104,7 +105,7 @@ subroutine constrallangles(n,at,xyz) kk=nb(ii,i) do jj=1,nb(20,i) ll=nb(jj,i) - call bangl(xyz,kk,i,ll,phi) + phi = angle_value(xyz, kk, i, ll) if(abs(pi-phi).lt.0.2)then irm=irm+1 cycle @@ -160,6 +161,8 @@ subroutine constrpot(nat,at,xyz,g,e) use xtb_mctc_constants use xtb_scanparam use xtb_splitparam + use xtb_internals_type, only : angle_value, dihedral_value + use xtb_bmatrix, only : bmat_angle, bmat_torsion implicit none integer nat,at(nat) real(wp) e @@ -167,11 +170,9 @@ subroutine constrpot(nat,at,xyz,g,e) real(wp) g(3,nat) integer i,j,k,l,kk - real(wp)rij(3),dum,r0,ffc,vp(3),ra(3),rb(3) - real(wp)va(3),vb(3),vc(3),vab(3),vcb(3),deda(3),dedc(3),dedb(3) - real(wp)dda(3),ddb(3),ddc(3),ddd(3) - real(wp)c0,rab2,rcb2,theta,dt,rmul1,rmul2,deddt,rp,cosa,ea,vlen,d - real(wp)c1,phi0,phi,dphi1,x1cos,x1sin,dij,valijkl,ff,dum1,dum2 + real(wp)rij(3),dum,r0,ffc,ra(3),rb(3) + real(wp)c0,theta,dt,deddt,d,brow9(9),bt(3,4) + real(wp)phi0,phi,dphi1,x1cos,x1sin,dij,ff,dum2 real(wp)dx,dy,dz,termx,termy,termz,abcx,abcy,abcz,a,z0,expo,expom1 if(nconstr.eq.0) return @@ -282,32 +283,14 @@ subroutine constrpot(nat,at,xyz,g,e) j= atconstr(2,kk) k= atconstr(3,kk) c0 =valconstr(kk) - va = xyz(1:3,i) - vb = xyz(1:3,j) - vc = xyz(1:3,k) - vab = va-vb - vcb = vc-vb - rab2 = vab(1)*vab(1) + vab(2)*vab(2) + vab(3)*vab(3) - rcb2 = vcb(1)*vcb(1) + vcb(2)*vcb(2) + vcb(3)*vcb(3) - call crprod(vcb,vab,vp) - rp = norm2(vp)+1.d-14 - call impsc(vab,vcb,cosa) - cosa = dble(min(1.0d0,max(-1.0d0,cosa))) - theta= dacos(cosa) - dt = theta - c0 - ea = ffc* dt**2 - deddt = 2.d0 * ffc * dt - e = e + ea - call crprod(vab,vp,deda) - rmul1 = -deddt / (rab2*rp) - deda = deda*rmul1 - call crprod(vcb,vp,dedc) - rmul2 = deddt / (rcb2*rp) - dedc = dedc*rmul2 - dedb = deda+dedc - g(1:3,i) = g(1:3,i) + deda(1:3) - g(1:3,j) = g(1:3,j) - dedb(1:3) - g(1:3,k) = g(1:3,k) + dedc(1:3) + theta = angle_value(xyz, i, j, k) + dt = theta - c0 + e = e + ffc * dt**2 + deddt = 2.0_wp * ffc * dt + brow9 = bmat_angle(xyz(:, i) - xyz(:, j), xyz(:, k) - xyz(:, j)) + g(:, i) = g(:, i) + deddt * brow9(1:3) + g(:, j) = g(:, j) + deddt * brow9(4:6) + g(:, k) = g(:, k) + deddt * brow9(7:9) endif ! dihed if(atconstr(4,kk).ne.0)then @@ -316,18 +299,17 @@ subroutine constrpot(nat,at,xyz,g,e) k= atconstr(3,kk) l= atconstr(4,kk) phi0 =valconstr(kk) - phi=valijkl(nat,xyz,i,j,k,l) - if(abs(phi-pi).lt.1.d-8.or.abs(phi).lt.1.d-8)phi=phi+1.d-8 - call dphidr(nat,xyz,i,j,k,l,phi,dda,ddb,ddc,ddd) - dphi1=phi0-phi+pi - x1cos=cos(dphi1) - x1sin=sin(dphi1) - dij =ffc*x1sin - g(1:3,i)=g(1:3,i)+dij*dda(1:3) - g(1:3,j)=g(1:3,j)+dij*ddb(1:3) - g(1:3,k)=g(1:3,k)+dij*ddc(1:3) - g(1:3,l)=g(1:3,l)+dij*ddd(1:3) - e=e+ffc*(1.+x1cos) + phi = dihedral_value(xyz, i, j, k, l) + bt = bmat_torsion(xyz(:, [i, j, k, l])) + dphi1 = phi0 - phi + pi + x1cos = cos(dphi1) + x1sin = sin(dphi1) + dij = ffc * x1sin + g(:, i) = g(:, i) + dij * bt(:, 1) + g(:, j) = g(:, j) + dij * bt(:, 2) + g(:, k) = g(:, k) + dij * bt(:, 3) + g(:, l) = g(:, l) + dij * bt(:, 4) + e = e + ffc * (1.0_wp + x1cos) endif enddo @@ -404,77 +386,6 @@ subroutine constrhess(nat,at,xyz0,Hess) end subroutine -! ..................................................................... - -Subroutine dthetadr(nat,xyz,j,k,i,theta,kijk,dei,dej,dek) - use xtb_mctc_accuracy, only : wp - - ! ..................................................................... - ! - ! Input - ! ----- - ! __ __ - ! ra,rb : distance ij and distnace ik - ! - ! sinth : sinus of angle j-i-k - ! - ! costh : cosinus of angle j-i-k - ! - ! - ! Output - ! ------ - ! - ! dei : derivatives of the atom i - ! - ! dej : derivatives of the atom j - ! - ! dek : derivatives of the atom k - ! - ! ..................................................................... - - implicit none - - external vecnorm - - integer ic,nat,j,k,i - - real(wp) :: & - & ran,rbn,theta,xyz(3,nat),& - & ra(3),rb(3),sinth,costh,& - & dei(3),dej(3),dek(3),kijk,& - & fac,vecnorm,eps - - parameter (eps=1.d-14) - - ! ... dtheta/dri, dtheta/drj and dtheta/drk - - sinth=sin(theta) - costh=cos(theta) - - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,i) - end do - - ran=vecnorm(ra,3,0) - rbn=vecnorm(rb,3,0) - if (abs(sinth).le.eps) then - fac=sign(kijk,costh)/(ran*rbn) - else - fac=1.d0/(sinth*ran*rbn) - end if - - do ic=1,3 - ! ... dedri - dei(ic)=fac*( ra(ic)+rb(ic)-(rbn*rbn*ra(ic)+ran*ran*rb(ic))*& - & costh/(ran*rbn)) - ! ... dedrj - dej(ic)=fac*(ra(ic)*costh*rbn/ran-rb(ic)) - ! ... dedrk - dek(ic)=fac*(rb(ic)*costh*ran/rbn-ra(ic)) - end do - -End subroutine @@ -658,81 +569,6 @@ Subroutine dphidrPBC(mode,nat,xyz,i,j,k,l,vTrR,vTrB,vTrC,phi,& End subroutine -! ..................................................................... - -Subroutine domegadr& - & (nat,xyz,& - & i,j,k,l,omega,& - & domegadri,domegadrj,domegadrk,domegadrl) - use xtb_mctc_accuracy, only : wp - - ! inversion derivatives - ! ..................................................................... - - implicit none - - external vecnorm - - integer ic,i,j,k,l,nat - - real(wp)& - & omega,sinomega,& - & xyz(3,nat),onenner,vecnorm,rnn,rvn,& - & rn(3),rv(3),rd(3),re(3),rdme(3),rve(3),& - & rne(3),rdv(3),rdn(3),& - & rvdme(3),rndme(3),nenner,& - & domegadri(3),domegadrj(3),domegadrk(3),domegadrl(3),eps - - parameter (eps=1.d-14) - - sinomega=sin(omega) - - do ic=1,3 - rv(ic)=xyz(ic,l)-xyz(ic,i) - rd(ic)=xyz(ic,k)-xyz(ic,j) - re(ic)=xyz(ic,i)-xyz(ic,j) - - rdme(ic)=rd(ic)-re(ic) - end do - - call crossprod(re,rd,rn) - rvn=vecnorm(rv,3,0) - rnn=vecnorm(rn,3,0) - - call crossprod(rv,re,rve) - call crossprod(rn,re,rne) - call crossprod(rd,rv,rdv) - call crossprod(rd,rn,rdn) - call crossprod(rv,rdme,rvdme) - call crossprod(rn,rdme,rndme) - - nenner=rnn*rvn*cos(omega) - if (abs(nenner).gt.eps) then - onenner=1.d0/nenner - do ic=1,3 - ! ... domega/dri - domegadri(ic)=onenner*(rdv(ic)-rn(ic)-& - & sinomega*(rvn/rnn*rdn(ic)-rnn/rvn*rv(ic))) - - ! ... domega/drj - domegadrj(ic)=onenner*(rvdme(ic)-sinomega*rvn/rnn*rndme(ic)) - - ! ... domega/drk - domegadrk(ic)=onenner*(rve(ic)-sinomega*rvn/rnn*rne(ic)) - - ! ... domega/drl - domegadrl(ic)=onenner*(rn(ic)-sinomega*rnn/rvn*rv(ic)) - end do - else - do ic=1,3 - domegadri(ic)=0.d0 - domegadrj(ic)=0.d0 - domegadrk(ic)=0.d0 - domegadrl(ic)=0.d0 - end do - end if - -End subroutine ! ..................................................................... @@ -811,217 +647,6 @@ Subroutine domegadrPBC& End subroutine -! ..................................................................... - -real(wp) Function valijkl(nat,xyz,i,j,k,l) - use xtb_mctc_accuracy, only : wp - - ! ..................................................................... - - implicit none - - external vecnorm,valijk - - integer ic,i,j,k,l,nat - - real(wp) :: & - & xyz(3,nat),& - & eps,ra(3),rb(3),rc(3),na(3),nb(3),& - & rab,rbc,thab,thbc,valijk,& - & vecnorm,nan,nbn,rcn,snanb,deter,pi,test,& - & raabs,rbabs,rcabs - - parameter (eps=1.0d-14) - data pi/3.1415926535897932384626433832795029d0/ - - raabs=0.0d0 - rbabs=0.0d0 - rcabs=0.0d0 - ! ... get torsion coordinate - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,j) - rc(ic)=xyz(ic,l)-xyz(ic,k) - raabs=raabs+ra(ic)*ra(ic) - rbabs=rbabs+rb(ic)*rb(ic) - rcabs=rcabs+rc(ic)*rc(ic) - end do - raabs=sqrt(raabs) - rbabs=sqrt(rbabs) - rcabs=sqrt(rcabs) - ! ... normalize vectors - do ic=1,3 - ra(ic)=ra(ic)/raabs - rb(ic)=rb(ic)/rbabs - rc(ic)=rc(ic)/rcabs - end do - ! ... determinante of rb,ra,rc - deter= ra(1)*(rb(2)*rc(3)-rb(3)*rc(2))& - & -ra(2)*(rb(1)*rc(3)-rb(3)*rc(1))& - & +ra(3)*(rb(1)*rc(2)-rb(2)*rc(1)) - - call crossprod(-ra,rc,na) - test=0.0d0 - do ic=1,3 - test=test+na(ic)*rb(ic) - end do - - thab=valijk(nat,xyz,i,k,j) - thbc=valijk(nat,xyz,j,l,k) - call crossprod(ra,rb,na) - call crossprod(rb,rc,nb) - nan=vecnorm(na,3,1) - nbn=vecnorm(nb,3,1) - - snanb=0.0d0 - do ic=1,3 - snanb=snanb+na(ic)*nb(ic) - end do - if (abs(abs(snanb)-1.d0).lt.eps) then - snanb=sign(1.d0,snanb) - end if - - valijkl=acos(snanb) - if(test.lt.0.0d0) valijkl=-acos(snanb) - - ! the gradient dphir is only compatible with this subroutine - ! if the statement below is commented out. If not, opt. and - ! Hessian show large errors and imags. I don't understand - ! this entirely but thats how it is. - ! SG, Sat May 2411:41:42 CEST 2014 - - ! if (deter.lt.0) then - ! valijkl=2.d0*pi-valijkl - ! end if - -End function - -! ..................................................................... - -real(wp) Function valijk(nat,xyz,j,k,i) - use xtb_mctc_accuracy, only : wp - - ! ..................................................................... - - implicit none - - external vecnorm - - integer nat,j,k,i,ic - - real(wp) :: & - & ra(3),rb(3),rab,eps,& - & xyz(3,nat),vecnorm,ran,rbn - - parameter (eps=1.d-14) - - do ic=1,3 - ra(ic)=xyz(ic,j)-xyz(ic,i) - rb(ic)=xyz(ic,k)-xyz(ic,i) - end do - - ran=vecnorm(ra,3,1) - rbn=vecnorm(rb,3,1) - rab=0.d0 - do ic=1,3 - rab=rab+ra(ic)*rb(ic) - end do - - if (abs(abs(rab)-1.d0).lt.eps) then - rab=sign(1.d0,rab) - end if - valijk=acos(rab) - -End function - - -! ..................................................................... - -real(wp) Function valijkPBC(mode,nat,xyz,j,k,i,vTr1,vTr2,vTr3) - use xtb_mctc_accuracy, only : wp - - ! ..................................................................... - - implicit none - - external vecnorm - - integer mode,nat,j,k,i,ic - - real(wp) :: & - & ra(3),rb(3),rab,eps,& - & xyz(3,nat),vecnorm,ran,rbn,vTr1(3),vTr2(3),vTr3(3) - - parameter (eps=1.d-14) - - if (mode.eq.1) then ! here j=l,k=j,i=i are inserted, vTr1=vTrl, vTr2=vTrj - do ic=1,3 - ra(ic)=(xyz(ic,j)+vTr1(ic))-xyz(ic,i) - rb(ic)=(xyz(ic,k)+vTr2(ic))-xyz(ic,i) - end do - elseif(mode.eq.2) then ! here j=i,k=k,i=j are inserted vTr1=vTrk, vTr2=vTrj - do ic=1,3 - ra(ic)= xyz(ic,j)-(xyz(ic,i)+vTr2(ic)) - rb(ic)=(xyz(ic,k)+vTr1(ic))-(xyz(ic,i)+vTr2(ic)) - end do - elseif(mode.eq.3) then ! here j=R k=C i=B vTr1=vTrR vTr2=vTrC vTr3=vTrB - do ic=1,3 - ra(ic)= (xyz(ic,j)+vTr1(ic))-(xyz(ic,i)+vTr3(ic)) ! R - B - rb(ic)= (xyz(ic,k)+vTr2(ic))-(xyz(ic,i)+vTr3(ic)) ! C - B - end do - elseif(mode.eq.4) then ! here j=B k=H i=C vTr1=vTrB vTr2=vTrC - do ic=1,3 - ra(ic)=(xyz(ic,j)+vTr1(ic))-(xyz(ic,i)+vTr2(ic)) - rb(ic)=(xyz(ic,k)) -(xyz(ic,i)+vTr2(ic)) - end do - endif - - ran=vecnorm(ra,3,1) - rbn=vecnorm(rb,3,1) - rab=0.d0 - do ic=1,3 - rab=rab+ra(ic)*rb(ic) - end do - - if (abs(abs(rab)-1.d0).lt.eps) then - rab=sign(1.d0,rab) - end if - valijkPBC=acos(rab) - -End function - -! ..................................................................... - -real(wp) Function omega (nat,xyz,i,j,k,l) - use xtb_mctc_accuracy, only : wp - - ! Calculates the inversion angle - ! ..................................................................... - - implicit none - - external vecnorm - - integer ic,nat,i,j,k,l - - real(wp) :: & - & xyz(3,nat),& - & rd(3),re(3),rn(3),rv(3),rnv,& - & vecnorm,rkjn,rljn,rnn,rvn - - do ic=1,3 - re(ic)=xyz(ic,i)-xyz(ic,j) - rd(ic)=xyz(ic,k)-xyz(ic,j) - rv(ic)=xyz(ic,l)-xyz(ic,i) - end do - call crossprod(re,rd,rn) - rnn=vecnorm(rn,3,1) - rvn=vecnorm(rv,3,1) - - rnv=rn(1)*rv(1)+rn(2)*rv(2)+rn(3)*rv(3) - omega=asin( rnv ) - -End function real(wp) Function omegaPBC (nat,xyz,i,j,k,l,vTr1,vTr2,vTr3) @@ -1114,24 +739,6 @@ subroutine crprod(a,b,c) return end subroutine crprod -! ..................................................................... - -subroutine vsc1(a,scale,tol) - use xtb_mctc_accuracy, only : wp - implicit integer (i-n) - implicit double precision (a-h,o-z) - dimension a(3) - - rlen = norm2(a) - - if (rlen.gt.tol) then - do i=1,3 - a(i) = a(i)*scale/rlen - end do - endif - - return -end subroutine vsc1 ! ..................................................................... @@ -1161,27 +768,6 @@ pure subroutine impsc(a,b,c) return end subroutine impsc -! ..................................................................... - -pure subroutine bangl(xyz,i,j,k,angle) - use xtb_mctc_accuracy, only : wp - implicit none - real(wp),intent(in) :: xyz(3,*) - real(wp),intent(out) :: angle - integer, intent(in) :: i,j,k - - real(wp) d2ij,d2jk,d2ik,xy,temp - - d2ij = sum((xyz(:,i)-xyz(:,j))**2) - d2jk = sum((xyz(:,j)-xyz(:,k))**2) - d2ik = sum((xyz(:,i)-xyz(:,k))**2) - xy = sqrt(d2ij*d2jk) - temp = 0.5d0 * (d2ij+d2jk-d2ik) / xy - if (temp .gt. 1.0d0) temp= 1.0d0 - if (temp .lt. -1.0d0) temp=-1.0d0 - angle = acos( temp ) - -end subroutine bangl pure subroutine banglPBC(mode,xyz,i,j,k,iTr,iTr2,neigh,angle) use xtb_mctc_accuracy, only : wp diff --git a/src/constrain_param.f90 b/src/constrain_param.f90 index 732554a23..83aebae89 100644 --- a/src/constrain_param.f90 +++ b/src/constrain_param.f90 @@ -544,6 +544,7 @@ subroutine set_constr(env,key,val,nat,at,idMap,xyz) use xtb_type_atomlist use xtb_scanparam use xtb_splitparam + use xtb_internals_type, only : angle_value, dihedral_value implicit none character(len=*), parameter :: source = 'userdata_constr' type(TEnvironment), intent(inout) :: env @@ -565,7 +566,6 @@ subroutine set_constr(env,key,val,nat,at,idMap,xyz) logical :: ldum integer :: i,j,k,l real(wp) :: phi,dist,ra(3),rb(3) - real(wp),external :: valijkl integer :: narg character(len=p_str_length),dimension(p_arg_length) :: argv @@ -722,7 +722,7 @@ subroutine set_constr(env,key,val,nat,at,idMap,xyz) potset%angle%n = potset%angle%n-1 ! remove invalid contrain return end if - call bangl(xyz,i,j,k,phi) + phi = angle_value(xyz, i, j, k) if (trim(argv(narg)).eq.'auto') then potset%angle%val(potset%angle%n) = phi else @@ -766,7 +766,7 @@ subroutine set_constr(env,key,val,nat,at,idMap,xyz) potset%dihedral%n = potset%dihedral%n-1 ! remove invalid contrain return end if - phi=valijkl(nat,xyz,i,j,k,l) + phi = dihedral_value(xyz, i, j, k, l) if (trim(argv(narg)).eq.'auto') then potset%dihedral%val(potset%dihedral%n) = phi else @@ -852,7 +852,7 @@ subroutine set_constr(env,key,val,nat,at,idMap,xyz) nconstr = nconstr-1 ! remove invalid contrain return end if - call bangl(xyz,i,j,k,phi) + phi = angle_value(xyz, i, j, k) if (trim(argv(narg)).eq.'auto') then valconstr(nconstr) = phi else @@ -898,7 +898,7 @@ subroutine set_constr(env,key,val,nat,at,idMap,xyz) nconstr = nconstr-1 ! remove invalid contrain return end if - phi=valijkl(nat,xyz,i,j,k,l) + phi = dihedral_value(xyz, i, j, k, l) if (trim(argv(narg)).eq.'auto') then valconstr(nconstr) = phi else diff --git a/src/constrain_pot.f90 b/src/constrain_pot.f90 index 71fad3460..7c849b414 100644 --- a/src/constrain_pot.f90 +++ b/src/constrain_pot.f90 @@ -179,7 +179,7 @@ subroutine constrain_dist(fix,n,at,xyz,g,e) real(wp)va(3),vb(3),vc(3),vab(3),vcb(3),deda(3),dedc(3),dedb(3) real(wp)dda(3),ddb(3),ddc(3),ddd(3) real(wp)c0,rab2,rcb2,theta,dt,rmul1,rmul2,deddt,rp,cosa,ea,vlen,d - real(wp)c1,phi0,phi,dphi1,x1cos,x1sin,dij,valijkl,ff,dum1,dum2 + real(wp)c1,phi0,phi,dphi1,x1cos,x1sin,dij,ff,dum1,dum2 real(wp)dx,dy,dz,termx,termy,termz,abcx,abcy,abcz,a,z0,expo,expom1 if(fix%n.eq.0) return @@ -210,7 +210,8 @@ end subroutine constrain_dist subroutine constrain_angle(fix,n,at,xyz,g,e) use xtb_mctc_accuracy, only : wp use xtb_type_setvar - use xtb_basic_geo + use xtb_internals_type, only : angle_value + use xtb_bmatrix, only : bmat_angle implicit none type(fix_setvar),intent(in) :: fix integer, intent(in) :: n @@ -219,12 +220,8 @@ subroutine constrain_angle(fix,n,at,xyz,g,e) real(wp),intent(inout) :: e real(wp),intent(inout) :: g(3,n) - integer i,j,k,l,m,mm - real(wp)rij(3),dum,r0,ffc,vp(3),ra(3),rb(3) - real(wp)va(3),vb(3),vc(3),vab(3),vcb(3),deda(3),dedc(3),dedb(3) - real(wp)dda(3),ddb(3),ddc(3),ddd(3) - real(wp)c0,rab2,rcb2,theta,dt,rmul1,rmul2,deddt,rp,cosa,ea,vlen,d - real(wp)dx,dy,dz,termx,termy,termz,abcx,abcy,abcz,a,z0,expo,expom1 + integer :: i, j, k, m, mm + real(wp) :: c0, theta, dt, deddt, brow9(9) if(fix%n.eq.0) return @@ -234,32 +231,14 @@ subroutine constrain_angle(fix,n,at,xyz,g,e) j = fix%atoms(mm+1) k = fix%atoms(mm+2) c0 = fix%val(m) - va = xyz(1:3,i) - vb = xyz(1:3,j) - vc = xyz(1:3,k) - vab = va-vb - vcb = vc-vb - rab2 = vab(1)*vab(1) + vab(2)*vab(2) + vab(3)*vab(3) - rcb2 = vcb(1)*vcb(1) + vcb(2)*vcb(2) + vcb(3)*vcb(3) - call crprod(vcb,vab,vp) - rp = norm2(vp)+1.d-14 - call impsc(vab,vcb,cosa) - cosa = dble(min(1.0d0,max(-1.0d0,cosa))) - theta= dacos(cosa) - dt = theta - c0 - ea = fix%fc* dt**2 - deddt = 2.d0 * fix%fc * dt - e = e + ea - call crprod(vab,vp,deda) - rmul1 = -deddt / (rab2*rp) - deda = deda*rmul1 - call crprod(vcb,vp,dedc) - rmul2 = deddt / (rcb2*rp) - dedc = dedc*rmul2 - dedb = deda+dedc - g(:,i) = g(:,i) + deda - g(:,j) = g(:,j) - dedb - g(:,k) = g(:,k) + dedc + theta = angle_value(xyz, i, j, k) + dt = theta - c0 + e = e + fix%fc * dt**2 + deddt = 2.0_wp * fix%fc * dt + brow9 = bmat_angle(xyz(:, i) - xyz(:, j), xyz(:, k) - xyz(:, j)) + g(:, i) = g(:, i) + deddt * brow9(1:3) + g(:, j) = g(:, j) + deddt * brow9(4:6) + g(:, k) = g(:, k) + deddt * brow9(7:9) enddo end subroutine constrain_angle @@ -268,7 +247,8 @@ subroutine constrain_dihedral(fix,n,at,xyz,g,e) use xtb_mctc_accuracy, only : wp use xtb_mctc_constants use xtb_type_setvar - use xtb_basic_geo + use xtb_internals_type, only : dihedral_value + use xtb_bmatrix, only : bmat_torsion implicit none type(fix_setvar),intent(in) :: fix integer, intent(in) :: n @@ -277,13 +257,8 @@ subroutine constrain_dihedral(fix,n,at,xyz,g,e) real(wp),intent(inout) :: e real(wp),intent(inout) :: g(3,n) - integer i,j,k,l,m,mm - real(wp)rij(3),dum,r0,ffc,vp(3),ra(3),rb(3) - real(wp)va(3),vb(3),vc(3),vab(3),vcb(3),deda(3),dedc(3),dedb(3) - real(wp)dda(3),ddb(3),ddc(3),ddd(3) - real(wp)c0,rab2,rcb2,theta,dt,rmul1,rmul2,deddt,rp,cosa,ea,vlen,d - real(wp)c1,phi0,phi,dphi1,x1cos,x1sin,dij,ff,dum1,dum2 - real(wp)dx,dy,dz,termx,termy,termz,abcx,abcy,abcz,a,z0,expo,expom1 + integer :: i, j, k, l, m, mm + real(wp) :: phi0, phi, dphi1, x1cos, x1sin, dij, bt(3, 4) if(fix%n.eq.0) return @@ -294,18 +269,17 @@ subroutine constrain_dihedral(fix,n,at,xyz,g,e) k= fix%atoms(mm+2) l= fix%atoms(mm+3) phi0 =fix%val(m) - phi=valijkl(n,xyz,i,j,k,l) - if(abs(phi-pi).lt.1.d-8.or.abs(phi).lt.1.d-8) phi=phi+1.d-8 - call dphidr(n,xyz,i,j,k,l,phi,dda,ddb,ddc,ddd) - dphi1=phi0-phi+pi - x1cos=cos(dphi1) - x1sin=sin(dphi1) - dij =fix%fc*x1sin - g(:,i)=g(:,i)+dij*dda - g(:,j)=g(:,j)+dij*ddb - g(:,k)=g(:,k)+dij*ddc - g(:,l)=g(:,l)+dij*ddd - e=e+fix%fc*(1.0_wp+x1cos) + phi = dihedral_value(xyz, i, j, k, l) + bt = bmat_torsion(xyz(:, [i, j, k, l])) + dphi1 = phi0 - phi + pi + x1cos = cos(dphi1) + x1sin = sin(dphi1) + dij = fix%fc * x1sin + g(:, i) = g(:, i) + dij * bt(:, 1) + g(:, j) = g(:, j) + dij * bt(:, 2) + g(:, k) = g(:, k) + dij * bt(:, 3) + g(:, l) = g(:, l) + dij * bt(:, 4) + e = e + fix%fc * (1.0_wp + x1cos) enddo end subroutine constrain_dihedral diff --git a/src/geosum.f90 b/src/geosum.f90 index 44a431d8e..cc19557e2 100644 --- a/src/geosum.f90 +++ b/src/geosum.f90 @@ -16,6 +16,9 @@ ! along with xtb. If not, see . subroutine geosum(n,ic,xyzin) + use xtb_mctc_accuracy, only : wp + use xtb_mctc_constants, only : pi + use xtb_internals_type, only : angle_value, dihedral_value use xtb_mctc_convert, only : autoaa use xtb_mctc_symbols, only : toSymbol use xtb_param_atomicrad, only : atomicRad @@ -29,7 +32,7 @@ subroutine geosum(n,ic,xyzin) integer,allocatable :: bondlist(:,:),nbond(:),nty(:) real*8 brad,r,xxx,yyy,zzz,avv - real*8 xyz(3,3),ang,xyz4(3,4),ra(3),rb(3) + real*8 ang,ra(3),rb(3) integer i, j, nn, k, imin, jmin, iat1, iat2 integer nm,ij,m,iat,jat, nsel integer nt,a,b,ia,ib,ja,jb,ii,jj,lin,linij @@ -128,9 +131,6 @@ subroutine geosum(n,ic,xyzin) do i=1,nsel m=nbond(i) if(m.gt.1)then - xyz(1,2)=x(i) - xyz(2,2)=y(i) - xyz(3,2)=z(i) do a=1,m ia=bondlist(i,a) do b=1,a-1 @@ -139,13 +139,7 @@ subroutine geosum(n,ic,xyzin) write(lab,'(a2,i5,''-'',a2,i5,''-'',a2,i5)')& &toSymbol(ic(ia)),ia,toSymbol(ic(i)),i,toSymbol(ic(ib)),ib call rmblank(lab,lab2) - xyz(1,1)=x(ia) - xyz(2,1)=y(ia) - xyz(3,1)=z(ia) - xyz(1,3)=x(ib) - xyz(2,3)=y(ib) - xyz(3,3)=z(ib) - call xbangle(xyz,ang) + ang = angle_value(xyzin, ia, i, ib) * 180.0_wp / pi write(angsum1(nt),'(a,''='',f6.2)')trim(lab2),ang enddo enddo @@ -158,14 +152,8 @@ subroutine geosum(n,ic,xyzin) write(*,*)'---------------------------------' nt=0 do i=1,nsel - xyz4(1,2)=x(i) - xyz4(2,2)=y(i) - xyz4(3,2)=z(i) do j=1,i if(i.eq.j)cycle - xyz4(1,3)=x(j) - xyz4(2,3)=y(j) - xyz4(3,3)=z(j) k=k+1 r=sqrt((x(i)-x(j))**2+(y(i)-y(j))**2+(z(i)-z(j))**2)& & *0.529177260d0 @@ -184,13 +172,7 @@ subroutine geosum(n,ic,xyzin) & toSymbol(ic(ib)),ib,toSymbol(ic(i)),i,& & toSymbol(ic(j)),j, toSymbol(ic(jb)),jb call rmblank(lab3,lab4) - xyz4(1,1)=x(ib) - xyz4(2,1)=y(ib) - xyz4(3,1)=z(ib) - xyz4(1,4)=x(jb) - xyz4(2,4)=y(jb) - xyz4(3,4)=z(jb) - call xdihed(xyz4,ang) + ang = modulo(dihedral_value(xyzin, ib, i, j, jb), 2.0_wp * pi) * 180.0_wp / pi write(angsum2(nt),'(a,''='',f6.2)')& & trim(lab4),ang endif @@ -230,127 +212,3 @@ subroutine rmblank(as,re) end do end subroutine rmblank -subroutine xbangle(xyz,angle) - implicit integer (i-n) - implicit double precision (a-h,o-z) - dimension xyz(3,3) - !******************************************************************** - ! - ! bangle calculates the angle between atoms i,j, and k. the - ! cartesian coordinates are in xyz. - ! - !******************************************************************** - i=1 - j=2 - k=3 - d2ij = (xyz(1,i)-xyz(1,j))**2+& - & (xyz(2,i)-xyz(2,j))**2+& - & (xyz(3,i)-xyz(3,j))**2 - d2jk = (xyz(1,j)-xyz(1,k))**2+& - & (xyz(2,j)-xyz(2,k))**2+& - & (xyz(3,j)-xyz(3,k))**2 - d2ik = (xyz(1,i)-xyz(1,k))**2+& - & (xyz(2,i)-xyz(2,k))**2+& - & (xyz(3,i)-xyz(3,k))**2 - xy = sqrt(d2ij*d2jk) - temp = 0.5d0 * (d2ij+d2jk-d2ik) / xy - if (temp .gt. 1.0d0) temp=1.0d0 - if (temp .lt. -1.0d0) temp=-1.0d0 - angle = acos( temp )*180.0d0/3.14159265358979d0 - return -end subroutine xbangle -subroutine xdihed(xyz,angle) - implicit integer (i-n) - implicit double precision (a-h,o-z) - dimension xyz(3,4) - !******************************************************************** - ! - ! dihed calculates the dihedral angle between atoms i, j, k, - ! and l. the cartesian coordinates of these atoms - ! are in array xyz. - ! - ! dihed is a modified version of a subroutine of the same name - ! which was written by dr. w. theil in 1973. - ! - !******************************************************************** - data pi/3.14159265358979d0/ - i=1 - j=2 - k=3 - l=4 - xi1=xyz(1,i)-xyz(1,k) - xj1=xyz(1,j)-xyz(1,k) - xl1=xyz(1,l)-xyz(1,k) - yi1=xyz(2,i)-xyz(2,k) - yj1=xyz(2,j)-xyz(2,k) - yl1=xyz(2,l)-xyz(2,k) - zi1=xyz(3,i)-xyz(3,k) - zj1=xyz(3,j)-xyz(3,k) - zl1=xyz(3,l)-xyz(3,k) - ! rotate around z axis to put kj along y axis - dist= sqrt(xj1**2+yj1**2+zj1**2) - cosa=zj1/dist - if(cosa.gt.1.0d0) cosa=1.0d0 - if(cosa.lt.-1.0d0) cosa=-1.0d0 - ddd=1.0d0-cosa**2 - if(ddd.le.0.0) go to 10 - yxdist=dist* sqrt(ddd) - if(yxdist.gt.1.0d-9) go to 20 - 10 continue - xi2=xi1 - xl2=xl1 - yi2=yi1 - yl2=yl1 - costh=cosa - sinth=0.d0 - go to 30 - 20 cosph=yj1/yxdist - sinph=xj1/yxdist - xi2=xi1*cosph-yi1*sinph - xj2=xj1*cosph-yj1*sinph - xl2=xl1*cosph-yl1*sinph - yi2=xi1*sinph+yi1*cosph - yj2=xj1*sinph+yj1*cosph - yl2=xl1*sinph+yl1*cosph - ! rotate kj around the x axis so kj lies along the z axis - costh=cosa - sinth=yj2/dist - 30 continue - yi3=yi2*costh-zi1*sinth - yl3=yl2*costh-zl1*sinth - call xdang(xl2,yl3,xi2,yi3,angle) - if (angle .lt. 0.) angle=2.0d0*pi+angle - if (angle .ge. 2.0d0*pi ) angle=0.d0 - angle=angle*180.0d0/pi - return -end subroutine xdihed -subroutine xdang(a1,a2,b1,b2,rcos) - implicit double precision (a-h,o-z) - !********************************************************************* - ! - ! dang determines the angle between the points (a1,a2), (0,0), - ! and (b1,b2). the result is put in rcos. - ! - !********************************************************************* - data pi/3.14159265358979d0/ - zero=1.0d-6 - if( abs(a1).lt.zero.and. abs(a2).lt.zero) go to 10 - if( abs(b1).lt.zero.and. abs(b2).lt.zero) go to 10 - anorm=1.0d0/ sqrt(a1**2+a2**2) - bnorm=1.0d0/ sqrt(b1**2+b2**2) - a1=a1*anorm - a2=a2*anorm - b1=b1*bnorm - b2=b2*bnorm - sinth=(a1*b2)-(a2*b1) - costh=a1*b1+a2*b2 - if(costh.gt.1.0d0) costh=1.0d0 - if(costh.lt.-1.0d0) costh=-1.0d0 - rcos= acos(costh) - if( abs(rcos).lt.4.0d-4) go to 10 - if(sinth.gt.0.d0) rcos=2.0d0*pi-rcos - rcos=-rcos - return - 10 rcos=0.0d0 - return -end subroutine xdang diff --git a/src/gfnff/math.f b/src/gfnff/math.f index 544dd7f41..801ebf9aa 100644 --- a/src/gfnff/math.f +++ b/src/gfnff/math.f @@ -50,7 +50,7 @@ real*8 function valijklff(natoms,xyz,i,j,k,l) implicit none external - , vecnorm,valijk + , vecnorm integer , ic,i,j,k,l,natoms @@ -58,11 +58,9 @@ real*8 function valijklff(natoms,xyz,i,j,k,l) real*8 , xyz(3,natoms), , eps,ra(3),rb(3),rc(3),na(3),nb(3), - , rab,rbc,thab,thbc,valijk, - , vecnorm,nan,nbn,rcn,snanb,deter,pi + , vecnorm,nan,nbn,snanb parameter (eps=1.0d-14) - data pi/3.1415926535897932384626433832795029d0/ c ... get torsion coordinate do ic=1,3 @@ -71,13 +69,6 @@ real*8 function valijklff(natoms,xyz,i,j,k,l) rc(ic)=xyz(ic,l)-xyz(ic,k) end do -c ... determinante of rb,ra,rc - deter= ra(1)*(rb(2)*rc(3)-rb(3)*rc(2)) - , -ra(2)*(rb(1)*rc(3)-rb(3)*rc(1)) - , +ra(3)*(rb(1)*rc(2)-rb(2)*rc(1)) - - thab=valijk(natoms,xyz,i,k,j) - thbc=valijk(natoms,xyz,j,l,k) call crossprod(ra,rb,na) call crossprod(rb,rc,nb) nan=vecnorm(na,3,1) @@ -101,20 +92,17 @@ real*8 function valijklffPBC(mo,natoms,xyz,i,j,k,l,vTrj,vTrk,vTrl) implicit none external - , vecnorm,valijkPBC + , vecnorm integer , ic,i,j,k,l,natoms,mo real*8 - , xyz(3,natoms),vTrj(3),vTrk(3),vTrl(3),vDum(3), + , xyz(3,natoms),vTrj(3),vTrk(3),vTrl(3), , eps,ra(3),rb(3),rc(3),na(3),nb(3), - , rab,rbc,thab,thbc,valijkPBC, - , vecnorm,nan,nbn,rcn,snanb,deter,pi + , vecnorm,nan,nbn,snanb parameter (eps=1.0d-14) - data pi/3.1415926535897932384626433832795029d0/ - vDum=0.0 ! Dummy vector for valijkPBC function c ... get torsion coordinate if(mo.eq.1)then ! egtors call -> j (=ii) in central cell @@ -131,20 +119,6 @@ real*8 function valijklffPBC(mo,natoms,xyz,i,j,k,l,vTrj,vTrk,vTrl) enddo endif -c ... determinante of rb,ra,rc (triple product) - deter= ra(1)*(rb(2)*rc(3)-rb(3)*rc(2)) - , -ra(2)*(rb(1)*rc(3)-rb(3)*rc(1)) - , +ra(3)*(rb(1)*rc(2)-rb(2)*rc(1)) - - if(mo.eq.1)then ! not used - thab=valijkPBC(1,natoms,xyz,i,k,j,vTrl,vTrj,vDum) - thbc=valijkPBC(2,natoms,xyz,j,l,k,vTrk,vTrj,vDum) - else - thab=valijkPBC(3,natoms,xyz,i,k,j,vTrj,vTrl,vTrk) ! i=R k=C j=B - ! vTrj=vTrR vTrl=vTrC vTrk=vTrB - thbc=valijkPBC(4,natoms,xyz,j,l,k,vTrk,vTrl,vDum) ! j=B l=H k=C - ! vTrk=vTrB vTrl=vTrC - endif call crossprod(ra,rb,na) call crossprod(rb,rc,nb) nan=vecnorm(na,3,1) diff --git a/src/iff/iff_lmo.f90 b/src/iff/iff_lmo.f90 index 57657cc5b..6a6ecaa46 100644 --- a/src/iff/iff_lmo.f90 +++ b/src/iff/iff_lmo.f90 @@ -351,6 +351,7 @@ end subroutine setlmo !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc subroutine internal4(n, xyz, c, j, k, l, r, alp, bet) + use xtb_internals_type, only : angle_value, dihedral_value integer, intent(in) :: n, j, k, l real(wp), intent(in) :: xyz(3, n), c(3) real(wp), intent(out) :: r, alp, bet @@ -367,10 +368,10 @@ subroutine internal4(n, xyz, c, j, k, l, r, alp, bet) xyz2(1:3, 1) = c(1:3) xyz2(1:3, 2) = xyz(1:3, j) xyz2(1:3, 3) = xyz(1:3, k) - call xbangle(XYZ2, 1, 2, 3, alp) + alp = angle_value(xyz2, 1, 2, 3) if (l .gt. 0) then xyz2(1:3, 4) = xyz(1:3, l) - call xDIHED(XYZ2, 1, 2, 3, 4, bet) + bet = dihedral_value(xyz2, 1, 2, 3, 4) end if end if @@ -469,122 +470,5 @@ subroutine trflp(n, coord, nai, nbi, nci, geo1i, geo2i, geo3i, ci) end subroutine trflp !cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc - subroutine xBANGLE(XYZ, I, J, K, ANGLE) - integer, intent(in) :: I, J, K - real(wp), intent(in) :: XYZ(3, *) - real(wp), intent(out) :: ANGLE - - real(wp) :: D2IJ, D2JK, D2IK, XY, TEMP -! BANGLE CALCULATES THE ANGLE BETWEEN ATOMS I,J, AND K. THE -! CARTESIAN COORDINATES ARE IN XYZ. - D2IJ = (XYZ(1, I) - XYZ(1, J))**2 +& - &(XYZ(2, I) - XYZ(2, J))**2 +& - &(XYZ(3, I) - XYZ(3, J))**2 - D2JK = (XYZ(1, J) - XYZ(1, K))**2 +& - &(XYZ(2, J) - XYZ(2, K))**2 +& - &(XYZ(3, J) - XYZ(3, K))**2 - D2IK = (XYZ(1, I) - XYZ(1, K))**2 +& - &(XYZ(2, I) - XYZ(2, K))**2 +& - &(XYZ(3, I) - XYZ(3, K))**2 - XY = SQRT(D2IJ*D2JK + 1.d-14) - TEMP = 0.5D0*(D2IJ + D2JK - D2IK)/XY - IF (TEMP .GT. 1.0D0) TEMP = 1.0D0 - IF (TEMP .LT. -1.0D0) TEMP = -1.0D0 - ANGLE = ACOS(TEMP) - RETURN - end subroutine xBANGLE - - subroutine xDIHED(XYZ, I, J, K, L, ANGLE) - real(wp), intent(in) :: XYZ(3, 4) - integer, intent(in) :: I, J, K, L - real(wp), intent(out) :: ANGLE - - real(wp) :: XI1, XJ1, XL1, YI1, YJ1, YL1, ZI1, ZJ1, ZL1 - real(wp) :: XI2, XJ2, XL2, YI2, YJ2, YL2, ZI2, ZJ2, ZL2 - real(wp) :: YI3, YL3 - real(wp) :: DIST, COSA, DDD, YXDIST, COSTH, SINTH, COSPH, SINPH - - real(wp), parameter :: PI = 3.14159265358979_wp - XI1 = XYZ(1, I) - XYZ(1, K) - XJ1 = XYZ(1, J) - XYZ(1, K) - XL1 = XYZ(1, L) - XYZ(1, K) - YI1 = XYZ(2, I) - XYZ(2, K) - YJ1 = XYZ(2, J) - XYZ(2, K) - YL1 = XYZ(2, L) - XYZ(2, K) - ZI1 = XYZ(3, I) - XYZ(3, K) - ZJ1 = XYZ(3, J) - XYZ(3, K) - ZL1 = XYZ(3, L) - XYZ(3, K) -! ROTATE AROUND Z AXIS TO PUT KJ ALONG Y AXIS - DIST = SQRT(XJ1**2 + YJ1**2 + ZJ1**2 + 1.d-14) - COSA = ZJ1/DIST - IF (COSA .GT. 1.0D0) COSA = 1.0D0 - IF (COSA .LT. -1.0D0) COSA = -1.0D0 - DDD = 1.0D0 - COSA**2 - IF (DDD .GT. 0.0) then - YXDIST = DIST*SQRT(DDD) - end if - IF (YXDIST .LE. 1.0D-9 .AND. DDD .GT. 0.0) then - XI2 = XI1 - XL2 = XL1 - YI2 = YI1 - YL2 = YL1 - COSTH = COSA - SINTH = 0.D0 - end if - IF (YXDIST .GT. 1.0D-9 .OR. DDD .LE. 0.0) then - COSPH = YJ1/YXDIST - SINPH = XJ1/YXDIST - XI2 = XI1*COSPH - YI1*SINPH - XJ2 = XJ1*COSPH - YJ1*SINPH - XL2 = XL1*COSPH - YL1*SINPH - YI2 = XI1*SINPH + YI1*COSPH - YJ2 = XJ1*SINPH + YJ1*COSPH - YL2 = XL1*SINPH + YL1*COSPH -! ROTATE KJ AROUND THE X AXIS SO KJ LIES ALONG THE Z AXIS - COSTH = COSA - SINTH = YJ2/DIST - end if - YI3 = YI2*COSTH - ZI1*SINTH - YL3 = YL2*COSTH - ZL1*SINTH - CALL xDANG(XL2, YL3, XI2, YI3, ANGLE) - IF (ANGLE .LT. 0.) ANGLE = 2.0D0*PI + ANGLE - IF (ANGLE .GE. 2.0d0*PI) ANGLE = 0.D0 - RETURN - end subroutine xDIHED - - subroutine xDANG(A1, A2, B1, B2, RCOS) - real(wp), intent(inout) :: A1, A2, B1, B2 - real(wp), intent(out) :: RCOS - - real(wp) :: ZERO, SINTH, COTH, ANORM, BNORM, COSTH - real(wp), parameter :: PI = 3.14159265358979_wp - ZERO = 1.0D-6 - IF (ABS(A1) .LT. ZERO .AND. ABS(A2) .LT. ZERO) then - RCOS = 0.0D0 - return - end if - IF (ABS(B1) .LT. ZERO .AND. ABS(B2) .LT. ZERO) then - RCOS = 0.0D0 - return - end if - ANORM = 1.0D0/SQRT(A1**2 + A2**2) - BNORM = 1.0D0/SQRT(B1**2 + B2**2) - A1 = A1*ANORM - A2 = A2*ANORM - B1 = B1*BNORM - B2 = B2*BNORM - SINTH = (A1*B2) - (A2*B1) - COSTH = A1*B1 + A2*B2 - IF (COSTH .GT. 1.0D0) COSTH = 1.0D0 - IF (COSTH .LT. -1.0D0) COSTH = -1.0D0 - RCOS = ACOS(COSTH) - IF (ABS(RCOS) .GE. 4.0D-4) then - IF (SINTH .GT. 0.D0) RCOS = 2.0D0*PI - RCOS - RCOS = -RCOS - RETURN - END IF - RCOS = 0.0D0 - RETURN - end subroutine xDANG end module xtb_iff_ifflmo diff --git a/src/internals/redundant.f90 b/src/internals/redundant.f90 index e7de4dd74..5ce291a76 100644 --- a/src/internals/redundant.f90 +++ b/src/internals/redundant.f90 @@ -23,12 +23,10 @@ module xtb_internals_redundant use xtb_mctc_accuracy, only : wp use xtb_mctc_constants, only : pi - use xtb_mctc_math, only : crossProd - use xtb_basic_geo, only : bangl use xtb_bmatrix, only : linbend_frame use xtb_internals_graph, only : graph_type use xtb_internals_type, only : internal_coords_set_type, coord_bond, & - & coord_angle, coord_dihedral, coord_linbend + & coord_angle, coord_dihedral, coord_linbend, angle_value, dihedral_value implicit none private @@ -80,7 +78,7 @@ pure subroutine new_redundant(self, graph, xyz) real(wp), intent(in) :: xyz(:, :) integer :: i, j, k, a, b, ia, la, ndeg, component - real(wp) :: angle, b1(3), b2(3), b3(3), n1(3), n2(3), m(3) + real(wp) :: angle real(wp) :: v1(3), v2(3), bend_frame(3, 2) self%n = graph%n @@ -99,7 +97,7 @@ pure subroutine new_redundant(self, graph, xyz) ndeg = graph%neighs(i) do a = 1, ndeg - 1 do b = a + 1, ndeg - call bangl(xyz, graph%i_neigh(a, i), i, graph%i_neigh(b, i), angle) + angle = angle_value(xyz, graph%i_neigh(a, i), i, graph%i_neigh(b, i)) if (angle > pi - lin_tol) then self%nangle = self%nangle + 2 else @@ -147,7 +145,7 @@ pure subroutine new_redundant(self, graph, xyz) ndeg = graph%neighs(i) do a = 1, ndeg - 1 do b = a + 1, ndeg - call bangl(xyz, graph%i_neigh(a, i), i, graph%i_neigh(b, i), angle) + angle = angle_value(xyz, graph%i_neigh(a, i), i, graph%i_neigh(b, i)) if (angle > pi - lin_tol) then v1 = xyz(:, graph%i_neigh(a, i)) - xyz(:, i) v1 = v1/norm2(v1) @@ -191,13 +189,7 @@ pure subroutine new_redundant(self, graph, xyz) self%atoms(2, ia) = i self%atoms(3, ia) = j self%atoms(4, ia) = la - b1 = xyz(:, i) - xyz(:, self%atoms(1, ia)) - b2 = xyz(:, j) - xyz(:, i) - b3 = xyz(:, la) - xyz(:, j) - n1 = crossProd(b1, b2) - n2 = crossProd(b2, b3) - m = crossProd(n1, b2/norm2(b2)) - self%q(ia) = atan2(dot_product(m, n2), dot_product(n1, n2)) + self%q(ia) = dihedral_value(xyz, self%atoms(1, ia), i, j, la) end do end do end do @@ -217,8 +209,8 @@ pure logical function valid_torsion(xyz, i, j, k, l) real(wp) :: angle1, angle2 - call bangl(xyz, i, j, k, angle1) - call bangl(xyz, j, k, l, angle2) + angle1 = angle_value(xyz, i, j, k) + angle2 = angle_value(xyz, j, k, l) valid_torsion = angle1 >= lin_tol .and. angle1 <= pi - lin_tol & & .and. angle2 >= lin_tol .and. angle2 <= pi - lin_tol end function valid_torsion diff --git a/src/internals/type.f90 b/src/internals/type.f90 index 687370682..50058d18b 100644 --- a/src/internals/type.f90 +++ b/src/internals/type.f90 @@ -22,11 +22,12 @@ !> it. Concrete sets extend this type and add their own state. module xtb_internals_type use xtb_mctc_accuracy, only : wp + use xtb_mctc_math, only : crossProd implicit none private public :: internal_coords_set_type, coord_bond, coord_angle, coord_dihedral, & - & coord_linbend + & coord_linbend, angle_value, dihedral_value !> Bond coordinate kind, one coordinate per edge integer, parameter :: coord_bond = 1 @@ -58,4 +59,45 @@ module xtb_internals_type end type internal_coords_set_type + +contains + + +!> Valence angle i-j-k with centre j, in radians in [0, pi] +pure function angle_value(xyz, i, j, k) result(val) + !> Cartesian coordinates, dimension (3, nat) + real(wp), intent(in) :: xyz(:, :) + !> Atom indices, centre at j + integer, intent(in) :: i, j, k + !> Valence angle in radians + real(wp) :: val + + real(wp) :: v1(3), v2(3) + + v1 = xyz(:, i) - xyz(:, j) + v2 = xyz(:, k) - xyz(:, j) + val = atan2(norm2(crossProd(v1, v2)), dot_product(v1, v2)) +end function angle_value + + +!> IUPAC (Klyne-Prelog) dihedral angle i-j-k-l, in radians in [-pi, pi] +pure function dihedral_value(xyz, i, j, k, l) result(val) + !> Cartesian coordinates, dimension (3, nat) + real(wp), intent(in) :: xyz(:, :) + !> Atom indices along the torsion + integer, intent(in) :: i, j, k, l + !> Dihedral angle in radians + real(wp) :: val + + real(wp) :: b1(3), b2(3), b3(3), n1(3), n2(3), m(3) + + b1 = xyz(:, j) - xyz(:, i) + b2 = xyz(:, k) - xyz(:, j) + b3 = xyz(:, l) - xyz(:, k) + n1 = crossProd(b1, b2) + n2 = crossProd(b2, b3) + m = crossProd(b2 / norm2(b2), n1) + val = atan2(dot_product(m, n2), dot_product(n1, n2)) +end function dihedral_value + end module xtb_internals_type diff --git a/src/intmodes.f90 b/src/intmodes.f90 index f3dc8eba0..333f2c87c 100644 --- a/src/intmodes.f90 +++ b/src/intmodes.f90 @@ -469,6 +469,8 @@ subroutine zmat2cart(n,n2,at,geo,xyz,na,nb,nc,fail) subroutine xyzgeo(xyz,numat,na,nb,nc,degree,geo) use xtb_mctc_accuracy, only : wp + use xtb_mctc_constants, only : pi + use xtb_internals_type, only : angle_value, dihedral_value implicit none integer, intent(in) :: numat real(wp),intent(in) :: xyz(3,numat) @@ -500,41 +502,11 @@ subroutine xyzgeo(xyz,numat,na,nb,nc,degree,geo) l=nc(i) if(i.lt.3) cycle ii=i - call bangle(xyz,ii,j,k,geo(2,i)) + geo(2,i) = angle_value(xyz, ii, j, k) geo(2,i)=geo(2,i)*degree if(i.lt.4) cycle - ! - ! make sure dihedral is meaninglful - ! - ! call bangle(xyz,j,k,l,angl) - ! tol=0.2617994d0 - ! if(angl.gt.3.1415926d0-tol.or.angl.lt.tol)then - ! - ! angle is unsatisfactory, let's search for another atom for - ! defining the dihedral. - ! 88 sum=100.d0 - ! do 74 i1=1,ii-1 - ! r=(xyz(1,i1)-xyz(1,k))**2+ - ! 1 (xyz(2,i1)-xyz(2,k))**2+ - ! 2 (xyz(3,i1)-xyz(3,k))**2 - ! if(r.lt.sum.and.i1.ne.j.and.i1.ne.k) then - ! call bangle(xyz,j,k,i1,angl) - ! if(angl.lt.3.1415926d0-tol.or.angl.gt.tol)then - ! sum=r - ! l=i1 - ! nc(ii)=l - ! endif - ! endif - ! 74 continue - ! if(sum.gt.99.d0.and.tol.gt.0.1d0)then - ! - ! anything within 5 degrees? - ! - ! tol=0.087266d0 - ! goto 88 - ! endif - ! endif - call dihed(xyz,ii,j,k,l,geo(3,i)) + geo(3,i) = dihedral_value(xyz, ii, j, k, l) + if (geo(3,i) > 0.0_wp) geo(3,i) = geo(3,i) - 2.0_wp*pi geo(3,i)=geo(3,i)*degree geo(1,i)= sqrt((xyz(1,i)-xyz(1,j))**2+& & (xyz(2,i)-xyz(2,j))**2+& @@ -619,136 +591,6 @@ subroutine xyzint(xyz,numat,na,nb,nc,degree,geo) call xyzgeo(xyz,numat,na,nb,nc,degree,geo) end subroutine xyzint -pure subroutine bangle(xyz,i,j,k,angle) - use xtb_mctc_accuracy, only : wp - implicit none - integer, intent(in) :: i,j,k - real(wp),intent(in) :: xyz(3,*) - real(wp),intent(out) :: angle - !******************************************************************** - ! - ! bangle calculates the angle between atoms i,j, and k. the - ! cartesian coordinates are in xyz. - ! - !******************************************************************** - real(wp) :: d2ij,d2jk,d2ik,xy,temp - d2ij = (xyz(1,i)-xyz(1,j))**2+& - & (xyz(2,i)-xyz(2,j))**2+& - & (xyz(3,i)-xyz(3,j))**2 - d2jk = (xyz(1,j)-xyz(1,k))**2+& - & (xyz(2,j)-xyz(2,k))**2+& - & (xyz(3,j)-xyz(3,k))**2 - d2ik = (xyz(1,i)-xyz(1,k))**2+& - & (xyz(2,i)-xyz(2,k))**2+& - & (xyz(3,i)-xyz(3,k))**2 - xy = sqrt(d2ij*d2jk+1.d-14) - temp = 0.5d0 * (d2ij+d2jk-d2ik) / xy - if (temp .gt. 1.0d0) temp=1.0d0 - if (temp .lt. -1.0d0) temp=-1.0d0 - angle = acos( temp ) -end subroutine bangle -subroutine dihed(xyz,i,j,k,l,angle) - use xtb_mctc_accuracy, only : wp - implicit none - integer, intent(in) :: i,j,k,l - real(wp),intent(inout) :: angle - real(wp),intent(in) :: xyz(3,*) - !******************************************************************** - ! - ! dihed calculates the dihedral angle between atoms i, j, k, - ! and l. the cartesian coordinates of these atoms - ! are in array xyz. - ! - ! dihed is a modified version of a subroutine of the same name - ! which was written by dr. w. theil in 1973. - ! - !******************************************************************** - real(wp),parameter :: pi = 3.14159265358979d0 - real(wp) :: xi1,xj1,xl1,xi2,xj2,xl2 - real(wp) :: yi1,yj1,yl1,yi2,yj2,yl2,yi3,yl3 - real(wp) :: zi1,zj1,zl1 - real(wp) :: dist,yxdist - real(wp) :: cosa,sinth,costh,cosph,sinph - real(wp) :: ddd - xi1=xyz(1,i)-xyz(1,k) - xj1=xyz(1,j)-xyz(1,k) - xl1=xyz(1,l)-xyz(1,k) - yi1=xyz(2,i)-xyz(2,k) - yj1=xyz(2,j)-xyz(2,k) - yl1=xyz(2,l)-xyz(2,k) - zi1=xyz(3,i)-xyz(3,k) - zj1=xyz(3,j)-xyz(3,k) - zl1=xyz(3,l)-xyz(3,k) - ! rotate around z axis to put kj along y axis - dist= sqrt(xj1**2+yj1**2+zj1**2+1.d-14) - cosa=zj1/dist - if(cosa.gt.1.0d0) cosa=1.0d0 - if(cosa.lt.-1.0d0) cosa=-1.0d0 - ddd=1.0d0-cosa**2 - if(ddd.le.0.0) go to 10 - yxdist=dist* sqrt(ddd) - if(yxdist.gt.1.0d-12) go to 20 - 10 continue - xi2=xi1 - xl2=xl1 - yi2=yi1 - yl2=yl1 - costh=cosa - sinth=0.d0 - go to 30 - 20 cosph=yj1/yxdist - sinph=xj1/yxdist - xi2=xi1*cosph-yi1*sinph - xj2=xj1*cosph-yj1*sinph - xl2=xl1*cosph-yl1*sinph - yi2=xi1*sinph+yi1*cosph - yj2=xj1*sinph+yj1*cosph - yl2=xl1*sinph+yl1*cosph - ! rotate kj around the x axis so kj lies along the z axis - costh=cosa - sinth=yj2/dist - 30 continue - yi3=yi2*costh-zi1*sinth - yl3=yl2*costh-zl1*sinth - call dang(xl2,yl3,xi2,yi3,angle) - ! if (angle .lt. 0.) angle=2.0d0*pi+angle - !sg if (angle .ge. 2.0d0*pi ) angle=0.d0 -end subroutine dihed -pure subroutine dang(a1,a2,b1,b2,rcos) - use xtb_mctc_accuracy, only : wp - implicit none - real(wp),intent(inout) :: a1,a2,b1,b2 - real(wp),intent(out) :: rcos - !********************************************************************* - ! - ! dang determines the angle between the points (a1,a2), (0,0), - ! and (b1,b2). the result is put in rcos. - ! - !********************************************************************* - real(wp),parameter :: pi = 3.14159265358979d0 - real(wp) :: zero - real(wp) :: anorm,bnorm - real(wp) :: sinth,costh - zero=1.0d-10 - if( abs(a1).lt.zero.and. abs(a2).lt.zero) go to 10 - if( abs(b1).lt.zero.and. abs(b2).lt.zero) go to 10 - anorm=1.0d0/ sqrt(a1**2+a2**2) - bnorm=1.0d0/ sqrt(b1**2+b2**2) - a1=a1*anorm - a2=a2*anorm - b1=b1*bnorm - b2=b2*bnorm - sinth=(a1*b2)-(a2*b1) - costh=a1*b1+a2*b2 - if(costh.gt.1.0d0) costh=1.0d0 - if(costh.lt.-1.0d0) costh=-1.0d0 - rcos= acos(costh) - if( abs(rcos).lt.zero) go to 10 - if(sinth.gt.0.d0) rcos=2.0d0*pi-rcos - rcos=-rcos - return - 10 rcos=0.0d0 -end subroutine dang !cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc diff --git a/src/main/geometry.f90 b/src/main/geometry.f90 index 7d7d6185c..460315880 100644 --- a/src/main/geometry.f90 +++ b/src/main/geometry.f90 @@ -419,6 +419,7 @@ end subroutine calc_distances subroutine calc_angles(n,at,xyz,bond,maxbend,nbend,bend,ib) use xtb_mctc_accuracy, only : wp + use xtb_internals_type, only : angle_value implicit none integer, intent(in) :: n integer, intent(in) :: at(n) @@ -446,7 +447,7 @@ subroutine calc_angles(n,at,xyz,bond,maxbend,nbend,bend,ib) do k = 1, j-1 if (i.eq.k .or. j.eq.k) cycle if (bond(k,j).lt.1) cycle - call bangl(xyz,k,j,i,phi) + phi = angle_value(xyz, k, j, i) if (m.ge.maxbend) exit get_bend m = m+1 ib(1,m) = k @@ -462,6 +463,7 @@ end subroutine calc_angles subroutine calc_torsions(n,at,xyz,bond,maxtrsn,ntrsn,trsn,it) use xtb_mctc_accuracy, only : wp + use xtb_internals_type, only : dihedral_value implicit none integer, intent(in) :: n integer, intent(in) :: at(n) @@ -474,7 +476,6 @@ subroutine calc_torsions(n,at,xyz,bond,maxtrsn,ntrsn,trsn,it) integer :: i,j,k,l,m real(wp) :: phi - real(wp),external :: valijkl m = 0 get_trsn: do i = 1, n @@ -488,7 +489,7 @@ subroutine calc_torsions(n,at,xyz,bond,maxtrsn,ntrsn,trsn,it) do l = 1, n if (bond(l,k).lt.1) cycle if (i.eq.l .or. j.eq.l .or. k.eq.l) cycle - phi = valijkl(n,xyz,l,k,j,i) + phi = dihedral_value(xyz, l, k, j, i) if (m.ge.maxtrsn) exit get_trsn m = m+1 it(1,m) = l diff --git a/src/meson.build b/src/meson.build index 2b3d88c2c..87f39ffa1 100644 --- a/src/meson.build +++ b/src/meson.build @@ -44,7 +44,6 @@ srcs += files( 'aoparam.f90', 'approxrab.f90', 'axis_trafo.f90', - 'basic_geo.f90', 'bfgs.f90', 'bias_path.f90', 'blowsy.f90', diff --git a/src/scanparam.f90 b/src/scanparam.f90 index 631f9b338..41ee9cc6d 100644 --- a/src/scanparam.f90 +++ b/src/scanparam.f90 @@ -148,13 +148,13 @@ subroutine pot_info(iunit,n,at,xyz) use xtb_mctc_convert use xtb_mctc_symbols, only : toSymbol use xtb_mctc_blas, only : mctc_nrm2 + use xtb_internals_type, only : angle_value, dihedral_value implicit none integer, intent(in) :: iunit integer, intent(in) :: n integer, intent(in) :: at(n) real(wp),intent(in) :: xyz(3,n) - real(wp),external :: valijkl integer :: i,ii,j,k,l,m,mm real(wp) :: val0,val @@ -254,7 +254,7 @@ subroutine pot_info(iunit,n,at,xyz) j = potset%angle%atoms(mm+1) k = potset%angle%atoms(mm+2) val0 = potset%angle%val(m) - call bangl(xyz,i,j,k,val) + val = angle_value(xyz, i, j, k) write(iunit,'(3(i6,1x,i3,1x,a2),13x,1x,2f14.7)') & i,at(i),toSymbol(at(i)), & j,at(j),toSymbol(at(j)), & @@ -290,7 +290,7 @@ subroutine pot_info(iunit,n,at,xyz) k = potset%dihedral%atoms(mm+2) l = potset%dihedral%atoms(mm+3) val0 = potset%dihedral%val(m) - val = valijkl(n,xyz,i,j,k,l) + val = dihedral_value(xyz, i, j, k, l) write(iunit,'(4(i6,1x,i3,1x,a2),1x,2f14.7)') & i,at(i),toSymbol(at(i)), & j,at(j),toSymbol(at(j)), & @@ -353,6 +353,7 @@ end subroutine constrain_all_bonds subroutine constrain_all_angles(n,at,xyz) use xtb_mctc_constants use xtb_mctc_param + use xtb_internals_type, only : angle_value implicit none integer, intent(in) :: n integer, intent(in) :: at(n) @@ -376,7 +377,7 @@ subroutine constrain_all_angles(n,at,xyz) do k = 1, j-1 if (i.eq.k .or. j.eq.k) cycle if (bond(k,j).lt.1) cycle - call bangl(xyz,k,j,i,phi) + phi = angle_value(xyz, k, j, i) if (abs(pi-phi).lt.thr) cycle ioffset = 3*potset%angle%n potset%angle%n = potset%angle%n+1 @@ -393,6 +394,7 @@ end subroutine constrain_all_angles subroutine constrain_all_torsions(n,at,xyz) use xtb_mctc_constants use xtb_mctc_param + use xtb_internals_type, only : angle_value, dihedral_value implicit none integer, intent(in) :: n integer, intent(in) :: at(n) @@ -403,7 +405,6 @@ subroutine constrain_all_torsions(n,at,xyz) real(wp) :: phi,thijk,thjkl integer, allocatable :: bond(:,:) real(wp),parameter :: thr = 0.2_wp - real(wp),external :: valijkl allocate( bond(n,n), source = 0 ) call get_bonds(n,at,xyz,bond) @@ -419,10 +420,10 @@ subroutine constrain_all_torsions(n,at,xyz) do l = 1, n if (bond(l,k).lt.1) cycle if (i.eq.l .or. j.eq.l .or. k.eq.l) cycle - call bangl(xyz,k,j,i,thijk) - call bangl(xyz,l,k,j,thjkl) + thijk = angle_value(xyz, k, j, i) + thjkl = angle_value(xyz, l, k, j) if (abs(pi-thijk).lt.thr .or. abs(pi-thjkl).lt.thr) cycle - phi = valijkl(n,xyz,l,k,j,i) + phi = dihedral_value(xyz, l, k, j, i) ioffset = 4*potset%dihedral%n potset%dihedral%n = potset%dihedral%n+1 potset%dihedral%atoms(ioffset+1) = l diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index fdf32dcc6..f8994cd32 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -30,6 +30,7 @@ set( "detrotra" "docking" "eeq" + "geovalues" "gfn0" "gfn1" "gfn2" diff --git a/test/unit/main.f90 b/test/unit/main.f90 index d9e76c709..ec6d9fd55 100644 --- a/test/unit/main.f90 +++ b/test/unit/main.f90 @@ -29,6 +29,7 @@ program tester use test_docking, only : collect_docking use test_eeq, only : collect_eeq use test_fermi, only : collect_fermi + use test_geovalues, only : collect_geovalues use test_gfn0, only : collect_gfn0 use test_gfn1, only : collect_gfn1 use test_gfn2, only : collect_gfn2 @@ -73,6 +74,7 @@ program tester new_testsuite("docking", collect_docking), & new_testsuite("eeq", collect_eeq), & new_testsuite("fermi", collect_fermi), & + new_testsuite("geovalues", collect_geovalues), & new_testsuite("gfn0", collect_gfn0), & new_testsuite("gfn1", collect_gfn1), & new_testsuite("gfn2", collect_gfn2), & diff --git a/test/unit/meson.build b/test/unit/meson.build index de6368cb3..c85d4190c 100644 --- a/test/unit/meson.build +++ b/test/unit/meson.build @@ -41,6 +41,7 @@ tests = [ 'detrotra', 'docking', 'eeq', + 'geovalues', 'gfn0', 'gfn1', 'gfn2', diff --git a/test/unit/test_bmatrix.f90 b/test/unit/test_bmatrix.f90 index 582a22078..eb7e99ea7 100644 --- a/test/unit/test_bmatrix.f90 +++ b/test/unit/test_bmatrix.f90 @@ -25,6 +25,7 @@ module test_bmatrix use xtb_mctc_constants, only : pi use xtb_bmatrix, only : bmat_bond, bmat_angle, bmat_linbend, linbend_frame, & & bmat_torsion, bmat_outofplane + use xtb_internals_type, only : angle_value, dihedral_value implicit none private @@ -42,6 +43,7 @@ subroutine collect_bmatrix(testsuite) type(unittest_type), allocatable, intent(out) :: testsuite(:) testsuite = [ & + new_unittest("value-iupac", test_value_iupac), & new_unittest("bond-fd", test_bond_fd), & new_unittest("angle-fd", test_angle_fd), & new_unittest("angle-near-antiparallel", test_angle_near_antiparallel), & @@ -57,6 +59,48 @@ subroutine collect_bmatrix(testsuite) end subroutine collect_bmatrix +!> Pins angle_value (atan2 form, including near-linear precision) and +!> dihedral_value (IUPAC Klyne-Prelog sign and range). +subroutine test_value_iupac(error) + !> Failure report, allocated when the check fails. + type(error_type), allocatable, intent(out) :: error + + real(wp), parameter :: delta = 1.0e-8_wp + real(wp) :: xyz(3, 4) + + xyz(:, 1) = [0.0_wp, 1.0_wp, 0.0_wp] + xyz(:, 2) = [0.0_wp, 0.0_wp, 0.0_wp] + xyz(:, 3) = [1.0_wp, 0.0_wp, 0.0_wp] + xyz(:, 4) = [1.0_wp, 0.5_wp, 0.5_wp * sqrt(3.0_wp)] + call check(error, dihedral_value(xyz, 1, 2, 3, 4), pi / 3.0_wp, thr=thr) + if (allocated(error)) return + + xyz(3, :) = -xyz(3, :) + call check(error, dihedral_value(xyz, 1, 2, 3, 4), -pi / 3.0_wp, thr=thr) + if (allocated(error)) return + + xyz(:, 4) = [1.0_wp, 1.0_wp, 0.0_wp] + call check(error, dihedral_value(xyz, 1, 2, 3, 4), 0.0_wp, thr=thr) + if (allocated(error)) return + + xyz(:, 4) = [1.0_wp, -1.0_wp, 0.0_wp] + call check(error, abs(dihedral_value(xyz, 1, 2, 3, 4)), pi, thr=thr) + if (allocated(error)) return + + call check(error, angle_value(xyz, 1, 2, 3), 0.5_wp * pi, thr=thr) + if (allocated(error)) return + + xyz(:, 1) = [1.0_wp, 0.0_wp, 0.0_wp] + xyz(:, 2) = [0.0_wp, 0.0_wp, 0.0_wp] + xyz(:, 3) = [-1.0_wp, 0.0_wp, 0.0_wp] + call check(error, angle_value(xyz, 1, 2, 3), pi, thr=thr) + if (allocated(error)) return + + xyz(:, 3) = [-1.0_wp, delta, 0.0_wp] + call check(error, angle_value(xyz, 1, 2, 3), pi - atan(delta), thr=thr) +end subroutine test_value_iupac + + !> Pins the analytic bond row of bmat_bond against central finite differences !> of the bond length for every Cartesian displacement. subroutine test_bond_fd(error) @@ -172,27 +216,13 @@ function fd_angle(xyz0, c, k) result(dval) xyz = xyz0 xyz(c, k) = xyz(c, k) + fd_eps - rp = angle_val(xyz) + rp = angle_value(xyz, 1, 2, 3) xyz = xyz0 xyz(c, k) = xyz(c, k) - fd_eps - rm = angle_val(xyz) + rm = angle_value(xyz, 1, 2, 3) dval = (rp - rm) / (2.0_wp*fd_eps) end function fd_angle -!> Valence angle (i-j-k) as scalar function of xyz -function angle_val(xyz) result(val) - !> Reference geometry, three atoms. - real(wp), intent(in) :: xyz(3, 3) - - real(wp) :: val - real(wp) :: v1(3), v2(3), co - v1 = xyz(:, 1) - xyz(:, 2) - v2 = xyz(:, 3) - xyz(:, 2) - co = dot_product(v1, v2) / (norm2(v1)*norm2(v2)) - co = max(-1.0_wp, min(1.0_wp, co)) - val = acos(co) -end function angle_val - !> Pins translational invariance and mutual orthogonality of the two rows a !> linear centre contributes: the linbend_frame directions through bmat_linbend. @@ -362,32 +392,13 @@ function fd_torsion(xyz0, c, k) result(dval) xyz = xyz0 xyz(c, k) = xyz(c, k) + fd_eps - rp = torsion_val(xyz) + rp = dihedral_value(xyz, 1, 2, 3, 4) xyz = xyz0 xyz(c, k) = xyz(c, k) - fd_eps - rm = torsion_val(xyz) + rm = dihedral_value(xyz, 1, 2, 3, 4) dval = (rp - rm) / (2.0_wp*fd_eps) end function fd_torsion -!> Dihedral angle as scalar function of xyz (4 atoms) -function torsion_val(xyz) result(val) - !> Reference geometry, four atoms. - real(wp), intent(in) :: xyz(3, 4) - - real(wp) :: val - real(wp) :: v1(3), v2(3), v3(3) - real(wp) :: n1(3), n2(3), s, c - - v1 = xyz(:, 2) - xyz(:, 1) - v2 = xyz(:, 3) - xyz(:, 2) - v3 = xyz(:, 4) - xyz(:, 3) - n1 = cross(v1, v2) - n2 = cross(v2, v3) - s = -norm2(v2) * dot_product(v1, n2) - c = dot_product(n1, n2) - val = atan2(s, c) -end function torsion_val - !> Pins the torsion sign convention: two geometries that differ only by flipping !> atom 1 across the i-j-k plane must give dihedrals of opposite sign. @@ -409,8 +420,8 @@ subroutine test_torsion_sign(error) & 1.5_wp, 0.8_wp, 0.0_wp], shape(xyz_b)) real(wp) :: tau_a, tau_b - tau_a = torsion_val(xyz_a) - tau_b = torsion_val(xyz_b) + tau_a = dihedral_value(xyz_a, 1, 2, 3, 4) + tau_b = dihedral_value(xyz_b, 1, 2, 3, 4) ! Dihedrals should be opposite in sign call check(error, tau_a, -tau_b, thr=thr, & @@ -438,8 +449,8 @@ subroutine test_torsion_atom_order(error) xyz_rev(:, k) = xyz_fwd(:, 5-k) end do - tau_fwd = torsion_val(xyz_fwd) - tau_rev = torsion_val(xyz_rev) + tau_fwd = dihedral_value(xyz_fwd, 1, 2, 3, 4) + tau_rev = dihedral_value(xyz_rev, 1, 2, 3, 4) ! |tau| should be the same for reversed ordering call check(error, abs(tau_fwd), abs(tau_rev), thr=thr, & diff --git a/test/unit/test_geovalues.f90 b/test/unit/test_geovalues.f90 new file mode 100644 index 000000000..205640059 --- /dev/null +++ b/test/unit/test_geovalues.f90 @@ -0,0 +1,312 @@ +! This file is part of xtb. +! +! Copyright (C) 2026 Leopold M. Seidler +! +! xtb is free software: you can redistribute it and/or modify it under +! the terms of the GNU Lesser General Public License as published by +! the Free Software Foundation, either version 3 of the License, or +! (at your option) any later version. +! +! xtb is distributed in the hope that it will be useful, +! but WITHOUT ANY WARRANTY; without even the implied warranty of +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +! GNU Lesser General Public License for more details. +! +! You should have received a copy of the GNU Lesser General Public License +! along with xtb. If not, see . + +!> Regression tests for internal-coordinate value and constraint-potential callers. +module test_geovalues + use testdrive, only : new_unittest, unittest_type, error_type, check + use xtb_mctc_accuracy, only : wp + use xtb_mctc_constants, only : pi + use xtb_intmodes, only : xyzgeo + use xtb_type_setvar, only : fix_setvar + use xtb_constrainpot, only : constrain_angle, constrain_dihedral + use xtb_scanparam, only : nconstr, fcconstr, atconstr, valconstr, clear_constr + implicit none + private + + public :: collect_geovalues + + real(wp), parameter :: xyz4(3, 4) = reshape([ & + & 0.0_wp, 0.0_wp, 0.0_wp, & + & 1.4_wp, 0.0_wp, 0.0_wp, & + & 2.1_wp, 1.2_wp, 0.3_wp, & + & 2.9_wp, 1.5_wp, -0.8_wp], [3, 4]) + + real(wp), parameter :: ref_constrain_e = 3.6499794648900553e-2_wp + real(wp), parameter :: ref_constrain_g(3, 4) = reshape([ & + & 1.1113262322961571e-17_wp, -2.9120284633054924e-2_wp, 3.2317570544484905e-2_wp, & + & -9.8267126626542817e-3_wp, 3.7751940745654286e-2_wp, -4.3915198782022297e-2_wp, & + & -1.6442336449696515e-2_wp, 1.0185180485609371e-2_wp, -2.3752702264789828e-3_wp, & + & 2.6269049112350784e-2_wp, -1.8816836598208722e-2_wp, 1.3972898464016375e-2_wp], & + & [3, 4]) + + interface + subroutine calc_angles(n, at, xyz, bond, maxbend, nbend, bend, ib) + import :: wp + integer, intent(in) :: n + integer, intent(in) :: at(n) + real(wp), intent(in) :: xyz(3, n) + integer, intent(in) :: bond(n, n) + integer(8), intent(in) :: maxbend + integer, intent(out) :: nbend + real(wp), intent(out) :: bend(maxbend) + integer, intent(out) :: ib(3, maxbend) + end subroutine calc_angles + + subroutine calc_torsions(n, at, xyz, bond, maxtrsn, ntrsn, trsn, it) + import :: wp + integer, intent(in) :: n + integer, intent(in) :: at(n) + real(wp), intent(in) :: xyz(3, n) + integer, intent(in) :: bond(n, n) + integer(8), intent(in) :: maxtrsn + integer, intent(out) :: ntrsn + real(wp), intent(out) :: trsn(maxtrsn) + integer, intent(out) :: it(4, maxtrsn) + end subroutine calc_torsions + + subroutine constrpot(nat, at, xyz, g, e) + import :: wp + integer :: nat, at(nat) + real(wp) :: e + real(wp) :: xyz(3, nat) + real(wp) :: g(3, nat) + end subroutine constrpot + end interface + + +contains + + +!> Collect all exported unit tests +subroutine collect_geovalues(testsuite) + + !> Collection of tests + type(unittest_type), allocatable, intent(out) :: testsuite(:) + + testsuite = [ & + & new_unittest("xyzgeo-values", test_xyzgeo_values), & + & new_unittest("xyzgeo-trans-continuity", test_xyzgeo_trans_continuity), & + & new_unittest("geometry-calcs", test_geometry_calcs), & + & new_unittest("constrain-angle-dihedral", test_constrain_angle_dihedral), & + & new_unittest("constrpot-angle-dihedral", test_constrpot_angle_dihedral) & + & ] + +end subroutine collect_geovalues + + +subroutine test_xyzgeo_values(error) + + !> Error handling + type(error_type), allocatable, intent(out) :: error + + integer, parameter :: na(4) = [0, 1, 2, 3] + integer, parameter :: nb(4) = [0, 0, 1, 2] + integer, parameter :: nc(4) = [0, 0, 0, 1] + real(wp), parameter :: thr = 1.0e-12_wp + real(wp) :: geo(3, 4) + + geo = 0.0_wp + call xyzgeo(xyz4, 4, na, nb, nc, 1.0_wp, geo) + + call check(error, geo(2, 3), 2.0857772899407054_wp, thr=thr) + if (allocated(error)) return + + call check(error, geo(2, 4), 1.8734356567038593_wp, thr=thr) + if (allocated(error)) return + + call check(error, geo(3, 4), -2.1114389127119253_wp, thr=thr) + +end subroutine test_xyzgeo_values + + +subroutine test_xyzgeo_trans_continuity(error) + + !> Error handling + type(error_type), allocatable, intent(out) :: error + + integer, parameter :: na(4) = [0, 1, 2, 3] + integer, parameter :: nb(4) = [0, 0, 1, 2] + integer, parameter :: nc(4) = [0, 0, 0, 1] + real(wp), parameter :: phi_p = 179.9_wp * pi / 180.0_wp + real(wp), parameter :: phi_m = -179.9_wp * pi / 180.0_wp + real(wp) :: xyz_p(3, 4), xyz_m(3, 4), geo_p(3, 4), geo_m(3, 4) + + xyz_p(:, 1) = [0.0_wp, 1.0_wp, 0.0_wp] + xyz_p(:, 2) = [0.0_wp, 0.0_wp, 0.0_wp] + xyz_p(:, 3) = [1.0_wp, 0.0_wp, 0.0_wp] + xyz_p(:, 4) = [1.0_wp, cos(phi_p), sin(phi_p)] + + xyz_m = xyz_p + xyz_m(:, 4) = [1.0_wp, cos(phi_m), sin(phi_m)] + + geo_p = 0.0_wp + geo_m = 0.0_wp + call xyzgeo(xyz_p, 4, na, nb, nc, 180.0_wp / pi, geo_p) + call xyzgeo(xyz_m, 4, na, nb, nc, 180.0_wp / pi, geo_m) + + call check(error, abs(geo_p(3, 4) - geo_m(3, 4)), 0.2_wp, thr=1.0e-10_wp) + +end subroutine test_xyzgeo_trans_continuity + + +subroutine test_geometry_calcs(error) + + !> Error handling + type(error_type), allocatable, intent(out) :: error + + real(wp), parameter :: xyz5(3, 5) = reshape([ & + & 0.0_wp, 0.0_wp, 0.0_wp, & + & 1.4_wp, 0.0_wp, 0.0_wp, & + & 2.1_wp, 1.2_wp, 0.3_wp, & + & 2.9_wp, 1.5_wp, -0.8_wp, & + & 4.1_wp, 0.9_wp, -0.4_wp], [3, 5]) + integer, parameter :: at5(5) = [6, 6, 6, 6, 6] + integer, parameter :: ref_ib(3, 2) = reshape([1, 2, 3, 2, 3, 4], [3, 2]) + integer, parameter :: ref_it(4, 2) = reshape([5, 4, 3, 2, 1, 2, 3, 4], [4, 2]) + real(wp), parameter :: ref_bend(2) = [2.0857772899407059_wp, 1.8734356567038595_wp] + real(wp), parameter :: ref_trsn(2) = [-1.6438986669503857_wp, -2.1114389127119249_wp] + real(wp), parameter :: thr = 1.0e-11_wp + integer(8), parameter :: maxc = 10 + integer :: bond(5, 5), nbend, ntrsn, ntrsn_mir, i + integer :: ib(3, maxc), it(4, maxc), it_mir(4, maxc) + real(wp) :: bend(maxc), trsn(maxc), trsn_mir(maxc), xyz5_mir(3, 5) + + bond = 0 + bond(1, 1) = 1 + bond(2, 2) = 2 + bond(3, 3) = 2 + bond(4, 4) = 2 + bond(5, 5) = 1 + bond(1, 2) = 1 + bond(2, 1) = 1 + bond(2, 3) = 1 + bond(3, 2) = 1 + bond(3, 4) = 1 + bond(4, 3) = 1 + bond(4, 5) = 1 + bond(5, 4) = 1 + + call calc_angles(5, at5, xyz5, bond, maxc, nbend, bend, ib) + call calc_torsions(5, at5, xyz5, bond, maxc, ntrsn, trsn, it) + + call check(error, nbend, 2) + if (allocated(error)) return + + call check(error, ntrsn, 2) + if (allocated(error)) return + + call check(error, all(ib(:, 1:2) == ref_ib)) + if (allocated(error)) return + + call check(error, all(it(:, 1:2) == ref_it)) + if (allocated(error)) return + + do i = 1, 2 + call check(error, bend(i), ref_bend(i), thr=thr) + if (allocated(error)) return + + call check(error, trsn(i), ref_trsn(i), thr=thr) + if (allocated(error)) return + end do + + xyz5_mir = xyz5 + xyz5_mir(3, :) = -xyz5_mir(3, :) + call calc_torsions(5, at5, xyz5_mir, bond, maxc, ntrsn_mir, trsn_mir, it_mir) + + call check(error, ntrsn_mir, ntrsn) + if (allocated(error)) return + + do i = 1, ntrsn + call check(error, trsn_mir(i), -trsn(i), thr=thr) + if (allocated(error)) return + end do + +end subroutine test_geometry_calcs + + +subroutine test_constrain_angle_dihedral(error) + + !> Error handling + type(error_type), allocatable, intent(out) :: error + + integer, parameter :: at4(4) = [6, 6, 6, 6] + type(fix_setvar) :: fix_ang, fix_dih + real(wp) :: e, g(3, 4) + integer :: i, c + + fix_ang%n = 1 + fix_ang%fc = 0.05_wp + fix_ang%atoms = [1, 2, 3] + fix_ang%val = [1.8_wp] + + fix_dih%n = 1 + fix_dih%fc = 0.05_wp + fix_dih%atoms = [1, 2, 3, 4] + fix_dih%val = [-0.9_wp] + + e = 0.0_wp + g = 0.0_wp + call constrain_angle(fix_ang, 4, at4, xyz4, g, e) + call constrain_dihedral(fix_dih, 4, at4, xyz4, g, e) + + call check(error, e, ref_constrain_e, thr=1.0e-10_wp) + if (allocated(error)) return + + do i = 1, 4 + do c = 1, 3 + call check(error, g(c, i), ref_constrain_g(c, i), thr=1.0e-9_wp) + if (allocated(error)) return + end do + end do + +end subroutine test_constrain_angle_dihedral + + +subroutine test_constrpot_angle_dihedral(error) + + !> Error handling + type(error_type), allocatable, intent(out) :: error + + integer, parameter :: at4(4) = [6, 6, 6, 6] + integer :: saved_nconstr, i, c + real(wp) :: saved_fc, e, g(3, 4) + + saved_nconstr = nconstr + saved_fc = fcconstr + call clear_constr + + nconstr = 2 + fcconstr = 0.05_wp + allocate(atconstr(4, 2), source=0) + allocate(valconstr(2), source=0.0_wp) + atconstr(:, 1) = [1, 2, 3, 0] + valconstr(1) = 1.8_wp + atconstr(:, 2) = [1, 2, 3, 4] + valconstr(2) = -0.9_wp + + e = 0.0_wp + g = 0.0_wp + call constrpot(4, at4, xyz4, g, e) + + call clear_constr + nconstr = saved_nconstr + fcconstr = saved_fc + + call check(error, e, ref_constrain_e, thr=1.0e-10_wp) + if (allocated(error)) return + + do i = 1, 4 + do c = 1, 3 + call check(error, g(c, i), ref_constrain_g(c, i), thr=1.0e-9_wp) + if (allocated(error)) return + end do + end do + +end subroutine test_constrpot_angle_dihedral + + +end module test_geovalues