计算化学公社

 找回密码 Forget password
 注册 Register
Views: 5061|回复 Reply: 9
打印 Print 上一主题 Last thread 下一主题 Next thread

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

[复制链接 Copy URL]

516

帖子

1

威望

4765

eV
积分
5301

Level 6 (一方通行)

跳转到指定楼层 Go to specific reply
楼主
本帖最后由 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

以下是脚本附件 nvt_NH.py (3.62 KB, 下载次数 Times of downloads: 17) nvt_B.py (3.62 KB, 下载次数 Times of downloads: 23)


npt_NH_PR.py (3.69 KB, 下载次数 Times of downloads: 19)

npt_B.py (3.71 KB, 下载次数 Times of downloads: 19) opt_Precon_ecf.py (2.76 KB, 下载次数 Times of downloads: 17)

opt_Precon.py (2.59 KB, 下载次数 Times of downloads: 16) opt_BFGSs.py (2.49 KB, 下载次数 Times of downloads: 16)


评分 Rate

参与人数
Participants 1
eV +5 收起 理由
Reason
ggdh + 5 赞!

查看全部评分 View all ratings

19

帖子

0

威望

859

eV
积分
878

Level 4 (黑子)

2#
发表于 Post on 2021-11-11 11:25:36 | 只看该作者 Only view this author
请问下这个是支持周期性的动力学的吗

516

帖子

1

威望

4765

eV
积分
5301

Level 6 (一方通行)

3#
 楼主 Author| 发表于 Post on 2021-11-11 21:09:16 | 只看该作者 Only view this author
zzp 发表于 2021-11-11 11:25
请问下这个是支持周期性的动力学的吗

理论上是的,但是我在正文里也说了存在的问题

19

帖子

0

威望

859

eV
积分
878

Level 4 (黑子)

4#
发表于 Post on 2021-11-12 10:18:55 | 只看该作者 Only view this author
tjuptz 发表于 2021-11-11 21:09
理论上是的,但是我在正文里也说了存在的问题

好的,谢谢楼主的脚本

19

帖子

0

威望

859

eV
积分
878

Level 4 (黑子)

5#
发表于 Post on 2022-5-16 00:01:50 | 只看该作者 Only view this author
请问这个是不是只能支持gfn0呀 我改成gfn1后就报错了ase.calculators.calculator.PropertyNotImplementedError: stress property not implemented

516

帖子

1

威望

4765

eV
积分
5301

Level 6 (一方通行)

6#
 楼主 Author| 发表于 Post on 2022-5-16 08:36:54 | 只看该作者 Only view this author
本帖最后由 tjuptz 于 2022-5-16 08:38 编辑

xtb里的gfn1没有周期性吧,不过我记得用ase里的周期性不影响吧

19

帖子

0

威望

859

eV
积分
878

Level 4 (黑子)

7#
发表于 Post on 2022-5-16 10:00:02 | 只看该作者 Only view this author
tjuptz 发表于 2022-5-16 08:36
xtb里的gfn1没有周期性吧,不过我记得用ase里的周期性不影响吧

我查了下会不会是gfn1不支持stress呀 只有gfn0支持

4

帖子

0

威望

518

eV
积分
522

Level 4 (黑子)

8#
发表于 Post on 2023-5-13 16:18:10 | 只看该作者 Only view this author
这个脚本可以用来跑gfnff吗

516

帖子

1

威望

4765

eV
积分
5301

Level 6 (一方通行)

9#
 楼主 Author| 发表于 Post on 2023-5-14 15:29:44 | 只看该作者 Only view this author
dongsong 发表于 2023-5-13 16:18
这个脚本可以用来跑gfnff吗

好久没关注了,写当时是不行的

4

帖子

0

威望

518

eV
积分
522

Level 4 (黑子)

10#
发表于 Post on 2023-5-14 20:08:14 | 只看该作者 Only view this author
tjuptz 发表于 2023-5-14 15:29
好久没关注了,写当时是不行的

研究了一下,现在也不行

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2026-2-21 05:02 , Processed in 0.185875 second(s), 24 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list