mlmm-toolkit 0.2.2.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (372) hide show
  1. hessian_ff/__init__.py +50 -0
  2. hessian_ff/analytical_hessian.py +609 -0
  3. hessian_ff/constants.py +46 -0
  4. hessian_ff/forcefield.py +339 -0
  5. hessian_ff/loaders.py +608 -0
  6. hessian_ff/native/Makefile +8 -0
  7. hessian_ff/native/__init__.py +28 -0
  8. hessian_ff/native/analytical_hessian.py +88 -0
  9. hessian_ff/native/analytical_hessian_ext.cpp +258 -0
  10. hessian_ff/native/bonded.py +82 -0
  11. hessian_ff/native/bonded_ext.cpp +640 -0
  12. hessian_ff/native/loader.py +349 -0
  13. hessian_ff/native/nonbonded.py +118 -0
  14. hessian_ff/native/nonbonded_ext.cpp +1150 -0
  15. hessian_ff/prmtop_parmed.py +23 -0
  16. hessian_ff/system.py +107 -0
  17. hessian_ff/terms/__init__.py +14 -0
  18. hessian_ff/terms/angle.py +73 -0
  19. hessian_ff/terms/bond.py +44 -0
  20. hessian_ff/terms/cmap.py +406 -0
  21. hessian_ff/terms/dihedral.py +141 -0
  22. hessian_ff/terms/nonbonded.py +209 -0
  23. hessian_ff/tests/__init__.py +0 -0
  24. hessian_ff/tests/conftest.py +75 -0
  25. hessian_ff/tests/data/small/complex.parm7 +1346 -0
  26. hessian_ff/tests/data/small/complex.pdb +125 -0
  27. hessian_ff/tests/data/small/complex.rst7 +63 -0
  28. hessian_ff/tests/test_coords_input.py +44 -0
  29. hessian_ff/tests/test_energy_force.py +49 -0
  30. hessian_ff/tests/test_hessian.py +137 -0
  31. hessian_ff/tests/test_smoke.py +18 -0
  32. hessian_ff/tests/test_validation.py +40 -0
  33. hessian_ff/workflows.py +889 -0
  34. mlmm/__init__.py +36 -0
  35. mlmm/__main__.py +7 -0
  36. mlmm/_version.py +34 -0
  37. mlmm/add_elem_info.py +374 -0
  38. mlmm/advanced_help.py +91 -0
  39. mlmm/align_freeze_atoms.py +601 -0
  40. mlmm/all.py +3535 -0
  41. mlmm/bond_changes.py +231 -0
  42. mlmm/bool_compat.py +223 -0
  43. mlmm/cli.py +574 -0
  44. mlmm/cli_utils.py +166 -0
  45. mlmm/default_group.py +337 -0
  46. mlmm/defaults.py +467 -0
  47. mlmm/define_layer.py +526 -0
  48. mlmm/dft.py +1041 -0
  49. mlmm/energy_diagram.py +253 -0
  50. mlmm/extract.py +2213 -0
  51. mlmm/fix_altloc.py +464 -0
  52. mlmm/freq.py +1406 -0
  53. mlmm/harmonic_constraints.py +140 -0
  54. mlmm/hessian_cache.py +44 -0
  55. mlmm/hessian_calc.py +174 -0
  56. mlmm/irc.py +638 -0
  57. mlmm/mlmm_calc.py +2262 -0
  58. mlmm/mm_parm.py +945 -0
  59. mlmm/oniom_export.py +1983 -0
  60. mlmm/oniom_import.py +457 -0
  61. mlmm/opt.py +1742 -0
  62. mlmm/path_opt.py +1353 -0
  63. mlmm/path_search.py +2299 -0
  64. mlmm/preflight.py +88 -0
  65. mlmm/py.typed +1 -0
  66. mlmm/pysis_runner.py +45 -0
  67. mlmm/scan.py +1047 -0
  68. mlmm/scan2d.py +1226 -0
  69. mlmm/scan3d.py +1265 -0
  70. mlmm/scan_common.py +184 -0
  71. mlmm/summary_log.py +736 -0
  72. mlmm/trj2fig.py +448 -0
  73. mlmm/tsopt.py +2871 -0
  74. mlmm/utils.py +2309 -0
  75. mlmm/xtb_embedcharge_correction.py +475 -0
  76. mlmm_toolkit-0.2.2.dev0.dist-info/METADATA +1159 -0
  77. mlmm_toolkit-0.2.2.dev0.dist-info/RECORD +372 -0
  78. mlmm_toolkit-0.2.2.dev0.dist-info/WHEEL +5 -0
  79. mlmm_toolkit-0.2.2.dev0.dist-info/entry_points.txt +2 -0
  80. mlmm_toolkit-0.2.2.dev0.dist-info/licenses/LICENSE +674 -0
  81. mlmm_toolkit-0.2.2.dev0.dist-info/top_level.txt +4 -0
  82. pysisyphus/Geometry.py +1667 -0
  83. pysisyphus/LICENSE +674 -0
  84. pysisyphus/TableFormatter.py +63 -0
  85. pysisyphus/TablePrinter.py +74 -0
  86. pysisyphus/__init__.py +12 -0
  87. pysisyphus/calculators/AFIR.py +452 -0
  88. pysisyphus/calculators/AnaPot.py +20 -0
  89. pysisyphus/calculators/AnaPot2.py +48 -0
  90. pysisyphus/calculators/AnaPot3.py +12 -0
  91. pysisyphus/calculators/AnaPot4.py +20 -0
  92. pysisyphus/calculators/AnaPotBase.py +337 -0
  93. pysisyphus/calculators/AnaPotCBM.py +25 -0
  94. pysisyphus/calculators/AtomAtomTransTorque.py +154 -0
  95. pysisyphus/calculators/CFOUR.py +250 -0
  96. pysisyphus/calculators/Calculator.py +844 -0
  97. pysisyphus/calculators/CerjanMiller.py +24 -0
  98. pysisyphus/calculators/Composite.py +123 -0
  99. pysisyphus/calculators/ConicalIntersection.py +171 -0
  100. pysisyphus/calculators/DFTBp.py +430 -0
  101. pysisyphus/calculators/DFTD3.py +66 -0
  102. pysisyphus/calculators/DFTD4.py +84 -0
  103. pysisyphus/calculators/Dalton.py +61 -0
  104. pysisyphus/calculators/Dimer.py +681 -0
  105. pysisyphus/calculators/Dummy.py +20 -0
  106. pysisyphus/calculators/EGO.py +76 -0
  107. pysisyphus/calculators/EnergyMin.py +224 -0
  108. pysisyphus/calculators/ExternalPotential.py +264 -0
  109. pysisyphus/calculators/FakeASE.py +35 -0
  110. pysisyphus/calculators/FourWellAnaPot.py +28 -0
  111. pysisyphus/calculators/FreeEndNEBPot.py +39 -0
  112. pysisyphus/calculators/Gaussian09.py +18 -0
  113. pysisyphus/calculators/Gaussian16.py +726 -0
  114. pysisyphus/calculators/HardSphere.py +159 -0
  115. pysisyphus/calculators/IDPPCalculator.py +49 -0
  116. pysisyphus/calculators/IPIClient.py +133 -0
  117. pysisyphus/calculators/IPIServer.py +234 -0
  118. pysisyphus/calculators/LEPSBase.py +24 -0
  119. pysisyphus/calculators/LEPSExpr.py +139 -0
  120. pysisyphus/calculators/LennardJones.py +80 -0
  121. pysisyphus/calculators/MOPAC.py +219 -0
  122. pysisyphus/calculators/MullerBrownSympyPot.py +51 -0
  123. pysisyphus/calculators/MultiCalc.py +85 -0
  124. pysisyphus/calculators/NFK.py +45 -0
  125. pysisyphus/calculators/OBabel.py +87 -0
  126. pysisyphus/calculators/ONIOMv2.py +1129 -0
  127. pysisyphus/calculators/ORCA.py +893 -0
  128. pysisyphus/calculators/ORCA5.py +6 -0
  129. pysisyphus/calculators/OpenMM.py +88 -0
  130. pysisyphus/calculators/OpenMolcas.py +281 -0
  131. pysisyphus/calculators/OverlapCalculator.py +908 -0
  132. pysisyphus/calculators/Psi4.py +218 -0
  133. pysisyphus/calculators/PyPsi4.py +37 -0
  134. pysisyphus/calculators/PySCF.py +341 -0
  135. pysisyphus/calculators/PyXTB.py +73 -0
  136. pysisyphus/calculators/QCEngine.py +106 -0
  137. pysisyphus/calculators/Rastrigin.py +22 -0
  138. pysisyphus/calculators/Remote.py +76 -0
  139. pysisyphus/calculators/Rosenbrock.py +15 -0
  140. pysisyphus/calculators/SocketCalc.py +97 -0
  141. pysisyphus/calculators/TIP3P.py +111 -0
  142. pysisyphus/calculators/TransTorque.py +161 -0
  143. pysisyphus/calculators/Turbomole.py +965 -0
  144. pysisyphus/calculators/VRIPot.py +37 -0
  145. pysisyphus/calculators/WFOWrapper.py +333 -0
  146. pysisyphus/calculators/WFOWrapper2.py +341 -0
  147. pysisyphus/calculators/XTB.py +418 -0
  148. pysisyphus/calculators/__init__.py +81 -0
  149. pysisyphus/calculators/cosmo_data.py +139 -0
  150. pysisyphus/calculators/parser.py +150 -0
  151. pysisyphus/color.py +19 -0
  152. pysisyphus/config.py +133 -0
  153. pysisyphus/constants.py +65 -0
  154. pysisyphus/cos/AdaptiveNEB.py +230 -0
  155. pysisyphus/cos/ChainOfStates.py +725 -0
  156. pysisyphus/cos/FreeEndNEB.py +25 -0
  157. pysisyphus/cos/FreezingString.py +103 -0
  158. pysisyphus/cos/GrowingChainOfStates.py +71 -0
  159. pysisyphus/cos/GrowingNT.py +309 -0
  160. pysisyphus/cos/GrowingString.py +508 -0
  161. pysisyphus/cos/NEB.py +189 -0
  162. pysisyphus/cos/SimpleZTS.py +64 -0
  163. pysisyphus/cos/__init__.py +22 -0
  164. pysisyphus/cos/stiffness.py +199 -0
  165. pysisyphus/drivers/__init__.py +17 -0
  166. pysisyphus/drivers/afir.py +855 -0
  167. pysisyphus/drivers/barriers.py +271 -0
  168. pysisyphus/drivers/birkholz.py +138 -0
  169. pysisyphus/drivers/cluster.py +318 -0
  170. pysisyphus/drivers/diabatization.py +133 -0
  171. pysisyphus/drivers/merge.py +368 -0
  172. pysisyphus/drivers/merge_mol2.py +322 -0
  173. pysisyphus/drivers/opt.py +375 -0
  174. pysisyphus/drivers/perf.py +91 -0
  175. pysisyphus/drivers/pka.py +52 -0
  176. pysisyphus/drivers/precon_pos_rot.py +669 -0
  177. pysisyphus/drivers/rates.py +480 -0
  178. pysisyphus/drivers/replace.py +219 -0
  179. pysisyphus/drivers/scan.py +212 -0
  180. pysisyphus/drivers/spectrum.py +166 -0
  181. pysisyphus/drivers/thermo.py +31 -0
  182. pysisyphus/dynamics/Gaussian.py +103 -0
  183. pysisyphus/dynamics/__init__.py +20 -0
  184. pysisyphus/dynamics/colvars.py +136 -0
  185. pysisyphus/dynamics/driver.py +297 -0
  186. pysisyphus/dynamics/helpers.py +256 -0
  187. pysisyphus/dynamics/lincs.py +105 -0
  188. pysisyphus/dynamics/mdp.py +364 -0
  189. pysisyphus/dynamics/rattle.py +121 -0
  190. pysisyphus/dynamics/thermostats.py +128 -0
  191. pysisyphus/dynamics/wigner.py +266 -0
  192. pysisyphus/elem_data.py +3473 -0
  193. pysisyphus/exceptions.py +2 -0
  194. pysisyphus/filtertrj.py +69 -0
  195. pysisyphus/helpers.py +623 -0
  196. pysisyphus/helpers_pure.py +649 -0
  197. pysisyphus/init_logging.py +50 -0
  198. pysisyphus/intcoords/Bend.py +69 -0
  199. pysisyphus/intcoords/Bend2.py +25 -0
  200. pysisyphus/intcoords/BondedFragment.py +32 -0
  201. pysisyphus/intcoords/Cartesian.py +41 -0
  202. pysisyphus/intcoords/CartesianCoords.py +140 -0
  203. pysisyphus/intcoords/Coords.py +56 -0
  204. pysisyphus/intcoords/DLC.py +197 -0
  205. pysisyphus/intcoords/DistanceFunction.py +34 -0
  206. pysisyphus/intcoords/DummyImproper.py +70 -0
  207. pysisyphus/intcoords/DummyTorsion.py +72 -0
  208. pysisyphus/intcoords/LinearBend.py +105 -0
  209. pysisyphus/intcoords/LinearDisplacement.py +80 -0
  210. pysisyphus/intcoords/OutOfPlane.py +59 -0
  211. pysisyphus/intcoords/PrimTypes.py +286 -0
  212. pysisyphus/intcoords/Primitive.py +137 -0
  213. pysisyphus/intcoords/RedundantCoords.py +659 -0
  214. pysisyphus/intcoords/RobustTorsion.py +59 -0
  215. pysisyphus/intcoords/Rotation.py +147 -0
  216. pysisyphus/intcoords/Stretch.py +31 -0
  217. pysisyphus/intcoords/Torsion.py +101 -0
  218. pysisyphus/intcoords/Torsion2.py +25 -0
  219. pysisyphus/intcoords/Translation.py +45 -0
  220. pysisyphus/intcoords/__init__.py +61 -0
  221. pysisyphus/intcoords/augment_bonds.py +126 -0
  222. pysisyphus/intcoords/derivatives.py +10512 -0
  223. pysisyphus/intcoords/eval.py +80 -0
  224. pysisyphus/intcoords/exceptions.py +37 -0
  225. pysisyphus/intcoords/findiffs.py +48 -0
  226. pysisyphus/intcoords/generate_derivatives.py +414 -0
  227. pysisyphus/intcoords/helpers.py +235 -0
  228. pysisyphus/intcoords/logging_conf.py +10 -0
  229. pysisyphus/intcoords/mp_derivatives.py +10836 -0
  230. pysisyphus/intcoords/setup.py +962 -0
  231. pysisyphus/intcoords/setup_fast.py +176 -0
  232. pysisyphus/intcoords/update.py +272 -0
  233. pysisyphus/intcoords/valid.py +89 -0
  234. pysisyphus/interpolate/Geodesic.py +93 -0
  235. pysisyphus/interpolate/IDPP.py +55 -0
  236. pysisyphus/interpolate/Interpolator.py +116 -0
  237. pysisyphus/interpolate/LST.py +70 -0
  238. pysisyphus/interpolate/Redund.py +152 -0
  239. pysisyphus/interpolate/__init__.py +9 -0
  240. pysisyphus/interpolate/helpers.py +34 -0
  241. pysisyphus/io/__init__.py +22 -0
  242. pysisyphus/io/aomix.py +178 -0
  243. pysisyphus/io/cjson.py +24 -0
  244. pysisyphus/io/crd.py +101 -0
  245. pysisyphus/io/cube.py +220 -0
  246. pysisyphus/io/fchk.py +184 -0
  247. pysisyphus/io/hdf5.py +49 -0
  248. pysisyphus/io/hessian.py +72 -0
  249. pysisyphus/io/mol2.py +146 -0
  250. pysisyphus/io/molden.py +293 -0
  251. pysisyphus/io/orca.py +189 -0
  252. pysisyphus/io/pdb.py +269 -0
  253. pysisyphus/io/psf.py +79 -0
  254. pysisyphus/io/pubchem.py +31 -0
  255. pysisyphus/io/qcschema.py +34 -0
  256. pysisyphus/io/sdf.py +29 -0
  257. pysisyphus/io/xyz.py +61 -0
  258. pysisyphus/io/zmat.py +175 -0
  259. pysisyphus/irc/DWI.py +108 -0
  260. pysisyphus/irc/DampedVelocityVerlet.py +134 -0
  261. pysisyphus/irc/Euler.py +22 -0
  262. pysisyphus/irc/EulerPC.py +345 -0
  263. pysisyphus/irc/GonzalezSchlegel.py +187 -0
  264. pysisyphus/irc/IMKMod.py +164 -0
  265. pysisyphus/irc/IRC.py +878 -0
  266. pysisyphus/irc/IRCDummy.py +10 -0
  267. pysisyphus/irc/Instanton.py +307 -0
  268. pysisyphus/irc/LQA.py +53 -0
  269. pysisyphus/irc/ModeKill.py +136 -0
  270. pysisyphus/irc/ParamPlot.py +53 -0
  271. pysisyphus/irc/RK4.py +36 -0
  272. pysisyphus/irc/__init__.py +31 -0
  273. pysisyphus/irc/initial_displ.py +219 -0
  274. pysisyphus/linalg.py +411 -0
  275. pysisyphus/line_searches/Backtracking.py +88 -0
  276. pysisyphus/line_searches/HagerZhang.py +184 -0
  277. pysisyphus/line_searches/LineSearch.py +232 -0
  278. pysisyphus/line_searches/StrongWolfe.py +108 -0
  279. pysisyphus/line_searches/__init__.py +9 -0
  280. pysisyphus/line_searches/interpol.py +15 -0
  281. pysisyphus/modefollow/NormalMode.py +40 -0
  282. pysisyphus/modefollow/__init__.py +10 -0
  283. pysisyphus/modefollow/davidson.py +199 -0
  284. pysisyphus/modefollow/lanczos.py +95 -0
  285. pysisyphus/optimizers/BFGS.py +99 -0
  286. pysisyphus/optimizers/BacktrackingOptimizer.py +113 -0
  287. pysisyphus/optimizers/ConjugateGradient.py +98 -0
  288. pysisyphus/optimizers/CubicNewton.py +75 -0
  289. pysisyphus/optimizers/FIRE.py +113 -0
  290. pysisyphus/optimizers/HessianOptimizer.py +1176 -0
  291. pysisyphus/optimizers/LBFGS.py +228 -0
  292. pysisyphus/optimizers/LayerOpt.py +411 -0
  293. pysisyphus/optimizers/MicroOptimizer.py +169 -0
  294. pysisyphus/optimizers/NCOptimizer.py +90 -0
  295. pysisyphus/optimizers/Optimizer.py +1084 -0
  296. pysisyphus/optimizers/PreconLBFGS.py +260 -0
  297. pysisyphus/optimizers/PreconSteepestDescent.py +7 -0
  298. pysisyphus/optimizers/QuickMin.py +74 -0
  299. pysisyphus/optimizers/RFOptimizer.py +181 -0
  300. pysisyphus/optimizers/RSA.py +99 -0
  301. pysisyphus/optimizers/StabilizedQNMethod.py +248 -0
  302. pysisyphus/optimizers/SteepestDescent.py +23 -0
  303. pysisyphus/optimizers/StringOptimizer.py +173 -0
  304. pysisyphus/optimizers/__init__.py +41 -0
  305. pysisyphus/optimizers/closures.py +301 -0
  306. pysisyphus/optimizers/cls_map.py +58 -0
  307. pysisyphus/optimizers/exceptions.py +6 -0
  308. pysisyphus/optimizers/gdiis.py +280 -0
  309. pysisyphus/optimizers/guess_hessians.py +311 -0
  310. pysisyphus/optimizers/hessian_updates.py +355 -0
  311. pysisyphus/optimizers/poly_fit.py +285 -0
  312. pysisyphus/optimizers/precon.py +153 -0
  313. pysisyphus/optimizers/restrict_step.py +24 -0
  314. pysisyphus/pack.py +172 -0
  315. pysisyphus/peakdetect.py +948 -0
  316. pysisyphus/plot.py +1031 -0
  317. pysisyphus/run.py +2106 -0
  318. pysisyphus/socket_helper.py +74 -0
  319. pysisyphus/stocastic/FragmentKick.py +132 -0
  320. pysisyphus/stocastic/Kick.py +81 -0
  321. pysisyphus/stocastic/Pipeline.py +303 -0
  322. pysisyphus/stocastic/__init__.py +21 -0
  323. pysisyphus/stocastic/align.py +127 -0
  324. pysisyphus/testing.py +96 -0
  325. pysisyphus/thermo.py +156 -0
  326. pysisyphus/trj.py +824 -0
  327. pysisyphus/tsoptimizers/RSIRFOptimizer.py +56 -0
  328. pysisyphus/tsoptimizers/RSPRFOptimizer.py +182 -0
  329. pysisyphus/tsoptimizers/TRIM.py +59 -0
  330. pysisyphus/tsoptimizers/TSHessianOptimizer.py +463 -0
  331. pysisyphus/tsoptimizers/__init__.py +23 -0
  332. pysisyphus/wavefunction/Basis.py +239 -0
  333. pysisyphus/wavefunction/DIIS.py +76 -0
  334. pysisyphus/wavefunction/__init__.py +25 -0
  335. pysisyphus/wavefunction/build_ext.py +42 -0
  336. pysisyphus/wavefunction/cart2sph.py +190 -0
  337. pysisyphus/wavefunction/diabatization.py +304 -0
  338. pysisyphus/wavefunction/excited_states.py +435 -0
  339. pysisyphus/wavefunction/gen_ints.py +1811 -0
  340. pysisyphus/wavefunction/helpers.py +104 -0
  341. pysisyphus/wavefunction/ints/__init__.py +0 -0
  342. pysisyphus/wavefunction/ints/boys.py +193 -0
  343. pysisyphus/wavefunction/ints/boys_table_N_64_xasym_27.1_step_0.01.npy +0 -0
  344. pysisyphus/wavefunction/ints/cart_gto3d.py +176 -0
  345. pysisyphus/wavefunction/ints/coulomb3d.py +25928 -0
  346. pysisyphus/wavefunction/ints/diag_quadrupole3d.py +10036 -0
  347. pysisyphus/wavefunction/ints/dipole3d.py +8762 -0
  348. pysisyphus/wavefunction/ints/int2c2e3d.py +7198 -0
  349. pysisyphus/wavefunction/ints/int3c2e3d_sph.py +65040 -0
  350. pysisyphus/wavefunction/ints/kinetic3d.py +8240 -0
  351. pysisyphus/wavefunction/ints/ovlp3d.py +3777 -0
  352. pysisyphus/wavefunction/ints/quadrupole3d.py +15054 -0
  353. pysisyphus/wavefunction/ints/self_ovlp3d.py +198 -0
  354. pysisyphus/wavefunction/localization.py +458 -0
  355. pysisyphus/wavefunction/multipole.py +159 -0
  356. pysisyphus/wavefunction/normalization.py +36 -0
  357. pysisyphus/wavefunction/pop_analysis.py +134 -0
  358. pysisyphus/wavefunction/shells.py +1171 -0
  359. pysisyphus/wavefunction/wavefunction.py +504 -0
  360. pysisyphus/wrapper/__init__.py +11 -0
  361. pysisyphus/wrapper/exceptions.py +2 -0
  362. pysisyphus/wrapper/jmol.py +120 -0
  363. pysisyphus/wrapper/mwfn.py +169 -0
  364. pysisyphus/wrapper/packmol.py +71 -0
  365. pysisyphus/xyzloader.py +168 -0
  366. pysisyphus/yaml_mods.py +45 -0
  367. thermoanalysis/LICENSE +674 -0
  368. thermoanalysis/QCData.py +244 -0
  369. thermoanalysis/__init__.py +0 -0
  370. thermoanalysis/config.py +3 -0
  371. thermoanalysis/constants.py +20 -0
  372. thermoanalysis/thermo.py +1011 -0
