Commit 5485a4e3 authored by peastman's avatar peastman
Browse files

Merge pull request #384 from rmcgibbo/magic-bytes

Magic bytes at the beginning of the checkpoint
parents c4877bec e78c723b
...@@ -44,9 +44,12 @@ ...@@ -44,9 +44,12 @@
#include <map> #include <map>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <string.h>
using namespace OpenMM; using namespace OpenMM;
using namespace std; using namespace std;
const static char CHECKPOINT_MAGIC_BYTES[] = "OpenMM Binary Checkpoint\n";
ContextImpl::ContextImpl(Context& owner, const System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties) : ContextImpl::ContextImpl(Context& owner, const System& system, Integrator& integrator, Platform* platform, const map<string, string>& properties) :
owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), integratorIsDeleted(false), owner(owner), system(system), integrator(integrator), hasInitializedForces(false), hasSetPositions(false), integratorIsDeleted(false),
...@@ -398,6 +401,7 @@ static string readString(istream& stream) { ...@@ -398,6 +401,7 @@ static string readString(istream& stream) {
} }
void ContextImpl::createCheckpoint(ostream& stream) { void ContextImpl::createCheckpoint(ostream& stream) {
stream.write(CHECKPOINT_MAGIC_BYTES, sizeof(CHECKPOINT_MAGIC_BYTES)/sizeof(CHECKPOINT_MAGIC_BYTES[0]));
writeString(stream, getPlatform().getName()); writeString(stream, getPlatform().getName());
int numParticles = getSystem().getNumParticles(); int numParticles = getSystem().getNumParticles();
stream.write((char*) &numParticles, sizeof(int)); stream.write((char*) &numParticles, sizeof(int));
...@@ -412,6 +416,12 @@ void ContextImpl::createCheckpoint(ostream& stream) { ...@@ -412,6 +416,12 @@ void ContextImpl::createCheckpoint(ostream& stream) {
} }
void ContextImpl::loadCheckpoint(istream& stream) { void ContextImpl::loadCheckpoint(istream& stream) {
static const int magiclength = sizeof(CHECKPOINT_MAGIC_BYTES)/sizeof(CHECKPOINT_MAGIC_BYTES[0]);
char magicbytes[magiclength];
stream.read(magicbytes, magiclength);
if (memcmp(magicbytes, CHECKPOINT_MAGIC_BYTES, magiclength) != 0)
throw OpenMMException("loadCheckpoint: Checkpoint header was not correct");
string platformName = readString(stream); string platformName = readString(stream);
if (platformName != getPlatform().getName()) if (platformName != getPlatform().getName())
throw OpenMMException("loadCheckpoint: Checkpoint was created with a different Platform: "+platformName); throw OpenMMException("loadCheckpoint: Checkpoint was created with a different Platform: "+platformName);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment