AmoebaCudaKernels.cpp 57.1 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
/* -------------------------------------------------------------------------- *
2
 *                               OpenMMAmoeba                                 *
Mark Friedrichs's avatar
Mark Friedrichs committed
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
 * -------------------------------------------------------------------------- *
 * 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-2009 Stanford University and the Authors.      *
 * Authors:                                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * 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.                                        *
 *                                                                            *
 * 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.                        *
 *                                                                            *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

#include "AmoebaCudaKernels.h"
#include "openmm/internal/ContextImpl.h"
#include "kernels/amoebaGpuTypes.h"
#include "kernels/cudaKernels.h"
#include "kernels/amoebaCudaKernels.h"
32
33
#include "openmm/internal/AmoebaMultipoleForceImpl.h"
#include "openmm/internal/AmoebaWcaDispersionForceImpl.h"
34
#include "openmm/internal/AmoebaTorsionTorsionForceImpl.h"
35
#include "openmm/internal/NonbondedForceImpl.h"
36
#include "CudaForceInfo.h"
37

Mark Friedrichs's avatar
Mark Friedrichs committed
38
39
40
41
42
43
44
45
46
47
#include <cmath>
#ifdef _MSC_VER
#include <windows.h>
#endif

extern "C" int gpuSetConstants( gpuContext gpu );

using namespace OpenMM;
using namespace std;

48
49
50
/* -------------------------------------------------------------------------- *
 *                           Calculates bonded forces                         *
 * -------------------------------------------------------------------------- */
Mark Friedrichs's avatar
Mark Friedrichs committed
51
52
53

static void computeAmoebaLocalForces( AmoebaCudaData& data ) {

54
    amoebaGpuContext gpu = data.getAmoebaGpu();
Mark Friedrichs's avatar
Update  
Mark Friedrichs committed
55
56
    if( 0 && data.getLog() ){
        (void) fprintf( data.getLog(), "computeAmoebaLocalForces\n" ); (void) fflush( data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
57
58
59
60
61
62
63
    }

    data.initializeGpu();
    kCalculateAmoebaLocalForces(gpu);

}

64
65
66
67
/* -------------------------------------------------------------------------- *
 *                           AmoebaHarmonicBond                               *
 * -------------------------------------------------------------------------- */

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
class CudaCalcAmoebaHarmonicBondForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaHarmonicBondForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumBonds();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2;
        double length, k;
        force.getBondParameters(index, particle1, particle2, length, k);
        particles.resize(2);
        particles[0] = particle1;
        particles[1] = particle2;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2;
        double length1, length2, k1, k2;
        force.getBondParameters(group1, particle1, particle2, length1, k1);
        force.getBondParameters(group2, particle1, particle2, length2, k2);
        return (length1 == length2 && k1 == k2);
    }
private:
    const AmoebaHarmonicBondForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
CudaCalcAmoebaHarmonicBondForceKernel::CudaCalcAmoebaHarmonicBondForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) : 
                CalcAmoebaHarmonicBondForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaHarmonicBondForceKernel::~CudaCalcAmoebaHarmonicBondForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaHarmonicBondForceKernel::initialize(const System& system, const AmoebaHarmonicBondForce& force) {

    data.setAmoebaLocalForcesKernel( this );

    numBonds = force.getNumBonds();
    std::vector<int>   particle1(numBonds);
    std::vector<int>   particle2(numBonds);
    std::vector<float> length(numBonds);
    std::vector<float> quadratic(numBonds);

    for (int i = 0; i < numBonds; i++) {

        int particle1Index, particle2Index;
116
117
        double lengthValue, kValue;
        force.getBondParameters(i, particle1Index, particle2Index, lengthValue, kValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
120
121
122
123

        particle1[i]     = particle1Index; 
        particle2[i]     = particle2Index; 
        length[i]        = static_cast<float>( lengthValue );
        quadratic[i]     = static_cast<float>( kValue );
    } 
124
125
126
    gpuSetAmoebaBondParameters( data.getAmoebaGpu(), particle1, particle2, length, quadratic, 
                                static_cast<float>(force.getAmoebaGlobalHarmonicBondCubic()),
                                static_cast<float>(force.getAmoebaGlobalHarmonicBondQuartic()) );
127
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
128
129
}

130
double CudaCalcAmoebaHarmonicBondForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
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
197
198
199
200
201
202
203
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

/* -------------------------------------------------------------------------- *
 *                           AmoebaUreyBradley                               *
 * -------------------------------------------------------------------------- */

class CudaCalcAmoebaUreyBradleyForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaUreyBradleyForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumInteractions();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2;
        double length, k;
        force.getUreyBradleyParameters(index, particle1, particle2, length, k);
        particles.resize(2);
        particles[0] = particle1;
        particles[1] = particle2;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2;
        double length1, length2, k1, k2;
        force.getUreyBradleyParameters(group1, particle1, particle2, length1, k1);
        force.getUreyBradleyParameters(group2, particle1, particle2, length2, k2);
        return (length1 == length2 && k1 == k2);
    }
private:
    const AmoebaUreyBradleyForce& force;
};

CudaCalcAmoebaUreyBradleyForceKernel::CudaCalcAmoebaUreyBradleyForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) : 
                CalcAmoebaUreyBradleyForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaUreyBradleyForceKernel::~CudaCalcAmoebaUreyBradleyForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaUreyBradleyForceKernel::initialize(const System& system, const AmoebaUreyBradleyForce& force) {

    data.setAmoebaLocalForcesKernel( this );

    numInteractions = force.getNumInteractions();
    std::vector<int>   particle1(numInteractions);
    std::vector<int>   particle2(numInteractions);
    std::vector<float> length(numInteractions);
    std::vector<float> quadratic(numInteractions);

    for (int i = 0; i < numInteractions; i++) {

        int particle1Index, particle2Index;
        double lengthValue, kValue;
        force.getUreyBradleyParameters(i, particle1Index, particle2Index, lengthValue, kValue );

        particle1[i]     = particle1Index; 
        particle2[i]     = particle2Index; 
        length[i]        = static_cast<float>( lengthValue );
        quadratic[i]     = static_cast<float>( kValue );
    } 
    gpuSetAmoebaUreyBradleyParameters( data.getAmoebaGpu(), particle1, particle2, length, quadratic, 
                                       static_cast<float>(force.getAmoebaGlobalUreyBradleyCubic()),
                                       static_cast<float>(force.getAmoebaGlobalUreyBradleyQuartic()) );
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
}

double CudaCalcAmoebaUreyBradleyForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
204
205
206
207
208
209
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

