BrookCalcStandardMMForceFieldKernel.cpp 35.4 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
/* -------------------------------------------------------------------------- *
 *                                   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"
38
#include "BrookCalcNonbondedForceKernel.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
39
40
#include "gpu/kforce.h"
#include "gpu/kinvmap_gather.h"
41
#include "NonbondedForce.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
42
43
44
45
46

using namespace OpenMM;
using namespace std;

/** 
47
 * BrookCalcNonbondedForceKernel constructor
Mark Friedrichs's avatar
Mark Friedrichs committed
48
49
50
51
52
53
 * 
 * @param name                      kernel name
 * @param platform                  platform
 *
 */

54
55
BrookCalcNonbondedForceKernel::BrookCalcNonbondedForceKernel( std::string name, const Platform& platform ) :
                     CalcNonbondedForceKernel( name, platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
56
57
58

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

59
   // static const std::string methodName      = "BrookCalcNonbondedForceKernel::BrookCalcNonbondedForceKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
60
61
62

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

63
64
65
66
   _numberOfAtoms                           = 0;
   _brookBonded                             = NULL;
   _brookNonBonded                          = NULL;
   _refForceField                           = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
67
68
69
70
71
72
73
   _log                                     = NULL;

   _refForceField                           = NULL;
   _refSystem                               = NULL;
   _refOpenMMContext                        = NULL;
   _referencePlatform                       = NULL;
   _refVerletIntegrator                     = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
74
75
76
77
78
79
80
81
82

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

/** 
83
 * BrookCalcNonbondedForceKernel destructor
Mark Friedrichs's avatar
Mark Friedrichs committed
84
85
86
 * 
 */

87
BrookCalcNonbondedForceKernel::~BrookCalcNonbondedForceKernel( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
88
89
90

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

91
   // static const std::string methodName      = "BrookCalcNonbondedForceKernel::BrookCalcNonbondedForceKernel";
Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
94
95
96

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

   delete _brookBonded;
   delete _brookNonBonded;
97
98
99
100

   // deleted w/ kernel delete? If activated, program crashes

   //delete _refForceField;
Mark Friedrichs's avatar
Mark Friedrichs committed
101
102
103
104
105
106
/*
   delete _refSystem;
   delete _refOpenMMContext;
   delete _referencePlatform;
   delete _refVerletIntegrator;
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
107
108
109
110
111
112
113
114
115
}

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

116
FILE* BrookCalcNonbondedForceKernel::getLog( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
117
118
119
120
121
122
123
124
125
126
127
128
   return _log;
}

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

129
int BrookCalcNonbondedForceKernel::setLog( FILE* log ){
Mark Friedrichs's avatar
Mark Friedrichs committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
   _log = log;
   return BrookCommon::DefaultReturnValue;
}

/** 
 * Initialize the kernel, setting up the values of all the force field parameters.
 * 
 * @param bondIndices               the two atoms connected by each bond term
 * @param bondParameters            the force parameters (length, k) for each bond term
 * @param angleIndices              the three atoms connected by each angle term
 * @param angleParameters           the force parameters (angle, k) for each angle term
 * @param periodicTorsionIndices    the four atoms connected by each periodic torsion term
 * @param periodicTorsionParameters the force parameters (k, phase, periodicity) for each periodic torsion term
 * @param rbTorsionIndices          the four atoms connected by each Ryckaert-Bellemans torsion term
 * @param rbTorsionParameters       the coefficients (in order of increasing powers) for each Ryckaert-Bellemans torsion term
 * @param bonded14Indices           each element contains the indices of two atoms whose nonbonded interactions should be reduced since
 *                                  they form a bonded 1-4 pair
 * @param lj14Scale                 the factor by which van der Waals interactions should be reduced for bonded 1-4 pairs
 * @param coulomb14Scale            the factor by which Coulomb interactions should be reduced for bonded 1-4 pairs
 * @param exclusions                the i'th element lists the indices of all atoms with which the i'th atom should not interact through
 *                                  nonbonded forces.  Bonded 1-4 pairs are also included in this list, since they should be omitted from
 *                                  the standard nonbonded calculation.
 * @param nonbondedParameters       the nonbonded force parameters (charge, sigma, epsilon) for each atom
 * @param nonbondedMethod           the method to use for handling long range nonbonded interactions
 * @param nonbondedCutoff           the cutoff distance for nonbonded interactions (if nonbondedMethod involves a cutoff)
 * @param periodicBoxSize           the size of the periodic box (if nonbondedMethod involves a periodic boundary conditions)
 */
 
158
void BrookCalcNonbondedForceKernel::initialize( 
Mark Friedrichs's avatar
Mark Friedrichs committed
159
160
161
162
163
164
165
166
167
168
        const vector<vector<int> >& bondIndices,            const vector<vector<double> >& bondParameters,
        const vector<vector<int> >& angleIndices,           const vector<vector<double> >& angleParameters,
        const vector<vector<int> >& periodicTorsionIndices, const vector<vector<double> >& periodicTorsionParameters,
        const vector<vector<int> >& rbTorsionIndices,       const vector<vector<double> >& rbTorsionParameters,
        const vector<vector<int> >& bonded14Indices,        double lj14Scale, double coulomb14Scale,
        const vector<set<int> >& exclusions, const vector<vector<double> >& nonbondedParameters,
         NonbondedMethod nonbondedMethod, double nonbondedCutoff, double periodicBoxSize[3] ){

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

169
   static const std::string methodName      = "BrookCalcNonbondedForceKernel::initialize";
Mark Friedrichs's avatar
Mark Friedrichs committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

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

   FILE* log                 = getLog();
   _numberOfAtoms            = nonbondedParameters.size();

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

   // bonded

   if( _brookBonded ){
      delete _brookBonded;
   }
   _brookBonded              = new BrookBonded();
   _brookBonded->setLog( log );
    
   _brookBonded->setup( _numberOfAtoms, 
                        bondIndices,            bondParameters, 
                        angleIndices,           angleParameters,
                        periodicTorsionIndices, periodicTorsionParameters,
                        rbTorsionIndices,       rbTorsionParameters,
                        bonded14Indices,        nonbondedParameters,
                        lj14Scale, coulomb14Scale, getPlatform() );

   // echo contents

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

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

   // nonbonded

   if( _brookNonBonded ){
      delete _brookNonBonded;
   }
   _brookNonBonded           = new BrookNonBonded();
   _brookNonBonded->setLog( log );

   _brookNonBonded->setup( _numberOfAtoms, nonbondedParameters, exclusions, getPlatform() );

   // echo contents

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

   // ---------------------------------------------------------------------------------------
223
224
225
226

   // used for calculating energy

/*
227
228
   _referenceCalcNonbondedForceKernel = new ReferenceCalcNonbondedForceKernel( getName(), getPlatform() );
   _referenceCalcNonbondedForceKernel->initialize( bondIndices,            bondParameters, 
229
230
231
232
233
234
235
                                                         angleIndices,           angleParameters,
                                                         periodicTorsionIndices, periodicTorsionParameters,
                                                         rbTorsionIndices,       rbTorsionParameters,
                                                         bonded14Indices,        lj14Scale, coulomb14Scale,
                                                         exclusions, nonbondedParameters,
                                                         nonbondedMethod, nonbondedCutoff, periodicBoxSize );
*/
236
   _refForceField = new NonbondedForce( _numberOfAtoms, bondIndices.size(), angleIndices.size(), 
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
                                              periodicTorsionIndices.size(),  rbTorsionIndices.size() );

   // bonds

   vector<vector<int> >::const_iterator bondI_it    = bondIndices.begin();
   vector<vector<double> >::const_iterator bondP_it = bondParameters.begin();
   for( unsigned int ii = 0; ii < bondIndices.size(); ii++, bondI_it++, bondP_it++ ){
      const vector<int>    bondInd = *bondI_it;
      const vector<double> bondPrm = *bondP_it;
      _refForceField->setBondParameters( ii, bondInd[0], bondInd[1], bondPrm[0], bondPrm[1] );
   }

   // angles

   vector<vector<int> >::const_iterator angleI_it    = angleIndices.begin();
   vector<vector<double> >::const_iterator angleP_it = angleParameters.begin();
   for( unsigned int ii = 0; ii < angleIndices.size(); ii++, angleI_it++, angleP_it++ ){
      const vector<int>    angleInd = *angleI_it;
      const vector<double> anglePrm = *angleP_it;
      _refForceField->setAngleParameters( ii, angleInd[0], angleInd[1], angleInd[2], anglePrm[0], anglePrm[1] );
   }

   // periodicTorsions

   vector<vector<int> >::const_iterator periodicTorsionI_it    = periodicTorsionIndices.begin();
   vector<vector<double> >::const_iterator periodicTorsionP_it = periodicTorsionParameters.begin();
   for( unsigned int ii = 0; ii < periodicTorsionIndices.size(); ii++, periodicTorsionI_it++, periodicTorsionP_it++ ){
      const vector<int>    periodicTorsionInd = *periodicTorsionI_it;
      const vector<double> periodicTorsionPrm = *periodicTorsionP_it;
      _refForceField->setPeriodicTorsionParameters( ii, periodicTorsionInd[0], periodicTorsionInd[1], periodicTorsionInd[2], periodicTorsionInd[3],
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
267
                                                    (int) (periodicTorsionPrm[2] + 0.001), periodicTorsionPrm[1], periodicTorsionPrm[0] );
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
printf( "PeriodicTor: [%d %d %d %d] [%.5e %.5e %.5e]\n", periodicTorsionInd[0], periodicTorsionInd[1], periodicTorsionInd[2], periodicTorsionInd[3],
                                                         periodicTorsionPrm[2], periodicTorsionPrm[1], periodicTorsionPrm[0] ); fflush( stdout );
*/
   }

   // rbTorsions

   vector<vector<int> >::const_iterator rbTorsionI_it    = rbTorsionIndices.begin();
   vector<vector<double> >::const_iterator rbTorsionP_it = rbTorsionParameters.begin();
   for( unsigned int ii = 0; ii < rbTorsionIndices.size(); ii++, rbTorsionI_it++, rbTorsionP_it++ ){
      const vector<int>    rbTorsionInd = *rbTorsionI_it;
      const vector<double> rbTorsionPrm = *rbTorsionP_it;
      _refForceField->setRBTorsionParameters( ii, rbTorsionInd[0], rbTorsionInd[1], rbTorsionInd[2], rbTorsionInd[3],
                                              rbTorsionPrm[0], rbTorsionPrm[1], rbTorsionPrm[2], rbTorsionPrm[3], 
                                              rbTorsionPrm[4], rbTorsionPrm[5] );

/*
printf( "RbTor: [%d %d %d %d] [%.5e %.5e %.5e %.5e %.5e %.5e]\n", rbTorsionInd[0], rbTorsionInd[1], rbTorsionInd[2], rbTorsionInd[3],
                                                         rbTorsionPrm[0], rbTorsionPrm[1], rbTorsionPrm[2], rbTorsionPrm[3], rbTorsionPrm[4], rbTorsionPrm[5] ); fflush( stdout );
*/
   }

   // nonbonded

   for( unsigned int ii = 0; ii < nonbondedParameters.size(); ii++ ){

      vector<double> nonbondedParameterVector 
                                     = nonbondedParameters[ii];

      double c6                      = nonbondedParameterVector[1];
      double c12                     = nonbondedParameterVector[2];
      double charge                  = nonbondedParameterVector[0];

      // int index, double charge, double radius, double depth

      _refForceField->setAtomParameters( ii, charge, c6, c12 );

   }

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

   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
311
312
313
314
315
316
317
318
319
320
}

/** 
 * Execute the kernel to calculate the bonded & nonbonded forces
 * 
 * @param positions   stream of type Double3 containing the position (x, y, z) of each atom
 * @param forces      stream of type Double3 containing the force (x, y, z) on each atom.  On entry, this contains the forces that
 *                    have been calculated so far.  The kernel should add its own forces to the values already in the stream.
 */

321
void BrookCalcNonbondedForceKernel::executeForces( const Stream& positions, Stream& forces ){
Mark Friedrichs's avatar
Mark Friedrichs committed
322
323
324

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

325
   static const std::string methodName      = "BrookCalcNonbondedForceKernel::executeForces";
Mark Friedrichs's avatar
Mark Friedrichs committed
326
327
328
329
330
331

   static const int I_Stream                = 0;
   static const int J_Stream                = 1;
   static const int K_Stream                = 2;
   static const int L_Stream                = 3;

332
   static const int PrintOn                 = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
333
334
   static const int MaxErrorMessages        = 2;
   static       int ErrorMessages           = 0;
335

Mark Friedrichs's avatar
Mark Friedrichs committed
336
337
338
339
340
341
342
343
344
345
346
   static const float4 dummyParameters( 0.0, 0.0, 0.0, 0.0 );

   // static const int debug                   = 1;

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


   const BrookStreamImpl& positionStreamC              = dynamic_cast<const BrookStreamImpl&> (positions.getImpl());
   BrookStreamImpl& positionStream                     = const_cast<BrookStreamImpl&>         (positionStreamC);
   BrookStreamImpl& forceStream                        = dynamic_cast<BrookStreamImpl&>       (forces.getImpl());
   
347
348
349
350
351
352
353
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
379
380
381
382
383
384
385
386
387
   // nonbonded forces

   // added charge stream to knbforce_CDLJ4
   // libs generated from ~/src/gmxgpu-nsqOpenMM

   BrookFloatStreamInternal**  nonbondedForceStreams = _brookNonBonded->getForceStreams();

   float epsfac                                      = 138.935485f;

   knbforce_CDLJ4(
             (float) _brookNonBonded->getNumberOfAtoms(),
             (float) _brookNonBonded->getAtomSizeCeiling(),
             (float) _brookNonBonded->getDuplicationFactor(),
             (float) _brookNonBonded->getAtomStreamHeight( ),
             (float) _brookNonBonded->getAtomStreamWidth( ),
             (float) _brookNonBonded->getJStreamWidth( ),
             (float) _brookNonBonded->getPartialForceStreamWidth( ),
             epsfac,
             dummyParameters,
             positionStream.getBrookStream(),
             _brookNonBonded->getChargeStream()->getBrookStream(),
             _brookNonBonded->getOuterVdwStream()->getBrookStream(),
             _brookNonBonded->getInnerSigmaStream()->getBrookStream(),
             _brookNonBonded->getInnerEpsilonStream()->getBrookStream(),
             _brookNonBonded->getExclusionStream()->getBrookStream(),
             nonbondedForceStreams[0]->getBrookStream(),
             nonbondedForceStreams[1]->getBrookStream(),
             nonbondedForceStreams[2]->getBrookStream(),
             nonbondedForceStreams[3]->getBrookStream()
           );

/*
float zerof = 0.0f;
nonbondedForceStreams[0]->fillWithValue( &zerof );
nonbondedForceStreams[1]->fillWithValue( &zerof );
nonbondedForceStreams[2]->fillWithValue( &zerof );
nonbondedForceStreams[3]->fillWithValue( &zerof );
*/

   // diagnostics

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
388
   if( 1 && PrintOn ){
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
      (void) fprintf( getLog(), "\nPost knbforce_CDLJ4: atoms=%6d ceiling=%3d dupFac=%3d", _brookNonBonded->getNumberOfAtoms(),  
                                                                                           _brookNonBonded->getAtomSizeCeiling(),
                                                                                           _brookNonBonded->getDuplicationFactor()  );

      (void) fprintf( getLog(), "\n                      hght=%6d   width=%3d   jWid=%3d", _brookNonBonded->getAtomStreamHeight( ),
                                                                                           _brookNonBonded->getAtomStreamWidth( ),
                                                                                           _brookNonBonded->getJStreamWidth( ) );
      (void) fprintf( getLog(), "\n                      pFrc=%6d     eps=%12.5e\n",       _brookNonBonded->getPartialForceStreamWidth( ), epsfac );

      (void) fprintf( getLog(), "\nOuterVdwStreamd\n" );
      _brookNonBonded->getOuterVdwStream()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nInnerSigmaStream\n" );
      _brookNonBonded->getInnerSigmaStream()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nInnerEpsilonStream\n" );
      _brookNonBonded->getInnerEpsilonStream()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nExclusionStream\n" );
      _brookNonBonded->getExclusionStream()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nChargeStream\n" );
      _brookNonBonded->getChargeStream()->printToFile( getLog() );

      for( int ii = 0; ii < 4; ii++ ){
         (void) fprintf( getLog(), "\nForce stream %d\n", ii );
         nonbondedForceStreams[ii]->printToFile( getLog() );
      }
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
419
420
// ---------------------------------------------------------------------------------------

421
422
423
424
425
426
427
428
429
430
431
432
433
434
   // gather forces

   kMergeFloat3_4_nobranch( (float) _brookNonBonded->getDuplicationFactor(),
                            (float) _brookNonBonded->getAtomStreamWidth(),
                            (float) _brookNonBonded->getPartialForceStreamWidth(),
                            (float) _brookNonBonded->getNumberOfAtoms(),
                            (float) _brookNonBonded->getAtomSizeCeiling(),
                            (float) _brookNonBonded->getOuterLoopUnroll(),
                            nonbondedForceStreams[0]->getBrookStream(),
                            nonbondedForceStreams[1]->getBrookStream(),
                            nonbondedForceStreams[2]->getBrookStream(),
                            nonbondedForceStreams[3]->getBrookStream(),
                            forceStream.getBrookStream() );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
435
436
437
438
439
440
441
442
443
444
445
446
   // diagnostics

   if( 0 && PrintOn ){

      (void) fprintf( getLog(), "\nNB forces" );
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
      brookStreamInternalF->printToFile( getLog() );

   }

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

447
448
449
   // bonded

         epsfac                                        = (float) (_brookBonded->getLJ_14Scale()*_brookBonded->getCoulombFactor());
Mark Friedrichs's avatar
Mark Friedrichs committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
   float width                                         = (float) (_brookBonded->getInverseMapStreamWidth());

   // bonded forces

   BrookFloatStreamInternal**  bondedParameters        = _brookBonded->getBondedParameterStreams();
   BrookFloatStreamInternal**  bondedForceStreams      = _brookBonded->getBondedForceStreams();

   BrookFloatStreamInternal**  inverseStreamMaps[4];
   inverseStreamMaps[0]                                = _brookBonded->getInverseStreamMapsStreams( 0 );
   inverseStreamMaps[1]                                = _brookBonded->getInverseStreamMapsStreams( 1 );
   inverseStreamMaps[2]                                = _brookBonded->getInverseStreamMapsStreams( 2 );
   inverseStreamMaps[3]                                = _brookBonded->getInverseStreamMapsStreams( 3 );

   kbonded_CDLJ( epsfac, 
                 (float) bondedForceStreams[0]->getStreamWidth(),
                 dummyParameters,
                 positionStream.getBrookStream(),
467
                 _brookBonded->getChargeStream()->getBrookStream(),
Mark Friedrichs's avatar
Mark Friedrichs committed
468
469
470
471
472
473
474
475
476
477
478
                 _brookBonded->getAtomIndicesStream()->getBrookStream(),
                 bondedParameters[0]->getBrookStream(),
                 bondedParameters[1]->getBrookStream(),
                 bondedParameters[2]->getBrookStream(),
                 bondedParameters[3]->getBrookStream(),
                 bondedParameters[4]->getBrookStream(),
                 bondedForceStreams[0]->getBrookStream(),
                 bondedForceStreams[1]->getBrookStream(),
                 bondedForceStreams[2]->getBrookStream(),
                 bondedForceStreams[3]->getBrookStream() );

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

   // diagnostics

   if( 1 && PrintOn ){

      int countPrintInvMap[4] = { 3, 5, 2, 4 }; 

      (void) fprintf( getLog(), "\nPost kbonded_CDLJ: epsFac=%.6f %.6f %.6f", epsfac, _brookBonded->getLJ_14Scale(), _brookBonded->getCoulombFactor());
      (void) fprintf( getLog(), "\nAtom indices stream\n" );
      _brookBonded->getAtomIndicesStream()->printToFile( getLog() );

      (void) fprintf( getLog(), "\nCharge stream\n" );
      _brookBonded->getChargeStream()->printToFile( getLog() );

      for( int ii = 0; ii < 5; ii++ ){
         (void) fprintf( getLog(), "\nParam stream %d\n", ii );
         bondedParameters[ii]->printToFile( getLog() );
      }
      for( int ii = 0; ii < 4; ii++ ){
         (void) fprintf( getLog(), "\nForce stream %d\n", ii );
         bondedForceStreams[ii]->printToFile( getLog() );
      }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
501
502
503
504
505
506
507
508

/*
      (void) fprintf( getLog(), "\nNB1 forces" );
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
      brookStreamInternalF->printToFile( getLog() );
*/

      (void) fprintf( getLog(), "\nInverse map streams -- K_Stream cnt=%d\n", _brookBonded->getInverseMapStreamCount( K_Stream ) );
509
510
511
512
513
514
515
516
      for( int ii = 0; ii < 4; ii++ ){
         for( int jj = 0; jj < countPrintInvMap[ii]; jj++ ){
            (void) fprintf( getLog(), "\n   Inverse map streams index=%d %d\n", ii, jj );
            inverseStreamMaps[ii][jj]->printToFile( getLog() );
         }
      }
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
517
518
   // gather forces

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
   if( _brookBonded->getInverseMapStreamCount( I_Stream ) == 3 && _brookBonded->getInverseMapStreamCount( K_Stream ) == 3 ){

      kinvmap_gather3_3( width,

                         inverseStreamMaps[I_Stream][0]->getBrookStream(),
                         inverseStreamMaps[I_Stream][1]->getBrookStream(),
                         inverseStreamMaps[I_Stream][2]->getBrookStream(),
                         bondedForceStreams[I_Stream]->getBrookStream(),

                         inverseStreamMaps[K_Stream][0]->getBrookStream(),
                         inverseStreamMaps[K_Stream][1]->getBrookStream(),
                         inverseStreamMaps[K_Stream][2]->getBrookStream(),
                         bondedForceStreams[K_Stream]->getBrookStream(),

                         forceStream.getBrookStream(), forceStream.getBrookStream() );

   } else if( _brookBonded->getInverseMapStreamCount( I_Stream ) == 3 && _brookBonded->getInverseMapStreamCount( K_Stream ) == 4 ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
536

Mark Friedrichs's avatar
Mark Friedrichs committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
      kinvmap_gather3_4( width,

                         inverseStreamMaps[I_Stream][0]->getBrookStream(),
                         inverseStreamMaps[I_Stream][1]->getBrookStream(),
                         inverseStreamMaps[I_Stream][2]->getBrookStream(),
                         bondedForceStreams[I_Stream]->getBrookStream(),

                         inverseStreamMaps[K_Stream][0]->getBrookStream(),
                         inverseStreamMaps[K_Stream][1]->getBrookStream(),
                         inverseStreamMaps[K_Stream][2]->getBrookStream(),
                         inverseStreamMaps[K_Stream][3]->getBrookStream(),
                         bondedForceStreams[K_Stream]->getBrookStream(),

                         forceStream.getBrookStream(), forceStream.getBrookStream() );

552
   } else if( _brookBonded->getInverseMapStreamCount( I_Stream ) == 3 && _brookBonded->getInverseMapStreamCount( K_Stream ) == 5 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570

      kinvmap_gather3_5( width,
                         inverseStreamMaps[I_Stream][0]->getBrookStream(),
                         inverseStreamMaps[I_Stream][1]->getBrookStream(),
                         inverseStreamMaps[I_Stream][2]->getBrookStream(),
                         bondedForceStreams[I_Stream]->getBrookStream(),
                         inverseStreamMaps[K_Stream][0]->getBrookStream(),
                         inverseStreamMaps[K_Stream][1]->getBrookStream(),
                         inverseStreamMaps[K_Stream][2]->getBrookStream(),
                         inverseStreamMaps[K_Stream][3]->getBrookStream(),
                         inverseStreamMaps[K_Stream][4]->getBrookStream(),
                         bondedForceStreams[K_Stream]->getBrookStream(),
                         forceStream.getBrookStream(), forceStream.getBrookStream() );

   } else {

      // case not handled -- throw an exception

Mark Friedrichs's avatar
Mark Friedrichs committed
571
      if( _brookBonded->getLog() && ErrorMessages++ < MaxErrorMessages ){
572
573
574
         (void) fprintf( _brookBonded->getLog(), "%s case: I-map=%d K-map=%d -- not handled.\n",
                          methodName.c_str(), _brookBonded->getInverseMapStreamCount( I_Stream ),
                                              _brookBonded->getInverseMapStreamCount( K_Stream ) );
Mark Friedrichs's avatar
Mark Friedrichs committed
575
576
577
         (void) fflush(  _brookBonded->getLog() );
      }

578
579
580
581
582
583
584
585
586
587
588
589
590
591
      kinvmap_gather3_3( width,

                         inverseStreamMaps[I_Stream][0]->getBrookStream(),
                         inverseStreamMaps[I_Stream][1]->getBrookStream(),
                         inverseStreamMaps[I_Stream][2]->getBrookStream(),
                         bondedForceStreams[I_Stream]->getBrookStream(),

                         inverseStreamMaps[K_Stream][0]->getBrookStream(),
                         inverseStreamMaps[K_Stream][1]->getBrookStream(),
                         inverseStreamMaps[K_Stream][2]->getBrookStream(),
                         bondedForceStreams[K_Stream]->getBrookStream(),

                         forceStream.getBrookStream(), forceStream.getBrookStream() );
/*
Mark Friedrichs's avatar
Mark Friedrichs committed
592
      std::stringstream message;
593
594
      message << methodName << "I-maps=" << _brookBonded->getInverseMapStreamCount( I_Stream ) << " and " << 
                               "K-maps=" << _brookBonded->getInverseMapStreamCount( K_Stream ) << " not handled.";
Mark Friedrichs's avatar
Mark Friedrichs committed
595
      throw OpenMMException( message.str() );
596
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
597
598
599

   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
600
601
602
603
604
605
606
607
608
609
   // diagnostics

   if( 0 && PrintOn ){

      (void) fprintf( getLog(), "\nPost 3_4/3_5 && NB forces" );
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
      brookStreamInternalF->printToFile( getLog() );

   }

610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
   if( _brookBonded->getInverseMapStreamCount( J_Stream ) == 5 && _brookBonded->getInverseMapStreamCount( L_Stream ) == 2 ){
      kinvmap_gather5_2( width,
                         inverseStreamMaps[J_Stream][0]->getBrookStream(),
                         inverseStreamMaps[J_Stream][1]->getBrookStream(),
                         inverseStreamMaps[J_Stream][2]->getBrookStream(),
                         inverseStreamMaps[J_Stream][3]->getBrookStream(),
                         inverseStreamMaps[J_Stream][4]->getBrookStream(),
                         bondedForceStreams[J_Stream]->getBrookStream(),
                         inverseStreamMaps[L_Stream][0]->getBrookStream(),
                         inverseStreamMaps[L_Stream][1]->getBrookStream(),
                         bondedForceStreams[L_Stream]->getBrookStream(),
                         forceStream.getBrookStream(), forceStream.getBrookStream() );
   
   } else {

      // case not handled -- throw an exception

Mark Friedrichs's avatar
Mark Friedrichs committed
627
      if( _brookBonded->getLog() && ErrorMessages++ < MaxErrorMessages ){
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
         (void) fprintf( _brookBonded->getLog(), "%s case: J-map=%d L-map=%d -- not handled.\n",
                          methodName.c_str(), _brookBonded->getInverseMapStreamCount( J_Stream ),
                                              _brookBonded->getInverseMapStreamCount( L_Stream ) );
         (void) fflush(  _brookBonded->getLog() );
      }

      kinvmap_gather5_2( width,
                         inverseStreamMaps[J_Stream][0]->getBrookStream(),
                         inverseStreamMaps[J_Stream][1]->getBrookStream(),
                         inverseStreamMaps[J_Stream][2]->getBrookStream(),
                         inverseStreamMaps[J_Stream][3]->getBrookStream(),
                         inverseStreamMaps[J_Stream][4]->getBrookStream(),
                         bondedForceStreams[J_Stream]->getBrookStream(),
                         inverseStreamMaps[L_Stream][0]->getBrookStream(),
                         inverseStreamMaps[L_Stream][1]->getBrookStream(),
                         bondedForceStreams[L_Stream]->getBrookStream(),
                         forceStream.getBrookStream(), forceStream.getBrookStream() );
/*
      std::stringstream message;
      message << methodName << "J-maps=" << _brookBonded->getInverseMapStreamCount( J_Stream ) << " and " << 
                               "L-maps=" << _brookBonded->getInverseMapStreamCount( L_Stream ) << " not handled.";
      throw OpenMMException( message.str() );
*/

   }
Mark Friedrichs's avatar
Mark Friedrichs committed
653

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
   // diagnostics

   if( 1 && PrintOn ){

      (void) fprintf( getLog(), "\nFinal NB & bonded forces" );
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
      brookStreamInternalF->printToFile( getLog() );
/*
      void* dataV = brookStreamInternalF->getData(1);
      float* data = (float*) dataV;
      (void) fprintf( getLog(), "\nFinal NB & bonded forces RAW\n" );
      for( int ii = 0; ii < _brookNonBonded->getNumberOfAtoms()*3; ii += 3 ){
         (void) fprintf( getLog(), "%d [%.6e %.6e %.6e]\n", ii, data[ii], data[ii+1], data[ii+2] );
      }
*/

   }

Mark Friedrichs's avatar
Mark Friedrichs committed
672
673
674
675
676
677
678
679
   // ---------------------------------------------------------------------------------------
}

/**
 * Execute the kernel to calculate the energy.
 * 
 * @param positions   atom positions
 *
680
 * @return  potential energy due to the NonbondedForce
Mark Friedrichs's avatar
Mark Friedrichs committed
681
682
683
684
 * Currently always return 0.0 since energies not calculated on gpu
 *
 */

685
double BrookCalcNonbondedForceKernel::executeEnergy( const Stream& atomPositions ){
Mark Friedrichs's avatar
Mark Friedrichs committed
686

Mark Friedrichs's avatar
Mark Friedrichs committed
687
688
// ---------------------------------------------------------------------------------------

689
   //static const std::string methodName      = "BrookCalcNonbondedForceKernel::executeEnergy";
Mark Friedrichs's avatar
Mark Friedrichs committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708

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

   const BrookStreamImpl& positionStreamC              = dynamic_cast<const BrookStreamImpl&> (atomPositions.getImpl());
   BrookStreamImpl& positionStream                     = const_cast<BrookStreamImpl&>         (positionStreamC);
   BrookOpenMMFloat* positionsF                        = (BrookOpenMMFloat*) positionStream.getData();

   OpenMMContext* context                              = getReferenceOpenMMContext( atomPositions.getSize() );

   vector<Vec3> positions( positionStream.getSize() );
   int index = 0;
   for( int ii = 0; ii < positionStream.getSize(); ii++, index += 3 ){
      positions[ii] = Vec3( positionsF[index], positionsF[index+1], positionsF[index+2] );
   }
   context->setPositions(positions);

   State state     = context->getState( State::Energy );
   double energy   = state.getPotentialEnergy();

709
// (void) fprintf( stdout, "BrookCalcNonbondedForceKernel::executeEnergy E=%.5e\n", energy ); fflush( stdout );
Mark Friedrichs's avatar
Mark Friedrichs committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723

   return energy;

}

/**
 * Get reference Context
 * 
 * @param numberOfAtoms  number of atoms
 *
 * @return  OpenMMContext
 *
 */

724
OpenMMContext* BrookCalcNonbondedForceKernel::getReferenceOpenMMContext( int numberOfAtoms ){
Mark Friedrichs's avatar
Mark Friedrichs committed
725
726
727

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

728
   //static const std::string methodName      = "BrookCalcNonbondedForceKernel::getReferenceOpenMMContext";
Mark Friedrichs's avatar
Mark Friedrichs committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

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

   if( _refOpenMMContext == NULL ){
      
      _referencePlatform       = new ReferencePlatform();
      _refSystem               = new System( numberOfAtoms, 0 ); 
      _refVerletIntegrator     = new VerletIntegrator( 0.01 );

      _refSystem->addForce( _refForceField );

      _refOpenMMContext        = new OpenMMContext( *_refSystem, *_refVerletIntegrator, *_referencePlatform );
   }

   return _refOpenMMContext;

}

/**
 * Execute the kernel to calculate the energy.
 * 
 * @param positions   atom positions
 *
752
 * @return  potential energy due to the NonbondedForce
Mark Friedrichs's avatar
Mark Friedrichs committed
753
754
755
756
 * Currently always return 0.0 since energies not calculated on gpu
 *
 */

757
double BrookCalcNonbondedForceKernel::executeEnergyOld( const Stream& atomPositions ){
Mark Friedrichs's avatar
Mark Friedrichs committed
758

Mark Friedrichs's avatar
Mark Friedrichs committed
759
760
// ---------------------------------------------------------------------------------------

761
   //static const std::string methodName      = "BrookCalcNonbondedForceKernel::executeEnergy";
Mark Friedrichs's avatar
Mark Friedrichs committed
762
763
764

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

765
766
767
768
769
770
771
772
773
774
   const BrookStreamImpl& positionStreamC              = dynamic_cast<const BrookStreamImpl&> (atomPositions.getImpl());
   BrookStreamImpl& positionStream                     = const_cast<BrookStreamImpl&>         (positionStreamC);
   BrookOpenMMFloat* positionsF                        = (BrookOpenMMFloat*) positionStream.getData();

   ReferencePlatform refPlatform;
   System system( atomPositions.getSize(), 0); 
   VerletIntegrator integrator( 0.01 );

   system.addForce( _refForceField );

Mark Friedrichs's avatar
Mark Friedrichs committed
775
   OpenMMContext context( system, integrator, refPlatform );
776
777
778
779
780
781
782
783
784
785
786
787
788

   vector<Vec3> positions( positionStream.getSize() );
   int index = 0;
   for( int ii = 0; ii < positionStream.getSize(); ii++, index += 3 ){
      positions[ii] = Vec3( positionsF[index], positionsF[index+1], positionsF[index+2] );
   }
   context.setPositions(positions);

   State state     = context.getState( State::Energy );
   double energy   = state.getPotentialEnergy();

   //delete forceField;

789
// (void) fprintf( stdout, "BrookCalcNonbondedForceKernel::executeEnergy E=%.5e\n", energy ); fflush( stdout );
790
791
792

   return energy;

Mark Friedrichs's avatar
Mark Friedrichs committed
793
}