"platforms/cpu/vscode:/vscode.git/clone" did not exist on "1a1e233e97399c5e3ef42c36eb273f9c9c3e86ff"
CudaKernels.h 35.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_CUDAKERNELS_H_
#define OPENMM_CUDAKERNELS_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.               *
 *                                                                            *
12
 * Portions copyright (c) 2008-2009 Stanford University and the Authors.      *
13
14
15
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
16
17
18
19
 * 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.                                        *
20
 *                                                                            *
21
22
23
24
 * 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.                        *
25
 *                                                                            *
26
27
 * 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/>.      *
28
29
 * -------------------------------------------------------------------------- */

30
#include "CudaPlatform.h"
31
#include "openmm/kernels.h"
32
#include "kernels/gputypes.h"
33
#include "openmm/System.h"
34
35
36
37
38
39
40
41
42

class CudaAndersenThermostat;
class CudaBrownianDynamics;
class CudaStochasticDynamics;
class CudaShakeAlgorithm;
class CudaVerletDynamics;

namespace OpenMM {

43
44
45
// Export internal cudaOpenMMInitializeIntegration() method so it can be used by NML plugin
void OPENMMCUDA_EXPORT cudaOpenMMInitializeIntegration(const System& system, CudaPlatform::PlatformData& data, const Integrator& integrator);

46
/**
47
48
49
 * This kernel is invoked at the beginning and end of force and energy computations.  It gives the
 * Platform a chance to clear buffers and do other initialization at the beginning, and to do any
 * necessary work at the end to determine the final results.
50
 */
51
class CudaCalcForcesAndEnergyKernel : public CalcForcesAndEnergyKernel {
52
public:
53
    CudaCalcForcesAndEnergyKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcForcesAndEnergyKernel(name, platform), data(data) {
54
55
56
57
58
59
60
61
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
62
     * This is called at the beginning of each force/energy computation, before calcForcesAndEnergy() has been called on
63
64
     * any ForceImpl.
     *
65
66
67
     * @param context       the context in which to execute this kernel
     * @param includeForce  true if forces should be computed
     * @param includeEnergy true if potential energy should be computed
68
     * @param groups        a set of bit flags for which force groups to include
69
     */
70
    void beginComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups);
71
    /**
72
     * This is called at the end of each force/energy computation, after calcForcesAndEnergy() has been called on
73
74
     * every ForceImpl.
     *
75
76
77
     * @param context       the context in which to execute this kernel
     * @param includeForce  true if forces should be computed
     * @param includeEnergy true if potential energy should be computed
78
     * @param groups        a set of bit flags for which force groups to include
79
     * @return the potential energy of the system.  This value is added to all values returned by ForceImpls'
80
     * calcForcesAndEnergy() methods.  That is, each force kernel may <i>either</i> return its contribution to the
81
     * energy directly, <i>or</i> add it to an internal buffer so that it will be included here.
82
     */
83
    double finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups);
84
85
private:
    CudaPlatform::PlatformData& data;
86
};
87

88
/**
89
90
 * This kernel provides methods for setting and retrieving various state data: time, positions,
 * velocities, and forces.
91
 */
