TestReferenceCustomGBForce.cpp 37.2 KB
Newer Older
1
2
3
4
5
6
7
8
9

/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
10
 * Portions copyright (c) 2008-2013 Stanford University and the Authors.      *
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
 * 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 all the different force terms in the reference implementation of CustomGBForce.
 */

37
#include "openmm/internal/AssertionUtilities.h"
38
#include "sfmt/SFMT.h"
39
40
41
42
#include "openmm/Context.h"
#include "ReferencePlatform.h"
#include "openmm/CustomGBForce.h"
#include "openmm/GBSAOBCForce.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
43
#include "openmm/GBVIForce.h"
44
#include "openmm/OpenMMException.h"
45
46
47
48
#include "openmm/System.h"
#include "openmm/VerletIntegrator.h"
#include <iostream>
#include <vector>
49
#include <algorithm>
50
51
52
53
54
55

using namespace OpenMM;
using namespace std;

const double TOL = 1e-5;

56
57
void testOBC(GBSAOBCForce::NonbondedMethod obcMethod, CustomGBForce::NonbondedMethod customMethod) {
    const int numMolecules = 70;
58
59
    const int numParticles = numMolecules*2;
    const double boxSize = 10.0;
60
    const double cutoff = 2.0;
61
62
63
64
65
66
67
68
69
70
    ReferencePlatform platform;

    // Create two systems: one with a GBSAOBCForce, and one using a CustomGBForce to implement the same interaction.

    System standardSystem;
    System customSystem;
    for (int i = 0; i < numParticles; i++) {
        standardSystem.addParticle(1.0);
        customSystem.addParticle(1.0);
    }
71
72
    standardSystem.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0.0, 0.0), Vec3(0.0, boxSize, 0.0), Vec3(0.0, 0.0, boxSize));
    customSystem.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0.0, 0.0), Vec3(0.0, boxSize, 0.0), Vec3(0.0, 0.0, boxSize));
73
74
    GBSAOBCForce* obc = new GBSAOBCForce();
    CustomGBForce* custom = new CustomGBForce();
75
76
    obc->setCutoffDistance(cutoff);
    custom->setCutoffDistance(cutoff);
77
78
79
80
81
    custom->addPerParticleParameter("q");
    custom->addPerParticleParameter("radius");
    custom->addPerParticleParameter("scale");
    custom->addGlobalParameter("solventDielectric", obc->getSolventDielectric());
    custom->addGlobalParameter("soluteDielectric", obc->getSoluteDielectric());
Peter Eastman's avatar
Peter Eastman committed
82
    custom->addComputedValue("I", "step(r+sr2-or1)*0.5*(1/L-1/U+0.25*(1/U^2-1/L^2)*(r-sr2*sr2/r)+0.5*log(L/U)/r+C);"
83
84
                                  "U=r+sr2;"
                                  "C=2*(1/or1-1/L)*step(sr2-r-or1);"
85
86
                                  "L=max(or1, D);"
                                  "D=abs(r-sr2);"
Peter Eastman's avatar
Peter Eastman committed
87
                                  "sr2 = scale2*or2;"
88
89
90
91
                                  "or1 = radius1-0.009; or2 = radius2-0.009", CustomGBForce::ParticlePairNoExclusions);
    custom->addComputedValue("B", "1/(1/or-tanh(1*psi-0.8*psi^2+4.85*psi^3)/radius);"
                                  "psi=I*or; or=radius-0.009", CustomGBForce::SingleParticle);
    custom->addEnergyTerm("28.3919551*(radius+0.14)^2*(radius/B)^6-0.5*138.935485*(1/soluteDielectric-1/solventDielectric)*q^2/B", CustomGBForce::SingleParticle);
92
93
94
95
96
97
98
    string invCutoffString = "";
    if (obcMethod != GBSAOBCForce::NoCutoff) {
        stringstream s;
        s<<(1.0/cutoff);
        invCutoffString = s.str();
    }
    custom->addEnergyTerm("138.935485*(1/soluteDielectric-1/solventDielectric)*q1*q2*("+invCutoffString+"-1/f);"
99
100
101
                          "f=sqrt(r^2+B1*B2*exp(-r^2/(4*B1*B2)))", CustomGBForce::ParticlePairNoExclusions);
    vector<Vec3> positions(numParticles);
    vector<Vec3> velocities(numParticles);
102
103
104
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    vector<double> params(3);
    for (int i = 0; i < numMolecules; i++) {
        if (i < numMolecules/2) {
            obc->addParticle(1.0, 0.2, 0.5);
            params[0] = 1.0;
            params[1] = 0.2;
            params[2] = 0.5;
            custom->addParticle(params);
            obc->addParticle(-1.0, 0.1, 0.5);
            params[0] = -1.0;
            params[1] = 0.1;
            custom->addParticle(params);
        }
        else {
            obc->addParticle(1.0, 0.2, 0.8);
            params[0] = 1.0;
            params[1] = 0.2;
            params[2] = 0.8;
            custom->addParticle(params);
            obc->addParticle(-1.0, 0.1, 0.8);
            params[0] = -1.0;
            params[1] = 0.1;
            custom->addParticle(params);
        }
129
        positions[2*i] = Vec3(boxSize*genrand_real2(sfmt), boxSize*genrand_real2(sfmt), boxSize*genrand_real2(sfmt));
130
        positions[2*i+1] = Vec3(positions[2*i][0]+1.0, positions[2*i][1], positions[2*i][2]);
131
132
        velocities[2*i] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
        velocities[2*i+1] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
133
    }
