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 "openmm/OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
28
#include "BrookStreamFactory.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
29
#include "BrookStreamImpl.h"
30
#include "openmm/internal/OpenMMContextImpl.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
31
#include "OpenMMBrookInterface.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
32

33
34
#include <sstream>

Mark Friedrichs's avatar
Mark Friedrichs committed
35
36
using namespace OpenMM;

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

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

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

BrookStreamFactory::BrookStreamFactory( void ){

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

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

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

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

}

/** 
 * BrookStreamFactory destructor
 * 
 */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

   return DefaultReturnValue;

122
123
124
}

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

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

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

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

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

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

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

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

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

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

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

   _defaultDangleValue = defaultDangleValue;

   return DefaultReturnValue;
266
267
268
269

}

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

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

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

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

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

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

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

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

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

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

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

}