BrookCalcGBSAOBCForceKernel.cpp 7.88 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, Mark Friedrichs                                    *
 * 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
Mods  
Mark Friedrichs committed
32
33
#include <cmath>
#include <limits>
Mark Friedrichs's avatar
Mark Friedrichs committed
34
35
#include "OpenMMException.h"
#include <sstream>
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
36
37
38
39
40
41

#include "BrookStreamImpl.h"
#include "BrookCalcGBSAOBCForceKernel.h"
#include "gpu/kgbsa.h"
#include "gpu/kforce.h"
#include "math.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
42
43
44
45
46

using namespace OpenMM;
using namespace std;

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
47
 * BrookCalcGBSAOBCForceKernel constructor
Mark Friedrichs's avatar
Mark Friedrichs committed
48
49
50
51
52
53
54
55
 * 
 * @param name                      kernel name
 * @param platform                  platform
 * @param OpenMMBrookInterface      OpenMM-Brook interface
 * @param System                    System reference
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
56
57
BrookCalcGBSAOBCForceKernel::BrookCalcGBSAOBCForceKernel( std::string name, const Platform& platform, OpenMMBrookInterface& openMMBrookInterface, System& system ) :
                             CalcGBSAOBCForceKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
Mark Friedrichs's avatar
Mark Friedrichs committed
58
59
60

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
61
   // static const std::string methodName      = "BrookCalcGBSAOBCForceKernel::BrookCalcGBSAOBCForceKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
62
63
64
65
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
66
   _numberOfParticles                = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
67
68
69
70
71
72
73
   _log                              = NULL;

   const BrookPlatform brookPlatform = dynamic_cast<const BrookPlatform&> (platform);
   if( brookPlatform.getLog() != NULL ){
      setLog( brookPlatform.getLog() );
   }
      
Mark Friedrichs's avatar
Mark Friedrichs committed
74
   _openMMBrookInterface.setNumberOfParticles( system.getNumParticles() );
Mark Friedrichs's avatar
Mark Friedrichs committed
75
76
77
}   

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
78
 * BrookCalcGBSAOBCForceKernel destructor
Mark Friedrichs's avatar
Mark Friedrichs committed
79
80
81
 * 
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
82
BrookCalcGBSAOBCForceKernel::~BrookCalcGBSAOBCForceKernel( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
83
84
85

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
86
   // static const std::string methodName      = "BrookCalcGBSAOBCForceKernel::BrookCalcGBSAOBCForceKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
87
88
89
90
91
92
93
94
95
96
97
98
99
   // static const int debug                   = 1;

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

}

/** 
 * Get log file reference
 * 
 * @return  log file reference
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
100
FILE* BrookCalcGBSAOBCForceKernel::getLog( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
101
102
103
104
105
106
107
108
109
110
111
112
   return _log;
}

/** 
 * Set log file reference
 * 
 * @param  log file reference
 *
 * @return  DefaultReturnValue
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
113
int BrookCalcGBSAOBCForceKernel::setLog( FILE* log ){
Mark Friedrichs's avatar
Mark Friedrichs committed
114
115
116
117
118
119
120
   _log = log;
   return BrookCommon::DefaultReturnValue;
}

/** 
 * Initialize the kernel, setting up the values of all the force field parameters.
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
121
122
 * @param system     system this kernel will be applied to
 * @param force      GBSAOBCForce this kernel will be used for
Mark Friedrichs's avatar
Mark Friedrichs committed
123
124
125
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
126
void BrookCalcGBSAOBCForceKernel::initialize( const System& system, const GBSAOBCForce& force ){
Mark Friedrichs's avatar
Mark Friedrichs committed
127
128
129

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
130
   static const std::string methodName      = "BrookCalcGBSAOBCForceKernel::initialize";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
131
132
   int printOn                              = 0;
   FILE* log;
Mark Friedrichs's avatar
Mark Friedrichs committed
133
134
135

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
136
137
138
139
140
   if( printOn && getLog() ){
       log = getLog();
   } else {
      printOn = 0;
   } 
Mark Friedrichs's avatar
Mark Friedrichs committed
141
142
143

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

Mark Friedrichs's avatar
Mark Friedrichs committed
144
   BrookGbsa& brookGbsa      = _openMMBrookInterface.getBrookGbsa();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
145
146
147
148
149
    
   // get parameters from force object
   // and initialize brookGbsa

   _numberOfParticles = system.getNumParticles();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
150
151
   std::vector<std::vector<double> > particleParameters( _numberOfParticles );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
152
153
154
155
156
   for( int ii = 0; ii < _numberOfParticles; ii++ ){

      double charge, radius, scalingFactor;
      force.getParticleParameters( ii, charge, radius, scalingFactor );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
157
158
159
      particleParameters[ii].push_back( charge );
      particleParameters[ii].push_back( radius );
      particleParameters[ii].push_back( scalingFactor );
Mark Friedrichs's avatar
Mark Friedrichs committed
160
   }   
Mark Friedrichs's avatar
Mark Friedrichs committed
161
   brookGbsa.setup( particleParameters, force.getSolventDielectric(), force.getSoluteDielectric(), getPlatform() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
162
   brookGbsa.setIsActive( 1 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
163

Mark Friedrichs's avatar
Mark Friedrichs committed
164
165
166
   _openMMBrookInterface.setTriggerForceKernel( this );
   _openMMBrookInterface.setTriggerEnergyKernel( this );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
167
   if( printOn ){
Mark Friedrichs's avatar
Mark Friedrichs committed
168
      std::string contents = brookGbsa.getContentsString( ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
169
170
171
172
173
174
175
176
177
      (void) fprintf( log, "%s brookGbsa::contents\n%s", methodName.c_str(), contents.c_str() );
      (void) fflush( log );
   }

   // ---------------------------------------------------------------------------------------
    
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
178
 * Compute forces given particle coordinates
Mark Friedrichs's avatar
Mark Friedrichs committed
179
180
181
182
183
 * 
 * @param context OpenMMContextImpl context
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
184
void BrookCalcGBSAOBCForceKernel::executeForces( OpenMMContextImpl& context ){
Mark Friedrichs's avatar
Mark Friedrichs committed
185
186
187

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
188
   //static const std::string methodName   = "BrookCalcGBSAOBCForceKernel::executeForces";
Mark Friedrichs's avatar
Mark Friedrichs committed
189
190
191
192
193

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

   if( _openMMBrookInterface.getTriggerForceKernel() == this ){
      _openMMBrookInterface.computeForces( context );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
194
   }   
Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
197
198

}

/**
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
199
 * Execute the kernel to calculate the OBC energy
Mark Friedrichs's avatar
Mark Friedrichs committed
200
201
202
 * 
 * @param context OpenMMContextImpl context
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
203
 * @return energy
Mark Friedrichs's avatar
Mark Friedrichs committed
204
205
206
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
207
double BrookCalcGBSAOBCForceKernel::executeEnergy( OpenMMContextImpl& context ){
Mark Friedrichs's avatar
Mark Friedrichs committed
208
209
210

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
211
   //static const std::string methodName      = "BrookCalcGBSAOBCForceKernel::executeEnergy";
Mark Friedrichs's avatar
Mark Friedrichs committed
212
213
214
215

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

   if( _openMMBrookInterface.getTriggerEnergyKernel() == this ){
Mark Friedrichs's avatar
Mark Friedrichs committed
216
      return (double) _openMMBrookInterface.computeEnergy( context, _system );
Mark Friedrichs's avatar
Mark Friedrichs committed
217
218
219
220
221
   } else {
      return 0.0;
   }

}