134
135
    obc->setNonbondedMethod(obcMethod);
    custom->setNonbondedMethod(customMethod);
136
137
    standardSystem.addForce(obc);
    customSystem.addForce(custom);
138
139
140
141
142
    if (customMethod == CustomGBForce::CutoffPeriodic) {
        ASSERT(custom->usesPeriodicBoundaryConditions());
        ASSERT(obc->usesPeriodicBoundaryConditions());
        ASSERT(standardSystem.usesPeriodicBoundaryConditions());
        ASSERT(customSystem.usesPeriodicBoundaryConditions());
143
144
    }
    else {
145
146
147
148
149
        ASSERT(!custom->usesPeriodicBoundaryConditions());
        ASSERT(!obc->usesPeriodicBoundaryConditions());
        ASSERT(!standardSystem.usesPeriodicBoundaryConditions());
        ASSERT(!customSystem.usesPeriodicBoundaryConditions());
    }
150
151
152
153
154
155
    VerletIntegrator integrator1(0.01);
    VerletIntegrator integrator2(0.01);
    Context context1(standardSystem, integrator1, platform);
    context1.setPositions(positions);
    context1.setVelocities(velocities);
    State state1 = context1.getState(State::Forces | State::Energy);
156
157
158
    Context context2(customSystem, integrator2, platform);
    context2.setPositions(positions);
    context2.setVelocities(velocities);
159
    State state2 = context2.getState(State::Forces | State::Energy);
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    ASSERT_EQUAL_TOL(state1.getPotentialEnergy(), state2.getPotentialEnergy(), 1e-4);
    for (int i = 0; i < numParticles; i++) {
        ASSERT_EQUAL_VEC(state1.getForces()[i], state2.getForces()[i], 1e-4);
    }
    
    // Try changing the particle parameters and make sure it's still correct.
    
    for (int i = 0; i < numMolecules/2; i++) {
        obc->setParticleParameters(2*i, 1.1, 0.3, 0.6);
        params[0] = 1.1;
        params[1] = 0.3;
        params[2] = 0.6;
        custom->setParticleParameters(2*i, params);
        obc->setParticleParameters(2*i+1, -1.1, 0.2, 0.4);
        params[0] = -1.1;
        params[1] = 0.2;
        params[2] = 0.4;
        custom->setParticleParameters(2*i+1, params);
    }
    obc->updateParametersInContext(context1);
    custom->updateParametersInContext(context2);
    state1 = context1.getState(State::Forces | State::Energy);
    state2 = context2.getState(State::Forces | State::Energy);
183
184
    ASSERT_EQUAL_TOL(state1.getPotentialEnergy(), state2.getPotentialEnergy(), 1e-4);
    for (int i = 0; i < numParticles; i++) {
185
        ASSERT_EQUAL_VEC(state1.getForces()[i], state2.getForces()[i], 1e-4);
186
187
188
    }
}

Peter Eastman's avatar
Peter Eastman committed
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
void testMembrane() {
    const int numMolecules = 70;
    const int numParticles = numMolecules*2;
    const double boxSize = 10.0;
    ReferencePlatform platform;

    // Create a system with an implicit membrane.

    System system;
    for (int i = 0; i < numParticles; i++) {
        system.addParticle(1.0);
    }
    system.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0.0, 0.0), Vec3(0.0, boxSize, 0.0), Vec3(0.0, 0.0, boxSize));
    CustomGBForce* custom = new CustomGBForce();
    custom->setCutoffDistance(2.0);
    custom->addPerParticleParameter("q");
    custom->addPerParticleParameter("radius");
    custom->addPerParticleParameter("scale");
    custom->addGlobalParameter("thickness", 3);
    custom->addGlobalParameter("solventDielectric", 78.3);
    custom->addGlobalParameter("soluteDielectric", 1);
    custom->addComputedValue("Imol", "step(r+sr2-or1)*0.5*(1/L-1/U+0.25*(1/U^2-1/L^2)*(r-sr2*sr2/r)+0.5*log(L/U)/r+C);"
                             "U=r+sr2;"
                             "C=2*(1/or1-1/L)*step(sr2-r-or1);"
                             "L=max(or1, D);"
                             "D=abs(r-sr2);"
                             "sr2 = scale2*or2;"
                             "or1 = radius1-0.009; or2 = radius2-0.009", CustomGBForce::ParticlePairNoExclusions);
    custom->addComputedValue("Imem", "(1/radius+2*log(2)/thickness)/(1+exp(7.2*(abs(z)+radius-0.5*thickness)))", CustomGBForce::SingleParticle);
    custom->addComputedValue("B", "1/(1/or-tanh(1*psi-0.8*psi^2+4.85*psi^3)/radius);"
                             "psi=max(Imol,Imem)*or; or=radius-0.009", CustomGBForce::SingleParticle);
    custom->addEnergyTerm("28.3919551*(radius+0.14)^2*(radius/B)^6-0.5*138.935456*(1/soluteDielectric-1/solventDielectric)*q^2/B", CustomGBForce::SingleParticle);
    custom->addEnergyTerm("-138.935456*(1/soluteDielectric-1/solventDielectric)*q1*q2/f;"
                          "f=sqrt(r^2+B1*B2*exp(-r^2/(4*B1*B2)))", CustomGBForce::ParticlePairNoExclusions);
    vector<Vec3> positions(numParticles);
    vector<Vec3> velocities(numParticles);
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);
    vector<double> params(3);
    for (int i = 0; i < numMolecules; i++) {
        if (i < numMolecules/2) {
            params[0] = 1.0;
            params[1] = 0.2;
            params[2] = 0.5;
            custom->addParticle(params);
            params[0] = -1.0;
            params[1] = 0.1;
            custom->addParticle(params);
        }
        else {
            params[0] = 1.0;
            params[1] = 0.2;
            params[2] = 0.8;
            custom->addParticle(params);
            params[0] = -1.0;
            params[1] = 0.1;
            custom->addParticle(params);
        }
        positions[2*i] = Vec3(boxSize*genrand_real2(sfmt), boxSize*genrand_real2(sfmt), boxSize*genrand_real2(sfmt));
        positions[2*i+1] = Vec3(positions[2*i][0]+1.0, positions[2*i][1], positions[2*i][2]);
        velocities[2*i] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
        velocities[2*i+1] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
    }
    system.addForce(custom);
    VerletIntegrator integrator(0.01);
    Context context(system, integrator, platform);
    context.setPositions(positions);
    context.setVelocities(velocities);
    State state = context.getState(State::Forces | State::Energy);
    const vector<Vec3>& forces = state.getForces();

    // Take a small step in the direction of the energy gradient and see whether the potential energy changes by the expected amount.

    double norm = 0.0;
    for (int i = 0; i < (int) forces.size(); ++i)
        norm += forces[i].dot(forces[i]);
    norm = std::sqrt(norm);
