BrookCalcGBSAOBCForceFieldKernel.cpp 16.6 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* -------------------------------------------------------------------------- *
 *                                   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.                                     *
 * -------------------------------------------------------------------------- */

#include <cmath>
#include <limits>
#include "OpenMMException.h"
#include <sstream>

#include "BrookStreamImpl.h"
#include "BrookCalcGBSAOBCForceFieldKernel.h"
#include "force.h"
#include "kgbsa.h"
#include "kforce.h"
#include "math.h"

using namespace OpenMM;
using namespace std;

/** 
 * BrookCalcGBSAOBCForceFieldKernel constructor
 * 
 * @param name                      kernel name
 * @param platform                  platform
 *
 */

BrookCalcGBSAOBCForceFieldKernel::BrookCalcGBSAOBCForceFieldKernel( std::string name, const Platform& platform ) :
                     CalcGBSAOBCForceFieldKernel( name, platform ){

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

   // static const std::string methodName      = "BrookCalcGBSAOBCForceFieldKernel::BrookCalcGBSAOBCForceFieldKernel";
   // static const int debug                   = 1;

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

   _numberOfAtoms                    = 0;
   _brookGbsa                        = NULL;
67
   _log                              = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

   const BrookPlatform brookPlatform = dynamic_cast<const BrookPlatform&> (platform);
   if( brookPlatform.getLog() != NULL ){
      setLog( brookPlatform.getLog() );
   }
      
}   

/** 
 * BrookCalcGBSAOBCForceFieldKernel destructor
 * 
 */

BrookCalcGBSAOBCForceFieldKernel::~BrookCalcGBSAOBCForceFieldKernel( ){

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

   // static const std::string methodName      = "BrookCalcGBSAOBCForceFieldKernel::BrookCalcGBSAOBCForceFieldKernel";
   // static const int debug                   = 1;

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

   delete _brookGbsa;
}

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

FILE* BrookCalcGBSAOBCForceFieldKernel::getLog( void ) const {
   return _log;
}

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

int BrookCalcGBSAOBCForceFieldKernel::setLog( FILE* log ){
   _log = log;
   return BrookCommon::DefaultReturnValue;
}

/** 
 * Initialize the kernel, setting up the values of all the force field parameters.
 * 
 * @param atomParameters            vector containing atom index, charge, radius, scalingFactor
 * @param solventDielectric         solvent dielectric
 * @param soluteDielectric          solute dielectric
 *
 */