210
211
212
213
/* -------------------------------------------------------------------------- *
 *                           AmoebaHarmonicAngle                              *
 * -------------------------------------------------------------------------- */

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
class CudaCalcAmoebaHarmonicAngleForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaHarmonicAngleForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumAngles();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3;
        double angle, k;
        force.getAngleParameters(index, particle1, particle2, particle3, angle, k);
        particles.resize(3);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3;
        double angle1, angle2, k1, k2;
        force.getAngleParameters(group1, particle1, particle2, particle3, angle1, k1);
        force.getAngleParameters(group2, particle1, particle2, particle3, angle2, k2);
        return (angle1 == angle2 && k1 == k2);
    }
private:
    const AmoebaHarmonicAngleForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
CudaCalcAmoebaHarmonicAngleForceKernel::CudaCalcAmoebaHarmonicAngleForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
            CalcAmoebaHarmonicAngleForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaHarmonicAngleForceKernel::~CudaCalcAmoebaHarmonicAngleForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaHarmonicAngleForceKernel::initialize(const System& system, const AmoebaHarmonicAngleForce& force) {

    data.setAmoebaLocalForcesKernel( this );

    numAngles                     = force.getNumAngles();
    std::vector<int> particle1(numAngles);
    std::vector<int> particle2(numAngles);
    std::vector<int> particle3(numAngles);
    std::vector<float> angle(numAngles);
    std::vector<float> k(numAngles);

    for (int i = 0; i < numAngles; i++) {
        double angleValue, kQuadratic;
        force.getAngleParameters(i, particle1[i], particle2[i], particle3[i], angleValue, kQuadratic);
        angle[i]            = static_cast<float>( angleValue );
        k[i]                = static_cast<float>( kQuadratic );
    }
    gpuSetAmoebaAngleParameters(data.getAmoebaGpu(), particle1, particle2, particle3, angle, k,
268
269
270
271
                                static_cast<float>(force.getAmoebaGlobalHarmonicAngleCubic()),
                                static_cast<float>(force.getAmoebaGlobalHarmonicAngleQuartic()),
                                static_cast<float>(force.getAmoebaGlobalHarmonicAnglePentic()),
                                static_cast<float>(force.getAmoebaGlobalHarmonicAngleSextic()) );
272
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
273
274
}

275
double CudaCalcAmoebaHarmonicAngleForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
276
277
278
279
280
281
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

282
283
284
285
/* -------------------------------------------------------------------------- *
 *                           AmoebaHarmonicInPlaneAngle                       *
 * -------------------------------------------------------------------------- */

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
class CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaHarmonicInPlaneAngleForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumAngles();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3, particle4;
        double angle, k;
        force.getAngleParameters(index, particle1, particle2, particle3, particle4, angle, k);
        particles.resize(4);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
        particles[3] = particle4;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3, particle4;
        double angle1, angle2, k1, k2;
        force.getAngleParameters(group1, particle1, particle2, particle3, particle4, angle1, k1);
        force.getAngleParameters(group2, particle1, particle2, particle3, particle4, angle2, k2);
        return (angle1 == angle2 && k1 == k2);
    }
private:
    const AmoebaHarmonicInPlaneAngleForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
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
CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::CudaCalcAmoebaHarmonicInPlaneAngleForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) : 
          CalcAmoebaHarmonicInPlaneAngleForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::~CudaCalcAmoebaHarmonicInPlaneAngleForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::initialize(const System& system, const AmoebaHarmonicInPlaneAngleForce& force) {

    data.setAmoebaLocalForcesKernel( this );

    numAngles = force.getNumAngles();

    std::vector<int> particle1(numAngles);
    std::vector<int> particle2(numAngles);
    std::vector<int> particle3(numAngles);
    std::vector<int> particle4(numAngles);
    std::vector<float> angle(numAngles);
    std::vector<float> k(numAngles);

    for (int i = 0; i < numAngles; i++) {
        double angleValue, kQuadratic;
        force.getAngleParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], angleValue, kQuadratic);
        //angle[i]            = static_cast<float>( (angleValue*RadiansToDegrees) );
        angle[i]            = static_cast<float>( angleValue );
        k[i]                = static_cast<float>( kQuadratic );
    }
    gpuSetAmoebaInPlaneAngleParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, angle, k,
344
345
346
347
                                       static_cast<float>( force.getAmoebaGlobalHarmonicInPlaneAngleCubic()),
                                       static_cast<float>( force.getAmoebaGlobalHarmonicInPlaneAngleQuartic()),
                                       static_cast<float>( force.getAmoebaGlobalHarmonicInPlaneAnglePentic()),
                                       static_cast<float>( force.getAmoebaGlobalHarmonicInPlaneAngleSextic() ) );
348
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
349
350
}

351
double CudaCalcAmoebaHarmonicInPlaneAngleForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
352
353
354
355
356
357
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

358
359
360
361
/* -------------------------------------------------------------------------- *
 *                           AmoebaHarmonicTorsion                            *
 * -------------------------------------------------------------------------- */

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
class CudaCalcAmoebaTorsionForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaTorsionForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumTorsions();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3, particle4;
        vector<double> torsion1, torsion2, torsion3;
        force.getTorsionParameters(index, particle1, particle2, particle3, particle4, torsion1, torsion2, torsion3);
        particles.resize(4);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
        particles[3] = particle4;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3, particle4;
        vector<double> torsion11, torsion21, torsion31;
        vector<double> torsion12, torsion22, torsion32;
        force.getTorsionParameters(group1, particle1, particle2, particle3, particle4, torsion11, torsion21, torsion31);
        force.getTorsionParameters(group2, particle1, particle2, particle3, particle4, torsion12, torsion22, torsion32);
        for (int i = 0; i < (int) torsion11.size(); ++i)
            if (torsion11[i] != torsion12[i])
                return false;
        for (int i = 0; i < (int) torsion21.size(); ++i)
            if (torsion21[i] != torsion22[i])
                return false;
        for (int i = 0; i < (int) torsion31.size(); ++i)
            if (torsion31[i] != torsion32[i])
                return false;
        return true;
    }
private:
    const AmoebaTorsionForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
CudaCalcAmoebaTorsionForceKernel::CudaCalcAmoebaTorsionForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
             CalcAmoebaTorsionForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaTorsionForceKernel::~CudaCalcAmoebaTorsionForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaTorsionForceKernel::initialize(const System& system, const AmoebaTorsionForce& force) {

    data.setAmoebaLocalForcesKernel( this );
    numTorsions                     = force.getNumTorsions();
    std::vector<int> particle1(numTorsions);
    std::vector<int> particle2(numTorsions);
    std::vector<int> particle3(numTorsions);
    std::vector<int> particle4(numTorsions);

    std::vector< std::vector<float> > torsionParameters1(numTorsions);
    std::vector< std::vector<float> > torsionParameters2(numTorsions);
    std::vector< std::vector<float> > torsionParameters3(numTorsions);

    for (int i = 0; i < numTorsions; i++) {

        std::vector<double> torsionParameter1;
        std::vector<double> torsionParameter2;
        std::vector<double> torsionParameter3;

        std::vector<float> torsionParameters1F(3);
        std::vector<float> torsionParameters2F(3);
        std::vector<float> torsionParameters3F(3);

        force.getTorsionParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], torsionParameter1, torsionParameter2, torsionParameter3 );
