HelloArgon.cpp 3.68 KB
Newer Older
1
// -----------------------------------------------------------------------------
2
//                 OpenMM HelloArgon example in C++ (June 2009)
3
4
// -----------------------------------------------------------------------------
// This program demonstrates a simple molecular simulation using the OpenMM
5
6
7
8
9
// API for GPU-accelerated molecular dynamics simulation. The primary goal is
// to make sure you can compile, link, and run with OpenMM and view the output.
// The example is available in C++, C, and Fortran 95.
//
// The system modeled here is a small number of argon atoms in a vacuum.
10
11
12
13
// 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
14
15
16
#include "OpenMM.h"
#include <cstdio>

17
18
19
// Forward declaration of routine for printing one frame of the
// trajectory, defined later in this source file.
void writePdbFrame(int frameNum, const OpenMM::State&);
Michael Sherman's avatar
Michael Sherman committed
20
21
22

void simulateArgon()
{
23
    // Load any shared libraries containing GPU implementations.
Michael Sherman's avatar
Michael Sherman committed
24
25
26
    OpenMM::Platform::loadPluginsFromDirectory(
        OpenMM::Platform::getDefaultPluginsDirectory());

27
    // Create a system with nonbonded forces.
Michael Sherman's avatar
Michael Sherman committed
28
29
30
31
    OpenMM::System system;
    OpenMM::NonbondedForce* nonbond = new OpenMM::NonbondedForce(); 
    system.addForce(nonbond);

32
33
    // Create three atoms.
    std::vector<OpenMM::Vec3> initPosInNm(3);
Michael Sherman's avatar
Michael Sherman committed
34
35
    for (int a = 0; a < 3; ++a) 
    {
36
        initPosInNm[a] = OpenMM::Vec3(0.5*a,0,0); // location, nm
Michael Sherman's avatar
Michael Sherman committed
37

38
        system.addParticle(39.95); // mass of Ar, grams per mole
Michael Sherman's avatar
Michael Sherman committed
39

40
41
        // charge, L-J sigma (nm), well depth (kJ)
        nonbond->addParticle(0.0, 0.3350, 0.996); // vdWRad(Ar)=.188 nm
Michael Sherman's avatar
Michael Sherman committed
42
43
    }

44
    OpenMM::VerletIntegrator integrator(0.004); // step size in ps
Michael Sherman's avatar
Michael Sherman committed
45
46

    // Let OpenMM Context choose best platform.
47
    OpenMM::Context context(system, integrator);
Michael Sherman's avatar
Michael Sherman committed
48
49
50
    printf( "REMARK  Using OpenMM platform %s\n", 
        context.getPlatform().getName().c_str() );

51
52
53
54
55
56
57
58
59
    // Set starting positions of the atoms. Leave time and velocity zero.
    context.setPositions(initPosInNm);

    // Simulate.
    for (int frameNum=1; ;++frameNum) {
        // Output current state information.
        OpenMM::State state    = context.getState(OpenMM::State::Positions);
        const double  timeInPs = state.getTime();
        writePdbFrame(frameNum, state); // output coordinates
Michael Sherman's avatar
Michael Sherman committed
60

61
62
63
64
65
        if (timeInPs >= 10.)
            break;

        // Advance state many steps at a time, for efficient use of OpenMM.
        integrator.step(10); // (use a lot more than this normally)
Michael Sherman's avatar
Michael Sherman committed
66
67
68
69
70
71
72
73
74
    }
}

int main() 
{
    try {
        simulateArgon();
        return 0; // success!
    }
75
76
77
78
79
    // 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; // failure!
    }
Michael Sherman's avatar
Michael Sherman committed
80
81
}

82
83
// Handy homebrew PDB writer for quick-and-dirty trajectory output.
void writePdbFrame(int frameNum, const OpenMM::State& state) 
Michael Sherman's avatar
Michael Sherman committed
84
{
85
86
    // Reference atomic positions in the OpenMM State.
    const std::vector<OpenMM::Vec3>& posInNm = state.getPositions();
Michael Sherman's avatar
Michael Sherman committed
87
88

    // Use PDB MODEL cards to number trajectory frames
89
90
    printf("MODEL     %d\n", frameNum); // start of frame
    for (int a = 0; a < (int)posInNm.size(); ++a)
Michael Sherman's avatar
Michael Sherman committed
91
    {
92
93
        printf("ATOM  %5d  AR   AR     1    ", a+1); // atom number
        printf("%8.3f%8.3f%8.3f  1.00  0.00\n",      // coordinates
Michael Sherman's avatar
Michael Sherman committed
94
            // "*10" converts nanometers to Angstroms
95
            posInNm[a][0]*10, posInNm[a][1]*10, posInNm[a][2]*10);
Michael Sherman's avatar
Michael Sherman committed
96
97
98
    }
    printf("ENDMDL\n"); // end of frame
}