266
    const double stepSize = 1e-2;
267
268
    double step = 0.5*stepSize/norm;
    vector<Vec3> positions2(numParticles), positions3(numParticles);
Peter Eastman's avatar
Peter Eastman committed
269
270
271
    for (int i = 0; i < (int) positions.size(); ++i) {
        Vec3 p = positions[i];
        Vec3 f = forces[i];
272
273
        positions2[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
        positions3[i] = Vec3(p[0]+f[0]*step, p[1]+f[1]*step, p[2]+f[2]*step);
Peter Eastman's avatar
Peter Eastman committed
274
    }
275
    context.setPositions(positions2);
Peter Eastman's avatar
Peter Eastman committed
276
    State state2 = context.getState(State::Energy);
277
278
279
    context.setPositions(positions3);
    State state3 = context.getState(State::Energy);
    ASSERT_EQUAL_TOL(norm, (state2.getPotentialEnergy()-state3.getPotentialEnergy())/stepSize, 1e-3);
Peter Eastman's avatar
Peter Eastman committed
280
281
}

282
void testTabulatedFunction() {
283
284
285
286
287
288
289
290
291
292
293
294
295
    ReferencePlatform platform;
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    VerletIntegrator integrator(0.01);
    CustomGBForce* force = new CustomGBForce();
    force->addComputedValue("a", "0", CustomGBForce::ParticlePair);
    force->addEnergyTerm("fn(r)+1", CustomGBForce::ParticlePair);
    force->addParticle(vector<double>());
    force->addParticle(vector<double>());
    vector<double> table;
    for (int i = 0; i < 21; i++)
        table.push_back(std::sin(0.25*i));
296
    force->addTabulatedFunction("fn", new Continuous1DFunction(table, 1.0, 6.0));
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    system.addForce(force);
    Context context(system, integrator, platform);
    vector<Vec3> positions(2);
    positions[0] = Vec3(0, 0, 0);
    for (int i = 1; i < 30; i++) {
        double x = (7.0/30.0)*i;
        positions[1] = Vec3(x, 0, 0);
        context.setPositions(positions);
        State state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces = state.getForces();
        double force = (x < 1.0 || x > 6.0 ? 0.0 : -std::cos(x-1.0));
        double energy = (x < 1.0 || x > 6.0 ? 0.0 : std::sin(x-1.0))+1.0;
        ASSERT_EQUAL_VEC(Vec3(-force, 0, 0), forces[0], 0.1);
        ASSERT_EQUAL_VEC(Vec3(force, 0, 0), forces[1], 0.1);
        ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), 0.02);
    }
}

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
void testMultipleChainRules() {
    ReferencePlatform platform;
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    VerletIntegrator integrator(0.01);
    CustomGBForce* force = new CustomGBForce();
    force->addComputedValue("a", "2*r", CustomGBForce::ParticlePair);
    force->addComputedValue("b", "a+1", CustomGBForce::SingleParticle);
    force->addComputedValue("c", "2*b+a", CustomGBForce::SingleParticle);
    force->addEnergyTerm("0.1*a+1*b+10*c", CustomGBForce::SingleParticle); // 0.1*(2*r) + 2*r+1 + 10*(3*a+2) = 0.2*r + 2*r+1 + 40*r+20+20*r = 62.2*r+21
    force->addParticle(vector<double>());
    force->addParticle(vector<double>());
    system.addForce(force);
    Context context(system, integrator, platform);
    vector<Vec3> positions(2);
    positions[0] = Vec3(0, 0, 0);
    for (int i = 1; i < 5; i++) {
        positions[1] = Vec3(i, 0, 0);
        context.setPositions(positions);
        State state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces = state.getForces();
        ASSERT_EQUAL_VEC(Vec3(124.4, 0, 0), forces[0], 1e-4);
        ASSERT_EQUAL_VEC(Vec3(-124.4, 0, 0), forces[1], 1e-4);
        ASSERT_EQUAL_TOL(2*(62.2*i+21), state.getPotentialEnergy(), 0.02);
    }
}

