PLUMED Biasing in AMS Molecular Dynamics#
This example shows how to run AMS molecular dynamics with AMS restraints, the AMS EngineAddOn WallPotential, and PLUMED restraints.
In the demonstration, the reaction H2CO3(g) -> H2O(g) + CO2(g) is encouraged by a PLUMED MovingRestraint between one hydrogen and one oxygen atom, while the WallPotential keeps the product molecules close to each other.
Initial imports#
from scm.base import ChemicalSystem, RKFTrajectory
from scm.inputs import ADF, AMS, ReaxFF
from scm.plams import AMSJob, Settings, plot_image_grid, view
import os
import numpy as np
import matplotlib.pyplot as plt
Initial system#
Define a ChemicalSystem from the coordinates and show the molecule.
O(3) is the right-most O atom
H(6) is the left-most H atom
mol = ChemicalSystem(
"""\
System
Atoms
O -0.1009275285 1.5113007791 -0.4061554537
C 0.0189044656 0.3835929386 0.1570043855
O 1.2796450751 -0.2325516597 0.3936038789
O -1.0798994361 -0.4640886294 0.4005134306
H 1.7530114719 -0.6822230417 -0.3461237499
H -1.8707340481 -0.5160303870 -0.1988424913
End
End
"""
)
view(mol, guess_bonds=True, width=200, height=200)
Calculation settings#
current_O3H6 = mol.get_distance(2, 5)
target_O3H6 = 0.95
print(
f"Scanning bond O3-H6 from {current_O3H6:.3f} to {target_O3H6:.3f} angstrom (this will form a water molecule)"
)
current_O1C2 = mol.get_distance(0, 1)
print(f"Restraining bond O1-C2 at {current_O1C2:.3f} angstrom")
Scanning bond O3-H6 from 3.218 to 0.950 angstrom (this will form a water molecule)
Restraining bond O1-C2 at 1.266 angstrom
nsteps = 10000 # number of MD steps
kappa = 500000.0 # strength of Plumed MovingRestraint
driver = AMS(Task="MolecularDynamics", Engine=ReaxFF(ForceField="CHO.ff"))
driver.MolecularDynamics.NSteps = nsteps
driver.MolecularDynamics.Trajectory.SamplingFreq = 100
driver.MolecularDynamics.InitialVelocities.Temperature = 200
driver.MolecularDynamics.Thermostat = [
{"Temperature": 500, "Tau": 100, "Type": "Berendsen"}
]
# Use an AMS restraint for one of the C-O bond lengths.
driver.Restraints.Distance = [f"1 2 {current_O1C2} 1.0"]
# Keep the product molecules within a sphere of radius 4 angstrom.
driver.EngineAddons.WallPotential.Enabled = True
driver.EngineAddons.WallPotential.Radius = 4.0
# PLUMED uses nanometers for distances, hence the factor of 0.1.
driver.MolecularDynamics.Plumed.Input = f"""
DISTANCE ATOMS=3,6 LABEL=d36
MOVINGRESTRAINT ARG=d36 STEP0=1 AT0={current_O3H6 * 0.1} KAPPA0={kappa} STEP1={nsteps} AT1={target_O3H6 * 0.1}
PRINT ARG=d36 FILE=colvar-d36.dat STRIDE=20
"""
s = Settings()
s.input = driver
s.runscript.nproc = 1
s.runscript.preamble_lines = ["export OMP_NUM_THREADS=1"]
job = AMSJob(settings=s, molecule=mol, name="dissociating-carbonic-acid")
print(job.get_input())
Task MolecularDynamics
Engine ReaxFF
ForceField CHO.ff
EndEngine
EngineAddons
WallPotential
Enabled True
Radius 4.0
End
End
... output trimmed ....
System
Atoms
O -0.1009275285 1.5113007791 -0.4061554537
C 0.0189044656 0.38359293859999993 0.1570043855
O 1.2796450751 -0.2325516597 0.3936038789
O -1.0798994361 -0.4640886294 0.4005134306
H 1.7530114719 -0.6822230417 -0.3461237499
H -1.8707340481 -0.516030387 -0.1988424913
End
End
Run the job#
job.run();
[05.11|16:08:32] JOB dissociating-carbonic-acid STARTED
[05.11|16:08:32] JOB dissociating-carbonic-acid RUNNING
[05.11|16:08:34] JOB dissociating-carbonic-acid FINISHED
[05.11|16:08:34] JOB dissociating-carbonic-acid SUCCESSFUL
Analyze the trajectory#
Extract the O3H6 distances at each stored frame, and plot some of the molecules
trajectory = RKFTrajectory(job.results.rkfpath())
every = 20 # picture every 20 frames in the trajectory
O3H6_distances = []
imgs = {}
for i, it in enumerate(trajectory, 1):
O3H6_distances.append(it.system.get_distance(2, 5))
if i % every == 1:
imgs[f"frame {it.frame_number}"] = view(it.system, width=200, height=200, guess_bonds=True)
plot_image_grid(imgs, rows=1);
The above pictures show how the H(6) approaches the O(3). At the end, the carbonic acid molecule has dissociated into CO2 and H2O.
fig, ax = plt.subplots()
ax.plot(O3H6_distances)
ax.set_ylabel("Distance (angstrom)")
ax.set_xlabel("Frame")
ax.set_title("O3-H6 distance")
ax;
energies = job.results.get_history_property("Energy")
fig, ax = plt.subplots()
ax.plot(energies)
ax.set_ylabel("Energy (hartree)")
ax;
A transition state search#
PLAMS makes it easy to extract any frame from an MD trajectory. As an example, let’s use highest-energy frame as an initial structure for a transition state search with the ADF DFT engine.
index = np.argmax(energies) + 1
approximate_ts_system = job.results.get_history_system(index)
print(f"Using frame {index} as initial approximate transition state:")
view(approximate_ts_system, width=300, height=300, guess_bonds=True)
Using frame 83 as initial approximate transition state:
ts_s = AMS(Task="TransitionStateSearch", Engine=ADF())
ts_s.GeometryOptimization.InitialHessian.Type = "Calculate"
ts_s.Properties.NormalModes = True
ts_s.Engine.XC.GGA = "PBE"
ts_job = AMSJob(settings=ts_s, molecule=approximate_ts_system, name="ts-search")
ts_job.run();
[05.11|16:08:36] JOB ts-search STARTED
[05.11|16:08:36] JOB ts-search RUNNING
[05.11|16:09:01] JOB ts-search FINISHED
[05.11|16:09:02] JOB ts-search SUCCESSFUL
print("Optimized transition state:")
view(ts_job.results.get_main_system(), width=300, height=300, guess_bonds=True)
Optimized transition state:
print("Frequencies (at a TS there should be 1 imaginary [given as negative])")
for f in ts_job.results.get_frequencies():
print(f"{f:.3f} cm^-1")
Frequencies (at a TS there should be 1 imaginary [given as negative])
-1426.237 cm^-1
319.492 cm^-1
367.126 cm^-1
544.900 cm^-1
704.804 cm^-1
745.325 cm^-1
877.287 cm^-1
1080.895 cm^-1
1123.433 cm^-1
1759.722 cm^-1
2064.987 cm^-1
3471.895 cm^-1
See also#
Python Script#
#!/usr/bin/env python
# coding: utf-8
# ## Initial imports
from scm.base import ChemicalSystem, RKFTrajectory
from scm.inputs import ADF, AMS, ReaxFF
from scm.plams import AMSJob, Settings, plot_image_grid, view
import os
import numpy as np
import matplotlib.pyplot as plt
# ## Initial system
#
# Define a ChemicalSystem from the coordinates and show the molecule.
#
# * O(3) is the right-most O atom
# * H(6) is the left-most H atom
mol = ChemicalSystem(
"""\
System
Atoms
O -0.1009275285 1.5113007791 -0.4061554537
C 0.0189044656 0.3835929386 0.1570043855
O 1.2796450751 -0.2325516597 0.3936038789
O -1.0798994361 -0.4640886294 0.4005134306
H 1.7530114719 -0.6822230417 -0.3461237499
H -1.8707340481 -0.5160303870 -0.1988424913
End
End
"""
)
view(mol, guess_bonds=True, width=200, height=200, picture_path="picture1.png")
# ## Calculation settings
current_O3H6 = mol.get_distance(2, 5)
target_O3H6 = 0.95
print(
f"Scanning bond O3-H6 from {current_O3H6:.3f} to {target_O3H6:.3f} angstrom (this will form a water molecule)"
)
current_O1C2 = mol.get_distance(0, 1)
print(f"Restraining bond O1-C2 at {current_O1C2:.3f} angstrom")
nsteps = 10000 # number of MD steps
kappa = 500000.0 # strength of Plumed MovingRestraint
driver = AMS(Task="MolecularDynamics", Engine=ReaxFF(ForceField="CHO.ff"))
driver.MolecularDynamics.NSteps = nsteps
driver.MolecularDynamics.Trajectory.SamplingFreq = 100
driver.MolecularDynamics.InitialVelocities.Temperature = 200
driver.MolecularDynamics.Thermostat = [
{"Temperature": 500, "Tau": 100, "Type": "Berendsen"}
]
# Use an AMS restraint for one of the C-O bond lengths.
driver.Restraints.Distance = [f"1 2 {current_O1C2} 1.0"]
# Keep the product molecules within a sphere of radius 4 angstrom.
driver.EngineAddons.WallPotential.Enabled = True
driver.EngineAddons.WallPotential.Radius = 4.0
# PLUMED uses nanometers for distances, hence the factor of 0.1.
driver.MolecularDynamics.Plumed.Input = f"""
DISTANCE ATOMS=3,6 LABEL=d36
MOVINGRESTRAINT ARG=d36 STEP0=1 AT0={current_O3H6 * 0.1} KAPPA0={kappa} STEP1={nsteps} AT1={target_O3H6 * 0.1}
PRINT ARG=d36 FILE=colvar-d36.dat STRIDE=20
"""
s = Settings()
s.input = driver
s.runscript.nproc = 1
s.runscript.preamble_lines = ["export OMP_NUM_THREADS=1"]
job = AMSJob(settings=s, molecule=mol, name="dissociating-carbonic-acid")
print(job.get_input())
# ## Run the job
job.run();
# ## Analyze the trajectory
#
# Extract the O3H6 distances at each stored frame, and plot some of the molecules
trajectory = RKFTrajectory(job.results.rkfpath())
every = 20 # picture every 20 frames in the trajectory
O3H6_distances = []
imgs = {}
for i, it in enumerate(trajectory, 1):
O3H6_distances.append(it.system.get_distance(2, 5))
if i % every == 1:
imgs[f"frame {it.frame_number}"] = view(it.system, width=200, height=200, guess_bonds=True)
plot_image_grid(imgs, rows=1, save_path="picture2.png");
# The above pictures show how the H(6) approaches the O(3). At the end, the carbonic acid molecule has dissociated into CO2 and H2O.
fig, ax = plt.subplots()
ax.plot(O3H6_distances)
ax.set_ylabel("Distance (angstrom)")
ax.set_xlabel("Frame")
ax.set_title("O3-H6 distance")
ax;
ax.figure.savefig("picture3.png")
energies = job.results.get_history_property("Energy")
fig, ax = plt.subplots()
ax.plot(energies)
ax.set_ylabel("Energy (hartree)")
ax;
ax.figure.savefig("picture4.png")
# ## A transition state search
#
# PLAMS makes it easy to extract any frame from an MD trajectory. As an example, let's use highest-energy frame as an initial structure for a transition state search with the ADF DFT engine.
index = np.argmax(energies) + 1
approximate_ts_system = job.results.get_history_system(index)
print(f"Using frame {index} as initial approximate transition state:")
view(approximate_ts_system, width=300, height=300, guess_bonds=True, picture_path="picture5.png")
ts_s = AMS(Task="TransitionStateSearch", Engine=ADF())
ts_s.GeometryOptimization.InitialHessian.Type = "Calculate"
ts_s.Properties.NormalModes = True
ts_s.Engine.XC.GGA = "PBE"
ts_job = AMSJob(settings=ts_s, molecule=approximate_ts_system, name="ts-search")
ts_job.run();
print("Optimized transition state:")
view(ts_job.results.get_main_system(), width=300, height=300, guess_bonds=True, picture_path="picture6.png")
print("Frequencies (at a TS there should be 1 imaginary [given as negative])")
for f in ts_job.results.get_frequencies():
print(f"{f:.3f} cm^-1")