BrookGbsa.cpp 27 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
/* -------------------------------------------------------------------------- *
 *                                   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 <sstream>
#include "BrookGbsa.h"
#include "BrookPlatform.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
35
36
#include "OpenMMException.h"
#include "BrookStreamImpl.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

using namespace OpenMM;
using namespace std;

/** 
 * Constructor
 * 
 */

BrookGbsa::BrookGbsa(  ){

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

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

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

   _atomSizeCeiling           = -1;
   _outerUnroll               =  4;
   _innerUnroll               =  4;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
62
63
64
   _gbsaAtomStreamWidth       = -1;
   _gbsaAtomStreamHeight      = -1;
   _gbsaAtomStreamSize        = -1;
Mark Friedrichs's avatar
Mark Friedrichs committed
65
66
67

   _duplicationFactor         =  4;

Mark Friedrichs's avatar
Mark Friedrichs committed
68
   _includeAce                = 1;
Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
   _solventDielectric         = 78.3;
   _soluteDielectric          = 1.0;
Mark Friedrichs's avatar
Mark Friedrichs committed
71
   _dielectricOffset          = 0.09;
Mark Friedrichs's avatar
Mark Friedrichs committed
72
73
74
75
76
77
78
79
80

   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      _gbsaStreams[ii] = NULL;
   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
81
82
   _bornRadiiInitialized      = 0;
   _cpuObc                    = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}   
 
/** 
 * Destructor
 * 
 */

BrookGbsa::~BrookGbsa( ){

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

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

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

   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      delete _gbsaStreams[ii];
   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
106
107
   delete _cpuObc;

Mark Friedrichs's avatar
Mark Friedrichs committed
108
109
110
111
112
113
114
115
116
117
118
119
120
}

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
121
122
123
124
125
126
127
128
129
130
131
132

/** 
 * Include ACE approximation in calculation of force
 * 
 * @return true if ACE approximation is to be included in calculation of force
 *
 */

