计算化学公社

标题: 在ASE中使用xtb-6.3.0(pre.2)做优化或MD的py脚本 [打印本页]

作者
Author:
tjuptz    时间: 2021-11-10 23:04
标题: 在ASE中使用xtb-6.3.0(pre.2)做优化或MD的py脚本
本帖最后由 tjuptz 于 2021-11-10 23:09 编辑

这几个脚本还是去年学习ASE及xtb时,仿照冰释之川老师帖子 在ASE中使用xtb-6.3.0(pre.2)的py脚本 练习写的。由于只简单测试了可以跑,没有实际派上用场一直搁置。与其遗忘在角落,还不如分享出来抛砖引玉。

优化的脚本就是把上贴中的优化算法换换,主体内容就不贴了。MD的分别写了NVT、NPT对应不同控温控压算法的,这里贴出一个Berendsen热浴压浴npt_B.py的内容示例。当时现学现卖写的进度条和计算速度统计。
  1. import xtb
  2. from xtb import GFN0

  3. import ase
  4. from ase.io import read, iread, write
  5. from ase.units import fs, kB
  6. from ase.md.nptberendsen import NPTBerendsen
  7. from ase.md.velocitydistribution import MaxwellBoltzmannDistribution, Stationary, ZeroRotation
  8. from ase.io.trajectory import Trajectory
  9. from ase.md import MDLogger

  10. import os,sys,datetime

  11. start_time = datetime.datetime.now()

  12. # read molecular structure data, here from energy minimization
  13. basename, suffix = sys.argv[1].split(".")
  14. name = basename+'_npt'
  15. mol = read(sys.argv[1])
  16. total_atoms = mol.get_global_number_of_atoms()

  17. print('***  ASE program with xTB started at {0}  ***\n'.format(start_time.strftime("%Y-%m-%d %H:%M:%S")))
  18. print('-> Task name:  %50s' %name)
  19. print('-> Orthorhombic cell parameters:  %31s' %mol.get_cell())
  20. print('-> Total number of atoms in a cell:  %27s' %total_atoms)
  21. print('-> PBC dimensions:  %45s' %mol.get_pbc())

  22. # set calculation level, charge, and magnetic moment
  23. total_charges = 0
  24. total_unpaired_electrons = 0
  25. mol.set_initial_charges(charges=[total_charges/total_atoms,]*total_atoms)
  26. mol.set_initial_magnetic_moments(magmoms=[total_unpaired_electrons/total_atoms,]*total_atoms)
  27. mol.calc = GFN0(accuracy=1, electronic_temperature=300, max_iterations=250)

  28. # setup the npt md
  29. nstep = 100
  30. dstep = 10
  31. istep = -10   # -dstep
  32. dt    = 1     # fs
  33. temp  = 400   # kB

  34. dyn = NPTBerendsen(mol, timestep=dt*fs, temperature=temp, taut=0.1*1000*fs,
  35.            pressure=1.01325, taup=1.0*1000*fs, compressibility=4.57e-5, logfile=name+'.log')

  36. # Set the momenta corresponding to reference temperature
  37. MaxwellBoltzmannDistribution(mol, temp*kB, force_temp=True)
  38. Stationary(mol)  # zero linear momentum
  39. #ZeroRotation(mol)  # zero angular momentum

  40. # save the positions of all atoms after every dstep
  41. traj = Trajectory(name+'.traj', 'w', mol)
  42. dyn.attach(traj.write, interval=dstep)

  43. # save the logfile after every dstep
  44. dyn.attach(MDLogger(dyn, mol, name+'.log', header=True, stress=True,
  45.            peratom=True, mode="a"), interval=dstep)

  46. # performance counter
  47. def perf_counter(dstep=dstep, nstep=nstep, start_time=start_time):
  48.     """Function to counter the performance."""
  49.     global istep
  50.     if istep < 0 :
  51.         istep += dstep
  52.     else :
  53.         istep += dstep
  54.         d_time = (datetime.datetime.now() - start_time).seconds
  55.         scale = 50
  56.         i = int(scale * istep/nstep)
  57.         prdt_end_time = datetime.datetime.now() + datetime.timedelta(seconds=d_time*nstep/istep)
  58.         a = '*' * i
  59.         b = '.' * (scale - i)
  60.         c = (istep/nstep)*100
  61.         print("\r{:^3.0f}%[{}->{}]{:.2f}s\n***  This task will ended at {}  ***".format(c,a,b,d_time,prdt_end_time.strftime("%Y-%m-%d %H:%M:%S")), end="")

  62. dyn.attach(perf_counter, interval=dstep)

  63. # Now run the dynamics
  64. print('\n'+' NPT MD started... '.center(60,'~'))
  65. dyn.run(nstep)
  66. print('\n'+' NPT MD terminated... '.center(60,'~'))

  67. # write trajectory and final geometry to file
  68. write(name+"_ed.xyz", read(name+".traj"), columns=['symbols','positions'],write_results=False)
  69. write(name+"_traj.xyz", iread(name+".traj"), columns=['symbols','positions'],write_results=False)
  70. #os.remove('xtbrestart')
  71. #os.remove('wbo')
  72. #os.remove('gfn0.out')

  73. end_time = datetime.datetime.now()
  74. print('***  ASE program with xTB ended at {0}  ***\n'.format(end_time.strftime("%Y-%m-%d %H:%M:%S")))

  75. tot_seconds = (end_time - start_time).seconds
  76. days = tot_seconds // 86400
  77. hours = (tot_seconds % 86400) // 3600
  78. minutes = (tot_seconds % 86400 % 3600)// 60
  79. seconds = tot_seconds % 60
  80. print(">> Elapsed time: {0:2d} days {1:2d} hours {2:2d} minutes {3:4.1f} seconds <<".format(days,hours,minutes,seconds))
  81. ps_per_day = dt * nstep * 86400 / tot_seconds / 1e3
  82. hour_per_ps = 24 / ps_per_day
  83. print(">> Performance: {0:.2f} ps/day  {1:.2f} hour/ps <<".format(ps_per_day,hour_per_ps))