Mark Friedrichs's avatar
Mark Friedrichs committed
433
        for ( unsigned int jj = 0; jj < torsionParameter1.size(); jj++) {
434
435
436
            torsionParameters1F[jj] = static_cast<float>(torsionParameter1[jj]);
            torsionParameters2F[jj] = static_cast<float>(torsionParameter2[jj]);
            torsionParameters3F[jj] = static_cast<float>(torsionParameter3[jj]);
Mark Friedrichs's avatar
Mark Friedrichs committed
437
438
439
440
441
442
        }
        torsionParameters1[i] = torsionParameters1F;
        torsionParameters2[i] = torsionParameters2F;
        torsionParameters3[i] = torsionParameters3F;
    }
    gpuSetAmoebaTorsionParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, torsionParameters1, torsionParameters2, torsionParameters3 );
443
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
444
445
}

446
double CudaCalcAmoebaTorsionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
447
448
449
450
451
452
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

453
454
455
456
/* -------------------------------------------------------------------------- *
 *                           AmoebaHarmonicPiTorsion                          *
 * -------------------------------------------------------------------------- */

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
class CudaCalcAmoebaPiTorsionForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaPiTorsionForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumPiTorsions();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3, particle4, particle5, particle6;
        double k;
        force.getPiTorsionParameters(index, particle1, particle2, particle3, particle4, particle5, particle6, k);
        particles.resize(6);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
        particles[3] = particle4;
        particles[4] = particle5;
        particles[5] = particle6;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3, particle4, particle5, particle6;
        double k1, k2;
        force.getPiTorsionParameters(group1, particle1, particle2, particle3, particle4, particle5, particle6, k1);
        force.getPiTorsionParameters(group2, particle1, particle2, particle3, particle4, particle5, particle6, k2);
        return (k1 == k2);
    }
private:
    const AmoebaPiTorsionForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
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
512
513
514
CudaCalcAmoebaPiTorsionForceKernel::CudaCalcAmoebaPiTorsionForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
         CalcAmoebaPiTorsionForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaPiTorsionForceKernel::~CudaCalcAmoebaPiTorsionForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaPiTorsionForceKernel::initialize(const System& system, const AmoebaPiTorsionForce& force) {

    data.setAmoebaLocalForcesKernel( this );
    numPiTorsions                     = force.getNumPiTorsions();

    std::vector<int> particle1(numPiTorsions);
    std::vector<int> particle2(numPiTorsions);
    std::vector<int> particle3(numPiTorsions);
    std::vector<int> particle4(numPiTorsions);
    std::vector<int> particle5(numPiTorsions);
    std::vector<int> particle6(numPiTorsions);

    std::vector<float> torsionKParameters(numPiTorsions);

    for (int i = 0; i < numPiTorsions; i++) {

        double torsionKParameter;

        force.getPiTorsionParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], particle5[i], particle6[i], torsionKParameter);
515
        torsionKParameters[i] = static_cast<float>(torsionKParameter);
Mark Friedrichs's avatar
Mark Friedrichs committed
516
517
    }
    gpuSetAmoebaPiTorsionParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, particle5, particle6, torsionKParameters);
518
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
519
520
}

521
double CudaCalcAmoebaPiTorsionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
522
523
524
525
526
527
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

528
529
530
531
/* -------------------------------------------------------------------------- *
 *                           AmoebaStretchBend                                *
 * -------------------------------------------------------------------------- */

532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
class CudaCalcAmoebaStretchBendForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaStretchBendForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumStretchBends();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3;
        double lengthAB, lengthCB, angle, k;
        force.getStretchBendParameters(index, particle1, particle2, particle3, lengthAB, lengthCB, angle, k);
        particles.resize(3);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3;
        double lengthAB1, lengthAB2, lengthCB1, lengthCB2, angle1, angle2, k1, k2;
        force.getStretchBendParameters(group1, particle1, particle2, particle3, lengthAB1, lengthCB1, angle1, k1);
        force.getStretchBendParameters(group2, particle1, particle2, particle3, lengthAB2, lengthCB2, angle2, k2);
        return (lengthAB1 == lengthAB2 && lengthCB1 == lengthCB2 && angle1 == angle2 && k1 == k2);
    }
private:
    const AmoebaStretchBendForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
CudaCalcAmoebaStretchBendForceKernel::CudaCalcAmoebaStretchBendForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
                   CalcAmoebaStretchBendForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaStretchBendForceKernel::~CudaCalcAmoebaStretchBendForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaStretchBendForceKernel::initialize(const System& system, const AmoebaStretchBendForce& force) {

    data.setAmoebaLocalForcesKernel( this );
    numStretchBends                     = force.getNumStretchBends();

    std::vector<int>   particle1(numStretchBends);
    std::vector<int>   particle2(numStretchBends);
    std::vector<int>   particle3(numStretchBends);
    std::vector<float> lengthABParameters(numStretchBends);
    std::vector<float> lengthCBParameters(numStretchBends);
    std::vector<float> angleParameters(numStretchBends);
    std::vector<float> kParameters(numStretchBends);

    for (int i = 0; i < numStretchBends; i++) {

        double lengthAB, lengthCB, angle, k;

        force.getStretchBendParameters(i, particle1[i], particle2[i], particle3[i], lengthAB, lengthCB, angle, k);
586
587
588
589
        lengthABParameters[i] = static_cast<float>(lengthAB);
        lengthCBParameters[i] = static_cast<float>(lengthCB);
        angleParameters[i]    = static_cast<float>(angle);
        kParameters[i]        = static_cast<float>(k);
Mark Friedrichs's avatar
Mark Friedrichs committed
590
591
    }
    gpuSetAmoebaStretchBendParameters(data.getAmoebaGpu(), particle1, particle2, particle3, lengthABParameters, lengthCBParameters, angleParameters, kParameters);
592
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
593
594
}

595
double CudaCalcAmoebaStretchBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
596
597
598
599
600
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}
601

602
603
604
605
/* -------------------------------------------------------------------------- *
 *                           AmoebaOutOfPlaneBend                             *
 * -------------------------------------------------------------------------- */

