Skip to content

Commit

Permalink
Add unit tests for PBC in GFN-FF
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas3R committed Dec 12, 2023
1 parent 3eb59ca commit 86b1a79
Show file tree
Hide file tree
Showing 2 changed files with 345 additions and 1 deletion.
156 changes: 156 additions & 0 deletions test/unit/molstock.f90
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ subroutine getMolecule(mol, name)
case('feco5'); call feco5(mol)
case('co_cnx6'); call co_cnx6(mol)
case('fe_cnx6'); call fe_cnx6(mol)
case('x06_benzene'); call x06_benzene(mol)
case('mcv15'); call mcv15(mol)
end select

end subroutine getMolecule
Expand Down Expand Up @@ -1129,4 +1131,158 @@ subroutine fe_cnx6(mol)
call init(mol, sym, xyz, chrg=charge, uhf=4)
end subroutine fe_cnx6

subroutine x06_benzene(mol)
type(TMolecule), intent(out) :: mol
integer, parameter :: nat = 48
character(len=*), parameter :: sym(nat) = [character(len=1) ::&
& "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", &
& "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", &
& "C", "C", "C", "C", "H", "H", "H", "H", "H", "H", &
& "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", &
& "H", "H", "H", "H", "H", "H", "H", "H"]
real(wp), parameter :: xyz(3, nat) = reshape([&
& 7.10986726811546E+00_wp, 1.39803317039751E+01_wp, 9.79695027924021E+00_wp, &
& 7.11375821259805E+00_wp, 8.76795203096078E+00_wp, 3.36407395831929E+00_wp, &
& 8.60482372324771E+00_wp, 5.13823133707231E+00_wp, 1.25907033747429E+01_wp, &
& 9.75112376027930E+00_wp, 7.53368379774428E+00_wp, 1.25988946977277E+01_wp, &
& 5.96704128865761E+00_wp, 6.37267695865231E+00_wp, 3.35549866706958E+00_wp, &
& 9.00267279659253E+00_wp, 9.34854414468852E+00_wp, 1.58886068020756E+00_wp, &
& 6.71563121464733E+00_wp, 4.55781661170807E+00_wp, 1.56671851026428E+00_wp, &
& 2.01592612889045E+00_wp, 1.34582777502016E+01_wp, 1.56454269009644E+00_wp, &
& 1.36276161633625E+01_wp, 5.11410651963663E-01_wp, 1.59001258500230E+00_wp, &
& 2.76479397948607E+00_wp, 1.52711868251473E+01_wp, 3.35524268822630E+00_wp, &
& 1.28790262373728E+01_wp, 1.64375153151443E+01_wp, 1.25977427929330E+01_wp, &
& 1.62071733930167E+00_wp, 5.14124693925177E+00_wp, 9.79797419461331E+00_wp, &
& 1.27150507198922E-01_wp, 8.76741986587029E+00_wp, 6.22310165885711E+00_wp, &
& 2.76687841403031E+00_wp, 7.53687678828724E+00_wp, 9.78696710435249E+00_wp, &
& 1.28772197274344E+01_wp, 6.37179001683482E+00_wp, 6.23423673853957E+00_wp, &
& 2.01689886501110E+00_wp, 9.34978586323301E+00_wp, 7.99677906390919E+00_wp, &
& 1.27706356410721E-01_wp, 1.40413533010183E+01_wp, 1.25881435863102E+01_wp, &
& 1.61946667857512E+00_wp, 1.76671714509098E+01_wp, 3.36548184195730E+00_wp, &
& 8.99919873901879E+00_wp, 1.33969013764314E+01_wp, 8.02314488476650E+00_wp, &
& 5.96523477871927E+00_wp, 1.63766711064646E+01_wp, 9.78632715724430E+00_wp, &
& 9.74959517494686E+00_wp, 1.52094556746502E+01_wp, 6.23270086547992E+00_wp, &
& 8.60482372324771E+00_wp, 1.76057950771396E+01_wp, 6.22182176464074E+00_wp, &
& 6.71535329004143E+00_wp, 4.50389054920479E-01_wp, 7.99562715911445E+00_wp, &
& 1.36270603141507E+01_wp, 4.55888094188905E+00_wp, 8.02442477898288E+00_wp, &
& 1.12337125704490E+01_wp, 7.97130089049293E+00_wp, 1.12071377268419E+01_wp, &
& 5.79806312827085E+00_wp, 2.31828852255051E+00_wp, 7.97514885165245E+00_wp, &
& 9.14344160948052E+00_wp, 3.72249480799774E+00_wp, 1.11681009532424E+01_wp, &
& 6.57555721327409E+00_wp, 1.01835111716719E+01_wp, 4.78718833750633E+00_wp, &
& 4.24696590274695E+00_wp, 1.48321506254907E+01_wp, 4.74712764853380E+00_wp, &
& 1.27099091146831E+01_wp, 2.37913273123019E+00_wp, 1.60972295593447E+00_wp, &
& 2.93391110217578E+00_wp, 1.15907330592985E+01_wp, 1.54457634032100E+00_wp, &
& 1.13967153518090E+01_wp, 1.68763741264374E+01_wp, 1.12061138114688E+01_wp, &
& 4.48473040309378E+00_wp, 5.93505986590366E+00_wp, 4.74751161679871E+00_wp, &
& 2.15975211244333E+00_wp, 3.72728429381217E+00_wp, 1.12222404785951E+01_wp, &
& 5.79667350524135E+00_wp, 2.69080408589553E+00_wp, 1.54803205470521E+00_wp, &
& 9.92149154369556E+00_wp, 1.12155566705011E+01_wp, 1.60729115692336E+00_wp, &
& 1.34851798028391E+01_wp, 1.26270358788517E+01_wp, 1.11638773023284E+01_wp, &
& 1.13944919549618E+01_wp, 5.93293120554168E+00_wp, 4.84311971476190E+00_wp, &
& 4.25002307341184E+00_wp, 7.97555821121688E+00_wp, 1.11778281492869E+01_wp, &
& 1.27082415670477E+01_wp, 2.69186841607651E+00_wp, 8.04490308644487E+00_wp, &
& 2.93557864981118E+00_wp, 1.12169757774090E+01_wp, 7.97591678818228E+00_wp, &
& 6.56985975885316E+00_wp, 1.25663690585355E+01_wp, 1.12210885738004E+01_wp, &
& 9.14510915711592E+00_wp, 1.28092137281638E+00_wp, 4.79793944892387E+00_wp, &
& 1.12321839851166E+01_wp, 1.47700646982665E+01_wp, 4.84171183112389E+00_wp, &
& 4.48153427012594E+00_wp, 1.68157073061212E+01_wp, 1.11765482550705E+01_wp, &
& 9.91718371230412E+00_wp, 1.15293566855284E+01_wp, 8.04375118165014E+00_wp, &
& 1.34839291421126E+01_wp, 1.01815598996734E+01_wp, 4.79909135371861E+00_wp, &
& 2.15794560250498E+00_wp, 1.34247513495005E+00_wp, 4.79000410478235E+00_wp], shape(xyz))
real(wp), parameter :: lattice(3,3) = reshape([&
& 13.8962302949642, 0.00000000000000, 0.00000000000000, &
& 0.00000000000000, 17.73883634976286, 0.00000000000000, &
& 0.00000000000000, 0.00000000000000, 12.79894216374709],shape(lattice))
real(wp), parameter :: charge = 0.0_wp
integer, parameter :: uhf = 0
logical, parameter :: pbc(3) = [.true., .true., .true. ]
call init(mol, sym, xyz, charge, uhf, lattice, pbc)
end subroutine x06_benzene


