BrookIntegrateLangevinStepKernel.cpp 6.51 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
32
#include "BrookIntegrateLangevinStepKernel.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
33
34
35
36
37
#include "BrookStreamInternal.h"

using namespace OpenMM;
using namespace std;

Mark Friedrichs's avatar
Mark Friedrichs committed
38
BrookIntegrateLangevinStepKernel::BrookIntegrateLangevinStepKernel( std::string name, const Platform& platform ) :
Mark Friedrichs's avatar
Mark Friedrichs committed
39
                                  IntegrateLangevinStepKernel( name, platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
40
41
42

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
43
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::BrookIntegrateLangevinStepKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
44
45

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
46
47
48

   _brookStochasticDynamics        = NULL;
   _brookShakeAlgorithm            = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
49
50
   _brookRandomNumberGenerator     = NULL;

Mark Friedrichs's avatar
Mark Friedrichs committed
51
52
}

Mark Friedrichs's avatar
Mark Friedrichs committed
53
BrookIntegrateLangevinStepKernel::~BrookIntegrateLangevinStepKernel( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
54
55
56

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
57
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::~BrookIntegrateLangevinStepKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
58
59
60

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
61
   delete _brookStochasticDynamics;
Mark Friedrichs's avatar
Mark Friedrichs committed
62
   delete _brookShakeAlgorithm;
Mark Friedrichs's avatar
Mark Friedrichs committed
63
   delete _brookRandomNumberGenerator;
Mark Friedrichs's avatar
Mark Friedrichs committed
64

Mark Friedrichs's avatar
Mark Friedrichs committed
65
66
}

Mark Friedrichs's avatar
Mark Friedrichs committed
67
void BrookIntegrateLangevinStepKernel::initialize( const vector<double>& masses,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
68
69
                                                   const vector<vector<int> >& constraintIndices,
                                                   const vector<double>& constraintLengths ){
Mark Friedrichs's avatar
Mark Friedrichs committed
70
71
72

// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
73
   // static const std::string methodName      = "BrookIntegrateLangevinStepKernel::initialize";
Mark Friedrichs's avatar
Mark Friedrichs committed
74
75
76

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
77
   _brookStochasticDynamics      = new BrookStochasticDynamics( );
Mark Friedrichs's avatar
Mark Friedrichs committed
78
79
   _brookStochasticDynamics->setup( masses, getPlatform() );

Mark Friedrichs's avatar
Mark Friedrichs committed
80
   _brookShakeAlgorithm          = new BrookShakeAlgorithm( );
Mark Friedrichs's avatar
Mark Friedrichs committed
81
   _brookShakeAlgorithm->setup( masses, constraintIndices, constraintLengths, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
82
83
84

   _brookRandomNumberGenerator   = new BrookRandomNumberGenerator( );
   _brookRandomNumberGenerator->setup( (int) masses.size(), getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
85
86
}

Mark Friedrichs's avatar
Mark Friedrichs committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/** 
 * Execute kernel
 * 
 * @param positions          atom coordinates
 * @param velocities         atom velocities
 * @param forces             atom forces
 * @param temperature        heat bath temperature
 * @param friction           friction coefficient coupling the system to the heat bath
 * @param stepSize           integration step size
 *
 */

void BrookIntegrateLangevinStepKernel::execute( Stream& positions, Stream& velocities,
                                                const Stream& forces, double temperature,
                                                double friction, double stepSize ){

Mark Friedrichs's avatar
Mark Friedrichs committed
103
104
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
105
   double epsilon = 1.0e-04;
106
   static const std::string methodName      = "BrookIntegrateLangevinStepKernel::execute";
Mark Friedrichs's avatar
Mark Friedrichs committed
107
108
109

// ---------------------------------------------------------------------------------------
   
Mark Friedrichs's avatar
Mark Friedrichs committed
110
111
112
113
   // first time through initialize _brookStochasticDynamics

   // for each subsequent call, check if parameters need to be updated due to a change
   // in T, gamma, or the step size
Mark Friedrichs's avatar
Mark Friedrichs committed
114

Mark Friedrichs's avatar
Mark Friedrichs committed
115
116
   // take step

Mark Friedrichs's avatar
Mark Friedrichs committed
117
118
119
120
121
122
   double differences[3];
   differences[0] = temperature - (double) _brookStochasticDynamics->getTemperature();
   differences[1] = friction    - (double) _brookStochasticDynamics->getFriction();
   differences[2] = stepSize    - (double) _brookStochasticDynamics->getStepSize();
   if( fabs( differences[0] ) < epsilon || fabs( differences[1] ) < epsilon || fabs( differences[2] ) < epsilon ){
      _brookStochasticDynamics->updateParameters( temperature, friction, stepSize );
Mark Friedrichs's avatar
Mark Friedrichs committed
123
   } 
124
125
126
127
128
129
130
131
132
133
134
135

   assert( _brookShakeAlgorithm );
/*
   if( _brookShakeAlgorithm == NULL ){
      std::stringstream message;
      message << methodName << " _brookShakeAlgorithm is not set -- case not handled.";
      throw OpenMMException( message.str() );
      return NULL;
   }
*/

   _brookStochasticDynamics->update( positions, velocities, forces, *_brookShakeAlgorithm, *_brookRandomNumberGenerator );
Mark Friedrichs's avatar
Mark Friedrichs committed
136
137

}