"...SimTKReference/ReferenceLJCoulombIxn.cpp.Ewald-optimized" did not exist on "2a53088236129e8a85e7d63965d8cce56212ba25"
TestReferenceRpmd.cpp 5.91 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
/* -------------------------------------------------------------------------- *
 *                                   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) 2011 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

/**
 * This tests the Reference implementation of RPMDIntegrator.
 */

#include "../../../tests/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/HarmonicBondForce.h"
#include "openmm/Platform.h"
#include "openmm/System.h"
#include "openmm/RPMDIntegrator.h"
#include "SimTKUtilities/SimTKOpenMMUtilities.h"
43
#include "sfmt/SFMT.h"
44
45
46
47
48
49
50
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

void testIntegration() {
51
52
    const int numParticles = 1;
    const int numCopies = 30;
53
54
    const double temperature = 300.0;
    System system;
55
    for (int i = 0; i < numParticles; i++)
56
57
58
59
60
61
        system.addParticle(i+1);
    HarmonicBondForce* bonds = new HarmonicBondForce();
    system.addForce(bonds);
    for (int i = 0; i < numParticles-1; i++)
        bonds->addBond(i, i+1, 1.0, 100.0);
    RPMDIntegrator integ(numCopies, temperature, 1.0, 0.001);
Peter Eastman's avatar
Peter Eastman committed
62
63
    Platform& platform = Platform::getPlatformByName("Reference");
    Context context(system, integ, platform);
64
65
66
67
68
69
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);
    vector<Vec3> positions(numParticles);
    for (int i = 0; i < numCopies; i++)
    {
        for (int j = 0; j < numParticles; j++)
Peter Eastman's avatar
Peter Eastman committed
70
            positions[j] = Vec3(j+0.01*genrand_real2(sfmt), 0.01*genrand_real2(sfmt), 0.01*genrand_real2(sfmt));
71
72
73
74
        integ.setPositions(i, positions);
    }
    const int numSteps = 100000;
    integ.step(10000);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    vector<double> ke(numCopies, 0.0);
    vector<double> rg(numParticles, 0.0);
    const RealOpenMM hbar = 1.054571628e-34*AVOGADRO/(1000*1e-12);
    const double wn = numCopies*BOLTZ*temperature/hbar;
    for (int i = 0; i < numSteps; i++) {
        integ.step(1);
        vector<State> state(numCopies);
        for (int j = 0; j < numCopies; j++)
            state[j] = integ.getState(j, State::Positions | State::Velocities | State::Energy);
        for (int j = 0; j < numCopies; j++)
            ke[j] += state[j].getKineticEnergy();
        double totalEnergy = 0.0;
        for (int j = 0; j < numCopies; j++) {
            totalEnergy += state[j].getKineticEnergy()+state[j].getPotentialEnergy();
            for (int k = 0; k < numParticles; k++) {
90
                Vec3 delta = state[j].getPositions()[k]-state[j == 0 ? numCopies-1 : j-1].getPositions()[k];
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
                totalEnergy += 0.5*system.getParticleMass(k)*wn*wn*delta.dot(delta);
            }
        }
        for (int j = 0; j < numParticles; j++) {
            double rg2 = 0.0;
            for (int k = 0; k < numCopies; k++)
                for (int m = 0; m < numCopies; m++) {
                    Vec3 delta = state[k].getPositions()[j]-state[m].getPositions()[j];
                    rg2 += delta.dot(delta);
                }
            rg[j] += sqrt(rg2/(2*numCopies*numCopies));
        }
    }
    for (int i = 0; i < numCopies; i++) {
        double value = ke[i]/numSteps;
        double expected = 0.5*numCopies*numParticles*3*BOLTZ*temperature;
        printf("%d: %g %g %g\n", i, value, expected, value/expected);
    }
    for (int i = 0; i < numParticles; i++) {
        double value = rg[i]/numSteps;
        double expected = hbar/(2*sqrt(system.getParticleMass(i)*BOLTZ*temperature));
        printf("%d: %g %g %g\n", i, value, expected, value/expected);
    }
}

int main() {
    try {
        Platform::loadPluginsFromDirectory(Platform::getDefaultPluginsDirectory());
        testIntegration();
    }
    catch(const std::exception& e) {
        std::cout << "exception: " << e.what() << std::endl;
        std::cout << "FAIL - ERROR.  Test failed." << std::endl;
        return 1;
    }
    std::cout << "Done" << std::endl;
    return 0;
}