92
class CudaUpdateStateDataKernel : public UpdateStateDataKernel {
93
public:
94
    CudaUpdateStateDataKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : UpdateStateDataKernel(name, platform), data(data) {
95
96
97
98
99
100
101
102
103
104
105
106
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
     * Get the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     */
107
    double getTime(const ContextImpl& context) const;
108
109
110
111
112
    /**
     * Set the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     */
113
    void setTime(ContextImpl& context, double time);
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
    /**
     * Get the positions of all particles.
     *
     * @param positions  on exit, this contains the particle positions
     */
    void getPositions(ContextImpl& context, std::vector<Vec3>& positions);
    /**
     * Set the positions of all particles.
     *
     * @param positions  a vector containg the particle positions
     */
    void setPositions(ContextImpl& context, const std::vector<Vec3>& positions);
    /**
     * Get the velocities of all particles.
     *
     * @param velocities  on exit, this contains the particle velocities
     */
    void getVelocities(ContextImpl& context, std::vector<Vec3>& velocities);
    /**
     * Set the velocities of all particles.
     *
     * @param velocities  a vector containg the particle velocities
     */
    void setVelocities(ContextImpl& context, const std::vector<Vec3>& velocities);
    /**
     * Get the current forces on all particles.
     *
     * @param forces  on exit, this contains the forces
     */
    void getForces(ContextImpl& context, std::vector<Vec3>& forces);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    /**
     * Get the current periodic box vectors.
     *
     * @param a      on exit, this contains the vector defining the first edge of the periodic box
     * @param b      on exit, this contains the vector defining the second edge of the periodic box
     * @param c      on exit, this contains the vector defining the third edge of the periodic box
     */
    void getPeriodicBoxVectors(ContextImpl& context, Vec3& a, Vec3& b, Vec3& c) const;
    /**
     * Set the current periodic box vectors.
     *
     * @param a      the vector defining the first edge of the periodic box
     * @param b      the vector defining the second edge of the periodic box
     * @param c      the vector defining the third edge of the periodic box
     */
    void setPeriodicBoxVectors(ContextImpl& context, const Vec3& a, const Vec3& b, const Vec3& c) const;
160
161
162
163
private:
    CudaPlatform::PlatformData& data;
};

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * This kernel modifies the positions of particles to enforce distance constraints.
 */
class CudaApplyConstraintsKernel : public ApplyConstraintsKernel {
public:
    CudaApplyConstraintsKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : ApplyConstraintsKernel(name, platform), data(data) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
     * Update particle positions to enforce constraints.
     *
     * @param context    the context in which to execute this kernel
     * @param tol        the distance tolerance within which constraints must be satisfied.
     */
    void apply(ContextImpl& context, double tol);
private:
    CudaPlatform::PlatformData& data;
};

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * This kernel recomputes the positions of virtual sites.
 */
class CudaVirtualSitesKernel : public VirtualSitesKernel {
public:
    CudaVirtualSitesKernel(std::string name, const Platform& platform) : VirtualSitesKernel(name, platform) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
     * Compute the virtual site locations.
     *
     * @param context    the context in which to execute this kernel
     */
    void computePositions(ContextImpl& context);
};

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
 * This kernel is invoked by HarmonicBondForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcHarmonicBondForceKernel : public CalcHarmonicBondForceKernel {
public:
    CudaCalcHarmonicBondForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcHarmonicBondForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcHarmonicBondForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the HarmonicBondForce this kernel will be used for
     */
    void initialize(const System& system, const HarmonicBondForce& force);
    /**
225
226
227
228
229
230
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
231
     */
232
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
233
private:
234
    class ForceInfo;
235
236
237
238
239
    int numBonds;
    CudaPlatform::PlatformData& data;
    System& system;
};

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * This kernel is invoked by CustomBondForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomBondForceKernel : public CalcCustomBondForceKernel {
public:
    CudaCalcCustomBondForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomBondForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomBondForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomBondForce this kernel will be used for
     */
    void initialize(const System& system, const CustomBondForce& force);
    /**
257
     * Execute the kernel to calculate the forces and/or energy.
258
     *
259
260
261
262
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
263
     */
264
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
265
private:
266
    class ForceInfo;
267
268
269
270
271
272
273
274
    void updateGlobalParams(ContextImpl& context);
    int numBonds;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * This kernel is invoked by HarmonicAngleForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcHarmonicAngleForceKernel : public CalcHarmonicAngleForceKernel {
public:
    CudaCalcHarmonicAngleForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcHarmonicAngleForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcHarmonicAngleForceKernel();
    /**
     * 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);
    /**
291
292
293
294
295
296
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
297
     */
298
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
299
private:
300
    class ForceInfo;
301
302
303
304
305
    int numAngles;
    CudaPlatform::PlatformData& data;
    System& system;
};

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
 * This kernel is invoked by CustomAngleForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomAngleForceKernel : public CalcCustomAngleForceKernel {
public:
    CudaCalcCustomAngleForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomAngleForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomAngleForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomAngleForce this kernel will be used for
     */
    void initialize(const System& system, const CustomAngleForce& force);
    /**
323
     * Execute the kernel to calculate the forces and/or energy.
324
     *
325
326
327
328
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
329
     */
330
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
331
private:
332
    class ForceInfo;
333
334
335
336
337
338
339
340
    void updateGlobalParams(ContextImpl& context);
    int numAngles;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 * This kernel is invoked by PeriodicTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcPeriodicTorsionForceKernel : public CalcPeriodicTorsionForceKernel {
public:
    CudaCalcPeriodicTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcPeriodicTorsionForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcPeriodicTorsionForceKernel();
    /**
     * 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);
    /**
357
358
359
360
361
362
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
363
     */
364
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
365
private:
366
    class ForceInfo;
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
    int numTorsions;
    CudaPlatform::PlatformData& data;
    System& system;
};

/**
 * This kernel is invoked by RBTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcRBTorsionForceKernel : public CalcRBTorsionForceKernel {
public:
    CudaCalcRBTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcRBTorsionForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcRBTorsionForceKernel();
    /**
     * 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);
    /**
388
389
390
391
392
393
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
394
     */
395
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
396
private:
397
    class ForceInfo;
398
399
400
401
402
    int numTorsions;
    CudaPlatform::PlatformData& data;
    System& system;
};

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
 * This kernel is invoked by CMAPTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCMAPTorsionForceKernel : public CalcCMAPTorsionForceKernel {
public:
    CudaCalcCMAPTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) :
            CalcCMAPTorsionForceKernel(name, platform), data(data), system(system), coefficients(NULL), mapPositions(NULL),
            torsionIndices(NULL), torsionMaps(NULL) {
    }
    ~CudaCalcCMAPTorsionForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CMAPTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const CMAPTorsionForce& force);
    /**
421
     * Execute the kernel to calculate the forces and/or energy.
422
     *
423
424
425
426
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
427
     */
428
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
429
private:
430
    class ForceInfo;
431
432
433
434
435
436
437
438
439
    CudaPlatform::PlatformData& data;
    System& system;
    int numTorsions;
    CUDAStream<float4>* coefficients;
    CUDAStream<int2>* mapPositions;
    CUDAStream<int4>* torsionIndices;
    CUDAStream<int>* torsionMaps;
};

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
 * This kernel is invoked by CustomTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomTorsionForceKernel : public CalcCustomTorsionForceKernel {
public:
    CudaCalcCustomTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomTorsionForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomTorsionForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const CustomTorsionForce& force);
    /**
457
     * Execute the kernel to calculate the forces and/or energy.
458
     *
459
460
461
462
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
463
     */
