"vscode:/vscode.git/clone" did not exist on "0bb76b011ef4b9e27a657e5bc556543e49708f92"
ValidateOpenMM.cpp 37.9 KB
Newer Older
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
/* -------------------------------------------------------------------------- *
 *                                   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: Mark Friedrichs                                                   *
 * 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 "ValidateOpenMM.h" 

using namespace OpenMM;

// fixed force names
    
const std::string ValidateOpenMM::HARMONIC_BOND_FORCE             = "HarmonicBond";
const std::string ValidateOpenMM::HARMONIC_ANGLE_FORCE            = "HarmonicAngle";
const std::string ValidateOpenMM::PERIODIC_TORSION_FORCE          = "PeriodicTorsion";
const std::string ValidateOpenMM::RB_TORSION_FORCE                = "RbTorsion";

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

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

44
45
const std::string ValidateOpenMM::GBSA_OBC_FORCE                  = "GBSAOBC";
const std::string ValidateOpenMM::GBSA_OBC_SOFTCORE_FORCE         = "GBSAOBCSoftcore";
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

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

const std::string ValidateOpenMM::CM_MOTION_REMOVER               = "CMMotionRemover";
const std::string ValidateOpenMM::ANDERSEN_THERMOSTAT             = "AndersenThermostat";
const std::string ValidateOpenMM::CUSTOM_BOND_FORCE               = "CustomBond";
const std::string ValidateOpenMM::CUSTOM_EXTERNAL_FORCE           = "CustomExternal";
const std::string ValidateOpenMM::CUSTOM_NONBONDED_FORCE          = "CustomNonBonded";


ValidateOpenMM::ValidateOpenMM( void ) {

    _log                                          = NULL;

    // force dependencies 
    // these may need to specialized depending on platform since
    // currently apply to Cuda platform, but may not apply to OpenCL platform

    std::vector< std::string > nbVector;
    nbVector.push_back( NB_FORCE );
    _forceDependencies[GBSA_OBC_FORCE]            = nbVector;
    _forceDependencies[GBVI_FORCE]                = nbVector;

    std::vector< std::string > nbSoftcoreVector;
    nbSoftcoreVector.push_back( NB_SOFTCORE_FORCE );
    _forceDependencies[GBSA_OBC_SOFTCORE_FORCE]   = nbSoftcoreVector;
    _forceDependencies[GBVI_SOFTCORE_FORCE]       = nbSoftcoreVector;
    
}

ValidateOpenMM::~ValidateOpenMM() {
}

80

Peter Eastman's avatar
Peter Eastman committed
81
82
int ValidateOpenMM::isNanOrInfinity( double number ){
    return (number != number || number == std::numeric_limits<double>::infinity() || number == -std::numeric_limits<double>::infinity()) ? 1 : 0;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
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
237
238
239
240
241
242
243
}

FILE* ValidateOpenMM::getLog( void ) const {
    return _log;
}

void ValidateOpenMM::setLog(  FILE* log ){
    _log = log;
}

std::string ValidateOpenMM::getForceName(const Force& force) const { 

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

    //static const std::string methodName      = "ValidateForce::getForceName";

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

    std::string forceName  = "NA";

    // bond force

    try {
        const HarmonicBondForce& harmonicBondForce = dynamic_cast<const HarmonicBondForce&>(force);
        return HARMONIC_BOND_FORCE;
    } catch( std::bad_cast ){
    }

    // angle force

    try {
        const HarmonicAngleForce& harmonicAngleForce = dynamic_cast<const HarmonicAngleForce&>(force);
        return HARMONIC_ANGLE_FORCE;
    } catch( std::bad_cast ){
    }

    // periodic torsion force

    try {
        const PeriodicTorsionForce & periodicTorsionForce = dynamic_cast<const PeriodicTorsionForce&>(force);
       return PERIODIC_TORSION_FORCE;
    } catch( std::bad_cast ){
    }

    // RB torsion force

    try {
        const RBTorsionForce& rBTorsionForce = dynamic_cast<const RBTorsionForce&>(force);
       return RB_TORSION_FORCE;
    } catch( std::bad_cast ){
    }

    // nonbonded force

    try {
        const NonbondedForce& nbForce = dynamic_cast<const NonbondedForce&>(force);
        return  NB_FORCE;
    } catch( std::bad_cast ){
    }

    // GBSA OBC

    try {
       const GBSAOBCForce& obcForce       = dynamic_cast<const GBSAOBCForce&>(force);
       return GBSA_OBC_FORCE;
    } catch( std::bad_cast ){
    }

    // GB/VI

    try {
        const GBVIForce& obcForce  = dynamic_cast<const GBVIForce&>(force);
        return GBVI_FORCE;
    } catch( std::bad_cast ){
    }

#ifdef INCLUDE_FREE_ENERGY_PLUGIN

    // free energy plugin forces

    // nonbonded softcore

    try {
        const NonbondedSoftcoreForce& nbForce = dynamic_cast<const NonbondedSoftcoreForce&>(force);
        return NB_SOFTCORE_FORCE;
    } catch( std::bad_cast ){
    }

    // GBSA OBC softcore

    try {
        const GBSAOBCSoftcoreForce& obcForce = dynamic_cast<const GBSAOBCSoftcoreForce&>(force);
        return GBSA_OBC_SOFTCORE_FORCE;
    } catch( std::bad_cast ){
    }

    // GB/VI softcore

    try {
        const GBVISoftcoreForce& gbviForce = dynamic_cast<const GBVISoftcoreForce&>(force);
        return  GBVI_SOFTCORE_FORCE;
    } catch( std::bad_cast ){
    }

#endif

    // CMMotionRemover

    try {
        const CMMotionRemover& cMMotionRemover  = dynamic_cast<const CMMotionRemover&>(force);
        return CM_MOTION_REMOVER;
    } catch( std::bad_cast ){
    }

    // AndersenThermostat

    try {
        const AndersenThermostat & andersenThermostat = dynamic_cast<const AndersenThermostat&>(force);
        return ANDERSEN_THERMOSTAT;
    } catch( std::bad_cast ){
    }

    // CustomBondForce

    try {
        const CustomBondForce & customBondForce = dynamic_cast<const CustomBondForce&>(force);
        return CUSTOM_BOND_FORCE;
    } catch( std::bad_cast ){
    }

    // CustomExternalForce

    try {
        const CustomExternalForce& customExternalForce = dynamic_cast<const CustomExternalForce&>(force);
        return CUSTOM_EXTERNAL_FORCE;
    } catch( std::bad_cast ){
    }

    // CustomNonbondedForce

    try {
        const CustomNonbondedForce& customNonbondedForce = dynamic_cast<const CustomNonbondedForce&>(force);
        return CUSTOM_NONBONDED_FORCE;
    } catch( std::bad_cast ){
    }

    return forceName;
}

Force* ValidateOpenMM::copyForce(const Force& force) const { 

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

    //static const std::string methodName      = "ValidateForce::copyForce";

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

    // bond force

    try {
        const HarmonicBondForce& harmonicBondForce = dynamic_cast<const HarmonicBondForce&>(force);
244
        return new HarmonicBondForce( harmonicBondForce );
245
246
247
248
249
250
251
    } catch( std::bad_cast ){
    }

    // angle force

    try {
        const HarmonicAngleForce& harmonicAngleForce = dynamic_cast<const HarmonicAngleForce&>(force);
252
        return new HarmonicAngleForce( harmonicAngleForce );
253
254
255
256
257
258
259
    } catch( std::bad_cast ){
    }

    // periodic torsion force

    try {
        const PeriodicTorsionForce & periodicTorsionForce = dynamic_cast<const PeriodicTorsionForce&>(force);
260
        return new PeriodicTorsionForce( periodicTorsionForce );
261
262
263
264
265
266
267
    } catch( std::bad_cast ){
    }

    // RB torsion force

    try {
        const RBTorsionForce& rBTorsionForce = dynamic_cast<const RBTorsionForce&>(force);
268
        return new RBTorsionForce( rBTorsionForce );
269
270
271
272
273
274
275
    } catch( std::bad_cast ){
    }

    // nonbonded force

    try {
        const NonbondedForce& nbForce = dynamic_cast<const NonbondedForce&>(force);
276
        return new NonbondedForce( nbForce ); 
277
278
279
280
281
282
283
    } catch( std::bad_cast ){
    }

    // GBSA OBC

    try {
        const GBSAOBCForce& obcForce       = dynamic_cast<const GBSAOBCForce&>(force);
284
        return new GBSAOBCForce( obcForce );
285
286
287
288
289
290
291
    } catch( std::bad_cast ){
    }

    // GB/VI

    try {
        const GBVIForce& gbviForce  = dynamic_cast<const GBVIForce&>(force);
292
        return new GBVIForce( gbviForce );
293
294
295
296
297
298
299
300
301
302
    } catch( std::bad_cast ){
    }

#ifdef INCLUDE_FREE_ENERGY_PLUGIN

    // free energy plugin forces

    // nonbonded softcore
    try {
         const NonbondedSoftcoreForce& nbForce = dynamic_cast<const NonbondedSoftcoreForce&>(force);
303
         return new NonbondedSoftcoreForce( nbForce );
304
305
306
307
308
309
310
    } catch( std::bad_cast ){
    }

    // GBSA OBC softcore

    try {
        const GBSAOBCSoftcoreForce& obcForce = dynamic_cast<const GBSAOBCSoftcoreForce&>(force);
311
        return new GBSAOBCSoftcoreForce( obcForce );
312
313
314
315
316
317
318
    } catch( std::bad_cast ){
    }

    // GB/VI softcore

    try {
        const GBVISoftcoreForce& gbviForce = dynamic_cast<const GBVISoftcoreForce&>(force);
319
        return new GBVISoftcoreForce( gbviForce );
320
321
322
323
324
325
326
327
    } catch( std::bad_cast ){
    }
#endif

    // CMMotionRemover

    try {
        const CMMotionRemover& cMMotionRemover  = dynamic_cast<const CMMotionRemover&>(force);
328
        return new CMMotionRemover( cMMotionRemover );
329
330
331
332
333
334
335
    } catch( std::bad_cast ){
    }

    // AndersenThermostat

    try {
        const AndersenThermostat & andersenThermostat = dynamic_cast<const AndersenThermostat&>(force);
336
        return new AndersenThermostat( andersenThermostat );
337
338
339
340
341
342
343
    } catch( std::bad_cast ){
    }

    // CustomBondForce

    try {
        const CustomBondForce & customBondForce = dynamic_cast<const CustomBondForce&>(force);
344
        return new CustomBondForce( customBondForce );
345
346
347
348
349
350
351
    } catch( std::bad_cast ){
    }

    // CustomExternalForce

    try {
        const CustomExternalForce& customExternalForce = dynamic_cast<const CustomExternalForce&>(force);
352
        return new CustomExternalForce( customExternalForce );
353
354
355
356
357
358
359
    } catch( std::bad_cast ){
    }

    // CustomNonbondedForce

    try {
        const CustomNonbondedForce& customNonbondedForce = dynamic_cast<const CustomNonbondedForce&>(force);
360
        return new CustomNonbondedForce( customNonbondedForce );
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
389
    } catch( std::bad_cast ){
    }

    return NULL;
}

/** 
 * Get copy of input system, but leave out forces
 * 
 * @param systemToCopy   system to copy
 * @return system  w/o forces
 */
