BrookBonded.cpp 74.3 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors: 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 <math.h>
#include <sstream>
#include "BrookBonded.h"
#include "BrookPlatform.h"
#include "BrookStreamFactory.h"
#include "OpenMMException.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
38
#include "gpu/kinvmap_gather.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
39
40
41
42
43
44
#include "gpu/invmap.h"
#include "gpu/kforce.h"

using namespace OpenMM;
using namespace std;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
45
#define ATOMS(X,Y) (particles[ 5*(X) + (Y) + 1 ])
Mark Friedrichs's avatar
Mark Friedrichs committed
46
47
48
#define PARAMS(X,Y,Z) (params[(Y)][4*(X) + Z])

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
49
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
50
51
52
53
54
55
56
57
58
59
60
61
 * BrookBonded constructor
 * 
 */

BrookBonded::BrookBonded( ){

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

   static const std::string methodName = "BrookBonded::BrookBonded";

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
62
   _setupCompleted            = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
63
   _numberOfParticles         = 0;
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
64

65
   _coulombFactor             = (BrookOpenMMFloat) 138.935485;
Mark Friedrichs's avatar
Mark Friedrichs committed
66

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
67
   _particleIndicesStream     = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
   _chargeStream              = NULL;

   // parameter streams

   for( int ii = 0; ii < getNumberOfParameterStreams(); ii++ ){
      _bondedParameters[ii] = NULL;
   }

   // inverse maps & force streams

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      _bondedForceStreams[ii]       = NULL;
      _inverseMapStreamCount[ii]    = 0;
      for( int jj = 0; jj < MaxNumberOfInverseMaps; jj++ ){
         _inverseStreamMaps[ii][jj] = NULL;
      }
   }

   _maxInverseMapStreamCount[StreamI] = 9;
   _maxInverseMapStreamCount[StreamJ] = 6;
   _maxInverseMapStreamCount[StreamK] = 6;
   _maxInverseMapStreamCount[StreamL] = 9;

   _maxNumberOfInverseMaps = _maxInverseMapStreamCount[0];
   for( int ii = 1; ii < getNumberOfForceStreams(); ii++ ){
      if( _maxNumberOfInverseMaps < _maxInverseMapStreamCount[ii] ){
         _maxNumberOfInverseMaps  = _maxInverseMapStreamCount[ii];
      }
   }

   // check that MaxNumberOfInverseMaps is big enough

   if( _maxNumberOfInverseMaps > MaxNumberOfInverseMaps ){
      std::stringstream message;
      message << methodName << " max number of inverse maps=" << _maxNumberOfInverseMaps << " is greater than hardwired value=" << MaxNumberOfInverseMaps;
      throw OpenMMException( message.str() );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
106
   _inverseMapStreamWidth = -1;
Mark Friedrichs's avatar
Mark Friedrichs committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}   
 
/** 
 * BrookBonded destructor
 * 
 */

BrookBonded::~BrookBonded( ){

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

   //static const std::string methodName = "BrookBonded::~BrookBonded";

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
122
   delete _particleIndicesStream;
Mark Friedrichs's avatar
Mark Friedrichs committed
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
   delete _chargeStream;

   for( int ii = 0; ii < getNumberOfParameterStreams(); ii++ ){
      delete _bondedParameters[ii];
   }
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      delete _bondedForceStreams[ii];
      for( int jj = 0; jj < MaxNumberOfInverseMaps; jj++ ){
         delete _inverseStreamMaps[ii][jj];
      }
   }

}

/** 
 * Get number of parameter streams
 * 
 * @return  number of parameter streams
 *
 */

int BrookBonded::getNumberOfParameterStreams( void ) const {
   return NumberOfParameterStreams;
}

/** 
 * Get number of force streams
 * 
 * @return  number of force streams
 *
 */

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

/** 
 * Get max number of inverse maps
 * 
 * @return  max number of inverse maps
 *
 */