hessian_ff/__init__.py ADDED
@@ -0,0 +1,50 @@
1
+ """hessian_ff
2
+
3
+ Minimal AMBER (parm7/prmtop) force field evaluation in PyTorch.
4
+
5
+ Design goals:
6
+ - ONIOM/MM style usage (no periodic boundary conditions, no Ewald/PME)
7
+ - Differentiable energies (PyTorch autograd)
8
+ - Match AMBER prmtop conventions: exclusions and 1-4 scaling
9
+
10
+ Primary entrypoints:
11
+ - :func:`hessian_ff.load_system`
12
+ - :class:`hessian_ff.forcefield.ForceFieldTorch`
13
+ - :func:`hessian_ff.workflows.torch_energy`
14
+ - :func:`hessian_ff.workflows.torch_force`
15
+ - :func:`hessian_ff.workflows.torch_hessian`
16
+ """
17
+
18
+ from .system import AmberSystem
19
+ from .forcefield import ForceFieldTorch
20
+ from .loaders import load_system, load_coords
21
+ from .native import build_native_extensions
22
+ from .workflows import (
23
+ clear_runtime_cache,
24
+ system_summary,
25
+ torch_energy,
26
+ torch_energy_batch,
27
+ torch_force,
28
+ torch_force_batch,
29
+ torch_hessian,
30
+ verify_openmm,
31
+ )
32
+
33
+ __version__ = "0.1.0"
34
+
35
+ __all__ = [
36
+ "__version__",
37
+ "AmberSystem",
38
+ "ForceFieldTorch",
39
+ "build_native_extensions",
40
+ "load_system",
41
+ "load_coords",
42
+ "clear_runtime_cache",
43
+ "system_summary",
44
+ "torch_energy",
45
+ "torch_energy_batch",
46
+ "torch_force",
47
+ "torch_force_batch",
48
+ "torch_hessian",
49
+ "verify_openmm",
50
+ ]
@@ -0,0 +1,609 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Dict, Sequence, Tuple
4
+
5
+ import torch
6
+
7
+ from .constants import TWO_PI
8
+ from .system import AmberSystem
9
+ from .native.analytical_hessian import (
10
+ bond_hessian_native,
11
+ nonbonded_hessian_native,
12
+ scatter_local_hessian_native,
13
+ )
14
+ from .native.loader import get_analytical_hessian_extension
15
+
16
+ Dual = Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
17
+ DualVec3 = Tuple[Dual, Dual, Dual]
18
+ _NATIVE_ANALYTICAL_AVAILABLE: bool | None = None
19
+
20
+
21
+ def _has_native_analytical_ext() -> bool:
22
+ global _NATIVE_ANALYTICAL_AVAILABLE
23
+ if _NATIVE_ANALYTICAL_AVAILABLE is None:
24
+ _NATIVE_ANALYTICAL_AVAILABLE = (
25
+ get_analytical_hessian_extension(verbose=False, force_rebuild=False) is not None
26
+ )
27
+ return bool(_NATIVE_ANALYTICAL_AVAILABLE)
28
+
29
+
30
+ def _make_active_map(
31
+ natom: int,
32
+ active_atoms: Sequence[int],
33
+ *,
34
+ device: torch.device,
35
+ ) -> torch.Tensor:
36
+ amap = torch.full((int(natom),), -1, dtype=torch.int64, device=device)
37
+ if len(active_atoms) == 0:
38
+ return amap
39
+ idx = torch.tensor([int(a) for a in active_atoms], dtype=torch.int64, device=device)
40
+ vals = torch.arange(len(active_atoms), dtype=torch.int64, device=device)
41
+ amap.index_put_((idx,), vals)
42
+ return amap
43
+
44
+
45
+ def _slice_by_rank(x: torch.Tensor, rank: int, size: int) -> torch.Tensor:
46
+ if size <= 1 or x.numel() == 0:
47
+ return x
48
+ return x[rank::size]
49
+
50
+
51
+ def _outer(a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
52
+ return a.unsqueeze(-1) * b.unsqueeze(-2)
53
+
54
+
55
+ def _dual_vars(x: torch.Tensor) -> list[Dual]:
56
+ t, m = int(x.shape[0]), int(x.shape[1])
57
+ eye = torch.eye(m, dtype=x.dtype, device=x.device).unsqueeze(0).expand(t, m, m)
58
+ h0 = x.new_zeros((t, m, m))
59
+ return [(x[:, i], eye[:, i, :], h0) for i in range(m)]
60
+
61
+
62
+ def _d_add(a: Dual, b: Dual) -> Dual:
63
+ return (a[0] + b[0], a[1] + b[1], a[2] + b[2])
64
+
65
+
66
+ def _d_sub(a: Dual, b: Dual) -> Dual:
67
+ return (a[0] - b[0], a[1] - b[1], a[2] - b[2])
68
+
69
+
70
+ def _d_mul(a: Dual, b: Dual) -> Dual:
71
+ av, ag, ah = a
72
+ bv, bg, bh = b
73
+ v = av * bv
74
+ g = ag * bv.unsqueeze(-1) + bg * av.unsqueeze(-1)
75
+ h = (
76
+ ah * bv.unsqueeze(-1).unsqueeze(-1)
77
+ + bh * av.unsqueeze(-1).unsqueeze(-1)
78
+ + _outer(ag, bg)
79
+ + _outer(bg, ag)
80
+ )
81
+ return v, g, h
82
+
83
+
84
+ def _d_inv(a: Dual) -> Dual:
85
+ av, ag, ah = a
86
+ inv = 1.0 / av
87
+ inv2 = inv * inv
88
+ inv3 = inv2 * inv
89
+ v = inv
90
+ g = -ag * inv2.unsqueeze(-1)
91
+ h = (2.0 * _outer(ag, ag) - ah * av.unsqueeze(-1).unsqueeze(-1)) * inv3.unsqueeze(-1).unsqueeze(-1)
92
+ return v, g, h
93
+
94
+
95
+ def _d_div(a: Dual, b: Dual) -> Dual:
96
+ return _d_mul(a, _d_inv(b))
97
+
98
+
99
+ def _d_clamp_min(a: Dual, lo: float) -> Dual:
100
+ av, ag, ah = a
101
+ v = torch.clamp(av, min=float(lo))
102
+ mask = av < float(lo)
103
+ if bool(torch.any(mask)):
104
+ g = ag.clone()
105
+ h = ah.clone()
106
+ g[mask] = 0.0
107
+ h[mask] = 0.0
108
+ return v, g, h
109
+ return v, ag, ah
110
+
111
+
112
+ def _d_sqrt(a: Dual) -> Dual:
113
+ av, ag, ah = a
114
+ v = torch.sqrt(av)
115
+ inv2v = 0.5 / v
116
+ g = ag * inv2v.unsqueeze(-1)
117
+ h = ah * inv2v.unsqueeze(-1).unsqueeze(-1) - _outer(ag, ag) * (0.25 / (v * v * v)).unsqueeze(-1).unsqueeze(-1)
118
+ return v, g, h
119
+
120
+
121
+ def _d_atan2(y: Dual, x: Dual) -> Dual:
122
+ yv, yg, yh = y
123
+ xv, xg, xh = x
124
+ d = xv * xv + yv * yv
125
+ d = torch.clamp(d, min=1.0e-24)
126
+ v = torch.atan2(yv, xv)
127
+
128
+ n = xv.unsqueeze(-1) * yg - yv.unsqueeze(-1) * xg
129
+ g = n / d.unsqueeze(-1)
130
+
131
+ n_jac = (
132
+ xv.unsqueeze(-1).unsqueeze(-1) * yh
133
+ - yv.unsqueeze(-1).unsqueeze(-1) * xh
134
+ + _outer(yg, xg)
135
+ - _outer(xg, yg)
136
+ )
137
+ d_grad = 2.0 * xv.unsqueeze(-1) * xg + 2.0 * yv.unsqueeze(-1) * yg
138
+ h = n_jac / d.unsqueeze(-1).unsqueeze(-1) - _outer(n, d_grad) / (d * d).unsqueeze(-1).unsqueeze(-1)
139
+ h = 0.5 * (h + h.transpose(-1, -2))
140
+ return v, g, h
141
+
142
+
143
+ def _dv_sub(a: DualVec3, b: DualVec3) -> DualVec3:
144
+ return (_d_sub(a[0], b[0]), _d_sub(a[1], b[1]), _d_sub(a[2], b[2]))
145
+
146
+
147
+ def _dv_cross(a: DualVec3, b: DualVec3) -> DualVec3:
148
+ c0 = _d_sub(_d_mul(a[1], b[2]), _d_mul(a[2], b[1]))
149
+ c1 = _d_sub(_d_mul(a[2], b[0]), _d_mul(a[0], b[2]))
150
+ c2 = _d_sub(_d_mul(a[0], b[1]), _d_mul(a[1], b[0]))
151
+ return c0, c1, c2
152
+
153
+
154
+ def _dv_dot(a: DualVec3, b: DualVec3) -> Dual:
155
+ return _d_add(_d_add(_d_mul(a[0], b[0]), _d_mul(a[1], b[1])), _d_mul(a[2], b[2]))
156
+
157
+
158
+ def _dv_norm(a: DualVec3, eps: float = 1.0e-24) -> Dual:
159
+ s2 = _dv_dot(a, a)
160
+ return _d_sqrt(_d_clamp_min(s2, float(eps)))
161
+
162
+
163
+ def _dual_angle_from_points(pi: DualVec3, pj: DualVec3, pk: DualVec3) -> Dual:
164
+ u = _dv_sub(pi, pj)
165
+ v = _dv_sub(pk, pj)
166
+ c = _dv_dot(u, v)
167
+ p = _dv_cross(u, v)
168
+ s = _dv_norm(p)
169
+ return _d_atan2(s, c)
170
+
171
+
172
+ def _dual_dihedral_from_points(p0: DualVec3, p1: DualVec3, p2: DualVec3, p3: DualVec3) -> Dual:
173
+ v1 = _dv_sub(p0, p1) # A-B
174
+ v2 = _dv_sub(p2, p1) # C-B
175
+ v3 = _dv_sub(p2, p3) # C-D
176
+
177
+ c12 = _dv_cross(v1, v2)
178
+ c23 = _dv_cross(v2, v3)
179
+ x = _dv_dot(c12, c23)
180
+ c = _dv_cross(c12, c23)
181
+ v2n = _dv_norm(v2)
182
+ y = _d_div(_dv_dot(v2, c), v2n)
183
+ return _d_atan2(y, x)
184
+
185
+
186
+ def _local_dof_indices(atom_idx: torch.Tensor, active_map: torch.Tensor) -> torch.Tensor:
187
+ t = int(atom_idx.shape[0])
188
+ na = int(atom_idx.shape[1])
189
+ dof_idx = torch.full((t, 3 * na), -1, dtype=torch.int64, device=active_map.device)
190
+ act = active_map[atom_idx]
191
+ for a in range(na):
192
+ aa = act[:, a]
193
+ mask = aa >= 0
194
+ if not bool(torch.any(mask)):
195
+ continue
196
+ base = 3 * aa[mask]
197
+ dof_idx[mask, 3 * a + 0] = base + 0
198
+ dof_idx[mask, 3 * a + 1] = base + 1
199
+ dof_idx[mask, 3 * a + 2] = base + 2
200
+ return dof_idx
201
+
202
+
203
+ def _scatter_local_hessian(
204
+ h2: torch.Tensor,
205
+ local_h: torch.Tensor,
206
+ dof_idx: torch.Tensor,
207
+ ) -> None:
208
+ h2.add_(
209
+ scatter_local_hessian_native(
210
+ local_h=local_h,
211
+ dof_idx=dof_idx,
212
+ ndof=int(h2.shape[0]),
213
+ )
214
+ )
215
+
216
+
217
+ def _filter_terms_with_active(active_map: torch.Tensor, atom_cols: Sequence[torch.Tensor]) -> torch.Tensor:
218
+ keep = torch.zeros_like(atom_cols[0], dtype=torch.bool)
219
+ for c in atom_cols:
220
+ keep |= active_map[c] >= 0
221
+ return keep
222
+
223
+
224
+ def _add_bond_hessian_closed_form(
225
+ h2: torch.Tensor,
226
+ system: AmberSystem,
227
+ coords: torch.Tensor,
228
+ active_map: torch.Tensor,
229
+ rank: int,
230
+ size: int,
231
+ ) -> int:
232
+ ii = _slice_by_rank(system.bond_i, rank, size)
233
+ jj = _slice_by_rank(system.bond_j, rank, size)
234
+ kk = _slice_by_rank(system.bond_k, rank, size)
235
+ r0 = _slice_by_rank(system.bond_r0, rank, size)
236
+ if ii.numel() == 0:
237
+ return 0
238
+
239
+ keep = _filter_terms_with_active(active_map, (ii, jj))
240
+ ii, jj, kk, r0 = ii[keep], jj[keep], kk[keep], r0[keep]
241
+ if ii.numel() == 0:
242
+ return 0
243
+
244
+ h2.add_(
245
+ bond_hessian_native(
246
+ coords=coords,
247
+ bond_i=ii,
248
+ bond_j=jj,
249
+ bond_k=kk,
250
+ bond_r0=r0,
251
+ active_map=active_map,
252
+ ndof=int(h2.shape[0]),
253
+ )
254
+ )
255
+ return int(ii.numel())
256
+
257
+
258
+ def _add_nonbonded_pairset_closed_form(
259
+ h2: torch.Tensor,
260
+ system: AmberSystem,
261
+ coords: torch.Tensor,
262
+ active_map: torch.Tensor,
263
+ pair_i: torch.Tensor,
264
+ pair_j: torch.Tensor,
265
+ inv_scee: torch.Tensor | None,
266
+ inv_scnb: torch.Tensor | None,
267
+ rank: int,
268
+ size: int,
269
+ ) -> int:
270
+ ii = _slice_by_rank(pair_i, rank, size)
271
+ jj = _slice_by_rank(pair_j, rank, size)
272
+ scee = _slice_by_rank(inv_scee, rank, size) if inv_scee is not None else None
273
+ scnb = _slice_by_rank(inv_scnb, rank, size) if inv_scnb is not None else None
274
+ if ii.numel() == 0:
275
+ return 0
276
+
277
+ keep = _filter_terms_with_active(active_map, (ii, jj))
278
+ ii, jj = ii[keep], jj[keep]
279
+ if scee is not None:
280
+ scee = scee[keep]
281
+ if scnb is not None:
282
+ scnb = scnb[keep]
283
+ n = int(ii.numel())
284
+ if n == 0:
285
+ return 0
286
+
287
+ h2.add_(
288
+ nonbonded_hessian_native(
289
+ coords=coords,
290
+ charge=system.charge,
291
+ atom_type=system.atom_type,
292
+ lj_acoef=system.lj_acoef,
293
+ lj_bcoef=system.lj_bcoef,
294
+ hb_acoef=system.hb_acoef,
295
+ hb_bcoef=system.hb_bcoef,
296
+ nb_index=system.nb_index,
297
+ pair_i=ii,
298
+ pair_j=jj,
299
+ inv_scee=scee,
300
+ inv_scnb=scnb,
301
+ active_map=active_map,
302
+ ndof=int(h2.shape[0]),
303
+ )
304
+ )
305
+ return n
306
+
307
+
308
+ def _add_angle_hessian_closed_form(
309
+ h2: torch.Tensor,
310
+ system: AmberSystem,
311
+ coords: torch.Tensor,
312
+ active_map: torch.Tensor,
313
+ rank: int,
314
+ size: int,
315
+ chunk_terms: int = 256,
316
+ ) -> int:
317
+ ii = _slice_by_rank(system.angle_i, rank, size)
318
+ jj = _slice_by_rank(system.angle_j, rank, size)
319
+ kk = _slice_by_rank(system.angle_k, rank, size)
320
+ ktheta = _slice_by_rank(system.angle_k0, rank, size)
321
+ t0 = _slice_by_rank(system.angle_t0, rank, size)
322
+ if ii.numel() == 0:
323
+ return 0
324
+
325
+ keep = _filter_terms_with_active(active_map, (ii, jj, kk))
326
+ ii, jj, kk, ktheta, t0 = ii[keep], jj[keep], kk[keep], ktheta[keep], t0[keep]
327
+ n = int(ii.numel())
328
+ if n == 0:
329
+ return 0
330
+
331
+ for s in range(0, n, int(chunk_terms)):
332
+ e = min(s + int(chunk_terms), n)
333
+ ic, jc, kc = ii[s:e], jj[s:e], kk[s:e]
334
+ ktc, t0c = ktheta[s:e], t0[s:e]
335
+ local_xyz = torch.cat((coords[ic], coords[jc], coords[kc]), dim=1) # [T,9]
336
+ vars_ = _dual_vars(local_xyz)
337
+ pi = (vars_[0], vars_[1], vars_[2])
338
+ pj = (vars_[3], vars_[4], vars_[5])
339
+ pk = (vars_[6], vars_[7], vars_[8])
340
+ th_v, th_g, th_h = _dual_angle_from_points(pi, pj, pk)
341
+
342
+ d1 = 2.0 * ktc * (th_v - t0c)
343
+ d2 = 2.0 * ktc
344
+ local_h = d2.unsqueeze(-1).unsqueeze(-1) * _outer(th_g, th_g) + d1.unsqueeze(-1).unsqueeze(-1) * th_h
345
+ local_h = 0.5 * (local_h + local_h.transpose(-1, -2))
346
+
347
+ atom_idx = torch.stack((ic, jc, kc), dim=1)
348
+ dof_idx = _local_dof_indices(atom_idx, active_map)
349
+ _scatter_local_hessian(h2, local_h, dof_idx)
350
+ return n
351
+
352
+
353
+ def _add_dihedral_hessian_closed_form(
354
+ h2: torch.Tensor,
355
+ system: AmberSystem,
356
+ coords: torch.Tensor,
357
+ active_map: torch.Tensor,
358
+ rank: int,
359
+ size: int,
360
+ chunk_terms: int = 192,
361
+ ) -> int:
362
+ ii = _slice_by_rank(system.dihed_i, rank, size)
363
+ jj = _slice_by_rank(system.dihed_j, rank, size)
364
+ kk = _slice_by_rank(system.dihed_k, rank, size)
365
+ ll = _slice_by_rank(system.dihed_l, rank, size)
366
+ force = _slice_by_rank(system.dihed_force, rank, size)
367
+ period = _slice_by_rank(system.dihed_period, rank, size)
368
+ phase = _slice_by_rank(system.dihed_phase, rank, size)
369
+ if ii.numel() == 0:
370
+ return 0
371
+
372
+ keep = _filter_terms_with_active(active_map, (ii, jj, kk, ll))
373
+ ii, jj, kk, ll = ii[keep], jj[keep], kk[keep], ll[keep]
374
+ force, period, phase = force[keep], period[keep], phase[keep]
375
+ n = int(ii.numel())
376
+ if n == 0:
377
+ return 0
378
+
379
+ for s in range(0, n, int(chunk_terms)):
380
+ e = min(s + int(chunk_terms), n)
381
+ ic, jc, kc, lc = ii[s:e], jj[s:e], kk[s:e], ll[s:e]
382
+ fc = force[s:e]
383
+ pc = torch.abs(period[s:e])
384
+ phc = phase[s:e]
385
+ local_xyz = torch.cat((coords[ic], coords[jc], coords[kc], coords[lc]), dim=1) # [T,12]
386
+ vars_ = _dual_vars(local_xyz)
387
+ p0 = (vars_[0], vars_[1], vars_[2])
388
+ p1 = (vars_[3], vars_[4], vars_[5])
389
+ p2 = (vars_[6], vars_[7], vars_[8])
390
+ p3 = (vars_[9], vars_[10], vars_[11])
391
+ phi_v, phi_g, phi_h = _dual_dihedral_from_points(p0, p1, p2, p3)
392
+
393
+ d = pc * phi_v - phc
394
+ e1 = -fc * pc * torch.sin(d)
395
+ e2 = -fc * pc * pc * torch.cos(d)
396
+ local_h = e2.unsqueeze(-1).unsqueeze(-1) * _outer(phi_g, phi_g) + e1.unsqueeze(-1).unsqueeze(-1) * phi_h
397
+ local_h = 0.5 * (local_h + local_h.transpose(-1, -2))
398
+
399
+ atom_idx = torch.stack((ic, jc, kc, lc), dim=1)
400
+ dof_idx = _local_dof_indices(atom_idx, active_map)
401
+ _scatter_local_hessian(h2, local_h, dof_idx)
402
+ return n
403
+
404
+
405
+ def _cmap_patch_derivatives(coeff: torch.Tensor, da: torch.Tensor, db: torch.Tensor) -> tuple[torch.Tensor, ...]:
406
+ # coeff: [T,4,4], da/db: [T]
407
+ db2 = db * db
408
+ da2 = da * da
409
+ da3 = da2 * da
410
+
411
+ p = (
412
+ coeff[:, :, 0]
413
+ + coeff[:, :, 1] * db.unsqueeze(-1)
414
+ + coeff[:, :, 2] * db2.unsqueeze(-1)
415
+ + coeff[:, :, 3] * (db2 * db).unsqueeze(-1)
416
+ )
417
+ pd = coeff[:, :, 1] + 2.0 * coeff[:, :, 2] * db.unsqueeze(-1) + 3.0 * coeff[:, :, 3] * db2.unsqueeze(-1)
418
+ pdd = 2.0 * coeff[:, :, 2] + 6.0 * coeff[:, :, 3] * db.unsqueeze(-1)
419
+
420
+ e = p[:, 0] + p[:, 1] * da + p[:, 2] * da2 + p[:, 3] * da3
421
+ e_da = p[:, 1] + 2.0 * p[:, 2] * da + 3.0 * p[:, 3] * da2
422
+ e_daa = 2.0 * p[:, 2] + 6.0 * p[:, 3] * da
423
+
424
+ e_db = pd[:, 0] + pd[:, 1] * da + pd[:, 2] * da2 + pd[:, 3] * da3
425
+ e_dbb = pdd[:, 0] + pdd[:, 1] * da + pdd[:, 2] * da2 + pdd[:, 3] * da3
426
+ e_dab = pd[:, 1] + 2.0 * pd[:, 2] * da + 3.0 * pd[:, 3] * da2
427
+ return e, e_da, e_db, e_daa, e_dbb, e_dab
428
+
429
+
430
+ def _add_cmap_hessian_closed_form(
431
+ h2: torch.Tensor,
432
+ system: AmberSystem,
433
+ coords: torch.Tensor,
434
+ active_map: torch.Tensor,
435
+ rank: int,
436
+ size: int,
437
+ chunk_terms: int = 96,
438
+ ) -> int:
439
+ if int(system.cmap_type.numel()) == 0:
440
+ return 0
441
+
442
+ tmap = _slice_by_rank(system.cmap_type, rank, size)
443
+ ia = _slice_by_rank(system.cmap_i, rank, size)
444
+ ja = _slice_by_rank(system.cmap_j, rank, size)
445
+ ka = _slice_by_rank(system.cmap_k, rank, size)
446
+ la = _slice_by_rank(system.cmap_l, rank, size)
447
+ ma = _slice_by_rank(system.cmap_m, rank, size)
448
+ if ia.numel() == 0:
449
+ return 0
450
+
451
+ keep = _filter_terms_with_active(active_map, (ia, ja, ka, la, ma))
452
+ tmap, ia, ja, ka, la, ma = tmap[keep], ia[keep], ja[keep], ka[keep], la[keep], ma[keep]
453
+ n = int(ia.numel())
454
+ if n == 0:
455
+ return 0
456
+
457
+ for s in range(0, n, int(chunk_terms)):
458
+ e = min(s + int(chunk_terms), n)
459
+ tc = tmap[s:e]
460
+ ic, jc, kc, lc, mc = ia[s:e], ja[s:e], ka[s:e], la[s:e], ma[s:e]
461
+
462
+ local_xyz = torch.cat((coords[ic], coords[jc], coords[kc], coords[lc], coords[mc]), dim=1) # [T,15]
463
+ vars_ = _dual_vars(local_xyz)
464
+ pi = (vars_[0], vars_[1], vars_[2])
465
+ pj = (vars_[3], vars_[4], vars_[5])
466
+ pk = (vars_[6], vars_[7], vars_[8])
467
+ pl = (vars_[9], vars_[10], vars_[11])
468
+ pm = (vars_[12], vars_[13], vars_[14])
469
+
470
+ phi_v, phi_g, phi_h = _dual_dihedral_from_points(pi, pj, pk, pl)
471
+ psi_v, psi_g, psi_h = _dual_dihedral_from_points(pj, pk, pl, pm)
472
+
473
+ size_map = system.cmap_size[tc]
474
+ delta = system.cmap_delta[tc].to(dtype=coords.dtype)
475
+ ang_a = torch.remainder(phi_v + TWO_PI, TWO_PI)
476
+ ang_b = torch.remainder(psi_v + TWO_PI, TWO_PI)
477
+ u = ang_a / delta
478
+ v = ang_b / delta
479
+ su = torch.floor(u).to(torch.int64)
480
+ sv = torch.floor(v).to(torch.int64)
481
+ su = torch.minimum(su, size_map - 1)
482
+ sv = torch.minimum(sv, size_map - 1)
483
+ da = u - su.to(dtype=coords.dtype)
484
+ db = v - sv.to(dtype=coords.dtype)
485
+
486
+ patch = su + size_map * sv
487
+ coeff_row = system.cmap_offset[tc] + patch
488
+ coeff = system.cmap_coeff[coeff_row].to(dtype=coords.dtype).reshape(-1, 4, 4)
489
+ _, e_da, e_db, e_daa, e_dbb, e_dab = _cmap_patch_derivatives(coeff, da, db)
490
+
491
+ inv_delta = 1.0 / delta
492
+ inv_delta2 = inv_delta * inv_delta
493
+ e_phi = e_da * inv_delta
494
+ e_psi = e_db * inv_delta
495
+ e_phiphi = e_daa * inv_delta2
496
+ e_psipsi = e_dbb * inv_delta2
497
+ e_phipsi = e_dab * inv_delta2
498
+
499
+ local_h = (
500
+ e_phiphi.unsqueeze(-1).unsqueeze(-1) * _outer(phi_g, phi_g)
501
+ + e_psipsi.unsqueeze(-1).unsqueeze(-1) * _outer(psi_g, psi_g)
502
+ + e_phipsi.unsqueeze(-1).unsqueeze(-1) * (_outer(phi_g, psi_g) + _outer(psi_g, phi_g))
503
+ + e_phi.unsqueeze(-1).unsqueeze(-1) * phi_h
504
+ + e_psi.unsqueeze(-1).unsqueeze(-1) * psi_h
505
+ )
506
+ local_h = 0.5 * (local_h + local_h.transpose(-1, -2))
507
+
508
+ atom_idx = torch.stack((ic, jc, kc, lc, mc), dim=1)
509
+ dof_idx = _local_dof_indices(atom_idx, active_map)
510
+ _scatter_local_hessian(h2, local_h, dof_idx)
511
+ return n
512
+
513
+
514
+ def build_analytical_hessian(
515
+ system: AmberSystem,
516
+ coords: torch.Tensor,
517
+ active_atoms: Sequence[int],
518
+ *,
519
+ mpi_rank: int = 0,
520
+ mpi_size: int = 1,
521
+ ) -> tuple[torch.Tensor, Dict[str, int]]:
522
+ """Build analytical Hessian without autograd Jacobian/JVP/Hessian.
523
+
524
+ Term coverage:
525
+ - Bond: closed-form
526
+ - Angle: closed-form (manual second-order chain rule)
527
+ - Dihedral: closed-form (manual second-order chain rule)
528
+ - CMAP: closed-form (manual second-order chain rule + bicubic patch derivatives)
529
+ - Nonbonded + 1-4 (including NB_INDEX<0 HB 12-10): closed-form
530
+ """
531
+ coords_work = coords.detach()
532
+ system_work = system.to(device=coords_work.device, dtype=coords_work.dtype)
533
+
534
+ active_list = [int(a) for a in active_atoms]
535
+ if not _has_native_analytical_ext():
536
+ raise RuntimeError(
537
+ "Native analytical Hessian extension is required. "
538
+ "Automatic torch fallback has been removed."
539
+ )
540
+ active_map = _make_active_map(system_work.natom, active_list, device=coords_work.device)
541
+ ndof = int(3 * len(active_list))
542
+ h2 = torch.zeros((ndof, ndof), dtype=coords_work.dtype, device=coords_work.device)
543
+ nbond = _add_bond_hessian_closed_form(
544
+ h2=h2,
545
+ system=system_work,
546
+ coords=coords_work,
547
+ active_map=active_map,
548
+ rank=int(mpi_rank),
549
+ size=int(mpi_size),
550
+ )
551
+ nangle = _add_angle_hessian_closed_form(
552
+ h2=h2,
553
+ system=system_work,
554
+ coords=coords_work,
555
+ active_map=active_map,
556
+ rank=int(mpi_rank),
557
+ size=int(mpi_size),
558
+ )
559
+ ndihed = _add_dihedral_hessian_closed_form(
560
+ h2=h2,
561
+ system=system_work,
562
+ coords=coords_work,
563
+ active_map=active_map,
564
+ rank=int(mpi_rank),
565
+ size=int(mpi_size),
566
+ )
567
+ ncmap = _add_cmap_hessian_closed_form(
568
+ h2=h2,
569
+ system=system_work,
570
+ coords=coords_work,
571
+ active_map=active_map,
572
+ rank=int(mpi_rank),
573
+ size=int(mpi_size),
574
+ )
575
+ n_nb = _add_nonbonded_pairset_closed_form(
576
+ h2=h2,
577
+ system=system_work,
578
+ coords=coords_work,
579
+ active_map=active_map,
580
+ pair_i=system_work.pair_i,
581
+ pair_j=system_work.pair_j,
582
+ inv_scee=None,
583
+ inv_scnb=None,
584
+ rank=int(mpi_rank),
585
+ size=int(mpi_size),
586
+ )
587
+ n_14 = _add_nonbonded_pairset_closed_form(
588
+ h2=h2,
589
+ system=system_work,
590
+ coords=coords_work,
591
+ active_map=active_map,
592
+ pair_i=system_work.pair14_i,
593
+ pair_j=system_work.pair14_j,
594
+ inv_scee=system_work.pair14_inv_scee,
595
+ inv_scnb=system_work.pair14_inv_scnb,
596
+ rank=int(mpi_rank),
597
+ size=int(mpi_size),
598
+ )
599
+
600
+ meta = {
601
+ "bond_pairs_used": int(nbond),
602
+ "angle_terms_used": int(nangle),
603
+ "dihedral_terms_used": int(ndihed),
604
+ "cmap_terms_used": int(ncmap),
605
+ "nonbond_pairs_used": int(n_nb),
606
+ "nonbond14_pairs_used": int(n_14),
607
+ "analytical_force_evals": 0,
608
+ }
609
+ return h2, meta
@@ -0,0 +1,46 @@
1
+ """Shared constants and validation utilities for hessian_ff."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import math
6
+
7
+ import torch
8
+
9
+ # Coulomb constant for kcal/mol when charges are in elementary charge units
10
+ # and distance in Angstrom.
11
+ COULOMB_K = 332.0637132991921
12
+
13
+ TWO_PI = 2.0 * math.pi
14
+
15
+
16
+ def validate_coords(
17
+ coords: torch.Tensor,
18
+ natom: int,
19
+ expected_dtype: torch.dtype,
20
+ expected_device: torch.device,
21
+ *,
22
+ label: str = "coords",
23
+ ) -> None:
24
+ """Validate coordinate tensor shape, dtype, and device."""
25
+ if not torch.is_tensor(coords):
26
+ raise TypeError(f"{label} must be torch.Tensor, got {type(coords)!r}")
27
+ if coords.ndim != 2:
28
+ raise ValueError(f"{label} must have shape [N,3], got ndim={coords.ndim}")
29
+ if int(coords.shape[0]) != int(natom) or int(coords.shape[1]) != 3:
30
+ raise ValueError(
31
+ f"{label} shape mismatch: expected [{natom},3], got {tuple(coords.shape)}"
32
+ )
33
+ if not coords.is_floating_point():
34
+ raise TypeError(f"{label} must be floating tensor, got dtype={coords.dtype}")
35
+ if coords.dtype != expected_dtype:
36
+ raise ValueError(
37
+ f"{label} dtype mismatch: "
38
+ f"got {coords.dtype}, expected {expected_dtype}. "
39
+ "Use matching precision for system and coordinates."
40
+ )
41
+ if coords.device != expected_device:
42
+ raise ValueError(
43
+ f"{label} device mismatch: "
44
+ f"got {coords.device}, expected {expected_device}. "
45
+ "Move coordinates to the same device as the force-field parameters."
46
+ )