System* ValidateOpenMM::copySystemExcludingForces( const System& systemToCopy ) const {

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

    //static const std::string methodName      = "ValidateOpenMM::copySystemExcludingForces";

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

    // create new system and add masses and box dimensions

    System* system = new System();
    for(int ii = 0; ii < systemToCopy.getNumParticles(); ii++ ){
        double mass = systemToCopy.getParticleMass( ii );
        system->addParticle( mass );
    }   

    Vec3 a, b, c;
390
391
    systemToCopy.getDefaultPeriodicBoxVectors( a, b, c );
    system->setDefaultPeriodicBoxVectors( a, b, c );
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

    copyConstraints( systemToCopy, system );

    return system;
}

/**---------------------------------------------------------------------------------------

   Set the velocities/positions of context2 to those of context1

   @param context1                 context1 
   @param context2                 context2 

   @return 0

   --------------------------------------------------------------------------------------- */

void ValidateOpenMM::synchContexts( const Context& context1, Context& context2 ) const {

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

    //static const char* methodName  = "ValidateOpenMM::synchContexts: ";

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

    const State state                       = context1.getState(State::Positions | State::Velocities);
    const std::vector<Vec3>& positions      = state.getPositions();
    const std::vector<Vec3>& velocities     = state.getVelocities();
 
    context2.setPositions( positions );
    context2.setVelocities( velocities );
 
    return;
}

