TestWcaDispersionForce.h 9.24 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
43
44
/* -------------------------------------------------------------------------- *
 *                                   OpenMMAmoeba                             *
 * -------------------------------------------------------------------------- *
 * 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-2012 Stanford University and the Authors.      *
 * Authors: Mark Friedrichs                                                   *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#include "openmm/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "OpenMMAmoeba.h"
#include "openmm/System.h"
#include "openmm/AmoebaWcaDispersionForce.h"
#include "openmm/LangevinIntegrator.h"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>

#define ASSERT_EQUAL_TOL_MOD(expected, found, tol, testname) {double _scale_ = std::abs(expected) > 1.0 ? std::abs(expected) : 1.0; if (!(std::abs((expected)-(found))/_scale_ <= (tol))) {std::stringstream details; details << testname << " Expected "<<(expected)<<", found "<<(found); throwException(__FILE__, __LINE__, details.str());}};

45
#define ASSERT_EQUAL_VEC_MOD(expected, found, tol, testname) {ASSERT_EQUAL_TOL_MOD((expected)[0], (found)[0], (tol),(testname)); ASSERT_EQUAL_TOL_MOD((expected)[1], (found)[1], (tol),(testname)); ASSERT_EQUAL_TOL_MOD((expected)[2], (found)[2], (tol),(testname));};
46
47
48
49
50


using namespace OpenMM;
const double TOL = 1e-4;

51
52
53
54
void compareForcesEnergy(std::string &testName, double expectedEnergy, double energy,
                         const std::vector<Vec3> &expectedForces,
                         const std::vector<Vec3> &forces, double tolerance) {

55
    for (unsigned int ii = 0; ii < forces.size(); ii++) {
56
        ASSERT_EQUAL_VEC_MOD(expectedForces[ii], forces[ii], tolerance, testName)
57
    }
58
    ASSERT_EQUAL_TOL_MOD(expectedEnergy, energy, tolerance, testName)
59
60
}

61
// Test Wca Dispersion
62
63
void testWcaDispersionAmmonia() {

64
65
    std::string testName = "testWcaDispersionAmmonia";
    int numberOfParticles = 8;
66
67

    // Create the system.
68

69
    System system;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    AmoebaWcaDispersionForce *amoebaWcaDispersionForce = new AmoebaWcaDispersionForce();

    // Convert kcal/mol to kJ/mol
    double epso = 0.1100e0 * 4.184e0;
    double epsh = 0.0135e0 * 4.184e0;
    // Convert A to nm.
    double rmino = 1.7025e-01;
    double rminh = 1.3275e-01;
    double dispoff = 1.056e-01;
    // Convert water number density from water / A^3 to water / nm^3.
    double awater = 0.033428e03;
    // No units.
    double slevy = 1.0e0;
    double shctd = 0.75e0;

    amoebaWcaDispersionForce->setEpso(epso);
    amoebaWcaDispersionForce->setEpsh(epsh);
    amoebaWcaDispersionForce->setRmino(rmino);
    amoebaWcaDispersionForce->setRminh(rminh);
    amoebaWcaDispersionForce->setDispoff(dispoff);
    amoebaWcaDispersionForce->setAwater(awater);
    amoebaWcaDispersionForce->setSlevy(slevy);
    amoebaWcaDispersionForce->setShctd(shctd);

    // Amoeba 2009
    // vdwtype                 BUFFERED-14-7
    // radiusrule              CUBIC-MEAN
    // radiustype              R-MIN
    // radiussize              DIAMETER
    // epsilonrule             HHG
    // vdw     45  3.710000000  0.105000000
    // vdw     46  2.700000000  0.020000000  0.910
102

103
104
105
    for (unsigned int ii = 0; ii < 2; ii++) {
        system.addParticle(1.4007000e+01);
        amoebaWcaDispersionForce->addParticle(3.71e-01 / 2.0e0, 0.105e0 * 4.184e0);
106

107
108
        system.addParticle(1.0080000e+00);
        amoebaWcaDispersionForce->addParticle(2.7e-01 / 2.0e0, 0.02e0 * 4.184e0);
109

110
111
112
113
114
        system.addParticle(1.0080000e+00);
        amoebaWcaDispersionForce->addParticle(2.7e-01 / 2.0e0, 0.02e0 * 4.184e0);

        system.addParticle(1.0080000e+00);
        amoebaWcaDispersionForce->addParticle(2.7e-01 / 2.0e0, 0.02e0 * 4.184e0);
115
116
117
118
    }

    std::vector<Vec3> positions(numberOfParticles);

119
120
121
122
123
124
125
126
    positions[0] = Vec3(1.57493055e-01, 0.00085545e-01, 0.12707740e-01);
    positions[1] = Vec3(1.94285651e-01, -0.81095826e-01, 0.60622179e-01);
    positions[2] = Vec3(1.95030610e-01, 0.80808023e-01, 0.60809749e-01);
    positions[3] = Vec3(1.99309092e-01, -0.00015413e-01, -0.79434283e-01);
    positions[4] = Vec3(-1.65212994e-01, 0.02174061e-01, -0.04650966e-01);
    positions[5] = Vec3(-1.97210029e-01, 0.82501943e-01, 0.47898953e-01);
    positions[6] = Vec3(-0.64164714e-01, 0.01492667e-01, 0.03495515e-01);
    positions[7] = Vec3(-1.98070588e-01, -0.79434787e-01, 0.45336787e-01);
127
128

    system.addForce(amoebaWcaDispersionForce);
129
130
    ASSERT(!amoebaWcaDispersionForce->usesPeriodicBoundaryConditions());
    ASSERT(!system.usesPeriodicBoundaryConditions());
131
132
133
134
135
136

    LangevinIntegrator integrator(0.0, 0.1, 0.01);
    Context context(system, integrator, platform);

    context.setPositions(positions);
    State state = context.getState(State::Forces | State::Energy);
137
    const std::vector<Vec3> &forces = state.getForces();
138
139
    double energy = state.getPotentialEnergy();

140
141
    // Force Field X / Tinker expected energy -5.55412658 * 4.184 and forces.
    double expectedEnergy = -5.55412658 * 4.184;
142
    std::vector<Vec3> expectedForces(numberOfParticles);
143
144
145
146
147
148
149
150
151
152
153

    expectedForces[0] = Vec3(-0.33370779, 0.00478895, -0.18780836);
    expectedForces[1] = Vec3(1.15078615, 0.07363123, -0.04881103);
    expectedForces[2] = Vec3(1.14509754, -0.08727205, -0.05086123);
    expectedForces[3] = Vec3(0.97122614, -0.00473535, 0.09417216);
    expectedForces[4] = Vec3(-1.64874538, 0.01074006, -0.08477238);
    expectedForces[5] = Vec3(-1.28585908, 0.00494013, 0.05864744);
    expectedForces[6] = Vec3(1.28677671, -0.00915430, 0.16081161);
    expectedForces[7] = Vec3(-1.28557430, 0.00706132, 0.05862179);

    double tolerance = 1.0e-04;
154
    compareForcesEnergy(testName, expectedEnergy, energy, expectedForces, forces, tolerance);
155

156
157
158
159
    // Try changing the particle parameters and make sure it's still correct.
    for (int i = 0; i < numberOfParticles; i++) {
        double radius, epsilon;
        amoebaWcaDispersionForce->getParticleParameters(i, radius, epsilon);
160
        amoebaWcaDispersionForce->setParticleParameters(i, 0.9 * radius, 2.0 * epsilon);
161
162
163
164
165
166
167
168
169
    }
    LangevinIntegrator integrator2(0.0, 0.1, 0.01);
    Context context2(system, integrator2, platform);
    context2.setPositions(positions);
    State state1 = context.getState(State::Forces | State::Energy);
    State state2 = context2.getState(State::Forces | State::Energy);
    bool exceptionThrown = false;
    try {
        // This should throw an exception.
170
171
        compareForcesEnergy(testName, state1.getPotentialEnergy(), state2.getPotentialEnergy(), state1.getForces(),
                            state2.getForces(), tolerance);
172
173
174
175
176
177
178
    }
    catch (std::exception ex) {
        exceptionThrown = true;
    }
    ASSERT(exceptionThrown);
    amoebaWcaDispersionForce->updateParametersInContext(context);
    state1 = context.getState(State::Forces | State::Energy);
179
180
    compareForcesEnergy(testName, state1.getPotentialEnergy(), state2.getPotentialEnergy(), state1.getForces(),
                        state2.getForces(), tolerance);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}

void setupKernels(int argc, char* argv[]);
void runPlatformTests();

int main(int argc, char* argv[]) {
    try {
        setupKernels(argc, argv);
        testWcaDispersionAmmonia();
        runPlatformTests();
    }
    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;
}