BrookRemoveCMMotionKernel.cpp 6.15 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
 * -------------------------------------------------------------------------- */

27
#include "openmm/internal/ContextImpl.h"
28
#include "openmm/System.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
29
30
#include "BrookRemoveCMMotionKernel.h"
#include "BrookStreamInternal.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
 * BrookRemoveCMMotionKernel constructor
37
 * 
Mark Friedrichs's avatar
Mark Friedrichs committed
38
39
 * @param name        name of the stream to create
 * @param platform    platform
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
40
41
 * @param openMMBrookInterface      OpenMM-Brook interface
 * @param System                    System reference
42
43
 *
 */
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
44
45
46
  
BrookRemoveCMMotionKernel::BrookRemoveCMMotionKernel( std::string name, const Platform& platform, OpenMMBrookInterface& openMMBrookInterface, System& system ) :
                              RemoveCMMotionKernel( name, platform ), _openMMBrookInterface( openMMBrookInterface ), _system( system ){
47
48
49

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

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

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

54
55
56
   _frequency                           = 0;
   _log                                 = NULL;
   _brookVelocityCenterOfMassRemoval    = NULL;
57

58
   const BrookPlatform& brookPlatform   = dynamic_cast<const BrookPlatform&> (platform);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
59
60
61
62
63
   if( brookPlatform.getLog() != NULL ){
      setLog( brookPlatform.getLog() );
   }   
   _openMMBrookInterface.setNumberOfParticles( system.getNumParticles() );

Mark Friedrichs's avatar
Mark Friedrichs committed
64
65
}

66
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
67
 * BrookRemoveCMMotionKernel destructor
68
69
 * 
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
70
71
  
BrookRemoveCMMotionKernel::~BrookRemoveCMMotionKernel( ){
72
73
74

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

Mark Friedrichs's avatar
Mark Friedrichs committed
75
   // static const std::string methodName      = "BrookRemoveCMMotionKernel::~BrookRemoveCMMotionKernel";
76
77
78

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

79
   delete _brookVelocityCenterOfMassRemoval;
Mark Friedrichs's avatar
Mark Friedrichs committed
80
81
}

82
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
83
 * Initialize the kernel
84
 * 
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
85
86
 * @param system   System reference
 * @param force    CMMotionRemover reference
87
88
89
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
90
void BrookRemoveCMMotionKernel::initialize( const System& system, const CMMotionRemover& force ){
91
92
93

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
94
95
96
   static const std::string methodName      = "BrookRemoveCMMotionKernel::initialize";
   static const int PrintOn                 = 0;
   FILE* log                                = getLog();
97
98
99

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
100
101
102
103
104
105
106
   _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);
   }

107
108
   _brookVelocityCenterOfMassRemoval = new BrookVelocityCenterOfMassRemoval();
   _brookVelocityCenterOfMassRemoval->setup( masses, getPlatform() );
Mark Friedrichs's avatar
Mark Friedrichs committed
109

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
110
111
112
113
114
   if( PrintOn && log ){
      (void) fprintf( log, "%s\n", methodName.c_str() );
      (void) fflush( log );
   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
117
118
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
154
155
/** 
 * 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;
}

156
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
157
 * Execute kernel
158
 * 
159
 * @param context    ContextImpl reference
160
161
162
 *
 */

163
void BrookRemoveCMMotionKernel::execute( ContextImpl& context ){
164
165
166

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
167
   static const std::string methodName      = "BrookRemoveCMMotionKernel::execute";
168
169
170

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
180
   return;
Mark Friedrichs's avatar
Mark Friedrichs committed
181
}