BrookCalcStandardMMForceFieldKernel.cpp 32.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
/* -------------------------------------------------------------------------- *
 *                                   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 "BrookCalcStandardMMForceFieldKernel.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
39
40
#include "gpu/kforce.h"
#include "gpu/kinvmap_gather.h"
41
42
43
#include "ReferencePlatform.h"
#include "VerletIntegrator.h"
#include "StandardMMForceField.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

using namespace OpenMM;
using namespace std;

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

BrookCalcStandardMMForceFieldKernel::BrookCalcStandardMMForceFieldKernel( std::string name, const Platform& platform ) :
                     CalcStandardMMForceFieldKernel( name, platform ){

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

   // static const std::string methodName      = "BrookCalcStandardMMForceFieldKernel::BrookCalcStandardMMForceFieldKernel";

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

65
66
67
68
   _numberOfAtoms                           = 0;
   _brookBonded                             = NULL;
   _brookNonBonded                          = NULL;
   _refForceField                           = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

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

/** 
 * BrookCalcStandardMMForceFieldKernel destructor
 * 
 */

BrookCalcStandardMMForceFieldKernel::~BrookCalcStandardMMForceFieldKernel( ){

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

   // static const std::string methodName      = "BrookCalcStandardMMForceFieldKernel::BrookCalcStandardMMForceFieldKernel";

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

   delete _brookBonded;
   delete _brookNonBonded;
92
93
94
95

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

   //delete _refForceField;
Mark Friedrichs's avatar
Mark Friedrichs committed
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
}

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

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

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

int BrookCalcStandardMMForceFieldKernel::setLog( FILE* log ){
   _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)
 */
 
void BrookCalcStandardMMForceFieldKernel::initialize( 
        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] ){

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

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

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

   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 );
   }

   // ---------------------------------------------------------------------------------------
212
213
214
215
216
217
218
219
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
249
250
251
252
253
254
255

   // used for calculating energy

/*
   _referenceCalcStandardMMForceFieldKernel = new ReferenceCalcStandardMMForceFieldKernel( getName(), getPlatform() );
   _referenceCalcStandardMMForceFieldKernel->initialize( bondIndices,            bondParameters, 
                                                         angleIndices,           angleParameters,
                                                         periodicTorsionIndices, periodicTorsionParameters,
                                                         rbTorsionIndices,       rbTorsionParameters,
                                                         bonded14Indices,        lj14Scale, coulomb14Scale,
                                                         exclusions, nonbondedParameters,
                                                         nonbondedMethod, nonbondedCutoff, periodicBoxSize );
*/
   _refForceField = new StandardMMForceField( _numberOfAtoms, bondIndices.size(), angleIndices.size(), 
                                              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
256
                                                    (int) (periodicTorsionPrm[2] + 0.001), periodicTorsionPrm[1], periodicTorsionPrm[0] );
257
258
259
260
261
262
263
264
265
266
267
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
/*
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
300
301
302
303
304
305
306
307
308
309
310
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.
 */

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

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

   static const std::string methodName      = "BrookCalcStandardMMForceFieldKernel::executeForces";

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

321
322
   static const int PrintOn                 = 0;

Mark Friedrichs's avatar
Mark Friedrichs committed
323
324
325
326
327
328
329
330
331
332
333
   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());
   
334
335
336
337
338
339
340
341
342
343
344
345
346
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
   // 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
375
   if( 1 && PrintOn ){
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
      (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
406
407
// ---------------------------------------------------------------------------------------

408
409
410
411
412
413
414
415
416
417
418
419
420
421
   // 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
422
423
424
425
426
427
428
429
430
431
432
433
   // diagnostics

   if( 0 && PrintOn ){

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

   }

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

434
435
436
   // bonded

         epsfac                                        = (float) (_brookBonded->getLJ_14Scale()*_brookBonded->getCoulombFactor());
Mark Friedrichs's avatar
Mark Friedrichs committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
   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(),
454
                 _brookBonded->getChargeStream()->getBrookStream(),
Mark Friedrichs's avatar
Mark Friedrichs committed
455
456
457
458
459
460
461
462
463
464
465
                 _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() );

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

   // 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
488
489
490
491
492
493
494
495

/*
      (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 ) );
496
497
498
499
500
501
502
503
      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
504
505
   // gather forces

506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
   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
523

Mark Friedrichs's avatar
Mark Friedrichs committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
      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() );

539
   } else if( _brookBonded->getInverseMapStreamCount( I_Stream ) == 3 && _brookBonded->getInverseMapStreamCount( K_Stream ) == 5 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

      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

      if( _brookBonded->getLog() ){
559
560
561
         (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
562
563
564
         (void) fflush(  _brookBonded->getLog() );
      }

565
566
567
568
569
570
571
572
573
574
575
576
577
578
      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
579
      std::stringstream message;
580
581
      message << methodName << "I-maps=" << _brookBonded->getInverseMapStreamCount( I_Stream ) << " and " << 
                               "K-maps=" << _brookBonded->getInverseMapStreamCount( K_Stream ) << " not handled.";
Mark Friedrichs's avatar
Mark Friedrichs committed
582
      throw OpenMMException( message.str() );
583
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
584
585
586

   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
587
588
589
590
591
592
593
594
595
596
   // diagnostics

   if( 0 && PrintOn ){

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

   }

597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
   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

      if( _brookBonded->getLog() ){
         (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
640

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
   // 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
659
660
661
662
663
664
665
666
667
668
669
670
671
   // ---------------------------------------------------------------------------------------
}

/**
 * Execute the kernel to calculate the energy.
 * 
 * @param positions   atom positions
 *
 * @return  potential energy due to the StandardMMForceField
 * Currently always return 0.0 since energies not calculated on gpu
 *
 */

672
double BrookCalcStandardMMForceFieldKernel::executeEnergy( const Stream& atomPositions ){
Mark Friedrichs's avatar
Mark Friedrichs committed
673
674
675
676
677
678
679

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

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

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

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
   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 );

   OpenMMContext context(system, integrator, refPlatform);

   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;

// (void) fprintf( stdout, "BrookCalcStandardMMForceFieldKernel::executeEnergy E=%.5e\n", energy ); fflush( stdout );

   return energy;

Mark Friedrichs's avatar
Mark Friedrichs committed
708
}