CudaFreeEnergyKernels.cpp 28.8 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* -------------------------------------------------------------------------- *
 *                                   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"
#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 "kernels/GpuLJ14Softcore.h" 

#include <cmath>

using namespace OpenMM;

#include <map>
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";

64
static void getForceMap(const System& system, MapStringInt& forceMap, FILE* log) { 
Mark Friedrichs's avatar
Mark Friedrichs committed
65
66
67
68
69
70
71

    // check forces and relevant parameters

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

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

        // bond

        if( !hit ){

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

        // angle

        if( !hit ){

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

        // PeriodicTorsionForce

        if( !hit ){

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

        // RBTorsionForce

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

        // nonbonded

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

        // nonbonded softcore

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

        // GBSA OBC

        if( !hit ){
            try {
               const GBSAOBCForce& obcForce       = dynamic_cast<const GBSAOBCForce&>(force);
               forceMap[GBSA_OBC_FORCE]           = 1;
153
               forceName                          = GBSA_OBC_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
154
155
156
157
158
159
160
161
162
163
164
               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;
165
               forceName                            = GBSA_OBC_SOFTCORE_FORCE;
Mark Friedrichs's avatar
Mark Friedrichs committed
166
167
168
169
170
171
172
173
174
175
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // GB/VI

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

        // GB/VI softcore

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

        if( log ){
196
            (void) fprintf( log, "Map: Force %d %s\n", i, forceName.c_str() );
197
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
     }
}

CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::~CudaFreeEnergyCalcNonbondedSoftcoreForceKernel() {
    if( log ){
        (void) fprintf( log, "CudaFreeEnergyCalcNonbondedSoftcoreForceKernel destructor called.\n" );
        (void) fflush( log );
    }
    delete gpuNonbondedSoftcore;
    delete gpuLJ14Softcore;
}

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

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

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

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

    if( log ){
        (void) fprintf( log, "%s called.\n", methodName.c_str() );
        (void) fflush( log );
    }

    // check forces and relevant parameters

    MapStringInt forceMap;
226
    getForceMap( system, forceMap, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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

    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();
    _gpuContext* gpu  = data.gpu;

    // 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.
    
    {
        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);
        float minSoftcoreLJLambda = 1.0e+20f;
        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 );
            if( minSoftcoreLJLambda > softcoreLJLambda ){
               minSoftcoreLJLambda = 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];
        system.getPeriodicBoxVectors(boxVectors[0], boxVectors[1], boxVectors[2]);
        gpuSetPeriodicBoxSize(gpu, static_cast<float>(boxVectors[0][0] ), static_cast<float>(boxVectors[1][1] ), static_cast<float>(boxVectors[2][2] ));
        CudaNonbondedMethod method = NO_CUTOFF;
        if (force.getNonbondedMethod() != NonbondedSoftcoreForce::NoCutoff) {
            gpuSetNonbondedCutoff(gpu, static_cast<float>(force.getCutoffDistance() ), force.getReactionFieldDielectric());
            method = CUTOFF;
        }
        if (force.getNonbondedMethod() == NonbondedSoftcoreForce::CutoffPeriodic) {
            method = PERIODIC;
        }
        if (force.getNonbondedMethod() == NonbondedSoftcoreForce::Ewald || force.getNonbondedMethod() == NonbondedSoftcoreForce::PME) {
            double ewaldErrorTol = force.getEwaldErrorTolerance();
            double alpha = (1.0/force.getCutoffDistance())*std::sqrt(-std::log(ewaldErrorTol));
            double mx = boxVectors[0][0]/force.getCutoffDistance();
            double my = boxVectors[1][1]/force.getCutoffDistance();
            double mz = boxVectors[2][2]/force.getCutoffDistance();
            double pi = 3.1415926535897932385;
            int kmaxx = (int)std::ceil(-(mx/pi)*std::log(ewaldErrorTol));
            int kmaxy = (int)std::ceil(-(my/pi)*std::log(ewaldErrorTol));
            int kmaxz = (int)std::ceil(-(mz/pi)*std::log(ewaldErrorTol));
            if (force.getNonbondedMethod() == NonbondedSoftcoreForce::Ewald) {
                if (kmaxx%2 == 0)
                    kmaxx++;
                if (kmaxy%2 == 0)
                    kmaxy++;
                if (kmaxz%2 == 0)
                    kmaxz++;
                gpuSetEwaldParameters(gpu, static_cast<float>( alpha ), kmaxx, kmaxy, kmaxz);
                method = EWALD;
            }
            else {
                int gridSizeX = kmaxx*3;
                int gridSizeY = kmaxy*3;
                int gridSizeZ = kmaxz*3;
                gridSizeX = ((gridSizeX+3)/4)*4;
                gridSizeY = ((gridSizeY+3)/4)*4;
                gridSizeZ = ((gridSizeZ+3)/4)*4;
                gpuSetPMEParameters(gpu, static_cast<float>( alpha ), gridSizeX, gridSizeY, gridSizeZ);
                method = PARTICLE_MESH_EWALD;
            }
        }
        data.nonbondedMethod = method;

        // setup parameters

        gpuNonbondedSoftcore = gpuSetNonbondedSoftcoreParameters(gpu, 138.935485f, particle, c6, c12, q,
                                                                 softcoreLJLambdaArray, symbol, exclusionList, method);

        // Compute the Ewald self energy.

        data.ewaldSelfEnergy = 0.0;
        if (force.getNonbondedMethod() == NonbondedSoftcoreForce::Ewald || force.getNonbondedMethod() == NonbondedSoftcoreForce::PME) {
            double selfEnergyScale = gpu->sim.epsfac*gpu->sim.alphaEwald/std::sqrt(PI);
                for (int i = 0; i < numParticles; i++)
                    data.ewaldSelfEnergy -= selfEnergyScale*q[i]*q[i];
        }
    }

    // Initialize 1-4 nonbonded interactions.
    
    {
        numExceptions = exceptions.size();
        std::vector<int> particle1(numExceptions);
        std::vector<int> particle2(numExceptions);
        std::vector<float> c6(numExceptions);
        std::vector<float> c12(numExceptions);
        std::vector<float> q1(numExceptions);
        std::vector<float> q2(numExceptions);
        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);
            c6[i]                    = static_cast<float>( (4*eps*pow(sig, 6.0)) );
            c12[i]                   = static_cast<float>( (4*eps*pow(sig, 12.0)) );
            q1[i]                    = static_cast<float>( charge );
            q2[i]                    = 1.0f;
            softcoreLJLambdaArray[i] = static_cast<float>( softcoreLJLambda );
        }
        gpuLJ14Softcore = gpuSetLJ14SoftcoreParameters(gpu, 138.935485f, 1.0f, particle1, particle2, c6, c12, q1, q2, softcoreLJLambdaArray);
    }
}

void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeForces(ContextImpl& context) {

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

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

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

    _gpuContext* gpu = data.gpu;
389
390
//    static int call  = 0;
//    call++;
Mark Friedrichs's avatar
Mark Friedrichs committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

    // write array, ... address's to board

    if( setSim == 0 ){
        setSim++;
        if( log ){
            (void) fprintf( log, "%s Obc=%d GB/VI=%d exceptions=%d\n",
                            methodName.c_str(), getIncludeGBSA(), getIncludeGBVI(), getNumExceptions() );
            (void) fflush( log );
        }
        SetCalculateLocalSoftcoreGpuSim( gpu );
        SetCalculateCDLJSoftcoreGpuSim( gpu );

        // flip strides (unsure if this is needed)

#if 0
        (void) fprintf( stderr, "flipping gpuLJ14Softcore\n" ); fflush( stderr );
        GpuLJ14Softcore* gpuLJ14Softcore = getGpuLJ14Softcore( );
        if( gpuLJ14Softcore ){
            gpuLJ14Softcore->flipStrides( gpu );
            if( log ){
                (void) fprintf( log, "CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeForces flipping LJ14\n" );
                (void) fflush( log );
            }
        }
#endif
417

Mark Friedrichs's avatar
Mark Friedrichs committed
418
419
420
421
422
423
424
425
426
427
428
    }
 
    // calculate nonbonded ixns here, only if implicit solvent is inactive

    if ( !getIncludeGBSA() && !getIncludeGBVI() ) {
        kCalculateCDLJSoftcoreForces(gpu);
    }
  
    // local LJ-14 forces

    kCalculateLocalSoftcoreForces(gpu);
429
//kPrintForces(gpu, "Post kCalculateLocalSoftcoreForces", call );
Mark Friedrichs's avatar
Mark Friedrichs committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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
487
488
//kReduceForces(gpu);
}

double CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::executeEnergy(ContextImpl& context) {
    executeForces(context);
    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;
}

bool CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::getIncludeSoftcore( void ) const {
    return bIncludeSoftcore;
}

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

void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::setIncludeSoftcore( bool inputIncludeSoftcore ){
    bIncludeSoftcore = inputIncludeSoftcore;
}

GpuLJ14Softcore* CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::getGpuLJ14Softcore( void ) const {
    return gpuLJ14Softcore;
}

void CudaFreeEnergyCalcNonbondedSoftcoreForceKernel::setGpuLJ14Softcore( GpuLJ14Softcore* inputGpuLJ14Softcore ){
    gpuLJ14Softcore = inputGpuLJ14Softcore;
}

CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::~CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel() {
    delete gpuObcGbsaSoftcore;
}

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

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

   //static const std::string methodName      = "CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::initialize";

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

    _gpuContext* gpu = data.gpu;

    MapStringInt forceMap;
489
    getForceMap( system, forceMap, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
490
491
492
493
494
495

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

    if( forceMap.find( NB_FORCE ) != forceMap.end() ){ 
        throw OpenMMException( "Mixing NonbondedForce and GBSAOBCSoftoreForce not allowed -- use NonbondedSoftcoreForce " );
    }
496
497
498
    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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

    int numParticles = system.getNumParticles();

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

    for (int i = 0; i < numParticles; i++) {
        double particleCharge, particleRadius, scalingFactor, particleNonPolarScalingFactor;
        force.getParticleParameters(i, particleCharge, particleRadius, scalingFactor, particleNonPolarScalingFactor);
        radius[i]                 = static_cast<float>( particleRadius);
        scale[i]                  = static_cast<float>( scalingFactor);
        charge[i]                 = static_cast<float>( particleCharge);
        nonPolarScalingFactors[i] = static_cast<float>( particleNonPolarScalingFactor);
    }
    gpuObcGbsaSoftcore = gpuSetObcSoftcoreParameters(gpu, static_cast<float>( force.getSoluteDielectric()),
                                                     static_cast<float>( force.getSolventDielectric()),
517
                                                     static_cast<float>( force.getNonPolarPrefactor()),
Mark Friedrichs's avatar
Mark Friedrichs committed
518
519
520
521
522
523
524
525
526
527
528
529
                                                     radius, scale, charge, nonPolarScalingFactors  );
}

void CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeForces(ContextImpl& context) {

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

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

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

    _gpuContext* gpu = data.gpu;
530

Mark Friedrichs's avatar
Mark Friedrichs committed
531
    int debug        = 1;
532
    static int call  = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

    // send address's of arrays, ... to device on first call
    // required since force/energy buffers not set when CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::initialize() was called

    if( setSim == 0 ){
       setSim++;
       SetCalculateObcGbsaSoftcoreBornSumSim( gpu );
       SetCalculateCDLJObcGbsaSoftcoreGpu1Sim( gpu );
       SetCalculateObcGbsaSoftcoreForces2Sim( gpu );
    }

    // required!!
    gpu->bRecalculateBornRadii = true;

    // calculate Born radii and first loop of Obc forces

    if( debug && log ){
550
551
552
553
554
        call++;
        if( log ){
            (void) fprintf( log, "\n%s: calling kCalculateCDLJObcGbsaSoftcoreForces1\n", methodName.c_str() );
            (void) fflush( log );
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
555
556
557
558
559
560
561
    }

    kClearBornForces(gpu);
    kCalculateObcGbsaSoftcoreBornSum(gpu);
    kReduceObcGbsaBornSum(gpu);
    kCalculateCDLJObcGbsaSoftcoreForces1(gpu);

562
//kPrintForces(gpu, "Post kCalculateCDLJObcGbsaSoftcoreForces1", call );
Mark Friedrichs's avatar
Mark Friedrichs committed
563
    if( debug && log ){
564
565
        (void) fprintf( log, "\n%s: calling kReduceObcGbsaBornForces\n", methodName.c_str()  );
        (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
566
567
568
569
    }

    // compute Born forces

570
    kReduceObcGbsaSoftcoreBornForces(gpu);
Mark Friedrichs's avatar
Mark Friedrichs committed
571
572

    if( debug && log ){
573
574
        (void) fprintf( log, "\n%s calling kCalculateObcGbsaForces2\n", methodName.c_str() );
        (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
    }

    // second loop of Obc GBSA forces

    kCalculateObcGbsaSoftcoreForces2(gpu);
}

double CudaFreeEnergyCalcGBSAOBCSoftcoreForceKernel::executeEnergy(ContextImpl& context) {
    executeForces( context );
	 return 0.0;
}

CudaFreeEnergyCalcGBVISoftcoreForceKernel::~CudaFreeEnergyCalcGBVISoftcoreForceKernel() {
    if( log ){
        (void) fprintf( log, "CudaFreeEnergyCalcGBVISoftcoreForceKernel destructor called -- freeing gpuGBVISoftcore.\n" );
        (void) fflush( log );
    }
    delete gpuGBVISoftcore;
}

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

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

   //static const std::string methodName      = "CudaFreeEnergyCalcGBVISoftcoreForceKernel::initialize";

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

    int numParticles = system.getNumParticles();
    _gpuContext* gpu = data.gpu;

    // check forces and relevant parameters

    MapStringInt forceMap;
609
    getForceMap( system, forceMap, log);
Mark Friedrichs's avatar
Mark Friedrichs committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674

    // 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);

    for (int i = 0; i < numParticles; i++) {
        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 );
    }

    // tanh not implemented

//    std::vector<float> tanhScaleFactors;
    std::vector<float> quinticSplineParameters;
    if( force.getBornRadiusScalingMethod() == GBVISoftcoreForce::Tanh ){
/*
        double alpha, beta, gamma;
        force.getTanhParameters( alpha, beta, gamma );
        tanhScaleFactors.resize( 3 );
        tanhScaleFactors[0] = static_cast<float>(alpha);
        tanhScaleFactors[1] = static_cast<float>(beta);
        tanhScaleFactors[2] = static_cast<float>(gamma);
*/
    } else if( force.getBornRadiusScalingMethod() == GBVISoftcoreForce::QuinticSpline ){

        // 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 ); 
        setQuinticScaling( 1 );
    }

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

    gpuGBVISoftcore = gpuSetGBVISoftcoreParameters(gpu, static_cast<float>( force.getSoluteDielectric() ), static_cast<float>( force.getSolventDielectric() ),
                                                    particle, radius, gammas, scaledRadii, bornRadiusScaleFactors, quinticSplineParameters);

}

void CudaFreeEnergyCalcGBVISoftcoreForceKernel::executeForces(ContextImpl& context) {

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

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

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

    _gpuContext* gpu = data.gpu;
    int debug        = 1;
675
    static int call  = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
676
677
678
679
680
681
682
683

    // send address's of arrays, ... to device on first call
    // required since force/energy buffers not set when CudaFreeEnergyCalcGBVISoftcoreForceKernel::initialize() was called

    if( setSim == 0 ){
       setSim++;
       SetCalculateGBVISoftcoreBornSumGpuSim( gpu );
       SetCalculateCDLJObcGbsaSoftcoreGpu1Sim( gpu );
684
       SetCalculateGBVISoftcoreForces2Sim( gpu );
Mark Friedrichs's avatar
Mark Friedrichs committed
685
686
    }

687
688
    // required?

Mark Friedrichs's avatar
Mark Friedrichs committed
689
690
691
692
693
    gpu->bRecalculateBornRadii = true; // fixed

    // calculate Born radii and first loop of GB/VI forces

    if( debug && log ){
694
695
696
697
698
699
        call++;
        if( log ){
            (void) fprintf( log, "\n%s: calling kCalculateCDLJObcGbsaSoftcoreForces1 & %s\n", methodName.c_str(),
                            getQuinticScaling() ? "kReduceGBVIBornSumQuinticScaling" : "kReduceGBVIBornSum" );
            (void) fflush( log );
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
700
701
702
703
704
705
706
707
708
709
710
711
    }

    kClearBornForces(gpu);
    kCalculateGBVISoftcoreBornSum(gpu);

    if( getQuinticScaling() ){
        kReduceGBVIBornSumQuinticScaling(gpu, gpuGBVISoftcore );
    } else {
        kReduceGBVIBornSum(gpu);
    }
    kCalculateCDLJObcGbsaSoftcoreForces1(gpu);

712
713
//kPrintForces(gpu, "Post kCalculateCDLJObcGbsaSoftcoreForces1", call );

Mark Friedrichs's avatar
Mark Friedrichs committed
714
    if( debug && log ){
715
        (void) fprintf( log, "\n%s: calling %s\n", methodName.c_str(),
Mark Friedrichs's avatar
Mark Friedrichs committed
716
                        getQuinticScaling() ? "kReduceGBVIBornForcesQuinticScaling" : "kReduceObcGbsaBornForces" );
717
        (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
718
719
720
721
722
723
724
725
726
727
728
729
730
    }

    // compute Born forces

    if( getQuinticScaling() ){
        kReduceGBVIBornForcesQuinticScaling(gpu);
    } else {
        gpu->bIncludeGBVI = true;
        kReduceObcGbsaBornForces(gpu);
        gpu->bIncludeGBVI = false;
    }

    if( debug && log ){
731
732
        (void) fprintf( log, "\n%s: calling kCalculateGBVIForces2\n", methodName.c_str() );
        (void) fflush( log );
Mark Friedrichs's avatar
Mark Friedrichs committed
733
734
735
736
    }

    // second loop of GB/VI forces

737
    kCalculateGBVISoftcoreForces2(gpu);
Mark Friedrichs's avatar
Mark Friedrichs committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
}

double CudaFreeEnergyCalcGBVISoftcoreForceKernel::executeEnergy(ContextImpl& context) {
    executeForces( context );
    return 0.0;
}

int CudaFreeEnergyCalcGBVISoftcoreForceKernel::getQuinticScaling( void ) const {

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

   //static const std::string methodName      = "CudaFreeEnergyCalcGBVISoftcoreForceKernel::getQuinticScaling";

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

    return quinticScaling;
}

void CudaFreeEnergyCalcGBVISoftcoreForceKernel::setQuinticScaling( int inputQuinticScaling) {

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

   //static const std::string methodName      = "CudaFreeEnergyCalcGBVISoftcoreForceKernel::setQuinticScaling";

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

    quinticScaling = inputQuinticScaling;
}