464
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
465
private:
466
    class ForceInfo;
467
468
469
470
471
472
473
474
    void updateGlobalParams(ContextImpl& context);
    int numTorsions;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

475
/**
476
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
477
 */
478
class CudaCalcNonbondedForceKernel : public CalcNonbondedForceKernel {
479
public:
480
    CudaCalcNonbondedForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcNonbondedForceKernel(name, platform), data(data), system(system) {
481
    }
482
    ~CudaCalcNonbondedForceKernel();
483
    /**
484
     * Initialize the kernel.
485
     * 
486
     * @param system     the System this kernel will be applied to
487
     * @param force      the NonbondedForce this kernel will be used for
488
     */
489
    void initialize(const System& system, const NonbondedForce& force);
490
    /**
491
492
493
494
495
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
496
     * @param includeReciprocal  true if reciprocal space interactions should be included
497
     * @return the potential energy due to the force
498
     */
499
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy, bool includeDirect, bool includeReciprocal);
500
private:
501
    class ForceInfo;
502
    CudaPlatform::PlatformData& data;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    int numParticles;
    System& system;
};

/**
 * This kernel is invoked by CustomNonbondedForce to calculate the forces acting on the system.
 */
class CudaCalcCustomNonbondedForceKernel : public CalcCustomNonbondedForceKernel {
public:
    CudaCalcCustomNonbondedForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomNonbondedForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcCustomNonbondedForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomNonbondedForce this kernel will be used for
     */
    void initialize(const System& system, const CustomNonbondedForce& force);
    /**
523
     * Execute the kernel to calculate the forces and/or energy.
524
     *
525
526
527
528
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
529
     */
530
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
531
private:
532
    class ForceInfo;
533
    void updateGlobalParams(ContextImpl& context);
534
535
    CudaPlatform::PlatformData& data;
    int numParticles;
536
537
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
538
    System& system;
539
540
};