subroutine mcv15(mol)
type(TMolecule), intent(out) :: mol
integer, parameter :: nat = 64
character(len=*), parameter :: sym(nat) = [character(len=2) :: &
& "S", "S", "P", "P", "O", "O", "O", "O", "N", "N", "N", "N", &
& "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", &
& "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "H", "H", &
& "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", &
& "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "Cl", "Cl", &
& "Cl", "Cl", "Cl", "Cl"]
real(wp), parameter :: xyz(3, nat) = reshape([&
& -0.704784105_wp, 1.8541209222_wp, 10.504707540_wp, &
& 6.2799557383_wp, 6.2379452489_wp, 14.645344247_wp, &
& 2.4354591602_wp, 11.092016366_wp, 9.0113485664_wp, &
& 3.1397124725_wp, -2.999950195_wp, 16.138703221_wp, &
& 4.4877885855_wp, 9.5312072789_wp, 10.174901564_wp, &
& 1.0873830473_wp, -1.439141107_wp, 14.975150223_wp, &
& -0.263379632_wp, 10.231208650_wp, 9.2521909253_wp, &
& 5.8385512657_wp, -2.139142479_wp, 15.897860862_wp, &
& 10.389327160_wp, -0.485994960_wp, 13.796934762_wp, &
& -4.814155527_wp, 8.5780611312_wp, 11.353117025_wp, &
& 8.7184295500_wp, 1.2296995930_wp, 10.014851170_wp, &
& -3.143257917_wp, 6.8623665781_wp, 15.135200617_wp, &
& 10.624000637_wp, 0.7016647959_wp, 11.630437889_wp, &
& -5.048829005_wp, 7.3904013751_wp, 13.519613898_wp, &
& -2.200587757_wp, 3.0764030007_wp, 7.8709638189_wp, &
& 7.7757593898_wp, 5.0156631703_wp, 17.279087969_wp, &
& 9.4811992345_wp, 2.5840131592_wp, 7.8605577886_wp, &
& -3.906027601_wp, 5.5080530118_wp, 17.289493999_wp, &
& 6.2207909803_wp, 0.0940870720_wp, 10.316875494_wp, &
& -0.645619347_wp, 7.9979790991_wp, 14.833176293_wp, &
& 7.6830144600_wp, 3.3804459587_wp, 5.9130125325_wp, &
& -2.107842827_wp, 4.7116202124_wp, 19.237039255_wp, &
& 5.5456882676_wp, 4.7937900149_wp, 6.5447240519_wp, &
& 0.0294833652_wp, 3.2982761562_wp, 18.605327736_wp, &
& 3.9093376146_wp, 5.6042597538_wp, 4.6698738502_wp, &
& 1.6658340182_wp, 2.4878064173_wp, 20.480177937_wp, &
& 4.3745843289_wp, 5.0132458319_wp, 2.1545473298_wp, &
& 1.2005873038_wp, 3.0788203392_wp, 22.995504458_wp, &
& 6.5193536846_wp, 3.6412446371_wp, 1.5131056695_wp, &
& -0.944182051_wp, 4.4508215339_wp, 23.636946118_wp, &
& 8.1726580439_wp, 2.8302489082_wp, 3.3779894677_wp, &
& -2.597486411_wp, 5.2618172628_wp, 21.772062320_wp, &
& -3.017508989_wp, 11.070724086_wp, 4.4375332419_wp, &
& 8.5926806220_wp, -2.978657915_wp, 20.712518546_wp, &
& -1.163194341_wp, 4.1330712708_wp, 6.4779746057_wp, &
& 6.7383659747_wp, 3.9589949002_wp, 18.672077182_wp, &
& 4.8672586925_wp, 1.2052465936_wp, 9.2468732542_wp, &
& 0.7079129402_wp, 6.8868195775_wp, 15.903178533_wp, &
& 5.6559681045_wp, 0.1201590543_wp, 12.298638968_wp, &
& -0.080796471_wp, 7.9719071168_wp, 12.851412819_wp, &
& 5.2054463494_wp, 5.3606192448_wp, 8.4822270795_wp, &
& 0.3697252834_wp, 2.7314469262_wp, 16.667824708_wp, &
& 2.2798534357_wp, 6.7368342734_wp, 5.1716119842_wp, &
& 3.2953181971_wp, 1.3552318977_wp, 19.978439803_wp, &
& 3.0839809259_wp, 5.6380927002_wp, 0.6944985262_wp, &
& 2.4911907069_wp, 2.4539734708_wp, 24.455553261_wp, &
& 1.9478108468_wp, -2.981417863_wp, 24.839373077_wp, &
& 3.6273607860_wp, 11.073484034_wp, 0.3106787105_wp, &
& 9.8399363161_wp, 1.7503364609_wp, 2.8851221671_wp, &
& -4.264764683_wp, 6.3417297101_wp, 22.264929621_wp, &
& 2.9626243744_wp, 11.460780681_wp, 6.4062306843_wp, &
& 2.6125472584_wp, -3.368714510_wp, 18.743821103_wp, &
& 8.6911615512_wp, -1.230382938_wp, 14.368502229_wp, &
& -3.115989918_wp, 9.3224491097_wp, 10.781549558_wp, &
& -2.127273673_wp, -1.000369135_wp, 14.678356340_wp, &
& 7.7024453060_wp, 9.0924353068_wp, 10.471695447_wp, &
& -2.177136732_wp, 10.793914095_wp, 6.2993188375_wp, &
& 7.7523083650_wp, -2.701847924_wp, 18.850732950_wp, &
& 1.7261160374_wp, -0.286764238_wp, 3.3630876937_wp, &
& 3.8490555954_wp, 8.3788304098_wp, 21.786964094_wp, &
& -1.998776063_wp, 8.5349880314_wp, 2.4962465787_wp, &
& 7.5739476960_wp, -0.442921860_wp, 22.653805209_wp, &
& 7.8365465637_wp, 11.032255979_wp, 4.7537064890_wp, &
& -2.261374930_wp, -2.940189808_wp, 20.396345299_wp] , shape(xyz))
real(wp), parameter :: lattice(3,3) = reshape([&
& 14.17654659798719_wp, -0.00424862639192_wp, -0.00394399802297_wp, &
& -3.64444068770540_wp, 14.26901592236016_wp, -0.13224999070830_wp, &
& -4.95693427750094_wp, -6.17270112491500_wp, 25.28624577677911_wp], shape(lattice))
real(wp), parameter :: charge = 0.0_wp
integer, parameter :: uhf = 0
logical, parameter :: pbc(3) = [.true., .true., .true. ]
call init(mol, sym, xyz, charge, uhf, lattice, pbc)
end subroutine mcv15

