ReferenceKernels.h 16.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef OPENMM_REFERENCEKERNELS_H_
#define OPENMM_REFERENCEKERNELS_H_

/* -------------------------------------------------------------------------- *
 *                                   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.           *
 * Authors: Peter Eastman                                                     *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#include "kernels.h"
36
#include "SimTKUtilities/SimTKOpenMMRealType.h"
37
#include "SimTKReference/ReferenceNeighborList.h"
38

39
class CpuObc;
40
class ReferenceAndersenThermostat;
41
class ReferenceBrownianDynamics;
42
43
class ReferenceStochasticDynamics;
class ReferenceShakeAlgorithm;
44
class ReferenceVerletDynamics;
45

46
47
48
namespace OpenMM {

/**
49
 * This kernel is invoked by HarmonicBondForce to calculate the forces acting on the system and the energy of the system.
50
 */
51
class ReferenceCalcHarmonicBondForceKernel : public CalcHarmonicBondForceKernel {
52
public:
53
    ReferenceCalcHarmonicBondForceKernel(std::string name, const Platform& platform) : CalcHarmonicBondForceKernel(name, platform) {
54
    }
55
    ~ReferenceCalcHarmonicBondForceKernel();
56
    /**
57
     * Initialize the kernel.
58
     * 
59
     * @param system     the System this kernel will be applied to
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
     * @param force      the HarmonicBondForce this kernel will be used for
     */
    void initialize(const System& system, const HarmonicBondForce& force);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the HarmonicBondForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numBonds;
    int **bondIndexArray;
    RealOpenMM **bondParamArray;
};

/**
 * This kernel is invoked by HarmonicAngleForce to calculate the forces acting on the system and the energy of the system.
 */
class ReferenceCalcHarmonicAngleForceKernel : public CalcHarmonicAngleForceKernel {
public:
    ReferenceCalcHarmonicAngleForceKernel(std::string name, const Platform& platform) : CalcHarmonicAngleForceKernel(name, platform) {
    }
    ~ReferenceCalcHarmonicAngleForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the HarmonicAngleForce this kernel will be used for
     */
    void initialize(const System& system, const HarmonicAngleForce& force);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the HarmonicAngleForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numAngles;
    int **angleIndexArray;
    RealOpenMM **angleParamArray;
};

/**
 * This kernel is invoked by PeriodicTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class ReferenceCalcPeriodicTorsionForceKernel : public CalcPeriodicTorsionForceKernel {
public:
    ReferenceCalcPeriodicTorsionForceKernel(std::string name, const Platform& platform) : CalcPeriodicTorsionForceKernel(name, platform) {
    }
    ~ReferenceCalcPeriodicTorsionForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the PeriodicTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const PeriodicTorsionForce& force);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the PeriodicTorsionForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numTorsions;
    int **torsionIndexArray;
    RealOpenMM **torsionParamArray;
};

/**
 * This kernel is invoked by RBTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class ReferenceCalcRBTorsionForceKernel : public CalcRBTorsionForceKernel {
public:
    ReferenceCalcRBTorsionForceKernel(std::string name, const Platform& platform) : CalcRBTorsionForceKernel(name, platform) {
    }
    ~ReferenceCalcRBTorsionForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the RBTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const RBTorsionForce& force);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the RBTorsionForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numTorsions;
    int **torsionIndexArray;
    RealOpenMM **torsionParamArray;
};

/**
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
 */
class ReferenceCalcNonbondedForceKernel : public CalcNonbondedForceKernel {
public:
    ReferenceCalcNonbondedForceKernel(std::string name, const Platform& platform) : CalcNonbondedForceKernel(name, platform) {
    }
    ~ReferenceCalcNonbondedForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the NonbondedForce this kernel will be used for
Peter Eastman's avatar
Peter Eastman committed
197
     * @param exclusions the i'th element lists the indices of all particles with which the i'th particle should not interact through
198
199
     *                   nonbonded forces.  Bonded 1-4 pairs are also included in this list, since they should be omitted from
     *                   the standard nonbonded calculation.
200
     */
201
    void initialize(const System& system, const NonbondedForce& force, const std::vector<std::set<int> >& exclusions);
202
    /**
203
     * Execute the kernel to calculate the forces.
204
     * 
205
     * @param context    the context in which to execute this kernel
206
     */
207
    void executeForces(OpenMMContextImpl& context);
208
    /**
209
     * Execute the kernel to calculate the energy.
210
     * 
211
     * @param context    the context in which to execute this kernel
212
     * @return the potential energy due to the NonbondedForce
213
     */
214
    double executeEnergy(OpenMMContextImpl& context);
215
private:
Peter Eastman's avatar
Peter Eastman committed
216
    int numParticles, num14;
217
    int **exclusionArray, **bonded14IndexArray;
Peter Eastman's avatar
Peter Eastman committed
218
    RealOpenMM **particleParamArray, **bonded14ParamArray;
219
220
221
222
    RealOpenMM nonbondedCutoff, periodicBoxSize[3];
    std::vector<std::set<int> > exclusions;
    NonbondedMethod nonbondedMethod;
    NeighborList* neighborList;
223
224
225
226
227
};