int BrookGbsa::includeAce( void ) const {
   return _includeAce;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
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
/** 
 * Get inner loop unroll
 * 
 * @return   inner loop unroll (fixed value)
 *
 */

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

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

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

/** 
 * Get solute dielectric
 * 
 * @return   solute dielectric
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
162
163
float BrookGbsa::getSoluteDielectric( void ) const {
   return (float) _soluteDielectric;
Mark Friedrichs's avatar
Mark Friedrichs committed
164
165
166
167
168
169
170
171
172
}

/** 
 * Get solvent dielectric
 * 
 * @return   solvent dielectric
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
173
174
175
176
177
178
179
180
181
182
183
184
185
float BrookGbsa::getSolventDielectric( void ) const {
   return (float) _solventDielectric;
}

/** 
 * Get OBC dielectric offset
 * 
 * @return  OBC dielectric offset
 *
 */

float BrookGbsa::getDielectricOffset( void ) const {
   return (float) _dielectricOffset;
Mark Friedrichs's avatar
Mark Friedrichs committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
}

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

int BrookGbsa::setOuterLoopUnroll( int outerUnroll ){
   if( outerUnroll != _outerUnroll ){
      _atomSizeCeiling = -1;
   }
   _outerUnroll = _outerUnroll;
   return _outerUnroll;
}

/** 
 * Get atom ceiling parameter
 * 
 * @return atom ceiling parameter
 *
 */

int BrookGbsa::getAtomSizeCeiling( void ) const {

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

   //static const std::string methodName      = "BrookGbsa::getAtomSizeCeiling";

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

   if( _atomSizeCeiling < 0 ){
      BrookGbsa* localThis = const_cast<BrookGbsa* const>(this);
      localThis->_atomSizeCeiling = localThis->getNumberOfAtoms() % localThis->getOuterLoopUnroll();
      if( localThis->_atomSizeCeiling ){
         localThis->_atomSizeCeiling = localThis->getOuterLoopUnroll() - localThis->_atomSizeCeiling;
      }   
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
226
      localThis->_atomSizeCeiling += localThis->getNumberOfAtoms();
Mark Friedrichs's avatar
Mark Friedrichs committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
   }

   return _atomSizeCeiling;
}

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

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

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

int BrookGbsa::getPartialForceStreamWidth( void ) const {

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

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

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

   return _partialForceStreamWidth;
}

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

int BrookGbsa::getPartialForceStreamHeight( void ) const {

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

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

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

   return _partialForceStreamHeight;
}

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

Mark Friedrichs's avatar
Mark Friedrichs committed
287
int BrookGbsa::getPartialForceStreamSize( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
288
289
290
291
   return _partialForceStreamSize;
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
292
293
294
 * Get Atom stream size
 *
 * @return  Atom stream size
Mark Friedrichs's avatar
Mark Friedrichs committed
295
296
297
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
298
299
int BrookGbsa::getGbsaAtomStreamSize( void ) const {
   return _gbsaAtomStreamSize;
Mark Friedrichs's avatar
Mark Friedrichs committed
300
301
302
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
303
 * Get atom stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
304
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
305
 * @return  atom stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
306
307
308
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
309
310
int BrookGbsa::getGbsaAtomStreamWidth( void ) const {
   return _gbsaAtomStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
311
312
313
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
314
 * Get atom stream height
Mark Friedrichs's avatar
Mark Friedrichs committed
315
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
316
 * @return atom stream height
Mark Friedrichs's avatar
Mark Friedrichs committed
317
318
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
319
320
int BrookGbsa::getGbsaAtomStreamHeight( void ) const {
   return _gbsaAtomStreamHeight;
Mark Friedrichs's avatar
Mark Friedrichs committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
}

/** 
 * Get Obc atomic radii stream 
 *
 * @return  Obc atomic radii stream
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcAtomicRadii( void ) const {
   return _gbsaStreams[ObcAtomicRadiiStream];
}

/** 
 * Get Obc scaled atomic radii stream 
 *
 * @return  Obc scaled atomic radii stream
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcScaledAtomicRadii( void ) const {
   return _gbsaStreams[ObcScaledAtomicRadiiStream];
}

/** 
 * Get Obc atomic radii w/ dielectric offset
 *
 * @return  Obc atomic radii w/ dielectric offset
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcAtomicRadiiWithDielectricOffset( void ) const {
   return _gbsaStreams[ObcAtomicRadiiWithDielectricOffsetStream];
}

/** 
 * Get Obc Born radii
 *
 * @return  Obc Born radii
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcBornRadii( void ) const {
   return _gbsaStreams[ObcBornRadiiStream];
}

/** 
 * Get Obc Born radii2
 *
 * @return  Obc Born radii2
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcBornRadii2( void ) const {
   return _gbsaStreams[ObcBornRadii2Stream];
}

/** 
 * Get Obc intermediate force stream
 *
 * @return  Obcintermediate force stream
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcIntermediateForce( void ) const {
   return _gbsaStreams[ObcIntermediateForceStream];
}

/** 
 * Get Obc chain stream 
 *
 * @return  Obc chain stream
 *
 */

BrookFloatStreamInternal* BrookGbsa::getObcChain( void ) const {
   return _gbsaStreams[ObcChainStream];
}

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

BrookFloatStreamInternal** BrookGbsa::getForceStreams( void ){
   return _gbsaForceStreams;
}

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

Mark Friedrichs's avatar
Mark Friedrichs committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/** 
 * Return true if Born radii have been initialized
 *
 * @return   true if Born radii have been initialized
 *
 */

int BrookGbsa::haveBornRadiiBeenInitialized( void ) const {
   return  _bornRadiiInitialized;
}

/** 
 * Calculate Born radii
 *
 * @return  calculate Born radii
 *
 */

int BrookGbsa::calculateBornRadii( const Stream& positions ){

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

   static const std::string methodName                 = "BrookGbsa::calculateBornRadii";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
446
   static const int PrintOn                            = 1;
Mark Friedrichs's avatar
Mark Friedrichs committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467

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

   const BrookStreamImpl& positionStreamC              = dynamic_cast<const BrookStreamImpl&> (positions.getImpl());
   BrookStreamImpl& positionStream                     = const_cast<BrookStreamImpl&>         (positionStreamC);
   const RealOpenMM* coordinates                       = (RealOpenMM*) positionStream.getData( );

   // load coordinates into RealOpenMM 2d array

   int numberOfAtoms                                   = getNumberOfAtoms();

   RealOpenMM** atomCoordinates                        = new RealOpenMM*[numberOfAtoms];
   RealOpenMM* atomCoordinatesBlk                      = new RealOpenMM[3*numberOfAtoms];

   // Born radii array size needs to match stream size since it will
   // be written down to board

   int streamSize                                      = getGbsaAtomStreamSize();
   RealOpenMM* bornRadii                               = new RealOpenMM[streamSize];
   memset( bornRadii, 0, sizeof( RealOpenMM )*streamSize );

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
468
469
470
471
472
   RealOpenMM* obcChain                                = new RealOpenMM[streamSize];
   memset( obcChain, 0, sizeof( RealOpenMM )*streamSize );

   int index                                           = 0;
   RealOpenMM* atomCoordinatesBlkPtr                   = atomCoordinatesBlk;
Mark Friedrichs's avatar
Mark Friedrichs committed
473
474
   for( int ii = 0; ii < numberOfAtoms; ii++ ){

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
475
476
      atomCoordinates[ii]    = atomCoordinatesBlkPtr;
      atomCoordinatesBlkPtr += 3;
Mark Friedrichs's avatar
Mark Friedrichs committed
477
478
479
480
481
482
483
484
485

      atomCoordinates[ii][0] = coordinates[index++];
      atomCoordinates[ii][1] = coordinates[index++];
      atomCoordinates[ii][2] = coordinates[index++];

   }

   // calculate Born radii

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
486
487
488
   _cpuObc->computeBornRadii( atomCoordinates, bornRadii, obcChain );

   // diagnostics
Mark Friedrichs's avatar
Mark Friedrichs committed
489

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
490
491
492
493
494
495
496
497
498
   if( PrintOn ){

      (void) fprintf( getLog(), "\n%s: atms=%d\n", methodName.c_str(), numberOfAtoms );
      for( int ii = 0; ii < numberOfAtoms; ii++ ){
         (void) fprintf( getLog(), "%d coord=[%.5e %.5e %.5e]  bR=%.5e obcChain=%.6e\n", ii,
                         atomCoordinates[ii][0], atomCoordinates[ii][1], atomCoordinates[ii][2], bornRadii[ii], obcChain[ii] );
      }
   }
  
Mark Friedrichs's avatar
Mark Friedrichs committed
499
500
501
   // write radii to board and set flag to indicate radii calculated once

   _gbsaStreams[ObcBornRadiiStream]->loadFromArray( bornRadii );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
502
   _gbsaStreams[ObcChainStream]->loadFromArray( obcChain );
Mark Friedrichs's avatar
Mark Friedrichs committed
503
504
505
506
507
508
509
   _bornRadiiInitialized   = 1;

   // free memory

   delete[] atomCoordinatesBlk;
   delete[] atomCoordinates;
   delete[] bornRadii;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
510
   delete[] obcChain;
Mark Friedrichs's avatar
Mark Friedrichs committed
511
512
513
514

   return DefaultReturnValue;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/** 
 * Initialize stream dimensions
 * 
 * @param numberOfAtoms             number of atoms
 * @param platform                  platform
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 */

int BrookGbsa::initializeStreamSizes( int numberOfAtoms, const Platform& platform ){

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

   static const std::string methodName      = "BrookGbsa::initializeStreamSizes";

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

Mark Friedrichs's avatar
Mark Friedrichs committed
533
534
535
   _gbsaAtomStreamSize     = getAtomStreamSize( platform );
   _gbsaAtomStreamWidth    = getAtomStreamWidth( platform );
   _gbsaAtomStreamHeight   = getAtomStreamHeight( platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

   return DefaultReturnValue;
}

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

int BrookGbsa::initializeStreams( const Platform& platform ){

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

   static const std::string methodName      = "BrookGbsa::initializeStreams";
   static const double dangleValue          = 0.0;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
558
559
560
561
562
563
564
565
   int gbsaAtomStreamSize   = getGbsaAtomStreamSize();
   int gbsaAtomStreamWidth  = getGbsaAtomStreamWidth();

   // atomic radii & charge

    _gbsaStreams[ObcAtomicRadiiStream]                          = new BrookFloatStreamInternal( BrookCommon::ObcAtomicRadiiStream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
                                                                                                BrookStreamInternal::Float2, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
566

Mark Friedrichs's avatar
Mark Friedrichs committed
567
   // scaled atomic radii
Mark Friedrichs's avatar
Mark Friedrichs committed
568

Mark Friedrichs's avatar
Mark Friedrichs committed
569
570
571
    _gbsaStreams[ObcScaledAtomicRadiiStream]                    = new BrookFloatStreamInternal( BrookCommon::ObcScaledAtomicRadiiStream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
                                                                                                BrookStreamInternal::Float2, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
572

Mark Friedrichs's avatar
Mark Friedrichs committed
573
   // atomic radii w/ DielectricOffset
Mark Friedrichs's avatar
Mark Friedrichs committed
574

Mark Friedrichs's avatar
Mark Friedrichs committed
575
576
577
    _gbsaStreams[ObcAtomicRadiiWithDielectricOffsetStream]      = new BrookFloatStreamInternal( BrookCommon::ObcAtomicRadiiWithDielectricOffsetStream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
                                                                                                BrookStreamInternal::Float, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
578

Mark Friedrichs's avatar
Mark Friedrichs committed
579
   // Born radii
Mark Friedrichs's avatar
Mark Friedrichs committed
580

Mark Friedrichs's avatar
Mark Friedrichs committed
581
582
583
    _gbsaStreams[ObcBornRadiiStream]                            = new BrookFloatStreamInternal( BrookCommon::ObcBornRadiiStream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
                                                                                                BrookStreamInternal::Float, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
584

Mark Friedrichs's avatar
Mark Friedrichs committed
585
   // Born2 radii
Mark Friedrichs's avatar
Mark Friedrichs committed
586

Mark Friedrichs's avatar
Mark Friedrichs committed
587
588
589
    _gbsaStreams[ObcBornRadii2Stream]                           = new BrookFloatStreamInternal( BrookCommon::ObcBornRadii2Stream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
                                                                                                BrookStreamInternal::Float, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
590

Mark Friedrichs's avatar
Mark Friedrichs committed
591
   // IntermediateForce
Mark Friedrichs's avatar
Mark Friedrichs committed
592

Mark Friedrichs's avatar
Mark Friedrichs committed
593
594
    _gbsaStreams[ObcIntermediateForceStream]                    = new BrookFloatStreamInternal( BrookCommon::ObcIntermediateForceStream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
595
                                                                                                BrookStreamInternal::Float4, dangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
596

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
597
   // Obc chain
Mark Friedrichs's avatar
Mark Friedrichs committed
598
599
600
601
602
603
604
605

    _gbsaStreams[ObcChainStream]                                = new BrookFloatStreamInternal( BrookCommon::ObcChainStream,
                                                                                                gbsaAtomStreamSize, gbsaAtomStreamWidth,
                                                                                                BrookStreamInternal::Float, dangleValue );

   // partial force streams

   std::string partialForceStream = BrookCommon::PartialForceStream;
Mark Friedrichs's avatar
Mark Friedrichs committed
606
607
608
609
610
611
612
613
614
615
616
617
618
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      std::stringstream name;
      name << partialForceStream << ii;
      _gbsaForceStreams[ii] = new BrookFloatStreamInternal( name.str(), getPartialForceStreamSize(),
                                                                 getPartialForceStreamWidth(), BrookStreamInternal::Float4, dangleValue );
   }

   return DefaultReturnValue;
}

/*  
 * Setup of Gbsa parameters
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
619
620
 * @param atomParameters        vector of OBC parameters [atomI][0=charge]
 *                                                       [atomI][1=radius]
Mark Friedrichs's avatar
Mark Friedrichs committed
621
622
623
 *                                                       [atomI][2=scaling factor]
 * @param solventDielectric     solvent dielectric
 * @param soluteDielectric      solute dielectric
Mark Friedrichs's avatar
Mark Friedrichs committed
624
 * @param platform              Brook platform
Mark Friedrichs's avatar
Mark Friedrichs committed
625
626
627
628
629
 *
 * @return nonzero value if error
 *
 * */
    
Mark Friedrichs's avatar
Mark Friedrichs committed
630
631
int BrookGbsa::setup( const std::vector<std::vector<double> >& vectorOfAtomParameters, 
                      double solventDielectric, double soluteDielectric, const Platform& platform ){
Mark Friedrichs's avatar
Mark Friedrichs committed
632
633
634
    
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
635
   static const int atomParametersSize      = 3; 
Mark Friedrichs's avatar
Mark Friedrichs committed
636
   static const int maxErrors               = 20; 
Mark Friedrichs's avatar
Mark Friedrichs committed
637
638
639
640
   static const std::string methodName      = "BrookGbsa::setup";

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

Mark Friedrichs's avatar
Mark Friedrichs committed
641
642
643
644
645
   int numberOfAtoms  = (int) vectorOfAtomParameters.size();
   setNumberOfAtoms( numberOfAtoms );

   _solventDielectric = solventDielectric;
   _soluteDielectric  = soluteDielectric;
Mark Friedrichs's avatar
Mark Friedrichs committed
646

Mark Friedrichs's avatar
Mark Friedrichs committed
647
   // initialize stream sizes and then Brook streams
Mark Friedrichs's avatar
Mark Friedrichs committed
648

Mark Friedrichs's avatar
Mark Friedrichs committed
649
   initializeStreamSizes( numberOfAtoms, platform );
Mark Friedrichs's avatar
Mark Friedrichs committed
650
651
   initializeStreams( platform );

Mark Friedrichs's avatar
Mark Friedrichs committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
   BrookOpenMMFloat* radiiAndCharge         = new BrookOpenMMFloat[getNumberOfAtoms()*2];
   BrookOpenMMFloat* scaledRadiiAndOffset   = new BrookOpenMMFloat[getNumberOfAtoms()*2];

   // used by CpuObc to calculate initial Born radii

   vector<RealOpenMM> atomicRadii(numberOfAtoms);
   vector<RealOpenMM> scaleFactors(numberOfAtoms);

   float dielectricOffset                  = getDielectricOffset();

   // loop over atom parameters
   // track any errors and then throw exception
   //    check parameter vector is right size
   // set parameter entries or board and arrays used by CpuObc

   int vectorIndex  = 0;
   int errors       = 0;
   std::stringstream message;

   typedef std::vector< std::vector<double> > VectorOfDoubleVectors;
   typedef VectorOfDoubleVectors::const_iterator VectorOfDoubleVectorsCI;

   for( VectorOfDoubleVectorsCI ii = vectorOfAtomParameters.begin(); ii != vectorOfAtomParameters.end(); ii++ ){

      std::vector<double> atomParameters = *ii;

      if( atomParameters.size() != atomParametersSize && errors < maxErrors ){
         message << methodName << " parameter size=" << atomParameters.size() << " for parameter vector index=" << vectorIndex << " is less than expected.\n";
         errors++;
      } else {

         double charge                            = atomParameters[0];     
         double radius                            = atomParameters[1];     
         double scalingFactor                     = atomParameters[2];     

         int streamIndex                          = 2*vectorIndex;

         atomicRadii[vectorIndex]                 = static_cast<RealOpenMM> (radius);
         scaleFactors[vectorIndex]                = static_cast<RealOpenMM> (scalingFactor);

         radiiAndCharge[streamIndex]              = static_cast<BrookOpenMMFloat> (radius);
         radiiAndCharge[streamIndex+1]            = static_cast<BrookOpenMMFloat> (charge);

         scaledRadiiAndOffset[streamIndex]        = static_cast<BrookOpenMMFloat> (radius*scalingFactor);
         scaledRadiiAndOffset[streamIndex+1]      = static_cast<BrookOpenMMFloat> (radius - dielectricOffset);

      }

      vectorIndex++;
   }

   // throw exception if errors detected

   if( errors ){
      throw OpenMMException( message.str() );
   }

   // load streams

   _gbsaStreams[ObcAtomicRadiiStream]->loadFromArray( radiiAndCharge );
   _gbsaStreams[ObcScaledAtomicRadiiStream]->loadFromArray( scaledRadiiAndOffset );

   delete[] radiiAndCharge;
   delete[] scaledRadiiAndOffset;

   // setup for Born radii

   ObcParameters* obcParameters  = new ObcParameters( numberOfAtoms, ObcParameters::ObcTypeII );
   obcParameters->setAtomicRadii( atomicRadii, SimTKOpenMMCommon::MdUnits);

   obcParameters->setScaledRadiusFactors( scaleFactors );
   obcParameters->setSolventDielectric( static_cast<RealOpenMM>(solventDielectric) );
   obcParameters->setSoluteDielectric(  static_cast<RealOpenMM>(soluteDielectric)  );

   _cpuObc  = new CpuObc(obcParameters);
   _cpuObc->setIncludeAceApproximation( true );

Mark Friedrichs's avatar
Mark Friedrichs committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
   return DefaultReturnValue;
}

/* 
 * Setup of stream dimensions for partial force streams
 *
 * @param atomStreamSize        atom stream size
 * @param atomStreamWidth       atom stream width
 *
 * @return ErrorReturnValue if error, else DefaultReturnValue
 *
 * */

int BrookGbsa::initializePartialForceStreamSize( int atomStreamSize, int atomStreamWidth ){

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

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

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

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

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

   _partialForceStreamSize    = atomStreamSize*getDuplicationFactor()/innerUnroll;
   _partialForceStreamHeight  = _partialForceStreamSize/_partialForceStreamWidth;
   _partialForceStreamHeight += ( (_partialForceStreamSize % _partialForceStreamWidth) ? 1 : 0);

   return DefaultReturnValue;
}

/* 
 * Setup of j-stream dimensions
 * 
 * Get contents of object
 *
 *
 * @param level   level of dump
 *
 * @return string containing contents
 *
 * */

std::string BrookGbsa::getContentsString( int level ) const {

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

   static const std::string methodName      = "BrookGbsa::getContentsString";

   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) );   
#else
#define LOCAL_SPRINTF(a,b,c) sprintf( (a), (b), (c) );   
#endif

   (void) LOCAL_SPRINTF( value, "%d", getNumberOfAtoms() );
   message << _getLine( tab, "Number of atoms:", value ); 

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

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

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

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

   (void) LOCAL_SPRINTF( value, "%d", getAtomSizeCeiling() );
   message << _getLine( tab, "Atom ceiling:", value ); 

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

   (void) LOCAL_SPRINTF( value, "%d", getAtomStreamHeight() );
   message << _getLine( tab, "Atom stream height:", value ); 

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

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

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

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

   message << _getLine( tab, "Log:",                  (getLog()                ? Set : NotSet) ); 
 
   for( int ii = 0; ii < LastStreamIndex; ii++ ){
      message << std::endl;
      if( _gbsaStreams[ii] ){
         message << _gbsaStreams[ii]->getContentsString( );
      }
   }

   // force streams

   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      char description[256];
      (void) LOCAL_SPRINTF( description, "PartialForceStream %d", ii );
      message << _getLine( tab, description,  (isForceStreamSet(ii) ? Set : NotSet) ); 
   }
 
   for( int ii = 0; ii < getNumberOfForceStreams(); ii++ ){
      message << std::endl;
      if( _gbsaForceStreams[ii] ){
         message << _gbsaForceStreams[ii]->getContentsString( );
      }
   }

#undef LOCAL_SPRINTF

   return message.str();
}