BrookRemoveCMMotionKernel.cpp 6.53 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
Mods  
Mark Friedrichs committed
32
33
#include "internal/OpenMMContextImpl.h"
#include "System.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
34
35
#include "BrookRemoveCMMotionKernel.h"
#include "BrookStreamInternal.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
 * BrookRemoveCMMotionKernel constructor
42
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
43
44
 * @param name        name of the stream to create
 * @param platform    platform
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
45
46
 * @param openMMBrookInterface      OpenMM-Brook interface
 * @param System                    System reference
47
48
 *
 */
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
49
50
51
  
BrookRemoveCMMotionKernel::BrookRemoveCMMotionKernel( std::string name, const Platform& platform, OpenMMBrookInterface& openMMBrookInterface, System& system ) :
                              RemoveCMMotionKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
52
53
54

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
59
60
   _frequency                        = 0;
   _log                              = NULL;
61
62
   _brookVelocityCenterOfMassRemoval = NULL;

63
   const BrookPlatform& brookPlatform = dynamic_cast<const BrookPlatform&> (platform);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
64
65
66
67
68
   if( brookPlatform.getLog() != NULL ){
      setLog( brookPlatform.getLog() );
   }   
   _openMMBrookInterface.setNumberOfParticles( system.getNumParticles() );

Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
}

71
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
72
 * BrookRemoveCMMotionKernel destructor
73
74
 * 
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
75
76
  
BrookRemoveCMMotionKernel::~BrookRemoveCMMotionKernel( ){
77
78
79

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

Mark Friedrichs's avatar
Mark Friedrichs committed
80
   // static const std::string methodName      = "BrookRemoveCMMotionKernel::~BrookRemoveCMMotionKernel";
81
82
83

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

84
   delete _brookVelocityCenterOfMassRemoval;
Mark Friedrichs's avatar
Mark Friedrichs committed
85
86
}

87
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
88
 * Initialize the kernel
89
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
90
91
 * @param system   System reference
 * @param force    CMMotionRemover reference
92
93
94
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
95
void BrookRemoveCMMotionKernel::initialize( const System& system, const CMMotionRemover& force ){
96
97
98

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
99
100
101
   static const std::string methodName      = "BrookRemoveCMMotionKernel::initialize";
   static const int PrintOn                 = 0;
   FILE* log                                = getLog();
102
103
104

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
105
106
107
108
109
110
111
   _frequency = force.getFrequency();
   std::vector<double> masses;
   masses.resize( system.getNumParticles() );
   for( size_t ii = 0; ii < masses.size(); ii++ ){
      masses[ii] = system.getParticleMass(ii);
   }

112
113
   _brookVelocityCenterOfMassRemoval = new BrookVelocityCenterOfMassRemoval();
   _brookVelocityCenterOfMassRemoval->setup( masses, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
114

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
115
116
117
118
119
   if( PrintOn && log ){
      (void) fprintf( log, "%s\n", methodName.c_str() );
      (void) fflush( log );
   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
122
123
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/** 
 * Get log file reference
 * 
 * @return  log file reference
 *
 */

FILE* BrookRemoveCMMotionKernel::getLog( void ) const {
   return _log;
}

/** 
 * Set log file reference
 * 
 * @param  log file reference
 *
 * @return  DefaultReturnValue
 *
 */

int BrookRemoveCMMotionKernel::setLog( FILE* log ){
   _log = log;
   return BrookCommon::DefaultReturnValue;
}


/** 
 * Get COM removal frequency
 * 
 * @return frequency 
 *
 */

int BrookRemoveCMMotionKernel::getFrequency( void ) const {
   return _frequency;
}

161
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
162
 * Execute kernel
163
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
164
 * @param context    OpenMMContextImpl reference
165
166
167
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
168
void BrookRemoveCMMotionKernel::execute( OpenMMContextImpl& context ){
169
170
171

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
172
   static const std::string methodName      = "BrookRemoveCMMotionKernel::execute";
173
174
175

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
176
177
178
179
180
181
182
183
   int step      = (int) floor( context.getTime()/context.getIntegrator().getStepSize() );
   int frequency = getFrequency();
   if( frequency <= 0 || (step % frequency) != 0 ){
        return;
   }

   BrookStreamImpl* velocities = _openMMBrookInterface.getParticleVelocities();
   _brookVelocityCenterOfMassRemoval->removeVelocityCenterOfMass( *velocities );
184

Mark Friedrichs's avatar
Mark Friedrichs committed
185
   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
186
}