/**
 * This kernel is invoked by GBSAOBCForceField to calculate the forces acting on the system.
 */
228
class ReferenceCalcGBSAOBCForceFieldKernel : public CalcGBSAOBCForceFieldKernel {
229
public:
230
    ReferenceCalcGBSAOBCForceFieldKernel(std::string name, const Platform& platform) : CalcGBSAOBCForceFieldKernel(name, platform) {
231
    }
232
    ~ReferenceCalcGBSAOBCForceFieldKernel();
233
    /**
234
     * Initialize the kernel.
235
     * 
236
237
     * @param system     the System this kernel will be applied to
     * @param force      the GBSAOBCForceField this kernel will be used for
238
     */
239
    void initialize(const System& system, const GBSAOBCForceField& force);
240
    /**
241
     * Execute the kernel to calculate the forces.
242
     * 
243
     * @param context    the context in which to execute this kernel
244
     */
245
    void executeForces(OpenMMContextImpl& context);
246
    /**
247
     * Execute the kernel to calculate the energy.
248
     * 
249
     * @param context    the context in which to execute this kernel
250
251
     * @return the potential energy due to the GBSAOBCForceField
     */
252
    double executeEnergy(OpenMMContextImpl& context);
253
254
255
private:
    CpuObc* obc;
    std::vector<RealOpenMM> charges;
256
257
258
259
260
261
262
};

/**
 * This kernel is invoked by VerletIntegrator to take one time step.
 */
class ReferenceIntegrateVerletStepKernel : public IntegrateVerletStepKernel {
public:
263
264
    ReferenceIntegrateVerletStepKernel(std::string name, const Platform& platform) : IntegrateVerletStepKernel(name, platform),
        dynamics(0), shake(0), masses(0), shakeParameters(0), constraintIndices(0) {
265
    }
266
    ~ReferenceIntegrateVerletStepKernel();
267
    /**
268
     * Initialize the kernel.
269
     * 
270
271
     * @param system     the System this kernel will be applied to
     * @param integrator the VerletIntegrator this kernel will be used for
272
     */
273
    void initialize(const System& system, const VerletIntegrator& integrator);
274
275
276
    /**
     * Execute the kernel.
     * 
277
278
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
279
     */
280
    void execute(OpenMMContextImpl& context, const VerletIntegrator& integrator);
281
282
283
284
285
286
287
288
private:
    ReferenceVerletDynamics* dynamics;
    ReferenceShakeAlgorithm* shake;
    RealOpenMM* masses;
    RealOpenMM** shakeParameters;
    int** constraintIndices;
    int numConstraints;
    double prevStepSize;
289
290
291
292
293
294
295
};

/**
 * This kernel is invoked by LangevinIntegrator to take one time step.
 */
class ReferenceIntegrateLangevinStepKernel : public IntegrateLangevinStepKernel {
public:
296
297
    ReferenceIntegrateLangevinStepKernel(std::string name, const Platform& platform) : IntegrateLangevinStepKernel(name, platform),
        dynamics(0), shake(0), masses(0), shakeParameters(0), constraintIndices(0) {
298
    }
299
    ~ReferenceIntegrateLangevinStepKernel();
300
    /**
Peter Eastman's avatar
Peter Eastman committed
301
     * Initialize the kernel, setting up the particle masses.
302
     * 
303
304
     * @param system     the System this kernel will be applied to
     * @param integrator the LangevinIntegrator this kernel will be used for
305
     */
306
    void initialize(const System& system, const LangevinIntegrator& integrator);
307
308
309
    /**
     * Execute the kernel.
     * 
310
311
     * @param context    the context in which to execute this kernel
     * @param integrator the LangevinIntegrator this kernel is being used for
312
     */
313
    void execute(OpenMMContextImpl& context, const LangevinIntegrator& integrator);
314
315
316
317
318
319
320
321
private:
    ReferenceStochasticDynamics* dynamics;
    ReferenceShakeAlgorithm* shake;
    RealOpenMM* masses;
    RealOpenMM** shakeParameters;
    int** constraintIndices;
    int numConstraints;
    double prevTemp, prevFriction, prevStepSize;
322
323
324
325
326
327
328
};