343
344
345
346
347
348
349
350
void testPositionDependence() {
    ReferencePlatform platform;
    System system;
    system.addParticle(1.0);
    system.addParticle(1.0);
    VerletIntegrator integrator(0.01);
    CustomGBForce* force = new CustomGBForce();
    force->addComputedValue("a", "r", CustomGBForce::ParticlePair);
Peter Eastman's avatar
Peter Eastman committed
351
    force->addComputedValue("b", "a+x*y", CustomGBForce::SingleParticle);
352
    force->addEnergyTerm("b*z", CustomGBForce::SingleParticle);
Peter Eastman's avatar
Peter Eastman committed
353
    force->addEnergyTerm("b1+b2", CustomGBForce::ParticlePair); // = 2*r+x1*y1+x2*y2
354
355
356
357
358
359
    force->addParticle(vector<double>());
    force->addParticle(vector<double>());
    system.addForce(force);
    Context context(system, integrator, platform);
    vector<Vec3> positions(2);
    vector<Vec3> forces(2);
360
361
362
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

363
    for (int i = 0; i < 5; i++) {
364
365
        positions[0] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
        positions[1] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
366
367
368
369
370
        context.setPositions(positions);
        State state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces = state.getForces();
        Vec3 delta = positions[0]-positions[1];
        double r = sqrt(delta.dot(delta));
Peter Eastman's avatar
Peter Eastman committed
371
        double energy = 2*r+positions[0][0]*positions[0][1]+positions[1][0]*positions[1][1];
372
        for (int j = 0; j < 2; j++)
Peter Eastman's avatar
Peter Eastman committed
373
374
375
376
377
378
379
            energy += positions[j][2]*(r+positions[j][0]*positions[j][1]);
        Vec3 force1(-(1+positions[0][2])*delta[0]/r-(1+positions[0][2])*positions[0][1]-(1+positions[1][2])*delta[0]/r,
                    -(1+positions[0][2])*delta[1]/r-(1+positions[0][2])*positions[0][0]-(1+positions[1][2])*delta[1]/r,
                    -(1+positions[0][2])*delta[2]/r-(r+positions[0][0]*positions[0][1])-(1+positions[1][2])*delta[2]/r);
        Vec3 force2((1+positions[0][2])*delta[0]/r+(1+positions[1][2])*delta[0]/r-(1+positions[1][2])*positions[1][1],
                    (1+positions[0][2])*delta[1]/r+(1+positions[1][2])*delta[1]/r-(1+positions[1][2])*positions[1][0],
                    (1+positions[0][2])*delta[2]/r+(1+positions[1][2])*delta[2]/r-(r+positions[1][0]*positions[1][1]));
380
381
382
383
384
385
386
387
388
389
390
        ASSERT_EQUAL_VEC(force1, forces[0], 1e-4);
        ASSERT_EQUAL_VEC(force2, forces[1], 1e-4);
        ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), 0.02);

        // Take a small step in the direction of the energy gradient and see whether the potential energy changes by the expected amount.

        double norm = 0.0;
        for (int i = 0; i < (int) forces.size(); ++i)
            norm += forces[i].dot(forces[i]);
        norm = std::sqrt(norm);
        const double stepSize = 1e-3;
391
392
        double step = 0.5*stepSize/norm;
        vector<Vec3> positions2(2), positions3(2);
393
394
395
        for (int i = 0; i < (int) positions.size(); ++i) {
            Vec3 p = positions[i];
            Vec3 f = forces[i];
396
397
            positions2[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
            positions3[i] = Vec3(p[0]+f[0]*step, p[1]+f[1]*step, p[2]+f[2]*step);
398
        }
399
        context.setPositions(positions2);
400
        State state2 = context.getState(State::Energy);
401
402
403
        context.setPositions(positions3);
        State state3 = context.getState(State::Energy);
        ASSERT_EQUAL_TOL(norm, (state2.getPotentialEnergy()-state3.getPotentialEnergy())/stepSize, 1e-3);
404
405
406
    }
}

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
void testExclusions() {
    ReferencePlatform platform;
    for (int i = 3; i < 4; i++) {
        System system;
        system.addParticle(1.0);
        system.addParticle(1.0);
        VerletIntegrator integrator(0.01);
        CustomGBForce* force = new CustomGBForce();
        force->addComputedValue("a", "r", i < 2 ? CustomGBForce::ParticlePair : CustomGBForce::ParticlePairNoExclusions);
        force->addEnergyTerm("a", CustomGBForce::SingleParticle);
        force->addEnergyTerm("(1+a1+a2)*r", i%2 == 0 ? CustomGBForce::ParticlePair : CustomGBForce::ParticlePairNoExclusions);
        force->addParticle(vector<double>());
        force->addParticle(vector<double>());
        force->addExclusion(0, 1);
        system.addForce(force);
        Context context(system, integrator, platform);
        vector<Vec3> positions(2);
        positions[0] = Vec3(0, 0, 0);
        positions[1] = Vec3(1, 0, 0);
        context.setPositions(positions);
        State state = context.getState(State::Forces | State::Energy);
        const vector<Vec3>& forces = state.getForces();
        double f, energy;
        switch (i)
        {
            case 0: // e = 0
                f = 0;
                energy = 0;
                break;
            case 1: // e = r
                f = 1;
                energy = 1;
                break;
            case 2: // e = 2r
                f = 2;
                energy = 2;
                break;
            case 3: // e = 3r + 2r^2
                f = 7;
                energy = 5;
                break;
            default:
                ASSERT(false);
        }
        ASSERT_EQUAL_VEC(Vec3(f, 0, 0), forces[0], 1e-4);
        ASSERT_EQUAL_VEC(Vec3(-f, 0, 0), forces[1], 1e-4);
        ASSERT_EQUAL_TOL(energy, state.getPotentialEnergy(), 1e-4);

        // Take a small step in the direction of the energy gradient and see whether the potential energy changes by the expected amount.

        double norm = 0.0;
        for (int i = 0; i < (int) forces.size(); ++i)
            norm += forces[i].dot(forces[i]);
        norm = std::sqrt(norm);
        const double stepSize = 1e-3;
        double step = stepSize/norm;
        for (int i = 0; i < (int) positions.size(); ++i) {
            Vec3 p = positions[i];
            Vec3 f = forces[i];
            positions[i] = Vec3(p[0]-f[0]*step, p[1]-f[1]*step, p[2]-f[2]*step);
        }
        context.setPositions(positions);
        State state2 = context.getState(State::Energy);
        ASSERT_EQUAL_TOL(norm, (state2.getPotentialEnergy()-state.getPotentialEnergy())/stepSize, 1e-3*abs(state.getPotentialEnergy()));
    }
}

