Commit 5bce6143 authored by Michael Sherman's avatar Michael Sherman
Browse files

(1) Changed HelloArgon to avoid use of now-obsolete context.getTime() to avoid...

(1) Changed HelloArgon to avoid use of now-obsolete context.getTime() to avoid future problems with variable step integration.
(2) Added HelloArgonInC.
parent 3007cf52
......@@ -2,59 +2,73 @@
// OpenMM(tm) HelloArgon example in C++ (June 2009)
// -----------------------------------------------------------------------------
// This program demonstrates a simple molecular simulation using the OpenMM
// API for GPU-accelerated molecular dynamics simulation. The system
// represents a small collection of argon atoms in a vaccuum.
// 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.
// 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.
// -----------------------------------------------------------------------------
// Suppress irrelevant warning from Microsoft's compiler.
#ifdef _MSC_VER
#pragma warning(disable:4251) // no dll interface for some classes
#endif
#include "OpenMM.h"
#include <cstdio>
// Forward declaration of writePdb() subroutine for printing atomic
// coordinates, defined later in this source file.
void writePdb(const OpenMM::OpenMMContext& context);
// Forward declaration of routine for printing one frame of the
// trajectory, defined later in this source file.
void writePdbFrame(int frameNum, const OpenMM::State&);
void simulateArgon()
{
// Load any shared libraries containing GPU implementations
// Load any shared libraries containing GPU implementations.
OpenMM::Platform::loadPluginsFromDirectory(
OpenMM::Platform::getDefaultPluginsDirectory());
// Create a system with nonbonded forces
// Create a system with nonbonded forces.
OpenMM::System system;
OpenMM::NonbondedForce* nonbond = new OpenMM::NonbondedForce();
system.addForce(nonbond);
// Create three atoms
std::vector<OpenMM::Vec3> initialPositions(3);
// Create three atoms.
std::vector<OpenMM::Vec3> initPosInNm(3);
for (int a = 0; a < 3; ++a)
{
system.addParticle(39.95); // mass, grams per mole
initPosInNm[a] = OpenMM::Vec3(a/2.,0,0); // location, nm
// charge, sigma, well depth
nonbond->addParticle(0.0, 0.3350, 0.996);
system.addParticle(39.95); // mass of Ar, grams per mole
initialPositions[a] = OpenMM::Vec3(0.5*a,0,0); // location, nanometers
// charge, L-J sigma (nm), well depth (kJ)
nonbond->addParticle(0.0, 0.3350, 0.996); // vdWRad(Ar)=.188 nm
}
OpenMM::VerletIntegrator integrator(0.004); // step size in picoseconds
OpenMM::VerletIntegrator integrator(0.004); // step size in ps
// Let OpenMM Context choose best platform.
OpenMM::OpenMMContext context(system, integrator);
printf( "REMARK Using OpenMM platform %s\n",
context.getPlatform().getName().c_str() );
// Set the starting positions of the atoms. Velocities will be zero.
context.setPositions(initialPositions);
// 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
// Simulate
while(context.getTime() < 10.0) { // picoseconds
writePdb(context); // output coordinates
// Run 100 steps at a time, for efficient use of OpenMM
integrator.step(100);
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)
}
writePdb(context); // output final coordinates
}
int main()
......@@ -70,25 +84,20 @@ int main()
}
}
// writePdb() subroutine for quick-and-dirty trajectory output.
void writePdb(const OpenMM::OpenMMContext& context)
// Handy homebrew PDB writer for quick-and-dirty trajectory output.
void writePdbFrame(int frameNum, const OpenMM::State& state)
{
// Request atomic positions from OpenMM
const OpenMM::State state = context.getState(OpenMM::State::Positions);
const std::vector<OpenMM::Vec3>& pos = state.getPositions();
// write out in PDB format
// Reference atomic positions in the OpenMM State.
const std::vector<OpenMM::Vec3>& posInNm = state.getPositions();
// Use PDB MODEL cards to number trajectory frames
static int modelFrameNumber = 0;
modelFrameNumber++;
printf("MODEL %d\n", modelFrameNumber); // start of frame
for (int a = 0; a < context.getSystem().getNumParticles(); ++a)
printf("MODEL %d\n", frameNum); // start of frame
for (int a = 0; a < (int)posInNm.size(); ++a)
{
printf("ATOM %5d AR AR 1 ", a+1); // atom number
printf("%8.3f%8.3f%8.3f 1.00 0.00 AR\n", // coordinates
printf("ATOM %5d AR AR 1 ", a+1); // atom number
printf("%8.3f%8.3f%8.3f 1.00 0.00\n", // coordinates
// "*10" converts nanometers to Angstroms
pos[a][0]*10, pos[a][1]*10, pos[a][2]*10);
posInNm[a][0]*10, posInNm[a][1]*10, posInNm[a][2]*10);
}
printf("ENDMDL\n"); // end of frame
}
/* -----------------------------------------------------------------------------
* OpenMM(tm) HelloArgon example in C (June 2009)
* -----------------------------------------------------------------------------
* This program demonstrates a simple molecular simulation using the OpenMM
* 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.
* 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.
* -------------------------------------------------------------------------- */
#include "wrappers/OpenMM_CWrapper.h"
#include <stdio.h>
/* Forward declaration of routine for printing one frame of the
trajectory, defined later in this source file. */
void writePdbFrame(int frameNum, const OpenMM_State*);
void simulateArgon()
{
OpenMM_System* system;
OpenMM_Integrator* integrator;
OpenMM_Context* context;
OpenMM_NonbondedForce* nonbond;
OpenMM_Vec3Array* initPosInNm;
int a, frameNum;
/* Load any shared libraries containing GPU implementations. */
OpenMM_Platform_loadPluginsFromDirectory(
OpenMM_Platform_getDefaultPluginsDirectory());
/* Create a system with nonbonded forces. System takes ownership
of Force; don't destroy it yourself. */
system = OpenMM_System_create();
nonbond = OpenMM_NonbondedForce_create();
OpenMM_System_addForce(system, (OpenMM_Force*)nonbond);
/* Create three atoms. */
initPosInNm = OpenMM_Vec3Array_create(3);
for (a = 0; a < 3; ++a)
{
const double posNm[3] = {a/2., 0, 0}; /*location, nm*/
OpenMM_Vec3Array_set(initPosInNm, a, posNm);
OpenMM_System_addParticle(system, 39.95); /*mass of Ar, grams/mole*/
/* charge, L-J sigma (nm), well depth (kJ) */
OpenMM_NonbondedForce_addParticle(nonbond, 0.0, 0.3350, 0.996); /*vdWRad(Ar)=.188 nm*/
}
/* Create particular integrator, and recast to generic one. */
integrator = (OpenMM_Integrator*)OpenMM_VerletIntegrator_create(0.004); /*step size in ps*/
/* Let OpenMM Context choose best platform. */
context = OpenMM_Context_create(system, integrator);
printf( "REMARK Using OpenMM platform %s\n",
OpenMM_Context_getPlatformName(context));
/* Set starting positions of the atoms. Leave time and velocity zero. */
OpenMM_Context_setPositions(context, initPosInNm);
/* Simulate. */
for (frameNum=1; ;++frameNum) {
/* Output current state information. */
OpenMM_State* state = OpenMM_Context_createState(context, OpenMM_State_Positions);
const double timeInPs = OpenMM_State_getTime(state);
writePdbFrame(frameNum, state); /*output coordinates*/
OpenMM_State_destroy(state);
if (timeInPs >= 10.)
break;
/* Advance state many steps at a time, for efficient use of OpenMM. */
OpenMM_Integrator_step(integrator, 10); /*(use a lot more than this normally)*/
}
/* Free heap space for all the objects created above. */
OpenMM_Vec3Array_destroy(initPosInNm);
OpenMM_Context_destroy(context);
OpenMM_Integrator_destroy(integrator);
OpenMM_System_destroy(system);
}
int main()
{
simulateArgon();
return 0;
}
/* Handy homebrew PDB writer for quick-and-dirty trajectory output. */
void writePdbFrame(int frameNum, const OpenMM_State* state)
{
int a;
/* Reference atomic positions in the OpenMM State. */
const OpenMM_Vec3Array* posInNm = OpenMM_State_getPositions(state);
/* Use PDB MODEL cards to number trajectory frames. */
printf("MODEL %d\n", frameNum); /*start of frame*/
for (a = 0; a < OpenMM_Vec3Array_size(posInNm); ++a)
{
double posInAng[3];
/* "10" here converts nanometers to Angstroms */
OpenMM_Vec3Array_getScaled(posInNm, a, 10., posInAng);
printf("ATOM %5d AR AR 1 ", a+1); /*atom number*/
printf("%8.3f%8.3f%8.3f 1.00 0.00\n", /*coordinates*/
posInAng[0], posInAng[1], posInAng[2]);
}
printf("ENDMDL\n"); /*end of frame*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment