kernels.h 22.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_KERNELS_H_
#define OPENMM_KERNELS_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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * 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.                                     *
 * -------------------------------------------------------------------------- */

35
36
37
#include "openmm/AndersenThermostat.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/CMMotionRemover.h"
38
#include "openmm/CustomNonbondedForce.h"
39
40
41
42
43
44
45
46
47
48
49
#include "openmm/GBSAOBCForce.h"
#include "openmm/GBVIForce.h"
#include "openmm/HarmonicAngleForce.h"
#include "openmm/HarmonicBondForce.h"
#include "openmm/KernelImpl.h"
#include "openmm/LangevinIntegrator.h"
#include "openmm/PeriodicTorsionForce.h"
#include "openmm/RBTorsionForce.h"
#include "openmm/NonbondedForce.h"
#include "openmm/Stream.h"
#include "openmm/System.h"
50
51
#include "openmm/VariableLangevinIntegrator.h"
#include "openmm/VariableVerletIntegrator.h"
52
#include "openmm/VerletIntegrator.h"
53
54
55
56
57
58
#include <set>
#include <string>
#include <vector>

namespace OpenMM {

59
/**
60
61
62
 * 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.
63
 */
64
class CalcForcesAndEnergyKernel : public KernelImpl {
65
66
public:
    static std::string Name() {
67
        return "CalcForcesAndEnergyKernel";
68
    }
69
    CalcForcesAndEnergyKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
70
71
72
73
74
75
76
77
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     */
    virtual void initialize(const System& system) = 0;
    /**
78
79
     * This is called at the beginning of each force computation, before calcForces() has been called on
     * any ForceImpl.
80
81
82
     * 
     * @param context    the context in which to execute this kernel
     */
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
    virtual void beginForceComputation(ContextImpl& context) = 0;
    /**
     * This is called at the end of each force computation, after calcForces() has been called on
     * every ForceImpl.
     *
     * @param context    the context in which to execute this kernel
     */
    virtual void finishForceComputation(ContextImpl& context) = 0;
    /**
     * This is called at the beginning of each energy computation, before calcEnergy() has been called on
     * any ForceImpl.
     *
     * @param context    the context in which to execute this kernel
     */
    virtual void beginEnergyComputation(ContextImpl& context) = 0;
    /**
     * This is called at the end of each energy computation, after calcEnergy() has been called on
     * every ForceImpl.
     *
     * @param context    the context in which to execute this kernel
     * @return the potential energy of the system.  This value is added to all values returned by ForceImpls'
     * calcEnergy() methods.  That is, each force kernel may <i>either</i> return its contribution to the
     * energy directly, <i>or</i> add it to an internal buffer so that it will be included here.
     */
    virtual double finishEnergyComputation(ContextImpl& context) = 0;
108
109
};

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * This kernel is invoked to get or set the current time.
 */
class UpdateTimeKernel : public KernelImpl {
public:
    static std::string Name() {
        return "UpdateTime";
    }
    UpdateTimeKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    virtual void initialize(const System& system) = 0;
    /**
     * Get the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     */
131
    virtual double getTime(const ContextImpl& context) const = 0;
132
133
134
135
136
137
    /**
     * Set the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     * @param time       the time
     */
138
    virtual void setTime(ContextImpl& context, double time) = 0;
139
140
};

141
/**
142
 * This kernel is invoked by HarmonicBondForce to calculate the forces acting on the system and the energy of the system.
143
 */
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class CalcHarmonicBondForceKernel : public KernelImpl {
public:
    static std::string Name() {
        return "CalcHarmonicBondForce";
    }
    CalcHarmonicBondForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the HarmonicBondForce this kernel will be used for
     */
    virtual void initialize(const System& system, const HarmonicBondForce& force) = 0;
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
163
    virtual void executeForces(ContextImpl& context) = 0;
164
165
166
167
168
169
    /**
     * 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
     */
170
    virtual double executeEnergy(ContextImpl& context) = 0;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
};

/**
 * This kernel is invoked by HarmonicAngleForce to calculate the forces acting on the system and the energy of the system.
 */
class CalcHarmonicAngleForceKernel : public KernelImpl {
public:
    static std::string Name() {
        return "CalcHarmonicAngleForce";
    }
    CalcHarmonicAngleForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the HarmonicAngleForce this kernel will be used for
     */
    virtual void initialize(const System& system, const HarmonicAngleForce& force) = 0;
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
195
    virtual void executeForces(ContextImpl& context) = 0;
196
197
198
199
200
201
    /**
     * 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
     */
202
    virtual double executeEnergy(ContextImpl& context) = 0;
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
};

/**
 * This kernel is invoked by PeriodicTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CalcPeriodicTorsionForceKernel : public KernelImpl {
public:
    static std::string Name() {
        return "CalcPeriodicTorsionForce";
    }
    CalcPeriodicTorsionForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the PeriodicTorsionForce this kernel will be used for
     */
    virtual void initialize(const System& system, const PeriodicTorsionForce& force) = 0;
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
227
    virtual void executeForces(ContextImpl& context) = 0;
228
229
230
231
232
233
    /**
     * 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
     */
234
    virtual double executeEnergy(ContextImpl& context) = 0;
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
};