end module xtb_test_molstock
190 changes: 189 additions & 1 deletion test/unit/test_gfnff.f90
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ subroutine collect_gfnff(testsuite)
new_unittest("mindless-solvation", test_gfnff_mindless_solvation), &
new_unittest("scaleup", test_gfnff_scaleup), &
new_unittest("pdb", test_gfnff_pdb), &
new_unittest("sdf", test_gfnff_sdf) &
new_unittest("sdf", test_gfnff_sdf), &
new_unittest("pbc", test_gfnff_pbc) &
]

end subroutine collect_gfnff
Expand Down Expand Up @@ -666,4 +667,191 @@ subroutine test_gfnff_sdf(error)

end subroutine test_gfnff_sdf


subroutine test_gfnff_pbc(error)
use xtb_mctc_accuracy, only : wp
use xtb_test_molstock, only : getMolecule

use xtb_type_molecule
use xtb_type_param
use xtb_type_pcem
use xtb_type_data, only : scc_results
use xtb_type_environment, only : TEnvironment, init
use xtb_type_restart, only : TRestart
use xtb_mctc_symbols, only : symbolLength

use xtb_gfnff_calculator, only : TGFFCalculator, newGFFCalculator

type(error_type), allocatable, intent(out) :: error

real(wp), parameter :: thr = 1.0e-8_wp
real(wp), parameter :: thr2 = 3.0e-3_wp

