HelloEthane.cpp 13.4 KB
Newer Older
Michael Sherman's avatar
Michael Sherman committed
1
2
3
4
5
/* -----------------------------------------------------------------------------
 *                    OpenMM(tm) HelloEthane example (May 2009)
 * -----------------------------------------------------------------------------
 * This is a complete, self-contained "hello world" example demonstrating 
 * GPU-accelerated simulation of a system with both bonded and nonbonded forces, 
6
7
8
 * using ethane (H3-C-C-H3) in a vacuum as an example. A multi-frame PDB file is 
 * written to stdout which can be read by VMD or other visualization tool to 
 * produce an animation of the resulting trajectory.
Michael Sherman's avatar
Michael Sherman committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * Pay particular attention to the handling of units in this example. Incorrect
 * handling of units is a very common error; this example shows how you can
 * continue to work with Amber-style units of Angstroms and kCals while correctly
 * communicating with OpenMM in nanometers and kJoules.
 * -------------------------------------------------------------------------- */

// Suppress irrelevant warnings from Microsoft's compiler.
#ifdef _MSC_VER
    #pragma warning(disable:4996)   // sprintf is unsafe 
    #pragma warning(disable:4251)   // no dll interface for some classes
#endif

#include "OpenMM.h"

#include <iostream>
#include <string>
#include <vector>
#include <utility>

using namespace OpenMM;

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
64
// -----------------------------------------------------------------------------
//                   MODELING AND SIMULATION PARAMETERS
// -----------------------------------------------------------------------------
const bool   UseConstraints      = false;   // Should we constrain C-H bonds?
const double StepSizeInFs        = 2;       // integration step size (fs)
const double ReportIntervalInFs  = 10;      // how often to generate PDB frame (fs)
const double SimulationTimeInPs  = 100;     // total simulation time (ps)

static void simulateEthane();
static void writePDB(const OpenMMContext&); // PDB file writer; see below.


// -----------------------------------------------------------------------------
//                                MAIN PROGRAM
// -----------------------------------------------------------------------------
int main() {
    // ALWAYS enclose all OpenMM calls with a try/catch block to make sure that
    // usage and runtime errors are caught and reported.
    try {
        // Load all available OpenMM plugins from their default location.
        Platform::loadPluginsFromDirectory(Platform::getDefaultPluginsDirectory());

        simulateEthane();

        return 0; // Normal return from main.
    }

    // Catch and report usage and runtime errors detected by OpenMM and fail.
    catch(const std::exception& e) {
        printf("EXCEPTION: %s\n", e.what());
        return 1;
    }
}

Michael Sherman's avatar
Michael Sherman committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

// -----------------------------------------------------------------------------
//              FORCE FIELD DATA
// -----------------------------------------------------------------------------
// These data structures are not part of OpenMM; they are a model of the kinds
// of data structures an MD code uses to hold a set of force field parameters.
// For this example we're using a tiny subset of the Amber99 force field.
// We want to keep the data in the original unit system to avoid conversion
// bugs; this requires conversion on the way in and out of OpenMM.

// Amber reduces nonbonded forces between 1-4 bonded atoms.
const double Coulomb14Scale      = 0.5;
const double LennardJones14Scale = 0.5;

struct AtomType {
    double mass, charge, vdwRadiusInAngstroms, vdwEnergyInKcal;
} atomType[] = {/*0 H*/ 1.008, 0.0605, 1.4870, 0.0157,
                /*1 C*/12.011, -.1815, 1.9080, 0.1094};
const int H = 0, C = 1;

struct BondType {
    double nominalLengthInAngstroms, stiffnessInKcalPerAngstrom2;
    bool   canConstrain;
} bondType[] = {/*0 CC*/1.526, 310., false,
                /*1 CH*/1.09 , 340., true};
const int CC = 0, CH = 1;

struct AngleType {
    double nominalAngleInDegrees, stiffnessInKcalPerRadian2;
} angleType[] = {/*0 HCC*/109.5, 50.,
                 /*1 HCH*/109.5, 35.};
const int HCC = 0, HCH = 1;

struct TorsionType {
    int    periodicity;
    double phaseInDegrees, amplitudeInKcal;
} torsionType[] = {/*0 HCCH*/3, 0., 0.150};
const int HCCH = 0;

// -----------------------------------------------------------------------------
//              MOLECULE DATA
// -----------------------------------------------------------------------------
// Now describe an ethane molecule by listing its atoms, bonds, angles, and 
// torsions. We'll provide a default configuration which centers the molecule 
// at (0,0,0) with the C-C bond along the x axis.