/**
 * This kernel is invoked by RBTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CalcRBTorsionForceKernel : public KernelImpl {
public:
    static std::string Name() {
        return "CalcRBTorsionForce";
    }
    CalcRBTorsionForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the RBTorsionForce this kernel will be used for
     */
    virtual void initialize(const System& system, const RBTorsionForce& force) = 0;
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
259
    virtual void executeForces(ContextImpl& context) = 0;
260
261
262
263
264
265
    /**
     * 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
     */
266
    virtual double executeEnergy(ContextImpl& context) = 0;
267
268
269
270
271
272
};

/**
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system and the energy of the system.
 */
class CalcNonbondedForceKernel : public KernelImpl {
273
public:
274
275
276
    enum NonbondedMethod {
        NoCutoff = 0,
        CutoffNonPeriodic = 1,
277
        CutoffPeriodic = 2,
278
279
        Ewald = 3,
        PME = 4
280
    };
281
    static std::string Name() {
282
        return "CalcNonbondedForce";
283
    }
284
    CalcNonbondedForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
285
286
    }
    /**
287
     * Initialize the kernel.
288
     * 
289
     * @param system     the System this kernel will be applied to
290
     * @param force      the NonbondedForce this kernel will be used for
291
     */
292
    virtual void initialize(const System& system, const NonbondedForce& force) = 0;
293
    /**
294
     * Execute the kernel to calculate the forces.
295
     * 
296
     * @param context    the context in which to execute this kernel
297
     */
298
    virtual void executeForces(ContextImpl& context) = 0;
299
    /**
300
     * Execute the kernel to calculate the energy.
301
     * 
302
     * @param context    the context in which to execute this kernel
303
     * @return the potential energy due to the NonbondedForce
304
     */
305
    virtual double executeEnergy(ContextImpl& context) = 0;
306
307
};

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * This kernel is invoked by CustomNonbondedForce to calculate the forces acting on the system and the energy of the system.
 */
class CalcCustomNonbondedForceKernel : public KernelImpl {
public:
    enum NonbondedMethod {
        NoCutoff = 0,
        CutoffNonPeriodic = 1,
        CutoffPeriodic = 2
    };
    static std::string Name() {
        return "CalcCustomNonbondedForce";
    }
    CalcCustomNonbondedForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomNonbondedForce this kernel will be used for
     */
    virtual void initialize(const System& system, const CustomNonbondedForce& force) = 0;
    /**
     * Execute the kernel to calculate the forces.
     *
     * @param context    the context in which to execute this kernel
     */
    virtual void executeForces(ContextImpl& context) = 0;
    /**
     * Execute the kernel to calculate the energy.
     *
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the CustomNonbondedForce
     */
    virtual double executeEnergy(ContextImpl& context) = 0;
};

345
/**
346
 * This kernel is invoked by GBSAOBCForce to calculate the forces acting on the system and the energy of the system.
347
 */
