BrookCalcKineticEnergyKernel.cpp 6.32 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
/* -------------------------------------------------------------------------- *
 *                                   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.           *
Mark Friedrichs's avatar
Mark Friedrichs committed
10
 * Authors: Peter Eastman                                                     *
Mark Friedrichs's avatar
Mark Friedrichs committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * 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
33
#include <sstream>
#include "OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
34
#include "BrookCalcKineticEnergyKernel.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
35
#include "BrookStreamImpl.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
36
37

using namespace OpenMM;
Mark Friedrichs's avatar
Mark Friedrichs committed
38
using namespace std;
Mark Friedrichs's avatar
Mark Friedrichs committed
39

40
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
41
 * BrookCalcKineticEnergyKernel constructor
42
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
43
44
45
46
 * @param name                      name of the stream to create
 * @param platform                  platform
 * @param OpenMMBrookInterface      OpenMM-Brook interface
 * @param System                    System reference
47
48
49
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
50
51
BrookCalcKineticEnergyKernel::BrookCalcKineticEnergyKernel( std::string name, const Platform& platform, OpenMMBrookInterface& openMMBrookInterface, System& system ) :
                              CalcKineticEnergyKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
52
53
54

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

Mark Friedrichs's avatar
Mark Friedrichs committed
55
   // static const std::string methodName      = "BrookCalcKineticEnergyKernel::BrookCalcKineticEnergyKernel";
56
57
58

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

Mark Friedrichs's avatar
Mark Friedrichs committed
59
60
   _openMMBrookInterface.setNumberOfParticles( system.getNumParticles() );
   _numberOfParticles  =  system.getNumParticles();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
61
   _masses             = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
62

Mark Friedrichs's avatar
Mark Friedrichs committed
63
64
}

65
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
66
 * BrookCalcKineticEnergyKernel destructor
67
68
 * 
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
  
BrookCalcKineticEnergyKernel::~BrookCalcKineticEnergyKernel( ){
71
72
73

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

Mark Friedrichs's avatar
Mark Friedrichs committed
74
   // static const std::string methodName      = "BrookCalcKineticEnergyKernel::~BrookCalcKineticEnergyKernel";
75
76
77

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

Mark Friedrichs's avatar
Mark Friedrichs committed
78
   delete[] _masses;
Mark Friedrichs's avatar
Mark Friedrichs committed
79
80
}

81
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
82
 * Initialize the kernel
83
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
84
 * @param system  System reference
85
86
87
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
88
void BrookCalcKineticEnergyKernel::initialize( const System& system ){
89
90
91

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

Mark Friedrichs's avatar
Mark Friedrichs committed
92
   // static const std::string methodName      = "BrookCalcKineticEnergyKernel::initialize";
93
94
95

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
96
97
98
   _numberOfParticles  = system.getNumParticles();

   // load masses
Mark Friedrichs's avatar
Mark Friedrichs committed
99
100
101
102
103

   if( _masses ){
      delete[] _masses;
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
104
   _masses = new BrookOpenMMFloat[_numberOfParticles];
Mark Friedrichs's avatar
Mark Friedrichs committed
105

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
106
107
   for( unsigned int ii = 0; ii < (unsigned int) _numberOfParticles; ii++ ){
      _masses[ii]  =  static_cast<BrookOpenMMFloat>(system.getParticleMass(ii));
Mark Friedrichs's avatar
Mark Friedrichs committed
108
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
109
110

   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
111
112
}

113
/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
114
 * Calculate kinetic energy
115
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
116
 * @param context OpenMMContextImpl reference
Mark Friedrichs's avatar
Mark Friedrichs committed
117
118
 *
 * @return kinetic energy of the system
119
120
121
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
122
double BrookCalcKineticEnergyKernel::execute( OpenMMContextImpl& context ){
123
124
125

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

Mark Friedrichs's avatar
Mark Friedrichs committed
126
   static const std::string methodName      = "BrookCalcKineticEnergyKernel::execute";
127
128
129

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

Mark Friedrichs's avatar
Mark Friedrichs committed
130
131
132
133
134
135
   if( _masses == NULL ){
      std::stringstream message;
      message << methodName << " masses not set.";
      throw OpenMMException( message.str() );
   }    

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
   void* dataV                            = _openMMBrookInterface.getParticleVelocities()->getData( 1 );
   float* velocity                        = (float*) dataV;
   double energy                          = 0.0;
   int index                              = 0;

printf( "BrookCalcKineticEnergyKernel:\n" );
double com[3] = { 0.0, 0.0, 0.0 };
for ( int ii = 0; ii < _numberOfParticles; ii++, index += 3 ){
   com[0] += velocity[index];
   com[1] += velocity[index+1];
   com[2] += velocity[index+2];
   printf( "  %d %.3f [%12.5e %12.5e %12.5e]\n", ii, _masses[ii], velocity[index], velocity[index+1], velocity[index+2] );
}
printf( "Com [%12.5e %12.5e %12.5e]\n", com[0], com[1], com[2] );

Mark Friedrichs's avatar
Mark Friedrichs committed
151
index = 0;
152

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
153
   for ( int ii = 0; ii < _numberOfParticles; ii++, index += 3 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
154
155
156
      energy += _masses[ii]*(velocity[index]*velocity[index] + velocity[index + 1]*velocity[index + 1] + velocity[index + 2]*velocity[index + 2]);
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
157
printf( " Ke=%12.5e\n", 0.5*energy );
Mark Friedrichs's avatar
Mark Friedrichs committed
158
   return 0.5*energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
159
}