BrookNonBonded.cpp 46.8 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
10
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
Mark Friedrichs's avatar
Mark Friedrichs committed
11
12
 * Contributors:                                                              *
 *                                                                            *
13
14
15
16
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
17
 *                                                                            *
18
19
20
21
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
22
 *                                                                            *
23
24
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
Mark Friedrichs's avatar
Mark Friedrichs committed
25
26
27
28
29
 * -------------------------------------------------------------------------- */

#include "BrookNonBonded.h"
#include "BrookPlatform.h"
#include "BrookStreamFactory.h"
30
#include "openmm/OpenMMException.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
31
32
//#include "kernels/invmap.h"
#include "kernels/kforce.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
33

34
35
#include <sstream>

Mark Friedrichs's avatar
Mark Friedrichs committed
36
37
38
39
40
41
42
43
using namespace OpenMM;
using namespace std;

/** 
 * Constructor
 * 
 */

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
44
BrookNonBonded::BrookNonBonded( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
45
46
47
48
49
50
51

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
52
   _particleSizeCeiling       = -1;
Mark Friedrichs's avatar
Mark Friedrichs committed
53
54
   _outerUnroll               =  4;
   _innerUnroll               =  4;
Mark Friedrichs's avatar
Mark Friedrichs committed
55
56
57
58
59

   _partialForceStreamWidth   = 64;
   _partialForceStreamHeight  = -1;
   _partialForceStreamSize    = -1;

Mark Friedrichs's avatar
Mark Friedrichs committed
60
   _duplicationFactor         =  4;
Mark Friedrichs's avatar
Mark Friedrichs committed
61
62
63
64
65

   _exclusionStreamWidth      = -1;
   _exclusionStreamHeight     = -1;
   _exclusionStreamSize       = -1;

Mark Friedrichs's avatar
Mark Friedrichs committed
66
   _jStreamWidth              = 16;
Mark Friedrichs's avatar
Mark Friedrichs committed
67
68
69
   _jStreamHeight             = -1;
   _jStreamSize               = -1;

Mark Friedrichs's avatar
Mark Friedrichs committed
70
71
72
   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      _nonbondedStreams[ii] = NULL;
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      _nonbondedForceStreams[ii] = NULL;
   }

}   
 
/** 
 * Destructor
 * 
 */

BrookNonBonded::~BrookNonBonded( ){

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
94
95
96
   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      delete _nonbondedStreams[ii];
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
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

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      delete _nonbondedForceStreams[ii];
   }

}

/** 
 * Get number of force streams
 * 
 * @return  number of force streams (fixed value)
 *
 */

int BrookNonBonded::getNumberOfForceStreams( void ) const {
   return NumberOfForceStreams;
}

/** 
 * Get inner loop unroll
 * 
 * @return   inner loop unroll (fixed value)
 *
 */

int BrookNonBonded::getInnerLoopUnroll( void ) const {
   return _innerUnroll;
}

/** 
 * Get outer loop unroll
 * 
 * @return   outer loop unroll (fixed value)
 *
 */

int BrookNonBonded::getOuterLoopUnroll( void ) const {
   return _outerUnroll;
}

/** 
 * Set outer loop unroll
 * 
 * @param  outer loop unroll (fixed value)
 *
 * @return updated outer loop unroll (fixed value)
 *
 */

