Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
bf0b43c5
Commit
bf0b43c5
authored
Aug 11, 2016
by
peastman
Browse files
Optimization to CustomNonbondedForce and CustomIntegrator on CPU
parent
1c416f54
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
81 additions
and
71 deletions
+81
-71
platforms/cpu/include/CpuCustomNonbondedForce.h
platforms/cpu/include/CpuCustomNonbondedForce.h
+2
-2
platforms/cpu/src/CpuCustomGBForce.cpp
platforms/cpu/src/CpuCustomGBForce.cpp
+6
-4
platforms/cpu/src/CpuCustomNonbondedForce.cpp
platforms/cpu/src/CpuCustomNonbondedForce.cpp
+21
-15
platforms/reference/include/ReferenceCustomDynamics.h
platforms/reference/include/ReferenceCustomDynamics.h
+5
-4
platforms/reference/src/SimTKReference/ReferenceCustomDynamics.cpp
.../reference/src/SimTKReference/ReferenceCustomDynamics.cpp
+47
-46
No files found.
platforms/cpu/include/CpuCustomNonbondedForce.h
View file @
bf0b43c5
...
@@ -183,8 +183,8 @@ public:
...
@@ -183,8 +183,8 @@ public:
Lepton
::
CompiledExpression
forceExpression
;
Lepton
::
CompiledExpression
forceExpression
;
std
::
vector
<
Lepton
::
CompiledExpression
>
energyParamDerivExpressions
;
std
::
vector
<
Lepton
::
CompiledExpression
>
energyParamDerivExpressions
;
CompiledExpressionSet
expressionSet
;
CompiledExpressionSet
expressionSet
;
std
::
vector
<
int
>
particleParam
Index
;
std
::
vector
<
double
>
particleParam
;
int
rIndex
;
double
r
;
std
::
vector
<
RealOpenMM
>
energyParamDerivs
;
std
::
vector
<
RealOpenMM
>
energyParamDerivs
;
};
};
...
...
platforms/cpu/src/CpuCustomGBForce.cpp
View file @
bf0b43c5
...
@@ -513,7 +513,8 @@ void CpuCustomGBForce::calculateOnePairEnergyTerm(int index, int atom1, int atom
...
@@ -513,7 +513,8 @@ void CpuCustomGBForce::calculateOnePairEnergyTerm(int index, int atom1, int atom
getDeltaR
(
pos2
,
pos1
,
deltaR
,
r2
,
periodic
,
boxSize
,
invBoxSize
);
getDeltaR
(
pos2
,
pos1
,
deltaR
,
r2
,
periodic
,
boxSize
,
invBoxSize
);
if
(
cutoff
&&
r2
>=
cutoffDistance2
)
if
(
cutoff
&&
r2
>=
cutoffDistance2
)
return
;
return
;
data
.
r
=
sqrtf
(
r2
);
float
r
=
sqrtf
(
r2
);
data
.
r
=
r
;
// Record variables for evaluating expressions.
// Record variables for evaluating expressions.
...
@@ -531,7 +532,7 @@ void CpuCustomGBForce::calculateOnePairEnergyTerm(int index, int atom1, int atom
...
@@ -531,7 +532,7 @@ void CpuCustomGBForce::calculateOnePairEnergyTerm(int index, int atom1, int atom
if
(
includeEnergy
)
if
(
includeEnergy
)
totalEnergy
+=
(
float
)
data
.
energyExpressions
[
index
].
evaluate
();
totalEnergy
+=
(
float
)
data
.
energyExpressions
[
index
].
evaluate
();
float
dEdR
=
(
float
)
data
.
energyDerivExpressions
[
index
][
0
].
evaluate
();
float
dEdR
=
(
float
)
data
.
energyDerivExpressions
[
index
][
0
].
evaluate
();
dEdR
*=
1
/
data
.
r
;
dEdR
*=
1
/
r
;
fvec4
result
=
deltaR
*
dEdR
;
fvec4
result
=
deltaR
*
dEdR
;
(
fvec4
(
forces
+
4
*
atom1
)
-
result
).
store
(
forces
+
4
*
atom1
);
(
fvec4
(
forces
+
4
*
atom1
)
-
result
).
store
(
forces
+
4
*
atom1
);
(
fvec4
(
forces
+
4
*
atom2
)
+
result
).
store
(
forces
+
4
*
atom2
);
(
fvec4
(
forces
+
4
*
atom2
)
+
result
).
store
(
forces
+
4
*
atom2
);
...
@@ -634,7 +635,8 @@ void CpuCustomGBForce::calculateOnePairChainRule(int atom1, int atom2, ThreadDat
...
@@ -634,7 +635,8 @@ void CpuCustomGBForce::calculateOnePairChainRule(int atom1, int atom2, ThreadDat
getDeltaR
(
pos2
,
pos1
,
deltaR
,
r2
,
periodic
,
boxSize
,
invBoxSize
);
getDeltaR
(
pos2
,
pos1
,
deltaR
,
r2
,
periodic
,
boxSize
,
invBoxSize
);
if
(
cutoff
&&
r2
>=
cutoffDistance2
)
if
(
cutoff
&&
r2
>=
cutoffDistance2
)
return
;
return
;
data
.
r
=
sqrtf
(
r2
);
float
r
=
sqrtf
(
r2
);
data
.
r
=
r
;
// Record variables for evaluating expressions.
// Record variables for evaluating expressions.
...
@@ -652,7 +654,7 @@ void CpuCustomGBForce::calculateOnePairChainRule(int atom1, int atom2, ThreadDat
...
@@ -652,7 +654,7 @@ void CpuCustomGBForce::calculateOnePairChainRule(int atom1, int atom2, ThreadDat
// Evaluate the derivative of each parameter with respect to position and apply forces.
// Evaluate the derivative of each parameter with respect to position and apply forces.
float
rinv
=
1
/
data
.
r
;
float
rinv
=
1
/
r
;
deltaR
*=
rinv
;
deltaR
*=
rinv
;
fvec4
f1
(
0.0
f
),
f2
(
0.0
f
);
fvec4
f1
(
0.0
f
),
f2
(
0.0
f
);
if
(
!
isExcluded
||
valueTypes
[
0
]
!=
CustomGBForce
::
ParticlePair
)
{
if
(
!
isExcluded
||
valueTypes
[
0
]
!=
CustomGBForce
::
ParticlePair
)
{
...
...
platforms/cpu/src/CpuCustomNonbondedForce.cpp
View file @
bf0b43c5
...
@@ -46,19 +46,25 @@ public:
...
@@ -46,19 +46,25 @@ public:
CpuCustomNonbondedForce
::
ThreadData
::
ThreadData
(
const
Lepton
::
CompiledExpression
&
energyExpression
,
const
Lepton
::
CompiledExpression
&
forceExpression
,
CpuCustomNonbondedForce
::
ThreadData
::
ThreadData
(
const
Lepton
::
CompiledExpression
&
energyExpression
,
const
Lepton
::
CompiledExpression
&
forceExpression
,
const
vector
<
string
>&
parameterNames
,
const
std
::
vector
<
Lepton
::
CompiledExpression
>
energyParamDerivExpressions
)
:
const
vector
<
string
>&
parameterNames
,
const
std
::
vector
<
Lepton
::
CompiledExpression
>
energyParamDerivExpressions
)
:
energyExpression
(
energyExpression
),
forceExpression
(
forceExpression
),
energyParamDerivExpressions
(
energyParamDerivExpressions
)
{
energyExpression
(
energyExpression
),
forceExpression
(
forceExpression
),
energyParamDerivExpressions
(
energyParamDerivExpressions
)
{
expressionSet
.
registerExpression
(
this
->
energyExpression
);
map
<
string
,
double
*>
variableLocations
;
expressionSet
.
registerExpression
(
this
->
forceExpression
);
variableLocations
[
"r"
]
=
&
r
;
for
(
int
i
=
0
;
i
<
this
->
energyParamDerivExpressions
.
size
();
i
++
)
particleParam
.
resize
(
2
*
parameterNames
.
size
());
expressionSet
.
registerExpression
(
this
->
energyParamDerivExpressions
[
i
]);
rIndex
=
expressionSet
.
getVariableIndex
(
"r"
);
for
(
int
i
=
0
;
i
<
(
int
)
parameterNames
.
size
();
i
++
)
{
for
(
int
i
=
0
;
i
<
(
int
)
parameterNames
.
size
();
i
++
)
{
for
(
int
j
=
1
;
j
<
3
;
j
++
)
{
for
(
int
j
=
0
;
j
<
2
;
j
++
)
{
stringstream
name
;
stringstream
name
;
name
<<
parameterNames
[
i
]
<<
j
;
name
<<
parameterNames
[
i
]
<<
(
j
+
1
)
;
p
ar
ticleParamIndex
.
push_back
(
expressionSet
.
getVariableIndex
(
name
.
str
()))
;
v
ar
iableLocations
[
name
.
str
()]
=
&
particleParam
[
i
*
2
+
j
]
;
}
}
}
}
energyParamDerivs
.
resize
(
energyParamDerivExpressions
.
size
());
energyParamDerivs
.
resize
(
energyParamDerivExpressions
.
size
());
this
->
energyExpression
.
setVariableLocations
(
variableLocations
);
this
->
forceExpression
.
setVariableLocations
(
variableLocations
);
expressionSet
.
registerExpression
(
this
->
energyExpression
);
expressionSet
.
registerExpression
(
this
->
forceExpression
);
for
(
int
i
=
0
;
i
<
this
->
energyParamDerivExpressions
.
size
();
i
++
)
{
this
->
energyParamDerivExpressions
[
i
].
setVariableLocations
(
variableLocations
);
expressionSet
.
registerExpression
(
this
->
energyParamDerivExpressions
[
i
]);
}
}
}
CpuCustomNonbondedForce
::
CpuCustomNonbondedForce
(
const
Lepton
::
CompiledExpression
&
energyExpression
,
CpuCustomNonbondedForce
::
CpuCustomNonbondedForce
(
const
Lepton
::
CompiledExpression
&
energyExpression
,
...
@@ -187,8 +193,8 @@ void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int thread
...
@@ -187,8 +193,8 @@ void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int thread
int
atom1
=
groupInteractions
[
i
].
first
;
int
atom1
=
groupInteractions
[
i
].
first
;
int
atom2
=
groupInteractions
[
i
].
second
;
int
atom2
=
groupInteractions
[
i
].
second
;
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
{
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
{
data
.
expressionSet
.
setVariable
(
data
.
particleParam
Index
[
j
*
2
]
,
atomParameters
[
atom1
][
j
]
)
;
data
.
particleParam
[
j
*
2
]
=
atomParameters
[
atom1
][
j
];
data
.
expressionSet
.
setVariable
(
data
.
particleParam
Index
[
j
*
2
+
1
]
,
atomParameters
[
atom2
][
j
]
)
;
data
.
particleParam
[
j
*
2
+
1
]
=
atomParameters
[
atom2
][
j
];
}
}
calculateOneIxn
(
atom1
,
atom2
,
data
,
forces
,
energy
,
boxSize
,
invBoxSize
);
calculateOneIxn
(
atom1
,
atom2
,
data
,
forces
,
energy
,
boxSize
,
invBoxSize
);
}
}
...
@@ -207,12 +213,12 @@ void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int thread
...
@@ -207,12 +213,12 @@ void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int thread
for
(
int
i
=
0
;
i
<
(
int
)
neighbors
.
size
();
i
++
)
{
for
(
int
i
=
0
;
i
<
(
int
)
neighbors
.
size
();
i
++
)
{
int
first
=
neighbors
[
i
];
int
first
=
neighbors
[
i
];
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
data
.
expressionSet
.
setVariable
(
data
.
particleParam
Index
[
j
*
2
]
,
atomParameters
[
first
][
j
]
)
;
data
.
particleParam
[
j
*
2
]
=
atomParameters
[
first
][
j
];
for
(
int
k
=
0
;
k
<
blockSize
;
k
++
)
{
for
(
int
k
=
0
;
k
<
blockSize
;
k
++
)
{
if
((
exclusions
[
i
]
&
(
1
<<
k
))
==
0
)
{
if
((
exclusions
[
i
]
&
(
1
<<
k
))
==
0
)
{
int
second
=
blockAtom
[
k
];
int
second
=
blockAtom
[
k
];
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
data
.
expressionSet
.
setVariable
(
data
.
particleParam
Index
[
j
*
2
+
1
]
,
atomParameters
[
second
][
j
]
)
;
data
.
particleParam
[
j
*
2
+
1
]
=
atomParameters
[
second
][
j
];
calculateOneIxn
(
first
,
second
,
data
,
forces
,
energy
,
boxSize
,
invBoxSize
);
calculateOneIxn
(
first
,
second
,
data
,
forces
,
energy
,
boxSize
,
invBoxSize
);
}
}
}
}
...
@@ -229,8 +235,8 @@ void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int thread
...
@@ -229,8 +235,8 @@ void CpuCustomNonbondedForce::threadComputeForce(ThreadPool& threads, int thread
for
(
int
jj
=
ii
+
1
;
jj
<
numberOfAtoms
;
jj
++
)
{
for
(
int
jj
=
ii
+
1
;
jj
<
numberOfAtoms
;
jj
++
)
{
if
(
exclusions
[
jj
].
find
(
ii
)
==
exclusions
[
jj
].
end
())
{
if
(
exclusions
[
jj
].
find
(
ii
)
==
exclusions
[
jj
].
end
())
{
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
{
for
(
int
j
=
0
;
j
<
(
int
)
paramNames
.
size
();
j
++
)
{
data
.
expressionSet
.
setVariable
(
data
.
particleParam
Index
[
j
*
2
]
,
atomParameters
[
ii
][
j
]
)
;
data
.
particleParam
[
j
*
2
]
=
atomParameters
[
ii
][
j
];
data
.
expressionSet
.
setVariable
(
data
.
particleParam
Index
[
j
*
2
+
1
]
,
atomParameters
[
jj
][
j
]
)
;
data
.
particleParam
[
j
*
2
+
1
]
=
atomParameters
[
jj
][
j
];
}
}
calculateOneIxn
(
ii
,
jj
,
data
,
forces
,
energy
,
boxSize
,
invBoxSize
);
calculateOneIxn
(
ii
,
jj
,
data
,
forces
,
energy
,
boxSize
,
invBoxSize
);
}
}
...
@@ -251,10 +257,10 @@ void CpuCustomNonbondedForce::calculateOneIxn(int ii, int jj, ThreadData& data,
...
@@ -251,10 +257,10 @@ void CpuCustomNonbondedForce::calculateOneIxn(int ii, int jj, ThreadData& data,
if
(
cutoff
&&
r2
>=
cutoffDistance
*
cutoffDistance
)
if
(
cutoff
&&
r2
>=
cutoffDistance
*
cutoffDistance
)
return
;
return
;
float
r
=
sqrtf
(
r2
);
float
r
=
sqrtf
(
r2
);
data
.
r
=
r
;
// accumulate forces
// accumulate forces
data
.
expressionSet
.
setVariable
(
data
.
rIndex
,
r
);
double
dEdR
=
(
includeForce
?
data
.
forceExpression
.
evaluate
()
/
r
:
0.0
);
double
dEdR
=
(
includeForce
?
data
.
forceExpression
.
evaluate
()
/
r
:
0.0
);
double
energy
=
(
includeEnergy
?
data
.
energyExpression
.
evaluate
()
:
0.0
);
double
energy
=
(
includeEnergy
?
data
.
energyExpression
.
evaluate
()
:
0.0
);
double
switchValue
=
1.0
;
double
switchValue
=
1.0
;
...
...
platforms/reference/include/ReferenceCustomDynamics.h
View file @
bf0b43c5
...
@@ -51,13 +51,14 @@ private:
...
@@ -51,13 +51,14 @@ private:
std
::
vector
<
CustomIntegratorUtilities
::
Comparison
>
comparisons
;
std
::
vector
<
CustomIntegratorUtilities
::
Comparison
>
comparisons
;
std
::
vector
<
bool
>
invalidatesForces
,
needsForces
,
needsEnergy
,
computeBothForceAndEnergy
;
std
::
vector
<
bool
>
invalidatesForces
,
needsForces
,
needsEnergy
,
computeBothForceAndEnergy
;
std
::
vector
<
int
>
forceGroupFlags
,
blockEnd
;
std
::
vector
<
int
>
forceGroupFlags
,
blockEnd
;
RealOpenMM
energy
;
std
::
map
<
std
::
string
,
double
>
energyParamDerivs
;
std
::
map
<
std
::
string
,
double
>
energyParamDerivs
;
Lepton
::
CompiledExpression
kineticEnergyExpression
;
Lepton
::
CompiledExpression
kineticEnergyExpression
;
bool
kineticEnergyNeedsForce
;
bool
kineticEnergyNeedsForce
;
CompiledExpressionSet
expressionSet
;
CompiledExpressionSet
expressionSet
;
int
xIndex
,
vIndex
,
mIndex
,
fIndex
,
energyIndex
,
gaussianIndex
,
uniformIndex
;
double
x
,
v
,
m
,
f
,
energy
,
gaussian
,
uniform
;
std
::
vector
<
int
>
forceVariableIndex
,
energyVariableIndex
,
perDofVariableIndex
,
stepVariableIndex
;
int
xIndex
,
vIndex
;
std
::
vector
<
int
>
perDofVariableIndex
,
stepVariableIndex
;
std
::
vector
<
double
>
perDofVariable
;
void
initialize
(
OpenMM
::
ContextImpl
&
context
,
std
::
vector
<
RealOpenMM
>&
masses
,
std
::
map
<
std
::
string
,
RealOpenMM
>&
globals
);
void
initialize
(
OpenMM
::
ContextImpl
&
context
,
std
::
vector
<
RealOpenMM
>&
masses
,
std
::
map
<
std
::
string
,
RealOpenMM
>&
globals
);
...
@@ -65,7 +66,7 @@ private:
...
@@ -65,7 +66,7 @@ private:
void
computePerDof
(
int
numberOfAtoms
,
std
::
vector
<
OpenMM
::
RealVec
>&
results
,
const
std
::
vector
<
OpenMM
::
RealVec
>&
atomCoordinates
,
void
computePerDof
(
int
numberOfAtoms
,
std
::
vector
<
OpenMM
::
RealVec
>&
results
,
const
std
::
vector
<
OpenMM
::
RealVec
>&
atomCoordinates
,
const
std
::
vector
<
OpenMM
::
RealVec
>&
velocities
,
const
std
::
vector
<
OpenMM
::
RealVec
>&
forces
,
const
std
::
vector
<
RealOpenMM
>&
masses
,
const
std
::
vector
<
OpenMM
::
RealVec
>&
velocities
,
const
std
::
vector
<
OpenMM
::
RealVec
>&
forces
,
const
std
::
vector
<
RealOpenMM
>&
masses
,
const
std
::
vector
<
std
::
vector
<
OpenMM
::
RealVec
>
>&
perDof
,
const
Lepton
::
CompiledExpression
&
expression
,
int
forceIndex
);
const
std
::
vector
<
std
::
vector
<
OpenMM
::
RealVec
>
>&
perDof
,
const
Lepton
::
CompiledExpression
&
expression
);
void
recordChangedParameters
(
OpenMM
::
ContextImpl
&
context
,
std
::
map
<
std
::
string
,
RealOpenMM
>&
globals
);
void
recordChangedParameters
(
OpenMM
::
ContextImpl
&
context
,
std
::
map
<
std
::
string
,
RealOpenMM
>&
globals
);
...
...
platforms/reference/src/SimTKReference/ReferenceCustomDynamics.cpp
View file @
bf0b43c5
...
@@ -78,11 +78,6 @@ ReferenceCustomDynamics::ReferenceCustomDynamics(int numberOfAtoms, const Custom
...
@@ -78,11 +78,6 @@ ReferenceCustomDynamics::ReferenceCustomDynamics(int numberOfAtoms, const Custom
string
expression
;
string
expression
;
integrator
.
getComputationStep
(
i
,
stepType
[
i
],
stepVariable
[
i
],
expression
);
integrator
.
getComputationStep
(
i
,
stepType
[
i
],
stepVariable
[
i
],
expression
);
}
}
kineticEnergyExpression
=
Parser
::
parse
(
integrator
.
getKineticEnergyExpression
()).
optimize
().
createCompiledExpression
();
expressionSet
.
registerExpression
(
kineticEnergyExpression
);
kineticEnergyNeedsForce
=
false
;
if
(
kineticEnergyExpression
.
getVariables
().
find
(
"f"
)
!=
kineticEnergyExpression
.
getVariables
().
end
())
kineticEnergyNeedsForce
=
true
;
}
}
/**---------------------------------------------------------------------------------------
/**---------------------------------------------------------------------------------------
...
@@ -98,6 +93,28 @@ void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<RealOpenMM
...
@@ -98,6 +93,28 @@ void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<RealOpenMM
// Some initialization can't be done in the constructor, since we need a ContextImpl from which to get the list of
// Some initialization can't be done in the constructor, since we need a ContextImpl from which to get the list of
// Context parameters. Instead, we do it the first time update() or computeKineticEnergy() is called.
// Context parameters. Instead, we do it the first time update() or computeKineticEnergy() is called.
std
::
map
<
std
::
string
,
double
*>
variableLocations
;
variableLocations
[
"x"
]
=
&
x
;
variableLocations
[
"v"
]
=
&
v
;
variableLocations
[
"m"
]
=
&
m
;
variableLocations
[
"f"
]
=
&
f
;
variableLocations
[
"energy"
]
=
&
energy
;
variableLocations
[
"gaussian"
]
=
&
gaussian
;
variableLocations
[
"uniform"
]
=
&
uniform
;
perDofVariable
.
resize
(
integrator
.
getNumPerDofVariables
());
for
(
int
i
=
0
;
i
<
integrator
.
getNumPerDofVariables
();
i
++
)
variableLocations
[
integrator
.
getPerDofVariableName
(
i
)]
=
&
perDofVariable
[
i
];
for
(
int
i
=
0
;
i
<
32
;
i
++
)
{
stringstream
fname
;
fname
<<
"f"
<<
i
;
variableLocations
[
fname
.
str
()]
=
&
f
;
stringstream
ename
;
ename
<<
"energy"
<<
i
;
variableLocations
[
ename
.
str
()]
=
&
energy
;
}
// Parse the expressions.
int
numSteps
=
stepType
.
size
();
int
numSteps
=
stepType
.
size
();
vector
<
int
>
forceGroup
;
vector
<
int
>
forceGroup
;
vector
<
vector
<
ParsedExpression
>
>
expressions
;
vector
<
vector
<
ParsedExpression
>
>
expressions
;
...
@@ -107,37 +124,25 @@ void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<RealOpenMM
...
@@ -107,37 +124,25 @@ void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<RealOpenMM
stepExpressions
[
i
].
resize
(
expressions
[
i
].
size
());
stepExpressions
[
i
].
resize
(
expressions
[
i
].
size
());
for
(
int
j
=
0
;
j
<
(
int
)
expressions
[
i
].
size
();
j
++
)
{
for
(
int
j
=
0
;
j
<
(
int
)
expressions
[
i
].
size
();
j
++
)
{
stepExpressions
[
i
][
j
]
=
ParsedExpression
(
replaceDerivFunctions
(
expressions
[
i
][
j
].
getRootNode
(),
context
)).
createCompiledExpression
();
stepExpressions
[
i
][
j
]
=
ParsedExpression
(
replaceDerivFunctions
(
expressions
[
i
][
j
].
getRootNode
(),
context
)).
createCompiledExpression
();
stepExpressions
[
i
][
j
].
setVariableLocations
(
variableLocations
);
expressionSet
.
registerExpression
(
stepExpressions
[
i
][
j
]);
expressionSet
.
registerExpression
(
stepExpressions
[
i
][
j
]);
}
}
if
(
stepType
[
i
]
==
CustomIntegrator
::
WhileBlockStart
)
if
(
stepType
[
i
]
==
CustomIntegrator
::
WhileBlockStart
)
blockEnd
[
blockEnd
[
i
]]
=
i
;
// Record where to branch back to.
blockEnd
[
blockEnd
[
i
]]
=
i
;
// Record where to branch back to.
}
}
kineticEnergyExpression
=
Parser
::
parse
(
integrator
.
getKineticEnergyExpression
()).
optimize
().
createCompiledExpression
();
kineticEnergyExpression
.
setVariableLocations
(
variableLocations
);
expressionSet
.
registerExpression
(
kineticEnergyExpression
);
kineticEnergyNeedsForce
=
false
;
if
(
kineticEnergyExpression
.
getVariables
().
find
(
"f"
)
!=
kineticEnergyExpression
.
getVariables
().
end
())
kineticEnergyNeedsForce
=
true
;
// Record the
variable names and flags for the force and energy in
each step.
// Record the
force group flags for
each step.
forceGroupFlags
.
resize
(
numSteps
,
-
1
);
forceGroupFlags
.
resize
(
numSteps
,
-
1
);
fIndex
=
expressionSet
.
getVariableIndex
(
"f"
);
for
(
int
i
=
0
;
i
<
numSteps
;
i
++
)
energyIndex
=
expressionSet
.
getVariableIndex
(
"energy"
);
forceVariableIndex
.
resize
(
numSteps
,
fIndex
);
energyVariableIndex
.
resize
(
numSteps
,
energyIndex
);
vector
<
string
>
forceGroupName
;
vector
<
string
>
energyGroupName
;
for
(
int
i
=
0
;
i
<
32
;
i
++
)
{
stringstream
fname
;
fname
<<
"f"
<<
i
;
forceGroupName
.
push_back
(
fname
.
str
());
stringstream
ename
;
ename
<<
"energy"
<<
i
;
energyGroupName
.
push_back
(
ename
.
str
());
}
for
(
int
i
=
0
;
i
<
numSteps
;
i
++
)
{
if
(
needsForces
[
i
]
&&
forceGroup
[
i
]
>
-
1
)
forceVariableIndex
[
i
]
=
expressionSet
.
getVariableIndex
(
forceGroupName
[
forceGroup
[
i
]]);
if
(
needsEnergy
[
i
]
&&
forceGroup
[
i
]
>
-
1
)
energyVariableIndex
[
i
]
=
expressionSet
.
getVariableIndex
(
energyGroupName
[
forceGroup
[
i
]]);
if
(
forceGroup
[
i
]
>
-
1
)
if
(
forceGroup
[
i
]
>
-
1
)
forceGroupFlags
[
i
]
=
1
<<
forceGroup
[
i
];
forceGroupFlags
[
i
]
=
1
<<
forceGroup
[
i
];
}
// Build the list of inverse masses.
// Build the list of inverse masses.
...
@@ -150,13 +155,10 @@ void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<RealOpenMM
...
@@ -150,13 +155,10 @@ void ReferenceCustomDynamics::initialize(ContextImpl& context, vector<RealOpenMM
inverseMasses
[
i
]
=
1.0
/
masses
[
i
];
inverseMasses
[
i
]
=
1.0
/
masses
[
i
];
}
}
// Record indices of
other
variables.
// Record indices of variables.
xIndex
=
expressionSet
.
getVariableIndex
(
"x"
);
xIndex
=
expressionSet
.
getVariableIndex
(
"x"
);
vIndex
=
expressionSet
.
getVariableIndex
(
"v"
);
vIndex
=
expressionSet
.
getVariableIndex
(
"v"
);
mIndex
=
expressionSet
.
getVariableIndex
(
"m"
);
gaussianIndex
=
expressionSet
.
getVariableIndex
(
"gaussian"
);
uniformIndex
=
expressionSet
.
getVariableIndex
(
"uniform"
);
for
(
int
i
=
0
;
i
<
integrator
.
getNumPerDofVariables
();
i
++
)
for
(
int
i
=
0
;
i
<
integrator
.
getNumPerDofVariables
();
i
++
)
perDofVariableIndex
.
push_back
(
expressionSet
.
getVariableIndex
(
integrator
.
getPerDofVariableName
(
i
)));
perDofVariableIndex
.
push_back
(
expressionSet
.
getVariableIndex
(
integrator
.
getPerDofVariableName
(
i
)));
for
(
int
i
=
0
;
i
<
stepVariable
.
size
();
i
++
)
for
(
int
i
=
0
;
i
<
stepVariable
.
size
();
i
++
)
...
@@ -222,15 +224,14 @@ void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, ve
...
@@ -222,15 +224,14 @@ void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, ve
}
}
forcesAreValid
=
true
;
forcesAreValid
=
true
;
}
}
expressionSet
.
setVariable
(
energyVariableIndex
[
step
],
energy
);
// Execute the step.
// Execute the step.
int
nextStep
=
step
+
1
;
int
nextStep
=
step
+
1
;
switch
(
stepType
[
step
])
{
switch
(
stepType
[
step
])
{
case
CustomIntegrator
::
ComputeGlobal
:
{
case
CustomIntegrator
::
ComputeGlobal
:
{
expressionSet
.
setVariable
(
uniform
Index
,
SimTKOpenMMUtilities
::
getUniformlyDistributedRandomNumber
()
)
;
uniform
=
SimTKOpenMMUtilities
::
getUniformlyDistributedRandomNumber
();
expressionSet
.
setVariable
(
gaussian
Index
,
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
()
)
;
gaussian
=
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
();
RealOpenMM
result
=
stepExpressions
[
step
][
0
].
evaluate
();
RealOpenMM
result
=
stepExpressions
[
step
][
0
].
evaluate
();
globals
[
stepVariable
[
step
]]
=
result
;
globals
[
stepVariable
[
step
]]
=
result
;
expressionSet
.
setVariable
(
stepVariableIndex
[
step
],
result
);
expressionSet
.
setVariable
(
stepVariableIndex
[
step
],
result
);
...
@@ -249,11 +250,11 @@ void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, ve
...
@@ -249,11 +250,11 @@ void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, ve
}
}
if
(
results
==
NULL
)
if
(
results
==
NULL
)
throw
OpenMMException
(
"Illegal per-DOF output variable: "
+
stepVariable
[
step
]);
throw
OpenMMException
(
"Illegal per-DOF output variable: "
+
stepVariable
[
step
]);
computePerDof
(
numberOfAtoms
,
*
results
,
atomCoordinates
,
velocities
,
forces
,
masses
,
perDof
,
stepExpressions
[
step
][
0
]
,
forceVariableIndex
[
step
]
);
computePerDof
(
numberOfAtoms
,
*
results
,
atomCoordinates
,
velocities
,
forces
,
masses
,
perDof
,
stepExpressions
[
step
][
0
]);
break
;
break
;
}
}
case
CustomIntegrator
::
ComputeSum
:
{
case
CustomIntegrator
::
ComputeSum
:
{
computePerDof
(
numberOfAtoms
,
sumBuffer
,
atomCoordinates
,
velocities
,
forces
,
masses
,
perDof
,
stepExpressions
[
step
][
0
]
,
forceVariableIndex
[
step
]
);
computePerDof
(
numberOfAtoms
,
sumBuffer
,
atomCoordinates
,
velocities
,
forces
,
masses
,
perDof
,
stepExpressions
[
step
][
0
]);
RealOpenMM
sum
=
0.0
;
RealOpenMM
sum
=
0.0
;
for
(
int
j
=
0
;
j
<
numberOfAtoms
;
j
++
)
for
(
int
j
=
0
;
j
<
numberOfAtoms
;
j
++
)
if
(
masses
[
j
]
!=
0.0
)
if
(
masses
[
j
]
!=
0.0
)
...
@@ -306,22 +307,22 @@ void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, ve
...
@@ -306,22 +307,22 @@ void ReferenceCustomDynamics::update(ContextImpl& context, int numberOfAtoms, ve
void
ReferenceCustomDynamics
::
computePerDof
(
int
numberOfAtoms
,
vector
<
RealVec
>&
results
,
const
vector
<
RealVec
>&
atomCoordinates
,
void
ReferenceCustomDynamics
::
computePerDof
(
int
numberOfAtoms
,
vector
<
RealVec
>&
results
,
const
vector
<
RealVec
>&
atomCoordinates
,
const
vector
<
RealVec
>&
velocities
,
const
vector
<
RealVec
>&
forces
,
const
vector
<
RealOpenMM
>&
masses
,
const
vector
<
RealVec
>&
velocities
,
const
vector
<
RealVec
>&
forces
,
const
vector
<
RealOpenMM
>&
masses
,
const
vector
<
vector
<
RealVec
>
>&
perDof
,
const
CompiledExpression
&
expression
,
int
forceIndex
)
{
const
vector
<
vector
<
RealVec
>
>&
perDof
,
const
CompiledExpression
&
expression
)
{
// Loop over all degrees of freedom.
// Loop over all degrees of freedom.
for
(
int
i
=
0
;
i
<
numberOfAtoms
;
i
++
)
{
for
(
int
i
=
0
;
i
<
numberOfAtoms
;
i
++
)
{
if
(
masses
[
i
]
!=
0.0
)
{
if
(
masses
[
i
]
!=
0.0
)
{
expressionSet
.
setVariable
(
mIndex
,
masses
[
i
]
)
;
m
=
masses
[
i
];
for
(
int
j
=
0
;
j
<
3
;
j
++
)
{
for
(
int
j
=
0
;
j
<
3
;
j
++
)
{
// Compute the expression.
// Compute the expression.
expressionSet
.
setVariable
(
xIndex
,
atomCoordinates
[
i
][
j
]
)
;
x
=
atomCoordinates
[
i
][
j
];
expressionSet
.
setVariable
(
vIndex
,
velocities
[
i
][
j
]
)
;
v
=
velocities
[
i
][
j
];
expressionSet
.
setVariable
(
forceIndex
,
forces
[
i
][
j
]
)
;
f
=
forces
[
i
][
j
];
expressionSet
.
setVariable
(
uniform
Index
,
SimTKOpenMMUtilities
::
getUniformlyDistributedRandomNumber
()
)
;
uniform
=
SimTKOpenMMUtilities
::
getUniformlyDistributedRandomNumber
();
expressionSet
.
setVariable
(
gaussian
Index
,
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
()
)
;
gaussian
=
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
();
for
(
int
k
=
0
;
k
<
(
int
)
perDof
.
size
();
k
++
)
for
(
int
k
=
0
;
k
<
(
int
)
perDof
.
size
();
k
++
)
expressionSet
.
setVariable
(
perDofVariable
Index
[
k
]
,
perDof
[
k
][
i
][
j
]
)
;
perDofVariable
[
k
]
=
perDof
[
k
][
i
][
j
];
results
[
i
][
j
]
=
expression
.
evaluate
();
results
[
i
][
j
]
=
expression
.
evaluate
();
}
}
}
}
...
@@ -329,8 +330,8 @@ void ReferenceCustomDynamics::computePerDof(int numberOfAtoms, vector<RealVec>&
...
@@ -329,8 +330,8 @@ void ReferenceCustomDynamics::computePerDof(int numberOfAtoms, vector<RealVec>&
}
}
bool
ReferenceCustomDynamics
::
evaluateCondition
(
int
step
)
{
bool
ReferenceCustomDynamics
::
evaluateCondition
(
int
step
)
{
expressionSet
.
setVariable
(
uniform
Index
,
SimTKOpenMMUtilities
::
getUniformlyDistributedRandomNumber
()
)
;
uniform
=
SimTKOpenMMUtilities
::
getUniformlyDistributedRandomNumber
();
expressionSet
.
setVariable
(
gaussian
Index
,
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
()
)
;
gaussian
=
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
();
double
lhs
=
stepExpressions
[
step
][
0
].
evaluate
();
double
lhs
=
stepExpressions
[
step
][
0
].
evaluate
();
double
rhs
=
stepExpressions
[
step
][
1
].
evaluate
();
double
rhs
=
stepExpressions
[
step
][
1
].
evaluate
();
switch
(
comparisons
[
step
])
{
switch
(
comparisons
[
step
])
{
...
@@ -390,7 +391,7 @@ double ReferenceCustomDynamics::computeKineticEnergy(OpenMM::ContextImpl& contex
...
@@ -390,7 +391,7 @@ double ReferenceCustomDynamics::computeKineticEnergy(OpenMM::ContextImpl& contex
energy
=
context
.
calcForcesAndEnergy
(
true
,
true
,
-
1
);
energy
=
context
.
calcForcesAndEnergy
(
true
,
true
,
-
1
);
forcesAreValid
=
true
;
forcesAreValid
=
true
;
}
}
computePerDof
(
numberOfAtoms
,
sumBuffer
,
atomCoordinates
,
velocities
,
forces
,
masses
,
perDof
,
kineticEnergyExpression
,
fIndex
);
computePerDof
(
numberOfAtoms
,
sumBuffer
,
atomCoordinates
,
velocities
,
forces
,
masses
,
perDof
,
kineticEnergyExpression
);
RealOpenMM
sum
=
0.0
;
RealOpenMM
sum
=
0.0
;
for
(
int
j
=
0
;
j
<
numberOfAtoms
;
j
++
)
for
(
int
j
=
0
;
j
<
numberOfAtoms
;
j
++
)
if
(
masses
[
j
]
!=
0.0
)
if
(
masses
[
j
]
!=
0.0
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment