System.cpp 5.2 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
38
39

using namespace OpenMM;

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

System::~System() {
    for (int i = 0; i < (int) forces.size(); ++i)
        delete forces[i];
Peter Eastman's avatar
Peter Eastman committed
49
50
    for (int i = 0; i < (int) virtualSites.size(); ++i)
        delete virtualSites[i];
51
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
67
68
69
70
        virtualSites.resize(getNumParticles(), NULL);
    virtualSites[index] = virtualSite;
}

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

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

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

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

95
96
97
98
99
100
101
102
103
104
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];
}

105
void System::getDefaultPeriodicBoxVectors(Vec3& a, Vec3& b, Vec3& c) const {
106
107
108
109
110
    a = periodicBoxVectors[0];
    b = periodicBoxVectors[1];
    c = periodicBoxVectors[2];
}

111
void System::setDefaultPeriodicBoxVectors(const Vec3& a, const Vec3& b, const Vec3& c) {
112
113
114
115
116
117
118
119
120
121
    if (a[1] != 0.0 || a[2] != 0.0)
        throw OpenMMException("First periodic box vector must be parallel to x.");
    if (b[0] != 0.0 || b[2] != 0.0)
        throw OpenMMException("Second periodic box vector must be parallel to y.");
    if (c[0] != 0.0 || c[1] != 0.0)
        throw OpenMMException("Third periodic box vector must be parallel to z.");
    periodicBoxVectors[0] = a;
    periodicBoxVectors[1] = b;
    periodicBoxVectors[2] = c;
}