/**---------------------------------------------------------------------------------------

   Copy constraints

   @param systemToCopy         system whose constraints are to be copied
   @param system               system to add constraints to
   @param log                  log file pointer -- may be NULL

   --------------------------------------------------------------------------------------- */

void ValidateOpenMM::copyConstraints( const System& systemToCopy, System* system, FILE* log ) const {

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

    // static const std::string methodName      = "copyConstraints";
   
// ---------------------------------------------------------------------------------------

445
    for( int ii = 0; ii < systemToCopy.getNumConstraints(); ii++ ){
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
489
490
491
492
        int particle1, particle2;
        double distance;
        systemToCopy.getConstraintParameters( ii, particle1, particle2, distance ); 
        system->addConstraint( particle1, particle2, distance );
    }

}

/**---------------------------------------------------------------------------------------
    
   Get force dependencies
    
   @param forceName            force to check if there exist any dependencies
   @param returnVector         vector of forces the input force is dependent on (example: GBSAOBC force requires Nonbonded force)
    
   --------------------------------------------------------------------------------------- */
    
void ValidateOpenMM::getForceDependencies( std::string forceName, StringVector& returnVector ) const {

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

    // static const std::string methodName      = "getForceDependencies";
   
// ---------------------------------------------------------------------------------------

    StringStringVectorMapCI dependencyVector = _forceDependencies.find( forceName );
    if( dependencyVector != _forceDependencies.end() ){
        StringVector dependices = (*dependencyVector).second;
        for( unsigned int ii = 0; ii < dependices.size(); ii++ ){
            returnVector.push_back( dependices[ii] );
        }
    }
}

/**
 * Write masses and box dimensions to parameter file
 */

void ValidateOpenMM::writeMasses( FILE* filePtr, const System& system ) const {

    (void) fprintf( filePtr, "Masses %d\n", system.getNumParticles() );
    for(int ii = 0; ii < system.getNumParticles(); ii++ ){
       double mass = system.getParticleMass( ii );
       (void) fprintf( filePtr, "%8d %14.7e\n", ii, mass );
    }

    Vec3 a, b, c;
493
    system.getDefaultPeriodicBoxVectors( a, b, c);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
    (void) fprintf( filePtr, "Box %14.6f %14.6f %14.6f  %14.6f %14.6f %14.6f   %14.6f %14.6f %14.6f\n",
                    a[0], a[1], a[2], b[0], b[1], b[2], c[0], c[1], c[2] );
}