606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
class CudaCalcAmoebaOutOfPlaneBendForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaOutOfPlaneBendForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumOutOfPlaneBends();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3, particle4;
        double k;
        force.getOutOfPlaneBendParameters(index, particle1, particle2, particle3, particle4, k);
        particles.resize(4);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
        particles[3] = particle4;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3, particle4;
        double k1, k2;
        force.getOutOfPlaneBendParameters(group1, particle1, particle2, particle3, particle4, k1);
        force.getOutOfPlaneBendParameters(group2, particle1, particle2, particle3, particle4, k2);
        return (k1 == k2);
    }
private:
    const AmoebaOutOfPlaneBendForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
CudaCalcAmoebaOutOfPlaneBendForceKernel::CudaCalcAmoebaOutOfPlaneBendForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
          CalcAmoebaOutOfPlaneBendForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaOutOfPlaneBendForceKernel::~CudaCalcAmoebaOutOfPlaneBendForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaOutOfPlaneBendForceKernel::initialize(const System& system, const AmoebaOutOfPlaneBendForce& force) {

    data.setAmoebaLocalForcesKernel( this );
    numOutOfPlaneBends                     = force.getNumOutOfPlaneBends();

    std::vector<int>   particle1(numOutOfPlaneBends);
    std::vector<int>   particle2(numOutOfPlaneBends);
    std::vector<int>   particle3(numOutOfPlaneBends);
    std::vector<int>   particle4(numOutOfPlaneBends);
    std::vector<float> kParameters(numOutOfPlaneBends);

    for (int i = 0; i < numOutOfPlaneBends; i++) {

        double k;

        force.getOutOfPlaneBendParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], k);
659
        kParameters[i] = static_cast<float>(k);
Mark Friedrichs's avatar
Mark Friedrichs committed
660
661
    }
    gpuSetAmoebaOutOfPlaneBendParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, kParameters,
662
663
664
665
                                         static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendCubic()),
                                         static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendQuartic()),
                                         static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendPentic()),
                                         static_cast<float>( force.getAmoebaGlobalOutOfPlaneBendSextic() ) );
666
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
667
668
}

669
double CudaCalcAmoebaOutOfPlaneBendForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
670
671
672
673
674
675
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

676
677
678
679
/* -------------------------------------------------------------------------- *
 *                           AmoebaTorsionTorsion                             *
 * -------------------------------------------------------------------------- */

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
class CudaCalcAmoebaTorsionTorsionForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaTorsionTorsionForce& force) : force(force) {
    }
    int getNumParticleGroups() {
        return force.getNumTorsionTorsions();
    }
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndex;
        force.getTorsionTorsionParameters(index, particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndex);
        particles.resize(5);
        particles[0] = particle1;
        particles[1] = particle2;
        particles[2] = particle3;
        particles[3] = particle4;
        particles[4] = particle5;
    }
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2, particle3, particle4, particle5;
        int chiral1, chiral2, grid1, grid2;
        force.getTorsionTorsionParameters(group1, particle1, particle2, particle3, particle4, particle5, chiral1, grid1);
        force.getTorsionTorsionParameters(group2, particle1, particle2, particle3, particle4, particle5, chiral2, grid2);
        return (grid1 == grid2);
    }
private:
    const AmoebaTorsionTorsionForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
CudaCalcAmoebaTorsionTorsionForceKernel::CudaCalcAmoebaTorsionTorsionForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
                CalcAmoebaTorsionTorsionForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaTorsionTorsionForceKernel::~CudaCalcAmoebaTorsionTorsionForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaTorsionTorsionForceKernel::initialize(const System& system, const AmoebaTorsionTorsionForce& force) {

    data.setAmoebaLocalForcesKernel( this );
    numTorsionTorsions = force.getNumTorsionTorsions();

    // torsion-torsion parameters

    std::vector<int>   particle1(numTorsionTorsions);
    std::vector<int>   particle2(numTorsionTorsions);
    std::vector<int>   particle3(numTorsionTorsions);
    std::vector<int>   particle4(numTorsionTorsions);
    std::vector<int>   particle5(numTorsionTorsions);
    std::vector<int>   chiralCheckAtomIndex(numTorsionTorsions);
    std::vector<int>   gridIndices(numTorsionTorsions);

    for (int i = 0; i < numTorsionTorsions; i++) {
        force.getTorsionTorsionParameters(i, particle1[i], particle2[i], particle3[i],
                                             particle4[i], particle5[i],
                                             chiralCheckAtomIndex[i], gridIndices[i]);
    }
    gpuSetAmoebaTorsionTorsionParameters(data.getAmoebaGpu(), particle1, particle2, particle3, particle4, particle5, chiralCheckAtomIndex, gridIndices );

    // torsion-torsion grids

    numTorsionTorsionGrids = force.getNumTorsionTorsionGrids();
742
    std::vector<TorsionTorsionGridFloat> floatGrids;
Mark Friedrichs's avatar
Mark Friedrichs committed
743
744

    floatGrids.resize(numTorsionTorsionGrids);
745
    for (int gridIndex = 0; gridIndex < numTorsionTorsionGrids; gridIndex++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
746

747
748
        const TorsionTorsionGrid& grid = force.getTorsionTorsionGrid( gridIndex );
        floatGrids[gridIndex].resize( grid.size() );
749
750
751
752
753
754
755
756
757

        // check if grid needs to be reordered: x-angle should be 'slow' index

        TorsionTorsionGrid reorderedGrid;
        int reorder = 0;
        if( grid[0][0][0] != grid[0][1][0] ){
            AmoebaTorsionTorsionForceImpl::reorderGrid( grid, reorderedGrid );
            reorder = 1;
            if( data.getLog() ){
758
759
760
                (void) fprintf( data.getLog(), "CudaCalcAmoebaTorsionTorsionForceKernel Reordered torsion-torsion grid %4d [%u %u] %12.3f %12.3f   [%u %u] %12.3f %12.3f.\n",
                                gridIndex, static_cast<unsigned int>(grid.size()), static_cast<unsigned int>(grid[0].size()), grid[0][0][0], grid[0][1][0],
                                static_cast<unsigned int>(reorderedGrid.size() ),  static_cast<unsigned int>(reorderedGrid[0].size() ), reorderedGrid[0][0][0], reorderedGrid[0][1][0] );
761
762
            }
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
763
764
        for (unsigned int ii = 0; ii < grid.size(); ii++) {

765
            floatGrids[gridIndex][ii].resize( grid[ii].size() );
Mark Friedrichs's avatar
Mark Friedrichs committed
766
767
            for (unsigned int jj = 0; jj < grid[ii].size(); jj++) {

768
                floatGrids[gridIndex][ii][jj].resize( grid[ii][jj].size() );
769
                if( reorder ){
770

771
772
773
                    for( unsigned int kk = 0; kk < grid[ii][jj].size(); kk++) {
                        floatGrids[gridIndex][ii][jj][kk] = static_cast<float>(reorderedGrid[ii][jj][kk]);
                    }
774

775
776
777
778
                } else {
                    for( unsigned int kk = 0; kk < grid[ii][jj].size(); kk++) {
                        floatGrids[gridIndex][ii][jj][kk] = static_cast<float>(grid[ii][jj][kk]);
                    }
Mark Friedrichs's avatar
Mark Friedrichs committed
779
780
781
782
783
                }
            }
        }
    }
    gpuSetAmoebaTorsionTorsionGrids(data.getAmoebaGpu(), floatGrids );
784
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
785
786
}

787
double CudaCalcAmoebaTorsionTorsionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
788
789
790
791
792
793
794
795
796
797
798
799
800
    if( data.getAmoebaLocalForcesKernel() == this ){
        computeAmoebaLocalForces( data );
    }
    return 0.0;
}

