"platforms/cuda/tests/TestCudaCustomCompoundBondForce.cpp" did not exist on "a402046652cab8ba297aa423e4cb57c904525144"
BrookCommon.cpp 15.4 KB
Newer Older
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
/* -------------------------------------------------------------------------- *
 *                                   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>
Mark Friedrichs's avatar
Mark Friedrichs committed
33
34
#include <stdlib.h>

35
#include <sstream>
Mark Friedrichs's avatar
Mark Friedrichs committed
36

37
38
39
40
41
42
43
44
#include "BrookCommon.h"
#include "BrookPlatform.h"
#include "BrookStreamFactory.h"
#include "OpenMMException.h"

using namespace OpenMM;
using namespace std;

Mark Friedrichs's avatar
Mark Friedrichs committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// bonded streams
    
const std::string BrookCommon::BondedAtomIndicesStream                            = "BondedAtomIndicesStream";
const std::string BrookCommon::BondedParametersStream                             = "BondedParametersStream";
const std::string BrookCommon::UnrolledForceStream                                = "UnrolledForceStream";
const std::string BrookCommon::BondedChargeStream                                 = "BondedChargeStream";
const std::string BrookCommon::BondedInverseMapStreams                            = "BondedInverseMapStreams";

// non-bonded streams

const std::string BrookCommon::NonBondedExclusionStream                           = "NonBondedExclusionStream";
const std::string BrookCommon::OuterVdwStream                                     = "OuterVdwStream";
const std::string BrookCommon::InnerSigmaStream                                   = "InnerSigmaStream";
const std::string BrookCommon::InnerEpsilonStream                                 = "InnerEpsilonStream";
const std::string BrookCommon::NonBondedChargeStream                              = "NonBondedChargeStream";
const std::string BrookCommon::PartialForceStream                                 = "PartialForceStream";

// OBC Gbsa streams

const std::string BrookCommon::ObcAtomicRadiiStream                               = "ObcAtomicRadiiStream";
const std::string BrookCommon::ObcScaledAtomicRadiiStream                         = "ObcScaledAtomicRadiiStream";
const std::string BrookCommon::ObcAtomicRadiiWithDielectricOffsetStream           = "ObcAtomicRadiiWithDielectricOffsetStream";
const std::string BrookCommon::ObcBornRadiiStream                                 = "ObcBornRadiiStream";
const std::string BrookCommon::ObcBornRadii2Stream                                = "ObcBornRadii2Stream";
const std::string BrookCommon::ObcIntermediateForceStream                         = "ObcIntermediateForceStream";
const std::string BrookCommon::ObcChainStream                                     = "ObcChainStream";

Mark Friedrichs's avatar
Mark Friedrichs committed
72
73
74
75
76
77
// StochasticDynamics streams

const std::string BrookCommon::SDPC1Stream                                        = "SDPC1Stream";
const std::string BrookCommon::SDPC2Stream                                        = "SDPC2Stream";
const std::string BrookCommon::SD2XStream                                         = "SD2XStream";
const std::string BrookCommon::SD1VStream                                         = "SD1VStream";
Mark Friedrichs's avatar
Mark Friedrichs committed
78
79
80
const std::string BrookCommon::VPrimeStream                                       = "VPrimeStream";
const std::string BrookCommon::XPrimeStream                                       = "XPrimeStream";
const std::string BrookCommon::InverseMassStream                                  = "InverseMassStream";
Mark Friedrichs's avatar
Mark Friedrichs committed
81

Mark Friedrichs's avatar
Mark Friedrichs committed
82
83
84
85
86
87
88
89
90
91
// Shake streams

const std::string BrookCommon::ShakeAtomIndicesStream                             = "ShakeAtomIndicesStream";
const std::string BrookCommon::ShakeAtomParameterStream                           = "ShakeAtomParameterStream";
const std::string BrookCommon::ShakeXCons0Stream                                  = "ShakeXCons0Stream";
const std::string BrookCommon::ShakeXCons1Stream                                  = "ShakeXCons1Stream";
const std::string BrookCommon::ShakeXCons2Stream                                  = "ShakeXCons2Stream";
const std::string BrookCommon::ShakeXCons3Stream                                  = "ShakeXCons3Stream";
const std::string BrookCommon::ShakeInverseMapStream                              = "ShakeInverseMapStream";

Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
94
95
// Random number streams

const std::string BrookCommon::ShuffleStream                                      = "ShuffleStream";

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/** 
 * Constructor
 * 
 */

