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
6aad09c3
"vscode:/vscode.git/clone" did not exist on "52594038e90a4f4b70ed448316e82eb5243e21ea"
Commit
6aad09c3
authored
Oct 07, 2019
by
peastman
Browse files
Reference implementation of BAOABLangevinIntegrator
parent
e813e19f
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
970 additions
and
3 deletions
+970
-3
olla/include/openmm/kernels.h
olla/include/openmm/kernels.h
+39
-1
openmmapi/include/OpenMM.h
openmmapi/include/OpenMM.h
+1
-0
openmmapi/include/openmm/BAOABLangevinIntegrator.h
openmmapi/include/openmm/BAOABLangevinIntegrator.h
+152
-0
openmmapi/src/BAOABLangevinIntegrator.cpp
openmmapi/src/BAOABLangevinIntegrator.cpp
+86
-0
platforms/reference/include/ReferenceBAOABDynamics.h
platforms/reference/include/ReferenceBAOABDynamics.h
+138
-0
platforms/reference/include/ReferenceKernels.h
platforms/reference/include/ReferenceKernels.h
+43
-1
platforms/reference/src/ReferenceKernelFactory.cpp
platforms/reference/src/ReferenceKernelFactory.cpp
+2
-0
platforms/reference/src/ReferenceKernels.cpp
platforms/reference/src/ReferenceKernels.cpp
+45
-1
platforms/reference/src/ReferencePlatform.cpp
platforms/reference/src/ReferencePlatform.cpp
+1
-0
platforms/reference/src/SimTKReference/ReferenceBAOABDynamics.cpp
...s/reference/src/SimTKReference/ReferenceBAOABDynamics.cpp
+145
-0
platforms/reference/tests/TestReferenceBAOABLangevinIntegrator.cpp
.../reference/tests/TestReferenceBAOABLangevinIntegrator.cpp
+36
-0
tests/TestBAOABLangevinIntegrator.h
tests/TestBAOABLangevinIntegrator.h
+282
-0
No files found.
olla/include/openmm/kernels.h
View file @
6aad09c3
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* *
* Portions copyright (c) 2008-201
8
Stanford University and the Authors. *
* Portions copyright (c) 2008-201
9
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Authors: Peter Eastman *
* Contributors: *
* Contributors: *
* *
* *
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
* -------------------------------------------------------------------------- */
* -------------------------------------------------------------------------- */
#include "openmm/AndersenThermostat.h"
#include "openmm/AndersenThermostat.h"
#include "openmm/BAOABLangevinIntegrator.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/CMAPTorsionForce.h"
#include "openmm/CMAPTorsionForce.h"
#include "openmm/CMMotionRemover.h"
#include "openmm/CMMotionRemover.h"
...
@@ -1090,6 +1091,43 @@ public:
...
@@ -1090,6 +1091,43 @@ public:
virtual
double
computeKineticEnergy
(
ContextImpl
&
context
,
const
LangevinIntegrator
&
integrator
)
=
0
;
virtual
double
computeKineticEnergy
(
ContextImpl
&
context
,
const
LangevinIntegrator
&
integrator
)
=
0
;
};
};
/**
* This kernel is invoked by BAOABLangevinIntegrator to take one time step.
*/
class
IntegrateBAOABStepKernel
:
public
KernelImpl
{
public:
static
std
::
string
Name
()
{
return
"IntegrateBAOABStep"
;
}
IntegrateBAOABStepKernel
(
std
::
string
name
,
const
Platform
&
platform
)
:
KernelImpl
(
name
,
platform
)
{
}
/**
* Initialize the kernel.
*
* @param system the System this kernel will be applied to
* @param integrator the BAOABLangevinIntegrator this kernel will be used for
*/
virtual
void
initialize
(
const
System
&
system
,
const
BAOABLangevinIntegrator
&
integrator
)
=
0
;
/**
* Execute the kernel.
*
* @param context the context in which to execute this kernel
* @param integrator the BAOABLangevinIntegrator this kernel is being used for
* @param forcesAreValid if the context has been modified since the last time step, this will be
* false to show that cached forces are invalid and must be recalculated.
* On exit, this should specify whether the cached forces are valid at the
* end of the step.
*/
virtual
void
execute
(
ContextImpl
&
context
,
const
BAOABLangevinIntegrator
&
integrator
,
bool
&
forcesAreValid
)
=
0
;
/**
* Compute the kinetic energy.
*
* @param context the context in which to execute this kernel
* @param integrator the BAOABLangevinIntegrator this kernel is being used for
*/
virtual
double
computeKineticEnergy
(
ContextImpl
&
context
,
const
BAOABLangevinIntegrator
&
integrator
)
=
0
;
};
/**
/**
* This kernel is invoked by BrownianIntegrator to take one time step.
* This kernel is invoked by BrownianIntegrator to take one time step.
*/
*/
...
...
openmmapi/include/OpenMM.h
View file @
6aad09c3
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
* -------------------------------------------------------------------------- */
* -------------------------------------------------------------------------- */
#include "openmm/AndersenThermostat.h"
#include "openmm/AndersenThermostat.h"
#include "openmm/BAOABLangevinIntegrator.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/BrownianIntegrator.h"
#include "openmm/CMAPTorsionForce.h"
#include "openmm/CMAPTorsionForce.h"
#include "openmm/CMMotionRemover.h"
#include "openmm/CMMotionRemover.h"
...
...
openmmapi/include/openmm/BAOABLangevinIntegrator.h
0 → 100644
View file @
6aad09c3
#ifndef OPENMM_BAOABLANGEVININTEGRATOR_H_
#define OPENMM_BAOABLANGEVININTEGRATOR_H_
/* -------------------------------------------------------------------------- *
* 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) 2008-2019 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 "Integrator.h"
#include "openmm/Kernel.h"
#include "internal/windowsExport.h"
namespace
OpenMM
{
/**
* This is an Integrator which simulates a System using Langevin dynamics, with
* the BAOAB discretization of Leimkuhler and Matthews (http://dx.doi.org/10.1093/amrx/abs010).
* This method tend to produce more accurate configurational sampling than other
* discretizations, such as the one used in LangevinIntegrator.
*/
class
OPENMM_EXPORT
BAOABLangevinIntegrator
:
public
Integrator
{
public:
/**
* Create a BAOABLangevinIntegrator.
*
* @param temperature the temperature of the heat bath (in Kelvin)
* @param frictionCoeff the friction coefficient which couples the system to the heat bath (in inverse picoseconds)
* @param stepSize the step size with which to integrate the system (in picoseconds)
*/
BAOABLangevinIntegrator
(
double
temperature
,
double
frictionCoeff
,
double
stepSize
);
/**
* Get the temperature of the heat bath (in Kelvin).
*
* @return the temperature of the heat bath, measured in Kelvin
*/
double
getTemperature
()
const
{
return
temperature
;
}
/**
* Set the temperature of the heat bath (in Kelvin).
*
* @param temp the temperature of the heat bath, measured in Kelvin
*/
void
setTemperature
(
double
temp
)
{
temperature
=
temp
;
}
/**
* Get the friction coefficient which determines how strongly the system is coupled to
* the heat bath (in inverse ps).
*
* @return the friction coefficient, measured in 1/ps
*/
double
getFriction
()
const
{
return
friction
;
}
/**
* Set the friction coefficient which determines how strongly the system is coupled to
* the heat bath (in inverse ps).
*
* @param coeff the friction coefficient, measured in 1/ps
*/
void
setFriction
(
double
coeff
)
{
friction
=
coeff
;
}
/**
* Get the random number seed. See setRandomNumberSeed() for details.
*/
int
getRandomNumberSeed
()
const
{
return
randomNumberSeed
;
}
/**
* Set the random number seed. The precise meaning of this parameter is undefined, and is left up
* to each Platform to interpret in an appropriate way. It is guaranteed that if two simulations
* are run with different random number seeds, the sequence of random forces will be different. On
* the other hand, no guarantees are made about the behavior of simulations that use the same seed.
* In particular, Platforms are permitted to use non-deterministic algorithms which produce different
* results on successive runs, even if those runs were initialized identically.
*
* If seed is set to 0 (which is the default value assigned), a unique seed is chosen when a Context
* is created from this Integrator. This is done to ensure that each Context receives unique random seeds
* without you needing to set them explicitly.
*/
void
setRandomNumberSeed
(
int
seed
)
{
randomNumberSeed
=
seed
;
}
/**
* Advance a simulation through time by taking a series of time steps.
*
* @param steps the number of time steps to take
*/
void
step
(
int
steps
);
protected:
/**
* This will be called by the Context when it is created. It informs the Integrator
* of what context it will be integrating, and gives it a chance to do any necessary initialization.
* It will also get called again if the application calls reinitialize() on the Context.
*/
void
initialize
(
ContextImpl
&
context
);
/**
* This will be called by the Context when it is destroyed to let the Integrator do any necessary
* cleanup. It will also get called again if the application calls reinitialize() on the Context.
*/
void
cleanup
();
/**
* When the user modifies the state, we need to mark that the forces need to be recalculated.
*/
void
stateChanged
(
State
::
DataType
changed
);
/**
* Get the names of all Kernels used by this Integrator.
*/
std
::
vector
<
std
::
string
>
getKernelNames
();
/**
* Compute the kinetic energy of the system at the current time.
*/
double
computeKineticEnergy
();
private:
double
temperature
,
friction
;
int
randomNumberSeed
;
bool
forcesAreValid
;
Kernel
kernel
;
};
}
// namespace OpenMM
#endif
/*OPENMM_BAOABLANGEVININTEGRATOR_H_*/
openmmapi/src/BAOABLangevinIntegrator.cpp
0 → 100644
View file @
6aad09c3
/* -------------------------------------------------------------------------- *
* 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) 2008-2019 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/BAOABLangevinIntegrator.h"
#include "openmm/Context.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/kernels.h"
#include <string>
using
namespace
OpenMM
;
using
std
::
string
;
using
std
::
vector
;
BAOABLangevinIntegrator
::
BAOABLangevinIntegrator
(
double
temperature
,
double
frictionCoeff
,
double
stepSize
)
{
setTemperature
(
temperature
);
setFriction
(
frictionCoeff
);
setStepSize
(
stepSize
);
setConstraintTolerance
(
1e-5
);
setRandomNumberSeed
(
0
);
forcesAreValid
=
false
;
}
void
BAOABLangevinIntegrator
::
initialize
(
ContextImpl
&
contextRef
)
{
if
(
owner
!=
NULL
&&
&
contextRef
.
getOwner
()
!=
owner
)
throw
OpenMMException
(
"This Integrator is already bound to a context"
);
context
=
&
contextRef
;
owner
=
&
contextRef
.
getOwner
();
kernel
=
context
->
getPlatform
().
createKernel
(
IntegrateBAOABStepKernel
::
Name
(),
contextRef
);
kernel
.
getAs
<
IntegrateBAOABStepKernel
>
().
initialize
(
contextRef
.
getSystem
(),
*
this
);
}
void
BAOABLangevinIntegrator
::
cleanup
()
{
kernel
=
Kernel
();
}
void
BAOABLangevinIntegrator
::
stateChanged
(
State
::
DataType
changed
)
{
forcesAreValid
=
false
;
}
vector
<
string
>
BAOABLangevinIntegrator
::
getKernelNames
()
{
std
::
vector
<
std
::
string
>
names
;
names
.
push_back
(
IntegrateBAOABStepKernel
::
Name
());
return
names
;
}
double
BAOABLangevinIntegrator
::
computeKineticEnergy
()
{
return
kernel
.
getAs
<
IntegrateBAOABStepKernel
>
().
computeKineticEnergy
(
*
context
,
*
this
);
}
void
BAOABLangevinIntegrator
::
step
(
int
steps
)
{
if
(
context
==
NULL
)
throw
OpenMMException
(
"This Integrator is not bound to a context!"
);
for
(
int
i
=
0
;
i
<
steps
;
++
i
)
{
context
->
updateContextState
();
kernel
.
getAs
<
IntegrateBAOABStepKernel
>
().
execute
(
*
context
,
*
this
,
forcesAreValid
);
}
}
platforms/reference/include/ReferenceBAOABDynamics.h
0 → 100644
View file @
6aad09c3
/* Portions copyright (c) 2006-2019 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 __ReferenceBAOABDynamics_H__
#define __ReferenceBAOABDynamics_H__
#include "ReferenceDynamics.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/windowsExport.h"
namespace
OpenMM
{
class
OPENMM_EXPORT
ReferenceBAOABDynamics
:
public
ReferenceDynamics
{
protected:
std
::
vector
<
OpenMM
::
Vec3
>
xPrime
,
oldx
;
std
::
vector
<
double
>
inverseMasses
;
double
friction
;
public:
/**---------------------------------------------------------------------------------------
Constructor
@param numberOfAtoms number of atoms
@param deltaT delta t for dynamics
@param friction friction coefficient
@param temperature temperature
--------------------------------------------------------------------------------------- */
ReferenceBAOABDynamics
(
int
numberOfAtoms
,
double
deltaT
,
double
friction
,
double
temperature
);
/**---------------------------------------------------------------------------------------
Destructor
--------------------------------------------------------------------------------------- */
~
ReferenceBAOABDynamics
();
/**---------------------------------------------------------------------------------------
Get friction coefficient
--------------------------------------------------------------------------------------- */
double
getFriction
()
const
;
/**---------------------------------------------------------------------------------------
Update
@param context the context this integrator is updating
@param system the System to be integrated
@param atomCoordinates atom coordinates
@param velocities velocities
@param masses atom masses
@param forcesAreValid whether the current forces are valid or need to be recomputed
@param tolerance the constraint tolerance
--------------------------------------------------------------------------------------- */
void
update
(
OpenMM
::
ContextImpl
&
context
,
std
::
vector
<
OpenMM
::
Vec3
>&
atomCoordinates
,
std
::
vector
<
OpenMM
::
Vec3
>&
velocities
,
std
::
vector
<
double
>&
masses
,
bool
&
forcesAreValid
,
double
tolerance
);
/**---------------------------------------------------------------------------------------
First update; based on code in update.c do_update_sd() Gromacs 3.1.4
@param numberOfAtoms number of atoms
@param atomCoordinates atom coordinates
@param velocities velocities
@param forces forces
@param masses atom masses
@param xPrime xPrime
--------------------------------------------------------------------------------------- */
virtual
void
updatePart1
(
int
numberOfAtoms
,
std
::
vector
<
OpenMM
::
Vec3
>&
atomCoordinates
,
std
::
vector
<
OpenMM
::
Vec3
>&
velocities
,
std
::
vector
<
OpenMM
::
Vec3
>&
forces
,
std
::
vector
<
double
>&
masses
,
std
::
vector
<
OpenMM
::
Vec3
>&
xPrime
);
/**---------------------------------------------------------------------------------------
Second update
@param numberOfAtoms number of atoms
@param atomCoordinates atom coordinates
@param velocities velocities
@param masses atom masses
--------------------------------------------------------------------------------------- */
virtual
void
updatePart2
(
int
numberOfAtoms
,
std
::
vector
<
OpenMM
::
Vec3
>&
atomCoordinates
,
std
::
vector
<
OpenMM
::
Vec3
>&
velocities
,
std
::
vector
<
double
>&
masses
,
std
::
vector
<
OpenMM
::
Vec3
>&
xPrime
);
/**---------------------------------------------------------------------------------------
Third update
@param numberOfAtoms number of atoms
@param atomCoordinates atom coordinates
@param velocities velocities
@param forces forces
@param masses atom masses
--------------------------------------------------------------------------------------- */
virtual
void
updatePart3
(
int
numberOfAtoms
,
std
::
vector
<
OpenMM
::
Vec3
>&
atomCoordinates
,
std
::
vector
<
OpenMM
::
Vec3
>&
velocities
,
std
::
vector
<
OpenMM
::
Vec3
>&
forces
,
std
::
vector
<
double
>&
masses
,
std
::
vector
<
OpenMM
::
Vec3
>&
xPrime
);
};
}
// namespace OpenMM
#endif // __ReferenceBAOABDynamics_H__
platforms/reference/include/ReferenceKernels.h
View file @
6aad09c3
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* *
* Portions copyright (c) 2008-201
8
Stanford University and the Authors. *
* Portions copyright (c) 2008-201
9
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Authors: Peter Eastman *
* Contributors: *
* Contributors: *
* *
* *
...
@@ -45,6 +45,7 @@ namespace OpenMM {
...
@@ -45,6 +45,7 @@ namespace OpenMM {
class
ReferenceObc
;
class
ReferenceObc
;
class
ReferenceAndersenThermostat
;
class
ReferenceAndersenThermostat
;
class
ReferenceBAOABDynamics
;
class
ReferenceCustomBondIxn
;
class
ReferenceCustomBondIxn
;
class
ReferenceCustomAngleIxn
;
class
ReferenceCustomAngleIxn
;
class
ReferenceCustomTorsionIxn
;
class
ReferenceCustomTorsionIxn
;
...
@@ -1170,6 +1171,47 @@ private:
...
@@ -1170,6 +1171,47 @@ private:
double
prevTemp
,
prevFriction
,
prevStepSize
;
double
prevTemp
,
prevFriction
,
prevStepSize
;
};
};
/**
* This kernel is invoked by LangevinIntegrator to take one time step.
*/
class
ReferenceIntegrateBAOABStepKernel
:
public
IntegrateBAOABStepKernel
{
public:
ReferenceIntegrateBAOABStepKernel
(
std
::
string
name
,
const
Platform
&
platform
,
ReferencePlatform
::
PlatformData
&
data
)
:
IntegrateBAOABStepKernel
(
name
,
platform
),
data
(
data
),
dynamics
(
0
)
{
}
~
ReferenceIntegrateBAOABStepKernel
();
/**
* Initialize the kernel, setting up the particle masses.
*
* @param system the System this kernel will be applied to
* @param integrator the BAOABLangevinIntegrator this kernel will be used for
*/
void
initialize
(
const
System
&
system
,
const
BAOABLangevinIntegrator
&
integrator
);
/**
* Execute the kernel.
*
* @param context the context in which to execute this kernel
* @param integrator the BAOABLangevinIntegrator this kernel is being used for
* @param forcesAreValid if the context has been modified since the last time step, this will be
* false to show that cached forces are invalid and must be recalculated.
* On exit, this should specify whether the cached forces are valid at the
* end of the step.
*/
void
execute
(
ContextImpl
&
context
,
const
BAOABLangevinIntegrator
&
integrator
,
bool
&
forcesAreValid
);
/**
* Compute the kinetic energy.
*
* @param context the context in which to execute this kernel
* @param integrator the BAOABLangevinIntegrator this kernel is being used for
*/
double
computeKineticEnergy
(
ContextImpl
&
context
,
const
BAOABLangevinIntegrator
&
integrator
);
private:
ReferencePlatform
::
PlatformData
&
data
;
ReferenceBAOABDynamics
*
dynamics
;
std
::
vector
<
double
>
masses
;
double
prevTemp
,
prevFriction
,
prevStepSize
;
};
/**
/**
* This kernel is invoked by BrownianIntegrator to take one time step.
* This kernel is invoked by BrownianIntegrator to take one time step.
*/
*/
...
...
platforms/reference/src/ReferenceKernelFactory.cpp
View file @
6aad09c3
...
@@ -90,6 +90,8 @@ KernelImpl* ReferenceKernelFactory::createKernelImpl(std::string name, const Pla
...
@@ -90,6 +90,8 @@ KernelImpl* ReferenceKernelFactory::createKernelImpl(std::string name, const Pla
return
new
ReferenceIntegrateVerletStepKernel
(
name
,
platform
,
data
);
return
new
ReferenceIntegrateVerletStepKernel
(
name
,
platform
,
data
);
if
(
name
==
IntegrateLangevinStepKernel
::
Name
())
if
(
name
==
IntegrateLangevinStepKernel
::
Name
())
return
new
ReferenceIntegrateLangevinStepKernel
(
name
,
platform
,
data
);
return
new
ReferenceIntegrateLangevinStepKernel
(
name
,
platform
,
data
);
if
(
name
==
IntegrateBAOABStepKernel
::
Name
())
return
new
ReferenceIntegrateBAOABStepKernel
(
name
,
platform
,
data
);
if
(
name
==
IntegrateBrownianStepKernel
::
Name
())
if
(
name
==
IntegrateBrownianStepKernel
::
Name
())
return
new
ReferenceIntegrateBrownianStepKernel
(
name
,
platform
,
data
);
return
new
ReferenceIntegrateBrownianStepKernel
(
name
,
platform
,
data
);
if
(
name
==
IntegrateVariableLangevinStepKernel
::
Name
())
if
(
name
==
IntegrateVariableLangevinStepKernel
::
Name
())
...
...
platforms/reference/src/ReferenceKernels.cpp
View file @
6aad09c3
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* *
* Portions copyright (c) 2008-201
8
Stanford University and the Authors. *
* Portions copyright (c) 2008-201
9
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Authors: Peter Eastman *
* Contributors: *
* Contributors: *
* *
* *
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
#include "ReferenceObc.h"
#include "ReferenceObc.h"
#include "ReferenceAndersenThermostat.h"
#include "ReferenceAndersenThermostat.h"
#include "ReferenceAngleBondIxn.h"
#include "ReferenceAngleBondIxn.h"
#include "ReferenceBAOABDynamics.h"
#include "ReferenceBondForce.h"
#include "ReferenceBondForce.h"
#include "ReferenceBrownianDynamics.h"
#include "ReferenceBrownianDynamics.h"
#include "ReferenceCCMAAlgorithm.h"
#include "ReferenceCCMAAlgorithm.h"
...
@@ -2149,6 +2150,49 @@ double ReferenceIntegrateLangevinStepKernel::computeKineticEnergy(ContextImpl& c
...
@@ -2149,6 +2150,49 @@ double ReferenceIntegrateLangevinStepKernel::computeKineticEnergy(ContextImpl& c
return
computeShiftedKineticEnergy
(
context
,
masses
,
0.5
*
integrator
.
getStepSize
());
return
computeShiftedKineticEnergy
(
context
,
masses
,
0.5
*
integrator
.
getStepSize
());
}
}
ReferenceIntegrateBAOABStepKernel
::~
ReferenceIntegrateBAOABStepKernel
()
{
if
(
dynamics
)
delete
dynamics
;
}
void
ReferenceIntegrateBAOABStepKernel
::
initialize
(
const
System
&
system
,
const
BAOABLangevinIntegrator
&
integrator
)
{
int
numParticles
=
system
.
getNumParticles
();
masses
.
resize
(
numParticles
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
masses
[
i
]
=
system
.
getParticleMass
(
i
);
SimTKOpenMMUtilities
::
setRandomNumberSeed
((
unsigned
int
)
integrator
.
getRandomNumberSeed
());
}
void
ReferenceIntegrateBAOABStepKernel
::
execute
(
ContextImpl
&
context
,
const
BAOABLangevinIntegrator
&
integrator
,
bool
&
forcesAreValid
)
{
double
temperature
=
integrator
.
getTemperature
();
double
friction
=
integrator
.
getFriction
();
double
stepSize
=
integrator
.
getStepSize
();
vector
<
Vec3
>&
posData
=
extractPositions
(
context
);
vector
<
Vec3
>&
velData
=
extractVelocities
(
context
);
if
(
dynamics
==
0
||
temperature
!=
prevTemp
||
friction
!=
prevFriction
||
stepSize
!=
prevStepSize
)
{
// Recreate the computation objects with the new parameters.
if
(
dynamics
)
delete
dynamics
;
dynamics
=
new
ReferenceBAOABDynamics
(
context
.
getSystem
().
getNumParticles
(),
stepSize
,
friction
,
temperature
);
dynamics
->
setReferenceConstraintAlgorithm
(
&
extractConstraints
(
context
));
prevTemp
=
temperature
;
prevFriction
=
friction
;
prevStepSize
=
stepSize
;
}
dynamics
->
update
(
context
,
posData
,
velData
,
masses
,
forcesAreValid
,
integrator
.
getConstraintTolerance
());
data
.
time
+=
stepSize
;
data
.
stepCount
++
;
}
double
ReferenceIntegrateBAOABStepKernel
::
computeKineticEnergy
(
ContextImpl
&
context
,
const
BAOABLangevinIntegrator
&
integrator
)
{
return
computeShiftedKineticEnergy
(
context
,
masses
,
0.0
);
}
ReferenceIntegrateBrownianStepKernel
::~
ReferenceIntegrateBrownianStepKernel
()
{
ReferenceIntegrateBrownianStepKernel
::~
ReferenceIntegrateBrownianStepKernel
()
{
if
(
dynamics
)
if
(
dynamics
)
delete
dynamics
;
delete
dynamics
;
...
...
platforms/reference/src/ReferencePlatform.cpp
View file @
6aad09c3
...
@@ -67,6 +67,7 @@ ReferencePlatform::ReferencePlatform() {
...
@@ -67,6 +67,7 @@ ReferencePlatform::ReferencePlatform() {
registerKernelFactory
(
CalcGayBerneForceKernel
::
Name
(),
factory
);
registerKernelFactory
(
CalcGayBerneForceKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateVerletStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateVerletStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateLangevinStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateLangevinStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateBAOABStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateBrownianStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateBrownianStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateVariableLangevinStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateVariableLangevinStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateVariableVerletStepKernel
::
Name
(),
factory
);
registerKernelFactory
(
IntegrateVariableVerletStepKernel
::
Name
(),
factory
);
...
...
platforms/reference/src/SimTKReference/ReferenceBAOABDynamics.cpp
0 → 100644
View file @
6aad09c3
/* Portions copyright (c) 2006-2019 Stanford University and Simbios.
* Contributors: Pande Group
*
* 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 <cstring>
#include <sstream>
#include "SimTKOpenMMUtilities.h"
#include "ReferenceBAOABDynamics.h"
#include "ReferencePlatform.h"
#include "ReferenceVirtualSites.h"
#include "openmm/OpenMMException.h"
#include "openmm/internal/ContextImpl.h"
using
std
::
vector
;
using
namespace
OpenMM
;
ReferenceBAOABDynamics
::
ReferenceBAOABDynamics
(
int
numberOfAtoms
,
double
deltaT
,
double
friction
,
double
temperature
)
:
ReferenceDynamics
(
numberOfAtoms
,
deltaT
,
temperature
),
friction
(
friction
)
{
xPrime
.
resize
(
numberOfAtoms
);
oldx
.
resize
(
numberOfAtoms
);
inverseMasses
.
resize
(
numberOfAtoms
);
}
ReferenceBAOABDynamics
::~
ReferenceBAOABDynamics
()
{
}
double
ReferenceBAOABDynamics
::
getFriction
()
const
{
return
friction
;
}
void
ReferenceBAOABDynamics
::
updatePart1
(
int
numberOfAtoms
,
vector
<
Vec3
>&
atomCoordinates
,
vector
<
Vec3
>&
velocities
,
vector
<
Vec3
>&
forces
,
vector
<
double
>&
inverseMasses
,
vector
<
Vec3
>&
xPrime
)
{
const
double
halfdt
=
0.5
*
getDeltaT
();
for
(
int
i
=
0
;
i
<
numberOfAtoms
;
i
++
)
{
if
(
inverseMasses
[
i
]
!=
0.0
)
{
velocities
[
i
]
+=
(
halfdt
*
inverseMasses
[
i
])
*
forces
[
i
];
xPrime
[
i
]
=
atomCoordinates
[
i
]
+
velocities
[
i
]
*
halfdt
;
oldx
[
i
]
=
xPrime
[
i
];
}
}
}
void
ReferenceBAOABDynamics
::
updatePart2
(
int
numberOfAtoms
,
vector
<
Vec3
>&
atomCoordinates
,
vector
<
Vec3
>&
velocities
,
vector
<
double
>&
inverseMasses
,
vector
<
Vec3
>&
xPrime
)
{
const
double
halfdt
=
0.5
*
getDeltaT
();
const
double
kT
=
BOLTZ
*
getTemperature
();
const
double
friction
=
getFriction
();
const
double
vscale
=
exp
(
-
getDeltaT
()
*
friction
);
const
double
noisescale
=
sqrt
(
1
-
vscale
*
vscale
);
for
(
int
i
=
0
;
i
<
numberOfAtoms
;
i
++
)
{
if
(
inverseMasses
[
i
]
!=
0.0
)
{
velocities
[
i
]
+=
(
xPrime
[
i
]
-
oldx
[
i
])
/
halfdt
;
velocities
[
i
]
=
vscale
*
velocities
[
i
]
+
noisescale
*
sqrt
(
kT
*
inverseMasses
[
i
])
*
Vec3
(
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
(),
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
(),
SimTKOpenMMUtilities
::
getNormallyDistributedRandomNumber
());
atomCoordinates
[
i
]
=
xPrime
[
i
];
xPrime
[
i
]
=
atomCoordinates
[
i
]
+
velocities
[
i
]
*
halfdt
;
oldx
[
i
]
=
xPrime
[
i
];
}
}
}
void
ReferenceBAOABDynamics
::
updatePart3
(
int
numberOfAtoms
,
vector
<
Vec3
>&
atomCoordinates
,
vector
<
Vec3
>&
velocities
,
vector
<
Vec3
>&
forces
,
vector
<
double
>&
inverseMasses
,
vector
<
Vec3
>&
xPrime
)
{
const
double
halfdt
=
0.5
*
getDeltaT
();
for
(
int
i
=
0
;
i
<
numberOfAtoms
;
i
++
)
{
if
(
inverseMasses
[
i
]
!=
0.0
)
{
velocities
[
i
]
+=
(
xPrime
[
i
]
-
oldx
[
i
])
/
halfdt
;
velocities
[
i
]
+=
(
halfdt
*
inverseMasses
[
i
])
*
forces
[
i
];
atomCoordinates
[
i
]
=
xPrime
[
i
];
}
}
}
void
ReferenceBAOABDynamics
::
update
(
ContextImpl
&
context
,
vector
<
Vec3
>&
atomCoordinates
,
vector
<
Vec3
>&
velocities
,
vector
<
double
>&
masses
,
bool
&
forcesAreValid
,
double
tolerance
)
{
int
numberOfAtoms
=
context
.
getSystem
().
getNumParticles
();
ReferenceConstraintAlgorithm
*
referenceConstraintAlgorithm
=
getReferenceConstraintAlgorithm
();
if
(
getTimeStep
()
==
0
)
{
// Invert masses
for
(
int
ii
=
0
;
ii
<
numberOfAtoms
;
ii
++
)
{
if
(
masses
[
ii
]
==
0.0
)
inverseMasses
[
ii
]
=
0.0
;
else
inverseMasses
[
ii
]
=
1.0
/
masses
[
ii
];
}
}
if
(
!
forcesAreValid
)
{
context
.
calcForcesAndEnergy
(
true
,
false
);
forcesAreValid
=
true
;
}
ReferencePlatform
::
PlatformData
*
data
=
reinterpret_cast
<
ReferencePlatform
::
PlatformData
*>
(
context
.
getPlatformData
());
vector
<
Vec3
>&
forces
=
*
data
->
forces
;
// 1st update
updatePart1
(
numberOfAtoms
,
atomCoordinates
,
velocities
,
forces
,
inverseMasses
,
xPrime
);
if
(
referenceConstraintAlgorithm
)
referenceConstraintAlgorithm
->
apply
(
atomCoordinates
,
xPrime
,
inverseMasses
,
tolerance
);
// 2nd update
updatePart2
(
numberOfAtoms
,
atomCoordinates
,
velocities
,
inverseMasses
,
xPrime
);
if
(
referenceConstraintAlgorithm
)
referenceConstraintAlgorithm
->
apply
(
atomCoordinates
,
xPrime
,
inverseMasses
,
tolerance
);
// 3rd update
context
.
calcForcesAndEnergy
(
true
,
false
);
updatePart3
(
numberOfAtoms
,
atomCoordinates
,
velocities
,
forces
,
inverseMasses
,
xPrime
);
if
(
referenceConstraintAlgorithm
)
referenceConstraintAlgorithm
->
applyToVelocities
(
atomCoordinates
,
velocities
,
inverseMasses
,
tolerance
);
ReferenceVirtualSites
::
computePositions
(
context
.
getSystem
(),
atomCoordinates
);
incrementTimeStep
();
}
platforms/reference/tests/TestReferenceBAOABLangevinIntegrator.cpp
0 → 100644
View file @
6aad09c3
/* -------------------------------------------------------------------------- *
* 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) 2019 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 "ReferenceTests.h"
#include "TestBAOABLangevinIntegrator.h"
void
runPlatformTests
()
{
}
tests/TestBAOABLangevinIntegrator.h
0 → 100644
View file @
6aad09c3
/* -------------------------------------------------------------------------- *
* 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) 2008-2019 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/internal/AssertionUtilities.h"
#include "openmm/Context.h"
#include "openmm/HarmonicBondForce.h"
#include "openmm/NonbondedForce.h"
#include "openmm/System.h"
#include "openmm/BAOABLangevinIntegrator.h"
#include "SimTKOpenMMRealType.h"
#include "sfmt/SFMT.h"
#include <iostream>
#include <vector>
using
namespace
OpenMM
;
using
namespace
std
;
const
double
TOL
=
1e-5
;
void
testSingleBond
()
{
System
system
;
system
.
addParticle
(
2.0
);
system
.
addParticle
(
2.0
);
BAOABLangevinIntegrator
integrator
(
0
,
0.1
,
0.01
);
HarmonicBondForce
*
forceField
=
new
HarmonicBondForce
();
forceField
->
addBond
(
0
,
1
,
1.5
,
1
);
system
.
addForce
(
forceField
);
Context
context
(
system
,
integrator
,
platform
);
vector
<
Vec3
>
positions
(
2
);
positions
[
0
]
=
Vec3
(
-
1
,
0
,
0
);
positions
[
1
]
=
Vec3
(
1
,
0
,
0
);
context
.
setPositions
(
positions
);
// This is simply a damped harmonic oscillator, so compare it to the analytical solution.
double
freq
=
std
::
sqrt
(
1
-
0.05
*
0.05
);
for
(
int
i
=
0
;
i
<
1000
;
++
i
)
{
State
state
=
context
.
getState
(
State
::
Positions
|
State
::
Velocities
);
double
time
=
state
.
getTime
();
double
expectedDist
=
1.5
+
0.5
*
std
::
exp
(
-
0.05
*
time
)
*
std
::
cos
(
freq
*
time
);
ASSERT_EQUAL_VEC
(
Vec3
(
-
0.5
*
expectedDist
,
0
,
0
),
state
.
getPositions
()[
0
],
0.02
);
ASSERT_EQUAL_VEC
(
Vec3
(
0.5
*
expectedDist
,
0
,
0
),
state
.
getPositions
()[
1
],
0.02
);
double
expectedSpeed
=
-
0.5
*
std
::
exp
(
-
0.05
*
time
)
*
(
0.05
*
std
::
cos
(
freq
*
time
)
+
freq
*
std
::
sin
(
freq
*
time
));
ASSERT_EQUAL_VEC
(
Vec3
(
-
0.5
*
expectedSpeed
,
0
,
0
),
state
.
getVelocities
()[
0
],
0.02
);
ASSERT_EQUAL_VEC
(
Vec3
(
0.5
*
expectedSpeed
,
0
,
0
),
state
.
getVelocities
()[
1
],
0.02
);
integrator
.
step
(
1
);
}
// Now set the friction to 0 and see if it conserves energy.
integrator
.
setFriction
(
0.0
);
context
.
setPositions
(
positions
);
State
state
=
context
.
getState
(
State
::
Energy
);
double
initialEnergy
=
state
.
getKineticEnergy
()
+
state
.
getPotentialEnergy
();
for
(
int
i
=
0
;
i
<
1000
;
++
i
)
{
state
=
context
.
getState
(
State
::
Energy
);
double
energy
=
state
.
getKineticEnergy
()
+
state
.
getPotentialEnergy
();
ASSERT_EQUAL_TOL
(
initialEnergy
,
energy
,
0.01
);
integrator
.
step
(
1
);
}
}
void
testTemperature
()
{
const
int
numParticles
=
8
;
const
double
temp
=
100.0
;
System
system
;
system
.
setDefaultPeriodicBoxVectors
(
Vec3
(
5
,
0
,
0
),
Vec3
(
0
,
5
,
0
),
Vec3
(
0
,
0
,
5
));
BAOABLangevinIntegrator
integrator
(
temp
,
3.0
,
0.01
);
NonbondedForce
*
forceField
=
new
NonbondedForce
();
forceField
->
setNonbondedMethod
(
NonbondedForce
::
CutoffPeriodic
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
{
system
.
addParticle
(
2.0
);
forceField
->
addParticle
((
i
%
2
==
0
?
1.0
:
-
1.0
),
1.0
,
5.0
);
}
system
.
addForce
(
forceField
);
Context
context
(
system
,
integrator
,
platform
);
vector
<
Vec3
>
positions
(
numParticles
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
positions
[
i
]
=
Vec3
((
i
%
2
==
0
?
2
:
-
2
),
(
i
%
4
<
2
?
2
:
-
2
),
(
i
<
4
?
2
:
-
2
));
context
.
setPositions
(
positions
);
// Let it equilibrate.
integrator
.
step
(
5000
);
// Now run it for a while and see if the temperature is correct.
double
ke
=
0.0
;
int
steps
=
10000
;
for
(
int
i
=
0
;
i
<
steps
;
++
i
)
{
State
state
=
context
.
getState
(
State
::
Energy
);
ke
+=
state
.
getKineticEnergy
();
integrator
.
step
(
1
);
}
ke
/=
steps
;
double
expected
=
0.5
*
numParticles
*
3
*
BOLTZ
*
temp
;
ASSERT_USUALLY_EQUAL_TOL
(
expected
,
ke
,
6
/
std
::
sqrt
((
double
)
steps
));
}
void
testConstraints
()
{
const
int
numParticles
=
8
;
const
int
numConstraints
=
5
;
const
double
temp
=
100.0
;
System
system
;
BAOABLangevinIntegrator
integrator
(
temp
,
2.0
,
0.01
);
integrator
.
setConstraintTolerance
(
1e-5
);
NonbondedForce
*
forceField
=
new
NonbondedForce
();
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
{
system
.
addParticle
(
10.0
);
forceField
->
addParticle
((
i
%
2
==
0
?
0.2
:
-
0.2
),
0.5
,
5.0
);
}
system
.
addConstraint
(
0
,
1
,
1.0
);
system
.
addConstraint
(
1
,
2
,
1.0
);
system
.
addConstraint
(
2
,
3
,
1.0
);
system
.
addConstraint
(
4
,
5
,
1.0
);
system
.
addConstraint
(
6
,
7
,
1.0
);
system
.
addForce
(
forceField
);
Context
context
(
system
,
integrator
,
platform
);
vector
<
Vec3
>
positions
(
numParticles
);
vector
<
Vec3
>
velocities
(
numParticles
);
OpenMM_SFMT
::
SFMT
sfmt
;
init_gen_rand
(
0
,
sfmt
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
{
positions
[
i
]
=
Vec3
(
i
/
2
,
(
i
+
1
)
/
2
,
0
);
velocities
[
i
]
=
Vec3
(
genrand_real2
(
sfmt
)
-
0.5
,
genrand_real2
(
sfmt
)
-
0.5
,
genrand_real2
(
sfmt
)
-
0.5
);
}
context
.
setPositions
(
positions
);
context
.
setVelocities
(
velocities
);
// Simulate it and see whether the constraints remain satisfied.
for
(
int
i
=
0
;
i
<
1000
;
++
i
)
{
State
state
=
context
.
getState
(
State
::
Positions
);
for
(
int
j
=
0
;
j
<
numConstraints
;
++
j
)
{
int
particle1
,
particle2
;
double
distance
;
system
.
getConstraintParameters
(
j
,
particle1
,
particle2
,
distance
);
Vec3
p1
=
state
.
getPositions
()[
particle1
];
Vec3
p2
=
state
.
getPositions
()[
particle2
];
double
dist
=
std
::
sqrt
((
p1
[
0
]
-
p2
[
0
])
*
(
p1
[
0
]
-
p2
[
0
])
+
(
p1
[
1
]
-
p2
[
1
])
*
(
p1
[
1
]
-
p2
[
1
])
+
(
p1
[
2
]
-
p2
[
2
])
*
(
p1
[
2
]
-
p2
[
2
]));
ASSERT_EQUAL_TOL
(
distance
,
dist
,
1e-4
);
}
integrator
.
step
(
1
);
}
}
void
testConstrainedMasslessParticles
()
{
System
system
;
system
.
addParticle
(
0.0
);
system
.
addParticle
(
1.0
);
system
.
addConstraint
(
0
,
1
,
1.5
);
vector
<
Vec3
>
positions
(
2
);
positions
[
0
]
=
Vec3
(
-
1
,
0
,
0
);
positions
[
1
]
=
Vec3
(
1
,
0
,
0
);
BAOABLangevinIntegrator
integrator
(
300.0
,
2.0
,
0.01
);
bool
failed
=
false
;
try
{
// This should throw an exception.
Context
context
(
system
,
integrator
,
platform
);
}
catch
(
exception
&
ex
)
{
failed
=
true
;
}
ASSERT
(
failed
);
// Now make both particles massless, which should work.
system
.
setParticleMass
(
1
,
0.0
);
Context
context
(
system
,
integrator
,
platform
);
context
.
setPositions
(
positions
);
context
.
setVelocitiesToTemperature
(
300.0
);
integrator
.
step
(
1
);
State
state
=
context
.
getState
(
State
::
Velocities
);
ASSERT_EQUAL
(
0.0
,
state
.
getVelocities
()[
0
][
0
]);
}
void
testRandomSeed
()
{
const
int
numParticles
=
8
;
const
double
temp
=
100.0
;
System
system
;
BAOABLangevinIntegrator
integrator
(
temp
,
2.0
,
0.01
);
NonbondedForce
*
forceField
=
new
NonbondedForce
();
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
{
system
.
addParticle
(
2.0
);
forceField
->
addParticle
((
i
%
2
==
0
?
1.0
:
-
1.0
),
1.0
,
5.0
);
}
system
.
addForce
(
forceField
);
vector
<
Vec3
>
positions
(
numParticles
);
vector
<
Vec3
>
velocities
(
numParticles
);
for
(
int
i
=
0
;
i
<
numParticles
;
++
i
)
{
positions
[
i
]
=
Vec3
((
i
%
2
==
0
?
2
:
-
2
),
(
i
%
4
<
2
?
2
:
-
2
),
(
i
<
4
?
2
:
-
2
));
velocities
[
i
]
=
Vec3
(
0
,
0
,
0
);
}
// Try twice with the same random seed.
integrator
.
setRandomNumberSeed
(
5
);
Context
context
(
system
,
integrator
,
platform
);
context
.
setPositions
(
positions
);
context
.
setVelocities
(
velocities
);
integrator
.
step
(
10
);
State
state1
=
context
.
getState
(
State
::
Positions
);
context
.
reinitialize
();
context
.
setPositions
(
positions
);
context
.
setVelocities
(
velocities
);
integrator
.
step
(
10
);
State
state2
=
context
.
getState
(
State
::
Positions
);
// Try twice with a different random seed.
integrator
.
setRandomNumberSeed
(
10
);
context
.
reinitialize
();
context
.
setPositions
(
positions
);
context
.
setVelocities
(
velocities
);
integrator
.
step
(
10
);
State
state3
=
context
.
getState
(
State
::
Positions
);
context
.
reinitialize
();
context
.
setPositions
(
positions
);
context
.
setVelocities
(
velocities
);
integrator
.
step
(
10
);
State
state4
=
context
.
getState
(
State
::
Positions
);
// Compare the results.
for
(
int
i
=
0
;
i
<
numParticles
;
i
++
)
{
for
(
int
j
=
0
;
j
<
3
;
j
++
)
{
ASSERT_EQUAL_TOL
(
state1
.
getPositions
()[
i
][
j
],
state2
.
getPositions
()[
i
][
j
],
1e-6
);
ASSERT_EQUAL_TOL
(
state3
.
getPositions
()[
i
][
j
],
state4
.
getPositions
()[
i
][
j
],
1e-6
);
ASSERT
(
state1
.
getPositions
()[
i
][
j
]
!=
state3
.
getPositions
()[
i
][
j
]);
}
}
}
void
runPlatformTests
();
int
main
(
int
argc
,
char
*
argv
[])
{
try
{
initializeTests
(
argc
,
argv
);
testSingleBond
();
testTemperature
();
testConstraints
();
testConstrainedMasslessParticles
();
testRandomSeed
();
runPlatformTests
();
}
catch
(
const
exception
&
e
)
{
cout
<<
"exception: "
<<
e
.
what
()
<<
endl
;
return
1
;
}
cout
<<
"Done"
<<
endl
;
return
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