/**
 * Write constraints to parameter file
 */

void ValidateOpenMM::writeConstraints( FILE* filePtr, const System& system ) const {

    (void) fprintf( filePtr, "Constraints %d\n", system.getNumConstraints() );
    for(int ii = 0; ii < system.getNumConstraints(); ii++ ){
       int particle1, particle2;
       double distance;
       system.getConstraintParameters( ii, particle1, particle2, distance );
       (void) fprintf( filePtr, "%8d %8d %8d %14.7e\n", ii, particle1, particle2, distance );
    }
}

/**
 * Write harmonicBondForce parameters to file
 */

void ValidateOpenMM::writeHarmonicBondForce( FILE* filePtr, const HarmonicBondForce& harmonicBondForce ) const {

    (void) fprintf( filePtr, "HarmonicBondForce %d\n", harmonicBondForce.getNumBonds() );
    for(int ii = 0; ii < harmonicBondForce.getNumBonds(); ii++ ){
       int particle1, particle2;
       double length, k;
       harmonicBondForce.getBondParameters( ii, particle1, particle2, length, k );
       (void) fprintf( filePtr, "%8d %8d %8d %14.7e %14.7e\n", ii, particle1, particle2, length, k);
    }
}

/**
 * Write harmonicAngleForce parameters to file
 */

void ValidateOpenMM::writeHarmonicAngleForce( FILE* filePtr, const HarmonicAngleForce& harmonicAngleForce ) const {

    (void) fprintf( filePtr, "HarmonicAngleForce %d\n", harmonicAngleForce.getNumAngles() );
    for(int ii = 0; ii < harmonicAngleForce.getNumAngles(); ii++ ){
       int particle1, particle2, particle3;
       double angle, k;
       harmonicAngleForce.getAngleParameters( ii, particle1, particle2, particle3, angle, k );
       (void) fprintf( filePtr, "%8d %8d %8d %8d %14.7e %14.7e\n", ii, particle1, particle2, particle3, angle, k);
    }
}

/**
 * Write rbTorsionForce parameters to file
 */

void ValidateOpenMM::writeRbTorsionForce( FILE* filePtr, const RBTorsionForce& rbTorsionForce ) const {

    (void) fprintf( filePtr, "RBTorsionForce %d\n", rbTorsionForce.getNumTorsions() );
    for(int ii = 0; ii < rbTorsionForce.getNumTorsions(); ii++ ){
       int particle1, particle2, particle3, particle4;
       double c0, c1, c2, c3, c4, c5;
       rbTorsionForce.getTorsionParameters( ii, particle1, particle2, particle3, particle4, c0, c1, c2, c3, c4, c5 );
       (void) fprintf( filePtr, "%8d %8d %8d %8d %8d %14.7e %14.7e %14.7e %14.7e %14.7e %14.7e\n", ii, particle1, particle2, particle3, particle4, c0, c1, c2, c3, c4, c5 );
    }
}

/**
 * Write periodicTorsionForce parameters to file
 */

void ValidateOpenMM::writePeriodicTorsionForce( FILE* filePtr, const PeriodicTorsionForce& periodicTorsionForce ) const {

    (void) fprintf( filePtr, "PeriodicTorsionForce %d\n", periodicTorsionForce.getNumTorsions() );
    for(int ii = 0; ii < periodicTorsionForce.getNumTorsions(); ii++ ){
       int particle1, particle2, particle3, particle4, periodicity;
       double phase, k;
       periodicTorsionForce.getTorsionParameters( ii, particle1, particle2, particle3, particle4, periodicity, phase, k );
       (void) fprintf( filePtr, "%8d %8d %8d %8d %8d %8d %14.7e %14.7e\n", ii, particle1, particle2, particle3, particle4, periodicity, phase, k );
    }
}

/**
 * Write GBSAOBCForce parameters to file
 */

void ValidateOpenMM::writeGbsaObcForce( FILE* filePtr, const GBSAOBCForce& gbsaObcForce ) const {

    (void) fprintf( filePtr, "GBSAOBCForce %d\n", gbsaObcForce.getNumParticles() );
    for(int ii = 0; ii <  gbsaObcForce.getNumParticles(); ii++ ){
       double charge, radius, scalingFactor;
       gbsaObcForce.getParticleParameters( ii, charge, radius, scalingFactor );
       (void) fprintf( filePtr, "%8d  %14.7e %14.7e %14.7e\n", ii, charge, radius, scalingFactor );
    }
    (void) fprintf( filePtr, "SoluteDielectric %14.7e\n", gbsaObcForce.getSoluteDielectric() );
    (void) fprintf( filePtr, "SolventDielectric %14.7e\n", gbsaObcForce.getSolventDielectric() );
}

#ifdef INCLUDE_FREE_ENERGY_PLUGIN

/**
 * Write GBSAOBCSoftcoreForce parameters to file
 */