复制代码


注:对于量子化学包括半经验的方法,我不太清楚ASE处理PBC的机制!像第一性原理程序,方法和基组本来就是处理周期性的,稍能理解。所以,不太清楚把这些程序当作力的计算器时候咋处理跨越边界的分子键。待大佬解惑。

另外,知乎有两篇文章是在ASE中使用xtb做优化和过渡态计算的,列出以供学习。
https://zhuanlan.zhihu.com/p/294508681
https://zhuanlan.zhihu.com/p/295273428

以下是脚本附件 (, 下载次数 Times of downloads: 17) (, 下载次数 Times of downloads: 23)


(, 下载次数 Times of downloads: 19)

(, 下载次数 Times of downloads: 19) (, 下载次数 Times of downloads: 17)

(, 下载次数 Times of downloads: 16) (, 下载次数 Times of downloads: 16)



作者
Author:
zzp    时间: 2021-11-11 11:25
请问下这个是支持周期性的动力学的吗
作者
Author:
tjuptz    时间: 2021-11-11 21:09
zzp 发表于 2021-11-11 11:25
请问下这个是支持周期性的动力学的吗

理论上是的,但是我在正文里也说了存在的问题
作者
Author:
zzp    时间: 2021-11-12 10:18
tjuptz 发表于 2021-11-11 21:09
理论上是的,但是我在正文里也说了存在的问题

好的,谢谢楼主的脚本
作者
Author:
zzp    时间: 2022-5-16 00:01
请问这个是不是只能支持gfn0呀 我改成gfn1后就报错了ase.calculators.calculator.PropertyNotImplementedError: stress property not implemented

作者
Author:
tjuptz    时间: 2022-5-16 08:36
本帖最后由 tjuptz 于 2022-5-16 08:38 编辑

xtb里的gfn1没有周期性吧,不过我记得用ase里的周期性不影响吧
作者
Author:
zzp    时间: 2022-5-16 10:00
tjuptz 发表于 2022-5-16 08:36
xtb里的gfn1没有周期性吧,不过我记得用ase里的周期性不影响吧

我查了下会不会是gfn1不支持stress呀 只有gfn0支持
作者
Author:
dongsong    时间: 2023-5-13 16:18
这个脚本可以用来跑gfnff吗
作者
Author:
tjuptz    时间: 2023-5-14 15:29
dongsong 发表于 2023-5-13 16:18
这个脚本可以用来跑gfnff吗

好久没关注了,写当时是不行的
作者
Author:
dongsong    时间: 2023-5-14 20:08
tjuptz 发表于 2023-5-14 15:29
好久没关注了,写当时是不行的

研究了一下,现在也不行




欢迎光临 计算化学公社 (http://bbs.keinsci.com/) Powered by Discuz! X3.3