In this tutorial, we will show how to create a simple state updater on an example of the steepest descent minimization algorithm. State updaters are used to update the state of a set of atoms.
State updater is a part of the simulation engine in SAMSON. They can be used to implement integrators (e.g. Verlet) or optimizers (e.g. the steepest descent algorithm).
Inside a simulator, the state updater and the interaction model share the same dynamical model. Each time the simulator requires an update through the updateState
method, the state updater moves the atoms in the dynamical model according to the forces given by the interaction model.
The state updater allows you to retrieve and modify position of particles (via SBParticleSystem::getPosition and SBParticleSystem::setPosition functions) and momentum of particles (via SBParticleSystem::getMomentum and SBParticleSystem::setMomentum functions); this is done via the associated dynamical model.
The source code for an Extension created in this tutorial can be found at https://github.com/1A-OneAngstrom/SAMSON-Developer-Tutorials.
We will start by creating a new SAMSON Extension called SteepestDescent thanks to the SAMSON Extension generator (please, refer to the SAMSON Extension generator tutorial for a reminder on how to use it):
SESteepestDescentStateUpdater
);The SAMSON Extension generator generates a state updater class (derived from the SBSStateUpdaterParticleSystem class) and a property widget class for the state updater.
SAMSON provides the steepest descent state updater with its State Updater pack, to distinguish we add "DevTutorial" in the description of the Extension.
Open the header file of your state updater descriptor (SESteepestDescentStateUpdaterDescriptor.hpp) and modify the class description:
Do the same for the property window descriptor (SESteepestDescentStateUpdaterPropertiesDescriptor.hpp). Modify the getName
function of the property window (SESteepestDescentStateUpdaterProperties.cpp) to have a user-friendly description of your interaction model inside SAMSON:
And modify the constructor for the state updater class (class: SESteepestDescentStateUpdater
, file: SESteepestDescentStateUpdater.cpp) by modifying an argument for the setName
function as follows:
Now, try building the SAMSON Extension.
Start SAMSON and open any file containing atoms or add atoms in the data graph, then create a new simulator (Simulation > Add simulator or ctrl / Cmd⌘ + shift + M ): your new Extension should be in the list of the available state updaters.
Let's first try to move atoms using the state updater by displacing their position. Note: In this example we will not be taking the forces obtained with an interaction model into account, we will be changing positions of atoms by a constant value.
Implement the method called updateState in the state updater (file SESteepestDescentStateUpdater.cpp). SAMSON Extension generator provides some commented code that allows you to implement a steepest descent if you uncomment it. Leave this code commented for the moment.
In order to move atoms we need to modify their positions. For that, we first get the number of atoms that are inside of the dynamical model (associated with the state updater).
We get the number of iterations per an interactive simulation step:
Note: The state updater Extension generated with the SAMSON Extension generator should already has a standard property window implemented that provides the user the possibility to modify the timestep and the number of interactive simulation steps.
Now we can iterate over the steps and atoms in the dynamical model. For each particle, we retrieve its position, add a displacement to it, and set the new position.
Now, build the Extension and launch SAMSON. Load or create any molecule and add a new simulator (Simulation > Add simulator or ctrl / Cmd⌘ + shift + M ) with any interaction model and with this state updater. Run the simulation (Simulation > Start simulation or Space or click on the Play icon) and the atoms will be shifted on the x-axis. Try modifying the number of steps (iterations per an interactive simulation step).
Let's now implement the steepest descent minimization algorithm. For that, in the updateState
function of the state updater we change the loop from the previous section to the following code:
Here, we first ask the interaction model to update the interactions (compute forces and energy), then we update positions of particles according to the computed forces.
Now you should have the basic implementation of the steepest descent minimization algorithm. Build the Extension and launch SAMSON. Load or create any molecule and add a new simulator with this state updater and with, for example, the springs interaction model. Run the simulation (Simulation > Start simulation or Space or click on the Play icon) and try slighlty pulling an atom.