type(TEnvironment) :: env
type(TMolecule) :: mol
type(TRestart) :: chk
type(TGFFCalculator) :: calc
type(scc_results) :: res

integer :: iMol, i
logical :: exitRun
real(wp) :: energy, energy2, hl_gap, sigma(3, 3)
real(wp), allocatable :: gradient(:, :)
real(wp), allocatable :: xyztmp(:,:), lattmp(:,:)
integer, allocatable :: attmp(:)
character(len=symbolLength), allocatable :: symtmp(:)
logical, parameter :: pbc(3) = [.true., .true., .true. ]
! structure names from X23 and mcVOL22 benchmark
character(len=*), parameter :: pbc_strucs(2) = [&
& "x06_benzene", "mcv15"]
!> references for original GFN-FF
real(wp), parameter :: ref_energies(2) = &
& [-9.522300429916_wp, -11.059826732607_wp ]
real(wp), parameter :: ref_gnorms(2) = &
& [ 0.083513313043_wp, 0.083870455567_wp ]
real(wp), parameter :: ref_snorm(2) = &
& [ 1.410826672_wp, 0.419545156_wp ]
!> references for mcGFN-FF
real(wp), parameter :: mcref_energies(2) = &
& [-9.419561502589_wp, -10.850342398282_wp ]
real(wp), parameter :: mcref_gnorms(2) = &
& [ 0.071130586474_wp, 0.104285414859_wp ]
real(wp), parameter :: mcref_snorm(2) = &
& [ 0.839554942_wp, 0.729174866_wp ]

call init(env)
do iMol = 1, 2

! Calucaltions with original parameterization !
! load molecule from molstock
call getMolecule(mol, pbc_strucs(iMol))
! reset energy, gradient, sigma
energy=0.0_wp
if (allocated(gradient)) deallocate(gradient)
allocate(gradient(3, mol%n))
sigma = 0.0_wp
! setup new calculator
call delete_file('charges')
! original angewChem2020_2 version is default
call newGFFCalculator(env, mol, calc, '.param_gfnff.xtb', .false.)
call env%check(exitRun)
call check_(error, .not.exitRun)
if (exitRun) exit
! run single point calculation
call calc%singlepoint(env, mol, chk, 2, .false., energy, gradient, sigma, &
& hl_gap, res)
call env%check(exitRun)
call check_(error, .not.exitRun)
if (exitRun) exit
! check energy, gradient and sigma versus reference calculation
call check_(error, energy, ref_energies(iMol), thr=thr)
call check_(error, norm2(gradient), ref_gnorms(iMol), thr=thr)
call check_(error, norm2(sigma), ref_snorm(iMol), thr=thr)

