"wrappers/vscode:/vscode.git/clone" did not exist on "37d4b47236f59eca6b370d7205750acc82d9ec3d"
TestReferenceEwald.cpp 11.1 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

void testEwaldExact() {

//    Use a NaCl crystal to compare the calculated and Madelung energies

    const int numParticles = 1000;
    const double cutoff = 1.0;
    const double boxSize = 2.82;

61
62
    ReferencePlatform platform;
    System system;
63
64

    for (int i = 0; i < numParticles/2; i++)
65
        system.addParticle(22.99);
66
    for (int i = 0; i < numParticles/2; i++)
67
68
69
        system.addParticle(35.45);
    VerletIntegrator integrator(0.01);
    NonbondedForce* nonbonded = new NonbondedForce();
70
    for (int i = 0; i < numParticles/2; i++)
71
        nonbonded->addParticle(1.0, 1.0,0.0);
72
    for (int i = 0; i < numParticles/2; i++)
73
        nonbonded->addParticle(-1.0, 1.0,0.0);
74
    nonbonded->setNonbondedMethod(NonbondedForce::Ewald);
75
    nonbonded->setCutoffDistance(cutoff);
76
    system.setPeriodicBoxVectors(Vec3(boxSize, 0, 0), Vec3(0, boxSize, 0), Vec3(0, 0, boxSize));
77
78
79
    nonbonded->setEwaldErrorTolerance(TOL);
    system.addForce(nonbonded);
    Context context(system, integrator, platform);
80
    vector<Vec3> positions(numParticles);
81
82
    #include "nacl_crystal.dat"
    context.setPositions(positions);
83

84
85
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();
86
87
88
89

//  Compare against Gromacs result for now
    ASSERT_EQUAL_TOL(-430494.0, state.getPotentialEnergy(), 10*TOL);

90
91
}

92
void testEwald2Ions() {
93
    ReferencePlatform platform;
94
95
96
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
97
    VerletIntegrator integrator(0.01);
98
99
100
    NonbondedForce* nonbonded = new NonbondedForce();
    nonbonded->addParticle(1.0, 1, 0);
    nonbonded->addParticle(-1.0, 1, 0);
101
102
103
    nonbonded->setNonbondedMethod(NonbondedForce::Ewald);
    const double cutoff = 2.0;
    nonbonded->setCutoffDistance(cutoff);
104
    system.setPeriodicBoxVectors(Vec3(6, 0, 0), Vec3(0, 6, 0), Vec3(0, 0, 6));
105
    nonbonded->setEwaldErrorTolerance(TOL);
106
    system.addForce(nonbonded);
107
    Context context(system, integrator, platform);
108
109
110
111
112
113
    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();
114
115
    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);
116
    ASSERT_EQUAL_TOL(-217.276, state.getPotentialEnergy(), 10*TOL);
117
118
}

119
void testWaterSystem() {
120
    ReferencePlatform platform;
121
    System system;
122
123
124
    static int numParticles;
    numParticles = 648;
    for (int i = 0 ; i < numParticles ; i++)
125
126
127
    {
       system.addParticle(1.0);
    }
128
    VerletIntegrator integrator(0.01);
129
    NonbondedForce* nonbonded = new NonbondedForce();
130
    for (int i = 0 ; i < numParticles/3 ; i++)
131
132
133
134
135
    {
      nonbonded->addParticle(-0.82, 1, 0);
      nonbonded->addParticle(0.41, 1, 0);
      nonbonded->addParticle(0.41, 1, 0);
    }
136
    nonbonded->setNonbondedMethod(NonbondedForce::CutoffPeriodic);
137
    const double cutoff = 0.8;
138
    nonbonded->setCutoffDistance(cutoff);
139
    system.setPeriodicBoxVectors(Vec3(1.86206, 0, 0), Vec3(0, 1.86206, 0), Vec3(0, 0, 1.86206));
140
    nonbonded->setEwaldErrorTolerance(TOL);
141
    system.addForce(nonbonded);
142
    Context context(system, integrator, platform);
143
    vector<Vec3> positions(numParticles);
144
    #include "water.dat"
145
    context.setPositions(positions);
146
147
    State state1 = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state1.getForces();
148
149
   // for (int i = 0 ; i < 42 ; i++)
   //   cout << "f [" << i << " : ]" << forces[i] << endl;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
   // cout << "PotentialEnergy: " << state1.getPotentialEnergy() << endl;

// Take a small step in the direction of the energy gradient.
    
    double norm = 0.0;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 f = state1.getForces()[i];
        norm += f[0]*f[0] + f[1]*f[1] + f[2]*f[2];
    }


    norm = std::sqrt(norm);
    const double delta = 1e-3;
    double step = delta/norm;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 p = positions[i];
        Vec3 f = state1.getForces()[i];
        positions[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
    }
    context.setPositions(positions);
    
    // See whether the potential energy changed by the expected amount.
    
    nonbonded->setNonbondedMethod(NonbondedForce::Ewald);
    State state2 = context.getState(State::Energy);
    ASSERT_EQUAL_TOL(norm, (state2.getPotentialEnergy()-state1.getPotentialEnergy())/delta, 0.01)


