Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion man/xtb.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,15 @@ INPUT
~~~~~

*.CHRG*::
molecular charge as 'int'
molecular charge as 'int'. For GFN-FF, an optional second line specifies
charges for each detected EEQ fragment, in fragment order. Explicit fragment
charges (or atomic reference charges in `charges`) override the default.
Without these inputs, the total charge is assigned to the largest fragment
by atom count. Equal sizes are ranked by elemental composition, comparing
counts from the highest atomic number downward. An unresolved tie requires
explicit fragment charges. Neutral systems retain zero charge on all fragments.
This deterministic fallback is independent of atom order, but does not predict
the chemically correct charge localization.

*.UHF*::
Number of unpaired electrons as 'int'
Expand Down
15 changes: 14 additions & 1 deletion src/gfnff/gfnff_ini.f90
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ subroutine gfnff_ini(env,pr,makeneighbor,mol,gen,param,topo,neigh,efield,accurac
use xtb_gfnff_generator, only : TGFFGenerator
use xtb_gfnff_ini2
use xtb_gfnff_eg, only : gfnff_dlogcoord, getCoordinationNumber
use xtb_gfnff_mrec, only : mrecgff, mrecgffPBC
use xtb_gfnff_mrec, only : mrecgff, mrecgffPBC, canonical_charge_fragment
use xtb_gfnff_fraghess
use xtb_restart
use xtb_mctc_constants
Expand Down Expand Up @@ -555,6 +555,7 @@ integer function itabrow6(i)
call env%warning("Rejecting external charges input due to missmatch", source)
else
topo%qfrag=dnint(qtmp)
topo%default_fragment_charges = .false.
if (setup_print) write(env%unit,'(10x,"fragment charges from <charges> :",10F7.3)') topo%qfrag(1:topo%nfrag)
end if
else
Expand All @@ -566,6 +567,18 @@ integer function itabrow6(i)
return
end if
endif
if (topo%default_fragment_charges) then
if (abs(mol%chrg) > sqrt(epsilon(1.0_wp)).and.topo%nfrag > 1) then
ifrag=canonical_charge_fragment(mol%n,mol%at,topo%nfrag,topo%fraglist)
if (ifrag == 0) then
call env%error('Ambiguous default fragment charge: provide explicit fragment charges in .CHRG', source)
return
endif
topo%qfrag(1:topo%nfrag)=0.0_wp
topo%qfrag(ifrag)=mol%chrg
endif
topo%default_fragment_charges=.false.
endif
if(mol%n.lt.100.and.topo%nfrag.gt.2.and.mol%chrg.ne.0.and.sum(topo%qfrag(2:topo%nfrag)).gt.999) then
itmp=0
do i=1,mol%n
Expand Down
14 changes: 12 additions & 2 deletions src/gfnff/gfnff_setup.f90
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ subroutine gfnff_setup(env,verbose,restart,mol,gen,param,topo,neigh,accuracy,efi
logical :: do_print
logical :: do_write

topo%default_fragment_charges = .false.
do_print = .true.
if (present(print_setup)) do_print = print_setup
do_write = .true.
Expand Down Expand Up @@ -171,6 +172,7 @@ subroutine gfnff_input(env, mol, topo, neigh, print_setup)
topo%qfrag(1) = mol%chrg
topo%qfrag(2:topo%nfrag) = 0.0_wp
topo%nfrag = 0
topo%default_fragment_charges = .true.
end if
!--------------------------------------------------------------------
! SDF case
Expand Down Expand Up @@ -221,6 +223,7 @@ subroutine gfnff_input(env, mol, topo, neigh, print_setup)
! initialize qfrag as in the default case
topo%qfrag(1)=mol%chrg
topo%qfrag(2:mol%n)=0
topo%default_fragment_charges = .true.
endif
!--------------------------------------------------------------------
! General case: input = xyz or coord
Expand All @@ -230,7 +233,11 @@ subroutine gfnff_input(env, mol, topo, neigh, print_setup)
if (ich.ne.-1) then
read(ich,'(a)') atmp_0 ! first line contains total charge
read(ich,'(a)',iostat=err)atmp ! second line contains fragment charges
if (err .ne. 0) atmp=atmp_0
if (err .ne. 0) then
atmp=atmp_0
! A single line supplies only the total charge, not fragment charges.
topo%default_fragment_charges = .true.
end if
call close_file(ich)
call readline(atmp,floats,s,ns,nf)
topo%qfrag(1:nf)=floats(1:nf)
Expand All @@ -239,11 +246,14 @@ subroutine gfnff_input(env, mol, topo, neigh, print_setup)
& nint(sum(topo%qfrag(1:nf)))
else
! ignore fragment charges if they are not consistent with the total charge
topo%qfrag(1:nf) = 0.0_wp
topo%qfrag = 0.0_wp
topo%qfrag(1) = mol%chrg
topo%default_fragment_charges = .true.
end if
else
topo%qfrag(1)=mol%chrg
topo%qfrag(2:mol%n)=0
topo%default_fragment_charges = .true.
end if
end select

Expand Down
43 changes: 42 additions & 1 deletion src/gfnff/mrec.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,50 @@
! along with xtb. If not, see <https://www.gnu.org/licenses/>.
module xtb_gfnff_mrec
private
public :: mrecgff, mrecgffPBC
public :: mrecgff, mrecgffPBC, canonical_charge_fragment
contains

!> Select a unique largest fragment without using its label or atom indices.
!> Equal sizes are ranked lexicographically by element counts, high Z first.
!> Return zero for an unresolved tie; charge localization then needs input.
integer function canonical_charge_fragment(nat,at,nfrag,fraglist) result(selected)
integer,intent(in) :: nat,nfrag,at(nat),fraglist(nat)
integer,allocatable :: counts(:,:)
integer :: i,f,z,comparison,best,maxz
logical :: tied

selected=0
if(nat<=0.or.nfrag<=0) return
if(any(fraglist<1).or.any(fraglist>nfrag)) return
maxz=maxval(at)
allocate(counts(0:maxz,nfrag),source=0)
do i=1,nat
f=fraglist(i)
counts(0,f)=counts(0,f)+1
counts(at(i),f)=counts(at(i),f)+1
enddo
best=1
tied=.false.
do f=2,nfrag
comparison=counts(0,f)-counts(0,best)
if(comparison==0) then
do z=maxz,1,-1
comparison=counts(z,f)-counts(z,best)
if(comparison/=0) exit
enddo
endif
if(comparison>0) then
best=f
tied=.false.
else if(comparison==0) then
tied=.true.
endif
enddo
if(.not.tied) selected=best
end function canonical_charge_fragment



subroutine mrecgff(nat,nb,molcount,molvec)
! molcount: number of total fragments (increased during search)
! nat: overall number of atoms
Expand Down
2 changes: 2 additions & 0 deletions src/gfnff/topology.f90
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ module xtb_gfnff_topology
integer :: natxbAB
integer :: nbatm
integer :: nfrag
!> No explicit fragment charges: choose an order-independent default.
logical :: default_fragment_charges = .false.
integer :: maxsystem ! max. number of fragmentsfor hessian
integer :: bond_hb_nr ! number of unique AH...B HB/bond terms
integer :: b_max ! number of B atoms per unique AH bond
Expand Down
117 changes: 117 additions & 0 deletions test/unit/test_gfnff.f90
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,130 @@ subroutine collect_gfnff(testsuite)
new_unittest("pdb", test_gfnff_pdb), &
new_unittest("sdf", test_gfnff_sdf), &
new_unittest("pbc", test_gfnff_pbc), &
new_unittest("canonical-fragment-selection", test_canonical_selection), &
new_unittest("default-fragment-charges", test_default_fragment_charges), &
new_unittest("fragmentation", test_gfnff_fragmentation), &
new_unittest("Ln_An", test_gfnff_LnAn_H) &
]

end subroutine collect_gfnff


subroutine test_canonical_selection(error)
use xtb_gfnff_mrec,only:canonical_charge_fragment
type(error_type),allocatable,intent(out) :: error
integer :: selected

! Largest fragment wins even if a smaller fragment contains heavier atoms.
selected=canonical_charge_fragment(5,[6,1,1,8,1],2,[1,1,1,2,2])
call check_(error,selected,1)
if(allocated(error)) return
! Equal sizes: high-Z composition breaks the tie, independent of labels.
selected=canonical_charge_fragment(4,[6,1,8,1],2,[1,1,2,2])
call check_(error,selected,2)
if(allocated(error)) return
selected=canonical_charge_fragment(4,[1,8,1,6],2,[1,1,2,2])
call check_(error,selected,1)
if(allocated(error)) return
! Equal compositions cannot be ranked by input order.
selected=canonical_charge_fragment(4,[8,1,1,8],2,[1,1,2,2])
call check_(error,selected,0)
end subroutine test_canonical_selection

subroutine test_default_fragment_charges(error)
use xtb_mctc_accuracy,only:wp
use xtb_type_environment,only:TEnvironment,init
use xtb_type_molecule,only:TMolecule,init
use xtb_gfnff_calculator,only:TGFFCalculator,newGFFCalculator
type(error_type),allocatable,intent(out) :: error
integer,parameter :: nat=8,at(nat)=[6,1,1,1,1,8,1,1]
real(wp),parameter :: xyz(3,nat)=reshape([ &
0.0_wp,0.0_wp,0.0_wp, &
1.186_wp,1.186_wp,1.186_wp, &
1.186_wp,-1.186_wp,-1.186_wp, &
-1.186_wp,1.186_wp,-1.186_wp, &
-1.186_wp,-1.186_wp,1.186_wp, &
12.0_wp,0.0_wp,0.0_wp, &
13.43_wp,1.11_wp,0.0_wp, &
13.43_wp,-1.11_wp,0.0_wp], [3,nat])
integer :: order(nat),i,k,carbon,oxygen,unit
real(wp) :: refq(nat)

do k=1,8
block
type(TEnvironment) :: env
type(TMolecule) :: mol
type(TGFFCalculator) :: calc
logical :: failed
order=[(i,i=1,nat)]
if(mod(k,2)==0) order=order(nat:1:-1)
carbon=findloc(order,1,dim=1)
oxygen=findloc(order,6,dim=1)
if(k>=3.and.k<=6) then
open(newunit=unit,file='.CHRG',status='replace')
write(unit,*) 1
if(k>=5) then
if(mod(k,2)==1) then
write(unit,*) 0,1
else
write(unit,*) 1,0
endif
endif
close(unit)
endif
if(k>=7) then
refq=0.0_wp
refq(6)=1.0_wp
open(newunit=unit,file='charges',status='replace')
do i=1,nat
write(unit,*) refq(order(i))
enddo
close(unit)
endif
call init(env)
call init(mol,at(order),xyz(:,order),chrg=1.0_wp)
call newGFFCalculator(env,mol,calc,'.param_gfnff.xtb',.false., &
printlevel=0,write_topology=.false.)
if(k>=3.and.k<=6) call delete_file('.CHRG')
if(k>=7) call delete_file('charges')
call env%check(failed)
call check_(error,.not.failed)
if(allocated(error)) return
call check_(error,calc%topo%nfrag,2)
if(allocated(error)) return
call check_(error,calc%topo%qfrag(calc%topo%fraglist(carbon)), &
real(merge(1,0,k<=4),wp),thr=1.e-12_wp)
if(allocated(error)) return
call check_(error,calc%topo%qfrag(calc%topo%fraglist(oxygen)), &
real(merge(0,1,k<=4),wp),thr=1.e-12_wp)
if(allocated(error)) return
end block
enddo
! Identical fragments: neutral succeeds; nonzero charge requires input.
do k=0,1
block
type(TEnvironment) :: env
type(TMolecule) :: mol
type(TGFFCalculator) :: calc
integer,parameter :: wat(6)=[8,1,1,8,1,1]
real(wp) :: water(3,6)
logical :: failed
water(:,1:3)=xyz(:,6:8)
water(:,4:6)=xyz(:,6:8)
water(1,4:6)=water(1,4:6)+12.0_wp
call init(env)
call init(mol,wat,water,chrg=real(k,wp))
call newGFFCalculator(env,mol,calc,'.param_gfnff.xtb',.false., &
printlevel=0,write_topology=.false.)
call env%check(failed)
call check_(error,failed.eqv.(k==1))
if(allocated(error)) return
if(k==0) call check_(error,sum(abs(calc%topo%qfrag)),0.0_wp,thr=1.e-12_wp)
if(allocated(error)) return
end block
enddo
end subroutine test_default_fragment_charges

subroutine test_gfnff_sp(error)
use xtb_mctc_accuracy, only : wp
use xtb_mctc_systools
Expand Down