541
/**
542
 * This kernel is invoked by GBSAOBCForce to calculate the forces acting on the system.
543
 */
544
class CudaCalcGBSAOBCForceKernel : public CalcGBSAOBCForceKernel {
545
public:
546
    CudaCalcGBSAOBCForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcGBSAOBCForceKernel(name, platform), data(data) {
547
    }
548
    ~CudaCalcGBSAOBCForceKernel();
549
    /**
550
     * Initialize the kernel.
551
     * 
552
     * @param system     the System this kernel will be applied to
553
     * @param force      the GBSAOBCForce this kernel will be used for
554
     */
555
    void initialize(const System& system, const GBSAOBCForce& force);
556
    /**
557
558
559
560
561
562
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
563
     */
564
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
Mark Friedrichs's avatar
Mark Friedrichs committed
565
private:
566
    class ForceInfo;
Mark Friedrichs's avatar
Mark Friedrichs committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
    CudaPlatform::PlatformData& data;
};

/**
 * This kernel is invoked by GBVIForce to calculate the forces acting on the system.
 */
class CudaCalcGBVIForceKernel : public CalcGBVIForceKernel {
public:
    CudaCalcGBVIForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcGBVIForceKernel(name, platform), data(data) {
    }
    ~CudaCalcGBVIForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system      the System this kernel will be applied to
     * @param force       the GBVIForce this kernel will be used for
     * @param scaledRadii the scaled radii (Eq. 5 of Labute paper)
     */
    void initialize(const System& system, const GBVIForce& force, const std::vector<double> & scaledRadii);
    /**
587
588
589
590
591
592
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
Mark Friedrichs's avatar
Mark Friedrichs committed
593
     */
594
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
595
private:
596
    class ForceInfo;
597
598
    CudaPlatform::PlatformData& data;
};
599

600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/**
 * This kernel is invoked by CustomExternalForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomExternalForceKernel : public CalcCustomExternalForceKernel {
public:
    CudaCalcCustomExternalForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomExternalForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomExternalForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomExternalForce this kernel will be used for
     */
    void initialize(const System& system, const CustomExternalForce& force);
    /**
617
     * Execute the kernel to calculate the forces and/or energy.
618
     *
619
620
621
622
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
623
     */
624
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
625
private:
626
    class ForceInfo;
627
628
629
630
631
632
633
634
    void updateGlobalParams(ContextImpl& context);
    int numParticles;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/**
 * This kernel is invoked by VerletIntegrator to take one time step.
 */
class CudaIntegrateVerletStepKernel : public IntegrateVerletStepKernel {
public:
    CudaIntegrateVerletStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateVerletStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateVerletStepKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param integrator the VerletIntegrator this kernel will be used for
     */
    void initialize(const System& system, const VerletIntegrator& integrator);
    /**
     * Execute the kernel.
     * 
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
     */
656
    void execute(ContextImpl& context, const VerletIntegrator& integrator);
657
658
659
660
private:
    CudaPlatform::PlatformData& data;
    double prevStepSize;
};
661
662
663
664
665
666

/**
 * This kernel is invoked by LangevinIntegrator to take one time step.
 */