void ValidateOpenMM::writeGbsaObcSoftcoreForce( FILE* filePtr, const GBSAOBCSoftcoreForce& gbsaObcForce ) const {

    (void) fprintf( filePtr, "GBSAOBCSoftcoreForce %d\n", gbsaObcForce.getNumParticles() );
    for(int ii = 0; ii <  gbsaObcForce.getNumParticles(); ii++ ){
       double charge, radius, scalingFactor;
       double nonPolarScalingFactor;
       gbsaObcForce.getParticleParameters( ii, charge, radius, scalingFactor, nonPolarScalingFactor );
       (void) fprintf( filePtr, "%8d  %14.7e %14.7e %14.7e %14.7e\n", ii, charge, radius, scalingFactor, nonPolarScalingFactor );
    }
    (void) fprintf( filePtr, "SoluteDielectric %14.7e\n", gbsaObcForce.getSoluteDielectric() );
    (void) fprintf( filePtr, "SolventDielectric %14.7e\n", gbsaObcForce.getSolventDielectric() );
}

#endif

/**
 * Write GBSA GB/VI Force parameters to file
 */

void ValidateOpenMM::writeGBVIForce( FILE* filePtr, const GBVIForce& gbviForce ) const {

    (void) fprintf( filePtr, "GBVIForce %d\n", gbviForce.getNumParticles() );
    for(int ii = 0; ii <  gbviForce.getNumParticles(); ii++ ){
       double charge, radius, gamma;
       gbviForce.getParticleParameters( ii, charge, radius, gamma );
       (void) fprintf( filePtr, "%8d  %14.7e %14.7e %14.7e\n", ii, charge, radius, gamma );
    }

    (void) fprintf( filePtr, "GBVIBonds  %d\n", gbviForce.getNumBonds() );
    for(int ii = 0; ii <  gbviForce.getNumBonds(); ii++ ){
       int atomI, atomJ;
       double bondLength;
       gbviForce.getBondParameters( ii, atomI, atomJ, bondLength );
       (void) fprintf( filePtr, "%8d %8d %8d %14.7e\n", ii, atomI, atomJ, bondLength );
    }
    (void) fprintf( filePtr, "SoluteDielectric %14.7e\n", gbviForce.getSoluteDielectric() );
    (void) fprintf( filePtr, "SolventDielectric %14.7e\n", gbviForce.getSolventDielectric() );

}

#ifdef INCLUDE_FREE_ENERGY_PLUGIN

/**
 * Write GBSA GB/VI softcore force parameters to file
 */

void ValidateOpenMM::writeGBVISoftcoreForce( FILE* filePtr, const GBVISoftcoreForce& gbviSoftcoreForce ) const {

    (void) fprintf( filePtr, "GBVISoftcoreForce %d\n", gbviSoftcoreForce.getNumParticles() );
    for(int ii = 0; ii <  gbviSoftcoreForce.getNumParticles(); ii++ ){
       double charge, radius, gamma;
       double nonPolarScalingFactor;
       gbviSoftcoreForce.getParticleParameters( ii, charge, radius, gamma, nonPolarScalingFactor );
       (void) fprintf( filePtr, "%8d  %14.7e %14.7e %14.7e %14.7e\n", ii, charge, radius, gamma, nonPolarScalingFactor );
    }

    (void) fprintf( filePtr, "GBVISoftcoreBonds  %d\n", gbviSoftcoreForce.getNumBonds() );
    for(int ii = 0; ii <  gbviSoftcoreForce.getNumBonds(); ii++ ){
       int atomI, atomJ;
       double bondLength;
       gbviSoftcoreForce.getBondParameters( ii, atomI, atomJ, bondLength );
       (void) fprintf( filePtr, "%8d %8d %8d %14.7e\n", ii, atomI, atomJ, bondLength );
    }
    (void) fprintf( filePtr, "SoluteDielectric %14.7e\n", gbviSoftcoreForce.getSoluteDielectric() );
    (void) fprintf( filePtr, "SolventDielectric %14.7e\n", gbviSoftcoreForce.getSolventDielectric() );

    (void) fprintf( filePtr, "BornRadiusScalingMethod %d\n", gbviSoftcoreForce.getBornRadiusScalingMethod() );
    (void) fprintf( filePtr, "QuinticLowerLimitFactor %14.7e\n", gbviSoftcoreForce.getQuinticLowerLimitFactor() );
    (void) fprintf( filePtr, "QuinticUpperBornRadiusLimit %14.7e\n", gbviSoftcoreForce.getQuinticUpperBornRadiusLimit() );
}

#endif

/**
 * Write coordinates to file
 */

void ValidateOpenMM::writeVec3( FILE* filePtr, const std::vector<Vec3>& vect3Array ) const {

    for( unsigned int ii = 0; ii < vect3Array.size(); ii++ ){
       (void) fprintf( filePtr, "%8d  %14.7e %14.7e %14.7e\n", ii, 
                       vect3Array[ii][0], vect3Array[ii][1], vect3Array[ii][2] );
    }
}

/**
 * Write context info to file
 */