/* -------------------------------------------------------------------------- *
 *                             AmoebaMultipole                                *
 * -------------------------------------------------------------------------- */

static void computeAmoebaMultipoleForce( AmoebaCudaData& data ) {

    amoebaGpuContext gpu = data.getAmoebaGpu();
Mark Friedrichs's avatar
Mark Friedrichs committed
801
    if( data.getMultipoleForceCount() == 0 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
802
        gpuCopyWorkUnit( gpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
803
804
    }
    data.incrementMultipoleForceCount();
Mark Friedrichs's avatar
Mark Friedrichs committed
805

Mark Friedrichs's avatar
Update  
Mark Friedrichs committed
806
    if( 0 && data.getLog() ){
807
        (void) fprintf( data.getLog(), "In computeAmoebaMultipoleForce hasAmoebaGeneralizedKirkwood=%d\n", data.getHasAmoebaGeneralizedKirkwood() );
Mark Friedrichs's avatar
Mark Friedrichs committed
808
809
810
        (void) fflush( data.getLog());
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
811
812
    data.initializeGpu();

813
    // calculate Born radii using either the Grycuk or OBC algorithm if GK is active
Mark Friedrichs's avatar
Mark Friedrichs committed
814
815

    if( data.getHasAmoebaGeneralizedKirkwood() ){
816
        kClearBornSum( gpu->gpuContext );
817
818
819
820
821
822
823
        if( data.getUseGrycuk() ){
            kCalculateAmoebaGrycukBornRadii( gpu );
            kReduceGrycukGbsaBornSum( gpu );
        } else {
            kCalculateObcGbsaBornSum(gpu->gpuContext);
            kReduceObcGbsaBornSum(gpu->gpuContext);
       }
Mark Friedrichs's avatar
Mark Friedrichs committed
824
825
826
827
828
    }   

    // multipoles

    kCalculateAmoebaMultipoleForces(gpu, data.getHasAmoebaGeneralizedKirkwood() );
829

Mark Friedrichs's avatar
Mark Friedrichs committed
830
831
832
833
834
835
    // GK

    if( data.getHasAmoebaGeneralizedKirkwood() ){
        kCalculateAmoebaKirkwood(gpu);
    }

Mark Friedrichs's avatar
Update  
Mark Friedrichs committed
836
    if( 0 && data.getLog() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
837
838
839
840
841
        (void) fprintf( data.getLog(), "completed computeAmoebaMultipoleForce\n" );
        (void) fflush( data.getLog());
    }
}

842
843
844
845
846
847
class CudaCalcAmoebaMultipoleForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaMultipoleForce& force) : force(force) {
    }
    bool areParticlesIdentical(int particle1, int particle2) {
        double charge1, charge2, thole1, thole2, damping1, damping2, polarity1, polarity2;
848
        int axis1, axis2, multipole11, multipole12, multipole21, multipole22, multipole31, multipole32;
849
        vector<double> dipole1, dipole2, quadrupole1, quadrupole2;
850
851
        force.getMultipoleParameters(particle1, charge1, dipole1, quadrupole1, axis1, multipole11, multipole21, multipole31, thole1, damping1, polarity1);
        force.getMultipoleParameters(particle2, charge2, dipole2, quadrupole2, axis2, multipole12, multipole22, multipole32, thole2, damping2, polarity2);
852
        if (charge1 != charge2 || thole1 != thole2 || damping1 != damping2 || polarity1 != polarity2 || axis1 != axis2){
853
            return false;
854
855
856
        }
        for (int i = 0; i < (int) dipole1.size(); ++i){
            if (dipole1[i] != dipole2[i]){
857
                return false;
858
859
860
861
            }
        }
        for (int i = 0; i < (int) quadrupole1.size(); ++i){
            if (quadrupole1[i] != quadrupole2[i]){
862
                return false;
863
864
            }
        }
865
866
867
868
869
870
        return true;
    }
private:
    const AmoebaMultipoleForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
CudaCalcAmoebaMultipoleForceKernel::CudaCalcAmoebaMultipoleForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) : 
         CalcAmoebaMultipoleForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaMultipoleForceKernel::~CudaCalcAmoebaMultipoleForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaMultipoleForceKernel::initialize(const System& system, const AmoebaMultipoleForce& force) {

    numMultipoles   = force.getNumMultipoles();

    data.setHasAmoebaMultipole( true );

    std::vector<float> charges(numMultipoles);
    std::vector<float> dipoles(3*numMultipoles);
    std::vector<float> quadrupoles(9*numMultipoles);
    std::vector<float> tholes(numMultipoles);
    std::vector<float> dampingFactors(numMultipoles);
    std::vector<float> polarity(numMultipoles);
    std::vector<int>   axisTypes(numMultipoles);
893
894
895
    std::vector<int>   multipoleAtomZs(numMultipoles);
    std::vector<int>   multipoleAtomXs(numMultipoles);
    std::vector<int>   multipoleAtomYs(numMultipoles);
Mark Friedrichs's avatar
Mark Friedrichs committed
896
897
898
899
    std::vector< std::vector< std::vector<int> > > multipoleAtomCovalentInfo(numMultipoles);
    std::vector<int> minCovalentIndices(numMultipoles);
    std::vector<int> minCovalentPolarizationIndices(numMultipoles);

Mark Friedrichs's avatar
Mark Friedrichs committed
900
901
    //float scalingDistanceCutoff = static_cast<float>(force.getScalingDistanceCutoff());
    float scalingDistanceCutoff = 50.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915

    std::vector<AmoebaMultipoleForce::CovalentType> covalentList;
    covalentList.push_back( AmoebaMultipoleForce::Covalent12 );
    covalentList.push_back( AmoebaMultipoleForce::Covalent13 );
    covalentList.push_back( AmoebaMultipoleForce::Covalent14 );
    covalentList.push_back( AmoebaMultipoleForce::Covalent15 );

    std::vector<AmoebaMultipoleForce::CovalentType> polarizationCovalentList;
    polarizationCovalentList.push_back( AmoebaMultipoleForce::PolarizationCovalent11 );
    polarizationCovalentList.push_back( AmoebaMultipoleForce::PolarizationCovalent12 );
    polarizationCovalentList.push_back( AmoebaMultipoleForce::PolarizationCovalent13 );
    polarizationCovalentList.push_back( AmoebaMultipoleForce::PolarizationCovalent14 );

    std::vector<int> covalentDegree;
916
    AmoebaMultipoleForceImpl::getCovalentDegree( force, covalentDegree );
Mark Friedrichs's avatar
Mark Friedrichs committed
917
918
919
920
921
922
923
924
    int dipoleIndex      = 0;
    int quadrupoleIndex  = 0;
    int maxCovalentRange = 0;
    double totalCharge   = 0.0;
    for (int i = 0; i < numMultipoles; i++) {

        // multipoles

925
        int axisType, multipoleAtomZ, multipoleAtomX, multipoleAtomY;
Mark Friedrichs's avatar
Mark Friedrichs committed
926
927
928
        double charge, tholeD, dampingFactorD, polarityD;
        std::vector<double> dipolesD;
        std::vector<double> quadrupolesD;
929
        force.getMultipoleParameters(i, charge, dipolesD, quadrupolesD, axisType, multipoleAtomZ, multipoleAtomX, multipoleAtomY,
Mark Friedrichs's avatar
Mark Friedrichs committed
930
931
932
933
                                     tholeD, dampingFactorD, polarityD );

        totalCharge                       += charge;
        axisTypes[i]                       = axisType;
934
935
936
        multipoleAtomZs[i]                 = multipoleAtomZ;
        multipoleAtomXs[i]                 = multipoleAtomX;
        multipoleAtomYs[i]                 = multipoleAtomY;
Mark Friedrichs's avatar
Mark Friedrichs committed
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963

        charges[i]                         = static_cast<float>(charge);
        tholes[i]                          = static_cast<float>(tholeD);
        dampingFactors[i]                  = static_cast<float>(dampingFactorD);
        polarity[i]                        = static_cast<float>(polarityD);

        dipoles[dipoleIndex++]             = static_cast<float>(dipolesD[0]);
        dipoles[dipoleIndex++]             = static_cast<float>(dipolesD[1]);
        dipoles[dipoleIndex++]             = static_cast<float>(dipolesD[2]);
        
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[0]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[1]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[2]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[3]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[4]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[5]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[6]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[7]);
        quadrupoles[quadrupoleIndex++]     = static_cast<float>(quadrupolesD[8]);

        // covalent info

        std::vector< std::vector<int> > covalentLists;
        force.getCovalentMaps(i, covalentLists );
        multipoleAtomCovalentInfo[i] = covalentLists;

        int minCovalentIndex, maxCovalentIndex;
964
        AmoebaMultipoleForceImpl::getCovalentRange( force, i, covalentList, &minCovalentIndex, &maxCovalentIndex );
Mark Friedrichs's avatar
Mark Friedrichs committed
965
966
967
968
969
        minCovalentIndices[i] = minCovalentIndex;
        if( maxCovalentRange < (maxCovalentIndex - minCovalentIndex) ){
            maxCovalentRange = maxCovalentIndex - minCovalentIndex;
        }

970
        AmoebaMultipoleForceImpl::getCovalentRange( force, i, polarizationCovalentList, &minCovalentIndex, &maxCovalentIndex );
Mark Friedrichs's avatar
Mark Friedrichs committed
971
972
973
974
975
976
        minCovalentPolarizationIndices[i] = minCovalentIndex;
        if( maxCovalentRange < (maxCovalentIndex - minCovalentIndex) ){
            maxCovalentRange = maxCovalentIndex - minCovalentIndex;
        }
    }

