CpuCustomGBForce.h 12.9 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

/* Portions copyright (c) 2009-2014 Stanford University and Simbios.
 * Contributors: Peter Eastman
 *
 * 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.
 */

#ifndef OPENMM_CPU_CUSTOM_GB_FORCE_H__
#define OPENMM_CPU_CUSTOM_GB_FORCE_H__

28
#include "CpuNeighborList.h"
29
30
#include "lepton/CompiledExpression.h"
#include "openmm/CustomGBForce.h"
31
#include "openmm/internal/CompiledExpressionSet.h"
peastman's avatar
peastman committed
32
#include "openmm/internal/ThreadPool.h"
33
#include "openmm/internal/vectorize.h"
34
35
36
37
#include <map>
#include <set>
#include <vector>

38
39
namespace OpenMM {

40
41
class CpuCustomGBForce {
private:
peastman's avatar
peastman committed
42
43
44
    class ComputeForceTask;
    class ThreadData;

45
46
    bool cutoff;
    bool periodic;
47
    const CpuNeighborList* neighborList;
48
    float periodicBoxSize[3];
49
    float cutoffDistance, cutoffDistance2;
50
    int numValues, numParams;
peastman's avatar
peastman committed
51
    const std::vector<std::set<int> > exclusions;
52
53
    std::vector<CustomGBForce::ComputationType> valueTypes;
    std::vector<CustomGBForce::ComputationType> energyTypes;
peastman's avatar
peastman committed
54
55
56
    ThreadPool& threads;
    std::vector<ThreadData*> threadData;
    std::vector<double> threadEnergy;
57
    std::vector<std::vector<std::vector<float> > > dValuedParam;
58
    // Workspace vectors
59
    std::vector<std::vector<float> > values, dEdV;
peastman's avatar
peastman committed
60
61
62
63
64
65
66
67
68
69
70
71
72
    // The following variables are used to make information accessible to the individual threads.
    int numberOfAtoms;
    float* posq;
    RealOpenMM** atomParameters;
    const std::map<std::string, double>* globalParameters;
    std::vector<AlignedArray<float> >* threadForce;
    bool includeForce, includeEnergy;
    void* atomicCounter;
    
    /**
     * This routine contains the code executed by each thread.
     */
    void threadComputeForce(ThreadPool& threads, int threadIndex);
73
74
75
76
77

    /**
     * Calculate a computed value that is based on particle pairs
     * 
     * @param index            the index of the value to compute
peastman's avatar
peastman committed
78
     * @param data             workspace for the current thread
79
     * @param numAtoms         number of atoms
80
     * @param posq             atom coordinates
81
82
83
84
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     * @param useExclusions    specifies whether to use exclusions
     */

peastman's avatar
peastman committed
85
86
    void calculateParticlePairValue(int index, ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters,
                                    bool useExclusions, const fvec4& boxSize, const fvec4& invBoxSize);
87
88
89
90
91
92
93

    /**
     * Evaluate a single atom pair as part of calculating a computed value
     * 
     * @param index            the index of the value to compute
     * @param atom1            the index of the first atom in the pair
     * @param atom2            the index of the second atom in the pair
peastman's avatar
peastman committed
94
     * @param data             workspace for the current thread
95
     * @param posq             atom coordinates
96
97
98
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     */

peastman's avatar
peastman committed
99
100
    void calculateOnePairValue(int index, int atom1, int atom2, ThreadData& data, float* posq, RealOpenMM** atomParameters,
                               std::vector<float>& valueArray, const fvec4& boxSize, const fvec4& invBoxSize);
101
102
103
104
105

    /**
     * Calculate an energy term of type SingleParticle
     * 
     * @param index            the index of the value to compute
peastman's avatar
peastman committed
106
     * @param data             workspace for the current thread
107
     * @param numAtoms         number of atoms
108
     * @param posq             atom coordinates
109
110
111
112
113
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     * @param forces           forces on atoms are added to this
     * @param totalEnergy      the energy contribution is added to this
     */

peastman's avatar
peastman committed
114
    void calculateSingleParticleEnergyTerm(int index, ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters, float* forces, double& totalEnergy);
115
116
117
118
119

