BrookIntStreamInternal.cpp 6.9 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.                                     *
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
32
#include "BrookIntStreamInternal.h"
33
34
#include "OpenMMException.h"
#include <sstream>
Mark Friedrichs's avatar
Mark Friedrichs committed
35
36
37

using namespace OpenMM;

38
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
39
 * BrookIntStreamInternal constructor
40
41
 * 
 * @param name                      stream name
Mark Friedrichs's avatar
Mark Friedrichs committed
42
43
44
45
 * @param size                      array size
 * @param streamWidth               stream width
 * @param type                      stream type (Integer, Integer2, ...)
 * @param dangleValue               fill value for tail of stream beyond array size
46
47
48
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
49
50
51
52
BrookIntStreamInternal::BrookIntStreamInternal( std::string name, int size, int streamWidth,
                                                BrookStreamInternal::DataType type,
                                                int dangleValue ) : 
                        BrookStreamInternal( name, size, streamWidth, type ){
53
54
55

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

Mark Friedrichs's avatar
Mark Friedrichs committed
56
   static const std::string methodName      = "BrookIntStreamInternal::BrookIntStreamInternal";
57
58
59

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

Mark Friedrichs's avatar
Mark Friedrichs committed
60
61
   _dangleValue = dangleValue;

62
63
   switch( type ){

Mark Friedrichs's avatar
Mark Friedrichs committed
64
      case BrookStreamInternal::Integer:
Mark Friedrichs's avatar
Mark Friedrichs committed
65
66

          _width = 1;
67
68
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
69
      case BrookStreamInternal::Integer2:
Mark Friedrichs's avatar
Mark Friedrichs committed
70
71

          _width = 2;
72
73
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
74
      case BrookStreamInternal::Integer3:
Mark Friedrichs's avatar
Mark Friedrichs committed
75
76

          _width = 3;
77
78
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
79
      case BrookStreamInternal::Integer4:
Mark Friedrichs's avatar
Mark Friedrichs committed
80
81

          _width = 4;
82
83
84
          break;

      default:
Mark Friedrichs's avatar
Mark Friedrichs committed
85

86
87
88
89
90
         std::stringstream message;
         message << methodName << " type=" << type << " not recognized.";
         throw OpenMMException( message.str() );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
91
   _data = new int[size*_width];
92

Mark Friedrichs's avatar
Mark Friedrichs committed
93
94
}

95
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
96
 * BrookIntStreamInternal destructor
97
98
99
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
100
BrookIntStreamInternal::~BrookIntStreamInternal() {
Mark Friedrichs's avatar
Mark Friedrichs committed
101
   delete[] _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
102
103
}

104
105
106
107
108
109
110
/** 
 * Load data from array into stream
 * 
 * @param array                     array to load (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
111
void BrookIntStreamInternal::loadFromArray( const void* array ){
112
113
114

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

Mark Friedrichs's avatar
Mark Friedrichs committed
115
   // static const std::string methodName      = "BrookIntStreamInternal::loadFromArray";
116
117
118
119
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
   return loadFromArray( array, getBaseDataType() );
}

/** 
 * Load data from array into stream
 * 
 * @param array                     array to load (length=size*width)
 *
 */

void BrookIntStreamInternal::loadFromArray( const void* array, BrookStreamInternal::DataType baseType ){

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

   static const std::string methodName      = "BrookIntStreamInternal::loadFromArray(1)";

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

   if( baseType != BrookStreamInternal::Integer ){
      std::stringstream message;
      message << methodName << " stream=" << getName() << " base type=" << getTypeString( baseType ) << " not handled -- add code.";
      throw OpenMMException( message.str() );
   }

144
   int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
145
146
147
148
   int totalSize  = getSize()*getWidth();

   for( int ii = 0; ii < totalSize; ii++ ){
      _data[ii] = arrayData[ii];
149
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
150
151
}

152
153
154
155
156
157
158
/** 
 * Save data from stream to array 
 * 
 * @param array                     array to save data to (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
159
void BrookIntStreamInternal::saveToArray( void* array ){
160
161
162

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

Mark Friedrichs's avatar
Mark Friedrichs committed
163
   // static const std::string methodName      = "BrookIntStreamInternal::saveToArray";
164
165
166
167

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

   int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
168
169
170
   int totalSize  = getSize()*getWidth();
   for( int ii = 0; ii < totalSize; ii++ ){
      arrayData[ii] = _data[ii];
171
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
172
173
}

174
175
176
177
178
179
180
/** 
 * Set all stream entries to input value
 * 
 * @param value                     value to load into stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
181
void BrookIntStreamInternal::fillWithValue( void* value ){
182
183
184

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

Mark Friedrichs's avatar
Mark Friedrichs committed
185
   // static const std::string methodName      = "BrookIntStreamInternal::fillWithValue";
186
187
188
189

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

   int valueData = *((int*) value);
Mark Friedrichs's avatar
Mark Friedrichs committed
190
   int totalSize  = getSize()*getWidth();
191

Mark Friedrichs's avatar
Mark Friedrichs committed
192
193
194
   for( int ii = 0; ii < totalSize; ii++ ){
         _data[ii] = valueData;
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
}

197
198
199
200
201
202
203
/** 
 * Get data
 * 
 * @return data ptr
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
204
205
void* BrookIntStreamInternal::getData( void ){
   return _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
206
207
}