/**
 * This kernel is invoked by BrownianIntegrator to take one time step.
 */
class ReferenceIntegrateBrownianStepKernel : public IntegrateBrownianStepKernel {
public:
329
330
    ReferenceIntegrateBrownianStepKernel(std::string name, const Platform& platform) : IntegrateBrownianStepKernel(name, platform),
        dynamics(0), shake(0), masses(0), shakeParameters(0), constraintIndices(0) {
331
    }
332
    ~ReferenceIntegrateBrownianStepKernel();
333
    /**
334
     * Initialize the kernel.
335
     * 
336
337
     * @param system     the System this kernel will be applied to
     * @param integrator the BrownianIntegrator this kernel will be used for
338
     */
339
    void initialize(const System& system, const BrownianIntegrator& integrator);
340
341
342
    /**
     * Execute the kernel.
     * 
343
344
     * @param context    the context in which to execute this kernel
     * @param integrator the BrownianIntegrator this kernel is being used for
345
     */
346
    void execute(OpenMMContextImpl& context, const BrownianIntegrator& integrator);
347
348
349
350
351
352
353
354
private:
    ReferenceBrownianDynamics* dynamics;
    ReferenceShakeAlgorithm* shake;
    RealOpenMM* masses;
    RealOpenMM** shakeParameters;
    int** constraintIndices;
    int numConstraints;
    double prevTemp, prevFriction, prevStepSize;
355
356
357
};

/**
Peter Eastman's avatar
Peter Eastman committed
358
 * This kernel is invoked by AndersenThermostat at the start of each time step to adjust the particle velocities.
359
360
361
 */
class ReferenceApplyAndersenThermostatKernel : public ApplyAndersenThermostatKernel {
public:
362
    ReferenceApplyAndersenThermostatKernel(std::string name, const Platform& platform) : ApplyAndersenThermostatKernel(name, platform), thermostat(0) {
363
    }
364
    ~ReferenceApplyAndersenThermostatKernel();
365
    /**
366
     * Initialize the kernel.
367
     * 
368
369
     * @param system     the System this kernel will be applied to
     * @param thermostat the AndersenThermostat this kernel will be used for
370
     */
371
    void initialize(const System& system, const AndersenThermostat& thermostat);
372
373
374
    /**
     * Execute the kernel.
     * 
375
     * @param context    the context in which to execute this kernel
376
     */
377
    void execute(OpenMMContextImpl& context);
378
379
380
private:
    ReferenceAndersenThermostat* thermostat;
    RealOpenMM* masses;
381
382
383
384
385
386
387
};

/**
 * This kernel is invoked to calculate the kinetic energy of the system.
 */
class ReferenceCalcKineticEnergyKernel : public CalcKineticEnergyKernel {
public:
388
    ReferenceCalcKineticEnergyKernel(std::string name, const Platform& platform) : CalcKineticEnergyKernel(name, platform) {
389
390
    }
    /**
391
     * Initialize the kernel.
392
     * 
393
     * @param system     the System this kernel will be applied to
394
     */
395
    void initialize(const System& system);
396
397
398
    /**
     * Execute the kernel.
     * 
399
     * @param context    the context in which to execute this kernel
400
     */
401
    double execute(OpenMMContextImpl& context);
402
403
private:
    std::vector<double> masses;
404
405
};

406
407
408
409
410
411
412
413
/**
 * This kernel is invoked to remove center of mass motion from the system.
 */
class ReferenceRemoveCMMotionKernel : public RemoveCMMotionKernel {
public:
    ReferenceRemoveCMMotionKernel(std::string name, const Platform& platform) : RemoveCMMotionKernel(name, platform) {
    }
    /**
Peter Eastman's avatar
Peter Eastman committed
414
     * Initialize the kernel, setting up the particle masses.
415
     * 
416
417
     * @param system     the System this kernel will be applied to
     * @param force      the CMMotionRemover this kernel will be used for
418
     */
419
    void initialize(const System& system, const CMMotionRemover& force);
420
421
422
    /**
     * Execute the kernel.
     * 
423
     * @param context    the context in which to execute this kernel
424
     */
425
    void execute(OpenMMContextImpl& context);
426
427
private:
    std::vector<double> masses;
428
    int frequency;
429
430
};

431
432
433
} // namespace OpenMM

#endif /*OPENMM_REFERENCEKERNELS_H_*/