Mark Friedrichs's avatar
Mark Friedrichs committed
474
475
// create custom GB/VI force

476
static CustomGBForce* createCustomGBVI(double solventDielectric, double soluteDielectric) {
Mark Friedrichs's avatar
Mark Friedrichs committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514

    CustomGBForce* customGbviForce  = new CustomGBForce();

    customGbviForce->setCutoffDistance(2.0);

    customGbviForce->addPerParticleParameter("q");
    customGbviForce->addPerParticleParameter("radius");
    customGbviForce->addPerParticleParameter("scaleFactor"); // derived in GBVIForce implmentation, but parameter here
    customGbviForce->addPerParticleParameter("gamma");

    customGbviForce->addGlobalParameter("solventDielectric", solventDielectric);
    customGbviForce->addGlobalParameter("soluteDielectric", soluteDielectric);

    customGbviForce->addComputedValue("V", "                uL - lL + factor3/(radius1*radius1*radius1);"
                                      "uL                   = 1.5*x2uI*(0.25*rI-0.33333*xuI+0.125*(r2-S2)*rI*x2uI);"
                                      "lL                   = 1.5*x2lI*(0.25*rI-0.33333*xlI+0.125*(r2-S2)*rI*x2lI);"
                                      "x2lI                 = 1.0/(xl*xl);"
                                      "xlI                  = 1.0/(xl);"
                                      "xuI                  = 1.0/(xu);"
                                      "x2uI                 = 1.0/(xu*xu);"
                                      "xu                   = (r+scaleFactor2);"
                                      "rI                   = 1.0/(r);"
                                      "r2                   = (r*r);"
                                      "xl                   = factor1*lMax + factor2*xuu + factor3*(r-scaleFactor2);"
                                      "xuu                  = (r+scaleFactor2);"
                                      "S2                   = (scaleFactor2*scaleFactor2);"
                                      "factor1              = step(r-absRadiusScaleDiff);"
                                      "absRadiusScaleDiff   = abs(radiusScaleDiff);"
                                      "radiusScaleDiff      = (radius1-scaleFactor2);"
                                      "factor2              = step(radius1-scaleFactor2-r);"
                                      "factor3              = step(scaleFactor2-radius1-r);"
                                      "lMax                 = max(radius1,r-scaleFactor2);"
                                      , CustomGBForce::ParticlePairNoExclusions);

    customGbviForce->addComputedValue("B", "(1.0/(radius*radius*radius)-V)^(-0.33333333)", CustomGBForce::SingleParticle);

    // nonpolar term + polar self energy

515
    customGbviForce->addEnergyTerm("(-138.935485*0.5*((1.0/soluteDielectric)-(1.0/solventDielectric))*q^2/B)-((1.0/soluteDielectric)-(1.0/solventDielectric))*((gamma*(radius/B)^3))", CustomGBForce::SingleParticle);
Mark Friedrichs's avatar
Mark Friedrichs committed
516
517
518
519
520
521
522
523
524
525
526

    // polar pair energy

    customGbviForce->addEnergyTerm("-138.935485*(1/soluteDielectric-1/solventDielectric)*q1*q2/f;"
                                   "f=sqrt(r^2+B1*B2*exp(-r^2/(4*B1*B2)))", CustomGBForce::ParticlePairNoExclusions);

    return customGbviForce;
}

// ethance GB/VI test case

