BrookStreamFactory.cpp 11.4 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
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors: Peter Eastman, Mark Friedrichs                                    *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

32
#include <sstream>
33
#include "openmm/OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
34
#include "BrookStreamFactory.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
35
#include "BrookStreamImpl.h"
36
#include "openmm/internal/OpenMMContextImpl.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
37
#include "OpenMMBrookInterface.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
38
39
40

using namespace OpenMM;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
41
42
43
44
45
const std::string BrookStreamFactory::ParticlePositions              = "particlePositions";
const std::string BrookStreamFactory::ParticleVelocities             = "particleVelocities";
const std::string BrookStreamFactory::ParticleForces                 = "particleForces";

const double DefaultDangleValue                                      = 1.0e+08;
46
47
48
49
50
51
52
53
54

/** 
 * BrookStreamFactory constructor
 * 
 * @return BrookStreamFactory
 */

BrookStreamFactory::BrookStreamFactory( void ){

Mark Friedrichs's avatar
Mark Friedrichs committed
55
// ---------------------------------------------------------------------------------------
56

Mark Friedrichs's avatar
Mark Friedrichs committed
57
   //static const std::string methodName      = "BrookStreamFactory::BrookStreamFactory";
58

Mark Friedrichs's avatar
Mark Friedrichs committed
59
// ---------------------------------------------------------------------------------------
60

61
	_defaultDangleValue                      = 1.0e+08;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
62
	_defaultParticleStreamWidth              = DefaultStreamParticleWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
63
64
   _defaultStreamRandomNumberWidth          = DefaultStreamRandomNumberWidth;
   _defaultStreamRandomNumberSize           = DefaultStreamRandomNumberSize;
65
66
67
68
69
70
71
72

}

/** 
 * BrookStreamFactory destructor
 * 
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
73
BrookStreamFactory::~BrookStreamFactory( ){
74
75
76
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
77
 * Get particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
78
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
79
 * @return particleStreamWidth
80
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
81
82
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
83
int BrookStreamFactory::getDefaultParticleStreamWidth( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
84
85
86

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
87
   //static const std::string methodName      = "BrookStreamFactory::getDefaultParticleStreamWidth";
Mark Friedrichs's avatar
Mark Friedrichs committed
88
89
90

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
91
   return _defaultParticleStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
94
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
95
 * Set particle stream width
96
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
97
 * @param particleStreamWidth  particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
98
99
100
 *
 * @return DefaultReturnValue
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
101
 * @throw OpenMMException if particleStreamWidth < 1
Mark Friedrichs's avatar
Mark Friedrichs committed
102
 *
103
104
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
105
int BrookStreamFactory::setDefaultParticleStreamWidth( int particleStreamWidth ){
Mark Friedrichs's avatar
Mark Friedrichs committed
106
107
108

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
109
   static const std::string methodName      = "BrookStreamFactory::setDefaultParticleStreamWidth";
110

Mark Friedrichs's avatar
Mark Friedrichs committed
111
112
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
113
   // validate particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
114

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
115
   if( particleStreamWidth < 1 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
116
      std::stringstream message;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
117
      message << methodName << " particleStreamWidth=" << particleStreamWidth << " is less than 1.";
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
120
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
121

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
122
   _defaultParticleStreamWidth = particleStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
123
124
125

   return DefaultReturnValue;

126
127
128
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
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
 * Get randomNumber stream width
 * 
 * @return randomNumberStreamWidth
 *
 */

int BrookStreamFactory::getDefaultRandomNumberStreamWidth( void ) const {

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

   //static const std::string methodName      = "BrookStreamFactory::getDefaultRandomNumberStreamWidth";

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

   return _defaultStreamRandomNumberWidth;
}

/** 
 * Set randomNumber stream width
 * 
 * @param randomNumberStreamWidth  randomNumber stream width
 *
 * @return DefaultReturnValue
 *
 * @throw OpenMMException if randomNumberStreamWidth < 1
 *
 */

int BrookStreamFactory::setDefaultRandomNumberStreamWidth( int randomNumberStreamWidth ){

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

   static const std::string methodName      = "BrookStreamFactory::setDefaultRandomNumberStreamWidth";

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

   // validate randomNumber stream width

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

   _defaultStreamRandomNumberWidth = randomNumberStreamWidth;

   return DefaultReturnValue;

}

/*
 * Get randomNumber stream size
 * 
 * @return randomNumberStreamSize
 *
 */

