Modifying external forces at runtime
While it is not yet possible to write external forces entirely in Python, with oxpy you can change the parameters of some external forces while the simulation is running. The list of the available forces (and the parameters that can be changed) can be found here. All external forces can be accessed by using ConfigInfo
’s forces
attribute.
Here is an example that can be run in the examples/TRAPS
folder which doubles the stiffness of all forces every 20000 simulation steps:
import oxpy
with oxpy.Context():
manager = oxpy.OxpyManager("inputMD")
for i in range(10):
manager.run(20000)
# double the stiffness of all forces after every iteration
for force in manager.config_info().forces:
force.stiff *= 2
Note that you can modify only some specific forces by using the id
and group_name
options in the external forces file.
For instance, imagine we have the following forces defined in the external forces file (similar to the external.conf
file
of the examples/TRAPS
folder):
{
type = trap
id = first_force
group_name = pulling_forces
particle = 0
stiff = 1.00
pos0 = 46.3113780977, 11.1604626391, 26.8730311801
rate = 0.
dir = 0.,0.,-1.
}
{
type = trap
id = last_force
group_name = pulling_forces
particle = 99
pos0 = 83.1532046751, 15.950789638, 37.3071701142
stiff = 1.0
rate = 0.
dir = 0.,0.,1.
}
We can now access only these two forces by using BaseForce
’s group_name
attribute:
for force in manager.config_info().forces:
if force.group_name == "pulling_forces":
force.stiff *= 2
or we can access one specific force by using ConfigInfo
’s get_force_by_id
:
my_force = manager.config_info().get_force_by_id("first_force")
my_force.stiff *= 2
Note
The id
and group_name
attributes can also be changed from the Python’s side!