void ValidateOpenMM::writeContext( FILE* filePtr, const Context& context ) const {

    State state                  = context.getState( State::Positions | State::Velocities | State::Forces | State::Energy );
    std::vector<Vec3> positions  = state.getPositions();
    std::vector<Vec3> velocities = state.getVelocities();
    std::vector<Vec3> forces     = state.getForces();

691
    (void) fprintf( filePtr, "Positions %zu\n", positions.size() );
692
693
    writeVec3( filePtr, positions );

694
    (void) fprintf( filePtr, "Velocities %zu\n", velocities.size() );
695
696
    writeVec3( filePtr, velocities );

697
    (void) fprintf( filePtr, "Forces %zu\n", forces.size() );
698
699
700
701
702
703
704
705
706
707
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
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
    writeVec3( filePtr, forces );

    (void) fprintf( filePtr, "KineticEnergy %14.7e\n", state.getKineticEnergy() );
    (void) fprintf( filePtr, "PotentialEnergy %14.7e\n", state.getPotentialEnergy() );
}

/**
 * Write nonbonded parameters to file
 */

void ValidateOpenMM::writeNonbondedForce( FILE* filePtr, const NonbondedForce & nonbondedForce ) const {

    // charge and vdw parameters

    (void) fprintf( filePtr, "NonbondedForce %d\n", nonbondedForce.getNumParticles() );
    for(int ii = 0; ii < nonbondedForce.getNumParticles(); ii++ ){
       double charge, sigma, epsilon;
       nonbondedForce.getParticleParameters( ii, charge, sigma, epsilon );
       (void) fprintf( filePtr, "%8d %14.7e %14.7e %14.7e\n", ii, charge, sigma, epsilon );
    }

    // cutoff, dielectric, Ewald tolerance

    (void) fprintf( filePtr, "CutoffDistance %14.7e\n", nonbondedForce.getCutoffDistance() );
    (void) fprintf( filePtr, "RFDielectric %14.7e\n", nonbondedForce.getReactionFieldDielectric() );
    (void) fprintf( filePtr, "EwaldRTolerance %14.7e\n", nonbondedForce.getEwaldErrorTolerance() );

    // cutoff mode

    std::string nonbondedForceMethod;
    switch( nonbondedForce.getNonbondedMethod() ){
        case NonbondedForce::NoCutoff:
            nonbondedForceMethod = "NoCutoff";
            break;
        case NonbondedForce::CutoffNonPeriodic:
            nonbondedForceMethod = "CutoffNonPeriodic";
            break;
        case NonbondedForce::CutoffPeriodic:
            nonbondedForceMethod = "CutoffPeriodic";
            break;
        case NonbondedForce::Ewald:
            nonbondedForceMethod = "Ewald";
            break;
        case NonbondedForce::PME:
            nonbondedForceMethod = "PME";
            break;
        default:
            nonbondedForceMethod = "Unknown";
    }
    (void) fprintf( filePtr, "NonbondedForceMethod %s\n", nonbondedForceMethod.c_str() );

    (void) fprintf( filePtr, "NonbondedForceExceptions %d\n", nonbondedForce.getNumExceptions() );
    for(int ii = 0; ii < nonbondedForce.getNumExceptions(); ii++ ){
       int particle1, particle2;
       double chargeProd, sigma, epsilon;
       nonbondedForce.getExceptionParameters( ii, particle1, particle2, chargeProd, sigma, epsilon );
       (void) fprintf( filePtr, "%8d %8d %8d %14.7e %14.7e %14.7e\n", ii, particle1, particle2, chargeProd, sigma, epsilon );
    }

}

#ifdef INCLUDE_FREE_ENERGY_PLUGIN

/**
 * Write nonbonded softcore parameters to file
 */

void ValidateOpenMM::writeNonbondedSoftcoreForce( FILE* filePtr, const NonbondedSoftcoreForce & nonbondedSoftcoreForce ) const {

    (void) fprintf( filePtr, "NonbondedSoftcoreForce %d\n", nonbondedSoftcoreForce.getNumParticles() );
    for(int ii = 0; ii < nonbondedSoftcoreForce.getNumParticles(); ii++ ){
       double charge, sigma, epsilon, softCoreLJ;
       nonbondedSoftcoreForce.getParticleParameters( ii, charge, sigma, epsilon, softCoreLJ );
       (void) fprintf( filePtr, "%8d %14.7e %14.7e %14.7e %14.7e\n", ii, charge, sigma, epsilon, softCoreLJ );
    }

    (void) fprintf( filePtr, "CutoffDistance %14.7e\n", nonbondedSoftcoreForce.getCutoffDistance() );
    (void) fprintf( filePtr, "RFDielectric %14.7e\n", nonbondedSoftcoreForce.getReactionFieldDielectric() );
    (void) fprintf( filePtr, "EwaldRTolerance %14.7e\n", nonbondedSoftcoreForce.getEwaldErrorTolerance() );

    std::string nonbondedSoftcoreForceMethod;
    switch( nonbondedSoftcoreForce.getNonbondedMethod() ){
        case NonbondedSoftcoreForce::NoCutoff:
            nonbondedSoftcoreForceMethod = "NoCutoff";
            break;
/*
        case NonbondedSoftcoreForce::CutoffNonPeriodic:
            nonbondedSoftcoreForceMethod = "CutoffNonPeriodic";
            break;
        case NonbondedSoftcoreForce::CutoffPeriodic:
            nonbondedSoftcoreForceMethod = "CutoffPeriodic";
            break;
        case NonbondedSoftcoreForce::Ewald:
            nonbondedSoftcoreForceMethod = "Ewald";
            break;
        case NonbondedSoftcoreForce::PME:
            nonbondedSoftcoreForceMethod = "PME";
            break;
*/
        default:
            nonbondedSoftcoreForceMethod = "Unknown";
    }
    (void) fprintf( filePtr, "NonbondedSoftcoreForceMethod %s\n", nonbondedSoftcoreForceMethod.c_str() );

    (void) fprintf( filePtr, "NonbondedSoftcoreForceExceptions %d\n", nonbondedSoftcoreForce.getNumExceptions() );
    for(int ii = 0; ii < nonbondedSoftcoreForce.getNumExceptions(); ii++ ){
       int particle1, particle2;
       double chargeProd, sigma, epsilon, softCoreLJ;
       nonbondedSoftcoreForce.getExceptionParameters( ii, particle1, particle2, chargeProd, sigma, epsilon, softCoreLJ );
       (void) fprintf( filePtr, "%8d %8d %8d %14.7e %14.7e %14.7e %14.7e\n", ii, particle1, particle2, chargeProd, sigma, epsilon, softCoreLJ );
    }

    return;
}
#endif