    /**
     * Calculate an energy term that is based on particle pairs
     * 
     * @param index            the index of the term to compute
peastman's avatar
peastman committed
120
     * @param data             workspace for the current thread
121
     * @param numAtoms         number of atoms
122
     * @param posq             atom coordinates
123
124
125
126
127
128
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     * @param useExclusions    specifies whether to use exclusions
     * @param forces           forces on atoms are added to this
     * @param totalEnergy      the energy contribution is added to this
     */

peastman's avatar
peastman committed
129
130
    void calculateParticlePairEnergyTerm(int index, ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters,
                                    bool useExclusions, float* forces, double& totalEnergy, const fvec4& boxSize, const fvec4& invBoxSize);
131
132
133
134
135
136
137

    /**
     * Evaluate a single atom pair as part of calculating an energy term
     * 
     * @param index            the index of the term to compute
     * @param atom1            the index of the first atom in the pair
     * @param atom2            the index of the second atom in the pair
peastman's avatar
peastman committed
138
     * @param data             workspace for the current thread
139
     * @param posq             atom coordinates
140
141
142
143
144
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     * @param forces           forces on atoms are added to this
     * @param totalEnergy      the energy contribution is added to this
     */

peastman's avatar
peastman committed
145
146
    void calculateOnePairEnergyTerm(int index, int atom1, int atom2, ThreadData& data, float* posq, RealOpenMM** atomParameters,
                               float* forces, double& totalEnergy, const fvec4& boxSize, const fvec4& invBoxSize);
147
148
149
150

    /**
     * Apply the chain rule to compute forces on atoms
     * 
peastman's avatar
peastman committed
151
     * @param data             workspace for the current thread
152
     * @param numAtoms         number of atoms
153
     * @param posq             atom coordinates
154
155
156
157
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     * @param forces           forces on atoms are added to this
     */

peastman's avatar
peastman committed
158
159
    void calculateChainRuleForces(ThreadData& data, int numAtoms, float* posq, RealOpenMM** atomParameters,
                                    float* forces, const fvec4& boxSize, const fvec4& invBoxSize);
160
161
162
163
164
165

    /**
     * Evaluate a single atom pair as part of applying the chain rule
     * 
     * @param atom1            the index of the first atom in the pair
     * @param atom2            the index of the second atom in the pair
peastman's avatar
peastman committed
166
     * @param data             workspace for the current thread
167
     * @param posq             atom coordinates
168
169
170
171
172
     * @param atomParameters   atomParameters[atomIndex][paramterIndex]
     * @param forces           forces on atoms are added to this
     * @param isExcluded       specifies whether this is an excluded pair
     */

peastman's avatar
peastman committed
173
174
    void calculateOnePairChainRule(int atom1, int atom2, ThreadData& data, float* posq, RealOpenMM** atomParameters,
                               float* forces, bool isExcluded, const fvec4& boxSize, const fvec4& invBoxSize);
175
176
177
178
179
180

    /**
     * Compute the displacement and squared distance between two points, optionally using
     * periodic boundary conditions.
     */
    void getDeltaR(const fvec4& posI, const fvec4& posJ, fvec4& deltaR, float& r2, bool periodic, const fvec4& boxSize, const fvec4& invBoxSize) const;
181
182
183
184
185
186
187

public:

    /**
     * Construct a new CpuCustomGBForce.
     */

peastman's avatar
peastman committed
188
189
190
191
     CpuCustomGBForce(int numAtoms, const std::vector<std::set<int> >& exclusions,
                        const std::vector<Lepton::CompiledExpression>& valueExpressions,
                        const std::vector<std::vector<Lepton::CompiledExpression> >& valueDerivExpressions,
                        const std::vector<std::vector<Lepton::CompiledExpression> >& valueGradientExpressions,
192
                        const std::vector<std::vector<Lepton::CompiledExpression> >& valueParamDerivExpressions,
peastman's avatar
peastman committed
193
194
195
196
197
                        const std::vector<std::string>& valueNames,
                        const std::vector<CustomGBForce::ComputationType>& valueTypes,
                        const std::vector<Lepton::CompiledExpression>& energyExpressions,
                        const std::vector<std::vector<Lepton::CompiledExpression> >& energyDerivExpressions,
                        const std::vector<std::vector<Lepton::CompiledExpression> >& energyGradientExpressions,
198
                        const std::vector<std::vector<Lepton::CompiledExpression> >& energyParamDerivExpressions,
peastman's avatar
peastman committed
199
200
                        const std::vector<CustomGBForce::ComputationType>& energyTypes,
                        const std::vector<std::string>& parameterNames, ThreadPool& threads);
201
202
203
204
205
206
207
208
209
210

