BrookStreamFactory.cpp 11 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
9
10
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
Mark Friedrichs's avatar
Mark Friedrichs committed
11
12
 * Contributors:                                                              *
 *                                                                            *
13
14
15
16
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
17
 *                                                                            *
18
19
20
21
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
22
 *                                                                            *
23
24
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
Mark Friedrichs's avatar
Mark Friedrichs committed
25
26
 * -------------------------------------------------------------------------- */

27
#include <sstream>
28
#include "openmm/OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
29
#include "BrookStreamFactory.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
30
#include "BrookStreamImpl.h"
31
#include "openmm/internal/OpenMMContextImpl.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
32
#include "OpenMMBrookInterface.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
33
34
35

using namespace OpenMM;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
36
37
38
39
40
const std::string BrookStreamFactory::ParticlePositions              = "particlePositions";
const std::string BrookStreamFactory::ParticleVelocities             = "particleVelocities";
const std::string BrookStreamFactory::ParticleForces                 = "particleForces";

const double DefaultDangleValue                                      = 1.0e+08;
41
42
43
44
45
46
47
48
49

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

BrookStreamFactory::BrookStreamFactory( void ){

Mark Friedrichs's avatar
Mark Friedrichs committed
50
// ---------------------------------------------------------------------------------------
51

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

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

56
	_defaultDangleValue                      = 1.0e+08;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
57
	_defaultParticleStreamWidth              = DefaultStreamParticleWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
58
59
   _defaultStreamRandomNumberWidth          = DefaultStreamRandomNumberWidth;
   _defaultStreamRandomNumberSize           = DefaultStreamRandomNumberSize;
60
61
62
63
64
65
66
67

}

/** 
 * BrookStreamFactory destructor
 * 
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
68
BrookStreamFactory::~BrookStreamFactory( ){
69
70
71
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
72
 * Get particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
73
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
74
 * @return particleStreamWidth
75
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
76
77
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
78
int BrookStreamFactory::getDefaultParticleStreamWidth( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
79
80
81

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
82
   //static const std::string methodName      = "BrookStreamFactory::getDefaultParticleStreamWidth";
Mark Friedrichs's avatar
Mark Friedrichs committed
83
84
85

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
86
   return _defaultParticleStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
87
88
89
}

/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
90
 * Set particle stream width
91
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
92
 * @param particleStreamWidth  particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
93
94
95
 *
 * @return DefaultReturnValue
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
96
 * @throw OpenMMException if particleStreamWidth < 1
Mark Friedrichs's avatar
Mark Friedrichs committed
97
 *
98
99
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
100
int BrookStreamFactory::setDefaultParticleStreamWidth( int particleStreamWidth ){
Mark Friedrichs's avatar
Mark Friedrichs committed
101
102
103

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
106
107
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
108
   // validate particle stream width
Mark Friedrichs's avatar
Mark Friedrichs committed
109

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
110
   if( particleStreamWidth < 1 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
111
      std::stringstream message;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
112
      message << methodName << " particleStreamWidth=" << particleStreamWidth << " is less than 1.";
Mark Friedrichs's avatar
Mark Friedrichs committed
113
114
      throw OpenMMException( message.str() );
      return ErrorReturnValue;
115
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
116

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
117
   _defaultParticleStreamWidth = particleStreamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
120

   return DefaultReturnValue;

121
122
123
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
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
 * 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
229
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
230
231
 * @return default dangle value
 *
232
233
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
234
235
236
237
238
double BrookStreamFactory::getDefaultDangleValue( void ) const {

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
240
// ---------------------------------------------------------------------------------------
241

Mark Friedrichs's avatar
Mark Friedrichs committed
242
   return _defaultDangleValue;
243
244
245
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
246
 * Set default dangle value
247
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
248
249
250
251
 * @param DefaultDangleValue default dangle value
 *
 * @return DefaultReturnValue
 *
252
253
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
254
255
256
257
258
259
260
261
262
263
264
int BrookStreamFactory::setDefaultDangleValue( double defaultDangleValue ){

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

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

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

   _defaultDangleValue = defaultDangleValue;

   return DefaultReturnValue;
265
266
267
268

}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
269
 * Create StreamInternal
270
271
272
273
274
 *
 * @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
275
 * @param context  context (currently ignored)
276
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
277
 * @return StreamInternal
278
279
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
280
281
StreamImpl* BrookStreamFactory::createStreamImpl( std::string name, int size, Stream::DataType type,
                                                  const Platform& platform, OpenMMContextImpl& context ) const {
282
283
284

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

Mark Friedrichs's avatar
Mark Friedrichs committed
285
   //static const std::string methodName      = "BrookStreamFactory::createStreamImpl";
286
287
288

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
289
290
   // 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
291
   int streamWidth                             = getDefaultParticleStreamWidth();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
292
293

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
295
296
297
298
299
300
301
302
303
   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 );
   }
304

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
305
   return brookStreamImpl;
306

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

/** 
 * 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;

}