Yes, you can do this using a hook function, in this case a 'post-step-hook', because you want to modify the forces acting on the partticles (see
http://docs.quantumwise.com/manuals/Types/MolecularDynamics/MolecularDynamics.html#moleculardynamics-f)
First you set up your MD simulation as ususal. Send the simulation to the Editor to get the python script.
You need to modify your script as shown in the attached example script.
First you need to specify the charges of the atoms.
If you are using a potential that uses electrostatics, you can use the partial charges defined in the potential by using the PartialCharges analysis object.
However, you can also define any other arbitrary partial charges on the atoms.
partial_charges = PartialCharges(bulk_configuration).evaluate()
# If the potential does not define partial charges, you can define charges yourself.
# Note that they only will be used to interact with the external electric field.
# e.g. partial_charges = [0.5, 0.5, -0.5, -0.5, ...]*elementary_charge
Then you need to define an electric field, which has the units Volt/Meter or Volt/Ang, etc.
# Define the electric field.
field = [0.0, 0.0, 1.0]*Volt/nm
Then you need to define a hook class, which adds the forces due to the electric field i.e. F=q*E to the vector of atomic forces.
Create an object of this class that will be passed to the MolecularDynamics function.
class ElectricFieldHook(object):
def __init__(self, charges, electric_field):
# Store the charges and the electric field.
charges = charges.reshape(-1, 1)
field = electric_field.reshape(1, 3)
self.efield_forces = (charges*field).convertTo(eV/Ang)
def __call__(self, step, time, configuration, forces, stress):
# Add the electric field forces to the forces vector.
forces += self.efield_forces
electric_field_hook = ElectricFieldHook(
charges=partial_charges,
electric_field=field
)
This hook object needs to be passed as post_step_hook to the MolecularDynamics function.
md_trajectory = MolecularDynamics(
bulk_configuration,
constraints=[],
trajectory_filename='efield_trajectory.nc',
steps=2000,
log_interval=20,
post_step_hook=electric_field_hook,
method=method
)