BrookIntStreamInternal.cpp 7.16 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
42
43
44
45
46
 * 
 * @param name                      stream name
 * @param size                      stream size
 * @param platform                  platform
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
47
48
BrookIntStreamInternal::BrookIntStreamInternal( std::string name, int size, int width, BrookStreamInternal::DataType type,
                                                int dangleValue ) : BrookStreamInternal( name, size, width, type ){
49
50
51

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

Mark Friedrichs's avatar
Mark Friedrichs committed
52
   static const std::string methodName      = "BrookIntStreamInternal::BrookIntStreamInternal";
53
54
55
56
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
57
58
   _dangleValue = dangleValue;

59
60
   switch( type ){

Mark Friedrichs's avatar
Mark Friedrichs committed
61
      case BrookStreamInternal::Integer:
62
63
64
          width = 1;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
65
      case BrookStreamInternal::Integer2:
66
67
68
          width = 2;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
69
      case BrookStreamInternal::Integer3:
70
71
72
          width = 3;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
73
      case BrookStreamInternal::Integer4:
74
75
76
77
78
79
80
81
82
83
84
85
86
87
          width = 4;
          break;

      default:
         std::stringstream message;
         message << methodName << " type=" << type << " not recognized.";
         throw OpenMMException( message.str() );
   }

   data = new int*[size];

   for( int ii = 0; ii < size; ii++ ){
       data[ii] = new int[width];
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
88
89
}

90
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
91
 * BrookIntStreamInternal destructor
92
93
94
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
95
BrookIntStreamInternal::~BrookIntStreamInternal() {
96
   delete[] data;
Mark Friedrichs's avatar
Mark Friedrichs committed
97
98
}

99
100
101
102
103
104
105
/** 
 * Load data from array into stream
 * 
 * @param array                     array to load (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
106
void BrookIntStreamInternal::loadFromArray( const void* array ){
107
108
109

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

Mark Friedrichs's avatar
Mark Friedrichs committed
110
   // static const std::string methodName      = "BrookIntStreamInternal::loadFromArray";
111
112
113
114
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
   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)";
   // static const int debug                   = 1;

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

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

140
141
142
   int* arrayData = (int*) array;
   int index      = 0;
   for( int ii = 0; ii < getSize(); ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
143
      for( int jj = 0; jj < getWidth(); jj++ ){
144
145
146
         data[ii][jj] = arrayData[index++];
      }
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
147
148
}

149
150
151
152
153
154
155
/** 
 * Save data from stream to array 
 * 
 * @param array                     array to save data to (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
156
void BrookIntStreamInternal::saveToArray( void* array ){
157
158
159

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

Mark Friedrichs's avatar
Mark Friedrichs committed
160
   // static const std::string methodName      = "BrookIntStreamInternal::saveToArray";
161
162
163
164
165
166
167
   // static const int debug                   = 1;

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

   int* arrayData = (int*) array;
   int index      = 0;
   for( int ii = 0; ii < getSize(); ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
168
      for( int jj = 0; jj < getWidth(); jj++ ){
169
170
171
         arrayData[index++] = data[ii][jj];
      }
   }
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
190
191
   // static const int debug                   = 1;

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

   int valueData = *((int*) value);
   for( int ii = 0; ii < getSize(); ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
192
      for (int jj = 0; jj < getWidth(); jj++ ){
193
194
195
         data[ii][jj] = valueData;
      }
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
196
197
}

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

Mark Friedrichs's avatar
Mark Friedrichs committed
205
const int* const * BrookIntStreamInternal::getData( void ) const {
206
   return data;
Mark Friedrichs's avatar
Mark Friedrichs committed
207
208
}

209
210
211
212
213
214
215
/** 
 * Get data
 * 
 * @return data ptr
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
216
int** BrookIntStreamInternal::getData( void ){
217
   return data;
Mark Friedrichs's avatar
Mark Friedrichs committed
218
219
}