int BrookStreamFactory::getDefaultRandomNumberStreamSize( void ) const {

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

   //static const std::string methodName      = "BrookStreamFactory::getDefaultRandomNumberStreamSize";

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

   return _defaultStreamRandomNumberSize;
}

/** 
 * Set randomNumber stream size
 * 
 * @param randomNumberStreamSize  randomNumber stream size
 *
 * @return DefaultReturnValue
 *
 * @throw OpenMMException if randomNumberStreamSize < 1
 *
 */

int BrookStreamFactory::setDefaultRandomNumberStreamSize( int randomNumberStreamSize ){

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

   static const std::string methodName      = "BrookStreamFactory::setDefaultRandomNumberStreamSize";

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

   // validate randomNumber stream size

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

   _defaultStreamRandomNumberSize = randomNumberStreamSize;

   return DefaultReturnValue;

}

/** 
 * Get default dangle value
234
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
235
236
 * @return default dangle value
 *
237
238
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
239
240
241
242
243
double BrookStreamFactory::getDefaultDangleValue( void ) const {

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

   //static const std::string methodName      = "BrookStreamFactory::getDefaultDangleValue";
244

Mark Friedrichs's avatar
Mark Friedrichs committed
245
// ---------------------------------------------------------------------------------------
246

Mark Friedrichs's avatar
Mark Friedrichs committed
247
   return _defaultDangleValue;
248
249
250
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
251
 * Set default dangle value
252
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
253
254
255
256
 * @param DefaultDangleValue default dangle value
 *
 * @return DefaultReturnValue
 *
257
258
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
259
260
261
262
263
264
265
266
267
268
269
int BrookStreamFactory::setDefaultDangleValue( double defaultDangleValue ){

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

   //static const std::string methodName      = "BrookStreamFactory::setDefaultDangleValue";

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

   _defaultDangleValue = defaultDangleValue;

   return DefaultReturnValue;
270
271
272
273

}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
274
 * Create StreamInternal
275
276
277
278
279
 *
 * @param name     stream name
 * @param size     stream size
 * @param type     data type (float, float2, ...)
 * @param platform platform reference
Mark Friedrichs's avatar
Mark Friedrichs committed
280
 * @param context  context (currently ignored)
281
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
282
 * @return StreamInternal
283
284
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
285
286
StreamImpl* BrookStreamFactory::createStreamImpl( std::string name, int size, Stream::DataType type,
                                                  const Platform& platform, OpenMMContextImpl& context ) const {
287
288
289

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

Mark Friedrichs's avatar
Mark Friedrichs committed
290
   //static const std::string methodName      = "BrookStreamFactory::createStreamImpl";
291
292
293

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
294
295
   // stream width hould be based on name & value set in platform; for now only particle stream types

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
296
   int streamWidth                             = getDefaultParticleStreamWidth();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
297
298

   BrookStreamImpl* brookStreamImpl            = new BrookStreamImpl( name, size, streamWidth, type, platform );
299

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
300
301
302
303
304
305
306
307
308
   OpenMMBrookInterface& openMMBrookInterface  = *static_cast<OpenMMBrookInterface*>(context.getPlatformData());

   if( name == ParticlePositions ){
      openMMBrookInterface.setParticlePositions( brookStreamImpl );
   } else if( name == ParticleVelocities ){
      openMMBrookInterface.setParticleVelocities( brookStreamImpl );
   } else if( name == ParticleForces ){
      openMMBrookInterface.setParticleForces( brookStreamImpl );
   }
309

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
310
   return brookStreamImpl;
311

Mark Friedrichs's avatar
Mark Friedrichs committed
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

/** 
 * Create StreamInternal
 *
 * @param name     stream name
 * @param size     stream size
 * @param type     data type (float, float2, ...)
 * @param platform platform reference
 * 
 * @return StreamInternal
 */

StreamImpl* BrookStreamFactory::createStreamImpl( std::string name, int size, Stream::DataType type,
                                                  const Platform& platform ) const {

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

   //static const std::string methodName      = "BrookStreamFactory::createStreamImpl";

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

   // stream width hould be based on name & value set in platform; for now only particle stream types

   int streamWidth                             = getDefaultParticleStreamWidth();

   BrookStreamImpl* brookStreamImpl            = new BrookStreamImpl( name, size, streamWidth, type, platform );

   return brookStreamImpl;

}