void ValidateOpenMM::writeIntegrator( FILE* filePtr, const Integrator& integrator ) const {

    // LangevinIntegrator
 
    try {
        const LangevinIntegrator langevinIntegrator = dynamic_cast<const LangevinIntegrator&>(integrator);
        (void) fprintf( filePtr, "Integrator LangevinIntegrator\n" );
        (void) fprintf( filePtr, "StepSize %14.7e\n", langevinIntegrator.getStepSize() );
        (void) fprintf( filePtr, "ConstraintTolerance %14.7e\n", langevinIntegrator.getConstraintTolerance() );
        (void) fprintf( filePtr, "Temperature %14.7e\n", langevinIntegrator.getTemperature() );
        (void) fprintf( filePtr, "Friction %14.7e\n", langevinIntegrator.getFriction() );
        (void) fprintf( filePtr, "RandomNumberSeed %d\n", langevinIntegrator.getRandomNumberSeed() );
        return;
    } catch( std::bad_cast ){
    }
 
    // VariableLangevinIntegrator
 
    try {
        const VariableLangevinIntegrator& langevinIntegrator = dynamic_cast<const VariableLangevinIntegrator&>(integrator);
        (void) fprintf( filePtr, "Integrator VariableLangevinIntegrator\n" );
        (void) fprintf( filePtr, "StepSize %14.7e\n", langevinIntegrator.getStepSize() );
        (void) fprintf( filePtr, "ConstraintTolerance %14.7e\n", langevinIntegrator.getConstraintTolerance() );
        (void) fprintf( filePtr, "Temperature %14.7e\n", langevinIntegrator.getTemperature() );
        (void) fprintf( filePtr, "Friction %14.7e\n", langevinIntegrator.getFriction() );
        (void) fprintf( filePtr, "RandomNumberSeed %d\n", langevinIntegrator.getRandomNumberSeed() );
        (void) fprintf( filePtr, "ErrorTolerance %14.7e\n", langevinIntegrator.getErrorTolerance() );
        return;
    } catch( std::bad_cast ){
    }
 
    // VerletIntegrator
 
    try {
        const VerletIntegrator& verletIntegrator = dynamic_cast<const VerletIntegrator&>(integrator);
        (void) fprintf( filePtr, "Integrator VerletIntegrator\n" );
        (void) fprintf( filePtr, "StepSize %14.7e\n", verletIntegrator.getStepSize() );
        (void) fprintf( filePtr, "ConstraintTolerance %14.7e\n", verletIntegrator.getConstraintTolerance() );
        return;
    } catch( std::bad_cast ){
    }
     
    // VariableVerletIntegrator
 
    try {
        const VariableVerletIntegrator & variableVerletIntegrator = dynamic_cast<const VariableVerletIntegrator&>(integrator);
        (void) fprintf( filePtr, "Integrator VariableVerletIntegrator\n" );
        (void) fprintf( filePtr, "StepSize %14.7e\n", variableVerletIntegrator.getStepSize() );
        (void) fprintf( filePtr, "ConstraintTolerance %14.7e\n", variableVerletIntegrator.getConstraintTolerance() );
        (void) fprintf( filePtr, "ErrorTolerance %14.7e\n", variableVerletIntegrator.getErrorTolerance() );
        return;
    } catch( std::bad_cast ){
    }
     
    // BrownianIntegrator
 
    try {
        const BrownianIntegrator& brownianIntegrator = dynamic_cast<const BrownianIntegrator&>(integrator);
        (void) fprintf( filePtr, "Integrator BrownianIntegrator\n" );
        (void) fprintf( filePtr, "StepSize %14.7e\n", brownianIntegrator.getStepSize() );
        (void) fprintf( filePtr, "ConstraintTolerance %14.7e\n", brownianIntegrator.getConstraintTolerance() );
        (void) fprintf( filePtr, "Temperature %14.7e\n", brownianIntegrator.getTemperature() );
        (void) fprintf( filePtr, "Friction %14.7e\n", brownianIntegrator.getFriction() );
        (void) fprintf( filePtr, "RandomNumberSeed %d\n", brownianIntegrator.getRandomNumberSeed() );
        return;
    } catch( std::bad_cast ){
    }
     
    if( getLog() ){
       (void) fprintf( getLog(), "Integrator not recognized." );
       (void) fflush( getLog() );
    }

    return;
}