BrookCommon::BrookCommon(  ){

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

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

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

   _numberOfAtoms             = 0;
   _atomSizeModified          = 0;

   _atomStreamWidth           = -1;
   _atomStreamHeight          = -1; 
   _atomStreamSize            = -1;

   _log                       = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
118
   _verbosity                 = 0;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

}   
 
/** 
 * Destructor
 * 
 */

BrookCommon::~BrookCommon( ){

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

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

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

}

/** 
 * Get number of atoms
 * 
 * @return  number of atoms
 *
 */

int BrookCommon::getNumberOfAtoms( void ) const {
   return _numberOfAtoms;
}

/** 
 * Get number of atoms
 * 
 * @param  numberOfAtoms number of atoms
 * @return  number of atoms
 *
 */

int BrookCommon::setNumberOfAtoms( int numberOfAtoms ){
   if( numberOfAtoms != _numberOfAtoms ){
      _atomSizeModified = numberOfAtoms;
   }
   _numberOfAtoms = numberOfAtoms;
   return _numberOfAtoms;
}

/** 
 * Get atom stream width
 * 
 * @param platform  platform
 *
 * @return  atom stream width
 *
 */

int BrookCommon::getAtomStreamWidth( const Platform& platform ){

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

   // static const std::string methodName      = "BrookCommon::getAtomStreamWidth";
   // static const int debug                   = 1;

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

   // get atom stream width

   if( _atomStreamWidth < 0 ){
      _getAtomStreamDimensions( platform );
   }
   return _atomStreamWidth;
}

/** 
 * Get atom stream width
 * 
 * @return  atom stream width
 *
 */

int BrookCommon::getAtomStreamWidth( void ) const {

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

   // static const std::string methodName      = "BrookCommon::getAtomStreamWidth";
   // static const int debug                   = 1;

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

   return _atomStreamWidth;
}

/** 
 * Get atom stream height
 * 
 * @param platform platform
 *
 * @return  atom stream height 
 *
 */

int BrookCommon::getAtomStreamHeight( const Platform& platform ){

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

   // static const std::string methodName      = "BrookCommon::getAtomStreamHeight";
   // static const int debug                   = 1;

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

   // get atom stream height

   if( _atomStreamHeight < 0 ){
      _getAtomStreamDimensions( platform );
   }
   return _atomStreamHeight;
}

/** 
 * Get atom stream height
 * 
 * @return  atom stream height 
 *
 */

int BrookCommon::getAtomStreamHeight( void ) const {

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

   // static const std::string methodName      = "BrookCommon::getAtomStreamHeight";
   // static const int debug                   = 1;

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

   return _atomStreamHeight;
}

/** 
 * Get atom stream size
 * 
 * @param platform  platform
 *
 * @return  atom stream size
 *
 */

int BrookCommon::getAtomStreamSize( const Platform& platform ){

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

   // static const std::string methodName      = "BrookCommon::getAtomStreamSize";
   // static const int debug                   = 1;

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

   // get atom stream size

   if( _atomStreamSize < 0 ){
      _getAtomStreamDimensions( platform );
   }
   return _atomStreamSize;
}

/** 
 * Get atom stream size
 * 
 * @return  atom stream size
 *
 */

int BrookCommon::getAtomStreamSize( void ) const {
   return _atomStreamSize;
}

/** 
 * Get atom stream dimensions
 * 
 * @param platform                  platform
 *
 */

