TestReferenceEwald.cpp 7.21 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
/* -------------------------------------------------------------------------- *
 *                                   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 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 Eewald summation method reference implementation of NonbondedForce.
 */

#include "../../../tests/AssertionUtilities.h"
37
#include "openmm/Context.h"
38
#include "ReferencePlatform.h"
39
40
41
#include "openmm/NonbondedForce.h"
#include "openmm/System.h"
#include "openmm/VerletIntegrator.h"
42
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
43
#include "openmm/HarmonicBondForce.h"
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

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
80
81
82
83
84
85
86
void testLargeSystem() {
    ReferencePlatform platform;
    System system;
    for (int i = 0; i < 500; i++)
        system.addParticle(22.99);
    for (int i = 0; i < 500; i++)
        system.addParticle(35.45);
    VerletIntegrator integrator(0.01);
    NonbondedForce* nonbonded = new NonbondedForce();
    for (int i = 0; i < 500; i++)
        nonbonded->addParticle(1.0, 1.0,0.0);
//        nonbonded->addParticle(1.0, 0.33284,0.0115897);
    for (int i = 0; i < 500; i++)
        nonbonded->addParticle(-1.0, 1.0,0.0);
//        nonbonded->addParticle(-1.0, 0.440104,0.4184);
    nonbonded->setNonbondedMethod(NonbondedForce::PME);
    const double cutoff = 1.0;
    nonbonded->setCutoffDistance(cutoff);
    nonbonded->setPeriodicBoxVectors(Vec3(2.82, 0, 0), Vec3(0, 2.82, 0), Vec3(0, 0, 2.82));
    nonbonded->setEwaldErrorTolerance(TOL);
    system.addForce(nonbonded);
    Context context(system, integrator, platform);
    vector<Vec3> positions(1000);
    #include "nacl_crystal.dat"
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
//    cout << "force 0: " << forces[0] << endl;
//    cout << "force 1: " << forces[1] << endl;
    cout << "PotentialEnergy: " << state.getPotentialEnergy() << endl;
    ASSERT_EQUAL_TOL(-430355, state.getPotentialEnergy(), 100*TOL);
//    ASSERT_EQUAL_VEC(Vec3(-123.711, 64.1877, -302.716), forces[0], 10*TOL);
//    ASSERT_EQUAL_VEC(Vec3(123.711, -64.1877, 302.716), forces[1], 10*TOL);
}

87
88
void testEwald() {
    ReferencePlatform platform;
89
90
91
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
92
    VerletIntegrator integrator(0.01);
93
94
95
    NonbondedForce* nonbonded = new NonbondedForce();
    nonbonded->addParticle(1.0, 1, 0);
    nonbonded->addParticle(-1.0, 1, 0);
96
97
98
// Sodium Chloride
//    nonbonded->addParticle(1.0, 0.33284,0.0115897);
//    nonbonded->addParticle(-1.0, 0.440104,0.4184);
99
100
101
102
    nonbonded->setNonbondedMethod(NonbondedForce::Ewald);
    const double cutoff = 2.0;
    nonbonded->setCutoffDistance(cutoff);
    nonbonded->setPeriodicBoxVectors(Vec3(6, 0, 0), Vec3(0, 6, 0), Vec3(0, 0, 6));
103
    nonbonded->setEwaldErrorTolerance(TOL);
104
    system.addForce(nonbonded);
105
    Context context(system, integrator, platform);
106
107
108
109
110
111
    vector<Vec3> positions(2);
    positions[0] = Vec3(3.048000,2.764000,3.156000);
    positions[1] = Vec3(2.809000,2.888000,2.571000);
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
112
113
114
//    cout << "force 0: " << forces[0] << endl;
//    cout << "force 1: " << forces[1] << endl;
//    cout << "PotentialEnergy: " << state.getPotentialEnergy() << endl;
115
116
    ASSERT_EQUAL_VEC(Vec3(-123.711, 64.1877, -302.716), forces[0], 10*TOL);
    ASSERT_EQUAL_VEC(Vec3(123.711, -64.1877, 302.716), forces[1], 10*TOL);
117
118
}

119
void testPME() {
120
    ReferencePlatform platform;
121
122
123
124
125
    System system;
    for (int i = 0 ; i < 42 ; i++)
    {
       system.addParticle(1.0);
    }
126
    VerletIntegrator integrator(0.01);
127
128
129
130
131
132
133
134
135
    NonbondedForce* nonbonded = new NonbondedForce();
    for (int i = 0 ; i < 14 ; i++)
    {
      nonbonded->addParticle(-0.82, 1, 0);
      nonbonded->addParticle(0.41, 1, 0);
      nonbonded->addParticle(0.41, 1, 0);
    }
    nonbonded->setNonbondedMethod(NonbondedForce::PME);
    const double cutoff = 0.8;
136
    nonbonded->setCutoffDistance(cutoff);
137
138
    nonbonded->setPeriodicBoxVectors(Vec3(1.86206, 0, 0), Vec3(0, 1.86206, 0), Vec3(0, 0, 1.86206));
    nonbonded->setEwaldErrorTolerance(TOL);
139
    system.addForce(nonbonded);
140
    Context context(system, integrator, platform);
141
    vector<Vec3> positions(42);
142
    #include "water.dat"
143
144
145
    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
146
147
148
149
150
   // for (int i = 0 ; i < 42 ; i++)
   //   cout << "f [" << i << " : ]" << forces[i] << endl;
   // cout << "PotentialEnergy: " << state.getPotentialEnergy() << endl;
//    ASSERT_EQUAL_VEC(Vec3(-123.711, 64.1877, -302.716), forces[0], 10*TOL);
//    ASSERT_EQUAL_VEC(Vec3(123.711, -64.1877, 302.716), forces[1], 10*TOL);
151
152
153
154
155
}


int main() {
    try {
156
157
158
     testLargeSystem();
     testEwald();
     testPME();
159
160
161
162
163
164
165
166
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}