"vscode:/vscode.git/clone" did not exist on "b5ab824a3ca6f7bd8ef3499688fe45c69111f23f"
CudaFreeEnergyKernels.cpp 25.5 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2008-2009 Stanford University and the Authors.      *
 * Authors: Peter Eastman                                                     *
 * 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 "CudaFreeEnergyKernels.h"
28
29
#include "CudaForceInfo.h"

Mark Friedrichs's avatar
Mark Friedrichs committed
30
31
32
33
34
35
36
37
#include "openmm/Context.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/ContextImpl.h"
#include "kernels/gputypes.h"
#include "kernels/cudaKernels.h"
#include "kernels/GpuFreeEnergyCudaKernels.h" 

#include <cmath>
38
39
40
41
#include <map>
#include <cstring>
#include <cstdlib>
#include <typeinfo>
Mark Friedrichs's avatar
Mark Friedrichs committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

using namespace OpenMM;

typedef std::map< std::string, int > MapStringInt;
typedef MapStringInt::iterator MapStringIntI;
typedef MapStringInt::const_iterator MapStringIntCI;

// force names

const std::string HARMONIC_BOND_FORCE             = "HarmonicBond";
const std::string HARMONIC_ANGLE_FORCE            = "HarmonicBond";
const std::string PERIODIC_TORSION_FORCE          = "PeriodicTorsion";
const std::string RB_TORSION_FORCE                = "RbTorsion";

const std::string NB_FORCE                        = "Nb";
const std::string NB_SOFTCORE_FORCE               = "NbSoftcore";

const std::string NB_EXCEPTION_FORCE              = "NbException";
const std::string NB_EXCEPTION_SOFTCORE_FORCE     = "NbSoftcoreException";

const std::string GBSA_OBC_FORCE                  = "Obc";
const std::string GBSA_OBC_SOFTCORE_FORCE         = "ObcSoftcore";

const std::string GBVI_FORCE                      = "GBVI";
const std::string GBVI_SOFTCORE_FORCE             = "GBVISoftcore";