/**
891
892
893
 * Write parameter file: Custom forces not implemented
 * Mesage is sent to stderr if a force is not recognized
 *
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
 */

void ValidateOpenMM::writeParameterFile( const Context& context, const std::string& parameterFileName ) const {

    // open file
 
    FILE* filePtr = fopen( parameterFileName.c_str(), "w" );

    const System& system          = context.getSystem();
    const Integrator& integrator  = context.getIntegrator();

    // (void) fprintf( filePtr, "Version %s\n", versionString.c_str() );
    (void) fprintf( filePtr, "Particles %8d\n", system.getNumParticles() );
    writeMasses( filePtr, system );
    (void) fprintf( filePtr, "NumberOfForces    %8d\n", system.getNumForces() );

    // print active forces and relevant parameters

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

        int hit                       = 0;
        const Force& force            = system.getForce(i);

        // bond

        if( !hit ){

            try {
               const HarmonicBondForce& harmonicBondForce = dynamic_cast<const HarmonicBondForce&>(force);
               writeHarmonicBondForce( filePtr, harmonicBondForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
    
        // angle

        if( !hit ){
    
            try {
               const HarmonicAngleForce& harmonicAngleForce = dynamic_cast<const HarmonicAngleForce&>(force);
               writeHarmonicAngleForce( filePtr, harmonicAngleForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }

        // PeriodicTorsionForce
    
        if( !hit ){
    
            try {
               const PeriodicTorsionForce & periodicTorsionForce = dynamic_cast<const PeriodicTorsionForce&>(force);
               writePeriodicTorsionForce( filePtr, periodicTorsionForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
    
        // RBTorsionForce
    
        if( !hit ){
            try {
               const RBTorsionForce& rBTorsionForce = dynamic_cast<const RBTorsionForce&>(force);
               writeRbTorsionForce(  filePtr, rBTorsionForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
    
        // nonbonded
    
        if( !hit ){
            try {
               const NonbondedForce& nbForce = dynamic_cast<const NonbondedForce&>(force);
               writeNonbondedForce( filePtr, nbForce );
               hit++;
            } catch( std::bad_cast ){
            }
        } 

        // nonbonded softcore
    
#ifdef INCLUDE_FREE_ENERGY_PLUGIN
        if( !hit ){
            try {
               const NonbondedSoftcoreForce& nbForce = dynamic_cast<const NonbondedSoftcoreForce&>(force);
               writeNonbondedSoftcoreForce( filePtr, nbForce );
               hit++;
            } catch( std::bad_cast ){
            }
        } 
#endif

        // GBSA OBC
    
        if( !hit ){
            try {
               const GBSAOBCForce& obcForce = dynamic_cast<const GBSAOBCForce&>(force);
               writeGbsaObcForce(  filePtr, obcForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
    

        // GBSA OBC softcore
    
#ifdef INCLUDE_FREE_ENERGY_PLUGIN
        if( !hit ){
            try {
               const GBSAOBCSoftcoreForce& obcForce = dynamic_cast<const GBSAOBCSoftcoreForce&>(force);
               writeGbsaObcSoftcoreForce(  filePtr, obcForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
#endif
    
        // GB/VI
    
        if( !hit ){
            try {
               const GBVIForce& obcForce = dynamic_cast<const GBVIForce&>(force);
               writeGBVIForce(  filePtr, obcForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
    
        // GB/VI softcore
    
#ifdef INCLUDE_FREE_ENERGY_PLUGIN
        if( !hit ){
            try {
               const GBVISoftcoreForce& obcForce = dynamic_cast<const GBVISoftcoreForce&>(force);
               writeGBVISoftcoreForce(  filePtr, obcForce );
               hit++;
            } catch( std::bad_cast ){
            }
        }
#endif
    
        // COM

        if( !hit ){
    
            try {
               const CMMotionRemover& cMMotionRemover = dynamic_cast<const CMMotionRemover&>(force);
               (void) fprintf( filePtr, "CMMotionRemover %d\n", cMMotionRemover.getFrequency() );
               hit++;
            } catch( std::bad_cast ){
            }
        }

        if( !hit ){
1050
1051
1052
1053
           char buffer[1024];
           (void) sprintf( buffer, "   %2d force not recognized.\n", i );
           (void) fprintf( stderr, "%s\n", buffer );
//           throw OpenMMException( buffer );
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
        }

    }

    // constraints

    writeConstraints( filePtr, system );

    // context

    writeContext( filePtr, context );

    // integrator 

    writeIntegrator( filePtr, integrator );

    // close file

    (void) fclose( filePtr );
}