TestBrookNonbondedForce.cpp 12.6 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.               *
 *                                                                            *
Mark Friedrichs's avatar
Mark Friedrichs committed
9
10
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Peter Eastman, Mark Friedrichs                                    *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
11
12
 * Contributors:                                                              *
 *                                                                            *
Mark Friedrichs's avatar
Mark Friedrichs committed
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
 *                                                                            *
Mark Friedrichs's avatar
Mark Friedrichs committed
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
 *                                                                            *
Mark Friedrichs's avatar
Mark Friedrichs committed
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
29
30
31
32
33
34
 * -------------------------------------------------------------------------- */

/**
 * This tests the Brook harmonic angle bond force/energy
 */

#include <vector>

#include "../../../tests/AssertionUtilities.h"
#include "BrookPlatform.h"
35
36
37
38
39
#include "openmm/OpenMMContext.h"
#include "openmm/NonbondedForce.h"
#include "openmm/HarmonicBondForce.h"
#include "openmm/System.h"
#include "openmm/LangevinIntegrator.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#define PI_M               3.141592653589

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

void testBrookCoulomb( FILE* log ){

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

   static const std::string methodName      = "Coulomb";
   int PrintOn                              = 0; 
   int numberOfParticles                    = 2;
   double mass                              = 2.0;

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

   PrintOn = log ? PrintOn : 0;

   if( PrintOn ){
      (void) fprintf( log, "%s\n", methodName.c_str() ); (void) fflush( log );
   }   

   BrookPlatform platform( 32, "cal", log );
66
67
68
   System system;
   system.addParticle(1.0);
   system.addParticle(1.0);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
69
70
71
72
   LangevinIntegrator integrator( 0, 0.1, 0.01 );

   // int index, double charge, double radius, double depth

73
74
75
   NonbondedForce* forceField = new NonbondedForce(); 
   forceField->addParticle(0.5, 1, 0);
   forceField->addParticle(-1.5, 1, 0);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
   system.addForce(forceField);

   //(void) fprintf( log, "%s: Calling context\n",  methodName.c_str() );
   //(void) fflush( log );

   OpenMMContext context(system, integrator, platform);
   vector<Vec3> positions(numberOfParticles);

   positions[0] = Vec3(0, 0, 0);
   positions[1] = Vec3(2, 0, 0);

   context.setPositions(positions);

   //(void) fprintf( log, "%s :Calling getState\n", methodName.c_str() );
   //(void) fflush( log );

   State state = context.getState( State::Forces | State::Energy );

   const vector<Vec3>& forces = state.getForces();
   if( PrintOn ){
      (void) fprintf( log, "\nCoulomb forces\n");
      for( int ii = 0; ii < numberOfParticles; ii++ ){
         (void) fprintf( log, "%d [%.5e %.5e %.5e]\n", ii, forces[ii][0], forces[ii][1], forces[ii][2] );
      }
      (void) fflush( log );
   }

   double force = 138.935485*(-0.75)/4.0;
   ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
   ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[1], TOL);
   ASSERT_EQUAL_TOL(138.935485*(-0.75)/2.0, state.getPotentialEnergy(), TOL);

   if( PrintOn ){
      (void) fprintf( log, "Coulomb forces ok\n"); fflush( log );
   }

   // delete forceField;

}