int BrookBonded::getMaxInverseMapStreamCount( void ) const {
   return _maxNumberOfInverseMaps;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
170
171
172
173
174
175
176
177
178
179
180
181
182
/** 
 * Get max number of inverse maps for specified force stream index
 * 
 * @param index index of force stream
 *
 * @return  max number of inverse maps or -1 if index is out of range
 *
 */

int BrookBonded::getMaxInverseMapStreamCount( int index ) const {
   return (index >= 0 && index < getNumberOfForceStreams())  ? _maxInverseMapStreamCount[index] : -1;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
183
184
185
186
187
188
189
/** 
 * Get width of inverse map streams
 * 
 * @return  width of inverse map streams
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
190
191
int BrookBonded::getInverseMapStreamWidth( void ) const {
   return _inverseMapStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
}

/** 
 * Get Coulomb factor
 * 
 * @return Coulomb factor
 *
 */

BrookOpenMMFloat BrookBonded::getCoulombFactor( void ) const {
   return _coulombFactor;
}

/** 
 * 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 BrookBonded::isForceStreamSet( int index ) const {
   return (index >= 0 && index < getNumberOfForceStreams() && _bondedForceStreams[index]) ? 1 : 0;
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
217
218
219
220
221
222
223
224
225
226
/** 
 * Return SetupCompleted flag
 *
 * @return SetupCompleted flag
 */
          
int BrookBonded::isSetupCompleted( void ) const {
   return _setupCompleted;
}
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
227
228
229
230
231
232
233
234
235
236
237
238
239
/** 
 * Set SetupCompleted flag
 *
 * @param  setupCompleted flag
 *
 * @return SetupCompleted flag
 */
          
int BrookBonded::setupCompleted( int setupCompleted ){
   _setupCompleted = setupCompleted;
   return _setupCompleted;
}
   
Mark Friedrichs's avatar
Mark Friedrichs committed
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
/** 
 * Return string showing if all inverse map streams are set
 *
 * @param    index into inverse map stream array
 *
 * @return   informative string
 *
 */

std::string BrookBonded::checkInverseMapStream( int index ) const {

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

   static const std::string methodName      = "BrookBonded::getContents";
   int ok                                   = 1;

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

   std::stringstream message;
   if( index < 0 || index >= getNumberOfForceStreams() ){
      message << "Inverse map index=" << index << " is out of range [0, "  << getNumberOfForceStreams() << ")";
      return message.str();
   }

   message << "[ ";
   for( int ii = 0; ii < getInverseMapStreamCount( index ); ii++ ){
      message << (_inverseStreamMaps[index][ii] ? 1 : 0) << " ";
      if( !_inverseStreamMaps[index][ii] ){
         ok = 0;
      }
   }
   message << " ]";
   if( !ok ){
      message << " ERROR!";
   }

   return message.str();
   
}

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

int BrookBonded::isParameterStreamSet( int index ) const {
   return (index >= 0 && index < getNumberOfParameterStreams() && _bondedParameters[index]) ? 1 : 0;
}

/** 
 * Validate stream count
 * 
 * @return  DefaultReturnValue if count valid; else return ErrorReturnValue
 *
 * @exception OpenMMException is thrown if count is invalid
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
302
int BrookBonded::_validateInverseMapStreamCount( int index, int count ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
303
304
305

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
306
   static const std::string methodName = "BrookBonded::_validateInverseMapStreamCount";
Mark Friedrichs's avatar
Mark Friedrichs committed
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

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

   int valid = DefaultReturnValue;

   if( index == 2 ){
      if( count > 5 ){
         valid = ErrorReturnValue;
      }
   }

   if( valid == ErrorReturnValue ){
      std::stringstream message;
      message << methodName << " input index=" << index << " has invalid count=" << count;
      throw OpenMMException( message.str() );
   }

   return DefaultReturnValue;
}

/** 
 * Get inverse map stream count
 * 
 * @return  count
 *
 * @exception OpenMMException is thrown if index is out of range [0, getNumberOfForceStreams() ]
 *
 */

int BrookBonded::getInverseMapStreamCount( int index ) const {

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

   static const std::string methodName = "BrookBonded::getInverseMapStreamCount";

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

   if( index < 0 || index >= getNumberOfForceStreams() ){
      std::stringstream message;
      message << methodName << " input index=" << index << " is out of range [0, " << getNumberOfForceStreams() << " )";
      throw OpenMMException( message.str() );
   }

   return _inverseMapStreamCount[index];
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
354
 * Get bonded particle indices stream
Mark Friedrichs's avatar
Mark Friedrichs committed
355
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
356
 * @return  particle indices stream
Mark Friedrichs's avatar
Mark Friedrichs committed
357
358
359
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
360
BrookFloatStreamInternal* BrookBonded::getParticleIndicesStream( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
361
362
363

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
364
   // static const std::string methodName = "BrookBonded::getParticleIndicesStream";
Mark Friedrichs's avatar
Mark Friedrichs committed
365
366
367

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
368
   return _particleIndicesStream;
Mark Friedrichs's avatar
Mark Friedrichs committed
369
370
}

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/** 
 * Get bonded charge stream
 * 
 * @return  charge stream
 *
 */

BrookFloatStreamInternal* BrookBonded::getChargeStream( void ) const {

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

   // static const std::string methodName = "BrookBonded::getChargeStream";

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

   return _chargeStream;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
389
390
391
392
393
394
395
/** 
 * Get array of bonded parameter streams
 * 
 * @return  array of bonded parameter streams
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
396
BrookFloatStreamInternal** BrookBonded::getBondedParameterStreams( void ){
Mark Friedrichs's avatar
Mark Friedrichs committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

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

   // static const std::string methodName = "BrookBonded::getBondedParameterStreams";

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

   return _bondedParameters;
}

/** 
 * Get array of force streams
 * 
 * @return  array  force streams
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
414
BrookFloatStreamInternal** BrookBonded::getBondedForceStreams( void ){
Mark Friedrichs's avatar
Mark Friedrichs committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

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

   // static const std::string methodName = "BrookBonded::getBondedForceStreams";

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

   return _bondedForceStreams;
}

/** 
 * Get array of inverse map streams
 * 
 * @param index  array index 
 *
 * @return  array inverse map streams
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
434
BrookFloatStreamInternal** BrookBonded::getInverseStreamMapsStreams( int index ){
Mark Friedrichs's avatar
Mark Friedrichs committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

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

   // static const std::string methodName = "BrookBonded::getInverseStreamMapsStreams";

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

   // no checking on index -- assume ok -- speed

   return _inverseStreamMaps[index];
}

/* Gromacs sorts most bonded interactions
 * We sort those that gromacs forgets
 * The parameters are all symmetric with respect 
 * to inversion of order.
 * 
 * Also we assume that the same set of indices are not
 * used with different sets of parameters in gromacs
 * This is just an assumption for convenience here,
 * nothing stops us from evaluating such terms in the
 * kernel.
 * 
 * To begin with all interactions have -1 -1 -1 -1
 * After we fit in all the interactions, we have
 * to convert the -1's to zeros to avoid indexing
 * errors on the GPU.
 * */

/*Flips i,j,k,l to l,k,j,i while correctly shuffling the params */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
466
void BrookBonded::_flipQuartet( int ibonded, int *particles ){
Mark Friedrichs's avatar
Mark Friedrichs committed
467
468
469

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
470
   // static const std::string methodName = "BrookBonded::_flipQuartet";
Mark Friedrichs's avatar
Mark Friedrichs committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

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

   int tmp;

   //For now, simply flip the indices
   //we're just studying the packing

   tmp = ATOMS( ibonded, 0 );
   ATOMS( ibonded, 0 ) = ATOMS( ibonded, 3 );
   ATOMS( ibonded, 3 ) = ATOMS( ibonded, 0 );

   tmp = ATOMS( ibonded, 1 );
   ATOMS( ibonded, 1 ) = ATOMS( ibonded, 2 );
   ATOMS( ibonded, 2 ) = ATOMS( ibonded, 1 );

}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
489
int BrookBonded::_matchTorsion( int i, int j, int k, int l, int nbondeds, int *particles ){
Mark Friedrichs's avatar
Mark Friedrichs committed
490
491
492

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
493
   // static const std::string methodName = "BrookBonded::_matchTorsion";
Mark Friedrichs's avatar
Mark Friedrichs committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

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

   int tmp;
   if( j > k ){
      //swap into order
      tmp = i;
      i = l;
      l = tmp;

      tmp = j;
      j = k;
      k = tmp;
   }
   
   for( int n = 0; n < nbondeds; n++ ){
      if( i == ATOMS(n, 0) && j == ATOMS(n, 1) && 
          k == ATOMS(n, 2) && l == ATOMS(n, 3) ){
         return n;
      }
   }

   return ErrorReturnValue;
}

/* 
 * We try to match i,j,k against the first 3 in a quartet, then
 * against the next three. Then we check if there's a free slot
 * in the fourth component and match the middle two components
 *
 * The last bit will not be needed as long as gromacs generates
Mark Friedrichs's avatar
Mark Friedrichs committed
525
526
 * all torsions. But I think there are force fields that don't 
 * use all torsions.
Mark Friedrichs's avatar
Mark Friedrichs committed
527
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
528
 * @return ErrorReturnValue if error; else particle index
Mark Friedrichs's avatar
Mark Friedrichs committed
529
530
531
 *
 **/

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
532
533
int BrookBonded::_matchAngle( int i, int j, int k, int nbondeds, 
                              int *particles, int *flag ){
Mark Friedrichs's avatar
Mark Friedrichs committed
534
535
536
537

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

   int n;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
538
   static const std::string methodName = "BrookBonded::_matchAngle";
Mark Friedrichs's avatar
Mark Friedrichs committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

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

   // validate i > k

   if( i > k ){
      if( getLog() ){
         (void) fprintf( getLog(), "%s Invalid triplet %d-%d-%d\n", methodName.c_str(), i, j, k );
         (void) fflush( getLog() );
      }
      std::stringstream message;
      message << methodName << " invalid triplet: " << i << "-" << j << "-" << k  << std::endl;
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }
   
   for( n = 0; n < nbondeds; n++ ){
      if( j == ATOMS(n, 1) ){
         if(   (i == ATOMS(n, 0) && k == ATOMS(n, 2)) 
             || (i == ATOMS(n, 2) && k == ATOMS(n, 0)) )  {
            *flag = 0;
            return n;
         }
      }
      else if( j == ATOMS(n, 2) ){
         if( i == ATOMS(n, 1) ){
            if( ATOMS(n, 3) == -1 || k == ATOMS(n, 3) ){
               ATOMS(n, 3) = k;
               *flag = 1;
               return n;
            }
         }
         else if( k == ATOMS(n, 1) ){
            if( ATOMS(n, 3) == -1 || i == ATOMS(n, 3) ){
               ATOMS(n, 3) = i;
               *flag = 1;
               return n;
            }
         }
      }
   }
   return ErrorReturnValue;
}

/*flag = 0 means match i-j, 1 means j-k and 2 means k-l
 * Again, like above, if we have uninitialized slots, we take em
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
588
int BrookBonded::_matchBond( int i, int j, int nbondeds, int *particles, int *flag ){
Mark Friedrichs's avatar
Mark Friedrichs committed
589
590
591

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
592
   static const std::string methodName = "BrookBonded::_matchBond";
Mark Friedrichs's avatar
Mark Friedrichs committed
593
594
595
596
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

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

   if( i > j ){
      if( getLog() ){
         (void) fprintf( getLog(), "%s invalid bond %d-%d\n", methodName.c_str(), i, j );
         (void) fflush( getLog() );
      }
      std::stringstream message;
      message << methodName << " invalid bond " << i << "-" << j << std::endl;
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
   }

   for( int n = 0; n < nbondeds; n++ ){

      if( ( i == ATOMS(n, 0) && j == ATOMS(n, 1) ) || ( i == ATOMS(n, 1) && j == ATOMS(n, 0) ) ){

         *flag = 0;
         return n;
      }
      
      //Try second spot

      if(  ATOMS(n, 2) == -1 ){ //One available spot
         if( i == ATOMS(n, 1) ){
            ATOMS(n, 2) = j;
            *flag = 1;
            return n;
         }
         if( j == ATOMS(n, 1) ){
            ATOMS(n, 2) = i;
            *flag = 1;
            return n;
         }
628
      } else if(  ( i == ATOMS(n, 1) && j == ATOMS(n, 2) ) ||( i == ATOMS(n, 2) && j == ATOMS(n, 1) ) ){
Mark Friedrichs's avatar
Mark Friedrichs committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
         *flag = 1;
         return n;
      }
      
      //Try third spot
      if( ATOMS(n, 3) == -1 ){
         if( i == ATOMS(n, 2) ){
            ATOMS(n, 3) = j;
            *flag = 2;
            return n;
         }
         if( j == ATOMS(n, 2) ){
            ATOMS(n, 3) = i;
            *flag = 2;
            return n;
         }
      } else if(  ( i == ATOMS(n, 2) && j == ATOMS(n, 3) ) ||( i == ATOMS(n, 3) && j == ATOMS(n, 2) ) ){
         *flag = 2;
         return n;
      }
   }

   return ErrorReturnValue;
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
654
int BrookBonded::_matchPair( int i, int j, int nbondeds, int *particles ){
Mark Friedrichs's avatar
Mark Friedrichs committed
655
656
657

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
658
   //static const std::string methodName = "BrookBonded::_matchPair";
Mark Friedrichs's avatar
Mark Friedrichs committed
659
660
661
662
663
664
665
666
667

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

   for( int n = 0; n < nbondeds; n++ ){

      //If there is an empty slot available:

      if( ATOMS(n, 0) == -1 ){

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
668
         //If one of i,j matches the l particle
Mark Friedrichs's avatar
Mark Friedrichs committed
669
670
671
672
673
674
675
676
677
678
679

         if( ATOMS(n, 3) == i ){
            ATOMS(n, 0) = j;
            return n;
         }
         if( ATOMS(n, 3) == j ){
            ATOMS(n, 0) = i;
            return n;
         }
      }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
680
      //If the l-particle is available
Mark Friedrichs's avatar
Mark Friedrichs committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
      if( ATOMS(n, 3) == -1 ){
         if( ATOMS(n, 0) == i ){
            ATOMS(n, 3) = j;
            return n;
         }
         if( ATOMS(n, 0) == j ){
            ATOMS(n, 3) = i;
            return n;
         }
      }

      //Both are unavailable, both much match
      if(   ( i == ATOMS(n, 0) && j == ATOMS(n, 3) ) 
          || ( i == ATOMS(n, 3) && j == ATOMS(n, 0) ) ){
         return n;
      }
   }
   return ErrorReturnValue;
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
702
 * Setup Ryckaert-Bellemans parameters/particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
703
704
 * 
 * @param nbondeds                  number of bonded entries
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
705
 * @param particles                 array of particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
706
 * @param params                    arrays of bond parameters
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
707
 * @param rbTorsionIndices          the four particles connected by each Ryckaert-Bellemans torsion term
Mark Friedrichs's avatar
Mark Friedrichs committed
708
709
710
711
712
713
 * @param rbTorsionParameters       the coefficients (in order of increasing powers) for each Ryckaert-Bellemans torsion term
 *
 * @return nonzero value if error
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
714
715
716
int BrookBonded::_addRBTorsions( int *nbondeds, int *particles, float *params[], 
                                 const vector<vector<int> >& rbTorsionIndices, 
                                 const vector<vector<double> >& rbTorsionParameters ){
Mark Friedrichs's avatar
Mark Friedrichs committed
717
718
719

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
720
   static const std::string methodName = "BrookBonded::_addRBTorsions";
Mark Friedrichs's avatar
Mark Friedrichs committed
721
722
723
724
725
726
727
728
729
730
   static const int debug              = 0;

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

   if( debug && getLog() ){
      (void) fprintf( getLog(), "%s number of bonds=%d\n", methodName.c_str(), rbTorsionIndices.size() );
   }

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
731
      vector<int> particlesIndices  = rbTorsionIndices[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
732
733
734
      vector<double> rbParameters   = rbTorsionParameters[ii];

      int index = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
735
736
737
738
      int i     = particlesIndices[index++];
      int j     = particlesIndices[index++];
      int k     = particlesIndices[index++];
      int l     = particlesIndices[index++];
Mark Friedrichs's avatar
Mark Friedrichs committed
739
      
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
740
      int ibonded = _matchTorsion( i, j, k, l, *nbondeds, particles );
Mark Friedrichs's avatar
Mark Friedrichs committed
741
742
743
744
745
746
747
748
749
750
      
      if( ibonded < 0 ){
         ibonded             = *nbondeds;
         ATOMS( ibonded, 0 ) = i;
         ATOMS( ibonded, 1 ) = j;
         ATOMS( ibonded, 2 ) = k;
         ATOMS( ibonded, 3 ) = l;
         (*nbondeds)++;
      }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
751
752
753
754
      // note -- we are starting w/ index c1 not c0 -- c0 is not used
      // to calculate the forces!

      index                   = 1;
Mark Friedrichs's avatar
Mark Friedrichs committed
755
756
757
758
759
760
761
762
763
      PARAMS( ibonded, 0, 0 ) = (BrookOpenMMFloat) rbParameters[index++];
      PARAMS( ibonded, 0, 1 ) = (BrookOpenMMFloat) rbParameters[index++];
      PARAMS( ibonded, 0, 2 ) = (BrookOpenMMFloat) rbParameters[index++];
      PARAMS( ibonded, 0, 3 ) = (BrookOpenMMFloat) rbParameters[index++];
      PARAMS( ibonded, 1, 0 ) = (BrookOpenMMFloat) rbParameters[index++];

      if( debug && getLog() ){
         (void) fprintf( getLog(), "   %d [%d %d %d %d] %.3e %.3e %.3e %.3e\n", ibonded, i, j, k, l,
                         rbParameters[0], rbParameters[1],
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
764
                         rbParameters[2], rbParameters[3], rbParameters[4], rbParameters[5] );
Mark Friedrichs's avatar
Mark Friedrichs committed
765
766
767
768
769
770
771
      }
   }

   return DefaultReturnValue;
}

/**
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
772
 * Setup periodic torsion parameters/particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
773
774
 * 
 * @param nbondeds                  number of bonded entries
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
775
 * @param particles                 array of particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
776
 * @param params                    arrays of bond parameters
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
777
 * @param periodicTorsionIndices    the four particles connected by each periodic torsion term
Mark Friedrichs's avatar
Mark Friedrichs committed
778
779
780
781
782
783
 * @param periodicTorsionParameters the force parameters (k, phase, periodicity) for each periodic torsion term
 *
 * @return nonzero value if error
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
784
785
786
int BrookBonded::_addPTorsions( int *nbondeds, int *particles, BrookOpenMMFloat* params[],
                                const vector<vector<int> >& periodicTorsionIndices, 
                                const vector<vector<double> >& periodicTorsionParameters ){
Mark Friedrichs's avatar
Mark Friedrichs committed
787
788
789

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
790
791
792
793
   static const BrookOpenMMFloat DEG2RAD =  (BrookOpenMMFloat) (M_PI/180.0);

   static const std::string methodName   = "BrookBonded::_addPTorsion";
   static const int debug                = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
794
795
796

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
797
798
799
800
801
   FILE* log = getLog();
   if( debug ){
      log = log ? log : stderr;
      (void) fprintf( log, "%s npdih=%d\n", methodName.c_str(), periodicTorsionIndices.size() );
      (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
802
803
804
805
   }

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
806
      vector<int> particlesIndices  = periodicTorsionIndices[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
807
808
809
      vector<double> pTParameters   = periodicTorsionParameters[ii];

      int index = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
810
811
812
813
      int i     = particlesIndices[index++];
      int j     = particlesIndices[index++];
      int k     = particlesIndices[index++];
      int l     = particlesIndices[index++];
Mark Friedrichs's avatar
Mark Friedrichs committed
814

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
815
      int ibonded = _matchTorsion( i, j, k, l, *nbondeds, particles );
Mark Friedrichs's avatar
Mark Friedrichs committed
816
817
818
819
820
821
822
823
824
      
      if( ibonded < 0 ){
         ibonded = *nbondeds;
         ATOMS( ibonded, 0 ) = i;
         ATOMS( ibonded, 1 ) = j;
         ATOMS( ibonded, 2 ) = k;
         ATOMS( ibonded, 3 ) = l;
         (*nbondeds)++;
      }
Mark Friedrichs's avatar
Mark Friedrichs committed
825
826
827
828

      // note: parameters 0 & 2 switched

      PARAMS( ibonded, 1, 1 ) = (BrookOpenMMFloat) pTParameters[2];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
829
      PARAMS( ibonded, 1, 2 ) = (BrookOpenMMFloat) pTParameters[1]*DEG2RAD;
Mark Friedrichs's avatar
Mark Friedrichs committed
830
      PARAMS( ibonded, 1, 3 ) = (BrookOpenMMFloat) pTParameters[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
831

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
832
833
      if( debug ){
         (void) fprintf( log, "   %d [%d %d %d %d] %.3e %.3e %.3e\n", ibonded, i, j, k, l,
Mark Friedrichs's avatar
Mark Friedrichs committed
834
                         pTParameters[0], pTParameters[1], pTParameters[2] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
835
        (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
836
837
838
839
840
841
842
      }
   }

   return DefaultReturnValue;
}

/**
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
843
 * Setup angle bond parameters/particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
844
845
 * 
 * @param nbondeds                  number of bonded entries
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
846
 * @param particles                 array of particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
847
 * @param params                    arrays of bond parameters
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
848
 * @param angleIndices              the angle bond particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
849
850
851
852
853
854
 * @param angleParameters           the angle parameters (angle in radians, force constant)
 *
 * @return nonzero value if error
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
855
856
int BrookBonded::_addAngles( int *nbondeds, int *particles, float *params[], const std::vector<std::vector<int> >& angleIndices,
                             const std::vector<std::vector<double> >& angleParameters ){
Mark Friedrichs's avatar
Mark Friedrichs committed
857
858
859

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
860
   static const std::string methodName = "BrookBonded::_addAngles";
Mark Friedrichs's avatar
Mark Friedrichs committed
861
862
863
864
865
866
867
868
869
870
871
872
   static const int debug              = 0;

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

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

   // loop over bonds

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
873
      vector<int> particlesIndices   = angleIndices[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
874
875
876
      vector<double> angParameters   = angleParameters[ii];

      int index = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
877
878
879
      int i     = particlesIndices[index++];
      int j     = particlesIndices[index++];
      int k     = particlesIndices[index++];
Mark Friedrichs's avatar
Mark Friedrichs committed
880
881
 
      int flag;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
882
      int ibonded = _matchAngle( i, j, k, *nbondeds, particles, &flag );
Mark Friedrichs's avatar
Mark Friedrichs committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
      if( ibonded < 0 ){
         ibonded = *nbondeds;
         ATOMS( ibonded, 0 ) = i;
         ATOMS( ibonded, 1 ) = j;
         ATOMS( ibonded, 2 ) = k;
         flag = 0;
         (*nbondeds)++;
      }
      PARAMS( ibonded, 2, flag*2 )     = (BrookOpenMMFloat) angParameters[0];
      PARAMS( ibonded, 2, flag*2 + 1 ) = (BrookOpenMMFloat) angParameters[1];
      
      if( debug && getLog() ){
         (void) fprintf( getLog(), "   %d [%d %d %d ] %.6e %.6e\n", ibonded, i, j, k,
                         angParameters[0], angParameters[1] );
      }
   }

   return DefaultReturnValue;
}

/**
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
904
 * Setup harmonic bond parameters/particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
905
906
 * 
 * @param nbondeds                  number of bonded entries
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
907
 * @param particles                 array of particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
908
 * @param params                    arrays of bond parameters
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
909
 * @param bondIndices               two harmonic bond particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
910
911
912
913
914
915
 * @param bondParameters            the force parameters (distance, k)
 *
 * @return nonzero value if error
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
916
917
int BrookBonded::_addBonds( int *nbondeds, int *particles, float *params[], const vector<vector<int> >& bondIndices,
                            const vector<vector<double> >& bondParameters ){
Mark Friedrichs's avatar
Mark Friedrichs committed
918
919
920

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
921
   static const std::string methodName = "BrookBonded::_addBonds";
Mark Friedrichs's avatar
Mark Friedrichs committed
922
   static const int debug              = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
923
924
925
926
927
928
929
930
931
932
933

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

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

   // loop over bonds

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
934
      vector<int> particlesIndices   = bondIndices[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
935
936
937
      vector<double> bndParameters   = bondParameters[ii];

      int index = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
938
939
      int i     = particlesIndices[index++];
      int j     = particlesIndices[index++];
Mark Friedrichs's avatar
Mark Friedrichs committed
940

941
942
943
944
945
946
947
948
      // insure i < j

      if( i > j ){
         int k = i;
         i     = j;
         j     = k;
      }

Mark Friedrichs's avatar
Mark Friedrichs committed
949
      int flag;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
950
      int ibonded = _matchBond( i, j, *nbondeds, particles, &flag );
951
int saveIbond = ibonded;
Mark Friedrichs's avatar
Mark Friedrichs committed
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
      if( ibonded < 0 ){
         ibonded = *nbondeds;
         ATOMS( ibonded, 0 ) = i;
         ATOMS( ibonded, 1 ) = j;
         flag = 0;
         (*nbondeds)++;
      }

      if( flag < 2 ){
         PARAMS( ibonded, 3, flag*2 )     = (BrookOpenMMFloat) bndParameters[0];
         PARAMS( ibonded, 3, flag*2 + 1 ) = (BrookOpenMMFloat) bndParameters[1];
      } else {
         PARAMS( ibonded, 4, 0 )          = (BrookOpenMMFloat) bndParameters[0];
         PARAMS( ibonded, 4, 1 )          = (BrookOpenMMFloat) bndParameters[1];
      }

      if( debug && getLog() ){
969
         (void) fprintf( getLog(), "   %d (%5d) [%6d %6d ] flag=%2d %.3e %.3e\n", ibonded, saveIbond, i, j, flag,
Mark Friedrichs's avatar
Mark Friedrichs committed
970
971
972
973
974
975
976
977
                         bndParameters[0], bndParameters[1] );
      }
   }

   return DefaultReturnValue;
}

/**
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
978
 * Setup LJ/Coulomb 1-4 parameters/particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
979
980
 * 
 * @param nbondeds                  number of bonded entries
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
981
 * @param particles                 array of particle indices
Mark Friedrichs's avatar
Mark Friedrichs committed
982
983
 * @param params                    arrays of bond parameters
 * @param charges                   array of charges
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
984
 * @param bonded14Indices           each element contains the indices of two particles whose nonbonded interactions should be reduced since
Mark Friedrichs's avatar
Mark Friedrichs committed
985
 *                                  they form a bonded 1-4 pair
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
986
 * @param nonbondedParameters       the nonbonded force parameters (charge, sigma, epsilon) for each particle
Mark Friedrichs's avatar
Mark Friedrichs committed
987
988
989
990
991
992
 * @param log                       log reference
 *
 * @return nonzero value if error
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
993
994
995
996
int BrookBonded::_addPairs( int *nbondeds, int *particles, BrookOpenMMFloat* params[],
                            BrookOpenMMFloat* charges,
                            const std::vector<std::vector<int> >& bonded14Indices,
                            const std::vector<std::vector<double> >& nonbondedParameters ){
Mark Friedrichs's avatar
Mark Friedrichs committed
997
998
999

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1000
   static const std::string methodName = "BrookBonded::_addPairs";
Mark Friedrichs's avatar
Mark Friedrichs committed
1001
   static const double oneSixth        = 1.0/6.0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1002
   static const int debug              = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
1003
1004
1005
1006

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

   if( debug && getLog() ){
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1007
      (void) fprintf( getLog(), "%s npairs=%d sz=%u %u\n", methodName.c_str(), bonded14Indices.size(), bonded14Indices.size(), nonbondedParameters.size() ); fflush( getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1008
1009
1010
1011
   }

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

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1012
1013
      int i     = bonded14Indices[ii][0];
      int j     = bonded14Indices[ii][1];
Mark Friedrichs's avatar
Mark Friedrichs committed
1014

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1015
      int ibonded = _matchPair( i, j, *nbondeds, particles );
Mark Friedrichs's avatar
Mark Friedrichs committed
1016
1017
1018
1019
1020
1021
1022
      if( ibonded < 0 ){
         ibonded = *nbondeds;
         ATOMS(ibonded, 0) = i;
         ATOMS(ibonded, 3) = j;
         (*nbondeds)++;
      }

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1023
//(void) fprintf( getLog(), "%s   %d %ibonded=%d done\n", methodName.c_str(), ii, ibonded ); fflush( getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1024

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1025
1026
1027
1028
      vector<double> iParameters  = nonbondedParameters[ii];
/*
      double c6                   = iParameters[1];
      double c12                  = 4.0*iParameters[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
1029
1030
      double sig, eps;
      if( c12 != 0.0 ){
1031
1032
1033
1034
         //eps = c6*c6/c12;
         //sig = pow( c12/c6, oneSixth );
         eps = c12;
         sig = c6;
Mark Friedrichs's avatar
Mark Friedrichs committed
1035
1036
1037
1038
      } else {
         eps = 0.0;
         sig = 1.0;
      }
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1039
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
1040

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1041
1042
      PARAMS( ibonded, 4, 2 ) = (BrookOpenMMFloat) iParameters[1];
      PARAMS( ibonded, 4, 3 ) = (BrookOpenMMFloat) (4.0*iParameters[2]);
Mark Friedrichs's avatar
Mark Friedrichs committed
1043
1044
1045

      // a little wasteful, but ...

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1046
      charges[ibonded] = (BrookOpenMMFloat) iParameters[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
1047
1048

      if( debug ){
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1049
         (void) fprintf( getLog(), "   %d [%d %d ] %.3e %.3e q=%.4f\n", ibonded, i, j, iParameters[1], iParameters[2], charges[ibonded] );
Mark Friedrichs's avatar
Mark Friedrichs committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
      }
   }

   return DefaultReturnValue;
}

/** 
 * Create and load inverse maps for bonded ixns
 * 
 * @param nbondeds                  number of bonded entries
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1060
1061
 * @param nparticles                number of particles
 * @param particles                 arrays of particle indices (particles[numberOfBonds][4])
Mark Friedrichs's avatar
Mark Friedrichs committed
1062
1063
1064
1065
1066
1067
1068
 * @param platform                  BrookPlatform reference
 * @param log                       log file reference (optional)
 *
 * @return nonzero value if error
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1069
int BrookBonded::_loadInvMaps( int nbondeds, int nparticles, int *particles, int particleStreamWidth, int particleStreamSize ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1070
1071
1072

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1073
   static const std::string methodName = "BrookBonded::_loadInvMaps";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1074
   static       int printOn            = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
1075
   double dangleValue                  = 0.0;
Mark Friedrichs's avatar
Mark Friedrichs committed
1076
1077
1078

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1079
   printOn = (printOn && getLog()) ? 1 : 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1080

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1081
   // get particle stream size
Mark Friedrichs's avatar
Mark Friedrichs committed
1082

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1083
/*
Mark Friedrichs's avatar
Mark Friedrichs committed
1084
   const BrookStreamFactory& brookStreamFactory = dynamic_cast<const BrookStreamFactory&> (brookPlatform.getDefaultStreamFactory() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1085
1086
1087
   int particleStreamWidth                      = brookStreamFactory.getDefaultParticleStreamWidth();
   int particleStreamSize                       = brookPlatform.getStreamSize( getNumberOfParticles(), particleStreamWidth, NULL );
*/
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1088
   _inverseMapStreamWidth                       = particleStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
1089
   
1090
1091
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
1092
1093
1094
   // allocate temp memory

   float4** invmaps = new float4*[getMaxInverseMapStreamCount()];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1095
   float* block     = new float[4*getMaxInverseMapStreamCount()*particleStreamSize];
1096

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1097
   //memset( block, 0, 4*getMaxInverseMapStreamCount()*particleStreamSize*sizeof( float ) );
1098
1099

   float* blockPtr = block;
Mark Friedrichs's avatar
Mark Friedrichs committed
1100
   for( int ii = 0; ii < getMaxInverseMapStreamCount(); ii++ ){
1101
      invmaps[ii]  = (float4*) blockPtr;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1102
      blockPtr    += 4*particleStreamSize;
Mark Friedrichs's avatar
Mark Friedrichs committed
1103
   }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1104
   int* counts = new int[particleStreamSize];
Mark Friedrichs's avatar
Mark Friedrichs committed
1105

1106
1107
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
1108
1109
   // get inverse maps and load into streams

Mark Friedrichs's avatar
Mark Friedrichs committed
1110
1111
1112
1113
   // create streams
   // done independently from loading since for test cases some stream counts == 0, but kernels expect stream to
   // have been created even though unused

1114
1115
   // load streams -- initialize all streams even if unused since gather methods will still pick up

Mark Friedrichs's avatar
Mark Friedrichs committed
1116
1117
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      for( int jj = 0; jj < getMaxInverseMapStreamCount(ii); jj++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1118
1119
         _inverseStreamMaps[ii][jj] = new BrookFloatStreamInternal( BrookCommon::BondedInverseMapStreams, particleStreamSize,
                                                                    particleStreamWidth, BrookStreamInternal::Float4, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
1120
1121
1122
      }
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1123
   if( printOn ){
1124
1125
      (void) fprintf( getLog(), "%s force stream strms=%d nbondeds=%d max counts=[%d %d %d %d] strSz&Wd=%d %d\n", methodName.c_str(), getNumberOfForceStreams(),
                      nbondeds, getMaxInverseMapStreamCount(0), getMaxInverseMapStreamCount(1), getMaxInverseMapStreamCount(2), getMaxInverseMapStreamCount(3),
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1126
                      particleStreamSize, particleStreamWidth );
1127
1128
1129
      (void) fflush( getLog() );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
1130
1131
   // load data

Mark Friedrichs's avatar
Mark Friedrichs committed
1132
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1133
      for( int jj = 0; jj < 4*getMaxInverseMapStreamCount()*particleStreamSize; jj++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1134
1135
         block[jj] = -1.0f;
      }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1136
1137

(void) fprintf( stderr, "%s _gpuCalcInvMap %d getInverseMapStreamCount=%d\n", methodName.c_str(), ii, getInverseMapStreamCount( ii ) ); (void) fflush( getLog() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1138
1139

      _gpuCalcInvMap( ii, 4, nbondeds, nparticles, particles, getMaxInverseMapStreamCount( ii ), counts, invmaps, &(_inverseMapStreamCount[ii]) );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1140
1141
1142

_gpuPrintInvMaps( _inverseMapStreamCount[ii], nparticles, counts, invmaps, stderr );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1143
      _validateInverseMapStreamCount( ii, _inverseMapStreamCount[ii] ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1144
1145
      for( int jj = 0; jj < _inverseMapStreamCount[ii]; jj++ ){
         _inverseStreamMaps[ii][jj]->loadFromArray( invmaps[jj] );
1146

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1147
1148
1149
1150
         //if( printOn ){
         if( 1 ){
            FILE* log = stderr;
            (void) fprintf( log, "%s inverseMap stream strms=%d count=%d index=%d %d InverseMapStreamCount[ii]=%d max=%d\n",
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1151
1152
                            methodName.c_str(), getNumberOfForceStreams(), _inverseMapStreamCount[ii], ii, jj,
                            getInverseMapStreamCount( ii ), getMaxInverseMapStreamCount( ii ) );
1153

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1154
            for( int kk = 0; kk < particleStreamSize; kk++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1155
               (void) fprintf( log, "%8d [ %.1f %.1f %.1f %.1f]\n", kk, invmaps[jj][kk].x, invmaps[jj][kk].y, invmaps[jj][kk].z, invmaps[jj][kk].w  );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1156
            }
1157
1158
1159
1160
1161
1162
1163
1164
         }

      }    

      // for small systems, must all initialize inverse maps to negative values in order to 
      // keep invalid entries from being included in forces

      if( _inverseMapStreamCount[ii] < getMaxInverseMapStreamCount( ii ) ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1165
         for( int jj = 0; jj < 4*particleStreamSize; jj++ ){
1166
1167
1168
1169
1170
            block[jj] = -1.0f;
         }
         for( int jj = _inverseMapStreamCount[ii]; jj < getMaxInverseMapStreamCount( ii ); jj++ ){
             _inverseStreamMaps[ii][jj]->loadFromArray( invmaps[0] );
         }
Mark Friedrichs's avatar
Mark Friedrichs committed
1171
1172
      }    

Mark Friedrichs's avatar
Mark Friedrichs committed
1173
1174
   }

1175
1176
   // diagnostics 

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1177
   if( printOn ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1178
1179
1180
1181
1182
1183
      (void) fprintf( getLog(), "%s done\n", methodName.c_str() );
      (void) fflush( getLog() );
   }

   // free memory

1184
1185
1186
   delete[] counts;
   delete[] invmaps[0];
   delete[] invmaps;
Mark Friedrichs's avatar
Mark Friedrichs committed
1187
1188
1189
1190
1191
1192
1193

   return DefaultReturnValue;
}

/* 
 * Setup for bonded ixns
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1194
1195
 * @param numberOfParticles            number of particles
 * @param bondIndices                  vector of vector of harmonic                   bond indices    -- one entry each bond (2 particles     )
Mark Friedrichs's avatar
Mark Friedrichs committed
1196
 * @param bondParameters               vector of vector of harmonic                   bond parameters -- one entry each bond (2 parameters)
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1197
 * @param angleIndices                 vector of vector of angle                      bond indices    -- one entry each bond (3 particles     )
Mark Friedrichs's avatar
Mark Friedrichs committed
1198
 * @param angleParameters              vector of vector of angle                      bond parameters -- one entry each bond (2 parameters)
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1199
 * @param periodicTorsionIndices       vector of vector of periodicTorsionIndices     bond indices    -- one entry each bond (4 particles     )
Mark Friedrichs's avatar
Mark Friedrichs committed
1200
 * @param periodicTorsionParameters    vector of vector of periodicTorsionParameters  bond parameters -- one entry each bond (3 parameters)
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1201
 * @param rbTorsionIndices             vector of vector of rb torsion                 bond indices    -- one entry each bond (4 particles     )
Mark Friedrichs's avatar
Mark Friedrichs committed
1202
 * @param rbTorsionParameters          vector of vector of rb torsion                 bond parameters -- one entry each bond (5 parameters)
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1203
 * @param bonded14Indices              vector of vector of Lennard-Jones 14           particle indices    -- one entry each bond (2 particles     )
Mark Friedrichs's avatar
Mark Friedrichs committed
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
 * @param nonbondedParameters          vector of vector of Lennard-Jones 14           parameters      -- one entry each bond (3 parameters)
 * @param platform                     Brook platform reference
 *
 * @return always 1
 *
 * We go through the interactions in the following order
 * torsions
 * angles
 * 14 interactions
 * bonds
 * For each new interaction, we try to fit it in with 
 * those already in. This may not necessarily result in
 * the optimal fit, but should not be too bad. 
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1219
int BrookBonded::setup( int numberOfParticles,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1220
1221
1222
1223
1224
                        BrookBondParameters* harmonicBondBrookBondParameters,
                        BrookBondParameters* harmonicAngleBrookBondParameters,
                        BrookBondParameters* periodicTorsionBrookBondParameters,
                        BrookBondParameters* rbTorsionBrookBondParameters,
                        BrookBondParameters* nonBonded14ForceParameters,  
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1225
                        int particleStreamWidth, int particleStreamSize ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1226
1227
1228
1229

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

   static const std::string methodName = "BrookBonded::setup";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1230
   static       int printOn            = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
1231
   double dangleValue                  = 0.0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1232
   FILE* log;
Mark Friedrichs's avatar
Mark Friedrichs committed
1233
1234
1235

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1236
1237
//setLog( stderr );
   printOn = (printOn && getLog()) ? printOn : 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1238

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1239
1240
1241
   if( printOn ){
      log = getLog();
      (void) fprintf( log, "%s particles=%d\n   [%p %p %p %p %p] (bond, angle, pd, rb, 14)\n"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1242
1243
1244
                      "StreamW=%d StreamSz=%d\n", methodName.c_str(), numberOfParticles, harmonicBondBrookBondParameters,
                      harmonicAngleBrookBondParameters,
                      periodicTorsionBrookBondParameters, rbTorsionBrookBondParameters, nonBonded14ForceParameters,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1245
                      particleStreamWidth, particleStreamSize ); fflush( log );
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1246
1247
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1248
   _numberOfParticles = numberOfParticles;
Mark Friedrichs's avatar
Mark Friedrichs committed
1249

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1250
   // check that particle indices & parameters agree
Mark Friedrichs's avatar
Mark Friedrichs committed
1251
1252
1253

   // allocate temp memory

Mark Friedrichs's avatar
Mark Friedrichs committed
1254
   int maxBonds     = 10*numberOfParticles;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1255
   int* particles   = new int[5*maxBonds];
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1256
   float* charges   = new BrookOpenMMFloat[maxBonds];
Mark Friedrichs's avatar
Mark Friedrichs committed
1257
1258
1259
1260
1261

   BrookOpenMMFloat** params = new BrookOpenMMFloat*[getNumberOfParameterStreams()];
   for( int ii = 0; ii < getNumberOfParameterStreams(); ii++ ){
      params[ii] = new BrookOpenMMFloat[4*maxBonds];
   }
1262
1263
1264
1265
1266

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

   // build streams

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1267
1268
//   const BrookStreamFactory& brookStreamFactory = dynamic_cast<const BrookStreamFactory&> (brookPlatform.getDefaultStreamFactory() );
//   int particleStreamWidth                      = brookStreamFactory.getDefaultParticleStreamWidth();
Mark Friedrichs's avatar
Mark Friedrichs committed
1269

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1270
   // Initialize all particle indices to -1 to indicate empty slots
Mark Friedrichs's avatar
Mark Friedrichs committed
1271
1272
1273
   // All parameters must be initialized to values that will 
   // produce zero for the corresponding force. 

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1274
   memset( charges, 0, maxBonds*sizeof( BrookOpenMMFloat ) );
Mark Friedrichs's avatar
Mark Friedrichs committed
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
   for( int ii = 0; ii < maxBonds; ii++ ){

      ATOMS( ii, 0 ) = -1;
      ATOMS( ii, 1 ) = -1;
      ATOMS( ii, 2 ) = -1;
      ATOMS( ii, 3 ) = -1;
   
      for( int jj = 0; jj < getNumberOfParameterStreams(); jj++ ){
         PARAMS( ii, jj, 0 ) = 0.0; 
         PARAMS( ii, jj, 1 ) = 0.0; 
         PARAMS( ii, jj, 2 ) = 0.0; 
         PARAMS( ii, jj, 3 ) = 0.0; 
      }

      //Set sigma negative to indicate no pair
      PARAMS( ii, 4, 2 ) = -1.0;
   }

   // nbondeds tracks number of ixn
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1294

Mark Friedrichs's avatar
Mark Friedrichs committed
1295
   int nbondeds = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1296

Mark Friedrichs's avatar
Mark Friedrichs committed
1297
   if( rbTorsionBrookBondParameters ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1298
      _addRBTorsions( &nbondeds, particles, params, rbTorsionBrookBondParameters->getParticleIndices(),         rbTorsionBrookBondParameters->getBondParameters() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1299
1300
   }
   if( periodicTorsionBrookBondParameters ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1301
      _addPTorsions(  &nbondeds, particles, params, periodicTorsionBrookBondParameters->getParticleIndices(),   periodicTorsionBrookBondParameters->getBondParameters() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1302
1303
   }
   if( harmonicAngleBrookBondParameters ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1304
      _addAngles(     &nbondeds, particles, params, harmonicAngleBrookBondParameters->getParticleIndices(),     harmonicAngleBrookBondParameters->getBondParameters() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1305
1306
   }
   if( harmonicBondBrookBondParameters ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1307
      _addBonds(      &nbondeds, particles, params, harmonicBondBrookBondParameters->getParticleIndices(),      harmonicBondBrookBondParameters->getBondParameters() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1308
1309
   }
   if( nonBonded14ForceParameters ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1310
      _addPairs(      &nbondeds, particles, params, charges, nonBonded14ForceParameters->getParticleIndices(), nonBonded14ForceParameters->getBondParameters() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1311
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
1312

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1313
1314
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
1315
1316
1317
1318
   // check that number of bonds not too large for memory allocated

   if( nbondeds >= maxBonds ){
      std::stringstream message;
Mark Friedrichs's avatar
Mark Friedrichs committed
1319
      message << methodName << " number of bonds=" << nbondeds << " is greater than maxBonds=" << maxBonds << " numberOfParticles=" << numberOfParticles;
Mark Friedrichs's avatar
Mark Friedrichs committed
1320
      throw OpenMMException( message.str() );
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1321
1322
1323
1324
1325

   } else if( nbondeds < 1 ){

      // return if no bonds

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1326
      if( printOn || getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1327
1328
1329
         (void) fprintf( getLog(), "%s WARNING: particles=%d number of bonds=%d maxBonds=%d\n", methodName.c_str(), numberOfParticles, nbondeds, maxBonds );
      }

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1330
1331
1332
      _setupCompleted = 1;
      return DefaultReturnValue;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1333
1334
1335
   } else if( printOn ){
      (void) fprintf( log, "%s particles=%d number of bonds=%d maxBonds=%d\n", methodName.c_str(), numberOfParticles, nbondeds, maxBonds );
      (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
1336
1337
   }

Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1338
1339
1340
1341
1342
1343
1344
// ---------------------------------------------------------------------------------------

   // charge stream

   _chargeStream             = new BrookFloatStreamInternal( BrookCommon::BondedChargeStream, nbondeds, particleStreamWidth,
                                                             BrookStreamInternal::Float, dangleValue );

1345
// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
1346

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1347
   // particle indices stream
Mark Friedrichs's avatar
Mark Friedrichs committed
1348

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1349
1350
   _particleIndicesStream    = new BrookFloatStreamInternal( BrookCommon::BondedParticleIndicesStream, nbondeds, particleStreamWidth,
                                                             BrookStreamInternal::Float4, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
1351

Mark Friedrichs's avatar
Mark Friedrichs committed
1352
   int* buffer               = new int[4*_particleIndicesStream->getStreamSize()];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1353
   memset( buffer, 0, sizeof( int )*4*_particleIndicesStream->getStreamSize() );
1354

Mark Friedrichs's avatar
Mark Friedrichs committed
1355
   int index                 = 0;
1356
1357
1358
   for( int ii = 0; ii < nbondeds; ii++ ){
      for( int jj = 0; jj < 4; jj++ ){
         buffer[index++] = ATOMS( ii, jj );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1359
//(void) fprintf( getLog(), "%s particleIndices %d %d  %d buffer=%d particles=%d\n", methodName.c_str(), ii, jj, index, buffer[index-1], ATOMS( ii, jj ) );
1360
1361
      }
   }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1362
   _particleIndicesStream->loadFromArray( buffer, BrookStreamInternal::Integer ); 
1363
1364
1365
1366
   delete[] buffer;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
1367
1368
   _chargeStream->loadFromArray( charges ); 

1369
1370
1371
1372
// ---------------------------------------------------------------------------------------

   // bonded parameters

Mark Friedrichs's avatar
Mark Friedrichs committed
1373
   for( int ii = 0; ii < getNumberOfParameterStreams(); ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1374
      _bondedParameters[ii]  = new BrookFloatStreamInternal( BrookCommon::BondedParametersStream, nbondeds, particleStreamWidth,
Mark Friedrichs's avatar
Mark Friedrichs committed
1375
                                                             BrookStreamInternal::Float4, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
1376
1377
1378
      _bondedParameters[ii]->loadFromArray( params[ii] );
   }

1379
1380
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
1381
1382
   // debug stuff

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1383
   if( printOn ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1384

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1385
      (void) fprintf( log, "%s nbondeds=%d strDim [%d %d ] sz=%d\n", methodName.c_str(), nbondeds,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1386
1387
1388
                      _particleIndicesStream->getStreamWidth(),
                      _particleIndicesStream->getStreamHeight(), 
                      _particleIndicesStream->getStreamSize() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1389
      (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404

      int kIndex = 0;
      int jIndex = 1;
      int iIndex = 2;
      int lIndex = 3;
      int mIndex = 4;
      
      /*
       * float4 parm0   (rbc[1], rbc[2], rbc[3], rbc[4])
       * float4 parm1   (rbc[5], cp, phi, mult) latter three are for pdih
       * float4 parm2   (theta, k, theta, k ) angles i-j-k and j-k-l
       * float4 parm3   (x0, k, x0, k) bonds i-j, j-k
       * float4 parm4   (x0, k, sig14, eps14) bond k-l, i-l
       */
      
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1405
      (void) fprintf( log, "\nParams\n" );
Mark Friedrichs's avatar
Mark Friedrichs committed
1406
1407
1408
      int index = 0; 
      for( int ii = 0; ii < 4*nbondeds; ii += 4, index++ ){
         //  #define PARAMS(X,Y,Z) (params[(Y)][4*(X) + Z])
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1409
         (void) fprintf( log, "\n%4d   [%4d %4d %4d %4d]\n"
Mark Friedrichs's avatar
Mark Friedrichs committed
1410
1411
1412
1413
                              "     rb[%10.6f %10.6f %10.6f %10.6f %10.6f]\n"
                              "    phi[%10.6f %6.1f %5.1f]\n"
                              "    ang[%6.3f %8.2f] [%6.3f %8.2f]\n"
                              "      h[%8.5f %14.6e] [%8.5f %14.6e] [%8.5f %14.6e]\n"
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1414
                              "     14[%15.6e %15.6e] q14=%15.6e\n", index,
Mark Friedrichs's avatar
Mark Friedrichs committed
1415
1416
1417
1418
1419
                         ATOMS(index, 0), ATOMS(index, 1), ATOMS(index, 2), ATOMS(index, 3),
                         params[kIndex][ii], params[kIndex][ii+1], params[kIndex][ii+2], params[kIndex][ii+3], params[jIndex][ii],
                         params[jIndex][ii+1], params[jIndex][ii+2], params[jIndex][ii+3],
                         params[iIndex][ii], params[iIndex][ii+1], params[iIndex][ii+2], params[iIndex][ii+3], 
                         params[lIndex][ii], params[lIndex][ii+1], params[lIndex][ii+2], params[lIndex][ii+3],
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
1420
                         params[mIndex][ii], params[mIndex][ii+1], params[mIndex][ii+2], params[mIndex][ii+3], charges[index] );
Mark Friedrichs's avatar
Mark Friedrichs committed
1421
1422
1423
1424
1425
      }
   }

   // load inverse maps to streams

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1426
1427
   if( printOn ){
      (void) fprintf( log, "\n_loadInvMaps %d %d %d %d\n", nbondeds, getNumberOfParticles(), particleStreamWidth, particleStreamSize ); (void) fflush( getLog() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1428
1429
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1430
   _loadInvMaps( nbondeds, getNumberOfParticles(), particles, particleStreamWidth, particleStreamSize );
Mark Friedrichs's avatar
Mark Friedrichs committed
1431
   
1432
1433
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
1434
1435
1436
1437
1438
   // free memory

   for( int ii = 0; ii < getNumberOfParameterStreams(); ii++ ){
      delete[] params[ii];
   }
1439

Mark Friedrichs's avatar
Mark Friedrichs committed
1440
   delete[] params;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1441
   delete[] particles;
Mark Friedrichs's avatar
Mark Friedrichs committed
1442
1443
1444
1445
1446
   delete[] charges;

   // initialize output streams

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1447
      _bondedForceStreams[ii] = new BrookFloatStreamInternal( BrookCommon::UnrolledForceStream, nbondeds, particleStreamWidth, 
Mark Friedrichs's avatar
Mark Friedrichs committed
1448
                                                              BrookStreamInternal::Float3, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
1449
1450
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1451
1452
   _setupCompleted = 1;

Mark Friedrichs's avatar
Mark Friedrichs committed
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
   return DefaultReturnValue;
}

/* 
 * Get contents of object
 *
 *
 * @param level   level of dump
 *
 * @return string containing contents
 *
 * */

Mark Friedrichs's avatar
Mark Friedrichs committed
1466
std::string BrookBonded::getContentsString( int level ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
1467
1468
1469

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

Mark Friedrichs's avatar
Mark Friedrichs committed
1470
   static const std::string methodName      = "BrookBonded::getContentsString";
Mark Friedrichs's avatar
Mark Friedrichs committed
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489

   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   = "   ";

#ifdef WIN32
#define LOCAL_SPRINTF(a,b,c) sprintf_s( (a), MAX_LINE_CHARS, (b), (c) );   
#define LOCAL_2_SPRINTF(a,b,c,d) sprintf_s( (a), MAX_LINE_CHARS, (b), (c), (d) );   
#else
#define LOCAL_SPRINTF(a,b,c) sprintf( (a), (b), (c) );   
#define LOCAL_2_SPRINTF(a,b,c,d) sprintf( (a), (b), (c), (d) );   
#endif

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1490
1491
   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParticles() );
   message << _getLine( tab, "Number of particles:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1492

Mark Friedrichs's avatar
Mark Friedrichs committed
1493
1494
1495
   (void) LOCAL_SPRINTF( value, "%d", getInverseMapStreamWidth() );
   message << _getLine( tab, "Inverse map stream width:", value ); 

Mark Friedrichs's avatar
Mark Friedrichs committed
1496
/*
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1497
1498
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamWidth() );
   message << _getLine( tab, "Particle stream width:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1499

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1503
1504
   (void) LOCAL_SPRINTF( value, "%d", getParticleStreamSize() );
   message << _getLine( tab, "Particle stream size:", value ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
*/

   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParameterStreams() );
   message << _getLine( tab, "Number of parameter streams:", value ); 
   for( int ii = 0; ii < getNumberOfParameterStreams(); ii++ ){
      char description[256];
      (void) LOCAL_SPRINTF( description, "Parameter stream %d", ii );
      message << _getLine( tab, description,  (isParameterStreamSet(ii) ? Set : NotSet) ); 
   }
 
   (void) LOCAL_SPRINTF( value, "%d", getMaxInverseMapStreamCount() )
   message << _getLine( tab, "Max inverseMap count:", value ); 

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      char description[256];
      (void) LOCAL_SPRINTF( description, "Inverse map count %d", ii );
      
      std::string checkInverseMap = checkInverseMapStream( ii );
      (void) LOCAL_2_SPRINTF( value, "%d %s", getInverseMapStreamCount( ii ), checkInverseMap.c_str() );
      message << _getLine( tab, description, value ); 
   }

   message << _getLine( tab, "Log:",                 (getLog()               ? Set : NotSet) ); 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1528
   message << _getLine( tab, "Particle indices stream:", (getParticleIndicesStream() ? Set : NotSet) ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
   //message << _getLine( tab, "Charge stream:",        (getChargeStream()      ? Set : NotSet) ); 
 
   (void) LOCAL_SPRINTF( value, "%d", getNumberOfForceStreams() );
   message << _getLine( tab, "Number of force streams:", value ); 

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      char description[256];
      (void) LOCAL_SPRINTF( description, "Force stream %d", ii );
      message << _getLine( tab, description,  (isForceStreamSet(ii) ? Set : NotSet) ); 
   }
 
#undef LOCAL_SPRINTF
#undef LOCAL_2_SPRINTF

   return message.str();
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1546
1547
1548
1549
/*
 * Helper functions for building inverse maps for 
 * torsions, impropers and angles.
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1550
 * For each particle, calculates the positions at which it's
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1551
1552
1553
 * forces are to be picked up from and stores the position
 * in the appropriate index.
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
1554
 * Input: number of torsions, the particle indices, and a flag indicating
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1555
 *        whether we're doing i(0), j(1), k(2) or l(3)
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1556
 * Output: an array of counts per particle
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1557
1558
1559
 *         arrays of inversemaps
 *         nimaps - the number of invmaps actually used.
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1560
1561
 * @param posflag       0-niparticles-1
 * @param niparticles       3 for angles, 4 for torsions, impropers
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1562
 * @param nints         number of interactions
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1563
1564
 * @param nparticles        number of particles
 * @param *particles        gromacs interaction list
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1565
 * @param nmaps         maximum number of inverse maps
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1566
 * @param   counts[]    output counts of how many places each particle occurs
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1567
1568
1569
1570
1571
1572
1573
 * @param *invmaps[]    output array of nmaps inverse maps
 * @param *nimaps,      output max number of inverse maps actually used
 *
 * @return DefaultReturnValue, unless error in which case exits w/ OpenMM exception
 *
 **/

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1574
1575
1576
int BrookBonded::_gpuCalcInvMap( int posflag, int niparticles, int nints, int nparticles,
                                 int *particles, int nmaps, int counts[], float4 *invmaps[],
                                 int *nimaps ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1577
1578
1579
1580

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

   int i, j;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1581
   int particle;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1582
1583
   int mapnum, mapcomp;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1584
   static const std::string methodName      = "BrookBonded::_gpuCalcInvMap";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1585
1586
1587
1588
1589

   static const unsigned int MAX_LINE_CHARS = 256;
   //char value[MAX_LINE_CHARS];
   static const char* Set                   = "Set";
   static const char* NotSet                = "Not set";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1590
   static const int printOn                 = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1591

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1592
1593
1594
   int particleRange[2]                     = { 90000000, -90000000 };
   int mapnumRange[2]                       = { 90000000, -90000000 };

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1595
1596
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1597
   memset( counts, 0, sizeof( int )*nparticles );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1598
1599

   for( i = 0; i < nmaps; i++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1600
      for( j = 0; j < nparticles; j++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1601
1602
1603
1604
1605
1606
1607
1608
         invmaps[i][j] = float4( -1.0, -1.0, -1.0, -1.0 );
      }
   }
   
   //This will hold the number of imaps actually used

   *nimaps = -1;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1609
   //Now note down the positions where each particle occurs
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1610

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1611
   if( printOn && getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1612
      (void) fprintf( getLog(), "%s: pos=%d ni=%d nints=%d nparticles=%d nmaps=<%d>\n", methodName.c_str(), posflag, niparticles, nints, nparticles, nmaps ); 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1613
1614
1615
1616
      (void) fflush( getLog() );
   }

   for(  i = 0; i < nints; i++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1617
1618
      //This is our particle
      particle = particles[ (niparticles + 1) * i + posflag + 1 ];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1619
1620

      //Special for merged bondeds
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1621
      if ( particle == -1 ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1622
1623
1624
         continue;
      }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1625
1626
if( particle < particleRange[0] ){
   particleRange[0] = particle;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1627
}
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1628
1629
if( particle > particleRange[1] ){
   particleRange[1] = particle;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1630
1631
}
      //Check to make sure we're inside the limits
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1632
      if ( counts[particle] > nmaps * 4 ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1633
         if( printOn && getLog() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1634
            (void) fprintf( getLog(), "%s Particle %d has too many proper torsions (%d, max %d)\n",
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1635
                            methodName.c_str(), particle, counts[particle], nmaps*4 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1636
1637
1638
            (void) fflush( getLog() );
         }
         std::stringstream message;
Mark Friedrichs's avatar
Mark Friedrichs committed
1639
         message << methodName << " Particle " << particle << " has too many proper torsions; valid range:(" << counts[particle] << ", " << nmaps*4 << ")";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1640
1641
1642
1643
1644
         throw OpenMMException( message.str() );
      }
      
      //Which invmap will this go into

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1645
      mapnum = counts[particle] / 4;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1646
1647
1648
1649
1650

      if ( mapnum > *nimaps )
         *nimaps = mapnum;

      //Which component will it be
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1651
      mapcomp = counts[particle] % 4;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1652
1653
1654
1655
1656

      //Set it
      //This is silly, but otherwise I have to declare it as float*
      //and things get even more confusing. :)
      switch (mapcomp){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1657
1658
1659
1660
         case 0: invmaps[mapnum][particle].x = (float) i; break;
         case 1: invmaps[mapnum][particle].y = (float) i; break;
         case 2: invmaps[mapnum][particle].z = (float) i; break;
         case 3: invmaps[mapnum][particle].w = (float) i; break;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1661
         default:
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1662
            if( printOn && getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1663
1664
1665
1666
1667
1668
1669
1670
1671
               (void) fprintf( getLog(), "mapcomp %d invalid -- impossible!\n", mapcomp );
               (void) fflush( getLog() );
            }
            std::stringstream message;
            message << methodName << " mapcomp " << mapcomp << " invalid -- actually impossible!";
            throw OpenMMException( message.str() );
            break;
      }
      
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1672
      counts[particle]++;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1673
1674
1675
1676
1677
1678
1679
1680

if( mapnum < mapnumRange[0] ){
   mapnumRange[0] = mapnum;
}
if( mapnum > mapnumRange[1] ){
   mapnumRange[1] = mapnum;
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1681
//fprintf( gpu->log, "%d particle=%d  mapcomp=%d counts[]=%d mapnum=%d\n", i, particle, mapcomp, counts[particle], mapnum );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1682
1683
1684
1685
1686

   }

   (*nimaps)++;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1687
if( printOn && getLog() ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1688
1689
   (void) fprintf( getLog(), "%s mnmaps=%d Ranges: particle [%d %d] mapnum [%d %d]\n",
                   methodName.c_str(), *nimaps, particleRange[0], particleRange[1], mapnumRange[0], mapnumRange[1] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1690
1691
1692
1693
1694
1695
1696
   (void) fflush( getLog() );
}

   return DefaultReturnValue;	
}


Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1697
void BrookBonded::_gpuPrintInvMaps( int nmaps, int nparticles, int counts[], float4 *invmap[], FILE* logFile ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1698
1699
   int i;
   int j;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1700
   for(  i = 0; i < nparticles; i++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
      fprintf( logFile, "%d %d ", i, counts[i] );
      for(  j = 0; j < nmaps; j++ ){
         fprintf( logFile, "%6.0f %6.0f %6.0f %6.0f", invmap[j][i].x, invmap[j][i].y, 
                  invmap[j][i].z, invmap[j][i].w );
      }
      fprintf( logFile, "\n");
   }
}

/* We are still plagued by kernel call overheads. This is for a big fat
 * merged inverse gather kernel:
 * Since we have 32 bit floats, we have 23 bits of mantissa or the largest
 * integer we can represent is 2^23. So it should be quite safe to add 
 * 100000 * n to the index where n is the stream in which we should do the
 * lookup. This assumes that nints < 100000, preferably nints << 100000
 * which should always be true
 * */
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1718

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1719
int BrookBonded::_gpuCalcInvMap_merged( 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1720
      int nints,    //number of interactions
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1721
1722
      int nparticles,   //number of particles
      int *particles,   //ijkl,ijkl,ijkl...
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1723
      int nmaps,      //maximum number of inverse maps
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1724
        int counts[],   //output counts of how many places each particle occurs
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1725
1726
1727
1728
      float4 *invmaps[], //output array of nmaps inverse maps
      int *nimaps        //output max number of inverse maps actually used
      ){
   int i, j;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1729
   int particle;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1730
1731
1732
   int mapnum, mapcomp;
   int pos;
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1733
   for(  i = 0; i < nparticles; i++ )
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1734
1735
1736
      counts[i] = 0;

   for(  i = 0; i < nmaps; i++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1737
      for(  j = 0; j < nparticles; j++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1738
1739
1740
1741
1742
1743
1744
         invmaps[i][j] = float4( -1.0, -1.0, -1.0, -1.0 );
      }
   }

   //This will hold the number of imaps actually used
   *nimaps = -1;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1745
   //For each particle
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1746
1747
1748
   for(  i = 0; i < nints; i++ ){
      for(  j = 0; j < 4; j++ ){
         
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1749
         particle = particles[ i * 4 + j ];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1750
         
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1751
1752
         if ( particle == -1 ){
         	//Nothing to be done for this particle, go to next
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1753
1754
1755
1756
         	continue;
         }
         
         //Which map
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1757
         mapnum = counts[ particle ] / 4;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1758
1759
1760
         
         //Make sure we have space
         if ( mapnum >= nmaps ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1761
1762
         	printf( "Particle %d has too many bondeds(%d, max %d)\n",
         			 particle, counts[particle], nmaps * 4 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1763
1764
1765
1766
1767
1768
1769
1770
         	return 0;
         }
         	
         if ( mapnum > *nimaps ){
         	*nimaps = mapnum;
         }

         //Which component
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1771
         mapcomp = counts[ particle ] % 4;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1772
1773
1774
1775
1776
         
         //Encode target stream and position
         pos = 100000 * j + i;

         switch ( mapcomp ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1777
1778
1779
1780
         	case 0: invmaps[mapnum][particle].x = (float) pos; break;
         	case 1: invmaps[mapnum][particle].y = (float) pos; break;
         	case 2: invmaps[mapnum][particle].z = (float) pos; break;
         	case 3: invmaps[mapnum][particle].w = (float) pos; break;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1781
1782
         }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1783
         counts[ particle ]++;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794

      }
   }
   
   (*nimaps)++;
   return 1;
}

/* Repacks the invmap streams for more efficient access in the
 * merged inverse gather kernel
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1795
 * buf should be nimaps * nparticles large.
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1796
 * */
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1797
1798
int BrookBonded::_gpuRepackInvMap_merged( int nparticles, int nmaps, int *counts, 
                                          float4 *invmaps[], float4 *buf ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1799
1800
1801
   int i, j;
   int nmaps_i;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1802
   for(  i = 0; i < nparticles; i++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1803
      for(  j = 0; j < nmaps; j++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1804
         buf[ i + j*nparticles ] = float4( -1.0f, -1.0f, -1.0f, -1.0f );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1805
1806
1807
      }
   }
   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1808
   for(  i = 0; i < nparticles; i++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1809
1810
1811
1812
1813
1814
      
      nmaps_i = counts[i] / 4;
      if ( counts[i] % 4 ) 
         nmaps_i++;
      
      for(  j = 0; j < nmaps_i; j++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1815
         buf[ i + j * nparticles ] = invmaps[j][i];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1816
1817
1818
1819
      }
   }
   return 1;
}
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831

/** 
 * Compute forces
 * 
 */

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1832
   static int printOn                       = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1833

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
   static const int I_Stream                = 0;
   static const int J_Stream                = 1;
   static const int K_Stream                = 2;
   static const int L_Stream                = 3;

   static const int MaxErrorMessages        = 2;
   static       int ErrorMessages           = 0;

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1844
   printOn = (printOn && getLog()) ? printOn : 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1845

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1846
1847
   // bonded

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1848
   float epsfac                                        = (float) (getCoulombFactor());
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
   float width                                         = (float) (getInverseMapStreamWidth());

   // bonded forces

   BrookFloatStreamInternal**  bondedParameters        = getBondedParameterStreams();
   BrookFloatStreamInternal**  bondedForceStreams      = getBondedForceStreams();

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

   kbonded_CDLJ( epsfac, 
                 (float) bondedForceStreams[0]->getStreamWidth(),
                 positionStream.getBrookStream(),
                 getChargeStream()->getBrookStream(),
                 getParticleIndicesStream()->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() );


   // diagnostics

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1880
1881
1882
   if( printOn ){
      FILE* log = getLog();
      //FILE* log = stderr;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1883
1884
1885

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1886
1887
1888
      (void) fprintf( log, "\nPost kbonded_CDLJ: epsFac=%.6f %.6f", epsfac, getCoulombFactor());
      (void) fprintf( log, "\nParticle indices stream\n" );
      getParticleIndicesStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1889

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1890
1891
      (void) fprintf( log, "\nCharge stream\n" );
      getChargeStream()->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1892
1893

      for( int ii = 0; ii < 5; ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1894
1895
         (void) fprintf( log, "\nParam stream %d\n", ii );
         bondedParameters[ii]->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1896
1897
      }
      for( int ii = 0; ii < 4; ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1898
1899
         (void) fprintf( log, "\nForce stream %d\n", ii );
         bondedForceStreams[ii]->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1900
1901
1902
      }

/*
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1903
      (void) fprintf( log, "\nNB1 forces" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1904
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1905
      brookStreamInternalF->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1906
1907
*/

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1908
      (void) fprintf( log, "\nInverse map streams\n" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1909
      for( int ii = 0; ii < 4; ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1910
         (void) fprintf( log, "\nInverse map streams -- StreamIndex=%d cnt=%d\n", ii, getInverseMapStreamCount( ii ) );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1911
         for( int jj = 0; jj < countPrintInvMap[ii]; jj++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1912
1913
            (void) fprintf( log, "\n   Inverse map streams index=%d %d\n", ii, jj );
            inverseStreamMaps[ii][jj]->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
         }
      }
   }

   // gather forces

   if( getInverseMapStreamCount( I_Stream ) == 3 && 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() );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
   } else if( getInverseMapStreamCount( I_Stream ) == 2 && getInverseMapStreamCount( K_Stream ) == 2 ){

      kinvmap_gather2_2( width,

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
   } else if( getInverseMapStreamCount( I_Stream ) == 3 && getInverseMapStreamCount( K_Stream ) == 4 ){

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

   } else if( getInverseMapStreamCount( I_Stream ) == 3 && getInverseMapStreamCount( K_Stream ) == 5 ){

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

Mark Friedrichs's avatar
Mark Friedrichs committed
1982
1983
1984
1985
1986
1987
1988
1989
1990
   } else if( getInverseMapStreamCount( I_Stream ) == 1 && getInverseMapStreamCount( K_Stream ) == 1 ){

      kinvmap_gather1_1( width,
                         inverseStreamMaps[I_Stream][0]->getBrookStream(),
                         bondedForceStreams[I_Stream]->getBrookStream(),
                         inverseStreamMaps[K_Stream][0]->getBrookStream(),
                         bondedForceStreams[K_Stream]->getBrookStream(),
                         forceStream.getBrookStream(), forceStream.getBrookStream() );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1991
1992
1993
1994
   } else {

      // case not handled -- throw an exception

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1995
1996
1997
      FILE* log =  getLog() ?  getLog() : stderr;
      if( ErrorMessages++ < MaxErrorMessages && getInverseMapStreamCount( I_Stream ) > 0 && getInverseMapStreamCount( K_Stream ) > 0 ){
         (void) fprintf( log, "%s case: I-map=%d K-map=%d -- not handled.\n",
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
1998
1999
                          methodName.c_str(), getInverseMapStreamCount( I_Stream ),
                                              getInverseMapStreamCount( K_Stream ) );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2000
         (void) fflush( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
      }

      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() );
/*
      std::stringstream message;
      message << methodName << "I-maps=" << getInverseMapStreamCount( I_Stream ) << " and " << 
                               "K-maps=" << getInverseMapStreamCount( K_Stream ) << " not handled.";
      throw OpenMMException( message.str() );
*/

   }

   // diagnostics

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2027
   if( printOn ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2028

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2029
2030
      FILE* log =  getLog() ?  getLog() : stderr;
      (void) fprintf( log, "\nPost 3_4/3_5 && NB forces" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2031
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2032
      brookStreamInternalF->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2033
2034
2035

   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
   if( getInverseMapStreamCount( J_Stream ) == 1 && getInverseMapStreamCount( L_Stream ) == 1 ){

      kinvmap_gather1_1( width,
                         inverseStreamMaps[J_Stream][0]->getBrookStream(),
                         bondedForceStreams[J_Stream]->getBrookStream(),
                         inverseStreamMaps[L_Stream][0]->getBrookStream(),
                         bondedForceStreams[L_Stream]->getBrookStream(),
                         forceStream.getBrookStream(), forceStream.getBrookStream() );
   
   } else if( getInverseMapStreamCount( J_Stream ) == 5 && getInverseMapStreamCount( L_Stream ) == 2 ){

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
      kinvmap_gather5_2( width,
                         inverseStreamMaps[J_Stream][0]->getBrookStream(),
                         inverseStreamMaps[J_Stream][1]->getBrookStream(),
                         inverseStreamMaps[J_Stream][2]->getBrookStream(),
                         inverseStreamMaps[J_Stream][3]->getBrookStream(),
                         inverseStreamMaps[J_Stream][4]->getBrookStream(),
                         bondedForceStreams[J_Stream]->getBrookStream(),
                         inverseStreamMaps[L_Stream][0]->getBrookStream(),
                         inverseStreamMaps[L_Stream][1]->getBrookStream(),
                         bondedForceStreams[L_Stream]->getBrookStream(),
                         forceStream.getBrookStream(), forceStream.getBrookStream() );
   
   } else {

      // case not handled -- throw an exception

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2063
2064
2065
      FILE* log =  getLog() ?  getLog() : stderr;
      if( ErrorMessages++ < MaxErrorMessages && getInverseMapStreamCount( J_Stream ) > 0 && getInverseMapStreamCount( L_Stream ) > 0 ){
         (void) fprintf( log, "%s case: J-map=%d L-map=%d -- not handled.\n",
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2066
2067
                          methodName.c_str(), getInverseMapStreamCount( J_Stream ),
                                              getInverseMapStreamCount( L_Stream ) );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2068
         (void) fflush(  log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2069
2070
      }

Mark Friedrichs's avatar
Mark Friedrichs committed
2071
2072
2073
      // this is for testing purposes a-- may need to be cleaned 
      // or add new gather functions

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
      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() );
Mark Friedrichs's avatar
Mark Friedrichs committed
2085

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
/*
      std::stringstream message;
      message << methodName << "J-maps=" << getInverseMapStreamCount( J_Stream ) << " and " << 
                               "L-maps=" << getInverseMapStreamCount( L_Stream ) << " not handled.";
      throw OpenMMException( message.str() );
*/

   }

   // diagnostics

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2097
   if( printOn ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2098

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2099
2100
      FILE* log =  getLog() ?  getLog() : stderr;
      (void) fprintf( log, "\nFinal NB & bonded forces" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2101
      BrookStreamInternal* brookStreamInternalF   = forceStream.getBrookStreamImpl();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2102
      brookStreamInternalF->printToFile( log );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
/*
      void* dataV = brookStreamInternalF->getData(1);
      float* data = (float*) dataV;
      (void) fprintf( getLog(), "\nFinal NB & bonded forces RAW\n" );
      for( int ii = 0; ii < _brookNonBonded->getNumberOfParticles()*3; ii += 3 ){
         (void) fprintf( getLog(), "%d [%.6e %.6e %.6e]\n", ii, data[ii], data[ii+1], data[ii+2] );
      }
*/

   }

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