"...ssh:/git@developer.sourcefind.cn:2222/tsoc/openmm.git" did not exist on "a62b6bff8ad247678dd74eadfb749c63bb896bcb"
System.cpp 6.16 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
Peter Eastman's avatar
Peter Eastman committed
9
 * Portions copyright (c) 2008-2012 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * 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.                                     *
 * -------------------------------------------------------------------------- */

32
#include "openmm/Force.h"
33
#include "openmm/OpenMMException.h"
34
#include "openmm/System.h"
35
36
#include "openmm/VirtualSite.h"
#include "openmm/internal/AssertionUtilities.h"
37
#include <cmath>
38
39
40

using namespace OpenMM;

41
System::System() {
42
43
44
    periodicBoxVectors[0] = Vec3(2, 0, 0);
    periodicBoxVectors[1] = Vec3(0, 2, 0);
    periodicBoxVectors[2] = Vec3(0, 0, 2);
45
46
47
}

System::~System() {
peastman's avatar
peastman committed
48
49
50
51
    for (auto force : forces)
        delete force;
    for (auto site : virtualSites)
        delete site;
52
53
}

54
55
56
57
58
59
60
61
62
63
double System::getParticleMass(int index) const {
    ASSERT_VALID_INDEX(index, masses);
    return masses[index];
}

void System::setParticleMass(int index, double mass) {
    ASSERT_VALID_INDEX(index, masses);
    masses[index] = mass;
}

64
void System::setVirtualSite(int index, VirtualSite* virtualSite) {
65
    if (index >= (int) virtualSites.size())
66
        virtualSites.resize(getNumParticles(), NULL);
67
68
    if (virtualSites[index] != NULL)
        delete virtualSites[index];
69
70
71
72
    virtualSites[index] = virtualSite;
}

const VirtualSite& System::getVirtualSite(int index) const {
73
    if (index >= (int) virtualSites.size() || virtualSites[index] == NULL)
74
75
76
77
        throw OpenMMException("This particle is not a virtual site");
    return *virtualSites[index];
}

78
int System::addConstraint(int particle1, int particle2, double distance) {
79
    constraints.push_back(ConstraintInfo(particle1, particle2, distance));
80
    return constraints.size()-1;
81
82
}

Peter Eastman's avatar
Peter Eastman committed
83
void System::getConstraintParameters(int index, int& particle1, int& particle2, double& distance) const {
84
    ASSERT_VALID_INDEX(index, constraints);
Peter Eastman's avatar
Peter Eastman committed
85
86
    particle1 = constraints[index].particle1;
    particle2 = constraints[index].particle2;
87
88
89
    distance = constraints[index].distance;
}

Peter Eastman's avatar
Peter Eastman committed
90
void System::setConstraintParameters(int index, int particle1, int particle2, double distance) {
91
    ASSERT_VALID_INDEX(index, constraints);
Peter Eastman's avatar
Peter Eastman committed
92
93
    constraints[index].particle1 = particle1;
    constraints[index].particle2 = particle2;
94
95
    constraints[index].distance = distance;
}
96

97
98
99
100
101
void System::removeConstraint(int index) {
    ASSERT_VALID_INDEX(index, constraints);
    constraints.erase(constraints.begin()+index);
}

102
103
104
105
106
107
108
109
110
111
const Force& System::getForce(int index) const {
    ASSERT_VALID_INDEX(index, forces);
    return *forces[index];
}

Force& System::getForce(int index) {
    ASSERT_VALID_INDEX(index, forces);
    return *forces[index];
}

112
113
114
115
116
117
void System::removeForce(int index) {
    ASSERT_VALID_INDEX(index, forces);
    delete forces[index];
    forces.erase(forces.begin()+index);
}

118
void System::getDefaultPeriodicBoxVectors(Vec3& a, Vec3& b, Vec3& c) const {
119
120
121
122
123
    a = periodicBoxVectors[0];
    b = periodicBoxVectors[1];
    c = periodicBoxVectors[2];
}

124
void System::setDefaultPeriodicBoxVectors(const Vec3& a, const Vec3& b, const Vec3& c) {
125
126
    if (a[1] != 0.0 || a[2] != 0.0)
        throw OpenMMException("First periodic box vector must be parallel to x.");
127
128
129
130
    if (b[2] != 0.0)
        throw OpenMMException("Second periodic box vector must be in the x-y plane.");
    if (a[0] <= 0.0 || b[1] <= 0.0 || c[2] <= 0.0 || a[0] < 2*fabs(b[0]) || a[0] < 2*fabs(c[0]) || b[1] < 2*fabs(c[1]))
        throw OpenMMException("Periodic box vectors must be in reduced form.");
131
132
133
134
    periodicBoxVectors[0] = a;
    periodicBoxVectors[1] = b;
    periodicBoxVectors[2] = c;
}
135

136
bool System::usesPeriodicBoundaryConditions() const {
137
138
139
140

    bool uses_pbc = false;
    bool all_forces_implement = true;
    for (std::vector<Force*>::const_iterator it = forces.begin();
141
            it != forces.end(); ++it) {
142
143
144
        try {
            if ((*it)->usesPeriodicBoundaryConditions())
                uses_pbc = true;
145
146
        }
        catch (OpenMMException &e) {
147
148
149
150
151
152
153
154
155
156
            all_forces_implement = false;
        }
    }

    if (!all_forces_implement && !uses_pbc) {
        throw OpenMMException("not all forces implement usesPeriodicBoundaryConditions");
    }

    return uses_pbc;
}