977
    int polarizationType = static_cast<int>(force.getPolarizationType());
978
979
980
981
982
    int nonbondedMethod = static_cast<int>(force.getNonbondedMethod());
    if( nonbondedMethod != 0 && nonbondedMethod != 1 ){
         throw OpenMMException("AmoebaMultipoleForce nonbonded method not recognized.\n");
    }

983
984
985
986
    if( polarizationType != 0 && polarizationType != 1 ){
         throw OpenMMException("AmoebaMultipoleForce polarization type not recognized.\n");
    }

987
    gpuSetAmoebaMultipoleParameters(data.getAmoebaGpu(), charges, dipoles, quadrupoles, axisTypes, multipoleAtomZs, multipoleAtomXs, multipoleAtomYs,
Mark Friedrichs's avatar
Mark Friedrichs committed
988
989
                                    tholes, scalingDistanceCutoff, dampingFactors, polarity,
                                    multipoleAtomCovalentInfo, covalentDegree, minCovalentIndices, minCovalentPolarizationIndices, (maxCovalentRange+2),
Mark Friedrichs's avatar
Mark Friedrichs committed
990
                                    0, force.getMutualInducedMaxIterations(),
Mark Friedrichs's avatar
Mark Friedrichs committed
991
                                    static_cast<float>( force.getMutualInducedTargetEpsilon()),
992
                                    nonbondedMethod, polarizationType,
993
                                    static_cast<float>( force.getCutoffDistance()),
Mark Friedrichs's avatar
Mark Friedrichs committed
994
                                    static_cast<float>( force.getAEwald()) );
