void ThreadedOptFossilCollManager::makeInitialCheckpoint() {
	vector<State *> *states = new vector<State*> (
			mySimManager->getNumberOfSimulationObjects(), NULL);
	checkpointedStates.insert(
			pair<int, vector<State*> *> (firstCheckpointTime, states));

	unsigned int simMgrID = mySimManager->getSimulationManagerID();
	for (int i = 0; i < mySimManager->getNumberOfSimulationObjects(); i++) {
		SimulationObject *object = mySimManager->getObjectHandle(
				ObjectID(i, simMgrID));

		// Save the state of the object at the checkpoint time.
		State *newState = object->allocateState();
		newState->copyState(object->getState());
		(*states)[i] = newState;
		lastCheckpointTime[i] = firstCheckpointTime;

		stringstream fname;
		fname << ckptFilePath << "LP" << simMgrID << "." << firstCheckpointTime
				<< "." << i;

		ofstream ckptFile(fname.str().c_str(), ofstream::binary);
		if (!ckptFile.is_open()) {
			cerr << simMgrID << " - Could not open file: " << fname.str()
					<< ", aborting simulation." << endl;
			abort();
		}
		//cout << mySimManager->getOutputManagerNew();
		mySimManager->getOutputManagerNew()->saveOutputCheckpoint(&ckptFile,
				ObjectID(i, simMgrID), lastCheckpointTime[i], 1); // ThreadId hard-coded as 1 as it is the manager

		ckptFile.close();
	}
}
void ThreadedOptFossilCollManager::checkpoint(const VTime &checkTime,
		const ObjectID &objId, const unsigned int &threadId) {
	int time = checkTime.getApproximateIntTime();
	int id = objId.getSimulationObjectID();

	// If the time is less than the last checkpoint time, then save at the last
	// checkpoint time again.
	updateCheckpointTime(id, time);

	while (time >= nextCheckpointTime[id]) {
		utils::debug << mySimManager->getSimulationManagerID()
				<< " - Checkpointing object " << id << " at " << time << endl;
		int highestNextCheckpointTime = nextCheckpointTime[0];
		for (int iter = 1; iter < mySimManager->getNumberOfSimulationObjects(); iter++) {
			if (nextCheckpointTime[iter] > highestNextCheckpointTime)
				highestNextCheckpointTime = nextCheckpointTime[iter];
		}

		vector<State *> *states;
		if (nextCheckpointTime[id] == highestNextCheckpointTime) {
			// No states have been check pointed for this time yet.
			// Thus we create a state vector for all the objects
			utils::debug << "Creating new states to be saved at time "
					<< nextCheckpointTime[id] << endl;
			states = new vector<State*> (
					mySimManager->getNumberOfSimulationObjects(), NULL);
			checkpointedStates.insert(
					pair<int, vector<State*> *> (nextCheckpointTime[id], states));
		} else {
			// States have been saved for other objects but not this object at this time
			// or a roll back in this object is causing this to happen.
			utils::debug
					<< "Adding the current state of the object to checkpointedStates "
					<< nextCheckpointTime[id] << endl;

			map<int, vector<State*>*>::iterator it = checkpointedStates.find(
					nextCheckpointTime[id]);
			states = it->second;
		}

		SimulationObject *object = mySimManager->getObjectHandle(objId);

		// Save the state of the object at the checkpoint time.
		State *newState = object->allocateState();
		newState->copyState(object->getState());
		(*states)[id] = newState;

		lastCheckpointTime[id] = nextCheckpointTime[id];
		nextCheckpointTime[id] += checkpointPeriod;

		stringstream filename;
		filename << ckptFilePath << "LP"
				<< mySimManager->getSimulationManagerID() << "."
				<< lastCheckpointTime[id] << "." << id;

		ofstream ckFile(filename.str().c_str(), ofstream::binary);
		if (!ckFile.is_open()) {
			cerr << mySimManager->getSimulationManagerID()
					<< " - Could not open file: " << filename.str()
					<< ", aborting simulation." << endl;
			abort();
		}

		mySimManager->getOutputManagerNew()->saveOutputCheckpoint(&ckFile,
				objId, lastCheckpointTime[id], threadId);
		ckFile.close();
	}
}