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
8cc22e17
Commit
8cc22e17
authored
Oct 30, 2012
by
Peter Eastman
Browse files
Added Context::applyVelocityConstraints() and Context::setVelocitiesToTemperature()
parent
d253ef4f
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
257 additions
and
1 deletion
+257
-1
olla/include/openmm/kernels.h
olla/include/openmm/kernels.h
+7
-0
openmmapi/include/openmm/Context.h
openmmapi/include/openmm/Context.h
+15
-0
openmmapi/include/openmm/internal/ContextImpl.h
openmmapi/include/openmm/internal/ContextImpl.h
+6
-0
openmmapi/src/Context.cpp
openmmapi/src/Context.cpp
+41
-0
openmmapi/src/ContextImpl.cpp
openmmapi/src/ContextImpl.cpp
+4
-0
platforms/cuda/src/CudaKernels.cpp
platforms/cuda/src/CudaKernels.cpp
+4
-0
platforms/cuda/src/CudaKernels.h
platforms/cuda/src/CudaKernels.h
+7
-0
platforms/cuda/tests/TestCudaRandom.cpp
platforms/cuda/tests/TestCudaRandom.cpp
+48
-0
platforms/opencl/src/OpenCLKernels.cpp
platforms/opencl/src/OpenCLKernels.cpp
+4
-0
platforms/opencl/src/OpenCLKernels.h
platforms/opencl/src/OpenCLKernels.h
+7
-0
platforms/opencl/tests/TestOpenCLRandom.cpp
platforms/opencl/tests/TestOpenCLRandom.cpp
+48
-0
platforms/reference/src/ReferenceKernels.cpp
platforms/reference/src/ReferenceKernels.cpp
+12
-0
platforms/reference/src/ReferenceKernels.h
platforms/reference/src/ReferenceKernels.h
+7
-0
platforms/reference/tests/TestReferenceRandom.cpp
platforms/reference/tests/TestReferenceRandom.cpp
+47
-1
No files found.
olla/include/openmm/kernels.h
View file @
8cc22e17
...
@@ -222,6 +222,13 @@ public:
...
@@ -222,6 +222,13 @@ public:
* @param tol the distance tolerance within which constraints must be satisfied.
* @param tol the distance tolerance within which constraints must be satisfied.
*/
*/
virtual
void
apply
(
ContextImpl
&
context
,
double
tol
)
=
0
;
virtual
void
apply
(
ContextImpl
&
context
,
double
tol
)
=
0
;
/**
* Update particle velocities to enforce constraints.
*
* @param context the context in which to execute this kernel
* @param tol the velocity tolerance within which constraints must be satisfied.
*/
virtual
void
applyToVelocities
(
ContextImpl
&
context
,
double
tol
)
=
0
;
};
};
/**
/**
...
...
openmmapi/include/openmm/Context.h
View file @
8cc22e17
...
@@ -35,6 +35,7 @@
...
@@ -35,6 +35,7 @@
#include "Integrator.h"
#include "Integrator.h"
#include "State.h"
#include "State.h"
#include "System.h"
#include "System.h"
#include <ctime>
#include <iosfwd>
#include <iosfwd>
#include <map>
#include <map>
#include <string>
#include <string>
...
@@ -148,6 +149,14 @@ public:
...
@@ -148,6 +149,14 @@ public:
* contains the velocity of the i'th particle.
* contains the velocity of the i'th particle.
*/
*/
void
setVelocities
(
const
std
::
vector
<
Vec3
>&
velocities
);
void
setVelocities
(
const
std
::
vector
<
Vec3
>&
velocities
);
/**
* Set the velocities of all particles in the System to random values chosen from a Boltzmann
* distribution at a given temperature.
*
* @param temperature the temperature for which to select the velocities (measured in Kelvin)
* @param randomSeed the random number seed to use when selecting velocities
*/
void
setVelocitiesToTemperature
(
double
temperature
,
int
randomSeed
=
time
(
NULL
));
/**
/**
* Get the value of an adjustable parameter defined by a Force object in the System.
* Get the value of an adjustable parameter defined by a Force object in the System.
*
*
...
@@ -180,6 +189,12 @@ public:
...
@@ -180,6 +189,12 @@ public:
* @param tol the distance tolerance within which constraints must be satisfied.
* @param tol the distance tolerance within which constraints must be satisfied.
*/
*/
void
applyConstraints
(
double
tol
);
void
applyConstraints
(
double
tol
);
/**
* Update the velocities of particles so the net velocity of each constrained distance is zero.
*
* @param tol the velocity tolerance within which constraints must be satisfied.
*/
void
applyVelocityConstraints
(
double
tol
);
/**
/**
* Recompute the locations of all virtual sites. There is rarely a reason to call
* Recompute the locations of all virtual sites. There is rarely a reason to call
* this, since virtual sites are also updated by applyConstraints(). This is only
* this, since virtual sites are also updated by applyConstraints(). This is only
...
...
openmmapi/include/openmm/internal/ContextImpl.h
View file @
8cc22e17
...
@@ -169,6 +169,12 @@ public:
...
@@ -169,6 +169,12 @@ public:
* @param tol the distance tolerance within which constraints must be satisfied.
* @param tol the distance tolerance within which constraints must be satisfied.
*/
*/
void
applyConstraints
(
double
tol
);
void
applyConstraints
(
double
tol
);
/**
* Update the velocities of particles so the net velocity of each constrained distance is zero.
*
* @param tol the velocity tolerance within which constraints must be satisfied.
*/
void
applyVelocityConstraints
(
double
tol
);
/**
/**
* Recompute the locations of all virtual sites. There is rarely a reason to call
* Recompute the locations of all virtual sites. There is rarely a reason to call
* this, since virtual sites are also updated by applyConstraints(). This is only
* this, since virtual sites are also updated by applyConstraints(). This is only
...
...
openmmapi/src/Context.cpp
View file @
8cc22e17
...
@@ -33,6 +33,8 @@
...
@@ -33,6 +33,8 @@
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/OpenMMException.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/ForceImpl.h"
#include "openmm/internal/ForceImpl.h"
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
#include "sfmt/SFMT.h"
#include <cmath>
#include <cmath>
using
namespace
OpenMM
;
using
namespace
OpenMM
;
...
@@ -151,6 +153,41 @@ void Context::setVelocities(const vector<Vec3>& velocities) {
...
@@ -151,6 +153,41 @@ void Context::setVelocities(const vector<Vec3>& velocities) {
impl
->
setVelocities
(
velocities
);
impl
->
setVelocities
(
velocities
);
}
}
void
Context
::
setVelocitiesToTemperature
(
double
temperature
,
int
randomSeed
)
{
System
&
system
=
impl
->
getSystem
();
// Generate the list of Gaussian random numbers.
OpenMM_SFMT
::
SFMT
sfmt
;
init_gen_rand
(
randomSeed
,
sfmt
);
vector
<
double
>
randoms
;
while
(
randoms
.
size
()
<
system
.
getNumParticles
()
*
3
)
{
double
x
,
y
,
r2
;
do
{
x
=
2.0
*
genrand_real2
(
sfmt
)
-
1.0
;
y
=
2.0
*
genrand_real2
(
sfmt
)
-
1.0
;
r2
=
x
*
x
+
y
*
y
;
}
while
(
r2
>=
1.0
||
r2
==
0.0
);
double
multiplier
=
sqrt
((
-
2.0
*
log
(
r2
))
/
r2
);
randoms
.
push_back
(
x
*
multiplier
);
randoms
.
push_back
(
y
*
multiplier
);
}
// Assign the velocities.
vector
<
Vec3
>
velocities
(
system
.
getNumParticles
(),
Vec3
());
int
nextRandom
=
0
;
for
(
int
i
=
0
;
i
<
system
.
getNumParticles
();
i
++
)
{
double
mass
=
system
.
getParticleMass
(
i
);
if
(
mass
!=
0
)
{
double
velocityScale
=
sqrt
(
BOLTZ
*
temperature
/
mass
);
velocities
[
i
]
=
Vec3
(
randoms
[
nextRandom
++
],
randoms
[
nextRandom
++
],
randoms
[
nextRandom
++
])
*
velocityScale
;
}
}
setVelocities
(
velocities
);
impl
->
applyVelocityConstraints
(
1e-5
);
}
double
Context
::
getParameter
(
const
string
&
name
)
{
double
Context
::
getParameter
(
const
string
&
name
)
{
return
impl
->
getParameter
(
name
);
return
impl
->
getParameter
(
name
);
}
}
...
@@ -167,6 +204,10 @@ void Context::applyConstraints(double tol) {
...
@@ -167,6 +204,10 @@ void Context::applyConstraints(double tol) {
impl
->
applyConstraints
(
tol
);
impl
->
applyConstraints
(
tol
);
}
}
void
Context
::
applyVelocityConstraints
(
double
tol
)
{
impl
->
applyVelocityConstraints
(
tol
);
}
void
Context
::
computeVirtualSites
()
{
void
Context
::
computeVirtualSites
()
{
impl
->
computeVirtualSites
();
impl
->
computeVirtualSites
();
}
}
...
...
openmmapi/src/ContextImpl.cpp
View file @
8cc22e17
...
@@ -192,6 +192,10 @@ void ContextImpl::applyConstraints(double tol) {
...
@@ -192,6 +192,10 @@ void ContextImpl::applyConstraints(double tol) {
applyConstraintsKernel
.
getAs
<
ApplyConstraintsKernel
>
().
apply
(
*
this
,
tol
);
applyConstraintsKernel
.
getAs
<
ApplyConstraintsKernel
>
().
apply
(
*
this
,
tol
);
}
}
void
ContextImpl
::
applyVelocityConstraints
(
double
tol
)
{
applyConstraintsKernel
.
getAs
<
ApplyConstraintsKernel
>
().
applyToVelocities
(
*
this
,
tol
);
}
void
ContextImpl
::
computeVirtualSites
()
{
void
ContextImpl
::
computeVirtualSites
()
{
virtualSitesKernel
.
getAs
<
VirtualSitesKernel
>
().
computePositions
(
*
this
);
virtualSitesKernel
.
getAs
<
VirtualSitesKernel
>
().
computePositions
(
*
this
);
}
}
...
...
platforms/cuda/src/CudaKernels.cpp
View file @
8cc22e17
...
@@ -399,6 +399,10 @@ void CudaApplyConstraintsKernel::apply(ContextImpl& context, double tol) {
...
@@ -399,6 +399,10 @@ void CudaApplyConstraintsKernel::apply(ContextImpl& context, double tol) {
integration
.
computeVirtualSites
();
integration
.
computeVirtualSites
();
}
}
void
CudaApplyConstraintsKernel
::
applyToVelocities
(
ContextImpl
&
context
,
double
tol
)
{
cu
.
getIntegrationUtilities
().
applyVelocityConstraints
(
tol
);
}
void
CudaVirtualSitesKernel
::
initialize
(
const
System
&
system
)
{
void
CudaVirtualSitesKernel
::
initialize
(
const
System
&
system
)
{
}
}
...
...
platforms/cuda/src/CudaKernels.h
View file @
8cc22e17
...
@@ -189,6 +189,13 @@ public:
...
@@ -189,6 +189,13 @@ public:
* @param tol the distance tolerance within which constraints must be satisfied.
* @param tol the distance tolerance within which constraints must be satisfied.
*/
*/
void
apply
(
ContextImpl
&
context
,
double
tol
);
void
apply
(
ContextImpl
&
context
,
double
tol
);
/**
* Update particle velocities to enforce constraints.
*
* @param context the context in which to execute this kernel
* @param tol the velocity tolerance within which constraints must be satisfied.
*/
void
applyToVelocities
(
ContextImpl
&
context
,
double
tol
);
private:
private:
CudaContext
&
cu
;
CudaContext
&
cu
;
bool
hasInitializedKernel
;
bool
hasInitializedKernel
;
...
...
platforms/cuda/tests/TestCudaRandom.cpp
View file @
8cc22e17
...
@@ -38,6 +38,10 @@
...
@@ -38,6 +38,10 @@
#include "../src/CudaContext.h"
#include "../src/CudaContext.h"
#include "../src/CudaIntegrationUtilities.h"
#include "../src/CudaIntegrationUtilities.h"
#include "openmm/System.h"
#include "openmm/System.h"
#include "openmm/Context.h"
#include "CudaPlatform.h"
#include "openmm/VerletIntegrator.h"
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
#include <iostream>
#include <iostream>
using
namespace
OpenMM
;
using
namespace
OpenMM
;
...
@@ -84,9 +88,53 @@ void testGaussian() {
...
@@ -84,9 +88,53 @@ void testGaussian() {
ASSERT_EQUAL_TOL
(
0.0
,
c4
,
3.0
/
pow
(
numValues
,
1.0
/
4.0
));
ASSERT_EQUAL_TOL
(
0.0
,
c4
,
3.0
/
pow
(
numValues
,
1.0
/
4.0
));
}
}
void
testRandomVelocities
()
{
// Create a system.
const
int
numParticles
=
10000
;
const
double
temperture
=
100.0
;
CudaPlatform
platform
;
System
system
;
VerletIntegrator
integrator
(
0.01
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
system
.
addParticle
(
10.0
+
sin
(
0.1
*
i
));
for
(
int
i
=
0
;
i
<
numParticles
-
1
;
++
i
)
system
.
addConstraint
(
i
,
i
+
1
,
1.0
);
Context
context
(
system
,
integrator
,
platform
);
vector
<
Vec3
>
positions
(
numParticles
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
positions
[
i
]
=
Vec3
(
i
/
2
,
(
i
+
1
)
/
2
,
0
);
context
.
setPositions
(
positions
);
// Ask the context to generate random velocities.
context
.
setVelocitiesToTemperature
(
temperture
);
State
state
=
context
.
getState
(
State
::
Velocities
);
// See if they respect constraints.
for
(
int
i
=
1
;
i
<
numParticles
;
i
++
)
{
Vec3
v1
=
state
.
getVelocities
()[
i
-
1
];
Vec3
v2
=
state
.
getVelocities
()[
i
];
double
vel
=
(
v1
-
v2
).
dot
(
positions
[
i
-
1
]
-
positions
[
i
]);
ASSERT_EQUAL_TOL
(
0.0
,
vel
,
2e-5
);
}
// See if the temperature is correct.
double
ke
=
0
;
for
(
int
i
=
0
;
i
<
numParticles
;
i
++
)
{
Vec3
v
=
state
.
getVelocities
()[
i
];
ke
+=
0.5
*
system
.
getParticleMass
(
i
)
*
v
.
dot
(
v
);
}
double
expected
=
0.5
*
(
numParticles
*
3
-
system
.
getNumConstraints
())
*
BOLTZ
*
temperture
;
ASSERT_USUALLY_EQUAL_TOL
(
expected
,
ke
,
4
/
sqrt
(
numParticles
));
}
int
main
()
{
int
main
()
{
try
{
try
{
testGaussian
();
testGaussian
();
testRandomVelocities
();
}
}
catch
(
const
exception
&
e
)
{
catch
(
const
exception
&
e
)
{
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
...
...
platforms/opencl/src/OpenCLKernels.cpp
View file @
8cc22e17
...
@@ -424,6 +424,10 @@ void OpenCLApplyConstraintsKernel::apply(ContextImpl& context, double tol) {
...
@@ -424,6 +424,10 @@ void OpenCLApplyConstraintsKernel::apply(ContextImpl& context, double tol) {
integration
.
computeVirtualSites
();
integration
.
computeVirtualSites
();
}
}
void
OpenCLApplyConstraintsKernel
::
applyToVelocities
(
ContextImpl
&
context
,
double
tol
)
{
cl
.
getIntegrationUtilities
().
applyVelocityConstraints
(
tol
);
}
void
OpenCLVirtualSitesKernel
::
initialize
(
const
System
&
system
)
{
void
OpenCLVirtualSitesKernel
::
initialize
(
const
System
&
system
)
{
}
}
...
...
platforms/opencl/src/OpenCLKernels.h
View file @
8cc22e17
...
@@ -189,6 +189,13 @@ public:
...
@@ -189,6 +189,13 @@ public:
* @param tol the distance tolerance within which constraints must be satisfied.
* @param tol the distance tolerance within which constraints must be satisfied.
*/
*/
void
apply
(
ContextImpl
&
context
,
double
tol
);
void
apply
(
ContextImpl
&
context
,
double
tol
);
/**
* Update particle velocities to enforce constraints.
*
* @param context the context in which to execute this kernel
* @param tol the velocity tolerance within which constraints must be satisfied.
*/
void
applyToVelocities
(
ContextImpl
&
context
,
double
tol
);
private:
private:
OpenCLContext
&
cl
;
OpenCLContext
&
cl
;
bool
hasInitializedKernel
;
bool
hasInitializedKernel
;
...
...
platforms/opencl/tests/TestOpenCLRandom.cpp
View file @
8cc22e17
...
@@ -38,6 +38,10 @@
...
@@ -38,6 +38,10 @@
#include "../src/OpenCLContext.h"
#include "../src/OpenCLContext.h"
#include "../src/OpenCLIntegrationUtilities.h"
#include "../src/OpenCLIntegrationUtilities.h"
#include "openmm/System.h"
#include "openmm/System.h"
#include "openmm/Context.h"
#include "OpenCLPlatform.h"
#include "openmm/VerletIntegrator.h"
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
#include <iostream>
#include <iostream>
using
namespace
OpenMM
;
using
namespace
OpenMM
;
...
@@ -82,9 +86,53 @@ void testGaussian() {
...
@@ -82,9 +86,53 @@ void testGaussian() {
ASSERT_EQUAL_TOL
(
0.0
,
c4
,
3.0
/
pow
(
numValues
,
1.0
/
4.0
));
ASSERT_EQUAL_TOL
(
0.0
,
c4
,
3.0
/
pow
(
numValues
,
1.0
/
4.0
));
}
}
void
testRandomVelocities
()
{
// Create a system.
const
int
numParticles
=
10000
;
const
double
temperture
=
100.0
;
OpenCLPlatform
platform
;
System
system
;
VerletIntegrator
integrator
(
0.01
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
system
.
addParticle
(
10.0
+
sin
(
0.1
*
i
));
for
(
int
i
=
0
;
i
<
numParticles
-
1
;
++
i
)
system
.
addConstraint
(
i
,
i
+
1
,
1.0
);
Context
context
(
system
,
integrator
,
platform
);
vector
<
Vec3
>
positions
(
numParticles
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
positions
[
i
]
=
Vec3
(
i
/
2
,
(
i
+
1
)
/
2
,
0
);
context
.
setPositions
(
positions
);
// Ask the context to generate random velocities.
context
.
setVelocitiesToTemperature
(
temperture
);
State
state
=
context
.
getState
(
State
::
Velocities
);
// See if they respect constraints.
for
(
int
i
=
1
;
i
<
numParticles
;
i
++
)
{
Vec3
v1
=
state
.
getVelocities
()[
i
-
1
];
Vec3
v2
=
state
.
getVelocities
()[
i
];
double
vel
=
(
v1
-
v2
).
dot
(
positions
[
i
-
1
]
-
positions
[
i
]);
ASSERT_EQUAL_TOL
(
0.0
,
vel
,
2e-5
);
}
// See if the temperature is correct.
double
ke
=
0
;
for
(
int
i
=
0
;
i
<
numParticles
;
i
++
)
{
Vec3
v
=
state
.
getVelocities
()[
i
];
ke
+=
0.5
*
system
.
getParticleMass
(
i
)
*
v
.
dot
(
v
);
}
double
expected
=
0.5
*
(
numParticles
*
3
-
system
.
getNumConstraints
())
*
BOLTZ
*
temperture
;
ASSERT_USUALLY_EQUAL_TOL
(
expected
,
ke
,
4
/
sqrt
(
numParticles
));
}
int
main
()
{
int
main
()
{
try
{
try
{
testGaussian
();
testGaussian
();
testRandomVelocities
();
}
}
catch
(
const
exception
&
e
)
{
catch
(
const
exception
&
e
)
{
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
...
...
platforms/reference/src/ReferenceKernels.cpp
View file @
8cc22e17
...
@@ -326,6 +326,18 @@ void ReferenceApplyConstraintsKernel::apply(ContextImpl& context, double tol) {
...
@@ -326,6 +326,18 @@ void ReferenceApplyConstraintsKernel::apply(ContextImpl& context, double tol) {
ReferenceVirtualSites
::
computePositions
(
context
.
getSystem
(),
positions
);
ReferenceVirtualSites
::
computePositions
(
context
.
getSystem
(),
positions
);
}
}
void
ReferenceApplyConstraintsKernel
::
applyToVelocities
(
ContextImpl
&
context
,
double
tol
)
{
if
(
constraints
==
NULL
)
{
vector
<
ReferenceCCMAAlgorithm
::
AngleInfo
>
angles
;
findAnglesForCCMA
(
context
.
getSystem
(),
angles
);
constraints
=
new
ReferenceCCMAAlgorithm
(
context
.
getSystem
().
getNumParticles
(),
numConstraints
,
constraintIndices
,
constraintDistances
,
masses
,
angles
,
tol
);
}
vector
<
RealVec
>&
positions
=
extractPositions
(
context
);
vector
<
RealVec
>&
velocities
=
extractVelocities
(
context
);
constraints
->
setTolerance
(
tol
);
constraints
->
applyToVelocities
(
data
.
numParticles
,
positions
,
velocities
,
inverseMasses
);
}
void
ReferenceVirtualSitesKernel
::
initialize
(
const
System
&
system
)
{
void
ReferenceVirtualSitesKernel
::
initialize
(
const
System
&
system
)
{
}
}
...
...
platforms/reference/src/ReferenceKernels.h
View file @
8cc22e17
...
@@ -206,6 +206,13 @@ public:
...
@@ -206,6 +206,13 @@ public:
* @param tol the distance tolerance within which constraints must be satisfied.
* @param tol the distance tolerance within which constraints must be satisfied.
*/
*/
void
apply
(
ContextImpl
&
context
,
double
tol
);
void
apply
(
ContextImpl
&
context
,
double
tol
);
/**
* Update particle velocities to enforce constraints.
*
* @param context the context in which to execute this kernel
* @param tol the velocity tolerance within which constraints must be satisfied.
*/
void
applyToVelocities
(
ContextImpl
&
context
,
double
tol
);
private:
private:
ReferencePlatform
::
PlatformData
&
data
;
ReferencePlatform
::
PlatformData
&
data
;
ReferenceConstraintAlgorithm
*
constraints
;
ReferenceConstraintAlgorithm
*
constraints
;
...
...
platforms/reference/tests/TestReferenceRandom.cpp
View file @
8cc22e17
...
@@ -35,6 +35,9 @@
...
@@ -35,6 +35,9 @@
#include "openmm/internal/AssertionUtilities.h"
#include "openmm/internal/AssertionUtilities.h"
#include "../src/SimTKUtilities/SimTKOpenMMUtilities.h"
#include "../src/SimTKUtilities/SimTKOpenMMUtilities.h"
#include "openmm/Context.h"
#include "ReferencePlatform.h"
#include "openmm/VerletIntegrator.h"
#include <iostream>
#include <iostream>
using
namespace
OpenMM
;
using
namespace
OpenMM
;
...
@@ -46,7 +49,6 @@ void testGaussian() {
...
@@ -46,7 +49,6 @@ void testGaussian() {
double
var
=
0.0
;
double
var
=
0.0
;
double
skew
=
0.0
;
double
skew
=
0.0
;
double
kurtosis
=
0.0
;
double
kurtosis
=
0.0
;
unsigned
long
jran
=
12399103
;
for
(
int
i
=
0
;
i
<
numValues
;
i
++
)
{
for
(
int
i
=
0
;
i
<
numValues
;
i
++
)
{
double
value
=
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
();
double
value
=
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
();
mean
+=
value
;
mean
+=
value
;
...
@@ -67,9 +69,53 @@ void testGaussian() {
...
@@ -67,9 +69,53 @@ void testGaussian() {
ASSERT_EQUAL_TOL
(
0.0
,
c4
,
0.01
);
ASSERT_EQUAL_TOL
(
0.0
,
c4
,
0.01
);
}
}
void
testRandomVelocities
()
{
// Create a system.
const
int
numParticles
=
10000
;
const
double
temperture
=
100.0
;
ReferencePlatform
platform
;
System
system
;
VerletIntegrator
integrator
(
0.01
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
system
.
addParticle
(
10.0
+
sin
(
0.1
*
i
));
for
(
int
i
=
0
;
i
<
numParticles
-
1
;
++
i
)
system
.
addConstraint
(
i
,
i
+
1
,
1.0
);
Context
context
(
system
,
integrator
,
platform
);
vector
<
Vec3
>
positions
(
numParticles
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
positions
[
i
]
=
Vec3
(
i
/
2
,
(
i
+
1
)
/
2
,
0
);
context
.
setPositions
(
positions
);
// Ask the context to generate random velocities.
context
.
setVelocitiesToTemperature
(
temperture
);
State
state
=
context
.
getState
(
State
::
Velocities
);
// See if they respect constraints.
for
(
int
i
=
1
;
i
<
numParticles
;
i
++
)
{
Vec3
v1
=
state
.
getVelocities
()[
i
-
1
];
Vec3
v2
=
state
.
getVelocities
()[
i
];
double
vel
=
(
v1
-
v2
).
dot
(
positions
[
i
-
1
]
-
positions
[
i
]);
ASSERT_EQUAL_TOL
(
0.0
,
vel
,
2e-5
);
}
// See if the temperature is correct.
double
ke
=
0
;
for
(
int
i
=
0
;
i
<
numParticles
;
i
++
)
{
Vec3
v
=
state
.
getVelocities
()[
i
];
ke
+=
0.5
*
system
.
getParticleMass
(
i
)
*
v
.
dot
(
v
);
}
double
expected
=
0.5
*
(
numParticles
*
3
-
system
.
getNumConstraints
())
*
BOLTZ
*
temperture
;
ASSERT_USUALLY_EQUAL_TOL
(
expected
,
ke
,
4
/
sqrt
(
numParticles
));
}
int
main
()
{
int
main
()
{
try
{
try
{
testGaussian
();
testGaussian
();
testRandomVelocities
();
}
}
catch
(
const
exception
&
e
)
{
catch
(
const
exception
&
e
)
{
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
...
...
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