"plugins/drude/vscode:/vscode.git/clone" did not exist on "f902295b79294fd04906c4151b87261fe3fc48ff"
ReferenceAndersenThermostat.cpp 3.85 KB
Newer Older
1

2
/* Portions copyright (c) 2008-2010 Stanford University and Simbios.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * Contributors: Peter Eastman
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

peastman's avatar
peastman committed
25
#include <cmath>
26
27
28
#include <string.h>
#include <sstream>

29
#include "SimTKOpenMMUtilities.h"
30
31
#include "ReferenceAndersenThermostat.h"

32
using std::vector;
33
using namespace OpenMM;
34

35
36
37
38
39
40
      /**---------------------------------------------------------------------------------------
      
         Constructor
      
         --------------------------------------------------------------------------------------- */

41
       ReferenceAndersenThermostat::ReferenceAndersenThermostat() {
42
43
44
45
46
47
48
49
       }

      /**---------------------------------------------------------------------------------------
      
         Destructor
      
         --------------------------------------------------------------------------------------- */

50
       ReferenceAndersenThermostat::~ReferenceAndersenThermostat() {
51
52
53
54
55
56
       }

      /**---------------------------------------------------------------------------------------
      
         Apply the thermostat at the start of a time step.
      
57
         @param atomGroups         the groups of atoms to apply the thermostat to
58
59
60
61
62
63
64
         @param atomVelocities     atom velocities
         @param temperature        thermostat temperature in Kelvin
         @param collisionFrequency collision frequency for each atom in fs^-1
         @param stepSize           integration step size in fs
                  
         --------------------------------------------------------------------------------------- */
          
peastman's avatar
peastman committed
65
66
      void ReferenceAndersenThermostat::applyThermostat(const vector<vector<int> >& atomGroups, vector<Vec3>& atomVelocities, vector<double>& atomMasses,
              double temperature, double collisionFrequency, double stepSize) const {
67
          
peastman's avatar
peastman committed
68
          const double collisionProbability = 1.0f - exp(-collisionFrequency*stepSize);
peastman's avatar
peastman committed
69
          for (auto& group : atomGroups) {
70
71
              if (SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber() < collisionProbability) {
                  
72
73
                  // A collision occurred, so set the velocities to new values chosen from a Boltzmann distribution.

peastman's avatar
peastman committed
74
                  for (int atom : group) {
75
                      if (atomMasses[atom] != 0) {
peastman's avatar
peastman committed
76
                          const double velocityScale = static_cast<double>(sqrt(BOLTZ*temperature/atomMasses[atom]));
77
78
79
80
                          atomVelocities[atom][0] = velocityScale*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                          atomVelocities[atom][1] = velocityScale*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                          atomVelocities[atom][2] = velocityScale*SimTKOpenMMUtilities::getNormallyDistributedRandomNumber();
                      }
81
                  }
82
83
84
85
86
              }
          }
          
      }