Interfaces#
Atomic Simulation Environment#
We provide a calculator
for Atomic Simulation Environment (ASE) that enables many routines such as optimization and molecular dynamics.
Examples#
Periodic box optimization#
import os
import numpy as np
from ase import Atoms
from ase.optimize import BFGS
from reptar.utils import parse_xyz
from reptar.writers import write_xyz
from mbgdml.interfaces.ase import mbeCalculator
from mbgdml.mbe import mbePredict
from mbgdml.models import gdmlModel
from mbgdml.periodic import Cell
from mbgdml.predictors import predict_gdml
from mbgdml.descriptors import Criteria, com_distance_sum
from mbgdml.utils import get_entity_ids, get_comp_ids, atoms_by_number
# Ensures we execute from script directory (for relative paths).
WORK_DIR = os.path.dirname(os.path.realpath(__file__))
os.chdir(WORK_DIR)
# Conversion factors.
HARTREE_TO_KCALMOL = 627.5094737775374055927342256 # Psi4 constant
HARTREE_TO_EV = 27.21138602 # Psi4 constant
EV_TO_KCALMOL = HARTREE_TO_KCALMOL / HARTREE_TO_EV
KCALMOL_TO_EV = HARTREE_TO_EV / HARTREE_TO_KCALMOL
print("Setting up system")
XYZ_PATH = "./33h2o.xyz"
Z, _, R = parse_xyz(XYZ_PATH)
Z = atoms_by_number(Z[0])
Z = np.array(Z)
R = np.array(R)
# Eventually we will make an ASE Atoms object that requires only a single structure.
if R.ndim == 3:
R = R[-1]
# Setup unit cell
BOX_LENGTH = 10.0 # Angstroms
MIC_CUTOFF = 5.0 # Maximum value is half of the box length
cell_v = [[BOX_LENGTH, 0.0, 0.0], [0.0, BOX_LENGTH, 0.0], [0.0, 0.0, BOX_LENGTH]]
periodic_cell = Cell(cell_v, pbc=True, cutoff=MIC_CUTOFF)
# Setup fragments
N_MOLECULES = 33
entity_ids = get_entity_ids(atoms_per_mol=3, num_mol=N_MOLECULES)
comp_ids = get_comp_ids(label="h2o", num_mol=N_MOLECULES)
# Setup ray.
# Note, this assumes you already ran `ray start --head` on your system's terminal.
N_WORKERS = 8 # Generally the number of CPU cores to use.
print("Setting up mbGDML")
model_paths = [
"./1h2o-model-gdml.npz",
"./2h2o-model-gdml-nbody.npz",
"./3h2o-model-gdml-nbody.npz",
]
model_comp_ids = [["h2o"], ["h2o", "h2o"], ["h2o", "h2o", "h2o"]]
model_desc_kwargs = (
{"entity_ids": get_entity_ids(atoms_per_mol=3, num_mol=1)},
{"entity_ids": get_entity_ids(atoms_per_mol=3, num_mol=2)},
{"entity_ids": get_entity_ids(atoms_per_mol=3, num_mol=3)},
)
model_desc_cutoffs = (None, 6.0, 10.0)
model_criteria = [
Criteria(com_distance_sum, desc_kwargs, cutoff)
for desc_kwargs, cutoff in zip(model_desc_kwargs, model_desc_cutoffs)
]
models = [
gdmlModel(path, comp_ids=model_comp_id, criteria=criteria)
for path, model_comp_id, criteria in zip(
model_paths, model_comp_ids, model_criteria
)
]
mbe_pred = mbePredict(
models, predict_gdml, use_ray=True, n_workers=N_WORKERS,
periodic_cell=periodic_cell
)
print("Setting up ASE")
ase_atoms = Atoms(numbers=Z, positions=R, cell=cell_v, pbc=True)
# Attach ASE calculator
mbe_calc = mbeCalculator(mbe_pred, e_conv=KCALMOL_TO_EV, f_conv=KCALMOL_TO_EV)
mbe_calc.directory = WORK_DIR
mbe_calc.set(entity_ids=entity_ids, comp_ids=comp_ids)
ase_atoms.calc = mbe_calc
# Setup logging
mbe_traj_path = os.path.join(WORK_DIR, "33h2o-opt.traj")
print("Starting optimization")
dyn = BFGS(atoms=ase_atoms, trajectory=mbe_traj_path)
dyn.run(fmax=0.4, steps=100)
print("Writing XYZ file")
Z = ase_atoms.get_atomic_numbers()
R = ase_atoms.get_positions()
write_xyz(os.path.join(WORK_DIR, "33h2o-opt.xyz"), Z, R[None, ...])
print("Done!")
Setting up system
Setting up mbGDML
2022-12-29 10:35:57,757 INFO worker.py:1342 -- Connecting to existing Ray cluster at address: 10.0.0.77:6379...
2022-12-29 10:35:57,764 INFO worker.py:1528 -- Connected to Ray cluster.
Setting up ASE
Starting optimization
Step Time Energy fmax
BFGS: 0 10:36:02 -68517.913397 3.5472
BFGS: 1 10:36:04 -68519.616345 2.8398
BFGS: 2 10:36:05 -68524.450556 2.0596
BFGS: 3 10:36:06 -68527.262168 3.1269
BFGS: 4 10:36:07 -68528.911255 2.3024
BFGS: 5 10:36:09 -68530.098907 1.8428
BFGS: 6 10:36:10 -68531.107443 2.6093
BFGS: 7 10:36:11 -68531.888864 2.6436
BFGS: 8 10:36:12 -68532.375126 1.6772
BFGS: 9 10:36:13 -68532.730120 1.7315
BFGS: 10 10:36:15 -68533.327400 1.0089
BFGS: 11 10:36:16 -68533.879248 1.3293
BFGS: 12 10:36:17 -68534.276709 1.6804
BFGS: 13 10:36:18 -68535.285333 1.4898
BFGS: 14 10:36:19 -68535.773468 1.1612
BFGS: 15 10:36:21 -68535.897528 1.2659
BFGS: 16 10:36:22 -68536.193563 1.0326
BFGS: 17 10:36:23 -68536.543744 0.7357
BFGS: 18 10:36:25 -68536.814560 1.3871
BFGS: 19 10:36:26 -68536.971200 1.0031
BFGS: 20 10:36:27 -68537.250519 1.1595
BFGS: 21 10:36:28 -68537.372511 1.2645
BFGS: 22 10:36:30 -68537.546845 1.1778
BFGS: 23 10:36:31 -68537.783378 1.8484
BFGS: 24 10:36:32 -68537.889268 2.0116
BFGS: 25 10:36:33 -68538.134710 0.8240
BFGS: 26 10:36:34 -68538.277066 0.7056
BFGS: 27 10:36:35 -68538.500159 0.9197
BFGS: 28 10:36:37 -68538.676092 1.2324
BFGS: 29 10:36:38 -68538.859638 0.8854
BFGS: 30 10:36:39 -68539.012425 0.5784
BFGS: 31 10:36:40 -68539.016547 0.6297
BFGS: 32 10:36:41 -68539.186143 0.6434
BFGS: 33 10:36:42 -68539.331553 0.4745
BFGS: 34 10:36:43 -68539.453231 0.9449
BFGS: 35 10:36:45 -68539.600969 0.8556
BFGS: 36 10:36:46 -68539.771981 0.8342
BFGS: 37 10:36:47 -68539.845176 0.7681
BFGS: 38 10:36:48 -68539.969053 0.5224
BFGS: 39 10:36:49 -68540.155790 0.6128
BFGS: 40 10:36:50 -68540.380225 0.6529
BFGS: 41 10:36:52 -68540.319302 0.5663
BFGS: 42 10:36:53 -68540.382694 0.7651
BFGS: 43 10:36:54 -68540.440462 0.7541
BFGS: 44 10:36:55 -68540.540893 0.8772
BFGS: 45 10:36:56 -68540.642185 0.4752
BFGS: 46 10:36:57 -68540.714986 1.2217
BFGS: 47 10:36:59 -68540.785903 0.9823
BFGS: 48 10:37:00 -68540.776744 0.7590
BFGS: 49 10:37:01 -68540.783109 0.6044
BFGS: 50 10:37:02 -68540.871331 0.4871
BFGS: 51 10:37:03 -68540.987260 0.5234
BFGS: 52 10:37:04 -68541.096528 0.6751
BFGS: 53 10:37:06 -68541.161885 0.6271
BFGS: 54 10:37:07 -68541.188346 0.5450
BFGS: 55 10:37:08 -68541.255231 0.6397
BFGS: 56 10:37:09 -68541.221991 0.4792
BFGS: 57 10:37:10 -68541.295788 0.4745
BFGS: 58 10:37:11 -68541.352476 0.5650
BFGS: 59 10:37:13 -68541.431386 0.5724
BFGS: 60 10:37:14 -68541.262401 0.7457
BFGS: 61 10:37:15 -68541.452363 0.6275
BFGS: 62 10:37:16 -68541.518779 0.4439
BFGS: 63 10:37:17 -68541.568480 0.3567
Writing XYZ file
Done!
99
Built with Packmol
O 4.448288 1.208086 2.746892
H 3.536319 0.983195 2.955685
H 4.512732 1.100818 1.792849
O 3.948733 1.316616 6.793027
H 3.853295 2.273996 6.806119
H 3.250261 1.010757 6.206132
O 8.520903 5.980787 8.300956
H 8.028530 5.491196 8.967086
H 7.856720 6.256644 7.661720
O 3.049999 7.894083 5.432018
H 3.435076 7.885144 6.313774
H 3.536801 7.220277 4.947388
O 5.809833 8.758480 8.132870
H 5.089620 9.004536 7.544135
H 5.378026 8.335505 8.881531
O 8.959022 3.799450 7.522505
H 8.105949 4.116766 7.210358
H 9.085442 2.958855 7.071643
O 6.721564 7.502117 2.659343
H 7.427039 7.328975 3.290362
H 5.947224 7.678085 3.202749
O 6.394610 5.178943 6.110874
H 5.559335 5.040733 5.653631
H 7.067268 4.938945 5.466056
O 5.896766 5.537198 8.219439
H 5.938502 6.136762 8.970864
H 6.029607 4.662834 8.598515
O 1.024728 3.675127 4.249228
H 1.003345 4.457946 4.808331
H 1.570091 3.927930 3.497877
O 8.331325 8.604727 7.490032
H 8.408019 8.327100 8.408128
H 8.943106 8.036109 7.012272
O 7.645795 2.445103 8.627870
H 8.006798 1.618302 8.962437
H 6.764088 2.497423 9.009594
O 1.012860 6.707380 3.907940
H 1.849672 6.236197 3.848027
H 0.985880 7.041051 4.810045
O 1.084508 1.394258 4.855807
H 1.918032 1.161958 4.434934
H 1.333853 1.960597 5.592656
O 8.159789 8.976462 1.691952
H 7.491096 8.999820 1.000459
H 8.996631 8.929985 1.219307
O 2.855454 5.441794 6.042908
H 2.200437 6.069425 6.363674
H 3.644715 5.628257 6.560745
O 1.114017 1.632434 2.395827
H 1.342919 1.006683 1.701638
H 1.006312 2.472781 1.939675
O 1.011581 8.889374 3.472575
H 1.546404 8.833852 2.674616
H 1.646378 9.034334 4.181009
O 4.188229 3.228318 4.365115
H 3.885257 3.724723 5.131696
H 4.095151 2.305409 4.620915
O 8.647352 6.535042 1.015165
H 9.010228 6.684375 1.893731
H 7.801287 6.104299 1.171665
O 1.020174 8.071019 8.119338
H 1.055278 8.326201 7.192241
H 1.048206 8.903140 8.601673
O 9.018138 6.527312 5.336616
H 8.117733 6.866508 5.345254
H 8.982312 5.757471 4.760492
O 7.337606 1.034847 6.955019
H 8.170080 1.054660 6.472884
H 6.661836 1.134434 6.277320
O 4.179361 5.717711 2.552372
H 3.333954 6.050423 2.235436
H 3.953449 5.145917 3.292557
O 3.645005 4.397988 8.472033
H 3.928850 5.095361 9.071167
H 3.373635 3.677386 9.049050
O 6.277071 2.534167 4.639107
H 7.162709 2.904082 4.707426
H 6.194422 2.270085 3.717539
O 4.668657 1.829959 8.805258
H 5.173153 1.037956 8.595323
H 3.783191 1.508985 9.002196
O 6.031231 4.470176 3.585363
H 6.031530 4.193076 2.663911
H 6.120551 5.427629 3.551246
O 1.586099 1.466764 8.924615
H 0.994265 2.161784 8.620414
H 1.835234 0.992591 8.125271
O 2.995187 7.565108 8.947375
H 2.925579 6.882351 8.272945
H 3.193377 8.370016 8.458805
O 1.610803 4.059269 7.707480
H 1.993036 3.406308 7.113007
H 0.945326 4.509693 7.178220
O 6.288949 7.378714 6.467604
H 6.605633 8.230985 6.152638
H 5.488556 7.213994 5.959572
O 8.406661 2.835502 2.118919
H 8.750038 3.656909 2.483939
H 8.229177 2.283705 2.886954
99
O 4.3365164306 1.3423312952 2.4680614887
H 3.6223666587 1.9200154683 2.7894458105
H 4.4922297106 1.6420587804 1.5642405023
O 3.9969205627 1.2148062826 6.2892190672
H 4.1438126772 1.3427232428 7.2465945190
H 3.0352304316 1.1416959865 6.1518635759
O 8.6759572739 6.1735198830 8.5773431809
H 9.2096899177 5.6095277977 9.1578604765
H 8.9229086087 5.8893750596 7.6831732226
O 3.2026433795 8.4389584990 5.3068891862
H 3.8229854890 9.1520199478 5.5201500055
H 3.7340433283 7.6314584852 5.3900613883
O 5.8951062061 8.9053249791 8.8505239550
H 6.0193388220 9.5917486916 8.1652709513
H 5.0955342055 8.4006311618 8.6070986121
O 8.4003957157 3.5776377262 6.3407474631
H 8.1014528557 3.0733467686 7.1154417969
H 8.5008245352 2.9199408920 5.6419479141
O 6.5154248729 7.4850903713 2.4569697808
H 6.6196064868 7.8607205816 3.3447962527
H 5.5577291786 7.4793661544 2.3129378375
O 5.9693745329 4.9388163589 6.2852134452
H 5.5642530821 4.9673213291 5.4047503522
H 6.8776003092 4.6100462597 6.1592096766
O 5.9521722963 5.0312227300 9.0785781509
H 6.5312041464 5.7779223864 9.2843914398
H 5.7344184890 5.1680393520 8.1403951222
O 0.4073601148 3.5306399912 4.3502353930
H 0.4167358633 4.4970246066 4.3091929571
H 1.2250438333 3.2557330003 3.9059767143
O 8.5038885662 8.8273101794 7.2947752720
H 8.2100506304 8.6434441319 8.1983181457
H 8.8295688561 7.9723899352 6.9746579019
O 7.9355078697 3.0899701546 9.1225894725
H 7.7622204981 2.4480894571 9.8209334541
H 7.1842056627 3.7111971524 9.1458955661
O 0.9807245450 6.2404889967 3.8325078149
H 1.9150770745 6.3251009239 3.5836390810
H 0.6847825042 7.1173886357 4.0966646454
O 1.5405666414 0.8813745076 5.1311522324
H 1.5662827044 1.2993620080 4.2505325519
H 0.9389943405 1.4311241596 5.6508655220
O 8.3071234657 9.6182639936 1.0340243249
H 7.6379382817 8.9458487511 1.2141068157
H 9.1129267902 9.2940976431 1.4552041768
O 3.1291845655 5.5160870138 6.1574778750
H 2.3605693539 6.0972385805 6.1694854411
H 3.5602550949 5.7357384155 7.0005546306
O 1.3664086071 1.8213545024 2.4078579857
H 1.5139918512 0.9242662587 2.0676713601
H 0.4874651642 2.0418742931 2.0672981717
O 1.3723547487 9.0422175991 2.7680102318
H 2.1525904042 8.5053693304 2.5828208659
H 1.5189291678 9.2920146249 3.6953894827
O 3.5371742075 3.3280609918 4.4038271320
H 3.3243579167 4.0311380493 5.0412252520
H 3.9074246788 2.6120513357 4.9581416489
O 9.2329506391 5.5885200400 1.5149386395
H 9.8337488079 5.9820197986 2.1634827702
H 8.5292970098 6.2361539998 1.3705969040
O 1.3887707607 8.8848474172 7.4710483637
H 1.7309420427 8.7542336962 6.5726565749
H 0.4223361446 8.9874117580 7.4188407656
O 8.8585235478 6.6324978613 5.6329327472
H 7.9142658363 6.8044033507 5.4412005754
H 9.0960483586 5.7974677740 5.2111021014
O 6.7432578402 0.8764668714 6.9874085641
H 7.6304507236 0.5004047643 6.9115472900
H 6.4866291421 1.1414478156 6.0860033937
O 3.6713866689 6.1259201054 2.6878740867
H 3.3839112705 5.7186780734 1.8552941913
H 4.2196347856 5.4458799893 3.1187221926
O 3.2513203992 4.8294200309 9.9325713334
H 4.1770565963 4.5599927885 9.8017203363
H 2.6916872210 4.2754902685 9.3746023213
O 6.6810968058 2.0729532918 4.2667336041
H 6.2285863656 2.9122742733 4.4237283979
H 6.0930677566 1.6431138869 3.6264127500
O 4.4012784913 1.3145924838 9.3979845760
H 4.9114737684 0.4857711909 9.5039783380
H 3.5018884783 1.1191794209 9.7141701270
O 6.3304732158 4.7798286989 3.1907307957
H 6.8244372117 4.0914433797 2.7106955176
H 6.6350347370 5.6434371837 2.8585034553
O 1.5535612173 1.1039274607 9.2665624347
H 1.5641604592 1.9398153721 8.7737998912
H 1.6381297019 0.3965081397 8.6065179365
O 3.5337792762 7.3532217571 8.5479758700
H 3.3750231720 6.7105496167 9.2605731833
H 2.7093517398 7.8679892216 8.4437662248
O 1.1885114676 3.5345922288 7.5497716810
H 1.7234369933 4.0098065215 6.9050397930
H 0.3230917141 3.4815064509 7.1259077138
O 6.2789113972 7.7137037236 5.7229220484
H 6.6784091843 8.3904157206 6.2988728539
H 6.0503415101 6.9817968075 6.3206044525
O 8.3557160006 2.8240939311 1.9732294490
H 8.8516177548 3.6509547288 1.8477623018
H 8.4158125805 2.6138864123 2.9219561606