BrookBondParameters.cpp 10.9 KB
Newer Older
Mark Friedrichs's avatar
Mods  
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
Mods  
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
Mods  
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
Mods  
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
Mods  
Mark Friedrichs committed
25
26
27
28
 * -------------------------------------------------------------------------- */

#include <sstream>
#include "BrookBondParameters.h"
29
#include "openmm/OpenMMException.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
30
31
32
33
34
35
36

using namespace OpenMM;
using namespace std;

/** 
 * BrookBondParameters constructor
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
37
 * @param bondName                  bond name
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
38
 * @param numberOfParticlesInBond   no. of particles in each bond
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
39
40
41
42
43
44
45
46
 * @param numberOfParametersInBond  no. of parameters in each bond
 * @param numberOfBonds             no. of bonds
 * @param log                       optional log reference
 *
 */

BrookBondParameters::BrookBondParameters( std::string bondName, int numberOfParticlesInBond,
                                          int numberOfParametersInBond, int numberOfBonds, FILE* log = NULL ) :
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
47

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
48
49
50
51
52
53
54
55
56
57
58
 _bondName( bondName ), _numberOfParticlesInBond( numberOfParticlesInBond ),
 _numberOfParametersInBond( numberOfParametersInBond ), _numberOfBonds( numberOfBonds ), _log( log ){

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

   // static const std::string methodName      = "BrookBondParameters::BrookBondParameters";

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

   // allocate memory for particle indices/parameters

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
59
60
   _bondParameters.resize( numberOfBonds );
   _particleIndices.resize( numberOfBonds );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

}   

/** 
 * BrookBondParameters destructor
 * 
 */

BrookBondParameters::~BrookBondParameters( ){

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

   // static const std::string methodName      = "BrookBondParameters::BrookBondParameters";

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

}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** 
 * Get particle indices
 * 
 * @return particle indices
 *
 */
    
const std::vector<std::vector<int>>& BrookBondParameters::getParticleIndices( void ) const {
   return _particleIndices;
}
    
/** 
 * Get bond parameters
 * 
 * @return parameters
 *
 */
    
const std::vector<std::vector<double>>& BrookBondParameters::getBondParameters( void ) const {
   return _bondParameters;
}
    
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
101
102
103
104
105
106
107
108
109
110
111
/** 
 * Get log file reference
 * 
 * @return  log file reference
 *
 */