// Use this as an "end of list" marker so that we do not have to count; let the
// computer do that!
const int EndOfList=-1;

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
struct AtomInfo
{   int type; char pdb[5]; Vec3 initPosInAngstroms;} atoms[] = 
    {/*0*/C,    " C1 ",    Vec3( -.7605,  0,   0 ),
     /*1*/C,    " C2 ",    Vec3(  .7605,  0,   0 ),
     /*2*/H,    "1H1 ",    Vec3(-1.135, 1.03,  0 ), // bonded to C1
     /*3*/H,    "2H1 ",    Vec3(-1.135, -.51, .89),
     /*4*/H,    "3H1 ",    Vec3(-1.135, -.51,-.89),
     /*5*/H,    "1H2 ",    Vec3( 1.135, 1.03,  0 ), // bonded to C2
     /*6*/H,    "2H2 ",    Vec3( 1.135, -.51, .89),
     /*7*/H,    "3H2 ",    Vec3( 1.135, -.51,-.89),
     EndOfList};

struct {int type; int atoms[2];} bonds[] = 
    {CC,0,1,
     CH,0,2,CH,0,3,CH,0,4,          // C1 methyl
     CH,1,5,CH,1,6,CH,1,7,          // C2 methyl     
     EndOfList};
struct {int type; int atoms[3];} angles[] = 
    {HCC,2,0,1,HCC,3,0,1,HCC,4,0,1, // C1 methyl
     HCH,2,0,3,HCH,2,0,4,HCH,3,0,4,
     HCC,5,1,0,HCC,6,1,0,HCC,7,1,0, // C2 methyl
     HCH,5,1,6,HCH,5,1,7,HCH,6,1,7,             
     EndOfList};
struct {int type; int atoms[4];} torsions[] = 
    {HCCH,2,0,1,5,HCCH,2,0,1,6,HCCH,2,0,1,7,
     HCCH,3,0,1,5,HCCH,3,0,1,6,HCCH,3,0,1,7,
     HCCH,4,0,1,5,HCCH,4,0,1,6,HCCH,4,0,1,7,    
     EndOfList};



// Add missing scalar product operators for Vec3.
Vec3 operator*(const Vec3& v, double r) {return Vec3(v[0]*r, v[1]*r, v[2]*r);}
Vec3 operator*(double r, const Vec3& v) {return Vec3(r*v[0], r*v[1], r*v[2]);}
// This is the conversion factor that takes you from a van der Waals radius
// (defined as 1/2 the minimum energy separation) to the related Lennard Jones 
// "sigma" parameter (defined as the zero crossing separation).
static const double SigmaPerVdwRadius = 2*std::pow(2., -1./6.);
Michael Sherman's avatar
Michael Sherman committed
153
154

