In this tutorial, we show how to implement a simple exploration method based on a Metropolis Monte Carlo approach.
Note: This tutorial shows a simplified implementation.
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 MonteCarlo thanks to the SAMSON Extension generator (please, refer to the SAMSON Extension generator tutorial for a reminder on how to use it):
SEMonteCarloStateUpdater
);The SAMSON Extension generator generates a state updater class (derived from the SBSStateUpdaterParticleSystem class) and a property widget class for the state updater.
Open the header file of your state updater descriptor (file: SEMonteCarloStateUpdaterDescriptor.hpp) and modify the class description:
Do the same for the property window descriptor (file: SEMonteCarloStateUpdaterPropertiesDescriptor.hpp). Modify the getName
function of the property window (file: SEMonteCarloStateUpdaterProperties.cpp) to have a user-friendly description of your interaction model inside SAMSON:
And modify the constructor for the state updater class (class: SEMonteCarloStateUpdater
, file: SEMonteCarloStateUpdater.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.
Open the state updater header file (SEMonteCarloStateUpdaterProperties.hpp) and include the SBRandom.hpp header:
Add the following private variables to the state updater class definition (class: SEMonteCarloStateUpdater
, file: SEMonteCarloStateUpdaterProperties.hpp):
We initialize some of these variables in the constructor of the state updater class (class: SEMonteCarloStateUpdater
, file: SEMonteCarloStateUpdaterProperties.cpp):
Note: We set the seed for the random number generator based on the current SAMSON time. If you want to get reproductive results (e.g. for Debugging purposes) you can set the seed to 0 or any other constant.
Now, let's implement the updateState
function of the state updater class (class: SEMonteCarloStateUpdater
, file: SEMonteCarloStateUpdaterProperties.cpp).
First, we get the number of particles in the particle system:
We request the interaction model to update the interactions and get the energy of the particle system, which then will be used in the algorithm. Since we do not require forces we ask the interaction model to flush the force buffer.
We randomly determine the list of particles that will be moved by the algorithm:
We remember the current positions of these particles and compute the new ones based on a random displacement normalized by the maximum possible displacement:
We request the interaction model to update the interactions and get the new energy of the particle system. If the new energy is less then the current one, we accept all the displacements, else we reject the move for each displaced particle based on the probability.
After the certain number of trials (e.g. 100), we decide whether to increase or to decrease the maximum possible displacement for a particle based on the ratio of the number of rejects to the number of trials; and we reset the number of trials and rejects.
Now, you should have the basic implementation of a simple exploration method based on a Metropolis Monte Carlo approach. You can build the Extension, 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.
But for now, we have not exposed the possibility for the user to modify parameters of the method (e.g. temperature and number of moving particles). Let's do this in the Exposing parameters section.
We want to make it possible for the user to modify temperature and the number of moving particles. For that we will need to modify both state updater and state updater property window classes (SEMonteCarloStateUpdater
and SEMonteCarloStateUpdaterProperties
classes).
First, let's create setter functions in the state updater class (class: SEMonteCarloStateUpdater
, file: SEMonteCarloStateUpdater.hpp)
We implement these functions as follows (with checks for negative values):
We check for the maximum number of particles in the updateState
function of the state updater.
Let's now modify the property window class.
In the current GUI of the Extension we have the step size and the number of steps which we do not actually use in our state updater, we will modify these to allow the user to change the temperature and the number of moving particles. You might need to modify the maximum size of the widget and lables.
Modify the double spin box: change its name to doubleSpinBoxTemperature
, modify the default and maximum value, and the single step size as follows
Modify the spin box: change its name tp spinBoxNumberOfMovingParticles
modify the default and maximum value, and the single step size as follows
Add two slots: onTemperatureChanged(double)
and onNumberOfMovingParticlesChanged(int)
:
And connect signal from two spin boxes to these slots:
Add these public slots to the state updater property window class declaration (class: SEMonteCarloStateUpdaterProperties
, file: SEMonteCarloStateUpdaterProperties.hpp):
And implement these functions as follows (file: SEMonteCarloStateUpdaterProperties.cpp):
Modify the loadSettings
and loadSettings
functions to save the GUI state from one session to another:
In both setup
functions of the state updater property window class (class: SEMonteCarloStateUpdaterProperties
) modify the setting of steps size and number of steps to set temperature and number of moving particles as follows:
Now, when the user changes temperature or the number of moving particles in the property window it will automaticaly use the new parameters in the updateState
function of the state updater.
Build the Extension, 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.