FILE* BrookBondParameters::getLog( void ) const {
   return _log;
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
112
113
114
115
116
117
118
119
120
121
122
/** 
 * Get bond name
 * 
 * @return  bond name
 *
 */
    
std::string BrookBondParameters::getBondName( void ) const {
   return _bondName;
}
 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
123
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
/** 
 * Set log file reference
 * 
 * @param  log file reference
 *
 * @return  DefaultReturnValue
 *
 */

int BrookBondParameters::setLog( FILE* log ){
   _log = log;
   return DefaultReturnValue;
}

/** 
 * Get number of bonds
 * 
 * @return  numberOfBonds
 *
 */

int BrookBondParameters::getNumberOfBonds( void ) const {
   return _numberOfBonds;
}

/** 
 * Get number of particles in bond
 * 
 * @return  numberOfParticlesInBond
 *
 */

int BrookBondParameters::getNumberOfParticlesInBond( void ) const {
   return _numberOfParticlesInBond;
}

/** 
 * Get number of parameters in bond
 * 
 * @return numberOfParametersInBond 
 *
 */

int BrookBondParameters::getNumberOfParametersInBond( void ) const {
   return _numberOfParametersInBond;
}

/** 
 * Initialize the kernel, setting up the values of all the force field parameters.
 * 
 * @param bondIndex                 bond index to be set
 * @param particleIndices           indices of particles (dimension=numberOfParticlesInBond)
 * @param bondParameters            bond parameters (dimension=numberOfParametersInBond)
 *
 * @return DefaultReturnValue
 *
 * @throw OpenMMException exeception if bond index is invalid
 *
 */
 
int BrookBondParameters::setBond( int bondIndex, int* particleIndices, double* bondParameters ){ 

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

   static const std::string methodName      = "BrookBondParameters::setBond";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
188
   FILE* log                                = getLog();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

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

   // validate bond index

   if( bondIndex < 0 ){
      std::stringstream message;
      message << methodName << "BondIndex=" << bondIndex << " is < 0.";
      throw OpenMMException( message.str() );
   }

   if( bondIndex >= getNumberOfBonds() ){
      std::stringstream message;
      message << methodName << "BondIndex=" << bondIndex << " is >= " << getNumberOfBonds() << ".";
      throw OpenMMException( message.str() );
   }

   // load'em up

   int numberOfParticlesInBond = getNumberOfParticlesInBond();
   for( int ii = 0; ii < numberOfParticlesInBond; ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
210
      _particleIndices[bondIndex].push_back( particleIndices[ii] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
211
212
213
214
   }

   int numberOfParametersInBond = getNumberOfParametersInBond();
   for( int ii = 0; ii < numberOfParametersInBond; ii++ ){
Mark Friedrichs's avatar
Latest  
Mark Friedrichs committed
215
//fprintf( stderr, "Param %s %d %e\n", getBondName().c_str(), ii, bondParameters[ii] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
216
      _bondParameters[bondIndex].push_back( bondParameters[ii] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
217
218
219
220
221
222
223
224
   }

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

   return DefaultReturnValue;
}

/* 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
225
 * Format line
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
 *
 * @param tab         tab
 * @param description description
 * @param value       value
 *
 * @return string containing contents
 *
 * */

std::string BrookBondParameters::_getLine( const std::string& tab,
                                           const std::string& description,
                                           const std::string& value ) const {

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

   static const std::string methodName      = "BrookStreamInternal::_getLine";
   static const unsigned int MAX_LINE_CHARS = 256;
   char line[MAX_LINE_CHARS];

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

   std::stringstream message;
   memset( line, ' ', MAX_LINE_CHARS );  
249
#ifdef _MSC_VER
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
   (void) sprintf_s( line, MAX_LINE_CHARS, "%s %-40s %s", tab.c_str(), description.c_str(), value.c_str() );
#else
   (void) sprintf( line, "%s %-40s %s", tab.c_str(), description.c_str(), value.c_str() );
#endif
   message << std::string( line ) << std::endl;

   return message.str();

}

/* 
 * Get contents of object
 *
 * @param level   level of dump
 *
 * @return string containing contents
 *
 * */

std::string BrookBondParameters::getContentsString( int level ) const {

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

   static const std::string methodName      = "BrookBondParameters::getContentsString";

   static const unsigned int MAX_LINE_CHARS = 1024;
   char value[MAX_LINE_CHARS];
   static const char* Set                   = "Set";
   static const char* NotSet                = "Not set";

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

   std::stringstream message;
   std::string tab   = "   ";

285
#ifdef _MSC_VER
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
286
287
288
289
290
291
292
#define LOCAL_SPRINTF(a,b,c) sprintf_s( (a), MAX_LINE_CHARS, (b), (c) );   
#define LOCAL_2_SPRINTF(a,b,c,d) sprintf_s( (a), MAX_LINE_CHARS, (b), (c), (d) );   
#else
#define LOCAL_SPRINTF(a,b,c) sprintf( (a), (b), (c) );   
#define LOCAL_2_SPRINTF(a,b,c,d) sprintf( (a), (b), (c), (d) );   
#endif

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
293
   (void) LOCAL_SPRINTF( value, "%s", getBondName().c_str() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
294
295
296
297
298
299
300
301
302
303
304
305
306
   message << _getLine( tab, "Bond name:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getNumberOfBonds() );
   message << _getLine( tab, "Number of bonds:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParticlesInBond() );
   message << _getLine( tab, "Particles/bond:", value ); 

   (void) LOCAL_SPRINTF( value, "%d", getNumberOfParametersInBond() );
   message << _getLine( tab, "Parameters/bond:", value ); 

   message << "Bonds:"  << std::endl; 
   for( int ii = 0; ii < getNumberOfBonds(); ii++ ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
307
308
309
      const static size_t descriptionSz = 1024;
      char description[1024];
      char buffer[1024];
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
310
311
      (void) LOCAL_SPRINTF( description, "%6d [", ii );

312
#ifdef _MSC_VER
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
313

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
314
315
      // particle indices

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
316
317
318
319
320
321
322
323
324
325
326
327
      for( int jj = 0; jj < getNumberOfParticlesInBond(); jj++ ){
         (void) LOCAL_SPRINTF( buffer, "%6d ", _particleIndices[ii][jj] );
         (void) strcat_s( description, descriptionSz, buffer );
      }
      (void) strcat_s( description, descriptionSz, "] [" );

      // parameters

      for( int jj = 0; jj < getNumberOfParametersInBond(); jj++ ){
         (void) LOCAL_SPRINTF( buffer, "%18.10e ", _bondParameters[ii][jj] );
         (void) strcat_s( description, descriptionSz, buffer );
      }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
328

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
329
330
      (void) strcat_s( description, descriptionSz, "]" );
#else
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
331
332
333

      // particle indices

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
334
335
336
337
338
339
340
341
342
343
344
345
346
      for( int jj = 0; jj < getNumberOfParticlesInBond(); jj++ ){
         (void) LOCAL_SPRINTF( buffer, "%6d ", _particleIndices[ii][jj] );
         (void) strcat( description, buffer );
      }
      (void) strcat( description, "] [" );

      // parameters

      for( int jj = 0; jj < getNumberOfParametersInBond(); jj++ ){
         (void) LOCAL_SPRINTF( buffer, "%18.10e ", _bondParameters[ii][jj] );
         (void) strcat( description, buffer );
      }
      (void) strcat( description, "]" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
347
#endif
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
348
349
350
351
352
353
354
355
      message << _getLine( tab, "", description );
   }
 
#undef LOCAL_SPRINTF
#undef LOCAL_2_SPRINTF

   return message.str();
}