// -----------------------------------------------------------------------------
155
//                            ETHANE SIMULATION
Michael Sherman's avatar
Michael Sherman committed
156
// -----------------------------------------------------------------------------
157
static void simulateEthane() {
Michael Sherman's avatar
Michael Sherman committed
158
159
    // -------------------------------------------------------------------------
    // Create a System and Force objects within the System. Retain a reference
160
161
    // to each force object so we can fill in the forces. Note: OpenMM owns
    // the objects and will take care of deleting them; don't do it yourself!
Michael Sherman's avatar
Michael Sherman committed
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
    // -------------------------------------------------------------------------
    System system;
    NonbondedForce&         nonbond     = *new NonbondedForce();
    HarmonicBondForce&      bondStretch = *new HarmonicBondForce();
    HarmonicAngleForce&     bondBend    = *new HarmonicAngleForce();
    PeriodicTorsionForce&   bondTorsion = *new PeriodicTorsionForce();
    system.addForce(&nonbond);
    system.addForce(&bondStretch);
    system.addForce(&bondBend);
    system.addForce(&bondTorsion);

    // -------------------------------------------------------------------------
    // Specify the atoms and their properties:
    //  (1) System needs to know the masses.
    //  (2) NonbondedForce needs charges,van der Waals properties (in MD units!).
    //  (3) Collect default positions for initializing the simulation later.
    // -------------------------------------------------------------------------
    std::vector<Vec3> initialPositions;
    for (int n=0; atoms[n].type != EndOfList; ++n) {
        const AtomType& atype = atomType[atoms[n].type];
        system.addParticle(atype.mass);
        nonbond.addParticle(atype.charge,
                            atype.vdwRadiusInAngstroms * NmPerAngstrom * SigmaPerVdwRadius,
                            atype.vdwEnergyInKcal      * KJPerKcal);
        initialPositions.push_back(atoms[n].initPosInAngstroms * NmPerAngstrom);
    }

    // -------------------------------------------------------------------------
    // Process the bonds:
    //  (1) HarmonicBondForce needs bond stretch parameters (in MD units!).
    //  (2) If we're using constraints, tell System about constrainable bonds.
    //  (3) Create a list of bonds for generating nonbond exclusions.
    // -------------------------------------------------------------------------
    std::vector< std::pair<int,int> > bondPairs;
    for (int i=0; bonds[i].type != EndOfList; ++i) {
197
198
        const int*      atom = bonds[i].atoms;
        const BondType& bond = bondType[bonds[i].type];
Michael Sherman's avatar
Michael Sherman committed
199
200
201
202

        // Note factor of 2 for stiffness below because Amber specifies the constant
        // as it is used in the harmonic energy term kx^2 with force 2kx; OpenMM wants 
        // it as used in the force term kx, with energy kx^2/2.
203
        bondStretch.addBond(atom[0], atom[1],
Michael Sherman's avatar
Michael Sherman committed
204
205
206
                            bond.nominalLengthInAngstroms    * NmPerAngstrom,
                            bond.stiffnessInKcalPerAngstrom2 * 2 * KJPerKcal * AngstromsPerNm * AngstromsPerNm);
        if (UseConstraints && bond.canConstrain)
207
            system.addConstraint(atom[0], atom[1],
Michael Sherman's avatar
Michael Sherman committed
208
                                 bond.nominalLengthInAngstroms * NmPerAngstrom);
209
        bondPairs.push_back(std::make_pair(atom[0], atom[1]));
Michael Sherman's avatar
Michael Sherman committed
210
211
212
213
214
215
216
217
    }
    // Exclude 1-2, 1-3 bonded atoms from nonbonded forces, and scale down 1-4 bonded atoms.
    nonbond.createExceptionsFromBonds(bondPairs, Coulomb14Scale, LennardJones14Scale);

    // -------------------------------------------------------------------------
    // Create the 1-2-3 bond angle harmonic terms.
    // -------------------------------------------------------------------------
    for (int i=0; angles[i].type != EndOfList; ++i) {
218
        const int*       atom  = angles[i].atoms;
Michael Sherman's avatar
Michael Sherman committed
219
220
221
        const AngleType& angle = angleType[angles[i].type];

        // See note under bond stretch above regarding the factor of 2 here.
222
        bondBend.addAngle(atom[0],atom[1],atom[2],
Michael Sherman's avatar
Michael Sherman committed
223
224
225
226
227
228
229
230
                          angle.nominalAngleInDegrees     * RadiansPerDegree,
                          angle.stiffnessInKcalPerRadian2 * 2 * KJPerKcal);
    }

    // -------------------------------------------------------------------------
    // Create the 1-2-3-4 bond torsion (dihedral) terms.
    // -------------------------------------------------------------------------
    for (int i=0; torsions[i].type != EndOfList; ++i) {
231
        const int*         atom = torsions[i].atoms;
Michael Sherman's avatar
Michael Sherman committed
232
        const TorsionType& torsion = torsionType[torsions[i].type];
233
        bondTorsion.addTorsion(atom[0],atom[1],atom[2],atom[3], 
Michael Sherman's avatar
Michael Sherman committed
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
            torsion.periodicity, 
            torsion.phaseInDegrees  * RadiansPerDegree,
            torsion.amplitudeInKcal * KJPerKcal);
    }

    // -------------------------------------------------------------------------
    // Choose an Integrator for advancing time, and a Context connecting the
    // System with the Integrator for simulation. Let the Context choose the
    // best available Platform. Initialize the configuration from the default
    // positions we collected above. Initial velocities will be zero.
    // -------------------------------------------------------------------------
    VerletIntegrator integrator(StepSizeInFs * PsPerFs);
    OpenMMContext    context(system, integrator);
    context.setPositions(initialPositions);

    // -------------------------------------------------------------------------
    // Run the simulation:
    //  (1) Write the first line of the PDB file and the initial configuration.
    //  (2) Run silently entirely within OpenMM between reporting intervals.
    //  (3) Write a PDB frame when the time comes.
    // -------------------------------------------------------------------------
    printf("REMARK  Using OpenMM platform %s\n", context.getPlatform().getName().c_str() );
    writePDB(context);

    const int NumSilentSteps = (int)(ReportIntervalInFs / StepSizeInFs + 0.5);
    do {
        integrator.step(NumSilentSteps);
        writePDB(context);
    } while (context.getTime() < SimulationTimeInPs);
}


266

Michael Sherman's avatar
Michael Sherman committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// -----------------------------------------------------------------------------
//                               PDB FILE WRITER
// -----------------------------------------------------------------------------
static void
writePDB(const OpenMMContext& context) {
    // Caution: at the moment asking for energy requires use of slow Reference 
    // platform calculation.
    const State                 state       = context.getState(State::Positions | State::Velocities | State::Energy);
    const double                energy      = state.getPotentialEnergy() + state.getKineticEnergy();
    const std::vector<Vec3>&    positions   = state.getPositions();
    static int modelFrameNumber = 0; // numbering for MODEL records in pdb output

    modelFrameNumber++;
    printf("MODEL     %d\n", modelFrameNumber);
    printf("REMARK 250 time=%.3f picoseconds; Energy = %.3f kilojoules/mole\n", state.getTime(), energy);
    for (unsigned i=0; i < positions.size(); ++i) {
        const Vec3 pos = positions[i] * AngstromsPerNm;
284
285
        printf("ATOM  %5d %4s ETH     1    %8.3f%8.3f%8.3f  1.00  0.00            \n", 
            i+1, atoms[i].pdb, pos[0], pos[1], pos[2]);
Michael Sherman's avatar
Michael Sherman committed
286
287
288
289
    }
    printf("ENDMDL\n");
}