void BrookCalcGBSAOBCForceFieldKernel::initialize( const std::vector<std::vector<double> >& atomParameters, 
                                                   double solventDielectric, double soluteDielectric ){

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

   static const std::string methodName      = "BrookCalcGBSAOBCForceFieldKernel::initialize";

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

   FILE* log                 = getLog();
   _numberOfAtoms            = (int) atomParameters.size();

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

   // bonded

   if( _brookGbsa ){
      delete _brookGbsa;
   }
   _brookGbsa              = new BrookGbsa();
   _brookGbsa->setLog( log );
    
Mark Friedrichs's avatar
Mark Friedrichs committed
149
   _brookGbsa->setup( atomParameters, solventDielectric, soluteDielectric, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

   if( log ){
      std::string contents = _brookGbsa->getContentsString( ); 
      (void) fprintf( log, "%s brookGbsa::contents\n%s", methodName.c_str(), contents.c_str() );
      (void) fflush( log );
   }

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

/** 
 * Compute forces given atom coordinates
 * 
 * @param positions                 atom coordinates
 * @param forces                    output forces
 *
 */

void BrookCalcGBSAOBCForceFieldKernel::executeForces( const Stream& positions, Stream& forces ){

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
173
   static const std::string methodName   = "BrookCalcGBSAOBCForceFieldKernel::executeForces";
174
   static const int PrintOn              = 0; 
Mark Friedrichs's avatar
Mark Friedrichs committed
175
176
177
178
179
180
181
182
183
184
185
186

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

   // OBC

   const BrookStreamImpl& positionStreamC              = dynamic_cast<const BrookStreamImpl&> (positions.getImpl());
   BrookStreamImpl& positionStream                     = const_cast<BrookStreamImpl&>         (positionStreamC);
   BrookStreamImpl& forceStream                        = dynamic_cast<BrookStreamImpl&>       (forces.getImpl());

   float includeAce                                    = (float) (_brookGbsa->includeAce());
   BrookFloatStreamInternal**  gbsaForceStreams        = _brookGbsa->getForceStreams();

Mark Friedrichs's avatar
Mark Friedrichs committed
187
188
   // calculate Born radii first time thru and initialize on board

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
189
   if( !_brookGbsa->haveBornRadiiBeenInitialized() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
190
191
192
      _brookGbsa->calculateBornRadii( positions );  
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
   // first major loop

   kObcLoop1( (float) _brookGbsa->getNumberOfAtoms(),
              (float) _brookGbsa->getAtomSizeCeiling(),
              (float) _brookGbsa->getDuplicationFactor(),
              (float) _brookGbsa->getAtomStreamWidth( ),
              (float) _brookGbsa->getPartialForceStreamWidth( ),
              _brookGbsa->getSoluteDielectric(),
              _brookGbsa->getSolventDielectric(),
              includeAce,

              positionStream.getBrookStream(),

              _brookGbsa->getObcBornRadii()->getBrookStream(),
              _brookGbsa->getObcAtomicRadii()->getBrookStream(),

              gbsaForceStreams[0]->getBrookStream(),
              gbsaForceStreams[1]->getBrookStream(),
              gbsaForceStreams[2]->getBrookStream(),
              gbsaForceStreams[3]->getBrookStream()
            );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
215
216
217
218
// ---------------------------------------------------------------------------------------

   // diagnostics

219
   if( 1 && PrintOn && getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

      (void) fprintf( getLog(), "\nPost kObcLoop1: atms=%d ceil=%d dup=%d atomStrW=%3d prtlF=%3d diel=%.3f %.3f ACE=%.1f\n",
                      _brookGbsa->getNumberOfAtoms(),
                      _brookGbsa->getAtomSizeCeiling(),
                      _brookGbsa->getDuplicationFactor(),
                      _brookGbsa->getAtomStreamWidth( ),
                      _brookGbsa->getPartialForceStreamWidth( ),
                      _brookGbsa->getSoluteDielectric(),
                      _brookGbsa->getSolventDielectric(), includeAce );

      BrookStreamInternal* brookStreamInternalPos  = positionStream.getBrookStreamImpl();
      (void) fprintf( getLog(), "\nPositionStream\n" );
      brookStreamInternalPos->printToFile( getLog() );

      (void) fprintf( getLog(), "\nBornR\n" );
      _brookGbsa->getObcBornRadii()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nAtomR\n" );
      _brookGbsa->getObcAtomicRadii()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nForceStreams output\n" );
      for( int ii = 0; ii < 4; ii++ ){
         gbsaForceStreams[ii]->printToFile( getLog() );
      }

   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
   // gather for first loop

   kPostObcLoop1_nobranch(
              (float) _brookGbsa->getDuplicationFactor(),
              (float) _brookGbsa->getAtomStreamWidth( ),
              (float) _brookGbsa->getPartialForceStreamWidth( ),
              (float) _brookGbsa->getNumberOfAtoms(),
              (float) _brookGbsa->getAtomSizeCeiling(),
              (float) _brookGbsa->getInnerLoopUnroll(),
              gbsaForceStreams[0]->getBrookStream(),
              gbsaForceStreams[1]->getBrookStream(),
              gbsaForceStreams[2]->getBrookStream(),
              gbsaForceStreams[3]->getBrookStream(),
              _brookGbsa->getObcChain()->getBrookStream(),
              _brookGbsa->getObcBornRadii()->getBrookStream(),
              _brookGbsa->getObcIntermediateForce()->getBrookStream(),
              _brookGbsa->getObcBornRadii2()->getBrookStream() );
 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
267
268
269
270
// ---------------------------------------------------------------------------------------

   // diagnostics

271
   if( PrintOn && getLog()){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
272
273
274
275
276
277
278
279
280

      (void) fprintf( getLog(), "\nPost kPostObcLoop1_nobranch: dup=%d aStrW=%d pStrW=%d no.atms=%3d ceil=%3d Unroll=%1d\n",
                      _brookGbsa->getDuplicationFactor(),
                      _brookGbsa->getAtomStreamWidth( ),
                      _brookGbsa->getPartialForceStreamWidth( ),
                      _brookGbsa->getNumberOfAtoms(),
                      _brookGbsa->getAtomSizeCeiling(),
                      _brookGbsa->getInnerLoopUnroll() );

281
      (void) fprintf( getLog(), "\nForceStreams\n" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
      for( int ii = 0; ii < 4; ii++ ){
         gbsaForceStreams[ii]->printToFile( getLog() );
      }

      (void) fprintf( getLog(), "\nObcChain\n" );
      _brookGbsa->getObcChain()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nBornR\n" );
      _brookGbsa->getObcBornRadii()->printToFile( getLog() );

      // output

      (void) fprintf( getLog(), "\nObcIntermediateForce output\n" );
      _brookGbsa->getObcIntermediateForce()->printToFile( getLog() );

      // output

299
      (void) fprintf( getLog(), "\nObcBornRadii2 output\n" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
300
301
302
303
304
305
      _brookGbsa->getObcBornRadii2()->printToFile( getLog() );

   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
   // second major loop

   kObcLoop2( (float) _brookGbsa->getNumberOfAtoms(),
              (float) _brookGbsa->getAtomSizeCeiling(),
              (float) _brookGbsa->getDuplicationFactor(),
              (float) _brookGbsa->getAtomStreamWidth( ),
              (float) _brookGbsa->getPartialForceStreamWidth( ),
              positionStream.getBrookStream(),
              _brookGbsa->getObcScaledAtomicRadii()->getBrookStream(),
              _brookGbsa->getObcBornRadii2()->getBrookStream(),
              gbsaForceStreams[0]->getBrookStream(),
              gbsaForceStreams[1]->getBrookStream(),
              gbsaForceStreams[2]->getBrookStream(),
              gbsaForceStreams[3]->getBrookStream()
            );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
322
323
324
325
// ---------------------------------------------------------------------------------------

   // diagnostics

326
   if( PrintOn && getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353

      (void) fprintf( getLog(), "\nPost kObcLoop2: no.atms=%5d ceil=%3d dup=%3d strW=%3d pStrW=%3d\n",
                      _brookGbsa->getNumberOfAtoms(),
                      _brookGbsa->getAtomSizeCeiling(),
                      _brookGbsa->getDuplicationFactor(),
                      _brookGbsa->getAtomStreamWidth( ),
                      _brookGbsa->getPartialForceStreamWidth( ) );

      BrookStreamInternal* brookStreamInternalPos  = positionStream.getBrookStreamImpl();
      (void) fprintf( getLog(), "\nPositionStream\n" );
      brookStreamInternalPos->printToFile( getLog() );

      (void) fprintf( getLog(), "\nObcScaledAtomicRadii\n" );
      _brookGbsa->getObcScaledAtomicRadii()->printToFile( getLog() );

      (void) fprintf( getLog(), "\ngetObcBornRadii2\n" );
      _brookGbsa->getObcBornRadii2()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nForceStreams\n" );
      for( int ii = 0; ii < 4; ii++ ){
         gbsaForceStreams[ii]->printToFile( getLog() );
      }

   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
   // gather for second loop

    float mergeNonObcForces = 1.0f;
    float kcalMolTokJNM     = -0.4184f;
    kPostObcLoop2_nobranch(
              (float) _brookGbsa->getDuplicationFactor(),
              (float) _brookGbsa->getAtomStreamWidth( ),
              (float) _brookGbsa->getPartialForceStreamWidth( ),
              (float) _brookGbsa->getNumberOfAtoms(),
              (float) _brookGbsa->getAtomSizeCeiling(),
              (float) _brookGbsa->getInnerLoopUnroll(),
              kcalMolTokJNM,
              mergeNonObcForces,
              _brookGbsa->getObcIntermediateForce()->getBrookStream(),
              forceStream.getBrookStream(),
              gbsaForceStreams[0]->getBrookStream(),
              gbsaForceStreams[1]->getBrookStream(),
              gbsaForceStreams[2]->getBrookStream(),
              gbsaForceStreams[3]->getBrookStream(),
              _brookGbsa->getObcAtomicRadii()->getBrookStream(),
              _brookGbsa->getObcBornRadii()->getBrookStream(),
              _brookGbsa->getObcChain()->getBrookStream(),
              forceStream.getBrookStream()
           );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
379
380
381
382
// ---------------------------------------------------------------------------------------

   // diagnostics

383
   if( PrintOn && getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
384

385
      (void) fprintf( getLog(), "\nPost kPostObcLoop2_nobranch: atms=%d ceil=%d dup=%d atomStrW=%3d prtlF=%3d diel=%.3f %.3f ACE=%.1f\n",
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
386
387
388
389
390
391
392
393
                      _brookGbsa->getNumberOfAtoms(),
                      _brookGbsa->getAtomSizeCeiling(),
                      _brookGbsa->getDuplicationFactor(),
                      _brookGbsa->getAtomStreamWidth( ),
                      _brookGbsa->getPartialForceStreamWidth( ),
                      _brookGbsa->getSoluteDielectric(),
                      _brookGbsa->getSolventDielectric(), includeAce );

394
395
396
397
398
      (void) fprintf( getLog(), "\nPartialForceStreams\n" );
      for( int ii = 0; ii < 4; ii++ ){
         gbsaForceStreams[ii]->printToFile( getLog() );
      }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
399
400
401
402
403
404
405
406
407
408
409
410
      BrookStreamInternal* brookStreamInternalF  = forceStream.getBrookStreamImpl();
      (void) fprintf( getLog(), "\nForceStream\n" );
      brookStreamInternalF->printToFile( getLog() );

      (void) fprintf( getLog(), "\nChain\n" );
      _brookGbsa->getObcChain()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nBornR\n" );
      _brookGbsa->getObcBornRadii()->printToFile( getLog() );

   }

Mark Friedrichs's avatar
Mark Friedrichs committed
411
412
413
414
   // ---------------------------------------------------------------------------------------
}

/**
415
 * Execute the kernel to calculate the OBC energy
Mark Friedrichs's avatar
Mark Friedrichs committed
416
417
418
 * 
 * @param positions   atom positions
 *
419
 * @return  potential energy due to the OBC forces
Mark Friedrichs's avatar
Mark Friedrichs committed
420
421
422
423
424
425
426
427
428
429
430
 *
 */

double BrookCalcGBSAOBCForceFieldKernel::executeEnergy( const Stream& positions ){

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

   //static const std::string methodName      = "BrookCalcGBSAOBCForceFieldKernel::executeEnergy";

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

431
432
   return (double) _brookGbsa->getEnergy( positions );

Mark Friedrichs's avatar
Mark Friedrichs committed
433
}