BrookRemoveCMMotionKernel.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
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;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
63
64
65
66
67
68
   const BrookPlatform brookPlatform = dynamic_cast<const BrookPlatform&> (platform);
   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
Mark Friedrichs committed
99
   // static const std::string methodName      = "BrookRemoveCMMotionKernel::initialize";
100
101
102

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
103
104
105
106
107
108
109
   _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);
   }

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

   return;

Mark Friedrichs's avatar
Mark Friedrichs committed
115
116
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
117
118
119
120
121
122
123
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
/** 
 * 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;
}

154
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
155
 * Execute kernel
156
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
157
 * @param context    OpenMMContextImpl reference
158
159
160
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
161
void BrookRemoveCMMotionKernel::execute( OpenMMContextImpl& context ){
162
163
164

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

Mark Friedrichs's avatar
Mark Friedrichs committed
165
   // static const std::string methodName      = "BrookRemoveCMMotionKernel::execute";
166
167
168

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
169
170
171
172
173
174
175
176
   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 );
177

Mark Friedrichs's avatar
Mark Friedrichs committed
178
   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
179
}