class CudaIntegrateLangevinStepKernel : public IntegrateLangevinStepKernel {
public:
667
    CudaIntegrateLangevinStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateLangevinStepKernel(name, platform), data(data) {
668
669
670
    }
    ~CudaIntegrateLangevinStepKernel();
    /**
Peter Eastman's avatar
Peter Eastman committed
671
     * Initialize the kernel, setting up the particle masses.
672
     * 
673
674
     * @param system     the System this kernel will be applied to
     * @param integrator the LangevinIntegrator this kernel will be used for
675
     */
676
    void initialize(const System& system, const LangevinIntegrator& integrator);
677
678
679
    /**
     * Execute the kernel.
     * 
680
681
     * @param context    the context in which to execute this kernel
     * @param integrator the LangevinIntegrator this kernel is being used for
682
     */
683
    void execute(ContextImpl& context, const LangevinIntegrator& integrator);
684
private:
685
    CudaPlatform::PlatformData& data;
686
687
    double prevTemp, prevFriction, prevStepSize;
};
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

/**
 * This kernel is invoked by BrownianIntegrator to take one time step.
 */
class CudaIntegrateBrownianStepKernel : public IntegrateBrownianStepKernel {
public:
    CudaIntegrateBrownianStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateBrownianStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateBrownianStepKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param integrator the BrownianIntegrator this kernel will be used for
     */
    void initialize(const System& system, const BrownianIntegrator& integrator);
    /**
     * Execute the kernel.
     * 
     * @param context    the context in which to execute this kernel
     * @param integrator the BrownianIntegrator this kernel is being used for
     */
710
    void execute(ContextImpl& context, const BrownianIntegrator& integrator);
711
712
713
714
private:
    CudaPlatform::PlatformData& data;
    double prevTemp, prevFriction, prevStepSize;
};
715

716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/**
 * This kernel is invoked by VariableVerletIntegrator to take one time step.
 */
class CudaIntegrateVariableVerletStepKernel : public IntegrateVariableVerletStepKernel {
public:
    CudaIntegrateVariableVerletStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateVariableVerletStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateVariableVerletStepKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param integrator the VerletIntegrator this kernel will be used for
     */
    void initialize(const System& system, const VariableVerletIntegrator& integrator);
    /**
     * Execute the kernel.
     *
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
736
     * @param maxTime    the maximum time beyond which the simulation should not be advanced
737
     * @return the size of the step that was taken
738
     */
739
    double execute(ContextImpl& context, const VariableVerletIntegrator& integrator, double maxTime);
740
741
742
743
744
private:
    CudaPlatform::PlatformData& data;
    double prevErrorTol;
};

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/**
 * This kernel is invoked by VariableLangevinIntegrator to take one time step.
 */
class CudaIntegrateVariableLangevinStepKernel : public IntegrateVariableLangevinStepKernel {
public:
    CudaIntegrateVariableLangevinStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateVariableLangevinStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateVariableLangevinStepKernel();
    /**
     * Initialize the kernel, setting up the particle masses.
     *
     * @param system     the System this kernel will be applied to
     * @param integrator the VariableLangevinIntegrator this kernel will be used for
     */
    void initialize(const System& system, const VariableLangevinIntegrator& integrator);
    /**
     * Execute the kernel.
     *
     * @param context    the context in which to execute this kernel
     * @param integrator the VariableLangevinIntegrator this kernel is being used for
     * @param maxTime    the maximum time beyond which the simulation should not be advanced
766
     * @return the size of the step that was taken
767
     */
768
    double execute(ContextImpl& context, const VariableLangevinIntegrator& integrator, double maxTime);
769
770
771
772
773
private:
    CudaPlatform::PlatformData& data;
    double prevTemp, prevFriction, prevErrorTol;
};

774
775
776
777
778
/**
 * This kernel is invoked by AndersenThermostat at the start of each time step to adjust the particle velocities.
 */