178
179
}

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
void testEwaldPME() {

//      Use amorphoush NaCl system

    ReferencePlatform platform;
    const int numParticles = 216;
    System system;
    for (int i = 0; i < numParticles/2; i++)
        system.addParticle(22.99);
    for (int i = 0; i < numParticles/2; i++)
        system.addParticle(35.45);
    VerletIntegrator integrator(0.01);
    NonbondedForce* nonbonded = new NonbondedForce();
    for (int i = 0; i < numParticles/2; i++)
        nonbonded->addParticle(1.0, 1.0,0.0);
    for (int i = 0; i < numParticles/2; i++)
        nonbonded->addParticle(-1.0, 1.0,0.0);
    nonbonded->setNonbondedMethod(NonbondedForce::Ewald);
    const double cutoff = 0.8;
    nonbonded->setCutoffDistance(cutoff);
    system.setPeriodicBoxVectors(Vec3(1.86206, 0, 0), Vec3(0, 1.86206, 0), Vec3(0, 0, 1.86206));
    nonbonded->setEwaldErrorTolerance(TOL);
    system.addForce(nonbonded);
    Context context(system, integrator, platform);
    vector<Vec3> positions(numParticles);
    #include "nacl_amorph.dat"
    context.setPositions(positions);

    State state1 = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces1 = state1.getForces();

//   (1)   CHECK EXACT VALUE OF EWALD ENERGY

    ASSERT_EQUAL_TOL(-26651.9, state1.getPotentialEnergy(), TOL);

//   (2)   CHECK WHETHER THE EWALD FORCES ARE THE SAME AS THE GROMACS OUTPUT
// Even at tolerance 0.1 the test doesn't pass
//    #include "nacl_amorph_GromacsForcesEwald.dat"

//   (3)   CHECK SELF-CONSISTENCY

// Take a small step in the direction of the energy gradient.

    double norm = 0.0;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 f = state1.getForces()[i];
        norm += f[0]*f[0] + f[1]*f[1] + f[2]*f[2];
    }


    norm = std::sqrt(norm);
    const double delta = 1e-3;
    double step = delta/norm;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 p = positions[i];
        Vec3 f = state1.getForces()[i];
        positions[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
    }
    context.setPositions(positions);
    
    // See whether the potential energy changed by the expected amount.
    
    State state2 = context.getState(State::Energy);
    ASSERT_EQUAL_TOL(norm, (state2.getPotentialEnergy()-state1.getPotentialEnergy())/delta, 0.01)

//   (4)   CHECK EXACT VALUE OF PME ENERGY

    nonbonded->setNonbondedMethod(NonbondedForce::PME);
    context.reinitialize();
    #include "nacl_amorph.dat"
    context.setPositions(positions);
    State state3 = context.getState(State::Forces | State::Energy);
    ASSERT_EQUAL_TOL(-26651.9, state3.getPotentialEnergy(), 10*TOL);

//   (5) CHECK WHETHER PME FORCES ARE THE SAME AS THE GROMACS OUTPUT

// Even at tolerance 0.1 the test doesn't pass
//    #include "nacl_amorph_GromacsForcesEwald.dat"

//   (6) CHECK PME FOR SELF-CONSISTENCY

// Take a small step in the direction of the energy gradient.
    
    norm = 0.0;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 f = state3.getForces()[i];
        norm += f[0]*f[0] + f[1]*f[1] + f[2]*f[2];
    }
    norm = std::sqrt(norm);
    step = delta/norm;
    for (int i = 0; i < numParticles; ++i) {
        Vec3 p = positions[i];
        Vec3 f = state3.getForces()[i];
        positions[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
    }
    context.setPositions(positions);
    
    // See whether the potential energy changed by the expected amount.
    
    State state4 = context.getState(State::Energy);
    ASSERT_EQUAL_TOL(norm, (state4.getPotentialEnergy()-state3.getPotentialEnergy())/delta, 0.01)

}


285
286
287

int main() {
    try {
288
289
290
291
     testEwaldExact();           
//     testEwald2Ions();
//     testWaterSystem();
     testEwaldPME();
292
293
294
295
296
297
298
299
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}