int BrookNonBonded::setOuterLoopUnroll( int outerUnroll ){
   if( outerUnroll != _outerUnroll ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
148
      _particleSizeCeiling = -1;
Mark Friedrichs's avatar
Mark Friedrichs committed
149
150
151
152
153
154
   }
   _outerUnroll = _outerUnroll;
   return _outerUnroll;
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
155
 * Get particle ceiling parameter
Mark Friedrichs's avatar
Mark Friedrichs committed
156
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
157
 * @return particle ceiling parameter
Mark Friedrichs's avatar
Mark Friedrichs committed
158
159
160
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
161
int BrookNonBonded::getParticleSizeCeiling( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
162

Mark Friedrichs's avatar
Mark Friedrichs committed
163
164
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
165
   //static const std::string methodName      = "BrookNonBonded::getParticleSizeCeiling";
Mark Friedrichs's avatar
Mark Friedrichs committed
166
167
168

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
169
   if( _particleSizeCeiling < 0 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
170
      BrookNonBonded* localThis = const_cast<BrookNonBonded* const>(this);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
171
172
173
      localThis->_particleSizeCeiling = localThis->getNumberOfParticles() % localThis->getOuterLoopUnroll();
      if( localThis->_particleSizeCeiling ){
         localThis->_particleSizeCeiling = localThis->getOuterLoopUnroll() - localThis->_particleSizeCeiling;
Mark Friedrichs's avatar
Mark Friedrichs committed
174
      }   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
175
      localThis->_particleSizeCeiling += localThis->getNumberOfParticles();
Mark Friedrichs's avatar
Mark Friedrichs committed
176
177
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
178
   return _particleSizeCeiling;
Mark Friedrichs's avatar
Mark Friedrichs committed
179
180
181
182
183
184
185
186
187
188
189
190
191
}

/** 
 * Get duplication factor
 * 
 * @return   duplication factor
 *
 */

int BrookNonBonded::getDuplicationFactor( void ) const {
   return _duplicationFactor;
}

192
193
194
195
196
197
198
199
200
201
202
203
204
205
/** 
 * Set duplication factor
 * 
 * @param   duplication factor
 *
 * @return  DefaultReturnValue
 *
 */

int BrookNonBonded::setDuplicationFactor( int duplicationFactor ){
   _duplicationFactor = duplicationFactor;
   return DefaultReturnValue;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
206
207
208
209
210
211
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
256
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/** 
 * Get j-stream width
 * 
 * @return  j-stream width
 *
 */

int BrookNonBonded::getJStreamWidth( void ) const {

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

   // static const std::string methodName      = "BrookNonBonded::getJStreamWidth";

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

   return _jStreamWidth;
}

/** 
 * Get j-stream height
 * 
 * @return  j-stream height 
 *
 */

int BrookNonBonded::getJStreamHeight( void ) const {

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

   // static const std::string methodName      = "BrookNonBonded::getJStreamHeight";

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

   return _jStreamHeight;
}

/** 
 * Get j-stream size
 * 
 * @return  j-stream size
 *
 */

int BrookNonBonded::getJStreamSize( void ) const {
   return _jStreamSize;
}

/** 
 * Get partial force stream width
 * 
 * @param platform  platform
 *
 * @return  partial force stream width
 *
 */

int BrookNonBonded::getPartialForceStreamWidth( const Platform& platform ){

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

   // static const std::string methodName      = "BrookNonBonded::getPartialForceStreamWidth";

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

   // get partial force stream width

   if( _partialForceStreamWidth < 0 ){
      //_getPartialForceStreamDimensions( platform );
   }
   return _partialForceStreamWidth;
}

/** 
 * Get partial force stream width
 * 
 * @return  partial force stream width
 *
 */

int BrookNonBonded::getPartialForceStreamWidth( void ) const {

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

   // static const std::string methodName      = "BrookNonBonded::getPartialForceStreamWidth";

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

   return _partialForceStreamWidth;
}

/** 
 * Get partial force stream height
 * 
 * @param platform platform
 *
 * @return  partial force stream height 
 *
 */

int BrookNonBonded::getPartialForceStreamHeight( const Platform& platform ){

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

   // static const std::string methodName      = "BrookNonBonded::getPartialForceStreamHeight";

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

   // get partial force stream height

   if( _partialForceStreamHeight < 0 ){
      //_getPartialForceStreamDimensions( platform );
   }
   return _partialForceStreamHeight;
}

/** 
 * Get partial force stream height
 * 
 * @return  partial force stream height 
 *
 */

int BrookNonBonded::getPartialForceStreamHeight( void ) const {

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

   // static const std::string methodName      = "BrookNonBonded::getPartialForceStreamHeight";

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

   return _partialForceStreamHeight;
}

/** 
 * Get partial force stream size
 * 
 * @param platform  platform
 *
 * @return  partial force stream size
 *
 */

int BrookNonBonded::getPartialForceStreamSize( const Platform& platform ){

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

   // static const std::string methodName      = "BrookNonBonded::getPartialForceStreamSize";

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

   // get partial force stream size

   if( _partialForceStreamSize < 0 ){
      //_getPartialForceStreamDimensions( platform );
   }
   return _partialForceStreamSize;
}

/** 
 * Get partial force stream size
 * 
 * @return  partial force stream size
 *
 */

int BrookNonBonded::getPartialForceStreamSize( void ) const {
   return _partialForceStreamSize;
}

/** 
 * Get exclusion stream size
 *
 * @return  exclusion stream size
 *
 */

int BrookNonBonded::getExclusionStreamSize( void ) const {
   return _exclusionStreamSize;
}

/** 
 * Get exclusion stream width
 *
 * @return  exclusion stream width
 *
 */

int BrookNonBonded::getExclusionStreamWidth( void ) const {
   return _exclusionStreamWidth;
}

/** 
 * Get exclusion stream 
 *
 * @return  exclusion stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
404
405
BrookFloatStreamInternal* BrookNonBonded::getExclusionStream( void ) const {
   return _nonbondedStreams[ExclusionStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
406
407
408
409
410
411
412
413
414
}

/** 
 * Get vdw stream 
 *
 * @return  vdw stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
415
416
BrookFloatStreamInternal* BrookNonBonded::getOuterVdwStream( void ) const {
   return _nonbondedStreams[OuterVdwStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
417
418
419
420
421
422
423
424
425
}

/** 
 * Get charge stream 
 *
 * @return  charge stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
426
427
BrookFloatStreamInternal* BrookNonBonded::getChargeStream( void ) const {
   return _nonbondedStreams[ChargeStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
428
429
430
431
432
433
434
435
436
}

/** 
 * Get sigma stream 
 *
 * @return  sigma stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
437
438
BrookFloatStreamInternal* BrookNonBonded::getInnerSigmaStream( void ) const {
   return _nonbondedStreams[InnerSigmaStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
439
440
441
442
443
444
445
446
447
}

/** 
 * Get epsilon stream 
 *
 * @return  epsilon stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
448
449
BrookFloatStreamInternal* BrookNonBonded::getInnerEpsilonStream( void ) const {
   return _nonbondedStreams[InnerEpsilonStream];
Mark Friedrichs's avatar
Mark Friedrichs committed
450
451
452
453
454
455
456
457
458
}

/** 
 * Get force streams 
 *
 * @return  force streams
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
459
BrookFloatStreamInternal** BrookNonBonded::getForceStreams( void ){
Mark Friedrichs's avatar
Mark Friedrichs committed
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
   return _nonbondedForceStreams;
}

/** 
 * Return true if force[index] stream is set
 *
 * @param    index into force stream
 * @return   true if index is valid && force[index] stream is set; else false
 *
 */

int BrookNonBonded::isForceStreamSet( int index ) const {
   return (index >= 0 && index < getNumberOfForceStreams() && _nonbondedForceStreams[index]) ? 1 : 0;
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
476
 * Initialize stream dimensions
Mark Friedrichs's avatar
Mark Friedrichs committed
477
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
478
 * @param numberOfParticles         number of particles
Mark Friedrichs's avatar
Mark Friedrichs committed
479
480
481
482
483
484
 * @param platform                  platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
485
int BrookNonBonded::_initializeStreamSizes( int numberOfParticles, const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
486
487
488

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

489
   static const std::string methodName      = "BrookNonBonded::_initializeStreamSizes";
Mark Friedrichs's avatar
Mark Friedrichs committed
490
491
492
493
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
494
495
   int particleStreamSize                  = getParticleStreamSize( platform );
   int particleStreamWidth                 = getParticleStreamWidth( platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
496

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
497
498
499
500
   _initializeExclusionStreamSize( particleStreamSize, particleStreamWidth );
   _initializeJStreamSize( particleStreamSize, particleStreamWidth );
   _initializeOuterVdwStreamSize( particleStreamSize, particleStreamWidth );
   _initializePartialForceStreamSize( particleStreamSize, particleStreamWidth );
Mark Friedrichs's avatar
Mark Friedrichs committed
501
502
503
504
505
506
507
508
509
510
511
512
513

   return DefaultReturnValue;
}

/** 
 * Initialize streams
 * 
 * @param platform                  platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

514
int BrookNonBonded::_initializeStreams( const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
515
516
517

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

518
   static const std::string methodName      = "BrookNonBonded::_initializeStreams";
Mark Friedrichs's avatar
Mark Friedrichs committed
519
   static const double dangleValue          = 0.0;
Mark Friedrichs's avatar
Mark Friedrichs committed
520
521
522
523
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
524
   const BrookStreamFactory& brookStreamFactory = dynamic_cast<const BrookStreamFactory&> (platform.getDefaultStreamFactory());
Mark Friedrichs's avatar
Mark Friedrichs committed
525

Mark Friedrichs's avatar
Mark Friedrichs committed
526
   // exclusion
Mark Friedrichs's avatar
Mark Friedrichs committed
527

528
529
530
   _nonbondedStreams[ExclusionStream]           = new BrookFloatStreamInternal( BrookCommon::NonBondedExclusionStream,
                                                                               getExclusionStreamSize(), getExclusionStreamWidth(),
                                                                               BrookStreamInternal::Float, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
531

Mark Friedrichs's avatar
Mark Friedrichs committed
532
   // outer vdw
Mark Friedrichs's avatar
Mark Friedrichs committed
533

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
534
535
   _nonbondedStreams[OuterVdwStream]            = new BrookFloatStreamInternal( BrookCommon::OuterVdwStream, getParticleStreamSize(), 
                                                                                getParticleStreamWidth(), BrookStreamInternal::Float2, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
536

Mark Friedrichs's avatar
Mark Friedrichs committed
537
   // inner sigma & epsilon
Mark Friedrichs's avatar
Mark Friedrichs committed
538

539
540
   _nonbondedStreams[InnerSigmaStream]          = new BrookFloatStreamInternal( BrookCommon::InnerSigmaStream, getJStreamSize(), 
                                                                                getJStreamWidth(), BrookStreamInternal::Float4, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
541

542
543
   _nonbondedStreams[InnerEpsilonStream]        = new BrookFloatStreamInternal( BrookCommon::InnerEpsilonStream, getJStreamSize(),
                                                                                getJStreamWidth(), BrookStreamInternal::Float4, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
544
545

   // charge stream
Mark Friedrichs's avatar
Mark Friedrichs committed
546

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
547
548
   _nonbondedStreams[ChargeStream]              = new BrookFloatStreamInternal( BrookCommon::NonBondedChargeStream, getParticleStreamSize(),
                                                                                getParticleStreamWidth(), BrookStreamInternal::Float, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
549
550

   
551
552
   // partial force streams

Mark Friedrichs's avatar
Mark Friedrichs committed
553
   std::string partialForceStream = BrookCommon::PartialForceStream;
Mark Friedrichs's avatar
Mark Friedrichs committed
554
555
556
557
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      std::stringstream name;
      name << partialForceStream << ii;
      _nonbondedForceStreams[ii] = new BrookFloatStreamInternal( name.str(), getPartialForceStreamSize(),
558
                                                                 getPartialForceStreamWidth(), BrookStreamInternal::Float3, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
559
560
561
562
563
564
565
566
   }

   return DefaultReturnValue;
}

/** 
 * Set exclusion (4x4)
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
567
568
 * @param i                         particle i index
 * @param j                         particle j index
Mark Friedrichs's avatar
Mark Friedrichs committed
569
570
571
572
573
574
575
 * @param exclusionStreamWidth      exclusion stream width
 * @param exclusion                 array of packed exclusions
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

576
int BrookNonBonded::_setExclusion( int i, int j, int exclusionStreamWidth, BrookOpenMMFloat* exclusion ){
Mark Friedrichs's avatar
Mark Friedrichs committed
577
578
579

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

580
   static const std::string methodName      = "BrookNonBonded::_setExclusion";
Mark Friedrichs's avatar
Mark Friedrichs committed
581
582
583

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

Mark Friedrichs's avatar
Mark Friedrichs committed
584
   int index    = ( (j/4)* exclusionStreamWidth + i ); // index is the x coordinate!
Mark Friedrichs's avatar
Mark Friedrichs committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
   int joffset  = j % 4; 

   BrookOpenMMFloat flag;
   switch( joffset ) {

      case 0: 
         flag = (BrookOpenMMFloat) 2.0; 
         break;

      case 1:
         flag = (BrookOpenMMFloat) 3.0; 
         break;

      case 2: 
         flag = (BrookOpenMMFloat) 5.0; 
         break;

      case 3 :  
         flag = (BrookOpenMMFloat) 7.0; 
         break;

   }

   exclusion[index] /= flag;

   return DefaultReturnValue;
}

/** 
 * Initialize exclusions
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
616
 * @param exclusions            vector of sets containing exclusions (1 set entry for every particle)
Mark Friedrichs's avatar
Mark Friedrichs committed
617
618
619
620
621
622
623
 * @param platform              Brook platform
 * @param log                   optional Log file reference
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

624
int BrookNonBonded::_initializeExclusions( const std::vector<std::set<int> >& exclusionsVector, const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
625
626
627

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

628
   static const std::string methodName      = "BrookNonBonded::_initializeExclusions";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
629
   static const int debug                   = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
630
631
632
633

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

   if( debug && getLog() ){
634
      (void) fprintf( getLog(), "%s exclusions vector size=%d\n", methodName.c_str(), exclusionsVector.size() );
Mark Friedrichs's avatar
Mark Friedrichs committed
635
636
637
638
639
   }

   int exclusionStreamSize = getExclusionStreamSize();
   if( exclusionStreamSize < 1 ){
      std::stringstream message;
640
      message << methodName << " exclusionStreamSize=" << exclusionStreamSize << " is not set.";
Mark Friedrichs's avatar
Mark Friedrichs committed
641
642
643
644
645
646
647
648
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   // allocate and initialize exclusions array

   BrookOpenMMFloat* exclusions = new BrookOpenMMFloat[exclusionStreamSize];
   for( unsigned int ii = 0; ii < (unsigned int) exclusionStreamSize; ii++ ){
649
      exclusions[ii] = (BrookOpenMMFloat) 210.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
650
651
652
653
654
   }

   // pack in values

   int  exclusionStreamWidth = getExclusionStreamWidth();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
655
656
   int  numberOfParticles    = getNumberOfParticles();
   int  particleStreamSize       = getParticleStreamSize();
Mark Friedrichs's avatar
Mark Friedrichs committed
657
658
659
   for( unsigned int ii = 0; ii < exclusionsVector.size(); ii++ ){

      set<int> exclusionIndices  = exclusionsVector[ii];
660

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
661
//(void) fprintf( getLog(), "%s particles=%d excludeSz=%d\n", methodName.c_str(), ii, exclusionIndices.size() );
662
663

      int hitII = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
664
      for( set<int>::const_iterator jj = exclusionIndices.begin(); jj != exclusionIndices.end(); jj++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
665
//(void) fprintf( getLog(), "%s particles=%d exclude=%d\n", methodName.c_str(), ii, *jj );
666
667
668
669
670
         _setExclusion( ii, *jj, exclusionStreamWidth, exclusions );
         if( *jj == ii )hitII = 1;
      }
      if( !hitII ){
         _setExclusion( ii, ii, exclusionStreamWidth, exclusions );
Mark Friedrichs's avatar
Mark Friedrichs committed
671
672
      }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
673
674
      // explicitly exclude junk particles from interacting w/ particle ii
      for( int jj = numberOfParticles; jj < particleStreamSize; jj++ ){
675
         _setExclusion( ii, jj, exclusionStreamWidth, exclusions );
Mark Friedrichs's avatar
Mark Friedrichs committed
676
677
678
      }
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
679
   // explicitly exclude junk particles from interacting w/ all particles
Mark Friedrichs's avatar
Mark Friedrichs committed
680

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
681
682
   for( int ii = numberOfParticles; ii < particleStreamSize; ii++ ){
      for( int jj = 0; jj < particleStreamSize; jj++ ){
683
         _setExclusion( ii, jj, exclusionStreamWidth, exclusions );
Mark Friedrichs's avatar
Mark Friedrichs committed
684
685
686
      }
   }

687
688
689
690
   // diagnostics

   if( 1 && debug && getLog() ){
      FILE* log = getLog();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
691
692
      (void) fprintf( log, "%s particles=%d excl=%d exStrSz=%d w=%d atmStrSz=%d\n", methodName.c_str(), numberOfParticles, exclusionsVector.size(), 
                      exclusionStreamSize, exclusionStreamWidth, particleStreamSize );
693
694
695
696
697
698
699
700
/*
      for( int ii = 0; ii < exclusionStreamSize; ii++ ){
int index  = ii/exclusionStreamWidth;
int offset = ii - index*exclusionStreamWidth;
         (void) fprintf( log, "   %6d %6d [%6d %6d] %10.0f\n", ii, offset, index*4, index*4+3, exclusions[ii] );
      }
*/

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
701
      for( int ii = 0; ii < numberOfParticles; ii++ ){
702
703
         (void) fprintf( log, "%6d ", ii );
         int count = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
704
         for( int jj = 0; jj <= numberOfParticles/4; jj++ ){
705
706
707
708
            int index = jj*exclusionStreamWidth + ii;
            (void) fprintf( log, " [%4d %4d] %5.1f", jj*4, jj*4+3, exclusions[index] );
            int excludeValue = (int) (exclusions[index] + 0.01);
            if( (excludeValue % 2) )count++;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
709
710
711
            if( (jj*4+1) < numberOfParticles && (excludeValue % 3) )count++;
            if( (jj*4+2) < numberOfParticles && (excludeValue % 5) )count++;
            if( (jj*4+3) < numberOfParticles && (excludeValue % 7) )count++;
712
713
714
715
716
717
718
         }
         (void) fprintf( log, " TtlExcl=%d\n", count );
      }
         
      (void) fflush( log );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
719
720
   // load stream

Mark Friedrichs's avatar
Mark Friedrichs committed
721
   _nonbondedStreams[ExclusionStream]->loadFromArray( exclusions ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
   delete[] exclusions;

   return DefaultReturnValue;
}

/** 
 * Set sigma & epsilon given c6 & c12 (geometric rule)
 * 
 * @param c6                        vdw c6
 * @param c12                       vdw c12
 * @param sigma                     massaged sigma
 * @param epsilon                   massaged epsilon
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

739
int BrookNonBonded::_setSigmaEpsilon( double c6, double c12, double* sigma , double* epsilon ){
Mark Friedrichs's avatar
Mark Friedrichs committed
740
741
742

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

743
   // static const std::string methodName      = "BrookNonBonded::_setSigmaEpsilon";
Mark Friedrichs's avatar
Mark Friedrichs committed
744
745
746
747
748
749
750

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

   if( fabs( c12 ) < 1.0e-04 ){
      *sigma    = 1.0;
      *epsilon  = 0.0;
   } else {
751
752
753
754
      //*sigma    = 0.5*pow( c12/c6, 1.0/6.0 );
      //*epsilon  = sqrt( c6*c6/c12 );
      *sigma    = 0.5*c6;
      *epsilon  = 2.0*sqrt( c12 );
Mark Friedrichs's avatar
Mark Friedrichs committed
755
756
757
758
759
760
761
762
   }

   return DefaultReturnValue;
}

/** 
 * Initialize vdw & charge
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
763
 * @param exclusions                vector of sets containing exclusions (1 set entry for every particle)
Mark Friedrichs's avatar
Mark Friedrichs committed
764
765
766
767
768
769
 * @param platform                  platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

770
int BrookNonBonded::_initializeVdwAndCharge( const vector<vector<double> >& nonbondedParameters, const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
771
772
773

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

774
   static const std::string methodName      = "BrookNonBonded::_initializeVdwAndCharge";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
775
   static const int debug                   = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
776
777
778
779

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

   if( debug && getLog() ){
780
      (void) fprintf( getLog(), "%s nonbonded vector size=%d\n", methodName.c_str(), nonbondedParameters.size() );
Mark Friedrichs's avatar
Mark Friedrichs committed
781
782
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
783
   int particleStreamSize        = getParticleStreamSize();
Mark Friedrichs's avatar
Mark Friedrichs committed
784
785
786

   // allocate and initialize vdw & charge array

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
787
   unsigned int vdwParametersSize  = 2*particleStreamSize;
Mark Friedrichs's avatar
Mark Friedrichs committed
788
789
790
   BrookOpenMMFloat* vdwParameters = new BrookOpenMMFloat[vdwParametersSize];
   memset( vdwParameters, 0, sizeof( BrookOpenMMFloat )*vdwParametersSize );
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
791
792
   BrookOpenMMFloat* charges       = new BrookOpenMMFloat[particleStreamSize];
   memset( charges, 0, sizeof( BrookOpenMMFloat )*particleStreamSize );
Mark Friedrichs's avatar
Mark Friedrichs committed
793
   
794
795
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
796
797
   // pack in values

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
798
   int  numberOfParticles    = getNumberOfParticles();
Mark Friedrichs's avatar
Mark Friedrichs committed
799
   int trackingIndex         = 0;
800
   double sigma, epsilon;
Mark Friedrichs's avatar
Mark Friedrichs committed
801
802
   for( unsigned int ii = 0; ii < nonbondedParameters.size(); ii++ ){

Mark Friedrichs's avatar
Mark Friedrichs committed
803
804
      vector<double> nonbondedParameterVector 
                                     = nonbondedParameters[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
805

806
807
808
809
810
811
812
813
814
      double c6                      = nonbondedParameterVector[1];
      double c12                     = nonbondedParameterVector[2];
      double charge                  = nonbondedParameterVector[0];

/*
      double charge                  = nonbondedParameterVector[0];
      double sigma                   = nonbondedParameterVector[1];
      double epsilon                 = nonbondedParameterVector[2];
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
815
816
817

      // geometric combination rules

818
      _setSigmaEpsilon( c6, c12, &sigma, &epsilon );
Mark Friedrichs's avatar
Mark Friedrichs committed
819
820
821
822
823
824
825

      vdwParameters[trackingIndex++] = (BrookOpenMMFloat) sigma;
      vdwParameters[trackingIndex++] = (BrookOpenMMFloat) epsilon;
      charges[ii]                    = (BrookOpenMMFloat) charge;
         
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
826
   // set outlier particles
Mark Friedrichs's avatar
Mark Friedrichs committed
827

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
828
   for( unsigned int ii = nonbondedParameters.size(); ii < (unsigned int) particleStreamSize; ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
829
830
831
832
      vdwParameters[trackingIndex++] = (BrookOpenMMFloat) 1.0;
      vdwParameters[trackingIndex++] = (BrookOpenMMFloat) 0.0;
   }

833
834
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
835
836
   // load stream

Mark Friedrichs's avatar
Mark Friedrichs committed
837
838
   _nonbondedStreams[OuterVdwStream]->loadFromArray( vdwParameters ); 
   _nonbondedStreams[ChargeStream]->loadFromArray( charges ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
839

840
841
842
843
844
   // diagnostics

   if( 1 && debug && getLog() ){
      FILE* log = getLog();
      int trackingIndex         = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
845
846
      (void) fprintf( log, "%s particles=%d strSz=%d\n   vdw[Sig Eps] Q\n", methodName.c_str(), numberOfParticles, particleStreamSize );
      for( int ii = 0; ii < particleStreamSize; ii++, trackingIndex += 2 ){
847
848
849
850
851
         (void) fprintf( log, "   %d %10.3f %10.3f %10.3f\n", ii, vdwParameters[trackingIndex], vdwParameters[trackingIndex+1], charges[ii] );
      }
      (void) fflush( log );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
852
853
854
855
856
857
   delete[] vdwParameters;
   delete[] charges;

   return DefaultReturnValue;
}

858
859
860
861

/** 
 * Initialize vdw & charge
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
862
 * @param exclusions                vector of sets containing exclusions (1 set entry for every particle)
863
864
865
866
867
868
869
870
871
872
873
 * @param platform                  platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

int BrookNonBonded::_initializeJStreamVdw( const vector<vector<double> >& nonbondedParameters, const Platform& platform ){

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

   static const std::string methodName      = "BrookNonBonded::_initializeJStreamVdw";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
874
   static const int debug                   = 0;
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897

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

   if( debug && getLog() ){
      (void) fprintf( getLog(), "%s nonbonded vector size=%d\n", methodName.c_str(), nonbondedParameters.size() );
   }

   int jStreamSize        = getJStreamSize();

   // allocate and initialize vdw & charge array

   unsigned int jParametersSize        = 4*jStreamSize;

   BrookOpenMMFloat* sigmaParameters   = new BrookOpenMMFloat[jParametersSize];
   //memset( sigmaParameters, 0, sizeof( BrookOpenMMFloat )*jParametersSize );
   
   BrookOpenMMFloat* epsilonParameters = new BrookOpenMMFloat[jParametersSize];
   //memset( epsilonParameters, 0, sizeof( BrookOpenMMFloat )*jParametersSize );
   
// ---------------------------------------------------------------------------------------

   // pack in values

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
898
   int  numberOfParticles    = getNumberOfParticles();
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
   int trackingIndex         = 0;
   double sigma, epsilon;
   for( unsigned int ii = 0; ii < nonbondedParameters.size(); ii++ ){

      vector<double> nonbondedParameterVector 
                                     = nonbondedParameters[ii];

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

      //double sigma                   = nonbondedParameterVector[1];
      //double epsilon                 = nonbondedParameterVector[2];

      // geometric combination rules

      _setSigmaEpsilon( c6, c12, &sigma, &epsilon );

      sigmaParameters[ii]   = (BrookOpenMMFloat) sigma;
      epsilonParameters[ii] = (BrookOpenMMFloat) epsilon;
         
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
921
   // set outlier particles
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939

   for( unsigned int ii = nonbondedParameters.size(); ii < (unsigned int) jParametersSize; ii++ ){
      sigmaParameters[ii]   = (BrookOpenMMFloat) 1.0;
      epsilonParameters[ii] = (BrookOpenMMFloat) 0.0;
   }

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

   // load streams

   _nonbondedStreams[InnerSigmaStream]->loadFromArray( sigmaParameters ); 
   _nonbondedStreams[InnerEpsilonStream]->loadFromArray( epsilonParameters ); 

   // diagnostics

   if( 1 && debug && getLog() ){
      FILE* log = getLog();
      int trackingIndex         = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
940
      (void) fprintf( log, "%s particles=%d strSz=%d\n   Innervdw[Sig Eps]\n", methodName.c_str(), numberOfParticles, jStreamSize );
941
942
943
944
945
946
947
948
949
950
951
952
      for( unsigned int ii = 0; ii < jParametersSize; ii++ ){
         (void) fprintf( log, "   %d %10.3f %10.3f\n", ii, sigmaParameters[ii], epsilonParameters[ii] );
      }
      (void) fflush( log );
   }

   delete[] sigmaParameters;
   delete[] epsilonParameters;

   return DefaultReturnValue;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
953
954
955
/* 
 * Setup of nonbonded ixns
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
956
957
958
959
 * @param numberOfParticles     number of particles
 * @param nonbondedParameters   vector of nonbonded parameters [particleI][0=c6]
 *                                                             [particleI][1=c12]
 *                                                             [particleI][2=charge]
Mark Friedrichs's avatar
Mark Friedrichs committed
960
961
962
963
964
965
 * @param platform              Brook platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
966
int BrookNonBonded::setup( int numberOfParticles, const std::vector<std::vector<double> >& nonbondedParameters,
Mark Friedrichs's avatar
Mark Friedrichs committed
967
                           const std::vector<std::set<int> >& exclusions, const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
968
969
970

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

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
971
   //static const std::string methodName      = "BrookNonBonded::setup";
Mark Friedrichs's avatar
Mark Friedrichs committed
972
973
974

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
975
   setNumberOfParticles( numberOfParticles );
Mark Friedrichs's avatar
Mark Friedrichs committed
976

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
977
   _initializeStreamSizes( numberOfParticles, platform );
978
   _initializeStreams( platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
979

980
981
982
   _initializeExclusions( exclusions, platform );
   _initializeVdwAndCharge( nonbondedParameters, platform );
   _initializeJStreamVdw( nonbondedParameters, platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
983

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
984
985
   setIsActive( 1 );

Mark Friedrichs's avatar
Mark Friedrichs committed
986
987
988
989
   return DefaultReturnValue;
}

/* 
Mark Friedrichs's avatar
Mark Friedrichs committed
990
991
 * Setup of stream dimensions for exclusion stream
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
992
993
 * @param particleStreamSize        particle stream size
 * @param particleStreamWidth       particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
994
995
996
997
998
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
999
int BrookNonBonded::_initializeExclusionStreamSize( int particleStreamSize, int particleStreamWidth ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1000
1001
1002

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

1003
   static const std::string methodName      = "BrookNonBonded::_initializeExclusionStreamSize";
Mark Friedrichs's avatar
Mark Friedrichs committed
1004
1005
1006
1007
   //static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1008
   _exclusionStreamWidth     = particleStreamSize;
Mark Friedrichs's avatar
Mark Friedrichs committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
   int innerUnroll           = getInnerLoopUnroll();
   if( innerUnroll < 1 ){
      std::stringstream message;
      message << methodName << " innerUnrolls=" << innerUnroll << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   _exclusionStreamHeight    = _exclusionStreamWidth/innerUnroll; 
   _exclusionStreamSize      = _exclusionStreamWidth*_exclusionStreamHeight;

   return DefaultReturnValue;
}

/* 
 * Setup of j-stream dimensions
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1026
1027
 * @param particleStreamSize        particle stream size
 * @param particleStreamWidth       particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
1028
1029
1030
1031
1032
1033
1034
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 * @throw  OpenMMException  if jStreamWidth < 1 || innerUnroll < 1
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1035
int BrookNonBonded::_initializeJStreamSize( int particleStreamSize, int particleStreamWidth ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1036
1037
1038

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

1039
   static const std::string methodName      = "BrookNonBonded::_initializeJStreamSize";
Mark Friedrichs's avatar
Mark Friedrichs committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

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

   // validate stream width & inner unroll

   int jStreamWidth                             = getJStreamWidth();
   if( jStreamWidth < 1 ){
      std::stringstream message;
      message << methodName << " jStreamWidth=" << jStreamWidth << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   int innerUnroll           = getInnerLoopUnroll();
   if( innerUnroll < 1 ){
      std::stringstream message;
      message << methodName << " innerUnroll=" << innerUnroll << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   // set dimesnions

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1063
   _jStreamSize                                 = particleStreamSize/innerUnroll;
Mark Friedrichs's avatar
Mark Friedrichs committed
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
   _jStreamHeight                               = _jStreamSize/jStreamWidth; 
   _jStreamHeight                              += ( (_jStreamSize % _jStreamWidth) ? 1 : 0 ); 
   _jStreamSize                                 = jStreamWidth*_jStreamHeight;

   return DefaultReturnValue;
}

/* 
 * Setup of outer vdw stream size
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1074
1075
 * @param particleStreamSize        particle stream size
 * @param particleStreamWidth       particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
1076
1077
1078
1079
1080
1081
1082
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 * @throw  OpenMMException  if jStreamWidth < 1 || innerUnroll < 1
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1083
int BrookNonBonded::_initializeOuterVdwStreamSize( int particleStreamSize, int particleStreamWidth ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110

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

   static const std::string methodName      = "BrookNonBonded::initializeOuterVdwStreamSize";

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

   // validate stream width & inner unroll
/*
   int jStreamWidth                             = getJStreamWidth();
   if( jStreamWidth < 1 ){
      std::stringstream message;
      message << methodName << " jStreamWidth=" << jStreamWidth << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   int innerUnroll           = getInnerLoopUnroll();
   if( innerUnroll < 1 ){
      std::stringstream message;
      message << methodName << " innerUnroll=" << innerUnroll << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   // set dimesnions

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1111
   _jStreamSize                                 = particleStreamSize/innerUnroll;
Mark Friedrichs's avatar
Mark Friedrichs committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
   _jStreamHeight                               = _jStreamSize/jStreamWidth; 
   _jStreamHeight                              += ( (_jStreamSize % _jStreamWidth) ? 1 : 0 ); 
   _jStreamSize                                 = jStreamWidth*_jStreamHeight;
*/

   return DefaultReturnValue;
}

/* 
 * Setup of stream dimensions for partial force streams
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1123
1124
 * @param particleStreamSize        particle stream size
 * @param particleStreamWidth       particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
1125
1126
1127
1128
1129
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1130
int BrookNonBonded::_initializePartialForceStreamSize( int particleStreamSize, int particleStreamWidth ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1131
1132
1133

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

1134
   static const std::string methodName      = "BrookNonBonded::_initializePartialForceStreamSize";
Mark Friedrichs's avatar
Mark Friedrichs committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152

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

   int innerUnroll           = getInnerLoopUnroll();
   if( innerUnroll < 1 ){
      std::stringstream message;
      message << methodName << " innerUnrolls=" << innerUnroll << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   if( _partialForceStreamWidth < 1 ){
      std::stringstream message;
      message << methodName << " partial force stream width=" << _partialForceStreamWidth << " is less than 1.";
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1153
   _partialForceStreamSize    = particleStreamSize*getDuplicationFactor()/innerUnroll;
Mark Friedrichs's avatar
Mark Friedrichs committed
1154
1155
1156
1157
1158
1159
1160
   _partialForceStreamHeight  = _partialForceStreamSize/_partialForceStreamWidth;
   _partialForceStreamHeight += ( (_partialForceStreamSize % _partialForceStreamWidth) ? 1 : 0);

   return DefaultReturnValue;
}

/* 
Mark Friedrichs's avatar
Mark Friedrichs committed
1161
1162
1163
1164
1165
1166
 * Get contents of object
 *
 * @param level   level of dump
 *
 * @return string containing contents
 *
1167
 **/
Mark Friedrichs's avatar
Mark Friedrichs committed
1168

Mark Friedrichs's avatar
Mark Friedrichs committed
1169
std::string BrookNonBonded::getContentsString( int level ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
1170
1171
1172

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

Mark Friedrichs's avatar
Mark Friedrichs committed
1173
   static const std::string methodName      = "BrookNonBonded::getContentsString";
Mark Friedrichs's avatar
Mark Friedrichs committed
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184

   static const unsigned int MAX_LINE_CHARS = 256;
   char value[MAX_LINE_CHARS];
   static const char* Set                   = "Set";
   static const char* NotSet                = "Not set";

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

   std::stringstream message;
   std::string tab   = "   ";

1185
#ifdef _MSC_VER
Mark Friedrichs's avatar
Mark Friedrichs committed
1186
1187
1188
1189
1190
#define LOCAL_SPRINTF(a,b,c) sprintf_s( (a), MAX_LINE_CHARS, (b), (c) );   
#else
#define LOCAL_SPRINTF(a,b,c) sprintf( (a), (b), (c) );   
#endif

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1191
1192
   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParticles() );
   message << _getLine( tab, "Number of particles:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205

   (void) LOCAL_SPRINTF( value, "%d", getNumberOfForceStreams() );
   message << _getLine( tab, "Number of force streams:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getDuplicationFactor() );
   message << _getLine( tab, "Duplication factor:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getInnerLoopUnroll () )
   message << _getLine( tab, "Inner loop unroll:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getOuterLoopUnroll() )
   message << _getLine( tab, "Outer loop unroll:", value ); 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1206
1207
   (void) LOCAL_SPRINTF( value, "%d", getParticleSizeCeiling() );
   message << _getLine( tab, "Particle ceiling:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1208

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1209
1210
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamWidth() );
   message << _getLine( tab, "Particle stream width:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1211

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1212
1213
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamHeight() );
   message << _getLine( tab, "Particle stream height:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1214

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1215
1216
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamSize() );
   message << _getLine( tab, "Particle stream size:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244

   (void) LOCAL_SPRINTF( value, "%d", getJStreamWidth() );
   message << _getLine( tab, "J-stream width:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getJStreamHeight() );
   message << _getLine( tab, "J-stream height:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getJStreamSize() );
   message << _getLine( tab, "J-stream size:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getPartialForceStreamWidth() );
   message << _getLine( tab, "Partial force stream width:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getPartialForceStreamHeight() );
   message << _getLine( tab, "Partial force stream height:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getPartialForceStreamSize() );
   message << _getLine( tab, "Partial force stream size:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getExclusionStreamWidth() );
   message << _getLine( tab, "Exclusion stream width:", value ); 

   //(void) LOCAL_SPRINTF( value, "%d", getExclusionStreamHeight() );
   //message << _getLine( tab, "Exclusion stream height:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getExclusionStreamSize() );
   message << _getLine( tab, "Exclusion stream size:", value ); 

Mark Friedrichs's avatar
Mark Friedrichs committed
1245
1246
1247
1248
1249
1250
   message << _getLine( tab, "Log:",                  (getLog()                ? Set : NotSet) ); 
   message << _getLine( tab, "ExclusionStream:",      (getExclusionStream()    ? Set : NotSet) ); 
   message << _getLine( tab, "VdwStream:",            (getOuterVdwStream()     ? Set : NotSet) ); 
   message << _getLine( tab, "ChargeStream:",         (getChargeStream()       ? Set : NotSet) ); 
   message << _getLine( tab, "SigmaStream:",          (getInnerSigmaStream()   ? Set : NotSet) ); 
   message << _getLine( tab, "EpsilonStream:",        (getInnerEpsilonStream() ? Set : NotSet) ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1251
 
Mark Friedrichs's avatar
Mark Friedrichs committed
1252
1253
1254
1255
1256
1257
1258
1259
1260
   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      message << std::endl;
      if( _nonbondedStreams[ii] ){
         message << _nonbondedStreams[ii]->getContentsString( );
      }
   }

   // force streams

Mark Friedrichs's avatar
Mark Friedrichs committed
1261
1262
1263
1264
1265
1266
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      char description[256];
      (void) LOCAL_SPRINTF( description, "PartialForceStream %d", ii );
      message << _getLine( tab, description,  (isForceStreamSet(ii) ? Set : NotSet) ); 
   }
 
Mark Friedrichs's avatar
Mark Friedrichs committed
1267
1268
1269
1270
1271
1272
1273
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      message << std::endl;
      if( _nonbondedForceStreams[ii] ){
         message << _nonbondedForceStreams[ii]->getContentsString( );
      }
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
1274
1275
1276
1277
#undef LOCAL_SPRINTF

   return message.str();
}
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1278
1279
1280
1281

/** 
 * Compute forces
 * 
1282
 * @param context ContextImpl context
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1283
1284
1285
1286
1287
1288
1289
1290
1291
 *
 */

void BrookNonBonded::computeForces( BrookStreamImpl& positionStream, BrookStreamImpl& forceStream ){

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

   static const std::string methodName      = "BrookNonBonded::computeForces";

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1292
   static       int printOn                 = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1293
1294
1295
   static const int MaxErrorMessages        = 2;
   static       int ErrorMessages           = 0;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1296
   FILE* log;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1297
1298
1299

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

1300
1301
1302
1303
1304
    if( printOn && getLog() ){
       log = getLog();
    } else {
       printOn = 0;
    }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1305

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
   // nonbonded forces

   float epsfac                                      = 138.935485f;
   BrookFloatStreamInternal**  nonbondedForceStreams = getForceStreams();

   knbforce_CDLJ4(
             (float) getNumberOfParticles(),
             (float) getParticleSizeCeiling(),
             (float) getDuplicationFactor(),
             (float) getParticleStreamHeight( ),
             (float) getParticleStreamWidth( ),
             (float) getJStreamWidth( ),
             (float) getPartialForceStreamWidth( ),
             epsfac,
             positionStream.getBrookStream(),
             getChargeStream()->getBrookStream(),
             getOuterVdwStream()->getBrookStream(),
             getInnerSigmaStream()->getBrookStream(),
             getInnerEpsilonStream()->getBrookStream(),
             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
1342
1343
1344
   if( printOn ){
   //static int step = 0;
   //if( step++ < 1 ){
1345
1346
      FILE* log = getLog();
      //FILE* log = stderr;
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1347
      (void) fprintf( log, "%s\n", methodName.c_str() ); (void) fflush( log );
1348
      (void) fprintf( log, "Post knbforce_CDLJ4: particles=%6d ceiling=%3d dupFac=%3d\n", getNumberOfParticles(),  
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1349
1350
                                                                                          getParticleSizeCeiling(),
                                                                                          getDuplicationFactor()  );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1351

1352
1353
1354
1355
      (void) fprintf( log, "                        hght=%6d   width=%3d   jWid=%3d\n", getParticleStreamHeight( ),
                                                                                        getParticleStreamWidth( ),
                                                                                        getJStreamWidth( ) );
      (void) fprintf( log, "                        pFrc=%6d     eps=%12.5e\n",         getPartialForceStreamWidth( ), epsfac );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1356

1357
1358
      (void) fprintf( log, "%s Final NB & bonded forces\n", methodName.c_str() );
      (void) fprintf( log, "Forces here should be zero (pregather)\n" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1359
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamInternal();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1360
1361
      brookStreamInternalF->printToFile( log );
 
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
      (void) fprintf( log, "Coords\n" );
      brookStreamInternalF   = positionStream.getBrookStreamInternal();
      brookStreamInternalF->printToFile( log );
/*
void* dataArrayV = brookStreamInternalF->getData( 1 );
float* ddd = (float*) dataArrayV;
int idx1 = 0;
int idx2 = 3;
      (void) fprintf( log, "%d %d %.5e\n", idx1/3, idx2/3, computeDistance( idx1, idx2, ddd ) );
*/
 
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1373
1374
      (void) fprintf( log, "\nOuterVdwStreamd\n" );
      getOuterVdwStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1375

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1376
1377
      (void) fprintf( log, "\nInnerSigmaStream\n" );
      getInnerSigmaStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1378

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1379
1380
      (void) fprintf( log, "\nInnerEpsilonStream\n" );
      getInnerEpsilonStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1381

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1382
1383
//      (void) fprintf( log, "\nExclusionStream\n" );
//      getExclusionStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1384

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1385
1386
      (void) fprintf( log, "\nChargeStream\n" );
      getChargeStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1387
1388

      for( int ii = 0; ii < 4; ii++ ){
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1389
1390
         (void) fprintf( log, "\nForce stream %d\n", ii );
         nonbondedForceStreams[ii]->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1391
      }
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1392
      (void) fflush( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
   }

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

   // gather forces

   kMergeFloat3_4_nobranch( (float) getDuplicationFactor(),
                            (float) getParticleStreamWidth(),
                            (float) getPartialForceStreamWidth(),
                            (float) getNumberOfParticles(),
                            (float) getParticleSizeCeiling(),
                            (float) getOuterLoopUnroll(),
                            nonbondedForceStreams[0]->getBrookStream(),
                            nonbondedForceStreams[1]->getBrookStream(),
                            nonbondedForceStreams[2]->getBrookStream(),
                            nonbondedForceStreams[3]->getBrookStream(),
                            forceStream.getBrookStream() );

   // diagnostics

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1413
   if( printOn ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1414

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1415
      (void) fprintf( log, "\n%s NB forces\n", methodName.c_str() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1416
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamInternal();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1417
      brookStreamInternalF->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1418
      (void) fprintf( log, "\n%s Done\n", methodName.c_str() ); (void) fflush( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1419
1420
1421
1422
1423

   }

   return;
}
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452

/*  
 * Utility to compute distances between two points
 *
 * @param idx1       index of point1 into array 
 * @param idx2       index of point2 into array 
 * @param coords     array of points p1={coord[0], coord[1], coord[2] },
 *                                   p2={coord[3], coord[4], coord[5] },
 *                                   p3={coord[6], coord[7], coord[8] }
 *
 * @return distance
 *
 **/
    

float BrookNonBonded::_computeDistance( int idx1, int idx2, float* coords ){

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

   //static const std::string methodName      = "BrookNonBonded::_computeDistance";

   float d1 = sqrtf( 
                      (coords[idx1]-coords[idx2])*(coords[idx1]-coords[idx2]) +
                      (coords[idx1+1]-coords[idx2+1])*(coords[idx1+1]-coords[idx2+1]) +
                      (coords[idx1+2]-coords[idx2+2])*(coords[idx1+2]-coords[idx2+2]) );
   return d1;

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