TabulatedFunction.cpp 13.2 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
/* -------------------------------------------------------------------------- *
 *                                   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) 2014 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.                                     *
 * -------------------------------------------------------------------------- */

#include "openmm/TabulatedFunction.h"
#include "openmm/OpenMMException.h"

using namespace OpenMM;
using namespace std;

38
39
40
Continuous1DFunction::Continuous1DFunction(const vector<double>& values, double min, double max, bool periodic) {
    this->periodic = periodic;
    setFunctionParameters(values, min, max);
41
42
}

43
void Continuous1DFunction::getFunctionParameters(vector<double>& values, double& min, double& max) const {
44
45
46
47
48
    values = this->values;
    min = this->min;
    max = this->max;
}

49
50
51
52
void Continuous1DFunction::getPeriodicityStatus(bool& periodic) const {
    periodic = this->periodic;
}

53
void Continuous1DFunction::setFunctionParameters(const vector<double>& values, double min, double max) {
54
55
    if (max <= min)
        throw OpenMMException("Continuous1DFunction: max <= min for a tabulated function.");
56
57
58
59
60
61
62
63
64
    int n = values.size();
    if (periodic) {
        if (n < 3)
            throw OpenMMException("Continuous1DFunction: a periodic tabulated function must have at least three points");
        if (values[0] != values[n-1])
            throw OpenMMException("Continuous1DFunction: with periodic=true, the first and last points must have the same value");
    }
    else if (n < 2)
        throw OpenMMException("Continuous1DFunction: a non-periodic tabulated function must have at least two points");
65
66
67
68
    this->values = values;
    this->min = min;
    this->max = max;
}
peastman's avatar
peastman committed
69

