BrookCalcKineticEnergyKernel.cpp 7.02 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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
110
/*
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
111
112
113
114
115
116
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
117
*/
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
118

Mark Friedrichs's avatar
Mark Friedrichs committed
119
   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
120
121
}

122
/** 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
123
 * Calculate kinetic energy
124
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
125
 * @param context OpenMMContextImpl reference
Mark Friedrichs's avatar
Mark Friedrichs committed
126
127
 *
 * @return kinetic energy of the system
128
129
130
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
131
double BrookCalcKineticEnergyKernel::execute( OpenMMContextImpl& context ){
132
133
134

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
135
   static const std::string methodName    = "BrookCalcKineticEnergyKernel::execute";
136
137
138

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
139
140
//BrookStreamImpl* velocities = _openMMBrookInterface.getParticleVelocities();
//_brookVelocityCenterOfMassRemoval->removeVelocityCenterOfMass( *velocities );
Mark Friedrichs's avatar
Mark Friedrichs committed
141

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
142
143
144
145
146
   void* dataV                            = _openMMBrookInterface.getParticleVelocities()->getData( 1 );
   float* velocity                        = (float*) dataV;
   double energy                          = 0.0;
   int index                              = 0;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
147
/*
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
148
printf( "BrookCalcKineticEnergyKernel:\n" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
149
float com[3] = { 0.0, 0.0, 0.0 };
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
150
151
152
153
154
155
156
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
157
index = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
158
159
160
161
162
163
164
165
166
167
168
169
float newcom[3] = { 0.0, 0.0, 0.0 };
for ( int ii = 0; ii < _numberOfParticles; ii++, index += 3 ){
   velocity[index]   -= com[0];
   velocity[index+1] -= com[1];
   velocity[index+2] -= com[2];
   newcom[0]         += velocity[index];
   newcom[1]         += velocity[index+1];
   newcom[2]         += velocity[index+2];
}
printf( "NewCom [%12.5e %12.5e %12.5e]\n", newcom[0], newcom[1], newcom[2] );
index = 0;
*/
170

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
171

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
172
   for( int ii = 0; ii < _numberOfParticles; ii++, index += 3 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
173
174
175
      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
176
printf( " KQ=%12.5e\n", 0.5*energy );
Mark Friedrichs's avatar
Mark Friedrichs committed
177
   return 0.5*energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
178
}