68
static void getForceMap(const System& system, MapStringInt& forceMap, FILE* log) { 
Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
71
72
73
74
75

    // check forces and relevant parameters

    for(int i = 0; i < system.getNumForces(); ++i) {

        int hit                 = 0;
        const Force& force      = system.getForce(i);
76
         std::string forceName  = "NA";
Mark Friedrichs's avatar
Mark Friedrichs committed
77
78
79
80
81
82
83
84

        // bond

        if( !hit ){

            try {
               const HarmonicBondForce& harmonicBondForce = dynamic_cast<const HarmonicBondForce&>(force);
               forceMap[HARMONIC_BOND_FORCE]              = 1;
85
               forceName                                  = HARMONIC_BOND_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
86
87
88
89
90
91
92
93
94
95
96
97
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // angle

        if( !hit ){

            try {
               const HarmonicAngleForce& harmonicAngleForce = dynamic_cast<const HarmonicAngleForce&>(force);
               forceMap[HARMONIC_ANGLE_FORCE]               = 1;
98
               forceName                                    = HARMONIC_ANGLE_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
99
100
101
102
103
104
105
106
107
108
109
110
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // PeriodicTorsionForce

        if( !hit ){

            try {
               const PeriodicTorsionForce & periodicTorsionForce = dynamic_cast<const PeriodicTorsionForce&>(force);
               forceMap[PERIODIC_TORSION_FORCE]                  = 1;
111
               forceName                                         = PERIODIC_TORSION_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
112
113
114
115
116
117
118
119
120
121
122
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // RBTorsionForce

        if( !hit ){
            try {
               const RBTorsionForce& rBTorsionForce = dynamic_cast<const RBTorsionForce&>(force);
               forceMap[RB_TORSION_FORCE]           = 1;
123
               forceName                            = RB_TORSION_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
124
125
126
127
128
129
130
131
132
133
134
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // nonbonded

        if( !hit ){
            try {
               const NonbondedForce& nbForce = dynamic_cast<const NonbondedForce&>(force);
               forceMap[NB_FORCE]            = 1;
135
               forceName                     = NB_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
136
137
138
139
140
141
142
143
144
145
            } catch( std::bad_cast ){
            }
        }

        // nonbonded softcore

        if( !hit ){
            try {
               const NonbondedSoftcoreForce& nbForce = dynamic_cast<const NonbondedSoftcoreForce&>(force);
               forceMap[NB_SOFTCORE_FORCE]           = 1;
146
               forceName                             = NB_SOFTCORE_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
147
148
149
150
151
152
153
154
155
156
            } catch( std::bad_cast ){
            }
        }

        // GBSA OBC

        if( !hit ){
            try {
               const GBSAOBCForce& obcForce       = dynamic_cast<const GBSAOBCForce&>(force);
               forceMap[GBSA_OBC_FORCE]           = 1;
157
               forceName                          = GBSA_OBC_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
158
159
160
161
162
163
164
165
166
167
168
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // GBSA OBC softcore

        if( !hit ){
            try {
               const GBSAOBCSoftcoreForce& obcForce = dynamic_cast<const GBSAOBCSoftcoreForce&>(force);
               forceMap[GBSA_OBC_SOFTCORE_FORCE]    = 1;
169
               forceName                            = GBSA_OBC_SOFTCORE_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
170
171
172
173
174
175
176
177
178
179
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // GB/VI

        if( !hit ){
            try {
               const GBVIForce& obcForce  = dynamic_cast<const GBVIForce&>(force);
180
181
               forceMap[GBVI_FORCE]       = 1;
               forceName                  = GBVI_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
182
183
184
185
186
187
188
189
190
191
192
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // GB/VI softcore

        if( !hit ){
            try {
               const GBVISoftcoreForce& gbviForce = dynamic_cast<const GBVISoftcoreForce&>(force);
               forceMap[GBVI_SOFTCORE_FORCE]      = 1;
193
               forceName                          = GBVI_SOFTCORE_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
194
195
196
197
               hit++;
            } catch( std::bad_cast ){
            }
        }
198
199

        if( log ){
200
            (void) fprintf( log, "Map: Force %d %s\n", i, forceName.c_str() );
201
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
202
203
204
     }
}

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const NonbondedSoftcoreForce& force) : force(force) {
    }    
    bool areParticlesIdentical(int particle1, int particle2) {
        double charge1, charge2, sigma1, sigma2, epsilon1, epsilon2, softcoreLJLambda1, softcoreLJLambda2;
        force.getParticleParameters(particle1, charge1, sigma1, epsilon1, softcoreLJLambda1);
        force.getParticleParameters(particle2, charge2, sigma2, epsilon2, softcoreLJLambda2);
        return (charge1 == charge2 && sigma1 == sigma2 && epsilon1 == epsilon2 && softcoreLJLambda1 == softcoreLJLambda2);
    }    
    int getNumParticleGroups() {
        return force.getNumExceptions();
    }    
    void getParticlesInGroup(int index, std::vector<int>& particles) {
        int particle1, particle2;
        double chargeProd, sigma, epsilon, softcoreLJLambda;
        force.getExceptionParameters(index, particle1, particle2, chargeProd, sigma, epsilon, softcoreLJLambda);
        particles.resize(2);
        particles[0] = particle1;
        particles[1] = particle2;
    }    
    bool areGroupsIdentical(int group1, int group2) {
        int particle1, particle2;
        double chargeProd1, chargeProd2, sigma1, sigma2, epsilon1, epsilon2, softcoreLJLambda1, softcoreLJLambda2;
        force.getExceptionParameters(group1, particle1, particle2, chargeProd1, sigma1, epsilon1, softcoreLJLambda1);
        force.getExceptionParameters(group2, particle1, particle2, chargeProd2, sigma2, epsilon2, softcoreLJLambda2);
        return (chargeProd1 == chargeProd2 && sigma1 == sigma2 && epsilon1 == epsilon2 && softcoreLJLambda1 == softcoreLJLambda2);
    }    
private:
    const NonbondedSoftcoreForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
237
CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::~CudaFreeEnergyCalcNonbondedSoftcoreForceKernel() {
238
239
240
    if( 0 && data.getLog() ){
        (void) fprintf( data.getLog(), "~CudaFreeEnergyCalcNonbondedSoftcoreForceKernel called.\n" );
        (void) fflush( data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
241
    }
242
    data.decrementKernelCount();
Mark Friedrichs's avatar
Mark Friedrichs committed
243
244
245
246
247
248
249
250
251
252
}

void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::initialize(const System& system, const NonbondedSoftcoreForce& force) {

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

   static const std::string methodName      = "CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::initialize";

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

253
254
255
    if( data.getLog() ){
        (void) fprintf( data.getLog(), "%s called.\n", methodName.c_str() );
        (void) fflush( data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
256
257
258
259
260
    }

    // check forces and relevant parameters

    MapStringInt forceMap;
261
    getForceMap( system, forceMap, data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

    int softcore        = 0;
    if( forceMap.find( GBSA_OBC_FORCE ) != forceMap.end() ){
       setIncludeGBSA( true );
    }

    if( forceMap.find( GBSA_OBC_SOFTCORE_FORCE ) != forceMap.end() ){
       setIncludeGBSA( true );
       softcore++;
    }

    if( forceMap.find( GBVI_FORCE ) != forceMap.end() ){
       setIncludeGBVI( true );
    }
    if( forceMap.find( GBVI_SOFTCORE_FORCE ) != forceMap.end() ){
       setIncludeGBVI( true );
       softcore++;
    }

    if( forceMap.find( NB_SOFTCORE_FORCE ) != forceMap.end() ){
       softcore++;
    }

    setIncludeSoftcore( softcore );

    numParticles      = force.getNumParticles();

    // Identify which exceptions are 1-4 interactions.

    std::vector<pair<int, int> > exclusions;
    std::vector<int> exceptions;
    for (int i = 0; i < force.getNumExceptions(); i++) {
        int particle1, particle2;
        double chargeProd, sigma, epsilon, softcoreLJLambda;
        force.getExceptionParameters(i, particle1, particle2, chargeProd, sigma, epsilon, softcoreLJLambda);
        exclusions.push_back(pair<int, int>(particle1, particle2));
        if (chargeProd != 0.0 || epsilon != 0.0)
            exceptions.push_back(i);
    }

    // Initialize nonbonded interactions.
    
304
    if( numParticles > 0 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
        std::vector<int> particle(numParticles);
        std::vector<float> c6(numParticles);
        std::vector<float> c12(numParticles);
        std::vector<float> q(numParticles);
        std::vector<float> softcoreLJLambdaArray(numParticles);
        std::vector<char> symbol;
        std::vector<std::vector<int> > exclusionList(numParticles);
        for (int i = 0; i < numParticles; i++) {
            double charge, radius, depth, softcoreLJLambda;
            force.getParticleParameters(i, charge, radius, depth, softcoreLJLambda);
            particle[i]              = i;
            q[i]                     = static_cast<float>( charge );
            c6[i]                    = static_cast<float>( (4*depth*pow(radius, 6.0)) );
            c12[i]                   = static_cast<float>( (4*depth*pow(radius, 12.0)) );
            softcoreLJLambdaArray[i] = static_cast<float>( softcoreLJLambda );
            exclusionList[i].push_back(i);
        }

        for (int i = 0; i < (int)exclusions.size(); i++) {
            exclusionList[exclusions[i].first].push_back(exclusions[i].second);
            exclusionList[exclusions[i].second].push_back(exclusions[i].first);
        }
        Vec3 boxVectors[3];
Peter Eastman's avatar
Peter Eastman committed
328
        system.getDefaultPeriodicBoxVectors(boxVectors[0], boxVectors[1], boxVectors[2]);
329
330
331
        freeEnergyGpuSetPeriodicBoxSize( data.getFreeEnergyGpu(), static_cast<float>(boxVectors[0][0] ), static_cast<float>(boxVectors[1][1] ), static_cast<float>(boxVectors[2][2] ));

        CudaFreeEnergyNonbondedMethod method = FREE_ENERGY_NO_CUTOFF;
Mark Friedrichs's avatar
Mark Friedrichs committed
332
        if (force.getNonbondedMethod() != NonbondedSoftcoreForce::NoCutoff) {
333
            method = FREE_ENERGY_CUTOFF;
Mark Friedrichs's avatar
Mark Friedrichs committed
334
335
        }
        if (force.getNonbondedMethod() == NonbondedSoftcoreForce::CutoffPeriodic) {
336
            method = FREE_ENERGY_PERIODIC;
Mark Friedrichs's avatar
Mark Friedrichs committed
337
338
339
340
        }

        // setup parameters

341
342
343
        gpuSetNonbondedSoftcoreParameters( data.getFreeEnergyGpu(), 138.935485f, particle, c6, c12, q,
                                           softcoreLJLambdaArray, symbol, exclusionList, method,
                                           static_cast<float>(force.getCutoffDistance() ), static_cast<float>(force.getReactionFieldDielectric()));
Mark Friedrichs's avatar
Mark Friedrichs committed
344
345
346
    }

    // Initialize 1-4 nonbonded interactions.
347
348
349

    numExceptions = exceptions.size();
    if( numExceptions > 0 ){
Mark Friedrichs's avatar
Mark Friedrichs committed
350
351
352
353
        std::vector<int> particle1(numExceptions);
        std::vector<int> particle2(numExceptions);
        std::vector<float> c6(numExceptions);
        std::vector<float> c12(numExceptions);
354
        std::vector<float> qProd(numExceptions);
Mark Friedrichs's avatar
Mark Friedrichs committed
355
356
357
358
        std::vector<float> softcoreLJLambdaArray(numExceptions);
        for (int i = 0; i < numExceptions; i++) {
            double charge, sig, eps, softcoreLJLambda;
            force.getExceptionParameters(exceptions[i], particle1[i], particle2[i], charge, sig, eps, softcoreLJLambda);
359
360
            c6[i]                    = static_cast<float>( (4.0f*eps*powf(sig, 6.0f)) );
            c12[i]                   = static_cast<float>( (4.0f*eps*powf(sig, 12.0f)) );
361
            qProd[i]                 = static_cast<float>( charge );
Mark Friedrichs's avatar
Mark Friedrichs committed
362
363
            softcoreLJLambdaArray[i] = static_cast<float>( softcoreLJLambda );
        }
364
365
366
367
        gpuSetLJ14SoftcoreParameters( data.getFreeEnergyGpu(), 138.935485f, particle1, particle2, c6, c12, qProd, softcoreLJLambdaArray);
    } else if( data.getLog() ){
        (void) fprintf( data.getLog(), "Mo nonbonded softcore exceptions.\n" );
        (void) fflush( data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
368
    }
369
370

    data.getFreeEnergyGpu()->gpuContext->forces.push_back(new ForceInfo(force));
Mark Friedrichs's avatar
Mark Friedrichs committed
371
372
}

373
double CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::execute( ContextImpl& context, bool includeForces, bool includeEnergy ){
Mark Friedrichs's avatar
Mark Friedrichs committed
374
375
376
377
378
379
380

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

   static const std::string methodName      = "CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeForces";

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

381
    freeEnergyGpuContext gpu = data.getFreeEnergyGpu();
Mark Friedrichs's avatar
Mark Friedrichs committed
382

383
    data.initializeGpu( );
Mark Friedrichs's avatar
Mark Friedrichs committed
384
385
386
387
388
389
 
    // calculate nonbonded ixns here, only if implicit solvent is inactive

    if ( !getIncludeGBSA() && !getIncludeGBVI() ) {
        kCalculateCDLJSoftcoreForces(gpu);
    }
390

Mark Friedrichs's avatar
Mark Friedrichs committed
391
392
    // local LJ-14 forces

393
394
395
396
    if( getNumExceptions() > 0 ){
        kCalculateLocalSoftcoreForces(gpu);
    }

Mark Friedrichs's avatar
Mark Friedrichs committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    return 0.0;
}

bool CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::getIncludeGBSA( void ) const {
    return bIncludeGBSA;
}

void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::setIncludeGBSA( bool inputIncludeGBSA ){
    bIncludeGBSA = inputIncludeGBSA;
}

bool CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::getIncludeGBVI( void ) const {
    return bIncludeGBVI;
}

void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::setIncludeGBVI( bool inputIncludeGBVI ){
    bIncludeGBVI = inputIncludeGBVI;
}

416
417
int CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::getIncludeSoftcore( void ) const {
    return includeSoftcore;
Mark Friedrichs's avatar
Mark Friedrichs committed
418
419
420
421
422
423
}

int CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::getNumExceptions( void ) const {
    return numExceptions;
}

424
425
void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::setIncludeSoftcore( int inputIncludeSoftcore ){
    includeSoftcore = inputIncludeSoftcore;
Mark Friedrichs's avatar
Mark Friedrichs committed
426
427
}

428
429
430
431
432
433
434
435
436
437
438
439
440
class CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const GBSAOBCSoftcoreForce& force) : force(force) {
    }
    bool areParticlesIdentical(int particle1, int particle2) {
        double charge1, charge2, radius1, radius2, scale1, scale2, particleNonPolarScalingFactor1, particleNonPolarScalingFactor2;
        force.getParticleParameters(particle1, charge1, radius1, scale1, particleNonPolarScalingFactor1);
        force.getParticleParameters(particle2, charge2, radius2, scale2, particleNonPolarScalingFactor2);
        return (charge1 == charge2 && radius1 == radius2 && scale1 == scale2 && particleNonPolarScalingFactor1 == particleNonPolarScalingFactor2);
    }
private:
    const GBSAOBCSoftcoreForce& force;
};
Mark Friedrichs's avatar
Mark Friedrichs committed
441
442

CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::~CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel() {
443
444
445
446
447
    if( 0 && data.getLog() ){
        (void) fprintf( data.getLog(), "~CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel called.\n" );
        (void) fflush( data.getLog() );
    }
    data.decrementKernelCount();
Mark Friedrichs's avatar
Mark Friedrichs committed
448
449
450
451
452
453
}

void CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::initialize(const System& system, const GBSAOBCSoftcoreForce& force) {

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

454
    freeEnergyGpuContext gpu  = data.getFreeEnergyGpu();
Mark Friedrichs's avatar
Mark Friedrichs committed
455
456

    MapStringInt forceMap;
457
    getForceMap( system, forceMap, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
458
459
460
461

    // check that nonbonded (non-softcore is not active)

    if( forceMap.find( NB_FORCE ) != forceMap.end() ){ 
462
        throw OpenMMException( "Mixing NonbondedForce and GBSAOBCSoftoreForce is not allowed -- use NonbondedSoftcoreForce " );
Mark Friedrichs's avatar
Mark Friedrichs committed
463
    }
464
465
466
    if( forceMap.find( NB_SOFTCORE_FORCE ) == forceMap.end() ){ 
        throw OpenMMException( "NonbondedSoftcore force must be included w/ GBSAOBCSoftcore force." );
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
467
468
469
470
471
472
473
474

    int numParticles = system.getNumParticles();

    std::vector<float> radius(numParticles);
    std::vector<float> scale(numParticles);
    std::vector<float> charge(numParticles);
    std::vector<float> nonPolarScalingFactors(numParticles);

475
    for( int ii = 0; ii < numParticles; ii++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
476
        double particleCharge, particleRadius, scalingFactor, particleNonPolarScalingFactor;
477
478
479
480
481
        force.getParticleParameters( ii, particleCharge, particleRadius, scalingFactor, particleNonPolarScalingFactor);
        radius[ii]                 = static_cast<float>( particleRadius);
        scale[ii]                  = static_cast<float>( scalingFactor);
        charge[ii]                 = static_cast<float>( particleCharge);
        nonPolarScalingFactors[ii] = static_cast<float>( particleNonPolarScalingFactor);
Mark Friedrichs's avatar
Mark Friedrichs committed
482
    }
483

484
485
486
487
    gpuSetObcSoftcoreParameters( gpu, static_cast<float>( force.getSoluteDielectric()),
                                 static_cast<float>( force.getSolventDielectric()),
                                 static_cast<float>( force.getNonPolarPrefactor()),
                                 radius, scale, charge, nonPolarScalingFactors );
Mark Friedrichs's avatar
Mark Friedrichs committed
488

489
490
    data.getFreeEnergyGpu()->gpuContext->forces.push_back(new ForceInfo(force));
    return;
Mark Friedrichs's avatar
Mark Friedrichs committed
491

492
}
Mark Friedrichs's avatar
Mark Friedrichs committed
493

494
double CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
495
496
497

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

498
499
500
501
    freeEnergyGpuContext freeEnergyGpu = data.getFreeEnergyGpu();
    gpuContext gpu                     = freeEnergyGpu->gpuContext;
    int call = 0;
    
Mark Friedrichs's avatar
Mark Friedrichs committed
502
    // send address's of arrays, ... to device on first call
503
    // required since force/energy buffers not set when CudaFreeEnergyCalcGBVISoftcoreForceKernel::initialize() was called
Mark Friedrichs's avatar
Mark Friedrichs committed
504

505
    data.initializeGpu( );
Mark Friedrichs's avatar
Mark Friedrichs committed
506

507
508
509
510
511
    // (1) clear Born force array
    // (2) calculate Born radii and sum
    // (3) loop 1
    // (4) sum/calculate Born forces
    // (5) loop 2
Mark Friedrichs's avatar
Mark Friedrichs committed
512

513
    kClearSoftcoreBornForces(gpu);
514
    kCalculateObcGbsaSoftcoreBornSum( freeEnergyGpu );
515
    kReduceObcGbsaSoftcoreBornSum(gpu);
516
    kCalculateCDLJObcGbsaSoftcoreForces1( freeEnergyGpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
517

518
    // sum Born forces and  execute second OBC loop
Mark Friedrichs's avatar
Mark Friedrichs committed
519

520
    kReduceObcGbsaSoftcoreBornForces(gpu);
521
    kCalculateObcGbsaSoftcoreForces2( freeEnergyGpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
522

523
524
    if( data.getLog() ){
        kPrintObcGbsaSoftcore( freeEnergyGpu, "Post kCalculateObcGbsaSoftcoreForces2", call, data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
525
526
    }

527
    return 0.0;
Mark Friedrichs's avatar
Mark Friedrichs committed
528
529
}

530
531
532
533
534
535
536
537
538
539
540
541
542
543
class CudaFreeEnergyCalcGBVISoftcoreForceKernel::ForceInfo : public CudaForceInfo {
public:
    ForceInfo(const GBVISoftcoreForce& force) : force(force) {
    }    
    bool areParticlesIdentical(int particle1, int particle2) {
        double charge1, charge2, radius1, radius2, gamma1, gamma2, bornRadiusScaleFactor1, bornRadiusScaleFactor2;
        force.getParticleParameters(particle1, charge1, radius1, gamma1, bornRadiusScaleFactor1);
        force.getParticleParameters(particle2, charge2, radius2, gamma2, bornRadiusScaleFactor2);
        return (charge1 == charge2 && radius1 == radius2 && gamma1 == gamma2 && bornRadiusScaleFactor1 == bornRadiusScaleFactor2);
    }    
private:
    const GBVISoftcoreForce& force;
};

Mark Friedrichs's avatar
Mark Friedrichs committed
544
CudaFreeEnergyCalcGBVISoftcoreForceKernel::~CudaFreeEnergyCalcGBVISoftcoreForceKernel() {
545
546
547
    if( 0 && data.getLog() ){
        (void) fprintf( data.getLog(), "~CudaFreeEnergyCalcGBVISoftcoreForceKernel called.\n" );
        (void) fflush( data.getLog() );
Mark Friedrichs's avatar
Mark Friedrichs committed
548
    }
549
    data.decrementKernelCount();
Mark Friedrichs's avatar
Mark Friedrichs committed
550
551
552
553
554
555
}

void CudaFreeEnergyCalcGBVISoftcoreForceKernel::initialize(const System& system, const GBVISoftcoreForce& force, const std::vector<double> & inputScaledRadii) {

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

556
557
    int numParticles          = system.getNumParticles();
    freeEnergyGpuContext gpu  = data.getFreeEnergyGpu();
Mark Friedrichs's avatar
Mark Friedrichs committed
558
559
560
561

    // check forces and relevant parameters

    MapStringInt forceMap;
562
    getForceMap( system, forceMap, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
563
564
565
566
567
568
569
570
571
572
573
574
575

    // check that nonbonded (non-softcore is not active)

    if( forceMap.find( NB_FORCE ) != forceMap.end() ){ 
        throw OpenMMException( "Mixing NonbondedForce and GBVISoftoreForce not allowed -- use NonbondedSoftcoreForce " );
    }

    std::vector<int>   particle(numParticles);
    std::vector<float> radius(numParticles);
    std::vector<float> scaledRadii(numParticles);
    std::vector<float> gammas(numParticles);
    std::vector<float> bornRadiusScaleFactors(numParticles);

576
    for( int i = 0; i < numParticles; i++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
577
578
579
580
581
582
583
584
585
586
        double charge, particleRadius, gamma, bornRadiusScaleFactor;
        force.getParticleParameters(i, charge, particleRadius, gamma, bornRadiusScaleFactor);
        particle[i]                  = i;
        radius[i]                    = static_cast<float>( particleRadius );
        gammas[i]                    = static_cast<float>( gamma );
        scaledRadii[i]               = static_cast<float>( inputScaledRadii[i] );
        bornRadiusScaleFactors[i]    = static_cast<float>( bornRadiusScaleFactor );
    }

    std::vector<float> quinticSplineParameters;
587
    if( force.getBornRadiusScalingMethod() == GBVISoftcoreForce::QuinticSpline ){
Mark Friedrichs's avatar
Mark Friedrichs committed
588
589
590
591
592
593
594

        // quintic spline

        quinticSplineParameters.resize(2);
        quinticSplineParameters[0] = static_cast<float>(force.getQuinticLowerLimitFactor());
        quinticSplineParameters[1] = static_cast<float>(force.getQuinticUpperBornRadiusLimit());
        quinticSplineParameters[1] = powf( quinticSplineParameters[1], -3.0f ); 
595
        quinticScaling =  1;
Mark Friedrichs's avatar
Mark Friedrichs committed
596
597
598
599
600
    }

    // load parameters onto board
    // defined in kCalculateGBVISoftcore.cu

601
602
    gpuSetGBVISoftcoreParameters( gpu, static_cast<float>( force.getSoluteDielectric() ), static_cast<float>( force.getSolventDielectric() ),
                                  particle, radius, gammas, scaledRadii, bornRadiusScaleFactors, quinticSplineParameters);
Mark Friedrichs's avatar
Mark Friedrichs committed
603

604
605
606
    data.getFreeEnergyGpu()->gpuContext->forces.push_back(new ForceInfo(force));

    return;
Mark Friedrichs's avatar
Mark Friedrichs committed
607
608
}

609
double CudaFreeEnergyCalcGBVISoftcoreForceKernel::execute(ContextImpl& context, bool includeForces, bool includeEnergy) {
Mark Friedrichs's avatar
Mark Friedrichs committed
610

611
612
613
    freeEnergyGpuContext freeEnergyGpu = data.getFreeEnergyGpu();
    gpuContext gpu                     = freeEnergyGpu->gpuContext;
    
Mark Friedrichs's avatar
Mark Friedrichs committed
614
615
616
    // send address's of arrays, ... to device on first call
    // required since force/energy buffers not set when CudaFreeEnergyCalcGBVISoftcoreForceKernel::initialize() was called

617
    data.initializeGpu( );
Mark Friedrichs's avatar
Mark Friedrichs committed
618

619
620
621
622
623
    // (1) clear Born force array
    // (2) calculate Born radii and sum
    // (3) loop 1
    // (4) sum/calculate Born forces
    // (5) loop 2
Mark Friedrichs's avatar
Mark Friedrichs committed
624

625
    // calculate Born radii and first loop of GB/VI forces
Mark Friedrichs's avatar
Mark Friedrichs committed
626

627
    kClearSoftcoreBornForces(gpu);
Mark Friedrichs's avatar
Mark Friedrichs committed
628

629
    kCalculateGBVISoftcoreBornSum( freeEnergyGpu );
630

631
632
    if( quinticScaling ){
        kReduceGBVIBornSumQuinticScaling( freeEnergyGpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
633
    } else {
634
        kReduceGBVISoftcoreBornSum( freeEnergyGpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
635
636
    }

637
    kCalculateCDLJObcGbsaSoftcoreForces1( freeEnergyGpu );
638

639
640
    if( quinticScaling ){
        kReduceGBVIBornForcesQuinticScaling(freeEnergyGpu);
Mark Friedrichs's avatar
Mark Friedrichs committed
641
    } else {
642
        kReduceGBVISoftcoreBornForces( freeEnergyGpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
643
644
645
646
    }

    // second loop of GB/VI forces

647
648
649
650
    kCalculateGBVISoftcoreForces2( freeEnergyGpu );
    if( data.getLog() ){
        kPrintGBVISoftcore( freeEnergyGpu, "Post kCalculateGBVISoftcoreForces2", 0, data.getLog() );
    }
651

Mark Friedrichs's avatar
Mark Friedrichs committed
652
653
    return 0.0;
}