995
    if (nonbondedMethod == AmoebaMultipoleForce::PME) {
996
        double alpha = force.getAEwald();
997
998
999
1000
        int xsize, ysize, zsize;
        NonbondedForce nb;
        nb.setEwaldErrorTolerance(force.getEwaldErrorTolerance());
        nb.setCutoffDistance(force.getCutoffDistance());
Mark Friedrichs's avatar
Mark Friedrichs committed
1001
1002
        std::vector<int> pmeGridDimension;
        force.getPmeGridDimensions( pmeGridDimension );
Mark Friedrichs's avatar
Mark Friedrichs committed
1003
        int pmeParametersSetBasedOnEwaldErrorTolerance;
1004
        if( pmeGridDimension[0] == 0 || alpha == 0.0 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1005
            NonbondedForceImpl::calcPMEParameters(system, nb, alpha, xsize, ysize, zsize);
Mark Friedrichs's avatar
Mark Friedrichs committed
1006
            pmeParametersSetBasedOnEwaldErrorTolerance = 1;
Mark Friedrichs's avatar
Mark Friedrichs committed
1007
1008
1009
1010
1011
        } else {
            alpha = force.getAEwald();
            xsize = pmeGridDimension[0];
            ysize = pmeGridDimension[1];
            zsize = pmeGridDimension[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
1012
            pmeParametersSetBasedOnEwaldErrorTolerance = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
1013
        }
1014
1015
1016

        gpuSetAmoebaPMEParameters(data.getAmoebaGpu(), (float) alpha, xsize, ysize, zsize);

1017
        if( data.getLog() ){
1018
            (void) fprintf( data.getLog(), "AmoebaMultipoleForce: PME parameters tol=%12.3e cutoff=%12.3f alpha=%12.3f [%d %d %d]\n",
1019
                            force.getEwaldErrorTolerance(), force.getCutoffDistance(),  alpha, xsize, ysize, zsize );
Mark Friedrichs's avatar
Mark Friedrichs committed
1020
            if( pmeParametersSetBasedOnEwaldErrorTolerance  ){
1021
                 (void) fprintf( data.getLog(), "Parameters based on error tolerance and OpenMM algorithm.\n" );
Mark Friedrichs's avatar
Mark Friedrichs committed
1022
            } else {
Mark Friedrichs's avatar
Mark Friedrichs committed
1023
1024
1025
                 double alphaT;
                 int xsizeT, ysizeT, zsizeT;
                 NonbondedForceImpl::calcPMEParameters(system, nb, alphaT, xsizeT, ysizeT, zsizeT);
Mark Friedrichs's avatar
Mark Friedrichs committed
1026
1027
                 double impliedTolerance  = alpha*force.getCutoffDistance();
                        impliedTolerance  = 0.5*exp( -(impliedTolerance*impliedTolerance) );
1028
                 (void) fprintf( data.getLog(), "Using input parameters implied tolerance=%12.3e;", impliedTolerance );
Mark Friedrichs's avatar
Mark Friedrichs committed
1029
                 (void) fprintf( data.getLog(), "OpenMM param: aEwald=%12.3f [%6d %6d %6d]\n", alphaT, xsizeT, ysizeT, zsizeT);
Mark Friedrichs's avatar
Mark Friedrichs committed
1030
            }
1031
            (void) fprintf( data.getLog(), "\n" );
1032
1033
            (void) fflush( data.getLog() );
        }
1034

1035
        data.setApplyMultipoleCutoff( 1 );
1036

Mark Friedrichs's avatar
Mark Friedrichs committed
1037
1038
1039
1040
1041
        data.cudaPlatformData.nonbondedMethod = PARTICLE_MESH_EWALD;
        amoebaGpuContext amoebaGpu            = data.getAmoebaGpu();
        gpuContext gpu                        = amoebaGpu->gpuContext;
        gpu->sim.nonbondedCutoffSqr           = force.getCutoffDistance()*force.getCutoffDistance();
        gpu->sim.nonbondedMethod              = PARTICLE_MESH_EWALD;
1042
    }
1043
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
1044
1045
}

1046
double CudaCalcAmoebaMultipoleForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
1047
1048
1049
1050
1051
1052
1053
1054
    computeAmoebaMultipoleForce( data );
    return 0.0;
}

/* -------------------------------------------------------------------------- *
 *                       AmoebaGeneralizedKirkwood                            *
 * -------------------------------------------------------------------------- */

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
class CudaCalcAmoebaGeneralizedKirkwoodForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaGeneralizedKirkwoodForce& force) : force(force) {
    }
    bool areParticlesIdentical(int particle1, int particle2) {
        double charge1, charge2, radius1, radius2, scale1, scale2;
        force.getParticleParameters(particle1, charge1, radius1, scale1);
        force.getParticleParameters(particle2, charge2, radius2, scale2);
        return (charge1 == charge2 && radius1 == radius2 && scale1 == scale2);
    }
private:
    const AmoebaGeneralizedKirkwoodForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
CudaCalcAmoebaGeneralizedKirkwoodForceKernel::CudaCalcAmoebaGeneralizedKirkwoodForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) : 
           CalcAmoebaGeneralizedKirkwoodForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaGeneralizedKirkwoodForceKernel::~CudaCalcAmoebaGeneralizedKirkwoodForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaGeneralizedKirkwoodForceKernel::initialize(const System& system, const AmoebaGeneralizedKirkwoodForce& force) {

    data.setHasAmoebaGeneralizedKirkwood( true );
1081

Mark Friedrichs's avatar
Mark Friedrichs committed
1082
    int numParticles = system.getNumParticles();
1083

Mark Friedrichs's avatar
Mark Friedrichs committed
1084
1085
1086
    std::vector<float> radius(numParticles);
    std::vector<float> scale(numParticles);
    std::vector<float> charge(numParticles);
1087

Mark Friedrichs's avatar
Mark Friedrichs committed
1088
1089
1090
1091
1092
1093
1094
    for( int ii = 0; ii < numParticles; ii++ ){
        double particleCharge, particleRadius, scalingFactor;
        force.getParticleParameters(ii, particleCharge, particleRadius, scalingFactor);
        radius[ii]  = static_cast<float>( particleRadius );
        scale[ii]   = static_cast<float>( scalingFactor );
        charge[ii]  = static_cast<float>( particleCharge );
    }   
1095
1096
1097
1098
    if( data.getUseGrycuk() ){

        gpuSetAmoebaGrycukParameters( data.getAmoebaGpu(), static_cast<float>(force.getSoluteDielectric() ), 
                                      static_cast<float>( force.getSolventDielectric() ), 
Mark Friedrichs's avatar
Mark Friedrichs committed
1099
                                      radius, scale, charge,
1100
1101
1102
1103
1104
1105
1106
1107
                                      force.getIncludeCavityTerm(),
                                      static_cast<float>( force.getProbeRadius() ), 
                                      static_cast<float>( force.getSurfaceAreaFactor() ) ); 
        
    } else {

        gpuSetAmoebaObcParameters( data.getAmoebaGpu(), static_cast<float>(force.getSoluteDielectric() ), 
                                   static_cast<float>( force.getSolventDielectric() ), 
Mark Friedrichs's avatar
Mark Friedrichs committed
1108
                                   radius, scale, charge,
1109
1110
1111
1112
1113
                                   force.getIncludeCavityTerm(),
                                   static_cast<float>( force.getProbeRadius() ), 
                                   static_cast<float>( force.getSurfaceAreaFactor() ) ); 

    }
1114
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
1115
1116
}

1117
double CudaCalcAmoebaGeneralizedKirkwoodForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
    // handled in computeAmoebaMultipoleForce()
    return 0.0;
}

static void computeAmoebaVdwForce( AmoebaCudaData& data ) {

    amoebaGpuContext gpu = data.getAmoebaGpu();
    data.initializeGpu();

    // Vdw14_7F
1128
    kCalculateAmoebaVdw14_7Forces(gpu, data.getUseVdwNeighborList());
Mark Friedrichs's avatar
Mark Friedrichs committed
1129
1130
}

1131
1132
1133
1134
/* -------------------------------------------------------------------------- *
 *                           AmoebaVdw                                        *
 * -------------------------------------------------------------------------- */

