BrookCalcKineticEnergyKernel.cpp 7.37 KB
Newer Older
Mark Friedrichs's avatar
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
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
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
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
Mark Friedrichs committed
25
26
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
27
#include <sstream>
28
#include "openmm/OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
29
#include "BrookCalcKineticEnergyKernel.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
30
#include "BrookStreamImpl.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
31
32

using namespace OpenMM;
Mark Friedrichs's avatar
Mark Friedrichs committed
33
using namespace std;
Mark Friedrichs's avatar
Mark Friedrichs committed
34

35
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
36
 * BrookCalcKineticEnergyKernel constructor
37
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
38
39
40
41
 * @param name                      name of the stream to create
 * @param platform                  platform
 * @param OpenMMBrookInterface      OpenMM-Brook interface
 * @param System                    System reference
42
43
44
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
45
46
BrookCalcKineticEnergyKernel::BrookCalcKineticEnergyKernel( std::string name, const Platform& platform, OpenMMBrookInterface& openMMBrookInterface, System& system ) :
                              CalcKineticEnergyKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
47
48
49

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

Mark Friedrichs's avatar
Mark Friedrichs committed
50
   // static const std::string methodName      = "BrookCalcKineticEnergyKernel::BrookCalcKineticEnergyKernel";
51
52
53

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

Mark Friedrichs's avatar
Mark Friedrichs committed
54
55
   _openMMBrookInterface.setNumberOfParticles( system.getNumParticles() );
   _numberOfParticles  =  system.getNumParticles();
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
56
   _masses             = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
57

Mark Friedrichs's avatar
Mark Friedrichs committed
58
59
}

60
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
61
 * BrookCalcKineticEnergyKernel destructor
62
63
 * 
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
64
65
  
BrookCalcKineticEnergyKernel::~BrookCalcKineticEnergyKernel( ){
66
67
68

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

Mark Friedrichs's avatar
Mark Friedrichs committed
69
   // static const std::string methodName      = "BrookCalcKineticEnergyKernel::~BrookCalcKineticEnergyKernel";
70
71
72

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

Mark Friedrichs's avatar
Mark Friedrichs committed
73
   delete[] _masses;
Mark Friedrichs's avatar
Mark Friedrichs committed
74
75
}

76
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
77
 * Initialize the kernel
78
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
79
 * @param system  System reference
80
81
82
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
83
void BrookCalcKineticEnergyKernel::initialize( const System& system ){
84
85
86

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

Mark Friedrichs's avatar
Mark Friedrichs committed
87
   // static const std::string methodName      = "BrookCalcKineticEnergyKernel::initialize";
88
89
90

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
91
92
93
   _numberOfParticles  = system.getNumParticles();

   // load masses
Mark Friedrichs's avatar
Mark Friedrichs committed
94
95
96
97
98

   if( _masses ){
      delete[] _masses;
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
99
   _masses = new BrookOpenMMFloat[_numberOfParticles];
Mark Friedrichs's avatar
Mark Friedrichs committed
100

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
101
102
   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
103
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
104

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
105
/*
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
106
107
108
109
110
111
std::vector<double> masses;
for( unsigned int ii = 0; ii < (unsigned int) _numberOfParticles; ii++ ){
   masses.push_back( system.getParticleMass(ii) );
}
_brookVelocityCenterOfMassRemoval = new BrookVelocityCenterOfMassRemoval();
_brookVelocityCenterOfMassRemoval->setup( masses, getPlatform() );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
112
*/
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
113

Mark Friedrichs's avatar
Mark Friedrichs committed
114
   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
115
116
}

117
/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
118
 * Calculate kinetic energy
119
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
120
 * @param context OpenMMContextImpl reference
Mark Friedrichs's avatar
Mark Friedrichs committed
121
122
 *
 * @return kinetic energy of the system
123
124
125
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
126
double BrookCalcKineticEnergyKernel::execute( OpenMMContextImpl& context ){
127
128
129

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
130
   static const std::string methodName    = "BrookCalcKineticEnergyKernel::execute";
131
132
133

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
134
135
//BrookStreamImpl* velocities = _openMMBrookInterface.getParticleVelocities();
//_brookVelocityCenterOfMassRemoval->removeVelocityCenterOfMass( *velocities );
Mark Friedrichs's avatar
Mark Friedrichs committed
136

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
137
138
139
   void* dataV                            = _openMMBrookInterface.getParticleVelocities()->getData( 1 );
   float* velocity                        = (float*) dataV;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
140
if( 0 && _numberOfParticles ){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
141
printf( "BrookCalcKineticEnergyKernel:\n" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
float com[3]      = { 0.0, 0.0, 0.0 };
float localEnergy = 0.0f;
int localIndex    = 0;
float massSum     = 0.0f;
for ( int ii = 0; ii < _numberOfParticles; ii++, localIndex += 3 ){

   com[0]      += _masses[ii]*velocity[localIndex];
   com[1]      += _masses[ii]*velocity[localIndex+1];
   com[2]      += _masses[ii]*velocity[localIndex+2];
   localEnergy += _masses[ii]*(velocity[localIndex]*velocity[localIndex] + velocity[localIndex + 1]*velocity[localIndex + 1] + velocity[localIndex + 2]*velocity[localIndex + 2]);

   massSum  += _masses[ii];

   printf( "  %d %.3f [%12.5e %12.5e %12.5e]\n", ii, _masses[ii], velocity[localIndex], velocity[localIndex+1], velocity[localIndex+2] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
156
}
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
157
158
159
160
161
162
163
float inverseTotalMass = 1.0f/massSum;
com[0] *= inverseTotalMass;
com[1] *= inverseTotalMass;
com[2] *= inverseTotalMass;
printf( "KE raw=%.5e Com [%12.5e %12.5e %12.5e]\n", 0.5f*localEnergy, com[0], com[1], com[2] );


Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
164
float newcom[3] = { 0.0, 0.0, 0.0 };
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
165
166
167
168
169
170
171
172
173
localIndex      = 0;
for ( int ii = 0; ii < _numberOfParticles; ii++, localIndex += 3 ){
   velocity[localIndex]   -= com[0];
   velocity[localIndex+1] -= com[1];
   velocity[localIndex+2] -= com[2];
   newcom[0]              += velocity[localIndex];
   newcom[1]              += velocity[localIndex+1];
   newcom[2]              += velocity[localIndex+2];
   printf( "  %d %.3f [%12.5e %12.5e %12.5e]\n", ii, _masses[ii], velocity[localIndex], velocity[localIndex+1], velocity[localIndex+2] );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
174
175
}
printf( "NewCom [%12.5e %12.5e %12.5e]\n", newcom[0], newcom[1], newcom[2] );
176

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
177
}
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
178

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
179
180
   int index                              = 0;
   double energy                          = 0.0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
181
   for( int ii = 0; ii < _numberOfParticles; ii++, index += 3 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
182
183
184
      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
185
//printf( " KQ=%12.5e\n", 0.5*energy );
Mark Friedrichs's avatar
Mark Friedrichs committed
186
   return 0.5*energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
187
}