class CudaApplyAndersenThermostatKernel : public ApplyAndersenThermostatKernel {
public:
779
780
    CudaApplyAndersenThermostatKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : ApplyAndersenThermostatKernel(name, platform),
            data(data), atomGroups(NULL) {
781
782
783
784
785
786
787
788
789
790
791
792
793
794
    }
    ~CudaApplyAndersenThermostatKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param thermostat the AndersenThermostat this kernel will be used for
     */
    void initialize(const System& system, const AndersenThermostat& thermostat);
    /**
     * Execute the kernel.
     * 
     * @param context    the context in which to execute this kernel
     */
795
    void execute(ContextImpl& context);
796
797
798
private:
    CudaPlatform::PlatformData& data;
    double prevTemp, prevFrequency, prevStepSize;
799
    CUDAStream<int>* atomGroups;
800
};
801

802
803
804
805
806
807
/**
 * This kernel is invoked by MonteCarloBarostat to adjust the periodic box volume
 */
class CudaApplyMonteCarloBarostatKernel : public ApplyMonteCarloBarostatKernel {
public:
    CudaApplyMonteCarloBarostatKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : ApplyMonteCarloBarostatKernel(name, platform), data(data),
808
            hasInitializedMolecules(false), moleculeAtoms(NULL), moleculeStartIndex(NULL) {
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
    }
    ~CudaApplyMonteCarloBarostatKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param barostat   the MonteCarloBarostat this kernel will be used for
     */
    void initialize(const System& system, const MonteCarloBarostat& barostat);
    /**
     * Attempt a Monte Carlo step, scaling particle positions (or cluster centers) by a specified value.
     * This is called BEFORE the periodic box size is modified.  It should begin by translating each particle
     * or cluster into the first periodic box, so that coordinates will still be correct after the box size
     * is changed.
     *
     * @param context    the context in which to execute this kernel
     * @param scale      the scale factor by which to multiply particle positions
     */
    void scaleCoordinates(ContextImpl& context, double scale);
    /**
     * Reject the most recent Monte Carlo step, restoring the particle positions to where they were before
     * scaleCoordinates() was last called.
     *
     * @param context    the context in which to execute this kernel
     */
    void restoreCoordinates(ContextImpl& context);
private:
    CudaPlatform::PlatformData& data;
    bool hasInitializedMolecules;
    int numMolecules;
    CUDAStream<int>* moleculeAtoms;
    CUDAStream<int>* moleculeStartIndex;
};

843
844
845
846
847
/**
 * This kernel is invoked to calculate the kinetic energy of the system.
 */
class CudaCalcKineticEnergyKernel : public CalcKineticEnergyKernel {
public:
848
    CudaCalcKineticEnergyKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcKineticEnergyKernel(name, platform), data(data) {
849
850
    }
    /**
851
     * Initialize the kernel.
852
     * 
853
     * @param system     the System this kernel will be applied to
854
     */
855
    void initialize(const System& system);
856
857
858
    /**
     * Execute the kernel.
     * 
859
     * @param context    the context in which to execute this kernel
860
     */
861
    double execute(ContextImpl& context);
862
private:
863
    CudaPlatform::PlatformData& data;
864
865
    std::vector<double> masses;
};
866
867
868
869
870
871
872
873
874

/**
 * This kernel is invoked to remove center of mass motion from the system.
 */
class CudaRemoveCMMotionKernel : public RemoveCMMotionKernel {
public:
    CudaRemoveCMMotionKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : RemoveCMMotionKernel(name, platform), data(data) {
    }
    /**
Peter Eastman's avatar
Peter Eastman committed
875
     * Initialize the kernel, setting up the particle masses.
876
     * 
877
878
     * @param system     the System this kernel will be applied to
     * @param force      the CMMotionRemover this kernel will be used for
879
     */
880
    void initialize(const System& system, const CMMotionRemover& force);
881
882
883
    /**
     * Execute the kernel.
     * 
884
     * @param context    the context in which to execute this kernel
885
     */
886
    void execute(ContextImpl& context);
887
888
889
private:
    CudaPlatform::PlatformData& data;
};
890
891
892
893

} // namespace OpenMM

#endif /*OPENMM_CUDAKERNELS_H_*/