BrookCalcKineticEnergyKernel.cpp 5.84 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
Mark Friedrichs committed
43
44
 * @param name        name of the stream to create
 * @param platform    platform
45
46
47
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
48
49
BrookCalcKineticEnergyKernel::BrookCalcKineticEnergyKernel( std::string name, const Platform& platform ) :
                              CalcKineticEnergyKernel( name, platform ){
50
51
52

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
57
58
   _masses   = NULL;

Mark Friedrichs's avatar
Mark Friedrichs committed
59
60
}

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

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

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

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

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

77
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
78
 * Initialize the kernel
79
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
80
 * @param masses   mass of each particle
81
82
83
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
84
void BrookCalcKineticEnergyKernel::initialize( const vector<double>& masses ){
85
86
87

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
92
93
94
95
96
97
98
99
100
101
102
   // masses

   if( _masses ){
      delete[] _masses;
   }

   _masses = new BrookOpenMMFloat[masses.size()];

   for( unsigned int ii = 0; ii < masses.size(); ii++ ){
      _masses[ii]  = static_cast<BrookOpenMMFloat> (masses[ii]);
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
103
104

   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
105
106
}

107
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
108
 * Execute kernel
109
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
110
 * @param velocities  stream of particle velocities
Mark Friedrichs's avatar
Mark Friedrichs committed
111
112
 *
 * @return kinetic energy of the system
113
114
115
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
116
double BrookCalcKineticEnergyKernel::execute( const Stream& velocities ){
117
118
119

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

Mark Friedrichs's avatar
Mark Friedrichs committed
120
   static const std::string methodName      = "BrookCalcKineticEnergyKernel::execute";
121
122
123

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

Mark Friedrichs's avatar
Mark Friedrichs committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
   const BrookStreamImpl& velocityStreamC = dynamic_cast<const BrookStreamImpl&> (velocities.getImpl());
         BrookStreamImpl& velocityStream  = const_cast<BrookStreamImpl&> (velocityStreamC);
   void* dataV                            = velocityStream.getData( );
   float* velocity                        = (float*) dataV;

   double energy                          = 0.0;
   int index                              = 0;

   if( _masses == NULL ){
      std::stringstream message;
      message << methodName << " masses not set.";
      throw OpenMMException( message.str() );
   }    

Mark Friedrichs's avatar
Mark Friedrichs committed
138
/*
Mark Friedrichs's avatar
Mark Friedrichs committed
139
140
141
142
143
printf( "   BrookCalcKineticEnergyKernel Masses=%12.5e %12.5e", _masses[0], _masses[1] );
printf( " [%12.5e %12.5e %12.5e]", velocity[index], velocity[index+1], velocity[index+2] );
index += 3;
printf( " [%12.5e %12.5e %12.5e]\n", velocity[index], velocity[index+1], velocity[index+2] );
index = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
144
*/
145

Mark Friedrichs's avatar
Mark Friedrichs committed
146
147
148
149
150
   for ( int ii = 0; ii < velocityStream.getSize(); ii++, index += 3 ){
      energy += _masses[ii]*(velocity[index]*velocity[index] + velocity[index + 1]*velocity[index + 1] + velocity[index + 2]*velocity[index + 2]);
   }

   return 0.5*energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
151
}