348
class CalcGBSAOBCForceKernel : public KernelImpl {
349
350
351
352
public:
    static std::string Name() {
        return "CalcGBSAOBCForces";
    }
353
    CalcGBSAOBCForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
354
355
    }
    /**
356
     * Initialize the kernel.
357
     * 
358
     * @param system     the System this kernel will be applied to
359
     * @param force      the GBSAOBCForce this kernel will be used for
360
     */
361
    virtual void initialize(const System& system, const GBSAOBCForce& force) = 0;
362
    /**
363
     * Execute the kernel to calculate the forces.
364
     * 
365
     * @param context    the context in which to execute this kernel
366
     */
367
    virtual void executeForces(ContextImpl& context) = 0;
368
    /**
369
     * Execute the kernel to calculate the energy.
370
     * 
371
     * @param context    the context in which to execute this kernel
372
     * @return the potential energy due to the GBSAOBCForce
373
     */
374
    virtual double executeEnergy(ContextImpl& context) = 0;
375
376
};

Mark Friedrichs's avatar
Mark Friedrichs committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
 * This kernel is invoked by GBVIForce to calculate the forces acting on the system and the energy of the system.
 */
class CalcGBVIForceKernel : public KernelImpl {
public:
    static std::string Name() {
        return "CalcGBVIForces";
    }
    CalcGBVIForceKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * 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 scaled radii
     */
    virtual void initialize(const System& system, const GBVIForce& force, const std::vector<double>& scaledRadii) = 0;
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
400
    virtual void executeForces(ContextImpl& context) = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
401
402
403
404
405
406
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the GBVIForce
     */
407
    virtual double executeEnergy(ContextImpl& context) = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
408
409
};

410
411
412
413
414
415
416
417
/**
 * This kernel is invoked by VerletIntegrator to take one time step.
 */
class IntegrateVerletStepKernel : public KernelImpl {
public:
    static std::string Name() {
        return "IntegrateVerletStep";
    }
418
    IntegrateVerletStepKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
419
420
    }
    /**
421
     * Initialize the kernel.
422
     * 
423
424
     * @param system     the System this kernel will be applied to
     * @param integrator the VerletIntegrator this kernel will be used for
425
     */
426
    virtual void initialize(const System& system, const VerletIntegrator& integrator) = 0;
427
428
429
    /**
     * Execute the kernel.
     * 
430
431
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
432
     */
433
    virtual void execute(ContextImpl& context, const VerletIntegrator& integrator) = 0;
434
435
436
437
438
439
440
441
442
443
};

/**
 * This kernel is invoked by LangevinIntegrator to take one time step.
 */
class IntegrateLangevinStepKernel : public KernelImpl {
public:
    static std::string Name() {
        return "IntegrateLangevinStep";
    }
444
    IntegrateLangevinStepKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
445
446
    }
    /**
447
     * Initialize the kernel.
448
     * 
449
450
     * @param system     the System this kernel will be applied to
     * @param integrator the LangevinIntegrator this kernel will be used for
451
     */
452
    virtual void initialize(const System& system, const LangevinIntegrator& integrator) = 0;
453
454
455
    /**
     * Execute the kernel.
     * 
456
457
     * @param context    the context in which to execute this kernel
     * @param integrator the LangevinIntegrator this kernel is being used for
458
     */
459
    virtual void execute(ContextImpl& context, const LangevinIntegrator& integrator) = 0;
460
461
462
463
464
465
466
467
468
469
};

/**
 * This kernel is invoked by BrownianIntegrator to take one time step.
 */
class IntegrateBrownianStepKernel : public KernelImpl {
public:
    static std::string Name() {
        return "IntegrateBrownianStep";
    }
470
    IntegrateBrownianStepKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
471
472
    }
    /**
473
     * Initialize the kernel.
474
     * 
475
476
     * @param system     the System this kernel will be applied to
     * @param integrator the BrownianIntegrator this kernel will be used for
477
     */
478
    virtual void initialize(const System& system, const BrownianIntegrator& integrator) = 0;
479
480
481
    /**
     * Execute the kernel.
     * 
482
483
     * @param context    the context in which to execute this kernel
     * @param integrator the BrownianIntegrator this kernel is being used for
484
     */
485
    virtual void execute(ContextImpl& context, const BrownianIntegrator& integrator) = 0;