     ~CpuCustomGBForce();

    /**
     * Set the force to use a cutoff.
     * 
     * @param distance            the cutoff distance
     * @param neighbors           the neighbor list to use
     */

211
    void setUseCutoff(float distance, const CpuNeighborList& neighbors);
212
213
214
215
216
217
218
219
220

    /**
     * Set the force to use periodic boundary conditions.  This requires that a cutoff has
     * already been set, and the smallest side of the periodic box is at least twice the cutoff
     * distance.
     * 
     * @param boxSize             the X, Y, and Z widths of the periodic box
     */

221
    void setPeriodic(RealVec& boxSize);
222
223
224
225

    /**
     * Calculate custom GB ixn
     * 
226
227
228
229
230
231
232
     * @param numberOfAtoms      number of atoms
     * @param posq               atom coordinates
     * @param atomParameters     atomParameters[atomIndex][paramterIndex]
     * @param globalParameters   the values of global parameters
     * @param forces             force array (forces added)
     * @param totalEnergy        total energy
     * @param energyParamDerivs  derivatives of the energy with respect to global parameters
233
234
     */

235
236
    void calculateIxn(int numberOfAtoms, float* posq, RealOpenMM** atomParameters, std::map<std::string, double>& globalParameters,
            std::vector<AlignedArray<float> >& threadForce, bool includeForce, bool includeEnergy, double& totalEnergy, double* energyParamDerivs);
peastman's avatar
peastman committed
237
238
239
240
241
242
243
244
};

class CpuCustomGBForce::ThreadData {
public:
    ThreadData(int numAtoms, int numThreads, int threadIndex,
               const std::vector<Lepton::CompiledExpression>& valueExpressions,
               const std::vector<std::vector<Lepton::CompiledExpression> >& valueDerivExpressions,
               const std::vector<std::vector<Lepton::CompiledExpression> >& valueGradientExpressions,
245
               const std::vector<std::vector<Lepton::CompiledExpression> >& valueParamDerivExpressions,
peastman's avatar
peastman committed
246
247
248
249
               const std::vector<std::string>& valueNames,
               const std::vector<Lepton::CompiledExpression>& energyExpressions,
               const std::vector<std::vector<Lepton::CompiledExpression> >& energyDerivExpressions,
               const std::vector<std::vector<Lepton::CompiledExpression> >& energyGradientExpressions,
250
               const std::vector<std::vector<Lepton::CompiledExpression> >& energyParamDerivExpressions,
peastman's avatar
peastman committed
251
252
253
254
255
               const std::vector<std::string>& parameterNames);
    CompiledExpressionSet expressionSet;
    std::vector<Lepton::CompiledExpression> valueExpressions;
    std::vector<std::vector<Lepton::CompiledExpression> > valueDerivExpressions;
    std::vector<std::vector<Lepton::CompiledExpression> > valueGradientExpressions;
256
    std::vector<std::vector<Lepton::CompiledExpression> > valueParamDerivExpressions;
peastman's avatar
peastman committed
257
258
259
260
    std::vector<int> valueIndex;
    std::vector<Lepton::CompiledExpression> energyExpressions;
    std::vector<std::vector<Lepton::CompiledExpression> > energyDerivExpressions;
    std::vector<std::vector<Lepton::CompiledExpression> > energyGradientExpressions;
261
    std::vector<std::vector<Lepton::CompiledExpression> > energyParamDerivExpressions;
peastman's avatar
peastman committed
262
263
264
265
266
267
268
269
    std::vector<int> paramIndex;
    std::vector<int> particleParamIndex;
    std::vector<int> particleValueIndex;
    int xindex, yindex, zindex, rindex;
    int firstAtom, lastAtom;
    // Workspace vectors
    std::vector<float> value0, dVdR1, dVdR2, dVdX, dVdY, dVdZ;
    std::vector<std::vector<float> > dEdV;
270
271
    std::vector<std::vector<float> > dValue0dParam;
    std::vector<float> energyParamDerivs;
272
273
};

274
275
} // namespace OpenMM

276
#endif // OPENMM_CPU_CUSTOM_GB_FORCE_H__