! Calucaltions with mcGFN-FF parameterization !
! reset energy, gradient, sigma
energy=0.0_wp
if (allocated(gradient)) deallocate(gradient)
allocate(gradient(3, mol%n))
sigma = 0.0_wp
! setup new calculator
call delete_file('charges')
! mcGFN-FF is version=4
call newGFFCalculator(env, mol, calc, '.param_gfnff.xtb', .false., 4)
call env%check(exitRun)
call check_(error, .not.exitRun)
if (exitRun) exit
! run single point calculation
call calc%singlepoint(env, mol, chk, 2, .false., energy, gradient, sigma, &
& hl_gap, res)
call env%check(exitRun)
call check_(error, .not.exitRun)
if (exitRun) exit
! check energy, gradient and sigma versus reference calculation
call check_(error, energy, mcref_energies(iMol), thr=thr)
call check_(error, norm2(gradient), mcref_gnorms(iMol), thr=thr)
call check_(error, norm2(sigma), mcref_snorm(iMol), thr=thr)
end do

! check super cell scaling !
! load molecule from molstock
call getMolecule(mol, "mcv15")
! reset energy, gradient, sigma
energy=0.0_wp
if (allocated(gradient)) deallocate(gradient)
allocate(gradient(3, mol%n))
sigma = 0.0_wp
! setup new calculator
call delete_file('charges')
! mcGFN-FF is version=4
call newGFFCalculator(env, mol, calc, '.param_gfnff.xtb', .false., 4)
call env%check(exitRun)
call check_(error, .not.exitRun)
! run single point calculation
call calc%singlepoint(env, mol, chk, 2, .false., energy, gradient, sigma, &
& hl_gap, res)
call env%check(exitRun)
call check_(error, .not.exitRun)
! build 2x1x1 supercell
! coordinates
if (allocated(xyztmp)) deallocate(xyztmp)
allocate(xyztmp(3,mol%n))
xyztmp = mol%xyz
deallocate(mol%xyz)
allocate(mol%xyz(3,2*mol%n), source=0.0_wp)
mol%xyz(:,1:mol%n)=xyztmp
do i=1, mol%n
mol%xyz(:,i+mol%n) = xyztmp(:,i)+mol%lattice(:,1)
enddo
! atom types
allocate(attmp(mol%n), source=0)
attmp=mol%at
deallocate(mol%at)
allocate(mol%at(2*mol%n))
mol%at(1:mol%n)=attmp
mol%at(mol%n+1:2*mol%n)=attmp
! symbols
allocate(symtmp(2*mol%n))
symtmp = mol%sym
deallocate(mol%sym)
allocate(mol%sym(2*mol%n))
mol%sym(1:mol%n)= symtmp
mol%sym(mol%n+1:2*mol%n) = symtmp
! lattice
mol%lattice(:,1) = 2.0_wp*mol%lattice(:,1)
! number of atoms
mol%n = 2*mol%n
! init mol
deallocate(symtmp, xyztmp)
allocate(symtmp(mol%n))
allocate(xyztmp(3,mol%n), source=0.0_wp)
allocate(lattmp(3,3), source=0.0_wp)
symtmp=mol%sym
xyztmp=mol%xyz
lattmp=mol%lattice
call init(mol, symtmp, xyztmp, 0.0_wp, 0, lattmp, pbc)

energy2=0.0_wp ! energy of supercell
if (allocated(gradient)) deallocate(gradient)
allocate(gradient(3, mol%n))
sigma = 0.0_wp
! setup new calculator
call delete_file('charges')
! mcGFN-FF is version=4
call newGFFCalculator(env, mol, calc, '.param_gfnff.xtb', .false., 4)
call env%check(exitRun)
call check_(error, .not.exitRun)
! run single point calculation
call calc%singlepoint(env, mol, chk, 2, .false., energy2, gradient, sigma, &
& hl_gap, res)
call env%check(exitRun)
call check_(error, .not.exitRun)

! scale down energy of supercell and compare
energy2 = energy2/2.0_wp
call check_(error, energy2, energy, thr=thr2)

end subroutine test_gfnff_pbc


end module test_gfnff

0 comments on commit 86b1a79

Please sign in to comment.