486
487
};

488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
 * This kernel is invoked by VariableLangevinIntegrator to take one time step.
 */
class IntegrateVariableLangevinStepKernel : public KernelImpl {
public:
    static std::string Name() {
        return "IntegrateVariableLangevinStep";
    }
    IntegrateVariableLangevinStepKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param integrator the VariableLangevinIntegrator this kernel will be used for
     */
    virtual void initialize(const System& system, const VariableLangevinIntegrator& integrator) = 0;
    /**
     * Execute the kernel.
     *
     * @param context    the context in which to execute this kernel
     * @param integrator the LangevinIntegrator this kernel is being used for
     * @param maxTime    the maximum time beyond which the simulation should not be advanced
     */
512
    virtual void execute(ContextImpl& context, const VariableLangevinIntegrator& integrator, double maxTime) = 0;
513
514
};

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**
 * This kernel is invoked by VariableVerletIntegrator to take one time step.
 */
class IntegrateVariableVerletStepKernel : public KernelImpl {
public:
    static std::string Name() {
        return "IntegrateVariableVerletStep";
    }
    IntegrateVariableVerletStepKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param integrator the VariableVerletIntegrator this kernel will be used for
     */
    virtual void initialize(const System& system, const VariableVerletIntegrator& integrator) = 0;
    /**
     * Execute the kernel.
     *
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
537
     * @param maxTime    the maximum time beyond which the simulation should not be advanced
538
     */
539
    virtual void execute(ContextImpl& context, const VariableVerletIntegrator& integrator, double maxTime) = 0;
540
541
};

542
/**
Peter Eastman's avatar
Peter Eastman committed
543
 * This kernel is invoked by AndersenThermostat at the start of each time step to adjust the particle velocities.
544
545
546
547
548
549
 */
class ApplyAndersenThermostatKernel : public KernelImpl {
public:
    static std::string Name() {
        return "ApplyAndersenThermostat";
    }
550
    ApplyAndersenThermostatKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
551
552
    }
    /**
553
     * Initialize the kernel.
554
     * 
555
556
     * @param system     the System this kernel will be applied to
     * @param thermostat the AndersenThermostat this kernel will be used for
557
     */
558
    virtual void initialize(const System& system, const AndersenThermostat& thermostat) = 0;
559
560
561
    /**
     * Execute the kernel.
     * 
562
     * @param context    the context in which to execute this kernel
563
     */
564
    virtual void execute(ContextImpl& context) = 0;
565
566
567
568
569
570
571
572
573
574
};

/**
 * This kernel is invoked to calculate the kinetic energy of the system.
 */
class CalcKineticEnergyKernel : public KernelImpl {
public:
    static std::string Name() {
        return "CalcKineticEnergy";
    }
575
    CalcKineticEnergyKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
576
577
    }
    /**
578
     * Initialize the kernel.
579
     * 
580
     * @param system     the System this kernel will be applied to
581
     */
582
    virtual void initialize(const System& system) = 0;
583
584
585
    /**
     * Execute the kernel.
     * 
586
     * @param context    the context in which to execute this kernel
587
     */
588
    virtual double execute(ContextImpl& context) = 0;
589
590
};

591
592
593
594
595
596
597
598
599
600
601
/**
 * This kernel is invoked to remove center of mass motion from the system.
 */
class RemoveCMMotionKernel : public KernelImpl {
public:
    static std::string Name() {
        return "RemoveCMMotion";
    }
    RemoveCMMotionKernel(std::string name, const Platform& platform) : KernelImpl(name, platform) {
    }
    /**
602
     * Initialize the kernel.
603
     * 
604
605
     * @param system     the System this kernel will be applied to
     * @param force      the CMMotionRemover this kernel will be used for
606
     */
607
    virtual void initialize(const System& system, const CMMotionRemover& force) = 0;
608
609
610
    /**
     * Execute the kernel.
     * 
611
     * @param context    the context in which to execute this kernel
612
     */
613
    virtual void execute(ContextImpl& context) = 0;
614
615
};

616
617
618
} // namespace OpenMM

#endif /*OPENMM_KERNELS_H_*/