void testBrookLJ( FILE* log ){

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

   static const std::string methodName      = "LJ";
   
   int PrintOn                              = 0; 
   int numberOfParticles                    = 2;
   double mass                              = 2.0;

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

   PrintOn = log ? PrintOn : 0;

   if( PrintOn ){
      (void) fprintf( log, "%s\n", methodName.c_str() ); (void) fflush( log );
   }   

   BrookPlatform platform( 32, "cal", log );
   // ReferencePlatform platform;
136
137
138
   System system;
   system.addParticle(1.0);
   system.addParticle(1.0);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
139
140
141
142
   LangevinIntegrator integrator( 0, 0.1, 0.01 );

   // int index, double charge, double radius, double depth

143
144
145
   NonbondedForce* forceField = new NonbondedForce(); 
   forceField->addParticle(0, 1.2, 1);
   forceField->addParticle(0, 1.4, 2);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
   system.addForce(forceField);

   //(void) fprintf( log, "%s: Calling context\n",  methodName.c_str() );
   //(void) fflush( log );

   OpenMMContext context(system, integrator, platform);
   vector<Vec3> positions(numberOfParticles);

   positions[0] = Vec3(0, 0, 0);
   positions[1] = Vec3(2, 0, 0);

   context.setPositions(positions);

   //(void) fprintf( log, "%s :Calling getState\n", methodName.c_str() );
   //(void) fflush( log );

   State state = context.getState( State::Forces | State::Energy );

   const vector<Vec3>& forces = state.getForces();
   if( PrintOn ){
      (void) fprintf( log, "LJ forces\n");
      for( int ii = 0; ii < numberOfParticles; ii++ ){
         (void) fprintf( log, "%d [%.5e %.5e %.5e]\n", ii, forces[ii][0], forces[ii][1], forces[ii][2] );
      }
      (void) fflush( log );
   }

   double x   = 1.3/2.0;
   double eps = sqrt( 2.0 );
   double force = 4.0*eps*(12*std::pow(x, 12.0)-6*std::pow(x, 6.0))/2.0;
   ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
   ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[1], TOL);
   ASSERT_EQUAL_TOL(4.0*eps*(std::pow(x, 12.0)-std::pow(x, 6.0)), state.getPotentialEnergy(), TOL);

   if( PrintOn ){
      (void) fprintf( log, "LJ forces ok\n"); fflush( log );
   }

}

void testBrookExclusionsAnd14( FILE* log ){

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

   static const std::string methodName      = "ExclusionsAnd14";
   int numberOfParticles                    = 5;
   int PrintOn                              = 0; 
   double mass                              = 2.0;

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

   PrintOn = log ? PrintOn : 0;

   if( PrintOn ){
      (void) fprintf( log, "%s\n", methodName.c_str() ); (void) fflush( log );
   }   

   BrookPlatform platform( 32, "cpu", log );
   //ReferencePlatform platform;
205
   System system;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
206
207
208
209
   LangevinIntegrator integrator( 0, 0.1, 0.01 );

   // int index, double charge, double radius, double depth

210
   NonbondedForce* nonbonded = new NonbondedForce();
211
212
   for (int i = 0; i < numberOfParticles; i++) {
       system.addParticle(1.0);
213
       nonbonded->addParticle(0, 1.5, 0);
214
   }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
   vector<pair<int, int> > bonds;
   bonds.push_back(pair<int, int>(0, 1));
   bonds.push_back(pair<int, int>(1, 2));
   bonds.push_back(pair<int, int>(2, 3));
   bonds.push_back(pair<int, int>(3, 4));
   nonbonded->createExceptionsFromBonds(bonds, 0.0, 0.0);
   int first14, second14;
   for (int i = 0; i < nonbonded->getNumExceptions(); i++) {
       int particle1, particle2;
       double chargeProd, sigma, epsilon;
       nonbonded->getExceptionParameters(i, particle1, particle2, chargeProd, sigma, epsilon);
       if ((particle1 == 0 && particle2 == 3) || (particle1 == 3 && particle2 == 0))
           first14 = i;
       if ((particle1 == 1 && particle2 == 4) || (particle1 == 4 && particle2 == 1))
           second14 = i;
   }
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
   system.addForce(nonbonded);

   //(void) fprintf( log, "%s: Calling context\n",  methodName.c_str() );
   //(void) fflush( log );

   OpenMMContext context(system, integrator, platform);
   vector<Vec3> positions(numberOfParticles);

   const double r = 1.0;
   positions[0] = Vec3(0, 0, 0);
   for( int ii = 1; ii < numberOfParticles; ii++ ){
      positions[ii] = Vec3(r, 0, 0);
   }
   for( int ii = 1; ii < numberOfParticles; ii++ ){

      // Test LJ forces

       vector<Vec3> positions(5);
       const double r = 1.0;
       for (int j = 0; j < 5; ++j) {
           nonbonded->setParticleParameters(j, 0, 1.5, 0); 
           positions[j] = Vec3(0, j, 0); 
       }
       nonbonded->setParticleParameters(0, 0, 1.5, 1); 
       nonbonded->setParticleParameters(ii, 0, 1.5, 1); 
256
257
       nonbonded->setExceptionParameters(first14, 0, 3, 0, 1.5, ii == 3 ? 0.5 : 0.0);
       nonbonded->setExceptionParameters(second14, 1, 4, 0, 1.5, 0.0);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
       positions[ii] = Vec3(r, 0, 0); 

      context.reinitialize();
      context.setPositions(positions);

      State state = context.getState( State::Forces | State::Energy );
      const vector<Vec3>& forces = state.getForces();
      double x = 1.5/r;
      double eps = 1.0;
      double force = 4.0*eps*(12*std::pow(x, 12.0)-6*std::pow(x, 6.0))/r;
      double energy = 4.0*eps*(std::pow(x, 12.0)-std::pow(x, 6.0));
      if( ii == 3 ){
         force *= 0.5;
         energy *= 0.5;
      }
      if( ii < 3 ){
         force = 0;
         energy = 0;
      }

      if( PrintOn ){
         (void) fprintf( log, "14 LJ forces ii=%d F=%.6e\n", ii, force );
         for( int jj = 0; jj < numberOfParticles; jj++ ){
            (void) fprintf( log, "%d [%.5e %.5e %.5e]\n", jj, forces[jj][0], forces[jj][1], forces[jj][2] );
         }
         (void) fflush( log );
      }

      ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], TOL);
      ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[ii], TOL);

      if( PrintOn ){
         (void) fprintf( log, "14 LJ forces ok for index=%d\n\n", ii );
         (void) fflush( log );
      }

      ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), TOL);

      // Test Coulomb forces

      nonbonded->setParticleParameters( 0, 2, 1.5, 0 );
      nonbonded->setParticleParameters( ii, 2, 1.5, 0 );