void BrookCommon::_getAtomStreamDimensions( const Platform& platform ){

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

   // static const std::string methodName      = "BrookCommon::_getAtomStreamDimensions";
   // static const int debug                   = 1;

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

   // get atom stream size

   const BrookPlatform brookPlatform            = dynamic_cast<const BrookPlatform&> (platform);
   const BrookStreamFactory& brookStreamFactory = dynamic_cast<const BrookStreamFactory&> (platform.getDefaultStreamFactory() );
Mark Friedrichs's avatar
Mark Friedrichs committed
312
   _atomStreamWidth                             = brookStreamFactory.getDefaultAtomStreamWidth();
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
   _atomStreamSize                              = brookPlatform.getStreamSize( getNumberOfAtoms(), _atomStreamWidth, NULL );
   _atomStreamHeight                            = (int) ( ((float) _atomStreamSize)/( (float) _atomStreamWidth) + 0.001);

   return;
}

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

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

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

int BrookCommon::setLog( FILE* log ){
   _log = log;
   return BrookCommon::DefaultReturnValue;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
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
/** 
 * Get verbosity 
 * 
 * @return   verbosity
 *
 */

int BrookCommon::getVerbosity( void ) const {
   return _verbosity;
}

/** 
 * Set verbosity
 * 
 * @param  verbosity
 *
 * @return  DefaultReturnValue
 *
 */

int BrookCommon::setVerbosity( int verbosity ){
   _verbosity = verbosity;
   return BrookCommon::DefaultReturnValue;
}

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
/* 
 * Get contents of object
 *
 * @param tab         tab
 * @param description description
 * @param value       value
 *
 * @return string containing contents
 *
 * */

std::string BrookCommon::_getLine( const std::string& tab, const std::string& description, const std::string& value ) const {

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

   static const std::string methodName      = "BrookCommon::_getLine";

   static const unsigned int MAX_LINE_CHARS = 256;
   char line[MAX_LINE_CHARS];

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

   std::stringstream message;
   memset( line, ' ', MAX_LINE_CHARS ); 
#ifdef WIN32
   (void) sprintf_s( line, MAX_LINE_CHARS, "%s %-40s %s", tab.c_str(), description.c_str(), value.c_str() );
#else
   (void) sprintf( line, "%s %-40s %s", tab.c_str(), description.c_str(), value.c_str() );
#endif
   message << std::string( line ) << std::endl;

   return message.str();

}
Mark Friedrichs's avatar
Mark Friedrichs committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

/* 
 * Given number of stream elements and width, returns the appropriate
 * height of the stream
 *
 * @param streamSize   stream size 
 * @param width        stream width
 *
 * @return stream height
 *
 */

int BrookCommon::getStreamHeight( int streamSize, int streamWidth ){

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

   //static const std::string methodName      = "BrookCommon::getStreamHeight";

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

	int streamHeight = streamSize/streamWidth;

	if( streamSize % streamWidth ){
      streamHeight++;
   }

	return streamHeight;
}

/* 
 * Given number of stream elements, get stream width & height
 *
 * @param streamSize    stream size 
 * @param streamWidth   output stream width
 * @param streamHeight  output stream height
 *
 * @return stream height
 *
 */

void BrookCommon::getStreamDimensions( int streamSize, int *streamWidth, int *streamHeight ){

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

   //static const std::string methodName      = "BrookCommon::getStreamDimensions";

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

	// There are two conditions - stream should be as square 
	// as possible, but should also be multiple of 16 along
	// one dimension
	
	float s = sqrtf( (float) streamSize );

	// find nearest multiple of 16 to the perfect square size

	int low  = ( (int) floor( s/16.0f ) ) * 16;

	if ( !low ) {
		*streamWidth  = 16;
		*streamHeight = getStreamHeight( streamSize, *streamWidth );
	} else { 

	   int high = low + 1;

		// I'm not sure 48 is such a good stream width. Things seem
		// to be faster with 32 or 64. Can make this a special
		// case later.
		
		// Choose low or high depending on which one is 
		// more square

		int htlow  = getStreamHeight( streamSize, low );
		int hthigh = getStreamHeight( streamSize, high );
		
		if ( abs( htlow - low ) < abs( hthigh - high ) ) {
			*streamWidth   = low;
			*streamHeight  = htlow;
		} else {
			*streamWidth   = high;
			*streamHeight  = hthigh;
		}
	}
	return;
}