527
static void buildEthane(GBVIForce* gbviForce, std::vector<Vec3>& positions) {
Mark Friedrichs's avatar
Mark Friedrichs committed
528
529
530
531
532
533
534
535
536
537

    const int numParticles = 8;

    double C_HBondDistance   = 0.1097;
    double C_CBondDistance   = 0.1504;
    double C_radius, C_gamma, C_charge, H_radius, H_gamma, H_charge;

    int AM1_BCC = 1;
    H_charge    = -0.053;
    C_charge    = -3.0*H_charge;
538
    if (AM1_BCC) {
Mark Friedrichs's avatar
Mark Friedrichs committed
539
540
541
542
       C_radius =  0.180;
       C_gamma  = -0.2863;
       H_radius =  0.125;
       H_gamma  =  0.2437;
543
544
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
545
546
547
548
549
550
       C_radius =  0.215;
       C_gamma  = -1.1087;
       H_radius =  0.150;
       H_gamma  =  0.1237;
    }

551
552
    for (int i = 0; i < numParticles; i++) {
       gbviForce->addParticle(H_charge, H_radius, H_gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
553
    }
554
555
    gbviForce->setParticleParameters(1, C_charge, C_radius, C_gamma);
    gbviForce->setParticleParameters(4, C_charge, C_radius, C_gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
556
 
557
558
559
560
561
562
563
    gbviForce->addBond(0, 1, C_HBondDistance);
    gbviForce->addBond(2, 1, C_HBondDistance);
    gbviForce->addBond(3, 1, C_HBondDistance);
    gbviForce->addBond(1, 4, C_CBondDistance);
    gbviForce->addBond(5, 4, C_HBondDistance);
    gbviForce->addBond(6, 4, C_HBondDistance);
    gbviForce->addBond(7, 4, C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
564
565
566
567
568
    
    std::vector<pair<int, int> > bondExceptions;
    std::vector<double> bondDistances;
    
    bondExceptions.push_back(pair<int, int>(0, 1)); 
569
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
570
571
    
    bondExceptions.push_back(pair<int, int>(2, 1)); 
572
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
573
574
    
    bondExceptions.push_back(pair<int, int>(3, 1)); 
575
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
576
577
    
    bondExceptions.push_back(pair<int, int>(1, 4)); 
578
    bondDistances.push_back(C_CBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
579
580
    
    bondExceptions.push_back(pair<int, int>(5, 4)); 
581
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
582
583
    
    bondExceptions.push_back(pair<int, int>(6, 4)); 
584
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
585
586
 
    bondExceptions.push_back(pair<int, int>(7, 4));
587
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
 
    positions.resize(numParticles);
    positions[0] = Vec3(0.5480,    1.7661,    0.0000);
    positions[1] = Vec3(0.7286,    0.8978,    0.6468);
    positions[2] = Vec3(0.4974,    0.0000,    0.0588);
    positions[3] = Vec3(0.0000,    0.9459,    1.4666);
    positions[4] = Vec3(2.1421,    0.8746,    1.1615);
    positions[5] = Vec3(2.3239,    0.0050,    1.8065);
    positions[6] = Vec3(2.8705,    0.8295,    0.3416);
    positions[7] = Vec3(2.3722,    1.7711,    1.7518);

}

// dimer GB/VI test case

603
static void buildDimer(GBVIForce* gbviForce, std::vector<Vec3>& positions) {
Mark Friedrichs's avatar
Mark Friedrichs committed
604
605
606
607
608
609
610
611
612
613
614
615
616

    const int numParticles = 2;

    double C_HBondDistance   = 0.1097;
    double C_CBondDistance   = 0.1504;
    double C_radius, C_gamma, C_charge, H_radius, H_gamma, H_charge;

    int AM1_BCC = 1;
    H_charge    = -0.053;
    C_charge    = -3.0*H_charge;

    H_charge    = 0.0;
    C_charge    = 0.0;
617
    if (AM1_BCC) {
Mark Friedrichs's avatar
Mark Friedrichs committed
618
619
620
621
       C_radius =  0.180;
       C_gamma  = -0.2863;
       H_radius =  0.125;
       H_gamma  =  0.2437;
622
623
    }
    else {
Mark Friedrichs's avatar
Mark Friedrichs committed
624
625
626
627
628
629
       C_radius =  0.215;
       C_gamma  = -1.1087;
       H_radius =  0.150;
       H_gamma  =  0.1237;
    }

630
631
    for (int i = 0; i < numParticles; i++) {
       gbviForce->addParticle(H_charge, H_radius, H_gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
632
    }
633
    gbviForce->setParticleParameters(1, C_charge, C_radius, C_gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
634
 
635
    gbviForce->addBond(0, 1, C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
636
637
638
639
    std::vector<pair<int, int> > bondExceptions;
    std::vector<double> bondDistances;
    
    bondExceptions.push_back(pair<int, int>(0, 1)); 
640
    bondDistances.push_back(C_HBondDistance);
Mark Friedrichs's avatar
Mark Friedrichs committed
641
642
643
644
645
646
647
648
    
    positions.resize(numParticles);
    positions[0] = Vec3(0.0,       0.0,       0.0);
    positions[1] = Vec3(0.15,       0.0,       0.0);
}

// monomer GB/VI test case

649
static void buildMonomer(GBVIForce* gbviForce, std::vector<Vec3>& positions) {
Mark Friedrichs's avatar
Mark Friedrichs committed
650
651
652
653
654
655
656
657
658

    const int numParticles = 1;

    double H_radius, H_gamma, H_charge;

    H_charge = 1.0;
    H_radius = 0.125;
    H_gamma  = 0.2437;

659
660
    for (int i = 0; i < numParticles; i++) {
       gbviForce->addParticle(H_charge, H_radius, H_gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
661
662
663
664
665
666
667
668
    }
    positions.resize(numParticles);
    positions[0] = Vec3(0.0,    0.0,    0.0);
}

// taken from gbviForceImpl class
// computes the scaled radii based on covalent info and atomic radii

669
static void findScaledRadii(GBVIForce& gbviForce, std::vector<double> & scaledRadii) {
Mark Friedrichs's avatar
Mark Friedrichs committed
670
671
672
673
674
675
676
677

    int     numberOfParticles = gbviForce.getNumParticles();
    int numberOfBonds         = gbviForce.getNumBonds();
    
    // load 1-2 atom pairs along w/ bond distance using HarmonicBondForce & constraints
    // numberOfBonds < 1, indicating they were not set by the user
    
    std::vector< std::vector<int> > bondIndices;
678
    bondIndices.resize(numberOfBonds);
Mark Friedrichs's avatar
Mark Friedrichs committed
679
680
    
    std::vector<double> bondLengths;
681
    bondLengths.resize(numberOfBonds);
Mark Friedrichs's avatar
Mark Friedrichs committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705

    scaledRadii.resize(numberOfParticles);
    for (int i = 0; i < numberOfParticles; i++) {
        double charge, radius, gamma;
        gbviForce.getParticleParameters(i, charge, radius, gamma);
        scaledRadii[i] = radius;
    }

    for (int i = 0; i < numberOfBonds; i++) {
        int particle1, particle2;
        double bondLength;
        gbviForce.getBondParameters(i, particle1, particle2, bondLength);
        if (particle1 < 0 || particle1 >= gbviForce.getNumParticles()) {
            std::stringstream msg;
            msg << "GBVISoftcoreForce: Illegal particle index: ";
            msg << particle1;
            throw OpenMMException(msg.str());
        }
        if (particle2 < 0 || particle2 >= gbviForce.getNumParticles()) {
            std::stringstream msg;
            msg << "GBVISoftcoreForce: Illegal particle index: ";
            msg << particle2;
            throw OpenMMException(msg.str());
        }
706
        if (bondLength < 0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
707
708
709
710
711
            std::stringstream msg;
            msg << "GBVISoftcoreForce: negative bondlength: ";
            msg << bondLength;
            throw OpenMMException(msg.str());
        }
712
713
        bondIndices[i].push_back(particle1);
        bondIndices[i].push_back(particle2);
Mark Friedrichs's avatar
Mark Friedrichs committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
        bondLengths[i] = bondLength;
    }


    // load 1-2 indicies for each atom 

    std::vector<std::vector<int> > bonded12(numberOfParticles);

    for (int i = 0; i < (int) bondIndices.size(); ++i) {
        bonded12[bondIndices[i][0]].push_back(i);
        bonded12[bondIndices[i][1]].push_back(i);
    }

    int errors = 0;

    // compute scaled radii (Eq. 5 of Labute paper [JCC 29 p. 1693-1698 2008])

731
    for (int j = 0; j < (int) bonded12.size(); ++j) {
Mark Friedrichs's avatar
Mark Friedrichs committed
732
733
734
735
736
737
738
739

        double charge;
        double gamma;
        double radiusJ;
        double scaledRadiusJ;
     
        gbviForce.getParticleParameters(j, charge, radiusJ, gamma); 

740
        if ( bonded12[j].size() == 0) {
Mark Friedrichs's avatar
Mark Friedrichs committed
741
742
            scaledRadiusJ = radiusJ;
//             errors++;
743
744
        }
        else {
Mark Friedrichs's avatar
Mark Friedrichs committed
745
746
747
748
749
750

            double rJ2    = radiusJ*radiusJ;
    
            // loop over bonded neighbors of atom j, applying Eq. 5 in Labute

            scaledRadiusJ = 0.0;
751
            for (int i = 0; i < (int) bonded12[j].size(); ++i) {
Mark Friedrichs's avatar
Mark Friedrichs committed
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    
               int index            = bonded12[j][i];
               int bondedAtomIndex  = (j == bondIndices[index][0]) ? bondIndices[index][1] : bondIndices[index][0];
              
               double radiusI;
               gbviForce.getParticleParameters(bondedAtomIndex, charge, radiusI, gamma); 
               double rI2           = radiusI*radiusI;
    
               double a_ij          = (radiusI - bondLengths[index]);
                      a_ij         *= a_ij;
                      a_ij          = (rJ2 - a_ij)/(2.0*bondLengths[index]);
    
               double a_ji          = radiusJ - bondLengths[index];
                      a_ji         *= a_ji;
                      a_ji          = (rI2 - a_ji)/(2.0*bondLengths[index]);
    
768
               scaledRadiusJ       += a_ij*a_ij*(3.0*radiusI - a_ij) + a_ji*a_ji*(3.0*radiusJ - a_ji);
Mark Friedrichs's avatar
Mark Friedrichs committed
769
770
771
            }
    
            scaledRadiusJ  = (radiusJ*radiusJ*radiusJ) - 0.125*scaledRadiusJ; 
772
773
            if (scaledRadiusJ > 0.0) {
                scaledRadiusJ  = 0.95*pow(scaledRadiusJ, (1.0/3.0));
774
775
            }
            else {
Mark Friedrichs's avatar
Mark Friedrichs committed
776
777
778
779
780
781
782
783
784
                scaledRadiusJ  = 0.0;
            }
        }
        scaledRadii[j] = scaledRadiusJ;

    }

    // abort if errors

785
    if (errors) {
Mark Friedrichs's avatar
Mark Friedrichs committed
786
787
788
789
790
791
792
793
        throw OpenMMException("GBVIForceImpl::findScaledRadii errors -- aborting");
    }
}

// load parameters from gbviForce to customGbviForce
// findScaledRadii() is called to calculate the scaled radii (S)
// S is derived quantity in GBVIForce, not a parameter is the case here

794
static void loadGbviParameters(GBVIForce* gbviForce, CustomGBForce* customGbviForce) {
Mark Friedrichs's avatar
Mark Friedrichs committed
795
796
797
798
799
800
801

    int numParticles = gbviForce->getNumParticles();

    // charge, radius, scale factor, gamma

    vector<double> params(4);
    std::vector<double> scaledRadii;
802
    findScaledRadii(*gbviForce, scaledRadii);
Mark Friedrichs's avatar
Mark Friedrichs committed
803

804
    for (int ii = 0; ii < numParticles; ii++) {
Mark Friedrichs's avatar
Mark Friedrichs committed
805
        double charge, radius, gamma;
806
        gbviForce->getParticleParameters(ii, charge, radius, gamma);
Mark Friedrichs's avatar
Mark Friedrichs committed
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
        params[0] = charge;
        params[1] = radius;
        params[2] = scaledRadii[ii];
        params[3] = gamma;
        customGbviForce->addParticle(params);
    }

}

void testGBVI(GBVIForce::NonbondedMethod gbviMethod, CustomGBForce::NonbondedMethod customGbviMethod, std::string molecule) {

    const int numMolecules = 1;
    const double boxSize   = 10.0;
    ReferencePlatform platform;

    GBVIForce*        gbvi = new GBVIForce();
    std::vector<Vec3> positions;

    // select molecule

827
828
    if (molecule == "Monomer") {
        buildMonomer(gbvi, positions);
829
    }
830
831
    else if (molecule == "Dimer") {
        buildDimer(gbvi, positions);
832
833
    }
    else {
834
        buildEthane(gbvi, positions);
Mark Friedrichs's avatar
Mark Friedrichs committed
835
836
837
838
839
840
841
842
843
    }

    int numParticles = gbvi->getNumParticles();
    System standardSystem;
    System customGbviSystem;
    for (int i = 0; i < numParticles; i++) {
        standardSystem.addParticle(1.0);
        customGbviSystem.addParticle(1.0);
    }
844
845
    standardSystem.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0.0, 0.0), Vec3(0.0, boxSize, 0.0), Vec3(0.0, 0.0, boxSize));
    customGbviSystem.setDefaultPeriodicBoxVectors(Vec3(boxSize, 0.0, 0.0), Vec3(0.0, boxSize, 0.0), Vec3(0.0, 0.0, boxSize));
Mark Friedrichs's avatar
Mark Friedrichs committed
846
847
848
849
    gbvi->setCutoffDistance(2.0);

    // create customGbviForce GBVI force

850
    CustomGBForce* customGbviForce  = createCustomGBVI(gbvi->getSolventDielectric(), gbvi->getSoluteDielectric());
Mark Friedrichs's avatar
Mark Friedrichs committed
851
852
853
854
    customGbviForce->setCutoffDistance(2.0);

    // load parameters from gbvi to customGbviForce

855
    loadGbviParameters(gbvi, customGbviForce);
Mark Friedrichs's avatar
Mark Friedrichs committed
856

857
858
859
    OpenMM_SFMT::SFMT sfmt;
    init_gen_rand(0, sfmt);

Mark Friedrichs's avatar
Mark Friedrichs committed
860
861
    vector<Vec3> velocities(numParticles);
    for (int ii = 0; ii < numParticles; ii++) {
862
        velocities[ii] = Vec3(genrand_real2(sfmt), genrand_real2(sfmt), genrand_real2(sfmt));
Mark Friedrichs's avatar
Mark Friedrichs committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
    }
    gbvi->setNonbondedMethod(gbviMethod);
    customGbviForce->setNonbondedMethod(customGbviMethod);

    standardSystem.addForce(gbvi);
    customGbviSystem.addForce(customGbviForce);

    VerletIntegrator integrator1(0.01);
    VerletIntegrator integrator2(0.01);

    Context context1(standardSystem, integrator1, platform);
    context1.setPositions(positions);
    context1.setVelocities(velocities);
    State state1 = context1.getState(State::Forces | State::Energy);

    Context context2(customGbviSystem, integrator2, platform);
    context2.setPositions(positions);
    context2.setVelocities(velocities);
    State state2 = context2.getState(State::Forces | State::Energy);

    ASSERT_EQUAL_TOL(state1.getPotentialEnergy(), state2.getPotentialEnergy(), 1e-4);

    for (int i = 0; i < numParticles; i++) {
        ASSERT_EQUAL_VEC(state1.getForces()[i], state2.getForces()[i], 1e-4);
    }
}

890
int main() {
Mark Friedrichs's avatar
Mark Friedrichs committed
891

892
    try {
893
894
895
        testOBC(GBSAOBCForce::NoCutoff, CustomGBForce::NoCutoff);
        testOBC(GBSAOBCForce::CutoffNonPeriodic, CustomGBForce::CutoffNonPeriodic);
        testOBC(GBSAOBCForce::CutoffPeriodic, CustomGBForce::CutoffPeriodic);
Peter Eastman's avatar
Peter Eastman committed
896
        testMembrane();
897
        testTabulatedFunction();
898
        testMultipleChainRules();
899
        testPositionDependence();
900
        testExclusions();
Mark Friedrichs's avatar
Mark Friedrichs committed
901
902
903
904
905
906
907

        // GBVI tests

        testGBVI(GBVIForce::NoCutoff, CustomGBForce::NoCutoff, "Monomer");
        testGBVI(GBVIForce::NoCutoff, CustomGBForce::NoCutoff, "Dimer");
        testGBVI(GBVIForce::NoCutoff, CustomGBForce::NoCutoff, "Ethane");

908
909
910
911
912
913
914
915
916
    }
    catch(const exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}