Add a User Defined Function
This tutorial shows how to add a user defined function to access the thermostat of the system. This tutorial builds on the topics in Adding a Thermostat tutorial, so please read this first.
The following code pertains to a single component Lennard-Jones, NVT molecular dynamics simulation.
- Created 5-29-07; code revision 203
User Defined Function
In the Initialize System Lattice and Initialize System Random tutorials, we created a function within our custom LennardJonesSimulation class to initialize particles, celled InitializeParticles. We can easily create similar functions that will allow us to access the thermostat, for example, allowing us to set the temperature as we integrate through time.
We first need to make an instance of BerendsenThermostat that is accessible outside of the constructor, however, we only want this to be accessible by functions within the LennardJonesRandom class, so we will make it private:
#!c
private:
BerendsenThermostat *thermostat;
Next we need to modify the the constructor to avoid any redeclaration errors.
#!c
public:
LennardJonesSimulationRandom()
{
IntializeParticles();
SetInteraction(new LennardJones);
SetForceRoutine(new BruteForce);
SetBoundaryConditions(new PeriodicBoundary(10));
SetIntegrationScheme(new VelocityVerletIntegrator);
thermostat = new BerendsenThermostat;
thermostat->SetTemperature(1.0);
AddSimulationComponent(thermostat);
}
Next, we will create a method, call SetSimulationTemperature, that accepts temperature as an argument. Since we will want to access this from the main, we will make this function public. We now need only to pass temperature to the thermostats SetTemperature routine.
#!c
void SetSimulationTemperature(float temperature)
{
thermostat->SetTemperature(temperature);
}
We can now access this from the main time loop:
if(timesteps>1000)
my_simulation.SetSimulationTemperature(0.5);Attachments
- MDNVTAccessor.cpp (1.8 kB) - added by cri 15 months ago.