1135
1136
1137
1138
1139
1140
1141
1142
1143
class CudaCalcAmoebaVdwForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaVdwForce& force) : force(force) {
    }
    bool areParticlesIdentical(int particle1, int particle2) {
        int iv1, iv2, class1, class2;
        double sigma1, sigma2, epsilon1, epsilon2, reduction1, reduction2;
        force.getParticleParameters(particle1, iv1, class1, sigma1, epsilon1, reduction1);
        force.getParticleParameters(particle2, iv2, class2, sigma2, epsilon2, reduction2);
1144
        return (class1 == class2 && sigma1 == sigma2 && epsilon1 == epsilon2 && reduction1 == reduction2);
1145
1146
1147
1148
1149
    }
private:
    const AmoebaVdwForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
CudaCalcAmoebaVdwForceKernel::CudaCalcAmoebaVdwForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) :
       CalcAmoebaVdwForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaVdwForceKernel::~CudaCalcAmoebaVdwForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaVdwForceKernel::initialize(const System& system, const AmoebaVdwForce& force) {

    // per-particle parameters

    int numParticles = system.getNumParticles();
1164

Mark Friedrichs's avatar
Mark Friedrichs committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
    std::vector<int> indexIVs(numParticles);
    std::vector<int> indexClasses(numParticles);
    std::vector< std::vector<int> > allExclusions(numParticles);
    std::vector<float> sigmas(numParticles);
    std::vector<float> epsilons(numParticles);
    std::vector<float> reductions(numParticles);
    for( int ii = 0; ii < numParticles; ii++ ){

        int indexIV, indexClass;
1174
        double sigma, epsilon, reduction;
Mark Friedrichs's avatar
Mark Friedrichs committed
1175
1176
        std::vector<int> exclusions;

1177
        force.getParticleParameters( ii, indexIV, indexClass, sigma, epsilon, reduction );
Mark Friedrichs's avatar
Mark Friedrichs committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
        force.getParticleExclusions( ii, exclusions );
        for( unsigned int jj = 0; jj < exclusions.size(); jj++ ){
           allExclusions[ii].push_back( exclusions[jj] );
        }

        indexIVs[ii]      = indexIV;
        indexClasses[ii]  = indexClass;
        sigmas[ii]        = static_cast<float>( sigma );
        epsilons[ii]      = static_cast<float>( epsilon );
        reductions[ii]    = static_cast<float>( reduction );
    }   

1190
1191
    gpuSetAmoebaVdwParameters( data.getAmoebaGpu(), indexIVs, indexClasses, sigmas, epsilons, reductions,
                               force.getSigmaCombiningRule(), force.getEpsilonCombiningRule(),
Mark Friedrichs's avatar
Mark Friedrichs committed
1192
                               allExclusions, force.getPBC(), static_cast<float>(force.getCutoff()) );
1193
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
1194
1195
1196
1197
    if( data.getLog() ){
        (void) fprintf( data.getLog(), "CudaCalcAmoebaVdwForceKernel PBC=%d getUseNeighborList=%d\n",
                        force.getPBC(), force.getUseNeighborList() );
    }
1198
    data.setUseVdwNeighborList( force.getUseNeighborList() );
Mark Friedrichs's avatar
Mark Friedrichs committed
1199
1200
}

1201
double CudaCalcAmoebaVdwForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
    computeAmoebaVdwForce( data );
    return 0.0;
}

/* -------------------------------------------------------------------------- *
 *                           AmoebaWcaDispersion                              *
 * -------------------------------------------------------------------------- */

static void computeAmoebaWcaDispersionForce( AmoebaCudaData& data ) {

    data.initializeGpu();
Mark Friedrichs's avatar
Update  
Mark Friedrichs committed
1213
    if( 0 && data.getLog() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1214
1215
1216
1217
1218
        (void) fprintf( data.getLog(), "Calling computeAmoebaWcaDispersionForce  " ); (void) fflush( data.getLog() );
    }

    kCalculateAmoebaWcaDispersionForces( data.getAmoebaGpu() );

Mark Friedrichs's avatar
Update  
Mark Friedrichs committed
1219
    if( 0 && data.getLog() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
1220
1221
1222
1223
        (void) fprintf( data.getLog(), " -- completed\n" ); (void) fflush( data.getLog() );
    }
}

1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
class CudaCalcAmoebaWcaDispersionForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const AmoebaWcaDispersionForce& force) : force(force) {
    }
    bool areParticlesIdentical(int particle1, int particle2) {
        double radius1, radius2, epsilon1, epsilon2;
        force.getParticleParameters(particle1, radius1, epsilon1);
        force.getParticleParameters(particle2, radius2, epsilon2);
        return (radius1 == radius2 && epsilon1 == epsilon2);
    }
private:
    const AmoebaWcaDispersionForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
CudaCalcAmoebaWcaDispersionForceKernel::CudaCalcAmoebaWcaDispersionForceKernel(std::string name, const Platform& platform, AmoebaCudaData& data, System& system) : 
           CalcAmoebaWcaDispersionForceKernel(name, platform), data(data), system(system) {
    data.incrementKernelCount();
}

CudaCalcAmoebaWcaDispersionForceKernel::~CudaCalcAmoebaWcaDispersionForceKernel() {
    data.decrementKernelCount();
}

void CudaCalcAmoebaWcaDispersionForceKernel::initialize(const System& system, const AmoebaWcaDispersionForce& force) {

    // per-particle parameters

    int numParticles = system.getNumParticles();
    std::vector<float> radii(numParticles);
    std::vector<float> epsilons(numParticles);
    for( int ii = 0; ii < numParticles; ii++ ){

        double radius, epsilon;
        force.getParticleParameters( ii, radius, epsilon );

        radii[ii]         = static_cast<float>( radius );
        epsilons[ii]      = static_cast<float>( epsilon );
    }   
1262
    float totalMaximumDispersionEnergy =  static_cast<float>( AmoebaWcaDispersionForceImpl::getTotalMaximumDispersionEnergy( force ) );
Mark Friedrichs's avatar
Mark Friedrichs committed
1263
1264
1265
1266
1267
1268
1269
1270
    gpuSetAmoebaWcaDispersionParameters( data.getAmoebaGpu(), radii, epsilons, totalMaximumDispersionEnergy,
                                          static_cast<float>( force.getEpso( )),
                                          static_cast<float>( force.getEpsh( )),
                                          static_cast<float>( force.getRmino( )),
                                          static_cast<float>( force.getRminh( )),
                                          static_cast<float>( force.getAwater( )),
                                          static_cast<float>( force.getShctd( )),
                                          static_cast<float>( force.getDispoff( ) ) );
1271
    data.getAmoebaGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
1272
1273
}

1274
double CudaCalcAmoebaWcaDispersionForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
1275
1276
1277
    computeAmoebaWcaDispersionForce( data );
    return 0.0;
}