70
Continuous1DFunction* Continuous1DFunction::Copy() const {
71
72
73
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
74
    return new Continuous1DFunction(new_vec, min, max);
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
ContinuousPeriodic1DFunction::ContinuousPeriodic1DFunction(const vector<double>& values, double min, double max) {
    if (max <= min)
        throw OpenMMException("ContinuousPeriodic1DFunction: max <= min for a tabulated function.");
    int n = values.size();
    if (n < 3)
        throw OpenMMException("ContinuousPeriodic1DFunction: a periodic tabulated function must have at least three points");
    if (values[0] != values[n-1])
        throw OpenMMException("ContinuousPeriodic1DFunction: the first and last points must have the same value");
    this->values = values;
    this->min = min;
    this->max = max;
}

void ContinuousPeriodic1DFunction::getFunctionParameters(vector<double>& values, double& min, double& max) const {
    values = this->values;
    min = this->min;
    max = this->max;
}

void ContinuousPeriodic1DFunction::setFunctionParameters(const vector<double>& values, double min, double max) {
    if (max <= min)
        throw OpenMMException("ContinuousPeriodic1DFunction: max <= min for a tabulated function.");
    int n = values.size();
    if (n < 3)
        throw OpenMMException("ContinuousPeriodic1DFunction: a periodic tabulated function must have at least three points");
    if (values[0] != values[n-1])
        throw OpenMMException("ContinuousPeriodic1DFunction: the first and last points must have the same value");
    this->values = values;
    this->min = min;
    this->max = max;
}

ContinuousPeriodic1DFunction* ContinuousPeriodic1DFunction::Copy() const {
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
    return new ContinuousPeriodic1DFunction(new_vec, min, max);
}

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Continuous2DFunction::Continuous2DFunction(int xsize, int ysize, const vector<double>& values, double xmin, double xmax, double ymin, double ymax) {
    if (xsize < 2 || ysize < 2)
        throw OpenMMException("Continuous2DFunction: must have at least two points along each axis");
    if (values.size() != xsize*ysize)
        throw OpenMMException("Continuous2DFunction: incorrect number of values");
    if (xmax <= xmin)
        throw OpenMMException("Continuous2DFunction: xmax <= xmin for a tabulated function.");
    if (ymax <= ymin)
        throw OpenMMException("Continuous2DFunction: ymax <= ymin for a tabulated function.");
    this->values = values;
    this->xsize = xsize;
    this->ysize = ysize;
    this->xmin = xmin;
    this->xmax = xmax;
    this->ymin = ymin;
    this->ymax = ymax;
}

void Continuous2DFunction::getFunctionParameters(int& xsize, int& ysize, vector<double>& values, double& xmin, double& xmax, double& ymin, double& ymax) const {
    values = this->values;
    xsize = this->xsize;
    ysize = this->ysize;
    xmin = this->xmin;
    xmax = this->xmax;
    ymin = this->ymin;
    ymax = this->ymax;
}

void Continuous2DFunction::setFunctionParameters(int xsize, int ysize, const vector<double>& values, double xmin, double xmax, double ymin, double ymax) {
    if (xsize < 2 || ysize < 2)
        throw OpenMMException("Continuous2DFunction: must have at least two points along each axis");
    if (values.size() != xsize*ysize)
        throw OpenMMException("Continuous2DFunction: incorrect number of values");
    if (xmax <= xmin)
        throw OpenMMException("Continuous2DFunction: xmax <= xmin for a tabulated function.");
    if (ymax <= ymin)
        throw OpenMMException("Continuous2DFunction: ymax <= ymin for a tabulated function.");
    this->values = values;
    this->xsize = xsize;
    this->ysize = ysize;
    this->xmin = xmin;
    this->xmax = xmax;
    this->ymin = ymin;
    this->ymax = ymax;
}

162
Continuous2DFunction* Continuous2DFunction::Copy() const {
163
164
165
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
166
    return new Continuous2DFunction(xsize, ysize, new_vec, xmin, xmax, ymin, ymax);
167
168
}

169
170
171
172
173
174
175
176
177
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
Continuous3DFunction::Continuous3DFunction(int xsize, int ysize, int zsize, const vector<double>& values, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) {
    if (xsize < 2 || ysize < 2 || zsize < 2)
        throw OpenMMException("Continuous3DFunction: must have at least two points along each axis");
    if (values.size() != xsize*ysize*zsize)
        throw OpenMMException("Continuous3DFunction: incorrect number of values");
    if (xmax <= xmin)
        throw OpenMMException("Continuous3DFunction: xmax <= xmin for a tabulated function.");
    if (ymax <= ymin)
        throw OpenMMException("Continuous3DFunction: ymax <= ymin for a tabulated function.");
    if (zmax <= zmin)
        throw OpenMMException("Continuous3DFunction: zmax <= zmin for a tabulated function.");
    this->values = values;
    this->xsize = xsize;
    this->ysize = ysize;
    this->zsize = zsize;
    this->xmin = xmin;
    this->xmax = xmax;
    this->ymin = ymin;
    this->ymax = ymax;
    this->zmin = zmin;
    this->zmax = zmax;
}

void Continuous3DFunction::getFunctionParameters(int& xsize, int& ysize, int& zsize, vector<double>& values, double& xmin, double& xmax, double& ymin, double& ymax, double& zmin, double& zmax) const {
    values = this->values;
    xsize = this->xsize;
    ysize = this->ysize;
    zsize = this->zsize;
    xmin = this->xmin;
    xmax = this->xmax;
    ymin = this->ymin;
    ymax = this->ymax;
    zmin = this->zmin;
    zmax = this->zmax;
}

void Continuous3DFunction::setFunctionParameters(int xsize, int ysize, int zsize, const vector<double>& values, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) {
    if (xsize < 2 || ysize < 2 || zsize < 2)
        throw OpenMMException("Continuous3DFunction: must have at least two points along each axis");
    if (values.size() != xsize*ysize*zsize)
        throw OpenMMException("Continuous3DFunction: incorrect number of values");
    if (xmax <= xmin)
        throw OpenMMException("Continuous3DFunction: xmax <= xmin for a tabulated function.");
    if (ymax <= ymin)
        throw OpenMMException("Continuous3DFunction: ymax <= ymin for a tabulated function.");
    if (zmax <= zmin)
        throw OpenMMException("Continuous3DFunction: zmax <= zmin for a tabulated function.");
    this->values = values;
    this->xsize = xsize;
    this->ysize = ysize;
    this->zsize = zsize;
    this->xmin = xmin;
    this->xmax = xmax;
    this->ymin = ymin;
    this->ymax = ymax;
    this->zmin = zmin;
    this->zmax = zmax;
}

228
Continuous3DFunction* Continuous3DFunction::Copy() const {
229
230
231
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
232
    return new Continuous3DFunction(xsize, ysize, zsize, new_vec, xmin, xmax, ymin, ymax, zmin, zmax);
233
234
235
}


236
Discrete1DFunction::Discrete1DFunction(const vector<double>& values) {
peastman's avatar
peastman committed
237
238
239
    this->values = values;
}

240
void Discrete1DFunction::getFunctionParameters(vector<double>& values) const {
peastman's avatar
peastman committed
241
242
243
    values = this->values;
}

244
void Discrete1DFunction::setFunctionParameters(const vector<double>& values) {
peastman's avatar
peastman committed
245
246
    this->values = values;
}
247

248
Discrete1DFunction* Discrete1DFunction::Copy() const {
249
250
251
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
252
    return new Discrete1DFunction(new_vec);
253
254
}

255
Discrete2DFunction::Discrete2DFunction(int xsize, int ysize, const vector<double>& values) {
256
257
258
259
260
261
262
    if (values.size() != xsize*ysize)
        throw OpenMMException("Discrete2DFunction: incorrect number of values");
    this->xsize = xsize;
    this->ysize = ysize;
    this->values = values;
}

263
void Discrete2DFunction::getFunctionParameters(int& xsize, int& ysize, vector<double>& values) const {
264
265
266
267
268
    xsize = this->xsize;
    ysize = this->ysize;
    values = this->values;
}

269
void Discrete2DFunction::setFunctionParameters(int xsize, int ysize, const vector<double>& values) {
270
271
272
273
274
275
276
    if (values.size() != xsize*ysize)
        throw OpenMMException("Discrete2DFunction: incorrect number of values");
    this->xsize = xsize;
    this->ysize = ysize;
    this->values = values;
}

277
Discrete2DFunction* Discrete2DFunction::Copy() const {
278
279
280
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
281
    return new Discrete2DFunction(xsize, ysize, new_vec);
282
283
}

284
Discrete3DFunction::Discrete3DFunction(int xsize, int ysize, int zsize, const vector<double>& values) {
285
286
287
288
289
290
291
292
    if (values.size() != xsize*ysize*zsize)
        throw OpenMMException("Discrete3DFunction: incorrect number of values");
    this->xsize = xsize;
    this->ysize = ysize;
    this->zsize = zsize;
    this->values = values;
}

293
void Discrete3DFunction::getFunctionParameters(int& xsize, int& ysize, int& zsize, vector<double>& values) const {
294
295
296
297
298
299
    xsize = this->xsize;
    ysize = this->ysize;
    zsize = this->zsize;
    values = this->values;
}

300
void Discrete3DFunction::setFunctionParameters(int xsize, int ysize, int zsize, const vector<double>& values) {
301
302
303
304
305
306
307
    if (values.size() != xsize*ysize*zsize)
        throw OpenMMException("Discrete3DFunction: incorrect number of values");
    this->xsize = xsize;
    this->ysize = ysize;
    this->zsize = zsize;
    this->values = values;
}
308

309
Discrete3DFunction* Discrete3DFunction::Copy() const {
310
311
312
    vector<double> new_vec(values.size());
    for (size_t i = 0; i < values.size(); i++)
        new_vec[i] = values[i];
313
    return new Discrete3DFunction(xsize, ysize, zsize, new_vec);
314
}