300
301
      nonbonded->setExceptionParameters( first14, 0, 3, ii == 3 ? 4/1.2 : 0, 1.5, 0 );
      nonbonded->setExceptionParameters( second14, 1, 4, 0, 1.5, 0 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
302
303
304
305
306
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

      context.reinitialize();

      context.setPositions(positions);

      state = context.getState( State::Forces | State::Energy );

      const vector<Vec3>& forces2 = state.getForces();

      force = 138.935485*4/(r*r);
      energy = 138.935485*4/r;
      if( ii == 3 ){
         force /= 1.2;
         energy /= 1.2;
      }
      if( ii < 3 ){
          force = 0;
          energy = 0;
      }

      if( PrintOn ){
         (void) fprintf( log, "14 Coulomb forces ii=%d F=%.6e\n", ii, force );
         for( int jj = 0; jj < numberOfParticles; jj++ ){
            (void) fprintf( log, "%d [%.5e %.5e %.5e]\n", jj, forces2[jj][0], forces2[jj][1], forces2[jj][2] );
         }
         (void) fflush( log );
      }

      ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces2[0], TOL);
      ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces2[ii], TOL);

      if( PrintOn ){
         (void) fprintf( log, "14 Coulomb forces ok for index=%d\n\n", ii );
         (void) fflush( log );
      }
       ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), TOL);
   }

   if( PrintOn ){
      (void) fprintf( log, "ExclusionsAnd14 ok\n"); fflush( log );
   }

}

int main( ){

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

   static const std::string methodName      = "testBrookNonBonded";
   FILE* log                                = stdout;

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

   (void) fflush( stdout );
   (void) fflush( stderr );
   try {
      testBrookCoulomb( log );
      testBrookLJ( log );
      testBrookExclusionsAnd14( log );
    } catch( const exception& e ){
      (void) fprintf( log, "Exception %s %.s\n", methodName.c_str(),  e.what() ); (void) fflush( log );
      return 1;
   }   
   (void) fprintf( log, "\n%s done\n", methodName.c_str() ); (void) fflush( log );

   return 0;
}